2 Dialog box features module for the Midnight Commander
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2007, 2009, 2010, 2011, 2013
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * \brief Source: dialog box features module
35 #include <sys/types.h>
37 #include <fcntl.h> /* open() */
39 #include "lib/global.h"
41 #include "lib/tty/tty.h"
43 #include "lib/tty/key.h"
44 #include "lib/strutil.h"
45 #include "lib/widget.h"
46 #include "lib/fileloc.h" /* MC_HISTORY_FILE */
47 #include "lib/event.h" /* mc_event_raise() */
49 /*** global variables ****************************************************************************/
51 /* Color styles for normal and error dialogs */
52 dlg_colors_t dialog_colors
;
53 dlg_colors_t alarm_colors
;
55 /* Primitive way to check if the the current dialog is our dialog */
56 /* This is needed by async routines like load_prompt */
57 GList
*top_dlg
= NULL
;
59 /* A hook list for idle events */
60 hook_t
*idle_hook
= NULL
;
62 /* If set then dialogs just clean the screen when refreshing, else */
63 /* they do a complete refresh, refreshing all the parts of the program */
66 /* left click outside of dialog closes it */
67 int mouse_close_dialog
= 0;
69 const global_keymap_t
*dialog_map
= NULL
;
71 /*** file scope macro definitions ****************************************************************/
73 /*** file scope type declarations ****************************************************************/
75 /** What to do if the requested widget doesn't take focus */
78 SELECT_NEXT
, /* go the the next widget */
79 SELECT_PREV
, /* go the the previous widget */
80 SELECT_EXACT
/* use current widget */
83 /*** file scope variables ************************************************************************/
85 /*** file scope functions ************************************************************************/
88 dlg_widget_next (WDialog
* h
, GList
* l
)
92 next
= g_list_next (l
);
99 /* --------------------------------------------------------------------------------------------- */
102 dlg_widget_prev (WDialog
* h
, GList
* l
)
106 prev
= g_list_previous (l
);
108 prev
= g_list_last (h
->widgets
);
113 /* --------------------------------------------------------------------------------------------- */
115 * broadcast a message to all the widgets in a dialog that have
116 * the options set to flags. If flags is zero, the message is sent
121 dlg_broadcast_msg_to (WDialog
* h
, widget_msg_t msg
, gboolean reverse
, int flags
)
125 if (h
->widgets
== NULL
)
128 if (h
->current
== NULL
)
129 h
->current
= h
->widgets
;
132 p
= dlg_widget_prev (h
, h
->current
);
134 p
= dlg_widget_next (h
, h
->current
);
140 Widget
*w
= WIDGET (p
->data
);
143 p
= dlg_widget_prev (h
, p
);
145 p
= dlg_widget_next (h
, p
);
147 if ((flags
== 0) || ((flags
& w
->options
) != 0))
148 send_message (w
, NULL
, msg
, 0, NULL
);
153 /* --------------------------------------------------------------------------------------------- */
156 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
159 dlg_read_history (WDialog
* h
)
162 ev_history_load_save_t event_data
;
164 if (num_history_items_recorded
== 0) /* this is how to disable */
167 profile
= mc_config_get_full_path (MC_HISTORY_FILE
);
168 event_data
.cfg
= mc_config_init (profile
, TRUE
);
169 event_data
.receiver
= NULL
;
171 /* create all histories in dialog */
172 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_LOAD
, &event_data
);
174 mc_config_deinit (event_data
.cfg
);
178 /* --------------------------------------------------------------------------------------------- */
181 dlg_unfocus (WDialog
* h
)
183 /* we can unfocus disabled widget */
184 if ((h
->current
!= NULL
) && (h
->state
== DLG_CONSTRUCT
|| h
->state
== DLG_ACTIVE
))
186 Widget
*current
= WIDGET (h
->current
->data
);
188 if (send_message (current
, NULL
, MSG_UNFOCUS
, 0, NULL
) == MSG_HANDLED
)
190 send_message (h
, current
, MSG_UNFOCUS
, 0, NULL
);
198 /* --------------------------------------------------------------------------------------------- */
201 dlg_find_widget_callback (const void *a
, const void *b
)
203 const Widget
*w
= WIDGET (a
);
204 widget_cb_fn f
= (widget_cb_fn
) b
;
206 return (w
->callback
== f
) ? 0 : 1;
209 /* --------------------------------------------------------------------------------------------- */
211 * Try to select another widget. If forward is set, follow tab order.
212 * Otherwise go to the previous widget.
216 do_select_widget (WDialog
* h
, GList
* w
, select_dir_t dir
)
218 Widget
*w0
= WIDGET (h
->current
->data
);
220 if (!dlg_unfocus (h
))
233 h
->current
= g_list_find (h
->widgets
, w0
);
236 /* try find another widget that can take focus */
240 h
->current
= dlg_widget_next (h
, h
->current
);
243 h
->current
= dlg_widget_prev (h
, h
->current
);
247 while (h
->current
!= w
/* && (WIDGET (h->current->data)->options & W_DISABLED) == 0 */ );
249 if (widget_overlapped (w0
, WIDGET (h
->current
->data
)))
251 send_message (h
->current
->data
, NULL
, MSG_DRAW
, 0, NULL
);
252 send_message (h
->current
->data
, NULL
, MSG_FOCUS
, 0, NULL
);
256 /* --------------------------------------------------------------------------------------------- */
265 /* Use this if the refreshes fail */
268 #endif /* HAVE_SLANG */
271 /* --------------------------------------------------------------------------------------------- */
274 dlg_execute_cmd (WDialog
* h
, unsigned long command
)
276 cb_ret_t ret
= MSG_HANDLED
;
280 h
->ret_value
= B_ENTER
;
284 h
->ret_value
= B_CANCEL
;
299 ev_help_t event_data
= { NULL
, h
->help_ctx
};
300 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
305 mc_event_raise (MCEVENT_GROUP_CORE
, "suspend", NULL
);
314 dialog_switch_list ();
316 ret
= MSG_NOT_HANDLED
;
320 dialog_switch_next ();
322 ret
= MSG_NOT_HANDLED
;
326 dialog_switch_prev ();
328 ret
= MSG_NOT_HANDLED
;
332 ret
= MSG_NOT_HANDLED
;
338 /* --------------------------------------------------------------------------------------------- */
341 dlg_handle_key (WDialog
* h
, int d_key
)
343 unsigned long command
;
345 command
= keybind_lookup_keymap_command (dialog_map
, d_key
);
347 if (command
== CK_IgnoreKey
)
348 return MSG_NOT_HANDLED
;
350 if (send_message (h
, NULL
, MSG_ACTION
, command
, NULL
) == MSG_HANDLED
351 || dlg_execute_cmd (h
, command
) == MSG_HANDLED
)
354 return MSG_NOT_HANDLED
;
357 /* --------------------------------------------------------------------------------------------- */
360 dlg_mouse_event (WDialog
* h
, Gpm_Event
* event
)
362 Widget
*wh
= WIDGET (h
);
366 /* close the dialog by mouse left click out of dialog area */
367 if (mouse_close_dialog
&& !h
->fullscreen
&& ((event
->buttons
& GPM_B_LEFT
) != 0)
368 && ((event
->type
& GPM_DOWN
) != 0) && !mouse_global_in_widget (event
, wh
))
370 h
->ret_value
= B_CANCEL
;
375 if (wh
->mouse
!= NULL
)
379 mou
= wh
->mouse (event
, wh
);
380 if (mou
!= MOU_UNHANDLED
)
389 Widget
*w
= WIDGET (p
->data
);
391 p
= dlg_widget_prev (h
, p
);
393 if ((w
->options
& W_DISABLED
) == 0 && w
->mouse
!= NULL
)
395 /* put global cursor position to the widget */
398 ret
= w
->mouse (event
, w
);
399 if (ret
!= MOU_UNHANDLED
)
405 return MOU_UNHANDLED
;
408 /* --------------------------------------------------------------------------------------------- */
411 dlg_try_hotkey (WDialog
* h
, int d_key
)
418 if (h
->widgets
== NULL
)
419 return MSG_NOT_HANDLED
;
421 if (h
->current
== NULL
)
422 h
->current
= h
->widgets
;
425 * Explanation: we don't send letter hotkeys to other widgets if
426 * the currently selected widget is an input line
429 current
= WIDGET (h
->current
->data
);
431 if ((current
->options
& W_DISABLED
) != 0)
432 return MSG_NOT_HANDLED
;
434 if (current
->options
& W_IS_INPUT
)
436 /* skip ascii control characters, anything else can valid character in
438 if (d_key
>= 32 && d_key
< 256)
439 return MSG_NOT_HANDLED
;
442 /* If it's an alt key, send the message */
443 c
= d_key
& ~ALT (0);
444 if (d_key
& ALT (0) && g_ascii_isalpha (c
))
445 d_key
= g_ascii_tolower (c
);
447 handled
= MSG_NOT_HANDLED
;
448 if ((current
->options
& W_WANT_HOTKEY
) != 0)
449 handled
= send_message (current
, NULL
, MSG_HOTKEY
, d_key
, NULL
);
451 /* If not used, send hotkey to other widgets */
452 if (handled
== MSG_HANDLED
)
455 hot_cur
= dlg_widget_next (h
, h
->current
);
457 /* send it to all widgets */
458 while (h
->current
!= hot_cur
&& handled
== MSG_NOT_HANDLED
)
460 current
= WIDGET (hot_cur
->data
);
462 if ((current
->options
& W_WANT_HOTKEY
) != 0 && (current
->options
& W_DISABLED
) == 0)
463 handled
= send_message (current
, NULL
, MSG_HOTKEY
, d_key
, NULL
);
465 if (handled
== MSG_NOT_HANDLED
)
466 hot_cur
= dlg_widget_next (h
, hot_cur
);
469 if (handled
== MSG_HANDLED
)
470 do_select_widget (h
, hot_cur
, SELECT_EXACT
);
475 /* --------------------------------------------------------------------------------------------- */
478 dlg_key_event (WDialog
* h
, int d_key
)
482 if (h
->widgets
== NULL
)
485 if (h
->current
== NULL
)
486 h
->current
= h
->widgets
;
488 /* TAB used to cycle */
489 if ((h
->flags
& DLG_WANT_TAB
) == 0)
496 else if ((d_key
& ~(KEY_M_SHIFT
| KEY_M_CTRL
)) == '\t')
503 /* first can dlg_callback handle the key */
504 handled
= send_message (h
, NULL
, MSG_KEY
, d_key
, NULL
);
506 /* next try the hotkey */
507 if (handled
== MSG_NOT_HANDLED
)
508 handled
= dlg_try_hotkey (h
, d_key
);
510 if (handled
== MSG_HANDLED
)
511 send_message (h
, NULL
, MSG_HOTKEY_HANDLED
, 0, NULL
);
513 /* not used - then try widget_callback */
514 handled
= send_message (h
->current
->data
, NULL
, MSG_KEY
, d_key
, NULL
);
516 /* not used- try to use the unhandled case */
517 if (handled
== MSG_NOT_HANDLED
)
518 handled
= send_message (h
, NULL
, MSG_UNHANDLED_KEY
, d_key
, NULL
);
520 if (handled
== MSG_NOT_HANDLED
)
521 handled
= dlg_handle_key (h
, d_key
);
523 send_message (h
, NULL
, MSG_POST_KEY
, d_key
, NULL
);
526 /* --------------------------------------------------------------------------------------------- */
529 frontend_dlg_run (WDialog
* h
)
535 /* close opened editors, viewers, etc */
536 if (!h
->modal
&& mc_global
.midnight_shutdown
)
538 send_message (h
, NULL
, MSG_VALIDATE
, 0, NULL
);
542 while (h
->state
== DLG_ACTIVE
)
546 if (mc_global
.tty
.winch_flag
!= 0)
547 dialog_change_screen_size ();
552 execute_hooks (idle_hook
);
554 while ((WIDGET (h
)->options
& W_WANT_IDLE
) != 0 && is_idle ())
555 send_message (h
, NULL
, MSG_IDLE
, 0, NULL
);
557 /* Allow terminating the dialog from the idle handler */
558 if (h
->state
!= DLG_ACTIVE
)
564 /* Clear interrupt flag */
565 tty_got_interrupt ();
566 d_key
= tty_get_event (&event
, h
->mouse_status
== MOU_REPEAT
, TRUE
);
568 dlg_process_event (h
, d_key
, &event
);
570 if (h
->state
== DLG_CLOSED
)
571 send_message (h
, NULL
, MSG_VALIDATE
, 0, NULL
);
575 /* --------------------------------------------------------------------------------------------- */
578 dlg_find_widget_by_id (gconstpointer a
, gconstpointer b
)
580 Widget
*w
= WIDGET (a
);
581 unsigned long id
= GPOINTER_TO_UINT (b
);
583 return w
->id
== id
? 0 : 1;
586 /* --------------------------------------------------------------------------------------------- */
587 /*** public functions ****************************************************************************/
588 /* --------------------------------------------------------------------------------------------- */
590 /** Clean the dialog area, draw the frame and the title */
592 dlg_default_repaint (WDialog
* h
)
594 Widget
*wh
= WIDGET (h
);
598 if (h
->state
!= DLG_ACTIVE
)
601 space
= (h
->flags
& DLG_COMPACT
) ? 0 : 1;
603 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
605 tty_draw_box (wh
->y
+ space
, wh
->x
+ space
, wh
->lines
- 2 * space
, wh
->cols
- 2 * space
, FALSE
);
607 if (h
->title
!= NULL
)
609 tty_setcolor (h
->color
[DLG_COLOR_TITLE
]);
610 widget_move (h
, space
, (wh
->cols
- str_term_width1 (h
->title
)) / 2);
611 tty_print_string (h
->title
);
615 /* --------------------------------------------------------------------------------------------- */
616 /** this function allows to set dialog position */
619 dlg_set_position (WDialog
* h
, int y1
, int x1
, int y2
, int x2
)
621 Widget
*wh
= WIDGET (h
);
623 /* save old positions, will be used to reposition childs */
625 int shift_x
, shift_y
, scale_x
, scale_y
;
627 /* save old positions, will be used to reposition childs */
638 /* dialog is empty */
639 if (h
->widgets
== NULL
)
642 if (h
->current
== NULL
)
643 h
->current
= h
->widgets
;
645 /* values by which controls should be moved */
646 shift_x
= wh
->x
- ox
;
647 shift_y
= wh
->y
- oy
;
648 scale_x
= wh
->cols
- oc
;
649 scale_y
= wh
->lines
- ol
;
651 if ((shift_x
!= 0) || (shift_y
!= 0) || (scale_x
!= 0) || (scale_y
!= 0))
655 for (w
= h
->widgets
; w
!= NULL
; w
= g_list_next (w
))
657 /* there are, mainly, 2 generally possible
660 1. control sticks to one side - it
663 2. control sticks to two sides of
664 one direction - it should be sized */
666 Widget
*c
= WIDGET (w
->data
);
670 int lines
= c
->lines
;
672 if ((c
->pos_flags
& WPOS_CENTER_HORZ
) != 0)
673 x
= wh
->x
+ (wh
->cols
- c
->cols
) / 2;
674 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0 && (c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
679 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0)
681 else if ((c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
682 x
+= shift_x
+ scale_x
;
684 if ((c
->pos_flags
& WPOS_CENTER_VERT
) != 0)
685 y
= wh
->y
+ (wh
->lines
- c
->lines
) / 2;
686 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0 && (c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
691 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0)
693 else if ((c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
694 y
+= shift_y
+ scale_y
;
696 widget_set_size (c
, y
, x
, lines
, cols
);
701 /* --------------------------------------------------------------------------------------------- */
702 /** this function sets only size, leaving positioning to automatic methods */
705 dlg_set_size (WDialog
* h
, int lines
, int cols
)
707 int x
= WIDGET (h
)->x
;
708 int y
= WIDGET (h
)->y
;
710 if (h
->flags
& DLG_CENTER
)
712 y
= (LINES
- lines
) / 2;
713 x
= (COLS
- cols
) / 2;
716 if ((h
->flags
& DLG_TRYUP
) && (y
> 3))
719 dlg_set_position (h
, y
, x
, y
+ lines
, x
+ cols
);
722 /* --------------------------------------------------------------------------------------------- */
723 /** Default dialog callback */
726 dlg_default_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
728 WDialog
*h
= DIALOG (w
);
737 if (h
->color
!= NULL
)
739 dlg_default_repaint (h
);
742 return MSG_NOT_HANDLED
;
745 dlg_broadcast_msg_to (h
, MSG_IDLE
, FALSE
, W_WANT_IDLE
);
749 /* this is default resizing mechanism */
750 /* the main idea of this code is to resize dialog
751 according to flags (if any of flags require automatic
752 resizing, like DLG_CENTER, end after that reposition
753 controls in dialog according to flags of widget) */
754 dlg_set_size (h
, w
->lines
, w
->cols
);
761 return MSG_NOT_HANDLED
;
764 /* --------------------------------------------------------------------------------------------- */
767 dlg_create (gboolean modal
, int y1
, int x1
, int lines
, int cols
,
768 const int *colors
, widget_cb_fn callback
, mouse_h mouse_handler
,
769 const char *help_ctx
, const char *title
, dlg_flags_t flags
)
774 new_d
= g_new0 (WDialog
, 1);
776 widget_init (w
, y1
, x1
, lines
, cols
, (callback
!= NULL
) ? callback
: dlg_default_callback
,
778 widget_want_cursor (w
, FALSE
);
780 new_d
->state
= DLG_CONSTRUCT
;
781 new_d
->modal
= modal
;
783 memmove (new_d
->color
, colors
, sizeof (dlg_colors_t
));
784 new_d
->help_ctx
= help_ctx
;
785 new_d
->flags
= flags
;
788 dlg_set_size (new_d
, lines
, cols
);
789 new_d
->fullscreen
= (w
->x
== 0 && w
->y
== 0 && w
->cols
== COLS
&& w
->lines
== LINES
);
791 new_d
->mouse_status
= MOU_UNHANDLED
;
793 /* Strip existing spaces, add one space before and after the title */
794 if (title
!= NULL
&& *title
!= '\0')
798 t
= g_strstrip (g_strdup (title
));
800 new_d
->title
= g_strdup_printf (" %s ", t
);
804 /* unique name of event group for this dialog */
805 new_d
->event_group
= g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG
, (void *) new_d
);
810 /* --------------------------------------------------------------------------------------------- */
813 dlg_set_default_colors (void)
815 dialog_colors
[DLG_COLOR_NORMAL
] = COLOR_NORMAL
;
816 dialog_colors
[DLG_COLOR_FOCUS
] = COLOR_FOCUS
;
817 dialog_colors
[DLG_COLOR_HOT_NORMAL
] = COLOR_HOT_NORMAL
;
818 dialog_colors
[DLG_COLOR_HOT_FOCUS
] = COLOR_HOT_FOCUS
;
819 dialog_colors
[DLG_COLOR_TITLE
] = COLOR_TITLE
;
821 alarm_colors
[DLG_COLOR_NORMAL
] = ERROR_COLOR
;
822 alarm_colors
[DLG_COLOR_FOCUS
] = ERROR_FOCUS
;
823 alarm_colors
[DLG_COLOR_HOT_NORMAL
] = ERROR_HOT_NORMAL
;
824 alarm_colors
[DLG_COLOR_HOT_FOCUS
] = ERROR_HOT_FOCUS
;
825 alarm_colors
[DLG_COLOR_TITLE
] = ERROR_TITLE
;
828 /* --------------------------------------------------------------------------------------------- */
831 dlg_erase (WDialog
* h
)
833 if ((h
!= NULL
) && (h
->state
== DLG_ACTIVE
))
835 Widget
*wh
= WIDGET (h
);
837 tty_fill_region (wh
->y
, wh
->x
, wh
->lines
, wh
->cols
, ' ');
841 /* --------------------------------------------------------------------------------------------- */
843 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
847 add_widget_autopos (WDialog
* h
, void *w
, widget_pos_flags_t pos_flags
, const void *before
)
849 Widget
*wh
= WIDGET (h
);
852 /* Don't accept 0 widgets */
858 if ((pos_flags
& WPOS_CENTER_HORZ
) != 0)
859 widget
->x
= (wh
->cols
- widget
->cols
) / 2;
862 if ((pos_flags
& WPOS_CENTER_VERT
) != 0)
863 widget
->y
= (wh
->lines
- widget
->lines
) / 2;
867 widget
->pos_flags
= pos_flags
;
868 widget
->id
= h
->widget_id
++;
870 if (h
->widgets
== NULL
|| before
== NULL
)
872 h
->widgets
= g_list_append (h
->widgets
, widget
);
873 h
->current
= g_list_last (h
->widgets
);
879 b
= g_list_find (h
->widgets
, before
);
881 /* don't accept widget not from dialog. This shouldn't happen */
886 h
->widgets
= g_list_insert_before (h
->widgets
, b
, widget
);
888 h
->current
= g_list_previous (b
);
890 h
->current
= g_list_last (h
->widgets
);
893 /* widget has been added in runtime */
894 if (h
->state
== DLG_ACTIVE
)
896 send_message (widget
, NULL
, MSG_INIT
, 0, NULL
);
897 send_message (widget
, NULL
, MSG_DRAW
, 0, NULL
);
898 send_message (widget
, NULL
, MSG_FOCUS
, 0, NULL
);
904 /* --------------------------------------------------------------------------------------------- */
905 /** wrapper to simply add lefttop positioned controls */
908 add_widget (WDialog
* h
, void *w
)
910 return add_widget_autopos (h
, w
, WPOS_KEEP_DEFAULT
,
911 h
->current
!= NULL
? h
->current
->data
: NULL
);
914 /* --------------------------------------------------------------------------------------------- */
917 add_widget_before (WDialog
* h
, void *w
, void *before
)
919 return add_widget_autopos (h
, w
, WPOS_KEEP_DEFAULT
, before
);
922 /* --------------------------------------------------------------------------------------------- */
924 /** delete widget from dialog */
931 /* Don't accept NULL widget. This shouldn't happen */
935 h
= WIDGET (w
)->owner
;
937 d
= g_list_find (h
->widgets
, w
);
939 h
->current
= dlg_widget_next (h
, d
);
941 h
->widgets
= g_list_remove_link (h
->widgets
, d
);
942 send_message (d
->data
, NULL
, MSG_DESTROY
, 0, NULL
);
946 /* widget has been deleted in runtime */
947 if (h
->state
== DLG_ACTIVE
)
954 /* --------------------------------------------------------------------------------------------- */
963 if ((d
!= NULL
) && (d
->data
!= NULL
))
964 dlg_redraw (DIALOG (d
->data
));
968 /* Search first fullscreen dialog */
969 for (; d
!= NULL
; d
= g_list_next (d
))
970 if (d
->data
!= NULL
&& DIALOG (d
->data
)->fullscreen
)
972 /* back to top dialog */
973 for (; d
!= NULL
; d
= g_list_previous (d
))
975 dlg_redraw (DIALOG (d
->data
));
979 /* --------------------------------------------------------------------------------------------- */
980 /** broadcast a message to all the widgets in a dialog */
983 dlg_broadcast_msg (WDialog
* h
, widget_msg_t msg
)
985 dlg_broadcast_msg_to (h
, msg
, FALSE
, 0);
988 /* --------------------------------------------------------------------------------------------- */
991 dlg_focus (WDialog
* h
)
993 /* cannot focus disabled widget */
994 if ((h
->current
!= NULL
) && (h
->state
== DLG_CONSTRUCT
|| h
->state
== DLG_ACTIVE
))
996 Widget
*current
= WIDGET (h
->current
->data
);
998 if (((current
->options
& W_DISABLED
) == 0)
999 && (send_message (current
, NULL
, MSG_FOCUS
, 0, NULL
) == MSG_HANDLED
))
1001 send_message (h
, current
, MSG_FOCUS
, 0, NULL
);
1009 /* --------------------------------------------------------------------------------------------- */
1010 /** Find the widget with the given callback in the dialog h */
1013 find_widget_type (const WDialog
* h
, widget_cb_fn callback
)
1017 w
= g_list_find_custom (h
->widgets
, callback
, dlg_find_widget_callback
);
1019 return (w
== NULL
) ? NULL
: WIDGET (w
->data
);
1022 /* --------------------------------------------------------------------------------------------- */
1023 /** Find the widget with the given id */
1026 dlg_find_by_id (const WDialog
* h
, unsigned long id
)
1030 w
= g_list_find_custom (h
->widgets
, GUINT_TO_POINTER (id
), dlg_find_widget_by_id
);
1031 return w
!= NULL
? WIDGET (w
->data
) : NULL
;
1034 /* --------------------------------------------------------------------------------------------- */
1035 /** Find the widget with the given id in the dialog h and select it */
1038 dlg_select_by_id (const WDialog
* h
, unsigned long id
)
1042 w
= dlg_find_by_id (h
, id
);
1044 dlg_select_widget (w
);
1047 /* --------------------------------------------------------------------------------------------- */
1049 * Try to select widget in the dialog.
1053 dlg_select_widget (void *w
)
1055 Widget
*widget
= WIDGET (w
);
1056 WDialog
*h
= widget
->owner
;
1058 do_select_widget (h
, g_list_find (h
->widgets
, widget
), SELECT_EXACT
);
1061 /* --------------------------------------------------------------------------------------------- */
1064 * Set widget at top of widget list and make it current.
1068 dlg_set_top_widget (void *w
)
1070 Widget
*widget
= WIDGET (w
);
1071 WDialog
*h
= widget
->owner
;
1074 l
= g_list_find (h
->widgets
, w
);
1076 abort (); /* widget is not in dialog, this should not happen */
1078 /* unfocus prevoius widget and focus current one before widget reordering */
1079 if (h
->state
== DLG_ACTIVE
)
1080 do_select_widget (h
, l
, SELECT_EXACT
);
1082 /* widget reordering */
1083 h
->widgets
= g_list_remove_link (h
->widgets
, l
);
1084 h
->widgets
= g_list_concat (h
->widgets
, l
);
1088 /* --------------------------------------------------------------------------------------------- */
1089 /** Try to select previous widget in the tab order */
1092 dlg_one_up (WDialog
* h
)
1094 if (h
->widgets
!= NULL
)
1095 do_select_widget (h
, dlg_widget_prev (h
, h
->current
), SELECT_PREV
);
1098 /* --------------------------------------------------------------------------------------------- */
1099 /** Try to select next widget in the tab order */
1102 dlg_one_down (WDialog
* h
)
1104 if (h
->widgets
!= NULL
)
1105 do_select_widget (h
, dlg_widget_next (h
, h
->current
), SELECT_NEXT
);
1108 /* --------------------------------------------------------------------------------------------- */
1111 update_cursor (WDialog
* h
)
1113 GList
*p
= h
->current
;
1115 if ((p
!= NULL
) && (h
->state
== DLG_ACTIVE
))
1119 w
= WIDGET (p
->data
);
1121 if (((w
->options
& W_DISABLED
) == 0) && ((w
->options
& W_WANT_CURSOR
) != 0))
1122 send_message (w
, NULL
, MSG_CURSOR
, 0, NULL
);
1126 p
= dlg_widget_next (h
, p
);
1127 if (p
== h
->current
)
1130 w
= WIDGET (p
->data
);
1132 if (((w
->options
& W_DISABLED
) == 0) && ((w
->options
& W_WANT_CURSOR
) != 0))
1133 if (send_message (w
, NULL
, MSG_CURSOR
, 0, NULL
) == MSG_HANDLED
)
1140 /* --------------------------------------------------------------------------------------------- */
1142 * Redraw the widgets in reverse order, leaving the current widget
1147 dlg_redraw (WDialog
* h
)
1149 if (h
->state
!= DLG_ACTIVE
)
1152 if (h
->winch_pending
)
1154 h
->winch_pending
= FALSE
;
1155 send_message (h
, NULL
, MSG_RESIZE
, 0, NULL
);
1158 send_message (h
, NULL
, MSG_DRAW
, 0, NULL
);
1159 dlg_broadcast_msg (h
, MSG_DRAW
);
1163 /* --------------------------------------------------------------------------------------------- */
1166 dlg_stop (WDialog
* h
)
1168 h
->state
= DLG_CLOSED
;
1171 /* --------------------------------------------------------------------------------------------- */
1172 /** Init the process */
1175 dlg_init (WDialog
* h
)
1177 if (top_dlg
!= NULL
&& DIALOG (top_dlg
->data
)->modal
)
1180 /* add dialog to the stack */
1181 top_dlg
= g_list_prepend (top_dlg
, h
);
1183 /* Initialize dialog manager and widgets */
1184 if (h
->state
== DLG_CONSTRUCT
)
1187 dialog_switch_add (h
);
1189 send_message (h
, NULL
, MSG_INIT
, 0, NULL
);
1190 dlg_broadcast_msg (h
, MSG_INIT
);
1191 dlg_read_history (h
);
1194 h
->state
= DLG_ACTIVE
;
1196 /* first send MSG_DRAW to dialog itself and all widgets... */
1199 /* ...then send MSG_FOCUS to select the first widget that can take focus */
1200 while (h
->current
!= NULL
&& !dlg_focus (h
))
1201 h
->current
= dlg_widget_next (h
, h
->current
);
1207 /* --------------------------------------------------------------------------------------------- */
1210 dlg_process_event (WDialog
* h
, int key
, Gpm_Event
* event
)
1214 if (tty_got_interrupt ())
1215 if (send_message (h
, NULL
, MSG_ACTION
, CK_Cancel
, NULL
) != MSG_HANDLED
)
1216 dlg_execute_cmd (h
, CK_Cancel
);
1221 if (key
== EV_MOUSE
)
1222 h
->mouse_status
= dlg_mouse_event (h
, event
);
1224 dlg_key_event (h
, key
);
1227 /* --------------------------------------------------------------------------------------------- */
1228 /** Shutdown the dlg_run */
1231 dlg_run_done (WDialog
* h
)
1233 top_dlg
= g_list_remove (top_dlg
, h
);
1235 if (h
->state
== DLG_CLOSED
)
1237 send_message (h
, h
->current
->data
, MSG_END
, 0, NULL
);
1239 dialog_switch_remove (h
);
1243 /* --------------------------------------------------------------------------------------------- */
1245 * Standard run dialog routine
1246 * We have to keep this routine small so that we can duplicate it's
1247 * behavior on complex routines like the file routines, this way,
1248 * they can call the dlg_process_event without rewriting all the code
1252 dlg_run (WDialog
* h
)
1255 frontend_dlg_run (h
);
1257 return h
->ret_value
;
1260 /* --------------------------------------------------------------------------------------------- */
1263 dlg_destroy (WDialog
* h
)
1265 /* if some widgets have history, save all history at one moment here */
1266 dlg_save_history (h
);
1267 dlg_broadcast_msg (h
, MSG_DESTROY
);
1268 g_list_foreach (h
->widgets
, (GFunc
) g_free
, NULL
);
1269 g_list_free (h
->widgets
);
1270 mc_event_group_del (h
->event_group
);
1271 g_free (h
->event_group
);
1278 /* --------------------------------------------------------------------------------------------- */
1281 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1284 dlg_save_history (WDialog
* h
)
1289 if (num_history_items_recorded
== 0) /* this is how to disable */
1292 profile
= mc_config_get_full_path (MC_HISTORY_FILE
);
1293 i
= open (profile
, O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
);
1297 /* Make sure the history is only readable by the user */
1298 if (chmod (profile
, S_IRUSR
| S_IWUSR
) != -1 || errno
== ENOENT
)
1300 ev_history_load_save_t event_data
;
1302 event_data
.cfg
= mc_config_init (profile
, FALSE
);
1303 event_data
.receiver
= NULL
;
1305 /* get all histories in dialog */
1306 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_SAVE
, &event_data
);
1308 mc_config_save_file (event_data
.cfg
, NULL
);
1309 mc_config_deinit (event_data
.cfg
);
1315 /* --------------------------------------------------------------------------------------------- */
1318 dlg_get_title (const WDialog
* h
, size_t len
)
1325 if (h
->get_title
!= NULL
)
1326 t
= h
->get_title (h
, len
);
1333 /* --------------------------------------------------------------------------------------------- */