1 /* -*- c -*- ------------------------------------------------------------- *
3 * Copyright 2004-2005 Murali Krishnan Ganapathy - All Rights Reserved
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.
11 * ----------------------------------------------------------------------- */
13 /* This program can be compiled for DOS with the OpenWatcom compiler
14 * (http://www.openwatcom.org/):
16 * wcl -3 -osx -mt <filename>.c
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.
34 For e.g. the handler could
35 * Could just quit the menu program
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.
43 TIMEOUTNUMSTEPS of 3000 corresponds to a wait time of 300 seconds or 5 minutes
46 #define TIMEOUTSTEPSIZE 10
47 #define TIMEOUTNUMSTEPS 30000L
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
59 #define STATUSATTR 0x74
60 #define STATUSHLITE 0x7B // Status highlight
63 #define FILLATTR NORMALATTR
64 #define SHADOWATTR 0x00
68 #define TITLEATTR 0x70
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
75 #define MOREABOVE '^' // char to print when more menu items available above
76 #define MOREBELOW 'v' // more items available below
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
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
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)
97 // Used for printing debugging messages
98 #define DEBUGLINE 23 // debugging info goes here
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
107 typedef unsigned char uchar
;
113 typedef enum { OPT_INACTIVE
, OPT_SUBMENU
, OPT_RUN
, OPT_EXITMENU
, OPT_CHECKBOX
,
114 OPT_RADIOMENU
, OPT_SEP
, OPT_INVISIBLE
,
119 uchar submenunum
; // For submenu's
120 uchar checked
; // For check boxes
121 uchar radiomenunum
; // Item mapping to a radio menu
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
134 t_handler_return ACTION_VALID
, ACTION_INVALID
; // Specific values
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
144 typedef enum { HDLR_SCREEN
, HDLR_KEYS
} t_handler
;
145 // Types of handlers for menu system
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);
152 typedef struct s_menuitem
{
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
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.
169 typedef t_menuitem
*pt_menuitem
; // Pointer to type menuitem
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
178 uchar row
, col
; // Position where this menu should be displayed
179 uchar menuheight
; // Maximum number of items to be displayed
182 typedef t_menu
*pt_menu
; // Pointer to type menu
184 typedef struct s_menusystem
{
185 pt_menu menus
[MAXMENUS
];
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
201 uchar normalattr
[2]; // [0] is non-hlite attr, [1] is hlite attr
202 uchar reverseattr
[2];
204 uchar revinactattr
[2];
214 int maxrow
, minrow
, numrows
; // Number of rows in the window
215 int maxcol
, mincol
, numcols
; // Number of columns in the window
218 char box_horiz
, box_ltrt
, box_rtlt
; // Some chars of the box, for redrawing portions of the box
222 typedef t_menusystem
*pt_menusystem
; // Pointer to type menusystem
224 pt_menuitem
showmenus(uchar startmenu
);
226 pt_menusystem
init_menusystem(const char *title
);
228 void close_menusystem(void); // Deallocate memory used
230 void set_normal_attr(uchar normal
, uchar selected
, uchar inactivenormal
,
231 uchar inactiveselected
);
233 void set_normal_hlite(uchar normal
, uchar selected
, uchar inactivenormal
,
234 uchar inactiveselected
);
236 void set_status_info(uchar statusattr
, uchar statushlite
, uchar statline
);
238 void set_title_info(uchar tfillchar
, uchar titleattr
);
240 void set_misc_info(uchar fillchar
, uchar fillattr
, uchar spacechar
,
243 void set_window_size(uchar top
, uchar left
, uchar bot
, uchar right
); // Set the window which menusystem should use
245 void set_menu_options(uchar maxmenuheight
);
246 // maximum height of a menu
248 void reg_handler(t_handler htype
, void *handler
); // Register handler
250 void unreg_handler(t_handler htype
);
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);
258 void reg_ontotaltimeout(t_timeout_handler
, unsigned long numcentiseconds
);
259 void unreg_ontotaltimeout(void);
261 // Find the number of the menu given the name
262 // Returns -1 if not found
263 uchar
find_menu_num(const char *name
);
265 // Create a new menu and return its position
266 uchar
add_menu(const char *title
, int maxmenusize
);
268 // Create a named menu and return its position
269 uchar
add_named_menu(const char *name
, const char *title
, int maxmenusize
);
271 void set_menu_pos(uchar row
, uchar col
); // Set the position of this menu.
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
);
277 // Set shortcut key and help id
278 void set_item_options(uchar shortcut
, int helpid
);
280 // Set the shortcut key for the current item
281 static inline void set_shortcut(uchar shortcut
)
283 set_item_options(shortcut
, 0xFFFF);
286 // Add a separator to the "current" menu
287 pt_menuitem
add_sep(void);
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
);