Ticket #2170: Color collisions
[midnight-commander.git] / src / dialog.c
bloba824ac23c01f4c4c23c10ce6ec7d39cd5bfe84cb
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 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 *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 (Dlg_head *h)
111 int space;
113 if (h->state != DLG_ACTIVE)
114 return;
116 space = (h->flags & DLG_COMPACT) ? 0 : 1;
118 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
119 dlg_erase (h);
120 draw_box (h, space, space, h->lines - 2 * space, h->cols - 2 * space, FALSE);
122 if (h->title != NULL)
124 tty_setcolor (h->color[DLG_COLOR_TITLE]);
125 dlg_move (h, space, (h->cols - str_term_width1 (h->title)) / 2);
126 tty_print_string (h->title);
130 /* this function allows to set dialog position */
131 void
132 dlg_set_position (Dlg_head * h, int y1, int x1, int y2, int x2)
134 /* save old positions, will be used to reposition childs */
135 int ox, oy, oc, ol;
136 int shift_x, shift_y, scale_x, scale_y;
138 /* save old positions, will be used to reposition childs */
139 ox = h->x;
140 oy = h->y;
141 oc = h->cols;
142 ol = h->lines;
144 h->x = x1;
145 h->y = y1;
146 h->lines = y2 - y1;
147 h->cols = x2 - x1;
149 /* dialog is empty */
150 if (h->widgets == NULL)
151 return;
153 if (h->current == NULL)
154 h->current = h->widgets;
156 /* values by which controls should be moved */
157 shift_x = h->x - ox;
158 shift_y = h->y - oy;
159 scale_x = h->cols - oc;
160 scale_y = h->lines - ol;
162 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
164 GList *w;
166 for (w = h->widgets; w != NULL; w = g_list_next (w))
168 /* there are, mainly, 2 generally possible
169 situations:
171 1. control sticks to one side - it
172 should be moved
174 2. control sticks to two sides of
175 one direction - it should be sized */
177 Widget *c = (Widget *) w->data;
178 int x = c->x;
179 int y = c->y;
180 int cols = c->cols;
181 int lines = c->lines;
183 if ((c->pos_flags & WPOS_KEEP_LEFT) && (c->pos_flags & WPOS_KEEP_RIGHT))
185 x += shift_x;
186 cols += scale_x;
188 else if (c->pos_flags & WPOS_KEEP_LEFT)
189 x += shift_x;
190 else if (c->pos_flags & WPOS_KEEP_RIGHT)
191 x += shift_x + scale_x;
193 if ((c->pos_flags & WPOS_KEEP_TOP) && (c->pos_flags & WPOS_KEEP_BOTTOM))
195 y += shift_y;
196 lines += scale_y;
198 else if (c->pos_flags & WPOS_KEEP_TOP)
199 y += shift_y;
200 else if (c->pos_flags & WPOS_KEEP_BOTTOM)
201 y += shift_y + scale_y;
203 widget_set_size (c, y, x, lines, cols);
208 /* this function sets only size, leaving positioning to automatic methods */
209 void
210 dlg_set_size (Dlg_head * h, int lines, int cols)
212 int x = h->x;
213 int y = h->y;
215 if (h->flags & DLG_CENTER)
217 y = (LINES - lines) / 2;
218 x = (COLS - cols) / 2;
221 if ((h->flags & DLG_TRYUP) && (y > 3))
222 y -= 2;
224 dlg_set_position (h, y, x, y + lines, x + cols);
227 /* Default dialog callback */
228 cb_ret_t
229 default_dlg_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
231 (void) sender;
232 (void) parm;
233 (void) data;
235 switch (msg)
237 case DLG_DRAW:
238 if (h->color != NULL)
240 common_dialog_repaint (h);
241 return MSG_HANDLED;
243 return MSG_NOT_HANDLED;
245 case DLG_IDLE:
246 dlg_broadcast_msg_to (h, WIDGET_IDLE, FALSE, W_WANT_IDLE);
247 return MSG_HANDLED;
249 case DLG_RESIZE:
250 /* this is default resizing mechanism */
251 /* the main idea of this code is to resize dialog
252 according to flags (if any of flags require automatic
253 resizing, like DLG_CENTER, end after that reposition
254 controls in dialog according to flags of widget) */
255 dlg_set_size (h, h->lines, h->cols);
256 return MSG_HANDLED;
258 default:
259 break;
262 return MSG_NOT_HANDLED;
265 Dlg_head *
266 create_dlg (gboolean modal, int y1, int x1, int lines, int cols,
267 const int *colors, dlg_cb_fn callback, const char *help_ctx,
268 const char *title, dlg_flags_t flags)
270 Dlg_head *new_d;
272 new_d = g_new0 (Dlg_head, 1);
273 new_d->modal = modal;
274 if (colors != NULL)
276 memmove (new_d->color, colors, sizeof (dlg_colors_t));
278 new_d->help_ctx = help_ctx;
279 new_d->callback = (callback != NULL) ? callback : default_dlg_callback;
280 new_d->x = x1;
281 new_d->y = y1;
282 new_d->flags = flags;
283 new_d->data = NULL;
285 dlg_set_size (new_d, lines, cols);
286 new_d->fullscreen = (new_d->x == 0 && new_d->y == 0
287 && new_d->cols == COLS && new_d->lines == LINES);
289 new_d->mouse_status = MOU_NORMAL;
291 /* Strip existing spaces, add one space before and after the title */
292 if (title != NULL)
294 char *t;
296 t = g_strstrip (g_strdup (title));
297 if (*t != '\0')
298 new_d->title = g_strdup_printf (" %s ", t);
299 g_free (t);
302 return new_d;
305 void
306 dlg_set_default_colors (void)
308 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
309 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
310 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
311 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
312 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
314 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
315 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
316 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
317 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
318 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
321 void
322 dlg_erase (Dlg_head * h)
324 if ((h != NULL) && (h->state == DLG_ACTIVE))
325 tty_fill_region (h->y, h->x, h->lines, h->cols, ' ');
328 void
329 set_idle_proc (Dlg_head * d, int enable)
331 if (enable)
332 d->flags |= DLG_WANT_IDLE;
333 else
334 d->flags &= ~DLG_WANT_IDLE;
338 * Insert widget to dialog before current widget. For dialogs populated
339 * from the bottom, make the widget current. Return widget number.
342 add_widget_autopos (Dlg_head * h, void *w, widget_pos_flags_t pos_flags)
344 Widget *widget = (Widget *) w;
346 /* Don't accept 0 widgets */
347 if (w == NULL)
348 abort ();
350 widget->x += h->x;
351 widget->y += h->y;
352 widget->owner = h;
353 widget->pos_flags = pos_flags;
354 widget->id = g_list_length (h->widgets);
356 if ((h->flags & DLG_REVERSE) != 0)
357 h->widgets = g_list_prepend (h->widgets, widget);
358 else
359 h->widgets = g_list_append (h->widgets, widget);
361 h->current = h->widgets;
363 return widget->id;
366 /* wrapper to simply add lefttop positioned controls */
368 add_widget (Dlg_head * h, void *w)
370 return add_widget_autopos (h, w, WPOS_KEEP_LEFT | WPOS_KEEP_TOP);
373 void
374 do_refresh (void)
376 GList *d = top_dlg;
378 if (fast_refresh)
380 if ((d != NULL) && (d->data != NULL))
381 dlg_redraw ((Dlg_head *) d->data);
383 else
385 /* Search first fullscreen dialog */
386 for (; d != NULL; d = g_list_next (d))
387 if ((d->data != NULL) && ((Dlg_head *) d->data)->fullscreen)
388 break;
389 /* back to top dialog */
390 for (; d != NULL; d = g_list_previous (d))
391 if (d->data != NULL)
392 dlg_redraw ((Dlg_head *) d->data);
396 /* broadcast a message to all the widgets in a dialog that have
397 * the options set to flags. If flags is zero, the message is sent
398 * to all widgets.
400 static void
401 dlg_broadcast_msg_to (Dlg_head * h, widget_msg_t message, gboolean reverse, int flags)
403 GList *p, *first;
405 if (h->widgets == NULL)
406 return;
408 if (h->current == NULL)
409 h->current = h->widgets;
411 if (reverse)
413 p = g_list_previous (h->current);
415 if (p == NULL)
416 p = g_list_last (h->widgets);
418 else
420 p = g_list_next (h->current);
422 if (p == NULL)
423 p = h->widgets;
426 first = p;
430 Widget *w = (Widget *) p->data;
432 if (reverse)
434 p = g_list_previous (p);
436 if (p == NULL)
437 p = g_list_last (h->widgets);
439 else
441 p = g_list_next (p);
443 if (p == NULL)
444 p = h->widgets;
447 if ((flags == 0) || ((flags & w->options) != 0))
448 send_message (w, message, 0);
450 while (first != p);
453 /* broadcast a message to all the widgets in a dialog */
454 void
455 dlg_broadcast_msg (Dlg_head * h, widget_msg_t message, gboolean reverse)
457 dlg_broadcast_msg_to (h, message, reverse, 0);
461 dlg_focus (Dlg_head * h)
463 /* cannot focus disabled widget ... */
465 if ((h->current != NULL) && (h->state == DLG_ACTIVE))
467 Widget *current = (Widget *) h->current->data;
469 if (((current->options & W_DISABLED) == 0)
470 && (send_message (current, WIDGET_FOCUS, 0) == MSG_HANDLED))
472 h->callback (h, current, DLG_FOCUS, 0, NULL);
473 return 1;
477 return 0;
480 static int
481 dlg_unfocus (Dlg_head * h)
483 /* ... but can unfocus disabled widget */
485 if ((h->current != NULL) && (h->state == DLG_ACTIVE))
487 Widget *current = (Widget *) h->current->data;
489 if (send_message (current, WIDGET_UNFOCUS, 0) == MSG_HANDLED)
491 h->callback (h, current, DLG_UNFOCUS, 0, NULL);
492 return 1;
496 return 0;
499 /* Return true if the windows overlap */
501 dlg_overlap (Widget * a, Widget * b)
503 return !((b->x >= a->x + a->cols)
504 || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
507 static int
508 dlg_find_widget_callback (const void *a, const void *b)
510 const Widget *w = (const Widget *) a;
511 callback_fn f = (callback_fn) b;
513 return (w->callback == f) ? 0 : 1;
516 /* Find the widget with the given callback in the dialog h */
517 Widget *
518 find_widget_type (const Dlg_head * h, callback_fn callback)
520 GList *w;
522 w = g_list_find_custom (h->widgets, callback, dlg_find_widget_callback);
524 return (w == NULL) ? NULL : (Widget *) w->data;
527 /* Find the widget with the given id */
528 Widget *
529 dlg_find_by_id (const Dlg_head * h, unsigned int id)
531 if (h->widgets != NULL)
533 GList *w;
535 for (w = h->widgets; w != NULL; w = g_list_next (w))
536 if (((Widget *) w->data)->id == id)
537 return (Widget *) w->data;
540 return NULL;
543 /* Find the widget with the given id in the dialog h and select it */
544 void
545 dlg_select_by_id (const Dlg_head * h, unsigned int id)
547 Widget *w;
549 w = dlg_find_by_id (h, id);
550 if (w != NULL)
551 dlg_select_widget (w);
554 /* What to do if the requested widget doesn't take focus */
555 typedef enum
557 SELECT_NEXT, /* go the the next widget */
558 SELECT_PREV, /* go the the previous widget */
559 SELECT_EXACT /* use current widget */
560 } select_dir_t;
563 * Try to select another widget. If forward is set, follow tab order.
564 * Otherwise go to the previous widget.
566 static void
567 do_select_widget (Dlg_head * h, GList * w, select_dir_t dir)
569 Widget *w0 = (Widget *) h->current->data;
571 if (!dlg_unfocus (h))
572 return;
574 h->current = w;
578 if (dlg_focus (h))
579 break;
581 switch (dir)
583 case SELECT_NEXT:
584 h->current = g_list_next (h->current);
585 if (h->current == NULL)
586 h->current = h->widgets;
587 break;
588 case SELECT_PREV:
589 h->current = g_list_previous (h->current);
590 if (h->current == NULL)
591 h->current = g_list_last (h->widgets);
592 break;
593 case SELECT_EXACT:
594 h->current = g_list_find (h->widgets, w0);
595 dlg_focus (h);
596 return;
599 while (h->current != w /* && (((Widget *) h->current->data)->options & W_DISABLED) == 0 */);
601 if (dlg_overlap (w0, (Widget *) h->current->data))
603 send_message ((Widget *) h->current->data, WIDGET_DRAW, 0);
604 send_message ((Widget *) h->current->data, WIDGET_FOCUS, 0);
609 * Try to select widget in the dialog.
611 void
612 dlg_select_widget (void *w)
614 const Widget *widget = (Widget *) w;
615 Dlg_head *h = widget->owner;
617 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_NEXT);
620 /* Try to select previous widget in the tab order */
621 void
622 dlg_one_up (Dlg_head * h)
624 if (h->widgets != NULL)
626 GList *prev;
628 prev = g_list_previous (h->current);
629 if (prev == NULL)
630 prev = g_list_last (h->widgets);
632 do_select_widget (h, prev, SELECT_PREV);
636 /* Try to select next widget in the tab order */
637 void
638 dlg_one_down (Dlg_head * h)
640 if (h->widgets != NULL)
642 GList *next;
644 next = g_list_next (h->current);
645 if (next == NULL)
646 next = h->widgets;
647 do_select_widget (h, next, SELECT_NEXT);
651 void
652 update_cursor (Dlg_head * h)
654 GList *p = h->current;
656 if ((p != NULL) && (h->state == DLG_ACTIVE))
658 Widget *w;
660 w = (Widget *) p->data;
662 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
663 send_message (w, WIDGET_CURSOR, 0);
664 else
667 p = g_list_next (p);
668 if (p == NULL)
669 p = h->widgets;
671 if (p == h->current)
672 break;
674 w = (Widget *) p->data;
676 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
677 if (send_message (w, WIDGET_CURSOR, 0) == MSG_HANDLED)
678 break;
680 while (TRUE);
684 /* Redraw the widgets in reverse order, leaving the current widget
685 * as the last one
687 void
688 dlg_redraw (Dlg_head * h)
690 if (h->state != DLG_ACTIVE)
691 return;
693 if (h->winch_pending)
695 h->winch_pending = FALSE;
696 h->callback (h, NULL, DLG_RESIZE, 0, NULL);
699 h->callback (h, NULL, DLG_DRAW, 0, NULL);
700 dlg_broadcast_msg (h, WIDGET_DRAW, TRUE);
701 update_cursor (h);
704 void
705 dlg_stop (Dlg_head * h)
707 h->state = DLG_CLOSED;
710 static void
711 refresh_cmd (void)
713 #ifdef HAVE_SLANG
714 tty_touch_screen ();
715 mc_refresh ();
716 #else
717 /* Use this if the refreshes fail */
718 clr_scr ();
719 repaint_screen ();
720 #endif /* HAVE_SLANG */
723 static cb_ret_t
724 dlg_execute_cmd (Dlg_head * h, unsigned long command)
726 cb_ret_t ret = MSG_HANDLED;
727 switch (command)
729 case CK_DialogOK:
730 h->ret_value = B_ENTER;
731 dlg_stop (h);
732 break;
733 case CK_DialogCancel:
734 h->ret_value = B_CANCEL;
735 dlg_stop (h);
736 break;
738 case CK_DialogPrevItem:
739 dlg_one_up (h);
740 break;
741 case CK_DialogNextItem:
742 dlg_one_down (h);
743 break;
745 case CK_DialogHelp:
746 interactive_display (NULL, h->help_ctx);
747 do_refresh ();
748 break;
750 case CK_DialogSuspend:
751 suspend_cmd ();
752 refresh_cmd ();
753 break;
754 case CK_DialogRefresh:
755 refresh_cmd ();
756 break;
758 case CK_DialogListCmd:
759 if (!h->modal)
760 dialog_switch_list ();
761 else
762 ret = MSG_NOT_HANDLED;
763 break;
764 case CK_DialogNextCmd:
765 if (!h->modal)
766 dialog_switch_next ();
767 else
768 ret = MSG_NOT_HANDLED;
769 break;
770 case CK_DialogPrevCmd:
771 if (!h->modal)
772 dialog_switch_prev ();
773 else
774 ret = MSG_NOT_HANDLED;
775 break;
777 default:
778 ret = MSG_NOT_HANDLED;
781 return ret;
784 static cb_ret_t
785 dlg_handle_key (Dlg_head * h, int d_key)
787 unsigned long command;
788 command = lookup_keymap_command (dialog_map, d_key);
789 if ((command == CK_Ignore_Key) || (dlg_execute_cmd (h, command) == MSG_NOT_HANDLED))
790 return MSG_NOT_HANDLED;
791 else
792 return MSG_HANDLED;
795 static int
796 dlg_mouse_event (Dlg_head * h, Gpm_Event * event)
798 GList *item;
799 GList *starting_widget = h->current;
800 Gpm_Event new_event;
801 int x = event->x;
802 int y = event->y;
804 /* close the dialog by mouse click out of dialog area */
805 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0) && ((event->type & GPM_DOWN) != 0) /* left click */
806 && !((x > h->x) && (x <= h->x + h->cols) && (y > h->y) && (y <= h->y + h->lines)))
808 h->ret_value = B_CANCEL;
809 dlg_stop (h);
810 return MOU_NORMAL;
813 item = starting_widget;
816 Widget *widget;
818 widget = (Widget *) item->data;
819 item = g_list_next (item);
820 if (item == NULL)
821 item = h->widgets;
823 if (((widget->options & W_DISABLED) == 0)
824 && (x > widget->x) && (x <= widget->x + widget->cols)
825 && (y > widget->y) && (y <= widget->y + widget->lines))
827 new_event = *event;
828 new_event.x -= widget->x;
829 new_event.y -= widget->y;
831 if (widget->mouse != NULL)
832 return widget->mouse (&new_event, widget);
835 while (item != starting_widget);
837 return MOU_NORMAL;
840 static cb_ret_t
841 dlg_try_hotkey (Dlg_head * h, int d_key)
843 GList *hot_cur;
844 Widget *current;
845 cb_ret_t handled;
846 int c;
848 if (h->widgets == NULL)
849 return MSG_NOT_HANDLED;
851 if (h->current == NULL)
852 h->current = h->widgets;
855 * Explanation: we don't send letter hotkeys to other widgets if
856 * the currently selected widget is an input line
859 current = (Widget *) h->current->data;
861 if ((current->options & W_DISABLED) != 0)
862 return MSG_NOT_HANDLED;
864 if (current->options & W_IS_INPUT)
866 /* skip ascii control characters, anything else can valid character in
867 * some encoding */
868 if (d_key >= 32 && d_key < 256)
869 return MSG_NOT_HANDLED;
872 /* If it's an alt key, send the message */
873 c = d_key & ~ALT (0);
874 if (d_key & ALT (0) && g_ascii_isalpha (c))
875 d_key = g_ascii_tolower (c);
877 handled = MSG_NOT_HANDLED;
878 if ((current->options & W_WANT_HOTKEY) != 0)
879 handled = send_message (current, WIDGET_HOTKEY, d_key);
881 /* If not used, send hotkey to other widgets */
882 if (handled == MSG_HANDLED)
883 return MSG_HANDLED;
885 hot_cur = g_list_next (h->current);
886 if (hot_cur == NULL)
887 hot_cur = h->widgets;
889 /* send it to all widgets */
890 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
892 current = (Widget *) hot_cur->data;
894 if ((current->options & W_WANT_HOTKEY) != 0)
895 handled = send_message (current, WIDGET_HOTKEY, d_key);
897 if (handled == MSG_NOT_HANDLED)
899 hot_cur = g_list_next (hot_cur);
900 if (hot_cur == NULL)
901 hot_cur = h->widgets;
905 if (handled == MSG_HANDLED)
906 do_select_widget (h, hot_cur, SELECT_EXACT);
908 return handled;
911 static void
912 dlg_key_event (Dlg_head * h, int d_key)
914 cb_ret_t handled;
916 if (h->widgets == NULL)
917 return;
919 if (h->current == NULL)
920 h->current = h->widgets;
922 /* TAB used to cycle */
923 if ((h->flags & DLG_WANT_TAB) == 0)
925 if (d_key == '\t')
927 dlg_one_down (h);
928 return;
930 else if (d_key == KEY_BTAB)
932 dlg_one_up (h);
933 return;
937 /* first can dlg_callback handle the key */
938 handled = h->callback (h, NULL, DLG_KEY, d_key, NULL);
940 /* next try the hotkey */
941 if (handled == MSG_NOT_HANDLED)
942 handled = dlg_try_hotkey (h, d_key);
944 if (handled == MSG_HANDLED)
945 h->callback (h, NULL, DLG_HOTKEY_HANDLED, 0, NULL);
946 else
947 /* not used - then try widget_callback */
948 handled = send_message ((Widget *) h->current->data, WIDGET_KEY, d_key);
950 /* not used- try to use the unhandled case */
951 if (handled == MSG_NOT_HANDLED)
952 handled = h->callback (h, NULL, DLG_UNHANDLED_KEY, d_key, NULL);
954 if (handled == MSG_NOT_HANDLED)
955 handled = dlg_handle_key (h, d_key);
957 h->callback (h, NULL, DLG_POST_KEY, d_key, NULL);
960 /* Init the process */
961 void
962 init_dlg (Dlg_head * h)
964 if ((top_dlg != NULL) && ((Dlg_head *) top_dlg->data)->modal)
965 h->modal = TRUE;
968 /* add dialog to the stack */
969 top_dlg = g_list_prepend (top_dlg, h);
971 /* Initialize dialog manager and widgets */
972 if (h->state == DLG_ACTIVE)
974 if (!h->modal)
975 dialog_switch_add (h);
977 h->callback (h, NULL, DLG_INIT, 0, NULL);
978 dlg_broadcast_msg (h, WIDGET_INIT, FALSE);
981 h->state = DLG_ACTIVE;
983 dlg_redraw (h);
985 /* Select the first widget that takes focus */
986 while (h->current != NULL && !dlg_focus (h))
988 h->current = g_list_next (h->current);
989 if (h->current == NULL)
990 h->current = h->widgets;
993 h->ret_value = 0;
996 void
997 dlg_process_event (Dlg_head * h, int key, Gpm_Event * event)
999 if (key == EV_NONE)
1001 if (tty_got_interrupt ())
1002 dlg_execute_cmd (h, CK_DialogCancel);
1004 return;
1007 if (key == EV_MOUSE)
1008 h->mouse_status = dlg_mouse_event (h, event);
1009 else
1010 dlg_key_event (h, key);
1013 static void
1014 frontend_run_dlg (Dlg_head * h)
1016 int d_key;
1017 Gpm_Event event;
1019 event.x = -1;
1021 /* close opened editors, viewers, etc */
1022 if (!h->modal && midnight_shutdown)
1024 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
1025 return;
1028 while (h->state == DLG_ACTIVE)
1030 if (winch_flag)
1031 change_screen_size ();
1033 if (is_idle ())
1035 if (idle_hook)
1036 execute_hooks (idle_hook);
1038 while ((h->flags & DLG_WANT_IDLE) && is_idle ())
1039 h->callback (h, NULL, DLG_IDLE, 0, NULL);
1041 /* Allow terminating the dialog from the idle handler */
1042 if (h->state != DLG_ACTIVE)
1043 break;
1046 update_cursor (h);
1048 /* Clear interrupt flag */
1049 tty_got_interrupt ();
1050 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
1052 dlg_process_event (h, d_key, &event);
1054 if (h->state == DLG_CLOSED)
1055 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
1059 /* Shutdown the run_dlg */
1060 void
1061 dlg_run_done (Dlg_head * h)
1063 top_dlg = g_list_remove (top_dlg, h);
1065 if (h->state == DLG_CLOSED)
1067 h->callback (h, (Widget *) h->current->data, DLG_END, 0, NULL);
1068 if (!h->modal)
1069 dialog_switch_remove (h);
1074 /* Standard run dialog routine
1075 * We have to keep this routine small so that we can duplicate it's
1076 * behavior on complex routines like the file routines, this way,
1077 * they can call the dlg_process_event without rewriting all the code
1080 run_dlg (Dlg_head * h)
1082 init_dlg (h);
1083 frontend_run_dlg (h);
1084 dlg_run_done (h);
1085 return h->ret_value;
1088 void
1089 destroy_dlg (Dlg_head * h)
1091 dlg_broadcast_msg (h, WIDGET_DESTROY, FALSE);
1092 g_list_foreach (h->widgets, (GFunc) g_free, NULL);
1093 g_list_free (h->widgets);
1094 g_free (h->title);
1095 g_free (h);
1097 do_refresh ();
1100 char *
1101 dlg_get_title (const Dlg_head *h, size_t len)
1103 char *t;
1105 if (h == NULL)
1106 abort ();
1108 if (h->get_title != NULL)
1109 t = h->get_title (h, len);
1110 else
1111 t = g_strdup ("");
1113 return t;
1116 /* Replace widget old_w for widget new_w in the dialog */
1117 void
1118 dlg_replace_widget (Widget * old_w, Widget * new_w)
1120 Dlg_head *h = old_w->owner;
1121 gboolean should_focus = FALSE;
1123 if (h->widgets == NULL)
1124 return;
1126 if (h->current == NULL)
1127 h->current = h->widgets;
1129 if (old_w == h->current->data)
1130 should_focus = TRUE;
1132 new_w->owner = h;
1133 new_w->id = old_w->id;
1135 if (should_focus)
1136 h->current->data = new_w;
1137 else
1138 g_list_find (h->widgets, old_w)->data = new_w;
1140 send_message (old_w, WIDGET_DESTROY, 0);
1141 send_message (new_w, WIDGET_INIT, 0);
1143 if (should_focus)
1144 dlg_select_widget (new_w);
1146 send_message (new_w, WIDGET_DRAW, 0);