widget-common.h 5.1 KB

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