Ticket #2459: switch to the left panel after mouse click on input line history pictogram.
[midnight-commander.git] / lib / widget / dialog.c
blobcb52c754ad5ad29326281a80b0c6e743af8499a6
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 ************************************************************************/
82 /* --------------------------------------------------------------------------------------------- */
84 static void dlg_broadcast_msg_to (Dlg_head * h, widget_msg_t msg, gboolean reverse, int flags);
86 /* --------------------------------------------------------------------------------------------- */
87 /**
88 * broadcast a message to all the widgets in a dialog that have
89 * the options set to flags. If flags is zero, the message is sent
90 * to all widgets.
93 static void
94 dlg_broadcast_msg_to (Dlg_head * h, widget_msg_t msg, gboolean reverse, int flags)
96 GList *p, *first;
98 if (h->widgets == NULL)
99 return;
101 if (h->current == NULL)
102 h->current = h->widgets;
104 if (reverse)
106 p = g_list_previous (h->current);
108 if (p == NULL)
109 p = g_list_last (h->widgets);
111 else
113 p = g_list_next (h->current);
115 if (p == NULL)
116 p = h->widgets;
119 first = p;
123 Widget *w = (Widget *) p->data;
125 if (reverse)
127 p = g_list_previous (p);
129 if (p == NULL)
130 p = g_list_last (h->widgets);
132 else
134 p = g_list_next (p);
136 if (p == NULL)
137 p = h->widgets;
140 if ((flags == 0) || ((flags & w->options) != 0))
141 send_message (w, msg, 0);
143 while (first != p);
146 /* --------------------------------------------------------------------------------------------- */
148 static int
149 dlg_unfocus (Dlg_head * h)
151 /* ... but can unfocus disabled widget */
153 if ((h->current != NULL) && (h->state == DLG_ACTIVE))
155 Widget *current = (Widget *) h->current->data;
157 if (send_message (current, WIDGET_UNFOCUS, 0) == MSG_HANDLED)
159 h->callback (h, current, DLG_UNFOCUS, 0, NULL);
160 return 1;
164 return 0;
167 /* --------------------------------------------------------------------------------------------- */
169 static int
170 dlg_find_widget_callback (const void *a, const void *b)
172 const Widget *w = (const Widget *) a;
173 callback_fn f = (callback_fn) b;
175 return (w->callback == f) ? 0 : 1;
178 /* --------------------------------------------------------------------------------------------- */
180 * Try to select another widget. If forward is set, follow tab order.
181 * Otherwise go to the previous widget.
184 static void
185 do_select_widget (Dlg_head * h, GList * w, select_dir_t dir)
187 Widget *w0 = (Widget *) h->current->data;
189 if (!dlg_unfocus (h))
190 return;
192 h->current = w;
196 if (dlg_focus (h))
197 break;
199 switch (dir)
201 case SELECT_NEXT:
202 h->current = g_list_next (h->current);
203 if (h->current == NULL)
204 h->current = h->widgets;
205 break;
206 case SELECT_PREV:
207 h->current = g_list_previous (h->current);
208 if (h->current == NULL)
209 h->current = g_list_last (h->widgets);
210 break;
211 case SELECT_EXACT:
212 h->current = g_list_find (h->widgets, w0);
213 dlg_focus (h);
214 return;
217 while (h->current != w /* && (((Widget *) h->current->data)->options & W_DISABLED) == 0 */ );
219 if (dlg_overlap (w0, (Widget *) h->current->data))
221 send_message ((Widget *) h->current->data, WIDGET_DRAW, 0);
222 send_message ((Widget *) h->current->data, WIDGET_FOCUS, 0);
226 /* --------------------------------------------------------------------------------------------- */
228 static void
229 refresh_cmd (void)
231 #ifdef HAVE_SLANG
232 tty_touch_screen ();
233 mc_refresh ();
234 #else
235 /* Use this if the refreshes fail */
236 clr_scr ();
237 repaint_screen ();
238 #endif /* HAVE_SLANG */
241 /* --------------------------------------------------------------------------------------------- */
243 static cb_ret_t
244 dlg_execute_cmd (Dlg_head * h, unsigned long command)
246 cb_ret_t ret = MSG_HANDLED;
247 switch (command)
249 case CK_DialogOK:
250 h->ret_value = B_ENTER;
251 dlg_stop (h);
252 break;
253 case CK_DialogCancel:
254 h->ret_value = B_CANCEL;
255 dlg_stop (h);
256 break;
258 case CK_DialogPrevItem:
259 dlg_one_up (h);
260 break;
261 case CK_DialogNextItem:
262 dlg_one_down (h);
263 break;
265 case CK_DialogHelp:
266 interactive_display (NULL, h->help_ctx);
267 do_refresh ();
268 break;
270 case CK_DialogSuspend:
271 suspend_cmd ();
272 refresh_cmd ();
273 break;
274 case CK_DialogRefresh:
275 refresh_cmd ();
276 break;
278 case CK_DialogListCmd:
279 if (!h->modal)
280 dialog_switch_list ();
281 else
282 ret = MSG_NOT_HANDLED;
283 break;
284 case CK_DialogNextCmd:
285 if (!h->modal)
286 dialog_switch_next ();
287 else
288 ret = MSG_NOT_HANDLED;
289 break;
290 case CK_DialogPrevCmd:
291 if (!h->modal)
292 dialog_switch_prev ();
293 else
294 ret = MSG_NOT_HANDLED;
295 break;
297 default:
298 ret = MSG_NOT_HANDLED;
301 return ret;
304 /* --------------------------------------------------------------------------------------------- */
306 static cb_ret_t
307 dlg_handle_key (Dlg_head * h, int d_key)
309 unsigned long command;
310 command = keybind_lookup_keymap_command (dialog_map, d_key);
311 if ((command == CK_Ignore_Key) || (dlg_execute_cmd (h, command) == MSG_NOT_HANDLED))
312 return MSG_NOT_HANDLED;
313 else
314 return MSG_HANDLED;
317 /* --------------------------------------------------------------------------------------------- */
319 static int
320 dlg_mouse_event (Dlg_head * h, Gpm_Event * event)
322 GList *item;
323 GList *starting_widget = h->current;
324 Gpm_Event new_event;
325 int x = event->x;
326 int y = event->y;
328 /* close the dialog by mouse click out of dialog area */
329 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0) && ((event->type & GPM_DOWN) != 0) /* left click */
330 && !((x > h->x) && (x <= h->x + h->cols) && (y > h->y) && (y <= h->y + h->lines)))
332 h->ret_value = B_CANCEL;
333 dlg_stop (h);
334 return MOU_NORMAL;
337 item = starting_widget;
340 Widget *widget;
342 widget = (Widget *) item->data;
343 item = g_list_next (item);
344 if (item == NULL)
345 item = h->widgets;
347 if (((widget->options & W_DISABLED) == 0)
348 && (x > widget->x) && (x <= widget->x + widget->cols)
349 && (y > widget->y) && (y <= widget->y + widget->lines))
351 new_event = *event;
352 new_event.x -= widget->x;
353 new_event.y -= widget->y;
355 if (widget->mouse != NULL)
356 return widget->mouse (&new_event, widget);
359 while (item != starting_widget);
361 return MOU_NORMAL;
364 /* --------------------------------------------------------------------------------------------- */
366 static cb_ret_t
367 dlg_try_hotkey (Dlg_head * h, int d_key)
369 GList *hot_cur;
370 Widget *current;
371 cb_ret_t handled;
372 int c;
374 if (h->widgets == NULL)
375 return MSG_NOT_HANDLED;
377 if (h->current == NULL)
378 h->current = h->widgets;
381 * Explanation: we don't send letter hotkeys to other widgets if
382 * the currently selected widget is an input line
385 current = (Widget *) h->current->data;
387 if ((current->options & W_DISABLED) != 0)
388 return MSG_NOT_HANDLED;
390 if (current->options & W_IS_INPUT)
392 /* skip ascii control characters, anything else can valid character in
393 * some encoding */
394 if (d_key >= 32 && d_key < 256)
395 return MSG_NOT_HANDLED;
398 /* If it's an alt key, send the message */
399 c = d_key & ~ALT (0);
400 if (d_key & ALT (0) && g_ascii_isalpha (c))
401 d_key = g_ascii_tolower (c);
403 handled = MSG_NOT_HANDLED;
404 if ((current->options & W_WANT_HOTKEY) != 0)
405 handled = send_message (current, WIDGET_HOTKEY, d_key);
407 /* If not used, send hotkey to other widgets */
408 if (handled == MSG_HANDLED)
409 return MSG_HANDLED;
411 hot_cur = g_list_next (h->current);
412 if (hot_cur == NULL)
413 hot_cur = h->widgets;
415 /* send it to all widgets */
416 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
418 current = (Widget *) hot_cur->data;
420 if ((current->options & W_WANT_HOTKEY) != 0)
421 handled = send_message (current, WIDGET_HOTKEY, d_key);
423 if (handled == MSG_NOT_HANDLED)
425 hot_cur = g_list_next (hot_cur);
426 if (hot_cur == NULL)
427 hot_cur = h->widgets;
431 if (handled == MSG_HANDLED)
432 do_select_widget (h, hot_cur, SELECT_EXACT);
434 return handled;
437 /* --------------------------------------------------------------------------------------------- */
439 static void
440 dlg_key_event (Dlg_head * h, int d_key)
442 cb_ret_t handled;
444 if (h->widgets == NULL)
445 return;
447 if (h->current == NULL)
448 h->current = h->widgets;
450 /* TAB used to cycle */
451 if ((h->flags & DLG_WANT_TAB) == 0)
453 if (d_key == '\t')
455 dlg_one_down (h);
456 return;
458 else if (d_key == KEY_BTAB)
460 dlg_one_up (h);
461 return;
465 /* first can dlg_callback handle the key */
466 handled = h->callback (h, NULL, DLG_KEY, d_key, NULL);
468 /* next try the hotkey */
469 if (handled == MSG_NOT_HANDLED)
470 handled = dlg_try_hotkey (h, d_key);
472 if (handled == MSG_HANDLED)
473 h->callback (h, NULL, DLG_HOTKEY_HANDLED, 0, NULL);
474 else
475 /* not used - then try widget_callback */
476 handled = send_message ((Widget *) h->current->data, WIDGET_KEY, d_key);
478 /* not used- try to use the unhandled case */
479 if (handled == MSG_NOT_HANDLED)
480 handled = h->callback (h, NULL, DLG_UNHANDLED_KEY, d_key, NULL);
482 if (handled == MSG_NOT_HANDLED)
483 handled = dlg_handle_key (h, d_key);
485 h->callback (h, NULL, DLG_POST_KEY, d_key, NULL);
488 /* --------------------------------------------------------------------------------------------- */
490 static void
491 frontend_run_dlg (Dlg_head * h)
493 int d_key;
494 Gpm_Event event;
496 event.x = -1;
498 /* close opened editors, viewers, etc */
499 if (!h->modal && midnight_shutdown)
501 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
502 return;
505 while (h->state == DLG_ACTIVE)
507 if (winch_flag)
508 change_screen_size ();
510 if (is_idle ())
512 if (idle_hook)
513 execute_hooks (idle_hook);
515 while ((h->flags & DLG_WANT_IDLE) && is_idle ())
516 h->callback (h, NULL, DLG_IDLE, 0, NULL);
518 /* Allow terminating the dialog from the idle handler */
519 if (h->state != DLG_ACTIVE)
520 break;
523 update_cursor (h);
525 /* Clear interrupt flag */
526 tty_got_interrupt ();
527 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
529 dlg_process_event (h, d_key, &event);
531 if (h->state == DLG_CLOSED)
532 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
536 /* --------------------------------------------------------------------------------------------- */
537 /*** public functions ****************************************************************************/
538 /* --------------------------------------------------------------------------------------------- */
540 /** draw box in window */
541 void
542 draw_box (Dlg_head * h, int y, int x, int ys, int xs, gboolean single)
544 tty_draw_box (h->y + y, h->x + x, ys, xs, single);
547 /* --------------------------------------------------------------------------------------------- */
549 /** Clean the dialog area, draw the frame and the title */
550 void
551 common_dialog_repaint (Dlg_head * h)
553 int space;
555 if (h->state != DLG_ACTIVE)
556 return;
558 space = (h->flags & DLG_COMPACT) ? 0 : 1;
560 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
561 dlg_erase (h);
562 draw_box (h, space, space, h->lines - 2 * space, h->cols - 2 * space, FALSE);
564 if (h->title != NULL)
566 tty_setcolor (h->color[DLG_COLOR_TITLE]);
567 dlg_move (h, space, (h->cols - str_term_width1 (h->title)) / 2);
568 tty_print_string (h->title);
572 /* --------------------------------------------------------------------------------------------- */
573 /** this function allows to set dialog position */
575 void
576 dlg_set_position (Dlg_head * h, int y1, int x1, int y2, int x2)
578 /* save old positions, will be used to reposition childs */
579 int ox, oy, oc, ol;
580 int shift_x, shift_y, scale_x, scale_y;
582 /* save old positions, will be used to reposition childs */
583 ox = h->x;
584 oy = h->y;
585 oc = h->cols;
586 ol = h->lines;
588 h->x = x1;
589 h->y = y1;
590 h->lines = y2 - y1;
591 h->cols = x2 - x1;
593 /* dialog is empty */
594 if (h->widgets == NULL)
595 return;
597 if (h->current == NULL)
598 h->current = h->widgets;
600 /* values by which controls should be moved */
601 shift_x = h->x - ox;
602 shift_y = h->y - oy;
603 scale_x = h->cols - oc;
604 scale_y = h->lines - ol;
606 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
608 GList *w;
610 for (w = h->widgets; w != NULL; w = g_list_next (w))
612 /* there are, mainly, 2 generally possible
613 situations:
615 1. control sticks to one side - it
616 should be moved
618 2. control sticks to two sides of
619 one direction - it should be sized */
621 Widget *c = (Widget *) w->data;
622 int x = c->x;
623 int y = c->y;
624 int cols = c->cols;
625 int lines = c->lines;
627 if ((c->pos_flags & WPOS_KEEP_LEFT) && (c->pos_flags & WPOS_KEEP_RIGHT))
629 x += shift_x;
630 cols += scale_x;
632 else if (c->pos_flags & WPOS_KEEP_LEFT)
633 x += shift_x;
634 else if (c->pos_flags & WPOS_KEEP_RIGHT)
635 x += shift_x + scale_x;
637 if ((c->pos_flags & WPOS_KEEP_TOP) && (c->pos_flags & WPOS_KEEP_BOTTOM))
639 y += shift_y;
640 lines += scale_y;
642 else if (c->pos_flags & WPOS_KEEP_TOP)
643 y += shift_y;
644 else if (c->pos_flags & WPOS_KEEP_BOTTOM)
645 y += shift_y + scale_y;
647 widget_set_size (c, y, x, lines, cols);
652 /* --------------------------------------------------------------------------------------------- */
653 /** this function sets only size, leaving positioning to automatic methods */
655 void
656 dlg_set_size (Dlg_head * h, int lines, int cols)
658 int x = h->x;
659 int y = h->y;
661 if (h->flags & DLG_CENTER)
663 y = (LINES - lines) / 2;
664 x = (COLS - cols) / 2;
667 if ((h->flags & DLG_TRYUP) && (y > 3))
668 y -= 2;
670 dlg_set_position (h, y, x, y + lines, x + cols);
673 /* --------------------------------------------------------------------------------------------- */
674 /** Default dialog callback */
676 cb_ret_t
677 default_dlg_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
679 (void) sender;
680 (void) parm;
681 (void) data;
683 switch (msg)
685 case DLG_DRAW:
686 if (h->color != NULL)
688 common_dialog_repaint (h);
689 return MSG_HANDLED;
691 return MSG_NOT_HANDLED;
693 case DLG_IDLE:
694 dlg_broadcast_msg_to (h, WIDGET_IDLE, FALSE, W_WANT_IDLE);
695 return MSG_HANDLED;
697 case DLG_RESIZE:
698 /* this is default resizing mechanism */
699 /* the main idea of this code is to resize dialog
700 according to flags (if any of flags require automatic
701 resizing, like DLG_CENTER, end after that reposition
702 controls in dialog according to flags of widget) */
703 dlg_set_size (h, h->lines, h->cols);
704 return MSG_HANDLED;
706 default:
707 break;
710 return MSG_NOT_HANDLED;
713 /* --------------------------------------------------------------------------------------------- */
715 Dlg_head *
716 create_dlg (gboolean modal, int y1, int x1, int lines, int cols,
717 const int *colors, dlg_cb_fn callback, const char *help_ctx,
718 const char *title, dlg_flags_t flags)
720 Dlg_head *new_d;
722 new_d = g_new0 (Dlg_head, 1);
723 new_d->modal = modal;
724 if (colors != NULL)
726 memmove (new_d->color, colors, sizeof (dlg_colors_t));
728 new_d->help_ctx = help_ctx;
729 new_d->callback = (callback != NULL) ? callback : default_dlg_callback;
730 new_d->x = x1;
731 new_d->y = y1;
732 new_d->flags = flags;
733 new_d->data = NULL;
735 dlg_set_size (new_d, lines, cols);
736 new_d->fullscreen = (new_d->x == 0 && new_d->y == 0
737 && new_d->cols == COLS && new_d->lines == LINES);
739 new_d->mouse_status = MOU_NORMAL;
741 /* Strip existing spaces, add one space before and after the title */
742 if (title != NULL)
744 char *t;
746 t = g_strstrip (g_strdup (title));
747 if (*t != '\0')
748 new_d->title = g_strdup_printf (" %s ", t);
749 g_free (t);
752 return new_d;
755 /* --------------------------------------------------------------------------------------------- */
757 void
758 dlg_set_default_colors (void)
760 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
761 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
762 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
763 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
764 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
766 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
767 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
768 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
769 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
770 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
773 /* --------------------------------------------------------------------------------------------- */
775 void
776 dlg_erase (Dlg_head * h)
778 if ((h != NULL) && (h->state == DLG_ACTIVE))
779 tty_fill_region (h->y, h->x, h->lines, h->cols, ' ');
782 /* --------------------------------------------------------------------------------------------- */
784 void
785 set_idle_proc (Dlg_head * d, int enable)
787 if (enable)
788 d->flags |= DLG_WANT_IDLE;
789 else
790 d->flags &= ~DLG_WANT_IDLE;
793 /* --------------------------------------------------------------------------------------------- */
795 * Insert widget to dialog before current widget. For dialogs populated
796 * from the bottom, make the widget current. Return widget number.
800 add_widget_autopos (Dlg_head * h, void *w, widget_pos_flags_t pos_flags)
802 Widget *widget = (Widget *) w;
804 /* Don't accept 0 widgets */
805 if (w == NULL)
806 abort ();
808 widget->x += h->x;
809 widget->y += h->y;
810 widget->owner = h;
811 widget->pos_flags = pos_flags;
812 widget->id = g_list_length (h->widgets);
814 if ((h->flags & DLG_REVERSE) != 0)
815 h->widgets = g_list_prepend (h->widgets, widget);
816 else
817 h->widgets = g_list_append (h->widgets, widget);
819 h->current = h->widgets;
821 return widget->id;
824 /* --------------------------------------------------------------------------------------------- */
825 /** wrapper to simply add lefttop positioned controls */
828 add_widget (Dlg_head * h, void *w)
830 return add_widget_autopos (h, w, WPOS_KEEP_LEFT | WPOS_KEEP_TOP);
833 /* --------------------------------------------------------------------------------------------- */
835 void
836 do_refresh (void)
838 GList *d = top_dlg;
840 if (fast_refresh)
842 if ((d != NULL) && (d->data != NULL))
843 dlg_redraw ((Dlg_head *) d->data);
845 else
847 /* Search first fullscreen dialog */
848 for (; d != NULL; d = g_list_next (d))
849 if ((d->data != NULL) && ((Dlg_head *) d->data)->fullscreen)
850 break;
851 /* back to top dialog */
852 for (; d != NULL; d = g_list_previous (d))
853 if (d->data != NULL)
854 dlg_redraw ((Dlg_head *) d->data);
858 /* --------------------------------------------------------------------------------------------- */
859 /** broadcast a message to all the widgets in a dialog */
861 void
862 dlg_broadcast_msg (Dlg_head * h, widget_msg_t msg, gboolean reverse)
864 dlg_broadcast_msg_to (h, msg, reverse, 0);
867 /* --------------------------------------------------------------------------------------------- */
870 dlg_focus (Dlg_head * h)
872 /* cannot focus disabled widget ... */
874 if ((h->current != NULL) && (h->state == DLG_ACTIVE))
876 Widget *current = (Widget *) h->current->data;
878 if (((current->options & W_DISABLED) == 0)
879 && (send_message (current, WIDGET_FOCUS, 0) == MSG_HANDLED))
881 h->callback (h, current, DLG_FOCUS, 0, NULL);
882 return 1;
886 return 0;
889 /* --------------------------------------------------------------------------------------------- */
890 /** Return true if the windows overlap */
893 dlg_overlap (Widget * a, Widget * b)
895 return !((b->x >= a->x + a->cols)
896 || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
900 /* --------------------------------------------------------------------------------------------- */
901 /** Find the widget with the given callback in the dialog h */
903 Widget *
904 find_widget_type (const Dlg_head * h, callback_fn callback)
906 GList *w;
908 w = g_list_find_custom (h->widgets, callback, dlg_find_widget_callback);
910 return (w == NULL) ? NULL : (Widget *) w->data;
913 /* --------------------------------------------------------------------------------------------- */
914 /** Find the widget with the given id */
916 Widget *
917 dlg_find_by_id (const Dlg_head * h, unsigned int id)
919 if (h->widgets != NULL)
921 GList *w;
923 for (w = h->widgets; w != NULL; w = g_list_next (w))
924 if (((Widget *) w->data)->id == id)
925 return (Widget *) w->data;
928 return NULL;
931 /* --------------------------------------------------------------------------------------------- */
932 /** Find the widget with the given id in the dialog h and select it */
934 void
935 dlg_select_by_id (const Dlg_head * h, unsigned int id)
937 Widget *w;
939 w = dlg_find_by_id (h, id);
940 if (w != NULL)
941 dlg_select_widget (w);
944 /* --------------------------------------------------------------------------------------------- */
946 * Try to select widget in the dialog.
949 void
950 dlg_select_widget (void *w)
952 const Widget *widget = (Widget *) w;
953 Dlg_head *h = widget->owner;
955 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
958 /* --------------------------------------------------------------------------------------------- */
959 /** Try to select previous widget in the tab order */
961 void
962 dlg_one_up (Dlg_head * h)
964 if (h->widgets != NULL)
966 GList *prev;
968 prev = g_list_previous (h->current);
969 if (prev == NULL)
970 prev = g_list_last (h->widgets);
972 do_select_widget (h, prev, SELECT_PREV);
976 /* --------------------------------------------------------------------------------------------- */
977 /** Try to select next widget in the tab order */
979 void
980 dlg_one_down (Dlg_head * h)
982 if (h->widgets != NULL)
984 GList *next;
986 next = g_list_next (h->current);
987 if (next == NULL)
988 next = h->widgets;
989 do_select_widget (h, next, SELECT_NEXT);
993 /* --------------------------------------------------------------------------------------------- */
995 void
996 update_cursor (Dlg_head * h)
998 GList *p = h->current;
1000 if ((p != NULL) && (h->state == DLG_ACTIVE))
1002 Widget *w;
1004 w = (Widget *) p->data;
1006 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1007 send_message (w, WIDGET_CURSOR, 0);
1008 else
1011 p = g_list_next (p);
1012 if (p == NULL)
1013 p = h->widgets;
1015 if (p == h->current)
1016 break;
1018 w = (Widget *) p->data;
1020 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1021 if (send_message (w, WIDGET_CURSOR, 0) == MSG_HANDLED)
1022 break;
1024 while (TRUE);
1028 /* --------------------------------------------------------------------------------------------- */
1030 * Redraw the widgets in reverse order, leaving the current widget
1031 * as the last one
1034 void
1035 dlg_redraw (Dlg_head * h)
1037 if (h->state != DLG_ACTIVE)
1038 return;
1040 if (h->winch_pending)
1042 h->winch_pending = FALSE;
1043 h->callback (h, NULL, DLG_RESIZE, 0, NULL);
1046 h->callback (h, NULL, DLG_DRAW, 0, NULL);
1047 dlg_broadcast_msg (h, WIDGET_DRAW, TRUE);
1048 update_cursor (h);
1051 /* --------------------------------------------------------------------------------------------- */
1053 void
1054 dlg_stop (Dlg_head * h)
1056 h->state = DLG_CLOSED;
1059 /* --------------------------------------------------------------------------------------------- */
1060 /** Init the process */
1062 void
1063 init_dlg (Dlg_head * h)
1065 if ((top_dlg != NULL) && ((Dlg_head *) top_dlg->data)->modal)
1066 h->modal = TRUE;
1069 /* add dialog to the stack */
1070 top_dlg = g_list_prepend (top_dlg, h);
1072 /* Initialize dialog manager and widgets */
1073 if (h->state == DLG_ACTIVE)
1075 if (!h->modal)
1076 dialog_switch_add (h);
1078 h->callback (h, NULL, DLG_INIT, 0, NULL);
1079 dlg_broadcast_msg (h, WIDGET_INIT, FALSE);
1082 h->state = DLG_ACTIVE;
1084 dlg_redraw (h);
1086 /* Select the first widget that takes focus */
1087 while (h->current != NULL && !dlg_focus (h))
1089 h->current = g_list_next (h->current);
1090 if (h->current == NULL)
1091 h->current = h->widgets;
1094 h->ret_value = 0;
1097 /* --------------------------------------------------------------------------------------------- */
1099 void
1100 dlg_process_event (Dlg_head * h, int key, Gpm_Event * event)
1102 if (key == EV_NONE)
1104 if (tty_got_interrupt ())
1105 dlg_execute_cmd (h, CK_DialogCancel);
1107 return;
1110 if (key == EV_MOUSE)
1111 h->mouse_status = dlg_mouse_event (h, event);
1112 else
1113 dlg_key_event (h, key);
1116 /* --------------------------------------------------------------------------------------------- */
1117 /** Shutdown the run_dlg */
1119 void
1120 dlg_run_done (Dlg_head * h)
1122 top_dlg = g_list_remove (top_dlg, h);
1124 if (h->state == DLG_CLOSED)
1126 h->callback (h, (Widget *) h->current->data, DLG_END, 0, NULL);
1127 if (!h->modal)
1128 dialog_switch_remove (h);
1133 /* --------------------------------------------------------------------------------------------- */
1135 * Standard run dialog routine
1136 * We have to keep this routine small so that we can duplicate it's
1137 * behavior on complex routines like the file routines, this way,
1138 * they can call the dlg_process_event without rewriting all the code
1142 run_dlg (Dlg_head * h)
1144 init_dlg (h);
1145 frontend_run_dlg (h);
1146 dlg_run_done (h);
1147 return h->ret_value;
1150 /* --------------------------------------------------------------------------------------------- */
1152 void
1153 destroy_dlg (Dlg_head * h)
1155 dlg_broadcast_msg (h, WIDGET_DESTROY, FALSE);
1156 g_list_foreach (h->widgets, (GFunc) g_free, NULL);
1157 g_list_free (h->widgets);
1158 g_free (h->title);
1159 g_free (h);
1161 do_refresh ();
1164 /* --------------------------------------------------------------------------------------------- */
1166 char *
1167 dlg_get_title (const Dlg_head * h, size_t len)
1169 char *t;
1171 if (h == NULL)
1172 abort ();
1174 if (h->get_title != NULL)
1175 t = h->get_title (h, len);
1176 else
1177 t = g_strdup ("");
1179 return t;
1182 /* --------------------------------------------------------------------------------------------- */
1183 /** Replace widget old_w for widget new_w in the dialog */
1185 void
1186 dlg_replace_widget (Widget * old_w, Widget * new_w)
1188 Dlg_head *h = old_w->owner;
1189 gboolean should_focus = FALSE;
1191 if (h->widgets == NULL)
1192 return;
1194 if (h->current == NULL)
1195 h->current = h->widgets;
1197 if (old_w == h->current->data)
1198 should_focus = TRUE;
1200 new_w->owner = h;
1201 new_w->id = old_w->id;
1203 if (should_focus)
1204 h->current->data = new_w;
1205 else
1206 g_list_find (h->widgets, old_w)->data = new_w;
1208 send_message (old_w, WIDGET_DESTROY, 0);
1209 send_message (new_w, WIDGET_INIT, 0);
1211 if (should_focus)
1212 dlg_select_widget (new_w);
1214 send_message (new_w, WIDGET_DRAW, 0);
1217 /* --------------------------------------------------------------------------------------------- */