item.c revision 1.9 1 1.9 lukem /* $NetBSD: item.c,v 1.9 2003/03/09 01:08:48 lukem 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.9 lukem
29 1.9 lukem #include <sys/cdefs.h>
30 1.9 lukem __RCSID("$NetBSD: item.c,v 1.9 2003/03/09 01:08:48 lukem Exp $");
31 1.1 blymn
32 1.1 blymn #include <menu.h>
33 1.3 kleink #include <stdlib.h>
34 1.3 kleink #include <string.h>
35 1.1 blymn
36 1.1 blymn /* the following is defined in menu.c - it is the default menu struct */
37 1.1 blymn extern MENU _menui_default_menu;
38 1.1 blymn
39 1.1 blymn /* keep default item options for setting in new_item */
40 1.1 blymn ITEM _menui_default_item = {
41 1.1 blymn {NULL, 0}, /* item name struct */
42 1.1 blymn {NULL, 0}, /* item description struct */
43 1.1 blymn NULL, /* user pointer */
44 1.1 blymn 0, /* is item visible? */
45 1.1 blymn 0, /* is item selected? */
46 1.1 blymn 0, /* row item is on */
47 1.1 blymn 0, /* column item is on */
48 1.1 blymn O_SELECTABLE, /* item options */
49 1.1 blymn NULL, /* parent menu item is bound to */
50 1.1 blymn -1, /* index number if item attached to a menu */
51 1.1 blymn NULL, /* left neighbour */
52 1.1 blymn NULL, /* right neighbour */
53 1.1 blymn NULL, /* up neighbour */
54 1.1 blymn NULL /* down neighbour */
55 1.1 blymn };
56 1.1 blymn
57 1.1 blymn /*
58 1.1 blymn * Return the item visibility flag
59 1.1 blymn */
60 1.1 blymn int
61 1.6 blymn item_visible(ITEM *item)
62 1.1 blymn {
63 1.1 blymn if (item == NULL)
64 1.1 blymn return E_BAD_ARGUMENT;
65 1.1 blymn if (item->parent == NULL)
66 1.1 blymn return E_NOT_CONNECTED;
67 1.1 blymn
68 1.1 blymn return item->visible;
69 1.1 blymn }
70 1.1 blymn
71 1.1 blymn /*
72 1.1 blymn * Return the pointer to the item name
73 1.1 blymn */
74 1.1 blymn char *
75 1.6 blymn item_name(ITEM *item)
76 1.1 blymn {
77 1.1 blymn if (item == NULL)
78 1.1 blymn return NULL;
79 1.1 blymn
80 1.1 blymn return item->name.string;
81 1.1 blymn }
82 1.1 blymn
83 1.1 blymn /*
84 1.1 blymn * Return the pointer to the item description
85 1.1 blymn */
86 1.1 blymn char *
87 1.6 blymn item_description(ITEM *item)
88 1.1 blymn {
89 1.1 blymn if (item == NULL)
90 1.1 blymn return NULL;
91 1.1 blymn
92 1.1 blymn return item->description.string;
93 1.1 blymn }
94 1.1 blymn
95 1.1 blymn /*
96 1.1 blymn * Set the application defined function called when the menu is posted or
97 1.1 blymn * just after the current item changes.
98 1.1 blymn */
99 1.1 blymn int
100 1.6 blymn set_item_init(MENU *menu, Menu_Hook func)
101 1.1 blymn {
102 1.1 blymn if (menu == NULL)
103 1.1 blymn _menui_default_menu.item_init = func;
104 1.1 blymn else
105 1.1 blymn menu->item_init = func;
106 1.1 blymn return E_OK;
107 1.1 blymn }
108 1.1 blymn
109 1.1 blymn
110 1.1 blymn /*
111 1.1 blymn * Return a pointer to the item initialisation routine.
112 1.1 blymn */
113 1.4 blymn Menu_Hook
114 1.6 blymn item_init(MENU *menu)
115 1.1 blymn {
116 1.1 blymn if (menu == NULL)
117 1.1 blymn return _menui_default_menu.item_init;
118 1.1 blymn else
119 1.1 blymn return menu->item_init;
120 1.1 blymn }
121 1.1 blymn
122 1.1 blymn /*
123 1.1 blymn * Set the user defined function to be called when menu is unposted or just
124 1.1 blymn * before the current item changes.
125 1.1 blymn */
126 1.1 blymn int
127 1.6 blymn set_item_term(MENU *menu, Menu_Hook func)
128 1.1 blymn {
129 1.1 blymn if (menu == NULL)
130 1.1 blymn _menui_default_menu.item_term = func;
131 1.1 blymn else
132 1.1 blymn menu->item_term = func;
133 1.1 blymn return E_OK;
134 1.1 blymn }
135 1.1 blymn
136 1.1 blymn /*
137 1.1 blymn * Return a pointer to the termination function
138 1.1 blymn */
139 1.4 blymn Menu_Hook
140 1.6 blymn item_term(MENU *menu)
141 1.1 blymn {
142 1.1 blymn if (menu == NULL)
143 1.1 blymn return _menui_default_menu.item_term;
144 1.1 blymn else
145 1.1 blymn return menu->item_term;
146 1.8 blymn }
147 1.8 blymn
148 1.8 blymn /*
149 1.8 blymn * Returns the number of items that are selected.
150 1.8 blymn * The index numbers of the items are placed in the dynamically allocated
151 1.8 blymn * int array *sel.
152 1.8 blymn */
153 1.8 blymn int
154 1.8 blymn item_selected(MENU *menu, int **sel)
155 1.8 blymn {
156 1.8 blymn int i, j;
157 1.8 blymn
158 1.8 blymn if (menu == NULL)
159 1.8 blymn return E_BAD_ARGUMENT;
160 1.8 blymn
161 1.8 blymn /* count selected */
162 1.8 blymn for (i = 0, j = 0; i < menu->item_count; i++)
163 1.8 blymn if (menu->items[i]->selected)
164 1.8 blymn j++;
165 1.8 blymn
166 1.8 blymn if (j == 0) {
167 1.8 blymn *sel = NULL;
168 1.8 blymn return 0;
169 1.8 blymn }
170 1.8 blymn
171 1.8 blymn if ( (*sel = malloc(sizeof(int) * j)) == NULL)
172 1.8 blymn return E_SYSTEM_ERROR;
173 1.8 blymn
174 1.8 blymn for (i = 0, j = 0; i < menu->item_count; i++)
175 1.8 blymn if (menu->items[i]->selected)
176 1.8 blymn (*sel)[j++] = i;
177 1.8 blymn
178 1.8 blymn return j;
179 1.1 blymn }
180 1.1 blymn
181 1.1 blymn /*
182 1.1 blymn * Set the item options. We keep a global copy of the current item options
183 1.1 blymn * as subsequent new_item calls will use the updated options as their
184 1.1 blymn * defaults.
185 1.1 blymn */
186 1.1 blymn int
187 1.1 blymn set_item_opts(item, opts)
188 1.1 blymn ITEM *item;
189 1.1 blymn OPTIONS opts;
190 1.1 blymn {
191 1.1 blymn /* selectable seems to be the only allowable item opt! */
192 1.1 blymn if (opts != O_SELECTABLE)
193 1.1 blymn return E_SYSTEM_ERROR;
194 1.1 blymn
195 1.1 blymn if (item == NULL)
196 1.1 blymn _menui_default_item.opts = opts;
197 1.1 blymn else
198 1.1 blymn item->opts = opts;
199 1.1 blymn return E_OK;
200 1.1 blymn }
201 1.1 blymn
202 1.1 blymn /*
203 1.1 blymn * Set item options on.
204 1.1 blymn */
205 1.1 blymn int
206 1.6 blymn item_opts_on(ITEM *item, OPTIONS opts)
207 1.1 blymn {
208 1.1 blymn if (opts != O_SELECTABLE)
209 1.1 blymn return E_SYSTEM_ERROR;
210 1.1 blymn
211 1.1 blymn if (item == NULL)
212 1.1 blymn _menui_default_item.opts |= opts;
213 1.1 blymn else
214 1.1 blymn item->opts |= opts;
215 1.1 blymn return E_OK;
216 1.1 blymn }
217 1.1 blymn
218 1.1 blymn /*
219 1.1 blymn * Turn off the named options.
220 1.1 blymn */
221 1.1 blymn int
222 1.6 blymn item_opts_off(ITEM *item, OPTIONS opts)
223 1.1 blymn {
224 1.1 blymn if (opts != O_SELECTABLE)
225 1.1 blymn return E_SYSTEM_ERROR;
226 1.1 blymn
227 1.1 blymn if (item == NULL)
228 1.1 blymn _menui_default_item.opts &= ~(opts);
229 1.1 blymn else
230 1.1 blymn item->opts &= ~(opts);
231 1.1 blymn return E_OK;
232 1.1 blymn }
233 1.1 blymn
234 1.1 blymn /*
235 1.1 blymn * Return the current options set in item.
236 1.1 blymn */
237 1.1 blymn OPTIONS
238 1.6 blymn item_opts(ITEM *item)
239 1.1 blymn {
240 1.1 blymn if (item == NULL)
241 1.1 blymn return _menui_default_item.opts;
242 1.1 blymn else
243 1.1 blymn return item->opts;
244 1.1 blymn }
245 1.1 blymn
246 1.1 blymn /*
247 1.1 blymn * Set the selected flag of the item iff the menu options allow it.
248 1.1 blymn */
249 1.1 blymn int
250 1.6 blymn set_item_value(ITEM *param_item, int flag)
251 1.1 blymn {
252 1.1 blymn ITEM *item = (param_item != NULL) ? param_item : &_menui_default_item;
253 1.1 blymn
254 1.1 blymn /* not bound to a menu */
255 1.1 blymn if (item->parent == NULL)
256 1.1 blymn return E_NOT_CONNECTED;
257 1.1 blymn
258 1.1 blymn /* menu options do not allow multi-selection */
259 1.1 blymn if ((item->parent->opts & O_ONEVALUE) == O_ONEVALUE)
260 1.1 blymn return E_REQUEST_DENIED;
261 1.1 blymn
262 1.1 blymn item->selected = flag;
263 1.1 blymn return E_OK;
264 1.1 blymn }
265 1.1 blymn
266 1.1 blymn /*
267 1.1 blymn * Return the item value of the item.
268 1.1 blymn */
269 1.1 blymn int
270 1.6 blymn item_value(ITEM *item)
271 1.1 blymn {
272 1.1 blymn if (item == NULL)
273 1.1 blymn return _menui_default_item.selected;
274 1.1 blymn else
275 1.1 blymn return item->selected;
276 1.1 blymn }
277 1.1 blymn
278 1.1 blymn /*
279 1.1 blymn * Allocate a new item and return the pointer to the newly allocated
280 1.1 blymn * structure.
281 1.1 blymn */
282 1.1 blymn ITEM *
283 1.6 blymn new_item(char *name, char *description)
284 1.1 blymn {
285 1.1 blymn ITEM *new_one;
286 1.1 blymn
287 1.1 blymn /* allocate a new item structure for ourselves */
288 1.1 blymn if ((new_one = (ITEM *)malloc(sizeof(ITEM))) == NULL)
289 1.1 blymn return NULL;
290 1.1 blymn
291 1.1 blymn /* copy in the defaults for the item */
292 1.3 kleink (void)memcpy(new_one, &_menui_default_item, sizeof(ITEM));
293 1.1 blymn
294 1.1 blymn /* fill in the name structure - first the length and then
295 1.1 blymn allocate room for the string & copy that. */
296 1.1 blymn new_one->name.length = strlen(name);
297 1.1 blymn if ((new_one->name.string = (char *)
298 1.1 blymn malloc(sizeof(char) * new_one->name.length + 1)) == NULL) {
299 1.1 blymn /* uh oh malloc failed - clean up & exit */
300 1.1 blymn free(new_one);
301 1.1 blymn return NULL;
302 1.1 blymn }
303 1.1 blymn
304 1.1 blymn strcpy(new_one->name.string, name);
305 1.1 blymn
306 1.1 blymn /* fill in the description structure, stash the length then
307 1.1 blymn allocate room for description string and copy it in */
308 1.1 blymn new_one->description.length = strlen(description);
309 1.1 blymn if ((new_one->description.string = (char *)
310 1.1 blymn malloc(sizeof(char) * new_one->description.length + 1)) == NULL) {
311 1.1 blymn /* malloc has failed - free up allocated memory and return */
312 1.1 blymn free(new_one->name.string);
313 1.1 blymn free(new_one);
314 1.1 blymn return NULL;
315 1.1 blymn }
316 1.1 blymn
317 1.1 blymn strcpy(new_one->description.string, description);
318 1.1 blymn
319 1.1 blymn return new_one;
320 1.1 blymn }
321 1.1 blymn
322 1.1 blymn /*
323 1.1 blymn * Free the allocated storage associated with item.
324 1.1 blymn */
325 1.1 blymn int
326 1.6 blymn free_item(ITEM *item)
327 1.1 blymn {
328 1.1 blymn if (item == NULL)
329 1.1 blymn return E_BAD_ARGUMENT;
330 1.1 blymn
331 1.1 blymn /* check for connection to menu */
332 1.1 blymn if (item->parent != NULL)
333 1.1 blymn return E_CONNECTED;
334 1.1 blymn
335 1.1 blymn /* no connections, so free storage starting with the strings */
336 1.1 blymn free(item->name.string);
337 1.1 blymn free(item->description.string);
338 1.1 blymn free(item);
339 1.1 blymn return E_OK;
340 1.1 blymn }
341 1.1 blymn
342 1.1 blymn /*
343 1.1 blymn * Set the menu's current item to the one given.
344 1.1 blymn */
345 1.1 blymn int
346 1.6 blymn set_current_item(MENU *param_menu, ITEM *item)
347 1.1 blymn {
348 1.1 blymn MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
349 1.1 blymn int i = 0;
350 1.1 blymn
351 1.1 blymn /* check if we have been called from an init type function */
352 1.1 blymn if (menu->in_init == 1)
353 1.1 blymn return E_BAD_STATE;
354 1.1 blymn
355 1.1 blymn /* check we have items in the menu */
356 1.1 blymn if (menu->items == NULL)
357 1.1 blymn return E_NOT_CONNECTED;
358 1.1 blymn
359 1.1 blymn if ((i = item_index(item)) < 0)
360 1.1 blymn /* item must not be a part of this menu */
361 1.1 blymn return E_BAD_ARGUMENT;
362 1.1 blymn
363 1.1 blymn menu->cur_item = i;
364 1.1 blymn return E_OK;
365 1.1 blymn }
366 1.1 blymn
367 1.1 blymn /*
368 1.1 blymn * Return a pointer to the current item for the menu
369 1.1 blymn */
370 1.1 blymn ITEM *
371 1.6 blymn current_item(MENU *menu)
372 1.1 blymn {
373 1.1 blymn if (menu == NULL)
374 1.1 blymn return NULL;
375 1.1 blymn
376 1.1 blymn if (menu->items == NULL)
377 1.1 blymn return NULL;
378 1.1 blymn
379 1.1 blymn return menu->items[menu->cur_item];
380 1.1 blymn }
381 1.1 blymn
382 1.1 blymn /*
383 1.1 blymn * Return the index into the item array that matches item.
384 1.1 blymn */
385 1.1 blymn int
386 1.6 blymn item_index(ITEM *item)
387 1.1 blymn {
388 1.1 blymn if (item == NULL)
389 1.1 blymn return _menui_default_item.index;
390 1.1 blymn else
391 1.1 blymn return item->index;
392 1.1 blymn }
393