(dlg_mouse_event): minor refactoring.
[midnight-commander.git] / lib / widget / dialog.c
blob69fca07209b1a0764540c1ae3ec985d10f826f7b
1 /*
2 Dialog box features module for the Midnight Commander
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2007, 2009, 2010, 2011, 2013
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /** \file dialog.c
25 * \brief Source: dialog box features module
28 #include <config.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h> /* open() */
39 #include "lib/global.h"
41 #include "lib/tty/tty.h"
42 #include "lib/skin.h"
43 #include "lib/tty/key.h"
44 #include "lib/strutil.h"
45 #include "lib/widget.h"
46 #include "lib/fileloc.h" /* MC_HISTORY_FILE */
47 #include "lib/event.h" /* mc_event_raise() */
49 /*** global variables ****************************************************************************/
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_t *idle_hook = NULL;
62 /* If set then dialogs just clean the screen when refreshing, else */
63 /* they do a complete refresh, refreshing all the parts of the program */
64 int fast_refresh = 0;
66 /* left click outside of dialog closes it */
67 int mouse_close_dialog = 0;
69 const global_keymap_t *dialog_map = NULL;
71 /*** file scope macro definitions ****************************************************************/
73 /*** file scope type declarations ****************************************************************/
75 /** What to do if the requested widget doesn't take focus */
76 typedef enum
78 SELECT_NEXT, /* go the the next widget */
79 SELECT_PREV, /* go the the previous widget */
80 SELECT_EXACT /* use current widget */
81 } select_dir_t;
83 /*** file scope variables ************************************************************************/
85 /*** file scope functions ************************************************************************/
87 static GList *
88 dlg_widget_next (WDialog * h, GList * l)
90 GList *next;
92 next = g_list_next (l);
93 if (next == NULL)
94 next = h->widgets;
96 return next;
99 /* --------------------------------------------------------------------------------------------- */
101 static GList *
102 dlg_widget_prev (WDialog * h, GList * l)
104 GList *prev;
106 prev = g_list_previous (l);
107 if (prev == NULL)
108 prev = g_list_last (h->widgets);
110 return prev;
113 /* --------------------------------------------------------------------------------------------- */
115 * broadcast a message to all the widgets in a dialog that have
116 * the options set to flags. If flags is zero, the message is sent
117 * to all widgets.
120 static void
121 dlg_broadcast_msg_to (WDialog * h, widget_msg_t msg, gboolean reverse, int flags)
123 GList *p, *first;
125 if (h->widgets == NULL)
126 return;
128 if (h->current == NULL)
129 h->current = h->widgets;
131 if (reverse)
132 p = dlg_widget_prev (h, h->current);
133 else
134 p = dlg_widget_next (h, h->current);
136 first = p;
140 Widget *w = WIDGET (p->data);
142 if (reverse)
143 p = dlg_widget_prev (h, p);
144 else
145 p = dlg_widget_next (h, p);
147 if ((flags == 0) || ((flags & w->options) != 0))
148 send_message (w, NULL, msg, 0, NULL);
150 while (first != p);
153 /* --------------------------------------------------------------------------------------------- */
156 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
158 static void
159 dlg_read_history (WDialog * h)
161 char *profile;
162 ev_history_load_save_t event_data;
164 if (num_history_items_recorded == 0) /* this is how to disable */
165 return;
167 profile = mc_config_get_full_path (MC_HISTORY_FILE);
168 event_data.cfg = mc_config_init (profile, TRUE);
169 event_data.receiver = NULL;
171 /* create all histories in dialog */
172 mc_event_raise (h->event_group, MCEVENT_HISTORY_LOAD, &event_data);
174 mc_config_deinit (event_data.cfg);
175 g_free (profile);
178 /* --------------------------------------------------------------------------------------------- */
180 static gboolean
181 dlg_unfocus (WDialog * h)
183 /* we can unfocus disabled widget */
184 if ((h->current != NULL) && (h->state == DLG_CONSTRUCT || h->state == DLG_ACTIVE))
186 Widget *current = WIDGET (h->current->data);
188 if (send_message (current, NULL, MSG_UNFOCUS, 0, NULL) == MSG_HANDLED)
190 send_message (h, current, MSG_UNFOCUS, 0, NULL);
191 return TRUE;
195 return FALSE;
198 /* --------------------------------------------------------------------------------------------- */
200 static int
201 dlg_find_widget_callback (const void *a, const void *b)
203 const Widget *w = WIDGET (a);
204 widget_cb_fn f = (widget_cb_fn) b;
206 return (w->callback == f) ? 0 : 1;
209 /* --------------------------------------------------------------------------------------------- */
211 * Try to select another widget. If forward is set, follow tab order.
212 * Otherwise go to the previous widget.
215 static void
216 do_select_widget (WDialog * h, GList * w, select_dir_t dir)
218 Widget *w0 = WIDGET (h->current->data);
220 if (!dlg_unfocus (h))
221 return;
223 h->current = w;
227 if (dlg_focus (h))
228 break;
230 switch (dir)
232 case SELECT_EXACT:
233 h->current = g_list_find (h->widgets, w0);
234 if (dlg_focus (h))
235 return;
236 /* try find another widget that can take focus */
237 dir = SELECT_NEXT;
238 /* fallthrough */
239 case SELECT_NEXT:
240 h->current = dlg_widget_next (h, h->current);
241 break;
242 case SELECT_PREV:
243 h->current = dlg_widget_prev (h, h->current);
244 break;
247 while (h->current != w /* && (WIDGET (h->current->data)->options & W_DISABLED) == 0 */ );
249 if (dlg_overlap (w0, WIDGET (h->current->data)))
251 send_message (h->current->data, NULL, MSG_DRAW, 0, NULL);
252 send_message (h->current->data, NULL, MSG_FOCUS, 0, NULL);
256 /* --------------------------------------------------------------------------------------------- */
258 static void
259 refresh_cmd (void)
261 #ifdef HAVE_SLANG
262 tty_touch_screen ();
263 mc_refresh ();
264 #else
265 /* Use this if the refreshes fail */
266 clr_scr ();
267 repaint_screen ();
268 #endif /* HAVE_SLANG */
271 /* --------------------------------------------------------------------------------------------- */
273 static cb_ret_t
274 dlg_execute_cmd (WDialog * h, unsigned long command)
276 cb_ret_t ret = MSG_HANDLED;
277 switch (command)
279 case CK_Ok:
280 h->ret_value = B_ENTER;
281 dlg_stop (h);
282 break;
283 case CK_Cancel:
284 h->ret_value = B_CANCEL;
285 dlg_stop (h);
286 break;
288 case CK_Up:
289 case CK_Left:
290 dlg_one_up (h);
291 break;
292 case CK_Down:
293 case CK_Right:
294 dlg_one_down (h);
295 break;
297 case CK_Help:
299 ev_help_t event_data = { NULL, h->help_ctx };
300 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
302 break;
304 case CK_Suspend:
305 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
306 refresh_cmd ();
307 break;
308 case CK_Refresh:
309 refresh_cmd ();
310 break;
312 case CK_ScreenList:
313 if (!h->modal)
314 dialog_switch_list ();
315 else
316 ret = MSG_NOT_HANDLED;
317 break;
318 case CK_ScreenNext:
319 if (!h->modal)
320 dialog_switch_next ();
321 else
322 ret = MSG_NOT_HANDLED;
323 break;
324 case CK_ScreenPrev:
325 if (!h->modal)
326 dialog_switch_prev ();
327 else
328 ret = MSG_NOT_HANDLED;
329 break;
331 default:
332 ret = MSG_NOT_HANDLED;
335 return ret;
338 /* --------------------------------------------------------------------------------------------- */
340 static cb_ret_t
341 dlg_handle_key (WDialog * h, int d_key)
343 unsigned long command;
345 command = keybind_lookup_keymap_command (dialog_map, d_key);
347 if (command == CK_IgnoreKey)
348 return MSG_NOT_HANDLED;
350 if (send_message (h, NULL, MSG_ACTION, command, NULL) == MSG_HANDLED
351 || dlg_execute_cmd (h, command) == MSG_HANDLED)
352 return MSG_HANDLED;
354 return MSG_NOT_HANDLED;
357 /* --------------------------------------------------------------------------------------------- */
359 static int
360 dlg_mouse_event (WDialog * h, Gpm_Event * event)
362 Widget *wh = WIDGET (h);
364 GList *p, *first;
366 /* close the dialog by mouse left click out of dialog area */
367 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0)
368 && ((event->type & GPM_DOWN) != 0) && !mouse_global_in_widget (event, wh))
370 h->ret_value = B_CANCEL;
371 dlg_stop (h);
372 return MOU_NORMAL;
375 if (wh->mouse != NULL)
377 int mou;
379 mou = wh->mouse (event, wh);
380 if (mou != MOU_UNHANDLED)
381 return mou;
384 first = h->current;
385 p = first;
389 Widget *w = WIDGET (p->data);
391 p = dlg_widget_prev (h, p);
393 if ((w->options & W_DISABLED) == 0 && w->mouse != NULL)
395 /* put global cursor position to the widget */
396 int ret;
398 ret = w->mouse (event, w);
399 if (ret != MOU_UNHANDLED)
400 return ret;
403 while (p != first);
405 return MOU_UNHANDLED;
408 /* --------------------------------------------------------------------------------------------- */
410 static cb_ret_t
411 dlg_try_hotkey (WDialog * h, int d_key)
413 GList *hot_cur;
414 Widget *current;
415 cb_ret_t handled;
416 int c;
418 if (h->widgets == NULL)
419 return MSG_NOT_HANDLED;
421 if (h->current == NULL)
422 h->current = h->widgets;
425 * Explanation: we don't send letter hotkeys to other widgets if
426 * the currently selected widget is an input line
429 current = WIDGET (h->current->data);
431 if ((current->options & W_DISABLED) != 0)
432 return MSG_NOT_HANDLED;
434 if (current->options & W_IS_INPUT)
436 /* skip ascii control characters, anything else can valid character in
437 * some encoding */
438 if (d_key >= 32 && d_key < 256)
439 return MSG_NOT_HANDLED;
442 /* If it's an alt key, send the message */
443 c = d_key & ~ALT (0);
444 if (d_key & ALT (0) && g_ascii_isalpha (c))
445 d_key = g_ascii_tolower (c);
447 handled = MSG_NOT_HANDLED;
448 if ((current->options & W_WANT_HOTKEY) != 0)
449 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
451 /* If not used, send hotkey to other widgets */
452 if (handled == MSG_HANDLED)
453 return MSG_HANDLED;
455 hot_cur = dlg_widget_next (h, h->current);
457 /* send it to all widgets */
458 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
460 current = WIDGET (hot_cur->data);
462 if ((current->options & W_WANT_HOTKEY) != 0 && (current->options & W_DISABLED) == 0)
463 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
465 if (handled == MSG_NOT_HANDLED)
466 hot_cur = dlg_widget_next (h, hot_cur);
469 if (handled == MSG_HANDLED)
470 do_select_widget (h, hot_cur, SELECT_EXACT);
472 return handled;
475 /* --------------------------------------------------------------------------------------------- */
477 static void
478 dlg_key_event (WDialog * h, int d_key)
480 cb_ret_t handled;
482 if (h->widgets == NULL)
483 return;
485 if (h->current == NULL)
486 h->current = h->widgets;
488 /* TAB used to cycle */
489 if ((h->flags & DLG_WANT_TAB) == 0)
491 if (d_key == '\t')
493 dlg_one_down (h);
494 return;
496 else if (d_key == KEY_BTAB)
498 dlg_one_up (h);
499 return;
503 /* first can dlg_callback handle the key */
504 handled = send_message (h, NULL, MSG_KEY, d_key, NULL);
506 /* next try the hotkey */
507 if (handled == MSG_NOT_HANDLED)
508 handled = dlg_try_hotkey (h, d_key);
510 if (handled == MSG_HANDLED)
511 send_message (h, NULL, MSG_HOTKEY_HANDLED, 0, NULL);
512 else
513 /* not used - then try widget_callback */
514 handled = send_message (h->current->data, NULL, MSG_KEY, d_key, NULL);
516 /* not used- try to use the unhandled case */
517 if (handled == MSG_NOT_HANDLED)
518 handled = send_message (h, NULL, MSG_UNHANDLED_KEY, d_key, NULL);
520 if (handled == MSG_NOT_HANDLED)
521 handled = dlg_handle_key (h, d_key);
523 send_message (h, NULL, MSG_POST_KEY, d_key, NULL);
526 /* --------------------------------------------------------------------------------------------- */
528 static void
529 frontend_run_dlg (WDialog * h)
531 int d_key;
532 Gpm_Event event;
534 event.x = -1;
536 /* close opened editors, viewers, etc */
537 if (!h->modal && mc_global.midnight_shutdown)
539 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
540 return;
543 while (h->state == DLG_ACTIVE)
545 if (mc_global.tty.winch_flag != 0)
546 dialog_change_screen_size ();
548 if (is_idle ())
550 if (idle_hook)
551 execute_hooks (idle_hook);
553 while ((WIDGET (h)->options & W_WANT_IDLE) != 0 && is_idle ())
554 send_message (h, NULL, MSG_IDLE, 0, NULL);
556 /* Allow terminating the dialog from the idle handler */
557 if (h->state != DLG_ACTIVE)
558 break;
561 update_cursor (h);
563 /* Clear interrupt flag */
564 tty_got_interrupt ();
565 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
567 dlg_process_event (h, d_key, &event);
569 if (h->state == DLG_CLOSED)
570 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
574 /* --------------------------------------------------------------------------------------------- */
576 static int
577 dlg_find_widget_by_id (gconstpointer a, gconstpointer b)
579 Widget *w = WIDGET (a);
580 unsigned long id = GPOINTER_TO_UINT (b);
582 return w->id == id ? 0 : 1;
585 /* --------------------------------------------------------------------------------------------- */
586 /*** public functions ****************************************************************************/
587 /* --------------------------------------------------------------------------------------------- */
589 /** Clean the dialog area, draw the frame and the title */
590 void
591 dlg_default_repaint (WDialog * h)
593 Widget *wh = WIDGET (h);
595 int space;
597 if (h->state != DLG_ACTIVE)
598 return;
600 space = (h->flags & DLG_COMPACT) ? 0 : 1;
602 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
603 dlg_erase (h);
604 tty_draw_box (wh->y + space, wh->x + space, wh->lines - 2 * space, wh->cols - 2 * space, FALSE);
606 if (h->title != NULL)
608 tty_setcolor (h->color[DLG_COLOR_TITLE]);
609 widget_move (h, space, (wh->cols - str_term_width1 (h->title)) / 2);
610 tty_print_string (h->title);
614 /* --------------------------------------------------------------------------------------------- */
615 /** this function allows to set dialog position */
617 void
618 dlg_set_position (WDialog * h, int y1, int x1, int y2, int x2)
620 Widget *wh = WIDGET (h);
622 /* save old positions, will be used to reposition childs */
623 int ox, oy, oc, ol;
624 int shift_x, shift_y, scale_x, scale_y;
626 /* save old positions, will be used to reposition childs */
627 ox = wh->x;
628 oy = wh->y;
629 oc = wh->cols;
630 ol = wh->lines;
632 wh->x = x1;
633 wh->y = y1;
634 wh->lines = y2 - y1;
635 wh->cols = x2 - x1;
637 /* dialog is empty */
638 if (h->widgets == NULL)
639 return;
641 if (h->current == NULL)
642 h->current = h->widgets;
644 /* values by which controls should be moved */
645 shift_x = wh->x - ox;
646 shift_y = wh->y - oy;
647 scale_x = wh->cols - oc;
648 scale_y = wh->lines - ol;
650 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
652 GList *w;
654 for (w = h->widgets; w != NULL; w = g_list_next (w))
656 /* there are, mainly, 2 generally possible
657 situations:
659 1. control sticks to one side - it
660 should be moved
662 2. control sticks to two sides of
663 one direction - it should be sized */
665 Widget *c = WIDGET (w->data);
666 int x = c->x;
667 int y = c->y;
668 int cols = c->cols;
669 int lines = c->lines;
671 if ((c->pos_flags & WPOS_CENTER_HORZ) != 0)
672 x = wh->x + (wh->cols - c->cols) / 2;
673 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0 && (c->pos_flags & WPOS_KEEP_RIGHT) != 0)
675 x += shift_x;
676 cols += scale_x;
678 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0)
679 x += shift_x;
680 else if ((c->pos_flags & WPOS_KEEP_RIGHT) != 0)
681 x += shift_x + scale_x;
683 if ((c->pos_flags & WPOS_CENTER_VERT) != 0)
684 y = wh->y + (wh->lines - c->lines) / 2;
685 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0 && (c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
687 y += shift_y;
688 lines += scale_y;
690 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0)
691 y += shift_y;
692 else if ((c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
693 y += shift_y + scale_y;
695 widget_set_size (c, y, x, lines, cols);
700 /* --------------------------------------------------------------------------------------------- */
701 /** this function sets only size, leaving positioning to automatic methods */
703 void
704 dlg_set_size (WDialog * h, int lines, int cols)
706 int x = WIDGET (h)->x;
707 int y = WIDGET (h)->y;
709 if (h->flags & DLG_CENTER)
711 y = (LINES - lines) / 2;
712 x = (COLS - cols) / 2;
715 if ((h->flags & DLG_TRYUP) && (y > 3))
716 y -= 2;
718 dlg_set_position (h, y, x, y + lines, x + cols);
721 /* --------------------------------------------------------------------------------------------- */
722 /** Default dialog callback */
724 cb_ret_t
725 dlg_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
727 WDialog *h = DIALOG (w);
729 (void) sender;
730 (void) parm;
731 (void) data;
733 switch (msg)
735 case MSG_DRAW:
736 if (h->color != NULL)
738 dlg_default_repaint (h);
739 return MSG_HANDLED;
741 return MSG_NOT_HANDLED;
743 case MSG_IDLE:
744 dlg_broadcast_msg_to (h, MSG_IDLE, FALSE, W_WANT_IDLE);
745 return MSG_HANDLED;
747 case MSG_RESIZE:
748 /* this is default resizing mechanism */
749 /* the main idea of this code is to resize dialog
750 according to flags (if any of flags require automatic
751 resizing, like DLG_CENTER, end after that reposition
752 controls in dialog according to flags of widget) */
753 dlg_set_size (h, w->lines, w->cols);
754 return MSG_HANDLED;
756 default:
757 break;
760 return MSG_NOT_HANDLED;
763 /* --------------------------------------------------------------------------------------------- */
765 WDialog *
766 create_dlg (gboolean modal, int y1, int x1, int lines, int cols,
767 const int *colors, widget_cb_fn callback, mouse_h mouse_handler,
768 const char *help_ctx, const char *title, dlg_flags_t flags)
770 WDialog *new_d;
771 Widget *w;
773 new_d = g_new0 (WDialog, 1);
774 w = WIDGET (new_d);
775 init_widget (w, y1, x1, lines, cols, (callback != NULL) ? callback : dlg_default_callback,
776 mouse_handler);
777 widget_want_cursor (w, FALSE);
779 new_d->state = DLG_CONSTRUCT;
780 new_d->modal = modal;
781 if (colors != NULL)
782 memmove (new_d->color, colors, sizeof (dlg_colors_t));
783 new_d->help_ctx = help_ctx;
784 new_d->flags = flags;
785 new_d->data = NULL;
787 dlg_set_size (new_d, lines, cols);
788 new_d->fullscreen = (w->x == 0 && w->y == 0 && w->cols == COLS && w->lines == LINES);
790 new_d->mouse_status = MOU_UNHANDLED;
792 /* Strip existing spaces, add one space before and after the title */
793 if (title != NULL && *title != '\0')
795 char *t;
797 t = g_strstrip (g_strdup (title));
798 if (*t != '\0')
799 new_d->title = g_strdup_printf (" %s ", t);
800 g_free (t);
803 /* unique name got event group for this dialog */
804 new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
806 return new_d;
809 /* --------------------------------------------------------------------------------------------- */
811 void
812 dlg_set_default_colors (void)
814 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
815 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
816 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
817 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
818 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
820 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
821 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
822 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
823 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
824 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
827 /* --------------------------------------------------------------------------------------------- */
829 void
830 dlg_erase (WDialog * h)
832 if ((h != NULL) && (h->state == DLG_ACTIVE))
834 Widget *wh = WIDGET (h);
836 tty_fill_region (wh->y, wh->x, wh->lines, wh->cols, ' ');
840 /* --------------------------------------------------------------------------------------------- */
842 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
845 unsigned long
846 add_widget_autopos (WDialog * h, void *w, widget_pos_flags_t pos_flags, const void *before)
848 Widget *wh = WIDGET (h);
849 Widget *widget;
851 /* Don't accept 0 widgets */
852 if (w == NULL)
853 abort ();
855 widget = WIDGET (w);
857 if ((pos_flags & WPOS_CENTER_HORZ) != 0)
858 widget->x = (wh->cols - widget->cols) / 2;
859 widget->x += wh->x;
861 if ((pos_flags & WPOS_CENTER_VERT) != 0)
862 widget->y = (wh->lines - widget->lines) / 2;
863 widget->y += wh->y;
865 widget->owner = h;
866 widget->pos_flags = pos_flags;
867 widget->id = h->widget_id++;
869 if (h->widgets == NULL || before == NULL)
871 h->widgets = g_list_append (h->widgets, widget);
872 h->current = g_list_last (h->widgets);
874 else
876 GList *b;
878 b = g_list_find (h->widgets, before);
880 /* don't accept widget not from dialog. This shouldn't happen */
881 if (b == NULL)
882 abort ();
884 b = g_list_next (b);
885 h->widgets = g_list_insert_before (h->widgets, b, widget);
886 if (b != NULL)
887 h->current = g_list_previous (b);
888 else
889 h->current = g_list_last (h->widgets);
892 /* widget has been added in runtime */
893 if (h->state == DLG_ACTIVE)
895 send_message (widget, NULL, MSG_INIT, 0, NULL);
896 send_message (widget, NULL, MSG_DRAW, 0, NULL);
897 send_message (widget, NULL, MSG_FOCUS, 0, NULL);
900 return widget->id;
903 /* --------------------------------------------------------------------------------------------- */
904 /** wrapper to simply add lefttop positioned controls */
906 unsigned long
907 add_widget (WDialog * h, void *w)
909 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT,
910 h->current != NULL ? h->current->data : NULL);
913 /* --------------------------------------------------------------------------------------------- */
915 unsigned long
916 add_widget_before (WDialog * h, void *w, void *before)
918 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT, before);
921 /* --------------------------------------------------------------------------------------------- */
923 /** delete widget from dialog */
924 void
925 del_widget (void *w)
927 WDialog *h;
928 GList *d;
930 /* Don't accept NULL widget. This shouldn't happen */
931 if (w == NULL)
932 abort ();
934 h = WIDGET (w)->owner;
936 d = g_list_find (h->widgets, w);
937 if (d == h->current)
938 h->current = dlg_widget_next (h, d);
940 h->widgets = g_list_remove_link (h->widgets, d);
941 send_message (d->data, NULL, MSG_DESTROY, 0, NULL);
942 g_free (d->data);
943 g_list_free_1 (d);
945 /* widget has been deleted in runtime */
946 if (h->state == DLG_ACTIVE)
948 dlg_redraw (h);
949 dlg_focus (h);
953 /* --------------------------------------------------------------------------------------------- */
955 void
956 do_refresh (void)
958 GList *d = top_dlg;
960 if (fast_refresh)
962 if ((d != NULL) && (d->data != NULL))
963 dlg_redraw (DIALOG (d->data));
965 else
967 /* Search first fullscreen dialog */
968 for (; d != NULL; d = g_list_next (d))
969 if (d->data != NULL && DIALOG (d->data)->fullscreen)
970 break;
971 /* back to top dialog */
972 for (; d != NULL; d = g_list_previous (d))
973 if (d->data != NULL)
974 dlg_redraw (DIALOG (d->data));
978 /* --------------------------------------------------------------------------------------------- */
979 /** broadcast a message to all the widgets in a dialog */
981 void
982 dlg_broadcast_msg (WDialog * h, widget_msg_t msg)
984 dlg_broadcast_msg_to (h, msg, FALSE, 0);
987 /* --------------------------------------------------------------------------------------------- */
989 gboolean
990 dlg_focus (WDialog * h)
992 /* cannot focus disabled widget */
993 if ((h->current != NULL) && (h->state == DLG_CONSTRUCT || h->state == DLG_ACTIVE))
995 Widget *current = WIDGET (h->current->data);
997 if (((current->options & W_DISABLED) == 0)
998 && (send_message (current, NULL, MSG_FOCUS, 0, NULL) == MSG_HANDLED))
1000 send_message (h, current, MSG_FOCUS, 0, NULL);
1001 return TRUE;
1005 return FALSE;
1008 /* --------------------------------------------------------------------------------------------- */
1009 /** Return true if the windows overlap */
1012 dlg_overlap (Widget * a, Widget * b)
1014 return !((b->x >= a->x + a->cols)
1015 || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
1019 /* --------------------------------------------------------------------------------------------- */
1020 /** Find the widget with the given callback in the dialog h */
1022 Widget *
1023 find_widget_type (const WDialog * h, widget_cb_fn callback)
1025 GList *w;
1027 w = g_list_find_custom (h->widgets, callback, dlg_find_widget_callback);
1029 return (w == NULL) ? NULL : WIDGET (w->data);
1032 /* --------------------------------------------------------------------------------------------- */
1033 /** Find the widget with the given id */
1035 Widget *
1036 dlg_find_by_id (const WDialog * h, unsigned long id)
1038 GList *w;
1040 w = g_list_find_custom (h->widgets, GUINT_TO_POINTER (id), dlg_find_widget_by_id);
1041 return w != NULL ? WIDGET (w->data) : NULL;
1044 /* --------------------------------------------------------------------------------------------- */
1045 /** Find the widget with the given id in the dialog h and select it */
1047 void
1048 dlg_select_by_id (const WDialog * h, unsigned long id)
1050 Widget *w;
1052 w = dlg_find_by_id (h, id);
1053 if (w != NULL)
1054 dlg_select_widget (w);
1057 /* --------------------------------------------------------------------------------------------- */
1059 * Try to select widget in the dialog.
1062 void
1063 dlg_select_widget (void *w)
1065 Widget *widget = WIDGET (w);
1066 WDialog *h = widget->owner;
1068 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
1071 /* --------------------------------------------------------------------------------------------- */
1074 * Set widget at top of widget list and make it current.
1077 void
1078 dlg_set_top_widget (void *w)
1080 Widget *widget = WIDGET (w);
1081 WDialog *h = widget->owner;
1082 GList *l;
1084 l = g_list_find (h->widgets, w);
1085 if (l == NULL)
1086 abort (); /* widget is not in dialog, this should not happen */
1088 /* unfocus prevoius widget and focus current one before widget reordering */
1089 if (h->state == DLG_ACTIVE)
1090 do_select_widget (h, l, SELECT_EXACT);
1092 /* widget reordering */
1093 h->widgets = g_list_remove_link (h->widgets, l);
1094 h->widgets = g_list_concat (h->widgets, l);
1095 h->current = l;
1098 /* --------------------------------------------------------------------------------------------- */
1099 /** Try to select previous widget in the tab order */
1101 void
1102 dlg_one_up (WDialog * h)
1104 if (h->widgets != NULL)
1105 do_select_widget (h, dlg_widget_prev (h, h->current), SELECT_PREV);
1108 /* --------------------------------------------------------------------------------------------- */
1109 /** Try to select next widget in the tab order */
1111 void
1112 dlg_one_down (WDialog * h)
1114 if (h->widgets != NULL)
1115 do_select_widget (h, dlg_widget_next (h, h->current), SELECT_NEXT);
1118 /* --------------------------------------------------------------------------------------------- */
1120 void
1121 update_cursor (WDialog * h)
1123 GList *p = h->current;
1125 if ((p != NULL) && (h->state == DLG_ACTIVE))
1127 Widget *w;
1129 w = WIDGET (p->data);
1131 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1132 send_message (w, NULL, MSG_CURSOR, 0, NULL);
1133 else
1136 p = dlg_widget_next (h, p);
1137 if (p == h->current)
1138 break;
1140 w = WIDGET (p->data);
1142 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1143 if (send_message (w, NULL, MSG_CURSOR, 0, NULL) == MSG_HANDLED)
1144 break;
1146 while (TRUE);
1150 /* --------------------------------------------------------------------------------------------- */
1152 * Redraw the widgets in reverse order, leaving the current widget
1153 * as the last one
1156 void
1157 dlg_redraw (WDialog * h)
1159 if (h->state != DLG_ACTIVE)
1160 return;
1162 if (h->winch_pending)
1164 h->winch_pending = FALSE;
1165 send_message (h, NULL, MSG_RESIZE, 0, NULL);
1168 send_message (h, NULL, MSG_DRAW, 0, NULL);
1169 dlg_broadcast_msg (h, MSG_DRAW);
1170 update_cursor (h);
1173 /* --------------------------------------------------------------------------------------------- */
1175 void
1176 dlg_stop (WDialog * h)
1178 h->state = DLG_CLOSED;
1181 /* --------------------------------------------------------------------------------------------- */
1182 /** Init the process */
1184 void
1185 init_dlg (WDialog * h)
1187 if (top_dlg != NULL && DIALOG (top_dlg->data)->modal)
1188 h->modal = TRUE;
1190 /* add dialog to the stack */
1191 top_dlg = g_list_prepend (top_dlg, h);
1193 /* Initialize dialog manager and widgets */
1194 if (h->state == DLG_CONSTRUCT)
1196 if (!h->modal)
1197 dialog_switch_add (h);
1199 send_message (h, NULL, MSG_INIT, 0, NULL);
1200 dlg_broadcast_msg (h, MSG_INIT);
1201 dlg_read_history (h);
1204 h->state = DLG_ACTIVE;
1206 /* Select the first widget that takes focus */
1207 while (h->current != NULL && !dlg_focus (h))
1208 h->current = dlg_widget_next (h, h->current);
1210 dlg_redraw (h);
1212 h->ret_value = 0;
1215 /* --------------------------------------------------------------------------------------------- */
1217 void
1218 dlg_process_event (WDialog * h, int key, Gpm_Event * event)
1220 if (key == EV_NONE)
1222 if (tty_got_interrupt ())
1223 if (send_message (h, NULL, MSG_ACTION, CK_Cancel, NULL) != MSG_HANDLED)
1224 dlg_execute_cmd (h, CK_Cancel);
1226 return;
1229 if (key == EV_MOUSE)
1230 h->mouse_status = dlg_mouse_event (h, event);
1231 else
1232 dlg_key_event (h, key);
1235 /* --------------------------------------------------------------------------------------------- */
1236 /** Shutdown the run_dlg */
1238 void
1239 dlg_run_done (WDialog * h)
1241 top_dlg = g_list_remove (top_dlg, h);
1243 if (h->state == DLG_CLOSED)
1245 send_message (h, h->current->data, MSG_END, 0, NULL);
1246 if (!h->modal)
1247 dialog_switch_remove (h);
1251 /* --------------------------------------------------------------------------------------------- */
1253 * Standard run dialog routine
1254 * We have to keep this routine small so that we can duplicate it's
1255 * behavior on complex routines like the file routines, this way,
1256 * they can call the dlg_process_event without rewriting all the code
1260 run_dlg (WDialog * h)
1262 init_dlg (h);
1263 frontend_run_dlg (h);
1264 dlg_run_done (h);
1265 return h->ret_value;
1268 /* --------------------------------------------------------------------------------------------- */
1270 void
1271 destroy_dlg (WDialog * h)
1273 /* if some widgets have history, save all history at one moment here */
1274 dlg_save_history (h);
1275 dlg_broadcast_msg (h, MSG_DESTROY);
1276 g_list_foreach (h->widgets, (GFunc) g_free, NULL);
1277 g_list_free (h->widgets);
1278 mc_event_group_del (h->event_group);
1279 g_free (h->event_group);
1280 g_free (h->title);
1281 g_free (h);
1283 do_refresh ();
1286 /* --------------------------------------------------------------------------------------------- */
1289 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1291 void
1292 dlg_save_history (WDialog * h)
1294 char *profile;
1295 int i;
1297 if (num_history_items_recorded == 0) /* this is how to disable */
1298 return;
1300 profile = mc_config_get_full_path (MC_HISTORY_FILE);
1301 i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
1302 if (i != -1)
1303 close (i);
1305 /* Make sure the history is only readable by the user */
1306 if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
1308 ev_history_load_save_t event_data;
1310 event_data.cfg = mc_config_init (profile, FALSE);
1311 event_data.receiver = NULL;
1313 /* get all histories in dialog */
1314 mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
1316 mc_config_save_file (event_data.cfg, NULL);
1317 mc_config_deinit (event_data.cfg);
1320 g_free (profile);
1323 /* --------------------------------------------------------------------------------------------- */
1325 char *
1326 dlg_get_title (const WDialog * h, size_t len)
1328 char *t;
1330 if (h == NULL)
1331 abort ();
1333 if (h->get_title != NULL)
1334 t = h->get_title (h, len);
1335 else
1336 t = g_strdup ("");
1338 return t;
1341 /* --------------------------------------------------------------------------------------------- */
1342 /** Replace widget old_w for widget new_w in the dialog */
1344 void
1345 dlg_replace_widget (Widget * old_w, Widget * new_w)
1347 WDialog *h = old_w->owner;
1348 gboolean should_focus = FALSE;
1350 if (h->widgets == NULL)
1351 return;
1353 if (h->current == NULL)
1354 h->current = h->widgets;
1356 if (old_w == h->current->data)
1357 should_focus = TRUE;
1359 new_w->owner = h;
1360 new_w->id = old_w->id;
1362 if (should_focus)
1363 h->current->data = new_w;
1364 else
1365 g_list_find (h->widgets, old_w)->data = new_w;
1367 send_message (old_w, NULL, MSG_DESTROY, 0, NULL);
1368 send_message (new_w, NULL, MSG_INIT, 0, NULL);
1370 if (should_focus)
1371 dlg_select_widget (new_w);
1373 widget_redraw (new_w);
1376 /* --------------------------------------------------------------------------------------------- */