1 /* Dialog box features module for the Midnight Commander
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2007 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
34 #include "../src/tty/tty.h"
35 #include "../src/skin/skin.h"
36 #include "../src/tty/mouse.h"
37 #include "../src/tty/key.h"
39 #include "help.h" /* interactive_display() */
42 #include "execute.h" /* suspend_cmd() */
43 #include "main.h" /* fast_refresh */
45 #include "setup.h" /* mouse_close_dialog */
47 /* Color styles for normal and error dialogs */
48 int dialog_colors
[4];
51 /* Primitive way to check if the the current dialog is our dialog */
52 /* This is needed by async routines like load_prompt */
53 Dlg_head
*current_dlg
= 0;
55 /* A hook list for idle events */
58 /* left click outside of dialog closes it */
59 int mouse_close_dialog
= 0;
61 static void dlg_broadcast_msg_to (Dlg_head
* h
, widget_msg_t message
,
62 int reverse
, int flags
);
64 /* draw box in window */
66 draw_box (Dlg_head
*h
, int y
, int x
, int ys
, int xs
)
68 tty_draw_box (h
->y
+ y
, h
->x
+ x
, ys
, xs
);
72 widget_erase (Widget
*w
)
74 tty_fill_region (w
->y
, w
->x
, w
->lines
, w
->cols
, ' ');
78 dlg_erase (Dlg_head
*h
)
81 tty_fill_region (h
->y
, h
->x
, h
->lines
, h
->cols
, ' ');
85 init_widget (Widget
*w
, int y
, int x
, int lines
, int cols
,
86 callback_fn callback
, mouse_h mouse_handler
)
92 w
->callback
= callback
;
93 w
->mouse
= mouse_handler
;
96 /* Almost all widgets want to put the cursor in a suitable place */
97 w
->options
= W_WANT_CURSOR
;
100 /* Clean the dialog area, draw the frame and the title */
102 common_dialog_repaint (struct Dlg_head
*h
)
106 space
= (h
->flags
& DLG_COMPACT
) ? 0 : 1;
108 tty_setcolor (DLG_NORMALC (h
));
110 draw_box (h
, space
, space
, h
->lines
- 2 * space
, h
->cols
- 2 * space
);
113 tty_setcolor (DLG_HOT_NORMALC (h
));
114 dlg_move (h
, space
, (h
->cols
- str_term_width1 (h
->title
)) / 2);
115 tty_print_string (h
->title
);
119 /* this function allows to set dialog position */
121 dlg_set_position (Dlg_head
*h
, int y1
, int x1
, int y2
, int x2
)
123 /* save old positions, will be used to reposition childs */
125 int shift_x
, shift_y
, scale_x
, scale_y
;
127 /* save old positions, will be used to reposition childs */
138 /* values by which controls should be moved */
141 scale_x
= h
->cols
- oc
;
142 scale_y
= h
->lines
- ol
;
144 if (h
->current
== NULL
)
147 if ((shift_x
!= 0) || (shift_y
!= 0) || (scale_x
!= 0) || (scale_y
!= 0)) {
148 Widget
*c
= h
->current
;
151 /* there are, mainly, 2 generally possible
154 1. control sticks to one side - it
157 2. control sticks to two sides of
158 one direction - it should be sized */
163 int lines
= c
->lines
;
165 if ((c
->pos_flags
& WPOS_KEEP_LEFT
) && (c
->pos_flags
& WPOS_KEEP_RIGHT
)) {
168 } else if (c
->pos_flags
& WPOS_KEEP_LEFT
)
170 else if (c
->pos_flags
& WPOS_KEEP_RIGHT
)
171 x
+= shift_x
+ scale_x
;
173 if ((c
->pos_flags
& WPOS_KEEP_TOP
) && (c
->pos_flags
& WPOS_KEEP_BOTTOM
)) {
176 } else if (c
->pos_flags
& WPOS_KEEP_TOP
)
178 else if (c
->pos_flags
& WPOS_KEEP_BOTTOM
)
179 y
+= shift_y
+ scale_y
;
181 widget_set_size (c
, y
, x
, lines
, cols
);
184 } while (h
->current
!= c
);
188 /* this function sets only size, leaving positioning to automatic methods */
190 dlg_set_size (Dlg_head
*h
, int lines
, int cols
)
195 if (h
->flags
& DLG_CENTER
) {
196 y
= (LINES
- lines
) / 2;
197 x
= (COLS
- cols
) / 2;
200 if ((h
->flags
& DLG_TRYUP
) && (y
> 3))
203 dlg_set_position (h
, y
, x
, y
+ lines
, x
+ cols
);
206 /* Default dialog callback */
207 cb_ret_t
default_dlg_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
213 if (h
->color
!= NULL
) {
214 common_dialog_repaint (h
);
217 return MSG_NOT_HANDLED
;
220 dlg_broadcast_msg_to (h
, WIDGET_IDLE
, 0, W_WANT_IDLE
);
224 /* this is default resizing mechanism */
225 /* the main idea of this code is to resize dialog
226 according to flags (if any of flags require automatic
227 resizing, like DLG_CENTER, end after that reposition
228 controls in dialog according to flags of widget) */
229 dlg_set_size (h
, h
->lines
, h
->cols
);
236 return MSG_NOT_HANDLED
;
240 create_dlg (int y1
, int x1
, int lines
, int cols
, const int *color_set
,
241 dlg_cb_fn callback
, const char *help_ctx
, const char *title
,
246 new_d
= g_new0 (Dlg_head
, 1);
247 if (color_set
!= NULL
) {
248 new_d
->color
= g_new (int, DLG_COLOR_NUM
);
249 memmove (new_d
->color
, color_set
, sizeof (int) * DLG_COLOR_NUM
);
251 new_d
->help_ctx
= help_ctx
;
252 new_d
->callback
= (callback
!= NULL
) ? callback
: default_dlg_callback
;
255 new_d
->flags
= flags
;
258 dlg_set_size (new_d
, lines
, cols
);
260 /* Strip existing spaces, add one space before and after the title */
264 t
= g_strstrip (g_strdup (title
));
266 new_d
->title
= g_strdup_printf (" %s ", t
);
274 dlg_set_default_colors (void)
276 dialog_colors
[0] = COLOR_NORMAL
;
277 dialog_colors
[1] = COLOR_FOCUS
;
278 dialog_colors
[2] = COLOR_HOT_NORMAL
;
279 dialog_colors
[3] = COLOR_HOT_FOCUS
;
281 alarm_colors
[0] = ERROR_COLOR
;
282 alarm_colors
[1] = REVERSE_COLOR
;
283 alarm_colors
[2] = ERROR_HOT_NORMAL
;
284 alarm_colors
[3] = ERROR_HOT_FOCUS
;
288 set_idle_proc (Dlg_head
*d
, int enable
)
291 d
->flags
|= DLG_WANT_IDLE
;
293 d
->flags
&= ~DLG_WANT_IDLE
;
297 * Insert widget to dialog before current widget. For dialogs populated
298 * from the bottom, make the widget current. Return widget number.
301 add_widget_autopos (Dlg_head
*h
, void *w
, widget_pos_flags_t pos_flags
)
303 Widget
*widget
= (Widget
*) w
;
305 /* Don't accept 0 widgets, and running dialogs */
306 if (!widget
|| h
->running
)
312 widget
->dlg_id
= h
->count
++;
313 widget
->pos_flags
= pos_flags
;
316 widget
->next
= h
->current
;
317 widget
->prev
= h
->current
->prev
;
318 h
->current
->prev
->next
= widget
;
319 h
->current
->prev
= widget
;
321 widget
->prev
= widget
;
322 widget
->next
= widget
;
325 if ((h
->flags
& DLG_REVERSE
) || !h
->current
)
328 return widget
->dlg_id
;
331 /* wrapper to simply add lefttop positioned controls */
333 add_widget (Dlg_head
*h
, void *w
)
335 return add_widget_autopos (h
, w
, WPOS_KEEP_LEFT
| WPOS_KEEP_TOP
);
339 REFRESH_COVERS_PART
, /* If the refresh fn convers only a part */
340 REFRESH_COVERS_ALL
/* If the refresh fn convers all the screen */
344 do_complete_refresh (Dlg_head
*dlg
)
346 if (!dlg
->fullscreen
&& dlg
->parent
)
347 do_complete_refresh (dlg
->parent
);
359 dlg_redraw (current_dlg
);
361 do_complete_refresh (current_dlg
);
365 /* broadcast a message to all the widgets in a dialog that have
366 * the options set to flags. If flags is zero, the message is sent
370 dlg_broadcast_msg_to (Dlg_head
*h
, widget_msg_t message
, int reverse
,
373 Widget
*p
, *first
, *wi
;
379 first
= p
= h
->current
->prev
;
381 first
= p
= h
->current
->next
;
389 if (flags
== 0 || (flags
& wi
->options
))
390 send_message (wi
, message
, 0);
391 } while (first
!= p
);
394 /* broadcast a message to all the widgets in a dialog */
396 dlg_broadcast_msg (Dlg_head
*h
, widget_msg_t message
, int reverse
)
398 dlg_broadcast_msg_to (h
, message
, reverse
, 0);
401 int dlg_focus (Dlg_head
*h
)
406 if (send_message (h
->current
, WIDGET_FOCUS
, 0)){
407 (*h
->callback
) (h
, DLG_FOCUS
, 0);
414 dlg_unfocus (Dlg_head
*h
)
419 if (send_message (h
->current
, WIDGET_UNFOCUS
, 0)){
420 (*h
->callback
) (h
, DLG_UNFOCUS
, 0);
427 /* Return true if the windows overlap */
428 int dlg_overlap (Widget
*a
, Widget
*b
)
430 if ((b
->x
>= a
->x
+ a
->cols
)
431 || (a
->x
>= b
->x
+ b
->cols
)
432 || (b
->y
>= a
->y
+ a
->lines
)
433 || (a
->y
>= b
->y
+ b
->lines
))
439 /* Find the widget with the given callback in the dialog h */
441 find_widget_type (Dlg_head
*h
, callback_fn callback
)
453 for (i
= 0, item
= h
->current
; i
< h
->count
; i
++, item
= item
->next
) {
454 if (item
->callback
== callback
) {
462 /* Find the widget with the given dialog id in the dialog h and select it */
464 dlg_select_by_id (Dlg_head
*h
, int id
)
475 if (w
->dlg_id
== id
) {
480 } while (w
!= h
->current
);
483 dlg_select_widget(w_found
);
487 /* What to do if the requested widget doesn't take focus */
489 SELECT_NEXT
, /* go the the next widget */
490 SELECT_PREV
, /* go the the previous widget */
491 SELECT_EXACT
/* use current widget */
495 * Try to select another widget. If forward is set, follow tab order.
496 * Otherwise go to the previous widget.
499 do_select_widget (Dlg_head
*h
, Widget
*w
, select_dir_t dir
)
501 Widget
*w0
= h
->current
;
503 if (!dlg_unfocus (h
))
513 h
->current
= h
->current
->next
;
516 h
->current
= h
->current
->prev
;
523 } while (h
->current
!= w
);
525 if (dlg_overlap (w0
, h
->current
)) {
526 send_message (h
->current
, WIDGET_DRAW
, 0);
527 send_message (h
->current
, WIDGET_FOCUS
, 0);
533 * Try to select widget in the dialog.
536 dlg_select_widget (void *w
)
538 do_select_widget (((Widget
*) w
)->parent
, w
, SELECT_NEXT
);
542 /* Try to select previous widget in the tab order */
544 dlg_one_up (Dlg_head
*h
)
547 do_select_widget (h
, h
->current
->prev
, SELECT_PREV
);
551 /* Try to select next widget in the tab order */
553 dlg_one_down (Dlg_head
*h
)
556 do_select_widget (h
, h
->current
->next
, SELECT_NEXT
);
560 void update_cursor (Dlg_head
*h
)
562 Widget
*p
= h
->current
;
565 if (p
->options
& W_WANT_CURSOR
)
566 send_message (p
, WIDGET_CURSOR
, 0);
568 while ((p
= p
->next
) != h
->current
)
569 if (p
->options
& W_WANT_CURSOR
)
570 if (send_message (p
, WIDGET_CURSOR
, 0) == MSG_HANDLED
)
575 /* Redraw the widgets in reverse order, leaving the current widget
578 void dlg_redraw (Dlg_head
*h
)
580 (h
->callback
)(h
, DLG_DRAW
, 0);
582 dlg_broadcast_msg (h
, WIDGET_DRAW
, 1);
587 void dlg_stop (Dlg_head
*h
)
593 dialog_handle_key (Dlg_head
*h
, int d_key
)
595 if (is_abort_char (d_key
)) {
596 h
->ret_value
= B_CANCEL
;
604 h
->ret_value
= B_ENTER
;
619 interactive_display (NULL
, h
->help_ctx
);
632 /* Use this if the refreshes fail */
635 #endif /* HAVE_SLANG */
644 dlg_try_hotkey (Dlg_head
*h
, int d_key
)
650 if (h
->current
== NULL
)
651 return MSG_NOT_HANDLED
;
654 * Explanation: we don't send letter hotkeys to other widgets if
655 * the currently selected widget is an input line
658 if (h
->current
->options
& W_IS_INPUT
) {
659 /* skip ascii control characters, anything else can valid character in
661 if (d_key
>= 32 && d_key
< 256)
662 return MSG_NOT_HANDLED
;
665 /* If it's an alt key, send the message */
666 c
= d_key
& ~ALT (0);
667 if (d_key
& ALT (0) && g_ascii_isalpha (c
))
668 d_key
= g_ascii_tolower (c
);
670 handled
= MSG_NOT_HANDLED
;
671 if (h
->current
->options
& W_WANT_HOTKEY
)
672 handled
= send_message (h
->current
, WIDGET_HOTKEY
, d_key
);
674 /* If not used, send hotkey to other widgets */
675 if (handled
== MSG_HANDLED
)
678 hot_cur
= h
->current
->next
;
680 /* send it to all widgets */
681 while (h
->current
!= hot_cur
&& handled
== MSG_NOT_HANDLED
) {
682 if (hot_cur
->options
& W_WANT_HOTKEY
)
683 handled
= send_message (hot_cur
, WIDGET_HOTKEY
, d_key
);
685 if (handled
== MSG_NOT_HANDLED
)
686 hot_cur
= hot_cur
->next
;
689 if (handled
== MSG_HANDLED
)
690 do_select_widget (h
, hot_cur
, SELECT_EXACT
);
696 dlg_key_event (Dlg_head
*h
, int d_key
)
700 if (h
->current
== NULL
)
703 /* TAB used to cycle */
704 if (!(h
->flags
& DLG_WANT_TAB
)) {
708 } else if (d_key
== KEY_BTAB
) {
714 /* first can dlg_callback handle the key */
715 handled
= (*h
->callback
) (h
, DLG_KEY
, d_key
);
717 /* next try the hotkey */
718 if (handled
== MSG_NOT_HANDLED
)
719 handled
= dlg_try_hotkey (h
, d_key
);
721 if (handled
== MSG_HANDLED
)
722 (*h
->callback
) (h
, DLG_HOTKEY_HANDLED
, 0);
724 /* not used - then try widget_callback */
725 handled
= send_message (h
->current
, WIDGET_KEY
, d_key
);
727 /* not used- try to use the unhandled case */
728 if (handled
== MSG_NOT_HANDLED
)
729 handled
= (*h
->callback
) (h
, DLG_UNHANDLED_KEY
, d_key
);
731 if (handled
== MSG_NOT_HANDLED
)
732 dialog_handle_key (h
, d_key
);
734 (*h
->callback
) (h
, DLG_POST_KEY
, d_key
);
738 dlg_mouse_event (Dlg_head
* h
, Gpm_Event
* event
)
741 Widget
*starting_widget
= h
->current
;
746 /* close the dialog by mouse click out of dialog area */
747 if (mouse_close_dialog
&& !h
->fullscreen
748 && ((event
->buttons
& GPM_B_LEFT
) != 0) && ((event
->type
& GPM_DOWN
) != 0) /* left click */
749 && !((x
> h
->x
) && (x
<= h
->x
+ h
->cols
) && (y
> h
->y
) && (y
<= h
->y
+ h
->lines
))) {
750 h
->ret_value
= B_CANCEL
;
755 item
= starting_widget
;
762 if ((x
> widget
->x
) && (x
<= widget
->x
+ widget
->cols
)
763 && (y
> widget
->y
) && (y
<= widget
->y
+ widget
->lines
)) {
765 new_event
.x
-= widget
->x
;
766 new_event
.y
-= widget
->y
;
768 if (widget
->mouse
!= NULL
)
769 return widget
->mouse (&new_event
, widget
);
771 } while (item
!= starting_widget
);
776 /* Run dialog routines */
778 /* Init the process */
779 void init_dlg (Dlg_head
*h
)
781 /* Initialize dialog manager and widgets */
782 (*h
->callback
) (h
, DLG_INIT
, 0);
783 dlg_broadcast_msg (h
, WIDGET_INIT
, 0);
785 if (h
->x
== 0 && h
->y
== 0 && h
->cols
== COLS
&& h
->lines
== LINES
)
788 h
->parent
= current_dlg
;
791 /* Initialize the mouse status */
792 h
->mouse_status
= MOU_NORMAL
;
794 /* Select the first widget that takes focus */
795 while (!dlg_focus (h
) && h
->current
)
796 h
->current
= h
->current
->next
;
798 /* Redraw the screen */
805 /* Shutdown the run_dlg */
806 void dlg_run_done (Dlg_head
*h
)
809 (*h
->callback
) (h
, DLG_END
, 0);
811 current_dlg
= h
->parent
;
814 void dlg_process_event (Dlg_head
*h
, int key
, Gpm_Event
*event
)
817 if (tty_got_interrupt ())
824 h
->mouse_status
= dlg_mouse_event (h
, event
);
826 dlg_key_event (h
, key
);
830 frontend_run_dlg (Dlg_head
*h
)
838 change_screen_size ();
842 execute_hooks (idle_hook
);
844 while ((h
->flags
& DLG_WANT_IDLE
) && is_idle ())
845 (*h
->callback
) (h
, DLG_IDLE
, 0);
847 /* Allow terminating the dialog from the idle handler */
854 /* Clear interrupt flag */
855 tty_got_interrupt ();
856 d_key
= tty_get_event (&event
, h
->mouse_status
== MOU_REPEAT
, TRUE
);
858 dlg_process_event (h
, d_key
, &event
);
861 (*h
->callback
) (h
, DLG_VALIDATE
, 0);
865 /* Standard run dialog routine
866 * We have to keep this routine small so that we can duplicate it's
867 * behavior on complex routines like the file routines, this way,
868 * they can call the dlg_process_event without rewriting all the code
870 int run_dlg (Dlg_head
*h
)
873 frontend_run_dlg (h
);
879 destroy_dlg (Dlg_head
*h
)
884 dlg_broadcast_msg (h
, WIDGET_DESTROY
, 0);
886 for (i
= 0; i
< h
->count
; i
++) {
898 void widget_set_size (Widget
*widget
, int y
, int x
, int lines
, int cols
)
903 widget
->lines
= lines
;
904 send_message (widget
, WIDGET_RESIZED
, 0 /* unused */);
907 /* Replace widget old_w for widget new_w in the dialog */
909 dlg_replace_widget (Widget
*old_w
, Widget
*new_w
)
911 Dlg_head
*h
= old_w
->parent
;
912 int should_focus
= 0;
917 if (old_w
== h
->current
)
921 new_w
->dlg_id
= old_w
->dlg_id
;
923 if (old_w
== old_w
->next
) {
924 /* just one widget */
928 new_w
->prev
= old_w
->prev
;
929 new_w
->next
= old_w
->next
;
930 old_w
->prev
->next
= new_w
;
931 old_w
->next
->prev
= new_w
;
937 send_message (old_w
, WIDGET_DESTROY
, 0);
938 send_message (new_w
, WIDGET_INIT
, 0);
941 dlg_select_widget (new_w
);
943 send_message (new_w
, WIDGET_DRAW
, 0);