(create_dlg): ignore empty string as title.
[midnight-commander.git] / lib / widget / dialog.c
blob745a82b4f18c0645cbcb7341d97335d0b223df23
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
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 (Dlg_head * 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 (Dlg_head * 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 (Dlg_head * 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 (Dlg_head * 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 (Dlg_head * 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, WIDGET_UNFOCUS, 0, NULL) == MSG_HANDLED)
190 h->callback (h, current, DLG_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 (Dlg_head * 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 (WIDGET (h->current->data), NULL, WIDGET_DRAW, 0, NULL);
252 send_message (WIDGET (h->current->data), NULL, WIDGET_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 (Dlg_head * 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 (Dlg_head * 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 (h->callback (h, NULL, DLG_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 (Dlg_head * h, Gpm_Event * event)
362 Widget *wh = WIDGET (h);
364 GList *item;
365 GList *starting_widget = h->current;
366 int x = event->x;
367 int y = event->y;
369 /* close the dialog by mouse click out of dialog area */
370 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0) && ((event->type & GPM_DOWN) != 0) /* left click */
371 && !((x > wh->x) && (x <= wh->x + wh->cols) && (y > wh->y) && (y <= wh->y + wh->lines)))
373 h->ret_value = B_CANCEL;
374 dlg_stop (h);
375 return MOU_NORMAL;
378 if (wh->mouse != NULL)
380 int mou;
382 mou = wh->mouse (event, wh);
383 if (mou != MOU_UNHANDLED)
384 return mou;
387 item = starting_widget;
390 Widget *widget = WIDGET (item->data);
392 if ((h->flags & DLG_REVERSE) == 0)
393 item = dlg_widget_prev (h, item);
394 else
395 item = dlg_widget_next (h, item);
397 if ((widget->options & W_DISABLED) == 0 && widget->mouse != NULL)
399 /* put global cursor position to the widget */
400 int ret;
402 ret = widget->mouse (event, widget);
403 if (ret != MOU_UNHANDLED)
404 return ret;
407 while (item != starting_widget);
409 return MOU_UNHANDLED;
412 /* --------------------------------------------------------------------------------------------- */
414 static cb_ret_t
415 dlg_try_hotkey (Dlg_head * h, int d_key)
417 GList *hot_cur;
418 Widget *current;
419 cb_ret_t handled;
420 int c;
422 if (h->widgets == NULL)
423 return MSG_NOT_HANDLED;
425 if (h->current == NULL)
426 h->current = h->widgets;
429 * Explanation: we don't send letter hotkeys to other widgets if
430 * the currently selected widget is an input line
433 current = WIDGET (h->current->data);
435 if ((current->options & W_DISABLED) != 0)
436 return MSG_NOT_HANDLED;
438 if (current->options & W_IS_INPUT)
440 /* skip ascii control characters, anything else can valid character in
441 * some encoding */
442 if (d_key >= 32 && d_key < 256)
443 return MSG_NOT_HANDLED;
446 /* If it's an alt key, send the message */
447 c = d_key & ~ALT (0);
448 if (d_key & ALT (0) && g_ascii_isalpha (c))
449 d_key = g_ascii_tolower (c);
451 handled = MSG_NOT_HANDLED;
452 if ((current->options & W_WANT_HOTKEY) != 0)
453 handled = send_message (current, NULL, WIDGET_HOTKEY, d_key, NULL);
455 /* If not used, send hotkey to other widgets */
456 if (handled == MSG_HANDLED)
457 return MSG_HANDLED;
459 hot_cur = dlg_widget_next (h, h->current);
461 /* send it to all widgets */
462 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
464 current = WIDGET (hot_cur->data);
466 if ((current->options & W_WANT_HOTKEY) != 0 && (current->options & W_DISABLED) == 0)
467 handled = send_message (current, NULL, WIDGET_HOTKEY, d_key, NULL);
469 if (handled == MSG_NOT_HANDLED)
470 hot_cur = dlg_widget_next (h, hot_cur);
473 if (handled == MSG_HANDLED)
474 do_select_widget (h, hot_cur, SELECT_EXACT);
476 return handled;
479 /* --------------------------------------------------------------------------------------------- */
481 static void
482 dlg_key_event (Dlg_head * h, int d_key)
484 cb_ret_t handled;
486 if (h->widgets == NULL)
487 return;
489 if (h->current == NULL)
490 h->current = h->widgets;
492 /* TAB used to cycle */
493 if ((h->flags & DLG_WANT_TAB) == 0)
495 if (d_key == '\t')
497 dlg_one_down (h);
498 return;
500 else if (d_key == KEY_BTAB)
502 dlg_one_up (h);
503 return;
507 /* first can dlg_callback handle the key */
508 handled = h->callback (h, NULL, DLG_KEY, d_key, NULL);
510 /* next try the hotkey */
511 if (handled == MSG_NOT_HANDLED)
512 handled = dlg_try_hotkey (h, d_key);
514 if (handled == MSG_HANDLED)
515 h->callback (h, NULL, DLG_HOTKEY_HANDLED, 0, NULL);
516 else
517 /* not used - then try widget_callback */
518 handled = send_message (WIDGET (h->current->data), NULL, WIDGET_KEY, d_key, NULL);
520 /* not used- try to use the unhandled case */
521 if (handled == MSG_NOT_HANDLED)
522 handled = h->callback (h, NULL, DLG_UNHANDLED_KEY, d_key, NULL);
524 if (handled == MSG_NOT_HANDLED)
525 handled = dlg_handle_key (h, d_key);
527 h->callback (h, NULL, DLG_POST_KEY, d_key, NULL);
530 /* --------------------------------------------------------------------------------------------- */
532 static void
533 frontend_run_dlg (Dlg_head * h)
535 int d_key;
536 Gpm_Event event;
538 event.x = -1;
540 /* close opened editors, viewers, etc */
541 if (!h->modal && mc_global.midnight_shutdown)
543 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
544 return;
547 while (h->state == DLG_ACTIVE)
549 if (mc_global.tty.winch_flag != 0)
550 dialog_change_screen_size ();
552 if (is_idle ())
554 if (idle_hook)
555 execute_hooks (idle_hook);
557 while ((h->flags & DLG_WANT_IDLE) && is_idle ())
558 h->callback (h, NULL, DLG_IDLE, 0, NULL);
560 /* Allow terminating the dialog from the idle handler */
561 if (h->state != DLG_ACTIVE)
562 break;
565 update_cursor (h);
567 /* Clear interrupt flag */
568 tty_got_interrupt ();
569 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
571 dlg_process_event (h, d_key, &event);
573 if (h->state == DLG_CLOSED)
574 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
578 /* --------------------------------------------------------------------------------------------- */
580 static int
581 dlg_find_widget_by_id (gconstpointer a, gconstpointer b)
583 Widget *w = WIDGET (a);
584 unsigned long id = GPOINTER_TO_UINT (b);
586 return w->id == id ? 0 : 1;
589 /* --------------------------------------------------------------------------------------------- */
590 /*** public functions ****************************************************************************/
591 /* --------------------------------------------------------------------------------------------- */
593 /** draw box in window */
594 void
595 draw_box (const Dlg_head * h, int y, int x, int ys, int xs, gboolean single)
597 const Widget *wh = WIDGET (h);
599 tty_draw_box (wh->y + y, wh->x + x, ys, xs, single);
602 /* --------------------------------------------------------------------------------------------- */
604 /** Clean the dialog area, draw the frame and the title */
605 void
606 common_dialog_repaint (Dlg_head * h)
608 Widget *wh = WIDGET (h);
610 int space;
612 if (h->state != DLG_ACTIVE)
613 return;
615 space = (h->flags & DLG_COMPACT) ? 0 : 1;
617 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
618 dlg_erase (h);
619 draw_box (h, space, space, wh->lines - 2 * space, wh->cols - 2 * space, FALSE);
621 if (h->title != NULL)
623 tty_setcolor (h->color[DLG_COLOR_TITLE]);
624 widget_move (h, space, (wh->cols - str_term_width1 (h->title)) / 2);
625 tty_print_string (h->title);
629 /* --------------------------------------------------------------------------------------------- */
630 /** this function allows to set dialog position */
632 void
633 dlg_set_position (Dlg_head * h, int y1, int x1, int y2, int x2)
635 Widget *wh = WIDGET (h);
637 /* save old positions, will be used to reposition childs */
638 int ox, oy, oc, ol;
639 int shift_x, shift_y, scale_x, scale_y;
641 /* save old positions, will be used to reposition childs */
642 ox = wh->x;
643 oy = wh->y;
644 oc = wh->cols;
645 ol = wh->lines;
647 wh->x = x1;
648 wh->y = y1;
649 wh->lines = y2 - y1;
650 wh->cols = x2 - x1;
652 /* dialog is empty */
653 if (h->widgets == NULL)
654 return;
656 if (h->current == NULL)
657 h->current = h->widgets;
659 /* values by which controls should be moved */
660 shift_x = wh->x - ox;
661 shift_y = wh->y - oy;
662 scale_x = wh->cols - oc;
663 scale_y = wh->lines - ol;
665 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
667 GList *w;
669 for (w = h->widgets; w != NULL; w = g_list_next (w))
671 /* there are, mainly, 2 generally possible
672 situations:
674 1. control sticks to one side - it
675 should be moved
677 2. control sticks to two sides of
678 one direction - it should be sized */
680 Widget *c = WIDGET (w->data);
681 int x = c->x;
682 int y = c->y;
683 int cols = c->cols;
684 int lines = c->lines;
686 if ((c->pos_flags & WPOS_CENTER_HORZ) != 0)
687 x = wh->x + (wh->cols - c->cols) / 2;
688 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0 && (c->pos_flags & WPOS_KEEP_RIGHT) != 0)
690 x += shift_x;
691 cols += scale_x;
693 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0)
694 x += shift_x;
695 else if ((c->pos_flags & WPOS_KEEP_RIGHT) != 0)
696 x += shift_x + scale_x;
698 if ((c->pos_flags & WPOS_CENTER_VERT) != 0)
699 y = wh->y + (wh->lines - c->lines) / 2;
700 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0 && (c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
702 y += shift_y;
703 lines += scale_y;
705 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0)
706 y += shift_y;
707 else if ((c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
708 y += shift_y + scale_y;
710 widget_set_size (c, y, x, lines, cols);
715 /* --------------------------------------------------------------------------------------------- */
716 /** this function sets only size, leaving positioning to automatic methods */
718 void
719 dlg_set_size (Dlg_head * h, int lines, int cols)
721 int x = WIDGET (h)->x;
722 int y = WIDGET (h)->y;
724 if (h->flags & DLG_CENTER)
726 y = (LINES - lines) / 2;
727 x = (COLS - cols) / 2;
730 if ((h->flags & DLG_TRYUP) && (y > 3))
731 y -= 2;
733 dlg_set_position (h, y, x, y + lines, x + cols);
736 /* --------------------------------------------------------------------------------------------- */
737 /** Default dialog callback */
739 cb_ret_t
740 default_dlg_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
742 (void) sender;
743 (void) parm;
744 (void) data;
746 switch (msg)
748 case DLG_DRAW:
749 if (h->color != NULL)
751 common_dialog_repaint (h);
752 return MSG_HANDLED;
754 return MSG_NOT_HANDLED;
756 case DLG_IDLE:
757 dlg_broadcast_msg_to (h, WIDGET_IDLE, FALSE, W_WANT_IDLE);
758 return MSG_HANDLED;
760 case DLG_RESIZE:
761 /* this is default resizing mechanism */
762 /* the main idea of this code is to resize dialog
763 according to flags (if any of flags require automatic
764 resizing, like DLG_CENTER, end after that reposition
765 controls in dialog according to flags of widget) */
766 dlg_set_size (h, WIDGET (h)->lines, WIDGET (h)->cols);
767 return MSG_HANDLED;
769 default:
770 break;
773 return MSG_NOT_HANDLED;
776 /* --------------------------------------------------------------------------------------------- */
778 Dlg_head *
779 create_dlg (gboolean modal, int y1, int x1, int lines, int cols,
780 const int *colors, dlg_cb_fn callback, mouse_h mouse_handler,
781 const char *help_ctx, const char *title, dlg_flags_t flags)
783 Dlg_head *new_d;
784 Widget *w;
786 new_d = g_new0 (Dlg_head, 1);
787 w = WIDGET (new_d);
788 init_widget (w, y1, x1, lines, cols, NULL, mouse_handler);
789 widget_want_cursor (w, FALSE);
791 new_d->state = DLG_CONSTRUCT;
792 new_d->modal = modal;
793 if (colors != NULL)
794 memmove (new_d->color, colors, sizeof (dlg_colors_t));
795 new_d->help_ctx = help_ctx;
796 new_d->callback = (callback != NULL) ? callback : default_dlg_callback;
797 new_d->flags = flags;
798 new_d->data = NULL;
800 dlg_set_size (new_d, lines, cols);
801 new_d->fullscreen = (w->x == 0 && w->y == 0 && w->cols == COLS && w->lines == LINES);
803 new_d->mouse_status = MOU_UNHANDLED;
805 /* Strip existing spaces, add one space before and after the title */
806 if (title != NULL && *title != '\0')
808 char *t;
810 t = g_strstrip (g_strdup (title));
811 if (*t != '\0')
812 new_d->title = g_strdup_printf (" %s ", t);
813 g_free (t);
816 /* unique name got event group for this dialog */
817 new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
819 return new_d;
822 /* --------------------------------------------------------------------------------------------- */
824 void
825 dlg_set_default_colors (void)
827 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
828 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
829 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
830 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
831 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
833 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
834 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
835 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
836 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
837 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
840 /* --------------------------------------------------------------------------------------------- */
842 void
843 dlg_erase (Dlg_head * h)
845 if ((h != NULL) && (h->state == DLG_ACTIVE))
847 Widget *wh = WIDGET (h);
849 tty_fill_region (wh->y, wh->x, wh->lines, wh->cols, ' ');
853 /* --------------------------------------------------------------------------------------------- */
855 void
856 set_idle_proc (Dlg_head * d, int enable)
858 if (enable)
859 d->flags |= DLG_WANT_IDLE;
860 else
861 d->flags &= ~DLG_WANT_IDLE;
864 /* --------------------------------------------------------------------------------------------- */
866 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
869 unsigned long
870 add_widget_autopos (Dlg_head * h, void *w, widget_pos_flags_t pos_flags, const void *before)
872 Widget *wh = WIDGET (h);
873 Widget *widget = WIDGET (w);
875 /* Don't accept 0 widgets */
876 if (w == NULL)
877 abort ();
879 if ((pos_flags & WPOS_CENTER_HORZ) != 0)
880 widget->x = (wh->cols - widget->cols) / 2;
881 widget->x += wh->x;
883 if ((pos_flags & WPOS_CENTER_VERT) != 0)
884 widget->y = (wh->lines - widget->lines) / 2;
885 widget->y += wh->y;
887 widget->owner = h;
888 widget->pos_flags = pos_flags;
889 widget->id = h->widget_id++;
891 if ((h->flags & DLG_REVERSE) != 0)
893 if (h->widgets == NULL || before == NULL)
895 h->widgets = g_list_prepend (h->widgets, widget);
896 h->current = h->widgets;
898 else
900 GList *b;
902 b = g_list_find (h->widgets, before);
904 /* don't accept widget not from dialog. This shouldn't happen */
905 if (b == NULL)
906 abort ();
908 h->widgets = g_list_insert_before (h->widgets, b, widget);
909 h->current = g_list_previous (b);
912 else
914 if (h->widgets == NULL || before == NULL)
916 h->widgets = g_list_append (h->widgets, widget);
917 h->current = g_list_last (h->widgets);
919 else
921 GList *b;
923 b = g_list_find (h->widgets, before);
925 /* don't accept widget not from dialog. This shouldn't happen */
926 if (b == NULL)
927 abort ();
929 b = g_list_next (b);
930 h->widgets = g_list_insert_before (h->widgets, b, widget);
931 if (b != NULL)
932 h->current = g_list_previous (b);
933 else
934 h->current = g_list_last (h->widgets);
938 /* widget has been added in runtime */
939 if (h->state == DLG_ACTIVE)
941 send_message (widget, NULL, WIDGET_INIT, 0, NULL);
942 send_message (widget, NULL, WIDGET_DRAW, 0, NULL);
943 send_message (widget, NULL, WIDGET_FOCUS, 0, NULL);
946 return widget->id;
949 /* --------------------------------------------------------------------------------------------- */
950 /** wrapper to simply add lefttop positioned controls */
952 unsigned long
953 add_widget (Dlg_head * h, void *w)
955 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT,
956 h->current != NULL ? h->current->data : NULL);
959 /* --------------------------------------------------------------------------------------------- */
961 unsigned long
962 add_widget_before (Dlg_head * h, void *w, void *before)
964 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT, before);
967 /* --------------------------------------------------------------------------------------------- */
969 /** delete widget from dialog */
970 void
971 del_widget (void *w)
973 Dlg_head *h = WIDGET (w)->owner;
974 GList *d;
976 /* Don't accept NULL widget. This shouldn't happen */
977 if (w == NULL)
978 abort ();
980 d = g_list_find (h->widgets, w);
981 if (d == h->current)
983 if ((h->flags & DLG_REVERSE) != 0)
984 h->current = dlg_widget_prev (h, d);
985 else
986 h->current = dlg_widget_next (h, d);
989 h->widgets = g_list_remove_link (h->widgets, d);
990 send_message (d->data, NULL, WIDGET_DESTROY, 0, NULL);
991 g_list_free_1 (d);
993 /* widget has been deleted in runtime */
994 if (h->state == DLG_ACTIVE)
996 dlg_redraw (h);
997 dlg_focus (h);
1001 /* --------------------------------------------------------------------------------------------- */
1003 void
1004 do_refresh (void)
1006 GList *d = top_dlg;
1008 if (fast_refresh)
1010 if ((d != NULL) && (d->data != NULL))
1011 dlg_redraw ((Dlg_head *) d->data);
1013 else
1015 /* Search first fullscreen dialog */
1016 for (; d != NULL; d = g_list_next (d))
1017 if ((d->data != NULL) && ((Dlg_head *) d->data)->fullscreen)
1018 break;
1019 /* back to top dialog */
1020 for (; d != NULL; d = g_list_previous (d))
1021 if (d->data != NULL)
1022 dlg_redraw ((Dlg_head *) d->data);
1026 /* --------------------------------------------------------------------------------------------- */
1027 /** broadcast a message to all the widgets in a dialog */
1029 void
1030 dlg_broadcast_msg (Dlg_head * h, widget_msg_t msg, gboolean reverse)
1032 dlg_broadcast_msg_to (h, msg, reverse, 0);
1035 /* --------------------------------------------------------------------------------------------- */
1037 gboolean
1038 dlg_focus (Dlg_head * h)
1040 /* cannot focus disabled widget */
1041 if ((h->current != NULL) && (h->state == DLG_CONSTRUCT || h->state == DLG_ACTIVE))
1043 Widget *current = WIDGET (h->current->data);
1045 if (((current->options & W_DISABLED) == 0)
1046 && (send_message (current, NULL, WIDGET_FOCUS, 0, NULL) == MSG_HANDLED))
1048 h->callback (h, current, DLG_FOCUS, 0, NULL);
1049 return TRUE;
1053 return FALSE;
1056 /* --------------------------------------------------------------------------------------------- */
1057 /** Return true if the windows overlap */
1060 dlg_overlap (Widget * a, Widget * b)
1062 return !((b->x >= a->x + a->cols)
1063 || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
1067 /* --------------------------------------------------------------------------------------------- */
1068 /** Find the widget with the given callback in the dialog h */
1070 Widget *
1071 find_widget_type (const Dlg_head * h, widget_cb_fn callback)
1073 GList *w;
1075 w = g_list_find_custom (h->widgets, callback, dlg_find_widget_callback);
1077 return (w == NULL) ? NULL : WIDGET (w->data);
1080 /* --------------------------------------------------------------------------------------------- */
1081 /** Find the widget with the given id */
1083 Widget *
1084 dlg_find_by_id (const Dlg_head * h, unsigned long id)
1086 GList *w;
1088 w = g_list_find_custom (h->widgets, GUINT_TO_POINTER (id), dlg_find_widget_by_id);
1089 return w != NULL ? WIDGET (w->data) : NULL;
1092 /* --------------------------------------------------------------------------------------------- */
1093 /** Find the widget with the given id in the dialog h and select it */
1095 void
1096 dlg_select_by_id (const Dlg_head * h, unsigned long id)
1098 Widget *w;
1100 w = dlg_find_by_id (h, id);
1101 if (w != NULL)
1102 dlg_select_widget (w);
1105 /* --------------------------------------------------------------------------------------------- */
1107 * Try to select widget in the dialog.
1110 void
1111 dlg_select_widget (void *w)
1113 Widget *widget = WIDGET (w);
1114 Dlg_head *h = widget->owner;
1116 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
1119 /* --------------------------------------------------------------------------------------------- */
1122 * Set widget at top of widget list and make it current.
1125 void
1126 dlg_set_top_widget (void *w)
1128 Widget *widget = WIDGET (w);
1129 Dlg_head *h = widget->owner;
1130 GList *l;
1132 l = g_list_find (h->widgets, w);
1133 if (l == NULL)
1134 abort (); /* widget is not in dialog, this should not happen */
1136 /* unfocus prevoius widget and focus current one before widget reordering */
1137 if (h->state == DLG_ACTIVE)
1138 do_select_widget (h, l, SELECT_EXACT);
1140 /* widget reordering */
1141 h->widgets = g_list_remove_link (h->widgets, l);
1142 if ((h->flags & DLG_REVERSE) != 0)
1143 h->widgets = g_list_concat (l, h->widgets);
1144 else
1145 h->widgets = g_list_concat (h->widgets, l);
1146 h->current = l;
1149 /* --------------------------------------------------------------------------------------------- */
1150 /** Try to select previous widget in the tab order */
1152 void
1153 dlg_one_up (Dlg_head * h)
1155 if (h->widgets != NULL)
1156 do_select_widget (h, dlg_widget_prev (h, h->current), SELECT_PREV);
1159 /* --------------------------------------------------------------------------------------------- */
1160 /** Try to select next widget in the tab order */
1162 void
1163 dlg_one_down (Dlg_head * h)
1165 if (h->widgets != NULL)
1166 do_select_widget (h, dlg_widget_next (h, h->current), SELECT_NEXT);
1169 /* --------------------------------------------------------------------------------------------- */
1171 void
1172 update_cursor (Dlg_head * h)
1174 GList *p = h->current;
1176 if ((p != NULL) && (h->state == DLG_ACTIVE))
1178 Widget *w;
1180 w = WIDGET (p->data);
1182 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1183 send_message (w, NULL, WIDGET_CURSOR, 0, NULL);
1184 else
1187 p = dlg_widget_next (h, p);
1188 if (p == h->current)
1189 break;
1191 w = WIDGET (p->data);
1193 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1194 if (send_message (w, NULL, WIDGET_CURSOR, 0, NULL) == MSG_HANDLED)
1195 break;
1197 while (TRUE);
1201 /* --------------------------------------------------------------------------------------------- */
1203 * Redraw the widgets in reverse order, leaving the current widget
1204 * as the last one
1207 void
1208 dlg_redraw (Dlg_head * h)
1210 if (h->state != DLG_ACTIVE)
1211 return;
1213 if (h->winch_pending)
1215 h->winch_pending = FALSE;
1216 h->callback (h, NULL, DLG_RESIZE, 0, NULL);
1219 h->callback (h, NULL, DLG_DRAW, 0, NULL);
1220 dlg_broadcast_msg (h, WIDGET_DRAW, (h->flags & DLG_REVERSE) != 0);
1221 update_cursor (h);
1224 /* --------------------------------------------------------------------------------------------- */
1226 void
1227 dlg_stop (Dlg_head * h)
1229 h->state = DLG_CLOSED;
1232 /* --------------------------------------------------------------------------------------------- */
1233 /** Init the process */
1235 void
1236 init_dlg (Dlg_head * h)
1238 if ((top_dlg != NULL) && ((Dlg_head *) top_dlg->data)->modal)
1239 h->modal = TRUE;
1241 /* add dialog to the stack */
1242 top_dlg = g_list_prepend (top_dlg, h);
1244 /* Initialize dialog manager and widgets */
1245 if (h->state == DLG_CONSTRUCT)
1247 if (!h->modal)
1248 dialog_switch_add (h);
1250 h->callback (h, NULL, DLG_INIT, 0, NULL);
1251 dlg_broadcast_msg (h, WIDGET_INIT, FALSE);
1252 dlg_read_history (h);
1255 h->state = DLG_ACTIVE;
1257 /* Select the first widget that takes focus */
1258 while (h->current != NULL && !dlg_focus (h))
1259 h->current = dlg_widget_next (h, h->current);
1261 dlg_redraw (h);
1263 h->ret_value = 0;
1266 /* --------------------------------------------------------------------------------------------- */
1268 void
1269 dlg_process_event (Dlg_head * h, int key, Gpm_Event * event)
1271 if (key == EV_NONE)
1273 if (tty_got_interrupt ())
1274 if (h->callback (h, NULL, DLG_ACTION, CK_Cancel, NULL) != MSG_HANDLED)
1275 dlg_execute_cmd (h, CK_Cancel);
1277 return;
1280 if (key == EV_MOUSE)
1281 h->mouse_status = dlg_mouse_event (h, event);
1282 else
1283 dlg_key_event (h, key);
1286 /* --------------------------------------------------------------------------------------------- */
1287 /** Shutdown the run_dlg */
1289 void
1290 dlg_run_done (Dlg_head * h)
1292 top_dlg = g_list_remove (top_dlg, h);
1294 if (h->state == DLG_CLOSED)
1296 h->callback (h, WIDGET (h->current->data), DLG_END, 0, NULL);
1297 if (!h->modal)
1298 dialog_switch_remove (h);
1303 /* --------------------------------------------------------------------------------------------- */
1305 * Standard run dialog routine
1306 * We have to keep this routine small so that we can duplicate it's
1307 * behavior on complex routines like the file routines, this way,
1308 * they can call the dlg_process_event without rewriting all the code
1312 run_dlg (Dlg_head * h)
1314 init_dlg (h);
1315 frontend_run_dlg (h);
1316 dlg_run_done (h);
1317 return h->ret_value;
1320 /* --------------------------------------------------------------------------------------------- */
1322 void
1323 destroy_dlg (Dlg_head * h)
1325 /* if some widgets have history, save all history at one moment here */
1326 dlg_save_history (h);
1327 dlg_broadcast_msg (h, WIDGET_DESTROY, FALSE);
1328 g_list_foreach (h->widgets, (GFunc) g_free, NULL);
1329 g_list_free (h->widgets);
1330 mc_event_group_del (h->event_group);
1331 g_free (h->event_group);
1332 g_free (h->title);
1333 g_free (h);
1335 do_refresh ();
1338 /* --------------------------------------------------------------------------------------------- */
1341 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1343 void
1344 dlg_save_history (Dlg_head * h)
1346 char *profile;
1347 int i;
1349 if (num_history_items_recorded == 0) /* this is how to disable */
1350 return;
1352 profile = mc_config_get_full_path (MC_HISTORY_FILE);
1353 i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
1354 if (i != -1)
1355 close (i);
1357 /* Make sure the history is only readable by the user */
1358 if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
1360 ev_history_load_save_t event_data;
1362 event_data.cfg = mc_config_init (profile, FALSE);
1363 event_data.receiver = NULL;
1365 /* get all histories in dialog */
1366 mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
1368 mc_config_save_file (event_data.cfg, NULL);
1369 mc_config_deinit (event_data.cfg);
1372 g_free (profile);
1375 /* --------------------------------------------------------------------------------------------- */
1377 char *
1378 dlg_get_title (const Dlg_head * h, size_t len)
1380 char *t;
1382 if (h == NULL)
1383 abort ();
1385 if (h->get_title != NULL)
1386 t = h->get_title (h, len);
1387 else
1388 t = g_strdup ("");
1390 return t;
1393 /* --------------------------------------------------------------------------------------------- */
1394 /** Replace widget old_w for widget new_w in the dialog */
1396 void
1397 dlg_replace_widget (Widget * old_w, Widget * new_w)
1399 Dlg_head *h = old_w->owner;
1400 gboolean should_focus = FALSE;
1402 if (h->widgets == NULL)
1403 return;
1405 if (h->current == NULL)
1406 h->current = h->widgets;
1408 if (old_w == h->current->data)
1409 should_focus = TRUE;
1411 new_w->owner = h;
1412 new_w->id = old_w->id;
1414 if (should_focus)
1415 h->current->data = new_w;
1416 else
1417 g_list_find (h->widgets, old_w)->data = new_w;
1419 send_message (old_w, NULL, WIDGET_DESTROY, 0, NULL);
1420 send_message (new_w, NULL, WIDGET_INIT, 0, NULL);
1422 if (should_focus)
1423 dlg_select_widget (new_w);
1425 if (new_w->owner->state == DLG_ACTIVE)
1426 send_message (new_w, NULL, WIDGET_DRAW, 0, NULL);
1429 /* --------------------------------------------------------------------------------------------- */