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_adjust_position
  13. widget_set_size
  14. widget_selectcolor
  15. widget_erase
  16. widget_is_active
  17. widget_redraw
  18. widget_replace
  19. widget_select
  20. widget_set_bottom
  21. widget_overlapped
  22. mouse_get_local
  23. 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_adjust_position (widget_pos_flags_t pos_flags, int *y, int *x, int *lines, int *cols)
     /* [previous][next][first][last][top][bottom][index][help]  */
 334 {
 335     if ((pos_flags & WPOS_FULLSCREEN) != 0)
 336     {
 337         *y = 0;
 338         *x = 0;
 339         *lines = LINES;
 340         *cols = COLS;
 341     }
 342     else
 343     {
 344         if ((pos_flags & WPOS_CENTER_HORZ) != 0)
 345             *x = (COLS - *cols) / 2;
 346 
 347         if ((pos_flags & WPOS_CENTER_VERT) != 0)
 348             *y = (LINES - *lines) / 2;
 349 
 350         if ((pos_flags & WPOS_TRYUP) != 0)
 351         {
 352             if (*y > 3)
 353                 *y -= 2;
 354             else if (*y == 3)
 355                 *y = 2;
 356         }
 357     }
 358 }
 359 
 360 /* --------------------------------------------------------------------------------------------- */
 361 
 362 void
 363 widget_set_size (Widget * widget, int y, int x, int lines, int cols)
     /* [previous][next][first][last][top][bottom][index][help]  */
 364 {
 365     widget->x = x;
 366     widget->y = y;
 367     widget->cols = cols;
 368     widget->lines = lines;
 369     send_message (widget, NULL, MSG_RESIZE, 0, NULL);
 370     if (widget->owner != NULL && widget_get_state (WIDGET (widget->owner), WST_ACTIVE))
 371         send_message (widget, NULL, MSG_DRAW, 0, NULL);
 372 }
 373 
 374 /* --------------------------------------------------------------------------------------------- */
 375 
 376 void
 377 widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey)
     /* [previous][next][first][last][top][bottom][index][help]  */
 378 {
 379     WDialog *h = w->owner;
 380     int color;
 381 
 382     if (widget_get_state (w, WST_DISABLED))
 383         color = DISABLED_COLOR;
 384     else if (hotkey)
 385     {
 386         if (focused)
 387             color = h->color[DLG_COLOR_HOT_FOCUS];
 388         else
 389             color = h->color[DLG_COLOR_HOT_NORMAL];
 390     }
 391     else
 392     {
 393         if (focused)
 394             color = h->color[DLG_COLOR_FOCUS];
 395         else
 396             color = h->color[DLG_COLOR_NORMAL];
 397     }
 398 
 399     tty_setcolor (color);
 400 }
 401 
 402 /* --------------------------------------------------------------------------------------------- */
 403 
 404 void
 405 widget_erase (Widget * w)
     /* [previous][next][first][last][top][bottom][index][help]  */
 406 {
 407     if (w != NULL)
 408         tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
 409 }
 410 
 411 /* --------------------------------------------------------------------------------------------- */
 412 /**
 413   * Check whether widget is active or not.
 414   * @param w the widget
 415   *
 416   * @return TRUE if the widget is active, FALSE otherwise
 417   */
 418 
 419 gboolean
 420 widget_is_active (const void *w)
     /* [previous][next][first][last][top][bottom][index][help]  */
 421 {
 422     return (w == CONST_WIDGET (w)->owner->current->data);
 423 }
 424 
 425 /* --------------------------------------------------------------------------------------------- */
 426 
 427 void
 428 widget_redraw (Widget * w)
     /* [previous][next][first][last][top][bottom][index][help]  */
 429 {
 430     if (w != NULL)
 431     {
 432         WDialog *h = w->owner;
 433 
 434         if (h != NULL && widget_get_state (WIDGET (h), WST_ACTIVE))
 435             w->callback (w, NULL, MSG_DRAW, 0, NULL);
 436     }
 437 }
 438 
 439 /* --------------------------------------------------------------------------------------------- */
 440 /**
 441   * Replace widget in the dialog.
 442   *
 443   * @param old_w old widget that need to be replaced
 444   * @param new_w new widget that will replace @old_w
 445   */
 446 
 447 void
 448 widget_replace (Widget * old_w, Widget * new_w)
     /* [previous][next][first][last][top][bottom][index][help]  */
 449 {
 450     WDialog *h = old_w->owner;
 451     gboolean should_focus = FALSE;
 452     GList *holder;
 453 
 454     if (h->widgets == NULL)
 455         return;
 456 
 457     if (h->current == NULL)
 458         h->current = h->widgets;
 459 
 460     /* locate widget position in the list */
 461     if (old_w == h->current->data)
 462         holder = h->current;
 463     else
 464         holder = g_list_find (h->widgets, old_w);
 465 
 466     /* if old widget is focused, we should focus the new one... */
 467     if (widget_get_state (old_w, WST_FOCUSED))
 468         should_focus = TRUE;
 469     /* ...but if new widget isn't selectable, we cannot focus it */
 470     if (!widget_get_options (new_w, WOP_SELECTABLE))
 471         should_focus = FALSE;
 472 
 473     /* if new widget isn't selectable, select other widget before replace */
 474     if (!should_focus)
 475     {
 476         GList *l;
 477 
 478         for (l = dlg_get_widget_next_of (holder);
 479              !widget_get_options (WIDGET (l->data), WOP_SELECTABLE)
 480              && !widget_get_state (WIDGET (l->data), WST_DISABLED); l = dlg_get_widget_next_of (l))
 481             ;
 482 
 483         widget_select (WIDGET (l->data));
 484     }
 485 
 486     /* replace widget */
 487     new_w->owner = h;
 488     new_w->id = old_w->id;
 489     holder->data = new_w;
 490 
 491     send_message (old_w, NULL, MSG_DESTROY, 0, NULL);
 492     send_message (new_w, NULL, MSG_INIT, 0, NULL);
 493 
 494     if (should_focus)
 495         widget_select (new_w);
 496     else
 497         widget_redraw (new_w);
 498 }
 499 
 500 /* --------------------------------------------------------------------------------------------- */
 501 /**
 502  * Select specified widget in it's owner.
 503  *
 504  * Note: this function (and widget_focus(), which it calls) is a no-op
 505  * if the widget is already selected.
 506  *
 507  * @param w widget to be selected
 508  */
 509 
 510 void
 511 widget_select (Widget * w)
     /* [previous][next][first][last][top][bottom][index][help]  */
 512 {
 513     WDialog *h;
 514 
 515     if (!widget_get_options (w, WOP_SELECTABLE))
 516         return;
 517 
 518     h = w->owner;
 519     if (h != NULL)
 520     {
 521         if (widget_get_options (w, WOP_TOP_SELECT))
 522         {
 523             GList *l;
 524 
 525             l = dlg_find (h, w);
 526             widget_reorder (l, TRUE);
 527         }
 528 
 529         widget_focus (w);
 530     }
 531 }
 532 
 533 /* --------------------------------------------------------------------------------------------- */
 534 /**
 535  * Set widget at bottom of widget list.
 536  */
 537 
 538 void
 539 widget_set_bottom (Widget * w)
     /* [previous][next][first][last][top][bottom][index][help]  */
 540 {
 541     widget_reorder (dlg_find (w->owner, w), FALSE);
 542 }
 543 
 544 /* --------------------------------------------------------------------------------------------- */
 545 /**
 546   * Check whether two widgets are overlapped or not.
 547   * @param a 1st widget
 548   * @param b 2nd widget
 549   *
 550   * @return TRUE if widgets are overlapped, FALSE otherwise.
 551   */
 552 
 553 gboolean
 554 widget_overlapped (const Widget * a, const Widget * b)
     /* [previous][next][first][last][top][bottom][index][help]  */
 555 {
 556     return !((b->x >= a->x + a->cols)
 557              || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
 558 }
 559 
 560 /* --------------------------------------------------------------------------------------------- */
 561 /* get mouse pointer location within widget */
 562 
 563 Gpm_Event
 564 mouse_get_local (const Gpm_Event * global, const Widget * w)
     /* [previous][next][first][last][top][bottom][index][help]  */
 565 {
 566     Gpm_Event local;
 567 
 568     local.buttons = global->buttons;
 569 #ifdef HAVE_LIBGPM
 570     local.clicks = 0;
 571     local.margin = 0;
 572     local.modifiers = 0;
 573     local.vc = 0;
 574 #endif
 575     local.x = global->x - w->x;
 576     local.y = global->y - w->y;
 577     local.type = global->type;
 578 
 579     return local;
 580 }
 581 
 582 /* --------------------------------------------------------------------------------------------- */
 583 
 584 gboolean
 585 mouse_global_in_widget (const Gpm_Event * event, const Widget * w)
     /* [previous][next][first][last][top][bottom][index][help]  */
 586 {
 587     return (event->x > w->x) && (event->y > w->y) && (event->x <= w->x + w->cols)
 588         && (event->y <= w->y + w->lines);
 589 }
 590 
 591 /* --------------------------------------------------------------------------------------------- */

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