widget-common.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /** \file widget-common.h
  2. * \brief Header: shared stuff of widgets
  3. */
  4. #ifndef MC__WIDGET_COMMON_H
  5. #define MC__WIDGET_COMMON_H
  6. #include "lib/keybind.h" /* global_keymap_t */
  7. #include "lib/tty/mouse.h"
  8. #include "lib/widget/mouse.h" /* mouse_msg_t, mouse_event_t */
  9. /*** typedefs(not structures) and defined constants **********************************************/
  10. #define WIDGET(x) ((Widget *) (x))
  11. #define CONST_WIDGET(x) ((const Widget *) (x))
  12. #define widget_gotoyx(w, _y, _x) \
  13. tty_gotoyx (CONST_WIDGET (w)->rect.y + (_y), CONST_WIDGET (w)->rect.x + (_x))
  14. /* Sets/clear the specified flag in the options field */
  15. #define widget_want_cursor(w, i) widget_set_options (w, WOP_WANT_CURSOR, i)
  16. #define widget_want_hotkey(w, i) widget_set_options (w, WOP_WANT_HOTKEY, i)
  17. #define widget_want_tab(w, i) widget_set_options (w, WOP_WANT_TAB, i)
  18. #define widget_idle(w, i) widget_set_state (w, WST_IDLE, i)
  19. #define widget_disable(w, i) widget_set_state (w, WST_DISABLED, i)
  20. /*** enums ***************************************************************************************/
  21. /* Widget messages */
  22. typedef enum
  23. {
  24. MSG_INIT = 0, /* Initialize widget */
  25. MSG_FOCUS, /* Draw widget in focused state or widget has got focus */
  26. MSG_UNFOCUS, /* Draw widget in unfocused state or widget has been unfocused */
  27. MSG_CHANGED_FOCUS, /* Notification to owner about focus state change */
  28. MSG_ENABLE, /* Change state to enabled */
  29. MSG_DISABLE, /* Change state to disabled */
  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 */
  37. MSG_NOTIFY, /* Typically sent to dialog to inform it of state-change
  38. * of listboxes, check- and radiobuttons. */
  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;
  46. /* Widgets are expected to answer to the following messages:
  47. MSG_FOCUS: MSG_HANDLED if the accept the focus, MSG_NOT_HANDLED if they do not.
  48. MSG_UNFOCUS: MSG_HANDLED if they accept to release the focus, MSG_NOT_HANDLED if they don't.
  49. MSG_KEY: MSG_HANDLED if they actually used the key, MSG_NOT_HANDLED if not.
  50. MSG_HOTKEY: MSG_HANDLED if they actually used the key, MSG_NOT_HANDLED if not.
  51. */
  52. typedef enum
  53. {
  54. MSG_NOT_HANDLED = 0,
  55. MSG_HANDLED = 1
  56. } cb_ret_t;
  57. /* Widget options */
  58. typedef enum
  59. {
  60. WOP_DEFAULT = (0 << 0),
  61. WOP_WANT_HOTKEY = (1 << 0),
  62. WOP_WANT_CURSOR = (1 << 1),
  63. WOP_WANT_TAB = (1 << 2), /* Should the tab key be sent to the dialog? */
  64. WOP_IS_INPUT = (1 << 3),
  65. WOP_SELECTABLE = (1 << 4),
  66. WOP_TOP_SELECT = (1 << 5)
  67. } widget_options_t;
  68. /* Widget state */
  69. typedef enum
  70. {
  71. WST_DEFAULT = (0 << 0),
  72. WST_VISIBLE = (1 << 0), /* Widget is visible */
  73. WST_DISABLED = (1 << 1), /* Widget cannot be selected */
  74. WST_IDLE = (1 << 2),
  75. WST_MODAL = (1 << 3), /* Widget (dialog) is modal */
  76. WST_FOCUSED = (1 << 4),
  77. WST_CONSTRUCT = (1 << 15), /* Widget has been constructed but not run yet */
  78. WST_ACTIVE = (1 << 16), /* Dialog is visible and active */
  79. WST_SUSPENDED = (1 << 17), /* Dialog is suspended */
  80. WST_CLOSED = (1 << 18) /* Dialog is closed */
  81. } widget_state_t;
  82. /* Flags for widget repositioning on dialog resize */
  83. typedef enum
  84. {
  85. WPOS_FULLSCREEN = (1 << 0), /* widget occupies the whole screen */
  86. WPOS_CENTER_HORZ = (1 << 1), /* center widget in horizontal */
  87. WPOS_CENTER_VERT = (1 << 2), /* center widget in vertical */
  88. WPOS_CENTER = WPOS_CENTER_HORZ | WPOS_CENTER_VERT, /* center widget */
  89. WPOS_TRYUP = (1 << 3), /* try to move two lines up the widget */
  90. WPOS_KEEP_LEFT = (1 << 4), /* keep widget distance to left border of dialog */
  91. WPOS_KEEP_RIGHT = (1 << 5), /* keep widget distance to right border of dialog */
  92. WPOS_KEEP_TOP = (1 << 6), /* keep widget distance to top border of dialog */
  93. WPOS_KEEP_BOTTOM = (1 << 7), /* keep widget distance to bottom border of dialog */
  94. WPOS_KEEP_HORZ = WPOS_KEEP_LEFT | WPOS_KEEP_RIGHT,
  95. WPOS_KEEP_VERT = WPOS_KEEP_TOP | WPOS_KEEP_BOTTOM,
  96. WPOS_KEEP_ALL = WPOS_KEEP_HORZ | WPOS_KEEP_VERT,
  97. WPOS_KEEP_DEFAULT = WPOS_KEEP_LEFT | WPOS_KEEP_TOP
  98. } widget_pos_flags_t;
  99. /* NOTES:
  100. * If WPOS_FULLSCREEN is set then all other position flags are ignored.
  101. * If WPOS_CENTER_HORZ flag is used, other horizontal flags (WPOS_KEEP_LEFT, WPOS_KEEP_RIGHT,
  102. * and WPOS_KEEP_HORZ) are ignored.
  103. * If WPOS_CENTER_VERT flag is used, other horizontal flags (WPOS_KEEP_TOP, WPOS_KEEP_BOTTOM,
  104. * and WPOS_KEEP_VERT) are ignored.
  105. */
  106. /*** structures declarations (and typedefs of structures)*****************************************/
  107. /* Widget callback */
  108. typedef cb_ret_t (*widget_cb_fn) (Widget *widget, Widget *sender, widget_msg_t msg, int parm,
  109. void *data);
  110. /* Widget mouse callback */
  111. typedef void (*widget_mouse_cb_fn) (Widget *w, mouse_msg_t msg, mouse_event_t *event);
  112. /* translate mouse event and process it */
  113. typedef int (*widget_mouse_handle_fn) (Widget *w, Gpm_Event *event);
  114. /* Every Widget must have this as its first element */
  115. struct Widget
  116. {
  117. WRect rect; /* position and size */
  118. /* ATTENTION! For groups, don't change @rect members directly to avoid
  119. incorrect reposion and resize of group members. */
  120. widget_pos_flags_t pos_flags; /* repositioning flags */
  121. widget_options_t options;
  122. widget_state_t state;
  123. unsigned long id; /* uniq widget ID */
  124. widget_cb_fn callback;
  125. widget_mouse_cb_fn mouse_callback;
  126. WGroup *owner;
  127. /* Key-related fields */
  128. const global_keymap_t *keymap; /* main keymap */
  129. const global_keymap_t *ext_keymap; /* extended keymap */
  130. gboolean ext_mode; /* use keymap or ext_keymap */
  131. /* Mouse-related fields. */
  132. widget_mouse_handle_fn mouse_handler;
  133. struct
  134. {
  135. /* Public members: */
  136. gboolean
  137. forced_capture; /* Overrides the 'capture' member. Set explicitly by the programmer. */
  138. /* Implementation details: */
  139. gboolean capture; /* Whether the widget "owns" the mouse. */
  140. mouse_msg_t last_msg; /* The previous event type processed. */
  141. int last_buttons_down;
  142. } mouse;
  143. void (*make_global) (Widget *w, const WRect *delta);
  144. void (*make_local) (Widget *w, const WRect *delta);
  145. GList *(*find) (const Widget *w, const Widget *what);
  146. Widget *(*find_by_type) (const Widget *w, widget_cb_fn cb);
  147. Widget *(*find_by_id) (const Widget *w, unsigned long id);
  148. /* *INDENT-OFF* */
  149. cb_ret_t (*set_state) (Widget *w, widget_state_t state, gboolean enable);
  150. /* *INDENT-ON* */
  151. void (*destroy) (Widget *w);
  152. const int *(*get_colors) (const Widget *w);
  153. };
  154. /* structure for label (caption) with hotkey, if original text does not contain
  155. * hotkey, only start is valid and is equal to original text
  156. * hotkey is defined as char*, but mc support only singlebyte hotkey
  157. */
  158. typedef struct hotkey_t
  159. {
  160. char *start; /* never NULL */
  161. char *hotkey; /* can be NULL */
  162. char *end; /* can be NULL */
  163. } hotkey_t;
  164. /*** global variables defined in .c file *********************************************************/
  165. /*** declarations of public functions ************************************************************/
  166. /* create hotkey from text */
  167. hotkey_t hotkey_new (const char *text);
  168. /* release hotkey, free all mebers of hotkey_t */
  169. void hotkey_free (const hotkey_t hotkey);
  170. /* return width on terminal of hotkey */
  171. int hotkey_width (const hotkey_t hotkey);
  172. /* compare two hotkeys */
  173. gboolean hotkey_equal (const hotkey_t hotkey1, const hotkey_t hotkey2);
  174. /* draw hotkey of widget */
  175. void hotkey_draw (const Widget *w, const hotkey_t hotkey, gboolean focused);
  176. /* get text of hotkey */
  177. char *hotkey_get_text (const hotkey_t hotkey);
  178. /* widget initialization */
  179. void widget_init (Widget *w, const WRect *r, widget_cb_fn callback,
  180. widget_mouse_cb_fn mouse_callback);
  181. /* Default callback for widgets */
  182. cb_ret_t widget_default_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm,
  183. void *data);
  184. void widget_set_options (Widget *w, widget_options_t options, gboolean enable);
  185. void widget_adjust_position (widget_pos_flags_t pos_flags, WRect *r);
  186. void widget_set_size (Widget *w, int y, int x, int lines, int cols);
  187. void widget_set_size_rect (Widget *w, WRect *r);
  188. /* select color for widget in dependence of state */
  189. void widget_selectcolor (const Widget *w, gboolean focused, gboolean hotkey);
  190. cb_ret_t widget_draw (Widget *w);
  191. void widget_erase (Widget *w);
  192. void widget_set_visibility (Widget *w, gboolean make_visible);
  193. gboolean widget_is_active (const void *w);
  194. void widget_replace (Widget *old, Widget *new);
  195. gboolean widget_is_focusable (const Widget *w);
  196. void widget_select (Widget *w);
  197. void widget_set_bottom (Widget *w);
  198. long widget_lookup_key (Widget *w, int key);
  199. void widget_default_make_global (Widget *w, const WRect *delta);
  200. void widget_default_make_local (Widget *w, const WRect *delta);
  201. GList *widget_default_find (const Widget *w, const Widget *what);
  202. Widget *widget_default_find_by_type (const Widget *w, widget_cb_fn cb);
  203. Widget *widget_default_find_by_id (const Widget *w, unsigned long id);
  204. cb_ret_t widget_default_set_state (Widget *w, widget_state_t state, gboolean enable);
  205. void widget_default_destroy (Widget *w);
  206. /* get mouse pointer location within widget */
  207. Gpm_Event mouse_get_local (const Gpm_Event *global, const Widget *w);
  208. gboolean mouse_global_in_widget (const Gpm_Event *event, const Widget *w);
  209. /* --------------------------------------------------------------------------------------------- */
  210. /*** inline functions ****************************************************************************/
  211. /* --------------------------------------------------------------------------------------------- */
  212. static inline cb_ret_t
  213. send_message (void *w, void *sender, widget_msg_t msg, int parm, void *data)
  214. {
  215. cb_ret_t ret = MSG_NOT_HANDLED;
  216. #if 1
  217. if (w != NULL) /* This must be always true, but... */
  218. #endif
  219. ret = WIDGET (w)->callback (WIDGET (w), WIDGET (sender), msg, parm, data);
  220. return ret;
  221. }
  222. /* --------------------------------------------------------------------------------------------- */
  223. /**
  224. * Check whether one or several option flags are set or not.
  225. * @param w widget
  226. * @param options widget option flags
  227. *
  228. * @return TRUE if all requested option flags are set, FALSE otherwise.
  229. */
  230. static inline gboolean
  231. widget_get_options (const Widget *w, widget_options_t options)
  232. {
  233. return ((w->options & options) == options);
  234. }
  235. /* --------------------------------------------------------------------------------------------- */
  236. /**
  237. * Check whether one or several state flags are set or not.
  238. * @param w widget
  239. * @param state widget state flags
  240. *
  241. * @return TRUE if all requested state flags are set, FALSE otherwise.
  242. */
  243. static inline gboolean
  244. widget_get_state (const Widget *w, widget_state_t state)
  245. {
  246. return ((w->state & state) == state);
  247. }
  248. /* --------------------------------------------------------------------------------------------- */
  249. /**
  250. * Convert widget coordinates from local (relative to owner) to global (relative to screen).
  251. *
  252. * @param w widget
  253. */
  254. static inline void
  255. widget_make_global (Widget *w)
  256. {
  257. w->make_global (w, NULL);
  258. }
  259. /* --------------------------------------------------------------------------------------------- */
  260. /**
  261. * Convert widget coordinates from global (relative to screen) to local (relative to owner).
  262. *
  263. * @param w widget
  264. */
  265. static inline void
  266. widget_make_local (Widget *w)
  267. {
  268. w->make_local (w, NULL);
  269. }
  270. /* --------------------------------------------------------------------------------------------- */
  271. /**
  272. * Find widget.
  273. *
  274. * @param w widget
  275. * @param what widget to find
  276. *
  277. * @return result of @w->find()
  278. */
  279. static inline GList *
  280. widget_find (const Widget *w, const Widget *what)
  281. {
  282. return w->find (w, what);
  283. }
  284. /* --------------------------------------------------------------------------------------------- */
  285. /**
  286. * Find widget by widget type using widget callback.
  287. *
  288. * @param w widget
  289. * @param cb widget callback
  290. *
  291. * @return result of @w->find_by_type()
  292. */
  293. static inline Widget *
  294. widget_find_by_type (const Widget *w, widget_cb_fn cb)
  295. {
  296. return w->find_by_type (w, cb);
  297. }
  298. /* --------------------------------------------------------------------------------------------- */
  299. /**
  300. * Find widget by widget ID.
  301. *
  302. * @param w widget
  303. * @param id widget ID
  304. *
  305. * @return result of @w->find_by_id()
  306. */
  307. static inline Widget *
  308. widget_find_by_id (const Widget *w, unsigned long id)
  309. {
  310. return w->find_by_id (w, id);
  311. }
  312. /* --------------------------------------------------------------------------------------------- */
  313. /**
  314. * Modify state of widget.
  315. *
  316. * @param w widget
  317. * @param state widget state flag to modify
  318. * @param enable specifies whether to turn the flag on (TRUE) or off (FALSE).
  319. * Only one flag per call can be modified.
  320. * @return MSG_HANDLED if set was handled successfully, MSG_NOT_HANDLED otherwise.
  321. */
  322. static inline cb_ret_t
  323. widget_set_state (Widget *w, widget_state_t state, gboolean enable)
  324. {
  325. return w->set_state (w, state, enable);
  326. }
  327. /* --------------------------------------------------------------------------------------------- */
  328. /**
  329. * Destroy widget.
  330. *
  331. * @param w widget
  332. */
  333. static inline void
  334. widget_destroy (Widget *w)
  335. {
  336. w->destroy (w);
  337. }
  338. /* --------------------------------------------------------------------------------------------- */
  339. /**
  340. * Get color colors of widget.
  341. *
  342. * @param w widget
  343. * @return color colors
  344. */
  345. static inline const int *
  346. widget_get_colors (const Widget *w)
  347. {
  348. return w->get_colors (w);
  349. }
  350. /* --------------------------------------------------------------------------------------------- */
  351. /**
  352. * Update cursor position in the specified widget.
  353. *
  354. * @param w widget
  355. *
  356. * @return TRUE if cursor was updated successfully, FALSE otherwise
  357. */
  358. static inline gboolean
  359. widget_update_cursor (Widget *w)
  360. {
  361. return (send_message (w, NULL, MSG_CURSOR, 0, NULL) == MSG_HANDLED);
  362. }
  363. /* --------------------------------------------------------------------------------------------- */
  364. static inline void
  365. widget_show (Widget *w)
  366. {
  367. widget_set_visibility (w, TRUE);
  368. }
  369. /* --------------------------------------------------------------------------------------------- */
  370. static inline void
  371. widget_hide (Widget *w)
  372. {
  373. widget_set_visibility (w, FALSE);
  374. }
  375. /* --------------------------------------------------------------------------------------------- */
  376. /**
  377. * Check whether two widgets are overlapped or not.
  378. * @param a 1st widget
  379. * @param b 2nd widget
  380. *
  381. * @return TRUE if widgets are overlapped, FALSE otherwise.
  382. */
  383. static inline gboolean
  384. widget_overlapped (const Widget *a, const Widget *b)
  385. {
  386. return rects_are_overlapped (&a->rect, &b->rect);
  387. }
  388. /* --------------------------------------------------------------------------------------------- */
  389. #endif /* MC__WIDGET_COMMON_H */