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