Merge branch '3643_cons_handler_min_macro'
[midnight-commander.git] / lib / widget / widget-common.h
blob9cb9d13718f25fb701ca8665627458e3b839522e
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, W_WANT_CURSOR, i)
20 #define widget_want_hotkey(w,i) widget_set_options(w, W_WANT_HOTKEY, i)
21 #define widget_want_idle(w,i) widget_set_options(w, W_WANT_IDLE, i)
22 #define widget_disable(w,i) widget_set_options(w, W_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_DRAW, /* Draw widget on screen */
33 MSG_KEY, /* Sent to widgets on key press */
34 MSG_HOTKEY, /* Sent to widget to catch preprocess key */
35 MSG_HOTKEY_HANDLED, /* A widget has got the hotkey */
36 MSG_UNHANDLED_KEY, /* Key that no widget handled */
37 MSG_POST_KEY, /* The key has been handled */
38 MSG_ACTION, /* Send to widget to handle command */
39 MSG_NOTIFY, /* Typically sent to dialog to inform it of state-change
40 * of listboxes, check- and radiobuttons. */
41 MSG_CURSOR, /* Sent to widget to position the cursor */
42 MSG_IDLE, /* The idle state is active */
43 MSG_RESIZE, /* Screen size has changed */
44 MSG_VALIDATE, /* Dialog is to be closed */
45 MSG_END, /* Shut down dialog */
46 MSG_DESTROY /* Sent to widget at destruction time */
47 } widget_msg_t;
49 /* Widgets are expected to answer to the following messages:
50 MSG_FOCUS: MSG_HANDLED if the accept the focus, MSG_NOT_HANDLED if they do not.
51 MSG_UNFOCUS: MSG_HANDLED if they accept to release the focus, MSG_NOT_HANDLED if they don't.
52 MSG_KEY: MSG_HANDLED if they actually used the key, MSG_NOT_HANDLED if not.
53 MSG_HOTKEY: MSG_HANDLED if they actually used the key, MSG_NOT_HANDLED if not.
56 typedef enum
58 MSG_NOT_HANDLED = 0,
59 MSG_HANDLED = 1
60 } cb_ret_t;
62 /* Widget options */
63 typedef enum
65 W_DEFAULT = (0 << 0),
66 W_WANT_HOTKEY = (1 << 1),
67 W_WANT_CURSOR = (1 << 2),
68 W_WANT_IDLE = (1 << 3),
69 W_IS_INPUT = (1 << 4),
70 W_DISABLED = (1 << 5) /* Widget cannot be selected */
71 } widget_options_t;
73 /* Flags for widget repositioning on dialog resize */
74 typedef enum
76 WPOS_CENTER_HORZ = (1 << 0), /* center widget in horizontal */
77 WPOS_CENTER_VERT = (1 << 1), /* center widget in vertical */
78 WPOS_KEEP_LEFT = (1 << 2), /* keep widget distance to left border of dialog */
79 WPOS_KEEP_RIGHT = (1 << 3), /* keep widget distance to right border of dialog */
80 WPOS_KEEP_TOP = (1 << 4), /* keep widget distance to top border of dialog */
81 WPOS_KEEP_BOTTOM = (1 << 5), /* keep widget distance to bottom border of dialog */
82 WPOS_KEEP_HORZ = WPOS_KEEP_LEFT | WPOS_KEEP_RIGHT,
83 WPOS_KEEP_VERT = WPOS_KEEP_TOP | WPOS_KEEP_BOTTOM,
84 WPOS_KEEP_ALL = WPOS_KEEP_HORZ | WPOS_KEEP_VERT,
85 WPOS_KEEP_DEFAULT = WPOS_KEEP_LEFT | WPOS_KEEP_TOP
86 } widget_pos_flags_t;
87 /* NOTE: if WPOS_CENTER_HORZ flag is used, other horizontal flags (WPOS_KEEP_LEFT, WPOS_KEEP_RIGHT,
88 * and WPOS_KEEP_HORZ are ignored).
89 * If WPOS_CENTER_VERT flag is used, other horizontal flags (WPOS_KEEP_TOP, WPOS_KEEP_BOTTOM,
90 * and WPOS_KEEP_VERT are ignored).
93 /*** structures declarations (and typedefs of structures)*****************************************/
95 /* Widget callback */
96 typedef cb_ret_t (*widget_cb_fn) (Widget * widget, Widget * sender, widget_msg_t msg, int parm,
97 void *data);
98 /* Widget mouse callback */
99 typedef void (*widget_mouse_cb_fn) (Widget * w, mouse_msg_t msg, mouse_event_t * event);
101 /* Every Widget must have this as its first element */
102 struct Widget
104 int x, y;
105 int cols, lines;
106 widget_options_t options;
107 widget_pos_flags_t pos_flags; /* repositioning flags */
108 unsigned int id; /* Number of the widget, starting with 0 */
109 widget_cb_fn callback;
110 widget_mouse_cb_fn mouse_callback;
111 void (*set_options) (Widget * w, widget_options_t options, gboolean enable);
112 WDialog *owner;
113 /* Mouse-related fields. */
114 struct
116 /* Public members: */
117 gboolean forced_capture; /* Overrides the 'capture' member. Set explicitly by the programmer. */
119 /* Implementation details: */
120 gboolean capture; /* Whether the widget "owns" the mouse. */
121 mouse_msg_t last_msg; /* The previous event type processed. */
122 int last_buttons_down;
123 } mouse;
126 /* structure for label (caption) with hotkey, if original text does not contain
127 * hotkey, only start is valid and is equal to original text
128 * hotkey is defined as char*, but mc support only singlebyte hotkey
130 typedef struct hotkey_t
132 char *start;
133 char *hotkey;
134 char *end;
135 } hotkey_t;
137 /*** global variables defined in .c file *********************************************************/
139 /*** declarations of public functions ************************************************************/
141 /* create hotkey from text */
142 hotkey_t parse_hotkey (const char *text);
143 /* release hotkey, free all mebers of hotkey_t */
144 void release_hotkey (const hotkey_t hotkey);
145 /* return width on terminal of hotkey */
146 int hotkey_width (const hotkey_t hotkey);
147 /* draw hotkey of widget */
148 void hotkey_draw (Widget * w, const hotkey_t hotkey, gboolean focused);
150 /* widget initialization */
151 void widget_init (Widget * w, int y, int x, int lines, int cols,
152 widget_cb_fn callback, widget_mouse_cb_fn mouse_callback);
153 /* Default callback for widgets */
154 cb_ret_t widget_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm,
155 void *data);
156 void widget_default_set_options_callback (Widget * w, widget_options_t options, gboolean enable);
157 void widget_set_options (Widget * w, widget_options_t options, gboolean enable);
158 void widget_set_size (Widget * widget, int y, int x, int lines, int cols);
159 /* select color for widget in dependance of state */
160 void widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey);
161 void widget_redraw (Widget * w);
162 void widget_erase (Widget * w);
163 gboolean widget_is_active (const void *w);
164 gboolean widget_overlapped (const Widget * a, const Widget * b);
165 void widget_replace (Widget * old, Widget * new);
167 /* get mouse pointer location within widget */
168 Gpm_Event mouse_get_local (const Gpm_Event * global, const Widget * w);
169 gboolean mouse_global_in_widget (const Gpm_Event * event, const Widget * w);
171 /* --------------------------------------------------------------------------------------------- */
172 /*** inline functions ****************************************************************************/
173 /* --------------------------------------------------------------------------------------------- */
175 static inline cb_ret_t
176 send_message (void *w, void *sender, widget_msg_t msg, int parm, void *data)
178 cb_ret_t ret = MSG_NOT_HANDLED;
180 #if 1
181 if (w != NULL) /* This must be always true, but... */
182 #endif
183 ret = WIDGET (w)->callback (WIDGET (w), WIDGET (sender), msg, parm, data);
185 return ret;
188 /* --------------------------------------------------------------------------------------------- */
190 #endif /* MC__WIDGET_INTERNAL_H */