root/lib/widget/dialog.c

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

DEFINITIONS

This source file includes following definitions.
  1. dlg_default_get_colors
  2. dlg_read_history
  3. refresh_cmd
  4. dlg_help
  5. dlg_execute_cmd
  6. dlg_handle_key
  7. dlg_key_event
  8. dlg_handle_mouse_event
  9. frontend_dlg_run
  10. dlg_default_destroy
  11. dlg_default_callback
  12. dlg_default_mouse_callback
  13. dlg_create
  14. dlg_set_default_colors
  15. dlg_close
  16. dlg_init
  17. dlg_process_event
  18. dlg_run_done
  19. dlg_run
  20. dlg_save_history
  21. dlg_get_title

   1 /*
   2    Dialog box features module for the Midnight Commander
   3 
   4    Copyright (C) 1994-2024
   5    Free Software Foundation, Inc.
   6 
   7    This file is part of the Midnight Commander.
   8 
   9    The Midnight Commander is free software: you can redistribute it
  10    and/or modify it under the terms of the GNU General Public License as
  11    published by the Free Software Foundation, either version 3 of the License,
  12    or (at your option) any later version.
  13 
  14    The Midnight Commander is distributed in the hope that it will be useful,
  15    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17    GNU General Public License for more details.
  18 
  19    You should have received a copy of the GNU General Public License
  20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21  */
  22 
  23 /** \file dialog.c
  24  *  \brief Source: dialog box features module
  25  */
  26 
  27 #include <config.h>
  28 
  29 #include <ctype.h>
  30 #include <errno.h>
  31 #include <stdlib.h>
  32 #include <stdio.h>
  33 #include <string.h>
  34 #include <sys/types.h>
  35 #include <sys/stat.h>
  36 
  37 #include "lib/global.h"
  38 
  39 #include "lib/tty/tty.h"
  40 #include "lib/skin.h"
  41 #include "lib/tty/key.h"
  42 #include "lib/strutil.h"
  43 #include "lib/fileloc.h"        /* MC_HISTORY_FILE */
  44 #include "lib/event.h"          /* mc_event_raise() */
  45 #include "lib/util.h"           /* MC_PTR_FREE */
  46 #include "lib/mcconfig.h"       /* num_history_items_recorded */
  47 
  48 #include "lib/widget.h"
  49 #include "lib/widget/mouse.h"
  50 
  51 /*** global variables ****************************************************************************/
  52 
  53 /* Color styles for normal and error dialogs */
  54 dlg_colors_t dialog_colors;
  55 dlg_colors_t alarm_colors;
  56 dlg_colors_t listbox_colors;
  57 
  58 /* A hook list for idle events */
  59 hook_t *idle_hook = NULL;
  60 
  61 /* left click outside of dialog closes it */
  62 gboolean mouse_close_dialog = FALSE;
  63 
  64 const global_keymap_t *dialog_map = NULL;
  65 
  66 /*** file scope macro definitions ****************************************************************/
  67 
  68 /*** file scope type declarations ****************************************************************/
  69 
  70 /*** forward declarations (file scope functions) *************************************************/
  71 
  72 /*** file scope variables ************************************************************************/
  73 
  74 /* --------------------------------------------------------------------------------------------- */
  75 /*** file scope functions ************************************************************************/
  76 /* --------------------------------------------------------------------------------------------- */
  77 
  78 static const int *
  79 dlg_default_get_colors (const Widget *w)
     /* [previous][next][first][last][top][bottom][index][help]  */
  80 {
  81     return CONST_DIALOG (w)->colors;
  82 }
  83 
  84 /* --------------------------------------------------------------------------------------------- */
  85 /**
  86   * Read histories from the ${XDG_DATA_HOME}/mc/history file
  87   */
  88 static void
  89 dlg_read_history (WDialog *h)
     /* [previous][next][first][last][top][bottom][index][help]  */
  90 {
  91     char *profile;
  92     ev_history_load_save_t event_data;
  93 
  94     if (num_history_items_recorded == 0)        /* this is how to disable */
  95         return;
  96 
  97     profile = mc_config_get_full_path (MC_HISTORY_FILE);
  98     event_data.cfg = mc_config_init (profile, TRUE);
  99     event_data.receiver = NULL;
 100 
 101     /* create all histories in dialog */
 102     mc_event_raise (h->event_group, MCEVENT_HISTORY_LOAD, &event_data);
 103 
 104     mc_config_deinit (event_data.cfg);
 105     g_free (profile);
 106 }
 107 
 108 /* --------------------------------------------------------------------------------------------- */
 109 
 110 static void
 111 refresh_cmd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 112 {
 113 #ifdef HAVE_SLANG
 114     tty_touch_screen ();
 115     mc_refresh ();
 116 #else
 117     /* Use this if the refreshes fail */
 118     tty_clear_screen ();
 119     repaint_screen ();
 120 #endif /* HAVE_SLANG */
 121 }
 122 
 123 /* --------------------------------------------------------------------------------------------- */
 124 
 125 static void
 126 dlg_help (const WDialog *h)
     /* [previous][next][first][last][top][bottom][index][help]  */
 127 {
 128     ev_help_t event_data = { NULL, h->help_ctx };
 129 
 130     mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
 131 }
 132 
 133 /* --------------------------------------------------------------------------------------------- */
 134 
 135 static cb_ret_t
 136 dlg_execute_cmd (WDialog *h, long command)
     /* [previous][next][first][last][top][bottom][index][help]  */
 137 {
 138     WGroup *g = GROUP (h);
 139     cb_ret_t ret = MSG_HANDLED;
 140 
 141     if (send_message (h, NULL, MSG_ACTION, command, NULL) == MSG_HANDLED)
 142         return MSG_HANDLED;
 143 
 144     switch (command)
 145     {
 146     case CK_Ok:
 147         h->ret_value = B_ENTER;
 148         dlg_close (h);
 149         break;
 150     case CK_Cancel:
 151         h->ret_value = B_CANCEL;
 152         dlg_close (h);
 153         break;
 154 
 155     case CK_Up:
 156     case CK_Left:
 157         group_select_prev_widget (g);
 158         break;
 159     case CK_Down:
 160     case CK_Right:
 161         group_select_next_widget (g);
 162         break;
 163 
 164     case CK_Help:
 165         dlg_help (h);
 166         break;
 167 
 168     case CK_Suspend:
 169         mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
 170         refresh_cmd ();
 171         break;
 172     case CK_Refresh:
 173         refresh_cmd ();
 174         break;
 175 
 176     case CK_ScreenList:
 177         if (!widget_get_state (WIDGET (h), WST_MODAL))
 178             dialog_switch_list ();
 179         else
 180             ret = MSG_NOT_HANDLED;
 181         break;
 182     case CK_ScreenNext:
 183         if (!widget_get_state (WIDGET (h), WST_MODAL))
 184             dialog_switch_next ();
 185         else
 186             ret = MSG_NOT_HANDLED;
 187         break;
 188     case CK_ScreenPrev:
 189         if (!widget_get_state (WIDGET (h), WST_MODAL))
 190             dialog_switch_prev ();
 191         else
 192             ret = MSG_NOT_HANDLED;
 193         break;
 194 
 195     default:
 196         ret = MSG_NOT_HANDLED;
 197     }
 198 
 199     return ret;
 200 }
 201 
 202 /* --------------------------------------------------------------------------------------------- */
 203 
 204 static cb_ret_t
 205 dlg_handle_key (WDialog *h, int d_key)
     /* [previous][next][first][last][top][bottom][index][help]  */
 206 {
 207     long command;
 208 
 209     command = widget_lookup_key (WIDGET (h), d_key);
 210     if (command == CK_IgnoreKey)
 211         command = keybind_lookup_keymap_command (dialog_map, d_key);
 212     if (command != CK_IgnoreKey)
 213         return dlg_execute_cmd (h, command);
 214 
 215     return MSG_NOT_HANDLED;
 216 }
 217 
 218 /* --------------------------------------------------------------------------------------------- */
 219 
 220 static void
 221 dlg_key_event (WDialog *h, int d_key)
     /* [previous][next][first][last][top][bottom][index][help]  */
 222 {
 223     Widget *w = WIDGET (h);
 224     WGroup *g = GROUP (h);
 225     cb_ret_t handled;
 226 
 227     if (g->widgets == NULL)
 228         return;
 229 
 230     if (g->current == NULL)
 231         g->current = g->widgets;
 232 
 233     /* TAB used to cycle */
 234     if (!widget_get_options (w, WOP_WANT_TAB))
 235     {
 236         if (d_key == '\t')
 237         {
 238             group_select_next_widget (g);
 239             return;
 240         }
 241         else if ((d_key & ~(KEY_M_SHIFT | KEY_M_CTRL)) == '\t')
 242         {
 243             group_select_prev_widget (g);
 244             return;
 245         }
 246     }
 247 
 248     /* first can dlalog handle the key itself */
 249     handled = send_message (h, NULL, MSG_KEY, d_key, NULL);
 250 
 251     if (handled == MSG_NOT_HANDLED)
 252         handled = group_default_callback (w, NULL, MSG_KEY, d_key, NULL);
 253 
 254     if (handled == MSG_NOT_HANDLED)
 255         handled = dlg_handle_key (h, d_key);
 256 
 257     (void) handled;
 258     send_message (h, NULL, MSG_POST_KEY, d_key, NULL);
 259 }
 260 
 261 /* --------------------------------------------------------------------------------------------- */
 262 
 263 static int
 264 dlg_handle_mouse_event (Widget *w, Gpm_Event *event)
     /* [previous][next][first][last][top][bottom][index][help]  */
 265 {
 266     if (w->mouse_callback != NULL)
 267     {
 268         int mou;
 269 
 270         mou = mouse_handle_event (w, event);
 271         if (mou != MOU_UNHANDLED)
 272             return mou;
 273     }
 274 
 275     return group_handle_mouse_event (w, event);
 276 }
 277 
 278 /* --------------------------------------------------------------------------------------------- */
 279 
 280 static void
 281 frontend_dlg_run (WDialog *h)
     /* [previous][next][first][last][top][bottom][index][help]  */
 282 {
 283     Widget *wh = WIDGET (h);
 284     Gpm_Event event;
 285 
 286     event.x = -1;
 287 
 288     /* close opened editors, viewers, etc */
 289     if (!widget_get_state (wh, WST_MODAL) && mc_global.midnight_shutdown)
 290     {
 291         send_message (h, NULL, MSG_VALIDATE, 0, NULL);
 292         return;
 293     }
 294 
 295     while (widget_get_state (wh, WST_ACTIVE))
 296     {
 297         int d_key;
 298 
 299         if (tty_got_winch ())
 300             dialog_change_screen_size ();
 301 
 302         if (is_idle ())
 303         {
 304             if (idle_hook)
 305                 execute_hooks (idle_hook);
 306 
 307             while (widget_get_state (wh, WST_IDLE) && is_idle ())
 308                 send_message (wh, NULL, MSG_IDLE, 0, NULL);
 309 
 310             /* Allow terminating the dialog from the idle handler */
 311             if (!widget_get_state (wh, WST_ACTIVE))
 312                 break;
 313         }
 314 
 315         widget_update_cursor (wh);
 316 
 317         /* Clear interrupt flag */
 318         tty_got_interrupt ();
 319         d_key = tty_get_event (&event, GROUP (h)->mouse_status == MOU_REPEAT, TRUE);
 320 
 321         dlg_process_event (h, d_key, &event);
 322 
 323         if (widget_get_state (wh, WST_CLOSED))
 324             send_message (h, NULL, MSG_VALIDATE, 0, NULL);
 325     }
 326 }
 327 
 328 /* --------------------------------------------------------------------------------------------- */
 329 
 330 static void
 331 dlg_default_destroy (Widget *w)
     /* [previous][next][first][last][top][bottom][index][help]  */
 332 {
 333     WDialog *h = DIALOG (w);
 334 
 335     /* if some widgets have history, save all histories at one moment here */
 336     dlg_save_history (h);
 337     group_default_callback (w, NULL, MSG_DESTROY, 0, NULL);
 338     send_message (w, NULL, MSG_DESTROY, 0, NULL);
 339     mc_event_group_del (h->event_group);
 340     g_free (h->event_group);
 341     g_free (h);
 342 
 343     do_refresh ();
 344 }
 345 
 346 /* --------------------------------------------------------------------------------------------- */
 347 /*** public functions ****************************************************************************/
 348 /* --------------------------------------------------------------------------------------------- */
 349 /** Default dialog callback */
 350 
 351 cb_ret_t
 352 dlg_default_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 353 {
 354     switch (msg)
 355     {
 356     case MSG_INIT:
 357         /* nothing to init in dialog itself */
 358         return MSG_HANDLED;
 359 
 360     case MSG_IDLE:
 361         /* we don't want endless loop */
 362         widget_idle (w, FALSE);
 363         return MSG_HANDLED;
 364 
 365     case MSG_DESTROY:
 366         /* nothing to deinit in dialog itself */
 367         return MSG_HANDLED;
 368 
 369     default:
 370         return group_default_callback (w, sender, msg, parm, data);
 371     }
 372 }
 373 
 374 /* --------------------------------------------------------------------------------------------- */
 375 
 376 void
 377 dlg_default_mouse_callback (Widget *w, mouse_msg_t msg, mouse_event_t *event)
     /* [previous][next][first][last][top][bottom][index][help]  */
 378 {
 379     switch (msg)
 380     {
 381     case MSG_MOUSE_CLICK:
 382         if (event->y < 0 || event->y >= w->rect.lines || event->x < 0 || event->x >= w->rect.cols)
 383         {
 384             DIALOG (w)->ret_value = B_CANCEL;
 385             dlg_close (DIALOG (w));
 386         }
 387         break;
 388 
 389     default:
 390         /* return MOU_UNHANDLED */
 391         event->result.abort = TRUE;
 392         break;
 393     }
 394 }
 395 
 396 /* --------------------------------------------------------------------------------------------- */
 397 
 398 WDialog *
 399 dlg_create (gboolean modal, int y1, int x1, int lines, int cols, widget_pos_flags_t pos_flags,
     /* [previous][next][first][last][top][bottom][index][help]  */
 400             gboolean compact, const int *colors, widget_cb_fn callback,
 401             widget_mouse_cb_fn mouse_callback, const char *help_ctx, const char *title)
 402 {
 403     WRect r = { y1, x1, lines, cols };
 404     WDialog *new_d;
 405     Widget *w;
 406     WGroup *g;
 407 
 408     new_d = g_new0 (WDialog, 1);
 409     w = WIDGET (new_d);
 410     g = GROUP (new_d);
 411     widget_adjust_position (pos_flags, &r);
 412     group_init (g, &r, callback != NULL ? callback : dlg_default_callback,
 413                 mouse_callback != NULL ? mouse_callback : dlg_default_mouse_callback);
 414 
 415     w->pos_flags = pos_flags;
 416     w->options |= WOP_SELECTABLE | WOP_TOP_SELECT;
 417     w->state |= WST_FOCUSED;
 418     /* Temporary hack: dialog doesn't have an owner, own itself. */
 419     w->owner = g;
 420 
 421     w->keymap = dialog_map;
 422 
 423     w->mouse_handler = dlg_handle_mouse_event;
 424     w->mouse.forced_capture = mouse_close_dialog && (w->pos_flags & WPOS_FULLSCREEN) == 0;
 425 
 426     w->destroy = dlg_default_destroy;
 427     w->get_colors = dlg_default_get_colors;
 428 
 429     new_d->colors = colors;
 430     new_d->help_ctx = help_ctx;
 431     new_d->compact = compact;
 432     new_d->data.p = NULL;
 433 
 434     if (modal)
 435     {
 436         w->state |= WST_MODAL;
 437 
 438         new_d->bg =
 439             WIDGET (frame_new (0, 0, w->rect.lines, w->rect.cols, title, FALSE, new_d->compact));
 440         group_add_widget (g, new_d->bg);
 441         frame_set_title (FRAME (new_d->bg), title);
 442     }
 443 
 444     /* unique name of event group for this dialog */
 445     new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
 446 
 447     return new_d;
 448 }
 449 
 450 /* --------------------------------------------------------------------------------------------- */
 451 
 452 void
 453 dlg_set_default_colors (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 454 {
 455     dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
 456     dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
 457     dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
 458     dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
 459     dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
 460 
 461     alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
 462     alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
 463     alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
 464     alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
 465     alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
 466 
 467     listbox_colors[DLG_COLOR_NORMAL] = PMENU_ENTRY_COLOR;
 468     listbox_colors[DLG_COLOR_FOCUS] = PMENU_SELECTED_COLOR;
 469     listbox_colors[DLG_COLOR_HOT_NORMAL] = PMENU_ENTRY_COLOR;
 470     listbox_colors[DLG_COLOR_HOT_FOCUS] = PMENU_SELECTED_COLOR;
 471     listbox_colors[DLG_COLOR_TITLE] = PMENU_TITLE_COLOR;
 472 }
 473 
 474 /* --------------------------------------------------------------------------------------------- */
 475 
 476 void
 477 dlg_close (WDialog *h)
     /* [previous][next][first][last][top][bottom][index][help]  */
 478 {
 479     widget_set_state (WIDGET (h), WST_CLOSED, TRUE);
 480 }
 481 
 482 /* --------------------------------------------------------------------------------------------- */
 483 /** Init the process */
 484 
 485 void
 486 dlg_init (WDialog *h)
     /* [previous][next][first][last][top][bottom][index][help]  */
 487 {
 488     WGroup *g = GROUP (h);
 489     Widget *wh = WIDGET (h);
 490 
 491     if (top_dlg != NULL && widget_get_state (WIDGET (top_dlg->data), WST_MODAL))
 492         widget_set_state (wh, WST_MODAL, TRUE);
 493 
 494     /* add dialog to the stack */
 495     top_dlg = g_list_prepend (top_dlg, h);
 496 
 497     /* Initialize dialog manager and widgets */
 498     if (widget_get_state (wh, WST_CONSTRUCT))
 499     {
 500         if (!widget_get_state (wh, WST_MODAL))
 501             dialog_switch_add (h);
 502 
 503         send_message (h, NULL, MSG_INIT, 0, NULL);
 504         group_default_callback (wh, NULL, MSG_INIT, 0, NULL);
 505         dlg_read_history (h);
 506     }
 507 
 508     /* Select the first widget that takes focus */
 509     while (g->current != NULL && !widget_is_focusable (g->current->data))
 510         group_set_current_widget_next (g);
 511 
 512     widget_set_state (wh, WST_ACTIVE, TRUE);
 513     widget_draw (wh);
 514 
 515     h->ret_value = 0;
 516 }
 517 
 518 /* --------------------------------------------------------------------------------------------- */
 519 
 520 void
 521 dlg_process_event (WDialog *h, int key, Gpm_Event *event)
     /* [previous][next][first][last][top][bottom][index][help]  */
 522 {
 523     switch (key)
 524     {
 525     case EV_NONE:
 526         if (tty_got_interrupt ())
 527             dlg_execute_cmd (h, CK_Cancel);
 528         break;
 529 
 530     case EV_MOUSE:
 531         {
 532             Widget *w = WIDGET (h);
 533 
 534             GROUP (h)->mouse_status = w->mouse_handler (w, event);
 535             break;
 536         }
 537 
 538     default:
 539         dlg_key_event (h, key);
 540         break;
 541     }
 542 }
 543 
 544 /* --------------------------------------------------------------------------------------------- */
 545 /** Shutdown the dlg_run */
 546 
 547 void
 548 dlg_run_done (WDialog *h)
     /* [previous][next][first][last][top][bottom][index][help]  */
 549 {
 550     top_dlg = g_list_remove (top_dlg, h);
 551 
 552     if (widget_get_state (WIDGET (h), WST_CLOSED))
 553     {
 554         send_message (h, GROUP (h)->current == NULL ? NULL : WIDGET (GROUP (h)->current->data),
 555                       MSG_END, 0, NULL);
 556         if (!widget_get_state (WIDGET (h), WST_MODAL))
 557             dialog_switch_remove (h);
 558     }
 559 }
 560 
 561 /* --------------------------------------------------------------------------------------------- */
 562 /**
 563  * Standard run dialog routine
 564  * We have to keep this routine small so that we can duplicate it's
 565  * behavior on complex routines like the file routines, this way,
 566  * they can call the dlg_process_event without rewriting all the code
 567  */
 568 
 569 int
 570 dlg_run (WDialog *h)
     /* [previous][next][first][last][top][bottom][index][help]  */
 571 {
 572     dlg_init (h);
 573     frontend_dlg_run (h);
 574     dlg_run_done (h);
 575     return h->ret_value;
 576 }
 577 
 578 /* --------------------------------------------------------------------------------------------- */
 579 
 580 /**
 581   * Write history to the ${XDG_DATA_HOME}/mc/history file
 582   */
 583 void
 584 dlg_save_history (WDialog *h)
     /* [previous][next][first][last][top][bottom][index][help]  */
 585 {
 586     char *profile;
 587     int i;
 588 
 589     if (num_history_items_recorded == 0)        /* this is how to disable */
 590         return;
 591 
 592     profile = mc_config_get_full_path (MC_HISTORY_FILE);
 593     i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
 594     if (i != -1)
 595         close (i);
 596 
 597     /* Make sure the history is only readable by the user */
 598     if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
 599     {
 600         ev_history_load_save_t event_data;
 601 
 602         event_data.cfg = mc_config_init (profile, FALSE);
 603         event_data.receiver = NULL;
 604 
 605         /* get all histories in dialog */
 606         mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
 607 
 608         mc_config_save_file (event_data.cfg, NULL);
 609         mc_config_deinit (event_data.cfg);
 610     }
 611 
 612     g_free (profile);
 613 }
 614 
 615 /* --------------------------------------------------------------------------------------------- */
 616 
 617 char *
 618 dlg_get_title (const WDialog *h, size_t len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 619 {
 620     char *t;
 621 
 622     if (h == NULL)
 623         abort ();
 624 
 625     if (h->get_title != NULL)
 626         t = h->get_title (h, len);
 627     else
 628         t = g_strdup ("");
 629 
 630     return t;
 631 }
 632 
 633 /* --------------------------------------------------------------------------------------------- */

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