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
, widget_options_t 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
)
187 Widget
*wh
= WIDGET (h
);
189 if (widget_get_state (wh
, WST_CONSTRUCT
) || widget_get_state (wh
, WST_ACTIVE
))
191 Widget
*current
= WIDGET (h
->current
->data
);
193 if (send_message (current
, NULL
, MSG_UNFOCUS
, 0, NULL
) == MSG_HANDLED
)
195 send_message (h
, current
, MSG_UNFOCUS
, 0, NULL
);
205 /* --------------------------------------------------------------------------------------------- */
208 dlg_find_widget_callback (const void *a
, const void *b
)
210 const Widget
*w
= CONST_WIDGET (a
);
211 const widget_cb_fn f
= b
;
213 return (w
->callback
== f
) ? 0 : 1;
216 /* --------------------------------------------------------------------------------------------- */
219 * Put widget on top or bottom of Z-order.
222 dlg_reorder_widgets (GList
* l
, gboolean set_top
)
224 WDialog
*h
= WIDGET (l
->data
)->owner
;
226 h
->widgets
= g_list_remove_link (h
->widgets
, l
);
228 h
->widgets
= g_list_concat (h
->widgets
, l
);
230 h
->widgets
= g_list_concat (l
, h
->widgets
);
233 /* --------------------------------------------------------------------------------------------- */
235 * Try to select another widget. If forward is set, follow tab order.
236 * Otherwise go to the previous widget.
240 do_select_widget (WDialog
* h
, GList
* w
, select_dir_t dir
)
242 Widget
*w0
= WIDGET (h
->current
->data
);
244 if (!dlg_unfocus (h
))
257 h
->current
= g_list_find (h
->widgets
, w0
);
260 /* try find another widget that can take focus */
264 h
->current
= dlg_widget_next (h
, h
->current
);
267 h
->current
= dlg_widget_prev (h
, h
->current
);
273 while (h
->current
!= w
);
275 if (widget_get_options (WIDGET (h
->current
->data
), WOP_TOP_SELECT
))
276 dlg_reorder_widgets (h
->current
, TRUE
);
278 if (widget_overlapped (w0
, WIDGET (h
->current
->data
)))
280 send_message (h
->current
->data
, NULL
, MSG_DRAW
, 0, NULL
);
281 send_message (h
->current
->data
, NULL
, MSG_FOCUS
, 0, NULL
);
285 /* --------------------------------------------------------------------------------------------- */
294 /* Use this if the refreshes fail */
297 #endif /* HAVE_SLANG */
300 /* --------------------------------------------------------------------------------------------- */
303 dlg_execute_cmd (WDialog
* h
, long command
)
305 cb_ret_t ret
= MSG_HANDLED
;
309 h
->ret_value
= B_ENTER
;
313 h
->ret_value
= B_CANCEL
;
328 ev_help_t event_data
= { NULL
, h
->help_ctx
};
329 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
334 mc_event_raise (MCEVENT_GROUP_CORE
, "suspend", NULL
);
342 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
343 dialog_switch_list ();
345 ret
= MSG_NOT_HANDLED
;
348 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
349 dialog_switch_next ();
351 ret
= MSG_NOT_HANDLED
;
354 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
355 dialog_switch_prev ();
357 ret
= MSG_NOT_HANDLED
;
361 ret
= MSG_NOT_HANDLED
;
367 /* --------------------------------------------------------------------------------------------- */
370 dlg_handle_key (WDialog
* h
, int d_key
)
374 command
= keybind_lookup_keymap_command (dialog_map
, d_key
);
376 if (command
== CK_IgnoreKey
)
377 return MSG_NOT_HANDLED
;
379 if (send_message (h
, NULL
, MSG_ACTION
, command
, NULL
) == MSG_HANDLED
380 || dlg_execute_cmd (h
, command
) == MSG_HANDLED
)
383 return MSG_NOT_HANDLED
;
386 /* --------------------------------------------------------------------------------------------- */
388 * This is the low-level mouse handler.
389 * It receives a Gpm_Event event and translates it into a higher level protocol.
392 dlg_mouse_translator (Gpm_Event
* event
, Widget
* w
)
396 me
= mouse_translate_event (w
, event
);
398 return mouse_process_event (w
, &me
);
401 /* --------------------------------------------------------------------------------------------- */
404 dlg_mouse_event (WDialog
* h
, Gpm_Event
* event
)
406 Widget
*wh
= WIDGET (h
);
410 /* close the dialog by mouse left click out of dialog area */
411 if (mouse_close_dialog
&& (wh
->pos_flags
& WPOS_FULLSCREEN
) == 0
412 && ((event
->buttons
& GPM_B_LEFT
) != 0) && ((event
->type
& GPM_DOWN
) != 0)
413 && !mouse_global_in_widget (event
, wh
))
415 h
->ret_value
= B_CANCEL
;
420 if (wh
->mouse_callback
!= NULL
)
424 mou
= dlg_mouse_translator (event
, wh
);
425 if (mou
!= MOU_UNHANDLED
)
429 /* send the event to widgets in reverse Z-order */
430 p
= g_list_last (h
->widgets
);
433 Widget
*w
= WIDGET (p
->data
);
435 if (!widget_get_state (w
, WST_DISABLED
) && w
->mouse_callback
!= NULL
)
437 /* put global cursor position to the widget */
440 ret
= dlg_mouse_translator (event
, w
);
441 if (ret
!= MOU_UNHANDLED
)
445 p
= g_list_previous (p
);
449 return MOU_UNHANDLED
;
452 /* --------------------------------------------------------------------------------------------- */
455 dlg_try_hotkey (WDialog
* h
, int d_key
)
462 if (h
->widgets
== NULL
)
463 return MSG_NOT_HANDLED
;
465 if (h
->current
== NULL
)
466 h
->current
= h
->widgets
;
469 * Explanation: we don't send letter hotkeys to other widgets if
470 * the currently selected widget is an input line
473 current
= WIDGET (h
->current
->data
);
475 if (widget_get_state (current
, WST_DISABLED
))
476 return MSG_NOT_HANDLED
;
478 if (widget_get_options (current
, WOP_IS_INPUT
))
480 /* skip ascii control characters, anything else can valid character in
482 if (d_key
>= 32 && d_key
< 256)
483 return MSG_NOT_HANDLED
;
486 /* If it's an alt key, send the message */
487 c
= d_key
& ~ALT (0);
488 if (d_key
& ALT (0) && g_ascii_isalpha (c
))
489 d_key
= g_ascii_tolower (c
);
491 handled
= MSG_NOT_HANDLED
;
492 if (widget_get_options (current
, WOP_WANT_HOTKEY
))
493 handled
= send_message (current
, NULL
, MSG_HOTKEY
, d_key
, NULL
);
495 /* If not used, send hotkey to other widgets */
496 if (handled
== MSG_HANDLED
)
499 hot_cur
= dlg_widget_next (h
, h
->current
);
501 /* send it to all widgets */
502 while (h
->current
!= hot_cur
&& handled
== MSG_NOT_HANDLED
)
504 current
= WIDGET (hot_cur
->data
);
506 if (widget_get_options (current
, WOP_WANT_HOTKEY
)
507 && !widget_get_state (current
, WST_DISABLED
))
508 handled
= send_message (current
, NULL
, MSG_HOTKEY
, d_key
, NULL
);
510 if (handled
== MSG_NOT_HANDLED
)
511 hot_cur
= dlg_widget_next (h
, hot_cur
);
514 if (handled
== MSG_HANDLED
)
515 do_select_widget (h
, hot_cur
, SELECT_EXACT
);
520 /* --------------------------------------------------------------------------------------------- */
523 dlg_key_event (WDialog
* h
, int d_key
)
527 if (h
->widgets
== NULL
)
530 if (h
->current
== NULL
)
531 h
->current
= h
->widgets
;
533 /* TAB used to cycle */
534 if (!widget_get_options (WIDGET (h
), WOP_WANT_TAB
))
541 else if ((d_key
& ~(KEY_M_SHIFT
| KEY_M_CTRL
)) == '\t')
548 /* first can dlg_callback handle the key */
549 handled
= send_message (h
, NULL
, MSG_KEY
, d_key
, NULL
);
551 /* next try the hotkey */
552 if (handled
== MSG_NOT_HANDLED
)
553 handled
= dlg_try_hotkey (h
, d_key
);
555 if (handled
== MSG_HANDLED
)
556 send_message (h
, NULL
, MSG_HOTKEY_HANDLED
, 0, NULL
);
558 /* not used - then try widget_callback */
559 handled
= send_message (h
->current
->data
, NULL
, MSG_KEY
, d_key
, NULL
);
561 /* not used- try to use the unhandled case */
562 if (handled
== MSG_NOT_HANDLED
)
563 handled
= send_message (h
, NULL
, MSG_UNHANDLED_KEY
, d_key
, NULL
);
565 if (handled
== MSG_NOT_HANDLED
)
566 handled
= dlg_handle_key (h
, d_key
);
569 send_message (h
, NULL
, MSG_POST_KEY
, d_key
, NULL
);
572 /* --------------------------------------------------------------------------------------------- */
575 frontend_dlg_run (WDialog
* h
)
577 Widget
*wh
= WIDGET (h
);
582 /* close opened editors, viewers, etc */
583 if (!widget_get_state (wh
, WST_MODAL
) && mc_global
.midnight_shutdown
)
585 send_message (h
, NULL
, MSG_VALIDATE
, 0, NULL
);
589 while (widget_get_state (wh
, WST_ACTIVE
))
593 if (mc_global
.tty
.winch_flag
!= 0)
594 dialog_change_screen_size ();
599 execute_hooks (idle_hook
);
601 while (widget_get_state (wh
, WST_IDLE
) && is_idle ())
602 send_message (wh
, NULL
, MSG_IDLE
, 0, NULL
);
604 /* Allow terminating the dialog from the idle handler */
605 if (!widget_get_state (wh
, WST_ACTIVE
))
611 /* Clear interrupt flag */
612 tty_got_interrupt ();
613 d_key
= tty_get_event (&event
, h
->mouse_status
== MOU_REPEAT
, TRUE
);
615 dlg_process_event (h
, d_key
, &event
);
617 if (widget_get_state (wh
, WST_CLOSED
))
618 send_message (h
, NULL
, MSG_VALIDATE
, 0, NULL
);
622 /* --------------------------------------------------------------------------------------------- */
625 dlg_find_widget_by_id (gconstpointer a
, gconstpointer b
)
627 const Widget
*w
= CONST_WIDGET (a
);
628 unsigned long id
= GPOINTER_TO_UINT (b
);
630 return w
->id
== id
? 0 : 1;
633 /* --------------------------------------------------------------------------------------------- */
634 /*** public functions ****************************************************************************/
635 /* --------------------------------------------------------------------------------------------- */
637 /** Clean the dialog area, draw the frame and the title */
639 dlg_default_repaint (WDialog
* h
)
641 Widget
*wh
= WIDGET (h
);
645 if (!widget_get_state (wh
, WST_ACTIVE
))
648 space
= h
->compact
? 0 : 1;
650 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
652 tty_draw_box (wh
->y
+ space
, wh
->x
+ space
, wh
->lines
- 2 * space
, wh
->cols
- 2 * space
, FALSE
);
654 if (h
->title
!= NULL
)
656 tty_setcolor (h
->color
[DLG_COLOR_TITLE
]);
657 widget_move (h
, space
, (wh
->cols
- str_term_width1 (h
->title
)) / 2);
658 tty_print_string (h
->title
);
662 /* --------------------------------------------------------------------------------------------- */
663 /** this function allows to set dialog position */
666 dlg_set_position (WDialog
* h
, int y
, int x
, int lines
, int cols
)
668 Widget
*wh
= WIDGET (h
);
670 /* save old positions, will be used to reposition childs */
672 int shift_x
, shift_y
, scale_x
, scale_y
;
674 /* save old positions, will be used to reposition childs */
685 /* dialog is empty */
686 if (h
->widgets
== NULL
)
689 if (h
->current
== NULL
)
690 h
->current
= h
->widgets
;
692 /* values by which controls should be moved */
693 shift_x
= wh
->x
- ox
;
694 shift_y
= wh
->y
- oy
;
695 scale_x
= wh
->cols
- oc
;
696 scale_y
= wh
->lines
- ol
;
698 if ((shift_x
!= 0) || (shift_y
!= 0) || (scale_x
!= 0) || (scale_y
!= 0))
702 for (w
= h
->widgets
; w
!= NULL
; w
= g_list_next (w
))
704 /* there are, mainly, 2 generally possible
707 1. control sticks to one side - it
710 2. control sticks to two sides of
711 one direction - it should be sized */
713 Widget
*c
= WIDGET (w
->data
);
717 int clines
= c
->lines
;
719 if ((c
->pos_flags
& WPOS_CENTER_HORZ
) != 0)
720 cx
= wh
->x
+ (wh
->cols
- c
->cols
) / 2;
721 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0 && (c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
726 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0)
728 else if ((c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
729 cx
+= shift_x
+ scale_x
;
731 if ((c
->pos_flags
& WPOS_CENTER_VERT
) != 0)
732 cy
= wh
->y
+ (wh
->lines
- c
->lines
) / 2;
733 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0 && (c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
738 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0)
740 else if ((c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
741 cy
+= shift_y
+ scale_y
;
743 widget_set_size (c
, cy
, cx
, clines
, ccols
);
748 /* --------------------------------------------------------------------------------------------- */
749 /** Set dialog size and position */
752 dlg_set_size (WDialog
* h
, int lines
, int cols
)
754 Widget
*w
= WIDGET (h
);
757 if ((w
->pos_flags
& WPOS_FULLSCREEN
) != 0)
766 if ((w
->pos_flags
& WPOS_CENTER_HORZ
) != 0)
767 x
= (COLS
- cols
) / 2;
771 if ((w
->pos_flags
& WPOS_CENTER_VERT
) != 0)
772 y
= (LINES
- lines
) / 2;
776 if ((w
->pos_flags
& WPOS_TRYUP
) != 0)
785 dlg_set_position (h
, y
, x
, lines
, cols
);
788 /* --------------------------------------------------------------------------------------------- */
789 /** Default dialog callback */
792 dlg_default_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
794 WDialog
*h
= DIALOG (w
);
803 if (h
->color
!= NULL
)
805 dlg_default_repaint (h
);
808 return MSG_NOT_HANDLED
;
811 /* we don't want endless loop */
812 widget_idle (w
, FALSE
);
816 /* this is default resizing mechanism */
817 /* the main idea of this code is to resize dialog
818 according to flags (if any of flags require automatic
819 resizing, like WPOS_CENTER, end after that reposition
820 controls in dialog according to flags of widget) */
821 dlg_set_size (h
, w
->lines
, w
->cols
);
828 return MSG_NOT_HANDLED
;
831 /* --------------------------------------------------------------------------------------------- */
834 dlg_create (gboolean modal
, int y1
, int x1
, int lines
, int cols
, widget_pos_flags_t pos_flags
,
835 gboolean compact
, const int *colors
, widget_cb_fn callback
,
836 widget_mouse_cb_fn mouse_callback
, const char *help_ctx
, const char *title
)
841 if ((pos_flags
& WPOS_FULLSCREEN
) != 0)
849 new_d
= g_new0 (WDialog
, 1);
851 widget_init (w
, y1
, x1
, lines
, cols
, (callback
!= NULL
) ? callback
: dlg_default_callback
,
853 w
->pos_flags
= pos_flags
;
854 w
->options
|= WOP_TOP_SELECT
;
856 w
->state
|= WST_CONSTRUCT
;
858 w
->state
|= WST_MODAL
;
860 new_d
->color
= colors
;
861 new_d
->help_ctx
= help_ctx
;
862 new_d
->compact
= compact
;
865 dlg_set_size (new_d
, lines
, cols
);
867 new_d
->mouse_status
= MOU_UNHANDLED
;
869 /* Strip existing spaces, add one space before and after the title */
870 if (title
!= NULL
&& *title
!= '\0')
874 t
= g_strstrip (g_strdup (title
));
876 new_d
->title
= g_strdup_printf (" %s ", t
);
880 /* unique name of event group for this dialog */
881 new_d
->event_group
= g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG
, (void *) new_d
);
886 /* --------------------------------------------------------------------------------------------- */
889 dlg_set_default_colors (void)
891 dialog_colors
[DLG_COLOR_NORMAL
] = COLOR_NORMAL
;
892 dialog_colors
[DLG_COLOR_FOCUS
] = COLOR_FOCUS
;
893 dialog_colors
[DLG_COLOR_HOT_NORMAL
] = COLOR_HOT_NORMAL
;
894 dialog_colors
[DLG_COLOR_HOT_FOCUS
] = COLOR_HOT_FOCUS
;
895 dialog_colors
[DLG_COLOR_TITLE
] = COLOR_TITLE
;
897 alarm_colors
[DLG_COLOR_NORMAL
] = ERROR_COLOR
;
898 alarm_colors
[DLG_COLOR_FOCUS
] = ERROR_FOCUS
;
899 alarm_colors
[DLG_COLOR_HOT_NORMAL
] = ERROR_HOT_NORMAL
;
900 alarm_colors
[DLG_COLOR_HOT_FOCUS
] = ERROR_HOT_FOCUS
;
901 alarm_colors
[DLG_COLOR_TITLE
] = ERROR_TITLE
;
903 listbox_colors
[DLG_COLOR_NORMAL
] = PMENU_ENTRY_COLOR
;
904 listbox_colors
[DLG_COLOR_FOCUS
] = PMENU_SELECTED_COLOR
;
905 listbox_colors
[DLG_COLOR_HOT_NORMAL
] = PMENU_ENTRY_COLOR
;
906 listbox_colors
[DLG_COLOR_HOT_FOCUS
] = PMENU_SELECTED_COLOR
;
907 listbox_colors
[DLG_COLOR_TITLE
] = PMENU_TITLE_COLOR
;
910 /* --------------------------------------------------------------------------------------------- */
913 dlg_erase (WDialog
* h
)
915 Widget
*wh
= WIDGET (h
);
917 if (wh
!= NULL
&& widget_get_state (wh
, WST_ACTIVE
))
918 tty_fill_region (wh
->y
, wh
->x
, wh
->lines
, wh
->cols
, ' ');
921 /* --------------------------------------------------------------------------------------------- */
923 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
927 add_widget_autopos (WDialog
* h
, void *w
, widget_pos_flags_t pos_flags
, const void *before
)
929 Widget
*wh
= WIDGET (h
);
932 /* Don't accept 0 widgets */
938 if ((pos_flags
& WPOS_CENTER_HORZ
) != 0)
939 widget
->x
= (wh
->cols
- widget
->cols
) / 2;
942 if ((pos_flags
& WPOS_CENTER_VERT
) != 0)
943 widget
->y
= (wh
->lines
- widget
->lines
) / 2;
947 widget
->pos_flags
= pos_flags
;
948 widget
->id
= h
->widget_id
++;
950 if (h
->widgets
== NULL
|| before
== NULL
)
952 h
->widgets
= g_list_append (h
->widgets
, widget
);
953 h
->current
= g_list_last (h
->widgets
);
959 b
= g_list_find (h
->widgets
, before
);
961 /* don't accept widget not from dialog. This shouldn't happen */
966 h
->widgets
= g_list_insert_before (h
->widgets
, b
, widget
);
968 h
->current
= g_list_previous (b
);
970 h
->current
= g_list_last (h
->widgets
);
973 /* widget has been added in runtime */
974 if (widget_get_state (wh
, WST_ACTIVE
))
976 send_message (widget
, NULL
, MSG_INIT
, 0, NULL
);
977 send_message (widget
, NULL
, MSG_DRAW
, 0, NULL
);
978 send_message (widget
, NULL
, MSG_FOCUS
, 0, NULL
);
984 /* --------------------------------------------------------------------------------------------- */
985 /** wrapper to simply add lefttop positioned controls */
988 add_widget (WDialog
* h
, void *w
)
990 return add_widget_autopos (h
, w
, WPOS_KEEP_DEFAULT
,
991 h
->current
!= NULL
? h
->current
->data
: NULL
);
994 /* --------------------------------------------------------------------------------------------- */
997 add_widget_before (WDialog
* h
, void *w
, void *before
)
999 return add_widget_autopos (h
, w
, WPOS_KEEP_DEFAULT
, before
);
1002 /* --------------------------------------------------------------------------------------------- */
1004 /** delete widget from dialog */
1006 del_widget (void *w
)
1011 /* Don't accept NULL widget. This shouldn't happen */
1015 h
= WIDGET (w
)->owner
;
1017 d
= g_list_find (h
->widgets
, w
);
1018 if (d
== h
->current
)
1019 h
->current
= dlg_widget_next (h
, d
);
1021 h
->widgets
= g_list_remove_link (h
->widgets
, d
);
1022 send_message (d
->data
, NULL
, MSG_DESTROY
, 0, NULL
);
1026 /* widget has been deleted in runtime */
1027 if (widget_get_state (WIDGET (h
), WST_ACTIVE
))
1034 /* --------------------------------------------------------------------------------------------- */
1043 if ((d
!= NULL
) && (d
->data
!= NULL
))
1044 dlg_redraw (DIALOG (d
->data
));
1048 /* Search first fullscreen dialog */
1049 for (; d
!= NULL
; d
= g_list_next (d
))
1050 if (d
->data
!= NULL
&& (WIDGET (d
->data
)->pos_flags
& WPOS_FULLSCREEN
) != 0)
1052 /* back to top dialog */
1053 for (; d
!= NULL
; d
= g_list_previous (d
))
1054 if (d
->data
!= NULL
)
1055 dlg_redraw (DIALOG (d
->data
));
1059 /* --------------------------------------------------------------------------------------------- */
1060 /** broadcast a message to all the widgets in a dialog */
1063 dlg_broadcast_msg (WDialog
* h
, widget_msg_t msg
)
1065 dlg_broadcast_msg_to (h
, msg
, FALSE
, 0);
1068 /* --------------------------------------------------------------------------------------------- */
1071 dlg_focus (WDialog
* h
)
1073 /* cannot focus disabled widget */
1074 if (h
->current
!= NULL
)
1076 Widget
*wh
= WIDGET (h
);
1078 if (widget_get_state (wh
, WST_CONSTRUCT
) || widget_get_state (wh
, WST_ACTIVE
))
1080 Widget
*current
= WIDGET (h
->current
->data
);
1082 if (!widget_get_state (current
, WST_DISABLED
)
1083 && (send_message (current
, NULL
, MSG_FOCUS
, 0, NULL
) == MSG_HANDLED
))
1085 send_message (h
, current
, MSG_FOCUS
, 0, NULL
);
1094 /* --------------------------------------------------------------------------------------------- */
1095 /** Find the widget with the given callback in the dialog h */
1098 find_widget_type (const WDialog
* h
, widget_cb_fn callback
)
1102 w
= g_list_find_custom (h
->widgets
, (gconstpointer
) callback
, dlg_find_widget_callback
);
1104 return (w
== NULL
) ? NULL
: WIDGET (w
->data
);
1107 /* --------------------------------------------------------------------------------------------- */
1108 /** Find the widget with the given id */
1111 dlg_find_by_id (const WDialog
* h
, unsigned long id
)
1115 w
= g_list_find_custom (h
->widgets
, GUINT_TO_POINTER (id
), dlg_find_widget_by_id
);
1116 return w
!= NULL
? WIDGET (w
->data
) : NULL
;
1119 /* --------------------------------------------------------------------------------------------- */
1120 /** Find the widget with the given id in the dialog h and select it */
1123 dlg_select_by_id (const WDialog
* h
, unsigned long id
)
1127 w
= dlg_find_by_id (h
, id
);
1129 dlg_select_widget (w
);
1132 /* --------------------------------------------------------------------------------------------- */
1134 * Try to select widget in the dialog.
1138 dlg_select_widget (void *w
)
1140 Widget
*widget
= WIDGET (w
);
1141 WDialog
*h
= widget
->owner
;
1143 do_select_widget (h
, g_list_find (h
->widgets
, widget
), SELECT_EXACT
);
1146 /* --------------------------------------------------------------------------------------------- */
1148 * Set widget at bottom of widget list.
1152 dlg_set_bottom_widget (void *w
)
1154 Widget
*widget
= WIDGET (w
);
1155 WDialog
*h
= widget
->owner
;
1157 dlg_reorder_widgets (g_list_find (h
->widgets
, widget
), FALSE
);
1160 /* --------------------------------------------------------------------------------------------- */
1161 /** Try to select previous widget in the tab order */
1164 dlg_one_up (WDialog
* h
)
1166 if (h
->widgets
!= NULL
)
1167 do_select_widget (h
, dlg_widget_prev (h
, h
->current
), SELECT_PREV
);
1170 /* --------------------------------------------------------------------------------------------- */
1171 /** Try to select next widget in the tab order */
1174 dlg_one_down (WDialog
* h
)
1176 if (h
->widgets
!= NULL
)
1177 do_select_widget (h
, dlg_widget_next (h
, h
->current
), SELECT_NEXT
);
1180 /* --------------------------------------------------------------------------------------------- */
1183 update_cursor (WDialog
* h
)
1185 GList
*p
= h
->current
;
1187 if (p
!= NULL
&& widget_get_state (WIDGET (h
), WST_ACTIVE
))
1191 w
= WIDGET (p
->data
);
1193 if (!widget_get_state (w
, WST_DISABLED
) && widget_get_options (w
, WOP_WANT_CURSOR
))
1194 send_message (w
, NULL
, MSG_CURSOR
, 0, NULL
);
1198 p
= dlg_widget_next (h
, p
);
1199 if (p
== h
->current
)
1202 w
= WIDGET (p
->data
);
1204 if (!widget_get_state (w
, WST_DISABLED
) && widget_get_options (w
, WOP_WANT_CURSOR
)
1205 && send_message (w
, NULL
, MSG_CURSOR
, 0, NULL
) == MSG_HANDLED
)
1212 /* --------------------------------------------------------------------------------------------- */
1214 * Redraw the widgets in reverse order, leaving the current widget
1219 dlg_redraw (WDialog
* h
)
1221 if (!widget_get_state (WIDGET (h
), WST_ACTIVE
))
1224 if (h
->winch_pending
)
1226 h
->winch_pending
= FALSE
;
1227 send_message (h
, NULL
, MSG_RESIZE
, 0, NULL
);
1230 send_message (h
, NULL
, MSG_DRAW
, 0, NULL
);
1231 dlg_broadcast_msg (h
, MSG_DRAW
);
1235 /* --------------------------------------------------------------------------------------------- */
1238 dlg_stop (WDialog
* h
)
1240 widget_set_state (WIDGET (h
), WST_CLOSED
, TRUE
);
1243 /* --------------------------------------------------------------------------------------------- */
1244 /** Init the process */
1247 dlg_init (WDialog
* h
)
1249 Widget
*wh
= WIDGET (h
);
1251 if (top_dlg
!= NULL
&& widget_get_state (WIDGET (top_dlg
->data
), WST_MODAL
))
1252 widget_set_state (wh
, WST_MODAL
, TRUE
);
1254 /* add dialog to the stack */
1255 top_dlg
= g_list_prepend (top_dlg
, h
);
1257 /* Initialize dialog manager and widgets */
1258 if (widget_get_state (wh
, WST_CONSTRUCT
))
1260 if (!widget_get_state (wh
, WST_MODAL
))
1261 dialog_switch_add (h
);
1263 send_message (h
, NULL
, MSG_INIT
, 0, NULL
);
1264 dlg_broadcast_msg (h
, MSG_INIT
);
1265 dlg_read_history (h
);
1268 widget_set_state (wh
, WST_ACTIVE
, TRUE
);
1270 /* first send MSG_DRAW to dialog itself and all widgets... */
1273 /* ...then send MSG_FOCUS to select the first widget that can take focus */
1274 while (h
->current
!= NULL
&& !dlg_focus (h
))
1275 h
->current
= dlg_widget_next (h
, h
->current
);
1281 /* --------------------------------------------------------------------------------------------- */
1284 dlg_process_event (WDialog
* h
, int key
, Gpm_Event
* event
)
1288 if (tty_got_interrupt ())
1289 if (send_message (h
, NULL
, MSG_ACTION
, CK_Cancel
, NULL
) != MSG_HANDLED
)
1290 dlg_execute_cmd (h
, CK_Cancel
);
1292 else if (key
== EV_MOUSE
)
1293 h
->mouse_status
= dlg_mouse_event (h
, event
);
1295 dlg_key_event (h
, key
);
1298 /* --------------------------------------------------------------------------------------------- */
1299 /** Shutdown the dlg_run */
1302 dlg_run_done (WDialog
* h
)
1304 top_dlg
= g_list_remove (top_dlg
, h
);
1306 if (widget_get_state (WIDGET (h
), WST_CLOSED
))
1308 send_message (h
, h
->current
->data
, MSG_END
, 0, NULL
);
1309 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
1310 dialog_switch_remove (h
);
1314 /* --------------------------------------------------------------------------------------------- */
1316 * Standard run dialog routine
1317 * We have to keep this routine small so that we can duplicate it's
1318 * behavior on complex routines like the file routines, this way,
1319 * they can call the dlg_process_event without rewriting all the code
1323 dlg_run (WDialog
* h
)
1326 frontend_dlg_run (h
);
1328 return h
->ret_value
;
1331 /* --------------------------------------------------------------------------------------------- */
1334 dlg_destroy (WDialog
* h
)
1336 /* if some widgets have history, save all history at one moment here */
1337 dlg_save_history (h
);
1338 dlg_broadcast_msg (h
, MSG_DESTROY
);
1339 g_list_free_full (h
->widgets
, g_free
);
1340 mc_event_group_del (h
->event_group
);
1341 g_free (h
->event_group
);
1348 /* --------------------------------------------------------------------------------------------- */
1351 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1354 dlg_save_history (WDialog
* h
)
1359 if (num_history_items_recorded
== 0) /* this is how to disable */
1362 profile
= mc_config_get_full_path (MC_HISTORY_FILE
);
1363 i
= open (profile
, O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
);
1367 /* Make sure the history is only readable by the user */
1368 if (chmod (profile
, S_IRUSR
| S_IWUSR
) != -1 || errno
== ENOENT
)
1370 ev_history_load_save_t event_data
;
1372 event_data
.cfg
= mc_config_init (profile
, FALSE
);
1373 event_data
.receiver
= NULL
;
1375 /* get all histories in dialog */
1376 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_SAVE
, &event_data
);
1378 mc_config_save_file (event_data
.cfg
, NULL
);
1379 mc_config_deinit (event_data
.cfg
);
1385 /* --------------------------------------------------------------------------------------------- */
1388 dlg_get_title (const WDialog
* h
, size_t len
)
1395 if (h
->get_title
!= NULL
)
1396 t
= h
->get_title (h
, len
);
1403 /* --------------------------------------------------------------------------------------------- */