(enter): use GString instead of hand-made memory (re)allocation.
[midnight-commander.git] / lib / widget / widget-common.h
blobd5922e34eb42b5633905137568ec93ee9c52a4c3
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_move(w, _y, _x) tty_gotoyx (((Widget *)(w))->y + (_y), ((Widget *)(w))->x + (_x))
14 /* Sets/clear the specified flag in the options field */
15 #define widget_option(w,f,i) \
16 w.options = ((i) ? ((w).options | (f)) : ((w).options & (~(f))))
17 #define widget_want_cursor(w,i) widget_option((w), W_WANT_CURSOR, (i))
18 #define widget_want_hotkey(w,i) widget_option((w), W_WANT_HOTKEY, (i))
19 #define widget_disable(w,i) widget_option((w), W_DISABLED, (i))
21 /*** enums ***************************************************************************************/
23 /* Widget messages */
24 typedef enum
26 WIDGET_INIT, /* Initialize widget */
27 WIDGET_FOCUS, /* Draw widget in focused state */
28 WIDGET_UNFOCUS, /* Draw widget in unfocused state */
29 WIDGET_DRAW, /* Sent to widget to draw themselves */
30 WIDGET_KEY, /* Sent to widgets on key press */
31 WIDGET_HOTKEY, /* Sent to widget to catch preprocess key */
32 WIDGET_COMMAND, /* Send to widget to handle command */
33 WIDGET_DESTROY, /* Sent to widget at destruction time */
34 WIDGET_CURSOR, /* Sent to widget to position the cursor */
35 WIDGET_IDLE, /* Sent to widgets with options & W_WANT_IDLE */
36 WIDGET_RESIZED /* Sent after a widget has been resized */
37 } widget_msg_t;
39 /* Widgets are expected to answer to the following messages:
41 WIDGET_FOCUS: 1 if the accept the focus, 0 if they do not.
42 WIDGET_UNFOCUS: 1 if they accept to release the focus, 0 if they don't.
43 WIDGET_KEY: 1 if they actually used the key, 0 if not.
44 WIDGET_HOTKEY: 1 if they actually used the key, 0 if not.
47 typedef enum
49 MSG_NOT_HANDLED = 0,
50 MSG_HANDLED = 1
51 } cb_ret_t;
53 /* Widget options */
54 typedef enum
56 W_WANT_HOTKEY = (1 << 1),
57 W_WANT_CURSOR = (1 << 2),
58 W_WANT_IDLE = (1 << 3),
59 W_IS_INPUT = (1 << 4),
60 W_DISABLED = (1 << 5) /* Widget cannot be selected */
61 } widget_options_t;
63 /* Flags for widget repositioning on dialog resize */
64 typedef enum
66 WPOS_KEEP_LEFT = (1 << 0), /* keep widget distance to left border of dialog */
67 WPOS_KEEP_RIGHT = (1 << 1), /* keep widget distance to right border of dialog */
68 WPOS_KEEP_TOP = (1 << 2), /* keep widget distance to top border of dialog */
69 WPOS_KEEP_BOTTOM = (1 << 3), /* keep widget distance to bottom border of dialog */
70 WPOS_KEEP_HORZ = WPOS_KEEP_LEFT | WPOS_KEEP_RIGHT,
71 WPOS_KEEP_VERT = WPOS_KEEP_TOP | WPOS_KEEP_BOTTOM,
72 WPOS_KEEP_ALL = WPOS_KEEP_HORZ | WPOS_KEEP_VERT
73 } widget_pos_flags_t;
75 /*** structures declarations (and typedefs of structures)*****************************************/
77 /* Widget callback */
78 typedef cb_ret_t (*callback_fn) (struct Widget * widget, widget_msg_t msg, int parm);
80 /* Every Widget must have this as its first element */
81 struct Widget
83 int x, y;
84 int cols, lines;
85 widget_options_t options;
86 widget_pos_flags_t pos_flags; /* repositioning flags */
87 unsigned int id; /* Number of the widget, starting with 0 */
88 callback_fn callback;
89 mouse_h mouse;
90 struct Dlg_head *owner;
93 /* structure for label (caption) with hotkey, if original text does not contain
94 * hotkey, only start is valid and is equal to original text
95 * hotkey is defined as char*, but mc support only singlebyte hotkey
97 typedef struct hotkey_t
99 char *start;
100 char *hotkey;
101 char *end;
102 } hotkey_t;
104 /*** global variables defined in .c file *********************************************************/
106 /*** declarations of public functions ************************************************************/
108 /* create hotkey from text */
109 hotkey_t parse_hotkey (const char *text);
110 /* release hotkey, free all mebers of hotkey_t */
111 void release_hotkey (const hotkey_t hotkey);
112 /* return width on terminal of hotkey */
113 int hotkey_width (const hotkey_t hotkey);
114 /* draw hotkey of widget */
115 void hotkey_draw (struct Widget *w, const hotkey_t hotkey, gboolean focused);
117 /* widget initialization */
118 void init_widget (Widget * w, int y, int x, int lines, int cols,
119 callback_fn callback, mouse_h mouse_handler);
120 /* Default callback for widgets */
121 cb_ret_t default_proc (widget_msg_t msg, int parm);
122 void widget_set_size (Widget * widget, int y, int x, int lines, int cols);
123 /* select color for widget in dependance of state */
124 void widget_selectcolor (struct Widget *w, gboolean focused, gboolean hotkey);
125 void widget_erase (Widget * w);
127 /* get mouse pointer location within widget */
128 Gpm_Event mouse_get_local (const Gpm_Event * global, const Widget * w);
129 gboolean mouse_global_in_widget (const Gpm_Event * event, const Widget * w);
131 /*** inline functions ****************************************************************************/
133 static inline cb_ret_t
134 send_message (Widget * w, widget_msg_t msg, int parm)
136 return w->callback (w, msg, parm);
139 #endif /* MC__WIDGET_INTERNAL_H */