Drop old mouse API and use the new one.
[midnight-commander.git] / lib / widget / widget-common.h
blobbe524d1ffd06f8e2e3adae53cdfecfcdbf9ddd3e
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))
16 #define widget_move(w, _y, _x) tty_gotoyx (WIDGET(w)->y + (_y), WIDGET(w)->x + (_x))
17 /* Sets/clear the specified flag in the options field */
18 #define widget_want_cursor(w,i) widget_set_options(w, W_WANT_CURSOR, i)
19 #define widget_want_hotkey(w,i) widget_set_options(w, W_WANT_HOTKEY, i)
20 #define widget_want_idle(w,i) widget_set_options(w, W_WANT_IDLE, i)
21 #define widget_disable(w,i) widget_set_options(w, W_DISABLED, i)
23 /*** enums ***************************************************************************************/
25 /* Widget messages */
26 typedef enum
28 MSG_INIT = 0, /* Initialize widget */
29 MSG_FOCUS, /* Draw widget in focused state or widget has got focus */
30 MSG_UNFOCUS, /* Draw widget in unfocused state or widget has been unfocused */
31 MSG_DRAW, /* Draw widget on screen */
32 MSG_KEY, /* Sent to widgets on key press */
33 MSG_HOTKEY, /* Sent to widget to catch preprocess key */
34 MSG_HOTKEY_HANDLED, /* A widget has got the hotkey */
35 MSG_UNHANDLED_KEY, /* Key that no widget handled */
36 MSG_POST_KEY, /* The key has been handled */
37 MSG_ACTION, /* Send to widget to handle command */
38 MSG_NOTIFY, /* Typically sent to dialog to inform it of state-change
39 * of listboxes, check- and radiobuttons. */
40 MSG_CURSOR, /* Sent to widget to position the cursor */
41 MSG_IDLE, /* The idle state is active */
42 MSG_RESIZE, /* Screen size has changed */
43 MSG_VALIDATE, /* Dialog is to be closed */
44 MSG_END, /* Shut down dialog */
45 MSG_DESTROY /* Sent to widget at destruction time */
46 } widget_msg_t;
48 /* Widgets are expected to answer to the following messages:
49 MSG_FOCUS: MSG_HANDLED if the accept the focus, MSG_NOT_HANDLED if they do not.
50 MSG_UNFOCUS: MSG_HANDLED if they accept to release the focus, MSG_NOT_HANDLED if they don't.
51 MSG_KEY: MSG_HANDLED if they actually used the key, MSG_NOT_HANDLED if not.
52 MSG_HOTKEY: MSG_HANDLED if they actually used the key, MSG_NOT_HANDLED if not.
55 typedef enum
57 MSG_NOT_HANDLED = 0,
58 MSG_HANDLED = 1
59 } cb_ret_t;
61 /* Widget options */
62 typedef enum
64 W_DEFAULT = (0 << 0),
65 W_WANT_HOTKEY = (1 << 1),
66 W_WANT_CURSOR = (1 << 2),
67 W_WANT_IDLE = (1 << 3),
68 W_IS_INPUT = (1 << 4),
69 W_DISABLED = (1 << 5) /* Widget cannot be selected */
70 } widget_options_t;
72 /* Flags for widget repositioning on dialog resize */
73 typedef enum
75 WPOS_CENTER_HORZ = (1 << 0), /* center widget in horizontal */
76 WPOS_CENTER_VERT = (1 << 1), /* center widget in vertical */
77 WPOS_KEEP_LEFT = (1 << 2), /* keep widget distance to left border of dialog */
78 WPOS_KEEP_RIGHT = (1 << 3), /* keep widget distance to right border of dialog */
79 WPOS_KEEP_TOP = (1 << 4), /* keep widget distance to top border of dialog */
80 WPOS_KEEP_BOTTOM = (1 << 5), /* keep widget distance to bottom border of dialog */
81 WPOS_KEEP_HORZ = WPOS_KEEP_LEFT | WPOS_KEEP_RIGHT,
82 WPOS_KEEP_VERT = WPOS_KEEP_TOP | WPOS_KEEP_BOTTOM,
83 WPOS_KEEP_ALL = WPOS_KEEP_HORZ | WPOS_KEEP_VERT,
84 WPOS_KEEP_DEFAULT = WPOS_KEEP_LEFT | WPOS_KEEP_TOP
85 } widget_pos_flags_t;
86 /* NOTE: if WPOS_CENTER_HORZ flag is used, other horizontal flags (WPOS_KEEP_LEFT, WPOS_KEEP_RIGHT,
87 * and WPOS_KEEP_HORZ are ignored).
88 * If WPOS_CENTER_VERT flag is used, other horizontal flags (WPOS_KEEP_TOP, WPOS_KEEP_BOTTOM,
89 * and WPOS_KEEP_VERT are ignored).
92 /*** structures declarations (and typedefs of structures)*****************************************/
94 /* Widget callback */
95 typedef cb_ret_t (*widget_cb_fn) (Widget * widget, Widget * sender, widget_msg_t msg, int parm,
96 void *data);
97 /* Widget mouse callback */
98 typedef void (*widget_mouse_cb_fn) (Widget * w, mouse_msg_t msg, mouse_event_t * event);
100 /* Every Widget must have this as its first element */
101 struct Widget
103 int x, y;
104 int cols, lines;
105 widget_options_t options;
106 widget_pos_flags_t pos_flags; /* repositioning flags */
107 unsigned int id; /* Number of the widget, starting with 0 */
108 widget_cb_fn callback;
109 widget_mouse_cb_fn mouse_callback;
110 void (*set_options) (Widget * w, widget_options_t options, gboolean enable);
111 WDialog *owner;
112 /* Mouse-related fields. */
113 struct
115 gboolean capture; /* Whether the widget "owns" the mouse. */
116 gboolean forced_capture; /* Overrides the above. Set explicitly by the programmer. */
117 int last_buttons_down;
118 gboolean was_drag;
119 } mouse;
122 /* structure for label (caption) with hotkey, if original text does not contain
123 * hotkey, only start is valid and is equal to original text
124 * hotkey is defined as char*, but mc support only singlebyte hotkey
126 typedef struct hotkey_t
128 char *start;
129 char *hotkey;
130 char *end;
131 } hotkey_t;
133 /*** global variables defined in .c file *********************************************************/
135 /*** declarations of public functions ************************************************************/
137 /* create hotkey from text */
138 hotkey_t parse_hotkey (const char *text);
139 /* release hotkey, free all mebers of hotkey_t */
140 void release_hotkey (const hotkey_t hotkey);
141 /* return width on terminal of hotkey */
142 int hotkey_width (const hotkey_t hotkey);
143 /* draw hotkey of widget */
144 void hotkey_draw (Widget * w, const hotkey_t hotkey, gboolean focused);
146 /* widget initialization */
147 void widget_init (Widget * w, int y, int x, int lines, int cols,
148 widget_cb_fn callback, widget_mouse_cb_fn mouse_callback);
149 /* Default callback for widgets */
150 cb_ret_t widget_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm,
151 void *data);
152 void widget_default_set_options_callback (Widget * w, widget_options_t options, gboolean enable);
153 void widget_set_options (Widget * w, widget_options_t options, gboolean enable);
154 void widget_set_size (Widget * widget, int y, int x, int lines, int cols);
155 /* select color for widget in dependance of state */
156 void widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey);
157 void widget_redraw (Widget * w);
158 void widget_erase (Widget * w);
159 gboolean widget_is_active (const void *w);
160 gboolean widget_overlapped (const Widget * a, const Widget * b);
161 void widget_replace (Widget * old, Widget * new);
163 /* get mouse pointer location within widget */
164 Gpm_Event mouse_get_local (const Gpm_Event * global, const Widget * w);
165 gboolean mouse_global_in_widget (const Gpm_Event * event, const Widget * w);
167 /* --------------------------------------------------------------------------------------------- */
168 /*** inline functions ****************************************************************************/
169 /* --------------------------------------------------------------------------------------------- */
171 static inline cb_ret_t
172 send_message (void *w, void *sender, widget_msg_t msg, int parm, void *data)
174 cb_ret_t ret = MSG_NOT_HANDLED;
176 #if 1
177 if (w != NULL) /* This must be always true, but... */
178 #endif
179 ret = WIDGET (w)->callback (WIDGET (w), WIDGET (sender), msg, parm, data);
181 return ret;
184 /* --------------------------------------------------------------------------------------------- */
186 #endif /* MC__WIDGET_INTERNAL_H */