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.
30 #include "help.h" /* interactive_display() */
31 #include "key.h" /* mi_getch() */
33 #include "layout.h" /* winch_flag */
34 #include "execute.h" /* suspend_cmd() */
35 #include "main.h" /* slow_terminal */
37 #define waddc(w,y1,x1,c) move (w->y+y1, w->x+x1); addch (c)
39 /* Primitive way to check if the the current dialog is our dialog */
40 /* This is needed by async routines like load_prompt */
41 Dlg_head
*current_dlg
= 0;
43 /* A hook list for idle events */
46 static void dlg_broadcast_msg_to (Dlg_head
* h
, widget_msg_t message
,
47 int reverse
, int flags
);
49 static void slow_box (Dlg_head
*h
, int y
, int x
, int ys
, int xs
)
51 move (h
->y
+y
, h
->x
+x
);
54 move (h
->y
+y
, h
->x
+x
+xs
-1);
56 move (h
->y
+y
+ys
-1, h
->x
+x
);
60 /* draw box in window */
61 void draw_box (Dlg_head
*h
, int y
, int x
, int ys
, int xs
)
64 slow_box (h
, y
, x
, ys
, xs
);
69 waddc (h
, y
, x
, ACS_ULCORNER
);
70 hline (ACS_HLINE
, xs
- 2);
71 waddc (h
, y
+ ys
- 1, x
, ACS_LLCORNER
);
72 hline (ACS_HLINE
, xs
- 2);
74 waddc (h
, y
, x
+ xs
- 1, ACS_URCORNER
);
75 waddc (h
, y
+ ys
- 1, x
+ xs
- 1, ACS_LRCORNER
);
77 move (h
->y
+y
+1, h
->x
+x
);
78 vline (ACS_VLINE
, ys
- 2);
79 move (h
->y
+y
+1, h
->x
+x
+xs
-1);
80 vline (ACS_VLINE
, ys
- 2);
82 SLsmg_draw_box (h
->y
+y
, h
->x
+x
, ys
, xs
);
83 #endif /* HAVE_SLANG */
86 /* draw box in window */
87 void draw_double_box (Dlg_head
*h
, int y
, int x
, int ys
, int xs
)
90 draw_box (h
, y
, x
, ys
, xs
);
92 SLsmg_draw_double_box (h
->y
+y
, h
->x
+x
, ys
, xs
);
93 #endif /* HAVE_SLANG */
96 void widget_erase (Widget
*w
)
100 for (y
= 0; y
< w
->lines
; y
++){
101 widget_move (w
, y
, 0);
102 for (x
= 0; x
< w
->cols
; x
++)
107 void dlg_erase (Dlg_head
*h
)
111 for (y
= 0; y
< h
->lines
; y
++){
112 move (y
+h
->y
, h
->x
); /* FIXME: should test if ERR */
113 for (x
= 0; x
< h
->cols
; x
++){
120 init_widget (Widget
*w
, int y
, int x
, int lines
, int cols
,
121 callback_fn callback
, mouse_h mouse_handler
)
127 w
->callback
= callback
;
128 w
->mouse
= mouse_handler
;
131 /* Almost all widgets want to put the cursor in a suitable place */
132 w
->options
= W_WANT_CURSOR
;
135 /* Default callback for widgets */
137 default_proc (widget_msg_t msg
, int parm
)
152 return MSG_NOT_HANDLED
;
156 /* Clean the dialog area, draw the frame and the title */
158 common_dialog_repaint (struct Dlg_head
*h
)
162 space
= (h
->flags
& DLG_COMPACT
) ? 0 : 1;
164 attrset (DLG_NORMALC (h
));
166 draw_box (h
, space
, space
, h
->lines
- 2 * space
, h
->cols
- 2 * space
);
169 attrset (DLG_HOT_NORMALC (h
));
170 dlg_move (h
, space
, (h
->cols
- strlen (h
->title
)) / 2);
175 /* Default dialog callback */
176 cb_ret_t
default_dlg_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
180 if (msg
== DLG_DRAW
&& h
->color
) {
181 common_dialog_repaint (h
);
184 if (msg
== DLG_IDLE
){
185 dlg_broadcast_msg_to (h
, WIDGET_IDLE
, 0, W_WANT_IDLE
);
188 return MSG_NOT_HANDLED
;
192 create_dlg (int y1
, int x1
, int lines
, int cols
, const int *color_set
,
193 dlg_cb_fn callback
, const char *help_ctx
, const char *title
,
198 if (flags
& DLG_CENTER
) {
199 y1
= (LINES
- lines
) / 2;
200 x1
= (COLS
- cols
) / 2;
203 if ((flags
& DLG_TRYUP
) && (y1
> 3))
206 new_d
= g_new0 (Dlg_head
, 1);
207 new_d
->color
= color_set
;
208 new_d
->help_ctx
= help_ctx
;
209 new_d
->callback
= callback
? callback
: default_dlg_callback
;
213 new_d
->lines
= lines
;
214 new_d
->flags
= flags
;
216 /* Strip existing spaces, add one space before and after the title */
219 t
= g_strstrip (g_strdup (title
));
220 new_d
->title
= g_strconcat (" ", t
, " ", (char *) NULL
);
228 set_idle_proc (Dlg_head
*d
, int enable
)
231 d
->flags
|= DLG_WANT_IDLE
;
233 d
->flags
&= ~DLG_WANT_IDLE
;
237 * Insert widget to dialog before current widget. For dialogs populated
238 * from the bottom, make the widget current. Return widget number.
241 add_widget (Dlg_head
*h
, void *w
)
243 Widget
*widget
= (Widget
*) w
;
245 /* Don't accept 0 widgets, and running dialogs */
246 if (!widget
|| h
->running
)
252 widget
->dlg_id
= h
->count
++;
255 widget
->next
= h
->current
;
256 widget
->prev
= h
->current
->prev
;
257 h
->current
->prev
->next
= widget
;
258 h
->current
->prev
= widget
;
260 widget
->prev
= widget
;
261 widget
->next
= widget
;
264 if ((h
->flags
& DLG_REVERSE
) || !h
->current
)
267 return widget
->dlg_id
;
271 REFRESH_COVERS_PART
, /* If the refresh fn convers only a part */
272 REFRESH_COVERS_ALL
/* If the refresh fn convers all the screen */
276 do_complete_refresh (Dlg_head
*dlg
)
278 if (!dlg
->fullscreen
&& dlg
->parent
)
279 do_complete_refresh (dlg
->parent
);
291 dlg_redraw (current_dlg
);
293 do_complete_refresh (current_dlg
);
297 /* broadcast a message to all the widgets in a dialog that have
298 * the options set to flags. If flags is zero, the message is sent
302 dlg_broadcast_msg_to (Dlg_head
*h
, widget_msg_t message
, int reverse
,
305 Widget
*p
, *first
, *wi
;
311 first
= p
= h
->current
->prev
;
313 first
= p
= h
->current
->next
;
321 if (flags
== 0 || (flags
& wi
->options
))
322 send_message (wi
, message
, 0);
323 } while (first
!= p
);
326 /* broadcast a message to all the widgets in a dialog */
328 dlg_broadcast_msg (Dlg_head
*h
, widget_msg_t message
, int reverse
)
330 dlg_broadcast_msg_to (h
, message
, reverse
, 0);
333 int dlg_focus (Dlg_head
*h
)
338 if (send_message (h
->current
, WIDGET_FOCUS
, 0)){
339 (*h
->callback
) (h
, DLG_FOCUS
, 0);
346 dlg_unfocus (Dlg_head
*h
)
351 if (send_message (h
->current
, WIDGET_UNFOCUS
, 0)){
352 (*h
->callback
) (h
, DLG_UNFOCUS
, 0);
359 /* Return true if the windows overlap */
360 int dlg_overlap (Widget
*a
, Widget
*b
)
362 if ((b
->x
>= a
->x
+ a
->cols
)
363 || (a
->x
>= b
->x
+ b
->cols
)
364 || (b
->y
>= a
->y
+ a
->lines
)
365 || (a
->y
>= b
->y
+ b
->lines
))
371 /* Find the widget with the given callback in the dialog h */
373 find_widget_type (Dlg_head
*h
, callback_fn callback
)
385 for (i
= 0, item
= h
->current
; i
< h
->count
; i
++, item
= item
->next
) {
386 if (item
->callback
== callback
) {
394 /* Find the widget with the given dialog id in the dialog h and select it */
396 dlg_select_by_id (Dlg_head
*h
, int id
)
407 if (w
->dlg_id
== id
) {
412 } while (w
!= h
->current
);
415 dlg_select_widget(w_found
);
419 /* What to do if the requested widget doesn't take focus */
421 SELECT_NEXT
, /* go the the next widget */
422 SELECT_PREV
, /* go the the previous widget */
423 SELECT_EXACT
/* use current widget */
427 * Try to select another widget. If forward is set, follow tab order.
428 * Otherwise go to the previous widget.
431 do_select_widget (Dlg_head
*h
, Widget
*w
, select_dir_t dir
)
433 Widget
*w0
= h
->current
;
435 if (!dlg_unfocus (h
))
445 h
->current
= h
->current
->next
;
448 h
->current
= h
->current
->prev
;
455 } while (h
->current
!= w
);
457 if (dlg_overlap (w0
, h
->current
)) {
458 send_message (h
->current
, WIDGET_DRAW
, 0);
459 send_message (h
->current
, WIDGET_FOCUS
, 0);
465 * Try to select widget in the dialog.
468 dlg_select_widget (void *w
)
470 do_select_widget (((Widget
*) w
)->parent
, w
, SELECT_NEXT
);
474 /* Try to select previous widget in the tab order */
476 dlg_one_up (Dlg_head
*h
)
479 do_select_widget (h
, h
->current
->prev
, SELECT_PREV
);
483 /* Try to select next widget in the tab order */
485 dlg_one_down (Dlg_head
*h
)
488 do_select_widget (h
, h
->current
->next
, SELECT_NEXT
);
492 void update_cursor (Dlg_head
*h
)
496 if (h
->current
->options
& W_WANT_CURSOR
)
497 send_message (h
->current
, WIDGET_CURSOR
, 0);
499 Widget
*p
= h
->current
;
502 if (p
->options
& W_WANT_CURSOR
)
503 if ((*p
->callback
)(p
, WIDGET_CURSOR
, 0)){
507 } while (h
->current
!= p
);
511 /* Redraw the widgets in reverse order, leaving the current widget
514 void dlg_redraw (Dlg_head
*h
)
516 (h
->callback
)(h
, DLG_DRAW
, 0);
518 dlg_broadcast_msg (h
, WIDGET_DRAW
, 1);
523 void dlg_stop (Dlg_head
*h
)
528 static inline void dialog_handle_key (Dlg_head
*h
, int d_key
)
542 interactive_display (NULL
, h
->help_ctx
);
552 /* Use this if the refreshes fail */
557 #endif /* HAVE_SLANG */
564 h
->ret_value
= B_ENTER
;
572 h
->ret_value
= B_CANCEL
;
579 dlg_try_hotkey (Dlg_head
*h
, int d_key
)
588 * Explanation: we don't send letter hotkeys to other widgets if
589 * the currently selected widget is an input line
592 if (h
->current
->options
& W_IS_INPUT
) {
593 if (d_key
< 255 && isalpha (d_key
))
597 /* If it's an alt key, send the message */
598 c
= d_key
& ~ALT (0);
599 if (d_key
& ALT (0) && c
< 255 && isalpha (c
))
603 if (h
->current
->options
& W_WANT_HOTKEY
)
604 handled
= h
->current
->callback (h
->current
, WIDGET_HOTKEY
, d_key
);
606 /* If not used, send hotkey to other widgets */
610 hot_cur
= h
->current
;
612 /* send it to all widgets */
614 if (hot_cur
->options
& W_WANT_HOTKEY
)
616 (*hot_cur
->callback
) (hot_cur
, WIDGET_HOTKEY
, d_key
);
619 hot_cur
= hot_cur
->next
;
620 } while (h
->current
!= hot_cur
&& !handled
);
625 do_select_widget (h
, hot_cur
, SELECT_EXACT
);
630 dlg_key_event (Dlg_head
*h
, int d_key
)
637 /* TAB used to cycle */
638 if (!(h
->flags
& DLG_WANT_TAB
)) {
642 } else if (d_key
== KEY_BTAB
) {
648 /* first can dlg_callback handle the key */
649 handled
= (*h
->callback
) (h
, DLG_KEY
, d_key
);
651 /* next try the hotkey */
653 handled
= dlg_try_hotkey (h
, d_key
);
656 (*h
->callback
) (h
, DLG_HOTKEY_HANDLED
, 0);
658 /* not used - then try widget_callback */
660 handled
= h
->current
->callback (h
->current
, WIDGET_KEY
, d_key
);
662 /* not used- try to use the unhandled case */
664 handled
= (*h
->callback
) (h
, DLG_UNHANDLED_KEY
, d_key
);
667 dialog_handle_key (h
, d_key
);
669 (*h
->callback
) (h
, DLG_POST_KEY
, d_key
);
673 dlg_mouse_event (Dlg_head
* h
, Gpm_Event
* event
)
676 Widget
*starting_widget
= h
->current
;
681 item
= starting_widget
;
683 Widget
*widget
= item
;
687 if (!((x
> widget
->x
) && (x
<= widget
->x
+ widget
->cols
)
688 && (y
> widget
->y
) && (y
<= widget
->y
+ widget
->lines
)))
692 new_event
.x
-= widget
->x
;
693 new_event
.y
-= widget
->y
;
698 return (*widget
->mouse
) (&new_event
, widget
);
699 } while (item
!= starting_widget
);
704 /* Run dialog routines */
706 /* Init the process */
707 void init_dlg (Dlg_head
*h
)
709 /* Initialize dialog manager and widgets */
710 (*h
->callback
) (h
, DLG_INIT
, 0);
711 dlg_broadcast_msg (h
, WIDGET_INIT
, 0);
713 if (h
->x
== 0 && h
->y
== 0 && h
->cols
== COLS
&& h
->lines
== LINES
)
716 h
->parent
= current_dlg
;
719 /* Initialize the mouse status */
720 h
->mouse_status
= MOU_NORMAL
;
722 /* Select the first widget that takes focus */
723 while (!dlg_focus (h
) && h
->current
)
724 h
->current
= h
->current
->next
;
726 /* Redraw the screen */
733 /* Shutdown the run_dlg */
734 void dlg_run_done (Dlg_head
*h
)
737 (*h
->callback
) (h
, DLG_END
, 0);
739 current_dlg
= h
->parent
;
742 void dlg_process_event (Dlg_head
*h
, int key
, Gpm_Event
*event
)
745 if (got_interrupt ())
752 h
->mouse_status
= dlg_mouse_event (h
, event
);
754 dlg_key_event (h
, key
);
758 frontend_run_dlg (Dlg_head
*h
)
766 change_screen_size ();
770 execute_hooks (idle_hook
);
772 while ((h
->flags
& DLG_WANT_IDLE
) && is_idle ())
773 (*h
->callback
) (h
, DLG_IDLE
, 0);
775 /* Allow terminating the dialog from the idle handler */
782 /* Clear interrupt flag */
784 d_key
= get_event (&event
, h
->mouse_status
== MOU_REPEAT
, 1);
786 dlg_process_event (h
, d_key
, &event
);
789 (*h
->callback
) (h
, DLG_VALIDATE
, 0);
793 /* Standard run dialog routine
794 * We have to keep this routine small so that we can duplicate it's
795 * behavior on complex routines like the file routines, this way,
796 * they can call the dlg_process_event without rewriting all the code
798 int run_dlg (Dlg_head
*h
)
801 frontend_run_dlg (h
);
807 destroy_dlg (Dlg_head
*h
)
812 dlg_broadcast_msg (h
, WIDGET_DESTROY
, 0);
814 for (i
= 0; i
< h
->count
; i
++) {
825 void widget_set_size (Widget
*widget
, int y
, int x
, int lines
, int cols
)
830 widget
->lines
= lines
;
831 send_message (widget
, WIDGET_RESIZED
, 0 /* unused */);
834 /* Replace widget old_w for widget new_w in the dialog */
836 dlg_replace_widget (Widget
*old_w
, Widget
*new_w
)
838 Dlg_head
*h
= old_w
->parent
;
839 int should_focus
= 0;
844 if (old_w
== h
->current
)
848 new_w
->dlg_id
= old_w
->dlg_id
;
850 if (old_w
== old_w
->next
) {
851 /* just one widget */
855 new_w
->prev
= old_w
->prev
;
856 new_w
->next
= old_w
->next
;
857 old_w
->prev
->next
= new_w
;
858 old_w
->next
->prev
= new_w
;
864 send_message (old_w
, WIDGET_DESTROY
, 0);
865 send_message (new_w
, WIDGET_INIT
, 0);
868 dlg_select_widget (new_w
);
870 send_message (new_w
, WIDGET_DRAW
, 0);