widget-common.h 16 KB

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