Initial commit of the HEAD branch of the ELinks CVS repository, as of
[elinks/images.git] / src / bfu / menu.h
blob7f9f11fe4ca9f9c335f3dfe4cb1bb3d751c6ecde
1 /* $Id: menu.h,v 1.70 2005/06/10 04:47:02 miciah Exp $ */
3 #ifndef EL__BFU_MENU_H
4 #define EL__BFU_MENU_H
6 #include "config/kbdbind.h"
7 #include "util/box.h"
9 struct terminal;
10 struct window;
12 typedef void (*menu_func_T)(struct terminal *, void *, void *);
14 /* Which fields to free when zapping a list item - bitwise. */
15 enum menu_item_flags {
16 NO_FLAG = 0,
18 FREE_LIST = 1, /* Free the 'list' of menu items */
20 FREE_TEXT = 2, /* Free the (main) text */
21 FREE_RTEXT = 4, /* Free the right aligned text */
22 FREE_DATA = 8, /* Free the private data */
24 MENU_FULLNAME = 16, /* Catenate base string to <select> item text */
25 SUBMENU = 32, /* Item opens submenu, so show '>>' as rtext */
26 NO_INTL = 64, /* Don't translate the text */
27 NO_SELECT = 128, /* Mark item as unselectable */
28 RIGHT_INTL = 256, /* Force translation of the right text */
31 #define FREE_ANY (FREE_LIST|FREE_TEXT|FREE_RTEXT|FREE_DATA)
34 * Selectable menu item.
36 #define mi_is_selectable(mi) (!((mi)->flags & NO_SELECT))
39 * Menu item has left text.
41 #define mi_has_left_text(mi) ((mi)->text && *(mi)->text)
44 * Menu item has right text.
46 #define mi_has_right_text(mi) ((mi)->rtext && *(mi)->rtext)
49 * Horizontal bar
51 #define mi_is_horizontal_bar(mi) (!mi_is_selectable(mi) && (mi)->text && !(mi)->text[0])
54 * Submenu item
56 #define mi_is_submenu(mi) ((mi)->flags & SUBMENU)
59 * Texts should be translated or not.
61 #define mi_text_translate(mi) (!((mi)->flags & NO_INTL))
62 #define mi_rtext_translate(mi) ((mi)->flags & RIGHT_INTL)
65 * End of menu items list
67 #define mi_is_end_of_menu(mi) (!(mi)->text)
69 #define foreach_menu_item(iterator, items) \
70 for (iterator = (items); !mi_is_end_of_menu(iterator); (iterator)++)
72 enum hotkey_state {
73 HKS_SHOW = 0,
74 HKS_IGNORE,
75 HKS_CACHED,
78 /* XXX: keep order of fields, there's some hard initializations for it. --Zas
80 struct menu_item {
81 unsigned char *text; /* The item label */
83 /* The following three members are tightly coupled:
85 * - If @action is not MAIN_ACT_NONE the associated keybinding will be
86 * shown as the guiding right text and do_action() will be called
87 * when the item is selected rendering both @rtext and @func useless.
89 * - A few places however there is no associated keybinding and no
90 * ``default'' handler defined in which case @rtext (if non NULL)
91 * will be drawn and @func will be called when selecting the item. */
92 unsigned char *rtext; /* Right aligned guiding text */
93 enum main_action action_id; /* Default item handlers */
94 menu_func_T func; /* Called when selecting the item */
96 void *data; /* Private data passed to handler */
97 enum menu_item_flags flags; /* What to free() and display */
99 /* If true, don't try to translate text/rtext inside of the menu
100 * routines. */
101 enum hotkey_state hotkey_state; /* The state of the hotkey caching */
102 int hotkey_pos; /* The offset of the hotkey in @text */
105 #define INIT_MENU_ITEM(text, rtext, action_id, func, data, flags) \
107 (unsigned char *) (text), \
108 (unsigned char *) (rtext), \
109 (action_id), \
110 (func), \
111 (void *) (data), \
112 (flags), \
113 HKS_SHOW, \
117 #define INIT_MENU_ACTION(text, action_id) \
118 INIT_MENU_ITEM(text, NULL, action_id, NULL, NULL, 0)
120 #define NULL_MENU_ITEM \
121 INIT_MENU_ITEM(NULL, NULL, ACT_MAIN_NONE, NULL, NULL, 0)
123 #define BAR_MENU_ITEM \
124 INIT_MENU_ITEM("", NULL, ACT_MAIN_NONE, NULL, NULL, NO_SELECT)
126 #define SET_MENU_ITEM(e_, text_, rtext_, action_id_, func_, data_, \
127 flags_, hotkey_state_, hotkey_pos_) \
128 do { \
129 (e_)->text = (unsigned char *) (text_); \
130 (e_)->rtext = (unsigned char *) (rtext_); \
131 (e_)->action_id = (action_id_); \
132 (e_)->func = (func_); \
133 (e_)->data = (void *) (data_); \
134 (e_)->flags = (flags_); \
135 (e_)->hotkey_state = (hotkey_state_); \
136 (e_)->hotkey_pos = (hotkey_pos_); \
137 } while (0)
140 struct menu {
141 struct window *win; /* The terminal window the menu lives in */
143 struct menu_item *items;/* The items in the menu */
144 int size; /* The number of menu items */
146 int selected; /* The current selected item. -1 means none */
147 int first, last; /* The first and last visible menu items */
149 struct box box; /* The visible area of the menu */
150 int parent_x, parent_y; /* The coordinates of the parent window */
152 int hotkeys; /* Whether to check and display hotkeys */
153 #ifdef CONFIG_NLS
154 int lang; /* For keeping the hotkey cache in sync */
155 #endif
157 /* The private menu data that is passed as the 3. arg to the
158 * menu items' menu_func_T handler */
159 void *data;
163 struct menu_item *new_menu(enum menu_item_flags);
165 void
166 add_to_menu(struct menu_item **mi, unsigned char *text, unsigned char *rtext,
167 enum main_action action_id, menu_func_T func, void *data,
168 enum menu_item_flags flags);
170 #define add_menu_separator(menu) \
171 add_to_menu(menu, "", NULL, ACT_MAIN_NONE, NULL, NULL, NO_SELECT)
173 /* Implies that the action will be handled by do_action() */
174 #define add_menu_action(menu, text, action_id) \
175 add_to_menu(menu, text, NULL, action_id, NULL, NULL, NO_FLAG)
177 void do_menu(struct terminal *, struct menu_item *, void *, int);
178 void do_menu_selected(struct terminal *, struct menu_item *, void *, int, int);
179 void do_mainmenu(struct terminal *, struct menu_item *, void *, int);
181 #endif