Ticket #3598: cleanup some -Wcast-qual compiler warnings.
[midnight-commander.git] / lib / widget / dialog.c
blobc8dddf91610638c3b1dd582b7f8e2ae1010def1c
1 /*
2 Dialog box features module for the Midnight Commander
4 Copyright (C) 1994-2014
5 Free Software Foundation, Inc.
7 This file is part of the Midnight Commander.
9 The Midnight Commander is free software: you can redistribute it
10 and/or modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation, either version 3 of the License,
12 or (at your option) any later version.
14 The Midnight Commander is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /** \file dialog.c
24 * \brief Source: dialog box features module
27 #include <config.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
37 #include "lib/global.h"
39 #include "lib/tty/tty.h"
40 #include "lib/skin.h"
41 #include "lib/tty/key.h"
42 #include "lib/strutil.h"
43 #include "lib/fileloc.h" /* MC_HISTORY_FILE */
44 #include "lib/event.h" /* mc_event_raise() */
46 #include "lib/widget.h"
47 #include "lib/widget/mouse.h"
49 /*** global variables ****************************************************************************/
51 /* Color styles for normal and error dialogs */
52 dlg_colors_t dialog_colors;
53 dlg_colors_t alarm_colors;
54 dlg_colors_t listbox_colors;
56 /* Primitive way to check if the the current dialog is our dialog */
57 /* This is needed by async routines like load_prompt */
58 GList *top_dlg = NULL;
60 /* A hook list for idle events */
61 hook_t *idle_hook = NULL;
63 /* If set then dialogs just clean the screen when refreshing, else */
64 /* they do a complete refresh, refreshing all the parts of the program */
65 int fast_refresh = 0;
67 /* left click outside of dialog closes it */
68 int mouse_close_dialog = 0;
70 const global_keymap_t *dialog_map = NULL;
72 /*** file scope macro definitions ****************************************************************/
74 /*** file scope type declarations ****************************************************************/
76 /** What to do if the requested widget doesn't take focus */
77 typedef enum
79 SELECT_NEXT, /* go the the next widget */
80 SELECT_PREV, /* go the the previous widget */
81 SELECT_EXACT /* use current widget */
82 } select_dir_t;
84 /*** file scope variables ************************************************************************/
86 /*** file scope functions ************************************************************************/
88 static GList *
89 dlg_widget_next (WDialog * h, GList * l)
91 GList *next;
93 next = g_list_next (l);
94 if (next == NULL)
95 next = h->widgets;
97 return next;
100 /* --------------------------------------------------------------------------------------------- */
102 static GList *
103 dlg_widget_prev (WDialog * h, GList * l)
105 GList *prev;
107 prev = g_list_previous (l);
108 if (prev == NULL)
109 prev = g_list_last (h->widgets);
111 return prev;
114 /* --------------------------------------------------------------------------------------------- */
116 * broadcast a message to all the widgets in a dialog that have
117 * the options set to flags. If flags is zero, the message is sent
118 * to all widgets.
121 static void
122 dlg_broadcast_msg_to (WDialog * h, widget_msg_t msg, gboolean reverse, int flags)
124 GList *p, *first;
126 if (h->widgets == NULL)
127 return;
129 if (h->current == NULL)
130 h->current = h->widgets;
132 if (reverse)
133 p = dlg_widget_prev (h, h->current);
134 else
135 p = dlg_widget_next (h, h->current);
137 first = p;
141 Widget *w = WIDGET (p->data);
143 if (reverse)
144 p = dlg_widget_prev (h, p);
145 else
146 p = dlg_widget_next (h, p);
148 if ((flags == 0) || ((flags & w->options) != 0))
149 send_message (w, NULL, msg, 0, NULL);
151 while (first != p);
154 /* --------------------------------------------------------------------------------------------- */
157 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
159 static void
160 dlg_read_history (WDialog * h)
162 char *profile;
163 ev_history_load_save_t event_data;
165 if (num_history_items_recorded == 0) /* this is how to disable */
166 return;
168 profile = mc_config_get_full_path (MC_HISTORY_FILE);
169 event_data.cfg = mc_config_init (profile, TRUE);
170 event_data.receiver = NULL;
172 /* create all histories in dialog */
173 mc_event_raise (h->event_group, MCEVENT_HISTORY_LOAD, &event_data);
175 mc_config_deinit (event_data.cfg);
176 g_free (profile);
179 /* --------------------------------------------------------------------------------------------- */
181 static gboolean
182 dlg_unfocus (WDialog * h)
184 /* we can unfocus disabled widget */
185 if ((h->current != NULL) && (h->state == DLG_CONSTRUCT || h->state == DLG_ACTIVE))
187 Widget *current = WIDGET (h->current->data);
189 if (send_message (current, NULL, MSG_UNFOCUS, 0, NULL) == MSG_HANDLED)
191 send_message (h, current, MSG_UNFOCUS, 0, NULL);
192 return TRUE;
196 return FALSE;
199 /* --------------------------------------------------------------------------------------------- */
201 static int
202 dlg_find_widget_callback (const void *a, const void *b)
204 const Widget *w = CONST_WIDGET (a);
205 widget_cb_fn f = (widget_cb_fn) b;
207 return (w->callback == f) ? 0 : 1;
210 /* --------------------------------------------------------------------------------------------- */
212 * Try to select another widget. If forward is set, follow tab order.
213 * Otherwise go to the previous widget.
216 static void
217 do_select_widget (WDialog * h, GList * w, select_dir_t dir)
219 Widget *w0 = WIDGET (h->current->data);
221 if (!dlg_unfocus (h))
222 return;
224 h->current = w;
228 if (dlg_focus (h))
229 break;
231 switch (dir)
233 case SELECT_EXACT:
234 h->current = g_list_find (h->widgets, w0);
235 if (dlg_focus (h))
236 return;
237 /* try find another widget that can take focus */
238 dir = SELECT_NEXT;
239 /* fallthrough */
240 case SELECT_NEXT:
241 h->current = dlg_widget_next (h, h->current);
242 break;
243 case SELECT_PREV:
244 h->current = dlg_widget_prev (h, h->current);
245 break;
246 default:
247 break;
250 while (h->current != w /* && (WIDGET (h->current->data)->options & W_DISABLED) == 0 */ );
252 if (widget_overlapped (w0, WIDGET (h->current->data)))
254 send_message (h->current->data, NULL, MSG_DRAW, 0, NULL);
255 send_message (h->current->data, NULL, MSG_FOCUS, 0, NULL);
259 /* --------------------------------------------------------------------------------------------- */
261 static void
262 refresh_cmd (void)
264 #ifdef HAVE_SLANG
265 tty_touch_screen ();
266 mc_refresh ();
267 #else
268 /* Use this if the refreshes fail */
269 clr_scr ();
270 repaint_screen ();
271 #endif /* HAVE_SLANG */
274 /* --------------------------------------------------------------------------------------------- */
276 static cb_ret_t
277 dlg_execute_cmd (WDialog * h, long command)
279 cb_ret_t ret = MSG_HANDLED;
280 switch (command)
282 case CK_Ok:
283 h->ret_value = B_ENTER;
284 dlg_stop (h);
285 break;
286 case CK_Cancel:
287 h->ret_value = B_CANCEL;
288 dlg_stop (h);
289 break;
291 case CK_Up:
292 case CK_Left:
293 dlg_one_up (h);
294 break;
295 case CK_Down:
296 case CK_Right:
297 dlg_one_down (h);
298 break;
300 case CK_Help:
302 ev_help_t event_data = { NULL, h->help_ctx };
303 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
305 break;
307 case CK_Suspend:
308 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
309 refresh_cmd ();
310 break;
311 case CK_Refresh:
312 refresh_cmd ();
313 break;
315 case CK_ScreenList:
316 if (!h->modal)
317 dialog_switch_list ();
318 else
319 ret = MSG_NOT_HANDLED;
320 break;
321 case CK_ScreenNext:
322 if (!h->modal)
323 dialog_switch_next ();
324 else
325 ret = MSG_NOT_HANDLED;
326 break;
327 case CK_ScreenPrev:
328 if (!h->modal)
329 dialog_switch_prev ();
330 else
331 ret = MSG_NOT_HANDLED;
332 break;
334 default:
335 ret = MSG_NOT_HANDLED;
338 return ret;
341 /* --------------------------------------------------------------------------------------------- */
343 static cb_ret_t
344 dlg_handle_key (WDialog * h, int d_key)
346 long command;
348 command = keybind_lookup_keymap_command (dialog_map, d_key);
350 if (command == CK_IgnoreKey)
351 return MSG_NOT_HANDLED;
353 if (send_message (h, NULL, MSG_ACTION, command, NULL) == MSG_HANDLED
354 || dlg_execute_cmd (h, command) == MSG_HANDLED)
355 return MSG_HANDLED;
357 return MSG_NOT_HANDLED;
360 /* --------------------------------------------------------------------------------------------- */
362 * This is the low-level mouse handler.
363 * It receives a Gpm_Event event and translates it into a higher level protocol.
365 static int
366 dlg_mouse_translator (Gpm_Event * event, Widget * w)
368 mouse_event_t me;
370 me = mouse_translate_event (w, event);
372 return mouse_process_event (w, &me);
375 /* --------------------------------------------------------------------------------------------- */
377 static int
378 dlg_mouse_event (WDialog * h, Gpm_Event * event)
380 Widget *wh = WIDGET (h);
382 GList *p;
384 /* close the dialog by mouse left click out of dialog area */
385 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0)
386 && ((event->type & GPM_DOWN) != 0) && !mouse_global_in_widget (event, wh))
388 h->ret_value = B_CANCEL;
389 dlg_stop (h);
390 return MOU_NORMAL;
393 if (wh->mouse_callback != NULL)
395 int mou;
397 mou = dlg_mouse_translator (event, wh);
398 if (mou != MOU_UNHANDLED)
399 return mou;
402 /* send the event to widgets in reverse Z-order */
403 p = g_list_last (h->widgets);
406 Widget *w = WIDGET (p->data);
408 if ((w->options & W_DISABLED) == 0 && w->mouse_callback != NULL)
410 /* put global cursor position to the widget */
411 int ret;
413 ret = dlg_mouse_translator (event, w);
414 if (ret != MOU_UNHANDLED)
415 return ret;
418 p = g_list_previous (p);
420 while (p != NULL);
422 return MOU_UNHANDLED;
425 /* --------------------------------------------------------------------------------------------- */
427 static cb_ret_t
428 dlg_try_hotkey (WDialog * h, int d_key)
430 GList *hot_cur;
431 Widget *current;
432 cb_ret_t handled;
433 int c;
435 if (h->widgets == NULL)
436 return MSG_NOT_HANDLED;
438 if (h->current == NULL)
439 h->current = h->widgets;
442 * Explanation: we don't send letter hotkeys to other widgets if
443 * the currently selected widget is an input line
446 current = WIDGET (h->current->data);
448 if ((current->options & W_DISABLED) != 0)
449 return MSG_NOT_HANDLED;
451 if (current->options & W_IS_INPUT)
453 /* skip ascii control characters, anything else can valid character in
454 * some encoding */
455 if (d_key >= 32 && d_key < 256)
456 return MSG_NOT_HANDLED;
459 /* If it's an alt key, send the message */
460 c = d_key & ~ALT (0);
461 if (d_key & ALT (0) && g_ascii_isalpha (c))
462 d_key = g_ascii_tolower (c);
464 handled = MSG_NOT_HANDLED;
465 if ((current->options & W_WANT_HOTKEY) != 0)
466 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
468 /* If not used, send hotkey to other widgets */
469 if (handled == MSG_HANDLED)
470 return MSG_HANDLED;
472 hot_cur = dlg_widget_next (h, h->current);
474 /* send it to all widgets */
475 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
477 current = WIDGET (hot_cur->data);
479 if ((current->options & W_WANT_HOTKEY) != 0 && (current->options & W_DISABLED) == 0)
480 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
482 if (handled == MSG_NOT_HANDLED)
483 hot_cur = dlg_widget_next (h, hot_cur);
486 if (handled == MSG_HANDLED)
487 do_select_widget (h, hot_cur, SELECT_EXACT);
489 return handled;
492 /* --------------------------------------------------------------------------------------------- */
494 static void
495 dlg_key_event (WDialog * h, int d_key)
497 cb_ret_t handled;
499 if (h->widgets == NULL)
500 return;
502 if (h->current == NULL)
503 h->current = h->widgets;
505 /* TAB used to cycle */
506 if ((h->flags & DLG_WANT_TAB) == 0)
508 if (d_key == '\t')
510 dlg_one_down (h);
511 return;
513 else if ((d_key & ~(KEY_M_SHIFT | KEY_M_CTRL)) == '\t')
515 dlg_one_up (h);
516 return;
520 /* first can dlg_callback handle the key */
521 handled = send_message (h, NULL, MSG_KEY, d_key, NULL);
523 /* next try the hotkey */
524 if (handled == MSG_NOT_HANDLED)
525 handled = dlg_try_hotkey (h, d_key);
527 if (handled == MSG_HANDLED)
528 send_message (h, NULL, MSG_HOTKEY_HANDLED, 0, NULL);
529 else
530 /* not used - then try widget_callback */
531 handled = send_message (h->current->data, NULL, MSG_KEY, d_key, NULL);
533 /* not used- try to use the unhandled case */
534 if (handled == MSG_NOT_HANDLED)
535 handled = send_message (h, NULL, MSG_UNHANDLED_KEY, d_key, NULL);
537 if (handled == MSG_NOT_HANDLED)
538 handled = dlg_handle_key (h, d_key);
540 (void) handled;
541 send_message (h, NULL, MSG_POST_KEY, d_key, NULL);
544 /* --------------------------------------------------------------------------------------------- */
546 static void
547 frontend_dlg_run (WDialog * h)
549 Gpm_Event event;
551 event.x = -1;
553 /* close opened editors, viewers, etc */
554 if (!h->modal && mc_global.midnight_shutdown)
556 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
557 return;
560 while (h->state == DLG_ACTIVE)
562 int d_key;
564 if (mc_global.tty.winch_flag != 0)
565 dialog_change_screen_size ();
567 if (is_idle ())
569 if (idle_hook)
570 execute_hooks (idle_hook);
572 while ((WIDGET (h)->options & W_WANT_IDLE) != 0 && is_idle ())
573 send_message (h, NULL, MSG_IDLE, 0, NULL);
575 /* Allow terminating the dialog from the idle handler */
576 if (h->state != DLG_ACTIVE)
577 break;
580 update_cursor (h);
582 /* Clear interrupt flag */
583 tty_got_interrupt ();
584 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
586 dlg_process_event (h, d_key, &event);
588 if (h->state == DLG_CLOSED)
589 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
593 /* --------------------------------------------------------------------------------------------- */
595 static int
596 dlg_find_widget_by_id (gconstpointer a, gconstpointer b)
598 const Widget *w = CONST_WIDGET (a);
599 unsigned long id = GPOINTER_TO_UINT (b);
601 return w->id == id ? 0 : 1;
604 /* --------------------------------------------------------------------------------------------- */
605 /*** public functions ****************************************************************************/
606 /* --------------------------------------------------------------------------------------------- */
608 /** Clean the dialog area, draw the frame and the title */
609 void
610 dlg_default_repaint (WDialog * h)
612 Widget *wh = WIDGET (h);
614 int space;
616 if (h->state != DLG_ACTIVE)
617 return;
619 space = (h->flags & DLG_COMPACT) ? 0 : 1;
621 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
622 dlg_erase (h);
623 tty_draw_box (wh->y + space, wh->x + space, wh->lines - 2 * space, wh->cols - 2 * space, FALSE);
625 if (h->title != NULL)
627 tty_setcolor (h->color[DLG_COLOR_TITLE]);
628 widget_move (h, space, (wh->cols - str_term_width1 (h->title)) / 2);
629 tty_print_string (h->title);
633 /* --------------------------------------------------------------------------------------------- */
634 /** this function allows to set dialog position */
636 void
637 dlg_set_position (WDialog * h, int y1, int x1, int y2, int x2)
639 Widget *wh = WIDGET (h);
641 /* save old positions, will be used to reposition childs */
642 int ox, oy, oc, ol;
643 int shift_x, shift_y, scale_x, scale_y;
645 /* save old positions, will be used to reposition childs */
646 ox = wh->x;
647 oy = wh->y;
648 oc = wh->cols;
649 ol = wh->lines;
651 wh->x = x1;
652 wh->y = y1;
653 wh->lines = y2 - y1;
654 wh->cols = x2 - x1;
656 /* dialog is empty */
657 if (h->widgets == NULL)
658 return;
660 if (h->current == NULL)
661 h->current = h->widgets;
663 /* values by which controls should be moved */
664 shift_x = wh->x - ox;
665 shift_y = wh->y - oy;
666 scale_x = wh->cols - oc;
667 scale_y = wh->lines - ol;
669 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
671 GList *w;
673 for (w = h->widgets; w != NULL; w = g_list_next (w))
675 /* there are, mainly, 2 generally possible
676 situations:
678 1. control sticks to one side - it
679 should be moved
681 2. control sticks to two sides of
682 one direction - it should be sized */
684 Widget *c = WIDGET (w->data);
685 int x = c->x;
686 int y = c->y;
687 int cols = c->cols;
688 int lines = c->lines;
690 if ((c->pos_flags & WPOS_CENTER_HORZ) != 0)
691 x = wh->x + (wh->cols - c->cols) / 2;
692 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0 && (c->pos_flags & WPOS_KEEP_RIGHT) != 0)
694 x += shift_x;
695 cols += scale_x;
697 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0)
698 x += shift_x;
699 else if ((c->pos_flags & WPOS_KEEP_RIGHT) != 0)
700 x += shift_x + scale_x;
702 if ((c->pos_flags & WPOS_CENTER_VERT) != 0)
703 y = wh->y + (wh->lines - c->lines) / 2;
704 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0 && (c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
706 y += shift_y;
707 lines += scale_y;
709 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0)
710 y += shift_y;
711 else if ((c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
712 y += shift_y + scale_y;
714 widget_set_size (c, y, x, lines, cols);
719 /* --------------------------------------------------------------------------------------------- */
720 /** Set dialog size and position */
722 void
723 dlg_set_size (WDialog * h, int lines, int cols)
725 int x = WIDGET (h)->x;
726 int y = WIDGET (h)->y;
728 if ((h->flags & DLG_CENTER) != 0)
730 y = (LINES - lines) / 2;
731 x = (COLS - cols) / 2;
734 if ((h->flags & DLG_TRYUP) != 0)
736 if (y > 3)
737 y -= 2;
738 else if (y == 3)
739 y = 2;
742 dlg_set_position (h, y, x, y + lines, x + cols);
745 /* --------------------------------------------------------------------------------------------- */
746 /** Default dialog callback */
748 cb_ret_t
749 dlg_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
751 WDialog *h = DIALOG (w);
753 (void) sender;
754 (void) parm;
755 (void) data;
757 switch (msg)
759 case MSG_DRAW:
760 if (h->color != NULL)
762 dlg_default_repaint (h);
763 return MSG_HANDLED;
765 return MSG_NOT_HANDLED;
767 case MSG_IDLE:
768 dlg_broadcast_msg_to (h, MSG_IDLE, FALSE, W_WANT_IDLE);
769 return MSG_HANDLED;
771 case MSG_RESIZE:
772 /* this is default resizing mechanism */
773 /* the main idea of this code is to resize dialog
774 according to flags (if any of flags require automatic
775 resizing, like DLG_CENTER, end after that reposition
776 controls in dialog according to flags of widget) */
777 dlg_set_size (h, w->lines, w->cols);
778 return MSG_HANDLED;
780 default:
781 break;
784 return MSG_NOT_HANDLED;
787 /* --------------------------------------------------------------------------------------------- */
789 WDialog *
790 dlg_create (gboolean modal, int y1, int x1, int lines, int cols,
791 const int *colors, widget_cb_fn callback, widget_mouse_cb_fn mouse_callback,
792 const char *help_ctx, const char *title, dlg_flags_t flags)
794 WDialog *new_d;
795 Widget *w;
797 new_d = g_new0 (WDialog, 1);
798 w = WIDGET (new_d);
799 widget_init (w, y1, x1, lines, cols, (callback != NULL) ? callback : dlg_default_callback,
800 mouse_callback);
801 widget_want_cursor (w, FALSE);
803 new_d->state = DLG_CONSTRUCT;
804 new_d->modal = modal;
805 new_d->color = colors;
806 new_d->help_ctx = help_ctx;
807 new_d->flags = flags;
808 new_d->data = NULL;
810 dlg_set_size (new_d, lines, cols);
811 new_d->fullscreen = (w->x == 0 && w->y == 0 && w->cols == COLS && w->lines == LINES);
813 new_d->mouse_status = MOU_UNHANDLED;
815 /* Strip existing spaces, add one space before and after the title */
816 if (title != NULL && *title != '\0')
818 char *t;
820 t = g_strstrip (g_strdup (title));
821 if (*t != '\0')
822 new_d->title = g_strdup_printf (" %s ", t);
823 g_free (t);
826 /* unique name of event group for this dialog */
827 new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
829 return new_d;
832 /* --------------------------------------------------------------------------------------------- */
834 void
835 dlg_set_default_colors (void)
837 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
838 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
839 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
840 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
841 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
843 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
844 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
845 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
846 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
847 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
849 listbox_colors[DLG_COLOR_NORMAL] = PMENU_ENTRY_COLOR;
850 listbox_colors[DLG_COLOR_FOCUS] = PMENU_SELECTED_COLOR;
851 listbox_colors[DLG_COLOR_HOT_NORMAL] = PMENU_ENTRY_COLOR;
852 listbox_colors[DLG_COLOR_HOT_FOCUS] = PMENU_SELECTED_COLOR;
853 listbox_colors[DLG_COLOR_TITLE] = PMENU_TITLE_COLOR;
856 /* --------------------------------------------------------------------------------------------- */
858 void
859 dlg_erase (WDialog * h)
861 if ((h != NULL) && (h->state == DLG_ACTIVE))
863 Widget *wh = WIDGET (h);
865 tty_fill_region (wh->y, wh->x, wh->lines, wh->cols, ' ');
869 /* --------------------------------------------------------------------------------------------- */
871 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
874 unsigned long
875 add_widget_autopos (WDialog * h, void *w, widget_pos_flags_t pos_flags, const void *before)
877 Widget *wh = WIDGET (h);
878 Widget *widget;
880 /* Don't accept 0 widgets */
881 if (w == NULL)
882 abort ();
884 widget = WIDGET (w);
886 if ((pos_flags & WPOS_CENTER_HORZ) != 0)
887 widget->x = (wh->cols - widget->cols) / 2;
888 widget->x += wh->x;
890 if ((pos_flags & WPOS_CENTER_VERT) != 0)
891 widget->y = (wh->lines - widget->lines) / 2;
892 widget->y += wh->y;
894 widget->owner = h;
895 widget->pos_flags = pos_flags;
896 widget->id = h->widget_id++;
898 if (h->widgets == NULL || before == NULL)
900 h->widgets = g_list_append (h->widgets, widget);
901 h->current = g_list_last (h->widgets);
903 else
905 GList *b;
907 b = g_list_find (h->widgets, before);
909 /* don't accept widget not from dialog. This shouldn't happen */
910 if (b == NULL)
911 abort ();
913 b = g_list_next (b);
914 h->widgets = g_list_insert_before (h->widgets, b, widget);
915 if (b != NULL)
916 h->current = g_list_previous (b);
917 else
918 h->current = g_list_last (h->widgets);
921 /* widget has been added in runtime */
922 if (h->state == DLG_ACTIVE)
924 send_message (widget, NULL, MSG_INIT, 0, NULL);
925 send_message (widget, NULL, MSG_DRAW, 0, NULL);
926 send_message (widget, NULL, MSG_FOCUS, 0, NULL);
929 return widget->id;
932 /* --------------------------------------------------------------------------------------------- */
933 /** wrapper to simply add lefttop positioned controls */
935 unsigned long
936 add_widget (WDialog * h, void *w)
938 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT,
939 h->current != NULL ? h->current->data : NULL);
942 /* --------------------------------------------------------------------------------------------- */
944 unsigned long
945 add_widget_before (WDialog * h, void *w, void *before)
947 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT, before);
950 /* --------------------------------------------------------------------------------------------- */
952 /** delete widget from dialog */
953 void
954 del_widget (void *w)
956 WDialog *h;
957 GList *d;
959 /* Don't accept NULL widget. This shouldn't happen */
960 if (w == NULL)
961 abort ();
963 h = WIDGET (w)->owner;
965 d = g_list_find (h->widgets, w);
966 if (d == h->current)
967 h->current = dlg_widget_next (h, d);
969 h->widgets = g_list_remove_link (h->widgets, d);
970 send_message (d->data, NULL, MSG_DESTROY, 0, NULL);
971 g_free (d->data);
972 g_list_free_1 (d);
974 /* widget has been deleted in runtime */
975 if (h->state == DLG_ACTIVE)
977 dlg_redraw (h);
978 dlg_focus (h);
982 /* --------------------------------------------------------------------------------------------- */
984 void
985 do_refresh (void)
987 GList *d = top_dlg;
989 if (fast_refresh)
991 if ((d != NULL) && (d->data != NULL))
992 dlg_redraw (DIALOG (d->data));
994 else
996 /* Search first fullscreen dialog */
997 for (; d != NULL; d = g_list_next (d))
998 if (d->data != NULL && DIALOG (d->data)->fullscreen)
999 break;
1000 /* back to top dialog */
1001 for (; d != NULL; d = g_list_previous (d))
1002 if (d->data != NULL)
1003 dlg_redraw (DIALOG (d->data));
1007 /* --------------------------------------------------------------------------------------------- */
1008 /** broadcast a message to all the widgets in a dialog */
1010 void
1011 dlg_broadcast_msg (WDialog * h, widget_msg_t msg)
1013 dlg_broadcast_msg_to (h, msg, FALSE, 0);
1016 /* --------------------------------------------------------------------------------------------- */
1018 gboolean
1019 dlg_focus (WDialog * h)
1021 /* cannot focus disabled widget */
1022 if ((h->current != NULL) && (h->state == DLG_CONSTRUCT || h->state == DLG_ACTIVE))
1024 Widget *current = WIDGET (h->current->data);
1026 if (((current->options & W_DISABLED) == 0)
1027 && (send_message (current, NULL, MSG_FOCUS, 0, NULL) == MSG_HANDLED))
1029 send_message (h, current, MSG_FOCUS, 0, NULL);
1030 return TRUE;
1034 return FALSE;
1037 /* --------------------------------------------------------------------------------------------- */
1038 /** Find the widget with the given callback in the dialog h */
1040 Widget *
1041 find_widget_type (const WDialog * h, widget_cb_fn callback)
1043 GList *w;
1045 w = g_list_find_custom (h->widgets, callback, dlg_find_widget_callback);
1047 return (w == NULL) ? NULL : WIDGET (w->data);
1050 /* --------------------------------------------------------------------------------------------- */
1051 /** Find the widget with the given id */
1053 Widget *
1054 dlg_find_by_id (const WDialog * h, unsigned long id)
1056 GList *w;
1058 w = g_list_find_custom (h->widgets, GUINT_TO_POINTER (id), dlg_find_widget_by_id);
1059 return w != NULL ? WIDGET (w->data) : NULL;
1062 /* --------------------------------------------------------------------------------------------- */
1063 /** Find the widget with the given id in the dialog h and select it */
1065 void
1066 dlg_select_by_id (const WDialog * h, unsigned long id)
1068 Widget *w;
1070 w = dlg_find_by_id (h, id);
1071 if (w != NULL)
1072 dlg_select_widget (w);
1075 /* --------------------------------------------------------------------------------------------- */
1077 * Try to select widget in the dialog.
1080 void
1081 dlg_select_widget (void *w)
1083 Widget *widget = WIDGET (w);
1084 WDialog *h = widget->owner;
1086 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
1089 /* --------------------------------------------------------------------------------------------- */
1091 static void
1092 dlg_set_top_or_bottom_widget (void *w, gboolean set_top)
1094 Widget *widget = WIDGET (w);
1095 WDialog *h = widget->owner;
1096 GList *l;
1098 l = g_list_find (h->widgets, w);
1099 if (l == NULL)
1100 abort (); /* widget is not in dialog, this should not happen */
1102 /* unfocus prevoius widget and focus current one before widget reordering */
1103 if (set_top && h->state == DLG_ACTIVE)
1104 do_select_widget (h, l, SELECT_EXACT);
1106 /* widget reordering */
1107 h->widgets = g_list_remove_link (h->widgets, l);
1108 if (set_top)
1109 h->widgets = g_list_concat (h->widgets, l);
1110 else
1111 h->widgets = g_list_concat (l, h->widgets);
1114 /* --------------------------------------------------------------------------------------------- */
1116 * Set widget at top of widget list and make it current.
1119 void
1120 dlg_set_top_widget (void *w)
1122 dlg_set_top_or_bottom_widget (w, TRUE);
1125 /* --------------------------------------------------------------------------------------------- */
1127 * Set widget at bottom of widget list.
1130 void
1131 dlg_set_bottom_widget (void *w)
1133 dlg_set_top_or_bottom_widget (w, FALSE);
1136 /* --------------------------------------------------------------------------------------------- */
1137 /** Try to select previous widget in the tab order */
1139 void
1140 dlg_one_up (WDialog * h)
1142 if (h->widgets != NULL)
1143 do_select_widget (h, dlg_widget_prev (h, h->current), SELECT_PREV);
1146 /* --------------------------------------------------------------------------------------------- */
1147 /** Try to select next widget in the tab order */
1149 void
1150 dlg_one_down (WDialog * h)
1152 if (h->widgets != NULL)
1153 do_select_widget (h, dlg_widget_next (h, h->current), SELECT_NEXT);
1156 /* --------------------------------------------------------------------------------------------- */
1158 void
1159 update_cursor (WDialog * h)
1161 GList *p = h->current;
1163 if ((p != NULL) && (h->state == DLG_ACTIVE))
1165 Widget *w;
1167 w = WIDGET (p->data);
1169 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1170 send_message (w, NULL, MSG_CURSOR, 0, NULL);
1171 else
1174 p = dlg_widget_next (h, p);
1175 if (p == h->current)
1176 break;
1178 w = WIDGET (p->data);
1180 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1181 if (send_message (w, NULL, MSG_CURSOR, 0, NULL) == MSG_HANDLED)
1182 break;
1184 while (TRUE);
1188 /* --------------------------------------------------------------------------------------------- */
1190 * Redraw the widgets in reverse order, leaving the current widget
1191 * as the last one
1194 void
1195 dlg_redraw (WDialog * h)
1197 if (h->state != DLG_ACTIVE)
1198 return;
1200 if (h->winch_pending)
1202 h->winch_pending = FALSE;
1203 send_message (h, NULL, MSG_RESIZE, 0, NULL);
1206 send_message (h, NULL, MSG_DRAW, 0, NULL);
1207 dlg_broadcast_msg (h, MSG_DRAW);
1208 update_cursor (h);
1211 /* --------------------------------------------------------------------------------------------- */
1213 void
1214 dlg_stop (WDialog * h)
1216 h->state = DLG_CLOSED;
1219 /* --------------------------------------------------------------------------------------------- */
1220 /** Init the process */
1222 void
1223 dlg_init (WDialog * h)
1225 if (top_dlg != NULL && DIALOG (top_dlg->data)->modal)
1226 h->modal = TRUE;
1228 /* add dialog to the stack */
1229 top_dlg = g_list_prepend (top_dlg, h);
1231 /* Initialize dialog manager and widgets */
1232 if (h->state == DLG_CONSTRUCT)
1234 if (!h->modal)
1235 dialog_switch_add (h);
1237 send_message (h, NULL, MSG_INIT, 0, NULL);
1238 dlg_broadcast_msg (h, MSG_INIT);
1239 dlg_read_history (h);
1242 h->state = DLG_ACTIVE;
1244 /* first send MSG_DRAW to dialog itself and all widgets... */
1245 dlg_redraw (h);
1247 /* ...then send MSG_FOCUS to select the first widget that can take focus */
1248 while (h->current != NULL && !dlg_focus (h))
1249 h->current = dlg_widget_next (h, h->current);
1252 h->ret_value = 0;
1255 /* --------------------------------------------------------------------------------------------- */
1257 void
1258 dlg_process_event (WDialog * h, int key, Gpm_Event * event)
1260 if (key == EV_NONE)
1262 if (tty_got_interrupt ())
1263 if (send_message (h, NULL, MSG_ACTION, CK_Cancel, NULL) != MSG_HANDLED)
1264 dlg_execute_cmd (h, CK_Cancel);
1266 else if (key == EV_MOUSE)
1267 h->mouse_status = dlg_mouse_event (h, event);
1268 else
1269 dlg_key_event (h, key);
1272 /* --------------------------------------------------------------------------------------------- */
1273 /** Shutdown the dlg_run */
1275 void
1276 dlg_run_done (WDialog * h)
1278 top_dlg = g_list_remove (top_dlg, h);
1280 if (h->state == DLG_CLOSED)
1282 send_message (h, h->current->data, MSG_END, 0, NULL);
1283 if (!h->modal)
1284 dialog_switch_remove (h);
1288 /* --------------------------------------------------------------------------------------------- */
1290 * Standard run dialog routine
1291 * We have to keep this routine small so that we can duplicate it's
1292 * behavior on complex routines like the file routines, this way,
1293 * they can call the dlg_process_event without rewriting all the code
1297 dlg_run (WDialog * h)
1299 dlg_init (h);
1300 frontend_dlg_run (h);
1301 dlg_run_done (h);
1302 return h->ret_value;
1305 /* --------------------------------------------------------------------------------------------- */
1307 void
1308 dlg_destroy (WDialog * h)
1310 /* if some widgets have history, save all history at one moment here */
1311 dlg_save_history (h);
1312 dlg_broadcast_msg (h, MSG_DESTROY);
1313 g_list_free_full (h->widgets, g_free);
1314 mc_event_group_del (h->event_group);
1315 g_free (h->event_group);
1316 g_free (h->title);
1317 g_free (h);
1319 do_refresh ();
1322 /* --------------------------------------------------------------------------------------------- */
1325 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1327 void
1328 dlg_save_history (WDialog * h)
1330 char *profile;
1331 int i;
1333 if (num_history_items_recorded == 0) /* this is how to disable */
1334 return;
1336 profile = mc_config_get_full_path (MC_HISTORY_FILE);
1337 i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
1338 if (i != -1)
1339 close (i);
1341 /* Make sure the history is only readable by the user */
1342 if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
1344 ev_history_load_save_t event_data;
1346 event_data.cfg = mc_config_init (profile, FALSE);
1347 event_data.receiver = NULL;
1349 /* get all histories in dialog */
1350 mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
1352 mc_config_save_file (event_data.cfg, NULL);
1353 mc_config_deinit (event_data.cfg);
1356 g_free (profile);
1359 /* --------------------------------------------------------------------------------------------- */
1361 char *
1362 dlg_get_title (const WDialog * h, size_t len)
1364 char *t;
1366 if (h == NULL)
1367 abort ();
1369 if (h->get_title != NULL)
1370 t = h->get_title (h, len);
1371 else
1372 t = g_strdup ("");
1374 return t;
1377 /* --------------------------------------------------------------------------------------------- */