widget-common.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /** \file widget-common.h
  2. * \brief Header: shared stuff of widgets
  3. */
  4. #ifndef MC__WIDGET_INTERNAL_H
  5. #define MC__WIDGET_INTERNAL_H
  6. #include "lib/tty/mouse.h"
  7. #include "lib/widget/mouse.h" /* mouse_msg_t, mouse_event_t */
  8. /*** typedefs(not structures) and defined constants **********************************************/
  9. #define WIDGET(x) ((Widget *)(x))
  10. #define CONST_WIDGET(x) ((const Widget *)(x))
  11. #define widget_move(w, _y, _x) tty_gotoyx (CONST_WIDGET(w)->y + (_y), CONST_WIDGET(w)->x + (_x))
  12. /* Sets/clear the specified flag in the options field */
  13. #define widget_want_cursor(w,i) widget_set_options(w, WOP_WANT_CURSOR, i)
  14. #define widget_want_hotkey(w,i) widget_set_options(w, WOP_WANT_HOTKEY, i)
  15. #define widget_want_tab(w,i) widget_set_options(w, WOP_WANT_TAB, i)
  16. #define widget_idle(w,i) widget_set_state(w, WST_IDLE, i)
  17. #define widget_disable(w,i) widget_set_state(w, WST_DISABLED, i)
  18. /*** enums ***************************************************************************************/
  19. /* Widget messages */
  20. typedef enum
  21. {
  22. MSG_INIT = 0, /* Initialize widget */
  23. MSG_FOCUS, /* Draw widget in focused state or widget has got focus */
  24. MSG_UNFOCUS, /* Draw widget in unfocused state or widget has been unfocused */
  25. MSG_CHANGED_FOCUS, /* Notification to owner about focus state change */
  26. MSG_ENABLE, /* Change state to enabled */
  27. MSG_DISABLE, /* Change state to disabled */
  28. MSG_DRAW, /* Draw widget on screen */
  29. MSG_KEY, /* Sent to widgets on key press */
  30. MSG_HOTKEY, /* Sent to widget to catch preprocess key */
  31. MSG_HOTKEY_HANDLED, /* A widget has got the hotkey */
  32. MSG_UNHANDLED_KEY, /* Key that no widget handled */
  33. MSG_POST_KEY, /* The key has been handled */
  34. MSG_ACTION, /* Send to widget to handle command */
  35. MSG_NOTIFY, /* Typically sent to dialog to inform it of state-change
  36. * of listboxes, check- and radiobuttons. */
  37. MSG_CURSOR, /* Sent to widget to position the cursor */
  38. MSG_IDLE, /* The idle state is active */
  39. MSG_RESIZE, /* Screen size has changed */
  40. MSG_VALIDATE, /* Dialog is to be closed */
  41. MSG_END, /* Shut down dialog */
  42. MSG_DESTROY /* Sent to widget at destruction time */
  43. } widget_msg_t;
  44. /* Widgets are expected to answer to the following messages:
  45. MSG_FOCUS: MSG_HANDLED if the accept the focus, MSG_NOT_HANDLED if they do not.
  46. MSG_UNFOCUS: MSG_HANDLED if they accept to release the focus, MSG_NOT_HANDLED if they don't.
  47. MSG_KEY: MSG_HANDLED if they actually used the key, MSG_NOT_HANDLED if not.
  48. MSG_HOTKEY: MSG_HANDLED if they actually used the key, MSG_NOT_HANDLED if not.
  49. */
  50. typedef enum
  51. {
  52. MSG_NOT_HANDLED = 0,
  53. MSG_HANDLED = 1
  54. } cb_ret_t;
  55. /* Widget options */
  56. typedef enum
  57. {
  58. WOP_DEFAULT = (0 << 0),
  59. WOP_WANT_HOTKEY = (1 << 0),
  60. WOP_WANT_CURSOR = (1 << 1),
  61. WOP_WANT_TAB = (1 << 2), /* Should the tab key be sent to the dialog? */
  62. WOP_IS_INPUT = (1 << 3),
  63. WOP_SELECTABLE = (1 << 4),
  64. WOP_TOP_SELECT = (1 << 5)
  65. } widget_options_t;
  66. /* Widget state */
  67. typedef enum
  68. {
  69. WST_DEFAULT = (0 << 0),
  70. WST_DISABLED = (1 << 0), /* Widget cannot be selected */
  71. WST_IDLE = (1 << 1),
  72. WST_MODAL = (1 << 2), /* Widget (dialog) is modal */
  73. WST_FOCUSED = (1 << 3),
  74. WST_CONSTRUCT = (1 << 15), /* Dialog has been constructed but not run yet */
  75. WST_ACTIVE = (1 << 16), /* Dialog is visible and active */
  76. WST_SUSPENDED = (1 << 17), /* Dialog is suspended */
  77. WST_CLOSED = (1 << 18) /* Dialog is closed */
  78. } widget_state_t;
  79. /* Flags for widget repositioning on dialog resize */
  80. typedef enum
  81. {
  82. WPOS_FULLSCREEN = (1 << 0), /* widget occupies the whole screen */
  83. WPOS_CENTER_HORZ = (1 << 1), /* center widget in horizontal */
  84. WPOS_CENTER_VERT = (1 << 2), /* center widget in vertical */
  85. WPOS_CENTER = WPOS_CENTER_HORZ | WPOS_CENTER_VERT, /* center widget */
  86. WPOS_TRYUP = (1 << 3), /* try to move two lines up the widget */
  87. WPOS_KEEP_LEFT = (1 << 4), /* keep widget distance to left border of dialog */
  88. WPOS_KEEP_RIGHT = (1 << 5), /* keep widget distance to right border of dialog */
  89. WPOS_KEEP_TOP = (1 << 6), /* keep widget distance to top border of dialog */
  90. WPOS_KEEP_BOTTOM = (1 << 7), /* keep widget distance to bottom border of dialog */
  91. WPOS_KEEP_HORZ = WPOS_KEEP_LEFT | WPOS_KEEP_RIGHT,
  92. WPOS_KEEP_VERT = WPOS_KEEP_TOP | WPOS_KEEP_BOTTOM,
  93. WPOS_KEEP_ALL = WPOS_KEEP_HORZ | WPOS_KEEP_VERT,
  94. WPOS_KEEP_DEFAULT = WPOS_KEEP_LEFT | WPOS_KEEP_TOP
  95. } widget_pos_flags_t;
  96. /* NOTES:
  97. * If WPOS_FULLSCREEN is set then all other position flags are ignored.
  98. * If WPOS_CENTER_HORZ flag is used, other horizontal flags (WPOS_KEEP_LEFT, WPOS_KEEP_RIGHT,
  99. * and WPOS_KEEP_HORZ) are ignored.
  100. * If WPOS_CENTER_VERT flag is used, other horizontal flags (WPOS_KEEP_TOP, WPOS_KEEP_BOTTOM,
  101. * and WPOS_KEEP_VERT) are ignored.
  102. */
  103. /*** structures declarations (and typedefs of structures)*****************************************/
  104. /* Widget callback */
  105. typedef cb_ret_t (*widget_cb_fn) (Widget * widget, Widget * sender, widget_msg_t msg, int parm,
  106. void *data);
  107. /* Widget mouse callback */
  108. typedef void (*widget_mouse_cb_fn) (Widget * w, mouse_msg_t msg, mouse_event_t * event);
  109. /* Every Widget must have this as its first element */
  110. struct Widget
  111. {
  112. int x, y;
  113. int cols, lines;
  114. widget_pos_flags_t pos_flags; /* repositioning flags */
  115. widget_options_t options;
  116. widget_state_t state;
  117. unsigned int id; /* Number of the widget, starting with 0 */
  118. widget_cb_fn callback;
  119. widget_mouse_cb_fn mouse_callback;
  120. WDialog *owner;
  121. /* Mouse-related fields. */
  122. struct
  123. {
  124. /* Public members: */
  125. gboolean forced_capture; /* Overrides the 'capture' member. Set explicitly by the programmer. */
  126. /* Implementation details: */
  127. gboolean capture; /* Whether the widget "owns" the mouse. */
  128. mouse_msg_t last_msg; /* The previous event type processed. */
  129. int last_buttons_down;
  130. } mouse;
  131. };
  132. /* structure for label (caption) with hotkey, if original text does not contain
  133. * hotkey, only start is valid and is equal to original text
  134. * hotkey is defined as char*, but mc support only singlebyte hotkey
  135. */
  136. typedef struct hotkey_t
  137. {
  138. char *start;
  139. char *hotkey;
  140. char *end;
  141. } hotkey_t;
  142. /*** global variables defined in .c file *********************************************************/
  143. /*** declarations of public functions ************************************************************/
  144. /* create hotkey from text */
  145. hotkey_t parse_hotkey (const char *text);
  146. /* release hotkey, free all mebers of hotkey_t */
  147. void release_hotkey (const hotkey_t hotkey);
  148. /* return width on terminal of hotkey */
  149. int hotkey_width (const hotkey_t hotkey);
  150. /* draw hotkey of widget */
  151. void hotkey_draw (Widget * w, const hotkey_t hotkey, gboolean focused);
  152. /* widget initialization */
  153. void widget_init (Widget * w, int y, int x, int lines, int cols,
  154. widget_cb_fn callback, widget_mouse_cb_fn mouse_callback);
  155. /* Default callback for widgets */
  156. cb_ret_t widget_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm,
  157. void *data);
  158. void widget_set_options (Widget * w, widget_options_t options, gboolean enable);
  159. cb_ret_t widget_set_state (Widget * w, widget_state_t state, gboolean enable);
  160. void widget_set_size (Widget * widget, int y, int x, int lines, int cols);
  161. /* select color for widget in dependance of state */
  162. void widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey);
  163. void widget_redraw (Widget * w);
  164. void widget_erase (Widget * w);
  165. gboolean widget_is_active (const void *w);
  166. gboolean widget_overlapped (const Widget * a, const Widget * b);
  167. void widget_replace (Widget * old, Widget * new);
  168. void widget_select (Widget * w);
  169. void widget_set_bottom (Widget * w);
  170. /* get mouse pointer location within widget */
  171. Gpm_Event mouse_get_local (const Gpm_Event * global, const Widget * w);
  172. gboolean mouse_global_in_widget (const Gpm_Event * event, const Widget * w);
  173. /* --------------------------------------------------------------------------------------------- */
  174. /*** inline functions ****************************************************************************/
  175. /* --------------------------------------------------------------------------------------------- */
  176. static inline cb_ret_t
  177. send_message (void *w, void *sender, widget_msg_t msg, int parm, void *data)
  178. {
  179. cb_ret_t ret = MSG_NOT_HANDLED;
  180. #if 1
  181. if (w != NULL) /* This must be always true, but... */
  182. #endif
  183. ret = WIDGET (w)->callback (WIDGET (w), WIDGET (sender), msg, parm, data);
  184. return ret;
  185. }
  186. /* --------------------------------------------------------------------------------------------- */
  187. /**
  188. * Check whether one or several option flags are set or not.
  189. * @param w widget
  190. * @param options widget option flags
  191. *
  192. * @return TRUE if all requested option flags are set, FALSE otherwise.
  193. */
  194. static inline gboolean
  195. widget_get_options (const Widget * w, widget_options_t options)
  196. {
  197. return ((w->options & options) == options);
  198. }
  199. /* --------------------------------------------------------------------------------------------- */
  200. /**
  201. * Check whether one or several state flags are set or not.
  202. * @param w widget
  203. * @param state widget state flags
  204. *
  205. * @return TRUE if all requested state flags are set, FALSE otherwise.
  206. */
  207. static inline gboolean
  208. widget_get_state (const Widget * w, widget_state_t state)
  209. {
  210. return ((w->state & state) == state);
  211. }
  212. /* --------------------------------------------------------------------------------------------- */
  213. #endif /* MC__WIDGET_INTERNAL_H */