2 Dialog box features module for the Midnight Commander
4 Copyright (C) 1994-2020
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 */
46 #include "lib/mcconfig.h" /* num_history_items_recorded */
48 #include "lib/widget.h"
49 #include "lib/widget/mouse.h"
51 /*** global variables ****************************************************************************/
53 /* Color styles for normal and error dialogs */
54 dlg_colors_t dialog_colors
;
55 dlg_colors_t alarm_colors
;
56 dlg_colors_t listbox_colors
;
58 /* Primitive way to check if the the current dialog is our dialog */
59 /* This is needed by async routines like load_prompt */
60 GList
*top_dlg
= NULL
;
62 /* A hook list for idle events */
63 hook_t
*idle_hook
= NULL
;
65 /* If set then dialogs just clean the screen when refreshing, else */
66 /* they do a complete refresh, refreshing all the parts of the program */
67 gboolean fast_refresh
= FALSE
;
69 /* left click outside of dialog closes it */
70 gboolean mouse_close_dialog
= FALSE
;
72 const global_keymap_t
*dialog_map
= NULL
;
74 /*** file scope macro definitions ****************************************************************/
76 /*** file scope type declarations ****************************************************************/
78 /* Control widget positions in dialog */
85 } widget_shift_scale_t
;
87 /*** file scope variables ************************************************************************/
89 /* --------------------------------------------------------------------------------------------- */
90 /*** file scope functions ************************************************************************/
91 /* --------------------------------------------------------------------------------------------- */
94 * broadcast a message to all the widgets in a dialog that have
95 * the options set to flags. If flags is zero, the message is sent
100 dlg_broadcast_msg_to (WDialog
* h
, widget_msg_t msg
, gboolean reverse
, widget_options_t flags
)
103 WGroup
*g
= GROUP (h
);
105 if (g
->widgets
== NULL
)
108 if (g
->current
== NULL
)
109 g
->current
= g
->widgets
;
112 p
= group_get_widget_prev_of (g
->current
);
114 p
= group_get_widget_next_of (g
->current
);
120 Widget
*w
= WIDGET (p
->data
);
123 p
= group_get_widget_prev_of (p
);
125 p
= group_get_widget_next_of (p
);
127 if ((flags
== 0) || ((flags
& w
->options
) != 0))
128 send_message (w
, NULL
, msg
, 0, NULL
);
133 /* --------------------------------------------------------------------------------------------- */
136 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
139 dlg_read_history (WDialog
* h
)
142 ev_history_load_save_t event_data
;
144 if (num_history_items_recorded
== 0) /* this is how to disable */
147 profile
= mc_config_get_full_path (MC_HISTORY_FILE
);
148 event_data
.cfg
= mc_config_init (profile
, TRUE
);
149 event_data
.receiver
= NULL
;
151 /* create all histories in dialog */
152 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_LOAD
, &event_data
);
154 mc_config_deinit (event_data
.cfg
);
158 /* --------------------------------------------------------------------------------------------- */
161 dlg_find_widget_callback (const void *a
, const void *b
)
163 const Widget
*w
= CONST_WIDGET (a
);
164 const widget_cb_fn f
= b
;
166 return (w
->callback
== f
) ? 0 : 1;
169 /* --------------------------------------------------------------------------------------------- */
178 /* Use this if the refreshes fail */
181 #endif /* HAVE_SLANG */
184 /* --------------------------------------------------------------------------------------------- */
187 dlg_execute_cmd (WDialog
* h
, long command
)
189 WGroup
*g
= GROUP (h
);
190 cb_ret_t ret
= MSG_HANDLED
;
192 if (send_message (h
, NULL
, MSG_ACTION
, command
, NULL
) == MSG_HANDLED
)
198 h
->ret_value
= B_ENTER
;
202 h
->ret_value
= B_CANCEL
;
208 group_select_prev_widget (g
);
212 group_select_next_widget (g
);
217 ev_help_t event_data
= { NULL
, h
->help_ctx
};
218 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
223 mc_event_raise (MCEVENT_GROUP_CORE
, "suspend", NULL
);
231 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
232 dialog_switch_list ();
234 ret
= MSG_NOT_HANDLED
;
237 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
238 dialog_switch_next ();
240 ret
= MSG_NOT_HANDLED
;
243 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
244 dialog_switch_prev ();
246 ret
= MSG_NOT_HANDLED
;
250 ret
= MSG_NOT_HANDLED
;
256 /* --------------------------------------------------------------------------------------------- */
259 dlg_handle_key (WDialog
* h
, int d_key
)
263 command
= keybind_lookup_keymap_command (dialog_map
, d_key
);
264 if (command
!= CK_IgnoreKey
)
265 return dlg_execute_cmd (h
, command
);
267 return MSG_NOT_HANDLED
;
270 /* --------------------------------------------------------------------------------------------- */
272 * This is the low-level mouse handler.
273 * It receives a Gpm_Event event and translates it into a higher level protocol.
276 dlg_mouse_translator (Gpm_Event
* event
, Widget
* w
)
280 me
= mouse_translate_event (w
, event
);
282 return mouse_process_event (w
, &me
);
285 /* --------------------------------------------------------------------------------------------- */
288 dlg_mouse_event (WDialog
* h
, Gpm_Event
* event
)
290 Widget
*wh
= WIDGET (h
);
294 /* close the dialog by mouse left click out of dialog area */
295 if (mouse_close_dialog
&& (wh
->pos_flags
& WPOS_FULLSCREEN
) == 0
296 && ((event
->buttons
& GPM_B_LEFT
) != 0) && ((event
->type
& GPM_DOWN
) != 0)
297 && !mouse_global_in_widget (event
, wh
))
299 h
->ret_value
= B_CANCEL
;
304 if (wh
->mouse_callback
!= NULL
)
308 mou
= dlg_mouse_translator (event
, wh
);
309 if (mou
!= MOU_UNHANDLED
)
313 if (GROUP (h
)->widgets
== NULL
)
314 return MOU_UNHANDLED
;
316 /* send the event to widgets in reverse Z-order */
317 p
= g_list_last (GROUP (h
)->widgets
);
320 Widget
*w
= WIDGET (p
->data
);
322 if (!widget_get_state (w
, WST_DISABLED
) && w
->mouse_callback
!= NULL
)
324 /* put global cursor position to the widget */
327 ret
= dlg_mouse_translator (event
, w
);
328 if (ret
!= MOU_UNHANDLED
)
332 p
= g_list_previous (p
);
336 return MOU_UNHANDLED
;
339 /* --------------------------------------------------------------------------------------------- */
342 dlg_try_hotkey (WDialog
* h
, int d_key
)
344 WGroup
*g
= GROUP (h
);
350 if (g
->widgets
== NULL
)
351 return MSG_NOT_HANDLED
;
353 if (g
->current
== NULL
)
354 g
->current
= g
->widgets
;
357 * Explanation: we don't send letter hotkeys to other widgets if
358 * the currently selected widget is an input line
361 current
= WIDGET (g
->current
->data
);
363 if (widget_get_state (current
, WST_DISABLED
))
364 return MSG_NOT_HANDLED
;
366 if (widget_get_options (current
, WOP_IS_INPUT
))
368 /* skip ascii control characters, anything else can valid character in
370 if (d_key
>= 32 && d_key
< 256)
371 return MSG_NOT_HANDLED
;
374 /* If it's an alt key, send the message */
375 c
= d_key
& ~ALT (0);
376 if (d_key
& ALT (0) && g_ascii_isalpha (c
))
377 d_key
= g_ascii_tolower (c
);
379 handled
= MSG_NOT_HANDLED
;
380 if (widget_get_options (current
, WOP_WANT_HOTKEY
))
381 handled
= send_message (current
, NULL
, MSG_HOTKEY
, d_key
, NULL
);
383 /* If not used, send hotkey to other widgets */
384 if (handled
== MSG_HANDLED
)
387 hot_cur
= group_get_widget_next_of (g
->current
);
389 /* send it to all widgets */
390 while (g
->current
!= hot_cur
&& handled
== MSG_NOT_HANDLED
)
392 current
= WIDGET (hot_cur
->data
);
394 if (widget_get_options (current
, WOP_WANT_HOTKEY
)
395 && !widget_get_state (current
, WST_DISABLED
))
396 handled
= send_message (current
, NULL
, MSG_HOTKEY
, d_key
, NULL
);
398 if (handled
== MSG_NOT_HANDLED
)
399 hot_cur
= group_get_widget_next_of (hot_cur
);
402 if (handled
== MSG_HANDLED
)
403 widget_select (WIDGET (hot_cur
->data
));
408 /* --------------------------------------------------------------------------------------------- */
411 dlg_key_event (WDialog
* h
, int d_key
)
413 WGroup
*g
= GROUP (h
);
416 if (g
->widgets
== NULL
)
419 if (g
->current
== NULL
)
420 g
->current
= g
->widgets
;
422 /* TAB used to cycle */
423 if (!widget_get_options (WIDGET (g
), WOP_WANT_TAB
))
427 group_select_next_widget (g
);
430 else if ((d_key
& ~(KEY_M_SHIFT
| KEY_M_CTRL
)) == '\t')
432 group_select_prev_widget (g
);
437 /* first can dlg_callback handle the key */
438 handled
= send_message (h
, NULL
, MSG_KEY
, d_key
, NULL
);
440 /* next try the hotkey */
441 if (handled
== MSG_NOT_HANDLED
)
442 handled
= dlg_try_hotkey (h
, d_key
);
444 if (handled
== MSG_HANDLED
)
445 send_message (h
, NULL
, MSG_HOTKEY_HANDLED
, 0, NULL
);
447 /* not used - then try widget_callback */
448 handled
= send_message (g
->current
->data
, NULL
, MSG_KEY
, d_key
, NULL
);
450 /* not used- try to use the unhandled case */
451 if (handled
== MSG_NOT_HANDLED
)
452 handled
= send_message (h
, NULL
, MSG_UNHANDLED_KEY
, d_key
, NULL
);
454 if (handled
== MSG_NOT_HANDLED
)
455 handled
= dlg_handle_key (h
, d_key
);
458 send_message (h
, NULL
, MSG_POST_KEY
, d_key
, NULL
);
461 /* --------------------------------------------------------------------------------------------- */
464 frontend_dlg_run (WDialog
* h
)
466 Widget
*wh
= WIDGET (h
);
471 /* close opened editors, viewers, etc */
472 if (!widget_get_state (wh
, WST_MODAL
) && mc_global
.midnight_shutdown
)
474 send_message (h
, NULL
, MSG_VALIDATE
, 0, NULL
);
478 while (widget_get_state (wh
, WST_ACTIVE
))
482 if (tty_got_winch ())
483 dialog_change_screen_size ();
488 execute_hooks (idle_hook
);
490 while (widget_get_state (wh
, WST_IDLE
) && is_idle ())
491 send_message (wh
, NULL
, MSG_IDLE
, 0, NULL
);
493 /* Allow terminating the dialog from the idle handler */
494 if (!widget_get_state (wh
, WST_ACTIVE
))
500 /* Clear interrupt flag */
501 tty_got_interrupt ();
502 d_key
= tty_get_event (&event
, h
->mouse_status
== MOU_REPEAT
, TRUE
);
504 dlg_process_event (h
, d_key
, &event
);
506 if (widget_get_state (wh
, WST_CLOSED
))
507 send_message (h
, NULL
, MSG_VALIDATE
, 0, NULL
);
511 /* --------------------------------------------------------------------------------------------- */
514 dlg_find_widget_by_id (gconstpointer a
, gconstpointer b
)
516 const Widget
*w
= CONST_WIDGET (a
);
517 unsigned long id
= GPOINTER_TO_UINT (b
);
519 return w
->id
== id
? 0 : 1;
522 /* --------------------------------------------------------------------------------------------- */
524 dlg_widget_set_position (gpointer data
, gpointer user_data
)
526 /* there are, mainly, 2 generally possible situations:
527 * 1. control sticks to one side - it should be moved
528 * 2. control sticks to two sides of one direction - it should be sized
531 Widget
*c
= WIDGET (data
);
532 Widget
*wh
= WIDGET (c
->owner
);
533 const widget_shift_scale_t
*wss
= (const widget_shift_scale_t
*) user_data
;
537 int lines
= c
->lines
;
539 if ((c
->pos_flags
& WPOS_CENTER_HORZ
) != 0)
540 x
= wh
->x
+ (wh
->cols
- c
->cols
) / 2;
541 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0 && (c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
544 cols
+= wss
->scale_x
;
546 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0)
548 else if ((c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
549 x
+= wss
->shift_x
+ wss
->scale_x
;
551 if ((c
->pos_flags
& WPOS_CENTER_VERT
) != 0)
552 y
= wh
->y
+ (wh
->lines
- c
->lines
) / 2;
553 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0 && (c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
556 lines
+= wss
->scale_y
;
558 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0)
560 else if ((c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
561 y
+= wss
->shift_y
+ wss
->scale_y
;
563 widget_set_size (c
, y
, x
, lines
, cols
);
566 /* --------------------------------------------------------------------------------------------- */
567 /*** public functions ****************************************************************************/
568 /* --------------------------------------------------------------------------------------------- */
570 /** Clean the dialog area, draw the frame and the title */
572 dlg_default_repaint (WDialog
* h
)
574 Widget
*wh
= WIDGET (h
);
578 if (!widget_get_state (wh
, WST_ACTIVE
))
581 space
= h
->compact
? 0 : 1;
583 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
585 tty_draw_box (wh
->y
+ space
, wh
->x
+ space
, wh
->lines
- 2 * space
, wh
->cols
- 2 * space
, FALSE
);
587 if (h
->title
!= NULL
)
589 /* TODO: truncate long title */
590 tty_setcolor (h
->color
[DLG_COLOR_TITLE
]);
591 widget_gotoyx (h
, space
, (wh
->cols
- str_term_width1 (h
->title
)) / 2);
592 tty_print_string (h
->title
);
596 /* --------------------------------------------------------------------------------------------- */
597 /** this function allows to set dialog position */
600 dlg_set_position (WDialog
* h
, int y
, int x
, int lines
, int cols
)
602 WGroup
*g
= GROUP (h
);
603 Widget
*wh
= WIDGET (h
);
604 widget_shift_scale_t wss
;
606 /* save old positions, will be used to reposition childs */
609 /* save old positions, will be used to reposition childs */
620 /* dialog is empty */
621 if (g
->widgets
== NULL
)
624 if (g
->current
== NULL
)
625 g
->current
= g
->widgets
;
627 /* values by which controls should be moved */
628 wss
.shift_x
= wh
->x
- ox
;
629 wss
.scale_x
= wh
->cols
- oc
;
630 wss
.shift_y
= wh
->y
- oy
;
631 wss
.scale_y
= wh
->lines
- ol
;
633 if (wss
.shift_x
!= 0 || wss
.shift_y
!= 0 || wss
.scale_x
!= 0 || wss
.scale_y
!= 0)
634 g_list_foreach (g
->widgets
, dlg_widget_set_position
, &wss
);
637 /* --------------------------------------------------------------------------------------------- */
638 /** Set dialog size and position */
641 dlg_set_size (WDialog
* h
, int lines
, int cols
)
645 widget_adjust_position (WIDGET (h
)->pos_flags
, &y
, &x
, &lines
, &cols
);
646 dlg_set_position (h
, y
, x
, lines
, cols
);
649 /* --------------------------------------------------------------------------------------------- */
650 /** Default dialog callback */
653 dlg_default_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
655 WDialog
*h
= DIALOG (w
);
664 if (h
->color
!= NULL
)
666 dlg_default_repaint (h
);
669 return MSG_NOT_HANDLED
;
672 /* we don't want endless loop */
673 widget_idle (w
, FALSE
);
677 /* this is default resizing mechanism */
678 /* the main idea of this code is to resize dialog
679 according to flags (if any of flags require automatic
680 resizing, like WPOS_CENTER, end after that reposition
681 controls in dialog according to flags of widget) */
682 dlg_set_size (h
, w
->lines
, w
->cols
);
689 return MSG_NOT_HANDLED
;
692 /* --------------------------------------------------------------------------------------------- */
695 dlg_create (gboolean modal
, int y1
, int x1
, int lines
, int cols
, widget_pos_flags_t pos_flags
,
696 gboolean compact
, const int *colors
, widget_cb_fn callback
,
697 widget_mouse_cb_fn mouse_callback
, const char *help_ctx
, const char *title
)
702 new_d
= g_new0 (WDialog
, 1);
704 widget_adjust_position (pos_flags
, &y1
, &x1
, &lines
, &cols
);
705 widget_init (w
, y1
, x1
, lines
, cols
, (callback
!= NULL
) ? callback
: dlg_default_callback
,
707 w
->pos_flags
= pos_flags
;
708 w
->options
|= WOP_SELECTABLE
| WOP_TOP_SELECT
;
710 w
->state
|= WST_CONSTRUCT
| WST_FOCUSED
;
712 w
->state
|= WST_MODAL
;
714 new_d
->color
= colors
;
715 new_d
->help_ctx
= help_ctx
;
716 new_d
->compact
= compact
;
719 new_d
->mouse_status
= MOU_UNHANDLED
;
721 dlg_set_title (new_d
, title
);
723 /* unique name of event group for this dialog */
724 new_d
->event_group
= g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG
, (void *) new_d
);
729 /* --------------------------------------------------------------------------------------------- */
732 dlg_set_default_colors (void)
734 dialog_colors
[DLG_COLOR_NORMAL
] = COLOR_NORMAL
;
735 dialog_colors
[DLG_COLOR_FOCUS
] = COLOR_FOCUS
;
736 dialog_colors
[DLG_COLOR_HOT_NORMAL
] = COLOR_HOT_NORMAL
;
737 dialog_colors
[DLG_COLOR_HOT_FOCUS
] = COLOR_HOT_FOCUS
;
738 dialog_colors
[DLG_COLOR_TITLE
] = COLOR_TITLE
;
740 alarm_colors
[DLG_COLOR_NORMAL
] = ERROR_COLOR
;
741 alarm_colors
[DLG_COLOR_FOCUS
] = ERROR_FOCUS
;
742 alarm_colors
[DLG_COLOR_HOT_NORMAL
] = ERROR_HOT_NORMAL
;
743 alarm_colors
[DLG_COLOR_HOT_FOCUS
] = ERROR_HOT_FOCUS
;
744 alarm_colors
[DLG_COLOR_TITLE
] = ERROR_TITLE
;
746 listbox_colors
[DLG_COLOR_NORMAL
] = PMENU_ENTRY_COLOR
;
747 listbox_colors
[DLG_COLOR_FOCUS
] = PMENU_SELECTED_COLOR
;
748 listbox_colors
[DLG_COLOR_HOT_NORMAL
] = PMENU_ENTRY_COLOR
;
749 listbox_colors
[DLG_COLOR_HOT_FOCUS
] = PMENU_SELECTED_COLOR
;
750 listbox_colors
[DLG_COLOR_TITLE
] = PMENU_TITLE_COLOR
;
753 /* --------------------------------------------------------------------------------------------- */
756 dlg_erase (WDialog
* h
)
758 Widget
*wh
= WIDGET (h
);
760 if (wh
!= NULL
&& widget_get_state (wh
, WST_ACTIVE
))
761 tty_fill_region (wh
->y
, wh
->x
, wh
->lines
, wh
->cols
, ' ');
764 /* --------------------------------------------------------------------------------------------- */
774 dlg_draw (DIALOG (d
->data
));
778 /* Search first fullscreen dialog */
779 for (; d
!= NULL
; d
= g_list_next (d
))
780 if ((WIDGET (d
->data
)->pos_flags
& WPOS_FULLSCREEN
) != 0)
782 /* back to top dialog */
783 for (; d
!= NULL
; d
= g_list_previous (d
))
784 dlg_draw (DIALOG (d
->data
));
788 /* --------------------------------------------------------------------------------------------- */
789 /** broadcast a message to all the widgets in a dialog */
792 dlg_broadcast_msg (WDialog
* h
, widget_msg_t msg
)
794 dlg_broadcast_msg_to (h
, msg
, FALSE
, 0);
797 /* --------------------------------------------------------------------------------------------- */
798 /** Find the widget with the given callback in the dialog h */
801 find_widget_type (const WDialog
* h
, widget_cb_fn callback
)
805 w
= g_list_find_custom (CONST_GROUP (h
)->widgets
, (gconstpointer
) callback
,
806 dlg_find_widget_callback
);
808 return (w
== NULL
) ? NULL
: WIDGET (w
->data
);
811 /* --------------------------------------------------------------------------------------------- */
814 dlg_find (const WDialog
* h
, const Widget
* w
)
816 const WGroup
*g
= CONST_GROUP (h
);
818 return (w
->owner
== NULL
|| w
->owner
!= g
) ? NULL
: g_list_find (g
->widgets
, w
);
821 /* --------------------------------------------------------------------------------------------- */
822 /** Find the widget with the given id */
825 dlg_find_by_id (const WDialog
* h
, unsigned long id
)
829 w
= g_list_find_custom (CONST_GROUP (h
)->widgets
, GUINT_TO_POINTER (id
), dlg_find_widget_by_id
);
830 return w
!= NULL
? WIDGET (w
->data
) : NULL
;
833 /* --------------------------------------------------------------------------------------------- */
836 update_cursor (WDialog
* h
)
838 WGroup
*g
= GROUP (h
);
839 GList
*p
= g
->current
;
841 if (p
!= NULL
&& widget_get_state (WIDGET (h
), WST_ACTIVE
))
843 Widget
*w
= WIDGET (p
->data
);
845 if (!widget_get_state (w
, WST_DISABLED
) && widget_get_options (w
, WOP_WANT_CURSOR
))
846 send_message (w
, NULL
, MSG_CURSOR
, 0, NULL
);
850 p
= group_get_widget_next_of (p
);
854 w
= WIDGET (p
->data
);
856 if (!widget_get_state (w
, WST_DISABLED
) && widget_get_options (w
, WOP_WANT_CURSOR
)
857 && send_message (w
, NULL
, MSG_CURSOR
, 0, NULL
) == MSG_HANDLED
)
864 /* --------------------------------------------------------------------------------------------- */
866 * Redraw the widgets in reverse order, leaving the current widget
871 dlg_draw (WDialog
* h
)
873 WGroup
*g
= GROUP (h
);
875 if (!widget_get_state (WIDGET (h
), WST_ACTIVE
))
878 if (g
->winch_pending
)
880 g
->winch_pending
= FALSE
;
881 send_message (h
, NULL
, MSG_RESIZE
, 0, NULL
);
884 send_message (h
, NULL
, MSG_DRAW
, 0, NULL
);
885 dlg_broadcast_msg (h
, MSG_DRAW
);
889 /* --------------------------------------------------------------------------------------------- */
892 dlg_stop (WDialog
* h
)
894 widget_set_state (WIDGET (h
), WST_CLOSED
, TRUE
);
897 /* --------------------------------------------------------------------------------------------- */
898 /** Init the process */
901 dlg_init (WDialog
* h
)
903 WGroup
*g
= GROUP (h
);
904 Widget
*wh
= WIDGET (h
);
906 if (top_dlg
!= NULL
&& widget_get_state (WIDGET (top_dlg
->data
), WST_MODAL
))
907 widget_set_state (wh
, WST_MODAL
, TRUE
);
909 /* add dialog to the stack */
910 top_dlg
= g_list_prepend (top_dlg
, h
);
912 /* Initialize dialog manager and widgets */
913 if (widget_get_state (wh
, WST_CONSTRUCT
))
915 if (!widget_get_state (wh
, WST_MODAL
))
916 dialog_switch_add (h
);
918 send_message (h
, NULL
, MSG_INIT
, 0, NULL
);
919 dlg_broadcast_msg (h
, MSG_INIT
);
920 dlg_read_history (h
);
923 /* Select the first widget that takes focus */
924 while (g
->current
!= NULL
&& !widget_get_options (WIDGET (g
->current
->data
), WOP_SELECTABLE
)
925 && !widget_get_state (WIDGET (g
->current
->data
), WST_DISABLED
))
926 group_set_current_widget_next (g
);
928 widget_set_state (wh
, WST_ACTIVE
, TRUE
);
930 /* focus found widget */
931 if (g
->current
!= NULL
)
932 widget_set_state (WIDGET (g
->current
->data
), WST_FOCUSED
, TRUE
);
937 /* --------------------------------------------------------------------------------------------- */
940 dlg_process_event (WDialog
* h
, int key
, Gpm_Event
* event
)
945 if (tty_got_interrupt ())
946 dlg_execute_cmd (h
, CK_Cancel
);
950 h
->mouse_status
= dlg_mouse_event (h
, event
);
954 dlg_key_event (h
, key
);
959 /* --------------------------------------------------------------------------------------------- */
960 /** Shutdown the dlg_run */
963 dlg_run_done (WDialog
* h
)
965 top_dlg
= g_list_remove (top_dlg
, h
);
967 if (widget_get_state (WIDGET (h
), WST_CLOSED
))
969 send_message (h
, GROUP (h
)->current
== NULL
? NULL
: WIDGET (GROUP (h
)->current
->data
),
971 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
972 dialog_switch_remove (h
);
976 /* --------------------------------------------------------------------------------------------- */
978 * Standard run dialog routine
979 * We have to keep this routine small so that we can duplicate it's
980 * behavior on complex routines like the file routines, this way,
981 * they can call the dlg_process_event without rewriting all the code
985 dlg_run (WDialog
* h
)
988 frontend_dlg_run (h
);
993 /* --------------------------------------------------------------------------------------------- */
996 dlg_destroy (WDialog
* h
)
998 WGroup
*g
= GROUP (h
);
1000 /* if some widgets have history, save all history at one moment here */
1001 dlg_save_history (h
);
1002 g_list_foreach (g
->widgets
, (GFunc
) widget_destroy
, NULL
);
1003 g_list_free (g
->widgets
);
1004 mc_event_group_del (h
->event_group
);
1005 g_free (h
->event_group
);
1012 /* --------------------------------------------------------------------------------------------- */
1015 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1018 dlg_save_history (WDialog
* h
)
1023 if (num_history_items_recorded
== 0) /* this is how to disable */
1026 profile
= mc_config_get_full_path (MC_HISTORY_FILE
);
1027 i
= open (profile
, O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
);
1031 /* Make sure the history is only readable by the user */
1032 if (chmod (profile
, S_IRUSR
| S_IWUSR
) != -1 || errno
== ENOENT
)
1034 ev_history_load_save_t event_data
;
1036 event_data
.cfg
= mc_config_init (profile
, FALSE
);
1037 event_data
.receiver
= NULL
;
1039 /* get all histories in dialog */
1040 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_SAVE
, &event_data
);
1042 mc_config_save_file (event_data
.cfg
, NULL
);
1043 mc_config_deinit (event_data
.cfg
);
1049 /* --------------------------------------------------------------------------------------------- */
1052 dlg_set_title (WDialog
* h
, const char *title
)
1054 MC_PTR_FREE (h
->title
);
1056 /* Strip existing spaces, add one space before and after the title */
1057 if (title
!= NULL
&& title
[0] != '\0')
1061 t
= g_strstrip (g_strdup (title
));
1063 h
->title
= g_strdup_printf (" %s ", t
);
1068 /* --------------------------------------------------------------------------------------------- */
1071 dlg_get_title (const WDialog
* h
, size_t len
)
1078 if (h
->get_title
!= NULL
)
1079 t
= h
->get_title (h
, len
);
1086 /* --------------------------------------------------------------------------------------------- */