6d51fef95cbe82de54cbe59595983839a869e4f6
[midnight-commander.git] / lib / widget / dialog.c
blob6d51fef95cbe82de54cbe59595983839a869e4f6
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/key.h"
44 #include "lib/strutil.h"
45 #include "lib/widget.h"
46 #include "lib/fileloc.h" /* MC_HISTORY_FILE */
47 #include "lib/event.h" /* mc_event_raise() */
49 /*** global variables ****************************************************************************/
51 /* Color styles for normal and error dialogs */
52 dlg_colors_t dialog_colors;
53 dlg_colors_t alarm_colors;
55 /* Primitive way to check if the the current dialog is our dialog */
56 /* This is needed by async routines like load_prompt */
57 GList *top_dlg = NULL;
59 /* A hook list for idle events */
60 hook_t *idle_hook = NULL;
62 /* If set then dialogs just clean the screen when refreshing, else */
63 /* they do a complete refresh, refreshing all the parts of the program */
64 int fast_refresh = 0;
66 /* left click outside of dialog closes it */
67 int mouse_close_dialog = 0;
69 const global_keymap_t *dialog_map = NULL;
71 /*** file scope macro definitions ****************************************************************/
73 /*** file scope type declarations ****************************************************************/
75 /** What to do if the requested widget doesn't take focus */
76 typedef enum
78 SELECT_NEXT, /* go the the next widget */
79 SELECT_PREV, /* go the the previous widget */
80 SELECT_EXACT /* use current widget */
81 } select_dir_t;
83 /*** file scope variables ************************************************************************/
85 /*** file scope functions ************************************************************************/
87 static GList *
88 dlg_widget_next (Dlg_head * h, GList * l)
90 GList *next;
92 next = g_list_next (l);
93 if (next == NULL)
94 next = h->widgets;
96 return next;
99 /* --------------------------------------------------------------------------------------------- */
101 static GList *
102 dlg_widget_prev (Dlg_head * h, GList * l)
104 GList *prev;
106 prev = g_list_previous (l);
107 if (prev == NULL)
108 prev = g_list_last (h->widgets);
110 return prev;
113 /* --------------------------------------------------------------------------------------------- */
115 * broadcast a message to all the widgets in a dialog that have
116 * the options set to flags. If flags is zero, the message is sent
117 * to all widgets.
120 static void
121 dlg_broadcast_msg_to (Dlg_head * h, widget_msg_t msg, gboolean reverse, int flags)
123 GList *p, *first;
125 if (h->widgets == NULL)
126 return;
128 if (h->current == NULL)
129 h->current = h->widgets;
131 if (reverse)
132 p = dlg_widget_prev (h, h->current);
133 else
134 p = dlg_widget_next (h, h->current);
136 first = p;
140 Widget *w = (Widget *) p->data;
142 if (reverse)
143 p = dlg_widget_prev (h, p);
144 else
145 p = dlg_widget_next (h, p);
147 if ((flags == 0) || ((flags & w->options) != 0))
148 send_message (w, msg, 0);
150 while (first != p);
153 /* --------------------------------------------------------------------------------------------- */
156 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
158 static void
159 dlg_read_history (Dlg_head * h)
161 char *profile;
162 ev_history_load_save_t event_data;
164 if (num_history_items_recorded == 0) /* this is how to disable */
165 return;
167 profile = mc_config_get_full_path (MC_HISTORY_FILE);
168 event_data.cfg = mc_config_init (profile, TRUE);
169 event_data.receiver = NULL;
171 /* create all histories in dialog */
172 mc_event_raise (h->event_group, MCEVENT_HISTORY_LOAD, &event_data);
174 mc_config_deinit (event_data.cfg);
175 g_free (profile);
178 /* --------------------------------------------------------------------------------------------- */
180 static gboolean
181 dlg_unfocus (Dlg_head * h)
183 /* we can unfocus disabled widget */
184 if ((h->current != NULL) && (h->state == DLG_CONSTRUCT || h->state == DLG_ACTIVE))
186 Widget *current = (Widget *) h->current->data;
188 if (send_message (current, WIDGET_UNFOCUS, 0) == MSG_HANDLED)
190 h->callback (h, current, DLG_UNFOCUS, 0, NULL);
191 return TRUE;
195 return FALSE;
198 /* --------------------------------------------------------------------------------------------- */
200 static int
201 dlg_find_widget_callback (const void *a, const void *b)
203 const Widget *w = (const Widget *) a;
204 callback_fn f = (callback_fn) b;
206 return (w->callback == f) ? 0 : 1;
209 /* --------------------------------------------------------------------------------------------- */
211 * Try to select another widget. If forward is set, follow tab order.
212 * Otherwise go to the previous widget.
215 static void
216 do_select_widget (Dlg_head * h, GList * w, select_dir_t dir)
218 Widget *w0 = (Widget *) h->current->data;
220 if (!dlg_unfocus (h))
221 return;
223 h->current = w;
227 if (dlg_focus (h))
228 break;
230 switch (dir)
232 case SELECT_EXACT:
233 h->current = g_list_find (h->widgets, w0);
234 if (dlg_focus (h))
235 return;
236 /* try find another widget that can take focus */
237 dir = SELECT_NEXT;
238 /* fallthrough */
239 case SELECT_NEXT:
240 h->current = dlg_widget_next (h, h->current);
241 break;
242 case SELECT_PREV:
243 h->current = dlg_widget_prev (h, h->current);
244 break;
247 while (h->current != w /* && (((Widget *) h->current->data)->options & W_DISABLED) == 0 */ );
249 if (dlg_overlap (w0, (Widget *) h->current->data))
251 send_message ((Widget *) h->current->data, WIDGET_DRAW, 0);
252 send_message ((Widget *) h->current->data, WIDGET_FOCUS, 0);
256 /* --------------------------------------------------------------------------------------------- */
258 static void
259 refresh_cmd (void)
261 #ifdef HAVE_SLANG
262 tty_touch_screen ();
263 mc_refresh ();
264 #else
265 /* Use this if the refreshes fail */
266 clr_scr ();
267 repaint_screen ();
268 #endif /* HAVE_SLANG */
271 /* --------------------------------------------------------------------------------------------- */
273 static cb_ret_t
274 dlg_execute_cmd (Dlg_head * h, unsigned long command)
276 cb_ret_t ret = MSG_HANDLED;
277 switch (command)
279 case CK_Ok:
280 h->ret_value = B_ENTER;
281 dlg_stop (h);
282 break;
283 case CK_Cancel:
284 h->ret_value = B_CANCEL;
285 dlg_stop (h);
286 break;
288 case CK_Up:
289 case CK_Left:
290 dlg_one_up (h);
291 break;
292 case CK_Down:
293 case CK_Right:
294 dlg_one_down (h);
295 break;
297 case CK_Help:
299 ev_help_t event_data = { NULL, h->help_ctx };
300 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
302 break;
304 case CK_Suspend:
305 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
306 refresh_cmd ();
307 break;
308 case CK_Refresh:
309 refresh_cmd ();
310 break;
312 case CK_ScreenList:
313 if (!h->modal)
314 dialog_switch_list ();
315 else
316 ret = MSG_NOT_HANDLED;
317 break;
318 case CK_ScreenNext:
319 if (!h->modal)
320 dialog_switch_next ();
321 else
322 ret = MSG_NOT_HANDLED;
323 break;
324 case CK_ScreenPrev:
325 if (!h->modal)
326 dialog_switch_prev ();
327 else
328 ret = MSG_NOT_HANDLED;
329 break;
331 default:
332 ret = MSG_NOT_HANDLED;
335 return ret;
338 /* --------------------------------------------------------------------------------------------- */
340 static cb_ret_t
341 dlg_handle_key (Dlg_head * h, int d_key)
343 unsigned long command;
345 command = keybind_lookup_keymap_command (dialog_map, d_key);
347 if (command == CK_IgnoreKey)
348 return MSG_NOT_HANDLED;
350 if (h->callback (h, NULL, DLG_ACTION, command, NULL) == MSG_HANDLED
351 || dlg_execute_cmd (h, command) == MSG_HANDLED)
352 return MSG_HANDLED;
354 return MSG_NOT_HANDLED;
357 /* --------------------------------------------------------------------------------------------- */
359 static int
360 dlg_mouse_event (Dlg_head * h, Gpm_Event * event)
362 GList *item;
363 GList *starting_widget = h->current;
364 int x = event->x;
365 int y = event->y;
367 /* close the dialog by mouse click out of dialog area */
368 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0) && ((event->type & GPM_DOWN) != 0) /* left click */
369 && !((x > h->x) && (x <= h->x + h->cols) && (y > h->y) && (y <= h->y + h->lines)))
371 h->ret_value = B_CANCEL;
372 dlg_stop (h);
373 return MOU_NORMAL;
376 if (h->mouse != NULL)
378 int mou;
380 mou = h->mouse (event, h);
381 if (mou != MOU_UNHANDLED)
382 return mou;
385 item = starting_widget;
388 Widget *widget = (Widget *) item->data;
390 if ((h->flags & DLG_REVERSE) == 0)
391 item = dlg_widget_prev (h, item);
392 else
393 item = dlg_widget_next (h, item);
395 if ((widget->options & W_DISABLED) == 0 && widget->mouse != NULL)
397 /* put global cursor position to the widget */
398 int ret;
400 ret = widget->mouse (event, widget);
401 if (ret != MOU_UNHANDLED)
402 return ret;
405 while (item != starting_widget);
407 return MOU_UNHANDLED;
410 /* --------------------------------------------------------------------------------------------- */
412 static cb_ret_t
413 dlg_try_hotkey (Dlg_head * h, int d_key)
415 GList *hot_cur;
416 Widget *current;
417 cb_ret_t handled;
418 int c;
420 if (h->widgets == NULL)
421 return MSG_NOT_HANDLED;
423 if (h->current == NULL)
424 h->current = h->widgets;
427 * Explanation: we don't send letter hotkeys to other widgets if
428 * the currently selected widget is an input line
431 current = (Widget *) h->current->data;
433 if ((current->options & W_DISABLED) != 0)
434 return MSG_NOT_HANDLED;
436 if (current->options & W_IS_INPUT)
438 /* skip ascii control characters, anything else can valid character in
439 * some encoding */
440 if (d_key >= 32 && d_key < 256)
441 return MSG_NOT_HANDLED;
444 /* If it's an alt key, send the message */
445 c = d_key & ~ALT (0);
446 if (d_key & ALT (0) && g_ascii_isalpha (c))
447 d_key = g_ascii_tolower (c);
449 handled = MSG_NOT_HANDLED;
450 if ((current->options & W_WANT_HOTKEY) != 0)
451 handled = send_message (current, WIDGET_HOTKEY, d_key);
453 /* If not used, send hotkey to other widgets */
454 if (handled == MSG_HANDLED)
455 return MSG_HANDLED;
457 hot_cur = dlg_widget_next (h, h->current);
459 /* send it to all widgets */
460 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
462 current = (Widget *) hot_cur->data;
464 if ((current->options & W_WANT_HOTKEY) != 0 && (current->options & W_DISABLED) == 0)
465 handled = send_message (current, WIDGET_HOTKEY, d_key);
467 if (handled == MSG_NOT_HANDLED)
468 hot_cur = dlg_widget_next (h, hot_cur);
471 if (handled == MSG_HANDLED)
472 do_select_widget (h, hot_cur, SELECT_EXACT);
474 return handled;
477 /* --------------------------------------------------------------------------------------------- */
479 static void
480 dlg_key_event (Dlg_head * h, int d_key)
482 cb_ret_t handled;
484 if (h->widgets == NULL)
485 return;
487 if (h->current == NULL)
488 h->current = h->widgets;
490 /* TAB used to cycle */
491 if ((h->flags & DLG_WANT_TAB) == 0)
493 if (d_key == '\t')
495 dlg_one_down (h);
496 return;
498 else if (d_key == KEY_BTAB)
500 dlg_one_up (h);
501 return;
505 /* first can dlg_callback handle the key */
506 handled = h->callback (h, NULL, DLG_KEY, d_key, NULL);
508 /* next try the hotkey */
509 if (handled == MSG_NOT_HANDLED)
510 handled = dlg_try_hotkey (h, d_key);
512 if (handled == MSG_HANDLED)
513 h->callback (h, NULL, DLG_HOTKEY_HANDLED, 0, NULL);
514 else
515 /* not used - then try widget_callback */
516 handled = send_message ((Widget *) h->current->data, WIDGET_KEY, d_key);
518 /* not used- try to use the unhandled case */
519 if (handled == MSG_NOT_HANDLED)
520 handled = h->callback (h, NULL, DLG_UNHANDLED_KEY, d_key, NULL);
522 if (handled == MSG_NOT_HANDLED)
523 handled = dlg_handle_key (h, d_key);
525 h->callback (h, NULL, DLG_POST_KEY, d_key, NULL);
528 /* --------------------------------------------------------------------------------------------- */
530 static void
531 frontend_run_dlg (Dlg_head * h)
533 int d_key;
534 Gpm_Event event;
536 event.x = -1;
538 /* close opened editors, viewers, etc */
539 if (!h->modal && mc_global.midnight_shutdown)
541 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
542 return;
545 while (h->state == DLG_ACTIVE)
547 if (mc_global.tty.winch_flag)
548 dialog_change_screen_size ();
550 if (is_idle ())
552 if (idle_hook)
553 execute_hooks (idle_hook);
555 while ((h->flags & DLG_WANT_IDLE) && is_idle ())
556 h->callback (h, NULL, DLG_IDLE, 0, NULL);
558 /* Allow terminating the dialog from the idle handler */
559 if (h->state != DLG_ACTIVE)
560 break;
563 update_cursor (h);
565 /* Clear interrupt flag */
566 tty_got_interrupt ();
567 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
569 dlg_process_event (h, d_key, &event);
571 if (h->state == DLG_CLOSED)
572 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
576 /* --------------------------------------------------------------------------------------------- */
578 static int
579 dlg_find_widget_by_id (gconstpointer a, gconstpointer b)
581 Widget *w = (Widget *) a;
582 unsigned long id = GPOINTER_TO_UINT (b);
584 return w->id == id ? 0 : 1;
587 /* --------------------------------------------------------------------------------------------- */
588 /*** public functions ****************************************************************************/
589 /* --------------------------------------------------------------------------------------------- */
591 /** draw box in window */
592 void
593 draw_box (Dlg_head * h, int y, int x, int ys, int xs, gboolean single)
595 tty_draw_box (h->y + y, h->x + x, ys, xs, single);
598 /* --------------------------------------------------------------------------------------------- */
600 /** Clean the dialog area, draw the frame and the title */
601 void
602 common_dialog_repaint (Dlg_head * h)
604 int space;
606 if (h->state != DLG_ACTIVE)
607 return;
609 space = (h->flags & DLG_COMPACT) ? 0 : 1;
611 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
612 dlg_erase (h);
613 draw_box (h, space, space, h->lines - 2 * space, h->cols - 2 * space, FALSE);
615 if (h->title != NULL)
617 tty_setcolor (h->color[DLG_COLOR_TITLE]);
618 dlg_move (h, space, (h->cols - str_term_width1 (h->title)) / 2);
619 tty_print_string (h->title);
623 /* --------------------------------------------------------------------------------------------- */
624 /** this function allows to set dialog position */
626 void
627 dlg_set_position (Dlg_head * h, int y1, int x1, int y2, int x2)
629 /* save old positions, will be used to reposition childs */
630 int ox, oy, oc, ol;
631 int shift_x, shift_y, scale_x, scale_y;
633 /* save old positions, will be used to reposition childs */
634 ox = h->x;
635 oy = h->y;
636 oc = h->cols;
637 ol = h->lines;
639 h->x = x1;
640 h->y = y1;
641 h->lines = y2 - y1;
642 h->cols = x2 - x1;
644 /* dialog is empty */
645 if (h->widgets == NULL)
646 return;
648 if (h->current == NULL)
649 h->current = h->widgets;
651 /* values by which controls should be moved */
652 shift_x = h->x - ox;
653 shift_y = h->y - oy;
654 scale_x = h->cols - oc;
655 scale_y = h->lines - ol;
657 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
659 GList *w;
661 for (w = h->widgets; w != NULL; w = g_list_next (w))
663 /* there are, mainly, 2 generally possible
664 situations:
666 1. control sticks to one side - it
667 should be moved
669 2. control sticks to two sides of
670 one direction - it should be sized */
672 Widget *c = (Widget *) w->data;
673 int x = c->x;
674 int y = c->y;
675 int cols = c->cols;
676 int lines = c->lines;
678 if ((c->pos_flags & WPOS_KEEP_LEFT) && (c->pos_flags & WPOS_KEEP_RIGHT))
680 x += shift_x;
681 cols += scale_x;
683 else if (c->pos_flags & WPOS_KEEP_LEFT)
684 x += shift_x;
685 else if (c->pos_flags & WPOS_KEEP_RIGHT)
686 x += shift_x + scale_x;
688 if ((c->pos_flags & WPOS_KEEP_TOP) && (c->pos_flags & WPOS_KEEP_BOTTOM))
690 y += shift_y;
691 lines += scale_y;
693 else if (c->pos_flags & WPOS_KEEP_TOP)
694 y += shift_y;
695 else if (c->pos_flags & WPOS_KEEP_BOTTOM)
696 y += shift_y + scale_y;
698 widget_set_size (c, y, x, lines, cols);
703 /* --------------------------------------------------------------------------------------------- */
704 /** this function sets only size, leaving positioning to automatic methods */
706 void
707 dlg_set_size (Dlg_head * h, int lines, int cols)
709 int x = h->x;
710 int y = h->y;
712 if (h->flags & DLG_CENTER)
714 y = (LINES - lines) / 2;
715 x = (COLS - cols) / 2;
718 if ((h->flags & DLG_TRYUP) && (y > 3))
719 y -= 2;
721 dlg_set_position (h, y, x, y + lines, x + cols);
724 /* --------------------------------------------------------------------------------------------- */
725 /** Default dialog callback */
727 cb_ret_t
728 default_dlg_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
730 (void) sender;
731 (void) parm;
732 (void) data;
734 switch (msg)
736 case DLG_DRAW:
737 if (h->color != NULL)
739 common_dialog_repaint (h);
740 return MSG_HANDLED;
742 return MSG_NOT_HANDLED;
744 case DLG_IDLE:
745 dlg_broadcast_msg_to (h, WIDGET_IDLE, FALSE, W_WANT_IDLE);
746 return MSG_HANDLED;
748 case DLG_RESIZE:
749 /* this is default resizing mechanism */
750 /* the main idea of this code is to resize dialog
751 according to flags (if any of flags require automatic
752 resizing, like DLG_CENTER, end after that reposition
753 controls in dialog according to flags of widget) */
754 dlg_set_size (h, h->lines, h->cols);
755 return MSG_HANDLED;
757 default:
758 break;
761 return MSG_NOT_HANDLED;
764 /* --------------------------------------------------------------------------------------------- */
766 Dlg_head *
767 create_dlg (gboolean modal, int y1, int x1, int lines, int cols,
768 const int *colors, dlg_cb_fn callback, mouse_h mouse_handler,
769 const char *help_ctx, const char *title, dlg_flags_t flags)
771 Dlg_head *new_d;
773 new_d = g_new0 (Dlg_head, 1);
774 new_d->state = DLG_CONSTRUCT;
775 new_d->modal = modal;
776 if (colors != NULL)
777 memmove (new_d->color, colors, sizeof (dlg_colors_t));
778 new_d->help_ctx = help_ctx;
779 new_d->callback = (callback != NULL) ? callback : default_dlg_callback;
780 new_d->mouse = mouse_handler;
781 new_d->x = x1;
782 new_d->y = y1;
783 new_d->flags = flags;
784 new_d->data = NULL;
786 dlg_set_size (new_d, lines, cols);
787 new_d->fullscreen = (new_d->x == 0 && new_d->y == 0
788 && new_d->cols == COLS && new_d->lines == LINES);
790 new_d->mouse_status = MOU_UNHANDLED;
792 /* Strip existing spaces, add one space before and after the title */
793 if (title != NULL)
795 char *t;
797 t = g_strstrip (g_strdup (title));
798 if (*t != '\0')
799 new_d->title = g_strdup_printf (" %s ", t);
800 g_free (t);
803 /* unique name got event group for this dialog */
804 new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
806 return new_d;
809 /* --------------------------------------------------------------------------------------------- */
811 void
812 dlg_set_default_colors (void)
814 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
815 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
816 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
817 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
818 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
820 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
821 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
822 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
823 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
824 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
827 /* --------------------------------------------------------------------------------------------- */
829 void
830 dlg_erase (Dlg_head * h)
832 if ((h != NULL) && (h->state == DLG_ACTIVE))
833 tty_fill_region (h->y, h->x, h->lines, h->cols, ' ');
836 /* --------------------------------------------------------------------------------------------- */
838 void
839 set_idle_proc (Dlg_head * d, int enable)
841 if (enable)
842 d->flags |= DLG_WANT_IDLE;
843 else
844 d->flags &= ~DLG_WANT_IDLE;
847 /* --------------------------------------------------------------------------------------------- */
849 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
852 unsigned long
853 add_widget_autopos (Dlg_head * h, void *w, widget_pos_flags_t pos_flags, const void *before)
855 Widget *widget = (Widget *) w;
857 /* Don't accept 0 widgets */
858 if (w == NULL)
859 abort ();
861 widget->x += h->x;
862 widget->y += h->y;
863 widget->owner = h;
864 widget->pos_flags = pos_flags;
865 widget->id = h->widget_id++;
867 if ((h->flags & DLG_REVERSE) != 0)
869 if (h->widgets == NULL || before == NULL)
871 h->widgets = g_list_prepend (h->widgets, widget);
872 h->current = h->widgets;
874 else
876 GList *b;
878 b = g_list_find (h->widgets, before);
880 /* don't accept widget not from dialog. This shouldn't happen */
881 if (b == NULL)
882 abort ();
884 h->widgets = g_list_insert_before (h->widgets, b, widget);
885 h->current = g_list_previous (b);
888 else
890 if (h->widgets == NULL || before == NULL)
892 h->widgets = g_list_append (h->widgets, widget);
893 h->current = g_list_last (h->widgets);
895 else
897 GList *b;
899 b = g_list_find (h->widgets, before);
901 /* don't accept widget not from dialog. This shouldn't happen */
902 if (b == NULL)
903 abort ();
905 b = g_list_next (b);
906 h->widgets = g_list_insert_before (h->widgets, b, widget);
907 if (b != NULL)
908 h->current = g_list_previous (b);
909 else
910 h->current = g_list_last (h->widgets);
914 /* widget has been added in runtime */
915 if (h->state == DLG_ACTIVE)
917 send_message (widget, WIDGET_INIT, 0);
918 send_message (widget, WIDGET_DRAW, 0);
919 send_message (widget, WIDGET_FOCUS, 0);
922 return widget->id;
925 /* --------------------------------------------------------------------------------------------- */
926 /** wrapper to simply add lefttop positioned controls */
928 unsigned long
929 add_widget (Dlg_head * h, void *w)
931 return add_widget_autopos (h, w, WPOS_KEEP_LEFT | WPOS_KEEP_TOP,
932 h->current != NULL ? h->current->data : NULL);
935 /* --------------------------------------------------------------------------------------------- */
937 unsigned long
938 add_widget_before (Dlg_head * h, void *w, void *before)
940 return add_widget_autopos (h, w, WPOS_KEEP_LEFT | WPOS_KEEP_TOP, before);
943 /* --------------------------------------------------------------------------------------------- */
945 /** delete widget from dialog */
946 void
947 del_widget (void *w)
949 Dlg_head *h = ((Widget *) w)->owner;
950 GList *d;
952 /* Don't accept NULL widget. This shouldn't happen */
953 if (w == NULL)
954 abort ();
956 d = g_list_find (h->widgets, w);
957 if (d == h->current)
959 if ((h->flags & DLG_REVERSE) != 0)
960 h->current = dlg_widget_prev (h, d);
961 else
962 h->current = dlg_widget_next (h, d);
965 h->widgets = g_list_remove_link (h->widgets, d);
966 send_message (d->data, WIDGET_DESTROY, 0);
967 g_list_free_1 (d);
969 /* widget has been deleted in runtime */
970 if (h->state == DLG_ACTIVE)
972 dlg_redraw (h);
973 dlg_focus (h);
977 /* --------------------------------------------------------------------------------------------- */
979 void
980 do_refresh (void)
982 GList *d = top_dlg;
984 if (fast_refresh)
986 if ((d != NULL) && (d->data != NULL))
987 dlg_redraw ((Dlg_head *) d->data);
989 else
991 /* Search first fullscreen dialog */
992 for (; d != NULL; d = g_list_next (d))
993 if ((d->data != NULL) && ((Dlg_head *) d->data)->fullscreen)
994 break;
995 /* back to top dialog */
996 for (; d != NULL; d = g_list_previous (d))
997 if (d->data != NULL)
998 dlg_redraw ((Dlg_head *) d->data);
1002 /* --------------------------------------------------------------------------------------------- */
1003 /** broadcast a message to all the widgets in a dialog */
1005 void
1006 dlg_broadcast_msg (Dlg_head * h, widget_msg_t msg, gboolean reverse)
1008 dlg_broadcast_msg_to (h, msg, reverse, 0);
1011 /* --------------------------------------------------------------------------------------------- */
1013 gboolean
1014 dlg_focus (Dlg_head * h)
1016 /* cannot focus disabled widget */
1017 if ((h->current != NULL) && (h->state == DLG_CONSTRUCT || h->state == DLG_ACTIVE))
1019 Widget *current = (Widget *) h->current->data;
1021 if (((current->options & W_DISABLED) == 0)
1022 && (send_message (current, WIDGET_FOCUS, 0) == MSG_HANDLED))
1024 h->callback (h, current, DLG_FOCUS, 0, NULL);
1025 return TRUE;
1029 return FALSE;
1032 /* --------------------------------------------------------------------------------------------- */
1033 /** Return true if the windows overlap */
1036 dlg_overlap (Widget * a, Widget * b)
1038 return !((b->x >= a->x + a->cols)
1039 || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
1043 /* --------------------------------------------------------------------------------------------- */
1044 /** Find the widget with the given callback in the dialog h */
1046 Widget *
1047 find_widget_type (const Dlg_head * h, callback_fn callback)
1049 GList *w;
1051 w = g_list_find_custom (h->widgets, callback, dlg_find_widget_callback);
1053 return (w == NULL) ? NULL : (Widget *) w->data;
1056 /* --------------------------------------------------------------------------------------------- */
1057 /** Find the widget with the given id */
1059 Widget *
1060 dlg_find_by_id (const Dlg_head * h, unsigned long id)
1062 GList *w;
1064 w = g_list_find_custom (h->widgets, GUINT_TO_POINTER (id), dlg_find_widget_by_id);
1065 return w != NULL ? (Widget *) w->data : NULL;
1068 /* --------------------------------------------------------------------------------------------- */
1069 /** Find the widget with the given id in the dialog h and select it */
1071 void
1072 dlg_select_by_id (const Dlg_head * h, unsigned long id)
1074 Widget *w;
1076 w = dlg_find_by_id (h, id);
1077 if (w != NULL)
1078 dlg_select_widget (w);
1081 /* --------------------------------------------------------------------------------------------- */
1083 * Try to select widget in the dialog.
1086 void
1087 dlg_select_widget (void *w)
1089 const Widget *widget = (Widget *) w;
1090 Dlg_head *h = widget->owner;
1092 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
1095 /* --------------------------------------------------------------------------------------------- */
1098 * Set widget at top of widget list and make it current.
1101 void
1102 dlg_set_top_widget (void *w)
1104 Widget *widget = (Widget *) w;
1105 Dlg_head *h = widget->owner;
1106 GList *l;
1108 l = g_list_find (h->widgets, w);
1109 if (l == NULL)
1110 abort (); /* widget is not in dialog, this should not happen */
1112 /* unfocus prevoius widget and focus current one before widget reordering */
1113 if (h->state == DLG_ACTIVE)
1114 do_select_widget (h, l, SELECT_EXACT);
1116 /* widget reordering */
1117 h->widgets = g_list_remove_link (h->widgets, l);
1118 if ((h->flags & DLG_REVERSE) != 0)
1119 h->widgets = g_list_concat (l, h->widgets);
1120 else
1121 h->widgets = g_list_concat (h->widgets, l);
1122 h->current = l;
1125 /* --------------------------------------------------------------------------------------------- */
1126 /** Try to select previous widget in the tab order */
1128 void
1129 dlg_one_up (Dlg_head * h)
1131 if (h->widgets != NULL)
1132 do_select_widget (h, dlg_widget_prev (h, h->current), SELECT_PREV);
1135 /* --------------------------------------------------------------------------------------------- */
1136 /** Try to select next widget in the tab order */
1138 void
1139 dlg_one_down (Dlg_head * h)
1141 if (h->widgets != NULL)
1142 do_select_widget (h, dlg_widget_next (h, h->current), SELECT_NEXT);
1145 /* --------------------------------------------------------------------------------------------- */
1147 void
1148 update_cursor (Dlg_head * h)
1150 GList *p = h->current;
1152 if ((p != NULL) && (h->state == DLG_ACTIVE))
1154 Widget *w;
1156 w = (Widget *) p->data;
1158 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1159 send_message (w, WIDGET_CURSOR, 0);
1160 else
1163 p = dlg_widget_next (h, p);
1164 if (p == h->current)
1165 break;
1167 w = (Widget *) p->data;
1169 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1170 if (send_message (w, WIDGET_CURSOR, 0) == MSG_HANDLED)
1171 break;
1173 while (TRUE);
1177 /* --------------------------------------------------------------------------------------------- */
1179 * Redraw the widgets in reverse order, leaving the current widget
1180 * as the last one
1183 void
1184 dlg_redraw (Dlg_head * h)
1186 if (h->state != DLG_ACTIVE)
1187 return;
1189 if (h->winch_pending)
1191 h->winch_pending = FALSE;
1192 h->callback (h, NULL, DLG_RESIZE, 0, NULL);
1195 h->callback (h, NULL, DLG_DRAW, 0, NULL);
1196 dlg_broadcast_msg (h, WIDGET_DRAW, (h->flags & DLG_REVERSE) != 0);
1197 update_cursor (h);
1200 /* --------------------------------------------------------------------------------------------- */
1202 void
1203 dlg_stop (Dlg_head * h)
1205 h->state = DLG_CLOSED;
1208 /* --------------------------------------------------------------------------------------------- */
1209 /** Init the process */
1211 void
1212 init_dlg (Dlg_head * h)
1214 if ((top_dlg != NULL) && ((Dlg_head *) top_dlg->data)->modal)
1215 h->modal = TRUE;
1217 /* add dialog to the stack */
1218 top_dlg = g_list_prepend (top_dlg, h);
1220 /* Initialize dialog manager and widgets */
1221 if (h->state == DLG_CONSTRUCT)
1223 if (!h->modal)
1224 dialog_switch_add (h);
1226 h->callback (h, NULL, DLG_INIT, 0, NULL);
1227 dlg_broadcast_msg (h, WIDGET_INIT, FALSE);
1228 dlg_read_history (h);
1231 h->state = DLG_ACTIVE;
1233 /* Select the first widget that takes focus */
1234 while (h->current != NULL && !dlg_focus (h))
1235 h->current = dlg_widget_next (h, h->current);
1237 dlg_redraw (h);
1239 h->ret_value = 0;
1242 /* --------------------------------------------------------------------------------------------- */
1244 void
1245 dlg_process_event (Dlg_head * h, int key, Gpm_Event * event)
1247 if (key == EV_NONE)
1249 if (tty_got_interrupt ())
1250 if (h->callback (h, NULL, DLG_ACTION, CK_Cancel, NULL) != MSG_HANDLED)
1251 dlg_execute_cmd (h, CK_Cancel);
1253 return;
1256 if (key == EV_MOUSE)
1257 h->mouse_status = dlg_mouse_event (h, event);
1258 else
1259 dlg_key_event (h, key);
1262 /* --------------------------------------------------------------------------------------------- */
1263 /** Shutdown the run_dlg */
1265 void
1266 dlg_run_done (Dlg_head * h)
1268 top_dlg = g_list_remove (top_dlg, h);
1270 if (h->state == DLG_CLOSED)
1272 h->callback (h, (Widget *) h->current->data, DLG_END, 0, NULL);
1273 if (!h->modal)
1274 dialog_switch_remove (h);
1279 /* --------------------------------------------------------------------------------------------- */
1281 * Standard run dialog routine
1282 * We have to keep this routine small so that we can duplicate it's
1283 * behavior on complex routines like the file routines, this way,
1284 * they can call the dlg_process_event without rewriting all the code
1288 run_dlg (Dlg_head * h)
1290 init_dlg (h);
1291 frontend_run_dlg (h);
1292 dlg_run_done (h);
1293 return h->ret_value;
1296 /* --------------------------------------------------------------------------------------------- */
1298 void
1299 destroy_dlg (Dlg_head * h)
1301 /* if some widgets have history, save all history at one moment here */
1302 dlg_save_history (h);
1303 dlg_broadcast_msg (h, WIDGET_DESTROY, FALSE);
1304 g_list_foreach (h->widgets, (GFunc) g_free, NULL);
1305 g_list_free (h->widgets);
1306 mc_event_group_del (h->event_group);
1307 g_free (h->event_group);
1308 g_free (h->title);
1309 g_free (h);
1311 do_refresh ();
1314 /* --------------------------------------------------------------------------------------------- */
1317 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1319 void
1320 dlg_save_history (Dlg_head * h)
1322 char *profile;
1323 int i;
1325 if (num_history_items_recorded == 0) /* this is how to disable */
1326 return;
1328 profile = mc_config_get_full_path (MC_HISTORY_FILE);
1329 i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
1330 if (i != -1)
1331 close (i);
1333 /* Make sure the history is only readable by the user */
1334 if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
1336 ev_history_load_save_t event_data;
1338 event_data.cfg = mc_config_init (profile, FALSE);
1339 event_data.receiver = NULL;
1341 /* get all histories in dialog */
1342 mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
1344 mc_config_save_file (event_data.cfg, NULL);
1345 mc_config_deinit (event_data.cfg);
1348 g_free (profile);
1351 /* --------------------------------------------------------------------------------------------- */
1353 char *
1354 dlg_get_title (const Dlg_head * h, size_t len)
1356 char *t;
1358 if (h == NULL)
1359 abort ();
1361 if (h->get_title != NULL)
1362 t = h->get_title (h, len);
1363 else
1364 t = g_strdup ("");
1366 return t;
1369 /* --------------------------------------------------------------------------------------------- */
1370 /** Replace widget old_w for widget new_w in the dialog */
1372 void
1373 dlg_replace_widget (Widget * old_w, Widget * new_w)
1375 Dlg_head *h = old_w->owner;
1376 gboolean should_focus = FALSE;
1378 if (h->widgets == NULL)
1379 return;
1381 if (h->current == NULL)
1382 h->current = h->widgets;
1384 if (old_w == h->current->data)
1385 should_focus = TRUE;
1387 new_w->owner = h;
1388 new_w->id = old_w->id;
1390 if (should_focus)
1391 h->current->data = new_w;
1392 else
1393 g_list_find (h->widgets, old_w)->data = new_w;
1395 send_message (old_w, WIDGET_DESTROY, 0);
1396 send_message (new_w, WIDGET_INIT, 0);
1398 if (should_focus)
1399 dlg_select_widget (new_w);
1401 if (new_w->owner->state == DLG_ACTIVE)
1402 send_message (new_w, WIDGET_DRAW, 0);
1405 /* --------------------------------------------------------------------------------------------- */