root/lib/widget/widget-common.h

/* [previous][next][first][last][top][bottom][index][help]  */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. send_message
  2. widget_get_options
  3. widget_get_state

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

/* [previous][next][first][last][top][bottom][index][help]  */