Reimplemented widget list in dialog using GList.
[midnight-commander.git] / src / dialog.c
blobf7e2134384174be3094cafd33abe4be7441a4147
1 /* Dialog box features module for the Midnight Commander
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2007 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 "dialog.h"
42 #include "layout.h"
43 #include "execute.h" /* suspend_cmd() */
44 #include "cmddef.h"
45 #include "keybind.h"
46 #include "main.h" /* fast_refresh */
47 #include "setup.h" /* mouse_close_dialog */
49 /* Color styles for normal and error dialogs */
50 int dialog_colors[4];
51 int alarm_colors[4];
53 /* Primitive way to check if the the current dialog is our dialog */
54 /* This is needed by async routines like load_prompt */
55 GList *current_dlg = NULL;
57 /* A hook list for idle events */
58 Hook *idle_hook = NULL;
60 /* left click outside of dialog closes it */
61 int mouse_close_dialog = 0;
63 static void dlg_broadcast_msg_to (Dlg_head * h, widget_msg_t message, gboolean reverse, int flags);
65 /* draw box in window */
66 void
67 draw_box (Dlg_head * h, int y, int x, int ys, int xs, gboolean single)
69 tty_draw_box (h->y + y, h->x + x, ys, xs, single);
72 void
73 init_widget (Widget * w, int y, int x, int lines, int cols,
74 callback_fn callback, mouse_h mouse_handler)
76 w->x = x;
77 w->y = y;
78 w->cols = cols;
79 w->lines = lines;
80 w->callback = callback;
81 w->mouse = mouse_handler;
82 w->parent = 0;
84 /* Almost all widgets want to put the cursor in a suitable place */
85 w->options = W_WANT_CURSOR;
88 void
89 widget_set_size (Widget * widget, int y, int x, int lines, int cols)
91 widget->x = x;
92 widget->y = y;
93 widget->cols = cols;
94 widget->lines = lines;
95 send_message (widget, WIDGET_RESIZED, 0 /* unused */ );
98 void
99 widget_erase (Widget * w)
101 if (w != NULL)
102 tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
105 /* Clean the dialog area, draw the frame and the title */
106 void
107 common_dialog_repaint (struct Dlg_head *h)
109 int space;
111 space = (h->flags & DLG_COMPACT) ? 0 : 1;
113 tty_setcolor (DLG_NORMALC (h));
114 dlg_erase (h);
115 draw_box (h, space, space, h->lines - 2 * space, h->cols - 2 * space, FALSE);
117 if (h->title)
119 tty_setcolor (DLG_HOT_NORMALC (h));
120 dlg_move (h, space, (h->cols - str_term_width1 (h->title)) / 2);
121 tty_print_string (h->title);
125 /* this function allows to set dialog position */
126 void
127 dlg_set_position (Dlg_head * h, int y1, int x1, int y2, int x2)
129 /* save old positions, will be used to reposition childs */
130 int ox, oy, oc, ol;
131 int shift_x, shift_y, scale_x, scale_y;
133 /* save old positions, will be used to reposition childs */
134 ox = h->x;
135 oy = h->y;
136 oc = h->cols;
137 ol = h->lines;
139 h->x = x1;
140 h->y = y1;
141 h->lines = y2 - y1;
142 h->cols = x2 - x1;
144 /* dialog is empty */
145 if (h->widgets == NULL)
146 return;
148 if (h->current == NULL)
149 h->current = h->widgets;
151 /* values by which controls should be moved */
152 shift_x = h->x - ox;
153 shift_y = h->y - oy;
154 scale_x = h->cols - oc;
155 scale_y = h->lines - ol;
157 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
159 GList *w;
161 for (w = h->widgets; w != NULL; w = g_list_next (w))
163 /* there are, mainly, 2 generally possible
164 situations:
166 1. control sticks to one side - it
167 should be moved
169 2. control sticks to two sides of
170 one direction - it should be sized */
172 Widget *c = (Widget *) w->data;
173 int x = c->x;
174 int y = c->y;
175 int cols = c->cols;
176 int lines = c->lines;
178 if ((c->pos_flags & WPOS_KEEP_LEFT) && (c->pos_flags & WPOS_KEEP_RIGHT))
180 x += shift_x;
181 cols += scale_x;
183 else if (c->pos_flags & WPOS_KEEP_LEFT)
184 x += shift_x;
185 else if (c->pos_flags & WPOS_KEEP_RIGHT)
186 x += shift_x + scale_x;
188 if ((c->pos_flags & WPOS_KEEP_TOP) && (c->pos_flags & WPOS_KEEP_BOTTOM))
190 y += shift_y;
191 lines += scale_y;
193 else if (c->pos_flags & WPOS_KEEP_TOP)
194 y += shift_y;
195 else if (c->pos_flags & WPOS_KEEP_BOTTOM)
196 y += shift_y + scale_y;
198 widget_set_size (c, y, x, lines, cols);
203 /* this function sets only size, leaving positioning to automatic methods */
204 void
205 dlg_set_size (Dlg_head * h, int lines, int cols)
207 int x = h->x;
208 int y = h->y;
210 if (h->flags & DLG_CENTER)
212 y = (LINES - lines) / 2;
213 x = (COLS - cols) / 2;
216 if ((h->flags & DLG_TRYUP) && (y > 3))
217 y -= 2;
219 dlg_set_position (h, y, x, y + lines, x + cols);
222 /* Default dialog callback */
223 cb_ret_t
224 default_dlg_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
226 (void) sender;
227 (void) parm;
228 (void) data;
230 switch (msg)
232 case DLG_DRAW:
233 if (h->color != NULL)
235 common_dialog_repaint (h);
236 return MSG_HANDLED;
238 return MSG_NOT_HANDLED;
240 case DLG_IDLE:
241 dlg_broadcast_msg_to (h, WIDGET_IDLE, FALSE, W_WANT_IDLE);
242 return MSG_HANDLED;
244 case DLG_RESIZE:
245 /* this is default resizing mechanism */
246 /* the main idea of this code is to resize dialog
247 according to flags (if any of flags require automatic
248 resizing, like DLG_CENTER, end after that reposition
249 controls in dialog according to flags of widget) */
250 dlg_set_size (h, h->lines, h->cols);
251 return MSG_HANDLED;
253 default:
254 break;
257 return MSG_NOT_HANDLED;
260 Dlg_head *
261 create_dlg (int y1, int x1, int lines, int cols, const int *colors,
262 dlg_cb_fn callback, const char *help_ctx, const char *title, dlg_flags_t flags)
264 Dlg_head *new_d;
266 new_d = g_new0 (Dlg_head, 1);
267 if (colors != NULL)
269 new_d->color = g_new (int, DLG_COLOR_NUM);
270 memmove (new_d->color, colors, sizeof (int) * DLG_COLOR_NUM);
272 new_d->help_ctx = help_ctx;
273 new_d->callback = (callback != NULL) ? callback : default_dlg_callback;
274 new_d->x = x1;
275 new_d->y = y1;
276 new_d->flags = flags;
277 new_d->data = NULL;
279 dlg_set_size (new_d, lines, cols);
280 new_d->fullscreen = (new_d->x == 0 && new_d->y == 0
281 && new_d->cols == COLS && new_d->lines == LINES);
283 new_d->mouse_status = MOU_NORMAL;
285 /* Strip existing spaces, add one space before and after the title */
286 if (title != NULL)
288 char *t;
290 t = g_strstrip (g_strdup (title));
291 if (*t != '\0')
292 new_d->title = g_strdup_printf (" %s ", t);
293 g_free (t);
296 return new_d;
299 void
300 dlg_set_default_colors (void)
302 dialog_colors[0] = COLOR_NORMAL;
303 dialog_colors[1] = COLOR_FOCUS;
304 dialog_colors[2] = COLOR_HOT_NORMAL;
305 dialog_colors[3] = COLOR_HOT_FOCUS;
307 alarm_colors[0] = ERROR_COLOR;
308 alarm_colors[1] = REVERSE_COLOR;
309 alarm_colors[2] = ERROR_HOT_NORMAL;
310 alarm_colors[3] = ERROR_HOT_FOCUS;
313 void
314 dlg_erase (Dlg_head * h)
316 if (h != NULL)
317 tty_fill_region (h->y, h->x, h->lines, h->cols, ' ');
320 void
321 set_idle_proc (Dlg_head * d, int enable)
323 if (enable)
324 d->flags |= DLG_WANT_IDLE;
325 else
326 d->flags &= ~DLG_WANT_IDLE;
330 * Insert widget to dialog before current widget. For dialogs populated
331 * from the bottom, make the widget current. Return widget number.
334 add_widget_autopos (Dlg_head * h, void *w, widget_pos_flags_t pos_flags)
336 Widget *widget = (Widget *) w;
338 /* Don't accept 0 widgets */
339 if (w == NULL)
340 abort ();
342 widget->x += h->x;
343 widget->y += h->y;
344 widget->parent = h;
345 widget->pos_flags = pos_flags;
346 widget->dlg_id = g_list_length (h->widgets);
348 if ((h->flags & DLG_REVERSE) != 0)
349 h->widgets = g_list_prepend (h->widgets, widget);
350 else
351 h->widgets = g_list_append (h->widgets, widget);
353 h->current = h->widgets;
355 return widget->dlg_id;
358 /* wrapper to simply add lefttop positioned controls */
360 add_widget (Dlg_head * h, void *w)
362 return add_widget_autopos (h, w, WPOS_KEEP_LEFT | WPOS_KEEP_TOP);
365 void
366 do_refresh (void)
368 GList *d = current_dlg;
370 if (fast_refresh)
372 if ((d != NULL) && (d->data != NULL))
373 dlg_redraw ((Dlg_head *) d->data);
375 else
377 /* Search first fullscreen dialog */
378 for (; d != NULL; d = g_list_next (d))
379 if ((d->data != NULL) && ((Dlg_head *) d->data)->fullscreen)
380 break;
381 /* back to top dialog */
382 for (; d != NULL; d = g_list_previous (d))
383 if (d->data != NULL)
384 dlg_redraw ((Dlg_head *) d->data);
388 /* broadcast a message to all the widgets in a dialog that have
389 * the options set to flags. If flags is zero, the message is sent
390 * to all widgets.
392 static void
393 dlg_broadcast_msg_to (Dlg_head * h, widget_msg_t message, gboolean reverse, int flags)
395 GList *p, *first;
397 if (h->widgets == NULL)
398 return;
400 if (h->current == NULL)
401 h->current = h->widgets;
403 if (reverse)
405 p = g_list_previous (h->current);
407 if (p == NULL)
408 p = g_list_last (h->widgets);
410 else
412 p = g_list_next (h->current);
414 if (p == NULL)
415 p = h->widgets;
418 first = p;
422 Widget *w = (Widget *) p->data;
424 if (reverse)
426 p = g_list_previous (p);
428 if (p == NULL)
429 p = g_list_last (h->widgets);
431 else
433 p = g_list_next (p);
435 if (p == NULL)
436 p = h->widgets;
439 if ((flags == 0) || ((flags & w->options) != 0))
440 send_message (w, message, 0);
442 while (first != p);
445 /* broadcast a message to all the widgets in a dialog */
446 void
447 dlg_broadcast_msg (Dlg_head * h, widget_msg_t message, gboolean reverse)
449 dlg_broadcast_msg_to (h, message, reverse, 0);
453 dlg_focus (Dlg_head * h)
455 if ((h->current != NULL)
456 && (send_message ((Widget *) h->current->data, WIDGET_FOCUS, 0) == MSG_HANDLED))
458 h->callback (h, (Widget *) h->current->data, DLG_FOCUS, 0, NULL);
459 return 1;
461 return 0;
464 static int
465 dlg_unfocus (Dlg_head * h)
467 if ((h->current != NULL)
468 && (send_message ((Widget *) h->current->data, WIDGET_UNFOCUS, 0) == MSG_HANDLED))
470 h->callback (h, (Widget *) h->current->data, DLG_UNFOCUS, 0, NULL);
471 return 1;
473 return 0;
476 /* Return true if the windows overlap */
478 dlg_overlap (Widget * a, Widget * b)
480 return !((b->x >= a->x + a->cols)
481 || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
484 static int
485 dlg_find_widget_callback (const void *a, const void *b)
487 const Widget *w = (const Widget *) a;
488 callback_fn f = (callback_fn) b;
490 return (w->callback == f) ? 0 : 1;
493 /* Find the widget with the given callback in the dialog h */
494 Widget *
495 find_widget_type (const Dlg_head * h, callback_fn callback)
497 GList *w;
499 w = g_list_find_custom (h->widgets, callback, dlg_find_widget_callback);
501 return (w == NULL) ? NULL : (Widget *) w->data;
504 /* Find the widget with the given dialog id in the dialog h and select it */
505 void
506 dlg_select_by_id (const Dlg_head * h, unsigned int id)
508 if (h->widgets != NULL)
510 Widget *w_found;
512 w_found = (Widget *) g_list_nth_data (h->widgets, id);
514 if (w_found != NULL)
515 dlg_select_widget (w_found);
520 /* What to do if the requested widget doesn't take focus */
521 typedef enum
523 SELECT_NEXT, /* go the the next widget */
524 SELECT_PREV, /* go the the previous widget */
525 SELECT_EXACT /* use current widget */
526 } select_dir_t;
529 * Try to select another widget. If forward is set, follow tab order.
530 * Otherwise go to the previous widget.
532 static void
533 do_select_widget (Dlg_head * h, GList * w, select_dir_t dir)
535 Widget *w0 = (Widget *) h->current->data;
537 if (!dlg_unfocus (h))
538 return;
540 h->current = w;
544 if (dlg_focus (h))
545 break;
547 switch (dir)
549 case SELECT_NEXT:
550 h->current = g_list_next (h->current);
551 if (h->current == NULL)
552 h->current = h->widgets;
553 break;
554 case SELECT_PREV:
555 h->current = g_list_previous (h->current);
556 if (h->current == NULL)
557 h->current = g_list_last (h->widgets);
558 break;
559 case SELECT_EXACT:
560 h->current = g_list_find (h->widgets, w0);
561 dlg_focus (h);
562 return;
565 while (h->current != w);
567 if (dlg_overlap (w0, (Widget *) h->current->data))
569 send_message ((Widget *) h->current->data, WIDGET_DRAW, 0);
570 send_message ((Widget *) h->current->data, WIDGET_FOCUS, 0);
575 * Try to select widget in the dialog.
577 void
578 dlg_select_widget (void *w)
580 const Widget *widget = (Widget *) w;
581 Dlg_head *h = widget->parent;
583 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_NEXT);
586 /* Try to select previous widget in the tab order */
587 void
588 dlg_one_up (Dlg_head * h)
590 if (h->widgets != NULL)
592 GList *prev;
594 prev = g_list_previous (h->current);
595 if (prev == NULL)
596 prev = g_list_last (h->widgets);
598 do_select_widget (h, prev, SELECT_PREV);
602 /* Try to select next widget in the tab order */
603 void
604 dlg_one_down (Dlg_head * h)
606 if (h->widgets != NULL)
608 GList *next;
610 next = g_list_next (h->current);
611 if (next == NULL)
612 next = h->widgets;
613 do_select_widget (h, next, SELECT_NEXT);
617 void
618 update_cursor (Dlg_head * h)
620 GList *p = h->current;
622 if (p != NULL)
624 if (((Widget *) (p->data))->options & W_WANT_CURSOR)
625 send_message ((Widget *) p->data, WIDGET_CURSOR, 0);
626 else
629 p = g_list_next (p);
630 if (p == NULL)
631 p = h->widgets;
633 if (p == h->current)
634 break;
636 if (((Widget *) (p->data))->options & W_WANT_CURSOR)
637 if (send_message ((Widget *) p->data, WIDGET_CURSOR, 0) == MSG_HANDLED)
638 break;
640 while (TRUE);
644 /* Redraw the widgets in reverse order, leaving the current widget
645 * as the last one
647 void
648 dlg_redraw (Dlg_head * h)
650 h->callback (h, NULL, DLG_DRAW, 0, NULL);
651 dlg_broadcast_msg (h, WIDGET_DRAW, TRUE);
652 update_cursor (h);
655 void
656 dlg_stop (Dlg_head * h)
658 h->state = DLG_CLOSED;
661 static void
662 refresh_cmd (void)
664 #ifdef HAVE_SLANG
665 tty_touch_screen ();
666 mc_refresh ();
667 #else
668 /* Use this if the refreshes fail */
669 clr_scr ();
670 repaint_screen ();
671 #endif /* HAVE_SLANG */
674 static cb_ret_t
675 dlg_execute_cmd (Dlg_head * h, unsigned long command)
677 cb_ret_t ret = MSG_HANDLED;
678 switch (command)
680 case CK_DialogOK:
681 h->ret_value = B_ENTER;
682 dlg_stop (h);
683 break;
684 case CK_DialogCancel:
685 h->ret_value = B_CANCEL;
686 dlg_stop (h);
687 break;
688 case CK_DialogPrevItem:
689 dlg_one_up (h);
690 break;
691 case CK_DialogNextItem:
692 dlg_one_down (h);
693 break;
694 case CK_DialogHelp:
695 interactive_display (NULL, h->help_ctx);
696 do_refresh ();
697 break;
698 case CK_DialogSuspend:
699 suspend_cmd ();
700 refresh_cmd ();
701 break;
702 case CK_DialogRefresh:
703 refresh_cmd ();
704 break;
705 default:
706 ret = MSG_NOT_HANDLED;
709 return ret;
712 static cb_ret_t
713 dlg_handle_key (Dlg_head * h, int d_key)
715 unsigned long command;
716 command = lookup_keymap_command (dialog_map, d_key);
717 if ((command == CK_Ignore_Key) || (dlg_execute_cmd (h, command) == MSG_NOT_HANDLED))
718 return MSG_NOT_HANDLED;
719 else
720 return MSG_HANDLED;
723 static int
724 dlg_mouse_event (Dlg_head * h, Gpm_Event * event)
726 GList *item;
727 GList *starting_widget = h->current;
728 Gpm_Event new_event;
729 int x = event->x;
730 int y = event->y;
732 /* close the dialog by mouse click out of dialog area */
733 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0) && ((event->type & GPM_DOWN) != 0) /* left click */
734 && !((x > h->x) && (x <= h->x + h->cols) && (y > h->y) && (y <= h->y + h->lines)))
736 h->ret_value = B_CANCEL;
737 dlg_stop (h);
738 return MOU_NORMAL;
741 item = starting_widget;
744 Widget *widget;
746 widget = (Widget *) item->data;
747 item = g_list_next (item);
748 if (item == NULL)
749 item = h->widgets;
751 if ((x > widget->x) && (x <= widget->x + widget->cols)
752 && (y > widget->y) && (y <= widget->y + widget->lines))
754 new_event = *event;
755 new_event.x -= widget->x;
756 new_event.y -= widget->y;
758 if (widget->mouse != NULL)
759 return widget->mouse (&new_event, widget);
762 while (item != starting_widget);
764 return MOU_NORMAL;
767 static cb_ret_t
768 dlg_try_hotkey (Dlg_head * h, int d_key)
770 GList *hot_cur;
771 cb_ret_t handled;
772 int c;
774 if (h->widgets == NULL)
775 return MSG_NOT_HANDLED;
777 if (h->current == NULL)
778 h->current = h->widgets;
781 * Explanation: we don't send letter hotkeys to other widgets if
782 * the currently selected widget is an input line
785 if (((Widget *) h->current->data)->options & W_IS_INPUT)
787 /* skip ascii control characters, anything else can valid character in
788 * some encoding */
789 if (d_key >= 32 && d_key < 256)
790 return MSG_NOT_HANDLED;
793 /* If it's an alt key, send the message */
794 c = d_key & ~ALT (0);
795 if (d_key & ALT (0) && g_ascii_isalpha (c))
796 d_key = g_ascii_tolower (c);
798 handled = MSG_NOT_HANDLED;
799 if (((Widget *) h->current->data)->options & W_WANT_HOTKEY)
800 handled = send_message ((Widget *) h->current->data, WIDGET_HOTKEY, d_key);
802 /* If not used, send hotkey to other widgets */
803 if (handled == MSG_HANDLED)
804 return MSG_HANDLED;
806 hot_cur = g_list_next (h->current);
807 if (hot_cur == NULL)
808 hot_cur = h->widgets;
810 /* send it to all widgets */
811 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
813 if (((Widget *) hot_cur->data)->options & W_WANT_HOTKEY)
814 handled = send_message ((Widget *) hot_cur->data, WIDGET_HOTKEY, d_key);
816 if (handled == MSG_NOT_HANDLED)
818 hot_cur = g_list_next (hot_cur);
819 if (hot_cur == NULL)
820 hot_cur = h->widgets;
824 if (handled == MSG_HANDLED)
825 do_select_widget (h, hot_cur, SELECT_EXACT);
827 return handled;
830 static void
831 dlg_key_event (Dlg_head * h, int d_key)
833 cb_ret_t handled;
835 if (h->widgets == NULL)
836 return;
838 if (h->current == NULL)
839 h->current = h->widgets;
841 /* TAB used to cycle */
842 if ((h->flags & DLG_WANT_TAB) == 0)
844 if (d_key == '\t')
846 dlg_one_down (h);
847 return;
849 else if (d_key == KEY_BTAB)
851 dlg_one_up (h);
852 return;
856 /* first can dlg_callback handle the key */
857 handled = h->callback (h, NULL, DLG_KEY, d_key, NULL);
859 /* next try the hotkey */
860 if (handled == MSG_NOT_HANDLED)
861 handled = dlg_try_hotkey (h, d_key);
863 if (handled == MSG_HANDLED)
864 h->callback (h, NULL, DLG_HOTKEY_HANDLED, 0, NULL);
865 else
866 /* not used - then try widget_callback */
867 handled = send_message ((Widget *) h->current->data, WIDGET_KEY, d_key);
869 /* not used- try to use the unhandled case */
870 if (handled == MSG_NOT_HANDLED)
871 handled = h->callback (h, NULL, DLG_UNHANDLED_KEY, d_key, NULL);
873 if (handled == MSG_NOT_HANDLED)
874 handled = dlg_handle_key (h, d_key);
876 h->callback (h, NULL, DLG_POST_KEY, d_key, NULL);
879 /* Init the process */
880 void
881 init_dlg (Dlg_head * h)
883 /* add dialog to the stack */
884 current_dlg = g_list_prepend (current_dlg, h);
886 /* Initialize dialog manager and widgets */
887 h->callback (h, NULL, DLG_INIT, 0, NULL);
888 dlg_broadcast_msg (h, WIDGET_INIT, FALSE);
890 dlg_redraw (h);
892 /* Select the first widget that takes focus */
893 while (h->current != NULL && !dlg_focus (h))
895 h->current = g_list_next (h->current);
896 if (h->current == NULL)
897 h->current = h->widgets;
900 h->ret_value = 0;
901 h->state = DLG_ACTIVE;
904 void
905 dlg_process_event (Dlg_head * h, int key, Gpm_Event * event)
907 if (key == EV_NONE)
909 if (tty_got_interrupt ())
910 dlg_execute_cmd (h, CK_DialogCancel);
912 return;
915 if (key == EV_MOUSE)
916 h->mouse_status = dlg_mouse_event (h, event);
917 else
918 dlg_key_event (h, key);
921 static void
922 frontend_run_dlg (Dlg_head * h)
924 int d_key;
925 Gpm_Event event;
927 event.x = -1;
928 while (h->state == DLG_ACTIVE)
930 if (winch_flag)
931 change_screen_size ();
933 if (is_idle ())
935 if (idle_hook)
936 execute_hooks (idle_hook);
938 while ((h->flags & DLG_WANT_IDLE) && is_idle ())
939 h->callback (h, NULL, DLG_IDLE, 0, NULL);
941 /* Allow terminating the dialog from the idle handler */
942 if (h->state != DLG_ACTIVE)
943 break;
946 update_cursor (h);
948 /* Clear interrupt flag */
949 tty_got_interrupt ();
950 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
952 dlg_process_event (h, d_key, &event);
954 if (h->state == DLG_CLOSED)
955 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
959 /* Shutdown the run_dlg */
960 void
961 dlg_run_done (Dlg_head * h)
963 if (h->current != NULL)
964 h->callback (h, (Widget *) h->current->data, DLG_END, 0, NULL);
966 current_dlg = g_list_remove (current_dlg, h);
969 /* Standard run dialog routine
970 * We have to keep this routine small so that we can duplicate it's
971 * behavior on complex routines like the file routines, this way,
972 * they can call the dlg_process_event without rewriting all the code
975 run_dlg (Dlg_head * h)
977 init_dlg (h);
978 frontend_run_dlg (h);
979 dlg_run_done (h);
980 return h->ret_value;
983 void
984 destroy_dlg (Dlg_head * h)
986 dlg_broadcast_msg (h, WIDGET_DESTROY, FALSE);
987 g_list_foreach (h->widgets, (GFunc) g_free, NULL);
988 g_list_free (h->widgets);
989 g_free (h->color);
990 g_free (h->title);
991 g_free (h);
993 do_refresh ();
996 /* Replace widget old_w for widget new_w in the dialog */
997 void
998 dlg_replace_widget (Widget * old_w, Widget * new_w)
1000 Dlg_head *h = old_w->parent;
1001 gboolean should_focus = FALSE;
1003 if (h->widgets == NULL)
1004 return;
1006 if (h->current == NULL)
1007 h->current = h->widgets;
1009 if (old_w == h->current->data)
1010 should_focus = TRUE;
1012 new_w->parent = h;
1013 new_w->dlg_id = old_w->dlg_id;
1015 if (should_focus)
1016 h->current->data = new_w;
1017 else
1018 g_list_find (h->widgets, old_w)->data = new_w;
1020 send_message (old_w, WIDGET_DESTROY, 0);
1021 send_message (new_w, WIDGET_INIT, 0);
1023 if (should_focus)
1024 dlg_select_widget (new_w);
1026 send_message (new_w, WIDGET_DRAW, 0);