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
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/mouse.h"
44 #include "lib/tty/key.h"
45 #include "lib/strutil.h"
46 #include "lib/widget.h"
47 #include "lib/fileloc.h" /* MC_HISTORY_FILE */
48 #include "lib/event.h" /* mc_event_raise() */
50 /*** global variables ****************************************************************************/
52 /* Color styles for normal and error dialogs */
53 dlg_colors_t dialog_colors
;
54 dlg_colors_t alarm_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 (Dlg_head
* h
, GList
* l
)
93 next
= g_list_next (l
);
100 /* --------------------------------------------------------------------------------------------- */
103 dlg_widget_prev (Dlg_head
* 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 (Dlg_head
* 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
, msg
, 0);
154 /* --------------------------------------------------------------------------------------------- */
157 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
160 dlg_read_history (Dlg_head
* h
)
163 ev_history_load_save_t event_data
;
165 if (num_history_items_recorded
== 0) /* this is how to disable */
168 profile
= g_build_filename (mc_config_get_cache_path (), MC_HISTORY_FILE
, NULL
);
169 event_data
.cfg
= mc_config_init (profile
);
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 (Dlg_head
* h
)
184 /* ... but can unfocus disabled widget */
186 if ((h
->current
!= NULL
) && (h
->state
== DLG_ACTIVE
))
188 Widget
*current
= (Widget
*) h
->current
->data
;
190 if (send_message (current
, WIDGET_UNFOCUS
, 0) == MSG_HANDLED
)
192 h
->callback (h
, current
, DLG_UNFOCUS
, 0, NULL
);
200 /* --------------------------------------------------------------------------------------------- */
203 dlg_find_widget_callback (const void *a
, const void *b
)
205 const Widget
*w
= (const Widget
*) a
;
206 callback_fn f
= (callback_fn
) b
;
208 return (w
->callback
== f
) ? 0 : 1;
211 /* --------------------------------------------------------------------------------------------- */
213 * Try to select another widget. If forward is set, follow tab order.
214 * Otherwise go to the previous widget.
218 do_select_widget (Dlg_head
* h
, GList
* w
, select_dir_t dir
)
220 Widget
*w0
= (Widget
*) h
->current
->data
;
222 if (!dlg_unfocus (h
))
235 h
->current
= g_list_find (h
->widgets
, w0
);
238 /* try find another widget that can take focus */
242 h
->current
= dlg_widget_next (h
, h
->current
);
245 h
->current
= dlg_widget_prev (h
, h
->current
);
249 while (h
->current
!= w
/* && (((Widget *) h->current->data)->options & W_DISABLED) == 0 */ );
251 if (dlg_overlap (w0
, (Widget
*) h
->current
->data
))
253 send_message ((Widget
*) h
->current
->data
, WIDGET_DRAW
, 0);
254 send_message ((Widget
*) h
->current
->data
, WIDGET_FOCUS
, 0);
258 /* --------------------------------------------------------------------------------------------- */
267 /* Use this if the refreshes fail */
270 #endif /* HAVE_SLANG */
273 /* --------------------------------------------------------------------------------------------- */
276 dlg_execute_cmd (Dlg_head
* h
, unsigned long command
)
278 cb_ret_t ret
= MSG_HANDLED
;
282 h
->ret_value
= B_ENTER
;
286 h
->ret_value
= B_CANCEL
;
301 ev_help_t event_data
= { NULL
, h
->help_ctx
};
302 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
307 mc_event_raise (MCEVENT_GROUP_CORE
, "suspend", NULL
);
316 dialog_switch_list ();
318 ret
= MSG_NOT_HANDLED
;
322 dialog_switch_next ();
324 ret
= MSG_NOT_HANDLED
;
328 dialog_switch_prev ();
330 ret
= MSG_NOT_HANDLED
;
334 ret
= MSG_NOT_HANDLED
;
340 /* --------------------------------------------------------------------------------------------- */
343 dlg_handle_key (Dlg_head
* h
, int d_key
)
345 unsigned long command
;
346 command
= keybind_lookup_keymap_command (dialog_map
, d_key
);
347 if ((command
== CK_IgnoreKey
) || (dlg_execute_cmd (h
, command
) == MSG_NOT_HANDLED
))
348 return MSG_NOT_HANDLED
;
353 /* --------------------------------------------------------------------------------------------- */
356 dlg_mouse_event (Dlg_head
* h
, Gpm_Event
* event
)
359 GList
*starting_widget
= h
->current
;
364 /* close the dialog by mouse click out of dialog area */
365 if (mouse_close_dialog
&& !h
->fullscreen
&& ((event
->buttons
& GPM_B_LEFT
) != 0) && ((event
->type
& GPM_DOWN
) != 0) /* left click */
366 && !((x
> h
->x
) && (x
<= h
->x
+ h
->cols
) && (y
> h
->y
) && (y
<= h
->y
+ h
->lines
)))
368 h
->ret_value
= B_CANCEL
;
373 item
= starting_widget
;
378 widget
= (Widget
*) item
->data
;
379 item
= dlg_widget_next (h
, item
);
381 if (((widget
->options
& W_DISABLED
) == 0)
382 && (x
> widget
->x
) && (x
<= widget
->x
+ widget
->cols
)
383 && (y
> widget
->y
) && (y
<= widget
->y
+ widget
->lines
))
386 new_event
.x
-= widget
->x
;
387 new_event
.y
-= widget
->y
;
389 if (widget
->mouse
!= NULL
)
390 return widget
->mouse (&new_event
, widget
);
393 while (item
!= starting_widget
);
398 /* --------------------------------------------------------------------------------------------- */
401 dlg_try_hotkey (Dlg_head
* h
, int d_key
)
408 if (h
->widgets
== NULL
)
409 return MSG_NOT_HANDLED
;
411 if (h
->current
== NULL
)
412 h
->current
= h
->widgets
;
415 * Explanation: we don't send letter hotkeys to other widgets if
416 * the currently selected widget is an input line
419 current
= (Widget
*) h
->current
->data
;
421 if ((current
->options
& W_DISABLED
) != 0)
422 return MSG_NOT_HANDLED
;
424 if (current
->options
& W_IS_INPUT
)
426 /* skip ascii control characters, anything else can valid character in
428 if (d_key
>= 32 && d_key
< 256)
429 return MSG_NOT_HANDLED
;
432 /* If it's an alt key, send the message */
433 c
= d_key
& ~ALT (0);
434 if (d_key
& ALT (0) && g_ascii_isalpha (c
))
435 d_key
= g_ascii_tolower (c
);
437 handled
= MSG_NOT_HANDLED
;
438 if ((current
->options
& W_WANT_HOTKEY
) != 0)
439 handled
= send_message (current
, WIDGET_HOTKEY
, d_key
);
441 /* If not used, send hotkey to other widgets */
442 if (handled
== MSG_HANDLED
)
445 hot_cur
= dlg_widget_next (h
, h
->current
);
447 /* send it to all widgets */
448 while (h
->current
!= hot_cur
&& handled
== MSG_NOT_HANDLED
)
450 current
= (Widget
*) hot_cur
->data
;
452 if ((current
->options
& W_WANT_HOTKEY
) != 0)
453 handled
= send_message (current
, WIDGET_HOTKEY
, d_key
);
455 if (handled
== MSG_NOT_HANDLED
)
456 hot_cur
= dlg_widget_next (h
, hot_cur
);
459 if (handled
== MSG_HANDLED
)
460 do_select_widget (h
, hot_cur
, SELECT_EXACT
);
465 /* --------------------------------------------------------------------------------------------- */
468 dlg_key_event (Dlg_head
* h
, int d_key
)
472 if (h
->widgets
== NULL
)
475 if (h
->current
== NULL
)
476 h
->current
= h
->widgets
;
478 /* TAB used to cycle */
479 if ((h
->flags
& DLG_WANT_TAB
) == 0)
486 else if (d_key
== KEY_BTAB
)
493 /* first can dlg_callback handle the key */
494 handled
= h
->callback (h
, NULL
, DLG_KEY
, d_key
, NULL
);
496 /* next try the hotkey */
497 if (handled
== MSG_NOT_HANDLED
)
498 handled
= dlg_try_hotkey (h
, d_key
);
500 if (handled
== MSG_HANDLED
)
501 h
->callback (h
, NULL
, DLG_HOTKEY_HANDLED
, 0, NULL
);
503 /* not used - then try widget_callback */
504 handled
= send_message ((Widget
*) h
->current
->data
, WIDGET_KEY
, d_key
);
506 /* not used- try to use the unhandled case */
507 if (handled
== MSG_NOT_HANDLED
)
508 handled
= h
->callback (h
, NULL
, DLG_UNHANDLED_KEY
, d_key
, NULL
);
510 if (handled
== MSG_NOT_HANDLED
)
511 handled
= dlg_handle_key (h
, d_key
);
513 h
->callback (h
, NULL
, DLG_POST_KEY
, d_key
, NULL
);
516 /* --------------------------------------------------------------------------------------------- */
519 frontend_run_dlg (Dlg_head
* h
)
526 /* close opened editors, viewers, etc */
527 if (!h
->modal
&& mc_global
.widget
.midnight_shutdown
)
529 h
->callback (h
, NULL
, DLG_VALIDATE
, 0, NULL
);
533 while (h
->state
== DLG_ACTIVE
)
535 if (mc_global
.tty
.winch_flag
)
536 dialog_change_screen_size ();
541 execute_hooks (idle_hook
);
543 while ((h
->flags
& DLG_WANT_IDLE
) && is_idle ())
544 h
->callback (h
, NULL
, DLG_IDLE
, 0, NULL
);
546 /* Allow terminating the dialog from the idle handler */
547 if (h
->state
!= DLG_ACTIVE
)
553 /* Clear interrupt flag */
554 tty_got_interrupt ();
555 d_key
= tty_get_event (&event
, h
->mouse_status
== MOU_REPEAT
, TRUE
);
557 dlg_process_event (h
, d_key
, &event
);
559 if (h
->state
== DLG_CLOSED
)
560 h
->callback (h
, NULL
, DLG_VALIDATE
, 0, NULL
);
564 /* --------------------------------------------------------------------------------------------- */
567 dlg_find_widget_by_id (gconstpointer a
, gconstpointer b
)
569 Widget
*w
= (Widget
*) a
;
570 unsigned long id
= GPOINTER_TO_UINT (b
);
572 return w
->id
== id
? 0 : 1;
575 /* --------------------------------------------------------------------------------------------- */
576 /*** public functions ****************************************************************************/
577 /* --------------------------------------------------------------------------------------------- */
579 /** draw box in window */
581 draw_box (Dlg_head
* h
, int y
, int x
, int ys
, int xs
, gboolean single
)
583 tty_draw_box (h
->y
+ y
, h
->x
+ x
, ys
, xs
, single
);
586 /* --------------------------------------------------------------------------------------------- */
588 /** Clean the dialog area, draw the frame and the title */
590 common_dialog_repaint (Dlg_head
* h
)
594 if (h
->state
!= DLG_ACTIVE
)
597 space
= (h
->flags
& DLG_COMPACT
) ? 0 : 1;
599 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
601 draw_box (h
, space
, space
, h
->lines
- 2 * space
, h
->cols
- 2 * space
, FALSE
);
603 if (h
->title
!= NULL
)
605 tty_setcolor (h
->color
[DLG_COLOR_TITLE
]);
606 dlg_move (h
, space
, (h
->cols
- str_term_width1 (h
->title
)) / 2);
607 tty_print_string (h
->title
);
611 /* --------------------------------------------------------------------------------------------- */
612 /** this function allows to set dialog position */
615 dlg_set_position (Dlg_head
* h
, int y1
, int x1
, int y2
, int x2
)
617 /* save old positions, will be used to reposition childs */
619 int shift_x
, shift_y
, scale_x
, scale_y
;
621 /* save old positions, will be used to reposition childs */
632 /* dialog is empty */
633 if (h
->widgets
== NULL
)
636 if (h
->current
== NULL
)
637 h
->current
= h
->widgets
;
639 /* values by which controls should be moved */
642 scale_x
= h
->cols
- oc
;
643 scale_y
= h
->lines
- ol
;
645 if ((shift_x
!= 0) || (shift_y
!= 0) || (scale_x
!= 0) || (scale_y
!= 0))
649 for (w
= h
->widgets
; w
!= NULL
; w
= g_list_next (w
))
651 /* there are, mainly, 2 generally possible
654 1. control sticks to one side - it
657 2. control sticks to two sides of
658 one direction - it should be sized */
660 Widget
*c
= (Widget
*) w
->data
;
664 int lines
= c
->lines
;
666 if ((c
->pos_flags
& WPOS_KEEP_LEFT
) && (c
->pos_flags
& WPOS_KEEP_RIGHT
))
671 else if (c
->pos_flags
& WPOS_KEEP_LEFT
)
673 else if (c
->pos_flags
& WPOS_KEEP_RIGHT
)
674 x
+= shift_x
+ scale_x
;
676 if ((c
->pos_flags
& WPOS_KEEP_TOP
) && (c
->pos_flags
& WPOS_KEEP_BOTTOM
))
681 else if (c
->pos_flags
& WPOS_KEEP_TOP
)
683 else if (c
->pos_flags
& WPOS_KEEP_BOTTOM
)
684 y
+= shift_y
+ scale_y
;
686 widget_set_size (c
, y
, x
, lines
, cols
);
691 /* --------------------------------------------------------------------------------------------- */
692 /** this function sets only size, leaving positioning to automatic methods */
695 dlg_set_size (Dlg_head
* h
, int lines
, int cols
)
700 if (h
->flags
& DLG_CENTER
)
702 y
= (LINES
- lines
) / 2;
703 x
= (COLS
- cols
) / 2;
706 if ((h
->flags
& DLG_TRYUP
) && (y
> 3))
709 dlg_set_position (h
, y
, x
, y
+ lines
, x
+ cols
);
712 /* --------------------------------------------------------------------------------------------- */
713 /** Default dialog callback */
716 default_dlg_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
725 if (h
->color
!= NULL
)
727 common_dialog_repaint (h
);
730 return MSG_NOT_HANDLED
;
733 dlg_broadcast_msg_to (h
, WIDGET_IDLE
, FALSE
, W_WANT_IDLE
);
737 /* this is default resizing mechanism */
738 /* the main idea of this code is to resize dialog
739 according to flags (if any of flags require automatic
740 resizing, like DLG_CENTER, end after that reposition
741 controls in dialog according to flags of widget) */
742 dlg_set_size (h
, h
->lines
, h
->cols
);
749 return MSG_NOT_HANDLED
;
752 /* --------------------------------------------------------------------------------------------- */
755 create_dlg (gboolean modal
, int y1
, int x1
, int lines
, int cols
,
756 const int *colors
, dlg_cb_fn callback
, const char *help_ctx
,
757 const char *title
, dlg_flags_t flags
)
761 new_d
= g_new0 (Dlg_head
, 1);
762 new_d
->modal
= modal
;
764 memmove (new_d
->color
, colors
, sizeof (dlg_colors_t
));
765 new_d
->help_ctx
= help_ctx
;
766 new_d
->callback
= (callback
!= NULL
) ? callback
: default_dlg_callback
;
769 new_d
->flags
= flags
;
772 dlg_set_size (new_d
, lines
, cols
);
773 new_d
->fullscreen
= (new_d
->x
== 0 && new_d
->y
== 0
774 && new_d
->cols
== COLS
&& new_d
->lines
== LINES
);
776 new_d
->mouse_status
= MOU_NORMAL
;
778 /* Strip existing spaces, add one space before and after the title */
783 t
= g_strstrip (g_strdup (title
));
785 new_d
->title
= g_strdup_printf (" %s ", t
);
789 /* unique name got event group for this dialog */
790 new_d
->event_group
= g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG
, (void *) new_d
);
795 /* --------------------------------------------------------------------------------------------- */
798 dlg_set_default_colors (void)
800 dialog_colors
[DLG_COLOR_NORMAL
] = COLOR_NORMAL
;
801 dialog_colors
[DLG_COLOR_FOCUS
] = COLOR_FOCUS
;
802 dialog_colors
[DLG_COLOR_HOT_NORMAL
] = COLOR_HOT_NORMAL
;
803 dialog_colors
[DLG_COLOR_HOT_FOCUS
] = COLOR_HOT_FOCUS
;
804 dialog_colors
[DLG_COLOR_TITLE
] = COLOR_TITLE
;
806 alarm_colors
[DLG_COLOR_NORMAL
] = ERROR_COLOR
;
807 alarm_colors
[DLG_COLOR_FOCUS
] = ERROR_FOCUS
;
808 alarm_colors
[DLG_COLOR_HOT_NORMAL
] = ERROR_HOT_NORMAL
;
809 alarm_colors
[DLG_COLOR_HOT_FOCUS
] = ERROR_HOT_FOCUS
;
810 alarm_colors
[DLG_COLOR_TITLE
] = ERROR_TITLE
;
813 /* --------------------------------------------------------------------------------------------- */
816 dlg_erase (Dlg_head
* h
)
818 if ((h
!= NULL
) && (h
->state
== DLG_ACTIVE
))
819 tty_fill_region (h
->y
, h
->x
, h
->lines
, h
->cols
, ' ');
822 /* --------------------------------------------------------------------------------------------- */
825 set_idle_proc (Dlg_head
* d
, int enable
)
828 d
->flags
|= DLG_WANT_IDLE
;
830 d
->flags
&= ~DLG_WANT_IDLE
;
833 /* --------------------------------------------------------------------------------------------- */
835 * Insert widget to dialog before current widget. For dialogs populated
836 * from the bottom, make the widget current. Return widget number.
840 add_widget_autopos (Dlg_head
* h
, void *w
, widget_pos_flags_t pos_flags
)
842 Widget
*widget
= (Widget
*) w
;
844 /* Don't accept 0 widgets */
851 widget
->pos_flags
= pos_flags
;
852 widget
->id
= g_list_length (h
->widgets
);
854 if ((h
->flags
& DLG_REVERSE
) != 0)
855 h
->widgets
= g_list_prepend (h
->widgets
, widget
);
857 h
->widgets
= g_list_append (h
->widgets
, widget
);
859 h
->current
= h
->widgets
;
864 /* --------------------------------------------------------------------------------------------- */
865 /** wrapper to simply add lefttop positioned controls */
868 add_widget (Dlg_head
* h
, void *w
)
870 return add_widget_autopos (h
, w
, WPOS_KEEP_LEFT
| WPOS_KEEP_TOP
);
873 /* --------------------------------------------------------------------------------------------- */
882 if ((d
!= NULL
) && (d
->data
!= NULL
))
883 dlg_redraw ((Dlg_head
*) d
->data
);
887 /* Search first fullscreen dialog */
888 for (; d
!= NULL
; d
= g_list_next (d
))
889 if ((d
->data
!= NULL
) && ((Dlg_head
*) d
->data
)->fullscreen
)
891 /* back to top dialog */
892 for (; d
!= NULL
; d
= g_list_previous (d
))
894 dlg_redraw ((Dlg_head
*) d
->data
);
898 /* --------------------------------------------------------------------------------------------- */
899 /** broadcast a message to all the widgets in a dialog */
902 dlg_broadcast_msg (Dlg_head
* h
, widget_msg_t msg
, gboolean reverse
)
904 dlg_broadcast_msg_to (h
, msg
, reverse
, 0);
907 /* --------------------------------------------------------------------------------------------- */
910 dlg_focus (Dlg_head
* h
)
912 /* cannot focus disabled widget ... */
914 if ((h
->current
!= NULL
) && (h
->state
== DLG_ACTIVE
))
916 Widget
*current
= (Widget
*) h
->current
->data
;
918 if (((current
->options
& W_DISABLED
) == 0)
919 && (send_message (current
, WIDGET_FOCUS
, 0) == MSG_HANDLED
))
921 h
->callback (h
, current
, DLG_FOCUS
, 0, NULL
);
929 /* --------------------------------------------------------------------------------------------- */
930 /** Return true if the windows overlap */
933 dlg_overlap (Widget
* a
, Widget
* b
)
935 return !((b
->x
>= a
->x
+ a
->cols
)
936 || (a
->x
>= b
->x
+ b
->cols
) || (b
->y
>= a
->y
+ a
->lines
) || (a
->y
>= b
->y
+ b
->lines
));
940 /* --------------------------------------------------------------------------------------------- */
941 /** Find the widget with the given callback in the dialog h */
944 find_widget_type (const Dlg_head
* h
, callback_fn callback
)
948 w
= g_list_find_custom (h
->widgets
, callback
, dlg_find_widget_callback
);
950 return (w
== NULL
) ? NULL
: (Widget
*) w
->data
;
953 /* --------------------------------------------------------------------------------------------- */
954 /** Find the widget with the given id */
957 dlg_find_by_id (const Dlg_head
* h
, unsigned int id
)
961 w
= g_list_find_custom (h
->widgets
, GUINT_TO_POINTER (id
), dlg_find_widget_by_id
);
962 return w
!= NULL
? (Widget
*) w
->data
: NULL
;
965 /* --------------------------------------------------------------------------------------------- */
966 /** Find the widget with the given id in the dialog h and select it */
969 dlg_select_by_id (const Dlg_head
* h
, unsigned int id
)
973 w
= dlg_find_by_id (h
, id
);
975 dlg_select_widget (w
);
978 /* --------------------------------------------------------------------------------------------- */
980 * Try to select widget in the dialog.
984 dlg_select_widget (void *w
)
986 const Widget
*widget
= (Widget
*) w
;
987 Dlg_head
*h
= widget
->owner
;
989 do_select_widget (h
, g_list_find (h
->widgets
, widget
), SELECT_EXACT
);
992 /* --------------------------------------------------------------------------------------------- */
993 /** Try to select previous widget in the tab order */
996 dlg_one_up (Dlg_head
* h
)
998 if (h
->widgets
!= NULL
)
999 do_select_widget (h
, dlg_widget_prev (h
, h
->current
), SELECT_PREV
);
1002 /* --------------------------------------------------------------------------------------------- */
1003 /** Try to select next widget in the tab order */
1006 dlg_one_down (Dlg_head
* h
)
1008 if (h
->widgets
!= NULL
)
1009 do_select_widget (h
, dlg_widget_next (h
, h
->current
), SELECT_NEXT
);
1012 /* --------------------------------------------------------------------------------------------- */
1015 update_cursor (Dlg_head
* h
)
1017 GList
*p
= h
->current
;
1019 if ((p
!= NULL
) && (h
->state
== DLG_ACTIVE
))
1023 w
= (Widget
*) p
->data
;
1025 if (((w
->options
& W_DISABLED
) == 0) && ((w
->options
& W_WANT_CURSOR
) != 0))
1026 send_message (w
, WIDGET_CURSOR
, 0);
1030 p
= dlg_widget_next (h
, p
);
1031 if (p
== h
->current
)
1034 w
= (Widget
*) p
->data
;
1036 if (((w
->options
& W_DISABLED
) == 0) && ((w
->options
& W_WANT_CURSOR
) != 0))
1037 if (send_message (w
, WIDGET_CURSOR
, 0) == MSG_HANDLED
)
1044 /* --------------------------------------------------------------------------------------------- */
1046 * Redraw the widgets in reverse order, leaving the current widget
1051 dlg_redraw (Dlg_head
* h
)
1053 if (h
->state
!= DLG_ACTIVE
)
1056 if (h
->winch_pending
)
1058 h
->winch_pending
= FALSE
;
1059 h
->callback (h
, NULL
, DLG_RESIZE
, 0, NULL
);
1062 h
->callback (h
, NULL
, DLG_DRAW
, 0, NULL
);
1063 dlg_broadcast_msg (h
, WIDGET_DRAW
, TRUE
);
1067 /* --------------------------------------------------------------------------------------------- */
1070 dlg_stop (Dlg_head
* h
)
1072 h
->state
= DLG_CLOSED
;
1075 /* --------------------------------------------------------------------------------------------- */
1076 /** Init the process */
1079 init_dlg (Dlg_head
* h
)
1081 if ((top_dlg
!= NULL
) && ((Dlg_head
*) top_dlg
->data
)->modal
)
1084 /* add dialog to the stack */
1085 top_dlg
= g_list_prepend (top_dlg
, h
);
1087 /* Initialize dialog manager and widgets */
1088 if (h
->state
== DLG_ACTIVE
)
1091 dialog_switch_add (h
);
1093 h
->callback (h
, NULL
, DLG_INIT
, 0, NULL
);
1094 dlg_broadcast_msg (h
, WIDGET_INIT
, FALSE
);
1095 dlg_read_history (h
);
1098 h
->state
= DLG_ACTIVE
;
1102 /* Select the first widget that takes focus */
1103 while (h
->current
!= NULL
&& !dlg_focus (h
))
1104 h
->current
= dlg_widget_next (h
, h
->current
);
1109 /* --------------------------------------------------------------------------------------------- */
1112 dlg_process_event (Dlg_head
* h
, int key
, Gpm_Event
* event
)
1116 if (tty_got_interrupt ())
1117 if (h
->callback (h
, NULL
, DLG_ACTION
, CK_Cancel
, NULL
) != MSG_HANDLED
)
1118 dlg_execute_cmd (h
, CK_Cancel
);
1123 if (key
== EV_MOUSE
)
1124 h
->mouse_status
= dlg_mouse_event (h
, event
);
1126 dlg_key_event (h
, key
);
1129 /* --------------------------------------------------------------------------------------------- */
1130 /** Shutdown the run_dlg */
1133 dlg_run_done (Dlg_head
* h
)
1135 top_dlg
= g_list_remove (top_dlg
, h
);
1137 if (h
->state
== DLG_CLOSED
)
1139 h
->callback (h
, (Widget
*) h
->current
->data
, DLG_END
, 0, NULL
);
1141 dialog_switch_remove (h
);
1146 /* --------------------------------------------------------------------------------------------- */
1148 * Standard run dialog routine
1149 * We have to keep this routine small so that we can duplicate it's
1150 * behavior on complex routines like the file routines, this way,
1151 * they can call the dlg_process_event without rewriting all the code
1155 run_dlg (Dlg_head
* h
)
1158 frontend_run_dlg (h
);
1160 return h
->ret_value
;
1163 /* --------------------------------------------------------------------------------------------- */
1166 destroy_dlg (Dlg_head
* h
)
1168 /* if some widgets have history, save all history at one moment here */
1169 dlg_save_history (h
);
1170 dlg_broadcast_msg (h
, WIDGET_DESTROY
, FALSE
);
1171 g_list_foreach (h
->widgets
, (GFunc
) g_free
, NULL
);
1172 g_list_free (h
->widgets
);
1173 mc_event_group_del (h
->event_group
);
1174 g_free (h
->event_group
);
1181 /* --------------------------------------------------------------------------------------------- */
1184 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1187 dlg_save_history (Dlg_head
* h
)
1192 if (num_history_items_recorded
== 0) /* this is how to disable */
1195 profile
= g_build_filename (mc_config_get_cache_path (), MC_HISTORY_FILE
, (char *) NULL
);
1196 i
= open (profile
, O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
);
1200 /* Make sure the history is only readable by the user */
1201 if (chmod (profile
, S_IRUSR
| S_IWUSR
) != -1 || errno
== ENOENT
)
1203 ev_history_load_save_t event_data
;
1205 event_data
.cfg
= mc_config_init (profile
);
1206 event_data
.receiver
= NULL
;
1208 /* get all histories in dialog */
1209 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_SAVE
, &event_data
);
1211 mc_config_save_file (event_data
.cfg
, NULL
);
1212 mc_config_deinit (event_data
.cfg
);
1218 /* --------------------------------------------------------------------------------------------- */
1221 dlg_get_title (const Dlg_head
* h
, size_t len
)
1228 if (h
->get_title
!= NULL
)
1229 t
= h
->get_title (h
, len
);
1236 /* --------------------------------------------------------------------------------------------- */
1237 /** Replace widget old_w for widget new_w in the dialog */
1240 dlg_replace_widget (Widget
* old_w
, Widget
* new_w
)
1242 Dlg_head
*h
= old_w
->owner
;
1243 gboolean should_focus
= FALSE
;
1245 if (h
->widgets
== NULL
)
1248 if (h
->current
== NULL
)
1249 h
->current
= h
->widgets
;
1251 if (old_w
== h
->current
->data
)
1252 should_focus
= TRUE
;
1255 new_w
->id
= old_w
->id
;
1258 h
->current
->data
= new_w
;
1260 g_list_find (h
->widgets
, old_w
)->data
= new_w
;
1262 send_message (old_w
, WIDGET_DESTROY
, 0);
1263 send_message (new_w
, WIDGET_INIT
, 0);
1266 dlg_select_widget (new_w
);
1268 send_message (new_w
, WIDGET_DRAW
, 0);
1271 /* --------------------------------------------------------------------------------------------- */