2 Dialog box features module for the Midnight Commander
4 Copyright (C) 1994-2019
5 Free Software Foundation, Inc.
7 This file is part of the Midnight Commander.
9 The Midnight Commander is free software: you can redistribute it
10 and/or modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation, either version 3 of the License,
12 or (at your option) any later version.
14 The Midnight Commander is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
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() */
45 #include "lib/util.h" /* MC_PTR_FREE */
47 #include "lib/widget.h"
48 #include "lib/widget/mouse.h"
50 /*** global variables ****************************************************************************/
52 /* Color styles for normal and error dialogs */
53 dlg_colors_t dialog_colors
;
54 dlg_colors_t alarm_colors
;
55 dlg_colors_t listbox_colors
;
57 /* Primitive way to check if the the current dialog is our dialog */
58 /* This is needed by async routines like load_prompt */
59 GList
*top_dlg
= NULL
;
61 /* A hook list for idle events */
62 hook_t
*idle_hook
= NULL
;
64 /* If set then dialogs just clean the screen when refreshing, else */
65 /* they do a complete refresh, refreshing all the parts of the program */
66 gboolean fast_refresh
= FALSE
;
68 /* left click outside of dialog closes it */
69 gboolean mouse_close_dialog
= FALSE
;
71 const global_keymap_t
*dialog_map
= NULL
;
73 /*** file scope macro definitions ****************************************************************/
75 /*** file scope type declarations ****************************************************************/
77 /* Control widget positions in dialog */
84 } widget_shift_scale_t
;
86 /*** file scope variables ************************************************************************/
88 /* --------------------------------------------------------------------------------------------- */
89 /*** file scope functions ************************************************************************/
90 /* --------------------------------------------------------------------------------------------- */
93 dlg_get_next_or_prev_of (const GList
* list
, gboolean next
)
99 const WDialog
*owner
= CONST_WIDGET (list
->data
)->owner
;
105 l
= g_list_next (list
);
111 l
= g_list_previous (list
);
113 l
= g_list_last (owner
->widgets
);
121 /* --------------------------------------------------------------------------------------------- */
124 dlg_select_next_or_prev (WDialog
* h
, gboolean next
)
126 if (h
->widgets
!= NULL
&& h
->current
!= NULL
)
128 GList
*l
= h
->current
;
133 l
= dlg_get_next_or_prev_of (l
, next
);
134 w
= WIDGET (l
->data
);
136 while ((widget_get_state (w
, WST_DISABLED
) || !widget_get_options (w
, WOP_SELECTABLE
))
139 widget_select (l
->data
);
143 /* --------------------------------------------------------------------------------------------- */
145 * broadcast a message to all the widgets in a dialog that have
146 * the options set to flags. If flags is zero, the message is sent
151 dlg_broadcast_msg_to (WDialog
* h
, widget_msg_t msg
, gboolean reverse
, widget_options_t flags
)
155 if (h
->widgets
== NULL
)
158 if (h
->current
== NULL
)
159 h
->current
= h
->widgets
;
161 p
= dlg_get_next_or_prev_of (h
->current
, !reverse
);
166 Widget
*w
= WIDGET (p
->data
);
168 p
= dlg_get_next_or_prev_of (p
, !reverse
);
170 if ((flags
== 0) || ((flags
& w
->options
) != 0))
171 send_message (w
, NULL
, msg
, 0, NULL
);
176 /* --------------------------------------------------------------------------------------------- */
179 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
182 dlg_read_history (WDialog
* h
)
185 ev_history_load_save_t event_data
;
187 if (num_history_items_recorded
== 0) /* this is how to disable */
190 profile
= mc_config_get_full_path (MC_HISTORY_FILE
);
191 event_data
.cfg
= mc_config_init (profile
, TRUE
);
192 event_data
.receiver
= NULL
;
194 /* create all histories in dialog */
195 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_LOAD
, &event_data
);
197 mc_config_deinit (event_data
.cfg
);
201 /* --------------------------------------------------------------------------------------------- */
204 dlg_find_widget_callback (const void *a
, const void *b
)
206 const Widget
*w
= CONST_WIDGET (a
);
207 const widget_cb_fn f
= b
;
209 return (w
->callback
== f
) ? 0 : 1;
212 /* --------------------------------------------------------------------------------------------- */
221 /* Use this if the refreshes fail */
224 #endif /* HAVE_SLANG */
227 /* --------------------------------------------------------------------------------------------- */
230 dlg_execute_cmd (WDialog
* h
, long command
)
232 cb_ret_t ret
= MSG_HANDLED
;
236 h
->ret_value
= B_ENTER
;
240 h
->ret_value
= B_CANCEL
;
246 dlg_select_prev_widget (h
);
250 dlg_select_next_widget (h
);
255 ev_help_t event_data
= { NULL
, h
->help_ctx
};
256 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
261 mc_event_raise (MCEVENT_GROUP_CORE
, "suspend", NULL
);
269 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
270 dialog_switch_list ();
272 ret
= MSG_NOT_HANDLED
;
275 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
276 dialog_switch_next ();
278 ret
= MSG_NOT_HANDLED
;
281 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
282 dialog_switch_prev ();
284 ret
= MSG_NOT_HANDLED
;
288 ret
= MSG_NOT_HANDLED
;
294 /* --------------------------------------------------------------------------------------------- */
297 dlg_handle_key (WDialog
* h
, int d_key
)
301 command
= keybind_lookup_keymap_command (dialog_map
, d_key
);
303 if (command
== CK_IgnoreKey
)
304 return MSG_NOT_HANDLED
;
306 if (send_message (h
, NULL
, MSG_ACTION
, command
, NULL
) == MSG_HANDLED
307 || dlg_execute_cmd (h
, command
) == MSG_HANDLED
)
310 return MSG_NOT_HANDLED
;
313 /* --------------------------------------------------------------------------------------------- */
315 * This is the low-level mouse handler.
316 * It receives a Gpm_Event event and translates it into a higher level protocol.
319 dlg_mouse_translator (Gpm_Event
* event
, Widget
* w
)
323 me
= mouse_translate_event (w
, event
);
325 return mouse_process_event (w
, &me
);
328 /* --------------------------------------------------------------------------------------------- */
331 dlg_mouse_event (WDialog
* h
, Gpm_Event
* event
)
333 Widget
*wh
= WIDGET (h
);
337 /* close the dialog by mouse left click out of dialog area */
338 if (mouse_close_dialog
&& (wh
->pos_flags
& WPOS_FULLSCREEN
) == 0
339 && ((event
->buttons
& GPM_B_LEFT
) != 0) && ((event
->type
& GPM_DOWN
) != 0)
340 && !mouse_global_in_widget (event
, wh
))
342 h
->ret_value
= B_CANCEL
;
347 if (wh
->mouse_callback
!= NULL
)
351 mou
= dlg_mouse_translator (event
, wh
);
352 if (mou
!= MOU_UNHANDLED
)
356 if (h
->widgets
== NULL
)
357 return MOU_UNHANDLED
;
359 /* send the event to widgets in reverse Z-order */
360 p
= g_list_last (h
->widgets
);
363 Widget
*w
= WIDGET (p
->data
);
365 if (!widget_get_state (w
, WST_DISABLED
) && w
->mouse_callback
!= NULL
)
367 /* put global cursor position to the widget */
370 ret
= dlg_mouse_translator (event
, w
);
371 if (ret
!= MOU_UNHANDLED
)
375 p
= g_list_previous (p
);
379 return MOU_UNHANDLED
;
382 /* --------------------------------------------------------------------------------------------- */
385 dlg_try_hotkey (WDialog
* h
, int d_key
)
392 if (h
->widgets
== NULL
)
393 return MSG_NOT_HANDLED
;
395 if (h
->current
== NULL
)
396 h
->current
= h
->widgets
;
399 * Explanation: we don't send letter hotkeys to other widgets if
400 * the currently selected widget is an input line
403 current
= WIDGET (h
->current
->data
);
405 if (widget_get_state (current
, WST_DISABLED
))
406 return MSG_NOT_HANDLED
;
408 if (widget_get_options (current
, WOP_IS_INPUT
))
410 /* skip ascii control characters, anything else can valid character in
412 if (d_key
>= 32 && d_key
< 256)
413 return MSG_NOT_HANDLED
;
416 /* If it's an alt key, send the message */
417 c
= d_key
& ~ALT (0);
418 if (d_key
& ALT (0) && g_ascii_isalpha (c
))
419 d_key
= g_ascii_tolower (c
);
421 handled
= MSG_NOT_HANDLED
;
422 if (widget_get_options (current
, WOP_WANT_HOTKEY
))
423 handled
= send_message (current
, NULL
, MSG_HOTKEY
, d_key
, NULL
);
425 /* If not used, send hotkey to other widgets */
426 if (handled
== MSG_HANDLED
)
429 hot_cur
= dlg_get_widget_next_of (h
->current
);
431 /* send it to all widgets */
432 while (h
->current
!= hot_cur
&& handled
== MSG_NOT_HANDLED
)
434 current
= WIDGET (hot_cur
->data
);
436 if (widget_get_options (current
, WOP_WANT_HOTKEY
)
437 && !widget_get_state (current
, WST_DISABLED
))
438 handled
= send_message (current
, NULL
, MSG_HOTKEY
, d_key
, NULL
);
440 if (handled
== MSG_NOT_HANDLED
)
441 hot_cur
= dlg_get_widget_next_of (hot_cur
);
444 if (handled
== MSG_HANDLED
)
445 widget_select (WIDGET (hot_cur
->data
));
450 /* --------------------------------------------------------------------------------------------- */
453 dlg_key_event (WDialog
* h
, int d_key
)
457 if (h
->widgets
== NULL
)
460 if (h
->current
== NULL
)
461 h
->current
= h
->widgets
;
463 /* TAB used to cycle */
464 if (!widget_get_options (WIDGET (h
), WOP_WANT_TAB
))
468 dlg_select_next_widget (h
);
471 else if ((d_key
& ~(KEY_M_SHIFT
| KEY_M_CTRL
)) == '\t')
473 dlg_select_prev_widget (h
);
478 /* first can dlg_callback handle the key */
479 handled
= send_message (h
, NULL
, MSG_KEY
, d_key
, NULL
);
481 /* next try the hotkey */
482 if (handled
== MSG_NOT_HANDLED
)
483 handled
= dlg_try_hotkey (h
, d_key
);
485 if (handled
== MSG_HANDLED
)
486 send_message (h
, NULL
, MSG_HOTKEY_HANDLED
, 0, NULL
);
488 /* not used - then try widget_callback */
489 handled
= send_message (h
->current
->data
, NULL
, MSG_KEY
, d_key
, NULL
);
491 /* not used- try to use the unhandled case */
492 if (handled
== MSG_NOT_HANDLED
)
493 handled
= send_message (h
, NULL
, MSG_UNHANDLED_KEY
, d_key
, NULL
);
495 if (handled
== MSG_NOT_HANDLED
)
496 handled
= dlg_handle_key (h
, d_key
);
499 send_message (h
, NULL
, MSG_POST_KEY
, d_key
, NULL
);
502 /* --------------------------------------------------------------------------------------------- */
505 frontend_dlg_run (WDialog
* h
)
507 Widget
*wh
= WIDGET (h
);
512 /* close opened editors, viewers, etc */
513 if (!widget_get_state (wh
, WST_MODAL
) && mc_global
.midnight_shutdown
)
515 send_message (h
, NULL
, MSG_VALIDATE
, 0, NULL
);
519 while (widget_get_state (wh
, WST_ACTIVE
))
523 if (mc_global
.tty
.winch_flag
!= 0)
524 dialog_change_screen_size ();
529 execute_hooks (idle_hook
);
531 while (widget_get_state (wh
, WST_IDLE
) && is_idle ())
532 send_message (wh
, NULL
, MSG_IDLE
, 0, NULL
);
534 /* Allow terminating the dialog from the idle handler */
535 if (!widget_get_state (wh
, WST_ACTIVE
))
541 /* Clear interrupt flag */
542 tty_got_interrupt ();
543 d_key
= tty_get_event (&event
, h
->mouse_status
== MOU_REPEAT
, TRUE
);
545 dlg_process_event (h
, d_key
, &event
);
547 if (widget_get_state (wh
, WST_CLOSED
))
548 send_message (h
, NULL
, MSG_VALIDATE
, 0, NULL
);
552 /* --------------------------------------------------------------------------------------------- */
555 dlg_find_widget_by_id (gconstpointer a
, gconstpointer b
)
557 const Widget
*w
= CONST_WIDGET (a
);
558 unsigned long id
= GPOINTER_TO_UINT (b
);
560 return w
->id
== id
? 0 : 1;
563 /* --------------------------------------------------------------------------------------------- */
565 dlg_widget_set_position (gpointer data
, gpointer user_data
)
567 /* there are, mainly, 2 generally possible situations:
568 * 1. control sticks to one side - it should be moved
569 * 2. control sticks to two sides of one direction - it should be sized
572 Widget
*c
= WIDGET (data
);
573 Widget
*wh
= WIDGET (c
->owner
);
574 const widget_shift_scale_t
*wss
= (const widget_shift_scale_t
*) user_data
;
578 int lines
= c
->lines
;
580 if ((c
->pos_flags
& WPOS_CENTER_HORZ
) != 0)
581 x
= wh
->x
+ (wh
->cols
- c
->cols
) / 2;
582 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0 && (c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
585 cols
+= wss
->scale_x
;
587 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0)
589 else if ((c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
590 x
+= wss
->shift_x
+ wss
->scale_x
;
592 if ((c
->pos_flags
& WPOS_CENTER_VERT
) != 0)
593 y
= wh
->y
+ (wh
->lines
- c
->lines
) / 2;
594 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0 && (c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
597 lines
+= wss
->scale_y
;
599 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0)
601 else if ((c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
602 y
+= wss
->shift_y
+ wss
->scale_y
;
604 widget_set_size (c
, y
, x
, lines
, cols
);
607 /* --------------------------------------------------------------------------------------------- */
610 dlg_adjust_position (widget_pos_flags_t pos_flags
, int *y
, int *x
, int *lines
, int *cols
)
612 if ((pos_flags
& WPOS_FULLSCREEN
) != 0)
621 if ((pos_flags
& WPOS_CENTER_HORZ
) != 0)
622 *x
= (COLS
- *cols
) / 2;
624 if ((pos_flags
& WPOS_CENTER_VERT
) != 0)
625 *y
= (LINES
- *lines
) / 2;
627 if ((pos_flags
& WPOS_TRYUP
) != 0)
637 /* --------------------------------------------------------------------------------------------- */
638 /*** public functions ****************************************************************************/
639 /* --------------------------------------------------------------------------------------------- */
641 /** Clean the dialog area, draw the frame and the title */
643 dlg_default_repaint (WDialog
* h
)
645 Widget
*wh
= WIDGET (h
);
649 if (!widget_get_state (wh
, WST_ACTIVE
))
652 space
= h
->compact
? 0 : 1;
654 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
656 tty_draw_box (wh
->y
+ space
, wh
->x
+ space
, wh
->lines
- 2 * space
, wh
->cols
- 2 * space
, FALSE
);
658 if (h
->title
!= NULL
)
660 /* TODO: truncate long title */
661 tty_setcolor (h
->color
[DLG_COLOR_TITLE
]);
662 widget_move (h
, space
, (wh
->cols
- str_term_width1 (h
->title
)) / 2);
663 tty_print_string (h
->title
);
667 /* --------------------------------------------------------------------------------------------- */
668 /** this function allows to set dialog position */
671 dlg_set_position (WDialog
* h
, int y
, int x
, int lines
, int cols
)
673 Widget
*wh
= WIDGET (h
);
674 widget_shift_scale_t wss
;
676 /* save old positions, will be used to reposition childs */
679 /* save old positions, will be used to reposition childs */
690 /* dialog is empty */
691 if (h
->widgets
== NULL
)
694 if (h
->current
== NULL
)
695 h
->current
= h
->widgets
;
697 /* values by which controls should be moved */
698 wss
.shift_x
= wh
->x
- ox
;
699 wss
.scale_x
= wh
->cols
- oc
;
700 wss
.shift_y
= wh
->y
- oy
;
701 wss
.scale_y
= wh
->lines
- ol
;
703 if (wss
.shift_x
!= 0 || wss
.shift_y
!= 0 || wss
.scale_x
!= 0 || wss
.scale_y
!= 0)
704 g_list_foreach (h
->widgets
, dlg_widget_set_position
, &wss
);
707 /* --------------------------------------------------------------------------------------------- */
708 /** Set dialog size and position */
711 dlg_set_size (WDialog
* h
, int lines
, int cols
)
715 dlg_adjust_position (WIDGET (h
)->pos_flags
, &y
, &x
, &lines
, &cols
);
716 dlg_set_position (h
, y
, x
, lines
, cols
);
719 /* --------------------------------------------------------------------------------------------- */
720 /** Default dialog callback */
723 dlg_default_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
725 WDialog
*h
= DIALOG (w
);
734 if (h
->color
!= NULL
)
736 dlg_default_repaint (h
);
739 return MSG_NOT_HANDLED
;
742 /* we don't want endless loop */
743 widget_idle (w
, FALSE
);
747 /* this is default resizing mechanism */
748 /* the main idea of this code is to resize dialog
749 according to flags (if any of flags require automatic
750 resizing, like WPOS_CENTER, end after that reposition
751 controls in dialog according to flags of widget) */
752 dlg_set_size (h
, w
->lines
, w
->cols
);
759 return MSG_NOT_HANDLED
;
762 /* --------------------------------------------------------------------------------------------- */
765 dlg_create (gboolean modal
, int y1
, int x1
, int lines
, int cols
, widget_pos_flags_t pos_flags
,
766 gboolean compact
, const int *colors
, widget_cb_fn callback
,
767 widget_mouse_cb_fn mouse_callback
, const char *help_ctx
, const char *title
)
772 new_d
= g_new0 (WDialog
, 1);
774 dlg_adjust_position (pos_flags
, &y1
, &x1
, &lines
, &cols
);
775 widget_init (w
, y1
, x1
, lines
, cols
, (callback
!= NULL
) ? callback
: dlg_default_callback
,
777 w
->pos_flags
= pos_flags
;
778 w
->options
|= WOP_SELECTABLE
| WOP_TOP_SELECT
;
780 w
->state
|= WST_CONSTRUCT
| WST_FOCUSED
;
782 w
->state
|= WST_MODAL
;
784 new_d
->color
= colors
;
785 new_d
->help_ctx
= help_ctx
;
786 new_d
->compact
= compact
;
789 new_d
->mouse_status
= MOU_UNHANDLED
;
791 dlg_set_title (new_d
, title
);
793 /* unique name of event group for this dialog */
794 new_d
->event_group
= g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG
, (void *) new_d
);
799 /* --------------------------------------------------------------------------------------------- */
802 dlg_set_default_colors (void)
804 dialog_colors
[DLG_COLOR_NORMAL
] = COLOR_NORMAL
;
805 dialog_colors
[DLG_COLOR_FOCUS
] = COLOR_FOCUS
;
806 dialog_colors
[DLG_COLOR_HOT_NORMAL
] = COLOR_HOT_NORMAL
;
807 dialog_colors
[DLG_COLOR_HOT_FOCUS
] = COLOR_HOT_FOCUS
;
808 dialog_colors
[DLG_COLOR_TITLE
] = COLOR_TITLE
;
810 alarm_colors
[DLG_COLOR_NORMAL
] = ERROR_COLOR
;
811 alarm_colors
[DLG_COLOR_FOCUS
] = ERROR_FOCUS
;
812 alarm_colors
[DLG_COLOR_HOT_NORMAL
] = ERROR_HOT_NORMAL
;
813 alarm_colors
[DLG_COLOR_HOT_FOCUS
] = ERROR_HOT_FOCUS
;
814 alarm_colors
[DLG_COLOR_TITLE
] = ERROR_TITLE
;
816 listbox_colors
[DLG_COLOR_NORMAL
] = PMENU_ENTRY_COLOR
;
817 listbox_colors
[DLG_COLOR_FOCUS
] = PMENU_SELECTED_COLOR
;
818 listbox_colors
[DLG_COLOR_HOT_NORMAL
] = PMENU_ENTRY_COLOR
;
819 listbox_colors
[DLG_COLOR_HOT_FOCUS
] = PMENU_SELECTED_COLOR
;
820 listbox_colors
[DLG_COLOR_TITLE
] = PMENU_TITLE_COLOR
;
823 /* --------------------------------------------------------------------------------------------- */
826 dlg_erase (WDialog
* h
)
828 Widget
*wh
= WIDGET (h
);
830 if (wh
!= NULL
&& widget_get_state (wh
, WST_ACTIVE
))
831 tty_fill_region (wh
->y
, wh
->x
, wh
->lines
, wh
->cols
, ' ');
834 /* --------------------------------------------------------------------------------------------- */
836 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
840 add_widget_autopos (WDialog
* h
, void *w
, widget_pos_flags_t pos_flags
, const void *before
)
842 Widget
*wh
= WIDGET (h
);
846 /* Don't accept 0 widgets */
852 if ((pos_flags
& WPOS_CENTER_HORZ
) != 0)
853 widget
->x
= (wh
->cols
- widget
->cols
) / 2;
856 if ((pos_flags
& WPOS_CENTER_VERT
) != 0)
857 widget
->y
= (wh
->lines
- widget
->lines
) / 2;
861 widget
->pos_flags
= pos_flags
;
862 widget
->id
= h
->widget_id
++;
864 if (h
->widgets
== NULL
|| before
== NULL
)
866 h
->widgets
= g_list_append (h
->widgets
, widget
);
867 new_current
= g_list_last (h
->widgets
);
873 b
= g_list_find (h
->widgets
, before
);
875 /* don't accept widget not from dialog. This shouldn't happen */
880 h
->widgets
= g_list_insert_before (h
->widgets
, b
, widget
);
882 new_current
= g_list_previous (b
);
884 new_current
= g_list_last (h
->widgets
);
887 /* widget has been added at runtime */
888 if (widget_get_state (wh
, WST_ACTIVE
))
890 send_message (widget
, NULL
, MSG_INIT
, 0, NULL
);
891 widget_select (widget
);
894 h
->current
= new_current
;
899 /* --------------------------------------------------------------------------------------------- */
900 /** wrapper to simply add lefttop positioned controls */
903 add_widget (WDialog
* h
, void *w
)
905 return add_widget_autopos (h
, w
, WPOS_KEEP_DEFAULT
,
906 h
->current
!= NULL
? h
->current
->data
: NULL
);
909 /* --------------------------------------------------------------------------------------------- */
912 add_widget_before (WDialog
* h
, void *w
, void *before
)
914 return add_widget_autopos (h
, w
, WPOS_KEEP_DEFAULT
, before
);
917 /* --------------------------------------------------------------------------------------------- */
919 /** delete widget from dialog */
926 /* Don't accept NULL widget. This shouldn't happen */
930 h
= WIDGET (w
)->owner
;
932 d
= g_list_find (h
->widgets
, w
);
934 dlg_set_current_widget_next (h
);
936 h
->widgets
= g_list_remove_link (h
->widgets
, d
);
937 if (h
->widgets
== NULL
)
939 send_message (d
->data
, NULL
, MSG_DESTROY
, 0, NULL
);
943 /* widget has been deleted in runtime */
944 if (widget_get_state (WIDGET (h
), WST_ACTIVE
))
947 dlg_select_current_widget (h
);
951 /* --------------------------------------------------------------------------------------------- */
961 dlg_redraw (DIALOG (d
->data
));
965 /* Search first fullscreen dialog */
966 for (; d
!= NULL
; d
= g_list_next (d
))
967 if ((WIDGET (d
->data
)->pos_flags
& WPOS_FULLSCREEN
) != 0)
969 /* back to top dialog */
970 for (; d
!= NULL
; d
= g_list_previous (d
))
971 dlg_redraw (DIALOG (d
->data
));
975 /* --------------------------------------------------------------------------------------------- */
976 /** broadcast a message to all the widgets in a dialog */
979 dlg_broadcast_msg (WDialog
* h
, widget_msg_t msg
)
981 dlg_broadcast_msg_to (h
, msg
, FALSE
, 0);
984 /* --------------------------------------------------------------------------------------------- */
985 /** Find the widget with the given callback in the dialog h */
988 find_widget_type (const WDialog
* h
, widget_cb_fn callback
)
992 w
= g_list_find_custom (h
->widgets
, (gconstpointer
) callback
, dlg_find_widget_callback
);
994 return (w
== NULL
) ? NULL
: WIDGET (w
->data
);
997 /* --------------------------------------------------------------------------------------------- */
1000 dlg_find (const WDialog
* h
, const Widget
* w
)
1002 return (w
->owner
== NULL
|| w
->owner
!= h
) ? NULL
: g_list_find (h
->widgets
, w
);
1005 /* --------------------------------------------------------------------------------------------- */
1006 /** Find the widget with the given id */
1009 dlg_find_by_id (const WDialog
* h
, unsigned long id
)
1013 w
= g_list_find_custom (h
->widgets
, GUINT_TO_POINTER (id
), dlg_find_widget_by_id
);
1014 return w
!= NULL
? WIDGET (w
->data
) : NULL
;
1017 /* --------------------------------------------------------------------------------------------- */
1018 /** Find the widget with the given id in the dialog h and select it */
1021 dlg_select_by_id (const WDialog
* h
, unsigned long id
)
1025 w
= dlg_find_by_id (h
, id
);
1030 /* --------------------------------------------------------------------------------------------- */
1031 /** Try to select previous widget in the tab order */
1034 dlg_select_prev_widget (WDialog
* h
)
1036 dlg_select_next_or_prev (h
, FALSE
);
1039 /* --------------------------------------------------------------------------------------------- */
1040 /** Try to select next widget in the tab order */
1043 dlg_select_next_widget (WDialog
* h
)
1045 dlg_select_next_or_prev (h
, TRUE
);
1048 /* --------------------------------------------------------------------------------------------- */
1051 update_cursor (WDialog
* h
)
1053 GList
*p
= h
->current
;
1055 if (p
!= NULL
&& widget_get_state (WIDGET (h
), WST_ACTIVE
))
1057 Widget
*w
= WIDGET (p
->data
);
1059 if (!widget_get_state (w
, WST_DISABLED
) && widget_get_options (w
, WOP_WANT_CURSOR
))
1060 send_message (w
, NULL
, MSG_CURSOR
, 0, NULL
);
1064 p
= dlg_get_widget_next_of (p
);
1065 if (p
== h
->current
)
1068 w
= WIDGET (p
->data
);
1070 if (!widget_get_state (w
, WST_DISABLED
) && widget_get_options (w
, WOP_WANT_CURSOR
)
1071 && send_message (w
, NULL
, MSG_CURSOR
, 0, NULL
) == MSG_HANDLED
)
1078 /* --------------------------------------------------------------------------------------------- */
1080 * Redraw the widgets in reverse order, leaving the current widget
1085 dlg_redraw (WDialog
* h
)
1087 if (!widget_get_state (WIDGET (h
), WST_ACTIVE
))
1090 if (h
->winch_pending
)
1092 h
->winch_pending
= FALSE
;
1093 send_message (h
, NULL
, MSG_RESIZE
, 0, NULL
);
1096 send_message (h
, NULL
, MSG_DRAW
, 0, NULL
);
1097 dlg_broadcast_msg (h
, MSG_DRAW
);
1101 /* --------------------------------------------------------------------------------------------- */
1104 dlg_stop (WDialog
* h
)
1106 widget_set_state (WIDGET (h
), WST_CLOSED
, TRUE
);
1109 /* --------------------------------------------------------------------------------------------- */
1110 /** Init the process */
1113 dlg_init (WDialog
* h
)
1115 Widget
*wh
= WIDGET (h
);
1117 if (top_dlg
!= NULL
&& widget_get_state (WIDGET (top_dlg
->data
), WST_MODAL
))
1118 widget_set_state (wh
, WST_MODAL
, TRUE
);
1120 /* add dialog to the stack */
1121 top_dlg
= g_list_prepend (top_dlg
, h
);
1123 /* Initialize dialog manager and widgets */
1124 if (widget_get_state (wh
, WST_CONSTRUCT
))
1126 if (!widget_get_state (wh
, WST_MODAL
))
1127 dialog_switch_add (h
);
1129 send_message (h
, NULL
, MSG_INIT
, 0, NULL
);
1130 dlg_broadcast_msg (h
, MSG_INIT
);
1131 dlg_read_history (h
);
1134 /* Select the first widget that takes focus */
1135 while (h
->current
!= NULL
&& !widget_get_options (WIDGET (h
->current
->data
), WOP_SELECTABLE
)
1136 && !widget_get_state (WIDGET (h
->current
->data
), WST_DISABLED
))
1137 dlg_set_current_widget_next (h
);
1139 widget_set_state (wh
, WST_ACTIVE
, TRUE
);
1141 /* focus found widget */
1142 if (h
->current
!= NULL
)
1143 widget_set_state (WIDGET (h
->current
->data
), WST_FOCUSED
, TRUE
);
1148 /* --------------------------------------------------------------------------------------------- */
1151 dlg_process_event (WDialog
* h
, int key
, Gpm_Event
* event
)
1155 if (tty_got_interrupt ())
1156 if (send_message (h
, NULL
, MSG_ACTION
, CK_Cancel
, NULL
) != MSG_HANDLED
)
1157 dlg_execute_cmd (h
, CK_Cancel
);
1159 else if (key
== EV_MOUSE
)
1160 h
->mouse_status
= dlg_mouse_event (h
, event
);
1162 dlg_key_event (h
, key
);
1165 /* --------------------------------------------------------------------------------------------- */
1166 /** Shutdown the dlg_run */
1169 dlg_run_done (WDialog
* h
)
1171 top_dlg
= g_list_remove (top_dlg
, h
);
1173 if (widget_get_state (WIDGET (h
), WST_CLOSED
))
1175 send_message (h
, h
->current
== NULL
? NULL
: WIDGET (h
->current
->data
), MSG_END
, 0, NULL
);
1176 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
1177 dialog_switch_remove (h
);
1181 /* --------------------------------------------------------------------------------------------- */
1183 * Standard run dialog routine
1184 * We have to keep this routine small so that we can duplicate it's
1185 * behavior on complex routines like the file routines, this way,
1186 * they can call the dlg_process_event without rewriting all the code
1190 dlg_run (WDialog
* h
)
1193 frontend_dlg_run (h
);
1195 return h
->ret_value
;
1198 /* --------------------------------------------------------------------------------------------- */
1201 dlg_destroy (WDialog
* h
)
1203 /* if some widgets have history, save all history at one moment here */
1204 dlg_save_history (h
);
1205 dlg_broadcast_msg (h
, MSG_DESTROY
);
1206 g_list_free_full (h
->widgets
, g_free
);
1207 mc_event_group_del (h
->event_group
);
1208 g_free (h
->event_group
);
1215 /* --------------------------------------------------------------------------------------------- */
1218 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1221 dlg_save_history (WDialog
* h
)
1226 if (num_history_items_recorded
== 0) /* this is how to disable */
1229 profile
= mc_config_get_full_path (MC_HISTORY_FILE
);
1230 i
= open (profile
, O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
);
1234 /* Make sure the history is only readable by the user */
1235 if (chmod (profile
, S_IRUSR
| S_IWUSR
) != -1 || errno
== ENOENT
)
1237 ev_history_load_save_t event_data
;
1239 event_data
.cfg
= mc_config_init (profile
, FALSE
);
1240 event_data
.receiver
= NULL
;
1242 /* get all histories in dialog */
1243 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_SAVE
, &event_data
);
1245 mc_config_save_file (event_data
.cfg
, NULL
);
1246 mc_config_deinit (event_data
.cfg
);
1252 /* --------------------------------------------------------------------------------------------- */
1255 dlg_set_title (WDialog
* h
, const char *title
)
1257 MC_PTR_FREE (h
->title
);
1259 /* Strip existing spaces, add one space before and after the title */
1260 if (title
!= NULL
&& title
[0] != '\0')
1264 t
= g_strstrip (g_strdup (title
));
1266 h
->title
= g_strdup_printf (" %s ", t
);
1271 /* --------------------------------------------------------------------------------------------- */
1274 dlg_get_title (const WDialog
* h
, size_t len
)
1281 if (h
->get_title
!= NULL
)
1282 t
= h
->get_title (h
, len
);
1289 /* --------------------------------------------------------------------------------------------- */
1292 * Switch current widget to widget after current in dialog.
1294 * @param h WDialog widget
1298 dlg_set_current_widget_next (WDialog
* h
)
1300 h
->current
= dlg_get_next_or_prev_of (h
->current
, TRUE
);
1303 /* --------------------------------------------------------------------------------------------- */
1306 * Switch current widget to widget before current in dialog.
1308 * @param h WDialog widget
1312 dlg_set_current_widget_prev (WDialog
* h
)
1314 h
->current
= dlg_get_next_or_prev_of (h
->current
, FALSE
);
1317 /* --------------------------------------------------------------------------------------------- */
1320 * Get widget that is after specified widget in dialog.
1322 * @param w widget holder
1324 * @return widget that is after "w" or NULL if "w" is NULL or widget doesn't have owner
1328 dlg_get_widget_next_of (GList
* w
)
1330 return dlg_get_next_or_prev_of (w
, TRUE
);
1333 /* --------------------------------------------------------------------------------------------- */
1336 * Get widget that is before specified widget in dialog.
1338 * @param w widget holder
1340 * @return widget that is before "w" or NULL if "w" is NULL or widget doesn't have owner
1344 dlg_get_widget_prev_of (GList
* w
)
1346 return dlg_get_next_or_prev_of (w
, FALSE
);
1349 /* --------------------------------------------------------------------------------------------- */