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
, int 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
) && (h
->state
== DLG_CONSTRUCT
|| h
->state
== DLG_ACTIVE
))
187 Widget
*current
= WIDGET (h
->current
->data
);
189 if (send_message (current
, NULL
, MSG_UNFOCUS
, 0, NULL
) == MSG_HANDLED
)
191 send_message (h
, current
, MSG_UNFOCUS
, 0, NULL
);
199 /* --------------------------------------------------------------------------------------------- */
202 dlg_find_widget_callback (const void *a
, const void *b
)
204 const Widget
*w
= WIDGET (a
);
205 widget_cb_fn f
= (widget_cb_fn
) b
;
207 return (w
->callback
== f
) ? 0 : 1;
210 /* --------------------------------------------------------------------------------------------- */
212 * Try to select another widget. If forward is set, follow tab order.
213 * Otherwise go to the previous widget.
217 do_select_widget (WDialog
* h
, GList
* w
, select_dir_t dir
)
219 Widget
*w0
= WIDGET (h
->current
->data
);
221 if (!dlg_unfocus (h
))
234 h
->current
= g_list_find (h
->widgets
, w0
);
237 /* try find another widget that can take focus */
241 h
->current
= dlg_widget_next (h
, h
->current
);
244 h
->current
= dlg_widget_prev (h
, h
->current
);
250 while (h
->current
!= w
/* && (WIDGET (h->current->data)->options & W_DISABLED) == 0 */ );
252 if (widget_overlapped (w0
, WIDGET (h
->current
->data
)))
254 send_message (h
->current
->data
, NULL
, MSG_DRAW
, 0, NULL
);
255 send_message (h
->current
->data
, NULL
, MSG_FOCUS
, 0, NULL
);
259 /* --------------------------------------------------------------------------------------------- */
268 /* Use this if the refreshes fail */
271 #endif /* HAVE_SLANG */
274 /* --------------------------------------------------------------------------------------------- */
277 dlg_execute_cmd (WDialog
* h
, long command
)
279 cb_ret_t ret
= MSG_HANDLED
;
283 h
->ret_value
= B_ENTER
;
287 h
->ret_value
= B_CANCEL
;
302 ev_help_t event_data
= { NULL
, h
->help_ctx
};
303 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
308 mc_event_raise (MCEVENT_GROUP_CORE
, "suspend", NULL
);
317 dialog_switch_list ();
319 ret
= MSG_NOT_HANDLED
;
323 dialog_switch_next ();
325 ret
= MSG_NOT_HANDLED
;
329 dialog_switch_prev ();
331 ret
= MSG_NOT_HANDLED
;
335 ret
= MSG_NOT_HANDLED
;
341 /* --------------------------------------------------------------------------------------------- */
344 dlg_handle_key (WDialog
* h
, int d_key
)
348 command
= keybind_lookup_keymap_command (dialog_map
, d_key
);
350 if (command
== CK_IgnoreKey
)
351 return MSG_NOT_HANDLED
;
353 if (send_message (h
, NULL
, MSG_ACTION
, command
, NULL
) == MSG_HANDLED
354 || dlg_execute_cmd (h
, command
) == MSG_HANDLED
)
357 return MSG_NOT_HANDLED
;
360 /* --------------------------------------------------------------------------------------------- */
362 * This is the low-level mouse handler.
363 * It receives a Gpm_Event event and translates it into a higher level protocol.
366 dlg_mouse_translator (Gpm_Event
* event
, Widget
* w
)
371 me
= mouse_translate_event (w
, event
, &run_click
);
373 return mouse_process_event (w
, &me
, run_click
);
376 /* --------------------------------------------------------------------------------------------- */
379 dlg_mouse_event (WDialog
* h
, Gpm_Event
* event
)
381 Widget
*wh
= WIDGET (h
);
385 /* close the dialog by mouse left click out of dialog area */
386 if (mouse_close_dialog
&& !h
->fullscreen
&& ((event
->buttons
& GPM_B_LEFT
) != 0)
387 && ((event
->type
& GPM_DOWN
) != 0) && !mouse_global_in_widget (event
, wh
))
389 h
->ret_value
= B_CANCEL
;
394 if (wh
->mouse_callback
!= NULL
)
398 mou
= dlg_mouse_translator (event
, wh
);
399 if (mou
!= MOU_UNHANDLED
)
403 /* send the event to widgets in reverse Z-order */
404 p
= g_list_last (h
->widgets
);
407 Widget
*w
= WIDGET (p
->data
);
409 if ((w
->options
& W_DISABLED
) == 0 && w
->mouse_callback
!= NULL
)
411 /* put global cursor position to the widget */
414 ret
= dlg_mouse_translator (event
, w
);
415 if (ret
!= MOU_UNHANDLED
)
419 p
= g_list_previous (p
);
423 return MOU_UNHANDLED
;
426 /* --------------------------------------------------------------------------------------------- */
429 dlg_try_hotkey (WDialog
* h
, int d_key
)
436 if (h
->widgets
== NULL
)
437 return MSG_NOT_HANDLED
;
439 if (h
->current
== NULL
)
440 h
->current
= h
->widgets
;
443 * Explanation: we don't send letter hotkeys to other widgets if
444 * the currently selected widget is an input line
447 current
= WIDGET (h
->current
->data
);
449 if ((current
->options
& W_DISABLED
) != 0)
450 return MSG_NOT_HANDLED
;
452 if (current
->options
& W_IS_INPUT
)
454 /* skip ascii control characters, anything else can valid character in
456 if (d_key
>= 32 && d_key
< 256)
457 return MSG_NOT_HANDLED
;
460 /* If it's an alt key, send the message */
461 c
= d_key
& ~ALT (0);
462 if (d_key
& ALT (0) && g_ascii_isalpha (c
))
463 d_key
= g_ascii_tolower (c
);
465 handled
= MSG_NOT_HANDLED
;
466 if ((current
->options
& W_WANT_HOTKEY
) != 0)
467 handled
= send_message (current
, NULL
, MSG_HOTKEY
, d_key
, NULL
);
469 /* If not used, send hotkey to other widgets */
470 if (handled
== MSG_HANDLED
)
473 hot_cur
= dlg_widget_next (h
, h
->current
);
475 /* send it to all widgets */
476 while (h
->current
!= hot_cur
&& handled
== MSG_NOT_HANDLED
)
478 current
= WIDGET (hot_cur
->data
);
480 if ((current
->options
& W_WANT_HOTKEY
) != 0 && (current
->options
& W_DISABLED
) == 0)
481 handled
= send_message (current
, NULL
, MSG_HOTKEY
, d_key
, NULL
);
483 if (handled
== MSG_NOT_HANDLED
)
484 hot_cur
= dlg_widget_next (h
, hot_cur
);
487 if (handled
== MSG_HANDLED
)
488 do_select_widget (h
, hot_cur
, SELECT_EXACT
);
493 /* --------------------------------------------------------------------------------------------- */
496 dlg_key_event (WDialog
* h
, int d_key
)
500 if (h
->widgets
== NULL
)
503 if (h
->current
== NULL
)
504 h
->current
= h
->widgets
;
506 /* TAB used to cycle */
507 if ((h
->flags
& DLG_WANT_TAB
) == 0)
514 else if ((d_key
& ~(KEY_M_SHIFT
| KEY_M_CTRL
)) == '\t')
521 /* first can dlg_callback handle the key */
522 handled
= send_message (h
, NULL
, MSG_KEY
, d_key
, NULL
);
524 /* next try the hotkey */
525 if (handled
== MSG_NOT_HANDLED
)
526 handled
= dlg_try_hotkey (h
, d_key
);
528 if (handled
== MSG_HANDLED
)
529 send_message (h
, NULL
, MSG_HOTKEY_HANDLED
, 0, NULL
);
531 /* not used - then try widget_callback */
532 handled
= send_message (h
->current
->data
, NULL
, MSG_KEY
, d_key
, NULL
);
534 /* not used- try to use the unhandled case */
535 if (handled
== MSG_NOT_HANDLED
)
536 handled
= send_message (h
, NULL
, MSG_UNHANDLED_KEY
, d_key
, NULL
);
538 if (handled
== MSG_NOT_HANDLED
)
539 handled
= dlg_handle_key (h
, d_key
);
542 send_message (h
, NULL
, MSG_POST_KEY
, d_key
, NULL
);
545 /* --------------------------------------------------------------------------------------------- */
548 frontend_dlg_run (WDialog
* h
)
554 /* close opened editors, viewers, etc */
555 if (!h
->modal
&& mc_global
.midnight_shutdown
)
557 send_message (h
, NULL
, MSG_VALIDATE
, 0, NULL
);
561 while (h
->state
== DLG_ACTIVE
)
565 if (mc_global
.tty
.winch_flag
!= 0)
566 dialog_change_screen_size ();
571 execute_hooks (idle_hook
);
573 while ((WIDGET (h
)->options
& W_WANT_IDLE
) != 0 && is_idle ())
574 send_message (h
, NULL
, MSG_IDLE
, 0, NULL
);
576 /* Allow terminating the dialog from the idle handler */
577 if (h
->state
!= DLG_ACTIVE
)
583 /* Clear interrupt flag */
584 tty_got_interrupt ();
585 d_key
= tty_get_event (&event
, h
->mouse_status
== MOU_REPEAT
, TRUE
);
587 dlg_process_event (h
, d_key
, &event
);
589 if (h
->state
== DLG_CLOSED
)
590 send_message (h
, NULL
, MSG_VALIDATE
, 0, NULL
);
594 /* --------------------------------------------------------------------------------------------- */
597 dlg_find_widget_by_id (gconstpointer a
, gconstpointer b
)
599 Widget
*w
= WIDGET (a
);
600 unsigned long id
= GPOINTER_TO_UINT (b
);
602 return w
->id
== id
? 0 : 1;
605 /* --------------------------------------------------------------------------------------------- */
606 /*** public functions ****************************************************************************/
607 /* --------------------------------------------------------------------------------------------- */
609 /** Clean the dialog area, draw the frame and the title */
611 dlg_default_repaint (WDialog
* h
)
613 Widget
*wh
= WIDGET (h
);
617 if (h
->state
!= DLG_ACTIVE
)
620 space
= (h
->flags
& DLG_COMPACT
) ? 0 : 1;
622 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
624 tty_draw_box (wh
->y
+ space
, wh
->x
+ space
, wh
->lines
- 2 * space
, wh
->cols
- 2 * space
, FALSE
);
626 if (h
->title
!= NULL
)
628 tty_setcolor (h
->color
[DLG_COLOR_TITLE
]);
629 widget_move (h
, space
, (wh
->cols
- str_term_width1 (h
->title
)) / 2);
630 tty_print_string (h
->title
);
634 /* --------------------------------------------------------------------------------------------- */
635 /** this function allows to set dialog position */
638 dlg_set_position (WDialog
* h
, int y1
, int x1
, int y2
, int x2
)
640 Widget
*wh
= WIDGET (h
);
642 /* save old positions, will be used to reposition childs */
644 int shift_x
, shift_y
, scale_x
, scale_y
;
646 /* save old positions, will be used to reposition childs */
657 /* dialog is empty */
658 if (h
->widgets
== NULL
)
661 if (h
->current
== NULL
)
662 h
->current
= h
->widgets
;
664 /* values by which controls should be moved */
665 shift_x
= wh
->x
- ox
;
666 shift_y
= wh
->y
- oy
;
667 scale_x
= wh
->cols
- oc
;
668 scale_y
= wh
->lines
- ol
;
670 if ((shift_x
!= 0) || (shift_y
!= 0) || (scale_x
!= 0) || (scale_y
!= 0))
674 for (w
= h
->widgets
; w
!= NULL
; w
= g_list_next (w
))
676 /* there are, mainly, 2 generally possible
679 1. control sticks to one side - it
682 2. control sticks to two sides of
683 one direction - it should be sized */
685 Widget
*c
= WIDGET (w
->data
);
689 int lines
= c
->lines
;
691 if ((c
->pos_flags
& WPOS_CENTER_HORZ
) != 0)
692 x
= wh
->x
+ (wh
->cols
- c
->cols
) / 2;
693 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0 && (c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
698 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0)
700 else if ((c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
701 x
+= shift_x
+ scale_x
;
703 if ((c
->pos_flags
& WPOS_CENTER_VERT
) != 0)
704 y
= wh
->y
+ (wh
->lines
- c
->lines
) / 2;
705 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0 && (c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
710 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0)
712 else if ((c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
713 y
+= shift_y
+ scale_y
;
715 widget_set_size (c
, y
, x
, lines
, cols
);
720 /* --------------------------------------------------------------------------------------------- */
721 /** Set dialog size and position */
724 dlg_set_size (WDialog
* h
, int lines
, int cols
)
726 int x
= WIDGET (h
)->x
;
727 int y
= WIDGET (h
)->y
;
729 if ((h
->flags
& DLG_CENTER
) != 0)
731 y
= (LINES
- lines
) / 2;
732 x
= (COLS
- cols
) / 2;
735 if ((h
->flags
& DLG_TRYUP
) != 0)
743 dlg_set_position (h
, y
, x
, y
+ lines
, x
+ cols
);
746 /* --------------------------------------------------------------------------------------------- */
747 /** Default dialog callback */
750 dlg_default_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
752 WDialog
*h
= DIALOG (w
);
761 if (h
->color
!= NULL
)
763 dlg_default_repaint (h
);
766 return MSG_NOT_HANDLED
;
769 dlg_broadcast_msg_to (h
, MSG_IDLE
, FALSE
, W_WANT_IDLE
);
773 /* this is default resizing mechanism */
774 /* the main idea of this code is to resize dialog
775 according to flags (if any of flags require automatic
776 resizing, like DLG_CENTER, end after that reposition
777 controls in dialog according to flags of widget) */
778 dlg_set_size (h
, w
->lines
, w
->cols
);
785 return MSG_NOT_HANDLED
;
788 /* --------------------------------------------------------------------------------------------- */
791 dlg_create (gboolean modal
, int y1
, int x1
, int lines
, int cols
,
792 const int *colors
, widget_cb_fn callback
, widget_mouse_cb_fn mouse_callback
,
793 const char *help_ctx
, const char *title
, dlg_flags_t flags
)
798 new_d
= g_new0 (WDialog
, 1);
800 widget_init (w
, y1
, x1
, lines
, cols
, (callback
!= NULL
) ? callback
: dlg_default_callback
,
802 widget_want_cursor (w
, FALSE
);
804 new_d
->state
= DLG_CONSTRUCT
;
805 new_d
->modal
= modal
;
806 new_d
->color
= colors
;
807 new_d
->help_ctx
= help_ctx
;
808 new_d
->flags
= flags
;
811 dlg_set_size (new_d
, lines
, cols
);
812 new_d
->fullscreen
= (w
->x
== 0 && w
->y
== 0 && w
->cols
== COLS
&& w
->lines
== LINES
);
814 new_d
->mouse_status
= MOU_UNHANDLED
;
816 /* Strip existing spaces, add one space before and after the title */
817 if (title
!= NULL
&& *title
!= '\0')
821 t
= g_strstrip (g_strdup (title
));
823 new_d
->title
= g_strdup_printf (" %s ", t
);
827 /* unique name of event group for this dialog */
828 new_d
->event_group
= g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG
, (void *) new_d
);
833 /* --------------------------------------------------------------------------------------------- */
836 dlg_set_default_colors (void)
838 dialog_colors
[DLG_COLOR_NORMAL
] = COLOR_NORMAL
;
839 dialog_colors
[DLG_COLOR_FOCUS
] = COLOR_FOCUS
;
840 dialog_colors
[DLG_COLOR_HOT_NORMAL
] = COLOR_HOT_NORMAL
;
841 dialog_colors
[DLG_COLOR_HOT_FOCUS
] = COLOR_HOT_FOCUS
;
842 dialog_colors
[DLG_COLOR_TITLE
] = COLOR_TITLE
;
844 alarm_colors
[DLG_COLOR_NORMAL
] = ERROR_COLOR
;
845 alarm_colors
[DLG_COLOR_FOCUS
] = ERROR_FOCUS
;
846 alarm_colors
[DLG_COLOR_HOT_NORMAL
] = ERROR_HOT_NORMAL
;
847 alarm_colors
[DLG_COLOR_HOT_FOCUS
] = ERROR_HOT_FOCUS
;
848 alarm_colors
[DLG_COLOR_TITLE
] = ERROR_TITLE
;
850 listbox_colors
[DLG_COLOR_NORMAL
] = PMENU_ENTRY_COLOR
;
851 listbox_colors
[DLG_COLOR_FOCUS
] = PMENU_SELECTED_COLOR
;
852 listbox_colors
[DLG_COLOR_HOT_NORMAL
] = PMENU_ENTRY_COLOR
;
853 listbox_colors
[DLG_COLOR_HOT_FOCUS
] = PMENU_SELECTED_COLOR
;
854 listbox_colors
[DLG_COLOR_TITLE
] = PMENU_TITLE_COLOR
;
857 /* --------------------------------------------------------------------------------------------- */
860 dlg_erase (WDialog
* h
)
862 if ((h
!= NULL
) && (h
->state
== DLG_ACTIVE
))
864 Widget
*wh
= WIDGET (h
);
866 tty_fill_region (wh
->y
, wh
->x
, wh
->lines
, wh
->cols
, ' ');
870 /* --------------------------------------------------------------------------------------------- */
872 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
876 add_widget_autopos (WDialog
* h
, void *w
, widget_pos_flags_t pos_flags
, const void *before
)
878 Widget
*wh
= WIDGET (h
);
881 /* Don't accept 0 widgets */
887 if ((pos_flags
& WPOS_CENTER_HORZ
) != 0)
888 widget
->x
= (wh
->cols
- widget
->cols
) / 2;
891 if ((pos_flags
& WPOS_CENTER_VERT
) != 0)
892 widget
->y
= (wh
->lines
- widget
->lines
) / 2;
896 widget
->pos_flags
= pos_flags
;
897 widget
->id
= h
->widget_id
++;
899 if (h
->widgets
== NULL
|| before
== NULL
)
901 h
->widgets
= g_list_append (h
->widgets
, widget
);
902 h
->current
= g_list_last (h
->widgets
);
908 b
= g_list_find (h
->widgets
, before
);
910 /* don't accept widget not from dialog. This shouldn't happen */
915 h
->widgets
= g_list_insert_before (h
->widgets
, b
, widget
);
917 h
->current
= g_list_previous (b
);
919 h
->current
= g_list_last (h
->widgets
);
922 /* widget has been added in runtime */
923 if (h
->state
== DLG_ACTIVE
)
925 send_message (widget
, NULL
, MSG_INIT
, 0, NULL
);
926 send_message (widget
, NULL
, MSG_DRAW
, 0, NULL
);
927 send_message (widget
, NULL
, MSG_FOCUS
, 0, NULL
);
933 /* --------------------------------------------------------------------------------------------- */
934 /** wrapper to simply add lefttop positioned controls */
937 add_widget (WDialog
* h
, void *w
)
939 return add_widget_autopos (h
, w
, WPOS_KEEP_DEFAULT
,
940 h
->current
!= NULL
? h
->current
->data
: NULL
);
943 /* --------------------------------------------------------------------------------------------- */
946 add_widget_before (WDialog
* h
, void *w
, void *before
)
948 return add_widget_autopos (h
, w
, WPOS_KEEP_DEFAULT
, before
);
951 /* --------------------------------------------------------------------------------------------- */
953 /** delete widget from dialog */
960 /* Don't accept NULL widget. This shouldn't happen */
964 h
= WIDGET (w
)->owner
;
966 d
= g_list_find (h
->widgets
, w
);
968 h
->current
= dlg_widget_next (h
, d
);
970 h
->widgets
= g_list_remove_link (h
->widgets
, d
);
971 send_message (d
->data
, NULL
, MSG_DESTROY
, 0, NULL
);
975 /* widget has been deleted in runtime */
976 if (h
->state
== DLG_ACTIVE
)
983 /* --------------------------------------------------------------------------------------------- */
992 if ((d
!= NULL
) && (d
->data
!= NULL
))
993 dlg_redraw (DIALOG (d
->data
));
997 /* Search first fullscreen dialog */
998 for (; d
!= NULL
; d
= g_list_next (d
))
999 if (d
->data
!= NULL
&& DIALOG (d
->data
)->fullscreen
)
1001 /* back to top dialog */
1002 for (; d
!= NULL
; d
= g_list_previous (d
))
1003 if (d
->data
!= NULL
)
1004 dlg_redraw (DIALOG (d
->data
));
1008 /* --------------------------------------------------------------------------------------------- */
1009 /** broadcast a message to all the widgets in a dialog */
1012 dlg_broadcast_msg (WDialog
* h
, widget_msg_t msg
)
1014 dlg_broadcast_msg_to (h
, msg
, FALSE
, 0);
1017 /* --------------------------------------------------------------------------------------------- */
1020 dlg_focus (WDialog
* h
)
1022 /* cannot focus disabled widget */
1023 if ((h
->current
!= NULL
) && (h
->state
== DLG_CONSTRUCT
|| h
->state
== DLG_ACTIVE
))
1025 Widget
*current
= WIDGET (h
->current
->data
);
1027 if (((current
->options
& W_DISABLED
) == 0)
1028 && (send_message (current
, NULL
, MSG_FOCUS
, 0, NULL
) == MSG_HANDLED
))
1030 send_message (h
, current
, MSG_FOCUS
, 0, NULL
);
1038 /* --------------------------------------------------------------------------------------------- */
1039 /** Find the widget with the given callback in the dialog h */
1042 find_widget_type (const WDialog
* h
, widget_cb_fn callback
)
1046 w
= g_list_find_custom (h
->widgets
, callback
, dlg_find_widget_callback
);
1048 return (w
== NULL
) ? NULL
: WIDGET (w
->data
);
1051 /* --------------------------------------------------------------------------------------------- */
1052 /** Find the widget with the given id */
1055 dlg_find_by_id (const WDialog
* h
, unsigned long id
)
1059 w
= g_list_find_custom (h
->widgets
, GUINT_TO_POINTER (id
), dlg_find_widget_by_id
);
1060 return w
!= NULL
? WIDGET (w
->data
) : NULL
;
1063 /* --------------------------------------------------------------------------------------------- */
1064 /** Find the widget with the given id in the dialog h and select it */
1067 dlg_select_by_id (const WDialog
* h
, unsigned long id
)
1071 w
= dlg_find_by_id (h
, id
);
1073 dlg_select_widget (w
);
1076 /* --------------------------------------------------------------------------------------------- */
1078 * Try to select widget in the dialog.
1082 dlg_select_widget (void *w
)
1084 Widget
*widget
= WIDGET (w
);
1085 WDialog
*h
= widget
->owner
;
1087 do_select_widget (h
, g_list_find (h
->widgets
, widget
), SELECT_EXACT
);
1090 /* --------------------------------------------------------------------------------------------- */
1093 dlg_set_top_or_bottom_widget (void *w
, gboolean set_top
)
1095 Widget
*widget
= WIDGET (w
);
1096 WDialog
*h
= widget
->owner
;
1099 l
= g_list_find (h
->widgets
, w
);
1101 abort (); /* widget is not in dialog, this should not happen */
1103 /* unfocus prevoius widget and focus current one before widget reordering */
1104 if (set_top
&& h
->state
== DLG_ACTIVE
)
1105 do_select_widget (h
, l
, SELECT_EXACT
);
1107 /* widget reordering */
1108 h
->widgets
= g_list_remove_link (h
->widgets
, l
);
1110 h
->widgets
= g_list_concat (h
->widgets
, l
);
1112 h
->widgets
= g_list_concat (l
, h
->widgets
);
1115 /* --------------------------------------------------------------------------------------------- */
1117 * Set widget at top of widget list and make it current.
1121 dlg_set_top_widget (void *w
)
1123 dlg_set_top_or_bottom_widget (w
, TRUE
);
1126 /* --------------------------------------------------------------------------------------------- */
1128 * Set widget at bottom of widget list.
1132 dlg_set_bottom_widget (void *w
)
1134 dlg_set_top_or_bottom_widget (w
, FALSE
);
1137 /* --------------------------------------------------------------------------------------------- */
1138 /** Try to select previous widget in the tab order */
1141 dlg_one_up (WDialog
* h
)
1143 if (h
->widgets
!= NULL
)
1144 do_select_widget (h
, dlg_widget_prev (h
, h
->current
), SELECT_PREV
);
1147 /* --------------------------------------------------------------------------------------------- */
1148 /** Try to select next widget in the tab order */
1151 dlg_one_down (WDialog
* h
)
1153 if (h
->widgets
!= NULL
)
1154 do_select_widget (h
, dlg_widget_next (h
, h
->current
), SELECT_NEXT
);
1157 /* --------------------------------------------------------------------------------------------- */
1160 update_cursor (WDialog
* h
)
1162 GList
*p
= h
->current
;
1164 if ((p
!= NULL
) && (h
->state
== DLG_ACTIVE
))
1168 w
= WIDGET (p
->data
);
1170 if (((w
->options
& W_DISABLED
) == 0) && ((w
->options
& W_WANT_CURSOR
) != 0))
1171 send_message (w
, NULL
, MSG_CURSOR
, 0, NULL
);
1175 p
= dlg_widget_next (h
, p
);
1176 if (p
== h
->current
)
1179 w
= WIDGET (p
->data
);
1181 if (((w
->options
& W_DISABLED
) == 0) && ((w
->options
& W_WANT_CURSOR
) != 0))
1182 if (send_message (w
, NULL
, MSG_CURSOR
, 0, NULL
) == MSG_HANDLED
)
1189 /* --------------------------------------------------------------------------------------------- */
1191 * Redraw the widgets in reverse order, leaving the current widget
1196 dlg_redraw (WDialog
* h
)
1198 if (h
->state
!= DLG_ACTIVE
)
1201 if (h
->winch_pending
)
1203 h
->winch_pending
= FALSE
;
1204 send_message (h
, NULL
, MSG_RESIZE
, 0, NULL
);
1207 send_message (h
, NULL
, MSG_DRAW
, 0, NULL
);
1208 dlg_broadcast_msg (h
, MSG_DRAW
);
1212 /* --------------------------------------------------------------------------------------------- */
1215 dlg_stop (WDialog
* h
)
1217 h
->state
= DLG_CLOSED
;
1220 /* --------------------------------------------------------------------------------------------- */
1221 /** Init the process */
1224 dlg_init (WDialog
* h
)
1226 if (top_dlg
!= NULL
&& DIALOG (top_dlg
->data
)->modal
)
1229 /* add dialog to the stack */
1230 top_dlg
= g_list_prepend (top_dlg
, h
);
1232 /* Initialize dialog manager and widgets */
1233 if (h
->state
== DLG_CONSTRUCT
)
1236 dialog_switch_add (h
);
1238 send_message (h
, NULL
, MSG_INIT
, 0, NULL
);
1239 dlg_broadcast_msg (h
, MSG_INIT
);
1240 dlg_read_history (h
);
1243 h
->state
= DLG_ACTIVE
;
1245 /* first send MSG_DRAW to dialog itself and all widgets... */
1248 /* ...then send MSG_FOCUS to select the first widget that can take focus */
1249 while (h
->current
!= NULL
&& !dlg_focus (h
))
1250 h
->current
= dlg_widget_next (h
, h
->current
);
1256 /* --------------------------------------------------------------------------------------------- */
1259 dlg_process_event (WDialog
* h
, int key
, Gpm_Event
* event
)
1263 if (tty_got_interrupt ())
1264 if (send_message (h
, NULL
, MSG_ACTION
, CK_Cancel
, NULL
) != MSG_HANDLED
)
1265 dlg_execute_cmd (h
, CK_Cancel
);
1267 else if (key
== EV_MOUSE
)
1268 h
->mouse_status
= dlg_mouse_event (h
, event
);
1270 dlg_key_event (h
, key
);
1273 /* --------------------------------------------------------------------------------------------- */
1274 /** Shutdown the dlg_run */
1277 dlg_run_done (WDialog
* h
)
1279 top_dlg
= g_list_remove (top_dlg
, h
);
1281 if (h
->state
== DLG_CLOSED
)
1283 send_message (h
, h
->current
->data
, MSG_END
, 0, NULL
);
1285 dialog_switch_remove (h
);
1289 /* --------------------------------------------------------------------------------------------- */
1291 * Standard run dialog routine
1292 * We have to keep this routine small so that we can duplicate it's
1293 * behavior on complex routines like the file routines, this way,
1294 * they can call the dlg_process_event without rewriting all the code
1298 dlg_run (WDialog
* h
)
1301 frontend_dlg_run (h
);
1303 return h
->ret_value
;
1306 /* --------------------------------------------------------------------------------------------- */
1309 dlg_destroy (WDialog
* h
)
1311 /* if some widgets have history, save all history at one moment here */
1312 dlg_save_history (h
);
1313 dlg_broadcast_msg (h
, MSG_DESTROY
);
1314 g_list_free_full (h
->widgets
, g_free
);
1315 mc_event_group_del (h
->event_group
);
1316 g_free (h
->event_group
);
1323 /* --------------------------------------------------------------------------------------------- */
1326 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1329 dlg_save_history (WDialog
* h
)
1334 if (num_history_items_recorded
== 0) /* this is how to disable */
1337 profile
= mc_config_get_full_path (MC_HISTORY_FILE
);
1338 i
= open (profile
, O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
);
1342 /* Make sure the history is only readable by the user */
1343 if (chmod (profile
, S_IRUSR
| S_IWUSR
) != -1 || errno
== ENOENT
)
1345 ev_history_load_save_t event_data
;
1347 event_data
.cfg
= mc_config_init (profile
, FALSE
);
1348 event_data
.receiver
= NULL
;
1350 /* get all histories in dialog */
1351 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_SAVE
, &event_data
);
1353 mc_config_save_file (event_data
.cfg
, NULL
);
1354 mc_config_deinit (event_data
.cfg
);
1360 /* --------------------------------------------------------------------------------------------- */
1363 dlg_get_title (const WDialog
* h
, size_t len
)
1370 if (h
->get_title
!= NULL
)
1371 t
= h
->get_title (h
, len
);
1378 /* --------------------------------------------------------------------------------------------- */