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/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 (Dlg_head
* h
, GList
* l
)
92 next
= g_list_next (l
);
99 /* --------------------------------------------------------------------------------------------- */
102 dlg_widget_prev (Dlg_head
* 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 (Dlg_head
* 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
, msg
, 0);
153 /* --------------------------------------------------------------------------------------------- */
156 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
159 dlg_read_history (Dlg_head
* 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 (Dlg_head
* 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
, WIDGET_UNFOCUS
, 0) == MSG_HANDLED
)
190 h
->callback (h
, current
, DLG_UNFOCUS
, 0, NULL
);
198 /* --------------------------------------------------------------------------------------------- */
201 dlg_find_widget_callback (const void *a
, const void *b
)
203 const Widget
*w
= (const Widget
*) a
;
204 callback_fn f
= (callback_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 (Dlg_head
* 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 (dlg_overlap (w0
, (Widget
*) h
->current
->data
))
251 send_message ((Widget
*) h
->current
->data
, WIDGET_DRAW
, 0);
252 send_message ((Widget
*) h
->current
->data
, WIDGET_FOCUS
, 0);
256 /* --------------------------------------------------------------------------------------------- */
265 /* Use this if the refreshes fail */
268 #endif /* HAVE_SLANG */
271 /* --------------------------------------------------------------------------------------------- */
274 dlg_execute_cmd (Dlg_head
* 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 (Dlg_head
* 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 (h
->callback (h
, NULL
, DLG_ACTION
, command
, NULL
) == MSG_HANDLED
351 || dlg_execute_cmd (h
, command
) == MSG_HANDLED
)
354 return MSG_NOT_HANDLED
;
357 /* --------------------------------------------------------------------------------------------- */
360 dlg_mouse_event (Dlg_head
* h
, Gpm_Event
* event
)
363 GList
*starting_widget
= h
->current
;
367 /* close the dialog by mouse click out of dialog area */
368 if (mouse_close_dialog
&& !h
->fullscreen
&& ((event
->buttons
& GPM_B_LEFT
) != 0) && ((event
->type
& GPM_DOWN
) != 0) /* left click */
369 && !((x
> h
->x
) && (x
<= h
->x
+ h
->cols
) && (y
> h
->y
) && (y
<= h
->y
+ h
->lines
)))
371 h
->ret_value
= B_CANCEL
;
376 if (h
->mouse
!= NULL
)
380 mou
= h
->mouse (event
, h
);
381 if (mou
!= MOU_UNHANDLED
)
385 item
= starting_widget
;
388 Widget
*widget
= (Widget
*) item
->data
;
390 if ((h
->flags
& DLG_REVERSE
) == 0)
391 item
= dlg_widget_prev (h
, item
);
393 item
= dlg_widget_next (h
, item
);
395 if ((widget
->options
& W_DISABLED
) == 0 && widget
->mouse
!= NULL
)
397 /* put global cursor position to the widget */
400 ret
= widget
->mouse (event
, widget
);
401 if (ret
!= MOU_UNHANDLED
)
405 while (item
!= starting_widget
);
407 return MOU_UNHANDLED
;
410 /* --------------------------------------------------------------------------------------------- */
413 dlg_try_hotkey (Dlg_head
* h
, int d_key
)
420 if (h
->widgets
== NULL
)
421 return MSG_NOT_HANDLED
;
423 if (h
->current
== NULL
)
424 h
->current
= h
->widgets
;
427 * Explanation: we don't send letter hotkeys to other widgets if
428 * the currently selected widget is an input line
431 current
= (Widget
*) h
->current
->data
;
433 if ((current
->options
& W_DISABLED
) != 0)
434 return MSG_NOT_HANDLED
;
436 if (current
->options
& W_IS_INPUT
)
438 /* skip ascii control characters, anything else can valid character in
440 if (d_key
>= 32 && d_key
< 256)
441 return MSG_NOT_HANDLED
;
444 /* If it's an alt key, send the message */
445 c
= d_key
& ~ALT (0);
446 if (d_key
& ALT (0) && g_ascii_isalpha (c
))
447 d_key
= g_ascii_tolower (c
);
449 handled
= MSG_NOT_HANDLED
;
450 if ((current
->options
& W_WANT_HOTKEY
) != 0)
451 handled
= send_message (current
, WIDGET_HOTKEY
, d_key
);
453 /* If not used, send hotkey to other widgets */
454 if (handled
== MSG_HANDLED
)
457 hot_cur
= dlg_widget_next (h
, h
->current
);
459 /* send it to all widgets */
460 while (h
->current
!= hot_cur
&& handled
== MSG_NOT_HANDLED
)
462 current
= (Widget
*) hot_cur
->data
;
464 if ((current
->options
& W_WANT_HOTKEY
) != 0 && (current
->options
& W_DISABLED
) == 0)
465 handled
= send_message (current
, WIDGET_HOTKEY
, d_key
);
467 if (handled
== MSG_NOT_HANDLED
)
468 hot_cur
= dlg_widget_next (h
, hot_cur
);
471 if (handled
== MSG_HANDLED
)
472 do_select_widget (h
, hot_cur
, SELECT_EXACT
);
477 /* --------------------------------------------------------------------------------------------- */
480 dlg_key_event (Dlg_head
* h
, int d_key
)
484 if (h
->widgets
== NULL
)
487 if (h
->current
== NULL
)
488 h
->current
= h
->widgets
;
490 /* TAB used to cycle */
491 if ((h
->flags
& DLG_WANT_TAB
) == 0)
498 else if (d_key
== KEY_BTAB
)
505 /* first can dlg_callback handle the key */
506 handled
= h
->callback (h
, NULL
, DLG_KEY
, d_key
, NULL
);
508 /* next try the hotkey */
509 if (handled
== MSG_NOT_HANDLED
)
510 handled
= dlg_try_hotkey (h
, d_key
);
512 if (handled
== MSG_HANDLED
)
513 h
->callback (h
, NULL
, DLG_HOTKEY_HANDLED
, 0, NULL
);
515 /* not used - then try widget_callback */
516 handled
= send_message ((Widget
*) h
->current
->data
, WIDGET_KEY
, d_key
);
518 /* not used- try to use the unhandled case */
519 if (handled
== MSG_NOT_HANDLED
)
520 handled
= h
->callback (h
, NULL
, DLG_UNHANDLED_KEY
, d_key
, NULL
);
522 if (handled
== MSG_NOT_HANDLED
)
523 handled
= dlg_handle_key (h
, d_key
);
525 h
->callback (h
, NULL
, DLG_POST_KEY
, d_key
, NULL
);
528 /* --------------------------------------------------------------------------------------------- */
531 frontend_run_dlg (Dlg_head
* h
)
538 /* close opened editors, viewers, etc */
539 if (!h
->modal
&& mc_global
.midnight_shutdown
)
541 h
->callback (h
, NULL
, DLG_VALIDATE
, 0, NULL
);
545 while (h
->state
== DLG_ACTIVE
)
547 if (mc_global
.tty
.winch_flag
!= 0)
548 dialog_change_screen_size ();
553 execute_hooks (idle_hook
);
555 while ((h
->flags
& DLG_WANT_IDLE
) && is_idle ())
556 h
->callback (h
, NULL
, DLG_IDLE
, 0, NULL
);
558 /* Allow terminating the dialog from the idle handler */
559 if (h
->state
!= DLG_ACTIVE
)
565 /* Clear interrupt flag */
566 tty_got_interrupt ();
567 d_key
= tty_get_event (&event
, h
->mouse_status
== MOU_REPEAT
, TRUE
);
569 dlg_process_event (h
, d_key
, &event
);
571 if (h
->state
== DLG_CLOSED
)
572 h
->callback (h
, NULL
, DLG_VALIDATE
, 0, NULL
);
576 /* --------------------------------------------------------------------------------------------- */
579 dlg_find_widget_by_id (gconstpointer a
, gconstpointer b
)
581 Widget
*w
= (Widget
*) a
;
582 unsigned long id
= GPOINTER_TO_UINT (b
);
584 return w
->id
== id
? 0 : 1;
587 /* --------------------------------------------------------------------------------------------- */
588 /*** public functions ****************************************************************************/
589 /* --------------------------------------------------------------------------------------------- */
591 /** draw box in window */
593 draw_box (Dlg_head
* h
, int y
, int x
, int ys
, int xs
, gboolean single
)
595 tty_draw_box (h
->y
+ y
, h
->x
+ x
, ys
, xs
, single
);
598 /* --------------------------------------------------------------------------------------------- */
600 /** Clean the dialog area, draw the frame and the title */
602 common_dialog_repaint (Dlg_head
* h
)
606 if (h
->state
!= DLG_ACTIVE
)
609 space
= (h
->flags
& DLG_COMPACT
) ? 0 : 1;
611 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
613 draw_box (h
, space
, space
, h
->lines
- 2 * space
, h
->cols
- 2 * space
, FALSE
);
615 if (h
->title
!= NULL
)
617 tty_setcolor (h
->color
[DLG_COLOR_TITLE
]);
618 dlg_move (h
, space
, (h
->cols
- str_term_width1 (h
->title
)) / 2);
619 tty_print_string (h
->title
);
623 /* --------------------------------------------------------------------------------------------- */
624 /** this function allows to set dialog position */
627 dlg_set_position (Dlg_head
* h
, int y1
, int x1
, int y2
, int x2
)
629 /* save old positions, will be used to reposition childs */
631 int shift_x
, shift_y
, scale_x
, scale_y
;
633 /* save old positions, will be used to reposition childs */
644 /* dialog is empty */
645 if (h
->widgets
== NULL
)
648 if (h
->current
== NULL
)
649 h
->current
= h
->widgets
;
651 /* values by which controls should be moved */
654 scale_x
= h
->cols
- oc
;
655 scale_y
= h
->lines
- ol
;
657 if ((shift_x
!= 0) || (shift_y
!= 0) || (scale_x
!= 0) || (scale_y
!= 0))
661 for (w
= h
->widgets
; w
!= NULL
; w
= g_list_next (w
))
663 /* there are, mainly, 2 generally possible
666 1. control sticks to one side - it
669 2. control sticks to two sides of
670 one direction - it should be sized */
672 Widget
*c
= (Widget
*) w
->data
;
676 int lines
= c
->lines
;
678 if ((c
->pos_flags
& WPOS_KEEP_LEFT
) && (c
->pos_flags
& WPOS_KEEP_RIGHT
))
683 else if (c
->pos_flags
& WPOS_KEEP_LEFT
)
685 else if (c
->pos_flags
& WPOS_KEEP_RIGHT
)
686 x
+= shift_x
+ scale_x
;
688 if ((c
->pos_flags
& WPOS_KEEP_TOP
) && (c
->pos_flags
& WPOS_KEEP_BOTTOM
))
693 else if (c
->pos_flags
& WPOS_KEEP_TOP
)
695 else if (c
->pos_flags
& WPOS_KEEP_BOTTOM
)
696 y
+= shift_y
+ scale_y
;
698 widget_set_size (c
, y
, x
, lines
, cols
);
703 /* --------------------------------------------------------------------------------------------- */
704 /** this function sets only size, leaving positioning to automatic methods */
707 dlg_set_size (Dlg_head
* h
, int lines
, int cols
)
712 if (h
->flags
& DLG_CENTER
)
714 y
= (LINES
- lines
) / 2;
715 x
= (COLS
- cols
) / 2;
718 if ((h
->flags
& DLG_TRYUP
) && (y
> 3))
721 dlg_set_position (h
, y
, x
, y
+ lines
, x
+ cols
);
724 /* --------------------------------------------------------------------------------------------- */
725 /** Default dialog callback */
728 default_dlg_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
737 if (h
->color
!= NULL
)
739 common_dialog_repaint (h
);
742 return MSG_NOT_HANDLED
;
745 dlg_broadcast_msg_to (h
, WIDGET_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
, h
->lines
, h
->cols
);
761 return MSG_NOT_HANDLED
;
764 /* --------------------------------------------------------------------------------------------- */
767 create_dlg (gboolean modal
, int y1
, int x1
, int lines
, int cols
,
768 const int *colors
, dlg_cb_fn callback
, mouse_h mouse_handler
,
769 const char *help_ctx
, const char *title
, dlg_flags_t flags
)
773 new_d
= g_new0 (Dlg_head
, 1);
774 new_d
->state
= DLG_CONSTRUCT
;
775 new_d
->modal
= modal
;
777 memmove (new_d
->color
, colors
, sizeof (dlg_colors_t
));
778 new_d
->help_ctx
= help_ctx
;
779 new_d
->callback
= (callback
!= NULL
) ? callback
: default_dlg_callback
;
780 new_d
->mouse
= mouse_handler
;
783 new_d
->flags
= flags
;
786 dlg_set_size (new_d
, lines
, cols
);
787 new_d
->fullscreen
= (new_d
->x
== 0 && new_d
->y
== 0
788 && new_d
->cols
== COLS
&& new_d
->lines
== LINES
);
790 new_d
->mouse_status
= MOU_UNHANDLED
;
792 /* Strip existing spaces, add one space before and after the title */
797 t
= g_strstrip (g_strdup (title
));
799 new_d
->title
= g_strdup_printf (" %s ", t
);
803 /* unique name got event group for this dialog */
804 new_d
->event_group
= g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG
, (void *) new_d
);
809 /* --------------------------------------------------------------------------------------------- */
812 dlg_set_default_colors (void)
814 dialog_colors
[DLG_COLOR_NORMAL
] = COLOR_NORMAL
;
815 dialog_colors
[DLG_COLOR_FOCUS
] = COLOR_FOCUS
;
816 dialog_colors
[DLG_COLOR_HOT_NORMAL
] = COLOR_HOT_NORMAL
;
817 dialog_colors
[DLG_COLOR_HOT_FOCUS
] = COLOR_HOT_FOCUS
;
818 dialog_colors
[DLG_COLOR_TITLE
] = COLOR_TITLE
;
820 alarm_colors
[DLG_COLOR_NORMAL
] = ERROR_COLOR
;
821 alarm_colors
[DLG_COLOR_FOCUS
] = ERROR_FOCUS
;
822 alarm_colors
[DLG_COLOR_HOT_NORMAL
] = ERROR_HOT_NORMAL
;
823 alarm_colors
[DLG_COLOR_HOT_FOCUS
] = ERROR_HOT_FOCUS
;
824 alarm_colors
[DLG_COLOR_TITLE
] = ERROR_TITLE
;
827 /* --------------------------------------------------------------------------------------------- */
830 dlg_erase (Dlg_head
* h
)
832 if ((h
!= NULL
) && (h
->state
== DLG_ACTIVE
))
833 tty_fill_region (h
->y
, h
->x
, h
->lines
, h
->cols
, ' ');
836 /* --------------------------------------------------------------------------------------------- */
839 set_idle_proc (Dlg_head
* d
, int enable
)
842 d
->flags
|= DLG_WANT_IDLE
;
844 d
->flags
&= ~DLG_WANT_IDLE
;
847 /* --------------------------------------------------------------------------------------------- */
849 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
853 add_widget_autopos (Dlg_head
* h
, void *w
, widget_pos_flags_t pos_flags
, const void *before
)
855 Widget
*widget
= (Widget
*) w
;
857 /* Don't accept 0 widgets */
864 widget
->pos_flags
= pos_flags
;
865 widget
->id
= h
->widget_id
++;
867 if ((h
->flags
& DLG_REVERSE
) != 0)
869 if (h
->widgets
== NULL
|| before
== NULL
)
871 h
->widgets
= g_list_prepend (h
->widgets
, widget
);
872 h
->current
= h
->widgets
;
878 b
= g_list_find (h
->widgets
, before
);
880 /* don't accept widget not from dialog. This shouldn't happen */
884 h
->widgets
= g_list_insert_before (h
->widgets
, b
, widget
);
885 h
->current
= g_list_previous (b
);
890 if (h
->widgets
== NULL
|| before
== NULL
)
892 h
->widgets
= g_list_append (h
->widgets
, widget
);
893 h
->current
= g_list_last (h
->widgets
);
899 b
= g_list_find (h
->widgets
, before
);
901 /* don't accept widget not from dialog. This shouldn't happen */
906 h
->widgets
= g_list_insert_before (h
->widgets
, b
, widget
);
908 h
->current
= g_list_previous (b
);
910 h
->current
= g_list_last (h
->widgets
);
914 /* widget has been added in runtime */
915 if (h
->state
== DLG_ACTIVE
)
917 send_message (widget
, WIDGET_INIT
, 0);
918 send_message (widget
, WIDGET_DRAW
, 0);
919 send_message (widget
, WIDGET_FOCUS
, 0);
925 /* --------------------------------------------------------------------------------------------- */
926 /** wrapper to simply add lefttop positioned controls */
929 add_widget (Dlg_head
* h
, void *w
)
931 return add_widget_autopos (h
, w
, WPOS_KEEP_LEFT
| WPOS_KEEP_TOP
,
932 h
->current
!= NULL
? h
->current
->data
: NULL
);
935 /* --------------------------------------------------------------------------------------------- */
938 add_widget_before (Dlg_head
* h
, void *w
, void *before
)
940 return add_widget_autopos (h
, w
, WPOS_KEEP_LEFT
| WPOS_KEEP_TOP
, before
);
943 /* --------------------------------------------------------------------------------------------- */
945 /** delete widget from dialog */
949 Dlg_head
*h
= ((Widget
*) w
)->owner
;
952 /* Don't accept NULL widget. This shouldn't happen */
956 d
= g_list_find (h
->widgets
, w
);
959 if ((h
->flags
& DLG_REVERSE
) != 0)
960 h
->current
= dlg_widget_prev (h
, d
);
962 h
->current
= dlg_widget_next (h
, d
);
965 h
->widgets
= g_list_remove_link (h
->widgets
, d
);
966 send_message (d
->data
, WIDGET_DESTROY
, 0);
969 /* widget has been deleted in runtime */
970 if (h
->state
== DLG_ACTIVE
)
977 /* --------------------------------------------------------------------------------------------- */
986 if ((d
!= NULL
) && (d
->data
!= NULL
))
987 dlg_redraw ((Dlg_head
*) d
->data
);
991 /* Search first fullscreen dialog */
992 for (; d
!= NULL
; d
= g_list_next (d
))
993 if ((d
->data
!= NULL
) && ((Dlg_head
*) d
->data
)->fullscreen
)
995 /* back to top dialog */
996 for (; d
!= NULL
; d
= g_list_previous (d
))
998 dlg_redraw ((Dlg_head
*) d
->data
);
1002 /* --------------------------------------------------------------------------------------------- */
1003 /** broadcast a message to all the widgets in a dialog */
1006 dlg_broadcast_msg (Dlg_head
* h
, widget_msg_t msg
, gboolean reverse
)
1008 dlg_broadcast_msg_to (h
, msg
, reverse
, 0);
1011 /* --------------------------------------------------------------------------------------------- */
1014 dlg_focus (Dlg_head
* h
)
1016 /* cannot focus disabled widget */
1017 if ((h
->current
!= NULL
) && (h
->state
== DLG_CONSTRUCT
|| h
->state
== DLG_ACTIVE
))
1019 Widget
*current
= (Widget
*) h
->current
->data
;
1021 if (((current
->options
& W_DISABLED
) == 0)
1022 && (send_message (current
, WIDGET_FOCUS
, 0) == MSG_HANDLED
))
1024 h
->callback (h
, current
, DLG_FOCUS
, 0, NULL
);
1032 /* --------------------------------------------------------------------------------------------- */
1033 /** Return true if the windows overlap */
1036 dlg_overlap (Widget
* a
, Widget
* b
)
1038 return !((b
->x
>= a
->x
+ a
->cols
)
1039 || (a
->x
>= b
->x
+ b
->cols
) || (b
->y
>= a
->y
+ a
->lines
) || (a
->y
>= b
->y
+ b
->lines
));
1043 /* --------------------------------------------------------------------------------------------- */
1044 /** Find the widget with the given callback in the dialog h */
1047 find_widget_type (const Dlg_head
* h
, callback_fn callback
)
1051 w
= g_list_find_custom (h
->widgets
, callback
, dlg_find_widget_callback
);
1053 return (w
== NULL
) ? NULL
: (Widget
*) w
->data
;
1056 /* --------------------------------------------------------------------------------------------- */
1057 /** Find the widget with the given id */
1060 dlg_find_by_id (const Dlg_head
* h
, unsigned long id
)
1064 w
= g_list_find_custom (h
->widgets
, GUINT_TO_POINTER (id
), dlg_find_widget_by_id
);
1065 return w
!= NULL
? (Widget
*) w
->data
: NULL
;
1068 /* --------------------------------------------------------------------------------------------- */
1069 /** Find the widget with the given id in the dialog h and select it */
1072 dlg_select_by_id (const Dlg_head
* h
, unsigned long id
)
1076 w
= dlg_find_by_id (h
, id
);
1078 dlg_select_widget (w
);
1081 /* --------------------------------------------------------------------------------------------- */
1083 * Try to select widget in the dialog.
1087 dlg_select_widget (void *w
)
1089 const Widget
*widget
= (Widget
*) w
;
1090 Dlg_head
*h
= widget
->owner
;
1092 do_select_widget (h
, g_list_find (h
->widgets
, widget
), SELECT_EXACT
);
1095 /* --------------------------------------------------------------------------------------------- */
1098 * Set widget at top of widget list and make it current.
1102 dlg_set_top_widget (void *w
)
1104 Widget
*widget
= (Widget
*) w
;
1105 Dlg_head
*h
= widget
->owner
;
1108 l
= g_list_find (h
->widgets
, w
);
1110 abort (); /* widget is not in dialog, this should not happen */
1112 /* unfocus prevoius widget and focus current one before widget reordering */
1113 if (h
->state
== DLG_ACTIVE
)
1114 do_select_widget (h
, l
, SELECT_EXACT
);
1116 /* widget reordering */
1117 h
->widgets
= g_list_remove_link (h
->widgets
, l
);
1118 if ((h
->flags
& DLG_REVERSE
) != 0)
1119 h
->widgets
= g_list_concat (l
, h
->widgets
);
1121 h
->widgets
= g_list_concat (h
->widgets
, l
);
1125 /* --------------------------------------------------------------------------------------------- */
1126 /** Try to select previous widget in the tab order */
1129 dlg_one_up (Dlg_head
* h
)
1131 if (h
->widgets
!= NULL
)
1132 do_select_widget (h
, dlg_widget_prev (h
, h
->current
), SELECT_PREV
);
1135 /* --------------------------------------------------------------------------------------------- */
1136 /** Try to select next widget in the tab order */
1139 dlg_one_down (Dlg_head
* h
)
1141 if (h
->widgets
!= NULL
)
1142 do_select_widget (h
, dlg_widget_next (h
, h
->current
), SELECT_NEXT
);
1145 /* --------------------------------------------------------------------------------------------- */
1148 update_cursor (Dlg_head
* h
)
1150 GList
*p
= h
->current
;
1152 if ((p
!= NULL
) && (h
->state
== DLG_ACTIVE
))
1156 w
= (Widget
*) p
->data
;
1158 if (((w
->options
& W_DISABLED
) == 0) && ((w
->options
& W_WANT_CURSOR
) != 0))
1159 send_message (w
, WIDGET_CURSOR
, 0);
1163 p
= dlg_widget_next (h
, p
);
1164 if (p
== h
->current
)
1167 w
= (Widget
*) p
->data
;
1169 if (((w
->options
& W_DISABLED
) == 0) && ((w
->options
& W_WANT_CURSOR
) != 0))
1170 if (send_message (w
, WIDGET_CURSOR
, 0) == MSG_HANDLED
)
1177 /* --------------------------------------------------------------------------------------------- */
1179 * Redraw the widgets in reverse order, leaving the current widget
1184 dlg_redraw (Dlg_head
* h
)
1186 if (h
->state
!= DLG_ACTIVE
)
1189 if (h
->winch_pending
)
1191 h
->winch_pending
= FALSE
;
1192 h
->callback (h
, NULL
, DLG_RESIZE
, 0, NULL
);
1195 h
->callback (h
, NULL
, DLG_DRAW
, 0, NULL
);
1196 dlg_broadcast_msg (h
, WIDGET_DRAW
, (h
->flags
& DLG_REVERSE
) != 0);
1200 /* --------------------------------------------------------------------------------------------- */
1203 dlg_stop (Dlg_head
* h
)
1205 h
->state
= DLG_CLOSED
;
1208 /* --------------------------------------------------------------------------------------------- */
1209 /** Init the process */
1212 init_dlg (Dlg_head
* h
)
1214 if ((top_dlg
!= NULL
) && ((Dlg_head
*) top_dlg
->data
)->modal
)
1217 /* add dialog to the stack */
1218 top_dlg
= g_list_prepend (top_dlg
, h
);
1220 /* Initialize dialog manager and widgets */
1221 if (h
->state
== DLG_CONSTRUCT
)
1224 dialog_switch_add (h
);
1226 h
->callback (h
, NULL
, DLG_INIT
, 0, NULL
);
1227 dlg_broadcast_msg (h
, WIDGET_INIT
, FALSE
);
1228 dlg_read_history (h
);
1231 h
->state
= DLG_ACTIVE
;
1233 /* Select the first widget that takes focus */
1234 while (h
->current
!= NULL
&& !dlg_focus (h
))
1235 h
->current
= dlg_widget_next (h
, h
->current
);
1242 /* --------------------------------------------------------------------------------------------- */
1245 dlg_process_event (Dlg_head
* h
, int key
, Gpm_Event
* event
)
1249 if (tty_got_interrupt ())
1250 if (h
->callback (h
, NULL
, DLG_ACTION
, CK_Cancel
, NULL
) != MSG_HANDLED
)
1251 dlg_execute_cmd (h
, CK_Cancel
);
1256 if (key
== EV_MOUSE
)
1257 h
->mouse_status
= dlg_mouse_event (h
, event
);
1259 dlg_key_event (h
, key
);
1262 /* --------------------------------------------------------------------------------------------- */
1263 /** Shutdown the run_dlg */
1266 dlg_run_done (Dlg_head
* h
)
1268 top_dlg
= g_list_remove (top_dlg
, h
);
1270 if (h
->state
== DLG_CLOSED
)
1272 h
->callback (h
, (Widget
*) h
->current
->data
, DLG_END
, 0, NULL
);
1274 dialog_switch_remove (h
);
1279 /* --------------------------------------------------------------------------------------------- */
1281 * Standard run dialog routine
1282 * We have to keep this routine small so that we can duplicate it's
1283 * behavior on complex routines like the file routines, this way,
1284 * they can call the dlg_process_event without rewriting all the code
1288 run_dlg (Dlg_head
* h
)
1291 frontend_run_dlg (h
);
1293 return h
->ret_value
;
1296 /* --------------------------------------------------------------------------------------------- */
1299 destroy_dlg (Dlg_head
* h
)
1301 /* if some widgets have history, save all history at one moment here */
1302 dlg_save_history (h
);
1303 dlg_broadcast_msg (h
, WIDGET_DESTROY
, FALSE
);
1304 g_list_foreach (h
->widgets
, (GFunc
) g_free
, NULL
);
1305 g_list_free (h
->widgets
);
1306 mc_event_group_del (h
->event_group
);
1307 g_free (h
->event_group
);
1314 /* --------------------------------------------------------------------------------------------- */
1317 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1320 dlg_save_history (Dlg_head
* h
)
1325 if (num_history_items_recorded
== 0) /* this is how to disable */
1328 profile
= mc_config_get_full_path (MC_HISTORY_FILE
);
1329 i
= open (profile
, O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
);
1333 /* Make sure the history is only readable by the user */
1334 if (chmod (profile
, S_IRUSR
| S_IWUSR
) != -1 || errno
== ENOENT
)
1336 ev_history_load_save_t event_data
;
1338 event_data
.cfg
= mc_config_init (profile
, FALSE
);
1339 event_data
.receiver
= NULL
;
1341 /* get all histories in dialog */
1342 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_SAVE
, &event_data
);
1344 mc_config_save_file (event_data
.cfg
, NULL
);
1345 mc_config_deinit (event_data
.cfg
);
1351 /* --------------------------------------------------------------------------------------------- */
1354 dlg_get_title (const Dlg_head
* h
, size_t len
)
1361 if (h
->get_title
!= NULL
)
1362 t
= h
->get_title (h
, len
);
1369 /* --------------------------------------------------------------------------------------------- */
1370 /** Replace widget old_w for widget new_w in the dialog */
1373 dlg_replace_widget (Widget
* old_w
, Widget
* new_w
)
1375 Dlg_head
*h
= old_w
->owner
;
1376 gboolean should_focus
= FALSE
;
1378 if (h
->widgets
== NULL
)
1381 if (h
->current
== NULL
)
1382 h
->current
= h
->widgets
;
1384 if (old_w
== h
->current
->data
)
1385 should_focus
= TRUE
;
1388 new_w
->id
= old_w
->id
;
1391 h
->current
->data
= new_w
;
1393 g_list_find (h
->widgets
, old_w
)->data
= new_w
;
1395 send_message (old_w
, WIDGET_DESTROY
, 0);
1396 send_message (new_w
, WIDGET_INIT
, 0);
1399 dlg_select_widget (new_w
);
1401 if (new_w
->owner
->state
== DLG_ACTIVE
)
1402 send_message (new_w
, WIDGET_DRAW
, 0);
1405 /* --------------------------------------------------------------------------------------------- */