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 /* Control widget positions in dialog */
91 } widget_shift_scale_t
;
93 /*** file scope variables ************************************************************************/
95 /*** file scope functions ************************************************************************/
98 dlg_widget_next (WDialog
* h
, GList
* l
)
102 next
= g_list_next (l
);
109 /* --------------------------------------------------------------------------------------------- */
112 dlg_widget_prev (WDialog
* h
, GList
* l
)
116 prev
= g_list_previous (l
);
118 prev
= g_list_last (h
->widgets
);
123 /* --------------------------------------------------------------------------------------------- */
125 * broadcast a message to all the widgets in a dialog that have
126 * the options set to flags. If flags is zero, the message is sent
131 dlg_broadcast_msg_to (WDialog
* h
, widget_msg_t msg
, gboolean reverse
, widget_options_t flags
)
135 if (h
->widgets
== NULL
)
138 if (h
->current
== NULL
)
139 h
->current
= h
->widgets
;
142 p
= dlg_widget_prev (h
, h
->current
);
144 p
= dlg_widget_next (h
, h
->current
);
150 Widget
*w
= WIDGET (p
->data
);
153 p
= dlg_widget_prev (h
, p
);
155 p
= dlg_widget_next (h
, p
);
157 if ((flags
== 0) || ((flags
& w
->options
) != 0))
158 send_message (w
, NULL
, msg
, 0, NULL
);
163 /* --------------------------------------------------------------------------------------------- */
166 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
169 dlg_read_history (WDialog
* h
)
172 ev_history_load_save_t event_data
;
174 if (num_history_items_recorded
== 0) /* this is how to disable */
177 profile
= mc_config_get_full_path (MC_HISTORY_FILE
);
178 event_data
.cfg
= mc_config_init (profile
, TRUE
);
179 event_data
.receiver
= NULL
;
181 /* create all histories in dialog */
182 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_LOAD
, &event_data
);
184 mc_config_deinit (event_data
.cfg
);
188 /* --------------------------------------------------------------------------------------------- */
191 dlg_unfocus (WDialog
* h
)
193 /* we can unfocus disabled widget */
194 if (h
->current
!= NULL
)
196 Widget
*wh
= WIDGET (h
);
198 if (widget_get_state (wh
, WST_CONSTRUCT
) || widget_get_state (wh
, WST_ACTIVE
))
200 Widget
*current
= WIDGET (h
->current
->data
);
202 if (send_message (current
, NULL
, MSG_UNFOCUS
, 0, NULL
) == MSG_HANDLED
)
204 send_message (h
, current
, MSG_UNFOCUS
, 0, NULL
);
214 /* --------------------------------------------------------------------------------------------- */
217 dlg_find_widget_callback (const void *a
, const void *b
)
219 const Widget
*w
= CONST_WIDGET (a
);
220 const widget_cb_fn f
= b
;
222 return (w
->callback
== f
) ? 0 : 1;
225 /* --------------------------------------------------------------------------------------------- */
228 * Put widget on top or bottom of Z-order.
231 dlg_reorder_widgets (GList
* l
, gboolean set_top
)
233 WDialog
*h
= WIDGET (l
->data
)->owner
;
235 h
->widgets
= g_list_remove_link (h
->widgets
, l
);
237 h
->widgets
= g_list_concat (h
->widgets
, l
);
239 h
->widgets
= g_list_concat (l
, h
->widgets
);
242 /* --------------------------------------------------------------------------------------------- */
244 * Try to select another widget. If forward is set, follow tab order.
245 * Otherwise go to the previous widget.
249 do_select_widget (WDialog
* h
, GList
* w
, select_dir_t dir
)
251 Widget
*w0
= WIDGET (h
->current
->data
);
253 if (!dlg_unfocus (h
))
266 h
->current
= g_list_find (h
->widgets
, w0
);
269 /* try find another widget that can take focus */
273 h
->current
= dlg_widget_next (h
, h
->current
);
276 h
->current
= dlg_widget_prev (h
, h
->current
);
282 while (h
->current
!= w
);
284 if (widget_get_options (WIDGET (h
->current
->data
), WOP_TOP_SELECT
))
285 dlg_reorder_widgets (h
->current
, TRUE
);
287 if (widget_overlapped (w0
, WIDGET (h
->current
->data
)))
289 send_message (h
->current
->data
, NULL
, MSG_DRAW
, 0, NULL
);
290 send_message (h
->current
->data
, NULL
, MSG_FOCUS
, 0, NULL
);
294 /* --------------------------------------------------------------------------------------------- */
303 /* Use this if the refreshes fail */
306 #endif /* HAVE_SLANG */
309 /* --------------------------------------------------------------------------------------------- */
312 dlg_execute_cmd (WDialog
* h
, long command
)
314 cb_ret_t ret
= MSG_HANDLED
;
318 h
->ret_value
= B_ENTER
;
322 h
->ret_value
= B_CANCEL
;
337 ev_help_t event_data
= { NULL
, h
->help_ctx
};
338 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
343 mc_event_raise (MCEVENT_GROUP_CORE
, "suspend", NULL
);
351 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
352 dialog_switch_list ();
354 ret
= MSG_NOT_HANDLED
;
357 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
358 dialog_switch_next ();
360 ret
= MSG_NOT_HANDLED
;
363 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
364 dialog_switch_prev ();
366 ret
= MSG_NOT_HANDLED
;
370 ret
= MSG_NOT_HANDLED
;
376 /* --------------------------------------------------------------------------------------------- */
379 dlg_handle_key (WDialog
* h
, int d_key
)
383 command
= keybind_lookup_keymap_command (dialog_map
, d_key
);
385 if (command
== CK_IgnoreKey
)
386 return MSG_NOT_HANDLED
;
388 if (send_message (h
, NULL
, MSG_ACTION
, command
, NULL
) == MSG_HANDLED
389 || dlg_execute_cmd (h
, command
) == MSG_HANDLED
)
392 return MSG_NOT_HANDLED
;
395 /* --------------------------------------------------------------------------------------------- */
397 * This is the low-level mouse handler.
398 * It receives a Gpm_Event event and translates it into a higher level protocol.
401 dlg_mouse_translator (Gpm_Event
* event
, Widget
* w
)
405 me
= mouse_translate_event (w
, event
);
407 return mouse_process_event (w
, &me
);
410 /* --------------------------------------------------------------------------------------------- */
413 dlg_mouse_event (WDialog
* h
, Gpm_Event
* event
)
415 Widget
*wh
= WIDGET (h
);
419 /* close the dialog by mouse left click out of dialog area */
420 if (mouse_close_dialog
&& (wh
->pos_flags
& WPOS_FULLSCREEN
) == 0
421 && ((event
->buttons
& GPM_B_LEFT
) != 0) && ((event
->type
& GPM_DOWN
) != 0)
422 && !mouse_global_in_widget (event
, wh
))
424 h
->ret_value
= B_CANCEL
;
429 if (wh
->mouse_callback
!= NULL
)
433 mou
= dlg_mouse_translator (event
, wh
);
434 if (mou
!= MOU_UNHANDLED
)
438 /* send the event to widgets in reverse Z-order */
439 p
= g_list_last (h
->widgets
);
442 Widget
*w
= WIDGET (p
->data
);
444 if (!widget_get_state (w
, WST_DISABLED
) && w
->mouse_callback
!= NULL
)
446 /* put global cursor position to the widget */
449 ret
= dlg_mouse_translator (event
, w
);
450 if (ret
!= MOU_UNHANDLED
)
454 p
= g_list_previous (p
);
458 return MOU_UNHANDLED
;
461 /* --------------------------------------------------------------------------------------------- */
464 dlg_try_hotkey (WDialog
* h
, int d_key
)
471 if (h
->widgets
== NULL
)
472 return MSG_NOT_HANDLED
;
474 if (h
->current
== NULL
)
475 h
->current
= h
->widgets
;
478 * Explanation: we don't send letter hotkeys to other widgets if
479 * the currently selected widget is an input line
482 current
= WIDGET (h
->current
->data
);
484 if (widget_get_state (current
, WST_DISABLED
))
485 return MSG_NOT_HANDLED
;
487 if (widget_get_options (current
, WOP_IS_INPUT
))
489 /* skip ascii control characters, anything else can valid character in
491 if (d_key
>= 32 && d_key
< 256)
492 return MSG_NOT_HANDLED
;
495 /* If it's an alt key, send the message */
496 c
= d_key
& ~ALT (0);
497 if (d_key
& ALT (0) && g_ascii_isalpha (c
))
498 d_key
= g_ascii_tolower (c
);
500 handled
= MSG_NOT_HANDLED
;
501 if (widget_get_options (current
, WOP_WANT_HOTKEY
))
502 handled
= send_message (current
, NULL
, MSG_HOTKEY
, d_key
, NULL
);
504 /* If not used, send hotkey to other widgets */
505 if (handled
== MSG_HANDLED
)
508 hot_cur
= dlg_widget_next (h
, h
->current
);
510 /* send it to all widgets */
511 while (h
->current
!= hot_cur
&& handled
== MSG_NOT_HANDLED
)
513 current
= WIDGET (hot_cur
->data
);
515 if (widget_get_options (current
, WOP_WANT_HOTKEY
)
516 && !widget_get_state (current
, WST_DISABLED
))
517 handled
= send_message (current
, NULL
, MSG_HOTKEY
, d_key
, NULL
);
519 if (handled
== MSG_NOT_HANDLED
)
520 hot_cur
= dlg_widget_next (h
, hot_cur
);
523 if (handled
== MSG_HANDLED
)
524 do_select_widget (h
, hot_cur
, SELECT_EXACT
);
529 /* --------------------------------------------------------------------------------------------- */
532 dlg_key_event (WDialog
* h
, int d_key
)
536 if (h
->widgets
== NULL
)
539 if (h
->current
== NULL
)
540 h
->current
= h
->widgets
;
542 /* TAB used to cycle */
543 if (!widget_get_options (WIDGET (h
), WOP_WANT_TAB
))
550 else if ((d_key
& ~(KEY_M_SHIFT
| KEY_M_CTRL
)) == '\t')
557 /* first can dlg_callback handle the key */
558 handled
= send_message (h
, NULL
, MSG_KEY
, d_key
, NULL
);
560 /* next try the hotkey */
561 if (handled
== MSG_NOT_HANDLED
)
562 handled
= dlg_try_hotkey (h
, d_key
);
564 if (handled
== MSG_HANDLED
)
565 send_message (h
, NULL
, MSG_HOTKEY_HANDLED
, 0, NULL
);
567 /* not used - then try widget_callback */
568 handled
= send_message (h
->current
->data
, NULL
, MSG_KEY
, d_key
, NULL
);
570 /* not used- try to use the unhandled case */
571 if (handled
== MSG_NOT_HANDLED
)
572 handled
= send_message (h
, NULL
, MSG_UNHANDLED_KEY
, d_key
, NULL
);
574 if (handled
== MSG_NOT_HANDLED
)
575 handled
= dlg_handle_key (h
, d_key
);
578 send_message (h
, NULL
, MSG_POST_KEY
, d_key
, NULL
);
581 /* --------------------------------------------------------------------------------------------- */
584 frontend_dlg_run (WDialog
* h
)
586 Widget
*wh
= WIDGET (h
);
591 /* close opened editors, viewers, etc */
592 if (!widget_get_state (wh
, WST_MODAL
) && mc_global
.midnight_shutdown
)
594 send_message (h
, NULL
, MSG_VALIDATE
, 0, NULL
);
598 while (widget_get_state (wh
, WST_ACTIVE
))
602 if (mc_global
.tty
.winch_flag
!= 0)
603 dialog_change_screen_size ();
608 execute_hooks (idle_hook
);
610 while (widget_get_state (wh
, WST_IDLE
) && is_idle ())
611 send_message (wh
, NULL
, MSG_IDLE
, 0, NULL
);
613 /* Allow terminating the dialog from the idle handler */
614 if (!widget_get_state (wh
, WST_ACTIVE
))
620 /* Clear interrupt flag */
621 tty_got_interrupt ();
622 d_key
= tty_get_event (&event
, h
->mouse_status
== MOU_REPEAT
, TRUE
);
624 dlg_process_event (h
, d_key
, &event
);
626 if (widget_get_state (wh
, WST_CLOSED
))
627 send_message (h
, NULL
, MSG_VALIDATE
, 0, NULL
);
631 /* --------------------------------------------------------------------------------------------- */
634 dlg_find_widget_by_id (gconstpointer a
, gconstpointer b
)
636 const Widget
*w
= CONST_WIDGET (a
);
637 unsigned long id
= GPOINTER_TO_UINT (b
);
639 return w
->id
== id
? 0 : 1;
642 /* --------------------------------------------------------------------------------------------- */
644 dlg_widget_set_position (gpointer data
, gpointer user_data
)
646 /* there are, mainly, 2 generally possible situations:
647 * 1. control sticks to one side - it should be moved
648 * 2. control sticks to two sides of one direction - it should be sized
651 Widget
*c
= WIDGET (data
);
652 Widget
*wh
= WIDGET (c
->owner
);
653 const widget_shift_scale_t
*wss
= (const widget_shift_scale_t
*) user_data
;
657 int lines
= c
->lines
;
659 if ((c
->pos_flags
& WPOS_CENTER_HORZ
) != 0)
660 x
= wh
->x
+ (wh
->cols
- c
->cols
) / 2;
661 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0 && (c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
664 cols
+= wss
->scale_x
;
666 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0)
668 else if ((c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
669 x
+= wss
->shift_x
+ wss
->scale_x
;
671 if ((c
->pos_flags
& WPOS_CENTER_VERT
) != 0)
672 y
= wh
->y
+ (wh
->lines
- c
->lines
) / 2;
673 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0 && (c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
676 lines
+= wss
->scale_y
;
678 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0)
680 else if ((c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
681 y
+= wss
->shift_y
+ wss
->scale_y
;
683 widget_set_size (c
, y
, x
, lines
, cols
);
686 /* --------------------------------------------------------------------------------------------- */
689 dlg_adjust_position (const WDialog
* h
, int *y
, int *x
, int *lines
, int *cols
)
691 const Widget
*w
= CONST_WIDGET (h
);
693 if ((w
->pos_flags
& WPOS_FULLSCREEN
) != 0)
702 if ((w
->pos_flags
& WPOS_CENTER_HORZ
) != 0)
703 *x
= (COLS
- *cols
) / 2;
707 if ((w
->pos_flags
& WPOS_CENTER_VERT
) != 0)
708 *y
= (LINES
- *lines
) / 2;
712 if ((w
->pos_flags
& WPOS_TRYUP
) != 0)
722 /* --------------------------------------------------------------------------------------------- */
723 /*** public functions ****************************************************************************/
724 /* --------------------------------------------------------------------------------------------- */
726 /** Clean the dialog area, draw the frame and the title */
728 dlg_default_repaint (WDialog
* h
)
730 Widget
*wh
= WIDGET (h
);
734 if (!widget_get_state (wh
, WST_ACTIVE
))
737 space
= h
->compact
? 0 : 1;
739 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
741 tty_draw_box (wh
->y
+ space
, wh
->x
+ space
, wh
->lines
- 2 * space
, wh
->cols
- 2 * space
, FALSE
);
743 if (h
->title
!= NULL
)
745 tty_setcolor (h
->color
[DLG_COLOR_TITLE
]);
746 widget_move (h
, space
, (wh
->cols
- str_term_width1 (h
->title
)) / 2);
747 tty_print_string (h
->title
);
751 /* --------------------------------------------------------------------------------------------- */
752 /** this function allows to set dialog position */
755 dlg_set_position (WDialog
* h
, int y
, int x
, int lines
, int cols
)
757 Widget
*wh
= WIDGET (h
);
758 widget_shift_scale_t wss
;
760 /* save old positions, will be used to reposition childs */
763 /* save old positions, will be used to reposition childs */
774 /* dialog is empty */
775 if (h
->widgets
== NULL
)
778 if (h
->current
== NULL
)
779 h
->current
= h
->widgets
;
781 /* values by which controls should be moved */
782 wss
.shift_x
= wh
->x
- ox
;
783 wss
.scale_x
= wh
->cols
- oc
;
784 wss
.shift_y
= wh
->y
- oy
;
785 wss
.scale_y
= wh
->lines
- ol
;
787 if (wss
.shift_x
!= 0 || wss
.shift_y
!= 0 || wss
.scale_x
!= 0 || wss
.scale_y
!= 0)
788 g_list_foreach (h
->widgets
, dlg_widget_set_position
, &wss
);
791 /* --------------------------------------------------------------------------------------------- */
792 /** Set dialog size and position */
795 dlg_set_size (WDialog
* h
, int lines
, int cols
)
799 dlg_adjust_position (h
, &y
, &x
, &lines
, &cols
);
800 dlg_set_position (h
, y
, x
, lines
, cols
);
803 /* --------------------------------------------------------------------------------------------- */
804 /** Default dialog callback */
807 dlg_default_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
809 WDialog
*h
= DIALOG (w
);
818 if (h
->color
!= NULL
)
820 dlg_default_repaint (h
);
823 return MSG_NOT_HANDLED
;
826 /* we don't want endless loop */
827 widget_idle (w
, FALSE
);
831 /* this is default resizing mechanism */
832 /* the main idea of this code is to resize dialog
833 according to flags (if any of flags require automatic
834 resizing, like WPOS_CENTER, end after that reposition
835 controls in dialog according to flags of widget) */
836 dlg_set_size (h
, w
->lines
, w
->cols
);
843 return MSG_NOT_HANDLED
;
846 /* --------------------------------------------------------------------------------------------- */
849 dlg_create (gboolean modal
, int y1
, int x1
, int lines
, int cols
, widget_pos_flags_t pos_flags
,
850 gboolean compact
, const int *colors
, widget_cb_fn callback
,
851 widget_mouse_cb_fn mouse_callback
, const char *help_ctx
, const char *title
)
856 new_d
= g_new0 (WDialog
, 1);
858 w
->pos_flags
= pos_flags
; /* required for dlg_adjust_position() */
859 dlg_adjust_position (new_d
, &y1
, &x1
, &lines
, &cols
);
860 widget_init (w
, y1
, x1
, lines
, cols
, (callback
!= NULL
) ? callback
: dlg_default_callback
,
862 w
->pos_flags
= pos_flags
; /* restore after widget_init() */
863 w
->options
|= WOP_TOP_SELECT
;
865 w
->state
|= WST_CONSTRUCT
;
867 w
->state
|= WST_MODAL
;
869 new_d
->color
= colors
;
870 new_d
->help_ctx
= help_ctx
;
871 new_d
->compact
= compact
;
874 new_d
->mouse_status
= MOU_UNHANDLED
;
876 /* Strip existing spaces, add one space before and after the title */
877 if (title
!= NULL
&& *title
!= '\0')
881 t
= g_strstrip (g_strdup (title
));
883 new_d
->title
= g_strdup_printf (" %s ", t
);
887 /* unique name of event group for this dialog */
888 new_d
->event_group
= g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG
, (void *) new_d
);
893 /* --------------------------------------------------------------------------------------------- */
896 dlg_set_default_colors (void)
898 dialog_colors
[DLG_COLOR_NORMAL
] = COLOR_NORMAL
;
899 dialog_colors
[DLG_COLOR_FOCUS
] = COLOR_FOCUS
;
900 dialog_colors
[DLG_COLOR_HOT_NORMAL
] = COLOR_HOT_NORMAL
;
901 dialog_colors
[DLG_COLOR_HOT_FOCUS
] = COLOR_HOT_FOCUS
;
902 dialog_colors
[DLG_COLOR_TITLE
] = COLOR_TITLE
;
904 alarm_colors
[DLG_COLOR_NORMAL
] = ERROR_COLOR
;
905 alarm_colors
[DLG_COLOR_FOCUS
] = ERROR_FOCUS
;
906 alarm_colors
[DLG_COLOR_HOT_NORMAL
] = ERROR_HOT_NORMAL
;
907 alarm_colors
[DLG_COLOR_HOT_FOCUS
] = ERROR_HOT_FOCUS
;
908 alarm_colors
[DLG_COLOR_TITLE
] = ERROR_TITLE
;
910 listbox_colors
[DLG_COLOR_NORMAL
] = PMENU_ENTRY_COLOR
;
911 listbox_colors
[DLG_COLOR_FOCUS
] = PMENU_SELECTED_COLOR
;
912 listbox_colors
[DLG_COLOR_HOT_NORMAL
] = PMENU_ENTRY_COLOR
;
913 listbox_colors
[DLG_COLOR_HOT_FOCUS
] = PMENU_SELECTED_COLOR
;
914 listbox_colors
[DLG_COLOR_TITLE
] = PMENU_TITLE_COLOR
;
917 /* --------------------------------------------------------------------------------------------- */
920 dlg_erase (WDialog
* h
)
922 Widget
*wh
= WIDGET (h
);
924 if (wh
!= NULL
&& widget_get_state (wh
, WST_ACTIVE
))
925 tty_fill_region (wh
->y
, wh
->x
, wh
->lines
, wh
->cols
, ' ');
928 /* --------------------------------------------------------------------------------------------- */
930 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
934 add_widget_autopos (WDialog
* h
, void *w
, widget_pos_flags_t pos_flags
, const void *before
)
936 Widget
*wh
= WIDGET (h
);
939 /* Don't accept 0 widgets */
945 if ((pos_flags
& WPOS_CENTER_HORZ
) != 0)
946 widget
->x
= (wh
->cols
- widget
->cols
) / 2;
949 if ((pos_flags
& WPOS_CENTER_VERT
) != 0)
950 widget
->y
= (wh
->lines
- widget
->lines
) / 2;
954 widget
->pos_flags
= pos_flags
;
955 widget
->id
= h
->widget_id
++;
957 if (h
->widgets
== NULL
|| before
== NULL
)
959 h
->widgets
= g_list_append (h
->widgets
, widget
);
960 h
->current
= g_list_last (h
->widgets
);
966 b
= g_list_find (h
->widgets
, before
);
968 /* don't accept widget not from dialog. This shouldn't happen */
973 h
->widgets
= g_list_insert_before (h
->widgets
, b
, widget
);
975 h
->current
= g_list_previous (b
);
977 h
->current
= g_list_last (h
->widgets
);
980 /* widget has been added in runtime */
981 if (widget_get_state (wh
, WST_ACTIVE
))
983 send_message (widget
, NULL
, MSG_INIT
, 0, NULL
);
984 send_message (widget
, NULL
, MSG_DRAW
, 0, NULL
);
985 send_message (widget
, NULL
, MSG_FOCUS
, 0, NULL
);
991 /* --------------------------------------------------------------------------------------------- */
992 /** wrapper to simply add lefttop positioned controls */
995 add_widget (WDialog
* h
, void *w
)
997 return add_widget_autopos (h
, w
, WPOS_KEEP_DEFAULT
,
998 h
->current
!= NULL
? h
->current
->data
: NULL
);
1001 /* --------------------------------------------------------------------------------------------- */
1004 add_widget_before (WDialog
* h
, void *w
, void *before
)
1006 return add_widget_autopos (h
, w
, WPOS_KEEP_DEFAULT
, before
);
1009 /* --------------------------------------------------------------------------------------------- */
1011 /** delete widget from dialog */
1013 del_widget (void *w
)
1018 /* Don't accept NULL widget. This shouldn't happen */
1022 h
= WIDGET (w
)->owner
;
1024 d
= g_list_find (h
->widgets
, w
);
1025 if (d
== h
->current
)
1026 h
->current
= dlg_widget_next (h
, d
);
1028 h
->widgets
= g_list_remove_link (h
->widgets
, d
);
1029 send_message (d
->data
, NULL
, MSG_DESTROY
, 0, NULL
);
1033 /* widget has been deleted in runtime */
1034 if (widget_get_state (WIDGET (h
), WST_ACTIVE
))
1041 /* --------------------------------------------------------------------------------------------- */
1050 if ((d
!= NULL
) && (d
->data
!= NULL
))
1051 dlg_redraw (DIALOG (d
->data
));
1055 /* Search first fullscreen dialog */
1056 for (; d
!= NULL
; d
= g_list_next (d
))
1057 if (d
->data
!= NULL
&& (WIDGET (d
->data
)->pos_flags
& WPOS_FULLSCREEN
) != 0)
1059 /* back to top dialog */
1060 for (; d
!= NULL
; d
= g_list_previous (d
))
1061 if (d
->data
!= NULL
)
1062 dlg_redraw (DIALOG (d
->data
));
1066 /* --------------------------------------------------------------------------------------------- */
1067 /** broadcast a message to all the widgets in a dialog */
1070 dlg_broadcast_msg (WDialog
* h
, widget_msg_t msg
)
1072 dlg_broadcast_msg_to (h
, msg
, FALSE
, 0);
1075 /* --------------------------------------------------------------------------------------------- */
1078 dlg_focus (WDialog
* h
)
1080 /* cannot focus disabled widget */
1081 if (h
->current
!= NULL
)
1083 Widget
*wh
= WIDGET (h
);
1085 if (widget_get_state (wh
, WST_CONSTRUCT
) || widget_get_state (wh
, WST_ACTIVE
))
1087 Widget
*current
= WIDGET (h
->current
->data
);
1089 if (!widget_get_state (current
, WST_DISABLED
)
1090 && (send_message (current
, NULL
, MSG_FOCUS
, 0, NULL
) == MSG_HANDLED
))
1092 send_message (h
, current
, MSG_FOCUS
, 0, NULL
);
1101 /* --------------------------------------------------------------------------------------------- */
1102 /** Find the widget with the given callback in the dialog h */
1105 find_widget_type (const WDialog
* h
, widget_cb_fn callback
)
1109 w
= g_list_find_custom (h
->widgets
, (gconstpointer
) callback
, dlg_find_widget_callback
);
1111 return (w
== NULL
) ? NULL
: WIDGET (w
->data
);
1114 /* --------------------------------------------------------------------------------------------- */
1115 /** Find the widget with the given id */
1118 dlg_find_by_id (const WDialog
* h
, unsigned long id
)
1122 w
= g_list_find_custom (h
->widgets
, GUINT_TO_POINTER (id
), dlg_find_widget_by_id
);
1123 return w
!= NULL
? WIDGET (w
->data
) : NULL
;
1126 /* --------------------------------------------------------------------------------------------- */
1127 /** Find the widget with the given id in the dialog h and select it */
1130 dlg_select_by_id (const WDialog
* h
, unsigned long id
)
1134 w
= dlg_find_by_id (h
, id
);
1136 dlg_select_widget (w
);
1139 /* --------------------------------------------------------------------------------------------- */
1141 * Try to select widget in the dialog.
1145 dlg_select_widget (void *w
)
1147 Widget
*widget
= WIDGET (w
);
1148 WDialog
*h
= widget
->owner
;
1150 do_select_widget (h
, g_list_find (h
->widgets
, widget
), SELECT_EXACT
);
1153 /* --------------------------------------------------------------------------------------------- */
1155 * Set widget at bottom of widget list.
1159 dlg_set_bottom_widget (void *w
)
1161 Widget
*widget
= WIDGET (w
);
1162 WDialog
*h
= widget
->owner
;
1164 dlg_reorder_widgets (g_list_find (h
->widgets
, widget
), FALSE
);
1167 /* --------------------------------------------------------------------------------------------- */
1168 /** Try to select previous widget in the tab order */
1171 dlg_one_up (WDialog
* h
)
1173 if (h
->widgets
!= NULL
)
1174 do_select_widget (h
, dlg_widget_prev (h
, h
->current
), SELECT_PREV
);
1177 /* --------------------------------------------------------------------------------------------- */
1178 /** Try to select next widget in the tab order */
1181 dlg_one_down (WDialog
* h
)
1183 if (h
->widgets
!= NULL
)
1184 do_select_widget (h
, dlg_widget_next (h
, h
->current
), SELECT_NEXT
);
1187 /* --------------------------------------------------------------------------------------------- */
1190 update_cursor (WDialog
* h
)
1192 GList
*p
= h
->current
;
1194 if (p
!= NULL
&& widget_get_state (WIDGET (h
), WST_ACTIVE
))
1198 w
= WIDGET (p
->data
);
1200 if (!widget_get_state (w
, WST_DISABLED
) && widget_get_options (w
, WOP_WANT_CURSOR
))
1201 send_message (w
, NULL
, MSG_CURSOR
, 0, NULL
);
1205 p
= dlg_widget_next (h
, p
);
1206 if (p
== h
->current
)
1209 w
= WIDGET (p
->data
);
1211 if (!widget_get_state (w
, WST_DISABLED
) && widget_get_options (w
, WOP_WANT_CURSOR
)
1212 && send_message (w
, NULL
, MSG_CURSOR
, 0, NULL
) == MSG_HANDLED
)
1219 /* --------------------------------------------------------------------------------------------- */
1221 * Redraw the widgets in reverse order, leaving the current widget
1226 dlg_redraw (WDialog
* h
)
1228 if (!widget_get_state (WIDGET (h
), WST_ACTIVE
))
1231 if (h
->winch_pending
)
1233 h
->winch_pending
= FALSE
;
1234 send_message (h
, NULL
, MSG_RESIZE
, 0, NULL
);
1237 send_message (h
, NULL
, MSG_DRAW
, 0, NULL
);
1238 dlg_broadcast_msg (h
, MSG_DRAW
);
1242 /* --------------------------------------------------------------------------------------------- */
1245 dlg_stop (WDialog
* h
)
1247 widget_set_state (WIDGET (h
), WST_CLOSED
, TRUE
);
1250 /* --------------------------------------------------------------------------------------------- */
1251 /** Init the process */
1254 dlg_init (WDialog
* h
)
1256 Widget
*wh
= WIDGET (h
);
1258 if (top_dlg
!= NULL
&& widget_get_state (WIDGET (top_dlg
->data
), WST_MODAL
))
1259 widget_set_state (wh
, WST_MODAL
, TRUE
);
1261 /* add dialog to the stack */
1262 top_dlg
= g_list_prepend (top_dlg
, h
);
1264 /* Initialize dialog manager and widgets */
1265 if (widget_get_state (wh
, WST_CONSTRUCT
))
1267 if (!widget_get_state (wh
, WST_MODAL
))
1268 dialog_switch_add (h
);
1270 send_message (h
, NULL
, MSG_INIT
, 0, NULL
);
1271 dlg_broadcast_msg (h
, MSG_INIT
);
1272 dlg_read_history (h
);
1275 widget_set_state (wh
, WST_ACTIVE
, TRUE
);
1277 /* first send MSG_DRAW to dialog itself and all widgets... */
1280 /* ...then send MSG_FOCUS to select the first widget that can take focus */
1281 while (h
->current
!= NULL
&& !dlg_focus (h
))
1282 h
->current
= dlg_widget_next (h
, h
->current
);
1288 /* --------------------------------------------------------------------------------------------- */
1291 dlg_process_event (WDialog
* h
, int key
, Gpm_Event
* event
)
1295 if (tty_got_interrupt ())
1296 if (send_message (h
, NULL
, MSG_ACTION
, CK_Cancel
, NULL
) != MSG_HANDLED
)
1297 dlg_execute_cmd (h
, CK_Cancel
);
1299 else if (key
== EV_MOUSE
)
1300 h
->mouse_status
= dlg_mouse_event (h
, event
);
1302 dlg_key_event (h
, key
);
1305 /* --------------------------------------------------------------------------------------------- */
1306 /** Shutdown the dlg_run */
1309 dlg_run_done (WDialog
* h
)
1311 top_dlg
= g_list_remove (top_dlg
, h
);
1313 if (widget_get_state (WIDGET (h
), WST_CLOSED
))
1315 send_message (h
, h
->current
->data
, MSG_END
, 0, NULL
);
1316 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
1317 dialog_switch_remove (h
);
1321 /* --------------------------------------------------------------------------------------------- */
1323 * Standard run dialog routine
1324 * We have to keep this routine small so that we can duplicate it's
1325 * behavior on complex routines like the file routines, this way,
1326 * they can call the dlg_process_event without rewriting all the code
1330 dlg_run (WDialog
* h
)
1333 frontend_dlg_run (h
);
1335 return h
->ret_value
;
1338 /* --------------------------------------------------------------------------------------------- */
1341 dlg_destroy (WDialog
* h
)
1343 /* if some widgets have history, save all history at one moment here */
1344 dlg_save_history (h
);
1345 dlg_broadcast_msg (h
, MSG_DESTROY
);
1346 g_list_free_full (h
->widgets
, g_free
);
1347 mc_event_group_del (h
->event_group
);
1348 g_free (h
->event_group
);
1355 /* --------------------------------------------------------------------------------------------- */
1358 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1361 dlg_save_history (WDialog
* h
)
1366 if (num_history_items_recorded
== 0) /* this is how to disable */
1369 profile
= mc_config_get_full_path (MC_HISTORY_FILE
);
1370 i
= open (profile
, O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
);
1374 /* Make sure the history is only readable by the user */
1375 if (chmod (profile
, S_IRUSR
| S_IWUSR
) != -1 || errno
== ENOENT
)
1377 ev_history_load_save_t event_data
;
1379 event_data
.cfg
= mc_config_init (profile
, FALSE
);
1380 event_data
.receiver
= NULL
;
1382 /* get all histories in dialog */
1383 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_SAVE
, &event_data
);
1385 mc_config_save_file (event_data
.cfg
, NULL
);
1386 mc_config_deinit (event_data
.cfg
);
1392 /* --------------------------------------------------------------------------------------------- */
1395 dlg_get_title (const WDialog
* h
, size_t len
)
1402 if (h
->get_title
!= NULL
)
1403 t
= h
->get_title (h
, len
);
1410 /* --------------------------------------------------------------------------------------------- */