Home | History | Annotate | Line # | Download | only in libmenu
menu.c revision 1.16
      1  1.16   lukem /*	$NetBSD: menu.c,v 1.16 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.10     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.16   lukem 
     29  1.16   lukem #include <sys/cdefs.h>
     30  1.16   lukem __RCSID("$NetBSD: menu.c,v 1.16 2003/03/09 01:08:48 lukem Exp $");
     31   1.1   blymn 
     32   1.3  kleink #include <ctype.h>
     33   1.1   blymn #include <menu.h>
     34   1.3  kleink #include <string.h>
     35   1.1   blymn #include <stdlib.h>
     36   1.1   blymn #include "internals.h"
     37   1.1   blymn 
     38   1.1   blymn MENU _menui_default_menu = {
     39   1.1   blymn 	16,         /* number of item rows that will fit in window */
     40   1.1   blymn         1,          /* number of columns of items that will fit in window */
     41   1.1   blymn 	0,          /* number of rows of items we have */
     42   1.1   blymn 	0,          /* number of columns of items we have */
     43   1.1   blymn         0,          /* current cursor row */
     44   1.1   blymn         0,          /* current cursor column */
     45   1.1   blymn         {NULL, 0},  /* mark string */
     46   1.1   blymn         {NULL, 0},  /* unmark string */
     47   1.1   blymn         O_ONEVALUE, /* menu options */
     48   1.1   blymn         NULL,       /* the pattern buffer */
     49   1.1   blymn 	0,          /* length of pattern buffer */
     50   1.1   blymn 	0,          /* the length of matched buffer */
     51   1.1   blymn         0,          /* is the menu posted? */
     52   1.1   blymn         A_REVERSE, /* menu foreground */
     53   1.1   blymn         A_NORMAL,   /* menu background */
     54   1.1   blymn         A_UNDERLINE,      /* unselectable menu item */
     55   1.1   blymn         ' ',        /* filler between name and description */
     56   1.1   blymn         NULL,       /* user defined pointer */
     57   1.1   blymn 	0,          /* top row of menu */
     58   1.1   blymn 	0,          /* widest item in the menu */
     59   1.1   blymn 	0,          /* the width of a menu column */
     60   1.1   blymn 	0,          /* number of items attached to the menu */
     61   1.1   blymn         NULL,       /* items in the menu */
     62   1.1   blymn         0,          /* current menu item */
     63   1.1   blymn 	0,          /* currently in a hook function */
     64   1.1   blymn         NULL,       /* function called when menu posted */
     65   1.1   blymn         NULL,       /* function called when menu is unposted */
     66   1.1   blymn         NULL,       /* function called when current item changes */
     67   1.1   blymn         NULL,       /* function called when current item changes */
     68   1.1   blymn         NULL,       /* the menu window */
     69   1.1   blymn 	NULL,       /* the menu subwindow */
     70  1.11   blymn 	NULL,       /* the window to write to */
     71   1.1   blymn };
     72   1.1   blymn 
     73   1.1   blymn 
     74   1.1   blymn 
     75   1.1   blymn /*
     76   1.1   blymn  * Set the menu mark character
     77   1.1   blymn  */
     78   1.1   blymn int
     79   1.6   blymn set_menu_mark(MENU *m, char *mark)
     80   1.1   blymn {
     81   1.1   blymn 	MENU *menu = m;
     82   1.1   blymn 
     83   1.1   blymn 	if (m == NULL) menu = &_menui_default_menu;
     84   1.1   blymn 
     85   1.1   blymn           /* if there was an old mark string, free it first */
     86   1.1   blymn         if (menu->mark.string != NULL) free(menu->mark.string);
     87   1.1   blymn 
     88  1.14   blymn         if ((menu->mark.string = (char *) malloc(strlen(mark) + 1)) == NULL)
     89   1.1   blymn                 return E_SYSTEM_ERROR;
     90   1.1   blymn 
     91   1.1   blymn         strcpy(menu->mark.string, mark);
     92   1.1   blymn 	menu->mark.length = strlen(mark);
     93   1.1   blymn 
     94   1.1   blymn 	  /* max item size may have changed - recalculate. */
     95   1.4   blymn 	_menui_max_item_size(menu);
     96   1.1   blymn         return E_OK;
     97   1.1   blymn }
     98   1.1   blymn 
     99   1.1   blymn /*
    100   1.1   blymn  * Return the menu mark string for the menu.
    101   1.1   blymn  */
    102   1.1   blymn char *
    103   1.6   blymn menu_mark(MENU *menu)
    104   1.1   blymn {
    105   1.1   blymn 	if (menu == NULL)
    106   1.1   blymn 		return _menui_default_menu.mark.string;
    107   1.1   blymn 	else
    108   1.1   blymn 		return menu->mark.string;
    109   1.1   blymn }
    110   1.1   blymn 
    111   1.1   blymn /*
    112   1.1   blymn  * Set the menu unmark character
    113   1.1   blymn  */
    114   1.1   blymn int
    115   1.6   blymn set_menu_unmark(MENU *m, char *mark)
    116   1.1   blymn {
    117   1.1   blymn 	MENU *menu = m;
    118   1.1   blymn 
    119   1.1   blymn 	if (m == NULL) menu = &_menui_default_menu;
    120   1.1   blymn 
    121   1.1   blymn           /* if there was an old mark string, free it first */
    122   1.1   blymn         if (menu->unmark.string != NULL) free(menu->unmark.string);
    123   1.1   blymn 
    124  1.14   blymn         if ((menu->unmark.string = (char *) malloc(strlen(mark) + 1)) == NULL)
    125   1.1   blymn                 return E_SYSTEM_ERROR;
    126   1.1   blymn 
    127   1.1   blymn         strcpy(menu->unmark.string, mark);
    128   1.1   blymn 	menu->unmark.length = strlen(mark);
    129   1.1   blymn 	  /* max item size may have changed - recalculate. */
    130   1.4   blymn 	_menui_max_item_size(menu);
    131   1.1   blymn         return E_OK;
    132   1.1   blymn }
    133   1.1   blymn 
    134   1.1   blymn /*
    135   1.1   blymn  * Return the menu unmark string for the menu.
    136   1.1   blymn  */
    137   1.1   blymn char *
    138   1.1   blymn menu_unmark(menu)
    139   1.1   blymn         MENU *menu;
    140   1.1   blymn {
    141   1.1   blymn 	if (menu == NULL)
    142   1.1   blymn 		return _menui_default_menu.unmark.string;
    143   1.1   blymn 	else
    144   1.1   blymn 		return menu->unmark.string;
    145   1.1   blymn }
    146   1.1   blymn 
    147   1.1   blymn /*
    148   1.1   blymn  * Set the menu window to the window passed.
    149   1.1   blymn  */
    150   1.1   blymn int
    151   1.6   blymn set_menu_win(MENU *menu, WINDOW *win)
    152   1.1   blymn {
    153  1.11   blymn 	if (menu == NULL) {
    154   1.1   blymn 		_menui_default_menu.menu_win = win;
    155  1.11   blymn 		_menui_default_menu.scrwin = win;
    156  1.11   blymn 	} else {
    157  1.11   blymn 		if (menu->posted == TRUE) {
    158  1.11   blymn 			return E_POSTED;
    159  1.11   blymn 		} else {
    160  1.11   blymn 			menu->menu_win = win;
    161  1.11   blymn 			menu->scrwin = win;
    162  1.11   blymn 		}
    163  1.11   blymn 	}
    164  1.11   blymn 
    165   1.1   blymn         return E_OK;
    166   1.1   blymn }
    167   1.1   blymn 
    168   1.1   blymn /*
    169   1.1   blymn  * Return the pointer to the menu window
    170   1.1   blymn  */
    171   1.1   blymn WINDOW *
    172   1.6   blymn menu_win(MENU *menu)
    173   1.1   blymn {
    174   1.1   blymn 	if (menu == NULL)
    175   1.1   blymn 		return _menui_default_menu.menu_win;
    176   1.1   blymn 	else
    177   1.1   blymn 		return menu->menu_win;
    178   1.1   blymn }
    179   1.1   blymn 
    180   1.1   blymn /*
    181   1.1   blymn  * Set the menu subwindow for the menu.
    182   1.1   blymn  */
    183   1.1   blymn int
    184   1.1   blymn set_menu_sub(menu, sub)
    185   1.1   blymn         MENU *menu;
    186   1.1   blymn         WINDOW *sub;
    187   1.1   blymn {
    188  1.11   blymn 	if (menu == NULL) {
    189   1.1   blymn 		_menui_default_menu.menu_subwin = sub;
    190  1.11   blymn 		_menui_default_menu.scrwin = sub;
    191  1.11   blymn 	} else {
    192  1.11   blymn 		if (menu->posted == TRUE)
    193  1.11   blymn 			return E_POSTED;
    194  1.11   blymn 
    195   1.1   blymn 		menu->menu_subwin = sub;
    196  1.11   blymn 		menu->scrwin = sub;
    197  1.11   blymn 	}
    198  1.11   blymn 
    199   1.1   blymn         return E_OK;
    200   1.1   blymn }
    201   1.1   blymn 
    202   1.1   blymn /*
    203   1.1   blymn  * Return the subwindow pointer for the menu
    204   1.1   blymn  */
    205   1.1   blymn WINDOW *
    206   1.6   blymn menu_sub(MENU *menu)
    207   1.1   blymn {
    208   1.1   blymn 	if (menu == NULL)
    209   1.1   blymn 		return _menui_default_menu.menu_subwin;
    210   1.1   blymn 	else
    211   1.1   blymn 		return menu->menu_subwin;
    212   1.1   blymn }
    213   1.1   blymn 
    214   1.1   blymn /*
    215   1.1   blymn  * Set the maximum number of rows and columns of items that may be displayed.
    216   1.1   blymn  */
    217   1.1   blymn int
    218   1.6   blymn set_menu_format(MENU *param_menu, int rows, int cols)
    219   1.1   blymn {
    220   1.1   blymn 	MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
    221   1.1   blymn 
    222   1.1   blymn         menu->rows = rows;
    223   1.1   blymn         menu->cols = cols;
    224   1.1   blymn 
    225   1.1   blymn 	if (menu->items != NULL)
    226   1.1   blymn 		  /* recalculate the item neighbours */
    227   1.4   blymn 		return _menui_stitch_items(menu);
    228   1.1   blymn 
    229   1.1   blymn 	return E_OK;
    230   1.1   blymn }
    231   1.1   blymn 
    232   1.1   blymn /*
    233   1.1   blymn  * Return the max number of rows and cols that may be displayed.
    234   1.1   blymn  */
    235   1.1   blymn void
    236   1.6   blymn menu_format(MENU *param_menu, int *rows, int *cols)
    237   1.1   blymn {
    238   1.1   blymn 	MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
    239   1.1   blymn 
    240   1.1   blymn         *rows = menu->rows;
    241   1.1   blymn         *cols = menu->cols;
    242   1.1   blymn }
    243   1.1   blymn 
    244   1.1   blymn /*
    245   1.1   blymn  * Set the user defined function to call when a menu is posted.
    246   1.1   blymn  */
    247   1.1   blymn int
    248   1.6   blymn set_menu_init(MENU *menu, Menu_Hook func)
    249   1.1   blymn {
    250   1.1   blymn 	if (menu == NULL)
    251   1.1   blymn 		_menui_default_menu.menu_init = func;
    252   1.1   blymn 	else
    253   1.1   blymn 		menu->menu_init = func;
    254   1.1   blymn         return E_OK;
    255   1.1   blymn }
    256   1.1   blymn 
    257   1.1   blymn /*
    258   1.1   blymn  * Return the pointer to the menu init function.
    259   1.1   blymn  */
    260   1.4   blymn Menu_Hook
    261   1.1   blymn menu_init(MENU *menu)
    262   1.1   blymn {
    263   1.1   blymn 	if (menu == NULL)
    264   1.1   blymn 		return _menui_default_menu.menu_init;
    265   1.1   blymn 	else
    266   1.1   blymn 		return menu->menu_init;
    267   1.1   blymn }
    268   1.1   blymn 
    269   1.1   blymn /*
    270   1.1   blymn  * Set the user defined function called when a menu is unposted.
    271   1.1   blymn  */
    272   1.1   blymn int
    273   1.6   blymn set_menu_term(MENU *menu, Menu_Hook func)
    274   1.1   blymn {
    275   1.1   blymn 	if (menu == NULL)
    276   1.1   blymn 		_menui_default_menu.menu_term = func;
    277   1.1   blymn 	else
    278   1.1   blymn 		menu->menu_term = func;
    279   1.1   blymn         return E_OK;
    280   1.1   blymn }
    281   1.1   blymn 
    282   1.1   blymn /*
    283   1.1   blymn  * Return the user defined menu termination function pointer.
    284   1.1   blymn  */
    285   1.4   blymn Menu_Hook
    286   1.6   blymn menu_term(MENU *menu)
    287   1.1   blymn {
    288   1.1   blymn 	if (menu == NULL)
    289   1.1   blymn 		return _menui_default_menu.menu_term;
    290   1.1   blymn 	else
    291   1.1   blymn 		return menu->menu_term;
    292   1.1   blymn }
    293   1.1   blymn 
    294   1.1   blymn /*
    295   1.1   blymn  * Return the current menu options set.
    296   1.1   blymn  */
    297   1.1   blymn OPTIONS
    298   1.6   blymn menu_opts(MENU *menu)
    299   1.1   blymn {
    300   1.1   blymn 	if (menu == NULL)
    301   1.1   blymn 		return _menui_default_menu.opts;
    302   1.1   blymn 	else
    303   1.1   blymn 		return menu->opts;
    304   1.1   blymn }
    305   1.1   blymn 
    306   1.1   blymn /*
    307   1.1   blymn  * Set the menu options to the given options.
    308   1.1   blymn  */
    309   1.1   blymn int
    310   1.6   blymn set_menu_opts(MENU *param_menu, OPTIONS opts)
    311   1.1   blymn {
    312  1.12   blymn 	int i, seen;
    313   1.1   blymn 	MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
    314   1.1   blymn 	OPTIONS old_opts = menu->opts;
    315   1.1   blymn 
    316   1.1   blymn         menu->opts = opts;
    317   1.1   blymn 
    318  1.12   blymn 	  /*
    319  1.12   blymn 	   * If the radio option is selected then make sure only one
    320  1.12   blymn 	   * item is actually selected in the items.
    321  1.12   blymn 	   */
    322  1.12   blymn 	if (((opts & O_RADIO) == O_RADIO) && (menu->items != NULL) &&
    323  1.12   blymn 	    (menu->items[0] != NULL)) {
    324  1.12   blymn 		seen = 0;
    325  1.12   blymn 		for (i = 0; i < menu->item_count; i++) {
    326  1.12   blymn 			if (menu->items[i]->selected == 1) {
    327  1.12   blymn 				if (seen == 0) {
    328  1.12   blymn 					seen = 1;
    329  1.12   blymn 				} else {
    330  1.12   blymn 					menu->items[i]->selected = 0;
    331  1.12   blymn 				}
    332  1.12   blymn 			}
    333  1.12   blymn 		}
    334  1.12   blymn 
    335  1.12   blymn 		  /* if none selected, select the first item */
    336  1.12   blymn 		if (seen == 0)
    337  1.12   blymn 			menu->items[0]->selected = 1;
    338  1.12   blymn 	}
    339  1.12   blymn 
    340   1.1   blymn  	if ((menu->opts & O_ROWMAJOR) != (old_opts &  O_ROWMAJOR))
    341   1.1   blymn 		  /* changed menu layout - need to recalc neighbours */
    342   1.4   blymn 		_menui_stitch_items(menu);
    343   1.1   blymn 
    344   1.1   blymn         return E_OK;
    345   1.1   blymn }
    346   1.1   blymn 
    347   1.1   blymn /*
    348   1.1   blymn  * Turn on the options in menu given by opts.
    349   1.1   blymn  */
    350   1.1   blymn int
    351   1.6   blymn menu_opts_on(MENU *param_menu, OPTIONS opts)
    352   1.1   blymn {
    353  1.12   blymn 	int i, seen;
    354   1.1   blymn 	MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
    355   1.1   blymn 	OPTIONS old_opts = menu->opts;
    356   1.1   blymn 
    357   1.1   blymn         menu->opts |= opts;
    358   1.1   blymn 
    359  1.12   blymn 	  /*
    360  1.12   blymn 	   * If the radio option is selected then make sure only one
    361  1.12   blymn 	   * item is actually selected in the items.
    362  1.12   blymn 	   */
    363  1.12   blymn 	if (((opts & O_RADIO) == O_RADIO) && (menu->items != NULL) &&
    364  1.12   blymn 	    (menu->items[0] != NULL)) {
    365  1.12   blymn 		seen = 0;
    366  1.12   blymn 		for (i = 0; i < menu->item_count; i++) {
    367  1.12   blymn 			if (menu->items[i]->selected == 1) {
    368  1.12   blymn 				if (seen == 0) {
    369  1.12   blymn 					seen = 1;
    370  1.12   blymn 				} else {
    371  1.12   blymn 					menu->items[i]->selected = 0;
    372  1.12   blymn 				}
    373  1.12   blymn 			}
    374  1.12   blymn 		}
    375  1.12   blymn 		  /* if none selected then select the top item */
    376  1.12   blymn 		if (seen == 0)
    377  1.12   blymn 			menu->items[0]->selected = 1;
    378  1.12   blymn 	}
    379  1.12   blymn 
    380   1.1   blymn 	if ((menu->items != NULL) &&
    381   1.1   blymn 	    (menu->opts & O_ROWMAJOR) != (old_opts &  O_ROWMAJOR))
    382   1.1   blymn 		  /* changed menu layout - need to recalc neighbours */
    383   1.4   blymn 		_menui_stitch_items(menu);
    384   1.1   blymn 
    385   1.1   blymn         return E_OK;
    386   1.1   blymn }
    387   1.1   blymn 
    388   1.1   blymn /*
    389   1.1   blymn  * Turn off the menu options given in opts.
    390   1.1   blymn  */
    391   1.1   blymn int
    392   1.6   blymn menu_opts_off(MENU *param_menu, OPTIONS opts)
    393   1.1   blymn {
    394   1.1   blymn 	MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
    395   1.1   blymn 	OPTIONS old_opts = menu->opts;
    396   1.1   blymn 
    397   1.1   blymn         menu->opts &= ~(opts);
    398   1.1   blymn 
    399   1.1   blymn 	if ((menu->items != NULL ) &&
    400   1.1   blymn 	    (menu->opts & O_ROWMAJOR) != (old_opts &  O_ROWMAJOR))
    401   1.1   blymn 		  /* changed menu layout - need to recalc neighbours */
    402   1.4   blymn 		_menui_stitch_items(menu);
    403   1.1   blymn 
    404   1.1   blymn         return E_OK;
    405   1.1   blymn }
    406   1.1   blymn 
    407   1.1   blymn /*
    408   1.1   blymn  * Return the menu pattern buffer.
    409   1.1   blymn  */
    410   1.1   blymn char *
    411   1.6   blymn menu_pattern(MENU *menu)
    412   1.1   blymn {
    413   1.1   blymn 	if (menu == NULL)
    414   1.1   blymn 		return _menui_default_menu.pattern;
    415   1.1   blymn 	else
    416   1.1   blymn 		return menu->pattern;
    417   1.1   blymn }
    418   1.1   blymn 
    419   1.1   blymn /*
    420   1.1   blymn  * Set the menu pattern buffer to pat and attempt to match the pattern in
    421   1.1   blymn  * the item list.
    422   1.1   blymn  */
    423   1.1   blymn int
    424   1.6   blymn set_menu_pattern(MENU *param_menu, char *pat)
    425   1.1   blymn {
    426   1.1   blymn 	MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
    427   1.1   blymn 	char *p = pat;
    428   1.1   blymn 
    429   1.1   blymn 	  /* check pattern is all printable characters */
    430   1.1   blymn 	while (*p)
    431   1.9   itohy 		if (!isprint((unsigned char) *p++)) return E_BAD_ARGUMENT;
    432   1.1   blymn 
    433   1.1   blymn         if ((menu->pattern = (char *) realloc(menu->pattern,
    434  1.14   blymn                                      sizeof(char) * strlen(pat) + 1)) == NULL)
    435   1.1   blymn                 return E_SYSTEM_ERROR;
    436   1.1   blymn 
    437   1.1   blymn         strcpy(menu->pattern, pat);
    438   1.1   blymn 	menu->plen = strlen(pat);
    439   1.1   blymn 
    440   1.1   blymn           /* search item list for pat here */
    441   1.4   blymn 	return _menui_match_items(menu, MATCH_FORWARD, &menu->cur_item);
    442   1.1   blymn }
    443   1.1   blymn 
    444   1.1   blymn /*
    445   1.1   blymn  * Allocate a new menu structure and fill it in.
    446   1.1   blymn  */
    447   1.1   blymn MENU *
    448   1.6   blymn new_menu(ITEM **items)
    449   1.1   blymn {
    450   1.1   blymn         MENU *the_menu;
    451   1.1   blymn 
    452   1.1   blymn         if ((the_menu = (MENU *)malloc(sizeof(MENU))) == NULL)
    453   1.1   blymn                 return NULL;
    454   1.1   blymn 
    455   1.1   blymn           /* copy the defaults */
    456   1.3  kleink 	(void)memcpy(the_menu, &_menui_default_menu, sizeof(MENU));
    457   1.1   blymn 
    458   1.1   blymn 	  /* set a default window if none already set. */
    459   1.1   blymn 	if (the_menu->menu_win == NULL)
    460  1.11   blymn 		the_menu->scrwin = stdscr;
    461  1.13   blymn 
    462  1.13   blymn 	  /* make a private copy of the mark string */
    463  1.13   blymn 	if (_menui_default_menu.mark.string != NULL) {
    464  1.13   blymn 		if ((the_menu->mark.string =
    465  1.13   blymn 		     (char *) malloc((unsigned) _menui_default_menu.mark.length + 1))
    466  1.13   blymn 		    == NULL) {
    467  1.13   blymn 			free(the_menu);
    468  1.13   blymn 			return NULL;
    469  1.13   blymn 		}
    470  1.13   blymn 
    471  1.13   blymn 		strlcpy(the_menu->mark.string, _menui_default_menu.mark.string,
    472  1.14   blymn 			(unsigned) _menui_default_menu.mark.length + 1);
    473  1.13   blymn 	}
    474   1.1   blymn 
    475  1.13   blymn 	  /* make a private copy of the unmark string too */
    476  1.13   blymn 	if (_menui_default_menu.unmark.string != NULL) {
    477  1.13   blymn 		if ((the_menu->unmark.string =
    478  1.13   blymn 		     (char *) malloc((unsigned) _menui_default_menu.unmark.length + 1))
    479  1.13   blymn 		    == NULL) {
    480  1.13   blymn 			free(the_menu);
    481  1.13   blymn 			return NULL;
    482  1.13   blymn 		}
    483  1.13   blymn 
    484  1.13   blymn 		strlcpy(the_menu->unmark.string,
    485  1.13   blymn 			_menui_default_menu.unmark.string,
    486  1.14   blymn 			(unsigned) _menui_default_menu.unmark.length+ 1 );
    487  1.13   blymn 	}
    488  1.13   blymn 
    489   1.1   blymn           /* now attach the items, if any */
    490   1.1   blymn         if (items != NULL) {
    491   1.1   blymn 		if(set_menu_items(the_menu, items) < 0) {
    492  1.13   blymn 			if (the_menu->mark.string != NULL)
    493  1.13   blymn 				free(the_menu->mark.string);
    494  1.13   blymn 			if (the_menu->unmark.string != NULL)
    495  1.13   blymn 				free(the_menu->unmark.string);
    496   1.1   blymn 			free(the_menu);
    497   1.1   blymn 			return NULL;
    498   1.1   blymn 		}
    499   1.1   blymn 	}
    500   1.1   blymn 
    501   1.1   blymn 	return the_menu;
    502   1.1   blymn }
    503   1.1   blymn 
    504   1.1   blymn /*
    505   1.1   blymn  * Free up storage allocated to the menu object and destroy it.
    506   1.1   blymn  */
    507   1.1   blymn int
    508   1.6   blymn free_menu(MENU *menu)
    509   1.1   blymn {
    510   1.1   blymn 	int i;
    511   1.1   blymn 
    512   1.1   blymn 	if (menu == NULL)
    513   1.1   blymn 		return E_BAD_ARGUMENT;
    514   1.1   blymn 
    515   1.1   blymn 	if (menu->posted != 0)
    516   1.1   blymn 		return E_POSTED;
    517   1.1   blymn 
    518   1.1   blymn 	if (menu->pattern != NULL)
    519   1.1   blymn 		free(menu->pattern);
    520   1.1   blymn 
    521   1.1   blymn 	if (menu->mark.string != NULL)
    522   1.1   blymn 		free(menu->mark.string);
    523   1.1   blymn 
    524   1.1   blymn 	if (menu->items != NULL) {
    525   1.1   blymn 		  /* disconnect the items from this menu */
    526   1.1   blymn 		for (i = 0; i < menu->item_count; i++) {
    527   1.1   blymn 			menu->items[i]->parent = NULL;
    528   1.1   blymn 		}
    529   1.1   blymn 	}
    530   1.1   blymn 
    531   1.1   blymn 	free(menu);
    532   1.1   blymn 	return E_OK;
    533   1.1   blymn }
    534   1.1   blymn 
    535   1.1   blymn /*
    536   1.1   blymn  * Calculate the minimum window size for the menu.
    537   1.1   blymn  */
    538   1.1   blymn int
    539   1.6   blymn scale_menu(MENU *param_menu, int *rows, int *cols)
    540   1.1   blymn {
    541   1.1   blymn 	MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
    542   1.1   blymn 
    543   1.1   blymn 	if (menu->items == NULL)
    544   1.1   blymn 		return E_BAD_ARGUMENT;
    545   1.1   blymn 
    546   1.1   blymn 	  /* calculate the max item size */
    547   1.4   blymn 	_menui_max_item_size(menu);
    548   1.1   blymn 
    549   1.1   blymn 	*rows = menu->rows;
    550   1.1   blymn 	*cols = menu->cols * menu->max_item_width;
    551   1.1   blymn 
    552   1.1   blymn 	  /*
    553   1.1   blymn 	   * allow for spacing between columns...
    554   1.1   blymn 	   */
    555  1.15   blymn 	*cols += (menu->cols - 1);
    556   1.1   blymn 
    557   1.1   blymn 	return E_OK;
    558   1.1   blymn }
    559   1.1   blymn 
    560   1.1   blymn /*
    561   1.1   blymn  * Set the menu item list to the one given.
    562   1.1   blymn  */
    563   1.1   blymn int
    564   1.6   blymn set_menu_items(MENU *param_menu, ITEM **items)
    565   1.1   blymn {
    566   1.1   blymn 	MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
    567  1.12   blymn 	int i, new_count = 0, sel_count = 0;
    568   1.1   blymn 
    569   1.1   blymn 	  /* don't change if menu is posted */
    570   1.1   blymn 	if (menu->posted == 1)
    571   1.1   blymn 		return E_POSTED;
    572   1.1   blymn 
    573   1.1   blymn 	  /* count the new items and validate none are connected already */
    574   1.1   blymn 	while (items[new_count] != NULL) {
    575   1.1   blymn 		if ((items[new_count]->parent != NULL) &&
    576   1.1   blymn 		    (items[new_count]->parent != menu))
    577   1.1   blymn 			return E_CONNECTED;
    578  1.12   blymn 		if (items[new_count]->selected == 1)
    579  1.12   blymn 			sel_count++;
    580   1.1   blymn 		new_count++;
    581   1.1   blymn 	}
    582  1.12   blymn 
    583  1.12   blymn 	  /*
    584  1.12   blymn 	   * don't allow multiple selected items if menu is radio
    585  1.12   blymn 	   * button style.
    586  1.12   blymn 	   */
    587  1.12   blymn 	if (((menu->opts & O_RADIO) == O_RADIO) &&
    588  1.12   blymn 	    (sel_count > 1))
    589  1.12   blymn 		return E_BAD_ARGUMENT;
    590   1.1   blymn 
    591   1.1   blymn 	  /* if there were items connected then disconnect them. */
    592   1.1   blymn 	if (menu->items != NULL) {
    593   1.1   blymn 		for (i = 0; i < menu->item_count; i++) {
    594   1.1   blymn 			menu->items[i]->parent = NULL;
    595   1.1   blymn 			menu->items[i]->index = -1;
    596   1.1   blymn 		}
    597   1.1   blymn 	}
    598   1.1   blymn 
    599   1.1   blymn 	menu->item_count = new_count;
    600   1.1   blymn 
    601   1.1   blymn 	  /* connect the new items to the menu */
    602   1.1   blymn 	for (i = 0; i < new_count; i++) {
    603   1.1   blymn 		items[i]->parent = menu;
    604   1.1   blymn 		items[i]->index = i;
    605   1.1   blymn 	}
    606   1.1   blymn 
    607   1.1   blymn 	menu->items = items;
    608   1.1   blymn 	menu->cur_item = 0; /* reset current item just in case */
    609   1.1   blymn 	menu->top_row = 0; /* and the top row too */
    610   1.1   blymn 	if (menu->pattern != NULL) { /* and the pattern buffer....sigh */
    611   1.1   blymn 		free(menu->pattern);
    612   1.1   blymn 		menu->plen = 0;
    613   1.1   blymn 		menu->match_len = 0;
    614   1.1   blymn 	}
    615  1.12   blymn 
    616  1.12   blymn 	  /*
    617  1.12   blymn 	   * make sure at least one item is selected on a radio
    618  1.12   blymn 	   * button style menu.
    619  1.12   blymn 	   */
    620  1.12   blymn 	if (((menu->opts & O_RADIO) == O_RADIO) && (sel_count == 0))
    621  1.12   blymn 		menu->items[0]->selected = 1;
    622  1.12   blymn 
    623   1.1   blymn 
    624   1.4   blymn 	_menui_stitch_items(menu); /* recalculate the item neighbours */
    625   1.1   blymn 
    626   1.1   blymn 	return E_OK;
    627   1.1   blymn }
    628   1.1   blymn 
    629   1.1   blymn /*
    630   1.1   blymn  * Return the pointer to the menu items array.
    631   1.1   blymn  */
    632   1.1   blymn ITEM **
    633   1.6   blymn menu_items(MENU *menu)
    634   1.1   blymn {
    635   1.1   blymn 	if (menu == NULL)
    636   1.1   blymn 		return _menui_default_menu.items;
    637   1.1   blymn 	else
    638   1.1   blymn 		return menu->items;
    639   1.1   blymn }
    640   1.1   blymn 
    641   1.1   blymn /*
    642   1.1   blymn  * Return the count of items connected to the menu
    643   1.1   blymn  */
    644   1.1   blymn int
    645   1.6   blymn item_count(MENU *menu)
    646   1.1   blymn {
    647   1.1   blymn 	if (menu == NULL)
    648   1.1   blymn 		return _menui_default_menu.item_count;
    649   1.1   blymn 	else
    650   1.1   blymn 		return menu->item_count;
    651   1.1   blymn }
    652   1.1   blymn 
    653   1.1   blymn /*
    654   1.1   blymn  * Set the menu top row to be the given row.  The current item becomes the
    655   1.1   blymn  * leftmost item on that row in the menu.
    656   1.1   blymn  */
    657   1.1   blymn int
    658   1.6   blymn set_top_row(MENU *param_menu, int row)
    659   1.1   blymn {
    660   1.1   blymn 	MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
    661   1.1   blymn 	int i, cur_item, state = E_SYSTEM_ERROR;
    662   1.1   blymn 
    663   1.1   blymn 	if (row > menu->item_rows)
    664   1.1   blymn 		return E_BAD_ARGUMENT;
    665   1.1   blymn 
    666   1.1   blymn 	if (menu->items == NULL)
    667   1.1   blymn 		return E_NOT_CONNECTED;
    668   1.1   blymn 
    669   1.1   blymn 	if (menu->in_init == 1)
    670   1.1   blymn 		return E_BAD_STATE;
    671   1.1   blymn 
    672   1.8   blymn 	cur_item = 0;
    673   1.8   blymn 
    674   1.1   blymn 	for (i = 0; i < menu->item_count; i++) {
    675   1.1   blymn 		  /* search for first item that matches row - this will be
    676   1.1   blymn 		     the current item. */
    677   1.1   blymn 		if (row == menu->items[i]->row) {
    678   1.1   blymn 			cur_item = i;
    679   1.1   blymn 			state = E_OK;
    680   1.1   blymn 			break; /* found what we want - no need to go further */
    681   1.1   blymn 		}
    682   1.1   blymn 	}
    683   1.1   blymn 
    684   1.1   blymn 	menu->in_init = 1; /* just in case we call the init/term routines */
    685   1.1   blymn 
    686   1.1   blymn 	if (menu->posted == 1) {
    687   1.1   blymn 		if (menu->menu_term != NULL)
    688   1.1   blymn 			menu->menu_term(menu);
    689   1.1   blymn 		if (menu->item_term != NULL)
    690   1.1   blymn 			menu->item_term(menu);
    691   1.1   blymn 	}
    692   1.1   blymn 
    693   1.1   blymn 	menu->cur_item = cur_item;
    694   1.1   blymn 	menu->top_row = row;
    695   1.1   blymn 
    696   1.1   blymn 	if (menu->posted == 1) {
    697   1.1   blymn 		if (menu->menu_init != NULL)
    698   1.1   blymn 			menu->menu_init(menu);
    699   1.1   blymn 		if (menu->item_init != NULL)
    700   1.1   blymn 			menu->item_init(menu);
    701   1.1   blymn 	}
    702   1.1   blymn 
    703   1.1   blymn 	menu->in_init = 0;
    704   1.1   blymn 
    705   1.1   blymn 	  /* this should always be E_OK unless we are really screwed up */
    706   1.1   blymn 	return state;
    707   1.1   blymn }
    708   1.1   blymn 
    709   1.1   blymn /*
    710   1.1   blymn  * Return the current top row number.
    711   1.1   blymn  */
    712   1.1   blymn int
    713   1.6   blymn top_row(MENU *param_menu)
    714   1.1   blymn {
    715   1.1   blymn 	MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
    716   1.1   blymn 
    717   1.1   blymn 	if (menu->items == NULL)
    718   1.1   blymn 		return E_NOT_CONNECTED;
    719   1.1   blymn 
    720   1.1   blymn 	return menu->top_row;
    721   1.1   blymn }
    722   1.1   blymn 
    723   1.1   blymn /*
    724   1.1   blymn  * Position the cursor at the correct place in the menu.
    725   1.1   blymn  *
    726   1.1   blymn  */
    727   1.1   blymn int
    728   1.6   blymn pos_menu_cursor(MENU *menu)
    729   1.1   blymn {
    730   1.1   blymn 	int movx, maxmark;
    731   1.1   blymn 
    732   1.1   blymn 	if (menu == NULL)
    733   1.1   blymn 		return E_BAD_ARGUMENT;
    734   1.1   blymn 
    735   1.1   blymn 	maxmark = max(menu->mark.length, menu->unmark.length);
    736   1.1   blymn 	movx = maxmark + (menu->items[menu->cur_item]->col
    737  1.15   blymn 		* (menu->col_width + 1));
    738   1.1   blymn 
    739   1.1   blymn 	if (menu->match_len > 0)
    740   1.1   blymn 		movx += menu->match_len - 1;
    741   1.1   blymn 
    742  1.11   blymn 	wmove(menu->scrwin,
    743   1.1   blymn 	      menu->items[menu->cur_item]->row - menu->top_row, movx);
    744   1.1   blymn 
    745   1.1   blymn 	return E_OK;
    746   1.1   blymn }
    747