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