Get rid of some function forward declarations.
[midnight-commander.git] / lib / widget / dialog.c
blob026c036547db94d103edbcc4bd29476da6b9c477
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"
39 #include "lib/widget.h"
41 /* TODO: these includes should be removed! */
42 #include "src/help.h" /* interactive_display() */
43 #include "src/filemanager/layout.h"
44 #include "src/execute.h" /* suspend_cmd() */
45 #include "src/keybind-defaults.h"
47 /*** global variables ****************************************************************************/
49 /* Color styles for normal and error dialogs */
50 dlg_colors_t dialog_colors;
51 dlg_colors_t alarm_colors;
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 *top_dlg = NULL;
57 /* A hook list for idle events */
58 hook_t *idle_hook = NULL;
60 /* If set then dialogs just clean the screen when refreshing, else */
61 /* they do a complete refresh, refreshing all the parts of the program */
62 int fast_refresh = 0;
64 /* left click outside of dialog closes it */
65 int mouse_close_dialog = 0;
67 /*** file scope macro definitions ****************************************************************/
69 /*** file scope type declarations ****************************************************************/
71 /** What to do if the requested widget doesn't take focus */
72 typedef enum
74 SELECT_NEXT, /* go the the next widget */
75 SELECT_PREV, /* go the the previous widget */
76 SELECT_EXACT /* use current widget */
77 } select_dir_t;
79 /*** file scope variables ************************************************************************/
81 /*** file scope functions ************************************************************************/
83 /* --------------------------------------------------------------------------------------------- */
84 /**
85 * broadcast a message to all the widgets in a dialog that have
86 * the options set to flags. If flags is zero, the message is sent
87 * to all widgets.
90 static void
91 dlg_broadcast_msg_to (Dlg_head * h, widget_msg_t msg, gboolean reverse, int flags)
93 GList *p, *first;
95 if (h->widgets == NULL)
96 return;
98 if (h->current == NULL)
99 h->current = h->widgets;
101 if (reverse)
103 p = g_list_previous (h->current);
105 if (p == NULL)
106 p = g_list_last (h->widgets);
108 else
110 p = g_list_next (h->current);
112 if (p == NULL)
113 p = h->widgets;
116 first = p;
120 Widget *w = (Widget *) p->data;
122 if (reverse)
124 p = g_list_previous (p);
126 if (p == NULL)
127 p = g_list_last (h->widgets);
129 else
131 p = g_list_next (p);
133 if (p == NULL)
134 p = h->widgets;
137 if ((flags == 0) || ((flags & w->options) != 0))
138 send_message (w, msg, 0);
140 while (first != p);
143 /* --------------------------------------------------------------------------------------------- */
145 static int
146 dlg_unfocus (Dlg_head * h)
148 /* ... but can unfocus disabled widget */
150 if ((h->current != NULL) && (h->state == DLG_ACTIVE))
152 Widget *current = (Widget *) h->current->data;
154 if (send_message (current, WIDGET_UNFOCUS, 0) == MSG_HANDLED)
156 h->callback (h, current, DLG_UNFOCUS, 0, NULL);
157 return 1;
161 return 0;
164 /* --------------------------------------------------------------------------------------------- */
166 static int
167 dlg_find_widget_callback (const void *a, const void *b)
169 const Widget *w = (const Widget *) a;
170 callback_fn f = (callback_fn) b;
172 return (w->callback == f) ? 0 : 1;
175 /* --------------------------------------------------------------------------------------------- */
177 * Try to select another widget. If forward is set, follow tab order.
178 * Otherwise go to the previous widget.
181 static void
182 do_select_widget (Dlg_head * h, GList * w, select_dir_t dir)
184 Widget *w0 = (Widget *) h->current->data;
186 if (!dlg_unfocus (h))
187 return;
189 h->current = w;
193 if (dlg_focus (h))
194 break;
196 switch (dir)
198 case SELECT_NEXT:
199 h->current = g_list_next (h->current);
200 if (h->current == NULL)
201 h->current = h->widgets;
202 break;
203 case SELECT_PREV:
204 h->current = g_list_previous (h->current);
205 if (h->current == NULL)
206 h->current = g_list_last (h->widgets);
207 break;
208 case SELECT_EXACT:
209 h->current = g_list_find (h->widgets, w0);
210 dlg_focus (h);
211 return;
214 while (h->current != w /* && (((Widget *) h->current->data)->options & W_DISABLED) == 0 */ );
216 if (dlg_overlap (w0, (Widget *) h->current->data))
218 send_message ((Widget *) h->current->data, WIDGET_DRAW, 0);
219 send_message ((Widget *) h->current->data, WIDGET_FOCUS, 0);
223 /* --------------------------------------------------------------------------------------------- */
225 static void
226 refresh_cmd (void)
228 #ifdef HAVE_SLANG
229 tty_touch_screen ();
230 mc_refresh ();
231 #else
232 /* Use this if the refreshes fail */
233 clr_scr ();
234 repaint_screen ();
235 #endif /* HAVE_SLANG */
238 /* --------------------------------------------------------------------------------------------- */
240 static cb_ret_t
241 dlg_execute_cmd (Dlg_head * h, unsigned long command)
243 cb_ret_t ret = MSG_HANDLED;
244 switch (command)
246 case CK_DialogOK:
247 h->ret_value = B_ENTER;
248 dlg_stop (h);
249 break;
250 case CK_DialogCancel:
251 h->ret_value = B_CANCEL;
252 dlg_stop (h);
253 break;
255 case CK_DialogPrevItem:
256 dlg_one_up (h);
257 break;
258 case CK_DialogNextItem:
259 dlg_one_down (h);
260 break;
262 case CK_DialogHelp:
263 interactive_display (NULL, h->help_ctx);
264 do_refresh ();
265 break;
267 case CK_DialogSuspend:
268 suspend_cmd ();
269 refresh_cmd ();
270 break;
271 case CK_DialogRefresh:
272 refresh_cmd ();
273 break;
275 case CK_DialogListCmd:
276 if (!h->modal)
277 dialog_switch_list ();
278 else
279 ret = MSG_NOT_HANDLED;
280 break;
281 case CK_DialogNextCmd:
282 if (!h->modal)
283 dialog_switch_next ();
284 else
285 ret = MSG_NOT_HANDLED;
286 break;
287 case CK_DialogPrevCmd:
288 if (!h->modal)
289 dialog_switch_prev ();
290 else
291 ret = MSG_NOT_HANDLED;
292 break;
294 default:
295 ret = MSG_NOT_HANDLED;
298 return ret;
301 /* --------------------------------------------------------------------------------------------- */
303 static cb_ret_t
304 dlg_handle_key (Dlg_head * h, int d_key)
306 unsigned long command;
307 command = keybind_lookup_keymap_command (dialog_map, d_key);
308 if ((command == CK_Ignore_Key) || (dlg_execute_cmd (h, command) == MSG_NOT_HANDLED))
309 return MSG_NOT_HANDLED;
310 else
311 return MSG_HANDLED;
314 /* --------------------------------------------------------------------------------------------- */
316 static int
317 dlg_mouse_event (Dlg_head * h, Gpm_Event * event)
319 GList *item;
320 GList *starting_widget = h->current;
321 Gpm_Event new_event;
322 int x = event->x;
323 int y = event->y;
325 /* close the dialog by mouse click out of dialog area */
326 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0) && ((event->type & GPM_DOWN) != 0) /* left click */
327 && !((x > h->x) && (x <= h->x + h->cols) && (y > h->y) && (y <= h->y + h->lines)))
329 h->ret_value = B_CANCEL;
330 dlg_stop (h);
331 return MOU_NORMAL;
334 item = starting_widget;
337 Widget *widget;
339 widget = (Widget *) item->data;
340 item = g_list_next (item);
341 if (item == NULL)
342 item = h->widgets;
344 if (((widget->options & W_DISABLED) == 0)
345 && (x > widget->x) && (x <= widget->x + widget->cols)
346 && (y > widget->y) && (y <= widget->y + widget->lines))
348 new_event = *event;
349 new_event.x -= widget->x;
350 new_event.y -= widget->y;
352 if (widget->mouse != NULL)
353 return widget->mouse (&new_event, widget);
356 while (item != starting_widget);
358 return MOU_NORMAL;
361 /* --------------------------------------------------------------------------------------------- */
363 static cb_ret_t
364 dlg_try_hotkey (Dlg_head * h, int d_key)
366 GList *hot_cur;
367 Widget *current;
368 cb_ret_t handled;
369 int c;
371 if (h->widgets == NULL)
372 return MSG_NOT_HANDLED;
374 if (h->current == NULL)
375 h->current = h->widgets;
378 * Explanation: we don't send letter hotkeys to other widgets if
379 * the currently selected widget is an input line
382 current = (Widget *) h->current->data;
384 if ((current->options & W_DISABLED) != 0)
385 return MSG_NOT_HANDLED;
387 if (current->options & W_IS_INPUT)
389 /* skip ascii control characters, anything else can valid character in
390 * some encoding */
391 if (d_key >= 32 && d_key < 256)
392 return MSG_NOT_HANDLED;
395 /* If it's an alt key, send the message */
396 c = d_key & ~ALT (0);
397 if (d_key & ALT (0) && g_ascii_isalpha (c))
398 d_key = g_ascii_tolower (c);
400 handled = MSG_NOT_HANDLED;
401 if ((current->options & W_WANT_HOTKEY) != 0)
402 handled = send_message (current, WIDGET_HOTKEY, d_key);
404 /* If not used, send hotkey to other widgets */
405 if (handled == MSG_HANDLED)
406 return MSG_HANDLED;
408 hot_cur = g_list_next (h->current);
409 if (hot_cur == NULL)
410 hot_cur = h->widgets;
412 /* send it to all widgets */
413 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
415 current = (Widget *) hot_cur->data;
417 if ((current->options & W_WANT_HOTKEY) != 0)
418 handled = send_message (current, WIDGET_HOTKEY, d_key);
420 if (handled == MSG_NOT_HANDLED)
422 hot_cur = g_list_next (hot_cur);
423 if (hot_cur == NULL)
424 hot_cur = h->widgets;
428 if (handled == MSG_HANDLED)
429 do_select_widget (h, hot_cur, SELECT_EXACT);
431 return handled;
434 /* --------------------------------------------------------------------------------------------- */
436 static void
437 dlg_key_event (Dlg_head * h, int d_key)
439 cb_ret_t handled;
441 if (h->widgets == NULL)
442 return;
444 if (h->current == NULL)
445 h->current = h->widgets;
447 /* TAB used to cycle */
448 if ((h->flags & DLG_WANT_TAB) == 0)
450 if (d_key == '\t')
452 dlg_one_down (h);
453 return;
455 else if (d_key == KEY_BTAB)
457 dlg_one_up (h);
458 return;
462 /* first can dlg_callback handle the key */
463 handled = h->callback (h, NULL, DLG_KEY, d_key, NULL);
465 /* next try the hotkey */
466 if (handled == MSG_NOT_HANDLED)
467 handled = dlg_try_hotkey (h, d_key);
469 if (handled == MSG_HANDLED)
470 h->callback (h, NULL, DLG_HOTKEY_HANDLED, 0, NULL);
471 else
472 /* not used - then try widget_callback */
473 handled = send_message ((Widget *) h->current->data, WIDGET_KEY, d_key);
475 /* not used- try to use the unhandled case */
476 if (handled == MSG_NOT_HANDLED)
477 handled = h->callback (h, NULL, DLG_UNHANDLED_KEY, d_key, NULL);
479 if (handled == MSG_NOT_HANDLED)
480 handled = dlg_handle_key (h, d_key);
482 h->callback (h, NULL, DLG_POST_KEY, d_key, NULL);
485 /* --------------------------------------------------------------------------------------------- */
487 static void
488 frontend_run_dlg (Dlg_head * h)
490 int d_key;
491 Gpm_Event event;
493 event.x = -1;
495 /* close opened editors, viewers, etc */
496 if (!h->modal && midnight_shutdown)
498 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
499 return;
502 while (h->state == DLG_ACTIVE)
504 if (winch_flag)
505 change_screen_size ();
507 if (is_idle ())
509 if (idle_hook)
510 execute_hooks (idle_hook);
512 while ((h->flags & DLG_WANT_IDLE) && is_idle ())
513 h->callback (h, NULL, DLG_IDLE, 0, NULL);
515 /* Allow terminating the dialog from the idle handler */
516 if (h->state != DLG_ACTIVE)
517 break;
520 update_cursor (h);
522 /* Clear interrupt flag */
523 tty_got_interrupt ();
524 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
526 dlg_process_event (h, d_key, &event);
528 if (h->state == DLG_CLOSED)
529 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
533 /* --------------------------------------------------------------------------------------------- */
534 /*** public functions ****************************************************************************/
535 /* --------------------------------------------------------------------------------------------- */
537 /** draw box in window */
538 void
539 draw_box (Dlg_head * h, int y, int x, int ys, int xs, gboolean single)
541 tty_draw_box (h->y + y, h->x + x, ys, xs, single);
544 /* --------------------------------------------------------------------------------------------- */
546 /** Clean the dialog area, draw the frame and the title */
547 void
548 common_dialog_repaint (Dlg_head * h)
550 int space;
552 if (h->state != DLG_ACTIVE)
553 return;
555 space = (h->flags & DLG_COMPACT) ? 0 : 1;
557 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
558 dlg_erase (h);
559 draw_box (h, space, space, h->lines - 2 * space, h->cols - 2 * space, FALSE);
561 if (h->title != NULL)
563 tty_setcolor (h->color[DLG_COLOR_TITLE]);
564 dlg_move (h, space, (h->cols - str_term_width1 (h->title)) / 2);
565 tty_print_string (h->title);
569 /* --------------------------------------------------------------------------------------------- */
570 /** this function allows to set dialog position */
572 void
573 dlg_set_position (Dlg_head * h, int y1, int x1, int y2, int x2)
575 /* save old positions, will be used to reposition childs */
576 int ox, oy, oc, ol;
577 int shift_x, shift_y, scale_x, scale_y;
579 /* save old positions, will be used to reposition childs */
580 ox = h->x;
581 oy = h->y;
582 oc = h->cols;
583 ol = h->lines;
585 h->x = x1;
586 h->y = y1;
587 h->lines = y2 - y1;
588 h->cols = x2 - x1;
590 /* dialog is empty */
591 if (h->widgets == NULL)
592 return;
594 if (h->current == NULL)
595 h->current = h->widgets;
597 /* values by which controls should be moved */
598 shift_x = h->x - ox;
599 shift_y = h->y - oy;
600 scale_x = h->cols - oc;
601 scale_y = h->lines - ol;
603 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
605 GList *w;
607 for (w = h->widgets; w != NULL; w = g_list_next (w))
609 /* there are, mainly, 2 generally possible
610 situations:
612 1. control sticks to one side - it
613 should be moved
615 2. control sticks to two sides of
616 one direction - it should be sized */
618 Widget *c = (Widget *) w->data;
619 int x = c->x;
620 int y = c->y;
621 int cols = c->cols;
622 int lines = c->lines;
624 if ((c->pos_flags & WPOS_KEEP_LEFT) && (c->pos_flags & WPOS_KEEP_RIGHT))
626 x += shift_x;
627 cols += scale_x;
629 else if (c->pos_flags & WPOS_KEEP_LEFT)
630 x += shift_x;
631 else if (c->pos_flags & WPOS_KEEP_RIGHT)
632 x += shift_x + scale_x;
634 if ((c->pos_flags & WPOS_KEEP_TOP) && (c->pos_flags & WPOS_KEEP_BOTTOM))
636 y += shift_y;
637 lines += scale_y;
639 else if (c->pos_flags & WPOS_KEEP_TOP)
640 y += shift_y;
641 else if (c->pos_flags & WPOS_KEEP_BOTTOM)
642 y += shift_y + scale_y;
644 widget_set_size (c, y, x, lines, cols);
649 /* --------------------------------------------------------------------------------------------- */
650 /** this function sets only size, leaving positioning to automatic methods */
652 void
653 dlg_set_size (Dlg_head * h, int lines, int cols)
655 int x = h->x;
656 int y = h->y;
658 if (h->flags & DLG_CENTER)
660 y = (LINES - lines) / 2;
661 x = (COLS - cols) / 2;
664 if ((h->flags & DLG_TRYUP) && (y > 3))
665 y -= 2;
667 dlg_set_position (h, y, x, y + lines, x + cols);
670 /* --------------------------------------------------------------------------------------------- */
671 /** Default dialog callback */
673 cb_ret_t
674 default_dlg_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
676 (void) sender;
677 (void) parm;
678 (void) data;
680 switch (msg)
682 case DLG_DRAW:
683 if (h->color != NULL)
685 common_dialog_repaint (h);
686 return MSG_HANDLED;
688 return MSG_NOT_HANDLED;
690 case DLG_IDLE:
691 dlg_broadcast_msg_to (h, WIDGET_IDLE, FALSE, W_WANT_IDLE);
692 return MSG_HANDLED;
694 case DLG_RESIZE:
695 /* this is default resizing mechanism */
696 /* the main idea of this code is to resize dialog
697 according to flags (if any of flags require automatic
698 resizing, like DLG_CENTER, end after that reposition
699 controls in dialog according to flags of widget) */
700 dlg_set_size (h, h->lines, h->cols);
701 return MSG_HANDLED;
703 default:
704 break;
707 return MSG_NOT_HANDLED;
710 /* --------------------------------------------------------------------------------------------- */
712 Dlg_head *
713 create_dlg (gboolean modal, int y1, int x1, int lines, int cols,
714 const int *colors, dlg_cb_fn callback, const char *help_ctx,
715 const char *title, dlg_flags_t flags)
717 Dlg_head *new_d;
719 new_d = g_new0 (Dlg_head, 1);
720 new_d->modal = modal;
721 if (colors != NULL)
723 memmove (new_d->color, colors, sizeof (dlg_colors_t));
725 new_d->help_ctx = help_ctx;
726 new_d->callback = (callback != NULL) ? callback : default_dlg_callback;
727 new_d->x = x1;
728 new_d->y = y1;
729 new_d->flags = flags;
730 new_d->data = NULL;
732 dlg_set_size (new_d, lines, cols);
733 new_d->fullscreen = (new_d->x == 0 && new_d->y == 0
734 && new_d->cols == COLS && new_d->lines == LINES);
736 new_d->mouse_status = MOU_NORMAL;
738 /* Strip existing spaces, add one space before and after the title */
739 if (title != NULL)
741 char *t;
743 t = g_strstrip (g_strdup (title));
744 if (*t != '\0')
745 new_d->title = g_strdup_printf (" %s ", t);
746 g_free (t);
749 return new_d;
752 /* --------------------------------------------------------------------------------------------- */
754 void
755 dlg_set_default_colors (void)
757 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
758 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
759 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
760 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
761 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
763 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
764 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
765 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
766 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
767 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
770 /* --------------------------------------------------------------------------------------------- */
772 void
773 dlg_erase (Dlg_head * h)
775 if ((h != NULL) && (h->state == DLG_ACTIVE))
776 tty_fill_region (h->y, h->x, h->lines, h->cols, ' ');
779 /* --------------------------------------------------------------------------------------------- */
781 void
782 set_idle_proc (Dlg_head * d, int enable)
784 if (enable)
785 d->flags |= DLG_WANT_IDLE;
786 else
787 d->flags &= ~DLG_WANT_IDLE;
790 /* --------------------------------------------------------------------------------------------- */
792 * Insert widget to dialog before current widget. For dialogs populated
793 * from the bottom, make the widget current. Return widget number.
797 add_widget_autopos (Dlg_head * h, void *w, widget_pos_flags_t pos_flags)
799 Widget *widget = (Widget *) w;
801 /* Don't accept 0 widgets */
802 if (w == NULL)
803 abort ();
805 widget->x += h->x;
806 widget->y += h->y;
807 widget->owner = h;
808 widget->pos_flags = pos_flags;
809 widget->id = g_list_length (h->widgets);
811 if ((h->flags & DLG_REVERSE) != 0)
812 h->widgets = g_list_prepend (h->widgets, widget);
813 else
814 h->widgets = g_list_append (h->widgets, widget);
816 h->current = h->widgets;
818 return widget->id;
821 /* --------------------------------------------------------------------------------------------- */
822 /** wrapper to simply add lefttop positioned controls */
825 add_widget (Dlg_head * h, void *w)
827 return add_widget_autopos (h, w, WPOS_KEEP_LEFT | WPOS_KEEP_TOP);
830 /* --------------------------------------------------------------------------------------------- */
832 void
833 do_refresh (void)
835 GList *d = top_dlg;
837 if (fast_refresh)
839 if ((d != NULL) && (d->data != NULL))
840 dlg_redraw ((Dlg_head *) d->data);
842 else
844 /* Search first fullscreen dialog */
845 for (; d != NULL; d = g_list_next (d))
846 if ((d->data != NULL) && ((Dlg_head *) d->data)->fullscreen)
847 break;
848 /* back to top dialog */
849 for (; d != NULL; d = g_list_previous (d))
850 if (d->data != NULL)
851 dlg_redraw ((Dlg_head *) d->data);
855 /* --------------------------------------------------------------------------------------------- */
856 /** broadcast a message to all the widgets in a dialog */
858 void
859 dlg_broadcast_msg (Dlg_head * h, widget_msg_t msg, gboolean reverse)
861 dlg_broadcast_msg_to (h, msg, reverse, 0);
864 /* --------------------------------------------------------------------------------------------- */
867 dlg_focus (Dlg_head * h)
869 /* cannot focus disabled widget ... */
871 if ((h->current != NULL) && (h->state == DLG_ACTIVE))
873 Widget *current = (Widget *) h->current->data;
875 if (((current->options & W_DISABLED) == 0)
876 && (send_message (current, WIDGET_FOCUS, 0) == MSG_HANDLED))
878 h->callback (h, current, DLG_FOCUS, 0, NULL);
879 return 1;
883 return 0;
886 /* --------------------------------------------------------------------------------------------- */
887 /** Return true if the windows overlap */
890 dlg_overlap (Widget * a, Widget * b)
892 return !((b->x >= a->x + a->cols)
893 || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
897 /* --------------------------------------------------------------------------------------------- */
898 /** Find the widget with the given callback in the dialog h */
900 Widget *
901 find_widget_type (const Dlg_head * h, callback_fn callback)
903 GList *w;
905 w = g_list_find_custom (h->widgets, callback, dlg_find_widget_callback);
907 return (w == NULL) ? NULL : (Widget *) w->data;
910 /* --------------------------------------------------------------------------------------------- */
911 /** Find the widget with the given id */
913 Widget *
914 dlg_find_by_id (const Dlg_head * h, unsigned int id)
916 if (h->widgets != NULL)
918 GList *w;
920 for (w = h->widgets; w != NULL; w = g_list_next (w))
921 if (((Widget *) w->data)->id == id)
922 return (Widget *) w->data;
925 return NULL;
928 /* --------------------------------------------------------------------------------------------- */
929 /** Find the widget with the given id in the dialog h and select it */
931 void
932 dlg_select_by_id (const Dlg_head * h, unsigned int id)
934 Widget *w;
936 w = dlg_find_by_id (h, id);
937 if (w != NULL)
938 dlg_select_widget (w);
941 /* --------------------------------------------------------------------------------------------- */
943 * Try to select widget in the dialog.
946 void
947 dlg_select_widget (void *w)
949 const Widget *widget = (Widget *) w;
950 Dlg_head *h = widget->owner;
952 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
955 /* --------------------------------------------------------------------------------------------- */
956 /** Try to select previous widget in the tab order */
958 void
959 dlg_one_up (Dlg_head * h)
961 if (h->widgets != NULL)
963 GList *prev;
965 prev = g_list_previous (h->current);
966 if (prev == NULL)
967 prev = g_list_last (h->widgets);
969 do_select_widget (h, prev, SELECT_PREV);
973 /* --------------------------------------------------------------------------------------------- */
974 /** Try to select next widget in the tab order */
976 void
977 dlg_one_down (Dlg_head * h)
979 if (h->widgets != NULL)
981 GList *next;
983 next = g_list_next (h->current);
984 if (next == NULL)
985 next = h->widgets;
986 do_select_widget (h, next, SELECT_NEXT);
990 /* --------------------------------------------------------------------------------------------- */
992 void
993 update_cursor (Dlg_head * h)
995 GList *p = h->current;
997 if ((p != NULL) && (h->state == DLG_ACTIVE))
999 Widget *w;
1001 w = (Widget *) p->data;
1003 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1004 send_message (w, WIDGET_CURSOR, 0);
1005 else
1008 p = g_list_next (p);
1009 if (p == NULL)
1010 p = h->widgets;
1012 if (p == h->current)
1013 break;
1015 w = (Widget *) p->data;
1017 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1018 if (send_message (w, WIDGET_CURSOR, 0) == MSG_HANDLED)
1019 break;
1021 while (TRUE);
1025 /* --------------------------------------------------------------------------------------------- */
1027 * Redraw the widgets in reverse order, leaving the current widget
1028 * as the last one
1031 void
1032 dlg_redraw (Dlg_head * h)
1034 if (h->state != DLG_ACTIVE)
1035 return;
1037 if (h->winch_pending)
1039 h->winch_pending = FALSE;
1040 h->callback (h, NULL, DLG_RESIZE, 0, NULL);
1043 h->callback (h, NULL, DLG_DRAW, 0, NULL);
1044 dlg_broadcast_msg (h, WIDGET_DRAW, TRUE);
1045 update_cursor (h);
1048 /* --------------------------------------------------------------------------------------------- */
1050 void
1051 dlg_stop (Dlg_head * h)
1053 h->state = DLG_CLOSED;
1056 /* --------------------------------------------------------------------------------------------- */
1057 /** Init the process */
1059 void
1060 init_dlg (Dlg_head * h)
1062 if ((top_dlg != NULL) && ((Dlg_head *) top_dlg->data)->modal)
1063 h->modal = TRUE;
1065 /* add dialog to the stack */
1066 top_dlg = g_list_prepend (top_dlg, h);
1068 /* Initialize dialog manager and widgets */
1069 if (h->state == DLG_ACTIVE)
1071 if (!h->modal)
1072 dialog_switch_add (h);
1074 h->callback (h, NULL, DLG_INIT, 0, NULL);
1075 dlg_broadcast_msg (h, WIDGET_INIT, FALSE);
1078 h->state = DLG_ACTIVE;
1080 dlg_redraw (h);
1082 /* Select the first widget that takes focus */
1083 while (h->current != NULL && !dlg_focus (h))
1085 h->current = g_list_next (h->current);
1086 if (h->current == NULL)
1087 h->current = h->widgets;
1090 h->ret_value = 0;
1093 /* --------------------------------------------------------------------------------------------- */
1095 void
1096 dlg_process_event (Dlg_head * h, int key, Gpm_Event * event)
1098 if (key == EV_NONE)
1100 if (tty_got_interrupt ())
1101 if (h->callback (h, NULL, DLG_ACTION, CK_DialogCancel, NULL) != MSG_HANDLED)
1102 dlg_execute_cmd (h, CK_DialogCancel);
1104 return;
1107 if (key == EV_MOUSE)
1108 h->mouse_status = dlg_mouse_event (h, event);
1109 else
1110 dlg_key_event (h, key);
1113 /* --------------------------------------------------------------------------------------------- */
1114 /** Shutdown the run_dlg */
1116 void
1117 dlg_run_done (Dlg_head * h)
1119 top_dlg = g_list_remove (top_dlg, h);
1121 if (h->state == DLG_CLOSED)
1123 h->callback (h, (Widget *) h->current->data, DLG_END, 0, NULL);
1124 if (!h->modal)
1125 dialog_switch_remove (h);
1130 /* --------------------------------------------------------------------------------------------- */
1132 * Standard run dialog routine
1133 * We have to keep this routine small so that we can duplicate it's
1134 * behavior on complex routines like the file routines, this way,
1135 * they can call the dlg_process_event without rewriting all the code
1139 run_dlg (Dlg_head * h)
1141 init_dlg (h);
1142 frontend_run_dlg (h);
1143 dlg_run_done (h);
1144 return h->ret_value;
1147 /* --------------------------------------------------------------------------------------------- */
1149 void
1150 destroy_dlg (Dlg_head * h)
1152 dlg_broadcast_msg (h, WIDGET_DESTROY, FALSE);
1153 g_list_foreach (h->widgets, (GFunc) g_free, NULL);
1154 g_list_free (h->widgets);
1155 g_free (h->title);
1156 g_free (h);
1158 do_refresh ();
1161 /* --------------------------------------------------------------------------------------------- */
1163 char *
1164 dlg_get_title (const Dlg_head * h, size_t len)
1166 char *t;
1168 if (h == NULL)
1169 abort ();
1171 if (h->get_title != NULL)
1172 t = h->get_title (h, len);
1173 else
1174 t = g_strdup ("");
1176 return t;
1179 /* --------------------------------------------------------------------------------------------- */
1180 /** Replace widget old_w for widget new_w in the dialog */
1182 void
1183 dlg_replace_widget (Widget * old_w, Widget * new_w)
1185 Dlg_head *h = old_w->owner;
1186 gboolean should_focus = FALSE;
1188 if (h->widgets == NULL)
1189 return;
1191 if (h->current == NULL)
1192 h->current = h->widgets;
1194 if (old_w == h->current->data)
1195 should_focus = TRUE;
1197 new_w->owner = h;
1198 new_w->id = old_w->id;
1200 if (should_focus)
1201 h->current->data = new_w;
1202 else
1203 g_list_find (h->widgets, old_w)->data = new_w;
1205 send_message (old_w, WIDGET_DESTROY, 0);
1206 send_message (new_w, WIDGET_INIT, 0);
1208 if (should_focus)
1209 dlg_select_widget (new_w);
1211 send_message (new_w, WIDGET_DRAW, 0);
1214 /* --------------------------------------------------------------------------------------------- */