1 /* Dialog box features module for the Midnight Commander
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2009, 2010 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * \brief Source: dialog box features module
31 #include <sys/types.h>
33 #include <fcntl.h> /* open() */
35 #include "lib/global.h"
37 #include "lib/tty/tty.h"
39 #include "lib/tty/mouse.h"
40 #include "lib/tty/key.h"
41 #include "lib/strutil.h"
42 #include "lib/widget.h"
43 #include "lib/fileloc.h" /* MC_HISTORY_FILE */
44 #include "lib/event.h" /* mc_event_raise() */
46 /*** global variables ****************************************************************************/
48 /* Color styles for normal and error dialogs */
49 dlg_colors_t dialog_colors
;
50 dlg_colors_t alarm_colors
;
52 /* Primitive way to check if the the current dialog is our dialog */
53 /* This is needed by async routines like load_prompt */
54 GList
*top_dlg
= NULL
;
56 /* A hook list for idle events */
57 hook_t
*idle_hook
= NULL
;
59 /* If set then dialogs just clean the screen when refreshing, else */
60 /* they do a complete refresh, refreshing all the parts of the program */
63 /* left click outside of dialog closes it */
64 int mouse_close_dialog
= 0;
66 const global_keymap_t
*dialog_map
= NULL
;
68 /*** file scope macro definitions ****************************************************************/
70 /*** file scope type declarations ****************************************************************/
72 /** What to do if the requested widget doesn't take focus */
75 SELECT_NEXT
, /* go the the next widget */
76 SELECT_PREV
, /* go the the previous widget */
77 SELECT_EXACT
/* use current widget */
80 /*** file scope variables ************************************************************************/
82 /*** file scope functions ************************************************************************/
85 dlg_widget_next (Dlg_head
*h
, GList
*l
)
89 next
= g_list_next (l
);
96 /* --------------------------------------------------------------------------------------------- */
99 dlg_widget_prev (Dlg_head
*h
, GList
*l
)
103 prev
= g_list_previous (l
);
105 prev
= g_list_last (h
->widgets
);
110 /* --------------------------------------------------------------------------------------------- */
112 * broadcast a message to all the widgets in a dialog that have
113 * the options set to flags. If flags is zero, the message is sent
118 dlg_broadcast_msg_to (Dlg_head
* h
, widget_msg_t msg
, gboolean reverse
, int flags
)
122 if (h
->widgets
== NULL
)
125 if (h
->current
== NULL
)
126 h
->current
= h
->widgets
;
129 p
= dlg_widget_prev (h
, h
->current
);
131 p
= dlg_widget_next (h
, h
->current
);
137 Widget
*w
= (Widget
*) p
->data
;
140 p
= dlg_widget_prev (h
, p
);
142 p
= dlg_widget_next (h
, p
);
144 if ((flags
== 0) || ((flags
& w
->options
) != 0))
145 send_message (w
, msg
, 0);
150 /* --------------------------------------------------------------------------------------------- */
153 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
156 dlg_read_history (Dlg_head
* h
)
159 ev_history_load_save_t event_data
;
161 if (num_history_items_recorded
== 0) /* this is how to disable */
164 profile
= g_build_filename (mc_config_get_cache_path (), MC_HISTORY_FILE
, NULL
);
165 event_data
.cfg
= mc_config_init (profile
);
166 event_data
.receiver
= NULL
;
168 /* create all histories in dialog */
169 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_LOAD
, &event_data
);
171 mc_config_deinit (event_data
.cfg
);
175 /* --------------------------------------------------------------------------------------------- */
178 dlg_unfocus (Dlg_head
* h
)
180 /* ... but can unfocus disabled widget */
182 if ((h
->current
!= NULL
) && (h
->state
== DLG_ACTIVE
))
184 Widget
*current
= (Widget
*) h
->current
->data
;
186 if (send_message (current
, WIDGET_UNFOCUS
, 0) == MSG_HANDLED
)
188 h
->callback (h
, current
, DLG_UNFOCUS
, 0, NULL
);
196 /* --------------------------------------------------------------------------------------------- */
199 dlg_find_widget_callback (const void *a
, const void *b
)
201 const Widget
*w
= (const Widget
*) a
;
202 callback_fn f
= (callback_fn
) b
;
204 return (w
->callback
== f
) ? 0 : 1;
207 /* --------------------------------------------------------------------------------------------- */
209 * Try to select another widget. If forward is set, follow tab order.
210 * Otherwise go to the previous widget.
214 do_select_widget (Dlg_head
* h
, GList
* w
, select_dir_t dir
)
216 Widget
*w0
= (Widget
*) h
->current
->data
;
218 if (!dlg_unfocus (h
))
231 h
->current
= g_list_find (h
->widgets
, w0
);
234 /* try find another widget that can take focus */
238 h
->current
= dlg_widget_next (h
, h
->current
);
241 h
->current
= dlg_widget_prev (h
, h
->current
);
245 while (h
->current
!= w
/* && (((Widget *) h->current->data)->options & W_DISABLED) == 0 */ );
247 if (dlg_overlap (w0
, (Widget
*) h
->current
->data
))
249 send_message ((Widget
*) h
->current
->data
, WIDGET_DRAW
, 0);
250 send_message ((Widget
*) h
->current
->data
, WIDGET_FOCUS
, 0);
254 /* --------------------------------------------------------------------------------------------- */
263 /* Use this if the refreshes fail */
266 #endif /* HAVE_SLANG */
269 /* --------------------------------------------------------------------------------------------- */
272 dlg_execute_cmd (Dlg_head
* h
, unsigned long command
)
274 cb_ret_t ret
= MSG_HANDLED
;
278 h
->ret_value
= B_ENTER
;
282 h
->ret_value
= B_CANCEL
;
297 ev_help_t event_data
= { NULL
, h
->help_ctx
};
298 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
304 mc_event_raise (MCEVENT_GROUP_CORE
, "suspend", NULL
);
313 dialog_switch_list ();
315 ret
= MSG_NOT_HANDLED
;
319 dialog_switch_next ();
321 ret
= MSG_NOT_HANDLED
;
325 dialog_switch_prev ();
327 ret
= MSG_NOT_HANDLED
;
331 ret
= MSG_NOT_HANDLED
;
337 /* --------------------------------------------------------------------------------------------- */
340 dlg_handle_key (Dlg_head
* h
, int d_key
)
342 unsigned long command
;
343 command
= keybind_lookup_keymap_command (dialog_map
, d_key
);
344 if ((command
== CK_IgnoreKey
) || (dlg_execute_cmd (h
, command
) == MSG_NOT_HANDLED
))
345 return MSG_NOT_HANDLED
;
350 /* --------------------------------------------------------------------------------------------- */
353 dlg_mouse_event (Dlg_head
* h
, Gpm_Event
* event
)
356 GList
*starting_widget
= h
->current
;
361 /* close the dialog by mouse click out of dialog area */
362 if (mouse_close_dialog
&& !h
->fullscreen
&& ((event
->buttons
& GPM_B_LEFT
) != 0) && ((event
->type
& GPM_DOWN
) != 0) /* left click */
363 && !((x
> h
->x
) && (x
<= h
->x
+ h
->cols
) && (y
> h
->y
) && (y
<= h
->y
+ h
->lines
)))
365 h
->ret_value
= B_CANCEL
;
370 item
= starting_widget
;
375 widget
= (Widget
*) item
->data
;
376 item
= dlg_widget_next (h
, item
);
378 if (((widget
->options
& W_DISABLED
) == 0)
379 && (x
> widget
->x
) && (x
<= widget
->x
+ widget
->cols
)
380 && (y
> widget
->y
) && (y
<= widget
->y
+ widget
->lines
))
383 new_event
.x
-= widget
->x
;
384 new_event
.y
-= widget
->y
;
386 if (widget
->mouse
!= NULL
)
387 return widget
->mouse (&new_event
, widget
);
390 while (item
!= starting_widget
);
395 /* --------------------------------------------------------------------------------------------- */
398 dlg_try_hotkey (Dlg_head
* h
, int d_key
)
405 if (h
->widgets
== NULL
)
406 return MSG_NOT_HANDLED
;
408 if (h
->current
== NULL
)
409 h
->current
= h
->widgets
;
412 * Explanation: we don't send letter hotkeys to other widgets if
413 * the currently selected widget is an input line
416 current
= (Widget
*) h
->current
->data
;
418 if ((current
->options
& W_DISABLED
) != 0)
419 return MSG_NOT_HANDLED
;
421 if (current
->options
& W_IS_INPUT
)
423 /* skip ascii control characters, anything else can valid character in
425 if (d_key
>= 32 && d_key
< 256)
426 return MSG_NOT_HANDLED
;
429 /* If it's an alt key, send the message */
430 c
= d_key
& ~ALT (0);
431 if (d_key
& ALT (0) && g_ascii_isalpha (c
))
432 d_key
= g_ascii_tolower (c
);
434 handled
= MSG_NOT_HANDLED
;
435 if ((current
->options
& W_WANT_HOTKEY
) != 0)
436 handled
= send_message (current
, WIDGET_HOTKEY
, d_key
);
438 /* If not used, send hotkey to other widgets */
439 if (handled
== MSG_HANDLED
)
442 hot_cur
= dlg_widget_next (h
, h
->current
);
444 /* send it to all widgets */
445 while (h
->current
!= hot_cur
&& handled
== MSG_NOT_HANDLED
)
447 current
= (Widget
*) hot_cur
->data
;
449 if ((current
->options
& W_WANT_HOTKEY
) != 0)
450 handled
= send_message (current
, WIDGET_HOTKEY
, d_key
);
452 if (handled
== MSG_NOT_HANDLED
)
453 hot_cur
= dlg_widget_next (h
, hot_cur
);
456 if (handled
== MSG_HANDLED
)
457 do_select_widget (h
, hot_cur
, SELECT_EXACT
);
462 /* --------------------------------------------------------------------------------------------- */
465 dlg_key_event (Dlg_head
* h
, int d_key
)
469 if (h
->widgets
== NULL
)
472 if (h
->current
== NULL
)
473 h
->current
= h
->widgets
;
475 /* TAB used to cycle */
476 if ((h
->flags
& DLG_WANT_TAB
) == 0)
483 else if (d_key
== KEY_BTAB
)
490 /* first can dlg_callback handle the key */
491 handled
= h
->callback (h
, NULL
, DLG_KEY
, d_key
, NULL
);
493 /* next try the hotkey */
494 if (handled
== MSG_NOT_HANDLED
)
495 handled
= dlg_try_hotkey (h
, d_key
);
497 if (handled
== MSG_HANDLED
)
498 h
->callback (h
, NULL
, DLG_HOTKEY_HANDLED
, 0, NULL
);
500 /* not used - then try widget_callback */
501 handled
= send_message ((Widget
*) h
->current
->data
, WIDGET_KEY
, d_key
);
503 /* not used- try to use the unhandled case */
504 if (handled
== MSG_NOT_HANDLED
)
505 handled
= h
->callback (h
, NULL
, DLG_UNHANDLED_KEY
, d_key
, NULL
);
507 if (handled
== MSG_NOT_HANDLED
)
508 handled
= dlg_handle_key (h
, d_key
);
510 h
->callback (h
, NULL
, DLG_POST_KEY
, d_key
, NULL
);
513 /* --------------------------------------------------------------------------------------------- */
516 frontend_run_dlg (Dlg_head
* h
)
523 /* close opened editors, viewers, etc */
524 if (!h
->modal
&& mc_global
.widget
.midnight_shutdown
)
526 h
->callback (h
, NULL
, DLG_VALIDATE
, 0, NULL
);
530 while (h
->state
== DLG_ACTIVE
)
532 if (mc_global
.tty
.winch_flag
)
533 dialog_change_screen_size ();
538 execute_hooks (idle_hook
);
540 while ((h
->flags
& DLG_WANT_IDLE
) && is_idle ())
541 h
->callback (h
, NULL
, DLG_IDLE
, 0, NULL
);
543 /* Allow terminating the dialog from the idle handler */
544 if (h
->state
!= DLG_ACTIVE
)
550 /* Clear interrupt flag */
551 tty_got_interrupt ();
552 d_key
= tty_get_event (&event
, h
->mouse_status
== MOU_REPEAT
, TRUE
);
554 dlg_process_event (h
, d_key
, &event
);
556 if (h
->state
== DLG_CLOSED
)
557 h
->callback (h
, NULL
, DLG_VALIDATE
, 0, NULL
);
561 /* --------------------------------------------------------------------------------------------- */
564 dlg_find_widget_by_id (gconstpointer a
, gconstpointer b
)
566 Widget
*w
= (Widget
*) a
;
567 unsigned long id
= GPOINTER_TO_UINT (b
);
569 return w
->id
== id
? 0 : 1;
572 /* --------------------------------------------------------------------------------------------- */
573 /*** public functions ****************************************************************************/
574 /* --------------------------------------------------------------------------------------------- */
576 /** draw box in window */
578 draw_box (Dlg_head
* h
, int y
, int x
, int ys
, int xs
, gboolean single
)
580 tty_draw_box (h
->y
+ y
, h
->x
+ x
, ys
, xs
, single
);
583 /* --------------------------------------------------------------------------------------------- */
585 /** Clean the dialog area, draw the frame and the title */
587 common_dialog_repaint (Dlg_head
* h
)
591 if (h
->state
!= DLG_ACTIVE
)
594 space
= (h
->flags
& DLG_COMPACT
) ? 0 : 1;
596 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
598 draw_box (h
, space
, space
, h
->lines
- 2 * space
, h
->cols
- 2 * space
, FALSE
);
600 if (h
->title
!= NULL
)
602 tty_setcolor (h
->color
[DLG_COLOR_TITLE
]);
603 dlg_move (h
, space
, (h
->cols
- str_term_width1 (h
->title
)) / 2);
604 tty_print_string (h
->title
);
608 /* --------------------------------------------------------------------------------------------- */
609 /** this function allows to set dialog position */
612 dlg_set_position (Dlg_head
* h
, int y1
, int x1
, int y2
, int x2
)
614 /* save old positions, will be used to reposition childs */
616 int shift_x
, shift_y
, scale_x
, scale_y
;
618 /* save old positions, will be used to reposition childs */
629 /* dialog is empty */
630 if (h
->widgets
== NULL
)
633 if (h
->current
== NULL
)
634 h
->current
= h
->widgets
;
636 /* values by which controls should be moved */
639 scale_x
= h
->cols
- oc
;
640 scale_y
= h
->lines
- ol
;
642 if ((shift_x
!= 0) || (shift_y
!= 0) || (scale_x
!= 0) || (scale_y
!= 0))
646 for (w
= h
->widgets
; w
!= NULL
; w
= g_list_next (w
))
648 /* there are, mainly, 2 generally possible
651 1. control sticks to one side - it
654 2. control sticks to two sides of
655 one direction - it should be sized */
657 Widget
*c
= (Widget
*) w
->data
;
661 int lines
= c
->lines
;
663 if ((c
->pos_flags
& WPOS_KEEP_LEFT
) && (c
->pos_flags
& WPOS_KEEP_RIGHT
))
668 else if (c
->pos_flags
& WPOS_KEEP_LEFT
)
670 else if (c
->pos_flags
& WPOS_KEEP_RIGHT
)
671 x
+= shift_x
+ scale_x
;
673 if ((c
->pos_flags
& WPOS_KEEP_TOP
) && (c
->pos_flags
& WPOS_KEEP_BOTTOM
))
678 else if (c
->pos_flags
& WPOS_KEEP_TOP
)
680 else if (c
->pos_flags
& WPOS_KEEP_BOTTOM
)
681 y
+= shift_y
+ scale_y
;
683 widget_set_size (c
, y
, x
, lines
, cols
);
688 /* --------------------------------------------------------------------------------------------- */
689 /** this function sets only size, leaving positioning to automatic methods */
692 dlg_set_size (Dlg_head
* h
, int lines
, int cols
)
697 if (h
->flags
& DLG_CENTER
)
699 y
= (LINES
- lines
) / 2;
700 x
= (COLS
- cols
) / 2;
703 if ((h
->flags
& DLG_TRYUP
) && (y
> 3))
706 dlg_set_position (h
, y
, x
, y
+ lines
, x
+ cols
);
709 /* --------------------------------------------------------------------------------------------- */
710 /** Default dialog callback */
713 default_dlg_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
722 if (h
->color
!= NULL
)
724 common_dialog_repaint (h
);
727 return MSG_NOT_HANDLED
;
730 dlg_broadcast_msg_to (h
, WIDGET_IDLE
, FALSE
, W_WANT_IDLE
);
734 /* this is default resizing mechanism */
735 /* the main idea of this code is to resize dialog
736 according to flags (if any of flags require automatic
737 resizing, like DLG_CENTER, end after that reposition
738 controls in dialog according to flags of widget) */
739 dlg_set_size (h
, h
->lines
, h
->cols
);
746 return MSG_NOT_HANDLED
;
749 /* --------------------------------------------------------------------------------------------- */
752 create_dlg (gboolean modal
, int y1
, int x1
, int lines
, int cols
,
753 const int *colors
, dlg_cb_fn callback
, const char *help_ctx
,
754 const char *title
, dlg_flags_t flags
)
758 new_d
= g_new0 (Dlg_head
, 1);
759 new_d
->modal
= modal
;
761 memmove (new_d
->color
, colors
, sizeof (dlg_colors_t
));
762 new_d
->help_ctx
= help_ctx
;
763 new_d
->callback
= (callback
!= NULL
) ? callback
: default_dlg_callback
;
766 new_d
->flags
= flags
;
769 dlg_set_size (new_d
, lines
, cols
);
770 new_d
->fullscreen
= (new_d
->x
== 0 && new_d
->y
== 0
771 && new_d
->cols
== COLS
&& new_d
->lines
== LINES
);
773 new_d
->mouse_status
= MOU_NORMAL
;
775 /* Strip existing spaces, add one space before and after the title */
780 t
= g_strstrip (g_strdup (title
));
782 new_d
->title
= g_strdup_printf (" %s ", t
);
786 /* unique name got event group for this dialog */
787 new_d
->event_group
= g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG
, (void *) new_d
);
792 /* --------------------------------------------------------------------------------------------- */
795 dlg_set_default_colors (void)
797 dialog_colors
[DLG_COLOR_NORMAL
] = COLOR_NORMAL
;
798 dialog_colors
[DLG_COLOR_FOCUS
] = COLOR_FOCUS
;
799 dialog_colors
[DLG_COLOR_HOT_NORMAL
] = COLOR_HOT_NORMAL
;
800 dialog_colors
[DLG_COLOR_HOT_FOCUS
] = COLOR_HOT_FOCUS
;
801 dialog_colors
[DLG_COLOR_TITLE
] = COLOR_TITLE
;
803 alarm_colors
[DLG_COLOR_NORMAL
] = ERROR_COLOR
;
804 alarm_colors
[DLG_COLOR_FOCUS
] = ERROR_FOCUS
;
805 alarm_colors
[DLG_COLOR_HOT_NORMAL
] = ERROR_HOT_NORMAL
;
806 alarm_colors
[DLG_COLOR_HOT_FOCUS
] = ERROR_HOT_FOCUS
;
807 alarm_colors
[DLG_COLOR_TITLE
] = ERROR_TITLE
;
810 /* --------------------------------------------------------------------------------------------- */
813 dlg_erase (Dlg_head
* h
)
815 if ((h
!= NULL
) && (h
->state
== DLG_ACTIVE
))
816 tty_fill_region (h
->y
, h
->x
, h
->lines
, h
->cols
, ' ');
819 /* --------------------------------------------------------------------------------------------- */
822 set_idle_proc (Dlg_head
* d
, int enable
)
825 d
->flags
|= DLG_WANT_IDLE
;
827 d
->flags
&= ~DLG_WANT_IDLE
;
830 /* --------------------------------------------------------------------------------------------- */
832 * Insert widget to dialog before current widget. For dialogs populated
833 * from the bottom, make the widget current. Return widget number.
837 add_widget_autopos (Dlg_head
* h
, void *w
, widget_pos_flags_t pos_flags
)
839 Widget
*widget
= (Widget
*) w
;
841 /* Don't accept 0 widgets */
848 widget
->pos_flags
= pos_flags
;
849 widget
->id
= g_list_length (h
->widgets
);
851 if ((h
->flags
& DLG_REVERSE
) != 0)
852 h
->widgets
= g_list_prepend (h
->widgets
, widget
);
854 h
->widgets
= g_list_append (h
->widgets
, widget
);
856 h
->current
= h
->widgets
;
861 /* --------------------------------------------------------------------------------------------- */
862 /** wrapper to simply add lefttop positioned controls */
865 add_widget (Dlg_head
* h
, void *w
)
867 return add_widget_autopos (h
, w
, WPOS_KEEP_LEFT
| WPOS_KEEP_TOP
);
870 /* --------------------------------------------------------------------------------------------- */
879 if ((d
!= NULL
) && (d
->data
!= NULL
))
880 dlg_redraw ((Dlg_head
*) d
->data
);
884 /* Search first fullscreen dialog */
885 for (; d
!= NULL
; d
= g_list_next (d
))
886 if ((d
->data
!= NULL
) && ((Dlg_head
*) d
->data
)->fullscreen
)
888 /* back to top dialog */
889 for (; d
!= NULL
; d
= g_list_previous (d
))
891 dlg_redraw ((Dlg_head
*) d
->data
);
895 /* --------------------------------------------------------------------------------------------- */
896 /** broadcast a message to all the widgets in a dialog */
899 dlg_broadcast_msg (Dlg_head
* h
, widget_msg_t msg
, gboolean reverse
)
901 dlg_broadcast_msg_to (h
, msg
, reverse
, 0);
904 /* --------------------------------------------------------------------------------------------- */
907 dlg_focus (Dlg_head
* h
)
909 /* cannot focus disabled widget ... */
911 if ((h
->current
!= NULL
) && (h
->state
== DLG_ACTIVE
))
913 Widget
*current
= (Widget
*) h
->current
->data
;
915 if (((current
->options
& W_DISABLED
) == 0)
916 && (send_message (current
, WIDGET_FOCUS
, 0) == MSG_HANDLED
))
918 h
->callback (h
, current
, DLG_FOCUS
, 0, NULL
);
926 /* --------------------------------------------------------------------------------------------- */
927 /** Return true if the windows overlap */
930 dlg_overlap (Widget
* a
, Widget
* b
)
932 return !((b
->x
>= a
->x
+ a
->cols
)
933 || (a
->x
>= b
->x
+ b
->cols
) || (b
->y
>= a
->y
+ a
->lines
) || (a
->y
>= b
->y
+ b
->lines
));
937 /* --------------------------------------------------------------------------------------------- */
938 /** Find the widget with the given callback in the dialog h */
941 find_widget_type (const Dlg_head
* h
, callback_fn callback
)
945 w
= g_list_find_custom (h
->widgets
, callback
, dlg_find_widget_callback
);
947 return (w
== NULL
) ? NULL
: (Widget
*) w
->data
;
950 /* --------------------------------------------------------------------------------------------- */
951 /** Find the widget with the given id */
954 dlg_find_by_id (const Dlg_head
* h
, unsigned int id
)
958 w
= g_list_find_custom (h
->widgets
, GUINT_TO_POINTER (id
), dlg_find_widget_by_id
);
959 return w
!= NULL
? (Widget
*) w
->data
: NULL
;
962 /* --------------------------------------------------------------------------------------------- */
963 /** Find the widget with the given id in the dialog h and select it */
966 dlg_select_by_id (const Dlg_head
* h
, unsigned int id
)
970 w
= dlg_find_by_id (h
, id
);
972 dlg_select_widget (w
);
975 /* --------------------------------------------------------------------------------------------- */
977 * Try to select widget in the dialog.
981 dlg_select_widget (void *w
)
983 const Widget
*widget
= (Widget
*) w
;
984 Dlg_head
*h
= widget
->owner
;
986 do_select_widget (h
, g_list_find (h
->widgets
, widget
), SELECT_EXACT
);
989 /* --------------------------------------------------------------------------------------------- */
990 /** Try to select previous widget in the tab order */
993 dlg_one_up (Dlg_head
* h
)
995 if (h
->widgets
!= NULL
)
996 do_select_widget (h
, dlg_widget_prev (h
, h
->current
), SELECT_PREV
);
999 /* --------------------------------------------------------------------------------------------- */
1000 /** Try to select next widget in the tab order */
1003 dlg_one_down (Dlg_head
* h
)
1005 if (h
->widgets
!= NULL
)
1006 do_select_widget (h
, dlg_widget_next (h
, h
->current
), SELECT_NEXT
);
1009 /* --------------------------------------------------------------------------------------------- */
1012 update_cursor (Dlg_head
* h
)
1014 GList
*p
= h
->current
;
1016 if ((p
!= NULL
) && (h
->state
== DLG_ACTIVE
))
1020 w
= (Widget
*) p
->data
;
1022 if (((w
->options
& W_DISABLED
) == 0) && ((w
->options
& W_WANT_CURSOR
) != 0))
1023 send_message (w
, WIDGET_CURSOR
, 0);
1027 p
= dlg_widget_next (h
, p
);
1028 if (p
== h
->current
)
1031 w
= (Widget
*) p
->data
;
1033 if (((w
->options
& W_DISABLED
) == 0) && ((w
->options
& W_WANT_CURSOR
) != 0))
1034 if (send_message (w
, WIDGET_CURSOR
, 0) == MSG_HANDLED
)
1041 /* --------------------------------------------------------------------------------------------- */
1043 * Redraw the widgets in reverse order, leaving the current widget
1048 dlg_redraw (Dlg_head
* h
)
1050 if (h
->state
!= DLG_ACTIVE
)
1053 if (h
->winch_pending
)
1055 h
->winch_pending
= FALSE
;
1056 h
->callback (h
, NULL
, DLG_RESIZE
, 0, NULL
);
1059 h
->callback (h
, NULL
, DLG_DRAW
, 0, NULL
);
1060 dlg_broadcast_msg (h
, WIDGET_DRAW
, TRUE
);
1064 /* --------------------------------------------------------------------------------------------- */
1067 dlg_stop (Dlg_head
* h
)
1069 h
->state
= DLG_CLOSED
;
1072 /* --------------------------------------------------------------------------------------------- */
1073 /** Init the process */
1076 init_dlg (Dlg_head
* h
)
1078 if ((top_dlg
!= NULL
) && ((Dlg_head
*) top_dlg
->data
)->modal
)
1081 /* add dialog to the stack */
1082 top_dlg
= g_list_prepend (top_dlg
, h
);
1084 /* Initialize dialog manager and widgets */
1085 if (h
->state
== DLG_ACTIVE
)
1088 dialog_switch_add (h
);
1090 h
->callback (h
, NULL
, DLG_INIT
, 0, NULL
);
1091 dlg_broadcast_msg (h
, WIDGET_INIT
, FALSE
);
1092 dlg_read_history (h
);
1095 h
->state
= DLG_ACTIVE
;
1099 /* Select the first widget that takes focus */
1100 while (h
->current
!= NULL
&& !dlg_focus (h
))
1101 h
->current
= dlg_widget_next (h
, h
->current
);
1106 /* --------------------------------------------------------------------------------------------- */
1109 dlg_process_event (Dlg_head
* h
, int key
, Gpm_Event
* event
)
1113 if (tty_got_interrupt ())
1114 if (h
->callback (h
, NULL
, DLG_ACTION
, CK_Cancel
, NULL
) != MSG_HANDLED
)
1115 dlg_execute_cmd (h
, CK_Cancel
);
1120 if (key
== EV_MOUSE
)
1121 h
->mouse_status
= dlg_mouse_event (h
, event
);
1123 dlg_key_event (h
, key
);
1126 /* --------------------------------------------------------------------------------------------- */
1127 /** Shutdown the run_dlg */
1130 dlg_run_done (Dlg_head
* h
)
1132 top_dlg
= g_list_remove (top_dlg
, h
);
1134 if (h
->state
== DLG_CLOSED
)
1136 h
->callback (h
, (Widget
*) h
->current
->data
, DLG_END
, 0, NULL
);
1138 dialog_switch_remove (h
);
1143 /* --------------------------------------------------------------------------------------------- */
1145 * Standard run dialog routine
1146 * We have to keep this routine small so that we can duplicate it's
1147 * behavior on complex routines like the file routines, this way,
1148 * they can call the dlg_process_event without rewriting all the code
1152 run_dlg (Dlg_head
* h
)
1155 frontend_run_dlg (h
);
1157 return h
->ret_value
;
1160 /* --------------------------------------------------------------------------------------------- */
1163 destroy_dlg (Dlg_head
* h
)
1165 /* if some widgets have history, save all history at one moment here */
1166 dlg_save_history (h
);
1167 dlg_broadcast_msg (h
, WIDGET_DESTROY
, FALSE
);
1168 g_list_foreach (h
->widgets
, (GFunc
) g_free
, NULL
);
1169 g_list_free (h
->widgets
);
1170 mc_event_group_del (h
->event_group
);
1171 g_free (h
->event_group
);
1178 /* --------------------------------------------------------------------------------------------- */
1181 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1184 dlg_save_history (Dlg_head
* h
)
1189 if (num_history_items_recorded
== 0) /* this is how to disable */
1192 profile
= g_build_filename (mc_config_get_cache_path (), MC_HISTORY_FILE
, (char *) NULL
);
1193 i
= open (profile
, O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
);
1197 /* Make sure the history is only readable by the user */
1198 if (chmod (profile
, S_IRUSR
| S_IWUSR
) != -1 || errno
== ENOENT
)
1200 ev_history_load_save_t event_data
;
1202 event_data
.cfg
= mc_config_init (profile
);
1203 event_data
.receiver
= NULL
;
1205 /* get all histories in dialog */
1206 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_SAVE
, &event_data
);
1208 mc_config_save_file (event_data
.cfg
, NULL
);
1209 mc_config_deinit (event_data
.cfg
);
1215 /* --------------------------------------------------------------------------------------------- */
1218 dlg_get_title (const Dlg_head
* h
, size_t len
)
1225 if (h
->get_title
!= NULL
)
1226 t
= h
->get_title (h
, len
);
1233 /* --------------------------------------------------------------------------------------------- */
1234 /** Replace widget old_w for widget new_w in the dialog */
1237 dlg_replace_widget (Widget
* old_w
, Widget
* new_w
)
1239 Dlg_head
*h
= old_w
->owner
;
1240 gboolean should_focus
= FALSE
;
1242 if (h
->widgets
== NULL
)
1245 if (h
->current
== NULL
)
1246 h
->current
= h
->widgets
;
1248 if (old_w
== h
->current
->data
)
1249 should_focus
= TRUE
;
1252 new_w
->id
= old_w
->id
;
1255 h
->current
->data
= new_w
;
1257 g_list_find (h
->widgets
, old_w
)->data
= new_w
;
1259 send_message (old_w
, WIDGET_DESTROY
, 0);
1260 send_message (new_w
, WIDGET_INIT
, 0);
1263 dlg_select_widget (new_w
);
1265 send_message (new_w
, WIDGET_DRAW
, 0);
1268 /* --------------------------------------------------------------------------------------------- */