item.c revision 1.6 1 1.6 blymn /* $NetBSD: item.c,v 1.6 2000/04/20 12:17:57 blymn Exp $ */
2 1.1 blymn
3 1.1 blymn /*-
4 1.4 blymn * Copyright (c) 1998-1999 Brett Lymn (blymn (at) baea.com.au, brett_lymn (at) yahoo.com.au)
5 1.1 blymn * All rights reserved.
6 1.1 blymn *
7 1.1 blymn * Redistribution and use in source and binary forms, with or without
8 1.1 blymn * modification, are permitted provided that the following conditions
9 1.1 blymn * are met:
10 1.1 blymn * 1. Redistributions of source code must retain the above copyright
11 1.1 blymn * notice, this list of conditions and the following disclaimer.
12 1.1 blymn * 2. The name of the author may not be used to endorse or promote products
13 1.1 blymn * derived from this software withough specific prior written permission
14 1.1 blymn *
15 1.1 blymn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 blymn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 blymn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 blymn * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 blymn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 blymn * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 blymn * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 blymn * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 blymn * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 blymn * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 blymn *
26 1.1 blymn *
27 1.1 blymn */
28 1.1 blymn
29 1.1 blymn #include <menu.h>
30 1.3 kleink #include <stdlib.h>
31 1.3 kleink #include <string.h>
32 1.1 blymn
33 1.1 blymn /* the following is defined in menu.c - it is the default menu struct */
34 1.1 blymn extern MENU _menui_default_menu;
35 1.1 blymn
36 1.1 blymn /* keep default item options for setting in new_item */
37 1.1 blymn ITEM _menui_default_item = {
38 1.1 blymn {NULL, 0}, /* item name struct */
39 1.1 blymn {NULL, 0}, /* item description struct */
40 1.1 blymn NULL, /* user pointer */
41 1.1 blymn 0, /* is item visible? */
42 1.1 blymn 0, /* is item selected? */
43 1.1 blymn 0, /* row item is on */
44 1.1 blymn 0, /* column item is on */
45 1.1 blymn O_SELECTABLE, /* item options */
46 1.1 blymn NULL, /* parent menu item is bound to */
47 1.1 blymn -1, /* index number if item attached to a menu */
48 1.1 blymn NULL, /* left neighbour */
49 1.1 blymn NULL, /* right neighbour */
50 1.1 blymn NULL, /* up neighbour */
51 1.1 blymn NULL /* down neighbour */
52 1.1 blymn };
53 1.1 blymn
54 1.1 blymn /*
55 1.1 blymn * Return the item visibility flag
56 1.1 blymn */
57 1.1 blymn int
58 1.6 blymn item_visible(ITEM *item)
59 1.1 blymn {
60 1.1 blymn if (item == NULL)
61 1.1 blymn return E_BAD_ARGUMENT;
62 1.1 blymn if (item->parent == NULL)
63 1.1 blymn return E_NOT_CONNECTED;
64 1.1 blymn
65 1.1 blymn return item->visible;
66 1.1 blymn }
67 1.1 blymn
68 1.1 blymn /*
69 1.1 blymn * Return the pointer to the item name
70 1.1 blymn */
71 1.1 blymn char *
72 1.6 blymn item_name(ITEM *item)
73 1.1 blymn {
74 1.1 blymn if (item == NULL)
75 1.1 blymn return NULL;
76 1.1 blymn
77 1.1 blymn return item->name.string;
78 1.1 blymn }
79 1.1 blymn
80 1.1 blymn /*
81 1.1 blymn * Return the pointer to the item description
82 1.1 blymn */
83 1.1 blymn char *
84 1.6 blymn item_description(ITEM *item)
85 1.1 blymn {
86 1.1 blymn if (item == NULL)
87 1.1 blymn return NULL;
88 1.1 blymn
89 1.1 blymn return item->description.string;
90 1.1 blymn }
91 1.1 blymn
92 1.1 blymn /*
93 1.1 blymn * Set the application defined function called when the menu is posted or
94 1.1 blymn * just after the current item changes.
95 1.1 blymn */
96 1.1 blymn int
97 1.6 blymn set_item_init(MENU *menu, Menu_Hook func)
98 1.1 blymn {
99 1.1 blymn if (menu == NULL)
100 1.1 blymn _menui_default_menu.item_init = func;
101 1.1 blymn else
102 1.1 blymn menu->item_init = func;
103 1.1 blymn return E_OK;
104 1.1 blymn }
105 1.1 blymn
106 1.1 blymn
107 1.1 blymn /*
108 1.1 blymn * Return a pointer to the item initialisation routine.
109 1.1 blymn */
110 1.4 blymn Menu_Hook
111 1.6 blymn item_init(MENU *menu)
112 1.1 blymn {
113 1.1 blymn if (menu == NULL)
114 1.1 blymn return _menui_default_menu.item_init;
115 1.1 blymn else
116 1.1 blymn return menu->item_init;
117 1.1 blymn }
118 1.1 blymn
119 1.1 blymn /*
120 1.1 blymn * Set the user defined function to be called when menu is unposted or just
121 1.1 blymn * before the current item changes.
122 1.1 blymn */
123 1.1 blymn int
124 1.6 blymn set_item_term(MENU *menu, Menu_Hook func)
125 1.1 blymn {
126 1.1 blymn if (menu == NULL)
127 1.1 blymn _menui_default_menu.item_term = func;
128 1.1 blymn else
129 1.1 blymn menu->item_term = func;
130 1.1 blymn return E_OK;
131 1.1 blymn }
132 1.1 blymn
133 1.1 blymn /*
134 1.1 blymn * Return a pointer to the termination function
135 1.1 blymn */
136 1.4 blymn Menu_Hook
137 1.6 blymn item_term(MENU *menu)
138 1.1 blymn {
139 1.1 blymn if (menu == NULL)
140 1.1 blymn return _menui_default_menu.item_term;
141 1.1 blymn else
142 1.1 blymn return menu->item_term;
143 1.1 blymn }
144 1.1 blymn
145 1.1 blymn /*
146 1.1 blymn * Set the item options. We keep a global copy of the current item options
147 1.1 blymn * as subsequent new_item calls will use the updated options as their
148 1.1 blymn * defaults.
149 1.1 blymn */
150 1.1 blymn int
151 1.1 blymn set_item_opts(item, opts)
152 1.1 blymn ITEM *item;
153 1.1 blymn OPTIONS opts;
154 1.1 blymn {
155 1.1 blymn /* selectable seems to be the only allowable item opt! */
156 1.1 blymn if (opts != O_SELECTABLE)
157 1.1 blymn return E_SYSTEM_ERROR;
158 1.1 blymn
159 1.1 blymn if (item == NULL)
160 1.1 blymn _menui_default_item.opts = opts;
161 1.1 blymn else
162 1.1 blymn item->opts = opts;
163 1.1 blymn return E_OK;
164 1.1 blymn }
165 1.1 blymn
166 1.1 blymn /*
167 1.1 blymn * Set item options on.
168 1.1 blymn */
169 1.1 blymn int
170 1.6 blymn item_opts_on(ITEM *item, OPTIONS opts)
171 1.1 blymn {
172 1.1 blymn if (opts != O_SELECTABLE)
173 1.1 blymn return E_SYSTEM_ERROR;
174 1.1 blymn
175 1.1 blymn if (item == NULL)
176 1.1 blymn _menui_default_item.opts |= opts;
177 1.1 blymn else
178 1.1 blymn item->opts |= opts;
179 1.1 blymn return E_OK;
180 1.1 blymn }
181 1.1 blymn
182 1.1 blymn /*
183 1.1 blymn * Turn off the named options.
184 1.1 blymn */
185 1.1 blymn int
186 1.6 blymn item_opts_off(ITEM *item, OPTIONS opts)
187 1.1 blymn {
188 1.1 blymn if (opts != O_SELECTABLE)
189 1.1 blymn return E_SYSTEM_ERROR;
190 1.1 blymn
191 1.1 blymn if (item == NULL)
192 1.1 blymn _menui_default_item.opts &= ~(opts);
193 1.1 blymn else
194 1.1 blymn item->opts &= ~(opts);
195 1.1 blymn return E_OK;
196 1.1 blymn }
197 1.1 blymn
198 1.1 blymn /*
199 1.1 blymn * Return the current options set in item.
200 1.1 blymn */
201 1.1 blymn OPTIONS
202 1.6 blymn item_opts(ITEM *item)
203 1.1 blymn {
204 1.1 blymn if (item == NULL)
205 1.1 blymn return _menui_default_item.opts;
206 1.1 blymn else
207 1.1 blymn return item->opts;
208 1.1 blymn }
209 1.1 blymn
210 1.1 blymn /*
211 1.1 blymn * Set the selected flag of the item iff the menu options allow it.
212 1.1 blymn */
213 1.1 blymn int
214 1.6 blymn set_item_value(ITEM *param_item, int flag)
215 1.1 blymn {
216 1.1 blymn ITEM *item = (param_item != NULL) ? param_item : &_menui_default_item;
217 1.1 blymn
218 1.1 blymn /* not bound to a menu */
219 1.1 blymn if (item->parent == NULL)
220 1.1 blymn return E_NOT_CONNECTED;
221 1.1 blymn
222 1.1 blymn /* menu options do not allow multi-selection */
223 1.1 blymn if ((item->parent->opts & O_ONEVALUE) == O_ONEVALUE)
224 1.1 blymn return E_REQUEST_DENIED;
225 1.1 blymn
226 1.1 blymn item->selected = flag;
227 1.1 blymn return E_OK;
228 1.1 blymn }
229 1.1 blymn
230 1.1 blymn /*
231 1.1 blymn * Return the item value of the item.
232 1.1 blymn */
233 1.1 blymn int
234 1.6 blymn item_value(ITEM *item)
235 1.1 blymn {
236 1.1 blymn if (item == NULL)
237 1.1 blymn return _menui_default_item.selected;
238 1.1 blymn else
239 1.1 blymn return item->selected;
240 1.1 blymn }
241 1.1 blymn
242 1.1 blymn /*
243 1.1 blymn * Allocate a new item and return the pointer to the newly allocated
244 1.1 blymn * structure.
245 1.1 blymn */
246 1.1 blymn ITEM *
247 1.6 blymn new_item(char *name, char *description)
248 1.1 blymn {
249 1.1 blymn ITEM *new_one;
250 1.1 blymn
251 1.1 blymn /* allocate a new item structure for ourselves */
252 1.1 blymn if ((new_one = (ITEM *)malloc(sizeof(ITEM))) == NULL)
253 1.1 blymn return NULL;
254 1.1 blymn
255 1.1 blymn /* copy in the defaults for the item */
256 1.3 kleink (void)memcpy(new_one, &_menui_default_item, sizeof(ITEM));
257 1.1 blymn
258 1.1 blymn /* fill in the name structure - first the length and then
259 1.1 blymn allocate room for the string & copy that. */
260 1.1 blymn new_one->name.length = strlen(name);
261 1.1 blymn if ((new_one->name.string = (char *)
262 1.1 blymn malloc(sizeof(char) * new_one->name.length + 1)) == NULL) {
263 1.1 blymn /* uh oh malloc failed - clean up & exit */
264 1.1 blymn free(new_one);
265 1.1 blymn return NULL;
266 1.1 blymn }
267 1.1 blymn
268 1.1 blymn strcpy(new_one->name.string, name);
269 1.1 blymn
270 1.1 blymn /* fill in the description structure, stash the length then
271 1.1 blymn allocate room for description string and copy it in */
272 1.1 blymn new_one->description.length = strlen(description);
273 1.1 blymn if ((new_one->description.string = (char *)
274 1.1 blymn malloc(sizeof(char) * new_one->description.length + 1)) == NULL) {
275 1.1 blymn /* malloc has failed - free up allocated memory and return */
276 1.1 blymn free(new_one->name.string);
277 1.1 blymn free(new_one);
278 1.1 blymn return NULL;
279 1.1 blymn }
280 1.1 blymn
281 1.1 blymn strcpy(new_one->description.string, description);
282 1.1 blymn
283 1.1 blymn return new_one;
284 1.1 blymn }
285 1.1 blymn
286 1.1 blymn /*
287 1.1 blymn * Free the allocated storage associated with item.
288 1.1 blymn */
289 1.1 blymn int
290 1.6 blymn free_item(ITEM *item)
291 1.1 blymn {
292 1.1 blymn if (item == NULL)
293 1.1 blymn return E_BAD_ARGUMENT;
294 1.1 blymn
295 1.1 blymn /* check for connection to menu */
296 1.1 blymn if (item->parent != NULL)
297 1.1 blymn return E_CONNECTED;
298 1.1 blymn
299 1.1 blymn /* no connections, so free storage starting with the strings */
300 1.1 blymn free(item->name.string);
301 1.1 blymn free(item->description.string);
302 1.1 blymn free(item);
303 1.1 blymn return E_OK;
304 1.1 blymn }
305 1.1 blymn
306 1.1 blymn /*
307 1.1 blymn * Set the menu's current item to the one given.
308 1.1 blymn */
309 1.1 blymn int
310 1.6 blymn set_current_item(MENU *param_menu, ITEM *item)
311 1.1 blymn {
312 1.1 blymn MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
313 1.1 blymn int i = 0;
314 1.1 blymn
315 1.1 blymn /* check if we have been called from an init type function */
316 1.1 blymn if (menu->in_init == 1)
317 1.1 blymn return E_BAD_STATE;
318 1.1 blymn
319 1.1 blymn /* check we have items in the menu */
320 1.1 blymn if (menu->items == NULL)
321 1.1 blymn return E_NOT_CONNECTED;
322 1.1 blymn
323 1.1 blymn if ((i = item_index(item)) < 0)
324 1.1 blymn /* item must not be a part of this menu */
325 1.1 blymn return E_BAD_ARGUMENT;
326 1.1 blymn
327 1.1 blymn menu->cur_item = i;
328 1.1 blymn return E_OK;
329 1.1 blymn }
330 1.1 blymn
331 1.1 blymn /*
332 1.1 blymn * Return a pointer to the current item for the menu
333 1.1 blymn */
334 1.1 blymn ITEM *
335 1.6 blymn current_item(MENU *menu)
336 1.1 blymn {
337 1.1 blymn if (menu == NULL)
338 1.1 blymn return NULL;
339 1.1 blymn
340 1.1 blymn if (menu->items == NULL)
341 1.1 blymn return NULL;
342 1.1 blymn
343 1.1 blymn return menu->items[menu->cur_item];
344 1.1 blymn }
345 1.1 blymn
346 1.1 blymn /*
347 1.1 blymn * Return the index into the item array that matches item.
348 1.1 blymn */
349 1.1 blymn int
350 1.6 blymn item_index(ITEM *item)
351 1.1 blymn {
352 1.1 blymn if (item == NULL)
353 1.1 blymn return _menui_default_item.index;
354 1.1 blymn else
355 1.1 blymn return item->index;
356 1.1 blymn }
357