Clarify checks of dialog and widget existence.
[midnight-commander.git] / lib / widget / dialog.c
bloba1c78b103b51431a122637dd028af3ab8f64e809
1 /*
2 Dialog box features module for the Midnight Commander
4 Copyright (C) 1994-2019
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() */
45 #include "lib/util.h" /* MC_PTR_FREE */
47 #include "lib/widget.h"
48 #include "lib/widget/mouse.h"
50 /*** global variables ****************************************************************************/
52 /* Color styles for normal and error dialogs */
53 dlg_colors_t dialog_colors;
54 dlg_colors_t alarm_colors;
55 dlg_colors_t listbox_colors;
57 /* Primitive way to check if the the current dialog is our dialog */
58 /* This is needed by async routines like load_prompt */
59 GList *top_dlg = NULL;
61 /* A hook list for idle events */
62 hook_t *idle_hook = NULL;
64 /* If set then dialogs just clean the screen when refreshing, else */
65 /* they do a complete refresh, refreshing all the parts of the program */
66 gboolean fast_refresh = FALSE;
68 /* left click outside of dialog closes it */
69 gboolean mouse_close_dialog = FALSE;
71 const global_keymap_t *dialog_map = NULL;
73 /*** file scope macro definitions ****************************************************************/
75 /*** file scope type declarations ****************************************************************/
77 /* Control widget positions in dialog */
78 typedef struct
80 int shift_x;
81 int scale_x;
82 int shift_y;
83 int scale_y;
84 } widget_shift_scale_t;
86 /*** file scope variables ************************************************************************/
88 /* --------------------------------------------------------------------------------------------- */
89 /*** file scope functions ************************************************************************/
90 /* --------------------------------------------------------------------------------------------- */
92 static GList *
93 dlg_get_next_or_prev_of (const GList * list, gboolean next)
95 GList *l = NULL;
97 if (list != NULL)
99 const WDialog *owner = CONST_WIDGET (list->data)->owner;
101 if (owner != NULL)
103 if (next)
105 l = g_list_next (list);
106 if (l == NULL)
107 l = owner->widgets;
109 else
111 l = g_list_previous (list);
112 if (l == NULL)
113 l = g_list_last (owner->widgets);
118 return l;
121 /* --------------------------------------------------------------------------------------------- */
123 static void
124 dlg_select_next_or_prev (WDialog * h, gboolean next)
126 if (h->widgets != NULL && h->current != NULL)
128 GList *l = h->current;
129 Widget *w;
133 l = dlg_get_next_or_prev_of (l, next);
134 w = WIDGET (l->data);
136 while ((widget_get_state (w, WST_DISABLED) || !widget_get_options (w, WOP_SELECTABLE))
137 && l != h->current);
139 widget_select (l->data);
143 /* --------------------------------------------------------------------------------------------- */
145 * broadcast a message to all the widgets in a dialog that have
146 * the options set to flags. If flags is zero, the message is sent
147 * to all widgets.
150 static void
151 dlg_broadcast_msg_to (WDialog * h, widget_msg_t msg, gboolean reverse, widget_options_t flags)
153 GList *p, *first;
155 if (h->widgets == NULL)
156 return;
158 if (h->current == NULL)
159 h->current = h->widgets;
161 p = dlg_get_next_or_prev_of (h->current, !reverse);
162 first = p;
166 Widget *w = WIDGET (p->data);
168 p = dlg_get_next_or_prev_of (p, !reverse);
170 if ((flags == 0) || ((flags & w->options) != 0))
171 send_message (w, NULL, msg, 0, NULL);
173 while (first != p);
176 /* --------------------------------------------------------------------------------------------- */
179 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
181 static void
182 dlg_read_history (WDialog * h)
184 char *profile;
185 ev_history_load_save_t event_data;
187 if (num_history_items_recorded == 0) /* this is how to disable */
188 return;
190 profile = mc_config_get_full_path (MC_HISTORY_FILE);
191 event_data.cfg = mc_config_init (profile, TRUE);
192 event_data.receiver = NULL;
194 /* create all histories in dialog */
195 mc_event_raise (h->event_group, MCEVENT_HISTORY_LOAD, &event_data);
197 mc_config_deinit (event_data.cfg);
198 g_free (profile);
201 /* --------------------------------------------------------------------------------------------- */
203 static int
204 dlg_find_widget_callback (const void *a, const void *b)
206 const Widget *w = CONST_WIDGET (a);
207 const widget_cb_fn f = b;
209 return (w->callback == f) ? 0 : 1;
212 /* --------------------------------------------------------------------------------------------- */
214 static void
215 refresh_cmd (void)
217 #ifdef HAVE_SLANG
218 tty_touch_screen ();
219 mc_refresh ();
220 #else
221 /* Use this if the refreshes fail */
222 clr_scr ();
223 repaint_screen ();
224 #endif /* HAVE_SLANG */
227 /* --------------------------------------------------------------------------------------------- */
229 static cb_ret_t
230 dlg_execute_cmd (WDialog * h, long command)
232 cb_ret_t ret = MSG_HANDLED;
233 switch (command)
235 case CK_Ok:
236 h->ret_value = B_ENTER;
237 dlg_stop (h);
238 break;
239 case CK_Cancel:
240 h->ret_value = B_CANCEL;
241 dlg_stop (h);
242 break;
244 case CK_Up:
245 case CK_Left:
246 dlg_select_prev_widget (h);
247 break;
248 case CK_Down:
249 case CK_Right:
250 dlg_select_next_widget (h);
251 break;
253 case CK_Help:
255 ev_help_t event_data = { NULL, h->help_ctx };
256 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
258 break;
260 case CK_Suspend:
261 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
262 refresh_cmd ();
263 break;
264 case CK_Refresh:
265 refresh_cmd ();
266 break;
268 case CK_ScreenList:
269 if (!widget_get_state (WIDGET (h), WST_MODAL))
270 dialog_switch_list ();
271 else
272 ret = MSG_NOT_HANDLED;
273 break;
274 case CK_ScreenNext:
275 if (!widget_get_state (WIDGET (h), WST_MODAL))
276 dialog_switch_next ();
277 else
278 ret = MSG_NOT_HANDLED;
279 break;
280 case CK_ScreenPrev:
281 if (!widget_get_state (WIDGET (h), WST_MODAL))
282 dialog_switch_prev ();
283 else
284 ret = MSG_NOT_HANDLED;
285 break;
287 default:
288 ret = MSG_NOT_HANDLED;
291 return ret;
294 /* --------------------------------------------------------------------------------------------- */
296 static cb_ret_t
297 dlg_handle_key (WDialog * h, int d_key)
299 long command;
301 command = keybind_lookup_keymap_command (dialog_map, d_key);
303 if (command == CK_IgnoreKey)
304 return MSG_NOT_HANDLED;
306 if (send_message (h, NULL, MSG_ACTION, command, NULL) == MSG_HANDLED
307 || dlg_execute_cmd (h, command) == MSG_HANDLED)
308 return MSG_HANDLED;
310 return MSG_NOT_HANDLED;
313 /* --------------------------------------------------------------------------------------------- */
315 * This is the low-level mouse handler.
316 * It receives a Gpm_Event event and translates it into a higher level protocol.
318 static int
319 dlg_mouse_translator (Gpm_Event * event, Widget * w)
321 mouse_event_t me;
323 me = mouse_translate_event (w, event);
325 return mouse_process_event (w, &me);
328 /* --------------------------------------------------------------------------------------------- */
330 static int
331 dlg_mouse_event (WDialog * h, Gpm_Event * event)
333 Widget *wh = WIDGET (h);
335 GList *p;
337 /* close the dialog by mouse left click out of dialog area */
338 if (mouse_close_dialog && (wh->pos_flags & WPOS_FULLSCREEN) == 0
339 && ((event->buttons & GPM_B_LEFT) != 0) && ((event->type & GPM_DOWN) != 0)
340 && !mouse_global_in_widget (event, wh))
342 h->ret_value = B_CANCEL;
343 dlg_stop (h);
344 return MOU_NORMAL;
347 if (wh->mouse_callback != NULL)
349 int mou;
351 mou = dlg_mouse_translator (event, wh);
352 if (mou != MOU_UNHANDLED)
353 return mou;
356 if (h->widgets == NULL)
357 return MOU_UNHANDLED;
359 /* send the event to widgets in reverse Z-order */
360 p = g_list_last (h->widgets);
363 Widget *w = WIDGET (p->data);
365 if (!widget_get_state (w, WST_DISABLED) && w->mouse_callback != NULL)
367 /* put global cursor position to the widget */
368 int ret;
370 ret = dlg_mouse_translator (event, w);
371 if (ret != MOU_UNHANDLED)
372 return ret;
375 p = g_list_previous (p);
377 while (p != NULL);
379 return MOU_UNHANDLED;
382 /* --------------------------------------------------------------------------------------------- */
384 static cb_ret_t
385 dlg_try_hotkey (WDialog * h, int d_key)
387 GList *hot_cur;
388 Widget *current;
389 cb_ret_t handled;
390 int c;
392 if (h->widgets == NULL)
393 return MSG_NOT_HANDLED;
395 if (h->current == NULL)
396 h->current = h->widgets;
399 * Explanation: we don't send letter hotkeys to other widgets if
400 * the currently selected widget is an input line
403 current = WIDGET (h->current->data);
405 if (widget_get_state (current, WST_DISABLED))
406 return MSG_NOT_HANDLED;
408 if (widget_get_options (current, WOP_IS_INPUT))
410 /* skip ascii control characters, anything else can valid character in
411 * some encoding */
412 if (d_key >= 32 && d_key < 256)
413 return MSG_NOT_HANDLED;
416 /* If it's an alt key, send the message */
417 c = d_key & ~ALT (0);
418 if (d_key & ALT (0) && g_ascii_isalpha (c))
419 d_key = g_ascii_tolower (c);
421 handled = MSG_NOT_HANDLED;
422 if (widget_get_options (current, WOP_WANT_HOTKEY))
423 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
425 /* If not used, send hotkey to other widgets */
426 if (handled == MSG_HANDLED)
427 return MSG_HANDLED;
429 hot_cur = dlg_get_widget_next_of (h->current);
431 /* send it to all widgets */
432 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
434 current = WIDGET (hot_cur->data);
436 if (widget_get_options (current, WOP_WANT_HOTKEY)
437 && !widget_get_state (current, WST_DISABLED))
438 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
440 if (handled == MSG_NOT_HANDLED)
441 hot_cur = dlg_get_widget_next_of (hot_cur);
444 if (handled == MSG_HANDLED)
445 widget_select (WIDGET (hot_cur->data));
447 return handled;
450 /* --------------------------------------------------------------------------------------------- */
452 static void
453 dlg_key_event (WDialog * h, int d_key)
455 cb_ret_t handled;
457 if (h->widgets == NULL)
458 return;
460 if (h->current == NULL)
461 h->current = h->widgets;
463 /* TAB used to cycle */
464 if (!widget_get_options (WIDGET (h), WOP_WANT_TAB))
466 if (d_key == '\t')
468 dlg_select_next_widget (h);
469 return;
471 else if ((d_key & ~(KEY_M_SHIFT | KEY_M_CTRL)) == '\t')
473 dlg_select_prev_widget (h);
474 return;
478 /* first can dlg_callback handle the key */
479 handled = send_message (h, NULL, MSG_KEY, d_key, NULL);
481 /* next try the hotkey */
482 if (handled == MSG_NOT_HANDLED)
483 handled = dlg_try_hotkey (h, d_key);
485 if (handled == MSG_HANDLED)
486 send_message (h, NULL, MSG_HOTKEY_HANDLED, 0, NULL);
487 else
488 /* not used - then try widget_callback */
489 handled = send_message (h->current->data, NULL, MSG_KEY, d_key, NULL);
491 /* not used- try to use the unhandled case */
492 if (handled == MSG_NOT_HANDLED)
493 handled = send_message (h, NULL, MSG_UNHANDLED_KEY, d_key, NULL);
495 if (handled == MSG_NOT_HANDLED)
496 handled = dlg_handle_key (h, d_key);
498 (void) handled;
499 send_message (h, NULL, MSG_POST_KEY, d_key, NULL);
502 /* --------------------------------------------------------------------------------------------- */
504 static void
505 frontend_dlg_run (WDialog * h)
507 Widget *wh = WIDGET (h);
508 Gpm_Event event;
510 event.x = -1;
512 /* close opened editors, viewers, etc */
513 if (!widget_get_state (wh, WST_MODAL) && mc_global.midnight_shutdown)
515 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
516 return;
519 while (widget_get_state (wh, WST_ACTIVE))
521 int d_key;
523 if (mc_global.tty.winch_flag != 0)
524 dialog_change_screen_size ();
526 if (is_idle ())
528 if (idle_hook)
529 execute_hooks (idle_hook);
531 while (widget_get_state (wh, WST_IDLE) && is_idle ())
532 send_message (wh, NULL, MSG_IDLE, 0, NULL);
534 /* Allow terminating the dialog from the idle handler */
535 if (!widget_get_state (wh, WST_ACTIVE))
536 break;
539 update_cursor (h);
541 /* Clear interrupt flag */
542 tty_got_interrupt ();
543 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
545 dlg_process_event (h, d_key, &event);
547 if (widget_get_state (wh, WST_CLOSED))
548 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
552 /* --------------------------------------------------------------------------------------------- */
554 static int
555 dlg_find_widget_by_id (gconstpointer a, gconstpointer b)
557 const Widget *w = CONST_WIDGET (a);
558 unsigned long id = GPOINTER_TO_UINT (b);
560 return w->id == id ? 0 : 1;
563 /* --------------------------------------------------------------------------------------------- */
564 static void
565 dlg_widget_set_position (gpointer data, gpointer user_data)
567 /* there are, mainly, 2 generally possible situations:
568 * 1. control sticks to one side - it should be moved
569 * 2. control sticks to two sides of one direction - it should be sized
572 Widget *c = WIDGET (data);
573 Widget *wh = WIDGET (c->owner);
574 const widget_shift_scale_t *wss = (const widget_shift_scale_t *) user_data;
575 int x = c->x;
576 int y = c->y;
577 int cols = c->cols;
578 int lines = c->lines;
580 if ((c->pos_flags & WPOS_CENTER_HORZ) != 0)
581 x = wh->x + (wh->cols - c->cols) / 2;
582 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0 && (c->pos_flags & WPOS_KEEP_RIGHT) != 0)
584 x += wss->shift_x;
585 cols += wss->scale_x;
587 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0)
588 x += wss->shift_x;
589 else if ((c->pos_flags & WPOS_KEEP_RIGHT) != 0)
590 x += wss->shift_x + wss->scale_x;
592 if ((c->pos_flags & WPOS_CENTER_VERT) != 0)
593 y = wh->y + (wh->lines - c->lines) / 2;
594 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0 && (c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
596 y += wss->shift_y;
597 lines += wss->scale_y;
599 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0)
600 y += wss->shift_y;
601 else if ((c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
602 y += wss->shift_y + wss->scale_y;
604 widget_set_size (c, y, x, lines, cols);
607 /* --------------------------------------------------------------------------------------------- */
609 static void
610 dlg_adjust_position (widget_pos_flags_t pos_flags, int *y, int *x, int *lines, int *cols)
612 if ((pos_flags & WPOS_FULLSCREEN) != 0)
614 *y = 0;
615 *x = 0;
616 *lines = LINES;
617 *cols = COLS;
619 else
621 if ((pos_flags & WPOS_CENTER_HORZ) != 0)
622 *x = (COLS - *cols) / 2;
624 if ((pos_flags & WPOS_CENTER_VERT) != 0)
625 *y = (LINES - *lines) / 2;
627 if ((pos_flags & WPOS_TRYUP) != 0)
629 if (*y > 3)
630 *y -= 2;
631 else if (*y == 3)
632 *y = 2;
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->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 /* TODO: truncate long title */
661 tty_setcolor (h->color[DLG_COLOR_TITLE]);
662 widget_move (h, space, (wh->cols - str_term_width1 (h->title)) / 2);
663 tty_print_string (h->title);
667 /* --------------------------------------------------------------------------------------------- */
668 /** this function allows to set dialog position */
670 void
671 dlg_set_position (WDialog * h, int y, int x, int lines, int cols)
673 Widget *wh = WIDGET (h);
674 widget_shift_scale_t wss;
676 /* save old positions, will be used to reposition childs */
677 int ox, oy, oc, ol;
679 /* save old positions, will be used to reposition childs */
680 ox = wh->x;
681 oy = wh->y;
682 oc = wh->cols;
683 ol = wh->lines;
685 wh->x = x;
686 wh->y = y;
687 wh->lines = lines;
688 wh->cols = cols;
690 /* dialog is empty */
691 if (h->widgets == NULL)
692 return;
694 if (h->current == NULL)
695 h->current = h->widgets;
697 /* values by which controls should be moved */
698 wss.shift_x = wh->x - ox;
699 wss.scale_x = wh->cols - oc;
700 wss.shift_y = wh->y - oy;
701 wss.scale_y = wh->lines - ol;
703 if (wss.shift_x != 0 || wss.shift_y != 0 || wss.scale_x != 0 || wss.scale_y != 0)
704 g_list_foreach (h->widgets, dlg_widget_set_position, &wss);
707 /* --------------------------------------------------------------------------------------------- */
708 /** Set dialog size and position */
710 void
711 dlg_set_size (WDialog * h, int lines, int cols)
713 int x = 0, y = 0;
715 dlg_adjust_position (WIDGET (h)->pos_flags, &y, &x, &lines, &cols);
716 dlg_set_position (h, y, x, lines, cols);
719 /* --------------------------------------------------------------------------------------------- */
720 /** Default dialog callback */
722 cb_ret_t
723 dlg_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
725 WDialog *h = DIALOG (w);
727 (void) sender;
728 (void) parm;
729 (void) data;
731 switch (msg)
733 case MSG_DRAW:
734 if (h->color != NULL)
736 dlg_default_repaint (h);
737 return MSG_HANDLED;
739 return MSG_NOT_HANDLED;
741 case MSG_IDLE:
742 /* we don't want endless loop */
743 widget_idle (w, FALSE);
744 return MSG_HANDLED;
746 case MSG_RESIZE:
747 /* this is default resizing mechanism */
748 /* the main idea of this code is to resize dialog
749 according to flags (if any of flags require automatic
750 resizing, like WPOS_CENTER, end after that reposition
751 controls in dialog according to flags of widget) */
752 dlg_set_size (h, w->lines, w->cols);
753 return MSG_HANDLED;
755 default:
756 break;
759 return MSG_NOT_HANDLED;
762 /* --------------------------------------------------------------------------------------------- */
764 WDialog *
765 dlg_create (gboolean modal, int y1, int x1, int lines, int cols, widget_pos_flags_t pos_flags,
766 gboolean compact, const int *colors, widget_cb_fn callback,
767 widget_mouse_cb_fn mouse_callback, const char *help_ctx, const char *title)
769 WDialog *new_d;
770 Widget *w;
772 new_d = g_new0 (WDialog, 1);
773 w = WIDGET (new_d);
774 dlg_adjust_position (pos_flags, &y1, &x1, &lines, &cols);
775 widget_init (w, y1, x1, lines, cols, (callback != NULL) ? callback : dlg_default_callback,
776 mouse_callback);
777 w->pos_flags = pos_flags;
778 w->options |= WOP_SELECTABLE | WOP_TOP_SELECT;
780 w->state |= WST_CONSTRUCT | WST_FOCUSED;
781 if (modal)
782 w->state |= WST_MODAL;
784 new_d->color = colors;
785 new_d->help_ctx = help_ctx;
786 new_d->compact = compact;
787 new_d->data = NULL;
789 new_d->mouse_status = MOU_UNHANDLED;
791 dlg_set_title (new_d, title);
793 /* unique name of event group for this dialog */
794 new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
796 return new_d;
799 /* --------------------------------------------------------------------------------------------- */
801 void
802 dlg_set_default_colors (void)
804 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
805 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
806 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
807 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
808 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
810 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
811 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
812 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
813 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
814 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
816 listbox_colors[DLG_COLOR_NORMAL] = PMENU_ENTRY_COLOR;
817 listbox_colors[DLG_COLOR_FOCUS] = PMENU_SELECTED_COLOR;
818 listbox_colors[DLG_COLOR_HOT_NORMAL] = PMENU_ENTRY_COLOR;
819 listbox_colors[DLG_COLOR_HOT_FOCUS] = PMENU_SELECTED_COLOR;
820 listbox_colors[DLG_COLOR_TITLE] = PMENU_TITLE_COLOR;
823 /* --------------------------------------------------------------------------------------------- */
825 void
826 dlg_erase (WDialog * h)
828 Widget *wh = WIDGET (h);
830 if (wh != NULL && widget_get_state (wh, WST_ACTIVE))
831 tty_fill_region (wh->y, wh->x, wh->lines, wh->cols, ' ');
834 /* --------------------------------------------------------------------------------------------- */
836 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
839 unsigned long
840 add_widget_autopos (WDialog * h, void *w, widget_pos_flags_t pos_flags, const void *before)
842 Widget *wh = WIDGET (h);
843 Widget *widget;
844 GList *new_current;
846 /* Don't accept 0 widgets */
847 if (w == NULL)
848 abort ();
850 widget = WIDGET (w);
852 if ((pos_flags & WPOS_CENTER_HORZ) != 0)
853 widget->x = (wh->cols - widget->cols) / 2;
854 widget->x += wh->x;
856 if ((pos_flags & WPOS_CENTER_VERT) != 0)
857 widget->y = (wh->lines - widget->lines) / 2;
858 widget->y += wh->y;
860 widget->owner = h;
861 widget->pos_flags = pos_flags;
862 widget->id = h->widget_id++;
864 if (h->widgets == NULL || before == NULL)
866 h->widgets = g_list_append (h->widgets, widget);
867 new_current = g_list_last (h->widgets);
869 else
871 GList *b;
873 b = g_list_find (h->widgets, before);
875 /* don't accept widget not from dialog. This shouldn't happen */
876 if (b == NULL)
877 abort ();
879 b = g_list_next (b);
880 h->widgets = g_list_insert_before (h->widgets, b, widget);
881 if (b != NULL)
882 new_current = g_list_previous (b);
883 else
884 new_current = g_list_last (h->widgets);
887 /* widget has been added at runtime */
888 if (widget_get_state (wh, WST_ACTIVE))
890 send_message (widget, NULL, MSG_INIT, 0, NULL);
891 widget_select (widget);
893 else
894 h->current = new_current;
896 return widget->id;
899 /* --------------------------------------------------------------------------------------------- */
900 /** wrapper to simply add lefttop positioned controls */
902 unsigned long
903 add_widget (WDialog * h, void *w)
905 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT,
906 h->current != NULL ? h->current->data : NULL);
909 /* --------------------------------------------------------------------------------------------- */
911 unsigned long
912 add_widget_before (WDialog * h, void *w, void *before)
914 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT, before);
917 /* --------------------------------------------------------------------------------------------- */
919 /** delete widget from dialog */
920 void
921 del_widget (void *w)
923 WDialog *h;
924 GList *d;
926 /* Don't accept NULL widget. This shouldn't happen */
927 if (w == NULL)
928 abort ();
930 h = WIDGET (w)->owner;
932 d = g_list_find (h->widgets, w);
933 if (d == h->current)
934 dlg_set_current_widget_next (h);
936 h->widgets = g_list_remove_link (h->widgets, d);
937 if (h->widgets == NULL)
938 h->current = NULL;
939 send_message (d->data, NULL, MSG_DESTROY, 0, NULL);
940 g_free (d->data);
941 g_list_free_1 (d);
943 /* widget has been deleted in runtime */
944 if (widget_get_state (WIDGET (h), WST_ACTIVE))
946 dlg_redraw (h);
947 dlg_select_current_widget (h);
951 /* --------------------------------------------------------------------------------------------- */
953 void
954 do_refresh (void)
956 GList *d = top_dlg;
958 if (fast_refresh)
960 if (d != NULL)
961 dlg_redraw (DIALOG (d->data));
963 else
965 /* Search first fullscreen dialog */
966 for (; d != NULL; d = g_list_next (d))
967 if ((WIDGET (d->data)->pos_flags & WPOS_FULLSCREEN) != 0)
968 break;
969 /* back to top dialog */
970 for (; d != NULL; d = g_list_previous (d))
971 dlg_redraw (DIALOG (d->data));
975 /* --------------------------------------------------------------------------------------------- */
976 /** broadcast a message to all the widgets in a dialog */
978 void
979 dlg_broadcast_msg (WDialog * h, widget_msg_t msg)
981 dlg_broadcast_msg_to (h, msg, FALSE, 0);
984 /* --------------------------------------------------------------------------------------------- */
985 /** Find the widget with the given callback in the dialog h */
987 Widget *
988 find_widget_type (const WDialog * h, widget_cb_fn callback)
990 GList *w;
992 w = g_list_find_custom (h->widgets, (gconstpointer) callback, dlg_find_widget_callback);
994 return (w == NULL) ? NULL : WIDGET (w->data);
997 /* --------------------------------------------------------------------------------------------- */
999 GList *
1000 dlg_find (const WDialog * h, const Widget * w)
1002 return (w->owner == NULL || w->owner != h) ? NULL : g_list_find (h->widgets, w);
1005 /* --------------------------------------------------------------------------------------------- */
1006 /** Find the widget with the given id */
1008 Widget *
1009 dlg_find_by_id (const WDialog * h, unsigned long id)
1011 GList *w;
1013 w = g_list_find_custom (h->widgets, GUINT_TO_POINTER (id), dlg_find_widget_by_id);
1014 return w != NULL ? WIDGET (w->data) : NULL;
1017 /* --------------------------------------------------------------------------------------------- */
1018 /** Find the widget with the given id in the dialog h and select it */
1020 void
1021 dlg_select_by_id (const WDialog * h, unsigned long id)
1023 Widget *w;
1025 w = dlg_find_by_id (h, id);
1026 if (w != NULL)
1027 widget_select (w);
1030 /* --------------------------------------------------------------------------------------------- */
1031 /** Try to select previous widget in the tab order */
1033 void
1034 dlg_select_prev_widget (WDialog * h)
1036 dlg_select_next_or_prev (h, FALSE);
1039 /* --------------------------------------------------------------------------------------------- */
1040 /** Try to select next widget in the tab order */
1042 void
1043 dlg_select_next_widget (WDialog * h)
1045 dlg_select_next_or_prev (h, TRUE);
1048 /* --------------------------------------------------------------------------------------------- */
1050 void
1051 update_cursor (WDialog * h)
1053 GList *p = h->current;
1055 if (p != NULL && widget_get_state (WIDGET (h), WST_ACTIVE))
1057 Widget *w = WIDGET (p->data);
1059 if (!widget_get_state (w, WST_DISABLED) && widget_get_options (w, WOP_WANT_CURSOR))
1060 send_message (w, NULL, MSG_CURSOR, 0, NULL);
1061 else
1064 p = dlg_get_widget_next_of (p);
1065 if (p == h->current)
1066 break;
1068 w = WIDGET (p->data);
1070 if (!widget_get_state (w, WST_DISABLED) && widget_get_options (w, WOP_WANT_CURSOR)
1071 && send_message (w, NULL, MSG_CURSOR, 0, NULL) == MSG_HANDLED)
1072 break;
1074 while (TRUE);
1078 /* --------------------------------------------------------------------------------------------- */
1080 * Redraw the widgets in reverse order, leaving the current widget
1081 * as the last one
1084 void
1085 dlg_redraw (WDialog * h)
1087 if (!widget_get_state (WIDGET (h), WST_ACTIVE))
1088 return;
1090 if (h->winch_pending)
1092 h->winch_pending = FALSE;
1093 send_message (h, NULL, MSG_RESIZE, 0, NULL);
1096 send_message (h, NULL, MSG_DRAW, 0, NULL);
1097 dlg_broadcast_msg (h, MSG_DRAW);
1098 update_cursor (h);
1101 /* --------------------------------------------------------------------------------------------- */
1103 void
1104 dlg_stop (WDialog * h)
1106 widget_set_state (WIDGET (h), WST_CLOSED, TRUE);
1109 /* --------------------------------------------------------------------------------------------- */
1110 /** Init the process */
1112 void
1113 dlg_init (WDialog * h)
1115 Widget *wh = WIDGET (h);
1117 if (top_dlg != NULL && widget_get_state (WIDGET (top_dlg->data), WST_MODAL))
1118 widget_set_state (wh, WST_MODAL, TRUE);
1120 /* add dialog to the stack */
1121 top_dlg = g_list_prepend (top_dlg, h);
1123 /* Initialize dialog manager and widgets */
1124 if (widget_get_state (wh, WST_CONSTRUCT))
1126 if (!widget_get_state (wh, WST_MODAL))
1127 dialog_switch_add (h);
1129 send_message (h, NULL, MSG_INIT, 0, NULL);
1130 dlg_broadcast_msg (h, MSG_INIT);
1131 dlg_read_history (h);
1134 /* Select the first widget that takes focus */
1135 while (h->current != NULL && !widget_get_options (WIDGET (h->current->data), WOP_SELECTABLE)
1136 && !widget_get_state (WIDGET (h->current->data), WST_DISABLED))
1137 dlg_set_current_widget_next (h);
1139 widget_set_state (wh, WST_ACTIVE, TRUE);
1140 dlg_redraw (h);
1141 /* focus found widget */
1142 if (h->current != NULL)
1143 widget_set_state (WIDGET (h->current->data), WST_FOCUSED, TRUE);
1145 h->ret_value = 0;
1148 /* --------------------------------------------------------------------------------------------- */
1150 void
1151 dlg_process_event (WDialog * h, int key, Gpm_Event * event)
1153 if (key == EV_NONE)
1155 if (tty_got_interrupt ())
1156 if (send_message (h, NULL, MSG_ACTION, CK_Cancel, NULL) != MSG_HANDLED)
1157 dlg_execute_cmd (h, CK_Cancel);
1159 else if (key == EV_MOUSE)
1160 h->mouse_status = dlg_mouse_event (h, event);
1161 else
1162 dlg_key_event (h, key);
1165 /* --------------------------------------------------------------------------------------------- */
1166 /** Shutdown the dlg_run */
1168 void
1169 dlg_run_done (WDialog * h)
1171 top_dlg = g_list_remove (top_dlg, h);
1173 if (widget_get_state (WIDGET (h), WST_CLOSED))
1175 send_message (h, h->current == NULL ? NULL : WIDGET (h->current->data), MSG_END, 0, NULL);
1176 if (!widget_get_state (WIDGET (h), WST_MODAL))
1177 dialog_switch_remove (h);
1181 /* --------------------------------------------------------------------------------------------- */
1183 * Standard run dialog routine
1184 * We have to keep this routine small so that we can duplicate it's
1185 * behavior on complex routines like the file routines, this way,
1186 * they can call the dlg_process_event without rewriting all the code
1190 dlg_run (WDialog * h)
1192 dlg_init (h);
1193 frontend_dlg_run (h);
1194 dlg_run_done (h);
1195 return h->ret_value;
1198 /* --------------------------------------------------------------------------------------------- */
1200 void
1201 dlg_destroy (WDialog * h)
1203 /* if some widgets have history, save all history at one moment here */
1204 dlg_save_history (h);
1205 dlg_broadcast_msg (h, MSG_DESTROY);
1206 g_list_free_full (h->widgets, g_free);
1207 mc_event_group_del (h->event_group);
1208 g_free (h->event_group);
1209 g_free (h->title);
1210 g_free (h);
1212 do_refresh ();
1215 /* --------------------------------------------------------------------------------------------- */
1218 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1220 void
1221 dlg_save_history (WDialog * h)
1223 char *profile;
1224 int i;
1226 if (num_history_items_recorded == 0) /* this is how to disable */
1227 return;
1229 profile = mc_config_get_full_path (MC_HISTORY_FILE);
1230 i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
1231 if (i != -1)
1232 close (i);
1234 /* Make sure the history is only readable by the user */
1235 if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
1237 ev_history_load_save_t event_data;
1239 event_data.cfg = mc_config_init (profile, FALSE);
1240 event_data.receiver = NULL;
1242 /* get all histories in dialog */
1243 mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
1245 mc_config_save_file (event_data.cfg, NULL);
1246 mc_config_deinit (event_data.cfg);
1249 g_free (profile);
1252 /* --------------------------------------------------------------------------------------------- */
1254 void
1255 dlg_set_title (WDialog * h, const char *title)
1257 MC_PTR_FREE (h->title);
1259 /* Strip existing spaces, add one space before and after the title */
1260 if (title != NULL && title[0] != '\0')
1262 char *t;
1264 t = g_strstrip (g_strdup (title));
1265 if (t[0] != '\0')
1266 h->title = g_strdup_printf (" %s ", t);
1267 g_free (t);
1271 /* --------------------------------------------------------------------------------------------- */
1273 char *
1274 dlg_get_title (const WDialog * h, size_t len)
1276 char *t;
1278 if (h == NULL)
1279 abort ();
1281 if (h->get_title != NULL)
1282 t = h->get_title (h, len);
1283 else
1284 t = g_strdup ("");
1286 return t;
1289 /* --------------------------------------------------------------------------------------------- */
1292 * Switch current widget to widget after current in dialog.
1294 * @param h WDialog widget
1297 void
1298 dlg_set_current_widget_next (WDialog * h)
1300 h->current = dlg_get_next_or_prev_of (h->current, TRUE);
1303 /* --------------------------------------------------------------------------------------------- */
1306 * Switch current widget to widget before current in dialog.
1308 * @param h WDialog widget
1311 void
1312 dlg_set_current_widget_prev (WDialog * h)
1314 h->current = dlg_get_next_or_prev_of (h->current, FALSE);
1317 /* --------------------------------------------------------------------------------------------- */
1320 * Get widget that is after specified widget in dialog.
1322 * @param w widget holder
1324 * @return widget that is after "w" or NULL if "w" is NULL or widget doesn't have owner
1327 GList *
1328 dlg_get_widget_next_of (GList * w)
1330 return dlg_get_next_or_prev_of (w, TRUE);
1333 /* --------------------------------------------------------------------------------------------- */
1336 * Get widget that is before specified widget in dialog.
1338 * @param w widget holder
1340 * @return widget that is before "w" or NULL if "w" is NULL or widget doesn't have owner
1343 GList *
1344 dlg_get_widget_prev_of (GList * w)
1346 return dlg_get_next_or_prev_of (w, FALSE);
1349 /* --------------------------------------------------------------------------------------------- */