widget-common.h 7.2 KB

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