root/lib/widget/widget-common.c

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

DEFINITIONS

This source file includes following definitions.
  1. widget_do_focus
  2. widget_focus
  3. widget_reorder
  4. parse_hotkey
  5. release_hotkey
  6. hotkey_width
  7. hotkey_draw
  8. widget_init
  9. widget_default_callback
  10. widget_set_options
  11. widget_set_state
  12. widget_set_size
  13. widget_selectcolor
  14. widget_erase
  15. widget_is_active
  16. widget_redraw
  17. widget_replace
  18. widget_select
  19. widget_set_bottom
  20. widget_overlapped
  21. mouse_get_local
  22. mouse_global_in_widget

   1 /*
   2    Widgets for the Midnight Commander
   3 
   4    Copyright (C) 1994-2019
   5    Free Software Foundation, Inc.
   6 
   7    Authors:
   8    Radek Doulik, 1994, 1995
   9    Miguel de Icaza, 1994, 1995
  10    Jakub Jelinek, 1995
  11    Andrej Borsenkow, 1996
  12    Norbert Warmuth, 1997
  13    Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2011, 2012, 2013
  14 
  15    This file is part of the Midnight Commander.
  16 
  17    The Midnight Commander is free software: you can redistribute it
  18    and/or modify it under the terms of the GNU General Public License as
  19    published by the Free Software Foundation, either version 3 of the License,
  20    or (at your option) any later version.
  21 
  22    The Midnight Commander is distributed in the hope that it will be useful,
  23    but WITHOUT ANY WARRANTY; without even the implied warranty of
  24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25    GNU General Public License for more details.
  26 
  27    You should have received a copy of the GNU General Public License
  28    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  29  */
  30 
  31 /** \file widget-common.c
  32  *  \brief Source: shared stuff of widgets
  33  */
  34 
  35 #include <config.h>
  36 
  37 #include <stdlib.h>
  38 #include <string.h>
  39 
  40 #include "lib/global.h"
  41 
  42 #include "lib/tty/tty.h"
  43 #include "lib/tty/color.h"
  44 #include "lib/skin.h"
  45 #include "lib/strutil.h"
  46 #include "lib/widget.h"
  47 
  48 /*** global variables ****************************************************************************/
  49 
  50 /*** file scope macro definitions ****************************************************************/
  51 
  52 /*** file scope type declarations ****************************************************************/
  53 
  54 /*** file scope variables ************************************************************************/
  55 
  56 /* --------------------------------------------------------------------------------------------- */
  57 /*** file scope functions ************************************************************************/
  58 /* --------------------------------------------------------------------------------------------- */
  59 
  60 static void
  61 widget_do_focus (Widget * w, gboolean enable)
     /* [previous][next][first][last][top][bottom][index][help]  */
  62 {
  63     if (w != NULL && widget_get_state (WIDGET (w->owner), WST_FOCUSED))
  64         widget_set_state (w, WST_FOCUSED, enable);
  65 }
  66 
  67 /* --------------------------------------------------------------------------------------------- */
  68 /**
  69  * Focus specified widget in it's owner.
  70  *
  71  * @param w widget to be focused.
  72  */
  73 
  74 static void
  75 widget_focus (Widget * w)
     /* [previous][next][first][last][top][bottom][index][help]  */
  76 {
  77     WDialog *h = DIALOG (w->owner);
  78 
  79     if (h == NULL)
  80         return;
  81 
  82     if (WIDGET (h->current->data) != w)
  83     {
  84         widget_do_focus (WIDGET (h->current->data), FALSE);
  85         /* Test if focus lost was allowed and focus has really been loose */
  86         if (h->current == NULL || !widget_get_state (WIDGET (h->current->data), WST_FOCUSED))
  87         {
  88             widget_do_focus (w, TRUE);
  89             h->current = dlg_find (h, w);
  90         }
  91     }
  92     else if (!widget_get_state (w, WST_FOCUSED))
  93         widget_do_focus (w, TRUE);
  94 }
  95 
  96 /* --------------------------------------------------------------------------------------------- */
  97 
  98 /**
  99  * Put widget on top or bottom of Z-order.
 100  */
 101 static void
 102 widget_reorder (GList * l, gboolean set_top)
     /* [previous][next][first][last][top][bottom][index][help]  */
 103 {
 104     WDialog *h = WIDGET (l->data)->owner;
 105 
 106     h->widgets = g_list_remove_link (h->widgets, l);
 107     if (set_top)
 108         h->widgets = g_list_concat (h->widgets, l);
 109     else
 110         h->widgets = g_list_concat (l, h->widgets);
 111 }
 112 
 113 
 114 /* --------------------------------------------------------------------------------------------- */
 115 /*** public functions ****************************************************************************/
 116 /* --------------------------------------------------------------------------------------------- */
 117 
 118 struct hotkey_t
 119 parse_hotkey (const char *text)
     /* [previous][next][first][last][top][bottom][index][help]  */
 120 {
 121     hotkey_t result;
 122     const char *cp, *p;
 123 
 124     if (text == NULL)
 125         text = "";
 126 
 127     /* search for '&', that is not on the of text */
 128     cp = strchr (text, '&');
 129     if (cp != NULL && cp[1] != '\0')
 130     {
 131         result.start = g_strndup (text, cp - text);
 132 
 133         /* skip '&' */
 134         cp++;
 135         p = str_cget_next_char (cp);
 136         result.hotkey = g_strndup (cp, p - cp);
 137 
 138         cp = p;
 139         result.end = g_strdup (cp);
 140     }
 141     else
 142     {
 143         result.start = g_strdup (text);
 144         result.hotkey = NULL;
 145         result.end = NULL;
 146     }
 147 
 148     return result;
 149 }
 150 
 151 /* --------------------------------------------------------------------------------------------- */
 152 
 153 void
 154 release_hotkey (const hotkey_t hotkey)
     /* [previous][next][first][last][top][bottom][index][help]  */
 155 {
 156     g_free (hotkey.start);
 157     g_free (hotkey.hotkey);
 158     g_free (hotkey.end);
 159 }
 160 
 161 /* --------------------------------------------------------------------------------------------- */
 162 
 163 int
 164 hotkey_width (const hotkey_t hotkey)
     /* [previous][next][first][last][top][bottom][index][help]  */
 165 {
 166     int result;
 167 
 168     result = str_term_width1 (hotkey.start);
 169     result += (hotkey.hotkey != NULL) ? str_term_width1 (hotkey.hotkey) : 0;
 170     result += (hotkey.end != NULL) ? str_term_width1 (hotkey.end) : 0;
 171     return result;
 172 }
 173 
 174 /* --------------------------------------------------------------------------------------------- */
 175 
 176 void
 177 hotkey_draw (Widget * w, const hotkey_t hotkey, gboolean focused)
     /* [previous][next][first][last][top][bottom][index][help]  */
 178 {
 179     widget_selectcolor (w, focused, FALSE);
 180     tty_print_string (hotkey.start);
 181 
 182     if (hotkey.hotkey != NULL)
 183     {
 184         widget_selectcolor (w, focused, TRUE);
 185         tty_print_string (hotkey.hotkey);
 186         widget_selectcolor (w, focused, FALSE);
 187     }
 188 
 189     if (hotkey.end != NULL)
 190         tty_print_string (hotkey.end);
 191 }
 192 
 193 /* --------------------------------------------------------------------------------------------- */
 194 
 195 void
 196 widget_init (Widget * w, int y, int x, int lines, int cols,
     /* [previous][next][first][last][top][bottom][index][help]  */
 197              widget_cb_fn callback, widget_mouse_cb_fn mouse_callback)
 198 {
 199     w->x = x;
 200     w->y = y;
 201     w->cols = cols;
 202     w->lines = lines;
 203     w->pos_flags = WPOS_KEEP_DEFAULT;
 204     w->callback = callback;
 205     w->mouse_callback = mouse_callback;
 206     w->owner = NULL;
 207     w->mouse.forced_capture = FALSE;
 208     w->mouse.capture = FALSE;
 209     w->mouse.last_msg = MSG_MOUSE_NONE;
 210     w->mouse.last_buttons_down = 0;
 211 
 212     w->options = WOP_DEFAULT;
 213     w->state = WST_DEFAULT;
 214 }
 215 
 216 /* --------------------------------------------------------------------------------------------- */
 217 
 218 /* Default callback for widgets */
 219 cb_ret_t
 220 widget_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 221 {
 222     (void) w;
 223     (void) sender;
 224     (void) parm;
 225     (void) data;
 226 
 227     switch (msg)
 228     {
 229     case MSG_INIT:
 230     case MSG_FOCUS:
 231     case MSG_UNFOCUS:
 232     case MSG_ENABLE:
 233     case MSG_DISABLE:
 234     case MSG_DRAW:
 235     case MSG_DESTROY:
 236     case MSG_CURSOR:
 237     case MSG_IDLE:
 238         return MSG_HANDLED;
 239 
 240     default:
 241         return MSG_NOT_HANDLED;
 242     }
 243 }
 244 
 245 /* --------------------------------------------------------------------------------------------- */
 246 
 247 /**
 248  * Apply new options to widget.
 249  *
 250  * @param w       widget
 251  * @param options widget option flags to modify. Several flags per call can be modified.
 252  * @param enable  TRUE if specified options should be added, FALSE if options should be removed
 253  */
 254 void
 255 widget_set_options (Widget * w, widget_options_t options, gboolean enable)
     /* [previous][next][first][last][top][bottom][index][help]  */
 256 {
 257     if (enable)
 258         w->options |= options;
 259     else
 260         w->options &= ~options;
 261 }
 262 
 263 /* --------------------------------------------------------------------------------------------- */
 264 
 265 /**
 266  * Modify state of widget.
 267  *
 268  * @param w      widget
 269  * @param state  widget state flag to modify
 270  * @param enable specifies whether to turn the flag on (TRUE) or off (FALSE).
 271  *               Only one flag per call can be modified.
 272  * @return       MSG_HANDLED if set was handled successfully, MSG_NOT_HANDLED otherwise.
 273  */
 274 cb_ret_t
 275 widget_set_state (Widget * w, widget_state_t state, gboolean enable)
     /* [previous][next][first][last][top][bottom][index][help]  */
 276 {
 277     gboolean ret = MSG_HANDLED;
 278 
 279     if (enable)
 280         w->state |= state;
 281     else
 282         w->state &= ~state;
 283 
 284     if (enable)
 285     {
 286         /* exclusive bits */
 287         if ((state & WST_CONSTRUCT) != 0)
 288             w->state &= ~(WST_ACTIVE | WST_SUSPENDED | WST_CLOSED);
 289         else if ((state & WST_ACTIVE) != 0)
 290             w->state &= ~(WST_CONSTRUCT | WST_SUSPENDED | WST_CLOSED);
 291         else if ((state & WST_SUSPENDED) != 0)
 292             w->state &= ~(WST_CONSTRUCT | WST_ACTIVE | WST_CLOSED);
 293         else if ((state & WST_CLOSED) != 0)
 294             w->state &= ~(WST_CONSTRUCT | WST_ACTIVE | WST_SUSPENDED);
 295     }
 296 
 297     if (w->owner == NULL)
 298         return MSG_NOT_HANDLED;
 299 
 300     switch (state)
 301     {
 302     case WST_DISABLED:
 303         ret = send_message (w, NULL, enable ? MSG_DISABLE : MSG_ENABLE, 0, NULL);
 304         if (ret == MSG_HANDLED && widget_get_state (WIDGET (w->owner), WST_ACTIVE))
 305             ret = send_message (w, NULL, MSG_DRAW, 0, NULL);
 306         break;
 307 
 308     case WST_FOCUSED:
 309         {
 310             widget_msg_t msg;
 311 
 312             msg = enable ? MSG_FOCUS : MSG_UNFOCUS;
 313             ret = send_message (w, NULL, msg, 0, NULL);
 314             if (ret == MSG_HANDLED && widget_get_state (WIDGET (w->owner), WST_ACTIVE))
 315             {
 316                 send_message (w, NULL, MSG_DRAW, 0, NULL);
 317                 /* Notify owner that focus was moved from one widget to another */
 318                 send_message (w->owner, w, MSG_CHANGED_FOCUS, 0, NULL);
 319             }
 320         }
 321         break;
 322 
 323     default:
 324         break;
 325     }
 326 
 327     return ret;
 328 }
 329 
 330 /* --------------------------------------------------------------------------------------------- */
 331 
 332 void
 333 widget_set_size (Widget * widget, int y, int x, int lines, int cols)
     /* [previous][next][first][last][top][bottom][index][help]  */
 334 {
 335     widget->x = x;
 336     widget->y = y;
 337     widget->cols = cols;
 338     widget->lines = lines;
 339     send_message (widget, NULL, MSG_RESIZE, 0, NULL);
 340     if (widget->owner != NULL && widget_get_state (WIDGET (widget->owner), WST_ACTIVE))
 341         send_message (widget, NULL, MSG_DRAW, 0, NULL);
 342 }
 343 
 344 /* --------------------------------------------------------------------------------------------- */
 345 
 346 void
 347 widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey)
     /* [previous][next][first][last][top][bottom][index][help]  */
 348 {
 349     WDialog *h = w->owner;
 350     int color;
 351 
 352     if (widget_get_state (w, WST_DISABLED))
 353         color = DISABLED_COLOR;
 354     else if (hotkey)
 355     {
 356         if (focused)
 357             color = h->color[DLG_COLOR_HOT_FOCUS];
 358         else
 359             color = h->color[DLG_COLOR_HOT_NORMAL];
 360     }
 361     else
 362     {
 363         if (focused)
 364             color = h->color[DLG_COLOR_FOCUS];
 365         else
 366             color = h->color[DLG_COLOR_NORMAL];
 367     }
 368 
 369     tty_setcolor (color);
 370 }
 371 
 372 /* --------------------------------------------------------------------------------------------- */
 373 
 374 void
 375 widget_erase (Widget * w)
     /* [previous][next][first][last][top][bottom][index][help]  */
 376 {
 377     if (w != NULL)
 378         tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
 379 }
 380 
 381 /* --------------------------------------------------------------------------------------------- */
 382 /**
 383   * Check whether widget is active or not.
 384   * @param w the widget
 385   *
 386   * @return TRUE if the widget is active, FALSE otherwise
 387   */
 388 
 389 gboolean
 390 widget_is_active (const void *w)
     /* [previous][next][first][last][top][bottom][index][help]  */
 391 {
 392     return (w == CONST_WIDGET (w)->owner->current->data);
 393 }
 394 
 395 /* --------------------------------------------------------------------------------------------- */
 396 
 397 void
 398 widget_redraw (Widget * w)
     /* [previous][next][first][last][top][bottom][index][help]  */
 399 {
 400     if (w != NULL)
 401     {
 402         WDialog *h = w->owner;
 403 
 404         if (h != NULL && widget_get_state (WIDGET (h), WST_ACTIVE))
 405             w->callback (w, NULL, MSG_DRAW, 0, NULL);
 406     }
 407 }
 408 
 409 /* --------------------------------------------------------------------------------------------- */
 410 /**
 411   * Replace widget in the dialog.
 412   *
 413   * @param old_w old widget that need to be replaced
 414   * @param new_w new widget that will replace @old_w
 415   */
 416 
 417 void
 418 widget_replace (Widget * old_w, Widget * new_w)
     /* [previous][next][first][last][top][bottom][index][help]  */
 419 {
 420     WDialog *h = old_w->owner;
 421     gboolean should_focus = FALSE;
 422     GList *holder;
 423 
 424     if (h->widgets == NULL)
 425         return;
 426 
 427     if (h->current == NULL)
 428         h->current = h->widgets;
 429 
 430     /* locate widget position in the list */
 431     if (old_w == h->current->data)
 432         holder = h->current;
 433     else
 434         holder = g_list_find (h->widgets, old_w);
 435 
 436     /* if old widget is focused, we should focus the new one... */
 437     if (widget_get_state (old_w, WST_FOCUSED))
 438         should_focus = TRUE;
 439     /* ...but if new widget isn't selectable, we cannot focus it */
 440     if (!widget_get_options (new_w, WOP_SELECTABLE))
 441         should_focus = FALSE;
 442 
 443     /* if new widget isn't selectable, select other widget before replace */
 444     if (!should_focus)
 445     {
 446         GList *l;
 447 
 448         for (l = dlg_get_widget_next_of (holder);
 449              !widget_get_options (WIDGET (l->data), WOP_SELECTABLE)
 450              && !widget_get_state (WIDGET (l->data), WST_DISABLED); l = dlg_get_widget_next_of (l))
 451             ;
 452 
 453         widget_select (WIDGET (l->data));
 454     }
 455 
 456     /* replace widget */
 457     new_w->owner = h;
 458     new_w->id = old_w->id;
 459     holder->data = new_w;
 460 
 461     send_message (old_w, NULL, MSG_DESTROY, 0, NULL);
 462     send_message (new_w, NULL, MSG_INIT, 0, NULL);
 463 
 464     if (should_focus)
 465         widget_select (new_w);
 466     else
 467         widget_redraw (new_w);
 468 }
 469 
 470 /* --------------------------------------------------------------------------------------------- */
 471 /**
 472  * Select specified widget in it's owner.
 473  *
 474  * Note: this function (and widget_focus(), which it calls) is a no-op
 475  * if the widget is already selected.
 476  *
 477  * @param w widget to be selected
 478  */
 479 
 480 void
 481 widget_select (Widget * w)
     /* [previous][next][first][last][top][bottom][index][help]  */
 482 {
 483     WDialog *h;
 484 
 485     if (!widget_get_options (w, WOP_SELECTABLE))
 486         return;
 487 
 488     h = w->owner;
 489     if (h != NULL)
 490     {
 491         if (widget_get_options (w, WOP_TOP_SELECT))
 492         {
 493             GList *l;
 494 
 495             l = dlg_find (h, w);
 496             widget_reorder (l, TRUE);
 497         }
 498 
 499         widget_focus (w);
 500     }
 501 }
 502 
 503 /* --------------------------------------------------------------------------------------------- */
 504 /**
 505  * Set widget at bottom of widget list.
 506  */
 507 
 508 void
 509 widget_set_bottom (Widget * w)
     /* [previous][next][first][last][top][bottom][index][help]  */
 510 {
 511     widget_reorder (dlg_find (w->owner, w), FALSE);
 512 }
 513 
 514 /* --------------------------------------------------------------------------------------------- */
 515 /**
 516   * Check whether two widgets are overlapped or not.
 517   * @param a 1st widget
 518   * @param b 2nd widget
 519   *
 520   * @return TRUE if widgets are overlapped, FALSE otherwise.
 521   */
 522 
 523 gboolean
 524 widget_overlapped (const Widget * a, const Widget * b)
     /* [previous][next][first][last][top][bottom][index][help]  */
 525 {
 526     return !((b->x >= a->x + a->cols)
 527              || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
 528 }
 529 
 530 /* --------------------------------------------------------------------------------------------- */
 531 /* get mouse pointer location within widget */
 532 
 533 Gpm_Event
 534 mouse_get_local (const Gpm_Event * global, const Widget * w)
     /* [previous][next][first][last][top][bottom][index][help]  */
 535 {
 536     Gpm_Event local;
 537 
 538     local.buttons = global->buttons;
 539 #ifdef HAVE_LIBGPM
 540     local.clicks = 0;
 541     local.margin = 0;
 542     local.modifiers = 0;
 543     local.vc = 0;
 544 #endif
 545     local.x = global->x - w->x;
 546     local.y = global->y - w->y;
 547     local.type = global->type;
 548 
 549     return local;
 550 }
 551 
 552 /* --------------------------------------------------------------------------------------------- */
 553 
 554 gboolean
 555 mouse_global_in_widget (const Gpm_Event * event, const Widget * w)
     /* [previous][next][first][last][top][bottom][index][help]  */
 556 {
 557     return (event->x > w->x) && (event->y > w->y) && (event->x <= w->x + w->cols)
 558         && (event->y <= w->y + w->lines);
 559 }
 560 
 561 /* --------------------------------------------------------------------------------------------- */

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