(dlg_set_position): minor refactoring.
[midnight-commander.git] / lib / widget / dialog.c
blobaa906fc68b270eeac45c512a6486931aa1bfd480
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 /* --------------------------------------------------------------------------------------------- */
218 * Try to select another widget. If forward is set, follow tab order.
219 * Otherwise go to the previous widget.
222 static void
223 do_select_widget (WDialog * h, GList * w, select_dir_t dir)
225 Widget *w0 = WIDGET (h->current->data);
227 if (!dlg_unfocus (h))
228 return;
230 h->current = w;
234 if (dlg_focus (h))
235 break;
237 switch (dir)
239 case SELECT_EXACT:
240 h->current = g_list_find (h->widgets, w0);
241 if (dlg_focus (h))
242 return;
243 /* try find another widget that can take focus */
244 dir = SELECT_NEXT;
245 /* fallthrough */
246 case SELECT_NEXT:
247 h->current = dlg_widget_next (h, h->current);
248 break;
249 case SELECT_PREV:
250 h->current = dlg_widget_prev (h, h->current);
251 break;
252 default:
253 break;
256 while (h->current != w);
258 if (widget_overlapped (w0, WIDGET (h->current->data)))
260 send_message (h->current->data, NULL, MSG_DRAW, 0, NULL);
261 send_message (h->current->data, NULL, MSG_FOCUS, 0, NULL);
265 /* --------------------------------------------------------------------------------------------- */
267 static void
268 refresh_cmd (void)
270 #ifdef HAVE_SLANG
271 tty_touch_screen ();
272 mc_refresh ();
273 #else
274 /* Use this if the refreshes fail */
275 clr_scr ();
276 repaint_screen ();
277 #endif /* HAVE_SLANG */
280 /* --------------------------------------------------------------------------------------------- */
282 static cb_ret_t
283 dlg_execute_cmd (WDialog * h, long command)
285 cb_ret_t ret = MSG_HANDLED;
286 switch (command)
288 case CK_Ok:
289 h->ret_value = B_ENTER;
290 dlg_stop (h);
291 break;
292 case CK_Cancel:
293 h->ret_value = B_CANCEL;
294 dlg_stop (h);
295 break;
297 case CK_Up:
298 case CK_Left:
299 dlg_one_up (h);
300 break;
301 case CK_Down:
302 case CK_Right:
303 dlg_one_down (h);
304 break;
306 case CK_Help:
308 ev_help_t event_data = { NULL, h->help_ctx };
309 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
311 break;
313 case CK_Suspend:
314 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
315 refresh_cmd ();
316 break;
317 case CK_Refresh:
318 refresh_cmd ();
319 break;
321 case CK_ScreenList:
322 if (!widget_get_state (WIDGET (h), WST_MODAL))
323 dialog_switch_list ();
324 else
325 ret = MSG_NOT_HANDLED;
326 break;
327 case CK_ScreenNext:
328 if (!widget_get_state (WIDGET (h), WST_MODAL))
329 dialog_switch_next ();
330 else
331 ret = MSG_NOT_HANDLED;
332 break;
333 case CK_ScreenPrev:
334 if (!widget_get_state (WIDGET (h), WST_MODAL))
335 dialog_switch_prev ();
336 else
337 ret = MSG_NOT_HANDLED;
338 break;
340 default:
341 ret = MSG_NOT_HANDLED;
344 return ret;
347 /* --------------------------------------------------------------------------------------------- */
349 static cb_ret_t
350 dlg_handle_key (WDialog * h, int d_key)
352 long command;
354 command = keybind_lookup_keymap_command (dialog_map, d_key);
356 if (command == CK_IgnoreKey)
357 return MSG_NOT_HANDLED;
359 if (send_message (h, NULL, MSG_ACTION, command, NULL) == MSG_HANDLED
360 || dlg_execute_cmd (h, command) == MSG_HANDLED)
361 return MSG_HANDLED;
363 return MSG_NOT_HANDLED;
366 /* --------------------------------------------------------------------------------------------- */
368 * This is the low-level mouse handler.
369 * It receives a Gpm_Event event and translates it into a higher level protocol.
371 static int
372 dlg_mouse_translator (Gpm_Event * event, Widget * w)
374 mouse_event_t me;
376 me = mouse_translate_event (w, event);
378 return mouse_process_event (w, &me);
381 /* --------------------------------------------------------------------------------------------- */
383 static int
384 dlg_mouse_event (WDialog * h, Gpm_Event * event)
386 Widget *wh = WIDGET (h);
388 GList *p;
390 /* close the dialog by mouse left click out of dialog area */
391 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0)
392 && ((event->type & GPM_DOWN) != 0) && !mouse_global_in_widget (event, wh))
394 h->ret_value = B_CANCEL;
395 dlg_stop (h);
396 return MOU_NORMAL;
399 if (wh->mouse_callback != NULL)
401 int mou;
403 mou = dlg_mouse_translator (event, wh);
404 if (mou != MOU_UNHANDLED)
405 return mou;
408 /* send the event to widgets in reverse Z-order */
409 p = g_list_last (h->widgets);
412 Widget *w = WIDGET (p->data);
414 if (!widget_get_state (w, WST_DISABLED) && w->mouse_callback != NULL)
416 /* put global cursor position to the widget */
417 int ret;
419 ret = dlg_mouse_translator (event, w);
420 if (ret != MOU_UNHANDLED)
421 return ret;
424 p = g_list_previous (p);
426 while (p != NULL);
428 return MOU_UNHANDLED;
431 /* --------------------------------------------------------------------------------------------- */
433 static cb_ret_t
434 dlg_try_hotkey (WDialog * h, int d_key)
436 GList *hot_cur;
437 Widget *current;
438 cb_ret_t handled;
439 int c;
441 if (h->widgets == NULL)
442 return MSG_NOT_HANDLED;
444 if (h->current == NULL)
445 h->current = h->widgets;
448 * Explanation: we don't send letter hotkeys to other widgets if
449 * the currently selected widget is an input line
452 current = WIDGET (h->current->data);
454 if (widget_get_state (current, WST_DISABLED))
455 return MSG_NOT_HANDLED;
457 if (widget_get_options (current, WOP_IS_INPUT))
459 /* skip ascii control characters, anything else can valid character in
460 * some encoding */
461 if (d_key >= 32 && d_key < 256)
462 return MSG_NOT_HANDLED;
465 /* If it's an alt key, send the message */
466 c = d_key & ~ALT (0);
467 if (d_key & ALT (0) && g_ascii_isalpha (c))
468 d_key = g_ascii_tolower (c);
470 handled = MSG_NOT_HANDLED;
471 if (widget_get_options (current, WOP_WANT_HOTKEY))
472 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
474 /* If not used, send hotkey to other widgets */
475 if (handled == MSG_HANDLED)
476 return MSG_HANDLED;
478 hot_cur = dlg_widget_next (h, h->current);
480 /* send it to all widgets */
481 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
483 current = WIDGET (hot_cur->data);
485 if (widget_get_options (current, WOP_WANT_HOTKEY)
486 && !widget_get_state (current, WST_DISABLED))
487 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
489 if (handled == MSG_NOT_HANDLED)
490 hot_cur = dlg_widget_next (h, hot_cur);
493 if (handled == MSG_HANDLED)
494 do_select_widget (h, hot_cur, SELECT_EXACT);
496 return handled;
499 /* --------------------------------------------------------------------------------------------- */
501 static void
502 dlg_key_event (WDialog * h, int d_key)
504 cb_ret_t handled;
506 if (h->widgets == NULL)
507 return;
509 if (h->current == NULL)
510 h->current = h->widgets;
512 /* TAB used to cycle */
513 if (!widget_get_options (WIDGET (h), WOP_WANT_TAB))
515 if (d_key == '\t')
517 dlg_one_down (h);
518 return;
520 else if ((d_key & ~(KEY_M_SHIFT | KEY_M_CTRL)) == '\t')
522 dlg_one_up (h);
523 return;
527 /* first can dlg_callback handle the key */
528 handled = send_message (h, NULL, MSG_KEY, d_key, NULL);
530 /* next try the hotkey */
531 if (handled == MSG_NOT_HANDLED)
532 handled = dlg_try_hotkey (h, d_key);
534 if (handled == MSG_HANDLED)
535 send_message (h, NULL, MSG_HOTKEY_HANDLED, 0, NULL);
536 else
537 /* not used - then try widget_callback */
538 handled = send_message (h->current->data, NULL, MSG_KEY, d_key, NULL);
540 /* not used- try to use the unhandled case */
541 if (handled == MSG_NOT_HANDLED)
542 handled = send_message (h, NULL, MSG_UNHANDLED_KEY, d_key, NULL);
544 if (handled == MSG_NOT_HANDLED)
545 handled = dlg_handle_key (h, d_key);
547 (void) handled;
548 send_message (h, NULL, MSG_POST_KEY, d_key, NULL);
551 /* --------------------------------------------------------------------------------------------- */
553 static void
554 frontend_dlg_run (WDialog * h)
556 Widget *wh = WIDGET (h);
557 Gpm_Event event;
559 event.x = -1;
561 /* close opened editors, viewers, etc */
562 if (!widget_get_state (wh, WST_MODAL) && mc_global.midnight_shutdown)
564 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
565 return;
568 while (widget_get_state (wh, WST_ACTIVE))
570 int d_key;
572 if (mc_global.tty.winch_flag != 0)
573 dialog_change_screen_size ();
575 if (is_idle ())
577 if (idle_hook)
578 execute_hooks (idle_hook);
580 while (widget_get_state (wh, WST_IDLE) && is_idle ())
581 send_message (wh, NULL, MSG_IDLE, 0, NULL);
583 /* Allow terminating the dialog from the idle handler */
584 if (!widget_get_state (wh, WST_ACTIVE))
585 break;
588 update_cursor (h);
590 /* Clear interrupt flag */
591 tty_got_interrupt ();
592 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
594 dlg_process_event (h, d_key, &event);
596 if (widget_get_state (wh, WST_CLOSED))
597 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
601 /* --------------------------------------------------------------------------------------------- */
603 static int
604 dlg_find_widget_by_id (gconstpointer a, gconstpointer b)
606 const Widget *w = CONST_WIDGET (a);
607 unsigned long id = GPOINTER_TO_UINT (b);
609 return w->id == id ? 0 : 1;
612 /* --------------------------------------------------------------------------------------------- */
614 static void
615 dlg_set_top_or_bottom_widget (void *w, gboolean set_top)
617 Widget *widget = WIDGET (w);
618 WDialog *h = widget->owner;
619 GList *l;
621 l = g_list_find (h->widgets, w);
622 if (l == NULL)
623 abort (); /* widget is not in dialog, this should not happen */
625 /* unfocus prevoius widget and focus current one before widget reordering */
626 if (set_top && widget_get_state (WIDGET (h), WST_ACTIVE))
627 do_select_widget (h, l, SELECT_EXACT);
629 /* widget reordering */
630 h->widgets = g_list_remove_link (h->widgets, l);
631 if (set_top)
632 h->widgets = g_list_concat (h->widgets, l);
633 else
634 h->widgets = g_list_concat (l, h->widgets);
637 /* --------------------------------------------------------------------------------------------- */
638 /*** public functions ****************************************************************************/
639 /* --------------------------------------------------------------------------------------------- */
641 /** Clean the dialog area, draw the frame and the title */
642 void
643 dlg_default_repaint (WDialog * h)
645 Widget *wh = WIDGET (h);
647 int space;
649 if (!widget_get_state (wh, WST_ACTIVE))
650 return;
652 space = (h->flags & DLG_COMPACT) ? 0 : 1;
654 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
655 dlg_erase (h);
656 tty_draw_box (wh->y + space, wh->x + space, wh->lines - 2 * space, wh->cols - 2 * space, FALSE);
658 if (h->title != NULL)
660 tty_setcolor (h->color[DLG_COLOR_TITLE]);
661 widget_move (h, space, (wh->cols - str_term_width1 (h->title)) / 2);
662 tty_print_string (h->title);
666 /* --------------------------------------------------------------------------------------------- */
667 /** this function allows to set dialog position */
669 void
670 dlg_set_position (WDialog * h, int y, int x, int lines, int cols)
672 Widget *wh = WIDGET (h);
674 /* save old positions, will be used to reposition childs */
675 int ox, oy, oc, ol;
676 int shift_x, shift_y, scale_x, scale_y;
678 /* save old positions, will be used to reposition childs */
679 ox = wh->x;
680 oy = wh->y;
681 oc = wh->cols;
682 ol = wh->lines;
684 wh->x = x;
685 wh->y = y;
686 wh->lines = lines;
687 wh->cols = cols;
689 /* dialog is empty */
690 if (h->widgets == NULL)
691 return;
693 if (h->current == NULL)
694 h->current = h->widgets;
696 /* values by which controls should be moved */
697 shift_x = wh->x - ox;
698 shift_y = wh->y - oy;
699 scale_x = wh->cols - oc;
700 scale_y = wh->lines - ol;
702 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
704 GList *w;
706 for (w = h->widgets; w != NULL; w = g_list_next (w))
708 /* there are, mainly, 2 generally possible
709 situations:
711 1. control sticks to one side - it
712 should be moved
714 2. control sticks to two sides of
715 one direction - it should be sized */
717 Widget *c = WIDGET (w->data);
718 int cx = c->x;
719 int cy = c->y;
720 int ccols = c->cols;
721 int clines = c->lines;
723 if ((c->pos_flags & WPOS_CENTER_HORZ) != 0)
724 cx = wh->x + (wh->cols - c->cols) / 2;
725 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0 && (c->pos_flags & WPOS_KEEP_RIGHT) != 0)
727 cx += shift_x;
728 ccols += scale_x;
730 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0)
731 cx += shift_x;
732 else if ((c->pos_flags & WPOS_KEEP_RIGHT) != 0)
733 cx += shift_x + scale_x;
735 if ((c->pos_flags & WPOS_CENTER_VERT) != 0)
736 cy = wh->y + (wh->lines - c->lines) / 2;
737 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0 && (c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
739 cy += shift_y;
740 clines += scale_y;
742 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0)
743 cy += shift_y;
744 else if ((c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
745 cy += shift_y + scale_y;
747 widget_set_size (c, cy, cx, clines, ccols);
752 /* --------------------------------------------------------------------------------------------- */
753 /** Set dialog size and position */
755 void
756 dlg_set_size (WDialog * h, int lines, int cols)
758 int x = WIDGET (h)->x;
759 int y = WIDGET (h)->y;
761 if ((h->flags & DLG_CENTER) != 0)
763 y = (LINES - lines) / 2;
764 x = (COLS - cols) / 2;
767 if ((h->flags & DLG_TRYUP) != 0)
769 if (y > 3)
770 y -= 2;
771 else if (y == 3)
772 y = 2;
775 dlg_set_position (h, y, x, lines, cols);
778 /* --------------------------------------------------------------------------------------------- */
779 /** Default dialog callback */
781 cb_ret_t
782 dlg_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
784 WDialog *h = DIALOG (w);
786 (void) sender;
787 (void) parm;
788 (void) data;
790 switch (msg)
792 case MSG_DRAW:
793 if (h->color != NULL)
795 dlg_default_repaint (h);
796 return MSG_HANDLED;
798 return MSG_NOT_HANDLED;
800 case MSG_IDLE:
801 /* we don't want endless loop */
802 widget_idle (w, FALSE);
803 return MSG_HANDLED;
805 case MSG_RESIZE:
806 /* this is default resizing mechanism */
807 /* the main idea of this code is to resize dialog
808 according to flags (if any of flags require automatic
809 resizing, like DLG_CENTER, end after that reposition
810 controls in dialog according to flags of widget) */
811 dlg_set_size (h, w->lines, w->cols);
812 return MSG_HANDLED;
814 default:
815 break;
818 return MSG_NOT_HANDLED;
821 /* --------------------------------------------------------------------------------------------- */
823 WDialog *
824 dlg_create (gboolean modal, int y1, int x1, int lines, int cols,
825 const int *colors, widget_cb_fn callback, widget_mouse_cb_fn mouse_callback,
826 const char *help_ctx, const char *title, dlg_flags_t flags)
828 WDialog *new_d;
829 Widget *w;
831 new_d = g_new0 (WDialog, 1);
832 w = WIDGET (new_d);
833 widget_init (w, y1, x1, lines, cols, (callback != NULL) ? callback : dlg_default_callback,
834 mouse_callback);
835 w->options |= WOP_TOP_SELECT;
837 w->state |= WST_CONSTRUCT;
838 if (modal)
839 w->state |= WST_MODAL;
841 new_d->color = colors;
842 new_d->help_ctx = help_ctx;
843 new_d->flags = flags;
844 new_d->data = NULL;
846 dlg_set_size (new_d, lines, cols);
847 new_d->fullscreen = (w->x == 0 && w->y == 0 && w->cols == COLS && w->lines == LINES);
849 new_d->mouse_status = MOU_UNHANDLED;
851 /* Strip existing spaces, add one space before and after the title */
852 if (title != NULL && *title != '\0')
854 char *t;
856 t = g_strstrip (g_strdup (title));
857 if (*t != '\0')
858 new_d->title = g_strdup_printf (" %s ", t);
859 g_free (t);
862 /* unique name of event group for this dialog */
863 new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
865 return new_d;
868 /* --------------------------------------------------------------------------------------------- */
870 void
871 dlg_set_default_colors (void)
873 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
874 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
875 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
876 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
877 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
879 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
880 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
881 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
882 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
883 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
885 listbox_colors[DLG_COLOR_NORMAL] = PMENU_ENTRY_COLOR;
886 listbox_colors[DLG_COLOR_FOCUS] = PMENU_SELECTED_COLOR;
887 listbox_colors[DLG_COLOR_HOT_NORMAL] = PMENU_ENTRY_COLOR;
888 listbox_colors[DLG_COLOR_HOT_FOCUS] = PMENU_SELECTED_COLOR;
889 listbox_colors[DLG_COLOR_TITLE] = PMENU_TITLE_COLOR;
892 /* --------------------------------------------------------------------------------------------- */
894 void
895 dlg_erase (WDialog * h)
897 Widget *wh = WIDGET (h);
899 if (wh != NULL && widget_get_state (wh, WST_ACTIVE))
900 tty_fill_region (wh->y, wh->x, wh->lines, wh->cols, ' ');
903 /* --------------------------------------------------------------------------------------------- */
905 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
908 unsigned long
909 add_widget_autopos (WDialog * h, void *w, widget_pos_flags_t pos_flags, const void *before)
911 Widget *wh = WIDGET (h);
912 Widget *widget;
914 /* Don't accept 0 widgets */
915 if (w == NULL)
916 abort ();
918 widget = WIDGET (w);
920 if ((pos_flags & WPOS_CENTER_HORZ) != 0)
921 widget->x = (wh->cols - widget->cols) / 2;
922 widget->x += wh->x;
924 if ((pos_flags & WPOS_CENTER_VERT) != 0)
925 widget->y = (wh->lines - widget->lines) / 2;
926 widget->y += wh->y;
928 widget->owner = h;
929 widget->pos_flags = pos_flags;
930 widget->id = h->widget_id++;
932 if (h->widgets == NULL || before == NULL)
934 h->widgets = g_list_append (h->widgets, widget);
935 h->current = g_list_last (h->widgets);
937 else
939 GList *b;
941 b = g_list_find (h->widgets, before);
943 /* don't accept widget not from dialog. This shouldn't happen */
944 if (b == NULL)
945 abort ();
947 b = g_list_next (b);
948 h->widgets = g_list_insert_before (h->widgets, b, widget);
949 if (b != NULL)
950 h->current = g_list_previous (b);
951 else
952 h->current = g_list_last (h->widgets);
955 /* widget has been added in runtime */
956 if (widget_get_state (wh, WST_ACTIVE))
958 send_message (widget, NULL, MSG_INIT, 0, NULL);
959 send_message (widget, NULL, MSG_DRAW, 0, NULL);
960 send_message (widget, NULL, MSG_FOCUS, 0, NULL);
963 return widget->id;
966 /* --------------------------------------------------------------------------------------------- */
967 /** wrapper to simply add lefttop positioned controls */
969 unsigned long
970 add_widget (WDialog * h, void *w)
972 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT,
973 h->current != NULL ? h->current->data : NULL);
976 /* --------------------------------------------------------------------------------------------- */
978 unsigned long
979 add_widget_before (WDialog * h, void *w, void *before)
981 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT, before);
984 /* --------------------------------------------------------------------------------------------- */
986 /** delete widget from dialog */
987 void
988 del_widget (void *w)
990 WDialog *h;
991 GList *d;
993 /* Don't accept NULL widget. This shouldn't happen */
994 if (w == NULL)
995 abort ();
997 h = WIDGET (w)->owner;
999 d = g_list_find (h->widgets, w);
1000 if (d == h->current)
1001 h->current = dlg_widget_next (h, d);
1003 h->widgets = g_list_remove_link (h->widgets, d);
1004 send_message (d->data, NULL, MSG_DESTROY, 0, NULL);
1005 g_free (d->data);
1006 g_list_free_1 (d);
1008 /* widget has been deleted in runtime */
1009 if (widget_get_state (WIDGET (h), WST_ACTIVE))
1011 dlg_redraw (h);
1012 dlg_focus (h);
1016 /* --------------------------------------------------------------------------------------------- */
1018 void
1019 do_refresh (void)
1021 GList *d = top_dlg;
1023 if (fast_refresh)
1025 if ((d != NULL) && (d->data != NULL))
1026 dlg_redraw (DIALOG (d->data));
1028 else
1030 /* Search first fullscreen dialog */
1031 for (; d != NULL; d = g_list_next (d))
1032 if (d->data != NULL && DIALOG (d->data)->fullscreen)
1033 break;
1034 /* back to top dialog */
1035 for (; d != NULL; d = g_list_previous (d))
1036 if (d->data != NULL)
1037 dlg_redraw (DIALOG (d->data));
1041 /* --------------------------------------------------------------------------------------------- */
1042 /** broadcast a message to all the widgets in a dialog */
1044 void
1045 dlg_broadcast_msg (WDialog * h, widget_msg_t msg)
1047 dlg_broadcast_msg_to (h, msg, FALSE, 0);
1050 /* --------------------------------------------------------------------------------------------- */
1052 gboolean
1053 dlg_focus (WDialog * h)
1055 /* cannot focus disabled widget */
1056 if (h->current != NULL)
1058 Widget *wh = WIDGET (h);
1060 if (widget_get_state (wh, WST_CONSTRUCT) || widget_get_state (wh, WST_ACTIVE))
1062 Widget *current = WIDGET (h->current->data);
1064 if (!widget_get_state (current, WST_DISABLED)
1065 && (send_message (current, NULL, MSG_FOCUS, 0, NULL) == MSG_HANDLED))
1067 send_message (h, current, MSG_FOCUS, 0, NULL);
1068 return TRUE;
1073 return FALSE;
1076 /* --------------------------------------------------------------------------------------------- */
1077 /** Find the widget with the given callback in the dialog h */
1079 Widget *
1080 find_widget_type (const WDialog * h, widget_cb_fn callback)
1082 GList *w;
1084 w = g_list_find_custom (h->widgets, (gconstpointer) callback, dlg_find_widget_callback);
1086 return (w == NULL) ? NULL : WIDGET (w->data);
1089 /* --------------------------------------------------------------------------------------------- */
1090 /** Find the widget with the given id */
1092 Widget *
1093 dlg_find_by_id (const WDialog * h, unsigned long id)
1095 GList *w;
1097 w = g_list_find_custom (h->widgets, GUINT_TO_POINTER (id), dlg_find_widget_by_id);
1098 return w != NULL ? WIDGET (w->data) : NULL;
1101 /* --------------------------------------------------------------------------------------------- */
1102 /** Find the widget with the given id in the dialog h and select it */
1104 void
1105 dlg_select_by_id (const WDialog * h, unsigned long id)
1107 Widget *w;
1109 w = dlg_find_by_id (h, id);
1110 if (w != NULL)
1111 dlg_select_widget (w);
1114 /* --------------------------------------------------------------------------------------------- */
1116 * Try to select widget in the dialog.
1119 void
1120 dlg_select_widget (void *w)
1122 Widget *widget = WIDGET (w);
1123 WDialog *h = widget->owner;
1125 if (widget_get_options (widget, WOP_TOP_SELECT))
1126 dlg_set_top_or_bottom_widget (w, TRUE);
1127 else
1128 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
1131 /* --------------------------------------------------------------------------------------------- */
1133 * Set widget at bottom of widget list.
1136 void
1137 dlg_set_bottom_widget (void *w)
1139 dlg_set_top_or_bottom_widget (w, FALSE);
1142 /* --------------------------------------------------------------------------------------------- */
1143 /** Try to select previous widget in the tab order */
1145 void
1146 dlg_one_up (WDialog * h)
1148 if (h->widgets != NULL)
1149 do_select_widget (h, dlg_widget_prev (h, h->current), SELECT_PREV);
1152 /* --------------------------------------------------------------------------------------------- */
1153 /** Try to select next widget in the tab order */
1155 void
1156 dlg_one_down (WDialog * h)
1158 if (h->widgets != NULL)
1159 do_select_widget (h, dlg_widget_next (h, h->current), SELECT_NEXT);
1162 /* --------------------------------------------------------------------------------------------- */
1164 void
1165 update_cursor (WDialog * h)
1167 GList *p = h->current;
1169 if (p != NULL && widget_get_state (WIDGET (h), WST_ACTIVE))
1171 Widget *w;
1173 w = WIDGET (p->data);
1175 if (!widget_get_state (w, WST_DISABLED) && widget_get_options (w, WOP_WANT_CURSOR))
1176 send_message (w, NULL, MSG_CURSOR, 0, NULL);
1177 else
1180 p = dlg_widget_next (h, p);
1181 if (p == h->current)
1182 break;
1184 w = WIDGET (p->data);
1186 if (!widget_get_state (w, WST_DISABLED) && widget_get_options (w, WOP_WANT_CURSOR)
1187 && send_message (w, NULL, MSG_CURSOR, 0, NULL) == MSG_HANDLED)
1188 break;
1190 while (TRUE);
1194 /* --------------------------------------------------------------------------------------------- */
1196 * Redraw the widgets in reverse order, leaving the current widget
1197 * as the last one
1200 void
1201 dlg_redraw (WDialog * h)
1203 if (!widget_get_state (WIDGET (h), WST_ACTIVE))
1204 return;
1206 if (h->winch_pending)
1208 h->winch_pending = FALSE;
1209 send_message (h, NULL, MSG_RESIZE, 0, NULL);
1212 send_message (h, NULL, MSG_DRAW, 0, NULL);
1213 dlg_broadcast_msg (h, MSG_DRAW);
1214 update_cursor (h);
1217 /* --------------------------------------------------------------------------------------------- */
1219 void
1220 dlg_stop (WDialog * h)
1222 widget_set_state (WIDGET (h), WST_CLOSED, TRUE);
1225 /* --------------------------------------------------------------------------------------------- */
1226 /** Init the process */
1228 void
1229 dlg_init (WDialog * h)
1231 Widget *wh = WIDGET (h);
1233 if (top_dlg != NULL && widget_get_state (WIDGET (top_dlg->data), WST_MODAL))
1234 widget_set_state (wh, WST_MODAL, TRUE);
1236 /* add dialog to the stack */
1237 top_dlg = g_list_prepend (top_dlg, h);
1239 /* Initialize dialog manager and widgets */
1240 if (widget_get_state (wh, WST_CONSTRUCT))
1242 if (!widget_get_state (wh, WST_MODAL))
1243 dialog_switch_add (h);
1245 send_message (h, NULL, MSG_INIT, 0, NULL);
1246 dlg_broadcast_msg (h, MSG_INIT);
1247 dlg_read_history (h);
1250 widget_set_state (wh, WST_ACTIVE, TRUE);
1252 /* first send MSG_DRAW to dialog itself and all widgets... */
1253 dlg_redraw (h);
1255 /* ...then send MSG_FOCUS to select the first widget that can take focus */
1256 while (h->current != NULL && !dlg_focus (h))
1257 h->current = dlg_widget_next (h, h->current);
1260 h->ret_value = 0;
1263 /* --------------------------------------------------------------------------------------------- */
1265 void
1266 dlg_process_event (WDialog * h, int key, Gpm_Event * event)
1268 if (key == EV_NONE)
1270 if (tty_got_interrupt ())
1271 if (send_message (h, NULL, MSG_ACTION, CK_Cancel, NULL) != MSG_HANDLED)
1272 dlg_execute_cmd (h, CK_Cancel);
1274 else if (key == EV_MOUSE)
1275 h->mouse_status = dlg_mouse_event (h, event);
1276 else
1277 dlg_key_event (h, key);
1280 /* --------------------------------------------------------------------------------------------- */
1281 /** Shutdown the dlg_run */
1283 void
1284 dlg_run_done (WDialog * h)
1286 top_dlg = g_list_remove (top_dlg, h);
1288 if (widget_get_state (WIDGET (h), WST_CLOSED))
1290 send_message (h, h->current->data, MSG_END, 0, NULL);
1291 if (!widget_get_state (WIDGET (h), WST_MODAL))
1292 dialog_switch_remove (h);
1296 /* --------------------------------------------------------------------------------------------- */
1298 * Standard run dialog routine
1299 * We have to keep this routine small so that we can duplicate it's
1300 * behavior on complex routines like the file routines, this way,
1301 * they can call the dlg_process_event without rewriting all the code
1305 dlg_run (WDialog * h)
1307 dlg_init (h);
1308 frontend_dlg_run (h);
1309 dlg_run_done (h);
1310 return h->ret_value;
1313 /* --------------------------------------------------------------------------------------------- */
1315 void
1316 dlg_destroy (WDialog * h)
1318 /* if some widgets have history, save all history at one moment here */
1319 dlg_save_history (h);
1320 dlg_broadcast_msg (h, MSG_DESTROY);
1321 g_list_free_full (h->widgets, g_free);
1322 mc_event_group_del (h->event_group);
1323 g_free (h->event_group);
1324 g_free (h->title);
1325 g_free (h);
1327 do_refresh ();
1330 /* --------------------------------------------------------------------------------------------- */
1333 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1335 void
1336 dlg_save_history (WDialog * h)
1338 char *profile;
1339 int i;
1341 if (num_history_items_recorded == 0) /* this is how to disable */
1342 return;
1344 profile = mc_config_get_full_path (MC_HISTORY_FILE);
1345 i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
1346 if (i != -1)
1347 close (i);
1349 /* Make sure the history is only readable by the user */
1350 if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
1352 ev_history_load_save_t event_data;
1354 event_data.cfg = mc_config_init (profile, FALSE);
1355 event_data.receiver = NULL;
1357 /* get all histories in dialog */
1358 mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
1360 mc_config_save_file (event_data.cfg, NULL);
1361 mc_config_deinit (event_data.cfg);
1364 g_free (profile);
1367 /* --------------------------------------------------------------------------------------------- */
1369 char *
1370 dlg_get_title (const WDialog * h, size_t len)
1372 char *t;
1374 if (h == NULL)
1375 abort ();
1377 if (h->get_title != NULL)
1378 t = h->get_title (h, len);
1379 else
1380 t = g_strdup ("");
1382 return t;
1385 /* --------------------------------------------------------------------------------------------- */