Code reuse
[elinks.git] / src / bfu / menu.h
blobaa076dbea14cda326fcc998951d0e5b3e730b04c
1 #ifndef EL__BFU_MENU_H
2 #define EL__BFU_MENU_H
4 #include "config/kbdbind.h"
5 #include "util/box.h"
7 struct terminal;
8 struct window;
10 typedef void (*menu_func_T)(struct terminal *, void *, void *);
12 /* Which fields to free when zapping a list item - bitwise. */
13 enum menu_item_flags {
14 NO_FLAG = 0,
16 FREE_LIST = 1, /* Free the 'list' of menu items */
18 FREE_TEXT = 2, /* Free the (main) text */
19 FREE_RTEXT = 4, /* Free the right aligned text */
20 FREE_DATA = 8, /* Free the private data */
22 MENU_FULLNAME = 16, /* Catenate base string to <select> item text */
23 SUBMENU = 32, /* Item opens submenu, so show '>>' as rtext */
24 NO_INTL = 64, /* Don't translate the text */
25 NO_SELECT = 128, /* Mark item as unselectable */
26 RIGHT_INTL = 256, /* Force translation of the right text */
29 #define FREE_ANY (FREE_LIST|FREE_TEXT|FREE_RTEXT|FREE_DATA)
32 * Selectable menu item.
34 #define mi_is_selectable(mi) (!((mi)->flags & NO_SELECT))
37 * Menu item has left text.
39 #define mi_has_left_text(mi) ((mi)->text && *(mi)->text)
42 * Menu item has right text.
44 #define mi_has_right_text(mi) ((mi)->rtext && *(mi)->rtext)
47 * Horizontal bar
49 #define mi_is_horizontal_bar(mi) (!mi_is_selectable(mi) && (mi)->text && !(mi)->text[0])
52 * Submenu item
54 #define mi_is_submenu(mi) ((mi)->flags & SUBMENU)
57 * Texts should be translated or not.
59 #define mi_text_translate(mi) (!((mi)->flags & NO_INTL))
60 #define mi_rtext_translate(mi) ((mi)->flags & RIGHT_INTL)
63 * End of menu items list
65 #define mi_is_end_of_menu(mi) (!(mi)->text)
67 #define foreach_menu_item(iterator, items) \
68 for (iterator = (items); !mi_is_end_of_menu(iterator); (iterator)++)
70 enum hotkey_state {
71 HKS_SHOW = 0,
72 HKS_IGNORE,
73 HKS_CACHED,
76 /* XXX: keep order of fields, there's some hard initializations for it. --Zas
78 struct menu_item {
79 unsigned char *text; /* The item label */
81 /* The following three members are tightly coupled:
83 * - If @action is not MAIN_ACT_NONE the associated keybinding will be
84 * shown as the guiding right text and do_action() will be called
85 * when the item is selected rendering both @rtext and @func useless.
87 * - A few places however there is no associated keybinding and no
88 * ``default'' handler defined in which case @rtext (if non NULL)
89 * will be drawn and @func will be called when selecting the item. */
90 unsigned char *rtext; /* Right aligned guiding text */
91 enum main_action action_id; /* Default item handlers */
92 menu_func_T func; /* Called when selecting the item */
94 void *data; /* Private data passed to handler */
95 enum menu_item_flags flags; /* What to free() and display */
97 /* If true, don't try to translate text/rtext inside of the menu
98 * routines. */
99 enum hotkey_state hotkey_state; /* The state of the hotkey caching */
100 int hotkey_pos; /* The offset of the hotkey in @text */
103 #define INIT_MENU_ITEM(text, rtext, action_id, func, data, flags) \
105 (unsigned char *) (text), \
106 (unsigned char *) (rtext), \
107 (action_id), \
108 (func), \
109 (void *) (data), \
110 (flags), \
111 HKS_SHOW, \
115 #define INIT_MENU_ACTION(text, action_id) \
116 INIT_MENU_ITEM(text, NULL, action_id, NULL, NULL, 0)
118 #define NULL_MENU_ITEM \
119 INIT_MENU_ITEM(NULL, NULL, ACT_MAIN_NONE, NULL, NULL, 0)
121 #define BAR_MENU_ITEM \
122 INIT_MENU_ITEM("", NULL, ACT_MAIN_NONE, NULL, NULL, NO_SELECT)
124 #define SET_MENU_ITEM(e_, text_, rtext_, action_id_, func_, data_, \
125 flags_, hotkey_state_, hotkey_pos_) \
126 do { \
127 (e_)->text = (unsigned char *) (text_); \
128 (e_)->rtext = (unsigned char *) (rtext_); \
129 (e_)->action_id = (action_id_); \
130 (e_)->func = (func_); \
131 (e_)->data = (void *) (data_); \
132 (e_)->flags = (flags_); \
133 (e_)->hotkey_state = (hotkey_state_); \
134 (e_)->hotkey_pos = (hotkey_pos_); \
135 } while (0)
138 struct menu {
139 struct window *win; /* The terminal window the menu lives in */
141 struct menu_item *items;/* The items in the menu */
142 int size; /* The number of menu items */
144 int selected; /* The current selected item. -1 means none */
145 int first, last; /* The first and last visible menu items */
147 struct box box; /* The visible area of the menu */
148 int parent_x, parent_y; /* The coordinates of the parent window */
150 int hotkeys; /* Whether to check and display hotkeys */
151 #ifdef CONFIG_NLS
152 int lang; /* For keeping the hotkey cache in sync */
153 #endif
155 /* The private menu data that is passed as the 3. arg to the
156 * menu items' menu_func_T handler */
157 void *data;
161 struct menu_item *new_menu(enum menu_item_flags);
163 void
164 add_to_menu(struct menu_item **mi, unsigned char *text, unsigned char *rtext,
165 enum main_action action_id, menu_func_T func, void *data,
166 enum menu_item_flags flags);
168 #define add_menu_separator(menu) \
169 add_to_menu(menu, "", NULL, ACT_MAIN_NONE, NULL, NULL, NO_SELECT)
171 /* Implies that the action will be handled by do_action() */
172 #define add_menu_action(menu, text, action_id) \
173 add_to_menu(menu, text, NULL, action_id, NULL, NULL, NO_FLAG)
175 void do_menu(struct terminal *, struct menu_item *, void *, int);
176 void do_menu_selected(struct terminal *, struct menu_item *, void *, int, int);
177 void do_mainmenu(struct terminal *, struct menu_item *, void *, int);
178 void deselect_mainmenu(struct terminal *term, struct menu *menu);
180 #endif