1 /* Dialog box features module for the Midnight Commander
2 Copyright (C) 1994, 1995 Radek Doulik, Miguel de Icaza
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include "help.h" /* interactive_display() */
28 #include "key.h" /* mi_getch() */
30 #include "layout.h" /* winch_flag */
31 #include "execute.h" /* suspend_cmd() */
32 #include "main.h" /* slow_terminal */
34 #define waddc(w,y1,x1,c) move (w->y+y1, w->x+x1); addch (c)
36 /* Primitive way to check if the the current dialog is our dialog */
37 /* This is needed by async routines like load_prompt */
38 Dlg_head
*current_dlg
= 0;
40 /* A hook list for idle events */
43 static void dlg_broadcast_msg_to (Dlg_head
* h
, widget_msg_t message
,
44 int reverse
, int flags
);
46 static void slow_box (Dlg_head
*h
, int y
, int x
, int ys
, int xs
)
48 move (h
->y
+y
, h
->x
+x
);
51 move (h
->y
+y
, h
->x
+x
+xs
-1);
53 move (h
->y
+y
+ys
-1, h
->x
+x
);
57 /* draw box in window */
58 void draw_box (Dlg_head
*h
, int y
, int x
, int ys
, int xs
)
61 slow_box (h
, y
, x
, ys
, xs
);
66 waddc (h
, y
, x
, ACS_ULCORNER
);
67 hline (ACS_HLINE
, xs
- 2);
68 waddc (h
, y
+ ys
- 1, x
, ACS_LLCORNER
);
69 hline (ACS_HLINE
, xs
- 2);
71 waddc (h
, y
, x
+ xs
- 1, ACS_URCORNER
);
72 waddc (h
, y
+ ys
- 1, x
+ xs
- 1, ACS_LRCORNER
);
74 move (h
->y
+y
+1, h
->x
+x
);
75 vline (ACS_VLINE
, ys
- 2);
76 move (h
->y
+y
+1, h
->x
+x
+xs
-1);
77 vline (ACS_VLINE
, ys
- 2);
79 SLsmg_draw_box (h
->y
+y
, h
->x
+x
, ys
, xs
);
80 #endif /* HAVE_SLANG */
83 /* draw box in window */
84 void draw_double_box (Dlg_head
*h
, int y
, int x
, int ys
, int xs
)
87 draw_box (h
, y
, x
, ys
, xs
);
89 SLsmg_draw_double_box (h
->y
+y
, h
->x
+x
, ys
, xs
);
90 #endif /* HAVE_SLANG */
93 void widget_erase (Widget
*w
)
97 for (y
= 0; y
< w
->lines
; y
++){
98 widget_move (w
, y
, 0);
99 for (x
= 0; x
< w
->cols
; x
++)
104 void dlg_erase (Dlg_head
*h
)
108 for (y
= 0; y
< h
->lines
; y
++){
109 move (y
+h
->y
, h
->x
); /* FIXME: should test if ERR */
110 for (x
= 0; x
< h
->cols
; x
++){
117 init_widget (Widget
*w
, int y
, int x
, int lines
, int cols
,
118 callback_fn callback
, mouse_h mouse_handler
)
124 w
->callback
= callback
;
125 w
->mouse
= mouse_handler
;
128 /* Almost all widgets want to put the cursor in a suitable place */
129 w
->options
= W_WANT_CURSOR
;
132 /* Default callback for widgets */
134 default_proc (widget_msg_t msg
, int parm
)
147 return MSG_NOT_HANDLED
;
151 /* Clean the dialog area, draw the frame and the title */
153 common_dialog_repaint (struct Dlg_head
*h
)
157 space
= (h
->flags
& DLG_COMPACT
) ? 0 : 1;
161 draw_box (h
, space
, space
, h
->lines
- 2 * space
, h
->cols
- 2 * space
);
164 attrset (HOT_NORMALC
);
165 dlg_move (h
, space
, (h
->cols
- strlen (h
->title
)) / 2);
170 /* Default dialog callback */
171 cb_ret_t
default_dlg_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
173 if (msg
== DLG_DRAW
&& h
->color
) {
174 common_dialog_repaint (h
);
177 if (msg
== DLG_IDLE
){
178 dlg_broadcast_msg_to (h
, WIDGET_IDLE
, 0, W_WANT_IDLE
);
181 return MSG_NOT_HANDLED
;
185 create_dlg (int y1
, int x1
, int lines
, int cols
, const int *color_set
,
186 dlg_cb_fn callback
, const char *help_ctx
, const char *title
,
191 if (flags
& DLG_CENTER
) {
192 y1
= (LINES
- lines
) / 2;
193 x1
= (COLS
- cols
) / 2;
196 if ((flags
& DLG_TRYUP
) && (y1
> 3))
199 new_d
= g_new0 (Dlg_head
, 1);
200 new_d
->color
= color_set
;
201 new_d
->help_ctx
= const_cast(char *, help_ctx
);
202 new_d
->callback
= callback
? callback
: default_dlg_callback
;
206 new_d
->lines
= lines
;
207 new_d
->flags
= flags
;
209 /* Strip existing spaces, add one space before and after the title */
212 t
= g_strstrip (g_strdup (title
));
213 new_d
->title
= g_strconcat (" ", t
, " ", (char *) NULL
);
221 set_idle_proc (Dlg_head
*d
, int enable
)
224 d
->flags
|= DLG_WANT_IDLE
;
226 d
->flags
&= ~DLG_WANT_IDLE
;
230 * Insert widget to dialog before current widget. For dialogs populated
231 * from the bottom, make the widget current. Return widget number.
234 add_widget (Dlg_head
*h
, void *w
)
236 Widget
*widget
= (Widget
*) w
;
238 /* Don't accept 0 widgets, and running dialogs */
239 if (!widget
|| h
->running
)
245 widget
->dlg_id
= h
->count
++;
248 widget
->next
= h
->current
;
249 widget
->prev
= h
->current
->prev
;
250 h
->current
->prev
->next
= widget
;
251 h
->current
->prev
= widget
;
253 widget
->prev
= widget
;
254 widget
->next
= widget
;
257 if ((h
->flags
& DLG_REVERSE
) || !h
->current
)
260 return widget
->dlg_id
;
264 REFRESH_COVERS_PART
, /* If the refresh fn convers only a part */
265 REFRESH_COVERS_ALL
/* If the refresh fn convers all the screen */
269 do_complete_refresh (Dlg_head
*dlg
)
271 if (!dlg
->fullscreen
&& dlg
->parent
)
272 do_complete_refresh (dlg
->parent
);
284 dlg_redraw (current_dlg
);
286 do_complete_refresh (current_dlg
);
290 /* broadcast a message to all the widgets in a dialog that have
291 * the options set to flags.
294 dlg_broadcast_msg_to (Dlg_head
*h
, widget_msg_t message
, int reverse
,
297 Widget
*p
, *first
, *wi
;
303 first
= p
= h
->current
->prev
;
305 first
= p
= h
->current
->next
;
313 send_message (wi
, message
, 0);
314 } while (first
!= p
);
317 /* broadcast a message to all the widgets in a dialog */
319 dlg_broadcast_msg (Dlg_head
*h
, widget_msg_t message
, int reverse
)
321 dlg_broadcast_msg_to (h
, message
, reverse
, ~0);
324 int dlg_focus (Dlg_head
*h
)
329 if (send_message (h
->current
, WIDGET_FOCUS
, 0)){
330 (*h
->callback
) (h
, DLG_FOCUS
, 0);
337 dlg_unfocus (Dlg_head
*h
)
342 if (send_message (h
->current
, WIDGET_UNFOCUS
, 0)){
343 (*h
->callback
) (h
, DLG_UNFOCUS
, 0);
350 /* Return true if the windows overlap */
351 int dlg_overlap (Widget
*a
, Widget
*b
)
353 if ((b
->x
>= a
->x
+ a
->cols
)
354 || (a
->x
>= b
->x
+ b
->cols
)
355 || (b
->y
>= a
->y
+ a
->lines
)
356 || (a
->y
>= b
->y
+ b
->lines
))
362 /* Find the widget with the given callback in the dialog h */
364 find_widget_type (Dlg_head
*h
, callback_fn callback
)
376 for (i
= 0, item
= h
->current
; i
< h
->count
; i
++, item
= item
->next
) {
377 if (item
->callback
== callback
) {
385 /* Find the widget with the given dialog id in the dialog h and select it */
387 dlg_select_by_id (Dlg_head
*h
, int id
)
398 if (w
->dlg_id
== id
) {
403 } while (w
!= h
->current
);
406 dlg_select_widget(h
, w_found
);
410 /* What to do if the requested widget doesn't take focus */
412 SELECT_NEXT
, /* go the the next widget */
413 SELECT_PREV
, /* go the the previous widget */
414 SELECT_EXACT
/* use current widget */
418 * Try to select another widget. If forward is set, follow tab order.
419 * Otherwise go to the previous widget.
422 do_select_widget (Dlg_head
*h
, Widget
*w
, select_dir_t dir
)
424 Widget
*w0
= h
->current
;
426 if (!dlg_unfocus (h
))
436 h
->current
= h
->current
->next
;
439 h
->current
= h
->current
->prev
;
446 } while (h
->current
!= w
);
448 if (dlg_overlap (w0
, h
->current
)) {
449 send_message (h
->current
, WIDGET_DRAW
, 0);
450 send_message (h
->current
, WIDGET_FOCUS
, 0);
456 * Try to select widget in the dialog.
459 dlg_select_widget (Dlg_head
*h
, void *w
)
461 do_select_widget (h
, w
, SELECT_NEXT
);
465 /* Try to select previous widget in the tab order */
467 dlg_one_up (Dlg_head
*h
)
470 do_select_widget (h
, h
->current
->prev
, SELECT_PREV
);
474 /* Try to select next widget in the tab order */
476 dlg_one_down (Dlg_head
*h
)
479 do_select_widget (h
, h
->current
->next
, SELECT_NEXT
);
483 void update_cursor (Dlg_head
*h
)
487 if (h
->current
->options
& W_WANT_CURSOR
)
488 send_message (h
->current
, WIDGET_CURSOR
, 0);
490 Widget
*p
= h
->current
;
493 if (p
->options
& W_WANT_CURSOR
)
494 if ((*p
->callback
)(p
, WIDGET_CURSOR
, 0)){
498 } while (h
->current
!= p
);
502 /* Redraw the widgets in reverse order, leaving the current widget
505 void dlg_redraw (Dlg_head
*h
)
507 (h
->callback
)(h
, DLG_DRAW
, 0);
509 dlg_broadcast_msg (h
, WIDGET_DRAW
, 1);
514 void dlg_stop (Dlg_head
*h
)
519 static inline void dialog_handle_key (Dlg_head
*h
, int d_key
)
533 interactive_display (NULL
, h
->help_ctx
);
543 /* Use this if the refreshes fail */
548 #endif /* HAVE_SLANG */
555 h
->ret_value
= B_ENTER
;
563 h
->ret_value
= B_CANCEL
;
570 dlg_try_hotkey (Dlg_head
*h
, int d_key
)
579 * Explanation: we don't send letter hotkeys to other widgets if
580 * the currently selected widget is an input line
583 if (h
->current
->options
& W_IS_INPUT
) {
584 if (d_key
< 255 && isalpha (d_key
))
588 /* If it's an alt key, send the message */
589 c
= d_key
& ~ALT (0);
590 if (d_key
& ALT (0) && c
< 255 && isalpha (c
))
594 if (h
->current
->options
& W_WANT_HOTKEY
)
595 handled
= h
->current
->callback (h
->current
, WIDGET_HOTKEY
, d_key
);
597 /* If not used, send hotkey to other widgets */
601 hot_cur
= h
->current
;
603 /* send it to all widgets */
605 if (hot_cur
->options
& W_WANT_HOTKEY
)
607 (*hot_cur
->callback
) (hot_cur
, WIDGET_HOTKEY
, d_key
);
610 hot_cur
= hot_cur
->next
;
611 } while (h
->current
!= hot_cur
&& !handled
);
616 do_select_widget (h
, hot_cur
, SELECT_EXACT
);
621 dlg_key_event (Dlg_head
*h
, int d_key
)
628 /* TAB used to cycle */
629 if (!(h
->flags
& DLG_WANT_TAB
)) {
633 } else if (d_key
== KEY_BTAB
) {
639 /* first can dlg_callback handle the key */
640 handled
= (*h
->callback
) (h
, DLG_KEY
, d_key
);
642 /* next try the hotkey */
644 handled
= dlg_try_hotkey (h
, d_key
);
647 (*h
->callback
) (h
, DLG_HOTKEY_HANDLED
, 0);
649 /* not used - then try widget_callback */
651 handled
= h
->current
->callback (h
->current
, WIDGET_KEY
, d_key
);
653 /* not used- try to use the unhandled case */
655 handled
= (*h
->callback
) (h
, DLG_UNHANDLED_KEY
, d_key
);
658 dialog_handle_key (h
, d_key
);
660 (*h
->callback
) (h
, DLG_POST_KEY
, d_key
);
664 dlg_mouse_event (Dlg_head
* h
, Gpm_Event
* event
)
667 Widget
*starting_widget
= h
->current
;
672 item
= starting_widget
;
674 Widget
*widget
= item
;
678 if (!((x
> widget
->x
) && (x
<= widget
->x
+ widget
->cols
)
679 && (y
> widget
->y
) && (y
<= widget
->y
+ widget
->lines
)))
683 new_event
.x
-= widget
->x
;
684 new_event
.y
-= widget
->y
;
689 return (*widget
->mouse
) (&new_event
, widget
);
690 } while (item
!= starting_widget
);
695 /* Run dialog routines */
697 /* Init the process */
698 void init_dlg (Dlg_head
*h
)
700 /* Initialize dialog manager and widgets */
701 (*h
->callback
) (h
, DLG_INIT
, 0);
702 dlg_broadcast_msg (h
, WIDGET_INIT
, 0);
704 if (h
->x
== 0 && h
->y
== 0 && h
->cols
== COLS
&& h
->lines
== LINES
)
707 h
->parent
= current_dlg
;
710 /* Initialize the mouse status */
711 h
->mouse_status
= MOU_NORMAL
;
713 /* Select the first widget that takes focus */
714 while (!dlg_focus (h
) && h
->current
)
715 h
->current
= h
->current
->next
;
717 /* Redraw the screen */
724 /* Shutdown the run_dlg */
725 void dlg_run_done (Dlg_head
*h
)
728 (*h
->callback
) (h
, DLG_END
, 0);
730 current_dlg
= h
->parent
;
733 void dlg_process_event (Dlg_head
*h
, int key
, Gpm_Event
*event
)
736 if (got_interrupt ())
743 h
->mouse_status
= dlg_mouse_event (h
, event
);
745 dlg_key_event (h
, key
);
749 frontend_run_dlg (Dlg_head
*h
)
757 change_screen_size ();
761 execute_hooks (idle_hook
);
763 while ((h
->flags
& DLG_WANT_IDLE
) && is_idle ())
764 (*h
->callback
) (h
, DLG_IDLE
, 0);
766 /* Allow terminating the dialog from the idle handler */
773 /* Clear interrupt flag */
775 d_key
= get_event (&event
, h
->mouse_status
== MOU_REPEAT
, 1);
777 dlg_process_event (h
, d_key
, &event
);
780 (*h
->callback
) (h
, DLG_VALIDATE
, 0);
784 /* Standard run dialog routine
785 * We have to keep this routine small so that we can duplicate it's
786 * behavior on complex routines like the file routines, this way,
787 * they can call the dlg_process_event without rewriting all the code
789 int run_dlg (Dlg_head
*h
)
792 frontend_run_dlg (h
);
798 destroy_dlg (Dlg_head
*h
)
803 dlg_broadcast_msg (h
, WIDGET_DESTROY
, 0);
805 for (i
= 0; i
< h
->count
; i
++) {
816 void widget_set_size (Widget
*widget
, int y
, int x
, int lines
, int cols
)
821 widget
->lines
= lines
;
824 /* Replace widget old_w for widget new_w in the dialog h */
826 dlg_replace_widget (Dlg_head
*h
, Widget
*old_w
, Widget
*new_w
)
828 int should_focus
= 0;
833 if (old_w
== h
->current
)
837 new_w
->dlg_id
= old_w
->dlg_id
;
839 if (old_w
== old_w
->next
) {
840 /* just one widget */
844 new_w
->prev
= old_w
->prev
;
845 new_w
->next
= old_w
->next
;
846 old_w
->prev
->next
= new_w
;
847 old_w
->next
->prev
= new_w
;
853 send_message (old_w
, WIDGET_DESTROY
, 0);
854 send_message (new_w
, WIDGET_INIT
, 0);
857 dlg_select_widget (h
, new_w
);
859 send_message (new_w
, WIDGET_DRAW
, 0);