item.c revision 1.8 1 1.8 blymn /* $NetBSD: item.c,v 1.8 2002/07/29 13:03:51 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.7 wiz * derived from this software without 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.8 blymn }
144 1.8 blymn
145 1.8 blymn /*
146 1.8 blymn * Returns the number of items that are selected.
147 1.8 blymn * The index numbers of the items are placed in the dynamically allocated
148 1.8 blymn * int array *sel.
149 1.8 blymn */
150 1.8 blymn int
151 1.8 blymn item_selected(MENU *menu, int **sel)
152 1.8 blymn {
153 1.8 blymn int i, j;
154 1.8 blymn
155 1.8 blymn if (menu == NULL)
156 1.8 blymn return E_BAD_ARGUMENT;
157 1.8 blymn
158 1.8 blymn /* count selected */
159 1.8 blymn for (i = 0, j = 0; i < menu->item_count; i++)
160 1.8 blymn if (menu->items[i]->selected)
161 1.8 blymn j++;
162 1.8 blymn
163 1.8 blymn if (j == 0) {
164 1.8 blymn *sel = NULL;
165 1.8 blymn return 0;
166 1.8 blymn }
167 1.8 blymn
168 1.8 blymn if ( (*sel = malloc(sizeof(int) * j)) == NULL)
169 1.8 blymn return E_SYSTEM_ERROR;
170 1.8 blymn
171 1.8 blymn for (i = 0, j = 0; i < menu->item_count; i++)
172 1.8 blymn if (menu->items[i]->selected)
173 1.8 blymn (*sel)[j++] = i;
174 1.8 blymn
175 1.8 blymn return j;
176 1.1 blymn }
177 1.1 blymn
178 1.1 blymn /*
179 1.1 blymn * Set the item options. We keep a global copy of the current item options
180 1.1 blymn * as subsequent new_item calls will use the updated options as their
181 1.1 blymn * defaults.
182 1.1 blymn */
183 1.1 blymn int
184 1.1 blymn set_item_opts(item, opts)
185 1.1 blymn ITEM *item;
186 1.1 blymn OPTIONS opts;
187 1.1 blymn {
188 1.1 blymn /* selectable seems to be the only allowable item opt! */
189 1.1 blymn if (opts != O_SELECTABLE)
190 1.1 blymn return E_SYSTEM_ERROR;
191 1.1 blymn
192 1.1 blymn if (item == NULL)
193 1.1 blymn _menui_default_item.opts = opts;
194 1.1 blymn else
195 1.1 blymn item->opts = opts;
196 1.1 blymn return E_OK;
197 1.1 blymn }
198 1.1 blymn
199 1.1 blymn /*
200 1.1 blymn * Set item options on.
201 1.1 blymn */
202 1.1 blymn int
203 1.6 blymn item_opts_on(ITEM *item, OPTIONS opts)
204 1.1 blymn {
205 1.1 blymn if (opts != O_SELECTABLE)
206 1.1 blymn return E_SYSTEM_ERROR;
207 1.1 blymn
208 1.1 blymn if (item == NULL)
209 1.1 blymn _menui_default_item.opts |= opts;
210 1.1 blymn else
211 1.1 blymn item->opts |= opts;
212 1.1 blymn return E_OK;
213 1.1 blymn }
214 1.1 blymn
215 1.1 blymn /*
216 1.1 blymn * Turn off the named options.
217 1.1 blymn */
218 1.1 blymn int
219 1.6 blymn item_opts_off(ITEM *item, OPTIONS opts)
220 1.1 blymn {
221 1.1 blymn if (opts != O_SELECTABLE)
222 1.1 blymn return E_SYSTEM_ERROR;
223 1.1 blymn
224 1.1 blymn if (item == NULL)
225 1.1 blymn _menui_default_item.opts &= ~(opts);
226 1.1 blymn else
227 1.1 blymn item->opts &= ~(opts);
228 1.1 blymn return E_OK;
229 1.1 blymn }
230 1.1 blymn
231 1.1 blymn /*
232 1.1 blymn * Return the current options set in item.
233 1.1 blymn */
234 1.1 blymn OPTIONS
235 1.6 blymn item_opts(ITEM *item)
236 1.1 blymn {
237 1.1 blymn if (item == NULL)
238 1.1 blymn return _menui_default_item.opts;
239 1.1 blymn else
240 1.1 blymn return item->opts;
241 1.1 blymn }
242 1.1 blymn
243 1.1 blymn /*
244 1.1 blymn * Set the selected flag of the item iff the menu options allow it.
245 1.1 blymn */
246 1.1 blymn int
247 1.6 blymn set_item_value(ITEM *param_item, int flag)
248 1.1 blymn {
249 1.1 blymn ITEM *item = (param_item != NULL) ? param_item : &_menui_default_item;
250 1.1 blymn
251 1.1 blymn /* not bound to a menu */
252 1.1 blymn if (item->parent == NULL)
253 1.1 blymn return E_NOT_CONNECTED;
254 1.1 blymn
255 1.1 blymn /* menu options do not allow multi-selection */
256 1.1 blymn if ((item->parent->opts & O_ONEVALUE) == O_ONEVALUE)
257 1.1 blymn return E_REQUEST_DENIED;
258 1.1 blymn
259 1.1 blymn item->selected = flag;
260 1.1 blymn return E_OK;
261 1.1 blymn }
262 1.1 blymn
263 1.1 blymn /*
264 1.1 blymn * Return the item value of the item.
265 1.1 blymn */
266 1.1 blymn int
267 1.6 blymn item_value(ITEM *item)
268 1.1 blymn {
269 1.1 blymn if (item == NULL)
270 1.1 blymn return _menui_default_item.selected;
271 1.1 blymn else
272 1.1 blymn return item->selected;
273 1.1 blymn }
274 1.1 blymn
275 1.1 blymn /*
276 1.1 blymn * Allocate a new item and return the pointer to the newly allocated
277 1.1 blymn * structure.
278 1.1 blymn */
279 1.1 blymn ITEM *
280 1.6 blymn new_item(char *name, char *description)
281 1.1 blymn {
282 1.1 blymn ITEM *new_one;
283 1.1 blymn
284 1.1 blymn /* allocate a new item structure for ourselves */
285 1.1 blymn if ((new_one = (ITEM *)malloc(sizeof(ITEM))) == NULL)
286 1.1 blymn return NULL;
287 1.1 blymn
288 1.1 blymn /* copy in the defaults for the item */
289 1.3 kleink (void)memcpy(new_one, &_menui_default_item, sizeof(ITEM));
290 1.1 blymn
291 1.1 blymn /* fill in the name structure - first the length and then
292 1.1 blymn allocate room for the string & copy that. */
293 1.1 blymn new_one->name.length = strlen(name);
294 1.1 blymn if ((new_one->name.string = (char *)
295 1.1 blymn malloc(sizeof(char) * new_one->name.length + 1)) == NULL) {
296 1.1 blymn /* uh oh malloc failed - clean up & exit */
297 1.1 blymn free(new_one);
298 1.1 blymn return NULL;
299 1.1 blymn }
300 1.1 blymn
301 1.1 blymn strcpy(new_one->name.string, name);
302 1.1 blymn
303 1.1 blymn /* fill in the description structure, stash the length then
304 1.1 blymn allocate room for description string and copy it in */
305 1.1 blymn new_one->description.length = strlen(description);
306 1.1 blymn if ((new_one->description.string = (char *)
307 1.1 blymn malloc(sizeof(char) * new_one->description.length + 1)) == NULL) {
308 1.1 blymn /* malloc has failed - free up allocated memory and return */
309 1.1 blymn free(new_one->name.string);
310 1.1 blymn free(new_one);
311 1.1 blymn return NULL;
312 1.1 blymn }
313 1.1 blymn
314 1.1 blymn strcpy(new_one->description.string, description);
315 1.1 blymn
316 1.1 blymn return new_one;
317 1.1 blymn }
318 1.1 blymn
319 1.1 blymn /*
320 1.1 blymn * Free the allocated storage associated with item.
321 1.1 blymn */
322 1.1 blymn int
323 1.6 blymn free_item(ITEM *item)
324 1.1 blymn {
325 1.1 blymn if (item == NULL)
326 1.1 blymn return E_BAD_ARGUMENT;
327 1.1 blymn
328 1.1 blymn /* check for connection to menu */
329 1.1 blymn if (item->parent != NULL)
330 1.1 blymn return E_CONNECTED;
331 1.1 blymn
332 1.1 blymn /* no connections, so free storage starting with the strings */
333 1.1 blymn free(item->name.string);
334 1.1 blymn free(item->description.string);
335 1.1 blymn free(item);
336 1.1 blymn return E_OK;
337 1.1 blymn }
338 1.1 blymn
339 1.1 blymn /*
340 1.1 blymn * Set the menu's current item to the one given.
341 1.1 blymn */
342 1.1 blymn int
343 1.6 blymn set_current_item(MENU *param_menu, ITEM *item)
344 1.1 blymn {
345 1.1 blymn MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
346 1.1 blymn int i = 0;
347 1.1 blymn
348 1.1 blymn /* check if we have been called from an init type function */
349 1.1 blymn if (menu->in_init == 1)
350 1.1 blymn return E_BAD_STATE;
351 1.1 blymn
352 1.1 blymn /* check we have items in the menu */
353 1.1 blymn if (menu->items == NULL)
354 1.1 blymn return E_NOT_CONNECTED;
355 1.1 blymn
356 1.1 blymn if ((i = item_index(item)) < 0)
357 1.1 blymn /* item must not be a part of this menu */
358 1.1 blymn return E_BAD_ARGUMENT;
359 1.1 blymn
360 1.1 blymn menu->cur_item = i;
361 1.1 blymn return E_OK;
362 1.1 blymn }
363 1.1 blymn
364 1.1 blymn /*
365 1.1 blymn * Return a pointer to the current item for the menu
366 1.1 blymn */
367 1.1 blymn ITEM *
368 1.6 blymn current_item(MENU *menu)
369 1.1 blymn {
370 1.1 blymn if (menu == NULL)
371 1.1 blymn return NULL;
372 1.1 blymn
373 1.1 blymn if (menu->items == NULL)
374 1.1 blymn return NULL;
375 1.1 blymn
376 1.1 blymn return menu->items[menu->cur_item];
377 1.1 blymn }
378 1.1 blymn
379 1.1 blymn /*
380 1.1 blymn * Return the index into the item array that matches item.
381 1.1 blymn */
382 1.1 blymn int
383 1.6 blymn item_index(ITEM *item)
384 1.1 blymn {
385 1.1 blymn if (item == NULL)
386 1.1 blymn return _menui_default_item.index;
387 1.1 blymn else
388 1.1 blymn return item->index;
389 1.1 blymn }
390