By default, WOP_WANT_HOTKEY option is off.
[midnight-commander.git] / lib / widget / widget-common.h
blob1bc3fdb34675af612bbf2479e0bafc15a8919669
2 /** \file widget-common.h
3 * \brief Header: shared stuff of widgets
4 */
6 #ifndef MC__WIDGET_INTERNAL_H
7 #define MC__WIDGET_INTERNAL_H
9 #include "lib/tty/mouse.h"
10 #include "lib/widget/mouse.h" /* mouse_msg_t, mouse_event_t */
12 /*** typedefs(not structures) and defined constants **********************************************/
14 #define WIDGET(x) ((Widget *)(x))
15 #define CONST_WIDGET(x) ((const Widget *)(x))
17 #define widget_move(w, _y, _x) tty_gotoyx (CONST_WIDGET(w)->y + (_y), CONST_WIDGET(w)->x + (_x))
18 /* Sets/clear the specified flag in the options field */
19 #define widget_want_cursor(w,i) widget_set_options(w, WOP_WANT_CURSOR, i)
20 #define widget_want_hotkey(w,i) widget_set_options(w, WOP_WANT_HOTKEY, i)
21 #define widget_idle(w,i) widget_set_state(w, WST_IDLE, i)
22 #define widget_disable(w,i) widget_set_state(w, WST_DISABLED, i)
24 /*** enums ***************************************************************************************/
26 /* Widget messages */
27 typedef enum
29 MSG_INIT = 0, /* Initialize widget */
30 MSG_FOCUS, /* Draw widget in focused state or widget has got focus */
31 MSG_UNFOCUS, /* Draw widget in unfocused state or widget has been unfocused */
32 MSG_ENABLE, /* Change state to enabled */
33 MSG_DISABLE, /* Change state to disabled */
34 MSG_DRAW, /* Draw widget on screen */
35 MSG_KEY, /* Sent to widgets on key press */
36 MSG_HOTKEY, /* Sent to widget to catch preprocess key */
37 MSG_HOTKEY_HANDLED, /* A widget has got the hotkey */
38 MSG_UNHANDLED_KEY, /* Key that no widget handled */
39 MSG_POST_KEY, /* The key has been handled */
40 MSG_ACTION, /* Send to widget to handle command */
41 MSG_NOTIFY, /* Typically sent to dialog to inform it of state-change
42 * of listboxes, check- and radiobuttons. */
43 MSG_CURSOR, /* Sent to widget to position the cursor */
44 MSG_IDLE, /* The idle state is active */
45 MSG_RESIZE, /* Screen size has changed */
46 MSG_VALIDATE, /* Dialog is to be closed */
47 MSG_END, /* Shut down dialog */
48 MSG_DESTROY /* Sent to widget at destruction time */
49 } widget_msg_t;
51 /* Widgets are expected to answer to the following messages:
52 MSG_FOCUS: MSG_HANDLED if the accept the focus, MSG_NOT_HANDLED if they do not.
53 MSG_UNFOCUS: MSG_HANDLED if they accept to release the focus, MSG_NOT_HANDLED if they don't.
54 MSG_KEY: MSG_HANDLED if they actually used the key, MSG_NOT_HANDLED if not.
55 MSG_HOTKEY: MSG_HANDLED if they actually used the key, MSG_NOT_HANDLED if not.
58 typedef enum
60 MSG_NOT_HANDLED = 0,
61 MSG_HANDLED = 1
62 } cb_ret_t;
64 /* Widget options */
65 typedef enum
67 WOP_DEFAULT = (0 << 0),
68 WOP_WANT_HOTKEY = (1 << 1),
69 WOP_WANT_CURSOR = (1 << 2),
70 WOP_IS_INPUT = (1 << 3),
71 WOP_TOP_SELECT = (1 << 4)
72 } widget_options_t;
74 /* Widget state */
75 typedef enum
77 WST_DEFAULT = (0 << 0),
78 WST_DISABLED = (1 << 0), /* Widget cannot be selected */
79 WST_IDLE = (1 << 1),
80 WST_MODAL = (1 << 2), /* Widget (dialog) is modal */
82 WST_CONSTRUCT = (1 << 15), /* Dialog has been constructed but not run yet */
83 WST_ACTIVE = (1 << 16), /* Dialog is visible and active */
84 WST_SUSPENDED = (1 << 17), /* Dialog is suspended */
85 WST_CLOSED = (1 << 18) /* Dialog is closed */
86 } widget_state_t;
88 /* Flags for widget repositioning on dialog resize */
89 typedef enum
91 WPOS_CENTER_HORZ = (1 << 0), /* center widget in horizontal */
92 WPOS_CENTER_VERT = (1 << 1), /* center widget in vertical */
93 WPOS_KEEP_LEFT = (1 << 2), /* keep widget distance to left border of dialog */
94 WPOS_KEEP_RIGHT = (1 << 3), /* keep widget distance to right border of dialog */
95 WPOS_KEEP_TOP = (1 << 4), /* keep widget distance to top border of dialog */
96 WPOS_KEEP_BOTTOM = (1 << 5), /* keep widget distance to bottom border of dialog */
97 WPOS_KEEP_HORZ = WPOS_KEEP_LEFT | WPOS_KEEP_RIGHT,
98 WPOS_KEEP_VERT = WPOS_KEEP_TOP | WPOS_KEEP_BOTTOM,
99 WPOS_KEEP_ALL = WPOS_KEEP_HORZ | WPOS_KEEP_VERT,
100 WPOS_KEEP_DEFAULT = WPOS_KEEP_LEFT | WPOS_KEEP_TOP
101 } widget_pos_flags_t;
102 /* NOTE: if WPOS_CENTER_HORZ flag is used, other horizontal flags (WPOS_KEEP_LEFT, WPOS_KEEP_RIGHT,
103 * and WPOS_KEEP_HORZ are ignored).
104 * If WPOS_CENTER_VERT flag is used, other horizontal flags (WPOS_KEEP_TOP, WPOS_KEEP_BOTTOM,
105 * and WPOS_KEEP_VERT are ignored).
108 /*** structures declarations (and typedefs of structures)*****************************************/
110 /* Widget callback */
111 typedef cb_ret_t (*widget_cb_fn) (Widget * widget, Widget * sender, widget_msg_t msg, int parm,
112 void *data);
113 /* Widget mouse callback */
114 typedef void (*widget_mouse_cb_fn) (Widget * w, mouse_msg_t msg, mouse_event_t * event);
116 /* Every Widget must have this as its first element */
117 struct Widget
119 int x, y;
120 int cols, lines;
121 widget_pos_flags_t pos_flags; /* repositioning flags */
122 widget_options_t options;
123 widget_state_t state;
124 unsigned int id; /* Number of the widget, starting with 0 */
125 widget_cb_fn callback;
126 widget_mouse_cb_fn mouse_callback;
127 WDialog *owner;
128 /* Mouse-related fields. */
129 struct
131 /* Public members: */
132 gboolean forced_capture; /* Overrides the 'capture' member. Set explicitly by the programmer. */
134 /* Implementation details: */
135 gboolean capture; /* Whether the widget "owns" the mouse. */
136 mouse_msg_t last_msg; /* The previous event type processed. */
137 int last_buttons_down;
138 } mouse;
141 /* structure for label (caption) with hotkey, if original text does not contain
142 * hotkey, only start is valid and is equal to original text
143 * hotkey is defined as char*, but mc support only singlebyte hotkey
145 typedef struct hotkey_t
147 char *start;
148 char *hotkey;
149 char *end;
150 } hotkey_t;
152 /*** global variables defined in .c file *********************************************************/
154 /*** declarations of public functions ************************************************************/
156 /* create hotkey from text */
157 hotkey_t parse_hotkey (const char *text);
158 /* release hotkey, free all mebers of hotkey_t */
159 void release_hotkey (const hotkey_t hotkey);
160 /* return width on terminal of hotkey */
161 int hotkey_width (const hotkey_t hotkey);
162 /* draw hotkey of widget */
163 void hotkey_draw (Widget * w, const hotkey_t hotkey, gboolean focused);
165 /* widget initialization */
166 void widget_init (Widget * w, int y, int x, int lines, int cols,
167 widget_cb_fn callback, widget_mouse_cb_fn mouse_callback);
168 /* Default callback for widgets */
169 cb_ret_t widget_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm,
170 void *data);
171 void widget_set_options (Widget * w, widget_options_t options, gboolean enable);
172 gboolean widget_set_state (Widget * w, widget_state_t state, gboolean enable);
173 void widget_set_size (Widget * widget, int y, int x, int lines, int cols);
174 /* select color for widget in dependance of state */
175 void widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey);
176 void widget_redraw (Widget * w);
177 void widget_erase (Widget * w);
178 gboolean widget_is_active (const void *w);
179 gboolean widget_overlapped (const Widget * a, const Widget * b);
180 void widget_replace (Widget * old, Widget * new);
182 /* get mouse pointer location within widget */
183 Gpm_Event mouse_get_local (const Gpm_Event * global, const Widget * w);
184 gboolean mouse_global_in_widget (const Gpm_Event * event, const Widget * w);
186 /* --------------------------------------------------------------------------------------------- */
187 /*** inline functions ****************************************************************************/
188 /* --------------------------------------------------------------------------------------------- */
190 static inline cb_ret_t
191 send_message (void *w, void *sender, widget_msg_t msg, int parm, void *data)
193 cb_ret_t ret = MSG_NOT_HANDLED;
195 #if 1
196 if (w != NULL) /* This must be always true, but... */
197 #endif
198 ret = WIDGET (w)->callback (WIDGET (w), WIDGET (sender), msg, parm, data);
200 return ret;
203 /* --------------------------------------------------------------------------------------------- */
205 * Check whether one or several option flags are set or not.
206 * @param w widget
207 * @param options widget option flags
209 * @return TRUE if all requested option flags are set, FALSE otherwise.
212 static inline gboolean
213 widget_get_options (const Widget * w, widget_options_t options)
215 return ((w->options & options) == options);
218 /* --------------------------------------------------------------------------------------------- */
221 * Check whether one or several state flags are set or not.
222 * @param w widget
223 * @param state widget state flags
225 * @return TRUE if all requested state flags are set, FALSE otherwise.
228 static inline gboolean
229 widget_get_state (const Widget * w, widget_state_t state)
231 return ((w->state & state) == state);
234 /* --------------------------------------------------------------------------------------------- */
236 #endif /* MC__WIDGET_INTERNAL_H */