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
, NULL
, msg
, 0, NULL
);
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
, NULL
, WIDGET_UNFOCUS
, 0, NULL
) == 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
= WIDGET (a
);
204 widget_cb_fn f
= (widget_cb_fn
) b
;
206 return (w
->callback
== f
) ? 0 : 1;
209 /* --------------------------------------------------------------------------------------------- */
211 * Try to select another widget. If forward is set, follow tab order.
212 * Otherwise go to the previous widget.
216 do_select_widget (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
), NULL
, WIDGET_DRAW
, 0, NULL
);
252 send_message (WIDGET (h
->current
->data
), NULL
, WIDGET_FOCUS
, 0, NULL
);
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
)
362 Widget
*wh
= WIDGET (h
);
365 GList
*starting_widget
= h
->current
;
369 /* close the dialog by mouse click out of dialog area */
370 if (mouse_close_dialog
&& !h
->fullscreen
&& ((event
->buttons
& GPM_B_LEFT
) != 0) && ((event
->type
& GPM_DOWN
) != 0) /* left click */
371 && !((x
> wh
->x
) && (x
<= wh
->x
+ wh
->cols
) && (y
> wh
->y
) && (y
<= wh
->y
+ wh
->lines
)))
373 h
->ret_value
= B_CANCEL
;
378 if (wh
->mouse
!= NULL
)
382 mou
= wh
->mouse (event
, wh
);
383 if (mou
!= MOU_UNHANDLED
)
387 item
= starting_widget
;
390 Widget
*widget
= WIDGET (item
->data
);
392 if ((h
->flags
& DLG_REVERSE
) == 0)
393 item
= dlg_widget_prev (h
, item
);
395 item
= dlg_widget_next (h
, item
);
397 if ((widget
->options
& W_DISABLED
) == 0 && widget
->mouse
!= NULL
)
399 /* put global cursor position to the widget */
402 ret
= widget
->mouse (event
, widget
);
403 if (ret
!= MOU_UNHANDLED
)
407 while (item
!= starting_widget
);
409 return MOU_UNHANDLED
;
412 /* --------------------------------------------------------------------------------------------- */
415 dlg_try_hotkey (Dlg_head
* h
, int d_key
)
422 if (h
->widgets
== NULL
)
423 return MSG_NOT_HANDLED
;
425 if (h
->current
== NULL
)
426 h
->current
= h
->widgets
;
429 * Explanation: we don't send letter hotkeys to other widgets if
430 * the currently selected widget is an input line
433 current
= WIDGET (h
->current
->data
);
435 if ((current
->options
& W_DISABLED
) != 0)
436 return MSG_NOT_HANDLED
;
438 if (current
->options
& W_IS_INPUT
)
440 /* skip ascii control characters, anything else can valid character in
442 if (d_key
>= 32 && d_key
< 256)
443 return MSG_NOT_HANDLED
;
446 /* If it's an alt key, send the message */
447 c
= d_key
& ~ALT (0);
448 if (d_key
& ALT (0) && g_ascii_isalpha (c
))
449 d_key
= g_ascii_tolower (c
);
451 handled
= MSG_NOT_HANDLED
;
452 if ((current
->options
& W_WANT_HOTKEY
) != 0)
453 handled
= send_message (current
, NULL
, WIDGET_HOTKEY
, d_key
, NULL
);
455 /* If not used, send hotkey to other widgets */
456 if (handled
== MSG_HANDLED
)
459 hot_cur
= dlg_widget_next (h
, h
->current
);
461 /* send it to all widgets */
462 while (h
->current
!= hot_cur
&& handled
== MSG_NOT_HANDLED
)
464 current
= WIDGET (hot_cur
->data
);
466 if ((current
->options
& W_WANT_HOTKEY
) != 0 && (current
->options
& W_DISABLED
) == 0)
467 handled
= send_message (current
, NULL
, WIDGET_HOTKEY
, d_key
, NULL
);
469 if (handled
== MSG_NOT_HANDLED
)
470 hot_cur
= dlg_widget_next (h
, hot_cur
);
473 if (handled
== MSG_HANDLED
)
474 do_select_widget (h
, hot_cur
, SELECT_EXACT
);
479 /* --------------------------------------------------------------------------------------------- */
482 dlg_key_event (Dlg_head
* h
, int d_key
)
486 if (h
->widgets
== NULL
)
489 if (h
->current
== NULL
)
490 h
->current
= h
->widgets
;
492 /* TAB used to cycle */
493 if ((h
->flags
& DLG_WANT_TAB
) == 0)
500 else if (d_key
== KEY_BTAB
)
507 /* first can dlg_callback handle the key */
508 handled
= h
->callback (h
, NULL
, DLG_KEY
, d_key
, NULL
);
510 /* next try the hotkey */
511 if (handled
== MSG_NOT_HANDLED
)
512 handled
= dlg_try_hotkey (h
, d_key
);
514 if (handled
== MSG_HANDLED
)
515 h
->callback (h
, NULL
, DLG_HOTKEY_HANDLED
, 0, NULL
);
517 /* not used - then try widget_callback */
518 handled
= send_message (WIDGET (h
->current
->data
), NULL
, WIDGET_KEY
, d_key
, NULL
);
520 /* not used- try to use the unhandled case */
521 if (handled
== MSG_NOT_HANDLED
)
522 handled
= h
->callback (h
, NULL
, DLG_UNHANDLED_KEY
, d_key
, NULL
);
524 if (handled
== MSG_NOT_HANDLED
)
525 handled
= dlg_handle_key (h
, d_key
);
527 h
->callback (h
, NULL
, DLG_POST_KEY
, d_key
, NULL
);
530 /* --------------------------------------------------------------------------------------------- */
533 frontend_run_dlg (Dlg_head
* h
)
540 /* close opened editors, viewers, etc */
541 if (!h
->modal
&& mc_global
.midnight_shutdown
)
543 h
->callback (h
, NULL
, DLG_VALIDATE
, 0, NULL
);
547 while (h
->state
== DLG_ACTIVE
)
549 if (mc_global
.tty
.winch_flag
!= 0)
550 dialog_change_screen_size ();
555 execute_hooks (idle_hook
);
557 while ((h
->flags
& DLG_WANT_IDLE
) && is_idle ())
558 h
->callback (h
, NULL
, DLG_IDLE
, 0, NULL
);
560 /* Allow terminating the dialog from the idle handler */
561 if (h
->state
!= DLG_ACTIVE
)
567 /* Clear interrupt flag */
568 tty_got_interrupt ();
569 d_key
= tty_get_event (&event
, h
->mouse_status
== MOU_REPEAT
, TRUE
);
571 dlg_process_event (h
, d_key
, &event
);
573 if (h
->state
== DLG_CLOSED
)
574 h
->callback (h
, NULL
, DLG_VALIDATE
, 0, NULL
);
578 /* --------------------------------------------------------------------------------------------- */
581 dlg_find_widget_by_id (gconstpointer a
, gconstpointer b
)
583 Widget
*w
= WIDGET (a
);
584 unsigned long id
= GPOINTER_TO_UINT (b
);
586 return w
->id
== id
? 0 : 1;
589 /* --------------------------------------------------------------------------------------------- */
590 /*** public functions ****************************************************************************/
591 /* --------------------------------------------------------------------------------------------- */
593 /** draw box in window */
595 draw_box (const Dlg_head
* h
, int y
, int x
, int ys
, int xs
, gboolean single
)
597 const Widget
*wh
= WIDGET (h
);
599 tty_draw_box (wh
->y
+ y
, wh
->x
+ x
, ys
, xs
, single
);
602 /* --------------------------------------------------------------------------------------------- */
604 /** Clean the dialog area, draw the frame and the title */
606 common_dialog_repaint (Dlg_head
* h
)
608 Widget
*wh
= WIDGET (h
);
612 if (h
->state
!= DLG_ACTIVE
)
615 space
= (h
->flags
& DLG_COMPACT
) ? 0 : 1;
617 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
619 draw_box (h
, space
, space
, wh
->lines
- 2 * space
, wh
->cols
- 2 * space
, FALSE
);
621 if (h
->title
!= NULL
)
623 tty_setcolor (h
->color
[DLG_COLOR_TITLE
]);
624 widget_move (h
, space
, (wh
->cols
- str_term_width1 (h
->title
)) / 2);
625 tty_print_string (h
->title
);
629 /* --------------------------------------------------------------------------------------------- */
630 /** this function allows to set dialog position */
633 dlg_set_position (Dlg_head
* h
, int y1
, int x1
, int y2
, int x2
)
635 Widget
*wh
= WIDGET (h
);
637 /* save old positions, will be used to reposition childs */
639 int shift_x
, shift_y
, scale_x
, scale_y
;
641 /* save old positions, will be used to reposition childs */
652 /* dialog is empty */
653 if (h
->widgets
== NULL
)
656 if (h
->current
== NULL
)
657 h
->current
= h
->widgets
;
659 /* values by which controls should be moved */
660 shift_x
= wh
->x
- ox
;
661 shift_y
= wh
->y
- oy
;
662 scale_x
= wh
->cols
- oc
;
663 scale_y
= wh
->lines
- ol
;
665 if ((shift_x
!= 0) || (shift_y
!= 0) || (scale_x
!= 0) || (scale_y
!= 0))
669 for (w
= h
->widgets
; w
!= NULL
; w
= g_list_next (w
))
671 /* there are, mainly, 2 generally possible
674 1. control sticks to one side - it
677 2. control sticks to two sides of
678 one direction - it should be sized */
680 Widget
*c
= WIDGET (w
->data
);
684 int lines
= c
->lines
;
686 if ((c
->pos_flags
& WPOS_CENTER_HORZ
) != 0)
687 x
= wh
->x
+ (wh
->cols
- c
->cols
) / 2;
688 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0 && (c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
693 else if ((c
->pos_flags
& WPOS_KEEP_LEFT
) != 0)
695 else if ((c
->pos_flags
& WPOS_KEEP_RIGHT
) != 0)
696 x
+= shift_x
+ scale_x
;
698 if ((c
->pos_flags
& WPOS_CENTER_VERT
) != 0)
699 y
= wh
->y
+ (wh
->lines
- c
->lines
) / 2;
700 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0 && (c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
705 else if ((c
->pos_flags
& WPOS_KEEP_TOP
) != 0)
707 else if ((c
->pos_flags
& WPOS_KEEP_BOTTOM
) != 0)
708 y
+= shift_y
+ scale_y
;
710 widget_set_size (c
, y
, x
, lines
, cols
);
715 /* --------------------------------------------------------------------------------------------- */
716 /** this function sets only size, leaving positioning to automatic methods */
719 dlg_set_size (Dlg_head
* h
, int lines
, int cols
)
721 int x
= WIDGET (h
)->x
;
722 int y
= WIDGET (h
)->y
;
724 if (h
->flags
& DLG_CENTER
)
726 y
= (LINES
- lines
) / 2;
727 x
= (COLS
- cols
) / 2;
730 if ((h
->flags
& DLG_TRYUP
) && (y
> 3))
733 dlg_set_position (h
, y
, x
, y
+ lines
, x
+ cols
);
736 /* --------------------------------------------------------------------------------------------- */
737 /** Default dialog callback */
740 default_dlg_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
749 if (h
->color
!= NULL
)
751 common_dialog_repaint (h
);
754 return MSG_NOT_HANDLED
;
757 dlg_broadcast_msg_to (h
, WIDGET_IDLE
, FALSE
, W_WANT_IDLE
);
761 /* this is default resizing mechanism */
762 /* the main idea of this code is to resize dialog
763 according to flags (if any of flags require automatic
764 resizing, like DLG_CENTER, end after that reposition
765 controls in dialog according to flags of widget) */
766 dlg_set_size (h
, WIDGET (h
)->lines
, WIDGET (h
)->cols
);
773 return MSG_NOT_HANDLED
;
776 /* --------------------------------------------------------------------------------------------- */
779 create_dlg (gboolean modal
, int y1
, int x1
, int lines
, int cols
,
780 const int *colors
, dlg_cb_fn callback
, mouse_h mouse_handler
,
781 const char *help_ctx
, const char *title
, dlg_flags_t flags
)
786 new_d
= g_new0 (Dlg_head
, 1);
788 init_widget (w
, y1
, x1
, lines
, cols
, NULL
, mouse_handler
);
789 widget_want_cursor (w
, FALSE
);
791 new_d
->state
= DLG_CONSTRUCT
;
792 new_d
->modal
= modal
;
794 memmove (new_d
->color
, colors
, sizeof (dlg_colors_t
));
795 new_d
->help_ctx
= help_ctx
;
796 new_d
->callback
= (callback
!= NULL
) ? callback
: default_dlg_callback
;
797 new_d
->flags
= flags
;
800 dlg_set_size (new_d
, lines
, cols
);
801 new_d
->fullscreen
= (w
->x
== 0 && w
->y
== 0 && w
->cols
== COLS
&& w
->lines
== LINES
);
803 new_d
->mouse_status
= MOU_UNHANDLED
;
805 /* Strip existing spaces, add one space before and after the title */
806 if (title
!= NULL
&& *title
!= '\0')
810 t
= g_strstrip (g_strdup (title
));
812 new_d
->title
= g_strdup_printf (" %s ", t
);
816 /* unique name got event group for this dialog */
817 new_d
->event_group
= g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG
, (void *) new_d
);
822 /* --------------------------------------------------------------------------------------------- */
825 dlg_set_default_colors (void)
827 dialog_colors
[DLG_COLOR_NORMAL
] = COLOR_NORMAL
;
828 dialog_colors
[DLG_COLOR_FOCUS
] = COLOR_FOCUS
;
829 dialog_colors
[DLG_COLOR_HOT_NORMAL
] = COLOR_HOT_NORMAL
;
830 dialog_colors
[DLG_COLOR_HOT_FOCUS
] = COLOR_HOT_FOCUS
;
831 dialog_colors
[DLG_COLOR_TITLE
] = COLOR_TITLE
;
833 alarm_colors
[DLG_COLOR_NORMAL
] = ERROR_COLOR
;
834 alarm_colors
[DLG_COLOR_FOCUS
] = ERROR_FOCUS
;
835 alarm_colors
[DLG_COLOR_HOT_NORMAL
] = ERROR_HOT_NORMAL
;
836 alarm_colors
[DLG_COLOR_HOT_FOCUS
] = ERROR_HOT_FOCUS
;
837 alarm_colors
[DLG_COLOR_TITLE
] = ERROR_TITLE
;
840 /* --------------------------------------------------------------------------------------------- */
843 dlg_erase (Dlg_head
* 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 /* --------------------------------------------------------------------------------------------- */
856 set_idle_proc (Dlg_head
* d
, int enable
)
859 d
->flags
|= DLG_WANT_IDLE
;
861 d
->flags
&= ~DLG_WANT_IDLE
;
864 /* --------------------------------------------------------------------------------------------- */
866 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
870 add_widget_autopos (Dlg_head
* h
, void *w
, widget_pos_flags_t pos_flags
, const void *before
)
872 Widget
*wh
= WIDGET (h
);
873 Widget
*widget
= WIDGET (w
);
875 /* Don't accept 0 widgets */
879 if ((pos_flags
& WPOS_CENTER_HORZ
) != 0)
880 widget
->x
= (wh
->cols
- widget
->cols
) / 2;
883 if ((pos_flags
& WPOS_CENTER_VERT
) != 0)
884 widget
->y
= (wh
->lines
- widget
->lines
) / 2;
888 widget
->pos_flags
= pos_flags
;
889 widget
->id
= h
->widget_id
++;
891 if ((h
->flags
& DLG_REVERSE
) != 0)
893 if (h
->widgets
== NULL
|| before
== NULL
)
895 h
->widgets
= g_list_prepend (h
->widgets
, widget
);
896 h
->current
= h
->widgets
;
902 b
= g_list_find (h
->widgets
, before
);
904 /* don't accept widget not from dialog. This shouldn't happen */
908 h
->widgets
= g_list_insert_before (h
->widgets
, b
, widget
);
909 h
->current
= g_list_previous (b
);
914 if (h
->widgets
== NULL
|| before
== NULL
)
916 h
->widgets
= g_list_append (h
->widgets
, widget
);
917 h
->current
= g_list_last (h
->widgets
);
923 b
= g_list_find (h
->widgets
, before
);
925 /* don't accept widget not from dialog. This shouldn't happen */
930 h
->widgets
= g_list_insert_before (h
->widgets
, b
, widget
);
932 h
->current
= g_list_previous (b
);
934 h
->current
= g_list_last (h
->widgets
);
938 /* widget has been added in runtime */
939 if (h
->state
== DLG_ACTIVE
)
941 send_message (widget
, NULL
, WIDGET_INIT
, 0, NULL
);
942 send_message (widget
, NULL
, WIDGET_DRAW
, 0, NULL
);
943 send_message (widget
, NULL
, WIDGET_FOCUS
, 0, NULL
);
949 /* --------------------------------------------------------------------------------------------- */
950 /** wrapper to simply add lefttop positioned controls */
953 add_widget (Dlg_head
* h
, void *w
)
955 return add_widget_autopos (h
, w
, WPOS_KEEP_DEFAULT
,
956 h
->current
!= NULL
? h
->current
->data
: NULL
);
959 /* --------------------------------------------------------------------------------------------- */
962 add_widget_before (Dlg_head
* h
, void *w
, void *before
)
964 return add_widget_autopos (h
, w
, WPOS_KEEP_DEFAULT
, before
);
967 /* --------------------------------------------------------------------------------------------- */
969 /** delete widget from dialog */
973 Dlg_head
*h
= WIDGET (w
)->owner
;
976 /* Don't accept NULL widget. This shouldn't happen */
980 d
= g_list_find (h
->widgets
, w
);
983 if ((h
->flags
& DLG_REVERSE
) != 0)
984 h
->current
= dlg_widget_prev (h
, d
);
986 h
->current
= dlg_widget_next (h
, d
);
989 h
->widgets
= g_list_remove_link (h
->widgets
, d
);
990 send_message (d
->data
, NULL
, WIDGET_DESTROY
, 0, NULL
);
993 /* widget has been deleted in runtime */
994 if (h
->state
== DLG_ACTIVE
)
1001 /* --------------------------------------------------------------------------------------------- */
1010 if ((d
!= NULL
) && (d
->data
!= NULL
))
1011 dlg_redraw ((Dlg_head
*) d
->data
);
1015 /* Search first fullscreen dialog */
1016 for (; d
!= NULL
; d
= g_list_next (d
))
1017 if ((d
->data
!= NULL
) && ((Dlg_head
*) d
->data
)->fullscreen
)
1019 /* back to top dialog */
1020 for (; d
!= NULL
; d
= g_list_previous (d
))
1021 if (d
->data
!= NULL
)
1022 dlg_redraw ((Dlg_head
*) d
->data
);
1026 /* --------------------------------------------------------------------------------------------- */
1027 /** broadcast a message to all the widgets in a dialog */
1030 dlg_broadcast_msg (Dlg_head
* h
, widget_msg_t msg
, gboolean reverse
)
1032 dlg_broadcast_msg_to (h
, msg
, reverse
, 0);
1035 /* --------------------------------------------------------------------------------------------- */
1038 dlg_focus (Dlg_head
* h
)
1040 /* cannot focus disabled widget */
1041 if ((h
->current
!= NULL
) && (h
->state
== DLG_CONSTRUCT
|| h
->state
== DLG_ACTIVE
))
1043 Widget
*current
= WIDGET (h
->current
->data
);
1045 if (((current
->options
& W_DISABLED
) == 0)
1046 && (send_message (current
, NULL
, WIDGET_FOCUS
, 0, NULL
) == MSG_HANDLED
))
1048 h
->callback (h
, current
, DLG_FOCUS
, 0, NULL
);
1056 /* --------------------------------------------------------------------------------------------- */
1057 /** Return true if the windows overlap */
1060 dlg_overlap (Widget
* a
, Widget
* b
)
1062 return !((b
->x
>= a
->x
+ a
->cols
)
1063 || (a
->x
>= b
->x
+ b
->cols
) || (b
->y
>= a
->y
+ a
->lines
) || (a
->y
>= b
->y
+ b
->lines
));
1067 /* --------------------------------------------------------------------------------------------- */
1068 /** Find the widget with the given callback in the dialog h */
1071 find_widget_type (const Dlg_head
* h
, widget_cb_fn callback
)
1075 w
= g_list_find_custom (h
->widgets
, callback
, dlg_find_widget_callback
);
1077 return (w
== NULL
) ? NULL
: WIDGET (w
->data
);
1080 /* --------------------------------------------------------------------------------------------- */
1081 /** Find the widget with the given id */
1084 dlg_find_by_id (const Dlg_head
* h
, unsigned long id
)
1088 w
= g_list_find_custom (h
->widgets
, GUINT_TO_POINTER (id
), dlg_find_widget_by_id
);
1089 return w
!= NULL
? WIDGET (w
->data
) : NULL
;
1092 /* --------------------------------------------------------------------------------------------- */
1093 /** Find the widget with the given id in the dialog h and select it */
1096 dlg_select_by_id (const Dlg_head
* h
, unsigned long id
)
1100 w
= dlg_find_by_id (h
, id
);
1102 dlg_select_widget (w
);
1105 /* --------------------------------------------------------------------------------------------- */
1107 * Try to select widget in the dialog.
1111 dlg_select_widget (void *w
)
1113 Widget
*widget
= WIDGET (w
);
1114 Dlg_head
*h
= widget
->owner
;
1116 do_select_widget (h
, g_list_find (h
->widgets
, widget
), SELECT_EXACT
);
1119 /* --------------------------------------------------------------------------------------------- */
1122 * Set widget at top of widget list and make it current.
1126 dlg_set_top_widget (void *w
)
1128 Widget
*widget
= WIDGET (w
);
1129 Dlg_head
*h
= widget
->owner
;
1132 l
= g_list_find (h
->widgets
, w
);
1134 abort (); /* widget is not in dialog, this should not happen */
1136 /* unfocus prevoius widget and focus current one before widget reordering */
1137 if (h
->state
== DLG_ACTIVE
)
1138 do_select_widget (h
, l
, SELECT_EXACT
);
1140 /* widget reordering */
1141 h
->widgets
= g_list_remove_link (h
->widgets
, l
);
1142 if ((h
->flags
& DLG_REVERSE
) != 0)
1143 h
->widgets
= g_list_concat (l
, h
->widgets
);
1145 h
->widgets
= g_list_concat (h
->widgets
, l
);
1149 /* --------------------------------------------------------------------------------------------- */
1150 /** Try to select previous widget in the tab order */
1153 dlg_one_up (Dlg_head
* h
)
1155 if (h
->widgets
!= NULL
)
1156 do_select_widget (h
, dlg_widget_prev (h
, h
->current
), SELECT_PREV
);
1159 /* --------------------------------------------------------------------------------------------- */
1160 /** Try to select next widget in the tab order */
1163 dlg_one_down (Dlg_head
* h
)
1165 if (h
->widgets
!= NULL
)
1166 do_select_widget (h
, dlg_widget_next (h
, h
->current
), SELECT_NEXT
);
1169 /* --------------------------------------------------------------------------------------------- */
1172 update_cursor (Dlg_head
* h
)
1174 GList
*p
= h
->current
;
1176 if ((p
!= NULL
) && (h
->state
== DLG_ACTIVE
))
1180 w
= WIDGET (p
->data
);
1182 if (((w
->options
& W_DISABLED
) == 0) && ((w
->options
& W_WANT_CURSOR
) != 0))
1183 send_message (w
, NULL
, WIDGET_CURSOR
, 0, NULL
);
1187 p
= dlg_widget_next (h
, p
);
1188 if (p
== h
->current
)
1191 w
= WIDGET (p
->data
);
1193 if (((w
->options
& W_DISABLED
) == 0) && ((w
->options
& W_WANT_CURSOR
) != 0))
1194 if (send_message (w
, NULL
, WIDGET_CURSOR
, 0, NULL
) == MSG_HANDLED
)
1201 /* --------------------------------------------------------------------------------------------- */
1203 * Redraw the widgets in reverse order, leaving the current widget
1208 dlg_redraw (Dlg_head
* h
)
1210 if (h
->state
!= DLG_ACTIVE
)
1213 if (h
->winch_pending
)
1215 h
->winch_pending
= FALSE
;
1216 h
->callback (h
, NULL
, DLG_RESIZE
, 0, NULL
);
1219 h
->callback (h
, NULL
, DLG_DRAW
, 0, NULL
);
1220 dlg_broadcast_msg (h
, WIDGET_DRAW
, (h
->flags
& DLG_REVERSE
) != 0);
1224 /* --------------------------------------------------------------------------------------------- */
1227 dlg_stop (Dlg_head
* h
)
1229 h
->state
= DLG_CLOSED
;
1232 /* --------------------------------------------------------------------------------------------- */
1233 /** Init the process */
1236 init_dlg (Dlg_head
* h
)
1238 if ((top_dlg
!= NULL
) && ((Dlg_head
*) top_dlg
->data
)->modal
)
1241 /* add dialog to the stack */
1242 top_dlg
= g_list_prepend (top_dlg
, h
);
1244 /* Initialize dialog manager and widgets */
1245 if (h
->state
== DLG_CONSTRUCT
)
1248 dialog_switch_add (h
);
1250 h
->callback (h
, NULL
, DLG_INIT
, 0, NULL
);
1251 dlg_broadcast_msg (h
, WIDGET_INIT
, FALSE
);
1252 dlg_read_history (h
);
1255 h
->state
= DLG_ACTIVE
;
1257 /* Select the first widget that takes focus */
1258 while (h
->current
!= NULL
&& !dlg_focus (h
))
1259 h
->current
= dlg_widget_next (h
, h
->current
);
1266 /* --------------------------------------------------------------------------------------------- */
1269 dlg_process_event (Dlg_head
* h
, int key
, Gpm_Event
* event
)
1273 if (tty_got_interrupt ())
1274 if (h
->callback (h
, NULL
, DLG_ACTION
, CK_Cancel
, NULL
) != MSG_HANDLED
)
1275 dlg_execute_cmd (h
, CK_Cancel
);
1280 if (key
== EV_MOUSE
)
1281 h
->mouse_status
= dlg_mouse_event (h
, event
);
1283 dlg_key_event (h
, key
);
1286 /* --------------------------------------------------------------------------------------------- */
1287 /** Shutdown the run_dlg */
1290 dlg_run_done (Dlg_head
* h
)
1292 top_dlg
= g_list_remove (top_dlg
, h
);
1294 if (h
->state
== DLG_CLOSED
)
1296 h
->callback (h
, WIDGET (h
->current
->data
), DLG_END
, 0, NULL
);
1298 dialog_switch_remove (h
);
1303 /* --------------------------------------------------------------------------------------------- */
1305 * Standard run dialog routine
1306 * We have to keep this routine small so that we can duplicate it's
1307 * behavior on complex routines like the file routines, this way,
1308 * they can call the dlg_process_event without rewriting all the code
1312 run_dlg (Dlg_head
* h
)
1315 frontend_run_dlg (h
);
1317 return h
->ret_value
;
1320 /* --------------------------------------------------------------------------------------------- */
1323 destroy_dlg (Dlg_head
* h
)
1325 /* if some widgets have history, save all history at one moment here */
1326 dlg_save_history (h
);
1327 dlg_broadcast_msg (h
, WIDGET_DESTROY
, FALSE
);
1328 g_list_foreach (h
->widgets
, (GFunc
) g_free
, NULL
);
1329 g_list_free (h
->widgets
);
1330 mc_event_group_del (h
->event_group
);
1331 g_free (h
->event_group
);
1338 /* --------------------------------------------------------------------------------------------- */
1341 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1344 dlg_save_history (Dlg_head
* h
)
1349 if (num_history_items_recorded
== 0) /* this is how to disable */
1352 profile
= mc_config_get_full_path (MC_HISTORY_FILE
);
1353 i
= open (profile
, O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
);
1357 /* Make sure the history is only readable by the user */
1358 if (chmod (profile
, S_IRUSR
| S_IWUSR
) != -1 || errno
== ENOENT
)
1360 ev_history_load_save_t event_data
;
1362 event_data
.cfg
= mc_config_init (profile
, FALSE
);
1363 event_data
.receiver
= NULL
;
1365 /* get all histories in dialog */
1366 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_SAVE
, &event_data
);
1368 mc_config_save_file (event_data
.cfg
, NULL
);
1369 mc_config_deinit (event_data
.cfg
);
1375 /* --------------------------------------------------------------------------------------------- */
1378 dlg_get_title (const Dlg_head
* h
, size_t len
)
1385 if (h
->get_title
!= NULL
)
1386 t
= h
->get_title (h
, len
);
1393 /* --------------------------------------------------------------------------------------------- */
1394 /** Replace widget old_w for widget new_w in the dialog */
1397 dlg_replace_widget (Widget
* old_w
, Widget
* new_w
)
1399 Dlg_head
*h
= old_w
->owner
;
1400 gboolean should_focus
= FALSE
;
1402 if (h
->widgets
== NULL
)
1405 if (h
->current
== NULL
)
1406 h
->current
= h
->widgets
;
1408 if (old_w
== h
->current
->data
)
1409 should_focus
= TRUE
;
1412 new_w
->id
= old_w
->id
;
1415 h
->current
->data
= new_w
;
1417 g_list_find (h
->widgets
, old_w
)->data
= new_w
;
1419 send_message (old_w
, NULL
, WIDGET_DESTROY
, 0, NULL
);
1420 send_message (new_w
, NULL
, WIDGET_INIT
, 0, NULL
);
1423 dlg_select_widget (new_w
);
1425 if (new_w
->owner
->state
== DLG_ACTIVE
)
1426 send_message (new_w
, NULL
, WIDGET_DRAW
, 0, NULL
);
1429 /* --------------------------------------------------------------------------------------------- */