Ticket #2598: u7z: Improve handling of missing p7zip binaries.
[midnight-commander.git] / lib / widget / dialog.c
blob8032f3b405def98cf91c9487f7b5f9cc20e3c85b
1 /* Dialog box features module for the Midnight Commander
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2009, 2010 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 /** \file dialog.c
21 * \brief Source: dialog box features module
24 #include <config.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h> /* open() */
35 #include "lib/global.h"
37 #include "lib/tty/tty.h"
38 #include "lib/skin.h"
39 #include "lib/tty/mouse.h"
40 #include "lib/tty/key.h"
41 #include "lib/strutil.h"
42 #include "lib/widget.h"
43 #include "lib/fileloc.h" /* MC_HISTORY_FILE */
44 #include "lib/event.h" /* mc_event_raise() */
46 /*** global variables ****************************************************************************/
48 /* Color styles for normal and error dialogs */
49 dlg_colors_t dialog_colors;
50 dlg_colors_t alarm_colors;
52 /* Primitive way to check if the the current dialog is our dialog */
53 /* This is needed by async routines like load_prompt */
54 GList *top_dlg = NULL;
56 /* A hook list for idle events */
57 hook_t *idle_hook = NULL;
59 /* If set then dialogs just clean the screen when refreshing, else */
60 /* they do a complete refresh, refreshing all the parts of the program */
61 int fast_refresh = 0;
63 /* left click outside of dialog closes it */
64 int mouse_close_dialog = 0;
66 const global_keymap_t *dialog_map = NULL;
68 /*** file scope macro definitions ****************************************************************/
70 /*** file scope type declarations ****************************************************************/
72 /** What to do if the requested widget doesn't take focus */
73 typedef enum
75 SELECT_NEXT, /* go the the next widget */
76 SELECT_PREV, /* go the the previous widget */
77 SELECT_EXACT /* use current widget */
78 } select_dir_t;
80 /*** file scope variables ************************************************************************/
82 /*** file scope functions ************************************************************************/
84 static GList *
85 dlg_widget_next (Dlg_head *h, GList *l)
87 GList *next;
89 next = g_list_next (l);
90 if (next == NULL)
91 next = h->widgets;
93 return next;
96 /* --------------------------------------------------------------------------------------------- */
98 static GList *
99 dlg_widget_prev (Dlg_head *h, GList *l)
101 GList *prev;
103 prev = g_list_previous (l);
104 if (prev == NULL)
105 prev = g_list_last (h->widgets);
107 return prev;
110 /* --------------------------------------------------------------------------------------------- */
112 * broadcast a message to all the widgets in a dialog that have
113 * the options set to flags. If flags is zero, the message is sent
114 * to all widgets.
117 static void
118 dlg_broadcast_msg_to (Dlg_head * h, widget_msg_t msg, gboolean reverse, int flags)
120 GList *p, *first;
122 if (h->widgets == NULL)
123 return;
125 if (h->current == NULL)
126 h->current = h->widgets;
128 if (reverse)
129 p = dlg_widget_prev (h, h->current);
130 else
131 p = dlg_widget_next (h, h->current);
133 first = p;
137 Widget *w = (Widget *) p->data;
139 if (reverse)
140 p = dlg_widget_prev (h, p);
141 else
142 p = dlg_widget_next (h, p);
144 if ((flags == 0) || ((flags & w->options) != 0))
145 send_message (w, msg, 0);
147 while (first != p);
150 /* --------------------------------------------------------------------------------------------- */
153 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
155 static void
156 dlg_read_history (Dlg_head * h)
158 char *profile;
159 ev_history_load_save_t event_data;
161 if (num_history_items_recorded == 0) /* this is how to disable */
162 return;
164 profile = g_build_filename (mc_config_get_cache_path (), MC_HISTORY_FILE, NULL);
165 event_data.cfg = mc_config_init (profile);
166 event_data.receiver = NULL;
168 /* create all histories in dialog */
169 mc_event_raise (h->event_group, MCEVENT_HISTORY_LOAD, &event_data);
171 mc_config_deinit (event_data.cfg);
172 g_free (profile);
175 /* --------------------------------------------------------------------------------------------- */
177 static int
178 dlg_unfocus (Dlg_head * h)
180 /* ... but can unfocus disabled widget */
182 if ((h->current != NULL) && (h->state == DLG_ACTIVE))
184 Widget *current = (Widget *) h->current->data;
186 if (send_message (current, WIDGET_UNFOCUS, 0) == MSG_HANDLED)
188 h->callback (h, current, DLG_UNFOCUS, 0, NULL);
189 return 1;
193 return 0;
196 /* --------------------------------------------------------------------------------------------- */
198 static int
199 dlg_find_widget_callback (const void *a, const void *b)
201 const Widget *w = (const Widget *) a;
202 callback_fn f = (callback_fn) b;
204 return (w->callback == f) ? 0 : 1;
207 /* --------------------------------------------------------------------------------------------- */
209 * Try to select another widget. If forward is set, follow tab order.
210 * Otherwise go to the previous widget.
213 static void
214 do_select_widget (Dlg_head * h, GList * w, select_dir_t dir)
216 Widget *w0 = (Widget *) h->current->data;
218 if (!dlg_unfocus (h))
219 return;
221 h->current = w;
225 if (dlg_focus (h))
226 break;
228 switch (dir)
230 case SELECT_EXACT:
231 h->current = g_list_find (h->widgets, w0);
232 if (dlg_focus (h))
233 return;
234 /* try find another widget that can take focus */
235 dir = SELECT_NEXT;
236 /* fallthrough */
237 case SELECT_NEXT:
238 h->current = dlg_widget_next (h, h->current);
239 break;
240 case SELECT_PREV:
241 h->current = dlg_widget_prev (h, h->current);
242 break;
245 while (h->current != w /* && (((Widget *) h->current->data)->options & W_DISABLED) == 0 */ );
247 if (dlg_overlap (w0, (Widget *) h->current->data))
249 send_message ((Widget *) h->current->data, WIDGET_DRAW, 0);
250 send_message ((Widget *) h->current->data, WIDGET_FOCUS, 0);
254 /* --------------------------------------------------------------------------------------------- */
256 static void
257 refresh_cmd (void)
259 #ifdef HAVE_SLANG
260 tty_touch_screen ();
261 mc_refresh ();
262 #else
263 /* Use this if the refreshes fail */
264 clr_scr ();
265 repaint_screen ();
266 #endif /* HAVE_SLANG */
269 /* --------------------------------------------------------------------------------------------- */
271 static cb_ret_t
272 dlg_execute_cmd (Dlg_head * h, unsigned long command)
274 cb_ret_t ret = MSG_HANDLED;
275 switch (command)
277 case CK_Ok:
278 h->ret_value = B_ENTER;
279 dlg_stop (h);
280 break;
281 case CK_Cancel:
282 h->ret_value = B_CANCEL;
283 dlg_stop (h);
284 break;
286 case CK_Up:
287 case CK_Left:
288 dlg_one_up (h);
289 break;
290 case CK_Down:
291 case CK_Right:
292 dlg_one_down (h);
293 break;
295 case CK_Help:
297 ev_help_t event_data = { NULL, h->help_ctx };
298 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
299 do_refresh ();
301 break;
303 case CK_Suspend:
304 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
305 refresh_cmd ();
306 break;
307 case CK_Refresh:
308 refresh_cmd ();
309 break;
311 case CK_ScreenList:
312 if (!h->modal)
313 dialog_switch_list ();
314 else
315 ret = MSG_NOT_HANDLED;
316 break;
317 case CK_ScreenNext:
318 if (!h->modal)
319 dialog_switch_next ();
320 else
321 ret = MSG_NOT_HANDLED;
322 break;
323 case CK_ScreenPrev:
324 if (!h->modal)
325 dialog_switch_prev ();
326 else
327 ret = MSG_NOT_HANDLED;
328 break;
330 default:
331 ret = MSG_NOT_HANDLED;
334 return ret;
337 /* --------------------------------------------------------------------------------------------- */
339 static cb_ret_t
340 dlg_handle_key (Dlg_head * h, int d_key)
342 unsigned long command;
343 command = keybind_lookup_keymap_command (dialog_map, d_key);
344 if ((command == CK_IgnoreKey) || (dlg_execute_cmd (h, command) == MSG_NOT_HANDLED))
345 return MSG_NOT_HANDLED;
346 else
347 return MSG_HANDLED;
350 /* --------------------------------------------------------------------------------------------- */
352 static int
353 dlg_mouse_event (Dlg_head * h, Gpm_Event * event)
355 GList *item;
356 GList *starting_widget = h->current;
357 Gpm_Event new_event;
358 int x = event->x;
359 int y = event->y;
361 /* close the dialog by mouse click out of dialog area */
362 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0) && ((event->type & GPM_DOWN) != 0) /* left click */
363 && !((x > h->x) && (x <= h->x + h->cols) && (y > h->y) && (y <= h->y + h->lines)))
365 h->ret_value = B_CANCEL;
366 dlg_stop (h);
367 return MOU_NORMAL;
370 item = starting_widget;
373 Widget *widget;
375 widget = (Widget *) item->data;
376 item = dlg_widget_next (h, item);
378 if (((widget->options & W_DISABLED) == 0)
379 && (x > widget->x) && (x <= widget->x + widget->cols)
380 && (y > widget->y) && (y <= widget->y + widget->lines))
382 new_event = *event;
383 new_event.x -= widget->x;
384 new_event.y -= widget->y;
386 if (widget->mouse != NULL)
387 return widget->mouse (&new_event, widget);
390 while (item != starting_widget);
392 return MOU_NORMAL;
395 /* --------------------------------------------------------------------------------------------- */
397 static cb_ret_t
398 dlg_try_hotkey (Dlg_head * h, int d_key)
400 GList *hot_cur;
401 Widget *current;
402 cb_ret_t handled;
403 int c;
405 if (h->widgets == NULL)
406 return MSG_NOT_HANDLED;
408 if (h->current == NULL)
409 h->current = h->widgets;
412 * Explanation: we don't send letter hotkeys to other widgets if
413 * the currently selected widget is an input line
416 current = (Widget *) h->current->data;
418 if ((current->options & W_DISABLED) != 0)
419 return MSG_NOT_HANDLED;
421 if (current->options & W_IS_INPUT)
423 /* skip ascii control characters, anything else can valid character in
424 * some encoding */
425 if (d_key >= 32 && d_key < 256)
426 return MSG_NOT_HANDLED;
429 /* If it's an alt key, send the message */
430 c = d_key & ~ALT (0);
431 if (d_key & ALT (0) && g_ascii_isalpha (c))
432 d_key = g_ascii_tolower (c);
434 handled = MSG_NOT_HANDLED;
435 if ((current->options & W_WANT_HOTKEY) != 0)
436 handled = send_message (current, WIDGET_HOTKEY, d_key);
438 /* If not used, send hotkey to other widgets */
439 if (handled == MSG_HANDLED)
440 return MSG_HANDLED;
442 hot_cur = dlg_widget_next (h, h->current);
444 /* send it to all widgets */
445 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
447 current = (Widget *) hot_cur->data;
449 if ((current->options & W_WANT_HOTKEY) != 0)
450 handled = send_message (current, WIDGET_HOTKEY, d_key);
452 if (handled == MSG_NOT_HANDLED)
453 hot_cur = dlg_widget_next (h, hot_cur);
456 if (handled == MSG_HANDLED)
457 do_select_widget (h, hot_cur, SELECT_EXACT);
459 return handled;
462 /* --------------------------------------------------------------------------------------------- */
464 static void
465 dlg_key_event (Dlg_head * h, int d_key)
467 cb_ret_t handled;
469 if (h->widgets == NULL)
470 return;
472 if (h->current == NULL)
473 h->current = h->widgets;
475 /* TAB used to cycle */
476 if ((h->flags & DLG_WANT_TAB) == 0)
478 if (d_key == '\t')
480 dlg_one_down (h);
481 return;
483 else if (d_key == KEY_BTAB)
485 dlg_one_up (h);
486 return;
490 /* first can dlg_callback handle the key */
491 handled = h->callback (h, NULL, DLG_KEY, d_key, NULL);
493 /* next try the hotkey */
494 if (handled == MSG_NOT_HANDLED)
495 handled = dlg_try_hotkey (h, d_key);
497 if (handled == MSG_HANDLED)
498 h->callback (h, NULL, DLG_HOTKEY_HANDLED, 0, NULL);
499 else
500 /* not used - then try widget_callback */
501 handled = send_message ((Widget *) h->current->data, WIDGET_KEY, d_key);
503 /* not used- try to use the unhandled case */
504 if (handled == MSG_NOT_HANDLED)
505 handled = h->callback (h, NULL, DLG_UNHANDLED_KEY, d_key, NULL);
507 if (handled == MSG_NOT_HANDLED)
508 handled = dlg_handle_key (h, d_key);
510 h->callback (h, NULL, DLG_POST_KEY, d_key, NULL);
513 /* --------------------------------------------------------------------------------------------- */
515 static void
516 frontend_run_dlg (Dlg_head * h)
518 int d_key;
519 Gpm_Event event;
521 event.x = -1;
523 /* close opened editors, viewers, etc */
524 if (!h->modal && mc_global.widget.midnight_shutdown)
526 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
527 return;
530 while (h->state == DLG_ACTIVE)
532 if (mc_global.tty.winch_flag)
533 dialog_change_screen_size ();
535 if (is_idle ())
537 if (idle_hook)
538 execute_hooks (idle_hook);
540 while ((h->flags & DLG_WANT_IDLE) && is_idle ())
541 h->callback (h, NULL, DLG_IDLE, 0, NULL);
543 /* Allow terminating the dialog from the idle handler */
544 if (h->state != DLG_ACTIVE)
545 break;
548 update_cursor (h);
550 /* Clear interrupt flag */
551 tty_got_interrupt ();
552 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
554 dlg_process_event (h, d_key, &event);
556 if (h->state == DLG_CLOSED)
557 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
561 /* --------------------------------------------------------------------------------------------- */
563 static int
564 dlg_find_widget_by_id (gconstpointer a, gconstpointer b)
566 Widget *w = (Widget *) a;
567 unsigned long id = GPOINTER_TO_UINT (b);
569 return w->id == id ? 0 : 1;
572 /* --------------------------------------------------------------------------------------------- */
573 /*** public functions ****************************************************************************/
574 /* --------------------------------------------------------------------------------------------- */
576 /** draw box in window */
577 void
578 draw_box (Dlg_head * h, int y, int x, int ys, int xs, gboolean single)
580 tty_draw_box (h->y + y, h->x + x, ys, xs, single);
583 /* --------------------------------------------------------------------------------------------- */
585 /** Clean the dialog area, draw the frame and the title */
586 void
587 common_dialog_repaint (Dlg_head * h)
589 int space;
591 if (h->state != DLG_ACTIVE)
592 return;
594 space = (h->flags & DLG_COMPACT) ? 0 : 1;
596 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
597 dlg_erase (h);
598 draw_box (h, space, space, h->lines - 2 * space, h->cols - 2 * space, FALSE);
600 if (h->title != NULL)
602 tty_setcolor (h->color[DLG_COLOR_TITLE]);
603 dlg_move (h, space, (h->cols - str_term_width1 (h->title)) / 2);
604 tty_print_string (h->title);
608 /* --------------------------------------------------------------------------------------------- */
609 /** this function allows to set dialog position */
611 void
612 dlg_set_position (Dlg_head * h, int y1, int x1, int y2, int x2)
614 /* save old positions, will be used to reposition childs */
615 int ox, oy, oc, ol;
616 int shift_x, shift_y, scale_x, scale_y;
618 /* save old positions, will be used to reposition childs */
619 ox = h->x;
620 oy = h->y;
621 oc = h->cols;
622 ol = h->lines;
624 h->x = x1;
625 h->y = y1;
626 h->lines = y2 - y1;
627 h->cols = x2 - x1;
629 /* dialog is empty */
630 if (h->widgets == NULL)
631 return;
633 if (h->current == NULL)
634 h->current = h->widgets;
636 /* values by which controls should be moved */
637 shift_x = h->x - ox;
638 shift_y = h->y - oy;
639 scale_x = h->cols - oc;
640 scale_y = h->lines - ol;
642 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
644 GList *w;
646 for (w = h->widgets; w != NULL; w = g_list_next (w))
648 /* there are, mainly, 2 generally possible
649 situations:
651 1. control sticks to one side - it
652 should be moved
654 2. control sticks to two sides of
655 one direction - it should be sized */
657 Widget *c = (Widget *) w->data;
658 int x = c->x;
659 int y = c->y;
660 int cols = c->cols;
661 int lines = c->lines;
663 if ((c->pos_flags & WPOS_KEEP_LEFT) && (c->pos_flags & WPOS_KEEP_RIGHT))
665 x += shift_x;
666 cols += scale_x;
668 else if (c->pos_flags & WPOS_KEEP_LEFT)
669 x += shift_x;
670 else if (c->pos_flags & WPOS_KEEP_RIGHT)
671 x += shift_x + scale_x;
673 if ((c->pos_flags & WPOS_KEEP_TOP) && (c->pos_flags & WPOS_KEEP_BOTTOM))
675 y += shift_y;
676 lines += scale_y;
678 else if (c->pos_flags & WPOS_KEEP_TOP)
679 y += shift_y;
680 else if (c->pos_flags & WPOS_KEEP_BOTTOM)
681 y += shift_y + scale_y;
683 widget_set_size (c, y, x, lines, cols);
688 /* --------------------------------------------------------------------------------------------- */
689 /** this function sets only size, leaving positioning to automatic methods */
691 void
692 dlg_set_size (Dlg_head * h, int lines, int cols)
694 int x = h->x;
695 int y = h->y;
697 if (h->flags & DLG_CENTER)
699 y = (LINES - lines) / 2;
700 x = (COLS - cols) / 2;
703 if ((h->flags & DLG_TRYUP) && (y > 3))
704 y -= 2;
706 dlg_set_position (h, y, x, y + lines, x + cols);
709 /* --------------------------------------------------------------------------------------------- */
710 /** Default dialog callback */
712 cb_ret_t
713 default_dlg_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
715 (void) sender;
716 (void) parm;
717 (void) data;
719 switch (msg)
721 case DLG_DRAW:
722 if (h->color != NULL)
724 common_dialog_repaint (h);
725 return MSG_HANDLED;
727 return MSG_NOT_HANDLED;
729 case DLG_IDLE:
730 dlg_broadcast_msg_to (h, WIDGET_IDLE, FALSE, W_WANT_IDLE);
731 return MSG_HANDLED;
733 case DLG_RESIZE:
734 /* this is default resizing mechanism */
735 /* the main idea of this code is to resize dialog
736 according to flags (if any of flags require automatic
737 resizing, like DLG_CENTER, end after that reposition
738 controls in dialog according to flags of widget) */
739 dlg_set_size (h, h->lines, h->cols);
740 return MSG_HANDLED;
742 default:
743 break;
746 return MSG_NOT_HANDLED;
749 /* --------------------------------------------------------------------------------------------- */
751 Dlg_head *
752 create_dlg (gboolean modal, int y1, int x1, int lines, int cols,
753 const int *colors, dlg_cb_fn callback, const char *help_ctx,
754 const char *title, dlg_flags_t flags)
756 Dlg_head *new_d;
758 new_d = g_new0 (Dlg_head, 1);
759 new_d->modal = modal;
760 if (colors != NULL)
761 memmove (new_d->color, colors, sizeof (dlg_colors_t));
762 new_d->help_ctx = help_ctx;
763 new_d->callback = (callback != NULL) ? callback : default_dlg_callback;
764 new_d->x = x1;
765 new_d->y = y1;
766 new_d->flags = flags;
767 new_d->data = NULL;
769 dlg_set_size (new_d, lines, cols);
770 new_d->fullscreen = (new_d->x == 0 && new_d->y == 0
771 && new_d->cols == COLS && new_d->lines == LINES);
773 new_d->mouse_status = MOU_NORMAL;
775 /* Strip existing spaces, add one space before and after the title */
776 if (title != NULL)
778 char *t;
780 t = g_strstrip (g_strdup (title));
781 if (*t != '\0')
782 new_d->title = g_strdup_printf (" %s ", t);
783 g_free (t);
786 /* unique name got event group for this dialog */
787 new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
789 return new_d;
792 /* --------------------------------------------------------------------------------------------- */
794 void
795 dlg_set_default_colors (void)
797 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
798 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
799 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
800 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
801 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
803 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
804 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
805 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
806 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
807 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
810 /* --------------------------------------------------------------------------------------------- */
812 void
813 dlg_erase (Dlg_head * h)
815 if ((h != NULL) && (h->state == DLG_ACTIVE))
816 tty_fill_region (h->y, h->x, h->lines, h->cols, ' ');
819 /* --------------------------------------------------------------------------------------------- */
821 void
822 set_idle_proc (Dlg_head * d, int enable)
824 if (enable)
825 d->flags |= DLG_WANT_IDLE;
826 else
827 d->flags &= ~DLG_WANT_IDLE;
830 /* --------------------------------------------------------------------------------------------- */
832 * Insert widget to dialog before current widget. For dialogs populated
833 * from the bottom, make the widget current. Return widget number.
837 add_widget_autopos (Dlg_head * h, void *w, widget_pos_flags_t pos_flags)
839 Widget *widget = (Widget *) w;
841 /* Don't accept 0 widgets */
842 if (w == NULL)
843 abort ();
845 widget->x += h->x;
846 widget->y += h->y;
847 widget->owner = h;
848 widget->pos_flags = pos_flags;
849 widget->id = g_list_length (h->widgets);
851 if ((h->flags & DLG_REVERSE) != 0)
852 h->widgets = g_list_prepend (h->widgets, widget);
853 else
854 h->widgets = g_list_append (h->widgets, widget);
856 h->current = h->widgets;
858 return widget->id;
861 /* --------------------------------------------------------------------------------------------- */
862 /** wrapper to simply add lefttop positioned controls */
865 add_widget (Dlg_head * h, void *w)
867 return add_widget_autopos (h, w, WPOS_KEEP_LEFT | WPOS_KEEP_TOP);
870 /* --------------------------------------------------------------------------------------------- */
872 void
873 do_refresh (void)
875 GList *d = top_dlg;
877 if (fast_refresh)
879 if ((d != NULL) && (d->data != NULL))
880 dlg_redraw ((Dlg_head *) d->data);
882 else
884 /* Search first fullscreen dialog */
885 for (; d != NULL; d = g_list_next (d))
886 if ((d->data != NULL) && ((Dlg_head *) d->data)->fullscreen)
887 break;
888 /* back to top dialog */
889 for (; d != NULL; d = g_list_previous (d))
890 if (d->data != NULL)
891 dlg_redraw ((Dlg_head *) d->data);
895 /* --------------------------------------------------------------------------------------------- */
896 /** broadcast a message to all the widgets in a dialog */
898 void
899 dlg_broadcast_msg (Dlg_head * h, widget_msg_t msg, gboolean reverse)
901 dlg_broadcast_msg_to (h, msg, reverse, 0);
904 /* --------------------------------------------------------------------------------------------- */
907 dlg_focus (Dlg_head * h)
909 /* cannot focus disabled widget ... */
911 if ((h->current != NULL) && (h->state == DLG_ACTIVE))
913 Widget *current = (Widget *) h->current->data;
915 if (((current->options & W_DISABLED) == 0)
916 && (send_message (current, WIDGET_FOCUS, 0) == MSG_HANDLED))
918 h->callback (h, current, DLG_FOCUS, 0, NULL);
919 return 1;
923 return 0;
926 /* --------------------------------------------------------------------------------------------- */
927 /** Return true if the windows overlap */
930 dlg_overlap (Widget * a, Widget * b)
932 return !((b->x >= a->x + a->cols)
933 || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
937 /* --------------------------------------------------------------------------------------------- */
938 /** Find the widget with the given callback in the dialog h */
940 Widget *
941 find_widget_type (const Dlg_head * h, callback_fn callback)
943 GList *w;
945 w = g_list_find_custom (h->widgets, callback, dlg_find_widget_callback);
947 return (w == NULL) ? NULL : (Widget *) w->data;
950 /* --------------------------------------------------------------------------------------------- */
951 /** Find the widget with the given id */
953 Widget *
954 dlg_find_by_id (const Dlg_head * h, unsigned int id)
956 GList *w;
958 w = g_list_find_custom (h->widgets, GUINT_TO_POINTER (id), dlg_find_widget_by_id);
959 return w != NULL ? (Widget *) w->data : NULL;
962 /* --------------------------------------------------------------------------------------------- */
963 /** Find the widget with the given id in the dialog h and select it */
965 void
966 dlg_select_by_id (const Dlg_head * h, unsigned int id)
968 Widget *w;
970 w = dlg_find_by_id (h, id);
971 if (w != NULL)
972 dlg_select_widget (w);
975 /* --------------------------------------------------------------------------------------------- */
977 * Try to select widget in the dialog.
980 void
981 dlg_select_widget (void *w)
983 const Widget *widget = (Widget *) w;
984 Dlg_head *h = widget->owner;
986 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
989 /* --------------------------------------------------------------------------------------------- */
990 /** Try to select previous widget in the tab order */
992 void
993 dlg_one_up (Dlg_head * h)
995 if (h->widgets != NULL)
996 do_select_widget (h, dlg_widget_prev (h, h->current), SELECT_PREV);
999 /* --------------------------------------------------------------------------------------------- */
1000 /** Try to select next widget in the tab order */
1002 void
1003 dlg_one_down (Dlg_head * h)
1005 if (h->widgets != NULL)
1006 do_select_widget (h, dlg_widget_next (h, h->current), SELECT_NEXT);
1009 /* --------------------------------------------------------------------------------------------- */
1011 void
1012 update_cursor (Dlg_head * h)
1014 GList *p = h->current;
1016 if ((p != NULL) && (h->state == DLG_ACTIVE))
1018 Widget *w;
1020 w = (Widget *) p->data;
1022 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1023 send_message (w, WIDGET_CURSOR, 0);
1024 else
1027 p = dlg_widget_next (h, p);
1028 if (p == h->current)
1029 break;
1031 w = (Widget *) p->data;
1033 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1034 if (send_message (w, WIDGET_CURSOR, 0) == MSG_HANDLED)
1035 break;
1037 while (TRUE);
1041 /* --------------------------------------------------------------------------------------------- */
1043 * Redraw the widgets in reverse order, leaving the current widget
1044 * as the last one
1047 void
1048 dlg_redraw (Dlg_head * h)
1050 if (h->state != DLG_ACTIVE)
1051 return;
1053 if (h->winch_pending)
1055 h->winch_pending = FALSE;
1056 h->callback (h, NULL, DLG_RESIZE, 0, NULL);
1059 h->callback (h, NULL, DLG_DRAW, 0, NULL);
1060 dlg_broadcast_msg (h, WIDGET_DRAW, TRUE);
1061 update_cursor (h);
1064 /* --------------------------------------------------------------------------------------------- */
1066 void
1067 dlg_stop (Dlg_head * h)
1069 h->state = DLG_CLOSED;
1072 /* --------------------------------------------------------------------------------------------- */
1073 /** Init the process */
1075 void
1076 init_dlg (Dlg_head * h)
1078 if ((top_dlg != NULL) && ((Dlg_head *) top_dlg->data)->modal)
1079 h->modal = TRUE;
1081 /* add dialog to the stack */
1082 top_dlg = g_list_prepend (top_dlg, h);
1084 /* Initialize dialog manager and widgets */
1085 if (h->state == DLG_ACTIVE)
1087 if (!h->modal)
1088 dialog_switch_add (h);
1090 h->callback (h, NULL, DLG_INIT, 0, NULL);
1091 dlg_broadcast_msg (h, WIDGET_INIT, FALSE);
1092 dlg_read_history (h);
1095 h->state = DLG_ACTIVE;
1097 dlg_redraw (h);
1099 /* Select the first widget that takes focus */
1100 while (h->current != NULL && !dlg_focus (h))
1101 h->current = dlg_widget_next (h, h->current);
1103 h->ret_value = 0;
1106 /* --------------------------------------------------------------------------------------------- */
1108 void
1109 dlg_process_event (Dlg_head * h, int key, Gpm_Event * event)
1111 if (key == EV_NONE)
1113 if (tty_got_interrupt ())
1114 if (h->callback (h, NULL, DLG_ACTION, CK_Cancel, NULL) != MSG_HANDLED)
1115 dlg_execute_cmd (h, CK_Cancel);
1117 return;
1120 if (key == EV_MOUSE)
1121 h->mouse_status = dlg_mouse_event (h, event);
1122 else
1123 dlg_key_event (h, key);
1126 /* --------------------------------------------------------------------------------------------- */
1127 /** Shutdown the run_dlg */
1129 void
1130 dlg_run_done (Dlg_head * h)
1132 top_dlg = g_list_remove (top_dlg, h);
1134 if (h->state == DLG_CLOSED)
1136 h->callback (h, (Widget *) h->current->data, DLG_END, 0, NULL);
1137 if (!h->modal)
1138 dialog_switch_remove (h);
1143 /* --------------------------------------------------------------------------------------------- */
1145 * Standard run dialog routine
1146 * We have to keep this routine small so that we can duplicate it's
1147 * behavior on complex routines like the file routines, this way,
1148 * they can call the dlg_process_event without rewriting all the code
1152 run_dlg (Dlg_head * h)
1154 init_dlg (h);
1155 frontend_run_dlg (h);
1156 dlg_run_done (h);
1157 return h->ret_value;
1160 /* --------------------------------------------------------------------------------------------- */
1162 void
1163 destroy_dlg (Dlg_head * h)
1165 /* if some widgets have history, save all history at one moment here */
1166 dlg_save_history (h);
1167 dlg_broadcast_msg (h, WIDGET_DESTROY, FALSE);
1168 g_list_foreach (h->widgets, (GFunc) g_free, NULL);
1169 g_list_free (h->widgets);
1170 mc_event_group_del (h->event_group);
1171 g_free (h->event_group);
1172 g_free (h->title);
1173 g_free (h);
1175 do_refresh ();
1178 /* --------------------------------------------------------------------------------------------- */
1181 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1183 void
1184 dlg_save_history (Dlg_head * h)
1186 char *profile;
1187 int i;
1189 if (num_history_items_recorded == 0) /* this is how to disable */
1190 return;
1192 profile = g_build_filename (mc_config_get_cache_path (), MC_HISTORY_FILE, (char *) NULL);
1193 i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
1194 if (i != -1)
1195 close (i);
1197 /* Make sure the history is only readable by the user */
1198 if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
1200 ev_history_load_save_t event_data;
1202 event_data.cfg = mc_config_init (profile);
1203 event_data.receiver = NULL;
1205 /* get all histories in dialog */
1206 mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
1208 mc_config_save_file (event_data.cfg, NULL);
1209 mc_config_deinit (event_data.cfg);
1212 g_free (profile);
1215 /* --------------------------------------------------------------------------------------------- */
1217 char *
1218 dlg_get_title (const Dlg_head * h, size_t len)
1220 char *t;
1222 if (h == NULL)
1223 abort ();
1225 if (h->get_title != NULL)
1226 t = h->get_title (h, len);
1227 else
1228 t = g_strdup ("");
1230 return t;
1233 /* --------------------------------------------------------------------------------------------- */
1234 /** Replace widget old_w for widget new_w in the dialog */
1236 void
1237 dlg_replace_widget (Widget * old_w, Widget * new_w)
1239 Dlg_head *h = old_w->owner;
1240 gboolean should_focus = FALSE;
1242 if (h->widgets == NULL)
1243 return;
1245 if (h->current == NULL)
1246 h->current = h->widgets;
1248 if (old_w == h->current->data)
1249 should_focus = TRUE;
1251 new_w->owner = h;
1252 new_w->id = old_w->id;
1254 if (should_focus)
1255 h->current->data = new_w;
1256 else
1257 g_list_find (h->widgets, old_w)->data = new_w;
1259 send_message (old_w, WIDGET_DESTROY, 0);
1260 send_message (new_w, WIDGET_INIT, 0);
1262 if (should_focus)
1263 dlg_select_widget (new_w);
1265 send_message (new_w, WIDGET_DRAW, 0);
1268 /* --------------------------------------------------------------------------------------------- */