Updated italian translation.
[midnight-commander.git] / src / dialog.c
blob5585363c0db2cdf1dd24372596f76cd42db2852f
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 <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include "lib/global.h"
34 #include "lib/tty/tty.h"
35 #include "lib/skin.h"
36 #include "lib/tty/mouse.h"
37 #include "lib/tty/key.h"
38 #include "lib/strutil.h"
40 #include "help.h" /* interactive_display() */
41 #include "layout.h"
42 #include "execute.h" /* suspend_cmd() */
43 #include "cmddef.h"
44 #include "keybind.h"
45 #include "main.h" /* fast_refresh */
46 #include "setup.h" /* mouse_close_dialog */
47 #include "dialog.h"
49 #include "dialog-switch.h"
51 /* Color styles for normal and error dialogs */
52 int dialog_colors[4];
53 int alarm_colors[4];
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 *idle_hook = NULL;
62 /* left click outside of dialog closes it */
63 int mouse_close_dialog = 0;
65 static void dlg_broadcast_msg_to (Dlg_head * h, widget_msg_t message, gboolean reverse, int flags);
67 /* draw box in window */
68 void
69 draw_box (Dlg_head * h, int y, int x, int ys, int xs, gboolean single)
71 tty_draw_box (h->y + y, h->x + x, ys, xs, single);
74 void
75 init_widget (Widget * w, int y, int x, int lines, int cols,
76 callback_fn callback, mouse_h mouse_handler)
78 w->x = x;
79 w->y = y;
80 w->cols = cols;
81 w->lines = lines;
82 w->callback = callback;
83 w->mouse = mouse_handler;
84 w->owner = NULL;
86 /* Almost all widgets want to put the cursor in a suitable place */
87 w->options = W_WANT_CURSOR;
90 void
91 widget_set_size (Widget * widget, int y, int x, int lines, int cols)
93 widget->x = x;
94 widget->y = y;
95 widget->cols = cols;
96 widget->lines = lines;
97 send_message (widget, WIDGET_RESIZED, 0 /* unused */ );
100 void
101 widget_erase (Widget * w)
103 if (w != NULL)
104 tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
107 /* Clean the dialog area, draw the frame and the title */
108 void
109 common_dialog_repaint (struct Dlg_head *h)
111 int space;
113 space = (h->flags & DLG_COMPACT) ? 0 : 1;
115 tty_setcolor (DLG_NORMALC (h));
116 dlg_erase (h);
117 draw_box (h, space, space, h->lines - 2 * space, h->cols - 2 * space, FALSE);
119 if (h->title)
121 tty_setcolor (DLG_HOT_NORMALC (h));
122 dlg_move (h, space, (h->cols - str_term_width1 (h->title)) / 2);
123 tty_print_string (h->title);
127 /* this function allows to set dialog position */
128 void
129 dlg_set_position (Dlg_head * h, int y1, int x1, int y2, int x2)
131 /* save old positions, will be used to reposition childs */
132 int ox, oy, oc, ol;
133 int shift_x, shift_y, scale_x, scale_y;
135 /* save old positions, will be used to reposition childs */
136 ox = h->x;
137 oy = h->y;
138 oc = h->cols;
139 ol = h->lines;
141 h->x = x1;
142 h->y = y1;
143 h->lines = y2 - y1;
144 h->cols = x2 - x1;
146 /* dialog is empty */
147 if (h->widgets == NULL)
148 return;
150 if (h->current == NULL)
151 h->current = h->widgets;
153 /* values by which controls should be moved */
154 shift_x = h->x - ox;
155 shift_y = h->y - oy;
156 scale_x = h->cols - oc;
157 scale_y = h->lines - ol;
159 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
161 GList *w;
163 for (w = h->widgets; w != NULL; w = g_list_next (w))
165 /* there are, mainly, 2 generally possible
166 situations:
168 1. control sticks to one side - it
169 should be moved
171 2. control sticks to two sides of
172 one direction - it should be sized */
174 Widget *c = (Widget *) w->data;
175 int x = c->x;
176 int y = c->y;
177 int cols = c->cols;
178 int lines = c->lines;
180 if ((c->pos_flags & WPOS_KEEP_LEFT) && (c->pos_flags & WPOS_KEEP_RIGHT))
182 x += shift_x;
183 cols += scale_x;
185 else if (c->pos_flags & WPOS_KEEP_LEFT)
186 x += shift_x;
187 else if (c->pos_flags & WPOS_KEEP_RIGHT)
188 x += shift_x + scale_x;
190 if ((c->pos_flags & WPOS_KEEP_TOP) && (c->pos_flags & WPOS_KEEP_BOTTOM))
192 y += shift_y;
193 lines += scale_y;
195 else if (c->pos_flags & WPOS_KEEP_TOP)
196 y += shift_y;
197 else if (c->pos_flags & WPOS_KEEP_BOTTOM)
198 y += shift_y + scale_y;
200 widget_set_size (c, y, x, lines, cols);
205 /* this function sets only size, leaving positioning to automatic methods */
206 void
207 dlg_set_size (Dlg_head * h, int lines, int cols)
209 int x = h->x;
210 int y = h->y;
212 if (h->flags & DLG_CENTER)
214 y = (LINES - lines) / 2;
215 x = (COLS - cols) / 2;
218 if ((h->flags & DLG_TRYUP) && (y > 3))
219 y -= 2;
221 dlg_set_position (h, y, x, y + lines, x + cols);
224 /* Default dialog callback */
225 cb_ret_t
226 default_dlg_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
228 (void) sender;
229 (void) parm;
230 (void) data;
232 switch (msg)
234 case DLG_DRAW:
235 if (h->color != NULL)
237 common_dialog_repaint (h);
238 return MSG_HANDLED;
240 return MSG_NOT_HANDLED;
242 case DLG_IDLE:
243 dlg_broadcast_msg_to (h, WIDGET_IDLE, FALSE, W_WANT_IDLE);
244 return MSG_HANDLED;
246 case DLG_RESIZE:
247 /* this is default resizing mechanism */
248 /* the main idea of this code is to resize dialog
249 according to flags (if any of flags require automatic
250 resizing, like DLG_CENTER, end after that reposition
251 controls in dialog according to flags of widget) */
252 dlg_set_size (h, h->lines, h->cols);
253 return MSG_HANDLED;
255 default:
256 break;
259 return MSG_NOT_HANDLED;
262 Dlg_head *
263 create_dlg (gboolean modal, int y1, int x1, int lines, int cols,
264 const int *colors, dlg_cb_fn callback, const char *help_ctx,
265 const char *title, dlg_flags_t flags)
267 Dlg_head *new_d;
269 new_d = g_new0 (Dlg_head, 1);
270 new_d->modal = modal;
271 if (colors != NULL)
273 new_d->color = g_new (int, DLG_COLOR_NUM);
274 memmove (new_d->color, colors, sizeof (int) * DLG_COLOR_NUM);
276 new_d->help_ctx = help_ctx;
277 new_d->callback = (callback != NULL) ? callback : default_dlg_callback;
278 new_d->x = x1;
279 new_d->y = y1;
280 new_d->flags = flags;
281 new_d->data = NULL;
283 dlg_set_size (new_d, lines, cols);
284 new_d->fullscreen = (new_d->x == 0 && new_d->y == 0
285 && new_d->cols == COLS && new_d->lines == LINES);
287 new_d->mouse_status = MOU_NORMAL;
289 /* Strip existing spaces, add one space before and after the title */
290 if (title != NULL)
292 char *t;
294 t = g_strstrip (g_strdup (title));
295 if (*t != '\0')
296 new_d->title = g_strdup_printf (" %s ", t);
297 g_free (t);
300 return new_d;
303 void
304 dlg_set_default_colors (void)
306 dialog_colors[0] = COLOR_NORMAL;
307 dialog_colors[1] = COLOR_FOCUS;
308 dialog_colors[2] = COLOR_HOT_NORMAL;
309 dialog_colors[3] = COLOR_HOT_FOCUS;
311 alarm_colors[0] = ERROR_COLOR;
312 alarm_colors[1] = REVERSE_COLOR;
313 alarm_colors[2] = ERROR_HOT_NORMAL;
314 alarm_colors[3] = ERROR_HOT_FOCUS;
317 void
318 dlg_erase (Dlg_head * h)
320 if (h != NULL)
321 tty_fill_region (h->y, h->x, h->lines, h->cols, ' ');
324 void
325 set_idle_proc (Dlg_head * d, int enable)
327 if (enable)
328 d->flags |= DLG_WANT_IDLE;
329 else
330 d->flags &= ~DLG_WANT_IDLE;
334 * Insert widget to dialog before current widget. For dialogs populated
335 * from the bottom, make the widget current. Return widget number.
338 add_widget_autopos (Dlg_head * h, void *w, widget_pos_flags_t pos_flags)
340 Widget *widget = (Widget *) w;
342 /* Don't accept 0 widgets */
343 if (w == NULL)
344 abort ();
346 widget->x += h->x;
347 widget->y += h->y;
348 widget->owner = h;
349 widget->pos_flags = pos_flags;
350 widget->id = g_list_length (h->widgets);
352 if ((h->flags & DLG_REVERSE) != 0)
353 h->widgets = g_list_prepend (h->widgets, widget);
354 else
355 h->widgets = g_list_append (h->widgets, widget);
357 h->current = h->widgets;
359 return widget->id;
362 /* wrapper to simply add lefttop positioned controls */
364 add_widget (Dlg_head * h, void *w)
366 return add_widget_autopos (h, w, WPOS_KEEP_LEFT | WPOS_KEEP_TOP);
369 void
370 do_refresh (void)
372 GList *d = top_dlg;
374 if (fast_refresh)
376 if ((d != NULL) && (d->data != NULL))
377 dlg_redraw ((Dlg_head *) d->data);
379 else
381 /* Search first fullscreen dialog */
382 for (; d != NULL; d = g_list_next (d))
383 if ((d->data != NULL) && ((Dlg_head *) d->data)->fullscreen)
384 break;
385 /* back to top dialog */
386 for (; d != NULL; d = g_list_previous (d))
387 if (d->data != NULL)
388 dlg_redraw ((Dlg_head *) d->data);
392 /* broadcast a message to all the widgets in a dialog that have
393 * the options set to flags. If flags is zero, the message is sent
394 * to all widgets.
396 static void
397 dlg_broadcast_msg_to (Dlg_head * h, widget_msg_t message, gboolean reverse, int flags)
399 GList *p, *first;
401 if (h->widgets == NULL)
402 return;
404 if (h->current == NULL)
405 h->current = h->widgets;
407 if (reverse)
409 p = g_list_previous (h->current);
411 if (p == NULL)
412 p = g_list_last (h->widgets);
414 else
416 p = g_list_next (h->current);
418 if (p == NULL)
419 p = h->widgets;
422 first = p;
426 Widget *w = (Widget *) p->data;
428 if (reverse)
430 p = g_list_previous (p);
432 if (p == NULL)
433 p = g_list_last (h->widgets);
435 else
437 p = g_list_next (p);
439 if (p == NULL)
440 p = h->widgets;
443 if ((flags == 0) || ((flags & w->options) != 0))
444 send_message (w, message, 0);
446 while (first != p);
449 /* broadcast a message to all the widgets in a dialog */
450 void
451 dlg_broadcast_msg (Dlg_head * h, widget_msg_t message, gboolean reverse)
453 dlg_broadcast_msg_to (h, message, reverse, 0);
457 dlg_focus (Dlg_head * h)
459 if ((h->current != NULL)
460 && (send_message ((Widget *) h->current->data, WIDGET_FOCUS, 0) == MSG_HANDLED))
462 h->callback (h, (Widget *) h->current->data, DLG_FOCUS, 0, NULL);
463 return 1;
465 return 0;
468 static int
469 dlg_unfocus (Dlg_head * h)
471 if ((h->current != NULL)
472 && (send_message ((Widget *) h->current->data, WIDGET_UNFOCUS, 0) == MSG_HANDLED))
474 h->callback (h, (Widget *) h->current->data, DLG_UNFOCUS, 0, NULL);
475 return 1;
477 return 0;
480 /* Return true if the windows overlap */
482 dlg_overlap (Widget * a, Widget * b)
484 return !((b->x >= a->x + a->cols)
485 || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
488 static int
489 dlg_find_widget_callback (const void *a, const void *b)
491 const Widget *w = (const Widget *) a;
492 callback_fn f = (callback_fn) b;
494 return (w->callback == f) ? 0 : 1;
497 /* Find the widget with the given callback in the dialog h */
498 Widget *
499 find_widget_type (const Dlg_head * h, callback_fn callback)
501 GList *w;
503 w = g_list_find_custom (h->widgets, callback, dlg_find_widget_callback);
505 return (w == NULL) ? NULL : (Widget *) w->data;
508 /* Find the widget with the given dialog id in the dialog h and select it */
509 void
510 dlg_select_by_id (const Dlg_head * h, unsigned int id)
512 if (h->widgets != NULL)
514 Widget *w_found;
516 w_found = (Widget *) g_list_nth_data (h->widgets, id);
518 if (w_found != NULL)
519 dlg_select_widget (w_found);
524 /* What to do if the requested widget doesn't take focus */
525 typedef enum
527 SELECT_NEXT, /* go the the next widget */
528 SELECT_PREV, /* go the the previous widget */
529 SELECT_EXACT /* use current widget */
530 } select_dir_t;
533 * Try to select another widget. If forward is set, follow tab order.
534 * Otherwise go to the previous widget.
536 static void
537 do_select_widget (Dlg_head * h, GList * w, select_dir_t dir)
539 Widget *w0 = (Widget *) h->current->data;
541 if (!dlg_unfocus (h))
542 return;
544 h->current = w;
548 if (dlg_focus (h))
549 break;
551 switch (dir)
553 case SELECT_NEXT:
554 h->current = g_list_next (h->current);
555 if (h->current == NULL)
556 h->current = h->widgets;
557 break;
558 case SELECT_PREV:
559 h->current = g_list_previous (h->current);
560 if (h->current == NULL)
561 h->current = g_list_last (h->widgets);
562 break;
563 case SELECT_EXACT:
564 h->current = g_list_find (h->widgets, w0);
565 dlg_focus (h);
566 return;
569 while (h->current != w);
571 if (dlg_overlap (w0, (Widget *) h->current->data))
573 send_message ((Widget *) h->current->data, WIDGET_DRAW, 0);
574 send_message ((Widget *) h->current->data, WIDGET_FOCUS, 0);
579 * Try to select widget in the dialog.
581 void
582 dlg_select_widget (void *w)
584 const Widget *widget = (Widget *) w;
585 Dlg_head *h = widget->owner;
587 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_NEXT);
590 /* Try to select previous widget in the tab order */
591 void
592 dlg_one_up (Dlg_head * h)
594 if (h->widgets != NULL)
596 GList *prev;
598 prev = g_list_previous (h->current);
599 if (prev == NULL)
600 prev = g_list_last (h->widgets);
602 do_select_widget (h, prev, SELECT_PREV);
606 /* Try to select next widget in the tab order */
607 void
608 dlg_one_down (Dlg_head * h)
610 if (h->widgets != NULL)
612 GList *next;
614 next = g_list_next (h->current);
615 if (next == NULL)
616 next = h->widgets;
617 do_select_widget (h, next, SELECT_NEXT);
621 void
622 update_cursor (Dlg_head * h)
624 GList *p = h->current;
626 if (p != NULL)
628 if (((Widget *) (p->data))->options & W_WANT_CURSOR)
629 send_message ((Widget *) p->data, WIDGET_CURSOR, 0);
630 else
633 p = g_list_next (p);
634 if (p == NULL)
635 p = h->widgets;
637 if (p == h->current)
638 break;
640 if (((Widget *) (p->data))->options & W_WANT_CURSOR)
641 if (send_message ((Widget *) p->data, WIDGET_CURSOR, 0) == MSG_HANDLED)
642 break;
644 while (TRUE);
648 /* Redraw the widgets in reverse order, leaving the current widget
649 * as the last one
651 void
652 dlg_redraw (Dlg_head * h)
654 if (h->winch_pending)
656 h->winch_pending = FALSE;
657 h->callback (h, NULL, DLG_RESIZE, 0, NULL);
660 h->callback (h, NULL, DLG_DRAW, 0, NULL);
661 dlg_broadcast_msg (h, WIDGET_DRAW, TRUE);
662 update_cursor (h);
665 void
666 dlg_stop (Dlg_head * h)
668 h->state = DLG_CLOSED;
671 static void
672 refresh_cmd (void)
674 #ifdef HAVE_SLANG
675 tty_touch_screen ();
676 mc_refresh ();
677 #else
678 /* Use this if the refreshes fail */
679 clr_scr ();
680 repaint_screen ();
681 #endif /* HAVE_SLANG */
684 static cb_ret_t
685 dlg_execute_cmd (Dlg_head * h, unsigned long command)
687 cb_ret_t ret = MSG_HANDLED;
688 switch (command)
690 case CK_DialogOK:
691 h->ret_value = B_ENTER;
692 dlg_stop (h);
693 break;
694 case CK_DialogCancel:
695 h->ret_value = B_CANCEL;
696 dlg_stop (h);
697 break;
699 case CK_DialogPrevItem:
700 dlg_one_up (h);
701 break;
702 case CK_DialogNextItem:
703 dlg_one_down (h);
704 break;
706 case CK_DialogHelp:
707 interactive_display (NULL, h->help_ctx);
708 do_refresh ();
709 break;
711 case CK_DialogSuspend:
712 suspend_cmd ();
713 refresh_cmd ();
714 break;
715 case CK_DialogRefresh:
716 refresh_cmd ();
717 break;
719 case CK_DialogListCmd:
720 if (!h->modal)
721 dialog_switch_list ();
722 else
723 ret = MSG_NOT_HANDLED;
724 break;
725 case CK_DialogNextCmd:
726 if (!h->modal)
727 dialog_switch_next ();
728 else
729 ret = MSG_NOT_HANDLED;
730 break;
731 case CK_DialogPrevCmd:
732 if (!h->modal)
733 dialog_switch_prev ();
734 else
735 ret = MSG_NOT_HANDLED;
736 break;
738 default:
739 ret = MSG_NOT_HANDLED;
742 return ret;
745 static cb_ret_t
746 dlg_handle_key (Dlg_head * h, int d_key)
748 unsigned long command;
749 command = lookup_keymap_command (dialog_map, d_key);
750 if ((command == CK_Ignore_Key) || (dlg_execute_cmd (h, command) == MSG_NOT_HANDLED))
751 return MSG_NOT_HANDLED;
752 else
753 return MSG_HANDLED;
756 static int
757 dlg_mouse_event (Dlg_head * h, Gpm_Event * event)
759 GList *item;
760 GList *starting_widget = h->current;
761 Gpm_Event new_event;
762 int x = event->x;
763 int y = event->y;
765 /* close the dialog by mouse click out of dialog area */
766 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0) && ((event->type & GPM_DOWN) != 0) /* left click */
767 && !((x > h->x) && (x <= h->x + h->cols) && (y > h->y) && (y <= h->y + h->lines)))
769 h->ret_value = B_CANCEL;
770 dlg_stop (h);
771 return MOU_NORMAL;
774 item = starting_widget;
777 Widget *widget;
779 widget = (Widget *) item->data;
780 item = g_list_next (item);
781 if (item == NULL)
782 item = h->widgets;
784 if ((x > widget->x) && (x <= widget->x + widget->cols)
785 && (y > widget->y) && (y <= widget->y + widget->lines))
787 new_event = *event;
788 new_event.x -= widget->x;
789 new_event.y -= widget->y;
791 if (widget->mouse != NULL)
792 return widget->mouse (&new_event, widget);
795 while (item != starting_widget);
797 return MOU_NORMAL;
800 static cb_ret_t
801 dlg_try_hotkey (Dlg_head * h, int d_key)
803 GList *hot_cur;
804 cb_ret_t handled;
805 int c;
807 if (h->widgets == NULL)
808 return MSG_NOT_HANDLED;
810 if (h->current == NULL)
811 h->current = h->widgets;
814 * Explanation: we don't send letter hotkeys to other widgets if
815 * the currently selected widget is an input line
818 if (((Widget *) h->current->data)->options & W_IS_INPUT)
820 /* skip ascii control characters, anything else can valid character in
821 * some encoding */
822 if (d_key >= 32 && d_key < 256)
823 return MSG_NOT_HANDLED;
826 /* If it's an alt key, send the message */
827 c = d_key & ~ALT (0);
828 if (d_key & ALT (0) && g_ascii_isalpha (c))
829 d_key = g_ascii_tolower (c);
831 handled = MSG_NOT_HANDLED;
832 if (((Widget *) h->current->data)->options & W_WANT_HOTKEY)
833 handled = send_message ((Widget *) h->current->data, WIDGET_HOTKEY, d_key);
835 /* If not used, send hotkey to other widgets */
836 if (handled == MSG_HANDLED)
837 return MSG_HANDLED;
839 hot_cur = g_list_next (h->current);
840 if (hot_cur == NULL)
841 hot_cur = h->widgets;
843 /* send it to all widgets */
844 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
846 if (((Widget *) hot_cur->data)->options & W_WANT_HOTKEY)
847 handled = send_message ((Widget *) hot_cur->data, WIDGET_HOTKEY, d_key);
849 if (handled == MSG_NOT_HANDLED)
851 hot_cur = g_list_next (hot_cur);
852 if (hot_cur == NULL)
853 hot_cur = h->widgets;
857 if (handled == MSG_HANDLED)
858 do_select_widget (h, hot_cur, SELECT_EXACT);
860 return handled;
863 static void
864 dlg_key_event (Dlg_head * h, int d_key)
866 cb_ret_t handled;
868 if (h->widgets == NULL)
869 return;
871 if (h->current == NULL)
872 h->current = h->widgets;
874 /* TAB used to cycle */
875 if ((h->flags & DLG_WANT_TAB) == 0)
877 if (d_key == '\t')
879 dlg_one_down (h);
880 return;
882 else if (d_key == KEY_BTAB)
884 dlg_one_up (h);
885 return;
889 /* first can dlg_callback handle the key */
890 handled = h->callback (h, NULL, DLG_KEY, d_key, NULL);
892 /* next try the hotkey */
893 if (handled == MSG_NOT_HANDLED)
894 handled = dlg_try_hotkey (h, d_key);
896 if (handled == MSG_HANDLED)
897 h->callback (h, NULL, DLG_HOTKEY_HANDLED, 0, NULL);
898 else
899 /* not used - then try widget_callback */
900 handled = send_message ((Widget *) h->current->data, WIDGET_KEY, d_key);
902 /* not used- try to use the unhandled case */
903 if (handled == MSG_NOT_HANDLED)
904 handled = h->callback (h, NULL, DLG_UNHANDLED_KEY, d_key, NULL);
906 if (handled == MSG_NOT_HANDLED)
907 handled = dlg_handle_key (h, d_key);
909 h->callback (h, NULL, DLG_POST_KEY, d_key, NULL);
912 /* Init the process */
913 void
914 init_dlg (Dlg_head * h)
916 if ((top_dlg != NULL) && ((Dlg_head *) top_dlg->data)->modal)
917 h->modal = TRUE;
920 /* add dialog to the stack */
921 top_dlg = g_list_prepend (top_dlg, h);
923 /* Initialize dialog manager and widgets */
924 if (h->state == DLG_ACTIVE)
926 if (!h->modal)
927 dialog_switch_add (h);
929 h->callback (h, NULL, DLG_INIT, 0, NULL);
930 dlg_broadcast_msg (h, WIDGET_INIT, FALSE);
933 h->state = DLG_ACTIVE;
935 dlg_redraw (h);
937 /* Select the first widget that takes focus */
938 while (h->current != NULL && !dlg_focus (h))
940 h->current = g_list_next (h->current);
941 if (h->current == NULL)
942 h->current = h->widgets;
945 h->ret_value = 0;
948 void
949 dlg_process_event (Dlg_head * h, int key, Gpm_Event * event)
951 if (key == EV_NONE)
953 if (tty_got_interrupt ())
954 dlg_execute_cmd (h, CK_DialogCancel);
956 return;
959 if (key == EV_MOUSE)
960 h->mouse_status = dlg_mouse_event (h, event);
961 else
962 dlg_key_event (h, key);
965 static void
966 frontend_run_dlg (Dlg_head * h)
968 int d_key;
969 Gpm_Event event;
971 event.x = -1;
973 /* close opened editors, viewers, etc */
974 if (!h->modal && midnight_shutdown)
976 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
977 return;
980 while (h->state == DLG_ACTIVE)
982 if (winch_flag)
983 change_screen_size ();
985 if (is_idle ())
987 if (idle_hook)
988 execute_hooks (idle_hook);
990 while ((h->flags & DLG_WANT_IDLE) && is_idle ())
991 h->callback (h, NULL, DLG_IDLE, 0, NULL);
993 /* Allow terminating the dialog from the idle handler */
994 if (h->state != DLG_ACTIVE)
995 break;
998 update_cursor (h);
1000 /* Clear interrupt flag */
1001 tty_got_interrupt ();
1002 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
1004 dlg_process_event (h, d_key, &event);
1006 if (h->state == DLG_CLOSED)
1007 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
1011 /* Shutdown the run_dlg */
1012 void
1013 dlg_run_done (Dlg_head * h)
1015 if (h->state == DLG_CLOSED)
1017 h->callback (h, (Widget *) h->current->data, DLG_END, 0, NULL);
1018 if (!h->modal)
1019 dialog_switch_remove (h);
1022 top_dlg = g_list_remove (top_dlg, h);
1025 /* Standard run dialog routine
1026 * We have to keep this routine small so that we can duplicate it's
1027 * behavior on complex routines like the file routines, this way,
1028 * they can call the dlg_process_event without rewriting all the code
1031 run_dlg (Dlg_head * h)
1033 init_dlg (h);
1034 frontend_run_dlg (h);
1035 dlg_run_done (h);
1036 return h->ret_value;
1039 void
1040 destroy_dlg (Dlg_head * h)
1042 dlg_broadcast_msg (h, WIDGET_DESTROY, FALSE);
1043 g_list_foreach (h->widgets, (GFunc) g_free, NULL);
1044 g_list_free (h->widgets);
1045 g_free (h->color);
1046 g_free (h->title);
1047 g_free (h);
1049 do_refresh ();
1052 char *
1053 dlg_get_title (const Dlg_head *h, size_t len)
1055 char *t;
1057 if (h == NULL)
1058 abort ();
1060 if (h->get_title != NULL)
1061 t = h->get_title (h, len);
1062 else
1063 t = g_strdup ("");
1065 return t;
1068 /* Replace widget old_w for widget new_w in the dialog */
1069 void
1070 dlg_replace_widget (Widget * old_w, Widget * new_w)
1072 Dlg_head *h = old_w->owner;
1073 gboolean should_focus = FALSE;
1075 if (h->widgets == NULL)
1076 return;
1078 if (h->current == NULL)
1079 h->current = h->widgets;
1081 if (old_w == h->current->data)
1082 should_focus = TRUE;
1084 new_w->owner = h;
1085 new_w->id = old_w->id;
1087 if (should_focus)
1088 h->current->data = new_w;
1089 else
1090 g_list_find (h->widgets, old_w)->data = new_w;
1092 send_message (old_w, WIDGET_DESTROY, 0);
1093 send_message (new_w, WIDGET_INIT, 0);
1095 if (should_focus)
1096 dlg_select_widget (new_w);
1098 send_message (new_w, WIDGET_DRAW, 0);