Ticket #3660: unable to view archive content using F3.
[midnight-commander.git] / lib / widget / dialog.c
blob066dc6b05ba2891de14e063761234b8f420ca5f5
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, widget_options_t 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)
187 Widget *wh = WIDGET (h);
189 if (widget_get_state (wh, WST_CONSTRUCT) || widget_get_state (wh, WST_ACTIVE))
191 Widget *current = WIDGET (h->current->data);
193 if (send_message (current, NULL, MSG_UNFOCUS, 0, NULL) == MSG_HANDLED)
195 send_message (h, current, MSG_UNFOCUS, 0, NULL);
196 return TRUE;
202 return FALSE;
205 /* --------------------------------------------------------------------------------------------- */
207 static int
208 dlg_find_widget_callback (const void *a, const void *b)
210 const Widget *w = CONST_WIDGET (a);
211 const widget_cb_fn f = b;
213 return (w->callback == f) ? 0 : 1;
216 /* --------------------------------------------------------------------------------------------- */
219 * Put widget on top or bottom of Z-order.
221 static void
222 dlg_reorder_widgets (GList * l, gboolean set_top)
224 WDialog *h = WIDGET (l->data)->owner;
226 h->widgets = g_list_remove_link (h->widgets, l);
227 if (set_top)
228 h->widgets = g_list_concat (h->widgets, l);
229 else
230 h->widgets = g_list_concat (l, h->widgets);
233 /* --------------------------------------------------------------------------------------------- */
235 * Try to select another widget. If forward is set, follow tab order.
236 * Otherwise go to the previous widget.
239 static void
240 do_select_widget (WDialog * h, GList * w, select_dir_t dir)
242 Widget *w0 = WIDGET (h->current->data);
244 if (!dlg_unfocus (h))
245 return;
247 h->current = w;
251 if (dlg_focus (h))
252 break;
254 switch (dir)
256 case SELECT_EXACT:
257 h->current = g_list_find (h->widgets, w0);
258 if (dlg_focus (h))
259 return;
260 /* try find another widget that can take focus */
261 dir = SELECT_NEXT;
262 /* fallthrough */
263 case SELECT_NEXT:
264 h->current = dlg_widget_next (h, h->current);
265 break;
266 case SELECT_PREV:
267 h->current = dlg_widget_prev (h, h->current);
268 break;
269 default:
270 break;
273 while (h->current != w);
275 if (widget_get_options (WIDGET (h->current->data), WOP_TOP_SELECT))
276 dlg_reorder_widgets (h->current, TRUE);
278 if (widget_overlapped (w0, WIDGET (h->current->data)))
280 send_message (h->current->data, NULL, MSG_DRAW, 0, NULL);
281 send_message (h->current->data, NULL, MSG_FOCUS, 0, NULL);
285 /* --------------------------------------------------------------------------------------------- */
287 static void
288 refresh_cmd (void)
290 #ifdef HAVE_SLANG
291 tty_touch_screen ();
292 mc_refresh ();
293 #else
294 /* Use this if the refreshes fail */
295 clr_scr ();
296 repaint_screen ();
297 #endif /* HAVE_SLANG */
300 /* --------------------------------------------------------------------------------------------- */
302 static cb_ret_t
303 dlg_execute_cmd (WDialog * h, long command)
305 cb_ret_t ret = MSG_HANDLED;
306 switch (command)
308 case CK_Ok:
309 h->ret_value = B_ENTER;
310 dlg_stop (h);
311 break;
312 case CK_Cancel:
313 h->ret_value = B_CANCEL;
314 dlg_stop (h);
315 break;
317 case CK_Up:
318 case CK_Left:
319 dlg_one_up (h);
320 break;
321 case CK_Down:
322 case CK_Right:
323 dlg_one_down (h);
324 break;
326 case CK_Help:
328 ev_help_t event_data = { NULL, h->help_ctx };
329 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
331 break;
333 case CK_Suspend:
334 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
335 refresh_cmd ();
336 break;
337 case CK_Refresh:
338 refresh_cmd ();
339 break;
341 case CK_ScreenList:
342 if (!widget_get_state (WIDGET (h), WST_MODAL))
343 dialog_switch_list ();
344 else
345 ret = MSG_NOT_HANDLED;
346 break;
347 case CK_ScreenNext:
348 if (!widget_get_state (WIDGET (h), WST_MODAL))
349 dialog_switch_next ();
350 else
351 ret = MSG_NOT_HANDLED;
352 break;
353 case CK_ScreenPrev:
354 if (!widget_get_state (WIDGET (h), WST_MODAL))
355 dialog_switch_prev ();
356 else
357 ret = MSG_NOT_HANDLED;
358 break;
360 default:
361 ret = MSG_NOT_HANDLED;
364 return ret;
367 /* --------------------------------------------------------------------------------------------- */
369 static cb_ret_t
370 dlg_handle_key (WDialog * h, int d_key)
372 long command;
374 command = keybind_lookup_keymap_command (dialog_map, d_key);
376 if (command == CK_IgnoreKey)
377 return MSG_NOT_HANDLED;
379 if (send_message (h, NULL, MSG_ACTION, command, NULL) == MSG_HANDLED
380 || dlg_execute_cmd (h, command) == MSG_HANDLED)
381 return MSG_HANDLED;
383 return MSG_NOT_HANDLED;
386 /* --------------------------------------------------------------------------------------------- */
388 * This is the low-level mouse handler.
389 * It receives a Gpm_Event event and translates it into a higher level protocol.
391 static int
392 dlg_mouse_translator (Gpm_Event * event, Widget * w)
394 mouse_event_t me;
396 me = mouse_translate_event (w, event);
398 return mouse_process_event (w, &me);
401 /* --------------------------------------------------------------------------------------------- */
403 static int
404 dlg_mouse_event (WDialog * h, Gpm_Event * event)
406 Widget *wh = WIDGET (h);
408 GList *p;
410 /* close the dialog by mouse left click out of dialog area */
411 if (mouse_close_dialog && (wh->pos_flags & WPOS_FULLSCREEN) == 0
412 && ((event->buttons & GPM_B_LEFT) != 0) && ((event->type & GPM_DOWN) != 0)
413 && !mouse_global_in_widget (event, wh))
415 h->ret_value = B_CANCEL;
416 dlg_stop (h);
417 return MOU_NORMAL;
420 if (wh->mouse_callback != NULL)
422 int mou;
424 mou = dlg_mouse_translator (event, wh);
425 if (mou != MOU_UNHANDLED)
426 return mou;
429 /* send the event to widgets in reverse Z-order */
430 p = g_list_last (h->widgets);
433 Widget *w = WIDGET (p->data);
435 if (!widget_get_state (w, WST_DISABLED) && w->mouse_callback != NULL)
437 /* put global cursor position to the widget */
438 int ret;
440 ret = dlg_mouse_translator (event, w);
441 if (ret != MOU_UNHANDLED)
442 return ret;
445 p = g_list_previous (p);
447 while (p != NULL);
449 return MOU_UNHANDLED;
452 /* --------------------------------------------------------------------------------------------- */
454 static cb_ret_t
455 dlg_try_hotkey (WDialog * h, int d_key)
457 GList *hot_cur;
458 Widget *current;
459 cb_ret_t handled;
460 int c;
462 if (h->widgets == NULL)
463 return MSG_NOT_HANDLED;
465 if (h->current == NULL)
466 h->current = h->widgets;
469 * Explanation: we don't send letter hotkeys to other widgets if
470 * the currently selected widget is an input line
473 current = WIDGET (h->current->data);
475 if (widget_get_state (current, WST_DISABLED))
476 return MSG_NOT_HANDLED;
478 if (widget_get_options (current, WOP_IS_INPUT))
480 /* skip ascii control characters, anything else can valid character in
481 * some encoding */
482 if (d_key >= 32 && d_key < 256)
483 return MSG_NOT_HANDLED;
486 /* If it's an alt key, send the message */
487 c = d_key & ~ALT (0);
488 if (d_key & ALT (0) && g_ascii_isalpha (c))
489 d_key = g_ascii_tolower (c);
491 handled = MSG_NOT_HANDLED;
492 if (widget_get_options (current, WOP_WANT_HOTKEY))
493 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
495 /* If not used, send hotkey to other widgets */
496 if (handled == MSG_HANDLED)
497 return MSG_HANDLED;
499 hot_cur = dlg_widget_next (h, h->current);
501 /* send it to all widgets */
502 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
504 current = WIDGET (hot_cur->data);
506 if (widget_get_options (current, WOP_WANT_HOTKEY)
507 && !widget_get_state (current, WST_DISABLED))
508 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
510 if (handled == MSG_NOT_HANDLED)
511 hot_cur = dlg_widget_next (h, hot_cur);
514 if (handled == MSG_HANDLED)
515 do_select_widget (h, hot_cur, SELECT_EXACT);
517 return handled;
520 /* --------------------------------------------------------------------------------------------- */
522 static void
523 dlg_key_event (WDialog * h, int d_key)
525 cb_ret_t handled;
527 if (h->widgets == NULL)
528 return;
530 if (h->current == NULL)
531 h->current = h->widgets;
533 /* TAB used to cycle */
534 if (!widget_get_options (WIDGET (h), WOP_WANT_TAB))
536 if (d_key == '\t')
538 dlg_one_down (h);
539 return;
541 else if ((d_key & ~(KEY_M_SHIFT | KEY_M_CTRL)) == '\t')
543 dlg_one_up (h);
544 return;
548 /* first can dlg_callback handle the key */
549 handled = send_message (h, NULL, MSG_KEY, d_key, NULL);
551 /* next try the hotkey */
552 if (handled == MSG_NOT_HANDLED)
553 handled = dlg_try_hotkey (h, d_key);
555 if (handled == MSG_HANDLED)
556 send_message (h, NULL, MSG_HOTKEY_HANDLED, 0, NULL);
557 else
558 /* not used - then try widget_callback */
559 handled = send_message (h->current->data, NULL, MSG_KEY, d_key, NULL);
561 /* not used- try to use the unhandled case */
562 if (handled == MSG_NOT_HANDLED)
563 handled = send_message (h, NULL, MSG_UNHANDLED_KEY, d_key, NULL);
565 if (handled == MSG_NOT_HANDLED)
566 handled = dlg_handle_key (h, d_key);
568 (void) handled;
569 send_message (h, NULL, MSG_POST_KEY, d_key, NULL);
572 /* --------------------------------------------------------------------------------------------- */
574 static void
575 frontend_dlg_run (WDialog * h)
577 Widget *wh = WIDGET (h);
578 Gpm_Event event;
580 event.x = -1;
582 /* close opened editors, viewers, etc */
583 if (!widget_get_state (wh, WST_MODAL) && mc_global.midnight_shutdown)
585 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
586 return;
589 while (widget_get_state (wh, WST_ACTIVE))
591 int d_key;
593 if (mc_global.tty.winch_flag != 0)
594 dialog_change_screen_size ();
596 if (is_idle ())
598 if (idle_hook)
599 execute_hooks (idle_hook);
601 while (widget_get_state (wh, WST_IDLE) && is_idle ())
602 send_message (wh, NULL, MSG_IDLE, 0, NULL);
604 /* Allow terminating the dialog from the idle handler */
605 if (!widget_get_state (wh, WST_ACTIVE))
606 break;
609 update_cursor (h);
611 /* Clear interrupt flag */
612 tty_got_interrupt ();
613 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
615 dlg_process_event (h, d_key, &event);
617 if (widget_get_state (wh, WST_CLOSED))
618 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
622 /* --------------------------------------------------------------------------------------------- */
624 static int
625 dlg_find_widget_by_id (gconstpointer a, gconstpointer b)
627 const Widget *w = CONST_WIDGET (a);
628 unsigned long id = GPOINTER_TO_UINT (b);
630 return w->id == id ? 0 : 1;
633 /* --------------------------------------------------------------------------------------------- */
634 /*** public functions ****************************************************************************/
635 /* --------------------------------------------------------------------------------------------- */
637 /** Clean the dialog area, draw the frame and the title */
638 void
639 dlg_default_repaint (WDialog * h)
641 Widget *wh = WIDGET (h);
643 int space;
645 if (!widget_get_state (wh, WST_ACTIVE))
646 return;
648 space = h->compact ? 0 : 1;
650 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
651 dlg_erase (h);
652 tty_draw_box (wh->y + space, wh->x + space, wh->lines - 2 * space, wh->cols - 2 * space, FALSE);
654 if (h->title != NULL)
656 tty_setcolor (h->color[DLG_COLOR_TITLE]);
657 widget_move (h, space, (wh->cols - str_term_width1 (h->title)) / 2);
658 tty_print_string (h->title);
662 /* --------------------------------------------------------------------------------------------- */
663 /** this function allows to set dialog position */
665 void
666 dlg_set_position (WDialog * h, int y, int x, int lines, int cols)
668 Widget *wh = WIDGET (h);
670 /* save old positions, will be used to reposition childs */
671 int ox, oy, oc, ol;
672 int shift_x, shift_y, scale_x, scale_y;
674 /* save old positions, will be used to reposition childs */
675 ox = wh->x;
676 oy = wh->y;
677 oc = wh->cols;
678 ol = wh->lines;
680 wh->x = x;
681 wh->y = y;
682 wh->lines = lines;
683 wh->cols = cols;
685 /* dialog is empty */
686 if (h->widgets == NULL)
687 return;
689 if (h->current == NULL)
690 h->current = h->widgets;
692 /* values by which controls should be moved */
693 shift_x = wh->x - ox;
694 shift_y = wh->y - oy;
695 scale_x = wh->cols - oc;
696 scale_y = wh->lines - ol;
698 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
700 GList *w;
702 for (w = h->widgets; w != NULL; w = g_list_next (w))
704 /* there are, mainly, 2 generally possible
705 situations:
707 1. control sticks to one side - it
708 should be moved
710 2. control sticks to two sides of
711 one direction - it should be sized */
713 Widget *c = WIDGET (w->data);
714 int cx = c->x;
715 int cy = c->y;
716 int ccols = c->cols;
717 int clines = c->lines;
719 if ((c->pos_flags & WPOS_CENTER_HORZ) != 0)
720 cx = wh->x + (wh->cols - c->cols) / 2;
721 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0 && (c->pos_flags & WPOS_KEEP_RIGHT) != 0)
723 cx += shift_x;
724 ccols += scale_x;
726 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0)
727 cx += shift_x;
728 else if ((c->pos_flags & WPOS_KEEP_RIGHT) != 0)
729 cx += shift_x + scale_x;
731 if ((c->pos_flags & WPOS_CENTER_VERT) != 0)
732 cy = wh->y + (wh->lines - c->lines) / 2;
733 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0 && (c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
735 cy += shift_y;
736 clines += scale_y;
738 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0)
739 cy += shift_y;
740 else if ((c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
741 cy += shift_y + scale_y;
743 widget_set_size (c, cy, cx, clines, ccols);
748 /* --------------------------------------------------------------------------------------------- */
749 /** Set dialog size and position */
751 void
752 dlg_set_size (WDialog * h, int lines, int cols)
754 Widget *w = WIDGET (h);
755 int x, y;
757 if ((w->pos_flags & WPOS_FULLSCREEN) != 0)
759 y = 0;
760 x = 0;
761 lines = LINES;
762 cols = COLS;
764 else
766 if ((w->pos_flags & WPOS_CENTER_HORZ) != 0)
767 x = (COLS - cols) / 2;
768 else
769 x = w->x;
771 if ((w->pos_flags & WPOS_CENTER_VERT) != 0)
772 y = (LINES - lines) / 2;
773 else
774 y = w->y;
776 if ((w->pos_flags & WPOS_TRYUP) != 0)
778 if (y > 3)
779 y -= 2;
780 else if (y == 3)
781 y = 2;
785 dlg_set_position (h, y, x, lines, cols);
788 /* --------------------------------------------------------------------------------------------- */
789 /** Default dialog callback */
791 cb_ret_t
792 dlg_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
794 WDialog *h = DIALOG (w);
796 (void) sender;
797 (void) parm;
798 (void) data;
800 switch (msg)
802 case MSG_DRAW:
803 if (h->color != NULL)
805 dlg_default_repaint (h);
806 return MSG_HANDLED;
808 return MSG_NOT_HANDLED;
810 case MSG_IDLE:
811 /* we don't want endless loop */
812 widget_idle (w, FALSE);
813 return MSG_HANDLED;
815 case MSG_RESIZE:
816 /* this is default resizing mechanism */
817 /* the main idea of this code is to resize dialog
818 according to flags (if any of flags require automatic
819 resizing, like WPOS_CENTER, end after that reposition
820 controls in dialog according to flags of widget) */
821 dlg_set_size (h, w->lines, w->cols);
822 return MSG_HANDLED;
824 default:
825 break;
828 return MSG_NOT_HANDLED;
831 /* --------------------------------------------------------------------------------------------- */
833 WDialog *
834 dlg_create (gboolean modal, int y1, int x1, int lines, int cols, widget_pos_flags_t pos_flags,
835 gboolean compact, const int *colors, widget_cb_fn callback,
836 widget_mouse_cb_fn mouse_callback, const char *help_ctx, const char *title)
838 WDialog *new_d;
839 Widget *w;
841 if ((pos_flags & WPOS_FULLSCREEN) != 0)
843 y1 = 0;
844 x1 = 0;
845 lines = LINES;
846 cols = COLS;
849 new_d = g_new0 (WDialog, 1);
850 w = WIDGET (new_d);
851 widget_init (w, y1, x1, lines, cols, (callback != NULL) ? callback : dlg_default_callback,
852 mouse_callback);
853 w->pos_flags = pos_flags;
854 w->options |= WOP_TOP_SELECT;
856 w->state |= WST_CONSTRUCT;
857 if (modal)
858 w->state |= WST_MODAL;
860 new_d->color = colors;
861 new_d->help_ctx = help_ctx;
862 new_d->compact = compact;
863 new_d->data = NULL;
865 dlg_set_size (new_d, lines, cols);
867 new_d->mouse_status = MOU_UNHANDLED;
869 /* Strip existing spaces, add one space before and after the title */
870 if (title != NULL && *title != '\0')
872 char *t;
874 t = g_strstrip (g_strdup (title));
875 if (*t != '\0')
876 new_d->title = g_strdup_printf (" %s ", t);
877 g_free (t);
880 /* unique name of event group for this dialog */
881 new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
883 return new_d;
886 /* --------------------------------------------------------------------------------------------- */
888 void
889 dlg_set_default_colors (void)
891 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
892 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
893 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
894 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
895 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
897 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
898 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
899 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
900 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
901 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
903 listbox_colors[DLG_COLOR_NORMAL] = PMENU_ENTRY_COLOR;
904 listbox_colors[DLG_COLOR_FOCUS] = PMENU_SELECTED_COLOR;
905 listbox_colors[DLG_COLOR_HOT_NORMAL] = PMENU_ENTRY_COLOR;
906 listbox_colors[DLG_COLOR_HOT_FOCUS] = PMENU_SELECTED_COLOR;
907 listbox_colors[DLG_COLOR_TITLE] = PMENU_TITLE_COLOR;
910 /* --------------------------------------------------------------------------------------------- */
912 void
913 dlg_erase (WDialog * h)
915 Widget *wh = WIDGET (h);
917 if (wh != NULL && widget_get_state (wh, WST_ACTIVE))
918 tty_fill_region (wh->y, wh->x, wh->lines, wh->cols, ' ');
921 /* --------------------------------------------------------------------------------------------- */
923 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
926 unsigned long
927 add_widget_autopos (WDialog * h, void *w, widget_pos_flags_t pos_flags, const void *before)
929 Widget *wh = WIDGET (h);
930 Widget *widget;
932 /* Don't accept 0 widgets */
933 if (w == NULL)
934 abort ();
936 widget = WIDGET (w);
938 if ((pos_flags & WPOS_CENTER_HORZ) != 0)
939 widget->x = (wh->cols - widget->cols) / 2;
940 widget->x += wh->x;
942 if ((pos_flags & WPOS_CENTER_VERT) != 0)
943 widget->y = (wh->lines - widget->lines) / 2;
944 widget->y += wh->y;
946 widget->owner = h;
947 widget->pos_flags = pos_flags;
948 widget->id = h->widget_id++;
950 if (h->widgets == NULL || before == NULL)
952 h->widgets = g_list_append (h->widgets, widget);
953 h->current = g_list_last (h->widgets);
955 else
957 GList *b;
959 b = g_list_find (h->widgets, before);
961 /* don't accept widget not from dialog. This shouldn't happen */
962 if (b == NULL)
963 abort ();
965 b = g_list_next (b);
966 h->widgets = g_list_insert_before (h->widgets, b, widget);
967 if (b != NULL)
968 h->current = g_list_previous (b);
969 else
970 h->current = g_list_last (h->widgets);
973 /* widget has been added in runtime */
974 if (widget_get_state (wh, WST_ACTIVE))
976 send_message (widget, NULL, MSG_INIT, 0, NULL);
977 send_message (widget, NULL, MSG_DRAW, 0, NULL);
978 send_message (widget, NULL, MSG_FOCUS, 0, NULL);
981 return widget->id;
984 /* --------------------------------------------------------------------------------------------- */
985 /** wrapper to simply add lefttop positioned controls */
987 unsigned long
988 add_widget (WDialog * h, void *w)
990 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT,
991 h->current != NULL ? h->current->data : NULL);
994 /* --------------------------------------------------------------------------------------------- */
996 unsigned long
997 add_widget_before (WDialog * h, void *w, void *before)
999 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT, before);
1002 /* --------------------------------------------------------------------------------------------- */
1004 /** delete widget from dialog */
1005 void
1006 del_widget (void *w)
1008 WDialog *h;
1009 GList *d;
1011 /* Don't accept NULL widget. This shouldn't happen */
1012 if (w == NULL)
1013 abort ();
1015 h = WIDGET (w)->owner;
1017 d = g_list_find (h->widgets, w);
1018 if (d == h->current)
1019 h->current = dlg_widget_next (h, d);
1021 h->widgets = g_list_remove_link (h->widgets, d);
1022 send_message (d->data, NULL, MSG_DESTROY, 0, NULL);
1023 g_free (d->data);
1024 g_list_free_1 (d);
1026 /* widget has been deleted in runtime */
1027 if (widget_get_state (WIDGET (h), WST_ACTIVE))
1029 dlg_redraw (h);
1030 dlg_focus (h);
1034 /* --------------------------------------------------------------------------------------------- */
1036 void
1037 do_refresh (void)
1039 GList *d = top_dlg;
1041 if (fast_refresh)
1043 if ((d != NULL) && (d->data != NULL))
1044 dlg_redraw (DIALOG (d->data));
1046 else
1048 /* Search first fullscreen dialog */
1049 for (; d != NULL; d = g_list_next (d))
1050 if (d->data != NULL && (WIDGET (d->data)->pos_flags & WPOS_FULLSCREEN) != 0)
1051 break;
1052 /* back to top dialog */
1053 for (; d != NULL; d = g_list_previous (d))
1054 if (d->data != NULL)
1055 dlg_redraw (DIALOG (d->data));
1059 /* --------------------------------------------------------------------------------------------- */
1060 /** broadcast a message to all the widgets in a dialog */
1062 void
1063 dlg_broadcast_msg (WDialog * h, widget_msg_t msg)
1065 dlg_broadcast_msg_to (h, msg, FALSE, 0);
1068 /* --------------------------------------------------------------------------------------------- */
1070 gboolean
1071 dlg_focus (WDialog * h)
1073 /* cannot focus disabled widget */
1074 if (h->current != NULL)
1076 Widget *wh = WIDGET (h);
1078 if (widget_get_state (wh, WST_CONSTRUCT) || widget_get_state (wh, WST_ACTIVE))
1080 Widget *current = WIDGET (h->current->data);
1082 if (!widget_get_state (current, WST_DISABLED)
1083 && (send_message (current, NULL, MSG_FOCUS, 0, NULL) == MSG_HANDLED))
1085 send_message (h, current, MSG_FOCUS, 0, NULL);
1086 return TRUE;
1091 return FALSE;
1094 /* --------------------------------------------------------------------------------------------- */
1095 /** Find the widget with the given callback in the dialog h */
1097 Widget *
1098 find_widget_type (const WDialog * h, widget_cb_fn callback)
1100 GList *w;
1102 w = g_list_find_custom (h->widgets, (gconstpointer) callback, dlg_find_widget_callback);
1104 return (w == NULL) ? NULL : WIDGET (w->data);
1107 /* --------------------------------------------------------------------------------------------- */
1108 /** Find the widget with the given id */
1110 Widget *
1111 dlg_find_by_id (const WDialog * h, unsigned long id)
1113 GList *w;
1115 w = g_list_find_custom (h->widgets, GUINT_TO_POINTER (id), dlg_find_widget_by_id);
1116 return w != NULL ? WIDGET (w->data) : NULL;
1119 /* --------------------------------------------------------------------------------------------- */
1120 /** Find the widget with the given id in the dialog h and select it */
1122 void
1123 dlg_select_by_id (const WDialog * h, unsigned long id)
1125 Widget *w;
1127 w = dlg_find_by_id (h, id);
1128 if (w != NULL)
1129 dlg_select_widget (w);
1132 /* --------------------------------------------------------------------------------------------- */
1134 * Try to select widget in the dialog.
1137 void
1138 dlg_select_widget (void *w)
1140 Widget *widget = WIDGET (w);
1141 WDialog *h = widget->owner;
1143 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
1146 /* --------------------------------------------------------------------------------------------- */
1148 * Set widget at bottom of widget list.
1151 void
1152 dlg_set_bottom_widget (void *w)
1154 Widget *widget = WIDGET (w);
1155 WDialog *h = widget->owner;
1157 dlg_reorder_widgets (g_list_find (h->widgets, widget), FALSE);
1160 /* --------------------------------------------------------------------------------------------- */
1161 /** Try to select previous widget in the tab order */
1163 void
1164 dlg_one_up (WDialog * h)
1166 if (h->widgets != NULL)
1167 do_select_widget (h, dlg_widget_prev (h, h->current), SELECT_PREV);
1170 /* --------------------------------------------------------------------------------------------- */
1171 /** Try to select next widget in the tab order */
1173 void
1174 dlg_one_down (WDialog * h)
1176 if (h->widgets != NULL)
1177 do_select_widget (h, dlg_widget_next (h, h->current), SELECT_NEXT);
1180 /* --------------------------------------------------------------------------------------------- */
1182 void
1183 update_cursor (WDialog * h)
1185 GList *p = h->current;
1187 if (p != NULL && widget_get_state (WIDGET (h), WST_ACTIVE))
1189 Widget *w;
1191 w = WIDGET (p->data);
1193 if (!widget_get_state (w, WST_DISABLED) && widget_get_options (w, WOP_WANT_CURSOR))
1194 send_message (w, NULL, MSG_CURSOR, 0, NULL);
1195 else
1198 p = dlg_widget_next (h, p);
1199 if (p == h->current)
1200 break;
1202 w = WIDGET (p->data);
1204 if (!widget_get_state (w, WST_DISABLED) && widget_get_options (w, WOP_WANT_CURSOR)
1205 && send_message (w, NULL, MSG_CURSOR, 0, NULL) == MSG_HANDLED)
1206 break;
1208 while (TRUE);
1212 /* --------------------------------------------------------------------------------------------- */
1214 * Redraw the widgets in reverse order, leaving the current widget
1215 * as the last one
1218 void
1219 dlg_redraw (WDialog * h)
1221 if (!widget_get_state (WIDGET (h), WST_ACTIVE))
1222 return;
1224 if (h->winch_pending)
1226 h->winch_pending = FALSE;
1227 send_message (h, NULL, MSG_RESIZE, 0, NULL);
1230 send_message (h, NULL, MSG_DRAW, 0, NULL);
1231 dlg_broadcast_msg (h, MSG_DRAW);
1232 update_cursor (h);
1235 /* --------------------------------------------------------------------------------------------- */
1237 void
1238 dlg_stop (WDialog * h)
1240 widget_set_state (WIDGET (h), WST_CLOSED, TRUE);
1243 /* --------------------------------------------------------------------------------------------- */
1244 /** Init the process */
1246 void
1247 dlg_init (WDialog * h)
1249 Widget *wh = WIDGET (h);
1251 if (top_dlg != NULL && widget_get_state (WIDGET (top_dlg->data), WST_MODAL))
1252 widget_set_state (wh, WST_MODAL, TRUE);
1254 /* add dialog to the stack */
1255 top_dlg = g_list_prepend (top_dlg, h);
1257 /* Initialize dialog manager and widgets */
1258 if (widget_get_state (wh, WST_CONSTRUCT))
1260 if (!widget_get_state (wh, WST_MODAL))
1261 dialog_switch_add (h);
1263 send_message (h, NULL, MSG_INIT, 0, NULL);
1264 dlg_broadcast_msg (h, MSG_INIT);
1265 dlg_read_history (h);
1268 widget_set_state (wh, WST_ACTIVE, TRUE);
1270 /* first send MSG_DRAW to dialog itself and all widgets... */
1271 dlg_redraw (h);
1273 /* ...then send MSG_FOCUS to select the first widget that can take focus */
1274 while (h->current != NULL && !dlg_focus (h))
1275 h->current = dlg_widget_next (h, h->current);
1278 h->ret_value = 0;
1281 /* --------------------------------------------------------------------------------------------- */
1283 void
1284 dlg_process_event (WDialog * h, int key, Gpm_Event * event)
1286 if (key == EV_NONE)
1288 if (tty_got_interrupt ())
1289 if (send_message (h, NULL, MSG_ACTION, CK_Cancel, NULL) != MSG_HANDLED)
1290 dlg_execute_cmd (h, CK_Cancel);
1292 else if (key == EV_MOUSE)
1293 h->mouse_status = dlg_mouse_event (h, event);
1294 else
1295 dlg_key_event (h, key);
1298 /* --------------------------------------------------------------------------------------------- */
1299 /** Shutdown the dlg_run */
1301 void
1302 dlg_run_done (WDialog * h)
1304 top_dlg = g_list_remove (top_dlg, h);
1306 if (widget_get_state (WIDGET (h), WST_CLOSED))
1308 send_message (h, h->current->data, MSG_END, 0, NULL);
1309 if (!widget_get_state (WIDGET (h), WST_MODAL))
1310 dialog_switch_remove (h);
1314 /* --------------------------------------------------------------------------------------------- */
1316 * Standard run dialog routine
1317 * We have to keep this routine small so that we can duplicate it's
1318 * behavior on complex routines like the file routines, this way,
1319 * they can call the dlg_process_event without rewriting all the code
1323 dlg_run (WDialog * h)
1325 dlg_init (h);
1326 frontend_dlg_run (h);
1327 dlg_run_done (h);
1328 return h->ret_value;
1331 /* --------------------------------------------------------------------------------------------- */
1333 void
1334 dlg_destroy (WDialog * h)
1336 /* if some widgets have history, save all history at one moment here */
1337 dlg_save_history (h);
1338 dlg_broadcast_msg (h, MSG_DESTROY);
1339 g_list_free_full (h->widgets, g_free);
1340 mc_event_group_del (h->event_group);
1341 g_free (h->event_group);
1342 g_free (h->title);
1343 g_free (h);
1345 do_refresh ();
1348 /* --------------------------------------------------------------------------------------------- */
1351 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1353 void
1354 dlg_save_history (WDialog * h)
1356 char *profile;
1357 int i;
1359 if (num_history_items_recorded == 0) /* this is how to disable */
1360 return;
1362 profile = mc_config_get_full_path (MC_HISTORY_FILE);
1363 i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
1364 if (i != -1)
1365 close (i);
1367 /* Make sure the history is only readable by the user */
1368 if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
1370 ev_history_load_save_t event_data;
1372 event_data.cfg = mc_config_init (profile, FALSE);
1373 event_data.receiver = NULL;
1375 /* get all histories in dialog */
1376 mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
1378 mc_config_save_file (event_data.cfg, NULL);
1379 mc_config_deinit (event_data.cfg);
1382 g_free (profile);
1385 /* --------------------------------------------------------------------------------------------- */
1387 char *
1388 dlg_get_title (const WDialog * h, size_t len)
1390 char *t;
1392 if (h == NULL)
1393 abort ();
1395 if (h->get_title != NULL)
1396 t = h->get_title (h, len);
1397 else
1398 t = g_strdup ("");
1400 return t;
1403 /* --------------------------------------------------------------------------------------------- */