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
;
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 ************************************************************************/
83 /* --------------------------------------------------------------------------------------------- */
85 * broadcast a message to all the widgets in a dialog that have
86 * the options set to flags. If flags is zero, the message is sent
91 dlg_broadcast_msg_to (Dlg_head
* h
, widget_msg_t msg
, gboolean reverse
, int flags
)
95 if (h
->widgets
== NULL
)
98 if (h
->current
== NULL
)
99 h
->current
= h
->widgets
;
103 p
= g_list_previous (h
->current
);
106 p
= g_list_last (h
->widgets
);
110 p
= g_list_next (h
->current
);
120 Widget
*w
= (Widget
*) p
->data
;
124 p
= g_list_previous (p
);
127 p
= g_list_last (h
->widgets
);
137 if ((flags
== 0) || ((flags
& w
->options
) != 0))
138 send_message (w
, msg
, 0);
143 /* --------------------------------------------------------------------------------------------- */
146 dlg_unfocus (Dlg_head
* h
)
148 /* ... but can unfocus disabled widget */
150 if ((h
->current
!= NULL
) && (h
->state
== DLG_ACTIVE
))
152 Widget
*current
= (Widget
*) h
->current
->data
;
154 if (send_message (current
, WIDGET_UNFOCUS
, 0) == MSG_HANDLED
)
156 h
->callback (h
, current
, DLG_UNFOCUS
, 0, NULL
);
164 /* --------------------------------------------------------------------------------------------- */
167 dlg_find_widget_callback (const void *a
, const void *b
)
169 const Widget
*w
= (const Widget
*) a
;
170 callback_fn f
= (callback_fn
) b
;
172 return (w
->callback
== f
) ? 0 : 1;
175 /* --------------------------------------------------------------------------------------------- */
177 * Try to select another widget. If forward is set, follow tab order.
178 * Otherwise go to the previous widget.
182 do_select_widget (Dlg_head
* h
, GList
* w
, select_dir_t dir
)
184 Widget
*w0
= (Widget
*) h
->current
->data
;
186 if (!dlg_unfocus (h
))
199 h
->current
= g_list_find (h
->widgets
, w0
);
202 /* try find another widget that can take focus */
206 h
->current
= g_list_next (h
->current
);
207 if (h
->current
== NULL
)
208 h
->current
= h
->widgets
;
211 h
->current
= g_list_previous (h
->current
);
212 if (h
->current
== NULL
)
213 h
->current
= g_list_last (h
->widgets
);
217 while (h
->current
!= w
/* && (((Widget *) h->current->data)->options & W_DISABLED) == 0 */ );
219 if (dlg_overlap (w0
, (Widget
*) h
->current
->data
))
221 send_message ((Widget
*) h
->current
->data
, WIDGET_DRAW
, 0);
222 send_message ((Widget
*) h
->current
->data
, WIDGET_FOCUS
, 0);
226 /* --------------------------------------------------------------------------------------------- */
235 /* Use this if the refreshes fail */
238 #endif /* HAVE_SLANG */
241 /* --------------------------------------------------------------------------------------------- */
244 dlg_execute_cmd (Dlg_head
* h
, unsigned long command
)
246 cb_ret_t ret
= MSG_HANDLED
;
250 h
->ret_value
= B_ENTER
;
254 h
->ret_value
= B_CANCEL
;
269 ev_help_t event_data
= { NULL
, h
->help_ctx
};
270 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
276 mc_event_raise (MCEVENT_GROUP_CORE
, "suspend", NULL
);
285 dialog_switch_list ();
287 ret
= MSG_NOT_HANDLED
;
291 dialog_switch_next ();
293 ret
= MSG_NOT_HANDLED
;
297 dialog_switch_prev ();
299 ret
= MSG_NOT_HANDLED
;
303 ret
= MSG_NOT_HANDLED
;
309 /* --------------------------------------------------------------------------------------------- */
312 dlg_handle_key (Dlg_head
* h
, int d_key
)
314 unsigned long command
;
315 command
= keybind_lookup_keymap_command (dialog_map
, d_key
);
316 if ((command
== CK_IgnoreKey
) || (dlg_execute_cmd (h
, command
) == MSG_NOT_HANDLED
))
317 return MSG_NOT_HANDLED
;
322 /* --------------------------------------------------------------------------------------------- */
325 dlg_mouse_event (Dlg_head
* h
, Gpm_Event
* event
)
328 GList
*starting_widget
= h
->current
;
333 /* close the dialog by mouse click out of dialog area */
334 if (mouse_close_dialog
&& !h
->fullscreen
&& ((event
->buttons
& GPM_B_LEFT
) != 0) && ((event
->type
& GPM_DOWN
) != 0) /* left click */
335 && !((x
> h
->x
) && (x
<= h
->x
+ h
->cols
) && (y
> h
->y
) && (y
<= h
->y
+ h
->lines
)))
337 h
->ret_value
= B_CANCEL
;
342 item
= starting_widget
;
347 widget
= (Widget
*) item
->data
;
348 item
= g_list_next (item
);
352 if (((widget
->options
& W_DISABLED
) == 0)
353 && (x
> widget
->x
) && (x
<= widget
->x
+ widget
->cols
)
354 && (y
> widget
->y
) && (y
<= widget
->y
+ widget
->lines
))
357 new_event
.x
-= widget
->x
;
358 new_event
.y
-= widget
->y
;
360 if (widget
->mouse
!= NULL
)
361 return widget
->mouse (&new_event
, widget
);
364 while (item
!= starting_widget
);
369 /* --------------------------------------------------------------------------------------------- */
372 dlg_try_hotkey (Dlg_head
* h
, int d_key
)
379 if (h
->widgets
== NULL
)
380 return MSG_NOT_HANDLED
;
382 if (h
->current
== NULL
)
383 h
->current
= h
->widgets
;
386 * Explanation: we don't send letter hotkeys to other widgets if
387 * the currently selected widget is an input line
390 current
= (Widget
*) h
->current
->data
;
392 if ((current
->options
& W_DISABLED
) != 0)
393 return MSG_NOT_HANDLED
;
395 if (current
->options
& W_IS_INPUT
)
397 /* skip ascii control characters, anything else can valid character in
399 if (d_key
>= 32 && d_key
< 256)
400 return MSG_NOT_HANDLED
;
403 /* If it's an alt key, send the message */
404 c
= d_key
& ~ALT (0);
405 if (d_key
& ALT (0) && g_ascii_isalpha (c
))
406 d_key
= g_ascii_tolower (c
);
408 handled
= MSG_NOT_HANDLED
;
409 if ((current
->options
& W_WANT_HOTKEY
) != 0)
410 handled
= send_message (current
, WIDGET_HOTKEY
, d_key
);
412 /* If not used, send hotkey to other widgets */
413 if (handled
== MSG_HANDLED
)
416 hot_cur
= g_list_next (h
->current
);
418 hot_cur
= h
->widgets
;
420 /* send it to all widgets */
421 while (h
->current
!= hot_cur
&& handled
== MSG_NOT_HANDLED
)
423 current
= (Widget
*) hot_cur
->data
;
425 if ((current
->options
& W_WANT_HOTKEY
) != 0)
426 handled
= send_message (current
, WIDGET_HOTKEY
, d_key
);
428 if (handled
== MSG_NOT_HANDLED
)
430 hot_cur
= g_list_next (hot_cur
);
432 hot_cur
= h
->widgets
;
436 if (handled
== MSG_HANDLED
)
437 do_select_widget (h
, hot_cur
, SELECT_EXACT
);
442 /* --------------------------------------------------------------------------------------------- */
445 dlg_key_event (Dlg_head
* h
, int d_key
)
449 if (h
->widgets
== NULL
)
452 if (h
->current
== NULL
)
453 h
->current
= h
->widgets
;
455 /* TAB used to cycle */
456 if ((h
->flags
& DLG_WANT_TAB
) == 0)
463 else if (d_key
== KEY_BTAB
)
470 /* first can dlg_callback handle the key */
471 handled
= h
->callback (h
, NULL
, DLG_KEY
, d_key
, NULL
);
473 /* next try the hotkey */
474 if (handled
== MSG_NOT_HANDLED
)
475 handled
= dlg_try_hotkey (h
, d_key
);
477 if (handled
== MSG_HANDLED
)
478 h
->callback (h
, NULL
, DLG_HOTKEY_HANDLED
, 0, NULL
);
480 /* not used - then try widget_callback */
481 handled
= send_message ((Widget
*) h
->current
->data
, WIDGET_KEY
, d_key
);
483 /* not used- try to use the unhandled case */
484 if (handled
== MSG_NOT_HANDLED
)
485 handled
= h
->callback (h
, NULL
, DLG_UNHANDLED_KEY
, d_key
, NULL
);
487 if (handled
== MSG_NOT_HANDLED
)
488 handled
= dlg_handle_key (h
, d_key
);
490 h
->callback (h
, NULL
, DLG_POST_KEY
, d_key
, NULL
);
493 /* --------------------------------------------------------------------------------------------- */
496 frontend_run_dlg (Dlg_head
* h
)
503 /* close opened editors, viewers, etc */
504 if (!h
->modal
&& mc_global
.widget
.midnight_shutdown
)
506 h
->callback (h
, NULL
, DLG_VALIDATE
, 0, NULL
);
510 while (h
->state
== DLG_ACTIVE
)
512 if (mc_global
.tty
.winch_flag
)
513 dialog_change_screen_size ();
518 execute_hooks (idle_hook
);
520 while ((h
->flags
& DLG_WANT_IDLE
) && is_idle ())
521 h
->callback (h
, NULL
, DLG_IDLE
, 0, NULL
);
523 /* Allow terminating the dialog from the idle handler */
524 if (h
->state
!= DLG_ACTIVE
)
530 /* Clear interrupt flag */
531 tty_got_interrupt ();
532 d_key
= tty_get_event (&event
, h
->mouse_status
== MOU_REPEAT
, TRUE
);
534 dlg_process_event (h
, d_key
, &event
);
536 if (h
->state
== DLG_CLOSED
)
537 h
->callback (h
, NULL
, DLG_VALIDATE
, 0, NULL
);
541 /* --------------------------------------------------------------------------------------------- */
542 /*** public functions ****************************************************************************/
543 /* --------------------------------------------------------------------------------------------- */
545 /** draw box in window */
547 draw_box (Dlg_head
* h
, int y
, int x
, int ys
, int xs
, gboolean single
)
549 tty_draw_box (h
->y
+ y
, h
->x
+ x
, ys
, xs
, single
);
552 /* --------------------------------------------------------------------------------------------- */
554 /** Clean the dialog area, draw the frame and the title */
556 common_dialog_repaint (Dlg_head
* h
)
560 if (h
->state
!= DLG_ACTIVE
)
563 space
= (h
->flags
& DLG_COMPACT
) ? 0 : 1;
565 tty_setcolor (h
->color
[DLG_COLOR_NORMAL
]);
567 draw_box (h
, space
, space
, h
->lines
- 2 * space
, h
->cols
- 2 * space
, FALSE
);
569 if (h
->title
!= NULL
)
571 tty_setcolor (h
->color
[DLG_COLOR_TITLE
]);
572 dlg_move (h
, space
, (h
->cols
- str_term_width1 (h
->title
)) / 2);
573 tty_print_string (h
->title
);
577 /* --------------------------------------------------------------------------------------------- */
578 /** this function allows to set dialog position */
581 dlg_set_position (Dlg_head
* h
, int y1
, int x1
, int y2
, int x2
)
583 /* save old positions, will be used to reposition childs */
585 int shift_x
, shift_y
, scale_x
, scale_y
;
587 /* save old positions, will be used to reposition childs */
598 /* dialog is empty */
599 if (h
->widgets
== NULL
)
602 if (h
->current
== NULL
)
603 h
->current
= h
->widgets
;
605 /* values by which controls should be moved */
608 scale_x
= h
->cols
- oc
;
609 scale_y
= h
->lines
- ol
;
611 if ((shift_x
!= 0) || (shift_y
!= 0) || (scale_x
!= 0) || (scale_y
!= 0))
615 for (w
= h
->widgets
; w
!= NULL
; w
= g_list_next (w
))
617 /* there are, mainly, 2 generally possible
620 1. control sticks to one side - it
623 2. control sticks to two sides of
624 one direction - it should be sized */
626 Widget
*c
= (Widget
*) w
->data
;
630 int lines
= c
->lines
;
632 if ((c
->pos_flags
& WPOS_KEEP_LEFT
) && (c
->pos_flags
& WPOS_KEEP_RIGHT
))
637 else if (c
->pos_flags
& WPOS_KEEP_LEFT
)
639 else if (c
->pos_flags
& WPOS_KEEP_RIGHT
)
640 x
+= shift_x
+ scale_x
;
642 if ((c
->pos_flags
& WPOS_KEEP_TOP
) && (c
->pos_flags
& WPOS_KEEP_BOTTOM
))
647 else if (c
->pos_flags
& WPOS_KEEP_TOP
)
649 else if (c
->pos_flags
& WPOS_KEEP_BOTTOM
)
650 y
+= shift_y
+ scale_y
;
652 widget_set_size (c
, y
, x
, lines
, cols
);
657 /* --------------------------------------------------------------------------------------------- */
658 /** this function sets only size, leaving positioning to automatic methods */
661 dlg_set_size (Dlg_head
* h
, int lines
, int cols
)
666 if (h
->flags
& DLG_CENTER
)
668 y
= (LINES
- lines
) / 2;
669 x
= (COLS
- cols
) / 2;
672 if ((h
->flags
& DLG_TRYUP
) && (y
> 3))
675 dlg_set_position (h
, y
, x
, y
+ lines
, x
+ cols
);
678 /* --------------------------------------------------------------------------------------------- */
679 /** Default dialog callback */
682 default_dlg_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
691 if (h
->color
!= NULL
)
693 common_dialog_repaint (h
);
696 return MSG_NOT_HANDLED
;
699 dlg_broadcast_msg_to (h
, WIDGET_IDLE
, FALSE
, W_WANT_IDLE
);
703 /* this is default resizing mechanism */
704 /* the main idea of this code is to resize dialog
705 according to flags (if any of flags require automatic
706 resizing, like DLG_CENTER, end after that reposition
707 controls in dialog according to flags of widget) */
708 dlg_set_size (h
, h
->lines
, h
->cols
);
715 return MSG_NOT_HANDLED
;
718 /* --------------------------------------------------------------------------------------------- */
721 create_dlg (gboolean modal
, int y1
, int x1
, int lines
, int cols
,
722 const int *colors
, dlg_cb_fn callback
, const char *help_ctx
,
723 const char *title
, dlg_flags_t flags
)
727 new_d
= g_new0 (Dlg_head
, 1);
728 new_d
->modal
= modal
;
730 memmove (new_d
->color
, colors
, sizeof (dlg_colors_t
));
731 new_d
->help_ctx
= help_ctx
;
732 new_d
->callback
= (callback
!= NULL
) ? callback
: default_dlg_callback
;
735 new_d
->flags
= flags
;
738 dlg_set_size (new_d
, lines
, cols
);
739 new_d
->fullscreen
= (new_d
->x
== 0 && new_d
->y
== 0
740 && new_d
->cols
== COLS
&& new_d
->lines
== LINES
);
742 new_d
->mouse_status
= MOU_NORMAL
;
744 /* Strip existing spaces, add one space before and after the title */
749 t
= g_strstrip (g_strdup (title
));
751 new_d
->title
= g_strdup_printf (" %s ", t
);
755 /* unique name got event group for this dialog */
756 new_d
->event_group
= g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG
, (void *) new_d
);
761 /* --------------------------------------------------------------------------------------------- */
764 dlg_set_default_colors (void)
766 dialog_colors
[DLG_COLOR_NORMAL
] = COLOR_NORMAL
;
767 dialog_colors
[DLG_COLOR_FOCUS
] = COLOR_FOCUS
;
768 dialog_colors
[DLG_COLOR_HOT_NORMAL
] = COLOR_HOT_NORMAL
;
769 dialog_colors
[DLG_COLOR_HOT_FOCUS
] = COLOR_HOT_FOCUS
;
770 dialog_colors
[DLG_COLOR_TITLE
] = COLOR_TITLE
;
772 alarm_colors
[DLG_COLOR_NORMAL
] = ERROR_COLOR
;
773 alarm_colors
[DLG_COLOR_FOCUS
] = ERROR_FOCUS
;
774 alarm_colors
[DLG_COLOR_HOT_NORMAL
] = ERROR_HOT_NORMAL
;
775 alarm_colors
[DLG_COLOR_HOT_FOCUS
] = ERROR_HOT_FOCUS
;
776 alarm_colors
[DLG_COLOR_TITLE
] = ERROR_TITLE
;
779 /* --------------------------------------------------------------------------------------------- */
782 dlg_erase (Dlg_head
* h
)
784 if ((h
!= NULL
) && (h
->state
== DLG_ACTIVE
))
785 tty_fill_region (h
->y
, h
->x
, h
->lines
, h
->cols
, ' ');
788 /* --------------------------------------------------------------------------------------------- */
791 set_idle_proc (Dlg_head
* d
, int enable
)
794 d
->flags
|= DLG_WANT_IDLE
;
796 d
->flags
&= ~DLG_WANT_IDLE
;
799 /* --------------------------------------------------------------------------------------------- */
801 * Insert widget to dialog before current widget. For dialogs populated
802 * from the bottom, make the widget current. Return widget number.
806 add_widget_autopos (Dlg_head
* h
, void *w
, widget_pos_flags_t pos_flags
)
808 Widget
*widget
= (Widget
*) w
;
810 /* Don't accept 0 widgets */
817 widget
->pos_flags
= pos_flags
;
818 widget
->id
= g_list_length (h
->widgets
);
820 if ((h
->flags
& DLG_REVERSE
) != 0)
821 h
->widgets
= g_list_prepend (h
->widgets
, widget
);
823 h
->widgets
= g_list_append (h
->widgets
, widget
);
825 h
->current
= h
->widgets
;
830 /* --------------------------------------------------------------------------------------------- */
831 /** wrapper to simply add lefttop positioned controls */
834 add_widget (Dlg_head
* h
, void *w
)
836 return add_widget_autopos (h
, w
, WPOS_KEEP_LEFT
| WPOS_KEEP_TOP
);
839 /* --------------------------------------------------------------------------------------------- */
848 if ((d
!= NULL
) && (d
->data
!= NULL
))
849 dlg_redraw ((Dlg_head
*) d
->data
);
853 /* Search first fullscreen dialog */
854 for (; d
!= NULL
; d
= g_list_next (d
))
855 if ((d
->data
!= NULL
) && ((Dlg_head
*) d
->data
)->fullscreen
)
857 /* back to top dialog */
858 for (; d
!= NULL
; d
= g_list_previous (d
))
860 dlg_redraw ((Dlg_head
*) d
->data
);
864 /* --------------------------------------------------------------------------------------------- */
865 /** broadcast a message to all the widgets in a dialog */
868 dlg_broadcast_msg (Dlg_head
* h
, widget_msg_t msg
, gboolean reverse
)
870 dlg_broadcast_msg_to (h
, msg
, reverse
, 0);
873 /* --------------------------------------------------------------------------------------------- */
876 dlg_focus (Dlg_head
* h
)
878 /* cannot focus disabled widget ... */
880 if ((h
->current
!= NULL
) && (h
->state
== DLG_ACTIVE
))
882 Widget
*current
= (Widget
*) h
->current
->data
;
884 if (((current
->options
& W_DISABLED
) == 0)
885 && (send_message (current
, WIDGET_FOCUS
, 0) == MSG_HANDLED
))
887 h
->callback (h
, current
, DLG_FOCUS
, 0, NULL
);
895 /* --------------------------------------------------------------------------------------------- */
896 /** Return true if the windows overlap */
899 dlg_overlap (Widget
* a
, Widget
* b
)
901 return !((b
->x
>= a
->x
+ a
->cols
)
902 || (a
->x
>= b
->x
+ b
->cols
) || (b
->y
>= a
->y
+ a
->lines
) || (a
->y
>= b
->y
+ b
->lines
));
906 /* --------------------------------------------------------------------------------------------- */
907 /** Find the widget with the given callback in the dialog h */
910 find_widget_type (const Dlg_head
* h
, callback_fn callback
)
914 w
= g_list_find_custom (h
->widgets
, callback
, dlg_find_widget_callback
);
916 return (w
== NULL
) ? NULL
: (Widget
*) w
->data
;
919 /* --------------------------------------------------------------------------------------------- */
920 /** Find the widget with the given id */
923 dlg_find_by_id (const Dlg_head
* h
, unsigned int id
)
925 if (h
->widgets
!= NULL
)
929 for (w
= h
->widgets
; w
!= NULL
; w
= g_list_next (w
))
930 if (((Widget
*) w
->data
)->id
== id
)
931 return (Widget
*) w
->data
;
937 /* --------------------------------------------------------------------------------------------- */
938 /** Find the widget with the given id in the dialog h and select it */
941 dlg_select_by_id (const Dlg_head
* h
, unsigned int id
)
945 w
= dlg_find_by_id (h
, id
);
947 dlg_select_widget (w
);
950 /* --------------------------------------------------------------------------------------------- */
952 * Try to select widget in the dialog.
956 dlg_select_widget (void *w
)
958 const Widget
*widget
= (Widget
*) w
;
959 Dlg_head
*h
= widget
->owner
;
961 do_select_widget (h
, g_list_find (h
->widgets
, widget
), SELECT_EXACT
);
964 /* --------------------------------------------------------------------------------------------- */
965 /** Try to select previous widget in the tab order */
968 dlg_one_up (Dlg_head
* h
)
970 if (h
->widgets
!= NULL
)
974 prev
= g_list_previous (h
->current
);
976 prev
= g_list_last (h
->widgets
);
978 do_select_widget (h
, prev
, SELECT_PREV
);
982 /* --------------------------------------------------------------------------------------------- */
983 /** Try to select next widget in the tab order */
986 dlg_one_down (Dlg_head
* h
)
988 if (h
->widgets
!= NULL
)
992 next
= g_list_next (h
->current
);
995 do_select_widget (h
, next
, SELECT_NEXT
);
999 /* --------------------------------------------------------------------------------------------- */
1002 update_cursor (Dlg_head
* h
)
1004 GList
*p
= h
->current
;
1006 if ((p
!= NULL
) && (h
->state
== DLG_ACTIVE
))
1010 w
= (Widget
*) p
->data
;
1012 if (((w
->options
& W_DISABLED
) == 0) && ((w
->options
& W_WANT_CURSOR
) != 0))
1013 send_message (w
, WIDGET_CURSOR
, 0);
1017 p
= g_list_next (p
);
1021 if (p
== h
->current
)
1024 w
= (Widget
*) p
->data
;
1026 if (((w
->options
& W_DISABLED
) == 0) && ((w
->options
& W_WANT_CURSOR
) != 0))
1027 if (send_message (w
, WIDGET_CURSOR
, 0) == MSG_HANDLED
)
1034 /* --------------------------------------------------------------------------------------------- */
1036 * Redraw the widgets in reverse order, leaving the current widget
1041 dlg_redraw (Dlg_head
* h
)
1043 if (h
->state
!= DLG_ACTIVE
)
1046 if (h
->winch_pending
)
1048 h
->winch_pending
= FALSE
;
1049 h
->callback (h
, NULL
, DLG_RESIZE
, 0, NULL
);
1052 h
->callback (h
, NULL
, DLG_DRAW
, 0, NULL
);
1053 dlg_broadcast_msg (h
, WIDGET_DRAW
, TRUE
);
1057 /* --------------------------------------------------------------------------------------------- */
1060 dlg_stop (Dlg_head
* h
)
1062 h
->state
= DLG_CLOSED
;
1065 /* --------------------------------------------------------------------------------------------- */
1066 /** Init the process */
1069 init_dlg (Dlg_head
* h
)
1071 if ((top_dlg
!= NULL
) && ((Dlg_head
*) top_dlg
->data
)->modal
)
1074 /* add dialog to the stack */
1075 top_dlg
= g_list_prepend (top_dlg
, h
);
1077 /* Initialize dialog manager and widgets */
1078 if (h
->state
== DLG_ACTIVE
)
1081 dialog_switch_add (h
);
1083 h
->callback (h
, NULL
, DLG_INIT
, 0, NULL
);
1084 dlg_broadcast_msg (h
, WIDGET_INIT
, FALSE
);
1087 h
->state
= DLG_ACTIVE
;
1091 /* Select the first widget that takes focus */
1092 while (h
->current
!= NULL
&& !dlg_focus (h
))
1094 h
->current
= g_list_next (h
->current
);
1095 if (h
->current
== NULL
)
1096 h
->current
= h
->widgets
;
1102 /* --------------------------------------------------------------------------------------------- */
1105 dlg_process_event (Dlg_head
* h
, int key
, Gpm_Event
* event
)
1109 if (tty_got_interrupt ())
1110 if (h
->callback (h
, NULL
, DLG_ACTION
, CK_Cancel
, NULL
) != MSG_HANDLED
)
1111 dlg_execute_cmd (h
, CK_Cancel
);
1116 if (key
== EV_MOUSE
)
1117 h
->mouse_status
= dlg_mouse_event (h
, event
);
1119 dlg_key_event (h
, key
);
1122 /* --------------------------------------------------------------------------------------------- */
1123 /** Shutdown the run_dlg */
1126 dlg_run_done (Dlg_head
* h
)
1128 top_dlg
= g_list_remove (top_dlg
, h
);
1130 if (h
->state
== DLG_CLOSED
)
1132 h
->callback (h
, (Widget
*) h
->current
->data
, DLG_END
, 0, NULL
);
1134 dialog_switch_remove (h
);
1139 /* --------------------------------------------------------------------------------------------- */
1141 * Standard run dialog routine
1142 * We have to keep this routine small so that we can duplicate it's
1143 * behavior on complex routines like the file routines, this way,
1144 * they can call the dlg_process_event without rewriting all the code
1148 run_dlg (Dlg_head
* h
)
1151 frontend_run_dlg (h
);
1153 return h
->ret_value
;
1156 /* --------------------------------------------------------------------------------------------- */
1159 destroy_dlg (Dlg_head
* h
)
1161 /* if some widgets have history, save all history at one moment here */
1162 dlg_save_history (h
);
1163 dlg_broadcast_msg (h
, WIDGET_DESTROY
, FALSE
);
1164 g_list_foreach (h
->widgets
, (GFunc
) g_free
, NULL
);
1165 g_list_free (h
->widgets
);
1166 mc_event_group_del (h
->event_group
);
1167 g_free (h
->event_group
);
1174 /* --------------------------------------------------------------------------------------------- */
1177 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1180 dlg_save_history (Dlg_head
* h
)
1185 if (num_history_items_recorded
== 0) /* this is how to disable */
1188 profile
= g_build_filename (mc_config_get_cache_path (), MC_HISTORY_FILE
, (char *) NULL
);
1189 i
= open (profile
, O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
);
1193 /* Make sure the history is only readable by the user */
1194 if (chmod (profile
, S_IRUSR
| S_IWUSR
) != -1 || errno
== ENOENT
)
1196 ev_history_load_save_t event_data
;
1198 event_data
.cfg
= mc_config_init (profile
);
1199 event_data
.receiver
= NULL
;
1201 /* get all histories in dialog */
1202 mc_event_raise (h
->event_group
, MCEVENT_HISTORY_SAVE
, &event_data
);
1204 mc_config_save_file (event_data
.cfg
, NULL
);
1205 mc_config_deinit (event_data
.cfg
);
1211 /* --------------------------------------------------------------------------------------------- */
1214 dlg_get_title (const Dlg_head
* h
, size_t len
)
1221 if (h
->get_title
!= NULL
)
1222 t
= h
->get_title (h
, len
);
1229 /* --------------------------------------------------------------------------------------------- */
1230 /** Replace widget old_w for widget new_w in the dialog */
1233 dlg_replace_widget (Widget
* old_w
, Widget
* new_w
)
1235 Dlg_head
*h
= old_w
->owner
;
1236 gboolean should_focus
= FALSE
;
1238 if (h
->widgets
== NULL
)
1241 if (h
->current
== NULL
)
1242 h
->current
= h
->widgets
;
1244 if (old_w
== h
->current
->data
)
1245 should_focus
= TRUE
;
1248 new_w
->id
= old_w
->id
;
1251 h
->current
->data
= new_w
;
1253 g_list_find (h
->widgets
, old_w
)->data
= new_w
;
1255 send_message (old_w
, WIDGET_DESTROY
, 0);
1256 send_message (new_w
, WIDGET_INIT
, 0);
1259 dlg_select_widget (new_w
);
1261 send_message (new_w
, WIDGET_DRAW
, 0);
1264 /* --------------------------------------------------------------------------------------------- */