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