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