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/widget.h"
44 #include "lib/fileloc.h" /* MC_HISTORY_FILE */
45 #include "lib/event.h" /* mc_event_raise() */
47 /*** global variables ****************************************************************************/
49 /* Color styles for normal and error dialogs */
50 dlg_colors_t dialog_colors
;
51 dlg_colors_t alarm_colors
;
52 dlg_colors_t listbox_colors
;
54 /* Primitive way to check if the the current dialog is our dialog */
55 /* This is needed by async routines like load_prompt */
56 GList
*top_dlg
= NULL
;
58 /* A hook list for idle events */
59 hook_t
*idle_hook
= NULL
;
61 /* If set then dialogs just clean the screen when refreshing, else */
62 /* they do a complete refresh, refreshing all the parts of the program */
65 /* left click outside of dialog closes it */
66 int mouse_close_dialog
= 0;
68 const global_keymap_t
*dialog_map
= NULL
;
70 /*** file scope macro definitions ****************************************************************/
72 /*** file scope type declarations ****************************************************************/
74 /** What to do if the requested widget doesn't take focus */
77 SELECT_NEXT
, /* go the the next widget */
78 SELECT_PREV
, /* go the the previous widget */
79 SELECT_EXACT
/* use current widget */
82 /*** file scope variables ************************************************************************/
84 /*** file scope functions ************************************************************************/
87 dlg_widget_next (WDialog
* h
, GList
* l
)
91 next
= g_list_next (l
);
98 /* --------------------------------------------------------------------------------------------- */
101 dlg_widget_prev (WDialog
* h
, GList
* l
)
105 prev
= g_list_previous (l
);
107 prev
= g_list_last (h
->widgets
);
112 /* --------------------------------------------------------------------------------------------- */
114 * broadcast a message to all the widgets in a dialog that have
115 * the options set to flags. If flags is zero, the message is sent
120 dlg_broadcast_msg_to (WDialog
* h
, widget_msg_t msg
, gboolean reverse
, int flags
)
124 if (h
->widgets
== NULL
)
127 if (h
->current
== NULL
)
128 h
->current
= h
->widgets
;
131 p
= dlg_widget_prev (h
, h
->current
);
133 p
= dlg_widget_next (h
, h
->current
);
139 Widget
*w
= WIDGET (p
->data
);
142 p
= dlg_widget_prev (h
, p
);
144 p
= dlg_widget_next (h
, p
);
146 if ((flags
== 0) || ((flags
& w
->options
) != 0))
147 send_message (w
, NULL
, msg
, 0, NULL
);
152 /* --------------------------------------------------------------------------------------------- */
155 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
158 dlg_read_history (WDialog
* h
)
161 ev_history_load_save_t event_data
;
163 if (num_history_items_recorded
== 0) /* this is how to disable */
166 profile
= mc_config_get_full_path (MC_HISTORY_FILE
);
167 event_data
.cfg
= mc_config_init (profile
, TRUE
);
168 event_data
.receiver
= NULL
;
170 /* create all histories in dialog */
171 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_LOAD
, &event_data
);
173 mc_config_deinit (event_data
.cfg
);
177 /* --------------------------------------------------------------------------------------------- */
180 dlg_unfocus (WDialog
* h
)
182 /* we can unfocus disabled widget */
183 if ((h
->current
!= NULL
) && (h
->state
== DLG_CONSTRUCT
|| h
->state
== DLG_ACTIVE
))
185 Widget
*current
= WIDGET (h
->current
->data
);
187 if (send_message (current
, NULL
, MSG_UNFOCUS
, 0, NULL
) == MSG_HANDLED
)
189 send_message (h
, current
, MSG_UNFOCUS
, 0, NULL
);
197 /* --------------------------------------------------------------------------------------------- */
200 dlg_find_widget_callback (const void *a
, const void *b
)
202 const Widget
*w
= WIDGET (a
);
203 widget_cb_fn f
= (widget_cb_fn
) b
;
205 return (w
->callback
== f
) ? 0 : 1;
208 /* --------------------------------------------------------------------------------------------- */
210 * Try to select another widget. If forward is set, follow tab order.
211 * Otherwise go to the previous widget.
215 do_select_widget (WDialog
* h
, GList
* w
, select_dir_t dir
)
217 Widget
*w0
= WIDGET (h
->current
->data
);
219 if (!dlg_unfocus (h
))
232 h
->current
= g_list_find (h
->widgets
, w0
);
235 /* try find another widget that can take focus */
239 h
->current
= dlg_widget_next (h
, h
->current
);
242 h
->current
= dlg_widget_prev (h
, h
->current
);
248 while (h
->current
!= w
/* && (WIDGET (h->current->data)->options & W_DISABLED) == 0 */ );
250 if (widget_overlapped (w0
, WIDGET (h
->current
->data
)))
252 send_message (h
->current
->data
, NULL
, MSG_DRAW
, 0, NULL
);
253 send_message (h
->current
->data
, NULL
, MSG_FOCUS
, 0, NULL
);
257 /* --------------------------------------------------------------------------------------------- */
266 /* Use this if the refreshes fail */
269 #endif /* HAVE_SLANG */
272 /* --------------------------------------------------------------------------------------------- */
275 dlg_execute_cmd (WDialog
* h
, long command
)
277 cb_ret_t ret
= MSG_HANDLED
;
281 h
->ret_value
= B_ENTER
;
285 h
->ret_value
= B_CANCEL
;
300 ev_help_t event_data
= { NULL
, h
->help_ctx
};
301 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
306 mc_event_raise (MCEVENT_GROUP_CORE
, "suspend", NULL
);
315 dialog_switch_list ();
317 ret
= MSG_NOT_HANDLED
;
321 dialog_switch_next ();
323 ret
= MSG_NOT_HANDLED
;
327 dialog_switch_prev ();
329 ret
= MSG_NOT_HANDLED
;
333 ret
= MSG_NOT_HANDLED
;
339 /* --------------------------------------------------------------------------------------------- */
342 dlg_handle_key (WDialog
* h
, int d_key
)
346 command
= keybind_lookup_keymap_command (dialog_map
, d_key
);
348 if (command
== CK_IgnoreKey
)
349 return MSG_NOT_HANDLED
;
351 if (send_message (h
, NULL
, MSG_ACTION
, command
, NULL
) == MSG_HANDLED
352 || dlg_execute_cmd (h
, command
) == MSG_HANDLED
)
355 return MSG_NOT_HANDLED
;
358 /* --------------------------------------------------------------------------------------------- */
361 dlg_mouse_event (WDialog
* h
, Gpm_Event
* event
)
363 Widget
*wh
= WIDGET (h
);
367 /* close the dialog by mouse left click out of dialog area */
368 if (mouse_close_dialog
&& !h
->fullscreen
&& ((event
->buttons
& GPM_B_LEFT
) != 0)
369 && ((event
->type
& GPM_DOWN
) != 0) && !mouse_global_in_widget (event
, wh
))
371 h
->ret_value
= B_CANCEL
;
376 if (wh
->mouse
!= NULL
)
380 mou
= wh
->mouse (event
, wh
);
381 if (mou
!= MOU_UNHANDLED
)
390 Widget
*w
= WIDGET (p
->data
);
392 p
= dlg_widget_prev (h
, p
);
394 if ((w
->options
& W_DISABLED
) == 0 && w
->mouse
!= NULL
)
396 /* put global cursor position to the widget */
399 ret
= w
->mouse (event
, w
);
400 if (ret
!= MOU_UNHANDLED
)
406 return MOU_UNHANDLED
;
409 /* --------------------------------------------------------------------------------------------- */
412 dlg_try_hotkey (WDialog
* h
, int d_key
)
419 if (h
->widgets
== NULL
)
420 return MSG_NOT_HANDLED
;
422 if (h
->current
== NULL
)
423 h
->current
= h
->widgets
;
426 * Explanation: we don't send letter hotkeys to other widgets if
427 * the currently selected widget is an input line
430 current
= WIDGET (h
->current
->data
);
432 if ((current
->options
& W_DISABLED
) != 0)
433 return MSG_NOT_HANDLED
;
435 if (current
->options
& W_IS_INPUT
)
437 /* skip ascii control characters, anything else can valid character in
439 if (d_key
>= 32 && d_key
< 256)
440 return MSG_NOT_HANDLED
;
443 /* If it's an alt key, send the message */
444 c
= d_key
& ~ALT (0);
445 if (d_key
& ALT (0) && g_ascii_isalpha (c
))
446 d_key
= g_ascii_tolower (c
);
448 handled
= MSG_NOT_HANDLED
;
449 if ((current
->options
& W_WANT_HOTKEY
) != 0)
450 handled
= send_message (current
, NULL
, MSG_HOTKEY
, d_key
, NULL
);
452 /* If not used, send hotkey to other widgets */
453 if (handled
== MSG_HANDLED
)
456 hot_cur
= dlg_widget_next (h
, h
->current
);
458 /* send it to all widgets */
459 while (h
->current
!= hot_cur
&& handled
== MSG_NOT_HANDLED
)
461 current
= WIDGET (hot_cur
->data
);
463 if ((current
->options
& W_WANT_HOTKEY
) != 0 && (current
->options
& W_DISABLED
) == 0)
464 handled
= send_message (current
, NULL
, MSG_HOTKEY
, d_key
, NULL
);
466 if (handled
== MSG_NOT_HANDLED
)
467 hot_cur
= dlg_widget_next (h
, hot_cur
);
470 if (handled
== MSG_HANDLED
)
471 do_select_widget (h
, hot_cur
, SELECT_EXACT
);
476 /* --------------------------------------------------------------------------------------------- */
479 dlg_key_event (WDialog
* h
, int d_key
)
483 if (h
->widgets
== NULL
)
486 if (h
->current
== NULL
)
487 h
->current
= h
->widgets
;
489 /* TAB used to cycle */
490 if ((h
->flags
& DLG_WANT_TAB
) == 0)
497 else if ((d_key
& ~(KEY_M_SHIFT
| KEY_M_CTRL
)) == '\t')
504 /* first can dlg_callback handle the key */
505 handled
= send_message (h
, NULL
, MSG_KEY
, d_key
, NULL
);
507 /* next try the hotkey */
508 if (handled
== MSG_NOT_HANDLED
)
509 handled
= dlg_try_hotkey (h
, d_key
);
511 if (handled
== MSG_HANDLED
)
512 send_message (h
, NULL
, MSG_HOTKEY_HANDLED
, 0, NULL
);
514 /* not used - then try widget_callback */
515 handled
= send_message (h
->current
->data
, NULL
, MSG_KEY
, d_key
, NULL
);
517 /* not used- try to use the unhandled case */
518 if (handled
== MSG_NOT_HANDLED
)
519 handled
= send_message (h
, NULL
, MSG_UNHANDLED_KEY
, d_key
, NULL
);
521 if (handled
== MSG_NOT_HANDLED
)
522 handled
= dlg_handle_key (h
, d_key
);
525 send_message (h
, NULL
, MSG_POST_KEY
, d_key
, NULL
);
528 /* --------------------------------------------------------------------------------------------- */
531 frontend_dlg_run (WDialog
* h
)
537 /* close opened editors, viewers, etc */
538 if (!h
->modal
&& mc_global
.midnight_shutdown
)
540 send_message (h
, NULL
, MSG_VALIDATE
, 0, NULL
);
544 while (h
->state
== DLG_ACTIVE
)
548 if (mc_global
.tty
.winch_flag
!= 0)
549 dialog_change_screen_size ();
554 execute_hooks (idle_hook
);
556 while ((WIDGET (h
)->options
& W_WANT_IDLE
) != 0 && is_idle ())
557 send_message (h
, NULL
, MSG_IDLE
, 0, NULL
);
559 /* Allow terminating the dialog from the idle handler */
560 if (h
->state
!= DLG_ACTIVE
)
566 /* Clear interrupt flag */
567 tty_got_interrupt ();
568 d_key
= tty_get_event (&event
, h
->mouse_status
== MOU_REPEAT
, TRUE
);
570 dlg_process_event (h
, d_key
, &event
);
572 if (h
->state
== DLG_CLOSED
)
573 send_message (h
, NULL
, MSG_VALIDATE
, 0, NULL
);
577 /* --------------------------------------------------------------------------------------------- */
580 dlg_find_widget_by_id (gconstpointer a
, gconstpointer b
)
582 Widget
*w
= WIDGET (a
);
583 unsigned long id
= GPOINTER_TO_UINT (b
);
585 return w
->id
== id
? 0 : 1;
588 /* --------------------------------------------------------------------------------------------- */
589 /*** public functions ****************************************************************************/
590 /* --------------------------------------------------------------------------------------------- */
592 /** Clean the dialog area, draw the frame and the title */
594 dlg_default_repaint (WDialog
* h
)
596 Widget
*wh
= WIDGET (h
);
600 if (h
->state
!= DLG_ACTIVE
)
603 space
= (h
->flags
& DLG_COMPACT
) ? 0 : 1;
605 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
607 tty_draw_box (wh
->y
+ space
, wh
->x
+ space
, wh
->lines
- 2 * space
, wh
->cols
- 2 * space
, FALSE
);
609 if (h
->title
!= NULL
)
611 tty_setcolor (h
->color
[DLG_COLOR_TITLE
]);
612 widget_move (h
, space
, (wh
->cols
- str_term_width1 (h
->title
)) / 2);
613 tty_print_string (h
->title
);
617 /* --------------------------------------------------------------------------------------------- */
618 /** this function allows to set dialog position */
621 dlg_set_position (WDialog
* h
, int y1
, int x1
, int y2
, int x2
)
623 Widget
*wh
= WIDGET (h
);
625 /* save old positions, will be used to reposition childs */
627 int shift_x
, shift_y
, scale_x
, scale_y
;
629 /* save old positions, will be used to reposition childs */
640 /* dialog is empty */
641 if (h
->widgets
== NULL
)
644 if (h
->current
== NULL
)
645 h
->current
= h
->widgets
;
647 /* values by which controls should be moved */
648 shift_x
= wh
->x
- ox
;
649 shift_y
= wh
->y
- oy
;
650 scale_x
= wh
->cols
- oc
;
651 scale_y
= wh
->lines
- ol
;
653 if ((shift_x
!= 0) || (shift_y
!= 0) || (scale_x
!= 0) || (scale_y
!= 0))
657 for (w
= h
->widgets
; w
!= NULL
; w
= g_list_next (w
))
659 /* there are, mainly, 2 generally possible
662 1. control sticks to one side - it
665 2. control sticks to two sides of
666 one direction - it should be sized */
668 Widget
*c
= WIDGET (w
->data
);
672 int lines
= c
->lines
;
674 if ((c
->pos_flags
& WPOS_CENTER_HORZ
) != 0)
675 x
= wh
->x
+ (wh
->cols
- c
->cols
) / 2;
676 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0 && (c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
681 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0)
683 else if ((c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
684 x
+= shift_x
+ scale_x
;
686 if ((c
->pos_flags
& WPOS_CENTER_VERT
) != 0)
687 y
= wh
->y
+ (wh
->lines
- c
->lines
) / 2;
688 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0 && (c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
693 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0)
695 else if ((c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
696 y
+= shift_y
+ scale_y
;
698 widget_set_size (c
, y
, x
, lines
, cols
);
703 /* --------------------------------------------------------------------------------------------- */
704 /** Set dialog size and position */
707 dlg_set_size (WDialog
* h
, int lines
, int cols
)
709 int x
= WIDGET (h
)->x
;
710 int y
= WIDGET (h
)->y
;
712 if ((h
->flags
& DLG_CENTER
) != 0)
714 y
= (LINES
- lines
) / 2;
715 x
= (COLS
- cols
) / 2;
718 if ((h
->flags
& DLG_TRYUP
) != 0)
726 dlg_set_position (h
, y
, x
, y
+ lines
, x
+ cols
);
729 /* --------------------------------------------------------------------------------------------- */
730 /** Default dialog callback */
733 dlg_default_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
735 WDialog
*h
= DIALOG (w
);
744 if (h
->color
!= NULL
)
746 dlg_default_repaint (h
);
749 return MSG_NOT_HANDLED
;
752 dlg_broadcast_msg_to (h
, MSG_IDLE
, FALSE
, W_WANT_IDLE
);
756 /* this is default resizing mechanism */
757 /* the main idea of this code is to resize dialog
758 according to flags (if any of flags require automatic
759 resizing, like DLG_CENTER, end after that reposition
760 controls in dialog according to flags of widget) */
761 dlg_set_size (h
, w
->lines
, w
->cols
);
768 return MSG_NOT_HANDLED
;
771 /* --------------------------------------------------------------------------------------------- */
774 dlg_create (gboolean modal
, int y1
, int x1
, int lines
, int cols
,
775 const int *colors
, widget_cb_fn callback
, mouse_h mouse_handler
,
776 const char *help_ctx
, const char *title
, dlg_flags_t flags
)
781 new_d
= g_new0 (WDialog
, 1);
783 widget_init (w
, y1
, x1
, lines
, cols
, (callback
!= NULL
) ? callback
: dlg_default_callback
,
785 widget_want_cursor (w
, FALSE
);
787 new_d
->state
= DLG_CONSTRUCT
;
788 new_d
->modal
= modal
;
789 new_d
->color
= colors
;
790 new_d
->help_ctx
= help_ctx
;
791 new_d
->flags
= flags
;
794 dlg_set_size (new_d
, lines
, cols
);
795 new_d
->fullscreen
= (w
->x
== 0 && w
->y
== 0 && w
->cols
== COLS
&& w
->lines
== LINES
);
797 new_d
->mouse_status
= MOU_UNHANDLED
;
799 /* Strip existing spaces, add one space before and after the title */
800 if (title
!= NULL
&& *title
!= '\0')
804 t
= g_strstrip (g_strdup (title
));
806 new_d
->title
= g_strdup_printf (" %s ", t
);
810 /* unique name of event group for this dialog */
811 new_d
->event_group
= g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG
, (void *) new_d
);
816 /* --------------------------------------------------------------------------------------------- */
819 dlg_set_default_colors (void)
821 dialog_colors
[DLG_COLOR_NORMAL
] = COLOR_NORMAL
;
822 dialog_colors
[DLG_COLOR_FOCUS
] = COLOR_FOCUS
;
823 dialog_colors
[DLG_COLOR_HOT_NORMAL
] = COLOR_HOT_NORMAL
;
824 dialog_colors
[DLG_COLOR_HOT_FOCUS
] = COLOR_HOT_FOCUS
;
825 dialog_colors
[DLG_COLOR_TITLE
] = COLOR_TITLE
;
827 alarm_colors
[DLG_COLOR_NORMAL
] = ERROR_COLOR
;
828 alarm_colors
[DLG_COLOR_FOCUS
] = ERROR_FOCUS
;
829 alarm_colors
[DLG_COLOR_HOT_NORMAL
] = ERROR_HOT_NORMAL
;
830 alarm_colors
[DLG_COLOR_HOT_FOCUS
] = ERROR_HOT_FOCUS
;
831 alarm_colors
[DLG_COLOR_TITLE
] = ERROR_TITLE
;
833 listbox_colors
[DLG_COLOR_NORMAL
] = PMENU_ENTRY_COLOR
;
834 listbox_colors
[DLG_COLOR_FOCUS
] = PMENU_SELECTED_COLOR
;
835 listbox_colors
[DLG_COLOR_HOT_NORMAL
] = PMENU_ENTRY_COLOR
;
836 listbox_colors
[DLG_COLOR_HOT_FOCUS
] = PMENU_SELECTED_COLOR
;
837 listbox_colors
[DLG_COLOR_TITLE
] = PMENU_TITLE_COLOR
;
840 /* --------------------------------------------------------------------------------------------- */
843 dlg_erase (WDialog
* h
)
845 if ((h
!= NULL
) && (h
->state
== DLG_ACTIVE
))
847 Widget
*wh
= WIDGET (h
);
849 tty_fill_region (wh
->y
, wh
->x
, wh
->lines
, wh
->cols
, ' ');
853 /* --------------------------------------------------------------------------------------------- */
855 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
859 add_widget_autopos (WDialog
* h
, void *w
, widget_pos_flags_t pos_flags
, const void *before
)
861 Widget
*wh
= WIDGET (h
);
864 /* Don't accept 0 widgets */
870 if ((pos_flags
& WPOS_CENTER_HORZ
) != 0)
871 widget
->x
= (wh
->cols
- widget
->cols
) / 2;
874 if ((pos_flags
& WPOS_CENTER_VERT
) != 0)
875 widget
->y
= (wh
->lines
- widget
->lines
) / 2;
879 widget
->pos_flags
= pos_flags
;
880 widget
->id
= h
->widget_id
++;
882 if (h
->widgets
== NULL
|| before
== NULL
)
884 h
->widgets
= g_list_append (h
->widgets
, widget
);
885 h
->current
= g_list_last (h
->widgets
);
891 b
= g_list_find (h
->widgets
, before
);
893 /* don't accept widget not from dialog. This shouldn't happen */
898 h
->widgets
= g_list_insert_before (h
->widgets
, b
, widget
);
900 h
->current
= g_list_previous (b
);
902 h
->current
= g_list_last (h
->widgets
);
905 /* widget has been added in runtime */
906 if (h
->state
== DLG_ACTIVE
)
908 send_message (widget
, NULL
, MSG_INIT
, 0, NULL
);
909 send_message (widget
, NULL
, MSG_DRAW
, 0, NULL
);
910 send_message (widget
, NULL
, MSG_FOCUS
, 0, NULL
);
916 /* --------------------------------------------------------------------------------------------- */
917 /** wrapper to simply add lefttop positioned controls */
920 add_widget (WDialog
* h
, void *w
)
922 return add_widget_autopos (h
, w
, WPOS_KEEP_DEFAULT
,
923 h
->current
!= NULL
? h
->current
->data
: NULL
);
926 /* --------------------------------------------------------------------------------------------- */
929 add_widget_before (WDialog
* h
, void *w
, void *before
)
931 return add_widget_autopos (h
, w
, WPOS_KEEP_DEFAULT
, before
);
934 /* --------------------------------------------------------------------------------------------- */
936 /** delete widget from dialog */
943 /* Don't accept NULL widget. This shouldn't happen */
947 h
= WIDGET (w
)->owner
;
949 d
= g_list_find (h
->widgets
, w
);
951 h
->current
= dlg_widget_next (h
, d
);
953 h
->widgets
= g_list_remove_link (h
->widgets
, d
);
954 send_message (d
->data
, NULL
, MSG_DESTROY
, 0, NULL
);
958 /* widget has been deleted in runtime */
959 if (h
->state
== DLG_ACTIVE
)
966 /* --------------------------------------------------------------------------------------------- */
975 if ((d
!= NULL
) && (d
->data
!= NULL
))
976 dlg_redraw (DIALOG (d
->data
));
980 /* Search first fullscreen dialog */
981 for (; d
!= NULL
; d
= g_list_next (d
))
982 if (d
->data
!= NULL
&& DIALOG (d
->data
)->fullscreen
)
984 /* back to top dialog */
985 for (; d
!= NULL
; d
= g_list_previous (d
))
987 dlg_redraw (DIALOG (d
->data
));
991 /* --------------------------------------------------------------------------------------------- */
992 /** broadcast a message to all the widgets in a dialog */
995 dlg_broadcast_msg (WDialog
* h
, widget_msg_t msg
)
997 dlg_broadcast_msg_to (h
, msg
, FALSE
, 0);
1000 /* --------------------------------------------------------------------------------------------- */
1003 dlg_focus (WDialog
* h
)
1005 /* cannot focus disabled widget */
1006 if ((h
->current
!= NULL
) && (h
->state
== DLG_CONSTRUCT
|| h
->state
== DLG_ACTIVE
))
1008 Widget
*current
= WIDGET (h
->current
->data
);
1010 if (((current
->options
& W_DISABLED
) == 0)
1011 && (send_message (current
, NULL
, MSG_FOCUS
, 0, NULL
) == MSG_HANDLED
))
1013 send_message (h
, current
, MSG_FOCUS
, 0, NULL
);
1021 /* --------------------------------------------------------------------------------------------- */
1022 /** Find the widget with the given callback in the dialog h */
1025 find_widget_type (const WDialog
* h
, widget_cb_fn callback
)
1029 w
= g_list_find_custom (h
->widgets
, callback
, dlg_find_widget_callback
);
1031 return (w
== NULL
) ? NULL
: WIDGET (w
->data
);
1034 /* --------------------------------------------------------------------------------------------- */
1035 /** Find the widget with the given id */
1038 dlg_find_by_id (const WDialog
* h
, unsigned long id
)
1042 w
= g_list_find_custom (h
->widgets
, GUINT_TO_POINTER (id
), dlg_find_widget_by_id
);
1043 return w
!= NULL
? WIDGET (w
->data
) : NULL
;
1046 /* --------------------------------------------------------------------------------------------- */
1047 /** Find the widget with the given id in the dialog h and select it */
1050 dlg_select_by_id (const WDialog
* h
, unsigned long id
)
1054 w
= dlg_find_by_id (h
, id
);
1056 dlg_select_widget (w
);
1059 /* --------------------------------------------------------------------------------------------- */
1061 * Try to select widget in the dialog.
1065 dlg_select_widget (void *w
)
1067 Widget
*widget
= WIDGET (w
);
1068 WDialog
*h
= widget
->owner
;
1070 do_select_widget (h
, g_list_find (h
->widgets
, widget
), SELECT_EXACT
);
1073 /* --------------------------------------------------------------------------------------------- */
1076 * Set widget at top of widget list and make it current.
1080 dlg_set_top_widget (void *w
)
1082 Widget
*widget
= WIDGET (w
);
1083 WDialog
*h
= widget
->owner
;
1086 l
= g_list_find (h
->widgets
, w
);
1088 abort (); /* widget is not in dialog, this should not happen */
1090 /* unfocus prevoius widget and focus current one before widget reordering */
1091 if (h
->state
== DLG_ACTIVE
)
1092 do_select_widget (h
, l
, SELECT_EXACT
);
1094 /* widget reordering */
1095 h
->widgets
= g_list_remove_link (h
->widgets
, l
);
1096 h
->widgets
= g_list_concat (h
->widgets
, l
);
1100 /* --------------------------------------------------------------------------------------------- */
1101 /** Try to select previous widget in the tab order */
1104 dlg_one_up (WDialog
* h
)
1106 if (h
->widgets
!= NULL
)
1107 do_select_widget (h
, dlg_widget_prev (h
, h
->current
), SELECT_PREV
);
1110 /* --------------------------------------------------------------------------------------------- */
1111 /** Try to select next widget in the tab order */
1114 dlg_one_down (WDialog
* h
)
1116 if (h
->widgets
!= NULL
)
1117 do_select_widget (h
, dlg_widget_next (h
, h
->current
), SELECT_NEXT
);
1120 /* --------------------------------------------------------------------------------------------- */
1123 update_cursor (WDialog
* h
)
1125 GList
*p
= h
->current
;
1127 if ((p
!= NULL
) && (h
->state
== DLG_ACTIVE
))
1131 w
= WIDGET (p
->data
);
1133 if (((w
->options
& W_DISABLED
) == 0) && ((w
->options
& W_WANT_CURSOR
) != 0))
1134 send_message (w
, NULL
, MSG_CURSOR
, 0, NULL
);
1138 p
= dlg_widget_next (h
, p
);
1139 if (p
== h
->current
)
1142 w
= WIDGET (p
->data
);
1144 if (((w
->options
& W_DISABLED
) == 0) && ((w
->options
& W_WANT_CURSOR
) != 0))
1145 if (send_message (w
, NULL
, MSG_CURSOR
, 0, NULL
) == MSG_HANDLED
)
1152 /* --------------------------------------------------------------------------------------------- */
1154 * Redraw the widgets in reverse order, leaving the current widget
1159 dlg_redraw (WDialog
* h
)
1161 if (h
->state
!= DLG_ACTIVE
)
1164 if (h
->winch_pending
)
1166 h
->winch_pending
= FALSE
;
1167 send_message (h
, NULL
, MSG_RESIZE
, 0, NULL
);
1170 send_message (h
, NULL
, MSG_DRAW
, 0, NULL
);
1171 dlg_broadcast_msg (h
, MSG_DRAW
);
1175 /* --------------------------------------------------------------------------------------------- */
1178 dlg_stop (WDialog
* h
)
1180 h
->state
= DLG_CLOSED
;
1183 /* --------------------------------------------------------------------------------------------- */
1184 /** Init the process */
1187 dlg_init (WDialog
* h
)
1189 if (top_dlg
!= NULL
&& DIALOG (top_dlg
->data
)->modal
)
1192 /* add dialog to the stack */
1193 top_dlg
= g_list_prepend (top_dlg
, h
);
1195 /* Initialize dialog manager and widgets */
1196 if (h
->state
== DLG_CONSTRUCT
)
1199 dialog_switch_add (h
);
1201 send_message (h
, NULL
, MSG_INIT
, 0, NULL
);
1202 dlg_broadcast_msg (h
, MSG_INIT
);
1203 dlg_read_history (h
);
1206 h
->state
= DLG_ACTIVE
;
1208 /* first send MSG_DRAW to dialog itself and all widgets... */
1211 /* ...then send MSG_FOCUS to select the first widget that can take focus */
1212 while (h
->current
!= NULL
&& !dlg_focus (h
))
1213 h
->current
= dlg_widget_next (h
, h
->current
);
1219 /* --------------------------------------------------------------------------------------------- */
1222 dlg_process_event (WDialog
* h
, int key
, Gpm_Event
* event
)
1226 if (tty_got_interrupt ())
1227 if (send_message (h
, NULL
, MSG_ACTION
, CK_Cancel
, NULL
) != MSG_HANDLED
)
1228 dlg_execute_cmd (h
, CK_Cancel
);
1233 if (key
== EV_MOUSE
)
1234 h
->mouse_status
= dlg_mouse_event (h
, event
);
1236 dlg_key_event (h
, key
);
1239 /* --------------------------------------------------------------------------------------------- */
1240 /** Shutdown the dlg_run */
1243 dlg_run_done (WDialog
* h
)
1245 top_dlg
= g_list_remove (top_dlg
, h
);
1247 if (h
->state
== DLG_CLOSED
)
1249 send_message (h
, h
->current
->data
, MSG_END
, 0, NULL
);
1251 dialog_switch_remove (h
);
1255 /* --------------------------------------------------------------------------------------------- */
1257 * Standard run dialog routine
1258 * We have to keep this routine small so that we can duplicate it's
1259 * behavior on complex routines like the file routines, this way,
1260 * they can call the dlg_process_event without rewriting all the code
1264 dlg_run (WDialog
* h
)
1267 frontend_dlg_run (h
);
1269 return h
->ret_value
;
1272 /* --------------------------------------------------------------------------------------------- */
1275 dlg_destroy (WDialog
* h
)
1277 /* if some widgets have history, save all history at one moment here */
1278 dlg_save_history (h
);
1279 dlg_broadcast_msg (h
, MSG_DESTROY
);
1280 g_list_free_full (h
->widgets
, g_free
);
1281 mc_event_group_del (h
->event_group
);
1282 g_free (h
->event_group
);
1289 /* --------------------------------------------------------------------------------------------- */
1292 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1295 dlg_save_history (WDialog
* h
)
1300 if (num_history_items_recorded
== 0) /* this is how to disable */
1303 profile
= mc_config_get_full_path (MC_HISTORY_FILE
);
1304 i
= open (profile
, O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
);
1308 /* Make sure the history is only readable by the user */
1309 if (chmod (profile
, S_IRUSR
| S_IWUSR
) != -1 || errno
== ENOENT
)
1311 ev_history_load_save_t event_data
;
1313 event_data
.cfg
= mc_config_init (profile
, FALSE
);
1314 event_data
.receiver
= NULL
;
1316 /* get all histories in dialog */
1317 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_SAVE
, &event_data
);
1319 mc_config_save_file (event_data
.cfg
, NULL
);
1320 mc_config_deinit (event_data
.cfg
);
1326 /* --------------------------------------------------------------------------------------------- */
1329 dlg_get_title (const WDialog
* h
, size_t len
)
1336 if (h
->get_title
!= NULL
)
1337 t
= h
->get_title (h
, len
);
1344 /* --------------------------------------------------------------------------------------------- */