Optimization of history save.
[midnight-commander.git] / lib / widget / dialog.c
blobef49660a7c249e945f9779d6d82aa5884c10329b
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 <errno.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h> /* open() */
35 #include "lib/global.h"
37 #include "lib/tty/tty.h"
38 #include "lib/skin.h"
39 #include "lib/tty/mouse.h"
40 #include "lib/tty/key.h"
41 #include "lib/strutil.h"
42 #include "lib/widget.h"
43 #include "lib/fileloc.h" /* MC_HISTORY_FILE */
44 #include "lib/event.h" /* mc_event_raise() */
46 /*** global variables ****************************************************************************/
48 /* Color styles for normal and error dialogs */
49 dlg_colors_t dialog_colors;
50 dlg_colors_t alarm_colors;
52 /* Primitive way to check if the the current dialog is our dialog */
53 /* This is needed by async routines like load_prompt */
54 GList *top_dlg = NULL;
56 /* A hook list for idle events */
57 hook_t *idle_hook = NULL;
59 /* If set then dialogs just clean the screen when refreshing, else */
60 /* they do a complete refresh, refreshing all the parts of the program */
61 int fast_refresh = 0;
63 /* left click outside of dialog closes it */
64 int mouse_close_dialog = 0;
66 const global_keymap_t *dialog_map;
68 /*** file scope macro definitions ****************************************************************/
70 /*** file scope type declarations ****************************************************************/
72 /** What to do if the requested widget doesn't take focus */
73 typedef enum
75 SELECT_NEXT, /* go the the next widget */
76 SELECT_PREV, /* go the the previous widget */
77 SELECT_EXACT /* use current widget */
78 } select_dir_t;
80 /*** file scope variables ************************************************************************/
82 /*** file scope functions ************************************************************************/
83 /* --------------------------------------------------------------------------------------------- */
84 /**
85 * broadcast a message to all the widgets in a dialog that have
86 * the options set to flags. If flags is zero, the message is sent
87 * to all widgets.
90 static void
91 dlg_broadcast_msg_to (Dlg_head * h, widget_msg_t msg, gboolean reverse, int flags)
93 GList *p, *first;
95 if (h->widgets == NULL)
96 return;
98 if (h->current == NULL)
99 h->current = h->widgets;
101 if (reverse)
103 p = g_list_previous (h->current);
105 if (p == NULL)
106 p = g_list_last (h->widgets);
108 else
110 p = g_list_next (h->current);
112 if (p == NULL)
113 p = h->widgets;
116 first = p;
120 Widget *w = (Widget *) p->data;
122 if (reverse)
124 p = g_list_previous (p);
126 if (p == NULL)
127 p = g_list_last (h->widgets);
129 else
131 p = g_list_next (p);
133 if (p == NULL)
134 p = h->widgets;
137 if ((flags == 0) || ((flags & w->options) != 0))
138 send_message (w, msg, 0);
140 while (first != p);
143 /* --------------------------------------------------------------------------------------------- */
145 static int
146 dlg_unfocus (Dlg_head * h)
148 /* ... but can unfocus disabled widget */
150 if ((h->current != NULL) && (h->state == DLG_ACTIVE))
152 Widget *current = (Widget *) h->current->data;
154 if (send_message (current, WIDGET_UNFOCUS, 0) == MSG_HANDLED)
156 h->callback (h, current, DLG_UNFOCUS, 0, NULL);
157 return 1;
161 return 0;
164 /* --------------------------------------------------------------------------------------------- */
166 static int
167 dlg_find_widget_callback (const void *a, const void *b)
169 const Widget *w = (const Widget *) a;
170 callback_fn f = (callback_fn) b;
172 return (w->callback == f) ? 0 : 1;
175 /* --------------------------------------------------------------------------------------------- */
177 * Try to select another widget. If forward is set, follow tab order.
178 * Otherwise go to the previous widget.
181 static void
182 do_select_widget (Dlg_head * h, GList * w, select_dir_t dir)
184 Widget *w0 = (Widget *) h->current->data;
186 if (!dlg_unfocus (h))
187 return;
189 h->current = w;
193 if (dlg_focus (h))
194 break;
196 switch (dir)
198 case SELECT_EXACT:
199 h->current = g_list_find (h->widgets, w0);
200 if (dlg_focus (h))
201 return;
202 /* try find another widget that can take focus */
203 dir = SELECT_NEXT;
204 /* fallthrough */
205 case SELECT_NEXT:
206 h->current = g_list_next (h->current);
207 if (h->current == NULL)
208 h->current = h->widgets;
209 break;
210 case SELECT_PREV:
211 h->current = g_list_previous (h->current);
212 if (h->current == NULL)
213 h->current = g_list_last (h->widgets);
214 break;
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_Ok:
250 h->ret_value = B_ENTER;
251 dlg_stop (h);
252 break;
253 case CK_Cancel:
254 h->ret_value = B_CANCEL;
255 dlg_stop (h);
256 break;
258 case CK_Up:
259 case CK_Left:
260 dlg_one_up (h);
261 break;
262 case CK_Down:
263 case CK_Right:
264 dlg_one_down (h);
265 break;
267 case CK_Help:
269 ev_help_t event_data = { NULL, h->help_ctx };
270 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
271 do_refresh ();
273 break;
275 case CK_Suspend:
276 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
277 refresh_cmd ();
278 break;
279 case CK_Refresh:
280 refresh_cmd ();
281 break;
283 case CK_ScreenList:
284 if (!h->modal)
285 dialog_switch_list ();
286 else
287 ret = MSG_NOT_HANDLED;
288 break;
289 case CK_ScreenNext:
290 if (!h->modal)
291 dialog_switch_next ();
292 else
293 ret = MSG_NOT_HANDLED;
294 break;
295 case CK_ScreenPrev:
296 if (!h->modal)
297 dialog_switch_prev ();
298 else
299 ret = MSG_NOT_HANDLED;
300 break;
302 default:
303 ret = MSG_NOT_HANDLED;
306 return ret;
309 /* --------------------------------------------------------------------------------------------- */
311 static cb_ret_t
312 dlg_handle_key (Dlg_head * h, int d_key)
314 unsigned long command;
315 command = keybind_lookup_keymap_command (dialog_map, d_key);
316 if ((command == CK_IgnoreKey) || (dlg_execute_cmd (h, command) == MSG_NOT_HANDLED))
317 return MSG_NOT_HANDLED;
318 else
319 return MSG_HANDLED;
322 /* --------------------------------------------------------------------------------------------- */
324 static int
325 dlg_mouse_event (Dlg_head * h, Gpm_Event * event)
327 GList *item;
328 GList *starting_widget = h->current;
329 Gpm_Event new_event;
330 int x = event->x;
331 int y = event->y;
333 /* close the dialog by mouse click out of dialog area */
334 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0) && ((event->type & GPM_DOWN) != 0) /* left click */
335 && !((x > h->x) && (x <= h->x + h->cols) && (y > h->y) && (y <= h->y + h->lines)))
337 h->ret_value = B_CANCEL;
338 dlg_stop (h);
339 return MOU_NORMAL;
342 item = starting_widget;
345 Widget *widget;
347 widget = (Widget *) item->data;
348 item = g_list_next (item);
349 if (item == NULL)
350 item = h->widgets;
352 if (((widget->options & W_DISABLED) == 0)
353 && (x > widget->x) && (x <= widget->x + widget->cols)
354 && (y > widget->y) && (y <= widget->y + widget->lines))
356 new_event = *event;
357 new_event.x -= widget->x;
358 new_event.y -= widget->y;
360 if (widget->mouse != NULL)
361 return widget->mouse (&new_event, widget);
364 while (item != starting_widget);
366 return MOU_NORMAL;
369 /* --------------------------------------------------------------------------------------------- */
371 static cb_ret_t
372 dlg_try_hotkey (Dlg_head * h, int d_key)
374 GList *hot_cur;
375 Widget *current;
376 cb_ret_t handled;
377 int c;
379 if (h->widgets == NULL)
380 return MSG_NOT_HANDLED;
382 if (h->current == NULL)
383 h->current = h->widgets;
386 * Explanation: we don't send letter hotkeys to other widgets if
387 * the currently selected widget is an input line
390 current = (Widget *) h->current->data;
392 if ((current->options & W_DISABLED) != 0)
393 return MSG_NOT_HANDLED;
395 if (current->options & W_IS_INPUT)
397 /* skip ascii control characters, anything else can valid character in
398 * some encoding */
399 if (d_key >= 32 && d_key < 256)
400 return MSG_NOT_HANDLED;
403 /* If it's an alt key, send the message */
404 c = d_key & ~ALT (0);
405 if (d_key & ALT (0) && g_ascii_isalpha (c))
406 d_key = g_ascii_tolower (c);
408 handled = MSG_NOT_HANDLED;
409 if ((current->options & W_WANT_HOTKEY) != 0)
410 handled = send_message (current, WIDGET_HOTKEY, d_key);
412 /* If not used, send hotkey to other widgets */
413 if (handled == MSG_HANDLED)
414 return MSG_HANDLED;
416 hot_cur = g_list_next (h->current);
417 if (hot_cur == NULL)
418 hot_cur = h->widgets;
420 /* send it to all widgets */
421 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
423 current = (Widget *) hot_cur->data;
425 if ((current->options & W_WANT_HOTKEY) != 0)
426 handled = send_message (current, WIDGET_HOTKEY, d_key);
428 if (handled == MSG_NOT_HANDLED)
430 hot_cur = g_list_next (hot_cur);
431 if (hot_cur == NULL)
432 hot_cur = h->widgets;
436 if (handled == MSG_HANDLED)
437 do_select_widget (h, hot_cur, SELECT_EXACT);
439 return handled;
442 /* --------------------------------------------------------------------------------------------- */
444 static void
445 dlg_key_event (Dlg_head * h, int d_key)
447 cb_ret_t handled;
449 if (h->widgets == NULL)
450 return;
452 if (h->current == NULL)
453 h->current = h->widgets;
455 /* TAB used to cycle */
456 if ((h->flags & DLG_WANT_TAB) == 0)
458 if (d_key == '\t')
460 dlg_one_down (h);
461 return;
463 else if (d_key == KEY_BTAB)
465 dlg_one_up (h);
466 return;
470 /* first can dlg_callback handle the key */
471 handled = h->callback (h, NULL, DLG_KEY, d_key, NULL);
473 /* next try the hotkey */
474 if (handled == MSG_NOT_HANDLED)
475 handled = dlg_try_hotkey (h, d_key);
477 if (handled == MSG_HANDLED)
478 h->callback (h, NULL, DLG_HOTKEY_HANDLED, 0, NULL);
479 else
480 /* not used - then try widget_callback */
481 handled = send_message ((Widget *) h->current->data, WIDGET_KEY, d_key);
483 /* not used- try to use the unhandled case */
484 if (handled == MSG_NOT_HANDLED)
485 handled = h->callback (h, NULL, DLG_UNHANDLED_KEY, d_key, NULL);
487 if (handled == MSG_NOT_HANDLED)
488 handled = dlg_handle_key (h, d_key);
490 h->callback (h, NULL, DLG_POST_KEY, d_key, NULL);
493 /* --------------------------------------------------------------------------------------------- */
495 static void
496 frontend_run_dlg (Dlg_head * h)
498 int d_key;
499 Gpm_Event event;
501 event.x = -1;
503 /* close opened editors, viewers, etc */
504 if (!h->modal && mc_global.widget.midnight_shutdown)
506 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
507 return;
510 while (h->state == DLG_ACTIVE)
512 if (mc_global.tty.winch_flag)
513 dialog_change_screen_size ();
515 if (is_idle ())
517 if (idle_hook)
518 execute_hooks (idle_hook);
520 while ((h->flags & DLG_WANT_IDLE) && is_idle ())
521 h->callback (h, NULL, DLG_IDLE, 0, NULL);
523 /* Allow terminating the dialog from the idle handler */
524 if (h->state != DLG_ACTIVE)
525 break;
528 update_cursor (h);
530 /* Clear interrupt flag */
531 tty_got_interrupt ();
532 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
534 dlg_process_event (h, d_key, &event);
536 if (h->state == DLG_CLOSED)
537 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
541 /* --------------------------------------------------------------------------------------------- */
542 /*** public functions ****************************************************************************/
543 /* --------------------------------------------------------------------------------------------- */
545 /** draw box in window */
546 void
547 draw_box (Dlg_head * h, int y, int x, int ys, int xs, gboolean single)
549 tty_draw_box (h->y + y, h->x + x, ys, xs, single);
552 /* --------------------------------------------------------------------------------------------- */
554 /** Clean the dialog area, draw the frame and the title */
555 void
556 common_dialog_repaint (Dlg_head * h)
558 int space;
560 if (h->state != DLG_ACTIVE)
561 return;
563 space = (h->flags & DLG_COMPACT) ? 0 : 1;
565 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
566 dlg_erase (h);
567 draw_box (h, space, space, h->lines - 2 * space, h->cols - 2 * space, FALSE);
569 if (h->title != NULL)
571 tty_setcolor (h->color[DLG_COLOR_TITLE]);
572 dlg_move (h, space, (h->cols - str_term_width1 (h->title)) / 2);
573 tty_print_string (h->title);
577 /* --------------------------------------------------------------------------------------------- */
578 /** this function allows to set dialog position */
580 void
581 dlg_set_position (Dlg_head * h, int y1, int x1, int y2, int x2)
583 /* save old positions, will be used to reposition childs */
584 int ox, oy, oc, ol;
585 int shift_x, shift_y, scale_x, scale_y;
587 /* save old positions, will be used to reposition childs */
588 ox = h->x;
589 oy = h->y;
590 oc = h->cols;
591 ol = h->lines;
593 h->x = x1;
594 h->y = y1;
595 h->lines = y2 - y1;
596 h->cols = x2 - x1;
598 /* dialog is empty */
599 if (h->widgets == NULL)
600 return;
602 if (h->current == NULL)
603 h->current = h->widgets;
605 /* values by which controls should be moved */
606 shift_x = h->x - ox;
607 shift_y = h->y - oy;
608 scale_x = h->cols - oc;
609 scale_y = h->lines - ol;
611 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
613 GList *w;
615 for (w = h->widgets; w != NULL; w = g_list_next (w))
617 /* there are, mainly, 2 generally possible
618 situations:
620 1. control sticks to one side - it
621 should be moved
623 2. control sticks to two sides of
624 one direction - it should be sized */
626 Widget *c = (Widget *) w->data;
627 int x = c->x;
628 int y = c->y;
629 int cols = c->cols;
630 int lines = c->lines;
632 if ((c->pos_flags & WPOS_KEEP_LEFT) && (c->pos_flags & WPOS_KEEP_RIGHT))
634 x += shift_x;
635 cols += scale_x;
637 else if (c->pos_flags & WPOS_KEEP_LEFT)
638 x += shift_x;
639 else if (c->pos_flags & WPOS_KEEP_RIGHT)
640 x += shift_x + scale_x;
642 if ((c->pos_flags & WPOS_KEEP_TOP) && (c->pos_flags & WPOS_KEEP_BOTTOM))
644 y += shift_y;
645 lines += scale_y;
647 else if (c->pos_flags & WPOS_KEEP_TOP)
648 y += shift_y;
649 else if (c->pos_flags & WPOS_KEEP_BOTTOM)
650 y += shift_y + scale_y;
652 widget_set_size (c, y, x, lines, cols);
657 /* --------------------------------------------------------------------------------------------- */
658 /** this function sets only size, leaving positioning to automatic methods */
660 void
661 dlg_set_size (Dlg_head * h, int lines, int cols)
663 int x = h->x;
664 int y = h->y;
666 if (h->flags & DLG_CENTER)
668 y = (LINES - lines) / 2;
669 x = (COLS - cols) / 2;
672 if ((h->flags & DLG_TRYUP) && (y > 3))
673 y -= 2;
675 dlg_set_position (h, y, x, y + lines, x + cols);
678 /* --------------------------------------------------------------------------------------------- */
679 /** Default dialog callback */
681 cb_ret_t
682 default_dlg_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
684 (void) sender;
685 (void) parm;
686 (void) data;
688 switch (msg)
690 case DLG_DRAW:
691 if (h->color != NULL)
693 common_dialog_repaint (h);
694 return MSG_HANDLED;
696 return MSG_NOT_HANDLED;
698 case DLG_IDLE:
699 dlg_broadcast_msg_to (h, WIDGET_IDLE, FALSE, W_WANT_IDLE);
700 return MSG_HANDLED;
702 case DLG_RESIZE:
703 /* this is default resizing mechanism */
704 /* the main idea of this code is to resize dialog
705 according to flags (if any of flags require automatic
706 resizing, like DLG_CENTER, end after that reposition
707 controls in dialog according to flags of widget) */
708 dlg_set_size (h, h->lines, h->cols);
709 return MSG_HANDLED;
711 default:
712 break;
715 return MSG_NOT_HANDLED;
718 /* --------------------------------------------------------------------------------------------- */
720 Dlg_head *
721 create_dlg (gboolean modal, int y1, int x1, int lines, int cols,
722 const int *colors, dlg_cb_fn callback, const char *help_ctx,
723 const char *title, dlg_flags_t flags)
725 Dlg_head *new_d;
727 new_d = g_new0 (Dlg_head, 1);
728 new_d->modal = modal;
729 if (colors != NULL)
730 memmove (new_d->color, colors, sizeof (dlg_colors_t));
731 new_d->help_ctx = help_ctx;
732 new_d->callback = (callback != NULL) ? callback : default_dlg_callback;
733 new_d->x = x1;
734 new_d->y = y1;
735 new_d->flags = flags;
736 new_d->data = NULL;
738 dlg_set_size (new_d, lines, cols);
739 new_d->fullscreen = (new_d->x == 0 && new_d->y == 0
740 && new_d->cols == COLS && new_d->lines == LINES);
742 new_d->mouse_status = MOU_NORMAL;
744 /* Strip existing spaces, add one space before and after the title */
745 if (title != NULL)
747 char *t;
749 t = g_strstrip (g_strdup (title));
750 if (*t != '\0')
751 new_d->title = g_strdup_printf (" %s ", t);
752 g_free (t);
755 /* unique name got event group for this dialog */
756 new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
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 if (h->widgets != NULL)
927 GList *w;
929 for (w = h->widgets; w != NULL; w = g_list_next (w))
930 if (((Widget *) w->data)->id == id)
931 return (Widget *) w->data;
934 return NULL;
937 /* --------------------------------------------------------------------------------------------- */
938 /** Find the widget with the given id in the dialog h and select it */
940 void
941 dlg_select_by_id (const Dlg_head * h, unsigned int id)
943 Widget *w;
945 w = dlg_find_by_id (h, id);
946 if (w != NULL)
947 dlg_select_widget (w);
950 /* --------------------------------------------------------------------------------------------- */
952 * Try to select widget in the dialog.
955 void
956 dlg_select_widget (void *w)
958 const Widget *widget = (Widget *) w;
959 Dlg_head *h = widget->owner;
961 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
964 /* --------------------------------------------------------------------------------------------- */
965 /** Try to select previous widget in the tab order */
967 void
968 dlg_one_up (Dlg_head * h)
970 if (h->widgets != NULL)
972 GList *prev;
974 prev = g_list_previous (h->current);
975 if (prev == NULL)
976 prev = g_list_last (h->widgets);
978 do_select_widget (h, prev, SELECT_PREV);
982 /* --------------------------------------------------------------------------------------------- */
983 /** Try to select next widget in the tab order */
985 void
986 dlg_one_down (Dlg_head * h)
988 if (h->widgets != NULL)
990 GList *next;
992 next = g_list_next (h->current);
993 if (next == NULL)
994 next = h->widgets;
995 do_select_widget (h, next, SELECT_NEXT);
999 /* --------------------------------------------------------------------------------------------- */
1001 void
1002 update_cursor (Dlg_head * h)
1004 GList *p = h->current;
1006 if ((p != NULL) && (h->state == DLG_ACTIVE))
1008 Widget *w;
1010 w = (Widget *) p->data;
1012 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1013 send_message (w, WIDGET_CURSOR, 0);
1014 else
1017 p = g_list_next (p);
1018 if (p == NULL)
1019 p = h->widgets;
1021 if (p == h->current)
1022 break;
1024 w = (Widget *) p->data;
1026 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1027 if (send_message (w, WIDGET_CURSOR, 0) == MSG_HANDLED)
1028 break;
1030 while (TRUE);
1034 /* --------------------------------------------------------------------------------------------- */
1036 * Redraw the widgets in reverse order, leaving the current widget
1037 * as the last one
1040 void
1041 dlg_redraw (Dlg_head * h)
1043 if (h->state != DLG_ACTIVE)
1044 return;
1046 if (h->winch_pending)
1048 h->winch_pending = FALSE;
1049 h->callback (h, NULL, DLG_RESIZE, 0, NULL);
1052 h->callback (h, NULL, DLG_DRAW, 0, NULL);
1053 dlg_broadcast_msg (h, WIDGET_DRAW, TRUE);
1054 update_cursor (h);
1057 /* --------------------------------------------------------------------------------------------- */
1059 void
1060 dlg_stop (Dlg_head * h)
1062 h->state = DLG_CLOSED;
1065 /* --------------------------------------------------------------------------------------------- */
1066 /** Init the process */
1068 void
1069 init_dlg (Dlg_head * h)
1071 if ((top_dlg != NULL) && ((Dlg_head *) top_dlg->data)->modal)
1072 h->modal = TRUE;
1074 /* add dialog to the stack */
1075 top_dlg = g_list_prepend (top_dlg, h);
1077 /* Initialize dialog manager and widgets */
1078 if (h->state == DLG_ACTIVE)
1080 if (!h->modal)
1081 dialog_switch_add (h);
1083 h->callback (h, NULL, DLG_INIT, 0, NULL);
1084 dlg_broadcast_msg (h, WIDGET_INIT, FALSE);
1087 h->state = DLG_ACTIVE;
1089 dlg_redraw (h);
1091 /* Select the first widget that takes focus */
1092 while (h->current != NULL && !dlg_focus (h))
1094 h->current = g_list_next (h->current);
1095 if (h->current == NULL)
1096 h->current = h->widgets;
1099 h->ret_value = 0;
1102 /* --------------------------------------------------------------------------------------------- */
1104 void
1105 dlg_process_event (Dlg_head * h, int key, Gpm_Event * event)
1107 if (key == EV_NONE)
1109 if (tty_got_interrupt ())
1110 if (h->callback (h, NULL, DLG_ACTION, CK_Cancel, NULL) != MSG_HANDLED)
1111 dlg_execute_cmd (h, CK_Cancel);
1113 return;
1116 if (key == EV_MOUSE)
1117 h->mouse_status = dlg_mouse_event (h, event);
1118 else
1119 dlg_key_event (h, key);
1122 /* --------------------------------------------------------------------------------------------- */
1123 /** Shutdown the run_dlg */
1125 void
1126 dlg_run_done (Dlg_head * h)
1128 top_dlg = g_list_remove (top_dlg, h);
1130 if (h->state == DLG_CLOSED)
1132 h->callback (h, (Widget *) h->current->data, DLG_END, 0, NULL);
1133 if (!h->modal)
1134 dialog_switch_remove (h);
1139 /* --------------------------------------------------------------------------------------------- */
1141 * Standard run dialog routine
1142 * We have to keep this routine small so that we can duplicate it's
1143 * behavior on complex routines like the file routines, this way,
1144 * they can call the dlg_process_event without rewriting all the code
1148 run_dlg (Dlg_head * h)
1150 init_dlg (h);
1151 frontend_run_dlg (h);
1152 dlg_run_done (h);
1153 return h->ret_value;
1156 /* --------------------------------------------------------------------------------------------- */
1158 void
1159 destroy_dlg (Dlg_head * h)
1161 /* if some widgets have history, save all history at one moment here */
1162 dlg_save_history (h);
1163 dlg_broadcast_msg (h, WIDGET_DESTROY, FALSE);
1164 g_list_foreach (h->widgets, (GFunc) g_free, NULL);
1165 g_list_free (h->widgets);
1166 mc_event_group_del (h->event_group);
1167 g_free (h->event_group);
1168 g_free (h->title);
1169 g_free (h);
1171 do_refresh ();
1174 /* --------------------------------------------------------------------------------------------- */
1177 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1179 void
1180 dlg_save_history (Dlg_head * h)
1182 char *profile;
1183 int i;
1185 if (num_history_items_recorded == 0) /* this is how to disable */
1186 return;
1188 profile = g_build_filename (mc_config_get_cache_path (), MC_HISTORY_FILE, (char *) NULL);
1189 i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
1190 if (i != -1)
1191 close (i);
1193 /* Make sure the history is only readable by the user */
1194 if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
1196 ev_history_load_save_t event_data;
1198 event_data.cfg = mc_config_init (profile);
1199 event_data.receiver = NULL;
1201 /* get all histories in dialog */
1202 mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
1204 mc_config_save_file (event_data.cfg, NULL);
1205 mc_config_deinit (event_data.cfg);
1208 g_free (profile);
1211 /* --------------------------------------------------------------------------------------------- */
1213 char *
1214 dlg_get_title (const Dlg_head * h, size_t len)
1216 char *t;
1218 if (h == NULL)
1219 abort ();
1221 if (h->get_title != NULL)
1222 t = h->get_title (h, len);
1223 else
1224 t = g_strdup ("");
1226 return t;
1229 /* --------------------------------------------------------------------------------------------- */
1230 /** Replace widget old_w for widget new_w in the dialog */
1232 void
1233 dlg_replace_widget (Widget * old_w, Widget * new_w)
1235 Dlg_head *h = old_w->owner;
1236 gboolean should_focus = FALSE;
1238 if (h->widgets == NULL)
1239 return;
1241 if (h->current == NULL)
1242 h->current = h->widgets;
1244 if (old_w == h->current->data)
1245 should_focus = TRUE;
1247 new_w->owner = h;
1248 new_w->id = old_w->id;
1250 if (should_focus)
1251 h->current->data = new_w;
1252 else
1253 g_list_find (h->widgets, old_w)->data = new_w;
1255 send_message (old_w, WIDGET_DESTROY, 0);
1256 send_message (new_w, WIDGET_INIT, 0);
1258 if (should_focus)
1259 dlg_select_widget (new_w);
1261 send_message (new_w, WIDGET_DRAW, 0);
1264 /* --------------------------------------------------------------------------------------------- */