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
  4. widget_find
  5. widget_find_by_type
  6. widget_find_by_id
  7. widget_set_state
  8. widget_get_colors
  9. widget_update_cursor
  10. widget_set_size_rect

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

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