(widget_set_state): return cb_ret_t instead of gboolean.
[midnight-commander.git] / lib / widget / widget-common.h
blobc98fb72736aef2e3d5ad17da323ca8634719c6b9
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_want_tab(w,i) widget_set_options(w, WOP_WANT_TAB, i)
22 #define widget_idle(w,i) widget_set_state(w, WST_IDLE, i)
23 #define widget_disable(w,i) widget_set_state(w, WST_DISABLED, i)
25 /*** enums ***************************************************************************************/
27 /* Widget messages */
28 typedef enum
30 MSG_INIT = 0, /* Initialize widget */
31 MSG_FOCUS, /* Draw widget in focused state or widget has got focus */
32 MSG_UNFOCUS, /* Draw widget in unfocused state or widget has been unfocused */
33 MSG_ENABLE, /* Change state to enabled */
34 MSG_DISABLE, /* Change state to disabled */
35 MSG_DRAW, /* Draw widget on screen */
36 MSG_KEY, /* Sent to widgets on key press */
37 MSG_HOTKEY, /* Sent to widget to catch preprocess key */
38 MSG_HOTKEY_HANDLED, /* A widget has got the hotkey */
39 MSG_UNHANDLED_KEY, /* Key that no widget handled */
40 MSG_POST_KEY, /* The key has been handled */
41 MSG_ACTION, /* Send to widget to handle command */
42 MSG_NOTIFY, /* Typically sent to dialog to inform it of state-change
43 * of listboxes, check- and radiobuttons. */
44 MSG_CURSOR, /* Sent to widget to position the cursor */
45 MSG_IDLE, /* The idle state is active */
46 MSG_RESIZE, /* Screen size has changed */
47 MSG_VALIDATE, /* Dialog is to be closed */
48 MSG_END, /* Shut down dialog */
49 MSG_DESTROY /* Sent to widget at destruction time */
50 } widget_msg_t;
52 /* Widgets are expected to answer to the following messages:
53 MSG_FOCUS: MSG_HANDLED if the accept the focus, MSG_NOT_HANDLED if they do not.
54 MSG_UNFOCUS: MSG_HANDLED if they accept to release the focus, MSG_NOT_HANDLED if they don't.
55 MSG_KEY: MSG_HANDLED if they actually used the key, MSG_NOT_HANDLED if not.
56 MSG_HOTKEY: MSG_HANDLED if they actually used the key, MSG_NOT_HANDLED if not.
59 typedef enum
61 MSG_NOT_HANDLED = 0,
62 MSG_HANDLED = 1
63 } cb_ret_t;
65 /* Widget options */
66 typedef enum
68 WOP_DEFAULT = (0 << 0),
69 WOP_WANT_HOTKEY = (1 << 0),
70 WOP_WANT_CURSOR = (1 << 1),
71 WOP_WANT_TAB = (1 << 2), /* Should the tab key be sent to the dialog? */
72 WOP_IS_INPUT = (1 << 3),
73 WOP_SELECTABLE = (1 << 4),
74 WOP_TOP_SELECT = (1 << 5)
75 } widget_options_t;
77 /* Widget state */
78 typedef enum
80 WST_DEFAULT = (0 << 0),
81 WST_DISABLED = (1 << 0), /* Widget cannot be selected */
82 WST_IDLE = (1 << 1),
83 WST_MODAL = (1 << 2), /* Widget (dialog) is modal */
85 WST_CONSTRUCT = (1 << 15), /* Dialog has been constructed but not run yet */
86 WST_ACTIVE = (1 << 16), /* Dialog is visible and active */
87 WST_SUSPENDED = (1 << 17), /* Dialog is suspended */
88 WST_CLOSED = (1 << 18) /* Dialog is closed */
89 } widget_state_t;
91 /* Flags for widget repositioning on dialog resize */
92 typedef enum
94 WPOS_FULLSCREEN = (1 << 0), /* widget occupies the whole screen */
95 WPOS_CENTER_HORZ = (1 << 1), /* center widget in horizontal */
96 WPOS_CENTER_VERT = (1 << 2), /* center widget in vertical */
97 WPOS_CENTER = WPOS_CENTER_HORZ | WPOS_CENTER_VERT, /* center widget */
98 WPOS_TRYUP = (1 << 3), /* try to move two lines up the widget */
99 WPOS_KEEP_LEFT = (1 << 4), /* keep widget distance to left border of dialog */
100 WPOS_KEEP_RIGHT = (1 << 5), /* keep widget distance to right border of dialog */
101 WPOS_KEEP_TOP = (1 << 6), /* keep widget distance to top border of dialog */
102 WPOS_KEEP_BOTTOM = (1 << 7), /* keep widget distance to bottom border of dialog */
103 WPOS_KEEP_HORZ = WPOS_KEEP_LEFT | WPOS_KEEP_RIGHT,
104 WPOS_KEEP_VERT = WPOS_KEEP_TOP | WPOS_KEEP_BOTTOM,
105 WPOS_KEEP_ALL = WPOS_KEEP_HORZ | WPOS_KEEP_VERT,
106 WPOS_KEEP_DEFAULT = WPOS_KEEP_LEFT | WPOS_KEEP_TOP
107 } widget_pos_flags_t;
108 /* NOTES:
109 * If WPOS_FULLSCREEN is set then all other position flags are ignored.
110 * If WPOS_CENTER_HORZ flag is used, other horizontal flags (WPOS_KEEP_LEFT, WPOS_KEEP_RIGHT,
111 * and WPOS_KEEP_HORZ) are ignored.
112 * If WPOS_CENTER_VERT flag is used, other horizontal flags (WPOS_KEEP_TOP, WPOS_KEEP_BOTTOM,
113 * and WPOS_KEEP_VERT) are ignored.
116 /*** structures declarations (and typedefs of structures)*****************************************/
118 /* Widget callback */
119 typedef cb_ret_t (*widget_cb_fn) (Widget * widget, Widget * sender, widget_msg_t msg, int parm,
120 void *data);
121 /* Widget mouse callback */
122 typedef void (*widget_mouse_cb_fn) (Widget * w, mouse_msg_t msg, mouse_event_t * event);
124 /* Every Widget must have this as its first element */
125 struct Widget
127 int x, y;
128 int cols, lines;
129 widget_pos_flags_t pos_flags; /* repositioning flags */
130 widget_options_t options;
131 widget_state_t state;
132 unsigned int id; /* Number of the widget, starting with 0 */
133 widget_cb_fn callback;
134 widget_mouse_cb_fn mouse_callback;
135 WDialog *owner;
136 /* Mouse-related fields. */
137 struct
139 /* Public members: */
140 gboolean forced_capture; /* Overrides the 'capture' member. Set explicitly by the programmer. */
142 /* Implementation details: */
143 gboolean capture; /* Whether the widget "owns" the mouse. */
144 mouse_msg_t last_msg; /* The previous event type processed. */
145 int last_buttons_down;
146 } mouse;
149 /* structure for label (caption) with hotkey, if original text does not contain
150 * hotkey, only start is valid and is equal to original text
151 * hotkey is defined as char*, but mc support only singlebyte hotkey
153 typedef struct hotkey_t
155 char *start;
156 char *hotkey;
157 char *end;
158 } hotkey_t;
160 /*** global variables defined in .c file *********************************************************/
162 /*** declarations of public functions ************************************************************/
164 /* create hotkey from text */
165 hotkey_t parse_hotkey (const char *text);
166 /* release hotkey, free all mebers of hotkey_t */
167 void release_hotkey (const hotkey_t hotkey);
168 /* return width on terminal of hotkey */
169 int hotkey_width (const hotkey_t hotkey);
170 /* draw hotkey of widget */
171 void hotkey_draw (Widget * w, const hotkey_t hotkey, gboolean focused);
173 /* widget initialization */
174 void widget_init (Widget * w, int y, int x, int lines, int cols,
175 widget_cb_fn callback, widget_mouse_cb_fn mouse_callback);
176 /* Default callback for widgets */
177 cb_ret_t widget_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm,
178 void *data);
179 void widget_set_options (Widget * w, widget_options_t options, gboolean enable);
180 cb_ret_t widget_set_state (Widget * w, widget_state_t state, gboolean enable);
181 void widget_set_size (Widget * widget, int y, int x, int lines, int cols);
182 /* select color for widget in dependance of state */
183 void widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey);
184 void widget_redraw (Widget * w);
185 void widget_erase (Widget * w);
186 gboolean widget_is_active (const void *w);
187 gboolean widget_overlapped (const Widget * a, const Widget * b);
188 void widget_replace (Widget * old, Widget * new);
190 /* get mouse pointer location within widget */
191 Gpm_Event mouse_get_local (const Gpm_Event * global, const Widget * w);
192 gboolean mouse_global_in_widget (const Gpm_Event * event, const Widget * w);
194 /* --------------------------------------------------------------------------------------------- */
195 /*** inline functions ****************************************************************************/
196 /* --------------------------------------------------------------------------------------------- */
198 static inline cb_ret_t
199 send_message (void *w, void *sender, widget_msg_t msg, int parm, void *data)
201 cb_ret_t ret = MSG_NOT_HANDLED;
203 #if 1
204 if (w != NULL) /* This must be always true, but... */
205 #endif
206 ret = WIDGET (w)->callback (WIDGET (w), WIDGET (sender), msg, parm, data);
208 return ret;
211 /* --------------------------------------------------------------------------------------------- */
213 * Check whether one or several option flags are set or not.
214 * @param w widget
215 * @param options widget option flags
217 * @return TRUE if all requested option flags are set, FALSE otherwise.
220 static inline gboolean
221 widget_get_options (const Widget * w, widget_options_t options)
223 return ((w->options & options) == options);
226 /* --------------------------------------------------------------------------------------------- */
229 * Check whether one or several state flags are set or not.
230 * @param w widget
231 * @param state widget state flags
233 * @return TRUE if all requested state flags are set, FALSE otherwise.
236 static inline gboolean
237 widget_get_state (const Widget * w, widget_state_t state)
239 return ((w->state & state) == state);
242 /* --------------------------------------------------------------------------------------------- */
244 #endif /* MC__WIDGET_INTERNAL_H */