1
2 /** \file widget-common.h
3 * \brief Header: shared stuff of widgets
4 */
5
6 #ifndef MC__WIDGET_INTERNAL_H
7 #define MC__WIDGET_INTERNAL_H
8
9 #include "lib/keybind.h" /* global_keymap_t */
10 #include "lib/tty/mouse.h"
11 #include "lib/widget/mouse.h" /* mouse_msg_t, mouse_event_t */
12
13 /*** typedefs(not structures) and defined constants **********************************************/
14
15 #define WIDGET(x) ((Widget *)(x))
16 #define CONST_WIDGET(x) ((const Widget *)(x))
17
18 #define widget_gotoyx(w, _y, _x) tty_gotoyx (CONST_WIDGET(w)->y + (_y), CONST_WIDGET(w)->x + (_x))
19 /* Sets/clear the specified flag in the options field */
20 #define widget_want_cursor(w,i) widget_set_options(w, WOP_WANT_CURSOR, i)
21 #define widget_want_hotkey(w,i) widget_set_options(w, WOP_WANT_HOTKEY, i)
22 #define widget_want_tab(w,i) widget_set_options(w, WOP_WANT_TAB, i)
23 #define widget_idle(w,i) widget_set_state(w, WST_IDLE, i)
24 #define widget_disable(w,i) widget_set_state(w, WST_DISABLED, i)
25
26 /*** enums ***************************************************************************************/
27
28 /* Widget messages */
29 typedef enum
30 {
31 MSG_INIT = 0, /* Initialize widget */
32 MSG_FOCUS, /* Draw widget in focused state or widget has got focus */
33 MSG_UNFOCUS, /* Draw widget in unfocused state or widget has been unfocused */
34 MSG_CHANGED_FOCUS, /* Notification to owner about focus state change */
35 MSG_ENABLE, /* Change state to enabled */
36 MSG_DISABLE, /* Change state to disabled */
37 MSG_DRAW, /* Draw widget on screen */
38 MSG_KEY, /* Sent to widgets on key press */
39 MSG_HOTKEY, /* Sent to widget to catch preprocess key */
40 MSG_HOTKEY_HANDLED, /* A widget has got the hotkey */
41 MSG_UNHANDLED_KEY, /* Key that no widget handled */
42 MSG_POST_KEY, /* The key has been handled */
43 MSG_ACTION, /* Send to widget to handle command */
44 MSG_NOTIFY, /* Typically sent to dialog to inform it of state-change
45 * of listboxes, check- and radiobuttons. */
46 MSG_CURSOR, /* Sent to widget to position the cursor */
47 MSG_IDLE, /* The idle state is active */
48 MSG_RESIZE, /* Screen size has changed */
49 MSG_VALIDATE, /* Dialog is to be closed */
50 MSG_END, /* Shut down dialog */
51 MSG_DESTROY /* Sent to widget at destruction time */
52 } widget_msg_t;
53
54 /* Widgets are expected to answer to the following messages:
55 MSG_FOCUS: MSG_HANDLED if the accept the focus, MSG_NOT_HANDLED if they do not.
56 MSG_UNFOCUS: MSG_HANDLED if they accept to release the focus, MSG_NOT_HANDLED if they don't.
57 MSG_KEY: MSG_HANDLED if they actually used the key, MSG_NOT_HANDLED if not.
58 MSG_HOTKEY: MSG_HANDLED if they actually used the key, MSG_NOT_HANDLED if not.
59 */
60
61 typedef enum
62 {
63 MSG_NOT_HANDLED = 0,
64 MSG_HANDLED = 1
65 } cb_ret_t;
66
67 /* Widget options */
68 typedef enum
69 {
70 WOP_DEFAULT = (0 << 0),
71 WOP_WANT_HOTKEY = (1 << 0),
72 WOP_WANT_CURSOR = (1 << 1),
73 WOP_WANT_TAB = (1 << 2), /* Should the tab key be sent to the dialog? */
74 WOP_IS_INPUT = (1 << 3),
75 WOP_SELECTABLE = (1 << 4),
76 WOP_TOP_SELECT = (1 << 5)
77 } widget_options_t;
78
79 /* Widget state */
80 typedef enum
81 {
82 WST_DEFAULT = (0 << 0),
83 WST_VISIBLE = (1 << 0), /* Widget is visible */
84 WST_DISABLED = (1 << 1), /* Widget cannot be selected */
85 WST_IDLE = (1 << 2),
86 WST_MODAL = (1 << 3), /* Widget (dialog) is modal */
87 WST_FOCUSED = (1 << 4),
88
89 WST_CONSTRUCT = (1 << 15), /* Widget has been constructed but not run yet */
90 WST_ACTIVE = (1 << 16), /* Dialog is visible and active */
91 WST_SUSPENDED = (1 << 17), /* Dialog is suspended */
92 WST_CLOSED = (1 << 18) /* Dialog is closed */
93 } widget_state_t;
94
95 /* Flags for widget repositioning on dialog resize */
96 typedef enum
97 {
98 WPOS_FULLSCREEN = (1 << 0), /* widget occupies the whole screen */
99 WPOS_CENTER_HORZ = (1 << 1), /* center widget in horizontal */
100 WPOS_CENTER_VERT = (1 << 2), /* center widget in vertical */
101 WPOS_CENTER = WPOS_CENTER_HORZ | WPOS_CENTER_VERT, /* center widget */
102 WPOS_TRYUP = (1 << 3), /* try to move two lines up the widget */
103 WPOS_KEEP_LEFT = (1 << 4), /* keep widget distance to left border of dialog */
104 WPOS_KEEP_RIGHT = (1 << 5), /* keep widget distance to right border of dialog */
105 WPOS_KEEP_TOP = (1 << 6), /* keep widget distance to top border of dialog */
106 WPOS_KEEP_BOTTOM = (1 << 7), /* keep widget distance to bottom border of dialog */
107 WPOS_KEEP_HORZ = WPOS_KEEP_LEFT | WPOS_KEEP_RIGHT,
108 WPOS_KEEP_VERT = WPOS_KEEP_TOP | WPOS_KEEP_BOTTOM,
109 WPOS_KEEP_ALL = WPOS_KEEP_HORZ | WPOS_KEEP_VERT,
110 WPOS_KEEP_DEFAULT = WPOS_KEEP_LEFT | WPOS_KEEP_TOP
111 } widget_pos_flags_t;
112 /* NOTES:
113 * If WPOS_FULLSCREEN is set then all other position flags are ignored.
114 * If WPOS_CENTER_HORZ flag is used, other horizontal flags (WPOS_KEEP_LEFT, WPOS_KEEP_RIGHT,
115 * and WPOS_KEEP_HORZ) are ignored.
116 * If WPOS_CENTER_VERT flag is used, other horizontal flags (WPOS_KEEP_TOP, WPOS_KEEP_BOTTOM,
117 * and WPOS_KEEP_VERT) are ignored.
118 */
119
120 /*** structures declarations (and typedefs of structures)*****************************************/
121
122 /* Widget callback */
123 typedef cb_ret_t (*widget_cb_fn) (Widget * widget, Widget * sender, widget_msg_t msg, int parm,
124 void *data);
125 /* Widget mouse callback */
126 typedef void (*widget_mouse_cb_fn) (Widget * w, mouse_msg_t msg, mouse_event_t * event);
127 /* translate mouse event and process it */
128 typedef int (*widget_mouse_handle_fn) (Widget * w, Gpm_Event * event);
129
130 /* Every Widget must have this as its first element */
131 struct Widget
132 {
133 int x, y;
134 int cols, lines;
135 widget_pos_flags_t pos_flags; /* repositioning flags */
136 widget_options_t options;
137 widget_state_t state;
138 unsigned long id; /* uniq widget ID */
139 widget_cb_fn callback;
140 widget_mouse_cb_fn mouse_callback;
141 WGroup *owner;
142
143 /* Key-related fields */
144 const global_keymap_t *keymap; /* main keymap */
145 const global_keymap_t *ext_keymap; /* extended keymap */
146 gboolean ext_mode; /* use keymap or ext_keymap */
147
148 /* Mouse-related fields. */
149 widget_mouse_handle_fn mouse_handler;
150 struct
151 {
152 /* Public members: */
153 gboolean forced_capture; /* Overrides the 'capture' member. Set explicitly by the programmer. */
154
155 /* Implementation details: */
156 gboolean capture; /* Whether the widget "owns" the mouse. */
157 mouse_msg_t last_msg; /* The previous event type processed. */
158 int last_buttons_down;
159 } mouse;
160
161 void (*make_global) (Widget * w, const WRect * delta);
162 void (*make_local) (Widget * w, const WRect * delta);
163
164 GList *(*find) (const Widget * w, const Widget * what);
165 Widget *(*find_by_type) (const Widget * w, widget_cb_fn cb);
166 Widget *(*find_by_id) (const Widget * w, unsigned long id);
167
168 /* *INDENT-OFF* */
169 cb_ret_t (*set_state) (Widget * w, widget_state_t state, gboolean enable);
170 /* *INDENT-ON* */
171 void (*destroy) (Widget * w);
172
173 const int *(*get_colors) (const Widget * w);
174 };
175
176 /* structure for label (caption) with hotkey, if original text does not contain
177 * hotkey, only start is valid and is equal to original text
178 * hotkey is defined as char*, but mc support only singlebyte hotkey
179 */
180 typedef struct hotkey_t
181 {
182 char *start; /* never NULL */
183 char *hotkey; /* can be NULL */
184 char *end; /* can be NULL */
185 } hotkey_t;
186
187 /*** global variables defined in .c file *********************************************************/
188
189 /*** declarations of public functions ************************************************************/
190
191 /* create hotkey from text */
192 hotkey_t hotkey_new (const char *text);
193 /* release hotkey, free all mebers of hotkey_t */
194 void hotkey_free (const hotkey_t hotkey);
195 /* return width on terminal of hotkey */
196 int hotkey_width (const hotkey_t hotkey);
197 /* compare two hotkeys */
198 gboolean hotkey_equal (const hotkey_t hotkey1, const hotkey_t hotkey2);
199 /* draw hotkey of widget */
200 void hotkey_draw (Widget * w, const hotkey_t hotkey, gboolean focused);
201 /* get text of hotkey */
202 char *hotkey_get_text (const hotkey_t hotkey);
203
204 /* widget initialization */
205 void widget_init (Widget * w, int y, int x, int lines, int cols,
206 widget_cb_fn callback, widget_mouse_cb_fn mouse_callback);
207 /* Default callback for widgets */
208 cb_ret_t widget_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm,
209 void *data);
210 void widget_set_options (Widget * w, widget_options_t options, gboolean enable);
211 void widget_adjust_position (widget_pos_flags_t pos_flags, int *y, int *x, int *lines, int *cols);
212 void widget_set_size (Widget * w, int y, int x, int lines, int cols);
213 /* select color for widget in dependance of state */
214 void widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey);
215 cb_ret_t widget_draw (Widget * w);
216 void widget_erase (Widget * w);
217 void widget_set_visibility (Widget * w, gboolean make_visible);
218 gboolean widget_is_active (const void *w);
219 gboolean widget_overlapped (const Widget * a, const Widget * b);
220 void widget_replace (Widget * old, Widget * new);
221 gboolean widget_is_focusable (const Widget * w);
222 void widget_select (Widget * w);
223 void widget_set_bottom (Widget * w);
224
225 long widget_lookup_key (Widget * w, int key);
226
227 void widget_default_make_global (Widget * w, const WRect * delta);
228 void widget_default_make_local (Widget * w, const WRect * delta);
229
230 GList *widget_default_find (const Widget * w, const Widget * what);
231 Widget *widget_default_find_by_type (const Widget * w, widget_cb_fn cb);
232 Widget *widget_default_find_by_id (const Widget * w, unsigned long id);
233
234 cb_ret_t widget_default_set_state (Widget * w, widget_state_t state, gboolean enable);
235
236 void widget_default_destroy (Widget * w);
237
238 /* get mouse pointer location within widget */
239 Gpm_Event mouse_get_local (const Gpm_Event * global, const Widget * w);
240 gboolean mouse_global_in_widget (const Gpm_Event * event, const Widget * w);
241
242 /* --------------------------------------------------------------------------------------------- */
243 /*** inline functions ****************************************************************************/
244 /* --------------------------------------------------------------------------------------------- */
245
246 static inline cb_ret_t
send_message(void * w,void * sender,widget_msg_t msg,int parm,void * data)247 send_message (void *w, void *sender, widget_msg_t msg, int parm, void *data)
248 {
249 cb_ret_t ret = MSG_NOT_HANDLED;
250
251 #if 1
252 if (w != NULL) /* This must be always true, but... */
253 #endif
254 ret = WIDGET (w)->callback (WIDGET (w), WIDGET (sender), msg, parm, data);
255
256 return ret;
257 }
258
259 /* --------------------------------------------------------------------------------------------- */
260 /**
261 * Check whether one or several option flags are set or not.
262 * @param w widget
263 * @param options widget option flags
264 *
265 * @return TRUE if all requested option flags are set, FALSE otherwise.
266 */
267
268 static inline gboolean
widget_get_options(const Widget * w,widget_options_t options)269 widget_get_options (const Widget * w, widget_options_t options)
270 {
271 return ((w->options & options) == options);
272 }
273
274 /* --------------------------------------------------------------------------------------------- */
275
276 /**
277 * Check whether one or several state flags are set or not.
278 * @param w widget
279 * @param state widget state flags
280 *
281 * @return TRUE if all requested state flags are set, FALSE otherwise.
282 */
283
284 static inline gboolean
widget_get_state(const Widget * w,widget_state_t state)285 widget_get_state (const Widget * w, widget_state_t state)
286 {
287 return ((w->state & state) == state);
288 }
289
290 /* --------------------------------------------------------------------------------------------- */
291
292 /**
293 * Convert widget coordinates from local (relative to owner) to global (relative to screen).
294 *
295 * @param w widget
296 */
297
298 static inline void
widget_make_global(Widget * w)299 widget_make_global (Widget * w)
300 {
301 w->make_global (w, NULL);
302 }
303
304 /* --------------------------------------------------------------------------------------------- */
305
306 /**
307 * Convert widget coordinates from global (relative to screen) to local (relative to owner).
308 *
309 * @param w widget
310 */
311
312 static inline void
widget_make_local(Widget * w)313 widget_make_local (Widget * w)
314 {
315 w->make_local (w, NULL);
316 }
317
318 /* --------------------------------------------------------------------------------------------- */
319
320 /**
321 * Find widget.
322 *
323 * @param w widget
324 * @param what widget to find
325 *
326 * @return result of @w->find()
327 */
328
329 static inline GList *
widget_find(const Widget * w,const Widget * what)330 widget_find (const Widget * w, const Widget * what)
331 {
332 return w->find (w, what);
333 }
334
335 /* --------------------------------------------------------------------------------------------- */
336
337 /**
338 * Find widget by widget type using widget callback.
339 *
340 * @param w widget
341 * @param cb widget callback
342 *
343 * @return result of @w->find_by_type()
344 */
345
346 static inline Widget *
widget_find_by_type(const Widget * w,widget_cb_fn cb)347 widget_find_by_type (const Widget * w, widget_cb_fn cb)
348 {
349 return w->find_by_type (w, cb);
350 }
351
352 /* --------------------------------------------------------------------------------------------- */
353 /**
354 * Find widget by widget ID.
355 *
356 * @param w widget
357 * @param id widget ID
358 *
359 * @return result of @w->find_by_id()
360 */
361
362 static inline Widget *
widget_find_by_id(const Widget * w,unsigned long id)363 widget_find_by_id (const Widget * w, unsigned long id)
364 {
365 return w->find_by_id (w, id);
366 }
367
368 /* --------------------------------------------------------------------------------------------- */
369 /**
370 * Modify state of widget.
371 *
372 * @param w widget
373 * @param state widget state flag to modify
374 * @param enable specifies whether to turn the flag on (TRUE) or off (FALSE).
375 * Only one flag per call can be modified.
376 * @return MSG_HANDLED if set was handled successfully, MSG_NOT_HANDLED otherwise.
377 */
378
379 static inline cb_ret_t
widget_set_state(Widget * w,widget_state_t state,gboolean enable)380 widget_set_state (Widget * w, widget_state_t state, gboolean enable)
381 {
382 return w->set_state (w, state, enable);
383 }
384
385 /* --------------------------------------------------------------------------------------------- */
386 /**
387 * Destroy widget.
388 *
389 * @param w widget
390 */
391
392 static inline void
widget_destroy(Widget * w)393 widget_destroy (Widget * w)
394 {
395 w->destroy (w);
396 }
397
398 /* --------------------------------------------------------------------------------------------- */
399
400 /**
401 * Get color colors of widget.
402 *
403 * @param w widget
404 * @return color colors
405 */
406 static inline const int *
widget_get_colors(const Widget * w)407 widget_get_colors (const Widget * w)
408 {
409 return w->get_colors (w);
410 }
411
412 /* --------------------------------------------------------------------------------------------- */
413 /**
414 * Update cursor position in the specified widget.
415 *
416 * @param w widget
417 *
418 * @return TRUE if cursor was updated successfully, FALSE otherwise
419 */
420
421 static inline gboolean
widget_update_cursor(Widget * w)422 widget_update_cursor (Widget * w)
423 {
424 return (send_message (w, NULL, MSG_CURSOR, 0, NULL) == MSG_HANDLED);
425 }
426
427 /* --------------------------------------------------------------------------------------------- */
428
429 static inline void
widget_set_size_rect(Widget * w,const WRect * r)430 widget_set_size_rect (Widget * w, const WRect * r)
431 {
432 widget_set_size (w, r->y, r->x, r->lines, r->cols);
433 }
434
435 /* --------------------------------------------------------------------------------------------- */
436
437 static inline void
widget_show(Widget * w)438 widget_show (Widget * w)
439 {
440 widget_set_visibility (w, TRUE);
441 }
442
443 /* --------------------------------------------------------------------------------------------- */
444
445 static inline void
widget_hide(Widget * w)446 widget_hide (Widget * w)
447 {
448 widget_set_visibility (w, FALSE);
449 }
450
451 /* --------------------------------------------------------------------------------------------- */
452
453 #endif /* MC__WIDGET_INTERNAL_H */
454