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 /* --------------------------------------------------------------------------------------------- */
218 * Try to select another widget. If forward is set, follow tab order.
219 * Otherwise go to the previous widget.
223 do_select_widget (WDialog
* h
, GList
* w
, select_dir_t dir
)
225 Widget
*w0
= WIDGET (h
->current
->data
);
227 if (!dlg_unfocus (h
))
240 h
->current
= g_list_find (h
->widgets
, w0
);
243 /* try find another widget that can take focus */
247 h
->current
= dlg_widget_next (h
, h
->current
);
250 h
->current
= dlg_widget_prev (h
, h
->current
);
256 while (h
->current
!= w
);
258 if (widget_overlapped (w0
, WIDGET (h
->current
->data
)))
260 send_message (h
->current
->data
, NULL
, MSG_DRAW
, 0, NULL
);
261 send_message (h
->current
->data
, NULL
, MSG_FOCUS
, 0, NULL
);
265 /* --------------------------------------------------------------------------------------------- */
274 /* Use this if the refreshes fail */
277 #endif /* HAVE_SLANG */
280 /* --------------------------------------------------------------------------------------------- */
283 dlg_execute_cmd (WDialog
* h
, long command
)
285 cb_ret_t ret
= MSG_HANDLED
;
289 h
->ret_value
= B_ENTER
;
293 h
->ret_value
= B_CANCEL
;
308 ev_help_t event_data
= { NULL
, h
->help_ctx
};
309 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
314 mc_event_raise (MCEVENT_GROUP_CORE
, "suspend", NULL
);
322 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
323 dialog_switch_list ();
325 ret
= MSG_NOT_HANDLED
;
328 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
329 dialog_switch_next ();
331 ret
= MSG_NOT_HANDLED
;
334 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
335 dialog_switch_prev ();
337 ret
= MSG_NOT_HANDLED
;
341 ret
= MSG_NOT_HANDLED
;
347 /* --------------------------------------------------------------------------------------------- */
350 dlg_handle_key (WDialog
* h
, int d_key
)
354 command
= keybind_lookup_keymap_command (dialog_map
, d_key
);
356 if (command
== CK_IgnoreKey
)
357 return MSG_NOT_HANDLED
;
359 if (send_message (h
, NULL
, MSG_ACTION
, command
, NULL
) == MSG_HANDLED
360 || dlg_execute_cmd (h
, command
) == MSG_HANDLED
)
363 return MSG_NOT_HANDLED
;
366 /* --------------------------------------------------------------------------------------------- */
368 * This is the low-level mouse handler.
369 * It receives a Gpm_Event event and translates it into a higher level protocol.
372 dlg_mouse_translator (Gpm_Event
* event
, Widget
* w
)
376 me
= mouse_translate_event (w
, event
);
378 return mouse_process_event (w
, &me
);
381 /* --------------------------------------------------------------------------------------------- */
384 dlg_mouse_event (WDialog
* h
, Gpm_Event
* event
)
386 Widget
*wh
= WIDGET (h
);
390 /* close the dialog by mouse left click out of dialog area */
391 if (mouse_close_dialog
&& !h
->fullscreen
&& ((event
->buttons
& GPM_B_LEFT
) != 0)
392 && ((event
->type
& GPM_DOWN
) != 0) && !mouse_global_in_widget (event
, wh
))
394 h
->ret_value
= B_CANCEL
;
399 if (wh
->mouse_callback
!= NULL
)
403 mou
= dlg_mouse_translator (event
, wh
);
404 if (mou
!= MOU_UNHANDLED
)
408 /* send the event to widgets in reverse Z-order */
409 p
= g_list_last (h
->widgets
);
412 Widget
*w
= WIDGET (p
->data
);
414 if (!widget_get_state (w
, WST_DISABLED
) && w
->mouse_callback
!= NULL
)
416 /* put global cursor position to the widget */
419 ret
= dlg_mouse_translator (event
, w
);
420 if (ret
!= MOU_UNHANDLED
)
424 p
= g_list_previous (p
);
428 return MOU_UNHANDLED
;
431 /* --------------------------------------------------------------------------------------------- */
434 dlg_try_hotkey (WDialog
* h
, int d_key
)
441 if (h
->widgets
== NULL
)
442 return MSG_NOT_HANDLED
;
444 if (h
->current
== NULL
)
445 h
->current
= h
->widgets
;
448 * Explanation: we don't send letter hotkeys to other widgets if
449 * the currently selected widget is an input line
452 current
= WIDGET (h
->current
->data
);
454 if (widget_get_state (current
, WST_DISABLED
))
455 return MSG_NOT_HANDLED
;
457 if (widget_get_options (current
, WOP_IS_INPUT
))
459 /* skip ascii control characters, anything else can valid character in
461 if (d_key
>= 32 && d_key
< 256)
462 return MSG_NOT_HANDLED
;
465 /* If it's an alt key, send the message */
466 c
= d_key
& ~ALT (0);
467 if (d_key
& ALT (0) && g_ascii_isalpha (c
))
468 d_key
= g_ascii_tolower (c
);
470 handled
= MSG_NOT_HANDLED
;
471 if (widget_get_options (current
, WOP_WANT_HOTKEY
))
472 handled
= send_message (current
, NULL
, MSG_HOTKEY
, d_key
, NULL
);
474 /* If not used, send hotkey to other widgets */
475 if (handled
== MSG_HANDLED
)
478 hot_cur
= dlg_widget_next (h
, h
->current
);
480 /* send it to all widgets */
481 while (h
->current
!= hot_cur
&& handled
== MSG_NOT_HANDLED
)
483 current
= WIDGET (hot_cur
->data
);
485 if (widget_get_options (current
, WOP_WANT_HOTKEY
)
486 && !widget_get_state (current
, WST_DISABLED
))
487 handled
= send_message (current
, NULL
, MSG_HOTKEY
, d_key
, NULL
);
489 if (handled
== MSG_NOT_HANDLED
)
490 hot_cur
= dlg_widget_next (h
, hot_cur
);
493 if (handled
== MSG_HANDLED
)
494 do_select_widget (h
, hot_cur
, SELECT_EXACT
);
499 /* --------------------------------------------------------------------------------------------- */
502 dlg_key_event (WDialog
* h
, int d_key
)
506 if (h
->widgets
== NULL
)
509 if (h
->current
== NULL
)
510 h
->current
= h
->widgets
;
512 /* TAB used to cycle */
513 if (!widget_get_options (WIDGET (h
), WOP_WANT_TAB
))
520 else if ((d_key
& ~(KEY_M_SHIFT
| KEY_M_CTRL
)) == '\t')
527 /* first can dlg_callback handle the key */
528 handled
= send_message (h
, NULL
, MSG_KEY
, d_key
, NULL
);
530 /* next try the hotkey */
531 if (handled
== MSG_NOT_HANDLED
)
532 handled
= dlg_try_hotkey (h
, d_key
);
534 if (handled
== MSG_HANDLED
)
535 send_message (h
, NULL
, MSG_HOTKEY_HANDLED
, 0, NULL
);
537 /* not used - then try widget_callback */
538 handled
= send_message (h
->current
->data
, NULL
, MSG_KEY
, d_key
, NULL
);
540 /* not used- try to use the unhandled case */
541 if (handled
== MSG_NOT_HANDLED
)
542 handled
= send_message (h
, NULL
, MSG_UNHANDLED_KEY
, d_key
, NULL
);
544 if (handled
== MSG_NOT_HANDLED
)
545 handled
= dlg_handle_key (h
, d_key
);
548 send_message (h
, NULL
, MSG_POST_KEY
, d_key
, NULL
);
551 /* --------------------------------------------------------------------------------------------- */
554 frontend_dlg_run (WDialog
* h
)
556 Widget
*wh
= WIDGET (h
);
561 /* close opened editors, viewers, etc */
562 if (!widget_get_state (wh
, WST_MODAL
) && mc_global
.midnight_shutdown
)
564 send_message (h
, NULL
, MSG_VALIDATE
, 0, NULL
);
568 while (widget_get_state (wh
, WST_ACTIVE
))
572 if (mc_global
.tty
.winch_flag
!= 0)
573 dialog_change_screen_size ();
578 execute_hooks (idle_hook
);
580 while (widget_get_state (wh
, WST_IDLE
) && is_idle ())
581 send_message (wh
, NULL
, MSG_IDLE
, 0, NULL
);
583 /* Allow terminating the dialog from the idle handler */
584 if (!widget_get_state (wh
, WST_ACTIVE
))
590 /* Clear interrupt flag */
591 tty_got_interrupt ();
592 d_key
= tty_get_event (&event
, h
->mouse_status
== MOU_REPEAT
, TRUE
);
594 dlg_process_event (h
, d_key
, &event
);
596 if (widget_get_state (wh
, WST_CLOSED
))
597 send_message (h
, NULL
, MSG_VALIDATE
, 0, NULL
);
601 /* --------------------------------------------------------------------------------------------- */
604 dlg_find_widget_by_id (gconstpointer a
, gconstpointer b
)
606 const Widget
*w
= CONST_WIDGET (a
);
607 unsigned long id
= GPOINTER_TO_UINT (b
);
609 return w
->id
== id
? 0 : 1;
612 /* --------------------------------------------------------------------------------------------- */
615 dlg_set_top_or_bottom_widget (void *w
, gboolean set_top
)
617 Widget
*widget
= WIDGET (w
);
618 WDialog
*h
= widget
->owner
;
621 l
= g_list_find (h
->widgets
, w
);
623 abort (); /* widget is not in dialog, this should not happen */
625 /* unfocus prevoius widget and focus current one before widget reordering */
626 if (set_top
&& widget_get_state (WIDGET (h
), WST_ACTIVE
))
627 do_select_widget (h
, l
, SELECT_EXACT
);
629 /* widget reordering */
630 h
->widgets
= g_list_remove_link (h
->widgets
, l
);
632 h
->widgets
= g_list_concat (h
->widgets
, l
);
634 h
->widgets
= g_list_concat (l
, h
->widgets
);
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
->flags
& DLG_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 tty_setcolor (h
->color
[DLG_COLOR_TITLE
]);
661 widget_move (h
, space
, (wh
->cols
- str_term_width1 (h
->title
)) / 2);
662 tty_print_string (h
->title
);
666 /* --------------------------------------------------------------------------------------------- */
667 /** this function allows to set dialog position */
670 dlg_set_position (WDialog
* h
, int y
, int x
, int lines
, int cols
)
672 Widget
*wh
= WIDGET (h
);
674 /* save old positions, will be used to reposition childs */
676 int shift_x
, shift_y
, scale_x
, scale_y
;
678 /* save old positions, will be used to reposition childs */
689 /* dialog is empty */
690 if (h
->widgets
== NULL
)
693 if (h
->current
== NULL
)
694 h
->current
= h
->widgets
;
696 /* values by which controls should be moved */
697 shift_x
= wh
->x
- ox
;
698 shift_y
= wh
->y
- oy
;
699 scale_x
= wh
->cols
- oc
;
700 scale_y
= wh
->lines
- ol
;
702 if ((shift_x
!= 0) || (shift_y
!= 0) || (scale_x
!= 0) || (scale_y
!= 0))
706 for (w
= h
->widgets
; w
!= NULL
; w
= g_list_next (w
))
708 /* there are, mainly, 2 generally possible
711 1. control sticks to one side - it
714 2. control sticks to two sides of
715 one direction - it should be sized */
717 Widget
*c
= WIDGET (w
->data
);
721 int clines
= c
->lines
;
723 if ((c
->pos_flags
& WPOS_CENTER_HORZ
) != 0)
724 cx
= wh
->x
+ (wh
->cols
- c
->cols
) / 2;
725 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0 && (c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
730 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0)
732 else if ((c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
733 cx
+= shift_x
+ scale_x
;
735 if ((c
->pos_flags
& WPOS_CENTER_VERT
) != 0)
736 cy
= wh
->y
+ (wh
->lines
- c
->lines
) / 2;
737 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0 && (c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
742 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0)
744 else if ((c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
745 cy
+= shift_y
+ scale_y
;
747 widget_set_size (c
, cy
, cx
, clines
, ccols
);
752 /* --------------------------------------------------------------------------------------------- */
753 /** Set dialog size and position */
756 dlg_set_size (WDialog
* h
, int lines
, int cols
)
758 int x
= WIDGET (h
)->x
;
759 int y
= WIDGET (h
)->y
;
761 if ((h
->flags
& DLG_CENTER
) != 0)
763 y
= (LINES
- lines
) / 2;
764 x
= (COLS
- cols
) / 2;
767 if ((h
->flags
& DLG_TRYUP
) != 0)
775 dlg_set_position (h
, y
, x
, lines
, cols
);
778 /* --------------------------------------------------------------------------------------------- */
779 /** Default dialog callback */
782 dlg_default_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
784 WDialog
*h
= DIALOG (w
);
793 if (h
->color
!= NULL
)
795 dlg_default_repaint (h
);
798 return MSG_NOT_HANDLED
;
801 /* we don't want endless loop */
802 widget_idle (w
, FALSE
);
806 /* this is default resizing mechanism */
807 /* the main idea of this code is to resize dialog
808 according to flags (if any of flags require automatic
809 resizing, like DLG_CENTER, end after that reposition
810 controls in dialog according to flags of widget) */
811 dlg_set_size (h
, w
->lines
, w
->cols
);
818 return MSG_NOT_HANDLED
;
821 /* --------------------------------------------------------------------------------------------- */
824 dlg_create (gboolean modal
, int y1
, int x1
, int lines
, int cols
,
825 const int *colors
, widget_cb_fn callback
, widget_mouse_cb_fn mouse_callback
,
826 const char *help_ctx
, const char *title
, dlg_flags_t flags
)
831 new_d
= g_new0 (WDialog
, 1);
833 widget_init (w
, y1
, x1
, lines
, cols
, (callback
!= NULL
) ? callback
: dlg_default_callback
,
835 w
->options
|= WOP_TOP_SELECT
;
837 w
->state
|= WST_CONSTRUCT
;
839 w
->state
|= WST_MODAL
;
841 new_d
->color
= colors
;
842 new_d
->help_ctx
= help_ctx
;
843 new_d
->flags
= flags
;
846 dlg_set_size (new_d
, lines
, cols
);
847 new_d
->fullscreen
= (w
->x
== 0 && w
->y
== 0 && w
->cols
== COLS
&& w
->lines
== LINES
);
849 new_d
->mouse_status
= MOU_UNHANDLED
;
851 /* Strip existing spaces, add one space before and after the title */
852 if (title
!= NULL
&& *title
!= '\0')
856 t
= g_strstrip (g_strdup (title
));
858 new_d
->title
= g_strdup_printf (" %s ", t
);
862 /* unique name of event group for this dialog */
863 new_d
->event_group
= g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG
, (void *) new_d
);
868 /* --------------------------------------------------------------------------------------------- */
871 dlg_set_default_colors (void)
873 dialog_colors
[DLG_COLOR_NORMAL
] = COLOR_NORMAL
;
874 dialog_colors
[DLG_COLOR_FOCUS
] = COLOR_FOCUS
;
875 dialog_colors
[DLG_COLOR_HOT_NORMAL
] = COLOR_HOT_NORMAL
;
876 dialog_colors
[DLG_COLOR_HOT_FOCUS
] = COLOR_HOT_FOCUS
;
877 dialog_colors
[DLG_COLOR_TITLE
] = COLOR_TITLE
;
879 alarm_colors
[DLG_COLOR_NORMAL
] = ERROR_COLOR
;
880 alarm_colors
[DLG_COLOR_FOCUS
] = ERROR_FOCUS
;
881 alarm_colors
[DLG_COLOR_HOT_NORMAL
] = ERROR_HOT_NORMAL
;
882 alarm_colors
[DLG_COLOR_HOT_FOCUS
] = ERROR_HOT_FOCUS
;
883 alarm_colors
[DLG_COLOR_TITLE
] = ERROR_TITLE
;
885 listbox_colors
[DLG_COLOR_NORMAL
] = PMENU_ENTRY_COLOR
;
886 listbox_colors
[DLG_COLOR_FOCUS
] = PMENU_SELECTED_COLOR
;
887 listbox_colors
[DLG_COLOR_HOT_NORMAL
] = PMENU_ENTRY_COLOR
;
888 listbox_colors
[DLG_COLOR_HOT_FOCUS
] = PMENU_SELECTED_COLOR
;
889 listbox_colors
[DLG_COLOR_TITLE
] = PMENU_TITLE_COLOR
;
892 /* --------------------------------------------------------------------------------------------- */
895 dlg_erase (WDialog
* h
)
897 Widget
*wh
= WIDGET (h
);
899 if (wh
!= NULL
&& widget_get_state (wh
, WST_ACTIVE
))
900 tty_fill_region (wh
->y
, wh
->x
, wh
->lines
, wh
->cols
, ' ');
903 /* --------------------------------------------------------------------------------------------- */
905 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
909 add_widget_autopos (WDialog
* h
, void *w
, widget_pos_flags_t pos_flags
, const void *before
)
911 Widget
*wh
= WIDGET (h
);
914 /* Don't accept 0 widgets */
920 if ((pos_flags
& WPOS_CENTER_HORZ
) != 0)
921 widget
->x
= (wh
->cols
- widget
->cols
) / 2;
924 if ((pos_flags
& WPOS_CENTER_VERT
) != 0)
925 widget
->y
= (wh
->lines
- widget
->lines
) / 2;
929 widget
->pos_flags
= pos_flags
;
930 widget
->id
= h
->widget_id
++;
932 if (h
->widgets
== NULL
|| before
== NULL
)
934 h
->widgets
= g_list_append (h
->widgets
, widget
);
935 h
->current
= g_list_last (h
->widgets
);
941 b
= g_list_find (h
->widgets
, before
);
943 /* don't accept widget not from dialog. This shouldn't happen */
948 h
->widgets
= g_list_insert_before (h
->widgets
, b
, widget
);
950 h
->current
= g_list_previous (b
);
952 h
->current
= g_list_last (h
->widgets
);
955 /* widget has been added in runtime */
956 if (widget_get_state (wh
, WST_ACTIVE
))
958 send_message (widget
, NULL
, MSG_INIT
, 0, NULL
);
959 send_message (widget
, NULL
, MSG_DRAW
, 0, NULL
);
960 send_message (widget
, NULL
, MSG_FOCUS
, 0, NULL
);
966 /* --------------------------------------------------------------------------------------------- */
967 /** wrapper to simply add lefttop positioned controls */
970 add_widget (WDialog
* h
, void *w
)
972 return add_widget_autopos (h
, w
, WPOS_KEEP_DEFAULT
,
973 h
->current
!= NULL
? h
->current
->data
: NULL
);
976 /* --------------------------------------------------------------------------------------------- */
979 add_widget_before (WDialog
* h
, void *w
, void *before
)
981 return add_widget_autopos (h
, w
, WPOS_KEEP_DEFAULT
, before
);
984 /* --------------------------------------------------------------------------------------------- */
986 /** delete widget from dialog */
993 /* Don't accept NULL widget. This shouldn't happen */
997 h
= WIDGET (w
)->owner
;
999 d
= g_list_find (h
->widgets
, w
);
1000 if (d
== h
->current
)
1001 h
->current
= dlg_widget_next (h
, d
);
1003 h
->widgets
= g_list_remove_link (h
->widgets
, d
);
1004 send_message (d
->data
, NULL
, MSG_DESTROY
, 0, NULL
);
1008 /* widget has been deleted in runtime */
1009 if (widget_get_state (WIDGET (h
), WST_ACTIVE
))
1016 /* --------------------------------------------------------------------------------------------- */
1025 if ((d
!= NULL
) && (d
->data
!= NULL
))
1026 dlg_redraw (DIALOG (d
->data
));
1030 /* Search first fullscreen dialog */
1031 for (; d
!= NULL
; d
= g_list_next (d
))
1032 if (d
->data
!= NULL
&& DIALOG (d
->data
)->fullscreen
)
1034 /* back to top dialog */
1035 for (; d
!= NULL
; d
= g_list_previous (d
))
1036 if (d
->data
!= NULL
)
1037 dlg_redraw (DIALOG (d
->data
));
1041 /* --------------------------------------------------------------------------------------------- */
1042 /** broadcast a message to all the widgets in a dialog */
1045 dlg_broadcast_msg (WDialog
* h
, widget_msg_t msg
)
1047 dlg_broadcast_msg_to (h
, msg
, FALSE
, 0);
1050 /* --------------------------------------------------------------------------------------------- */
1053 dlg_focus (WDialog
* h
)
1055 /* cannot focus disabled widget */
1056 if (h
->current
!= NULL
)
1058 Widget
*wh
= WIDGET (h
);
1060 if (widget_get_state (wh
, WST_CONSTRUCT
) || widget_get_state (wh
, WST_ACTIVE
))
1062 Widget
*current
= WIDGET (h
->current
->data
);
1064 if (!widget_get_state (current
, WST_DISABLED
)
1065 && (send_message (current
, NULL
, MSG_FOCUS
, 0, NULL
) == MSG_HANDLED
))
1067 send_message (h
, current
, MSG_FOCUS
, 0, NULL
);
1076 /* --------------------------------------------------------------------------------------------- */
1077 /** Find the widget with the given callback in the dialog h */
1080 find_widget_type (const WDialog
* h
, widget_cb_fn callback
)
1084 w
= g_list_find_custom (h
->widgets
, (gconstpointer
) callback
, dlg_find_widget_callback
);
1086 return (w
== NULL
) ? NULL
: WIDGET (w
->data
);
1089 /* --------------------------------------------------------------------------------------------- */
1090 /** Find the widget with the given id */
1093 dlg_find_by_id (const WDialog
* h
, unsigned long id
)
1097 w
= g_list_find_custom (h
->widgets
, GUINT_TO_POINTER (id
), dlg_find_widget_by_id
);
1098 return w
!= NULL
? WIDGET (w
->data
) : NULL
;
1101 /* --------------------------------------------------------------------------------------------- */
1102 /** Find the widget with the given id in the dialog h and select it */
1105 dlg_select_by_id (const WDialog
* h
, unsigned long id
)
1109 w
= dlg_find_by_id (h
, id
);
1111 dlg_select_widget (w
);
1114 /* --------------------------------------------------------------------------------------------- */
1116 * Try to select widget in the dialog.
1120 dlg_select_widget (void *w
)
1122 Widget
*widget
= WIDGET (w
);
1123 WDialog
*h
= widget
->owner
;
1125 if (widget_get_options (widget
, WOP_TOP_SELECT
))
1126 dlg_set_top_or_bottom_widget (w
, TRUE
);
1128 do_select_widget (h
, g_list_find (h
->widgets
, widget
), SELECT_EXACT
);
1131 /* --------------------------------------------------------------------------------------------- */
1133 * Set widget at bottom of widget list.
1137 dlg_set_bottom_widget (void *w
)
1139 dlg_set_top_or_bottom_widget (w
, FALSE
);
1142 /* --------------------------------------------------------------------------------------------- */
1143 /** Try to select previous widget in the tab order */
1146 dlg_one_up (WDialog
* h
)
1148 if (h
->widgets
!= NULL
)
1149 do_select_widget (h
, dlg_widget_prev (h
, h
->current
), SELECT_PREV
);
1152 /* --------------------------------------------------------------------------------------------- */
1153 /** Try to select next widget in the tab order */
1156 dlg_one_down (WDialog
* h
)
1158 if (h
->widgets
!= NULL
)
1159 do_select_widget (h
, dlg_widget_next (h
, h
->current
), SELECT_NEXT
);
1162 /* --------------------------------------------------------------------------------------------- */
1165 update_cursor (WDialog
* h
)
1167 GList
*p
= h
->current
;
1169 if (p
!= NULL
&& widget_get_state (WIDGET (h
), WST_ACTIVE
))
1173 w
= WIDGET (p
->data
);
1175 if (!widget_get_state (w
, WST_DISABLED
) && widget_get_options (w
, WOP_WANT_CURSOR
))
1176 send_message (w
, NULL
, MSG_CURSOR
, 0, NULL
);
1180 p
= dlg_widget_next (h
, p
);
1181 if (p
== h
->current
)
1184 w
= WIDGET (p
->data
);
1186 if (!widget_get_state (w
, WST_DISABLED
) && widget_get_options (w
, WOP_WANT_CURSOR
)
1187 && send_message (w
, NULL
, MSG_CURSOR
, 0, NULL
) == MSG_HANDLED
)
1194 /* --------------------------------------------------------------------------------------------- */
1196 * Redraw the widgets in reverse order, leaving the current widget
1201 dlg_redraw (WDialog
* h
)
1203 if (!widget_get_state (WIDGET (h
), WST_ACTIVE
))
1206 if (h
->winch_pending
)
1208 h
->winch_pending
= FALSE
;
1209 send_message (h
, NULL
, MSG_RESIZE
, 0, NULL
);
1212 send_message (h
, NULL
, MSG_DRAW
, 0, NULL
);
1213 dlg_broadcast_msg (h
, MSG_DRAW
);
1217 /* --------------------------------------------------------------------------------------------- */
1220 dlg_stop (WDialog
* h
)
1222 widget_set_state (WIDGET (h
), WST_CLOSED
, TRUE
);
1225 /* --------------------------------------------------------------------------------------------- */
1226 /** Init the process */
1229 dlg_init (WDialog
* h
)
1231 Widget
*wh
= WIDGET (h
);
1233 if (top_dlg
!= NULL
&& widget_get_state (WIDGET (top_dlg
->data
), WST_MODAL
))
1234 widget_set_state (wh
, WST_MODAL
, TRUE
);
1236 /* add dialog to the stack */
1237 top_dlg
= g_list_prepend (top_dlg
, h
);
1239 /* Initialize dialog manager and widgets */
1240 if (widget_get_state (wh
, WST_CONSTRUCT
))
1242 if (!widget_get_state (wh
, WST_MODAL
))
1243 dialog_switch_add (h
);
1245 send_message (h
, NULL
, MSG_INIT
, 0, NULL
);
1246 dlg_broadcast_msg (h
, MSG_INIT
);
1247 dlg_read_history (h
);
1250 widget_set_state (wh
, WST_ACTIVE
, TRUE
);
1252 /* first send MSG_DRAW to dialog itself and all widgets... */
1255 /* ...then send MSG_FOCUS to select the first widget that can take focus */
1256 while (h
->current
!= NULL
&& !dlg_focus (h
))
1257 h
->current
= dlg_widget_next (h
, h
->current
);
1263 /* --------------------------------------------------------------------------------------------- */
1266 dlg_process_event (WDialog
* h
, int key
, Gpm_Event
* event
)
1270 if (tty_got_interrupt ())
1271 if (send_message (h
, NULL
, MSG_ACTION
, CK_Cancel
, NULL
) != MSG_HANDLED
)
1272 dlg_execute_cmd (h
, CK_Cancel
);
1274 else if (key
== EV_MOUSE
)
1275 h
->mouse_status
= dlg_mouse_event (h
, event
);
1277 dlg_key_event (h
, key
);
1280 /* --------------------------------------------------------------------------------------------- */
1281 /** Shutdown the dlg_run */
1284 dlg_run_done (WDialog
* h
)
1286 top_dlg
= g_list_remove (top_dlg
, h
);
1288 if (widget_get_state (WIDGET (h
), WST_CLOSED
))
1290 send_message (h
, h
->current
->data
, MSG_END
, 0, NULL
);
1291 if (!widget_get_state (WIDGET (h
), WST_MODAL
))
1292 dialog_switch_remove (h
);
1296 /* --------------------------------------------------------------------------------------------- */
1298 * Standard run dialog routine
1299 * We have to keep this routine small so that we can duplicate it's
1300 * behavior on complex routines like the file routines, this way,
1301 * they can call the dlg_process_event without rewriting all the code
1305 dlg_run (WDialog
* h
)
1308 frontend_dlg_run (h
);
1310 return h
->ret_value
;
1313 /* --------------------------------------------------------------------------------------------- */
1316 dlg_destroy (WDialog
* h
)
1318 /* if some widgets have history, save all history at one moment here */
1319 dlg_save_history (h
);
1320 dlg_broadcast_msg (h
, MSG_DESTROY
);
1321 g_list_free_full (h
->widgets
, g_free
);
1322 mc_event_group_del (h
->event_group
);
1323 g_free (h
->event_group
);
1330 /* --------------------------------------------------------------------------------------------- */
1333 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1336 dlg_save_history (WDialog
* h
)
1341 if (num_history_items_recorded
== 0) /* this is how to disable */
1344 profile
= mc_config_get_full_path (MC_HISTORY_FILE
);
1345 i
= open (profile
, O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
);
1349 /* Make sure the history is only readable by the user */
1350 if (chmod (profile
, S_IRUSR
| S_IWUSR
) != -1 || errno
== ENOENT
)
1352 ev_history_load_save_t event_data
;
1354 event_data
.cfg
= mc_config_init (profile
, FALSE
);
1355 event_data
.receiver
= NULL
;
1357 /* get all histories in dialog */
1358 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_SAVE
, &event_data
);
1360 mc_config_save_file (event_data
.cfg
, NULL
);
1361 mc_config_deinit (event_data
.cfg
);
1367 /* --------------------------------------------------------------------------------------------- */
1370 dlg_get_title (const WDialog
* h
, size_t len
)
1377 if (h
->get_title
!= NULL
)
1378 t
= h
->get_title (h
, len
);
1385 /* --------------------------------------------------------------------------------------------- */