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