Merge branch '3643_cons_handler_min_macro'
[midnight-commander.git] / lib / widget / dialog.c
blob46ca178e0070ef020bc126fe521bf6c772c49d2c
1 /*
2 Dialog box features module for the Midnight Commander
4 Copyright (C) 1994-2014
5 Free Software Foundation, Inc.
7 This file is part of the Midnight Commander.
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.
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.
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/>.
23 /** \file dialog.c
24 * \brief Source: dialog box features module
27 #include <config.h>
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>
37 #include "lib/global.h"
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() */
46 #include "lib/widget.h"
47 #include "lib/widget/mouse.h"
49 /*** global variables ****************************************************************************/
51 /* Color styles for normal and error dialogs */
52 dlg_colors_t dialog_colors;
53 dlg_colors_t alarm_colors;
54 dlg_colors_t listbox_colors;
56 /* Primitive way to check if the the current dialog is our dialog */
57 /* This is needed by async routines like load_prompt */
58 GList *top_dlg = NULL;
60 /* A hook list for idle events */
61 hook_t *idle_hook = NULL;
63 /* If set then dialogs just clean the screen when refreshing, else */
64 /* they do a complete refresh, refreshing all the parts of the program */
65 int fast_refresh = 0;
67 /* left click outside of dialog closes it */
68 int mouse_close_dialog = 0;
70 const global_keymap_t *dialog_map = NULL;
72 /*** file scope macro definitions ****************************************************************/
74 /*** file scope type declarations ****************************************************************/
76 /** What to do if the requested widget doesn't take focus */
77 typedef enum
79 SELECT_NEXT, /* go the the next widget */
80 SELECT_PREV, /* go the the previous widget */
81 SELECT_EXACT /* use current widget */
82 } select_dir_t;
84 /*** file scope variables ************************************************************************/
86 /*** file scope functions ************************************************************************/
88 static GList *
89 dlg_widget_next (WDialog * h, GList * l)
91 GList *next;
93 next = g_list_next (l);
94 if (next == NULL)
95 next = h->widgets;
97 return next;
100 /* --------------------------------------------------------------------------------------------- */
102 static GList *
103 dlg_widget_prev (WDialog * h, GList * l)
105 GList *prev;
107 prev = g_list_previous (l);
108 if (prev == NULL)
109 prev = g_list_last (h->widgets);
111 return prev;
114 /* --------------------------------------------------------------------------------------------- */
116 * broadcast a message to all the widgets in a dialog that have
117 * the options set to flags. If flags is zero, the message is sent
118 * to all widgets.
121 static void
122 dlg_broadcast_msg_to (WDialog * h, widget_msg_t msg, gboolean reverse, int flags)
124 GList *p, *first;
126 if (h->widgets == NULL)
127 return;
129 if (h->current == NULL)
130 h->current = h->widgets;
132 if (reverse)
133 p = dlg_widget_prev (h, h->current);
134 else
135 p = dlg_widget_next (h, h->current);
137 first = p;
141 Widget *w = WIDGET (p->data);
143 if (reverse)
144 p = dlg_widget_prev (h, p);
145 else
146 p = dlg_widget_next (h, p);
148 if ((flags == 0) || ((flags & w->options) != 0))
149 send_message (w, NULL, msg, 0, NULL);
151 while (first != p);
154 /* --------------------------------------------------------------------------------------------- */
157 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
159 static void
160 dlg_read_history (WDialog * h)
162 char *profile;
163 ev_history_load_save_t event_data;
165 if (num_history_items_recorded == 0) /* this is how to disable */
166 return;
168 profile = mc_config_get_full_path (MC_HISTORY_FILE);
169 event_data.cfg = mc_config_init (profile, TRUE);
170 event_data.receiver = NULL;
172 /* create all histories in dialog */
173 mc_event_raise (h->event_group, MCEVENT_HISTORY_LOAD, &event_data);
175 mc_config_deinit (event_data.cfg);
176 g_free (profile);
179 /* --------------------------------------------------------------------------------------------- */
181 static gboolean
182 dlg_unfocus (WDialog * h)
184 /* we can unfocus disabled widget */
185 if ((h->current != NULL) && (h->state == DLG_CONSTRUCT || h->state == DLG_ACTIVE))
187 Widget *current = WIDGET (h->current->data);
189 if (send_message (current, NULL, MSG_UNFOCUS, 0, NULL) == MSG_HANDLED)
191 send_message (h, current, MSG_UNFOCUS, 0, NULL);
192 return TRUE;
196 return FALSE;
199 /* --------------------------------------------------------------------------------------------- */
201 static int
202 dlg_find_widget_callback (const void *a, const void *b)
204 const Widget *w = CONST_WIDGET (a);
205 const widget_cb_fn f = b;
207 return (w->callback == f) ? 0 : 1;
210 /* --------------------------------------------------------------------------------------------- */
212 * Try to select another widget. If forward is set, follow tab order.
213 * Otherwise go to the previous widget.
216 static void
217 do_select_widget (WDialog * h, GList * w, select_dir_t dir)
219 Widget *w0 = WIDGET (h->current->data);
221 if (!dlg_unfocus (h))
222 return;
224 h->current = w;
228 if (dlg_focus (h))
229 break;
231 switch (dir)
233 case SELECT_EXACT:
234 h->current = g_list_find (h->widgets, w0);
235 if (dlg_focus (h))
236 return;
237 /* try find another widget that can take focus */
238 dir = SELECT_NEXT;
239 /* fallthrough */
240 case SELECT_NEXT:
241 h->current = dlg_widget_next (h, h->current);
242 break;
243 case SELECT_PREV:
244 h->current = dlg_widget_prev (h, h->current);
245 break;
246 default:
247 break;
250 while (h->current != w /* && (WIDGET (h->current->data)->options & W_DISABLED) == 0 */ );
252 if (widget_overlapped (w0, WIDGET (h->current->data)))
254 send_message (h->current->data, NULL, MSG_DRAW, 0, NULL);
255 send_message (h->current->data, NULL, MSG_FOCUS, 0, NULL);
259 /* --------------------------------------------------------------------------------------------- */
261 static void
262 refresh_cmd (void)
264 #ifdef HAVE_SLANG
265 tty_touch_screen ();
266 mc_refresh ();
267 #else
268 /* Use this if the refreshes fail */
269 clr_scr ();
270 repaint_screen ();
271 #endif /* HAVE_SLANG */
274 /* --------------------------------------------------------------------------------------------- */
276 static cb_ret_t
277 dlg_execute_cmd (WDialog * h, long command)
279 cb_ret_t ret = MSG_HANDLED;
280 switch (command)
282 case CK_Ok:
283 h->ret_value = B_ENTER;
284 dlg_stop (h);
285 break;
286 case CK_Cancel:
287 h->ret_value = B_CANCEL;
288 dlg_stop (h);
289 break;
291 case CK_Up:
292 case CK_Left:
293 dlg_one_up (h);
294 break;
295 case CK_Down:
296 case CK_Right:
297 dlg_one_down (h);
298 break;
300 case CK_Help:
302 ev_help_t event_data = { NULL, h->help_ctx };
303 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
305 break;
307 case CK_Suspend:
308 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
309 refresh_cmd ();
310 break;
311 case CK_Refresh:
312 refresh_cmd ();
313 break;
315 case CK_ScreenList:
316 if (!h->modal)
317 dialog_switch_list ();
318 else
319 ret = MSG_NOT_HANDLED;
320 break;
321 case CK_ScreenNext:
322 if (!h->modal)
323 dialog_switch_next ();
324 else
325 ret = MSG_NOT_HANDLED;
326 break;
327 case CK_ScreenPrev:
328 if (!h->modal)
329 dialog_switch_prev ();
330 else
331 ret = MSG_NOT_HANDLED;
332 break;
334 default:
335 ret = MSG_NOT_HANDLED;
338 return ret;
341 /* --------------------------------------------------------------------------------------------- */
343 static cb_ret_t
344 dlg_handle_key (WDialog * h, int d_key)
346 long command;
348 command = keybind_lookup_keymap_command (dialog_map, d_key);
350 if (command == CK_IgnoreKey)
351 return MSG_NOT_HANDLED;
353 if (send_message (h, NULL, MSG_ACTION, command, NULL) == MSG_HANDLED
354 || dlg_execute_cmd (h, command) == MSG_HANDLED)
355 return MSG_HANDLED;
357 return MSG_NOT_HANDLED;
360 /* --------------------------------------------------------------------------------------------- */
362 * This is the low-level mouse handler.
363 * It receives a Gpm_Event event and translates it into a higher level protocol.
365 static int
366 dlg_mouse_translator (Gpm_Event * event, Widget * w)
368 mouse_event_t me;
370 me = mouse_translate_event (w, event);
372 return mouse_process_event (w, &me);
375 /* --------------------------------------------------------------------------------------------- */
377 static int
378 dlg_mouse_event (WDialog * h, Gpm_Event * event)
380 Widget *wh = WIDGET (h);
382 GList *p;
384 /* close the dialog by mouse left click out of dialog area */
385 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0)
386 && ((event->type & GPM_DOWN) != 0) && !mouse_global_in_widget (event, wh))
388 h->ret_value = B_CANCEL;
389 dlg_stop (h);
390 return MOU_NORMAL;
393 if (wh->mouse_callback != NULL)
395 int mou;
397 mou = dlg_mouse_translator (event, wh);
398 if (mou != MOU_UNHANDLED)
399 return mou;
402 /* send the event to widgets in reverse Z-order */
403 p = g_list_last (h->widgets);
406 Widget *w = WIDGET (p->data);
408 if ((w->options & W_DISABLED) == 0 && w->mouse_callback != NULL)
410 /* put global cursor position to the widget */
411 int ret;
413 ret = dlg_mouse_translator (event, w);
414 if (ret != MOU_UNHANDLED)
415 return ret;
418 p = g_list_previous (p);
420 while (p != NULL);
422 return MOU_UNHANDLED;
425 /* --------------------------------------------------------------------------------------------- */
427 static cb_ret_t
428 dlg_try_hotkey (WDialog * h, int d_key)
430 GList *hot_cur;
431 Widget *current;
432 cb_ret_t handled;
433 int c;
435 if (h->widgets == NULL)
436 return MSG_NOT_HANDLED;
438 if (h->current == NULL)
439 h->current = h->widgets;
442 * Explanation: we don't send letter hotkeys to other widgets if
443 * the currently selected widget is an input line
446 current = WIDGET (h->current->data);
448 if ((current->options & W_DISABLED) != 0)
449 return MSG_NOT_HANDLED;
451 if (current->options & W_IS_INPUT)
453 /* skip ascii control characters, anything else can valid character in
454 * some encoding */
455 if (d_key >= 32 && d_key < 256)
456 return MSG_NOT_HANDLED;
459 /* If it's an alt key, send the message */
460 c = d_key & ~ALT (0);
461 if (d_key & ALT (0) && g_ascii_isalpha (c))
462 d_key = g_ascii_tolower (c);
464 handled = MSG_NOT_HANDLED;
465 if ((current->options & W_WANT_HOTKEY) != 0)
466 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
468 /* If not used, send hotkey to other widgets */
469 if (handled == MSG_HANDLED)
470 return MSG_HANDLED;
472 hot_cur = dlg_widget_next (h, h->current);
474 /* send it to all widgets */
475 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
477 current = WIDGET (hot_cur->data);
479 if ((current->options & W_WANT_HOTKEY) != 0 && (current->options & W_DISABLED) == 0)
480 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
482 if (handled == MSG_NOT_HANDLED)
483 hot_cur = dlg_widget_next (h, hot_cur);
486 if (handled == MSG_HANDLED)
487 do_select_widget (h, hot_cur, SELECT_EXACT);
489 return handled;
492 /* --------------------------------------------------------------------------------------------- */
494 static void
495 dlg_key_event (WDialog * h, int d_key)
497 cb_ret_t handled;
499 if (h->widgets == NULL)
500 return;
502 if (h->current == NULL)
503 h->current = h->widgets;
505 /* TAB used to cycle */
506 if ((h->flags & DLG_WANT_TAB) == 0)
508 if (d_key == '\t')
510 dlg_one_down (h);
511 return;
513 else if ((d_key & ~(KEY_M_SHIFT | KEY_M_CTRL)) == '\t')
515 dlg_one_up (h);
516 return;
520 /* first can dlg_callback handle the key */
521 handled = send_message (h, NULL, MSG_KEY, d_key, NULL);
523 /* next try the hotkey */
524 if (handled == MSG_NOT_HANDLED)
525 handled = dlg_try_hotkey (h, d_key);
527 if (handled == MSG_HANDLED)
528 send_message (h, NULL, MSG_HOTKEY_HANDLED, 0, NULL);
529 else
530 /* not used - then try widget_callback */
531 handled = send_message (h->current->data, NULL, MSG_KEY, d_key, NULL);
533 /* not used- try to use the unhandled case */
534 if (handled == MSG_NOT_HANDLED)
535 handled = send_message (h, NULL, MSG_UNHANDLED_KEY, d_key, NULL);
537 if (handled == MSG_NOT_HANDLED)
538 handled = dlg_handle_key (h, d_key);
540 (void) handled;
541 send_message (h, NULL, MSG_POST_KEY, d_key, NULL);
544 /* --------------------------------------------------------------------------------------------- */
546 static void
547 frontend_dlg_run (WDialog * h)
549 Gpm_Event event;
551 event.x = -1;
553 /* close opened editors, viewers, etc */
554 if (!h->modal && mc_global.midnight_shutdown)
556 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
557 return;
560 while (h->state == DLG_ACTIVE)
562 int d_key;
564 if (mc_global.tty.winch_flag != 0)
565 dialog_change_screen_size ();
567 if (is_idle ())
569 if (idle_hook)
570 execute_hooks (idle_hook);
572 while ((WIDGET (h)->options & W_WANT_IDLE) != 0 && is_idle ())
573 send_message (h, NULL, MSG_IDLE, 0, NULL);
575 /* Allow terminating the dialog from the idle handler */
576 if (h->state != DLG_ACTIVE)
577 break;
580 update_cursor (h);
582 /* Clear interrupt flag */
583 tty_got_interrupt ();
584 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
586 dlg_process_event (h, d_key, &event);
588 if (h->state == DLG_CLOSED)
589 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
593 /* --------------------------------------------------------------------------------------------- */
595 static int
596 dlg_find_widget_by_id (gconstpointer a, gconstpointer b)
598 const Widget *w = CONST_WIDGET (a);
599 unsigned long id = GPOINTER_TO_UINT (b);
601 return w->id == id ? 0 : 1;
604 /* --------------------------------------------------------------------------------------------- */
606 static void
607 dlg_set_top_or_bottom_widget (void *w, gboolean set_top)
609 Widget *widget = WIDGET (w);
610 WDialog *h = widget->owner;
611 GList *l;
613 l = g_list_find (h->widgets, w);
614 if (l == NULL)
615 abort (); /* widget is not in dialog, this should not happen */
617 /* unfocus prevoius widget and focus current one before widget reordering */
618 if (set_top && h->state == DLG_ACTIVE)
619 do_select_widget (h, l, SELECT_EXACT);
621 /* widget reordering */
622 h->widgets = g_list_remove_link (h->widgets, l);
623 if (set_top)
624 h->widgets = g_list_concat (h->widgets, l);
625 else
626 h->widgets = g_list_concat (l, h->widgets);
629 /* --------------------------------------------------------------------------------------------- */
630 /*** public functions ****************************************************************************/
631 /* --------------------------------------------------------------------------------------------- */
633 /** Clean the dialog area, draw the frame and the title */
634 void
635 dlg_default_repaint (WDialog * h)
637 Widget *wh = WIDGET (h);
639 int space;
641 if (h->state != DLG_ACTIVE)
642 return;
644 space = (h->flags & DLG_COMPACT) ? 0 : 1;
646 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
647 dlg_erase (h);
648 tty_draw_box (wh->y + space, wh->x + space, wh->lines - 2 * space, wh->cols - 2 * space, FALSE);
650 if (h->title != NULL)
652 tty_setcolor (h->color[DLG_COLOR_TITLE]);
653 widget_move (h, space, (wh->cols - str_term_width1 (h->title)) / 2);
654 tty_print_string (h->title);
658 /* --------------------------------------------------------------------------------------------- */
659 /** this function allows to set dialog position */
661 void
662 dlg_set_position (WDialog * h, int y1, int x1, int y2, int x2)
664 Widget *wh = WIDGET (h);
666 /* save old positions, will be used to reposition childs */
667 int ox, oy, oc, ol;
668 int shift_x, shift_y, scale_x, scale_y;
670 /* save old positions, will be used to reposition childs */
671 ox = wh->x;
672 oy = wh->y;
673 oc = wh->cols;
674 ol = wh->lines;
676 wh->x = x1;
677 wh->y = y1;
678 wh->lines = y2 - y1;
679 wh->cols = x2 - x1;
681 /* dialog is empty */
682 if (h->widgets == NULL)
683 return;
685 if (h->current == NULL)
686 h->current = h->widgets;
688 /* values by which controls should be moved */
689 shift_x = wh->x - ox;
690 shift_y = wh->y - oy;
691 scale_x = wh->cols - oc;
692 scale_y = wh->lines - ol;
694 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
696 GList *w;
698 for (w = h->widgets; w != NULL; w = g_list_next (w))
700 /* there are, mainly, 2 generally possible
701 situations:
703 1. control sticks to one side - it
704 should be moved
706 2. control sticks to two sides of
707 one direction - it should be sized */
709 Widget *c = WIDGET (w->data);
710 int x = c->x;
711 int y = c->y;
712 int cols = c->cols;
713 int lines = c->lines;
715 if ((c->pos_flags & WPOS_CENTER_HORZ) != 0)
716 x = wh->x + (wh->cols - c->cols) / 2;
717 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0 && (c->pos_flags & WPOS_KEEP_RIGHT) != 0)
719 x += shift_x;
720 cols += scale_x;
722 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0)
723 x += shift_x;
724 else if ((c->pos_flags & WPOS_KEEP_RIGHT) != 0)
725 x += shift_x + scale_x;
727 if ((c->pos_flags & WPOS_CENTER_VERT) != 0)
728 y = wh->y + (wh->lines - c->lines) / 2;
729 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0 && (c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
731 y += shift_y;
732 lines += scale_y;
734 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0)
735 y += shift_y;
736 else if ((c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
737 y += shift_y + scale_y;
739 widget_set_size (c, y, x, lines, cols);
744 /* --------------------------------------------------------------------------------------------- */
745 /** Set dialog size and position */
747 void
748 dlg_set_size (WDialog * h, int lines, int cols)
750 int x = WIDGET (h)->x;
751 int y = WIDGET (h)->y;
753 if ((h->flags & DLG_CENTER) != 0)
755 y = (LINES - lines) / 2;
756 x = (COLS - cols) / 2;
759 if ((h->flags & DLG_TRYUP) != 0)
761 if (y > 3)
762 y -= 2;
763 else if (y == 3)
764 y = 2;
767 dlg_set_position (h, y, x, y + lines, x + cols);
770 /* --------------------------------------------------------------------------------------------- */
771 /** Default dialog callback */
773 cb_ret_t
774 dlg_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
776 WDialog *h = DIALOG (w);
778 (void) sender;
779 (void) parm;
780 (void) data;
782 switch (msg)
784 case MSG_DRAW:
785 if (h->color != NULL)
787 dlg_default_repaint (h);
788 return MSG_HANDLED;
790 return MSG_NOT_HANDLED;
792 case MSG_IDLE:
793 dlg_broadcast_msg_to (h, MSG_IDLE, FALSE, W_WANT_IDLE);
794 return MSG_HANDLED;
796 case MSG_RESIZE:
797 /* this is default resizing mechanism */
798 /* the main idea of this code is to resize dialog
799 according to flags (if any of flags require automatic
800 resizing, like DLG_CENTER, end after that reposition
801 controls in dialog according to flags of widget) */
802 dlg_set_size (h, w->lines, w->cols);
803 return MSG_HANDLED;
805 default:
806 break;
809 return MSG_NOT_HANDLED;
812 /* --------------------------------------------------------------------------------------------- */
814 WDialog *
815 dlg_create (gboolean modal, int y1, int x1, int lines, int cols,
816 const int *colors, widget_cb_fn callback, widget_mouse_cb_fn mouse_callback,
817 const char *help_ctx, const char *title, dlg_flags_t flags)
819 WDialog *new_d;
820 Widget *w;
822 new_d = g_new0 (WDialog, 1);
823 w = WIDGET (new_d);
824 widget_init (w, y1, x1, lines, cols, (callback != NULL) ? callback : dlg_default_callback,
825 mouse_callback);
826 widget_want_cursor (w, FALSE);
828 new_d->state = DLG_CONSTRUCT;
829 new_d->modal = modal;
830 new_d->color = colors;
831 new_d->help_ctx = help_ctx;
832 new_d->flags = flags;
833 new_d->data = NULL;
835 dlg_set_size (new_d, lines, cols);
836 new_d->fullscreen = (w->x == 0 && w->y == 0 && w->cols == COLS && w->lines == LINES);
838 new_d->mouse_status = MOU_UNHANDLED;
840 /* Strip existing spaces, add one space before and after the title */
841 if (title != NULL && *title != '\0')
843 char *t;
845 t = g_strstrip (g_strdup (title));
846 if (*t != '\0')
847 new_d->title = g_strdup_printf (" %s ", t);
848 g_free (t);
851 /* unique name of event group for this dialog */
852 new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
854 return new_d;
857 /* --------------------------------------------------------------------------------------------- */
859 void
860 dlg_set_default_colors (void)
862 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
863 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
864 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
865 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
866 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
868 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
869 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
870 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
871 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
872 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
874 listbox_colors[DLG_COLOR_NORMAL] = PMENU_ENTRY_COLOR;
875 listbox_colors[DLG_COLOR_FOCUS] = PMENU_SELECTED_COLOR;
876 listbox_colors[DLG_COLOR_HOT_NORMAL] = PMENU_ENTRY_COLOR;
877 listbox_colors[DLG_COLOR_HOT_FOCUS] = PMENU_SELECTED_COLOR;
878 listbox_colors[DLG_COLOR_TITLE] = PMENU_TITLE_COLOR;
881 /* --------------------------------------------------------------------------------------------- */
883 void
884 dlg_erase (WDialog * h)
886 if ((h != NULL) && (h->state == DLG_ACTIVE))
888 Widget *wh = WIDGET (h);
890 tty_fill_region (wh->y, wh->x, wh->lines, wh->cols, ' ');
894 /* --------------------------------------------------------------------------------------------- */
896 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
899 unsigned long
900 add_widget_autopos (WDialog * h, void *w, widget_pos_flags_t pos_flags, const void *before)
902 Widget *wh = WIDGET (h);
903 Widget *widget;
905 /* Don't accept 0 widgets */
906 if (w == NULL)
907 abort ();
909 widget = WIDGET (w);
911 if ((pos_flags & WPOS_CENTER_HORZ) != 0)
912 widget->x = (wh->cols - widget->cols) / 2;
913 widget->x += wh->x;
915 if ((pos_flags & WPOS_CENTER_VERT) != 0)
916 widget->y = (wh->lines - widget->lines) / 2;
917 widget->y += wh->y;
919 widget->owner = h;
920 widget->pos_flags = pos_flags;
921 widget->id = h->widget_id++;
923 if (h->widgets == NULL || before == NULL)
925 h->widgets = g_list_append (h->widgets, widget);
926 h->current = g_list_last (h->widgets);
928 else
930 GList *b;
932 b = g_list_find (h->widgets, before);
934 /* don't accept widget not from dialog. This shouldn't happen */
935 if (b == NULL)
936 abort ();
938 b = g_list_next (b);
939 h->widgets = g_list_insert_before (h->widgets, b, widget);
940 if (b != NULL)
941 h->current = g_list_previous (b);
942 else
943 h->current = g_list_last (h->widgets);
946 /* widget has been added in runtime */
947 if (h->state == DLG_ACTIVE)
949 send_message (widget, NULL, MSG_INIT, 0, NULL);
950 send_message (widget, NULL, MSG_DRAW, 0, NULL);
951 send_message (widget, NULL, MSG_FOCUS, 0, NULL);
954 return widget->id;
957 /* --------------------------------------------------------------------------------------------- */
958 /** wrapper to simply add lefttop positioned controls */
960 unsigned long
961 add_widget (WDialog * h, void *w)
963 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT,
964 h->current != NULL ? h->current->data : NULL);
967 /* --------------------------------------------------------------------------------------------- */
969 unsigned long
970 add_widget_before (WDialog * h, void *w, void *before)
972 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT, before);
975 /* --------------------------------------------------------------------------------------------- */
977 /** delete widget from dialog */
978 void
979 del_widget (void *w)
981 WDialog *h;
982 GList *d;
984 /* Don't accept NULL widget. This shouldn't happen */
985 if (w == NULL)
986 abort ();
988 h = WIDGET (w)->owner;
990 d = g_list_find (h->widgets, w);
991 if (d == h->current)
992 h->current = dlg_widget_next (h, d);
994 h->widgets = g_list_remove_link (h->widgets, d);
995 send_message (d->data, NULL, MSG_DESTROY, 0, NULL);
996 g_free (d->data);
997 g_list_free_1 (d);
999 /* widget has been deleted in runtime */
1000 if (h->state == DLG_ACTIVE)
1002 dlg_redraw (h);
1003 dlg_focus (h);
1007 /* --------------------------------------------------------------------------------------------- */
1009 void
1010 do_refresh (void)
1012 GList *d = top_dlg;
1014 if (fast_refresh)
1016 if ((d != NULL) && (d->data != NULL))
1017 dlg_redraw (DIALOG (d->data));
1019 else
1021 /* Search first fullscreen dialog */
1022 for (; d != NULL; d = g_list_next (d))
1023 if (d->data != NULL && DIALOG (d->data)->fullscreen)
1024 break;
1025 /* back to top dialog */
1026 for (; d != NULL; d = g_list_previous (d))
1027 if (d->data != NULL)
1028 dlg_redraw (DIALOG (d->data));
1032 /* --------------------------------------------------------------------------------------------- */
1033 /** broadcast a message to all the widgets in a dialog */
1035 void
1036 dlg_broadcast_msg (WDialog * h, widget_msg_t msg)
1038 dlg_broadcast_msg_to (h, msg, FALSE, 0);
1041 /* --------------------------------------------------------------------------------------------- */
1043 gboolean
1044 dlg_focus (WDialog * h)
1046 /* cannot focus disabled widget */
1047 if ((h->current != NULL) && (h->state == DLG_CONSTRUCT || h->state == DLG_ACTIVE))
1049 Widget *current = WIDGET (h->current->data);
1051 if (((current->options & W_DISABLED) == 0)
1052 && (send_message (current, NULL, MSG_FOCUS, 0, NULL) == MSG_HANDLED))
1054 send_message (h, current, MSG_FOCUS, 0, NULL);
1055 return TRUE;
1059 return FALSE;
1062 /* --------------------------------------------------------------------------------------------- */
1063 /** Find the widget with the given callback in the dialog h */
1065 Widget *
1066 find_widget_type (const WDialog * h, widget_cb_fn callback)
1068 GList *w;
1070 w = g_list_find_custom (h->widgets, (gconstpointer) callback, dlg_find_widget_callback);
1072 return (w == NULL) ? NULL : WIDGET (w->data);
1075 /* --------------------------------------------------------------------------------------------- */
1076 /** Find the widget with the given id */
1078 Widget *
1079 dlg_find_by_id (const WDialog * h, unsigned long id)
1081 GList *w;
1083 w = g_list_find_custom (h->widgets, GUINT_TO_POINTER (id), dlg_find_widget_by_id);
1084 return w != NULL ? WIDGET (w->data) : NULL;
1087 /* --------------------------------------------------------------------------------------------- */
1088 /** Find the widget with the given id in the dialog h and select it */
1090 void
1091 dlg_select_by_id (const WDialog * h, unsigned long id)
1093 Widget *w;
1095 w = dlg_find_by_id (h, id);
1096 if (w != NULL)
1097 dlg_select_widget (w);
1100 /* --------------------------------------------------------------------------------------------- */
1102 * Try to select widget in the dialog.
1105 void
1106 dlg_select_widget (void *w)
1108 Widget *widget = WIDGET (w);
1109 WDialog *h = widget->owner;
1111 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
1114 /* --------------------------------------------------------------------------------------------- */
1116 * Set widget at top of widget list and make it current.
1119 void
1120 dlg_set_top_widget (void *w)
1122 dlg_set_top_or_bottom_widget (w, TRUE);
1125 /* --------------------------------------------------------------------------------------------- */
1127 * Set widget at bottom of widget list.
1130 void
1131 dlg_set_bottom_widget (void *w)
1133 dlg_set_top_or_bottom_widget (w, FALSE);
1136 /* --------------------------------------------------------------------------------------------- */
1137 /** Try to select previous widget in the tab order */
1139 void
1140 dlg_one_up (WDialog * h)
1142 if (h->widgets != NULL)
1143 do_select_widget (h, dlg_widget_prev (h, h->current), SELECT_PREV);
1146 /* --------------------------------------------------------------------------------------------- */
1147 /** Try to select next widget in the tab order */
1149 void
1150 dlg_one_down (WDialog * h)
1152 if (h->widgets != NULL)
1153 do_select_widget (h, dlg_widget_next (h, h->current), SELECT_NEXT);
1156 /* --------------------------------------------------------------------------------------------- */
1158 void
1159 update_cursor (WDialog * h)
1161 GList *p = h->current;
1163 if ((p != NULL) && (h->state == DLG_ACTIVE))
1165 Widget *w;
1167 w = WIDGET (p->data);
1169 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1170 send_message (w, NULL, MSG_CURSOR, 0, NULL);
1171 else
1174 p = dlg_widget_next (h, p);
1175 if (p == h->current)
1176 break;
1178 w = WIDGET (p->data);
1180 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1181 if (send_message (w, NULL, MSG_CURSOR, 0, NULL) == MSG_HANDLED)
1182 break;
1184 while (TRUE);
1188 /* --------------------------------------------------------------------------------------------- */
1190 * Redraw the widgets in reverse order, leaving the current widget
1191 * as the last one
1194 void
1195 dlg_redraw (WDialog * h)
1197 if (h->state != DLG_ACTIVE)
1198 return;
1200 if (h->winch_pending)
1202 h->winch_pending = FALSE;
1203 send_message (h, NULL, MSG_RESIZE, 0, NULL);
1206 send_message (h, NULL, MSG_DRAW, 0, NULL);
1207 dlg_broadcast_msg (h, MSG_DRAW);
1208 update_cursor (h);
1211 /* --------------------------------------------------------------------------------------------- */
1213 void
1214 dlg_stop (WDialog * h)
1216 h->state = DLG_CLOSED;
1219 /* --------------------------------------------------------------------------------------------- */
1220 /** Init the process */
1222 void
1223 dlg_init (WDialog * h)
1225 if (top_dlg != NULL && DIALOG (top_dlg->data)->modal)
1226 h->modal = TRUE;
1228 /* add dialog to the stack */
1229 top_dlg = g_list_prepend (top_dlg, h);
1231 /* Initialize dialog manager and widgets */
1232 if (h->state == DLG_CONSTRUCT)
1234 if (!h->modal)
1235 dialog_switch_add (h);
1237 send_message (h, NULL, MSG_INIT, 0, NULL);
1238 dlg_broadcast_msg (h, MSG_INIT);
1239 dlg_read_history (h);
1242 h->state = DLG_ACTIVE;
1244 /* first send MSG_DRAW to dialog itself and all widgets... */
1245 dlg_redraw (h);
1247 /* ...then send MSG_FOCUS to select the first widget that can take focus */
1248 while (h->current != NULL && !dlg_focus (h))
1249 h->current = dlg_widget_next (h, h->current);
1252 h->ret_value = 0;
1255 /* --------------------------------------------------------------------------------------------- */
1257 void
1258 dlg_process_event (WDialog * h, int key, Gpm_Event * event)
1260 if (key == EV_NONE)
1262 if (tty_got_interrupt ())
1263 if (send_message (h, NULL, MSG_ACTION, CK_Cancel, NULL) != MSG_HANDLED)
1264 dlg_execute_cmd (h, CK_Cancel);
1266 else if (key == EV_MOUSE)
1267 h->mouse_status = dlg_mouse_event (h, event);
1268 else
1269 dlg_key_event (h, key);
1272 /* --------------------------------------------------------------------------------------------- */
1273 /** Shutdown the dlg_run */
1275 void
1276 dlg_run_done (WDialog * h)
1278 top_dlg = g_list_remove (top_dlg, h);
1280 if (h->state == DLG_CLOSED)
1282 send_message (h, h->current->data, MSG_END, 0, NULL);
1283 if (!h->modal)
1284 dialog_switch_remove (h);
1288 /* --------------------------------------------------------------------------------------------- */
1290 * Standard run dialog routine
1291 * We have to keep this routine small so that we can duplicate it's
1292 * behavior on complex routines like the file routines, this way,
1293 * they can call the dlg_process_event without rewriting all the code
1297 dlg_run (WDialog * h)
1299 dlg_init (h);
1300 frontend_dlg_run (h);
1301 dlg_run_done (h);
1302 return h->ret_value;
1305 /* --------------------------------------------------------------------------------------------- */
1307 void
1308 dlg_destroy (WDialog * h)
1310 /* if some widgets have history, save all history at one moment here */
1311 dlg_save_history (h);
1312 dlg_broadcast_msg (h, MSG_DESTROY);
1313 g_list_free_full (h->widgets, g_free);
1314 mc_event_group_del (h->event_group);
1315 g_free (h->event_group);
1316 g_free (h->title);
1317 g_free (h);
1319 do_refresh ();
1322 /* --------------------------------------------------------------------------------------------- */
1325 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1327 void
1328 dlg_save_history (WDialog * h)
1330 char *profile;
1331 int i;
1333 if (num_history_items_recorded == 0) /* this is how to disable */
1334 return;
1336 profile = mc_config_get_full_path (MC_HISTORY_FILE);
1337 i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
1338 if (i != -1)
1339 close (i);
1341 /* Make sure the history is only readable by the user */
1342 if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
1344 ev_history_load_save_t event_data;
1346 event_data.cfg = mc_config_init (profile, FALSE);
1347 event_data.receiver = NULL;
1349 /* get all histories in dialog */
1350 mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
1352 mc_config_save_file (event_data.cfg, NULL);
1353 mc_config_deinit (event_data.cfg);
1356 g_free (profile);
1359 /* --------------------------------------------------------------------------------------------- */
1361 char *
1362 dlg_get_title (const WDialog * h, size_t len)
1364 char *t;
1366 if (h == NULL)
1367 abort ();
1369 if (h->get_title != NULL)
1370 t = h->get_title (h, len);
1371 else
1372 t = g_strdup ("");
1374 return t;
1377 /* --------------------------------------------------------------------------------------------- */