Updated Russian translation.
[midnight-commander.git] / lib / widget / dialog.c
blobf241d2b612309bcc07b42b8852bc23c374e6cdd9
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 GList *
85 dlg_widget_next (Dlg_head *h, GList *l)
87 GList *next;
89 next = g_list_next (l);
90 if (next == NULL)
91 next = h->widgets;
93 return next;
96 /* --------------------------------------------------------------------------------------------- */
98 static GList *
99 dlg_widget_prev (Dlg_head *h, GList *l)
101 GList *prev;
103 prev = g_list_previous (l);
104 if (prev == NULL)
105 prev = g_list_last (h->widgets);
107 return prev;
110 /* --------------------------------------------------------------------------------------------- */
112 * broadcast a message to all the widgets in a dialog that have
113 * the options set to flags. If flags is zero, the message is sent
114 * to all widgets.
117 static void
118 dlg_broadcast_msg_to (Dlg_head * h, widget_msg_t msg, gboolean reverse, int flags)
120 GList *p, *first;
122 if (h->widgets == NULL)
123 return;
125 if (h->current == NULL)
126 h->current = h->widgets;
128 if (reverse)
129 p = dlg_widget_prev (h, h->current);
130 else
131 p = dlg_widget_next (h, h->current);
133 first = p;
137 Widget *w = (Widget *) p->data;
139 if (reverse)
140 p = dlg_widget_prev (h, p);
141 else
142 p = dlg_widget_next (h, p);
144 if ((flags == 0) || ((flags & w->options) != 0))
145 send_message (w, msg, 0);
147 while (first != p);
150 /* --------------------------------------------------------------------------------------------- */
152 static int
153 dlg_unfocus (Dlg_head * h)
155 /* ... but can unfocus disabled widget */
157 if ((h->current != NULL) && (h->state == DLG_ACTIVE))
159 Widget *current = (Widget *) h->current->data;
161 if (send_message (current, WIDGET_UNFOCUS, 0) == MSG_HANDLED)
163 h->callback (h, current, DLG_UNFOCUS, 0, NULL);
164 return 1;
168 return 0;
171 /* --------------------------------------------------------------------------------------------- */
173 static int
174 dlg_find_widget_callback (const void *a, const void *b)
176 const Widget *w = (const Widget *) a;
177 callback_fn f = (callback_fn) b;
179 return (w->callback == f) ? 0 : 1;
182 /* --------------------------------------------------------------------------------------------- */
184 * Try to select another widget. If forward is set, follow tab order.
185 * Otherwise go to the previous widget.
188 static void
189 do_select_widget (Dlg_head * h, GList * w, select_dir_t dir)
191 Widget *w0 = (Widget *) h->current->data;
193 if (!dlg_unfocus (h))
194 return;
196 h->current = w;
200 if (dlg_focus (h))
201 break;
203 switch (dir)
205 case SELECT_EXACT:
206 h->current = g_list_find (h->widgets, w0);
207 if (dlg_focus (h))
208 return;
209 /* try find another widget that can take focus */
210 dir = SELECT_NEXT;
211 /* fallthrough */
212 case SELECT_NEXT:
213 h->current = dlg_widget_next (h, h->current);
214 break;
215 case SELECT_PREV:
216 h->current = dlg_widget_prev (h, h->current);
217 break;
220 while (h->current != w /* && (((Widget *) h->current->data)->options & W_DISABLED) == 0 */ );
222 if (dlg_overlap (w0, (Widget *) h->current->data))
224 send_message ((Widget *) h->current->data, WIDGET_DRAW, 0);
225 send_message ((Widget *) h->current->data, WIDGET_FOCUS, 0);
229 /* --------------------------------------------------------------------------------------------- */
231 static void
232 refresh_cmd (void)
234 #ifdef HAVE_SLANG
235 tty_touch_screen ();
236 mc_refresh ();
237 #else
238 /* Use this if the refreshes fail */
239 clr_scr ();
240 repaint_screen ();
241 #endif /* HAVE_SLANG */
244 /* --------------------------------------------------------------------------------------------- */
246 static cb_ret_t
247 dlg_execute_cmd (Dlg_head * h, unsigned long command)
249 cb_ret_t ret = MSG_HANDLED;
250 switch (command)
252 case CK_DialogOK:
253 h->ret_value = B_ENTER;
254 dlg_stop (h);
255 break;
256 case CK_DialogCancel:
257 h->ret_value = B_CANCEL;
258 dlg_stop (h);
259 break;
261 case CK_DialogPrevItem:
262 dlg_one_up (h);
263 break;
264 case CK_DialogNextItem:
265 dlg_one_down (h);
266 break;
268 case CK_DialogHelp:
269 interactive_display (NULL, h->help_ctx);
270 do_refresh ();
271 break;
273 case CK_DialogSuspend:
274 suspend_cmd ();
275 refresh_cmd ();
276 break;
277 case CK_DialogRefresh:
278 refresh_cmd ();
279 break;
281 case CK_DialogListCmd:
282 if (!h->modal)
283 dialog_switch_list ();
284 else
285 ret = MSG_NOT_HANDLED;
286 break;
287 case CK_DialogNextCmd:
288 if (!h->modal)
289 dialog_switch_next ();
290 else
291 ret = MSG_NOT_HANDLED;
292 break;
293 case CK_DialogPrevCmd:
294 if (!h->modal)
295 dialog_switch_prev ();
296 else
297 ret = MSG_NOT_HANDLED;
298 break;
300 default:
301 ret = MSG_NOT_HANDLED;
304 return ret;
307 /* --------------------------------------------------------------------------------------------- */
309 static cb_ret_t
310 dlg_handle_key (Dlg_head * h, int d_key)
312 unsigned long command;
313 command = keybind_lookup_keymap_command (dialog_map, d_key);
314 if ((command == CK_Ignore_Key) || (dlg_execute_cmd (h, command) == MSG_NOT_HANDLED))
315 return MSG_NOT_HANDLED;
316 else
317 return MSG_HANDLED;
320 /* --------------------------------------------------------------------------------------------- */
322 static int
323 dlg_mouse_event (Dlg_head * h, Gpm_Event * event)
325 GList *item;
326 GList *starting_widget = h->current;
327 Gpm_Event new_event;
328 int x = event->x;
329 int y = event->y;
331 /* close the dialog by mouse click out of dialog area */
332 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0) && ((event->type & GPM_DOWN) != 0) /* left click */
333 && !((x > h->x) && (x <= h->x + h->cols) && (y > h->y) && (y <= h->y + h->lines)))
335 h->ret_value = B_CANCEL;
336 dlg_stop (h);
337 return MOU_NORMAL;
340 item = starting_widget;
343 Widget *widget;
345 widget = (Widget *) item->data;
346 item = dlg_widget_next (h, item);
348 if (((widget->options & W_DISABLED) == 0)
349 && (x > widget->x) && (x <= widget->x + widget->cols)
350 && (y > widget->y) && (y <= widget->y + widget->lines))
352 new_event = *event;
353 new_event.x -= widget->x;
354 new_event.y -= widget->y;
356 if (widget->mouse != NULL)
357 return widget->mouse (&new_event, widget);
360 while (item != starting_widget);
362 return MOU_NORMAL;
365 /* --------------------------------------------------------------------------------------------- */
367 static cb_ret_t
368 dlg_try_hotkey (Dlg_head * h, int d_key)
370 GList *hot_cur;
371 Widget *current;
372 cb_ret_t handled;
373 int c;
375 if (h->widgets == NULL)
376 return MSG_NOT_HANDLED;
378 if (h->current == NULL)
379 h->current = h->widgets;
382 * Explanation: we don't send letter hotkeys to other widgets if
383 * the currently selected widget is an input line
386 current = (Widget *) h->current->data;
388 if ((current->options & W_DISABLED) != 0)
389 return MSG_NOT_HANDLED;
391 if (current->options & W_IS_INPUT)
393 /* skip ascii control characters, anything else can valid character in
394 * some encoding */
395 if (d_key >= 32 && d_key < 256)
396 return MSG_NOT_HANDLED;
399 /* If it's an alt key, send the message */
400 c = d_key & ~ALT (0);
401 if (d_key & ALT (0) && g_ascii_isalpha (c))
402 d_key = g_ascii_tolower (c);
404 handled = MSG_NOT_HANDLED;
405 if ((current->options & W_WANT_HOTKEY) != 0)
406 handled = send_message (current, WIDGET_HOTKEY, d_key);
408 /* If not used, send hotkey to other widgets */
409 if (handled == MSG_HANDLED)
410 return MSG_HANDLED;
412 hot_cur = dlg_widget_next (h, h->current);
414 /* send it to all widgets */
415 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
417 current = (Widget *) hot_cur->data;
419 if ((current->options & W_WANT_HOTKEY) != 0)
420 handled = send_message (current, WIDGET_HOTKEY, d_key);
422 if (handled == MSG_NOT_HANDLED)
423 hot_cur = dlg_widget_next (h, hot_cur);
426 if (handled == MSG_HANDLED)
427 do_select_widget (h, hot_cur, SELECT_EXACT);
429 return handled;
432 /* --------------------------------------------------------------------------------------------- */
434 static void
435 dlg_key_event (Dlg_head * h, int d_key)
437 cb_ret_t handled;
439 if (h->widgets == NULL)
440 return;
442 if (h->current == NULL)
443 h->current = h->widgets;
445 /* TAB used to cycle */
446 if ((h->flags & DLG_WANT_TAB) == 0)
448 if (d_key == '\t')
450 dlg_one_down (h);
451 return;
453 else if (d_key == KEY_BTAB)
455 dlg_one_up (h);
456 return;
460 /* first can dlg_callback handle the key */
461 handled = h->callback (h, NULL, DLG_KEY, d_key, NULL);
463 /* next try the hotkey */
464 if (handled == MSG_NOT_HANDLED)
465 handled = dlg_try_hotkey (h, d_key);
467 if (handled == MSG_HANDLED)
468 h->callback (h, NULL, DLG_HOTKEY_HANDLED, 0, NULL);
469 else
470 /* not used - then try widget_callback */
471 handled = send_message ((Widget *) h->current->data, WIDGET_KEY, d_key);
473 /* not used- try to use the unhandled case */
474 if (handled == MSG_NOT_HANDLED)
475 handled = h->callback (h, NULL, DLG_UNHANDLED_KEY, d_key, NULL);
477 if (handled == MSG_NOT_HANDLED)
478 handled = dlg_handle_key (h, d_key);
480 h->callback (h, NULL, DLG_POST_KEY, d_key, NULL);
483 /* --------------------------------------------------------------------------------------------- */
485 static void
486 frontend_run_dlg (Dlg_head * h)
488 int d_key;
489 Gpm_Event event;
491 event.x = -1;
493 /* close opened editors, viewers, etc */
494 if (!h->modal && midnight_shutdown)
496 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
497 return;
500 while (h->state == DLG_ACTIVE)
502 if (winch_flag)
503 change_screen_size ();
505 if (is_idle ())
507 if (idle_hook)
508 execute_hooks (idle_hook);
510 while ((h->flags & DLG_WANT_IDLE) && is_idle ())
511 h->callback (h, NULL, DLG_IDLE, 0, NULL);
513 /* Allow terminating the dialog from the idle handler */
514 if (h->state != DLG_ACTIVE)
515 break;
518 update_cursor (h);
520 /* Clear interrupt flag */
521 tty_got_interrupt ();
522 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
524 dlg_process_event (h, d_key, &event);
526 if (h->state == DLG_CLOSED)
527 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
531 /* --------------------------------------------------------------------------------------------- */
533 static int
534 dlg_find_widget_by_id (gconstpointer a, gconstpointer b)
536 Widget *w = (Widget *) a;
537 unsigned long id = GPOINTER_TO_UINT (b);
539 return w->id == id ? 0 : 1;
542 /* --------------------------------------------------------------------------------------------- */
543 /*** public functions ****************************************************************************/
544 /* --------------------------------------------------------------------------------------------- */
546 /** draw box in window */
547 void
548 draw_box (Dlg_head * h, int y, int x, int ys, int xs, gboolean single)
550 tty_draw_box (h->y + y, h->x + x, ys, xs, single);
553 /* --------------------------------------------------------------------------------------------- */
555 /** Clean the dialog area, draw the frame and the title */
556 void
557 common_dialog_repaint (Dlg_head * h)
559 int space;
561 if (h->state != DLG_ACTIVE)
562 return;
564 space = (h->flags & DLG_COMPACT) ? 0 : 1;
566 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
567 dlg_erase (h);
568 draw_box (h, space, space, h->lines - 2 * space, h->cols - 2 * space, FALSE);
570 if (h->title != NULL)
572 tty_setcolor (h->color[DLG_COLOR_TITLE]);
573 dlg_move (h, space, (h->cols - str_term_width1 (h->title)) / 2);
574 tty_print_string (h->title);
578 /* --------------------------------------------------------------------------------------------- */
579 /** this function allows to set dialog position */
581 void
582 dlg_set_position (Dlg_head * h, int y1, int x1, int y2, int x2)
584 /* save old positions, will be used to reposition childs */
585 int ox, oy, oc, ol;
586 int shift_x, shift_y, scale_x, scale_y;
588 /* save old positions, will be used to reposition childs */
589 ox = h->x;
590 oy = h->y;
591 oc = h->cols;
592 ol = h->lines;
594 h->x = x1;
595 h->y = y1;
596 h->lines = y2 - y1;
597 h->cols = x2 - x1;
599 /* dialog is empty */
600 if (h->widgets == NULL)
601 return;
603 if (h->current == NULL)
604 h->current = h->widgets;
606 /* values by which controls should be moved */
607 shift_x = h->x - ox;
608 shift_y = h->y - oy;
609 scale_x = h->cols - oc;
610 scale_y = h->lines - ol;
612 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
614 GList *w;
616 for (w = h->widgets; w != NULL; w = g_list_next (w))
618 /* there are, mainly, 2 generally possible
619 situations:
621 1. control sticks to one side - it
622 should be moved
624 2. control sticks to two sides of
625 one direction - it should be sized */
627 Widget *c = (Widget *) w->data;
628 int x = c->x;
629 int y = c->y;
630 int cols = c->cols;
631 int lines = c->lines;
633 if ((c->pos_flags & WPOS_KEEP_LEFT) && (c->pos_flags & WPOS_KEEP_RIGHT))
635 x += shift_x;
636 cols += scale_x;
638 else if (c->pos_flags & WPOS_KEEP_LEFT)
639 x += shift_x;
640 else if (c->pos_flags & WPOS_KEEP_RIGHT)
641 x += shift_x + scale_x;
643 if ((c->pos_flags & WPOS_KEEP_TOP) && (c->pos_flags & WPOS_KEEP_BOTTOM))
645 y += shift_y;
646 lines += scale_y;
648 else if (c->pos_flags & WPOS_KEEP_TOP)
649 y += shift_y;
650 else if (c->pos_flags & WPOS_KEEP_BOTTOM)
651 y += shift_y + scale_y;
653 widget_set_size (c, y, x, lines, cols);
658 /* --------------------------------------------------------------------------------------------- */
659 /** this function sets only size, leaving positioning to automatic methods */
661 void
662 dlg_set_size (Dlg_head * h, int lines, int cols)
664 int x = h->x;
665 int y = h->y;
667 if (h->flags & DLG_CENTER)
669 y = (LINES - lines) / 2;
670 x = (COLS - cols) / 2;
673 if ((h->flags & DLG_TRYUP) && (y > 3))
674 y -= 2;
676 dlg_set_position (h, y, x, y + lines, x + cols);
679 /* --------------------------------------------------------------------------------------------- */
680 /** Default dialog callback */
682 cb_ret_t
683 default_dlg_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
685 (void) sender;
686 (void) parm;
687 (void) data;
689 switch (msg)
691 case DLG_DRAW:
692 if (h->color != NULL)
694 common_dialog_repaint (h);
695 return MSG_HANDLED;
697 return MSG_NOT_HANDLED;
699 case DLG_IDLE:
700 dlg_broadcast_msg_to (h, WIDGET_IDLE, FALSE, W_WANT_IDLE);
701 return MSG_HANDLED;
703 case DLG_RESIZE:
704 /* this is default resizing mechanism */
705 /* the main idea of this code is to resize dialog
706 according to flags (if any of flags require automatic
707 resizing, like DLG_CENTER, end after that reposition
708 controls in dialog according to flags of widget) */
709 dlg_set_size (h, h->lines, h->cols);
710 return MSG_HANDLED;
712 default:
713 break;
716 return MSG_NOT_HANDLED;
719 /* --------------------------------------------------------------------------------------------- */
721 Dlg_head *
722 create_dlg (gboolean modal, int y1, int x1, int lines, int cols,
723 const int *colors, dlg_cb_fn callback, const char *help_ctx,
724 const char *title, dlg_flags_t flags)
726 Dlg_head *new_d;
728 new_d = g_new0 (Dlg_head, 1);
729 new_d->modal = modal;
730 if (colors != NULL)
732 memmove (new_d->color, colors, sizeof (dlg_colors_t));
734 new_d->help_ctx = help_ctx;
735 new_d->callback = (callback != NULL) ? callback : default_dlg_callback;
736 new_d->x = x1;
737 new_d->y = y1;
738 new_d->flags = flags;
739 new_d->data = NULL;
741 dlg_set_size (new_d, lines, cols);
742 new_d->fullscreen = (new_d->x == 0 && new_d->y == 0
743 && new_d->cols == COLS && new_d->lines == LINES);
745 new_d->mouse_status = MOU_NORMAL;
747 /* Strip existing spaces, add one space before and after the title */
748 if (title != NULL)
750 char *t;
752 t = g_strstrip (g_strdup (title));
753 if (*t != '\0')
754 new_d->title = g_strdup_printf (" %s ", t);
755 g_free (t);
758 return new_d;
761 /* --------------------------------------------------------------------------------------------- */
763 void
764 dlg_set_default_colors (void)
766 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
767 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
768 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
769 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
770 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
772 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
773 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
774 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
775 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
776 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
779 /* --------------------------------------------------------------------------------------------- */
781 void
782 dlg_erase (Dlg_head * h)
784 if ((h != NULL) && (h->state == DLG_ACTIVE))
785 tty_fill_region (h->y, h->x, h->lines, h->cols, ' ');
788 /* --------------------------------------------------------------------------------------------- */
790 void
791 set_idle_proc (Dlg_head * d, int enable)
793 if (enable)
794 d->flags |= DLG_WANT_IDLE;
795 else
796 d->flags &= ~DLG_WANT_IDLE;
799 /* --------------------------------------------------------------------------------------------- */
801 * Insert widget to dialog before current widget. For dialogs populated
802 * from the bottom, make the widget current. Return widget number.
806 add_widget_autopos (Dlg_head * h, void *w, widget_pos_flags_t pos_flags)
808 Widget *widget = (Widget *) w;
810 /* Don't accept 0 widgets */
811 if (w == NULL)
812 abort ();
814 widget->x += h->x;
815 widget->y += h->y;
816 widget->owner = h;
817 widget->pos_flags = pos_flags;
818 widget->id = g_list_length (h->widgets);
820 if ((h->flags & DLG_REVERSE) != 0)
821 h->widgets = g_list_prepend (h->widgets, widget);
822 else
823 h->widgets = g_list_append (h->widgets, widget);
825 h->current = h->widgets;
827 return widget->id;
830 /* --------------------------------------------------------------------------------------------- */
831 /** wrapper to simply add lefttop positioned controls */
834 add_widget (Dlg_head * h, void *w)
836 return add_widget_autopos (h, w, WPOS_KEEP_LEFT | WPOS_KEEP_TOP);
839 /* --------------------------------------------------------------------------------------------- */
841 void
842 do_refresh (void)
844 GList *d = top_dlg;
846 if (fast_refresh)
848 if ((d != NULL) && (d->data != NULL))
849 dlg_redraw ((Dlg_head *) d->data);
851 else
853 /* Search first fullscreen dialog */
854 for (; d != NULL; d = g_list_next (d))
855 if ((d->data != NULL) && ((Dlg_head *) d->data)->fullscreen)
856 break;
857 /* back to top dialog */
858 for (; d != NULL; d = g_list_previous (d))
859 if (d->data != NULL)
860 dlg_redraw ((Dlg_head *) d->data);
864 /* --------------------------------------------------------------------------------------------- */
865 /** broadcast a message to all the widgets in a dialog */
867 void
868 dlg_broadcast_msg (Dlg_head * h, widget_msg_t msg, gboolean reverse)
870 dlg_broadcast_msg_to (h, msg, reverse, 0);
873 /* --------------------------------------------------------------------------------------------- */
876 dlg_focus (Dlg_head * h)
878 /* cannot focus disabled widget ... */
880 if ((h->current != NULL) && (h->state == DLG_ACTIVE))
882 Widget *current = (Widget *) h->current->data;
884 if (((current->options & W_DISABLED) == 0)
885 && (send_message (current, WIDGET_FOCUS, 0) == MSG_HANDLED))
887 h->callback (h, current, DLG_FOCUS, 0, NULL);
888 return 1;
892 return 0;
895 /* --------------------------------------------------------------------------------------------- */
896 /** Return true if the windows overlap */
899 dlg_overlap (Widget * a, Widget * b)
901 return !((b->x >= a->x + a->cols)
902 || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
906 /* --------------------------------------------------------------------------------------------- */
907 /** Find the widget with the given callback in the dialog h */
909 Widget *
910 find_widget_type (const Dlg_head * h, callback_fn callback)
912 GList *w;
914 w = g_list_find_custom (h->widgets, callback, dlg_find_widget_callback);
916 return (w == NULL) ? NULL : (Widget *) w->data;
919 /* --------------------------------------------------------------------------------------------- */
920 /** Find the widget with the given id */
922 Widget *
923 dlg_find_by_id (const Dlg_head * h, unsigned int id)
925 GList *w;
927 w = g_list_find_custom (h->widgets, GUINT_TO_POINTER (id), dlg_find_widget_by_id);
928 return w != NULL ? (Widget *) w->data : 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)
965 do_select_widget (h, dlg_widget_prev (h, h->current), SELECT_PREV);
968 /* --------------------------------------------------------------------------------------------- */
969 /** Try to select next widget in the tab order */
971 void
972 dlg_one_down (Dlg_head * h)
974 if (h->widgets != NULL)
975 do_select_widget (h, dlg_widget_next (h, h->current), SELECT_NEXT);
978 /* --------------------------------------------------------------------------------------------- */
980 void
981 update_cursor (Dlg_head * h)
983 GList *p = h->current;
985 if ((p != NULL) && (h->state == DLG_ACTIVE))
987 Widget *w;
989 w = (Widget *) p->data;
991 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
992 send_message (w, WIDGET_CURSOR, 0);
993 else
996 p = dlg_widget_next (h, p);
997 if (p == h->current)
998 break;
1000 w = (Widget *) p->data;
1002 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1003 if (send_message (w, WIDGET_CURSOR, 0) == MSG_HANDLED)
1004 break;
1006 while (TRUE);
1010 /* --------------------------------------------------------------------------------------------- */
1012 * Redraw the widgets in reverse order, leaving the current widget
1013 * as the last one
1016 void
1017 dlg_redraw (Dlg_head * h)
1019 if (h->state != DLG_ACTIVE)
1020 return;
1022 if (h->winch_pending)
1024 h->winch_pending = FALSE;
1025 h->callback (h, NULL, DLG_RESIZE, 0, NULL);
1028 h->callback (h, NULL, DLG_DRAW, 0, NULL);
1029 dlg_broadcast_msg (h, WIDGET_DRAW, TRUE);
1030 update_cursor (h);
1033 /* --------------------------------------------------------------------------------------------- */
1035 void
1036 dlg_stop (Dlg_head * h)
1038 h->state = DLG_CLOSED;
1041 /* --------------------------------------------------------------------------------------------- */
1042 /** Init the process */
1044 void
1045 init_dlg (Dlg_head * h)
1047 if ((top_dlg != NULL) && ((Dlg_head *) top_dlg->data)->modal)
1048 h->modal = TRUE;
1050 /* add dialog to the stack */
1051 top_dlg = g_list_prepend (top_dlg, h);
1053 /* Initialize dialog manager and widgets */
1054 if (h->state == DLG_ACTIVE)
1056 if (!h->modal)
1057 dialog_switch_add (h);
1059 h->callback (h, NULL, DLG_INIT, 0, NULL);
1060 dlg_broadcast_msg (h, WIDGET_INIT, FALSE);
1063 h->state = DLG_ACTIVE;
1065 dlg_redraw (h);
1067 /* Select the first widget that takes focus */
1068 while (h->current != NULL && !dlg_focus (h))
1069 h->current = dlg_widget_next (h, h->current);
1071 h->ret_value = 0;
1074 /* --------------------------------------------------------------------------------------------- */
1076 void
1077 dlg_process_event (Dlg_head * h, int key, Gpm_Event * event)
1079 if (key == EV_NONE)
1081 if (tty_got_interrupt ())
1082 if (h->callback (h, NULL, DLG_ACTION, CK_DialogCancel, NULL) != MSG_HANDLED)
1083 dlg_execute_cmd (h, CK_DialogCancel);
1085 return;
1088 if (key == EV_MOUSE)
1089 h->mouse_status = dlg_mouse_event (h, event);
1090 else
1091 dlg_key_event (h, key);
1094 /* --------------------------------------------------------------------------------------------- */
1095 /** Shutdown the run_dlg */
1097 void
1098 dlg_run_done (Dlg_head * h)
1100 top_dlg = g_list_remove (top_dlg, h);
1102 if (h->state == DLG_CLOSED)
1104 h->callback (h, (Widget *) h->current->data, DLG_END, 0, NULL);
1105 if (!h->modal)
1106 dialog_switch_remove (h);
1111 /* --------------------------------------------------------------------------------------------- */
1113 * Standard run dialog routine
1114 * We have to keep this routine small so that we can duplicate it's
1115 * behavior on complex routines like the file routines, this way,
1116 * they can call the dlg_process_event without rewriting all the code
1120 run_dlg (Dlg_head * h)
1122 init_dlg (h);
1123 frontend_run_dlg (h);
1124 dlg_run_done (h);
1125 return h->ret_value;
1128 /* --------------------------------------------------------------------------------------------- */
1130 void
1131 destroy_dlg (Dlg_head * h)
1133 dlg_broadcast_msg (h, WIDGET_DESTROY, FALSE);
1134 g_list_foreach (h->widgets, (GFunc) g_free, NULL);
1135 g_list_free (h->widgets);
1136 g_free (h->title);
1137 g_free (h);
1139 do_refresh ();
1142 /* --------------------------------------------------------------------------------------------- */
1144 char *
1145 dlg_get_title (const Dlg_head * h, size_t len)
1147 char *t;
1149 if (h == NULL)
1150 abort ();
1152 if (h->get_title != NULL)
1153 t = h->get_title (h, len);
1154 else
1155 t = g_strdup ("");
1157 return t;
1160 /* --------------------------------------------------------------------------------------------- */
1161 /** Replace widget old_w for widget new_w in the dialog */
1163 void
1164 dlg_replace_widget (Widget * old_w, Widget * new_w)
1166 Dlg_head *h = old_w->owner;
1167 gboolean should_focus = FALSE;
1169 if (h->widgets == NULL)
1170 return;
1172 if (h->current == NULL)
1173 h->current = h->widgets;
1175 if (old_w == h->current->data)
1176 should_focus = TRUE;
1178 new_w->owner = h;
1179 new_w->id = old_w->id;
1181 if (should_focus)
1182 h->current->data = new_w;
1183 else
1184 g_list_find (h->widgets, old_w)->data = new_w;
1186 send_message (old_w, WIDGET_DESTROY, 0);
1187 send_message (new_w, WIDGET_INIT, 0);
1189 if (should_focus)
1190 dlg_select_widget (new_w);
1192 send_message (new_w, WIDGET_DRAW, 0);
1195 /* --------------------------------------------------------------------------------------------- */