Fix of f13 key handling.
[midnight-commander.git] / lib / widget / dialog.c
blob4c5a6c6279e13e9650d1d91c3ef40c038ec42730
1 /*
2 Dialog box features module for the Midnight Commander
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2007, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /** \file dialog.c
25 * \brief Source: dialog box features module
28 #include <config.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h> /* open() */
39 #include "lib/global.h"
41 #include "lib/tty/tty.h"
42 #include "lib/skin.h"
43 #include "lib/tty/mouse.h"
44 #include "lib/tty/key.h"
45 #include "lib/strutil.h"
46 #include "lib/widget.h"
47 #include "lib/fileloc.h" /* MC_HISTORY_FILE */
48 #include "lib/event.h" /* mc_event_raise() */
50 /*** global variables ****************************************************************************/
52 /* Color styles for normal and error dialogs */
53 dlg_colors_t dialog_colors;
54 dlg_colors_t alarm_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 (Dlg_head * 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 (Dlg_head * 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 (Dlg_head * 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, msg, 0);
151 while (first != p);
154 /* --------------------------------------------------------------------------------------------- */
157 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
159 static void
160 dlg_read_history (Dlg_head * 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);
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 int
182 dlg_unfocus (Dlg_head * h)
184 /* ... but can unfocus disabled widget */
186 if ((h->current != NULL) && (h->state == DLG_ACTIVE))
188 Widget *current = (Widget *) h->current->data;
190 if (send_message (current, WIDGET_UNFOCUS, 0) == MSG_HANDLED)
192 h->callback (h, current, DLG_UNFOCUS, 0, NULL);
193 return 1;
197 return 0;
200 /* --------------------------------------------------------------------------------------------- */
202 static int
203 dlg_find_widget_callback (const void *a, const void *b)
205 const Widget *w = (const Widget *) a;
206 callback_fn f = (callback_fn) b;
208 return (w->callback == f) ? 0 : 1;
211 /* --------------------------------------------------------------------------------------------- */
213 * Try to select another widget. If forward is set, follow tab order.
214 * Otherwise go to the previous widget.
217 static void
218 do_select_widget (Dlg_head * h, GList * w, select_dir_t dir)
220 Widget *w0 = (Widget *) h->current->data;
222 if (!dlg_unfocus (h))
223 return;
225 h->current = w;
229 if (dlg_focus (h))
230 break;
232 switch (dir)
234 case SELECT_EXACT:
235 h->current = g_list_find (h->widgets, w0);
236 if (dlg_focus (h))
237 return;
238 /* try find another widget that can take focus */
239 dir = SELECT_NEXT;
240 /* fallthrough */
241 case SELECT_NEXT:
242 h->current = dlg_widget_next (h, h->current);
243 break;
244 case SELECT_PREV:
245 h->current = dlg_widget_prev (h, h->current);
246 break;
249 while (h->current != w /* && (((Widget *) h->current->data)->options & W_DISABLED) == 0 */ );
251 if (dlg_overlap (w0, (Widget *) h->current->data))
253 send_message ((Widget *) h->current->data, WIDGET_DRAW, 0);
254 send_message ((Widget *) h->current->data, WIDGET_FOCUS, 0);
258 /* --------------------------------------------------------------------------------------------- */
260 static void
261 refresh_cmd (void)
263 #ifdef HAVE_SLANG
264 tty_touch_screen ();
265 mc_refresh ();
266 #else
267 /* Use this if the refreshes fail */
268 clr_scr ();
269 repaint_screen ();
270 #endif /* HAVE_SLANG */
273 /* --------------------------------------------------------------------------------------------- */
275 static cb_ret_t
276 dlg_execute_cmd (Dlg_head * h, unsigned long command)
278 cb_ret_t ret = MSG_HANDLED;
279 switch (command)
281 case CK_Ok:
282 h->ret_value = B_ENTER;
283 dlg_stop (h);
284 break;
285 case CK_Cancel:
286 h->ret_value = B_CANCEL;
287 dlg_stop (h);
288 break;
290 case CK_Up:
291 case CK_Left:
292 dlg_one_up (h);
293 break;
294 case CK_Down:
295 case CK_Right:
296 dlg_one_down (h);
297 break;
299 case CK_Help:
301 ev_help_t event_data = { NULL, h->help_ctx };
302 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
304 break;
306 case CK_Suspend:
307 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
308 refresh_cmd ();
309 break;
310 case CK_Refresh:
311 refresh_cmd ();
312 break;
314 case CK_ScreenList:
315 if (!h->modal)
316 dialog_switch_list ();
317 else
318 ret = MSG_NOT_HANDLED;
319 break;
320 case CK_ScreenNext:
321 if (!h->modal)
322 dialog_switch_next ();
323 else
324 ret = MSG_NOT_HANDLED;
325 break;
326 case CK_ScreenPrev:
327 if (!h->modal)
328 dialog_switch_prev ();
329 else
330 ret = MSG_NOT_HANDLED;
331 break;
333 default:
334 ret = MSG_NOT_HANDLED;
337 return ret;
340 /* --------------------------------------------------------------------------------------------- */
342 static cb_ret_t
343 dlg_handle_key (Dlg_head * h, int d_key)
345 unsigned long command;
346 command = keybind_lookup_keymap_command (dialog_map, d_key);
347 if ((command == CK_IgnoreKey) || (dlg_execute_cmd (h, command) == MSG_NOT_HANDLED))
348 return MSG_NOT_HANDLED;
349 else
350 return MSG_HANDLED;
353 /* --------------------------------------------------------------------------------------------- */
355 static int
356 dlg_mouse_event (Dlg_head * h, Gpm_Event * event)
358 GList *item;
359 GList *starting_widget = h->current;
360 Gpm_Event new_event;
361 int x = event->x;
362 int y = event->y;
364 /* close the dialog by mouse click out of dialog area */
365 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0) && ((event->type & GPM_DOWN) != 0) /* left click */
366 && !((x > h->x) && (x <= h->x + h->cols) && (y > h->y) && (y <= h->y + h->lines)))
368 h->ret_value = B_CANCEL;
369 dlg_stop (h);
370 return MOU_NORMAL;
373 item = starting_widget;
376 Widget *widget;
378 widget = (Widget *) item->data;
379 item = dlg_widget_next (h, item);
381 if (((widget->options & W_DISABLED) == 0)
382 && (x > widget->x) && (x <= widget->x + widget->cols)
383 && (y > widget->y) && (y <= widget->y + widget->lines))
385 new_event = *event;
386 new_event.x -= widget->x;
387 new_event.y -= widget->y;
389 if (widget->mouse != NULL)
390 return widget->mouse (&new_event, widget);
393 while (item != starting_widget);
395 return MOU_NORMAL;
398 /* --------------------------------------------------------------------------------------------- */
400 static cb_ret_t
401 dlg_try_hotkey (Dlg_head * h, int d_key)
403 GList *hot_cur;
404 Widget *current;
405 cb_ret_t handled;
406 int c;
408 if (h->widgets == NULL)
409 return MSG_NOT_HANDLED;
411 if (h->current == NULL)
412 h->current = h->widgets;
415 * Explanation: we don't send letter hotkeys to other widgets if
416 * the currently selected widget is an input line
419 current = (Widget *) h->current->data;
421 if ((current->options & W_DISABLED) != 0)
422 return MSG_NOT_HANDLED;
424 if (current->options & W_IS_INPUT)
426 /* skip ascii control characters, anything else can valid character in
427 * some encoding */
428 if (d_key >= 32 && d_key < 256)
429 return MSG_NOT_HANDLED;
432 /* If it's an alt key, send the message */
433 c = d_key & ~ALT (0);
434 if (d_key & ALT (0) && g_ascii_isalpha (c))
435 d_key = g_ascii_tolower (c);
437 handled = MSG_NOT_HANDLED;
438 if ((current->options & W_WANT_HOTKEY) != 0)
439 handled = send_message (current, WIDGET_HOTKEY, d_key);
441 /* If not used, send hotkey to other widgets */
442 if (handled == MSG_HANDLED)
443 return MSG_HANDLED;
445 hot_cur = dlg_widget_next (h, h->current);
447 /* send it to all widgets */
448 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
450 current = (Widget *) hot_cur->data;
452 if ((current->options & W_WANT_HOTKEY) != 0)
453 handled = send_message (current, WIDGET_HOTKEY, d_key);
455 if (handled == MSG_NOT_HANDLED)
456 hot_cur = dlg_widget_next (h, hot_cur);
459 if (handled == MSG_HANDLED)
460 do_select_widget (h, hot_cur, SELECT_EXACT);
462 return handled;
465 /* --------------------------------------------------------------------------------------------- */
467 static void
468 dlg_key_event (Dlg_head * h, int d_key)
470 cb_ret_t handled;
472 if (h->widgets == NULL)
473 return;
475 if (h->current == NULL)
476 h->current = h->widgets;
478 /* TAB used to cycle */
479 if ((h->flags & DLG_WANT_TAB) == 0)
481 if (d_key == '\t')
483 dlg_one_down (h);
484 return;
486 else if (d_key == KEY_BTAB)
488 dlg_one_up (h);
489 return;
493 /* first can dlg_callback handle the key */
494 handled = h->callback (h, NULL, DLG_KEY, d_key, NULL);
496 /* next try the hotkey */
497 if (handled == MSG_NOT_HANDLED)
498 handled = dlg_try_hotkey (h, d_key);
500 if (handled == MSG_HANDLED)
501 h->callback (h, NULL, DLG_HOTKEY_HANDLED, 0, NULL);
502 else
503 /* not used - then try widget_callback */
504 handled = send_message ((Widget *) h->current->data, WIDGET_KEY, d_key);
506 /* not used- try to use the unhandled case */
507 if (handled == MSG_NOT_HANDLED)
508 handled = h->callback (h, NULL, DLG_UNHANDLED_KEY, d_key, NULL);
510 if (handled == MSG_NOT_HANDLED)
511 handled = dlg_handle_key (h, d_key);
513 h->callback (h, NULL, DLG_POST_KEY, d_key, NULL);
516 /* --------------------------------------------------------------------------------------------- */
518 static void
519 frontend_run_dlg (Dlg_head * h)
521 int d_key;
522 Gpm_Event event;
524 event.x = -1;
526 /* close opened editors, viewers, etc */
527 if (!h->modal && mc_global.midnight_shutdown)
529 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
530 return;
533 while (h->state == DLG_ACTIVE)
535 if (mc_global.tty.winch_flag)
536 dialog_change_screen_size ();
538 if (is_idle ())
540 if (idle_hook)
541 execute_hooks (idle_hook);
543 while ((h->flags & DLG_WANT_IDLE) && is_idle ())
544 h->callback (h, NULL, DLG_IDLE, 0, NULL);
546 /* Allow terminating the dialog from the idle handler */
547 if (h->state != DLG_ACTIVE)
548 break;
551 update_cursor (h);
553 /* Clear interrupt flag */
554 tty_got_interrupt ();
555 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
557 dlg_process_event (h, d_key, &event);
559 if (h->state == DLG_CLOSED)
560 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
564 /* --------------------------------------------------------------------------------------------- */
566 static int
567 dlg_find_widget_by_id (gconstpointer a, gconstpointer b)
569 Widget *w = (Widget *) a;
570 unsigned long id = GPOINTER_TO_UINT (b);
572 return w->id == id ? 0 : 1;
575 /* --------------------------------------------------------------------------------------------- */
576 /*** public functions ****************************************************************************/
577 /* --------------------------------------------------------------------------------------------- */
579 /** draw box in window */
580 void
581 draw_box (Dlg_head * h, int y, int x, int ys, int xs, gboolean single)
583 tty_draw_box (h->y + y, h->x + x, ys, xs, single);
586 /* --------------------------------------------------------------------------------------------- */
588 /** Clean the dialog area, draw the frame and the title */
589 void
590 common_dialog_repaint (Dlg_head * h)
592 int space;
594 if (h->state != DLG_ACTIVE)
595 return;
597 space = (h->flags & DLG_COMPACT) ? 0 : 1;
599 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
600 dlg_erase (h);
601 draw_box (h, space, space, h->lines - 2 * space, h->cols - 2 * space, FALSE);
603 if (h->title != NULL)
605 tty_setcolor (h->color[DLG_COLOR_TITLE]);
606 dlg_move (h, space, (h->cols - str_term_width1 (h->title)) / 2);
607 tty_print_string (h->title);
611 /* --------------------------------------------------------------------------------------------- */
612 /** this function allows to set dialog position */
614 void
615 dlg_set_position (Dlg_head * h, int y1, int x1, int y2, int x2)
617 /* save old positions, will be used to reposition childs */
618 int ox, oy, oc, ol;
619 int shift_x, shift_y, scale_x, scale_y;
621 /* save old positions, will be used to reposition childs */
622 ox = h->x;
623 oy = h->y;
624 oc = h->cols;
625 ol = h->lines;
627 h->x = x1;
628 h->y = y1;
629 h->lines = y2 - y1;
630 h->cols = x2 - x1;
632 /* dialog is empty */
633 if (h->widgets == NULL)
634 return;
636 if (h->current == NULL)
637 h->current = h->widgets;
639 /* values by which controls should be moved */
640 shift_x = h->x - ox;
641 shift_y = h->y - oy;
642 scale_x = h->cols - oc;
643 scale_y = h->lines - ol;
645 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
647 GList *w;
649 for (w = h->widgets; w != NULL; w = g_list_next (w))
651 /* there are, mainly, 2 generally possible
652 situations:
654 1. control sticks to one side - it
655 should be moved
657 2. control sticks to two sides of
658 one direction - it should be sized */
660 Widget *c = (Widget *) w->data;
661 int x = c->x;
662 int y = c->y;
663 int cols = c->cols;
664 int lines = c->lines;
666 if ((c->pos_flags & WPOS_KEEP_LEFT) && (c->pos_flags & WPOS_KEEP_RIGHT))
668 x += shift_x;
669 cols += scale_x;
671 else if (c->pos_flags & WPOS_KEEP_LEFT)
672 x += shift_x;
673 else if (c->pos_flags & WPOS_KEEP_RIGHT)
674 x += shift_x + scale_x;
676 if ((c->pos_flags & WPOS_KEEP_TOP) && (c->pos_flags & WPOS_KEEP_BOTTOM))
678 y += shift_y;
679 lines += scale_y;
681 else if (c->pos_flags & WPOS_KEEP_TOP)
682 y += shift_y;
683 else if (c->pos_flags & WPOS_KEEP_BOTTOM)
684 y += shift_y + scale_y;
686 widget_set_size (c, y, x, lines, cols);
691 /* --------------------------------------------------------------------------------------------- */
692 /** this function sets only size, leaving positioning to automatic methods */
694 void
695 dlg_set_size (Dlg_head * h, int lines, int cols)
697 int x = h->x;
698 int y = h->y;
700 if (h->flags & DLG_CENTER)
702 y = (LINES - lines) / 2;
703 x = (COLS - cols) / 2;
706 if ((h->flags & DLG_TRYUP) && (y > 3))
707 y -= 2;
709 dlg_set_position (h, y, x, y + lines, x + cols);
712 /* --------------------------------------------------------------------------------------------- */
713 /** Default dialog callback */
715 cb_ret_t
716 default_dlg_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
718 (void) sender;
719 (void) parm;
720 (void) data;
722 switch (msg)
724 case DLG_DRAW:
725 if (h->color != NULL)
727 common_dialog_repaint (h);
728 return MSG_HANDLED;
730 return MSG_NOT_HANDLED;
732 case DLG_IDLE:
733 dlg_broadcast_msg_to (h, WIDGET_IDLE, FALSE, W_WANT_IDLE);
734 return MSG_HANDLED;
736 case DLG_RESIZE:
737 /* this is default resizing mechanism */
738 /* the main idea of this code is to resize dialog
739 according to flags (if any of flags require automatic
740 resizing, like DLG_CENTER, end after that reposition
741 controls in dialog according to flags of widget) */
742 dlg_set_size (h, h->lines, h->cols);
743 return MSG_HANDLED;
745 default:
746 break;
749 return MSG_NOT_HANDLED;
752 /* --------------------------------------------------------------------------------------------- */
754 Dlg_head *
755 create_dlg (gboolean modal, int y1, int x1, int lines, int cols,
756 const int *colors, dlg_cb_fn callback, const char *help_ctx,
757 const char *title, dlg_flags_t flags)
759 Dlg_head *new_d;
761 new_d = g_new0 (Dlg_head, 1);
762 new_d->modal = modal;
763 if (colors != NULL)
764 memmove (new_d->color, colors, sizeof (dlg_colors_t));
765 new_d->help_ctx = help_ctx;
766 new_d->callback = (callback != NULL) ? callback : default_dlg_callback;
767 new_d->x = x1;
768 new_d->y = y1;
769 new_d->flags = flags;
770 new_d->data = NULL;
772 dlg_set_size (new_d, lines, cols);
773 new_d->fullscreen = (new_d->x == 0 && new_d->y == 0
774 && new_d->cols == COLS && new_d->lines == LINES);
776 new_d->mouse_status = MOU_NORMAL;
778 /* Strip existing spaces, add one space before and after the title */
779 if (title != NULL)
781 char *t;
783 t = g_strstrip (g_strdup (title));
784 if (*t != '\0')
785 new_d->title = g_strdup_printf (" %s ", t);
786 g_free (t);
789 /* unique name got event group for this dialog */
790 new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
792 return new_d;
795 /* --------------------------------------------------------------------------------------------- */
797 void
798 dlg_set_default_colors (void)
800 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
801 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
802 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
803 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
804 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
806 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
807 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
808 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
809 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
810 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
813 /* --------------------------------------------------------------------------------------------- */
815 void
816 dlg_erase (Dlg_head * h)
818 if ((h != NULL) && (h->state == DLG_ACTIVE))
819 tty_fill_region (h->y, h->x, h->lines, h->cols, ' ');
822 /* --------------------------------------------------------------------------------------------- */
824 void
825 set_idle_proc (Dlg_head * d, int enable)
827 if (enable)
828 d->flags |= DLG_WANT_IDLE;
829 else
830 d->flags &= ~DLG_WANT_IDLE;
833 /* --------------------------------------------------------------------------------------------- */
835 * Insert widget to dialog before current widget. For dialogs populated
836 * from the bottom, make the widget current. Return widget number.
840 add_widget_autopos (Dlg_head * h, void *w, widget_pos_flags_t pos_flags)
842 Widget *widget = (Widget *) w;
844 /* Don't accept 0 widgets */
845 if (w == NULL)
846 abort ();
848 widget->x += h->x;
849 widget->y += h->y;
850 widget->owner = h;
851 widget->pos_flags = pos_flags;
852 widget->id = g_list_length (h->widgets);
854 if ((h->flags & DLG_REVERSE) != 0)
855 h->widgets = g_list_prepend (h->widgets, widget);
856 else
857 h->widgets = g_list_append (h->widgets, widget);
859 h->current = h->widgets;
861 return widget->id;
864 /* --------------------------------------------------------------------------------------------- */
865 /** wrapper to simply add lefttop positioned controls */
868 add_widget (Dlg_head * h, void *w)
870 return add_widget_autopos (h, w, WPOS_KEEP_LEFT | WPOS_KEEP_TOP);
873 /* --------------------------------------------------------------------------------------------- */
875 void
876 do_refresh (void)
878 GList *d = top_dlg;
880 if (fast_refresh)
882 if ((d != NULL) && (d->data != NULL))
883 dlg_redraw ((Dlg_head *) d->data);
885 else
887 /* Search first fullscreen dialog */
888 for (; d != NULL; d = g_list_next (d))
889 if ((d->data != NULL) && ((Dlg_head *) d->data)->fullscreen)
890 break;
891 /* back to top dialog */
892 for (; d != NULL; d = g_list_previous (d))
893 if (d->data != NULL)
894 dlg_redraw ((Dlg_head *) d->data);
898 /* --------------------------------------------------------------------------------------------- */
899 /** broadcast a message to all the widgets in a dialog */
901 void
902 dlg_broadcast_msg (Dlg_head * h, widget_msg_t msg, gboolean reverse)
904 dlg_broadcast_msg_to (h, msg, reverse, 0);
907 /* --------------------------------------------------------------------------------------------- */
910 dlg_focus (Dlg_head * h)
912 /* cannot focus disabled widget ... */
914 if ((h->current != NULL) && (h->state == DLG_ACTIVE))
916 Widget *current = (Widget *) h->current->data;
918 if (((current->options & W_DISABLED) == 0)
919 && (send_message (current, WIDGET_FOCUS, 0) == MSG_HANDLED))
921 h->callback (h, current, DLG_FOCUS, 0, NULL);
922 return 1;
926 return 0;
929 /* --------------------------------------------------------------------------------------------- */
930 /** Return true if the windows overlap */
933 dlg_overlap (Widget * a, Widget * b)
935 return !((b->x >= a->x + a->cols)
936 || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
940 /* --------------------------------------------------------------------------------------------- */
941 /** Find the widget with the given callback in the dialog h */
943 Widget *
944 find_widget_type (const Dlg_head * h, callback_fn callback)
946 GList *w;
948 w = g_list_find_custom (h->widgets, callback, dlg_find_widget_callback);
950 return (w == NULL) ? NULL : (Widget *) w->data;
953 /* --------------------------------------------------------------------------------------------- */
954 /** Find the widget with the given id */
956 Widget *
957 dlg_find_by_id (const Dlg_head * h, unsigned int id)
959 GList *w;
961 w = g_list_find_custom (h->widgets, GUINT_TO_POINTER (id), dlg_find_widget_by_id);
962 return w != NULL ? (Widget *) w->data : NULL;
965 /* --------------------------------------------------------------------------------------------- */
966 /** Find the widget with the given id in the dialog h and select it */
968 void
969 dlg_select_by_id (const Dlg_head * h, unsigned int id)
971 Widget *w;
973 w = dlg_find_by_id (h, id);
974 if (w != NULL)
975 dlg_select_widget (w);
978 /* --------------------------------------------------------------------------------------------- */
980 * Try to select widget in the dialog.
983 void
984 dlg_select_widget (void *w)
986 const Widget *widget = (Widget *) w;
987 Dlg_head *h = widget->owner;
989 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
992 /* --------------------------------------------------------------------------------------------- */
993 /** Try to select previous widget in the tab order */
995 void
996 dlg_one_up (Dlg_head * h)
998 if (h->widgets != NULL)
999 do_select_widget (h, dlg_widget_prev (h, h->current), SELECT_PREV);
1002 /* --------------------------------------------------------------------------------------------- */
1003 /** Try to select next widget in the tab order */
1005 void
1006 dlg_one_down (Dlg_head * h)
1008 if (h->widgets != NULL)
1009 do_select_widget (h, dlg_widget_next (h, h->current), SELECT_NEXT);
1012 /* --------------------------------------------------------------------------------------------- */
1014 void
1015 update_cursor (Dlg_head * h)
1017 GList *p = h->current;
1019 if ((p != NULL) && (h->state == DLG_ACTIVE))
1021 Widget *w;
1023 w = (Widget *) p->data;
1025 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1026 send_message (w, WIDGET_CURSOR, 0);
1027 else
1030 p = dlg_widget_next (h, p);
1031 if (p == h->current)
1032 break;
1034 w = (Widget *) p->data;
1036 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1037 if (send_message (w, WIDGET_CURSOR, 0) == MSG_HANDLED)
1038 break;
1040 while (TRUE);
1044 /* --------------------------------------------------------------------------------------------- */
1046 * Redraw the widgets in reverse order, leaving the current widget
1047 * as the last one
1050 void
1051 dlg_redraw (Dlg_head * h)
1053 if (h->state != DLG_ACTIVE)
1054 return;
1056 if (h->winch_pending)
1058 h->winch_pending = FALSE;
1059 h->callback (h, NULL, DLG_RESIZE, 0, NULL);
1062 h->callback (h, NULL, DLG_DRAW, 0, NULL);
1063 dlg_broadcast_msg (h, WIDGET_DRAW, TRUE);
1064 update_cursor (h);
1067 /* --------------------------------------------------------------------------------------------- */
1069 void
1070 dlg_stop (Dlg_head * h)
1072 h->state = DLG_CLOSED;
1075 /* --------------------------------------------------------------------------------------------- */
1076 /** Init the process */
1078 void
1079 init_dlg (Dlg_head * h)
1081 if ((top_dlg != NULL) && ((Dlg_head *) top_dlg->data)->modal)
1082 h->modal = TRUE;
1084 /* add dialog to the stack */
1085 top_dlg = g_list_prepend (top_dlg, h);
1087 /* Initialize dialog manager and widgets */
1088 if (h->state == DLG_ACTIVE)
1090 if (!h->modal)
1091 dialog_switch_add (h);
1093 h->callback (h, NULL, DLG_INIT, 0, NULL);
1094 dlg_broadcast_msg (h, WIDGET_INIT, FALSE);
1095 dlg_read_history (h);
1098 h->state = DLG_ACTIVE;
1100 dlg_redraw (h);
1102 /* Select the first widget that takes focus */
1103 while (h->current != NULL && !dlg_focus (h))
1104 h->current = dlg_widget_next (h, h->current);
1106 h->ret_value = 0;
1109 /* --------------------------------------------------------------------------------------------- */
1111 void
1112 dlg_process_event (Dlg_head * h, int key, Gpm_Event * event)
1114 if (key == EV_NONE)
1116 if (tty_got_interrupt ())
1117 if (h->callback (h, NULL, DLG_ACTION, CK_Cancel, NULL) != MSG_HANDLED)
1118 dlg_execute_cmd (h, CK_Cancel);
1120 return;
1123 if (key == EV_MOUSE)
1124 h->mouse_status = dlg_mouse_event (h, event);
1125 else
1126 dlg_key_event (h, key);
1129 /* --------------------------------------------------------------------------------------------- */
1130 /** Shutdown the run_dlg */
1132 void
1133 dlg_run_done (Dlg_head * h)
1135 top_dlg = g_list_remove (top_dlg, h);
1137 if (h->state == DLG_CLOSED)
1139 h->callback (h, (Widget *) h->current->data, DLG_END, 0, NULL);
1140 if (!h->modal)
1141 dialog_switch_remove (h);
1146 /* --------------------------------------------------------------------------------------------- */
1148 * Standard run dialog routine
1149 * We have to keep this routine small so that we can duplicate it's
1150 * behavior on complex routines like the file routines, this way,
1151 * they can call the dlg_process_event without rewriting all the code
1155 run_dlg (Dlg_head * h)
1157 init_dlg (h);
1158 frontend_run_dlg (h);
1159 dlg_run_done (h);
1160 return h->ret_value;
1163 /* --------------------------------------------------------------------------------------------- */
1165 void
1166 destroy_dlg (Dlg_head * h)
1168 /* if some widgets have history, save all history at one moment here */
1169 dlg_save_history (h);
1170 dlg_broadcast_msg (h, WIDGET_DESTROY, FALSE);
1171 g_list_foreach (h->widgets, (GFunc) g_free, NULL);
1172 g_list_free (h->widgets);
1173 mc_event_group_del (h->event_group);
1174 g_free (h->event_group);
1175 g_free (h->title);
1176 g_free (h);
1178 do_refresh ();
1181 /* --------------------------------------------------------------------------------------------- */
1184 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1186 void
1187 dlg_save_history (Dlg_head * h)
1189 char *profile;
1190 int i;
1192 if (num_history_items_recorded == 0) /* this is how to disable */
1193 return;
1195 profile = mc_config_get_full_path (MC_HISTORY_FILE);
1196 i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
1197 if (i != -1)
1198 close (i);
1200 /* Make sure the history is only readable by the user */
1201 if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
1203 ev_history_load_save_t event_data;
1205 event_data.cfg = mc_config_init (profile);
1206 event_data.receiver = NULL;
1208 /* get all histories in dialog */
1209 mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
1211 mc_config_save_file (event_data.cfg, NULL);
1212 mc_config_deinit (event_data.cfg);
1215 g_free (profile);
1218 /* --------------------------------------------------------------------------------------------- */
1220 char *
1221 dlg_get_title (const Dlg_head * h, size_t len)
1223 char *t;
1225 if (h == NULL)
1226 abort ();
1228 if (h->get_title != NULL)
1229 t = h->get_title (h, len);
1230 else
1231 t = g_strdup ("");
1233 return t;
1236 /* --------------------------------------------------------------------------------------------- */
1237 /** Replace widget old_w for widget new_w in the dialog */
1239 void
1240 dlg_replace_widget (Widget * old_w, Widget * new_w)
1242 Dlg_head *h = old_w->owner;
1243 gboolean should_focus = FALSE;
1245 if (h->widgets == NULL)
1246 return;
1248 if (h->current == NULL)
1249 h->current = h->widgets;
1251 if (old_w == h->current->data)
1252 should_focus = TRUE;
1254 new_w->owner = h;
1255 new_w->id = old_w->id;
1257 if (should_focus)
1258 h->current->data = new_w;
1259 else
1260 g_list_find (h->widgets, old_w)->data = new_w;
1262 send_message (old_w, WIDGET_DESTROY, 0);
1263 send_message (new_w, WIDGET_INIT, 0);
1265 if (should_focus)
1266 dlg_select_widget (new_w);
1268 send_message (new_w, WIDGET_DRAW, 0);
1271 /* --------------------------------------------------------------------------------------------- */