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
32 #include "lib/global.h"
34 #include "lib/tty/tty.h"
36 #include "lib/tty/mouse.h"
37 #include "lib/tty/key.h"
38 #include "lib/strutil.h"
40 #include "help.h" /* interactive_display() */
43 #include "execute.h" /* suspend_cmd() */
46 #include "main.h" /* fast_refresh */
47 #include "setup.h" /* mouse_close_dialog */
49 /* Color styles for normal and error dialogs */
53 /* Primitive way to check if the the current dialog is our dialog */
54 /* This is needed by async routines like load_prompt */
55 Dlg_head
*current_dlg
= 0;
57 /* A hook list for idle events */
60 /* left click outside of dialog closes it */
61 int mouse_close_dialog
= 0;
63 static void dlg_broadcast_msg_to (Dlg_head
* h
, widget_msg_t message
, int reverse
, int flags
);
65 /* draw box in window */
67 draw_box (Dlg_head
* h
, int y
, int x
, int ys
, int xs
, gboolean single
)
69 tty_draw_box (h
->y
+ y
, h
->x
+ x
, ys
, xs
, single
);
73 widget_erase (Widget
* w
)
75 tty_fill_region (w
->y
, w
->x
, w
->lines
, w
->cols
, ' ');
79 dlg_erase (Dlg_head
* h
)
82 tty_fill_region (h
->y
, h
->x
, h
->lines
, h
->cols
, ' ');
86 init_widget (Widget
* w
, int y
, int x
, int lines
, int cols
,
87 callback_fn callback
, mouse_h mouse_handler
)
93 w
->callback
= callback
;
94 w
->mouse
= mouse_handler
;
97 /* Almost all widgets want to put the cursor in a suitable place */
98 w
->options
= W_WANT_CURSOR
;
101 /* Clean the dialog area, draw the frame and the title */
103 common_dialog_repaint (struct Dlg_head
*h
)
107 space
= (h
->flags
& DLG_COMPACT
) ? 0 : 1;
109 tty_setcolor (DLG_NORMALC (h
));
111 draw_box (h
, space
, space
, h
->lines
- 2 * space
, h
->cols
- 2 * space
, FALSE
);
115 tty_setcolor (DLG_HOT_NORMALC (h
));
116 dlg_move (h
, space
, (h
->cols
- str_term_width1 (h
->title
)) / 2);
117 tty_print_string (h
->title
);
121 /* this function allows to set dialog position */
123 dlg_set_position (Dlg_head
* h
, int y1
, int x1
, int y2
, int x2
)
125 /* save old positions, will be used to reposition childs */
127 int shift_x
, shift_y
, scale_x
, scale_y
;
129 /* save old positions, will be used to reposition childs */
140 /* values by which controls should be moved */
143 scale_x
= h
->cols
- oc
;
144 scale_y
= h
->lines
- ol
;
146 if (h
->current
== NULL
)
149 if ((shift_x
!= 0) || (shift_y
!= 0) || (scale_x
!= 0) || (scale_y
!= 0))
151 Widget
*c
= h
->current
;
155 /* there are, mainly, 2 generally possible
158 1. control sticks to one side - it
161 2. control sticks to two sides of
162 one direction - it should be sized */
167 int lines
= c
->lines
;
169 if ((c
->pos_flags
& WPOS_KEEP_LEFT
) && (c
->pos_flags
& WPOS_KEEP_RIGHT
))
174 else if (c
->pos_flags
& WPOS_KEEP_LEFT
)
176 else if (c
->pos_flags
& WPOS_KEEP_RIGHT
)
177 x
+= shift_x
+ scale_x
;
179 if ((c
->pos_flags
& WPOS_KEEP_TOP
) && (c
->pos_flags
& WPOS_KEEP_BOTTOM
))
184 else if (c
->pos_flags
& WPOS_KEEP_TOP
)
186 else if (c
->pos_flags
& WPOS_KEEP_BOTTOM
)
187 y
+= shift_y
+ scale_y
;
189 widget_set_size (c
, y
, x
, lines
, cols
);
193 while (h
->current
!= c
);
197 /* this function sets only size, leaving positioning to automatic methods */
199 dlg_set_size (Dlg_head
* h
, int lines
, int cols
)
204 if (h
->flags
& DLG_CENTER
)
206 y
= (LINES
- lines
) / 2;
207 x
= (COLS
- cols
) / 2;
210 if ((h
->flags
& DLG_TRYUP
) && (y
> 3))
213 dlg_set_position (h
, y
, x
, y
+ lines
, x
+ cols
);
216 /* Default dialog callback */
218 default_dlg_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
227 if (h
->color
!= NULL
)
229 common_dialog_repaint (h
);
232 return MSG_NOT_HANDLED
;
235 dlg_broadcast_msg_to (h
, WIDGET_IDLE
, 0, W_WANT_IDLE
);
239 /* this is default resizing mechanism */
240 /* the main idea of this code is to resize dialog
241 according to flags (if any of flags require automatic
242 resizing, like DLG_CENTER, end after that reposition
243 controls in dialog according to flags of widget) */
244 dlg_set_size (h
, h
->lines
, h
->cols
);
251 return MSG_NOT_HANDLED
;
255 create_dlg (int y1
, int x1
, int lines
, int cols
, const int *colors
,
256 dlg_cb_fn callback
, const char *help_ctx
, const char *title
, int flags
)
260 new_d
= g_new0 (Dlg_head
, 1);
263 new_d
->color
= g_new (int, DLG_COLOR_NUM
);
264 memmove (new_d
->color
, colors
, sizeof (int) * DLG_COLOR_NUM
);
266 new_d
->help_ctx
= help_ctx
;
267 new_d
->callback
= (callback
!= NULL
) ? callback
: default_dlg_callback
;
270 new_d
->flags
= flags
;
273 dlg_set_size (new_d
, lines
, cols
);
275 /* Strip existing spaces, add one space before and after the title */
280 t
= g_strstrip (g_strdup (title
));
282 new_d
->title
= g_strdup_printf (" %s ", t
);
290 dlg_set_default_colors (void)
292 dialog_colors
[0] = COLOR_NORMAL
;
293 dialog_colors
[1] = COLOR_FOCUS
;
294 dialog_colors
[2] = COLOR_HOT_NORMAL
;
295 dialog_colors
[3] = COLOR_HOT_FOCUS
;
297 alarm_colors
[0] = ERROR_COLOR
;
298 alarm_colors
[1] = REVERSE_COLOR
;
299 alarm_colors
[2] = ERROR_HOT_NORMAL
;
300 alarm_colors
[3] = ERROR_HOT_FOCUS
;
304 set_idle_proc (Dlg_head
* d
, int enable
)
307 d
->flags
|= DLG_WANT_IDLE
;
309 d
->flags
&= ~DLG_WANT_IDLE
;
313 * Insert widget to dialog before current widget. For dialogs populated
314 * from the bottom, make the widget current. Return widget number.
317 add_widget_autopos (Dlg_head
* h
, void *w
, widget_pos_flags_t pos_flags
)
319 Widget
*widget
= (Widget
*) w
;
321 /* Don't accept 0 widgets, and running dialogs */
322 if (!widget
|| h
->running
)
328 widget
->dlg_id
= h
->count
++;
329 widget
->pos_flags
= pos_flags
;
333 widget
->next
= h
->current
;
334 widget
->prev
= h
->current
->prev
;
335 h
->current
->prev
->next
= widget
;
336 h
->current
->prev
= widget
;
340 widget
->prev
= widget
;
341 widget
->next
= widget
;
344 if ((h
->flags
& DLG_REVERSE
) || !h
->current
)
347 return widget
->dlg_id
;
350 /* wrapper to simply add lefttop positioned controls */
352 add_widget (Dlg_head
* h
, void *w
)
354 return add_widget_autopos (h
, w
, WPOS_KEEP_LEFT
| WPOS_KEEP_TOP
);
358 do_complete_refresh (Dlg_head
* dlg
)
360 if (!dlg
->fullscreen
&& dlg
->parent
)
361 do_complete_refresh (dlg
->parent
);
373 dlg_redraw (current_dlg
);
375 do_complete_refresh (current_dlg
);
378 /* broadcast a message to all the widgets in a dialog that have
379 * the options set to flags. If flags is zero, the message is sent
383 dlg_broadcast_msg_to (Dlg_head
* h
, widget_msg_t message
, int reverse
, int flags
)
385 Widget
*p
, *first
, *wi
;
391 first
= p
= h
->current
->prev
;
393 first
= p
= h
->current
->next
;
402 if (flags
== 0 || (flags
& wi
->options
))
403 send_message (wi
, message
, 0);
408 /* broadcast a message to all the widgets in a dialog */
410 dlg_broadcast_msg (Dlg_head
* h
, widget_msg_t message
, int reverse
)
412 dlg_broadcast_msg_to (h
, message
, reverse
, 0);
416 dlg_focus (Dlg_head
* h
)
418 if ((h
->current
!= NULL
) && (send_message (h
->current
, WIDGET_FOCUS
, 0) == MSG_HANDLED
))
420 h
->callback (h
, h
->current
, DLG_FOCUS
, 0, NULL
);
427 dlg_unfocus (Dlg_head
* h
)
429 if ((h
->current
!= NULL
) && (send_message (h
->current
, WIDGET_UNFOCUS
, 0) == MSG_HANDLED
))
431 h
->callback (h
, h
->current
, DLG_UNFOCUS
, 0, NULL
);
438 /* Return true if the windows overlap */
440 dlg_overlap (Widget
* a
, Widget
* b
)
442 if ((b
->x
>= a
->x
+ a
->cols
)
443 || (a
->x
>= b
->x
+ b
->cols
) || (b
->y
>= a
->y
+ a
->lines
) || (a
->y
>= b
->y
+ b
->lines
))
449 /* Find the widget with the given callback in the dialog h */
451 find_widget_type (const Dlg_head
* h
, callback_fn callback
)
455 if ((h
!= NULL
) && (h
->current
!= NULL
))
460 for (i
= 0, item
= h
->current
; i
< h
->count
; i
++, item
= item
->next
)
462 if (item
->callback
== callback
)
473 /* Find the widget with the given dialog id in the dialog h and select it */
475 dlg_select_by_id (const Dlg_head
* h
, int id
)
494 while (w
!= h
->current
);
497 dlg_select_widget (w_found
);
501 /* What to do if the requested widget doesn't take focus */
504 SELECT_NEXT
, /* go the the next widget */
505 SELECT_PREV
, /* go the the previous widget */
506 SELECT_EXACT
/* use current widget */
510 * Try to select another widget. If forward is set, follow tab order.
511 * Otherwise go to the previous widget.
514 do_select_widget (Dlg_head
* h
, Widget
* w
, select_dir_t dir
)
516 Widget
*w0
= h
->current
;
518 if (!dlg_unfocus (h
))
530 h
->current
= h
->current
->next
;
533 h
->current
= h
->current
->prev
;
541 while (h
->current
!= w
);
543 if (dlg_overlap (w0
, h
->current
))
545 send_message (h
->current
, WIDGET_DRAW
, 0);
546 send_message (h
->current
, WIDGET_FOCUS
, 0);
552 * Try to select widget in the dialog.
555 dlg_select_widget (void *w
)
557 do_select_widget (((Widget
*) w
)->parent
, w
, SELECT_NEXT
);
561 /* Try to select previous widget in the tab order */
563 dlg_one_up (Dlg_head
* h
)
566 do_select_widget (h
, h
->current
->prev
, SELECT_PREV
);
570 /* Try to select next widget in the tab order */
572 dlg_one_down (Dlg_head
* h
)
575 do_select_widget (h
, h
->current
->next
, SELECT_NEXT
);
580 update_cursor (Dlg_head
* h
)
582 Widget
*p
= h
->current
;
586 if (p
->options
& W_WANT_CURSOR
)
587 send_message (p
, WIDGET_CURSOR
, 0);
589 while ((p
= p
->next
) != h
->current
)
590 if (p
->options
& W_WANT_CURSOR
)
591 if (send_message (p
, WIDGET_CURSOR
, 0) == MSG_HANDLED
)
596 /* Redraw the widgets in reverse order, leaving the current widget
600 dlg_redraw (Dlg_head
* h
)
602 h
->callback (h
, NULL
, DLG_DRAW
, 0, NULL
);
603 dlg_broadcast_msg (h
, WIDGET_DRAW
, 1);
608 dlg_stop (Dlg_head
* h
)
620 /* Use this if the refreshes fail */
623 #endif /* HAVE_SLANG */
627 dlg_execute_cmd (Dlg_head
* h
, unsigned long command
)
629 cb_ret_t ret
= MSG_HANDLED
;
633 h
->ret_value
= B_ENTER
;
636 case CK_DialogCancel
:
637 h
->ret_value
= B_CANCEL
;
640 case CK_DialogPrevItem
:
643 case CK_DialogNextItem
:
647 interactive_display (NULL
, h
->help_ctx
);
650 case CK_DialogSuspend
:
654 case CK_DialogRefresh
:
658 ret
= MSG_NOT_HANDLED
;
665 dlg_handle_key (Dlg_head
* h
, int d_key
)
667 unsigned long command
;
668 command
= lookup_keymap_command (dialog_map
, d_key
);
669 if ((command
== CK_Ignore_Key
) || (dlg_execute_cmd (h
, command
) == MSG_NOT_HANDLED
))
670 return MSG_NOT_HANDLED
;
676 dlg_try_hotkey (Dlg_head
* h
, int d_key
)
682 if (h
->current
== NULL
)
683 return MSG_NOT_HANDLED
;
686 * Explanation: we don't send letter hotkeys to other widgets if
687 * the currently selected widget is an input line
690 if (h
->current
->options
& W_IS_INPUT
)
692 /* skip ascii control characters, anything else can valid character in
694 if (d_key
>= 32 && d_key
< 256)
695 return MSG_NOT_HANDLED
;
698 /* If it's an alt key, send the message */
699 c
= d_key
& ~ALT (0);
700 if (d_key
& ALT (0) && g_ascii_isalpha (c
))
701 d_key
= g_ascii_tolower (c
);
703 handled
= MSG_NOT_HANDLED
;
704 if (h
->current
->options
& W_WANT_HOTKEY
)
705 handled
= send_message (h
->current
, WIDGET_HOTKEY
, d_key
);
707 /* If not used, send hotkey to other widgets */
708 if (handled
== MSG_HANDLED
)
711 hot_cur
= h
->current
->next
;
713 /* send it to all widgets */
714 while (h
->current
!= hot_cur
&& handled
== MSG_NOT_HANDLED
)
716 if (hot_cur
->options
& W_WANT_HOTKEY
)
717 handled
= send_message (hot_cur
, WIDGET_HOTKEY
, d_key
);
719 if (handled
== MSG_NOT_HANDLED
)
720 hot_cur
= hot_cur
->next
;
723 if (handled
== MSG_HANDLED
)
724 do_select_widget (h
, hot_cur
, SELECT_EXACT
);
730 dlg_key_event (Dlg_head
* h
, int d_key
)
734 if (h
->current
== NULL
)
737 /* TAB used to cycle */
738 if (!(h
->flags
& DLG_WANT_TAB
))
745 else if (d_key
== KEY_BTAB
)
752 /* first can dlg_callback handle the key */
753 handled
= h
->callback (h
, NULL
, DLG_KEY
, d_key
, NULL
);
755 /* next try the hotkey */
756 if (handled
== MSG_NOT_HANDLED
)
757 handled
= dlg_try_hotkey (h
, d_key
);
759 if (handled
== MSG_HANDLED
)
760 h
->callback (h
, NULL
, DLG_HOTKEY_HANDLED
, 0, NULL
);
762 /* not used - then try widget_callback */
763 handled
= send_message (h
->current
, WIDGET_KEY
, d_key
);
765 /* not used- try to use the unhandled case */
766 if (handled
== MSG_NOT_HANDLED
)
767 handled
= h
->callback (h
, NULL
, DLG_UNHANDLED_KEY
, d_key
, NULL
);
769 if (handled
== MSG_NOT_HANDLED
)
770 handled
= dlg_handle_key (h
, d_key
);
772 h
->callback (h
, NULL
, DLG_POST_KEY
, d_key
, NULL
);
776 dlg_mouse_event (Dlg_head
* h
, Gpm_Event
* event
)
779 Widget
*starting_widget
= h
->current
;
784 /* close the dialog by mouse click out of dialog area */
785 if (mouse_close_dialog
&& !h
->fullscreen
&& ((event
->buttons
& GPM_B_LEFT
) != 0) && ((event
->type
& GPM_DOWN
) != 0) /* left click */
786 && !((x
> h
->x
) && (x
<= h
->x
+ h
->cols
) && (y
> h
->y
) && (y
<= h
->y
+ h
->lines
)))
788 h
->ret_value
= B_CANCEL
;
793 item
= starting_widget
;
801 if ((x
> widget
->x
) && (x
<= widget
->x
+ widget
->cols
)
802 && (y
> widget
->y
) && (y
<= widget
->y
+ widget
->lines
))
805 new_event
.x
-= widget
->x
;
806 new_event
.y
-= widget
->y
;
808 if (widget
->mouse
!= NULL
)
809 return widget
->mouse (&new_event
, widget
);
812 while (item
!= starting_widget
);
817 /* Run dialog routines */
819 /* Init the process */
821 init_dlg (Dlg_head
* h
)
823 /* Initialize dialog manager and widgets */
824 h
->callback (h
, NULL
, DLG_INIT
, 0, NULL
);
825 dlg_broadcast_msg (h
, WIDGET_INIT
, 0);
827 if (h
->x
== 0 && h
->y
== 0 && h
->cols
== COLS
&& h
->lines
== LINES
)
830 h
->parent
= current_dlg
;
833 /* Initialize the mouse status */
834 h
->mouse_status
= MOU_NORMAL
;
836 /* Select the first widget that takes focus */
837 while (!dlg_focus (h
) && h
->current
)
838 h
->current
= h
->current
->next
;
840 /* Redraw the screen */
847 /* Shutdown the run_dlg */
849 dlg_run_done (Dlg_head
* h
)
851 if (h
->current
!= NULL
)
852 h
->callback (h
, h
->current
, DLG_END
, 0, NULL
);
854 current_dlg
= h
->parent
;
858 dlg_process_event (Dlg_head
* h
, int key
, Gpm_Event
* event
)
862 if (tty_got_interrupt ())
863 dlg_execute_cmd (h
, CK_DialogCancel
);
869 h
->mouse_status
= dlg_mouse_event (h
, event
);
871 dlg_key_event (h
, key
);
875 frontend_run_dlg (Dlg_head
* h
)
884 change_screen_size ();
889 execute_hooks (idle_hook
);
891 while ((h
->flags
& DLG_WANT_IDLE
) && is_idle ())
892 h
->callback (h
, NULL
, DLG_IDLE
, 0, NULL
);
894 /* Allow terminating the dialog from the idle handler */
901 /* Clear interrupt flag */
902 tty_got_interrupt ();
903 d_key
= tty_get_event (&event
, h
->mouse_status
== MOU_REPEAT
, TRUE
);
905 dlg_process_event (h
, d_key
, &event
);
908 h
->callback (h
, NULL
, DLG_VALIDATE
, 0, NULL
);
912 /* Standard run dialog routine
913 * We have to keep this routine small so that we can duplicate it's
914 * behavior on complex routines like the file routines, this way,
915 * they can call the dlg_process_event without rewriting all the code
918 run_dlg (Dlg_head
* h
)
921 frontend_run_dlg (h
);
927 destroy_dlg (Dlg_head
* h
)
932 dlg_broadcast_msg (h
, WIDGET_DESTROY
, 0);
934 for (i
= 0; i
< h
->count
; i
++)
948 widget_set_size (Widget
* widget
, int y
, int x
, int lines
, int cols
)
953 widget
->lines
= lines
;
954 send_message (widget
, WIDGET_RESIZED
, 0 /* unused */ );
957 /* Replace widget old_w for widget new_w in the dialog */
959 dlg_replace_widget (Widget
* old_w
, Widget
* new_w
)
961 Dlg_head
*h
= old_w
->parent
;
962 int should_focus
= 0;
967 if (old_w
== h
->current
)
971 new_w
->dlg_id
= old_w
->dlg_id
;
973 if (old_w
== old_w
->next
)
975 /* just one widget */
981 new_w
->prev
= old_w
->prev
;
982 new_w
->next
= old_w
->next
;
983 old_w
->prev
->next
= new_w
;
984 old_w
->next
->prev
= new_w
;
990 send_message (old_w
, WIDGET_DESTROY
, 0);
991 send_message (new_w
, WIDGET_INIT
, 0);
994 dlg_select_widget (new_w
);
996 send_message (new_w
, WIDGET_DRAW
, 0);