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/>.
24 * \brief Source: dialog box features module
34 #include <sys/types.h>
37 #include "lib/global.h"
39 #include "lib/tty/tty.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 */
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 */
79 SELECT_NEXT
, /* go the the next widget */
80 SELECT_PREV
, /* go the the previous widget */
81 SELECT_EXACT
/* use current widget */
84 /*** file scope variables ************************************************************************/
86 /*** file scope functions ************************************************************************/
89 dlg_widget_next (WDialog
* h
, GList
* l
)
93 next
= g_list_next (l
);
100 /* --------------------------------------------------------------------------------------------- */
103 dlg_widget_prev (WDialog
* h
, GList
* l
)
107 prev
= g_list_previous (l
);
109 prev
= g_list_last (h
->widgets
);
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
122 dlg_broadcast_msg_to (WDialog
* h
, widget_msg_t msg
, gboolean reverse
, int flags
)
126 if (h
->widgets
== NULL
)
129 if (h
->current
== NULL
)
130 h
->current
= h
->widgets
;
133 p
= dlg_widget_prev (h
, h
->current
);
135 p
= dlg_widget_next (h
, h
->current
);
141 Widget
*w
= WIDGET (p
->data
);
144 p
= dlg_widget_prev (h
, p
);
146 p
= dlg_widget_next (h
, p
);
148 if ((flags
== 0) || ((flags
& w
->options
) != 0))
149 send_message (w
, NULL
, msg
, 0, NULL
);
154 /* --------------------------------------------------------------------------------------------- */
157 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
160 dlg_read_history (WDialog
* h
)
163 ev_history_load_save_t event_data
;
165 if (num_history_items_recorded
== 0) /* this is how to disable */
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
);
179 /* --------------------------------------------------------------------------------------------- */
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
);
199 /* --------------------------------------------------------------------------------------------- */
202 dlg_find_widget_callback (const void *a
, const void *b
)
204 const Widget
*w
= CONST_WIDGET (a
);
205 const widget_cb_fn f
= 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.
217 do_select_widget (WDialog
* h
, GList
* w
, select_dir_t dir
)
219 Widget
*w0
= WIDGET (h
->current
->data
);
221 if (!dlg_unfocus (h
))
234 h
->current
= g_list_find (h
->widgets
, w0
);
237 /* try find another widget that can take focus */
241 h
->current
= dlg_widget_next (h
, h
->current
);
244 h
->current
= dlg_widget_prev (h
, h
->current
);
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 /* --------------------------------------------------------------------------------------------- */
268 /* Use this if the refreshes fail */
271 #endif /* HAVE_SLANG */
274 /* --------------------------------------------------------------------------------------------- */
277 dlg_execute_cmd (WDialog
* h
, long command
)
279 cb_ret_t ret
= MSG_HANDLED
;
283 h
->ret_value
= B_ENTER
;
287 h
->ret_value
= B_CANCEL
;
302 ev_help_t event_data
= { NULL
, h
->help_ctx
};
303 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
308 mc_event_raise (MCEVENT_GROUP_CORE
, "suspend", NULL
);
317 dialog_switch_list ();
319 ret
= MSG_NOT_HANDLED
;
323 dialog_switch_next ();
325 ret
= MSG_NOT_HANDLED
;
329 dialog_switch_prev ();
331 ret
= MSG_NOT_HANDLED
;
335 ret
= MSG_NOT_HANDLED
;
341 /* --------------------------------------------------------------------------------------------- */
344 dlg_handle_key (WDialog
* h
, int d_key
)
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
)
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.
366 dlg_mouse_translator (Gpm_Event
* event
, Widget
* w
)
370 me
= mouse_translate_event (w
, event
);
372 return mouse_process_event (w
, &me
);
375 /* --------------------------------------------------------------------------------------------- */
378 dlg_mouse_event (WDialog
* h
, Gpm_Event
* event
)
380 Widget
*wh
= WIDGET (h
);
384 /* close the dialog by mouse left click out of dialog area */
385 if (mouse_close_dialog
&& !h
->fullscreen
&& ((event
->buttons
& GPM_B_LEFT
) != 0)
386 && ((event
->type
& GPM_DOWN
) != 0) && !mouse_global_in_widget (event
, wh
))
388 h
->ret_value
= B_CANCEL
;
393 if (wh
->mouse_callback
!= NULL
)
397 mou
= dlg_mouse_translator (event
, wh
);
398 if (mou
!= MOU_UNHANDLED
)
402 /* send the event to widgets in reverse Z-order */
403 p
= g_list_last (h
->widgets
);
406 Widget
*w
= WIDGET (p
->data
);
408 if ((w
->options
& W_DISABLED
) == 0 && w
->mouse_callback
!= NULL
)
410 /* put global cursor position to the widget */
413 ret
= dlg_mouse_translator (event
, w
);
414 if (ret
!= MOU_UNHANDLED
)
418 p
= g_list_previous (p
);
422 return MOU_UNHANDLED
;
425 /* --------------------------------------------------------------------------------------------- */
428 dlg_try_hotkey (WDialog
* h
, int d_key
)
435 if (h
->widgets
== NULL
)
436 return MSG_NOT_HANDLED
;
438 if (h
->current
== NULL
)
439 h
->current
= h
->widgets
;
442 * Explanation: we don't send letter hotkeys to other widgets if
443 * the currently selected widget is an input line
446 current
= WIDGET (h
->current
->data
);
448 if ((current
->options
& W_DISABLED
) != 0)
449 return MSG_NOT_HANDLED
;
451 if (current
->options
& W_IS_INPUT
)
453 /* skip ascii control characters, anything else can valid character in
455 if (d_key
>= 32 && d_key
< 256)
456 return MSG_NOT_HANDLED
;
459 /* If it's an alt key, send the message */
460 c
= d_key
& ~ALT (0);
461 if (d_key
& ALT (0) && g_ascii_isalpha (c
))
462 d_key
= g_ascii_tolower (c
);
464 handled
= MSG_NOT_HANDLED
;
465 if ((current
->options
& W_WANT_HOTKEY
) != 0)
466 handled
= send_message (current
, NULL
, MSG_HOTKEY
, d_key
, NULL
);
468 /* If not used, send hotkey to other widgets */
469 if (handled
== MSG_HANDLED
)
472 hot_cur
= dlg_widget_next (h
, h
->current
);
474 /* send it to all widgets */
475 while (h
->current
!= hot_cur
&& handled
== MSG_NOT_HANDLED
)
477 current
= WIDGET (hot_cur
->data
);
479 if ((current
->options
& W_WANT_HOTKEY
) != 0 && (current
->options
& W_DISABLED
) == 0)
480 handled
= send_message (current
, NULL
, MSG_HOTKEY
, d_key
, NULL
);
482 if (handled
== MSG_NOT_HANDLED
)
483 hot_cur
= dlg_widget_next (h
, hot_cur
);
486 if (handled
== MSG_HANDLED
)
487 do_select_widget (h
, hot_cur
, SELECT_EXACT
);
492 /* --------------------------------------------------------------------------------------------- */
495 dlg_key_event (WDialog
* h
, int d_key
)
499 if (h
->widgets
== NULL
)
502 if (h
->current
== NULL
)
503 h
->current
= h
->widgets
;
505 /* TAB used to cycle */
506 if ((h
->flags
& DLG_WANT_TAB
) == 0)
513 else if ((d_key
& ~(KEY_M_SHIFT
| KEY_M_CTRL
)) == '\t')
520 /* first can dlg_callback handle the key */
521 handled
= send_message (h
, NULL
, MSG_KEY
, d_key
, NULL
);
523 /* next try the hotkey */
524 if (handled
== MSG_NOT_HANDLED
)
525 handled
= dlg_try_hotkey (h
, d_key
);
527 if (handled
== MSG_HANDLED
)
528 send_message (h
, NULL
, MSG_HOTKEY_HANDLED
, 0, NULL
);
530 /* not used - then try widget_callback */
531 handled
= send_message (h
->current
->data
, NULL
, MSG_KEY
, d_key
, NULL
);
533 /* not used- try to use the unhandled case */
534 if (handled
== MSG_NOT_HANDLED
)
535 handled
= send_message (h
, NULL
, MSG_UNHANDLED_KEY
, d_key
, NULL
);
537 if (handled
== MSG_NOT_HANDLED
)
538 handled
= dlg_handle_key (h
, d_key
);
541 send_message (h
, NULL
, MSG_POST_KEY
, d_key
, NULL
);
544 /* --------------------------------------------------------------------------------------------- */
547 frontend_dlg_run (WDialog
* h
)
553 /* close opened editors, viewers, etc */
554 if (!h
->modal
&& mc_global
.midnight_shutdown
)
556 send_message (h
, NULL
, MSG_VALIDATE
, 0, NULL
);
560 while (h
->state
== DLG_ACTIVE
)
564 if (mc_global
.tty
.winch_flag
!= 0)
565 dialog_change_screen_size ();
570 execute_hooks (idle_hook
);
572 while ((WIDGET (h
)->options
& W_WANT_IDLE
) != 0 && is_idle ())
573 send_message (h
, NULL
, MSG_IDLE
, 0, NULL
);
575 /* Allow terminating the dialog from the idle handler */
576 if (h
->state
!= DLG_ACTIVE
)
582 /* Clear interrupt flag */
583 tty_got_interrupt ();
584 d_key
= tty_get_event (&event
, h
->mouse_status
== MOU_REPEAT
, TRUE
);
586 dlg_process_event (h
, d_key
, &event
);
588 if (h
->state
== DLG_CLOSED
)
589 send_message (h
, NULL
, MSG_VALIDATE
, 0, NULL
);
593 /* --------------------------------------------------------------------------------------------- */
596 dlg_find_widget_by_id (gconstpointer a
, gconstpointer b
)
598 const Widget
*w
= CONST_WIDGET (a
);
599 unsigned long id
= GPOINTER_TO_UINT (b
);
601 return w
->id
== id
? 0 : 1;
604 /* --------------------------------------------------------------------------------------------- */
607 dlg_set_top_or_bottom_widget (void *w
, gboolean set_top
)
609 Widget
*widget
= WIDGET (w
);
610 WDialog
*h
= widget
->owner
;
613 l
= g_list_find (h
->widgets
, w
);
615 abort (); /* widget is not in dialog, this should not happen */
617 /* unfocus prevoius widget and focus current one before widget reordering */
618 if (set_top
&& h
->state
== DLG_ACTIVE
)
619 do_select_widget (h
, l
, SELECT_EXACT
);
621 /* widget reordering */
622 h
->widgets
= g_list_remove_link (h
->widgets
, l
);
624 h
->widgets
= g_list_concat (h
->widgets
, l
);
626 h
->widgets
= g_list_concat (l
, h
->widgets
);
629 /* --------------------------------------------------------------------------------------------- */
630 /*** public functions ****************************************************************************/
631 /* --------------------------------------------------------------------------------------------- */
633 /** Clean the dialog area, draw the frame and the title */
635 dlg_default_repaint (WDialog
* h
)
637 Widget
*wh
= WIDGET (h
);
641 if (h
->state
!= DLG_ACTIVE
)
644 space
= (h
->flags
& DLG_COMPACT
) ? 0 : 1;
646 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
648 tty_draw_box (wh
->y
+ space
, wh
->x
+ space
, wh
->lines
- 2 * space
, wh
->cols
- 2 * space
, FALSE
);
650 if (h
->title
!= NULL
)
652 tty_setcolor (h
->color
[DLG_COLOR_TITLE
]);
653 widget_move (h
, space
, (wh
->cols
- str_term_width1 (h
->title
)) / 2);
654 tty_print_string (h
->title
);
658 /* --------------------------------------------------------------------------------------------- */
659 /** this function allows to set dialog position */
662 dlg_set_position (WDialog
* h
, int y1
, int x1
, int y2
, int x2
)
664 Widget
*wh
= WIDGET (h
);
666 /* save old positions, will be used to reposition childs */
668 int shift_x
, shift_y
, scale_x
, scale_y
;
670 /* save old positions, will be used to reposition childs */
681 /* dialog is empty */
682 if (h
->widgets
== NULL
)
685 if (h
->current
== NULL
)
686 h
->current
= h
->widgets
;
688 /* values by which controls should be moved */
689 shift_x
= wh
->x
- ox
;
690 shift_y
= wh
->y
- oy
;
691 scale_x
= wh
->cols
- oc
;
692 scale_y
= wh
->lines
- ol
;
694 if ((shift_x
!= 0) || (shift_y
!= 0) || (scale_x
!= 0) || (scale_y
!= 0))
698 for (w
= h
->widgets
; w
!= NULL
; w
= g_list_next (w
))
700 /* there are, mainly, 2 generally possible
703 1. control sticks to one side - it
706 2. control sticks to two sides of
707 one direction - it should be sized */
709 Widget
*c
= WIDGET (w
->data
);
713 int lines
= c
->lines
;
715 if ((c
->pos_flags
& WPOS_CENTER_HORZ
) != 0)
716 x
= wh
->x
+ (wh
->cols
- c
->cols
) / 2;
717 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0 && (c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
722 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0)
724 else if ((c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
725 x
+= shift_x
+ scale_x
;
727 if ((c
->pos_flags
& WPOS_CENTER_VERT
) != 0)
728 y
= wh
->y
+ (wh
->lines
- c
->lines
) / 2;
729 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0 && (c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
734 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0)
736 else if ((c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
737 y
+= shift_y
+ scale_y
;
739 widget_set_size (c
, y
, x
, lines
, cols
);
744 /* --------------------------------------------------------------------------------------------- */
745 /** Set dialog size and position */
748 dlg_set_size (WDialog
* h
, int lines
, int cols
)
750 int x
= WIDGET (h
)->x
;
751 int y
= WIDGET (h
)->y
;
753 if ((h
->flags
& DLG_CENTER
) != 0)
755 y
= (LINES
- lines
) / 2;
756 x
= (COLS
- cols
) / 2;
759 if ((h
->flags
& DLG_TRYUP
) != 0)
767 dlg_set_position (h
, y
, x
, y
+ lines
, x
+ cols
);
770 /* --------------------------------------------------------------------------------------------- */
771 /** Default dialog callback */
774 dlg_default_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
776 WDialog
*h
= DIALOG (w
);
785 if (h
->color
!= NULL
)
787 dlg_default_repaint (h
);
790 return MSG_NOT_HANDLED
;
793 dlg_broadcast_msg_to (h
, MSG_IDLE
, FALSE
, W_WANT_IDLE
);
797 /* this is default resizing mechanism */
798 /* the main idea of this code is to resize dialog
799 according to flags (if any of flags require automatic
800 resizing, like DLG_CENTER, end after that reposition
801 controls in dialog according to flags of widget) */
802 dlg_set_size (h
, w
->lines
, w
->cols
);
809 return MSG_NOT_HANDLED
;
812 /* --------------------------------------------------------------------------------------------- */
815 dlg_create (gboolean modal
, int y1
, int x1
, int lines
, int cols
,
816 const int *colors
, widget_cb_fn callback
, widget_mouse_cb_fn mouse_callback
,
817 const char *help_ctx
, const char *title
, dlg_flags_t flags
)
822 new_d
= g_new0 (WDialog
, 1);
824 widget_init (w
, y1
, x1
, lines
, cols
, (callback
!= NULL
) ? callback
: dlg_default_callback
,
826 widget_want_cursor (w
, FALSE
);
828 new_d
->state
= DLG_CONSTRUCT
;
829 new_d
->modal
= modal
;
830 new_d
->color
= colors
;
831 new_d
->help_ctx
= help_ctx
;
832 new_d
->flags
= flags
;
835 dlg_set_size (new_d
, lines
, cols
);
836 new_d
->fullscreen
= (w
->x
== 0 && w
->y
== 0 && w
->cols
== COLS
&& w
->lines
== LINES
);
838 new_d
->mouse_status
= MOU_UNHANDLED
;
840 /* Strip existing spaces, add one space before and after the title */
841 if (title
!= NULL
&& *title
!= '\0')
845 t
= g_strstrip (g_strdup (title
));
847 new_d
->title
= g_strdup_printf (" %s ", t
);
851 /* unique name of event group for this dialog */
852 new_d
->event_group
= g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG
, (void *) new_d
);
857 /* --------------------------------------------------------------------------------------------- */
860 dlg_set_default_colors (void)
862 dialog_colors
[DLG_COLOR_NORMAL
] = COLOR_NORMAL
;
863 dialog_colors
[DLG_COLOR_FOCUS
] = COLOR_FOCUS
;
864 dialog_colors
[DLG_COLOR_HOT_NORMAL
] = COLOR_HOT_NORMAL
;
865 dialog_colors
[DLG_COLOR_HOT_FOCUS
] = COLOR_HOT_FOCUS
;
866 dialog_colors
[DLG_COLOR_TITLE
] = COLOR_TITLE
;
868 alarm_colors
[DLG_COLOR_NORMAL
] = ERROR_COLOR
;
869 alarm_colors
[DLG_COLOR_FOCUS
] = ERROR_FOCUS
;
870 alarm_colors
[DLG_COLOR_HOT_NORMAL
] = ERROR_HOT_NORMAL
;
871 alarm_colors
[DLG_COLOR_HOT_FOCUS
] = ERROR_HOT_FOCUS
;
872 alarm_colors
[DLG_COLOR_TITLE
] = ERROR_TITLE
;
874 listbox_colors
[DLG_COLOR_NORMAL
] = PMENU_ENTRY_COLOR
;
875 listbox_colors
[DLG_COLOR_FOCUS
] = PMENU_SELECTED_COLOR
;
876 listbox_colors
[DLG_COLOR_HOT_NORMAL
] = PMENU_ENTRY_COLOR
;
877 listbox_colors
[DLG_COLOR_HOT_FOCUS
] = PMENU_SELECTED_COLOR
;
878 listbox_colors
[DLG_COLOR_TITLE
] = PMENU_TITLE_COLOR
;
881 /* --------------------------------------------------------------------------------------------- */
884 dlg_erase (WDialog
* h
)
886 if ((h
!= NULL
) && (h
->state
== DLG_ACTIVE
))
888 Widget
*wh
= WIDGET (h
);
890 tty_fill_region (wh
->y
, wh
->x
, wh
->lines
, wh
->cols
, ' ');
894 /* --------------------------------------------------------------------------------------------- */
896 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
900 add_widget_autopos (WDialog
* h
, void *w
, widget_pos_flags_t pos_flags
, const void *before
)
902 Widget
*wh
= WIDGET (h
);
905 /* Don't accept 0 widgets */
911 if ((pos_flags
& WPOS_CENTER_HORZ
) != 0)
912 widget
->x
= (wh
->cols
- widget
->cols
) / 2;
915 if ((pos_flags
& WPOS_CENTER_VERT
) != 0)
916 widget
->y
= (wh
->lines
- widget
->lines
) / 2;
920 widget
->pos_flags
= pos_flags
;
921 widget
->id
= h
->widget_id
++;
923 if (h
->widgets
== NULL
|| before
== NULL
)
925 h
->widgets
= g_list_append (h
->widgets
, widget
);
926 h
->current
= g_list_last (h
->widgets
);
932 b
= g_list_find (h
->widgets
, before
);
934 /* don't accept widget not from dialog. This shouldn't happen */
939 h
->widgets
= g_list_insert_before (h
->widgets
, b
, widget
);
941 h
->current
= g_list_previous (b
);
943 h
->current
= g_list_last (h
->widgets
);
946 /* widget has been added in runtime */
947 if (h
->state
== DLG_ACTIVE
)
949 send_message (widget
, NULL
, MSG_INIT
, 0, NULL
);
950 send_message (widget
, NULL
, MSG_DRAW
, 0, NULL
);
951 send_message (widget
, NULL
, MSG_FOCUS
, 0, NULL
);
957 /* --------------------------------------------------------------------------------------------- */
958 /** wrapper to simply add lefttop positioned controls */
961 add_widget (WDialog
* h
, void *w
)
963 return add_widget_autopos (h
, w
, WPOS_KEEP_DEFAULT
,
964 h
->current
!= NULL
? h
->current
->data
: NULL
);
967 /* --------------------------------------------------------------------------------------------- */
970 add_widget_before (WDialog
* h
, void *w
, void *before
)
972 return add_widget_autopos (h
, w
, WPOS_KEEP_DEFAULT
, before
);
975 /* --------------------------------------------------------------------------------------------- */
977 /** delete widget from dialog */
984 /* Don't accept NULL widget. This shouldn't happen */
988 h
= WIDGET (w
)->owner
;
990 d
= g_list_find (h
->widgets
, w
);
992 h
->current
= dlg_widget_next (h
, d
);
994 h
->widgets
= g_list_remove_link (h
->widgets
, d
);
995 send_message (d
->data
, NULL
, MSG_DESTROY
, 0, NULL
);
999 /* widget has been deleted in runtime */
1000 if (h
->state
== DLG_ACTIVE
)
1007 /* --------------------------------------------------------------------------------------------- */
1016 if ((d
!= NULL
) && (d
->data
!= NULL
))
1017 dlg_redraw (DIALOG (d
->data
));
1021 /* Search first fullscreen dialog */
1022 for (; d
!= NULL
; d
= g_list_next (d
))
1023 if (d
->data
!= NULL
&& DIALOG (d
->data
)->fullscreen
)
1025 /* back to top dialog */
1026 for (; d
!= NULL
; d
= g_list_previous (d
))
1027 if (d
->data
!= NULL
)
1028 dlg_redraw (DIALOG (d
->data
));
1032 /* --------------------------------------------------------------------------------------------- */
1033 /** broadcast a message to all the widgets in a dialog */
1036 dlg_broadcast_msg (WDialog
* h
, widget_msg_t msg
)
1038 dlg_broadcast_msg_to (h
, msg
, FALSE
, 0);
1041 /* --------------------------------------------------------------------------------------------- */
1044 dlg_focus (WDialog
* h
)
1046 /* cannot focus disabled widget */
1047 if ((h
->current
!= NULL
) && (h
->state
== DLG_CONSTRUCT
|| h
->state
== DLG_ACTIVE
))
1049 Widget
*current
= WIDGET (h
->current
->data
);
1051 if (((current
->options
& W_DISABLED
) == 0)
1052 && (send_message (current
, NULL
, MSG_FOCUS
, 0, NULL
) == MSG_HANDLED
))
1054 send_message (h
, current
, MSG_FOCUS
, 0, NULL
);
1062 /* --------------------------------------------------------------------------------------------- */
1063 /** Find the widget with the given callback in the dialog h */
1066 find_widget_type (const WDialog
* h
, widget_cb_fn callback
)
1070 w
= g_list_find_custom (h
->widgets
, (gconstpointer
) callback
, dlg_find_widget_callback
);
1072 return (w
== NULL
) ? NULL
: WIDGET (w
->data
);
1075 /* --------------------------------------------------------------------------------------------- */
1076 /** Find the widget with the given id */
1079 dlg_find_by_id (const WDialog
* h
, unsigned long id
)
1083 w
= g_list_find_custom (h
->widgets
, GUINT_TO_POINTER (id
), dlg_find_widget_by_id
);
1084 return w
!= NULL
? WIDGET (w
->data
) : NULL
;
1087 /* --------------------------------------------------------------------------------------------- */
1088 /** Find the widget with the given id in the dialog h and select it */
1091 dlg_select_by_id (const WDialog
* h
, unsigned long id
)
1095 w
= dlg_find_by_id (h
, id
);
1097 dlg_select_widget (w
);
1100 /* --------------------------------------------------------------------------------------------- */
1102 * Try to select widget in the dialog.
1106 dlg_select_widget (void *w
)
1108 Widget
*widget
= WIDGET (w
);
1109 WDialog
*h
= widget
->owner
;
1111 do_select_widget (h
, g_list_find (h
->widgets
, widget
), SELECT_EXACT
);
1114 /* --------------------------------------------------------------------------------------------- */
1116 * Set widget at top of widget list and make it current.
1120 dlg_set_top_widget (void *w
)
1122 dlg_set_top_or_bottom_widget (w
, TRUE
);
1125 /* --------------------------------------------------------------------------------------------- */
1127 * Set widget at bottom of widget list.
1131 dlg_set_bottom_widget (void *w
)
1133 dlg_set_top_or_bottom_widget (w
, FALSE
);
1136 /* --------------------------------------------------------------------------------------------- */
1137 /** Try to select previous widget in the tab order */
1140 dlg_one_up (WDialog
* h
)
1142 if (h
->widgets
!= NULL
)
1143 do_select_widget (h
, dlg_widget_prev (h
, h
->current
), SELECT_PREV
);
1146 /* --------------------------------------------------------------------------------------------- */
1147 /** Try to select next widget in the tab order */
1150 dlg_one_down (WDialog
* h
)
1152 if (h
->widgets
!= NULL
)
1153 do_select_widget (h
, dlg_widget_next (h
, h
->current
), SELECT_NEXT
);
1156 /* --------------------------------------------------------------------------------------------- */
1159 update_cursor (WDialog
* h
)
1161 GList
*p
= h
->current
;
1163 if ((p
!= NULL
) && (h
->state
== DLG_ACTIVE
))
1167 w
= WIDGET (p
->data
);
1169 if (((w
->options
& W_DISABLED
) == 0) && ((w
->options
& W_WANT_CURSOR
) != 0))
1170 send_message (w
, NULL
, MSG_CURSOR
, 0, NULL
);
1174 p
= dlg_widget_next (h
, p
);
1175 if (p
== h
->current
)
1178 w
= WIDGET (p
->data
);
1180 if (((w
->options
& W_DISABLED
) == 0) && ((w
->options
& W_WANT_CURSOR
) != 0))
1181 if (send_message (w
, NULL
, MSG_CURSOR
, 0, NULL
) == MSG_HANDLED
)
1188 /* --------------------------------------------------------------------------------------------- */
1190 * Redraw the widgets in reverse order, leaving the current widget
1195 dlg_redraw (WDialog
* h
)
1197 if (h
->state
!= DLG_ACTIVE
)
1200 if (h
->winch_pending
)
1202 h
->winch_pending
= FALSE
;
1203 send_message (h
, NULL
, MSG_RESIZE
, 0, NULL
);
1206 send_message (h
, NULL
, MSG_DRAW
, 0, NULL
);
1207 dlg_broadcast_msg (h
, MSG_DRAW
);
1211 /* --------------------------------------------------------------------------------------------- */
1214 dlg_stop (WDialog
* h
)
1216 h
->state
= DLG_CLOSED
;
1219 /* --------------------------------------------------------------------------------------------- */
1220 /** Init the process */
1223 dlg_init (WDialog
* h
)
1225 if (top_dlg
!= NULL
&& DIALOG (top_dlg
->data
)->modal
)
1228 /* add dialog to the stack */
1229 top_dlg
= g_list_prepend (top_dlg
, h
);
1231 /* Initialize dialog manager and widgets */
1232 if (h
->state
== DLG_CONSTRUCT
)
1235 dialog_switch_add (h
);
1237 send_message (h
, NULL
, MSG_INIT
, 0, NULL
);
1238 dlg_broadcast_msg (h
, MSG_INIT
);
1239 dlg_read_history (h
);
1242 h
->state
= DLG_ACTIVE
;
1244 /* first send MSG_DRAW to dialog itself and all widgets... */
1247 /* ...then send MSG_FOCUS to select the first widget that can take focus */
1248 while (h
->current
!= NULL
&& !dlg_focus (h
))
1249 h
->current
= dlg_widget_next (h
, h
->current
);
1255 /* --------------------------------------------------------------------------------------------- */
1258 dlg_process_event (WDialog
* h
, int key
, Gpm_Event
* event
)
1262 if (tty_got_interrupt ())
1263 if (send_message (h
, NULL
, MSG_ACTION
, CK_Cancel
, NULL
) != MSG_HANDLED
)
1264 dlg_execute_cmd (h
, CK_Cancel
);
1266 else if (key
== EV_MOUSE
)
1267 h
->mouse_status
= dlg_mouse_event (h
, event
);
1269 dlg_key_event (h
, key
);
1272 /* --------------------------------------------------------------------------------------------- */
1273 /** Shutdown the dlg_run */
1276 dlg_run_done (WDialog
* h
)
1278 top_dlg
= g_list_remove (top_dlg
, h
);
1280 if (h
->state
== DLG_CLOSED
)
1282 send_message (h
, h
->current
->data
, MSG_END
, 0, NULL
);
1284 dialog_switch_remove (h
);
1288 /* --------------------------------------------------------------------------------------------- */
1290 * Standard run dialog routine
1291 * We have to keep this routine small so that we can duplicate it's
1292 * behavior on complex routines like the file routines, this way,
1293 * they can call the dlg_process_event without rewriting all the code
1297 dlg_run (WDialog
* h
)
1300 frontend_dlg_run (h
);
1302 return h
->ret_value
;
1305 /* --------------------------------------------------------------------------------------------- */
1308 dlg_destroy (WDialog
* h
)
1310 /* if some widgets have history, save all history at one moment here */
1311 dlg_save_history (h
);
1312 dlg_broadcast_msg (h
, MSG_DESTROY
);
1313 g_list_free_full (h
->widgets
, g_free
);
1314 mc_event_group_del (h
->event_group
);
1315 g_free (h
->event_group
);
1322 /* --------------------------------------------------------------------------------------------- */
1325 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1328 dlg_save_history (WDialog
* h
)
1333 if (num_history_items_recorded
== 0) /* this is how to disable */
1336 profile
= mc_config_get_full_path (MC_HISTORY_FILE
);
1337 i
= open (profile
, O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
);
1341 /* Make sure the history is only readable by the user */
1342 if (chmod (profile
, S_IRUSR
| S_IWUSR
) != -1 || errno
== ENOENT
)
1344 ev_history_load_save_t event_data
;
1346 event_data
.cfg
= mc_config_init (profile
, FALSE
);
1347 event_data
.receiver
= NULL
;
1349 /* get all histories in dialog */
1350 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_SAVE
, &event_data
);
1352 mc_config_save_file (event_data
.cfg
, NULL
);
1353 mc_config_deinit (event_data
.cfg
);
1359 /* --------------------------------------------------------------------------------------------- */
1362 dlg_get_title (const WDialog
* h
, size_t len
)
1369 if (h
->get_title
!= NULL
)
1370 t
= h
->get_title (h
, len
);
1377 /* --------------------------------------------------------------------------------------------- */