Merge branch '3578_mcext_fixes'
[midnight-commander.git] / lib / widget / dialog.c
bloba53c046960042445df225846dc7e647e2f29ae4b
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/widget.h"
44 #include "lib/fileloc.h" /* MC_HISTORY_FILE */
45 #include "lib/event.h" /* mc_event_raise() */
47 /*** global variables ****************************************************************************/
49 /* Color styles for normal and error dialogs */
50 dlg_colors_t dialog_colors;
51 dlg_colors_t alarm_colors;
52 dlg_colors_t listbox_colors;
54 /* Primitive way to check if the the current dialog is our dialog */
55 /* This is needed by async routines like load_prompt */
56 GList *top_dlg = NULL;
58 /* A hook list for idle events */
59 hook_t *idle_hook = NULL;
61 /* If set then dialogs just clean the screen when refreshing, else */
62 /* they do a complete refresh, refreshing all the parts of the program */
63 int fast_refresh = 0;
65 /* left click outside of dialog closes it */
66 int mouse_close_dialog = 0;
68 const global_keymap_t *dialog_map = NULL;
70 /*** file scope macro definitions ****************************************************************/
72 /*** file scope type declarations ****************************************************************/
74 /** What to do if the requested widget doesn't take focus */
75 typedef enum
77 SELECT_NEXT, /* go the the next widget */
78 SELECT_PREV, /* go the the previous widget */
79 SELECT_EXACT /* use current widget */
80 } select_dir_t;
82 /*** file scope variables ************************************************************************/
84 /*** file scope functions ************************************************************************/
86 static GList *
87 dlg_widget_next (WDialog * h, GList * l)
89 GList *next;
91 next = g_list_next (l);
92 if (next == NULL)
93 next = h->widgets;
95 return next;
98 /* --------------------------------------------------------------------------------------------- */
100 static GList *
101 dlg_widget_prev (WDialog * h, GList * l)
103 GList *prev;
105 prev = g_list_previous (l);
106 if (prev == NULL)
107 prev = g_list_last (h->widgets);
109 return prev;
112 /* --------------------------------------------------------------------------------------------- */
114 * broadcast a message to all the widgets in a dialog that have
115 * the options set to flags. If flags is zero, the message is sent
116 * to all widgets.
119 static void
120 dlg_broadcast_msg_to (WDialog * h, widget_msg_t msg, gboolean reverse, int flags)
122 GList *p, *first;
124 if (h->widgets == NULL)
125 return;
127 if (h->current == NULL)
128 h->current = h->widgets;
130 if (reverse)
131 p = dlg_widget_prev (h, h->current);
132 else
133 p = dlg_widget_next (h, h->current);
135 first = p;
139 Widget *w = WIDGET (p->data);
141 if (reverse)
142 p = dlg_widget_prev (h, p);
143 else
144 p = dlg_widget_next (h, p);
146 if ((flags == 0) || ((flags & w->options) != 0))
147 send_message (w, NULL, msg, 0, NULL);
149 while (first != p);
152 /* --------------------------------------------------------------------------------------------- */
155 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
157 static void
158 dlg_read_history (WDialog * h)
160 char *profile;
161 ev_history_load_save_t event_data;
163 if (num_history_items_recorded == 0) /* this is how to disable */
164 return;
166 profile = mc_config_get_full_path (MC_HISTORY_FILE);
167 event_data.cfg = mc_config_init (profile, TRUE);
168 event_data.receiver = NULL;
170 /* create all histories in dialog */
171 mc_event_raise (h->event_group, MCEVENT_HISTORY_LOAD, &event_data);
173 mc_config_deinit (event_data.cfg);
174 g_free (profile);
177 /* --------------------------------------------------------------------------------------------- */
179 static gboolean
180 dlg_unfocus (WDialog * h)
182 /* we can unfocus disabled widget */
183 if ((h->current != NULL) && (h->state == DLG_CONSTRUCT || h->state == DLG_ACTIVE))
185 Widget *current = WIDGET (h->current->data);
187 if (send_message (current, NULL, MSG_UNFOCUS, 0, NULL) == MSG_HANDLED)
189 send_message (h, current, MSG_UNFOCUS, 0, NULL);
190 return TRUE;
194 return FALSE;
197 /* --------------------------------------------------------------------------------------------- */
199 static int
200 dlg_find_widget_callback (const void *a, const void *b)
202 const Widget *w = WIDGET (a);
203 widget_cb_fn f = (widget_cb_fn) b;
205 return (w->callback == f) ? 0 : 1;
208 /* --------------------------------------------------------------------------------------------- */
210 * Try to select another widget. If forward is set, follow tab order.
211 * Otherwise go to the previous widget.
214 static void
215 do_select_widget (WDialog * h, GList * w, select_dir_t dir)
217 Widget *w0 = WIDGET (h->current->data);
219 if (!dlg_unfocus (h))
220 return;
222 h->current = w;
226 if (dlg_focus (h))
227 break;
229 switch (dir)
231 case SELECT_EXACT:
232 h->current = g_list_find (h->widgets, w0);
233 if (dlg_focus (h))
234 return;
235 /* try find another widget that can take focus */
236 dir = SELECT_NEXT;
237 /* fallthrough */
238 case SELECT_NEXT:
239 h->current = dlg_widget_next (h, h->current);
240 break;
241 case SELECT_PREV:
242 h->current = dlg_widget_prev (h, h->current);
243 break;
244 default:
245 break;
248 while (h->current != w /* && (WIDGET (h->current->data)->options & W_DISABLED) == 0 */ );
250 if (widget_overlapped (w0, WIDGET (h->current->data)))
252 send_message (h->current->data, NULL, MSG_DRAW, 0, NULL);
253 send_message (h->current->data, NULL, MSG_FOCUS, 0, NULL);
257 /* --------------------------------------------------------------------------------------------- */
259 static void
260 refresh_cmd (void)
262 #ifdef HAVE_SLANG
263 tty_touch_screen ();
264 mc_refresh ();
265 #else
266 /* Use this if the refreshes fail */
267 clr_scr ();
268 repaint_screen ();
269 #endif /* HAVE_SLANG */
272 /* --------------------------------------------------------------------------------------------- */
274 static cb_ret_t
275 dlg_execute_cmd (WDialog * h, long command)
277 cb_ret_t ret = MSG_HANDLED;
278 switch (command)
280 case CK_Ok:
281 h->ret_value = B_ENTER;
282 dlg_stop (h);
283 break;
284 case CK_Cancel:
285 h->ret_value = B_CANCEL;
286 dlg_stop (h);
287 break;
289 case CK_Up:
290 case CK_Left:
291 dlg_one_up (h);
292 break;
293 case CK_Down:
294 case CK_Right:
295 dlg_one_down (h);
296 break;
298 case CK_Help:
300 ev_help_t event_data = { NULL, h->help_ctx };
301 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
303 break;
305 case CK_Suspend:
306 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
307 refresh_cmd ();
308 break;
309 case CK_Refresh:
310 refresh_cmd ();
311 break;
313 case CK_ScreenList:
314 if (!h->modal)
315 dialog_switch_list ();
316 else
317 ret = MSG_NOT_HANDLED;
318 break;
319 case CK_ScreenNext:
320 if (!h->modal)
321 dialog_switch_next ();
322 else
323 ret = MSG_NOT_HANDLED;
324 break;
325 case CK_ScreenPrev:
326 if (!h->modal)
327 dialog_switch_prev ();
328 else
329 ret = MSG_NOT_HANDLED;
330 break;
332 default:
333 ret = MSG_NOT_HANDLED;
336 return ret;
339 /* --------------------------------------------------------------------------------------------- */
341 static cb_ret_t
342 dlg_handle_key (WDialog * h, int d_key)
344 long command;
346 command = keybind_lookup_keymap_command (dialog_map, d_key);
348 if (command == CK_IgnoreKey)
349 return MSG_NOT_HANDLED;
351 if (send_message (h, NULL, MSG_ACTION, command, NULL) == MSG_HANDLED
352 || dlg_execute_cmd (h, command) == MSG_HANDLED)
353 return MSG_HANDLED;
355 return MSG_NOT_HANDLED;
358 /* --------------------------------------------------------------------------------------------- */
360 static int
361 dlg_mouse_event (WDialog * h, Gpm_Event * event)
363 Widget *wh = WIDGET (h);
365 GList *p, *first;
367 /* close the dialog by mouse left click out of dialog area */
368 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0)
369 && ((event->type & GPM_DOWN) != 0) && !mouse_global_in_widget (event, wh))
371 h->ret_value = B_CANCEL;
372 dlg_stop (h);
373 return MOU_NORMAL;
376 if (wh->mouse != NULL)
378 int mou;
380 mou = wh->mouse (event, wh);
381 if (mou != MOU_UNHANDLED)
382 return mou;
385 first = h->current;
386 p = first;
390 Widget *w = WIDGET (p->data);
392 p = dlg_widget_prev (h, p);
394 if ((w->options & W_DISABLED) == 0 && w->mouse != NULL)
396 /* put global cursor position to the widget */
397 int ret;
399 ret = w->mouse (event, w);
400 if (ret != MOU_UNHANDLED)
401 return ret;
404 while (p != first);
406 return MOU_UNHANDLED;
409 /* --------------------------------------------------------------------------------------------- */
411 static cb_ret_t
412 dlg_try_hotkey (WDialog * h, int d_key)
414 GList *hot_cur;
415 Widget *current;
416 cb_ret_t handled;
417 int c;
419 if (h->widgets == NULL)
420 return MSG_NOT_HANDLED;
422 if (h->current == NULL)
423 h->current = h->widgets;
426 * Explanation: we don't send letter hotkeys to other widgets if
427 * the currently selected widget is an input line
430 current = WIDGET (h->current->data);
432 if ((current->options & W_DISABLED) != 0)
433 return MSG_NOT_HANDLED;
435 if (current->options & W_IS_INPUT)
437 /* skip ascii control characters, anything else can valid character in
438 * some encoding */
439 if (d_key >= 32 && d_key < 256)
440 return MSG_NOT_HANDLED;
443 /* If it's an alt key, send the message */
444 c = d_key & ~ALT (0);
445 if (d_key & ALT (0) && g_ascii_isalpha (c))
446 d_key = g_ascii_tolower (c);
448 handled = MSG_NOT_HANDLED;
449 if ((current->options & W_WANT_HOTKEY) != 0)
450 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
452 /* If not used, send hotkey to other widgets */
453 if (handled == MSG_HANDLED)
454 return MSG_HANDLED;
456 hot_cur = dlg_widget_next (h, h->current);
458 /* send it to all widgets */
459 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
461 current = WIDGET (hot_cur->data);
463 if ((current->options & W_WANT_HOTKEY) != 0 && (current->options & W_DISABLED) == 0)
464 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
466 if (handled == MSG_NOT_HANDLED)
467 hot_cur = dlg_widget_next (h, hot_cur);
470 if (handled == MSG_HANDLED)
471 do_select_widget (h, hot_cur, SELECT_EXACT);
473 return handled;
476 /* --------------------------------------------------------------------------------------------- */
478 static void
479 dlg_key_event (WDialog * h, int d_key)
481 cb_ret_t handled;
483 if (h->widgets == NULL)
484 return;
486 if (h->current == NULL)
487 h->current = h->widgets;
489 /* TAB used to cycle */
490 if ((h->flags & DLG_WANT_TAB) == 0)
492 if (d_key == '\t')
494 dlg_one_down (h);
495 return;
497 else if ((d_key & ~(KEY_M_SHIFT | KEY_M_CTRL)) == '\t')
499 dlg_one_up (h);
500 return;
504 /* first can dlg_callback handle the key */
505 handled = send_message (h, NULL, MSG_KEY, d_key, NULL);
507 /* next try the hotkey */
508 if (handled == MSG_NOT_HANDLED)
509 handled = dlg_try_hotkey (h, d_key);
511 if (handled == MSG_HANDLED)
512 send_message (h, NULL, MSG_HOTKEY_HANDLED, 0, NULL);
513 else
514 /* not used - then try widget_callback */
515 handled = send_message (h->current->data, NULL, MSG_KEY, d_key, NULL);
517 /* not used- try to use the unhandled case */
518 if (handled == MSG_NOT_HANDLED)
519 handled = send_message (h, NULL, MSG_UNHANDLED_KEY, d_key, NULL);
521 if (handled == MSG_NOT_HANDLED)
522 handled = dlg_handle_key (h, d_key);
524 (void) handled;
525 send_message (h, NULL, MSG_POST_KEY, d_key, NULL);
528 /* --------------------------------------------------------------------------------------------- */
530 static void
531 frontend_dlg_run (WDialog * h)
533 Gpm_Event event;
535 event.x = -1;
537 /* close opened editors, viewers, etc */
538 if (!h->modal && mc_global.midnight_shutdown)
540 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
541 return;
544 while (h->state == DLG_ACTIVE)
546 int d_key;
548 if (mc_global.tty.winch_flag != 0)
549 dialog_change_screen_size ();
551 if (is_idle ())
553 if (idle_hook)
554 execute_hooks (idle_hook);
556 while ((WIDGET (h)->options & W_WANT_IDLE) != 0 && is_idle ())
557 send_message (h, NULL, MSG_IDLE, 0, NULL);
559 /* Allow terminating the dialog from the idle handler */
560 if (h->state != DLG_ACTIVE)
561 break;
564 update_cursor (h);
566 /* Clear interrupt flag */
567 tty_got_interrupt ();
568 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
570 dlg_process_event (h, d_key, &event);
572 if (h->state == DLG_CLOSED)
573 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
577 /* --------------------------------------------------------------------------------------------- */
579 static int
580 dlg_find_widget_by_id (gconstpointer a, gconstpointer b)
582 Widget *w = WIDGET (a);
583 unsigned long id = GPOINTER_TO_UINT (b);
585 return w->id == id ? 0 : 1;
588 /* --------------------------------------------------------------------------------------------- */
589 /*** public functions ****************************************************************************/
590 /* --------------------------------------------------------------------------------------------- */
592 /** Clean the dialog area, draw the frame and the title */
593 void
594 dlg_default_repaint (WDialog * h)
596 Widget *wh = WIDGET (h);
598 int space;
600 if (h->state != DLG_ACTIVE)
601 return;
603 space = (h->flags & DLG_COMPACT) ? 0 : 1;
605 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
606 dlg_erase (h);
607 tty_draw_box (wh->y + space, wh->x + space, wh->lines - 2 * space, wh->cols - 2 * space, FALSE);
609 if (h->title != NULL)
611 tty_setcolor (h->color[DLG_COLOR_TITLE]);
612 widget_move (h, space, (wh->cols - str_term_width1 (h->title)) / 2);
613 tty_print_string (h->title);
617 /* --------------------------------------------------------------------------------------------- */
618 /** this function allows to set dialog position */
620 void
621 dlg_set_position (WDialog * h, int y1, int x1, int y2, int x2)
623 Widget *wh = WIDGET (h);
625 /* save old positions, will be used to reposition childs */
626 int ox, oy, oc, ol;
627 int shift_x, shift_y, scale_x, scale_y;
629 /* save old positions, will be used to reposition childs */
630 ox = wh->x;
631 oy = wh->y;
632 oc = wh->cols;
633 ol = wh->lines;
635 wh->x = x1;
636 wh->y = y1;
637 wh->lines = y2 - y1;
638 wh->cols = x2 - x1;
640 /* dialog is empty */
641 if (h->widgets == NULL)
642 return;
644 if (h->current == NULL)
645 h->current = h->widgets;
647 /* values by which controls should be moved */
648 shift_x = wh->x - ox;
649 shift_y = wh->y - oy;
650 scale_x = wh->cols - oc;
651 scale_y = wh->lines - ol;
653 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
655 GList *w;
657 for (w = h->widgets; w != NULL; w = g_list_next (w))
659 /* there are, mainly, 2 generally possible
660 situations:
662 1. control sticks to one side - it
663 should be moved
665 2. control sticks to two sides of
666 one direction - it should be sized */
668 Widget *c = WIDGET (w->data);
669 int x = c->x;
670 int y = c->y;
671 int cols = c->cols;
672 int lines = c->lines;
674 if ((c->pos_flags & WPOS_CENTER_HORZ) != 0)
675 x = wh->x + (wh->cols - c->cols) / 2;
676 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0 && (c->pos_flags & WPOS_KEEP_RIGHT) != 0)
678 x += shift_x;
679 cols += scale_x;
681 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0)
682 x += shift_x;
683 else if ((c->pos_flags & WPOS_KEEP_RIGHT) != 0)
684 x += shift_x + scale_x;
686 if ((c->pos_flags & WPOS_CENTER_VERT) != 0)
687 y = wh->y + (wh->lines - c->lines) / 2;
688 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0 && (c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
690 y += shift_y;
691 lines += scale_y;
693 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0)
694 y += shift_y;
695 else if ((c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
696 y += shift_y + scale_y;
698 widget_set_size (c, y, x, lines, cols);
703 /* --------------------------------------------------------------------------------------------- */
704 /** Set dialog size and position */
706 void
707 dlg_set_size (WDialog * h, int lines, int cols)
709 int x = WIDGET (h)->x;
710 int y = WIDGET (h)->y;
712 if ((h->flags & DLG_CENTER) != 0)
714 y = (LINES - lines) / 2;
715 x = (COLS - cols) / 2;
718 if ((h->flags & DLG_TRYUP) != 0)
720 if (y > 3)
721 y -= 2;
722 else if (y == 3)
723 y = 2;
726 dlg_set_position (h, y, x, y + lines, x + cols);
729 /* --------------------------------------------------------------------------------------------- */
730 /** Default dialog callback */
732 cb_ret_t
733 dlg_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
735 WDialog *h = DIALOG (w);
737 (void) sender;
738 (void) parm;
739 (void) data;
741 switch (msg)
743 case MSG_DRAW:
744 if (h->color != NULL)
746 dlg_default_repaint (h);
747 return MSG_HANDLED;
749 return MSG_NOT_HANDLED;
751 case MSG_IDLE:
752 dlg_broadcast_msg_to (h, MSG_IDLE, FALSE, W_WANT_IDLE);
753 return MSG_HANDLED;
755 case MSG_RESIZE:
756 /* this is default resizing mechanism */
757 /* the main idea of this code is to resize dialog
758 according to flags (if any of flags require automatic
759 resizing, like DLG_CENTER, end after that reposition
760 controls in dialog according to flags of widget) */
761 dlg_set_size (h, w->lines, w->cols);
762 return MSG_HANDLED;
764 default:
765 break;
768 return MSG_NOT_HANDLED;
771 /* --------------------------------------------------------------------------------------------- */
773 WDialog *
774 dlg_create (gboolean modal, int y1, int x1, int lines, int cols,
775 const int *colors, widget_cb_fn callback, mouse_h mouse_handler,
776 const char *help_ctx, const char *title, dlg_flags_t flags)
778 WDialog *new_d;
779 Widget *w;
781 new_d = g_new0 (WDialog, 1);
782 w = WIDGET (new_d);
783 widget_init (w, y1, x1, lines, cols, (callback != NULL) ? callback : dlg_default_callback,
784 mouse_handler);
785 widget_want_cursor (w, FALSE);
787 new_d->state = DLG_CONSTRUCT;
788 new_d->modal = modal;
789 new_d->color = colors;
790 new_d->help_ctx = help_ctx;
791 new_d->flags = flags;
792 new_d->data = NULL;
794 dlg_set_size (new_d, lines, cols);
795 new_d->fullscreen = (w->x == 0 && w->y == 0 && w->cols == COLS && w->lines == LINES);
797 new_d->mouse_status = MOU_UNHANDLED;
799 /* Strip existing spaces, add one space before and after the title */
800 if (title != NULL && *title != '\0')
802 char *t;
804 t = g_strstrip (g_strdup (title));
805 if (*t != '\0')
806 new_d->title = g_strdup_printf (" %s ", t);
807 g_free (t);
810 /* unique name of event group for this dialog */
811 new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
813 return new_d;
816 /* --------------------------------------------------------------------------------------------- */
818 void
819 dlg_set_default_colors (void)
821 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
822 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
823 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
824 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
825 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
827 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
828 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
829 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
830 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
831 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
833 listbox_colors[DLG_COLOR_NORMAL] = PMENU_ENTRY_COLOR;
834 listbox_colors[DLG_COLOR_FOCUS] = PMENU_SELECTED_COLOR;
835 listbox_colors[DLG_COLOR_HOT_NORMAL] = PMENU_ENTRY_COLOR;
836 listbox_colors[DLG_COLOR_HOT_FOCUS] = PMENU_SELECTED_COLOR;
837 listbox_colors[DLG_COLOR_TITLE] = PMENU_TITLE_COLOR;
840 /* --------------------------------------------------------------------------------------------- */
842 void
843 dlg_erase (WDialog * h)
845 if ((h != NULL) && (h->state == DLG_ACTIVE))
847 Widget *wh = WIDGET (h);
849 tty_fill_region (wh->y, wh->x, wh->lines, wh->cols, ' ');
853 /* --------------------------------------------------------------------------------------------- */
855 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
858 unsigned long
859 add_widget_autopos (WDialog * h, void *w, widget_pos_flags_t pos_flags, const void *before)
861 Widget *wh = WIDGET (h);
862 Widget *widget;
864 /* Don't accept 0 widgets */
865 if (w == NULL)
866 abort ();
868 widget = WIDGET (w);
870 if ((pos_flags & WPOS_CENTER_HORZ) != 0)
871 widget->x = (wh->cols - widget->cols) / 2;
872 widget->x += wh->x;
874 if ((pos_flags & WPOS_CENTER_VERT) != 0)
875 widget->y = (wh->lines - widget->lines) / 2;
876 widget->y += wh->y;
878 widget->owner = h;
879 widget->pos_flags = pos_flags;
880 widget->id = h->widget_id++;
882 if (h->widgets == NULL || before == NULL)
884 h->widgets = g_list_append (h->widgets, widget);
885 h->current = g_list_last (h->widgets);
887 else
889 GList *b;
891 b = g_list_find (h->widgets, before);
893 /* don't accept widget not from dialog. This shouldn't happen */
894 if (b == NULL)
895 abort ();
897 b = g_list_next (b);
898 h->widgets = g_list_insert_before (h->widgets, b, widget);
899 if (b != NULL)
900 h->current = g_list_previous (b);
901 else
902 h->current = g_list_last (h->widgets);
905 /* widget has been added in runtime */
906 if (h->state == DLG_ACTIVE)
908 send_message (widget, NULL, MSG_INIT, 0, NULL);
909 send_message (widget, NULL, MSG_DRAW, 0, NULL);
910 send_message (widget, NULL, MSG_FOCUS, 0, NULL);
913 return widget->id;
916 /* --------------------------------------------------------------------------------------------- */
917 /** wrapper to simply add lefttop positioned controls */
919 unsigned long
920 add_widget (WDialog * h, void *w)
922 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT,
923 h->current != NULL ? h->current->data : NULL);
926 /* --------------------------------------------------------------------------------------------- */
928 unsigned long
929 add_widget_before (WDialog * h, void *w, void *before)
931 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT, before);
934 /* --------------------------------------------------------------------------------------------- */
936 /** delete widget from dialog */
937 void
938 del_widget (void *w)
940 WDialog *h;
941 GList *d;
943 /* Don't accept NULL widget. This shouldn't happen */
944 if (w == NULL)
945 abort ();
947 h = WIDGET (w)->owner;
949 d = g_list_find (h->widgets, w);
950 if (d == h->current)
951 h->current = dlg_widget_next (h, d);
953 h->widgets = g_list_remove_link (h->widgets, d);
954 send_message (d->data, NULL, MSG_DESTROY, 0, NULL);
955 g_free (d->data);
956 g_list_free_1 (d);
958 /* widget has been deleted in runtime */
959 if (h->state == DLG_ACTIVE)
961 dlg_redraw (h);
962 dlg_focus (h);
966 /* --------------------------------------------------------------------------------------------- */
968 void
969 do_refresh (void)
971 GList *d = top_dlg;
973 if (fast_refresh)
975 if ((d != NULL) && (d->data != NULL))
976 dlg_redraw (DIALOG (d->data));
978 else
980 /* Search first fullscreen dialog */
981 for (; d != NULL; d = g_list_next (d))
982 if (d->data != NULL && DIALOG (d->data)->fullscreen)
983 break;
984 /* back to top dialog */
985 for (; d != NULL; d = g_list_previous (d))
986 if (d->data != NULL)
987 dlg_redraw (DIALOG (d->data));
991 /* --------------------------------------------------------------------------------------------- */
992 /** broadcast a message to all the widgets in a dialog */
994 void
995 dlg_broadcast_msg (WDialog * h, widget_msg_t msg)
997 dlg_broadcast_msg_to (h, msg, FALSE, 0);
1000 /* --------------------------------------------------------------------------------------------- */
1002 gboolean
1003 dlg_focus (WDialog * h)
1005 /* cannot focus disabled widget */
1006 if ((h->current != NULL) && (h->state == DLG_CONSTRUCT || h->state == DLG_ACTIVE))
1008 Widget *current = WIDGET (h->current->data);
1010 if (((current->options & W_DISABLED) == 0)
1011 && (send_message (current, NULL, MSG_FOCUS, 0, NULL) == MSG_HANDLED))
1013 send_message (h, current, MSG_FOCUS, 0, NULL);
1014 return TRUE;
1018 return FALSE;
1021 /* --------------------------------------------------------------------------------------------- */
1022 /** Find the widget with the given callback in the dialog h */
1024 Widget *
1025 find_widget_type (const WDialog * h, widget_cb_fn callback)
1027 GList *w;
1029 w = g_list_find_custom (h->widgets, callback, dlg_find_widget_callback);
1031 return (w == NULL) ? NULL : WIDGET (w->data);
1034 /* --------------------------------------------------------------------------------------------- */
1035 /** Find the widget with the given id */
1037 Widget *
1038 dlg_find_by_id (const WDialog * h, unsigned long id)
1040 GList *w;
1042 w = g_list_find_custom (h->widgets, GUINT_TO_POINTER (id), dlg_find_widget_by_id);
1043 return w != NULL ? WIDGET (w->data) : NULL;
1046 /* --------------------------------------------------------------------------------------------- */
1047 /** Find the widget with the given id in the dialog h and select it */
1049 void
1050 dlg_select_by_id (const WDialog * h, unsigned long id)
1052 Widget *w;
1054 w = dlg_find_by_id (h, id);
1055 if (w != NULL)
1056 dlg_select_widget (w);
1059 /* --------------------------------------------------------------------------------------------- */
1061 * Try to select widget in the dialog.
1064 void
1065 dlg_select_widget (void *w)
1067 Widget *widget = WIDGET (w);
1068 WDialog *h = widget->owner;
1070 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
1073 /* --------------------------------------------------------------------------------------------- */
1076 * Set widget at top of widget list and make it current.
1079 void
1080 dlg_set_top_widget (void *w)
1082 Widget *widget = WIDGET (w);
1083 WDialog *h = widget->owner;
1084 GList *l;
1086 l = g_list_find (h->widgets, w);
1087 if (l == NULL)
1088 abort (); /* widget is not in dialog, this should not happen */
1090 /* unfocus prevoius widget and focus current one before widget reordering */
1091 if (h->state == DLG_ACTIVE)
1092 do_select_widget (h, l, SELECT_EXACT);
1094 /* widget reordering */
1095 h->widgets = g_list_remove_link (h->widgets, l);
1096 h->widgets = g_list_concat (h->widgets, l);
1097 h->current = l;
1100 /* --------------------------------------------------------------------------------------------- */
1101 /** Try to select previous widget in the tab order */
1103 void
1104 dlg_one_up (WDialog * h)
1106 if (h->widgets != NULL)
1107 do_select_widget (h, dlg_widget_prev (h, h->current), SELECT_PREV);
1110 /* --------------------------------------------------------------------------------------------- */
1111 /** Try to select next widget in the tab order */
1113 void
1114 dlg_one_down (WDialog * h)
1116 if (h->widgets != NULL)
1117 do_select_widget (h, dlg_widget_next (h, h->current), SELECT_NEXT);
1120 /* --------------------------------------------------------------------------------------------- */
1122 void
1123 update_cursor (WDialog * h)
1125 GList *p = h->current;
1127 if ((p != NULL) && (h->state == DLG_ACTIVE))
1129 Widget *w;
1131 w = WIDGET (p->data);
1133 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1134 send_message (w, NULL, MSG_CURSOR, 0, NULL);
1135 else
1138 p = dlg_widget_next (h, p);
1139 if (p == h->current)
1140 break;
1142 w = WIDGET (p->data);
1144 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1145 if (send_message (w, NULL, MSG_CURSOR, 0, NULL) == MSG_HANDLED)
1146 break;
1148 while (TRUE);
1152 /* --------------------------------------------------------------------------------------------- */
1154 * Redraw the widgets in reverse order, leaving the current widget
1155 * as the last one
1158 void
1159 dlg_redraw (WDialog * h)
1161 if (h->state != DLG_ACTIVE)
1162 return;
1164 if (h->winch_pending)
1166 h->winch_pending = FALSE;
1167 send_message (h, NULL, MSG_RESIZE, 0, NULL);
1170 send_message (h, NULL, MSG_DRAW, 0, NULL);
1171 dlg_broadcast_msg (h, MSG_DRAW);
1172 update_cursor (h);
1175 /* --------------------------------------------------------------------------------------------- */
1177 void
1178 dlg_stop (WDialog * h)
1180 h->state = DLG_CLOSED;
1183 /* --------------------------------------------------------------------------------------------- */
1184 /** Init the process */
1186 void
1187 dlg_init (WDialog * h)
1189 if (top_dlg != NULL && DIALOG (top_dlg->data)->modal)
1190 h->modal = TRUE;
1192 /* add dialog to the stack */
1193 top_dlg = g_list_prepend (top_dlg, h);
1195 /* Initialize dialog manager and widgets */
1196 if (h->state == DLG_CONSTRUCT)
1198 if (!h->modal)
1199 dialog_switch_add (h);
1201 send_message (h, NULL, MSG_INIT, 0, NULL);
1202 dlg_broadcast_msg (h, MSG_INIT);
1203 dlg_read_history (h);
1206 h->state = DLG_ACTIVE;
1208 /* first send MSG_DRAW to dialog itself and all widgets... */
1209 dlg_redraw (h);
1211 /* ...then send MSG_FOCUS to select the first widget that can take focus */
1212 while (h->current != NULL && !dlg_focus (h))
1213 h->current = dlg_widget_next (h, h->current);
1216 h->ret_value = 0;
1219 /* --------------------------------------------------------------------------------------------- */
1221 void
1222 dlg_process_event (WDialog * h, int key, Gpm_Event * event)
1224 if (key == EV_NONE)
1226 if (tty_got_interrupt ())
1227 if (send_message (h, NULL, MSG_ACTION, CK_Cancel, NULL) != MSG_HANDLED)
1228 dlg_execute_cmd (h, CK_Cancel);
1230 return;
1233 if (key == EV_MOUSE)
1234 h->mouse_status = dlg_mouse_event (h, event);
1235 else
1236 dlg_key_event (h, key);
1239 /* --------------------------------------------------------------------------------------------- */
1240 /** Shutdown the dlg_run */
1242 void
1243 dlg_run_done (WDialog * h)
1245 top_dlg = g_list_remove (top_dlg, h);
1247 if (h->state == DLG_CLOSED)
1249 send_message (h, h->current->data, MSG_END, 0, NULL);
1250 if (!h->modal)
1251 dialog_switch_remove (h);
1255 /* --------------------------------------------------------------------------------------------- */
1257 * Standard run dialog routine
1258 * We have to keep this routine small so that we can duplicate it's
1259 * behavior on complex routines like the file routines, this way,
1260 * they can call the dlg_process_event without rewriting all the code
1264 dlg_run (WDialog * h)
1266 dlg_init (h);
1267 frontend_dlg_run (h);
1268 dlg_run_done (h);
1269 return h->ret_value;
1272 /* --------------------------------------------------------------------------------------------- */
1274 void
1275 dlg_destroy (WDialog * h)
1277 /* if some widgets have history, save all history at one moment here */
1278 dlg_save_history (h);
1279 dlg_broadcast_msg (h, MSG_DESTROY);
1280 g_list_free_full (h->widgets, g_free);
1281 mc_event_group_del (h->event_group);
1282 g_free (h->event_group);
1283 g_free (h->title);
1284 g_free (h);
1286 do_refresh ();
1289 /* --------------------------------------------------------------------------------------------- */
1292 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1294 void
1295 dlg_save_history (WDialog * h)
1297 char *profile;
1298 int i;
1300 if (num_history_items_recorded == 0) /* this is how to disable */
1301 return;
1303 profile = mc_config_get_full_path (MC_HISTORY_FILE);
1304 i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
1305 if (i != -1)
1306 close (i);
1308 /* Make sure the history is only readable by the user */
1309 if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
1311 ev_history_load_save_t event_data;
1313 event_data.cfg = mc_config_init (profile, FALSE);
1314 event_data.receiver = NULL;
1316 /* get all histories in dialog */
1317 mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
1319 mc_config_save_file (event_data.cfg, NULL);
1320 mc_config_deinit (event_data.cfg);
1323 g_free (profile);
1326 /* --------------------------------------------------------------------------------------------- */
1328 char *
1329 dlg_get_title (const WDialog * h, size_t len)
1331 char *t;
1333 if (h == NULL)
1334 abort ();
1336 if (h->get_title != NULL)
1337 t = h->get_title (h, len);
1338 else
1339 t = g_strdup ("");
1341 return t;
1344 /* --------------------------------------------------------------------------------------------- */