Reimplement widget focus/selection.
[midnight-commander.git] / lib / widget / widget-common.h
blob540189ab89d8d77a8d69f8abacfd055c05e6a8d8
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 */
84 WST_FOCUSED = (1 << 3),
86 WST_CONSTRUCT = (1 << 15), /* Dialog has been constructed but not run yet */
87 WST_ACTIVE = (1 << 16), /* Dialog is visible and active */
88 WST_SUSPENDED = (1 << 17), /* Dialog is suspended */
89 WST_CLOSED = (1 << 18) /* Dialog is closed */
90 } widget_state_t;
92 /* Flags for widget repositioning on dialog resize */
93 typedef enum
95 WPOS_FULLSCREEN = (1 << 0), /* widget occupies the whole screen */
96 WPOS_CENTER_HORZ = (1 << 1), /* center widget in horizontal */
97 WPOS_CENTER_VERT = (1 << 2), /* center widget in vertical */
98 WPOS_CENTER = WPOS_CENTER_HORZ | WPOS_CENTER_VERT, /* center widget */
99 WPOS_TRYUP = (1 << 3), /* try to move two lines up the widget */
100 WPOS_KEEP_LEFT = (1 << 4), /* keep widget distance to left border of dialog */
101 WPOS_KEEP_RIGHT = (1 << 5), /* keep widget distance to right border of dialog */
102 WPOS_KEEP_TOP = (1 << 6), /* keep widget distance to top border of dialog */
103 WPOS_KEEP_BOTTOM = (1 << 7), /* keep widget distance to bottom border of dialog */
104 WPOS_KEEP_HORZ = WPOS_KEEP_LEFT | WPOS_KEEP_RIGHT,
105 WPOS_KEEP_VERT = WPOS_KEEP_TOP | WPOS_KEEP_BOTTOM,
106 WPOS_KEEP_ALL = WPOS_KEEP_HORZ | WPOS_KEEP_VERT,
107 WPOS_KEEP_DEFAULT = WPOS_KEEP_LEFT | WPOS_KEEP_TOP
108 } widget_pos_flags_t;
109 /* NOTES:
110 * If WPOS_FULLSCREEN is set then all other position flags are ignored.
111 * If WPOS_CENTER_HORZ flag is used, other horizontal flags (WPOS_KEEP_LEFT, WPOS_KEEP_RIGHT,
112 * and WPOS_KEEP_HORZ) are ignored.
113 * If WPOS_CENTER_VERT flag is used, other horizontal flags (WPOS_KEEP_TOP, WPOS_KEEP_BOTTOM,
114 * and WPOS_KEEP_VERT) are ignored.
117 /*** structures declarations (and typedefs of structures)*****************************************/
119 /* Widget callback */
120 typedef cb_ret_t (*widget_cb_fn) (Widget * widget, Widget * sender, widget_msg_t msg, int parm,
121 void *data);
122 /* Widget mouse callback */
123 typedef void (*widget_mouse_cb_fn) (Widget * w, mouse_msg_t msg, mouse_event_t * event);
125 /* Every Widget must have this as its first element */
126 struct Widget
128 int x, y;
129 int cols, lines;
130 widget_pos_flags_t pos_flags; /* repositioning flags */
131 widget_options_t options;
132 widget_state_t state;
133 unsigned int id; /* Number of the widget, starting with 0 */
134 widget_cb_fn callback;
135 widget_mouse_cb_fn mouse_callback;
136 WDialog *owner;
137 /* Mouse-related fields. */
138 struct
140 /* Public members: */
141 gboolean forced_capture; /* Overrides the 'capture' member. Set explicitly by the programmer. */
143 /* Implementation details: */
144 gboolean capture; /* Whether the widget "owns" the mouse. */
145 mouse_msg_t last_msg; /* The previous event type processed. */
146 int last_buttons_down;
147 } mouse;
150 /* structure for label (caption) with hotkey, if original text does not contain
151 * hotkey, only start is valid and is equal to original text
152 * hotkey is defined as char*, but mc support only singlebyte hotkey
154 typedef struct hotkey_t
156 char *start;
157 char *hotkey;
158 char *end;
159 } hotkey_t;
161 /*** global variables defined in .c file *********************************************************/
163 /*** declarations of public functions ************************************************************/
165 /* create hotkey from text */
166 hotkey_t parse_hotkey (const char *text);
167 /* release hotkey, free all mebers of hotkey_t */
168 void release_hotkey (const hotkey_t hotkey);
169 /* return width on terminal of hotkey */
170 int hotkey_width (const hotkey_t hotkey);
171 /* draw hotkey of widget */
172 void hotkey_draw (Widget * w, const hotkey_t hotkey, gboolean focused);
174 /* widget initialization */
175 void widget_init (Widget * w, int y, int x, int lines, int cols,
176 widget_cb_fn callback, widget_mouse_cb_fn mouse_callback);
177 /* Default callback for widgets */
178 cb_ret_t widget_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm,
179 void *data);
180 void widget_set_options (Widget * w, widget_options_t options, gboolean enable);
181 cb_ret_t widget_set_state (Widget * w, widget_state_t state, gboolean enable);
182 void widget_set_size (Widget * widget, int y, int x, int lines, int cols);
183 /* select color for widget in dependance of state */
184 void widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey);
185 void widget_redraw (Widget * w);
186 void widget_erase (Widget * w);
187 gboolean widget_is_active (const void *w);
188 gboolean widget_overlapped (const Widget * a, const Widget * b);
189 void widget_replace (Widget * old, Widget * new);
190 void widget_select (Widget * w);
191 void widget_set_bottom (Widget * w);
193 /* get mouse pointer location within widget */
194 Gpm_Event mouse_get_local (const Gpm_Event * global, const Widget * w);
195 gboolean mouse_global_in_widget (const Gpm_Event * event, const Widget * w);
197 /* --------------------------------------------------------------------------------------------- */
198 /*** inline functions ****************************************************************************/
199 /* --------------------------------------------------------------------------------------------- */
201 static inline cb_ret_t
202 send_message (void *w, void *sender, widget_msg_t msg, int parm, void *data)
204 cb_ret_t ret = MSG_NOT_HANDLED;
206 #if 1
207 if (w != NULL) /* This must be always true, but... */
208 #endif
209 ret = WIDGET (w)->callback (WIDGET (w), WIDGET (sender), msg, parm, data);
211 return ret;
214 /* --------------------------------------------------------------------------------------------- */
216 * Check whether one or several option flags are set or not.
217 * @param w widget
218 * @param options widget option flags
220 * @return TRUE if all requested option flags are set, FALSE otherwise.
223 static inline gboolean
224 widget_get_options (const Widget * w, widget_options_t options)
226 return ((w->options & options) == options);
229 /* --------------------------------------------------------------------------------------------- */
232 * Check whether one or several state flags are set or not.
233 * @param w widget
234 * @param state widget state flags
236 * @return TRUE if all requested state flags are set, FALSE otherwise.
239 static inline gboolean
240 widget_get_state (const Widget * w, widget_state_t state)
242 return ((w->state & state) == state);
245 /* --------------------------------------------------------------------------------------------- */
247 #endif /* MC__WIDGET_INTERNAL_H */