Drop old mouse API and use the new one.
[midnight-commander.git] / lib / widget / dialog.c
blob8fa00fc10989393b31e0380c4c755bffec5dbd12
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 = 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 gboolean run_click;
369 mouse_event_t me;
371 me = mouse_translate_event (w, event, &run_click);
373 return mouse_process_event (w, &me, run_click);
376 /* --------------------------------------------------------------------------------------------- */
378 static int
379 dlg_mouse_event (WDialog * h, Gpm_Event * event)
381 Widget *wh = WIDGET (h);
383 GList *p;
385 /* close the dialog by mouse left click out of dialog area */
386 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0)
387 && ((event->type & GPM_DOWN) != 0) && !mouse_global_in_widget (event, wh))
389 h->ret_value = B_CANCEL;
390 dlg_stop (h);
391 return MOU_NORMAL;
394 if (wh->mouse_callback != NULL)
396 int mou;
398 mou = dlg_mouse_translator (event, wh);
399 if (mou != MOU_UNHANDLED)
400 return mou;
403 /* send the event to widgets in reverse Z-order */
404 p = g_list_last (h->widgets);
407 Widget *w = WIDGET (p->data);
409 if ((w->options & W_DISABLED) == 0 && w->mouse_callback != NULL)
411 /* put global cursor position to the widget */
412 int ret;
414 ret = dlg_mouse_translator (event, w);
415 if (ret != MOU_UNHANDLED)
416 return ret;
419 p = g_list_previous (p);
421 while (p != NULL);
423 return MOU_UNHANDLED;
426 /* --------------------------------------------------------------------------------------------- */
428 static cb_ret_t
429 dlg_try_hotkey (WDialog * h, int d_key)
431 GList *hot_cur;
432 Widget *current;
433 cb_ret_t handled;
434 int c;
436 if (h->widgets == NULL)
437 return MSG_NOT_HANDLED;
439 if (h->current == NULL)
440 h->current = h->widgets;
443 * Explanation: we don't send letter hotkeys to other widgets if
444 * the currently selected widget is an input line
447 current = WIDGET (h->current->data);
449 if ((current->options & W_DISABLED) != 0)
450 return MSG_NOT_HANDLED;
452 if (current->options & W_IS_INPUT)
454 /* skip ascii control characters, anything else can valid character in
455 * some encoding */
456 if (d_key >= 32 && d_key < 256)
457 return MSG_NOT_HANDLED;
460 /* If it's an alt key, send the message */
461 c = d_key & ~ALT (0);
462 if (d_key & ALT (0) && g_ascii_isalpha (c))
463 d_key = g_ascii_tolower (c);
465 handled = MSG_NOT_HANDLED;
466 if ((current->options & W_WANT_HOTKEY) != 0)
467 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
469 /* If not used, send hotkey to other widgets */
470 if (handled == MSG_HANDLED)
471 return MSG_HANDLED;
473 hot_cur = dlg_widget_next (h, h->current);
475 /* send it to all widgets */
476 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
478 current = WIDGET (hot_cur->data);
480 if ((current->options & W_WANT_HOTKEY) != 0 && (current->options & W_DISABLED) == 0)
481 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
483 if (handled == MSG_NOT_HANDLED)
484 hot_cur = dlg_widget_next (h, hot_cur);
487 if (handled == MSG_HANDLED)
488 do_select_widget (h, hot_cur, SELECT_EXACT);
490 return handled;
493 /* --------------------------------------------------------------------------------------------- */
495 static void
496 dlg_key_event (WDialog * h, int d_key)
498 cb_ret_t handled;
500 if (h->widgets == NULL)
501 return;
503 if (h->current == NULL)
504 h->current = h->widgets;
506 /* TAB used to cycle */
507 if ((h->flags & DLG_WANT_TAB) == 0)
509 if (d_key == '\t')
511 dlg_one_down (h);
512 return;
514 else if ((d_key & ~(KEY_M_SHIFT | KEY_M_CTRL)) == '\t')
516 dlg_one_up (h);
517 return;
521 /* first can dlg_callback handle the key */
522 handled = send_message (h, NULL, MSG_KEY, d_key, NULL);
524 /* next try the hotkey */
525 if (handled == MSG_NOT_HANDLED)
526 handled = dlg_try_hotkey (h, d_key);
528 if (handled == MSG_HANDLED)
529 send_message (h, NULL, MSG_HOTKEY_HANDLED, 0, NULL);
530 else
531 /* not used - then try widget_callback */
532 handled = send_message (h->current->data, NULL, MSG_KEY, d_key, NULL);
534 /* not used- try to use the unhandled case */
535 if (handled == MSG_NOT_HANDLED)
536 handled = send_message (h, NULL, MSG_UNHANDLED_KEY, d_key, NULL);
538 if (handled == MSG_NOT_HANDLED)
539 handled = dlg_handle_key (h, d_key);
541 (void) handled;
542 send_message (h, NULL, MSG_POST_KEY, d_key, NULL);
545 /* --------------------------------------------------------------------------------------------- */
547 static void
548 frontend_dlg_run (WDialog * h)
550 Gpm_Event event;
552 event.x = -1;
554 /* close opened editors, viewers, etc */
555 if (!h->modal && mc_global.midnight_shutdown)
557 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
558 return;
561 while (h->state == DLG_ACTIVE)
563 int d_key;
565 if (mc_global.tty.winch_flag != 0)
566 dialog_change_screen_size ();
568 if (is_idle ())
570 if (idle_hook)
571 execute_hooks (idle_hook);
573 while ((WIDGET (h)->options & W_WANT_IDLE) != 0 && is_idle ())
574 send_message (h, NULL, MSG_IDLE, 0, NULL);
576 /* Allow terminating the dialog from the idle handler */
577 if (h->state != DLG_ACTIVE)
578 break;
581 update_cursor (h);
583 /* Clear interrupt flag */
584 tty_got_interrupt ();
585 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
587 dlg_process_event (h, d_key, &event);
589 if (h->state == DLG_CLOSED)
590 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
594 /* --------------------------------------------------------------------------------------------- */
596 static int
597 dlg_find_widget_by_id (gconstpointer a, gconstpointer b)
599 Widget *w = WIDGET (a);
600 unsigned long id = GPOINTER_TO_UINT (b);
602 return w->id == id ? 0 : 1;
605 /* --------------------------------------------------------------------------------------------- */
606 /*** public functions ****************************************************************************/
607 /* --------------------------------------------------------------------------------------------- */
609 /** Clean the dialog area, draw the frame and the title */
610 void
611 dlg_default_repaint (WDialog * h)
613 Widget *wh = WIDGET (h);
615 int space;
617 if (h->state != DLG_ACTIVE)
618 return;
620 space = (h->flags & DLG_COMPACT) ? 0 : 1;
622 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
623 dlg_erase (h);
624 tty_draw_box (wh->y + space, wh->x + space, wh->lines - 2 * space, wh->cols - 2 * space, FALSE);
626 if (h->title != NULL)
628 tty_setcolor (h->color[DLG_COLOR_TITLE]);
629 widget_move (h, space, (wh->cols - str_term_width1 (h->title)) / 2);
630 tty_print_string (h->title);
634 /* --------------------------------------------------------------------------------------------- */
635 /** this function allows to set dialog position */
637 void
638 dlg_set_position (WDialog * h, int y1, int x1, int y2, int x2)
640 Widget *wh = WIDGET (h);
642 /* save old positions, will be used to reposition childs */
643 int ox, oy, oc, ol;
644 int shift_x, shift_y, scale_x, scale_y;
646 /* save old positions, will be used to reposition childs */
647 ox = wh->x;
648 oy = wh->y;
649 oc = wh->cols;
650 ol = wh->lines;
652 wh->x = x1;
653 wh->y = y1;
654 wh->lines = y2 - y1;
655 wh->cols = x2 - x1;
657 /* dialog is empty */
658 if (h->widgets == NULL)
659 return;
661 if (h->current == NULL)
662 h->current = h->widgets;
664 /* values by which controls should be moved */
665 shift_x = wh->x - ox;
666 shift_y = wh->y - oy;
667 scale_x = wh->cols - oc;
668 scale_y = wh->lines - ol;
670 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
672 GList *w;
674 for (w = h->widgets; w != NULL; w = g_list_next (w))
676 /* there are, mainly, 2 generally possible
677 situations:
679 1. control sticks to one side - it
680 should be moved
682 2. control sticks to two sides of
683 one direction - it should be sized */
685 Widget *c = WIDGET (w->data);
686 int x = c->x;
687 int y = c->y;
688 int cols = c->cols;
689 int lines = c->lines;
691 if ((c->pos_flags & WPOS_CENTER_HORZ) != 0)
692 x = wh->x + (wh->cols - c->cols) / 2;
693 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0 && (c->pos_flags & WPOS_KEEP_RIGHT) != 0)
695 x += shift_x;
696 cols += scale_x;
698 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0)
699 x += shift_x;
700 else if ((c->pos_flags & WPOS_KEEP_RIGHT) != 0)
701 x += shift_x + scale_x;
703 if ((c->pos_flags & WPOS_CENTER_VERT) != 0)
704 y = wh->y + (wh->lines - c->lines) / 2;
705 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0 && (c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
707 y += shift_y;
708 lines += scale_y;
710 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0)
711 y += shift_y;
712 else if ((c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
713 y += shift_y + scale_y;
715 widget_set_size (c, y, x, lines, cols);
720 /* --------------------------------------------------------------------------------------------- */
721 /** Set dialog size and position */
723 void
724 dlg_set_size (WDialog * h, int lines, int cols)
726 int x = WIDGET (h)->x;
727 int y = WIDGET (h)->y;
729 if ((h->flags & DLG_CENTER) != 0)
731 y = (LINES - lines) / 2;
732 x = (COLS - cols) / 2;
735 if ((h->flags & DLG_TRYUP) != 0)
737 if (y > 3)
738 y -= 2;
739 else if (y == 3)
740 y = 2;
743 dlg_set_position (h, y, x, y + lines, x + cols);
746 /* --------------------------------------------------------------------------------------------- */
747 /** Default dialog callback */
749 cb_ret_t
750 dlg_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
752 WDialog *h = DIALOG (w);
754 (void) sender;
755 (void) parm;
756 (void) data;
758 switch (msg)
760 case MSG_DRAW:
761 if (h->color != NULL)
763 dlg_default_repaint (h);
764 return MSG_HANDLED;
766 return MSG_NOT_HANDLED;
768 case MSG_IDLE:
769 dlg_broadcast_msg_to (h, MSG_IDLE, FALSE, W_WANT_IDLE);
770 return MSG_HANDLED;
772 case MSG_RESIZE:
773 /* this is default resizing mechanism */
774 /* the main idea of this code is to resize dialog
775 according to flags (if any of flags require automatic
776 resizing, like DLG_CENTER, end after that reposition
777 controls in dialog according to flags of widget) */
778 dlg_set_size (h, w->lines, w->cols);
779 return MSG_HANDLED;
781 default:
782 break;
785 return MSG_NOT_HANDLED;
788 /* --------------------------------------------------------------------------------------------- */
790 WDialog *
791 dlg_create (gboolean modal, int y1, int x1, int lines, int cols,
792 const int *colors, widget_cb_fn callback, widget_mouse_cb_fn mouse_callback,
793 const char *help_ctx, const char *title, dlg_flags_t flags)
795 WDialog *new_d;
796 Widget *w;
798 new_d = g_new0 (WDialog, 1);
799 w = WIDGET (new_d);
800 widget_init (w, y1, x1, lines, cols, (callback != NULL) ? callback : dlg_default_callback,
801 mouse_callback);
802 widget_want_cursor (w, FALSE);
804 new_d->state = DLG_CONSTRUCT;
805 new_d->modal = modal;
806 new_d->color = colors;
807 new_d->help_ctx = help_ctx;
808 new_d->flags = flags;
809 new_d->data = NULL;
811 dlg_set_size (new_d, lines, cols);
812 new_d->fullscreen = (w->x == 0 && w->y == 0 && w->cols == COLS && w->lines == LINES);
814 new_d->mouse_status = MOU_UNHANDLED;
816 /* Strip existing spaces, add one space before and after the title */
817 if (title != NULL && *title != '\0')
819 char *t;
821 t = g_strstrip (g_strdup (title));
822 if (*t != '\0')
823 new_d->title = g_strdup_printf (" %s ", t);
824 g_free (t);
827 /* unique name of event group for this dialog */
828 new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
830 return new_d;
833 /* --------------------------------------------------------------------------------------------- */
835 void
836 dlg_set_default_colors (void)
838 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
839 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
840 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
841 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
842 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
844 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
845 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
846 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
847 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
848 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
850 listbox_colors[DLG_COLOR_NORMAL] = PMENU_ENTRY_COLOR;
851 listbox_colors[DLG_COLOR_FOCUS] = PMENU_SELECTED_COLOR;
852 listbox_colors[DLG_COLOR_HOT_NORMAL] = PMENU_ENTRY_COLOR;
853 listbox_colors[DLG_COLOR_HOT_FOCUS] = PMENU_SELECTED_COLOR;
854 listbox_colors[DLG_COLOR_TITLE] = PMENU_TITLE_COLOR;
857 /* --------------------------------------------------------------------------------------------- */
859 void
860 dlg_erase (WDialog * h)
862 if ((h != NULL) && (h->state == DLG_ACTIVE))
864 Widget *wh = WIDGET (h);
866 tty_fill_region (wh->y, wh->x, wh->lines, wh->cols, ' ');
870 /* --------------------------------------------------------------------------------------------- */
872 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
875 unsigned long
876 add_widget_autopos (WDialog * h, void *w, widget_pos_flags_t pos_flags, const void *before)
878 Widget *wh = WIDGET (h);
879 Widget *widget;
881 /* Don't accept 0 widgets */
882 if (w == NULL)
883 abort ();
885 widget = WIDGET (w);
887 if ((pos_flags & WPOS_CENTER_HORZ) != 0)
888 widget->x = (wh->cols - widget->cols) / 2;
889 widget->x += wh->x;
891 if ((pos_flags & WPOS_CENTER_VERT) != 0)
892 widget->y = (wh->lines - widget->lines) / 2;
893 widget->y += wh->y;
895 widget->owner = h;
896 widget->pos_flags = pos_flags;
897 widget->id = h->widget_id++;
899 if (h->widgets == NULL || before == NULL)
901 h->widgets = g_list_append (h->widgets, widget);
902 h->current = g_list_last (h->widgets);
904 else
906 GList *b;
908 b = g_list_find (h->widgets, before);
910 /* don't accept widget not from dialog. This shouldn't happen */
911 if (b == NULL)
912 abort ();
914 b = g_list_next (b);
915 h->widgets = g_list_insert_before (h->widgets, b, widget);
916 if (b != NULL)
917 h->current = g_list_previous (b);
918 else
919 h->current = g_list_last (h->widgets);
922 /* widget has been added in runtime */
923 if (h->state == DLG_ACTIVE)
925 send_message (widget, NULL, MSG_INIT, 0, NULL);
926 send_message (widget, NULL, MSG_DRAW, 0, NULL);
927 send_message (widget, NULL, MSG_FOCUS, 0, NULL);
930 return widget->id;
933 /* --------------------------------------------------------------------------------------------- */
934 /** wrapper to simply add lefttop positioned controls */
936 unsigned long
937 add_widget (WDialog * h, void *w)
939 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT,
940 h->current != NULL ? h->current->data : NULL);
943 /* --------------------------------------------------------------------------------------------- */
945 unsigned long
946 add_widget_before (WDialog * h, void *w, void *before)
948 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT, before);
951 /* --------------------------------------------------------------------------------------------- */
953 /** delete widget from dialog */
954 void
955 del_widget (void *w)
957 WDialog *h;
958 GList *d;
960 /* Don't accept NULL widget. This shouldn't happen */
961 if (w == NULL)
962 abort ();
964 h = WIDGET (w)->owner;
966 d = g_list_find (h->widgets, w);
967 if (d == h->current)
968 h->current = dlg_widget_next (h, d);
970 h->widgets = g_list_remove_link (h->widgets, d);
971 send_message (d->data, NULL, MSG_DESTROY, 0, NULL);
972 g_free (d->data);
973 g_list_free_1 (d);
975 /* widget has been deleted in runtime */
976 if (h->state == DLG_ACTIVE)
978 dlg_redraw (h);
979 dlg_focus (h);
983 /* --------------------------------------------------------------------------------------------- */
985 void
986 do_refresh (void)
988 GList *d = top_dlg;
990 if (fast_refresh)
992 if ((d != NULL) && (d->data != NULL))
993 dlg_redraw (DIALOG (d->data));
995 else
997 /* Search first fullscreen dialog */
998 for (; d != NULL; d = g_list_next (d))
999 if (d->data != NULL && DIALOG (d->data)->fullscreen)
1000 break;
1001 /* back to top dialog */
1002 for (; d != NULL; d = g_list_previous (d))
1003 if (d->data != NULL)
1004 dlg_redraw (DIALOG (d->data));
1008 /* --------------------------------------------------------------------------------------------- */
1009 /** broadcast a message to all the widgets in a dialog */
1011 void
1012 dlg_broadcast_msg (WDialog * h, widget_msg_t msg)
1014 dlg_broadcast_msg_to (h, msg, FALSE, 0);
1017 /* --------------------------------------------------------------------------------------------- */
1019 gboolean
1020 dlg_focus (WDialog * h)
1022 /* cannot focus disabled widget */
1023 if ((h->current != NULL) && (h->state == DLG_CONSTRUCT || h->state == DLG_ACTIVE))
1025 Widget *current = WIDGET (h->current->data);
1027 if (((current->options & W_DISABLED) == 0)
1028 && (send_message (current, NULL, MSG_FOCUS, 0, NULL) == MSG_HANDLED))
1030 send_message (h, current, MSG_FOCUS, 0, NULL);
1031 return TRUE;
1035 return FALSE;
1038 /* --------------------------------------------------------------------------------------------- */
1039 /** Find the widget with the given callback in the dialog h */
1041 Widget *
1042 find_widget_type (const WDialog * h, widget_cb_fn callback)
1044 GList *w;
1046 w = g_list_find_custom (h->widgets, callback, dlg_find_widget_callback);
1048 return (w == NULL) ? NULL : WIDGET (w->data);
1051 /* --------------------------------------------------------------------------------------------- */
1052 /** Find the widget with the given id */
1054 Widget *
1055 dlg_find_by_id (const WDialog * h, unsigned long id)
1057 GList *w;
1059 w = g_list_find_custom (h->widgets, GUINT_TO_POINTER (id), dlg_find_widget_by_id);
1060 return w != NULL ? WIDGET (w->data) : NULL;
1063 /* --------------------------------------------------------------------------------------------- */
1064 /** Find the widget with the given id in the dialog h and select it */
1066 void
1067 dlg_select_by_id (const WDialog * h, unsigned long id)
1069 Widget *w;
1071 w = dlg_find_by_id (h, id);
1072 if (w != NULL)
1073 dlg_select_widget (w);
1076 /* --------------------------------------------------------------------------------------------- */
1078 * Try to select widget in the dialog.
1081 void
1082 dlg_select_widget (void *w)
1084 Widget *widget = WIDGET (w);
1085 WDialog *h = widget->owner;
1087 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
1090 /* --------------------------------------------------------------------------------------------- */
1092 static void
1093 dlg_set_top_or_bottom_widget (void *w, gboolean set_top)
1095 Widget *widget = WIDGET (w);
1096 WDialog *h = widget->owner;
1097 GList *l;
1099 l = g_list_find (h->widgets, w);
1100 if (l == NULL)
1101 abort (); /* widget is not in dialog, this should not happen */
1103 /* unfocus prevoius widget and focus current one before widget reordering */
1104 if (set_top && h->state == DLG_ACTIVE)
1105 do_select_widget (h, l, SELECT_EXACT);
1107 /* widget reordering */
1108 h->widgets = g_list_remove_link (h->widgets, l);
1109 if (set_top)
1110 h->widgets = g_list_concat (h->widgets, l);
1111 else
1112 h->widgets = g_list_concat (l, h->widgets);
1115 /* --------------------------------------------------------------------------------------------- */
1117 * Set widget at top of widget list and make it current.
1120 void
1121 dlg_set_top_widget (void *w)
1123 dlg_set_top_or_bottom_widget (w, TRUE);
1126 /* --------------------------------------------------------------------------------------------- */
1128 * Set widget at bottom of widget list.
1131 void
1132 dlg_set_bottom_widget (void *w)
1134 dlg_set_top_or_bottom_widget (w, FALSE);
1137 /* --------------------------------------------------------------------------------------------- */
1138 /** Try to select previous widget in the tab order */
1140 void
1141 dlg_one_up (WDialog * h)
1143 if (h->widgets != NULL)
1144 do_select_widget (h, dlg_widget_prev (h, h->current), SELECT_PREV);
1147 /* --------------------------------------------------------------------------------------------- */
1148 /** Try to select next widget in the tab order */
1150 void
1151 dlg_one_down (WDialog * h)
1153 if (h->widgets != NULL)
1154 do_select_widget (h, dlg_widget_next (h, h->current), SELECT_NEXT);
1157 /* --------------------------------------------------------------------------------------------- */
1159 void
1160 update_cursor (WDialog * h)
1162 GList *p = h->current;
1164 if ((p != NULL) && (h->state == DLG_ACTIVE))
1166 Widget *w;
1168 w = WIDGET (p->data);
1170 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1171 send_message (w, NULL, MSG_CURSOR, 0, NULL);
1172 else
1175 p = dlg_widget_next (h, p);
1176 if (p == h->current)
1177 break;
1179 w = WIDGET (p->data);
1181 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1182 if (send_message (w, NULL, MSG_CURSOR, 0, NULL) == MSG_HANDLED)
1183 break;
1185 while (TRUE);
1189 /* --------------------------------------------------------------------------------------------- */
1191 * Redraw the widgets in reverse order, leaving the current widget
1192 * as the last one
1195 void
1196 dlg_redraw (WDialog * h)
1198 if (h->state != DLG_ACTIVE)
1199 return;
1201 if (h->winch_pending)
1203 h->winch_pending = FALSE;
1204 send_message (h, NULL, MSG_RESIZE, 0, NULL);
1207 send_message (h, NULL, MSG_DRAW, 0, NULL);
1208 dlg_broadcast_msg (h, MSG_DRAW);
1209 update_cursor (h);
1212 /* --------------------------------------------------------------------------------------------- */
1214 void
1215 dlg_stop (WDialog * h)
1217 h->state = DLG_CLOSED;
1220 /* --------------------------------------------------------------------------------------------- */
1221 /** Init the process */
1223 void
1224 dlg_init (WDialog * h)
1226 if (top_dlg != NULL && DIALOG (top_dlg->data)->modal)
1227 h->modal = TRUE;
1229 /* add dialog to the stack */
1230 top_dlg = g_list_prepend (top_dlg, h);
1232 /* Initialize dialog manager and widgets */
1233 if (h->state == DLG_CONSTRUCT)
1235 if (!h->modal)
1236 dialog_switch_add (h);
1238 send_message (h, NULL, MSG_INIT, 0, NULL);
1239 dlg_broadcast_msg (h, MSG_INIT);
1240 dlg_read_history (h);
1243 h->state = DLG_ACTIVE;
1245 /* first send MSG_DRAW to dialog itself and all widgets... */
1246 dlg_redraw (h);
1248 /* ...then send MSG_FOCUS to select the first widget that can take focus */
1249 while (h->current != NULL && !dlg_focus (h))
1250 h->current = dlg_widget_next (h, h->current);
1253 h->ret_value = 0;
1256 /* --------------------------------------------------------------------------------------------- */
1258 void
1259 dlg_process_event (WDialog * h, int key, Gpm_Event * event)
1261 if (key == EV_NONE)
1263 if (tty_got_interrupt ())
1264 if (send_message (h, NULL, MSG_ACTION, CK_Cancel, NULL) != MSG_HANDLED)
1265 dlg_execute_cmd (h, CK_Cancel);
1267 else if (key == EV_MOUSE)
1268 h->mouse_status = dlg_mouse_event (h, event);
1269 else
1270 dlg_key_event (h, key);
1273 /* --------------------------------------------------------------------------------------------- */
1274 /** Shutdown the dlg_run */
1276 void
1277 dlg_run_done (WDialog * h)
1279 top_dlg = g_list_remove (top_dlg, h);
1281 if (h->state == DLG_CLOSED)
1283 send_message (h, h->current->data, MSG_END, 0, NULL);
1284 if (!h->modal)
1285 dialog_switch_remove (h);
1289 /* --------------------------------------------------------------------------------------------- */
1291 * Standard run dialog routine
1292 * We have to keep this routine small so that we can duplicate it's
1293 * behavior on complex routines like the file routines, this way,
1294 * they can call the dlg_process_event without rewriting all the code
1298 dlg_run (WDialog * h)
1300 dlg_init (h);
1301 frontend_dlg_run (h);
1302 dlg_run_done (h);
1303 return h->ret_value;
1306 /* --------------------------------------------------------------------------------------------- */
1308 void
1309 dlg_destroy (WDialog * h)
1311 /* if some widgets have history, save all history at one moment here */
1312 dlg_save_history (h);
1313 dlg_broadcast_msg (h, MSG_DESTROY);
1314 g_list_free_full (h->widgets, g_free);
1315 mc_event_group_del (h->event_group);
1316 g_free (h->event_group);
1317 g_free (h->title);
1318 g_free (h);
1320 do_refresh ();
1323 /* --------------------------------------------------------------------------------------------- */
1326 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1328 void
1329 dlg_save_history (WDialog * h)
1331 char *profile;
1332 int i;
1334 if (num_history_items_recorded == 0) /* this is how to disable */
1335 return;
1337 profile = mc_config_get_full_path (MC_HISTORY_FILE);
1338 i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
1339 if (i != -1)
1340 close (i);
1342 /* Make sure the history is only readable by the user */
1343 if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
1345 ev_history_load_save_t event_data;
1347 event_data.cfg = mc_config_init (profile, FALSE);
1348 event_data.receiver = NULL;
1350 /* get all histories in dialog */
1351 mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
1353 mc_config_save_file (event_data.cfg, NULL);
1354 mc_config_deinit (event_data.cfg);
1357 g_free (profile);
1360 /* --------------------------------------------------------------------------------------------- */
1362 char *
1363 dlg_get_title (const WDialog * h, size_t len)
1365 char *t;
1367 if (h == NULL)
1368 abort ();
1370 if (h->get_title != NULL)
1371 t = h->get_title (h, len);
1372 else
1373 t = g_strdup ("");
1375 return t;
1378 /* --------------------------------------------------------------------------------------------- */