Home | History | Annotate | Download | only in libmenu
      1 /* -*- c -*- ------------------------------------------------------------- *
      2  *
      3  *   Copyright 2004-2005 Murali Krishnan Ganapathy - All Rights Reserved
      4  *
      5  *   This program is free software; you can redistribute it and/or modify
      6  *   it under the terms of the GNU General Public License as published by
      7  *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
      8  *   Boston MA 02111-1307, USA; either version 2 of the License, or
      9  *   (at your option) any later version; incorporated herein by reference.
     10  *
     11  * ----------------------------------------------------------------------- */
     12 
     13 /* This program can be compiled for DOS with the OpenWatcom compiler
     14  * (http://www.openwatcom.org/):
     15  *
     16  * wcl -3 -osx -mt <filename>.c
     17  */
     18 
     19 #ifndef __MENU_H__
     20 #define __MENU_H__
     21 
     22 #include "com32io.h"
     23 #include "tui.h"
     24 #include "syslnx.h"
     25 #include <string.h>
     26 #include <unistd.h>
     27 
     28 // TIMEOUT PARAMETERS
     29 /* If no key is pressed within TIMEOUTNUMSTEPS * TIMEOUTSTEPSIZE milliseconds
     30    and if a timeout handler is registered, then that will be called.
     31    The handler should either either take control from there on, or return without
     32    producing any change in the current video settings.
     33 
     34    For e.g. the handler could
     35    * Could just quit the menu program
     36    * beep and return.
     37 
     38    TIMEOUTSTEPSIZE is the interval for which the program sleeps without checking for
     39    any keystroke. So increasing this will make the response of the system slow.
     40    Decreasing this will make a lot of interrupt calls using up your CPU. Default
     41    value of TIMEOUTSTEPSIZE of 0.1 seconds should be right in most cases.
     42 
     43    TIMEOUTNUMSTEPS of 3000 corresponds to a wait time of 300 seconds or 5 minutes
     44 */
     45 
     46 #define TIMEOUTSTEPSIZE 10
     47 #define TIMEOUTNUMSTEPS 30000L
     48 
     49 // Attributes
     50 #define NORMALATTR    0x17
     51 #define NORMALHLITE   0x1F	// Normal Highlight attribute
     52 #define REVERSEATTR   0x70
     53 #define REVERSEHLITE  0x78	// Reverse Hightlight attribute
     54 #define INACTATTR     0x18
     55 #define INACTHLITE    0x10	// Inactive Highlight attribute
     56 #define REVINACTATTR  0x78
     57 #define REVINACTHLITE 0x70	// Reverse Inactive Highlight attr
     58 
     59 #define STATUSATTR    0x74
     60 #define STATUSHLITE   0x7B	// Status highlight
     61 
     62 #define FILLCHAR      ' '
     63 #define FILLATTR      NORMALATTR
     64 #define SHADOWATTR    0x00
     65 #define SPACECHAR     ' '
     66 
     67 #define TFILLCHAR     ' '
     68 #define TITLEATTR     0x70
     69 
     70 #define ENABLEHLITE   '<'	// Char which turns on highlight
     71 #define DISABLEHLITE  '>'	// Char which turns off highlight
     72 #define NOHLITE       0		// The offset into attrib array for non-hilite
     73 #define HLITE         1		// The offset for Hlite attrib
     74 
     75 #define MOREABOVE    '^'		// char to print when more menu items available above
     76 #define MOREBELOW    'v'		// more items available below
     77 
     78 // Attributes of the menu system
     79 #define MAXMENUS      250	// Maximum number of menu's allowed
     80 #define MAXMENUSIZE   100	// Default value for max num of entries in each menu
     81 #define MAXMENUHEIGHT 20	// Maximum number of entries displayed
     82 #define MENUBOXTYPE   BOX_SINSIN	// Default box type Look at tui.h for other values
     83 
     84 // Upper bounds on lengths
     85 // We copy the given string, so user can reuse the space used to store incoming arguments.
     86 #define MENULEN       78	// Each menu entry is atmost MENULEN chars
     87 #define STATLEN       78	// Maximum length of status string
     88 #define TITLELEN      78	// Maximum length of title string
     89 #define ACTIONLEN     255	// Maximum length of an action string
     90 
     91 // Layout of menu
     92 #define MENUROW       3		// Row where menu is displayed (relative to window)
     93 #define MENUCOL       4		// Col where menu is displayed (relative to window)
     94 #define MENUPAGE      1		// show in display page 1
     95 #define STATLINE      24	// Line number where status line starts (relative to window)
     96 
     97 // Used for printing debugging messages
     98 #define DEBUGLINE     23	// debugging info goes here
     99 
    100 // Other Chars
    101 #define RADIOMENUCHAR '>'	// > symbol for radio menu?
    102 #define CHECKED       '\140'	// Check mark
    103 #define UNCHECKED     '\146'	// Light bullet
    104 #define RADIOSEL      '.'	// Current Radio Selection
    105 #define RADIOUNSEL    ' '	// Radio option not selected
    106 
    107 typedef unsigned char uchar;
    108 
    109 // Types of menu's
    110 #define NORMALMENU 1
    111 #define RADIOMENU  2
    112 
    113 typedef enum { OPT_INACTIVE, OPT_SUBMENU, OPT_RUN, OPT_EXITMENU, OPT_CHECKBOX,
    114     OPT_RADIOMENU, OPT_SEP, OPT_INVISIBLE,
    115     OPT_RADIOITEM
    116 } t_action;
    117 
    118 typedef union {
    119     uchar submenunum;		// For submenu's
    120     uchar checked;		// For check boxes
    121     uchar radiomenunum;		// Item mapping to a radio menu
    122 } t_itemdata;
    123 
    124 struct s_menuitem;
    125 struct s_menu;
    126 struct s_menusystem;
    127 
    128 typedef struct {
    129     unsigned int valid:1;	// Is action valid?
    130     unsigned int refresh:1;	// Should we recompute menu stuff?
    131     unsigned int reserved:6;	// For future expansion
    132 } t_handler_return;
    133 
    134 t_handler_return ACTION_VALID, ACTION_INVALID;	// Specific values
    135 
    136 typedef t_handler_return(*t_item_handler) (struct s_menusystem *,
    137 					   struct s_menuitem *);
    138 typedef void (*t_menusystem_handler) (struct s_menusystem *,
    139 				      struct s_menuitem *);
    140 typedef void (*t_keys_handler) (struct s_menusystem *, struct s_menuitem *,
    141 				unsigned int scancode);
    142     // Last parameter = HIGH BYTE = scan code , LOW BYTE = ASCII CODE
    143 
    144 typedef enum { HDLR_SCREEN, HDLR_KEYS } t_handler;
    145 // Types of handlers for menu system
    146 
    147 // TIMEOUT is the list of possible values which can be returned by the handler
    148 // instructing the menusystem what to do. The default is CODE_WAIT
    149 typedef enum { CODE_WAIT, CODE_ENTER, CODE_ESCAPE } TIMEOUTCODE;
    150 typedef TIMEOUTCODE(*t_timeout_handler) (void);
    151 
    152 typedef struct s_menuitem {
    153     char *item;
    154     char *status;
    155     char *data;			// string containing kernel to run.. but...
    156     // for radio menu's this is a pointer to the item selected or NULL (initially)
    157     // for submenu's this string could be name of menu
    158     void *extra_data;		// Any other data user can point to
    159     unsigned int helpid;	// Used for Context sensitive help
    160     t_item_handler handler;	// Pointer to function of type menufn
    161     t_action action;
    162     t_itemdata itemdata;	// Data depends on action value
    163     uchar shortcut;		// one of [A-Za-z0-9] shortcut for this menu item
    164     uchar index;		// Index within the menu array
    165     uchar parindex;		// Index of the menu in which this item appears.
    166 
    167 } t_menuitem;
    168 
    169 typedef t_menuitem *pt_menuitem;	// Pointer to type menuitem
    170 
    171 typedef struct s_menu {
    172     pt_menuitem *items;		// pointer to array of pointer to menuitems
    173     char *title;		// Title string for menu
    174     char *name;			// menu can be referred to by this string
    175     int maxmenusize;		// the size of array allocated
    176     uchar numitems;		// how many items do we actually have
    177     uchar menuwidth;
    178     uchar row, col;		// Position where this menu should be displayed
    179     uchar menuheight;		// Maximum number of items to be displayed
    180 } t_menu;
    181 
    182 typedef t_menu *pt_menu;	// Pointer to type menu
    183 
    184 typedef struct s_menusystem {
    185     pt_menu menus[MAXMENUS];
    186     char *title;
    187     t_menusystem_handler handler;	// Menu system handler
    188     t_keys_handler keys_handler;	// Handler for unknown keys
    189     t_timeout_handler ontimeout;	// Timeout handler
    190     unsigned long tm_numsteps;
    191     // Time to wait for key press=numsteps * stepsize milliseconds
    192     unsigned int tm_stepsize;	// Timeout step size (in milliseconds)
    193     // Total timeout max time spent idle before we call handler
    194     unsigned long tm_total_timeout;	// (in milli seconds)
    195     unsigned long tm_sofar_timeout;	// All accumulated timeout
    196     // total timeout handler
    197     t_timeout_handler ontotaltimeout;	// Total timeout handler
    198 
    199     int maxmenuheight;
    200     uchar nummenus;
    201     uchar normalattr[2];	// [0] is non-hlite attr, [1] is hlite attr
    202     uchar reverseattr[2];
    203     uchar inactattr[2];
    204     uchar revinactattr[2];
    205     uchar statusattr[2];
    206     uchar fillchar;
    207     uchar fillattr;
    208     uchar spacechar;
    209     uchar tfillchar;
    210     uchar titleattr;
    211     uchar shadowattr;
    212     uchar statline;
    213     uchar menupage;
    214     int maxrow, minrow, numrows;	// Number of rows in the window
    215     int maxcol, mincol, numcols;	// Number of columns in the window
    216 
    217     // Menu box look
    218     char box_horiz, box_ltrt, box_rtlt;	// Some chars of the box, for redrawing portions of the box
    219 
    220 } t_menusystem;
    221 
    222 typedef t_menusystem *pt_menusystem;	// Pointer to type menusystem
    223 
    224 pt_menuitem showmenus(uchar startmenu);
    225 
    226 pt_menusystem init_menusystem(const char *title);
    227 
    228 void close_menusystem(void);	// Deallocate memory used
    229 
    230 void set_normal_attr(uchar normal, uchar selected, uchar inactivenormal,
    231 		     uchar inactiveselected);
    232 
    233 void set_normal_hlite(uchar normal, uchar selected, uchar inactivenormal,
    234 		      uchar inactiveselected);
    235 
    236 void set_status_info(uchar statusattr, uchar statushlite, uchar statline);
    237 
    238 void set_title_info(uchar tfillchar, uchar titleattr);
    239 
    240 void set_misc_info(uchar fillchar, uchar fillattr, uchar spacechar,
    241 		   uchar shadowattr);
    242 
    243 void set_window_size(uchar top, uchar left, uchar bot, uchar right);	// Set the window which menusystem should use
    244 
    245 void set_menu_options(uchar maxmenuheight);
    246 // maximum height of a menu
    247 
    248 void reg_handler(t_handler htype, void *handler);	// Register handler
    249 
    250 void unreg_handler(t_handler htype);
    251 
    252 void reg_ontimeout(t_timeout_handler, unsigned int numsteps,
    253 		   unsigned int stepsize);
    254 // Set timeout handler, set 0 for default values.
    255 // So stepsize=0 means numsteps is measured in centiseconds.
    256 void unreg_ontimeout(void);
    257 
    258 void reg_ontotaltimeout(t_timeout_handler, unsigned long numcentiseconds);
    259 void unreg_ontotaltimeout(void);
    260 
    261 // Find the number of the menu given the name
    262 // Returns -1 if not found
    263 uchar find_menu_num(const char *name);
    264 
    265 // Create a new menu and return its position
    266 uchar add_menu(const char *title, int maxmenusize);
    267 
    268 // Create a named menu and return its position
    269 uchar add_named_menu(const char *name, const char *title, int maxmenusize);
    270 
    271 void set_menu_pos(uchar row, uchar col);	// Set the position of this menu.
    272 
    273 // Add item to the "current" menu
    274 pt_menuitem add_item(const char *item, const char *status, t_action action,
    275 		     const char *data, uchar itemdata);
    276 
    277 // Set shortcut key and help id
    278 void set_item_options(uchar shortcut, int helpid);
    279 
    280 // Set the shortcut key for the current item
    281 static inline void set_shortcut(uchar shortcut)
    282 {
    283     set_item_options(shortcut, 0xFFFF);
    284 }
    285 
    286 // Add a separator to the "current" menu
    287 pt_menuitem add_sep(void);
    288 
    289 // Generate string based on state of checkboxes and radioitem in given menu
    290 // and append string to existing contents of "line"
    291 // line must have enough space allocated
    292 void gen_append_line(const char *menu_name, char *line);
    293 
    294 #endif
    295