3 /* {{{ Copyright Notice */
5 /* Widget based utility functions.
6 Copyright (C) 1994, 1995 the Free Software Foundation
8 Authors: 1994, 1995, 1996 Miguel de Icaza
9 1994, 1995 Radek Doulik
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
49 #include "key.h" /* For mi_getch() */
50 #include "dialog.h" /* For do_refresh() and my_wputs() */
51 #include "complete.h" /* INPUT_COMPLETE_CD */
56 /* {{{ Common dialog callback */
58 void dialog_repaint (struct Dlg_head
*h
, int back
, int title_fore
)
63 dialog_repaint (struct Dlg_head
*h
, int back
, int title_fore
)
67 draw_box (h
, 1, 1, h
->lines
- 2, h
->cols
- 2);
70 dlg_move (h
, 1, (h
->cols
-strlen (h
->title
))/2);
76 common_dialog_repaint (struct Dlg_head
*h
)
78 dialog_repaint (h
, COLOR_NORMAL
, COLOR_HOT_NORMAL
);
82 common_dialog_callback (struct Dlg_head
*h
, int id
, int msg
)
85 common_dialog_repaint (h
);
90 /* {{{ Listbox utility functions */
93 listbox_refresh (Dlg_head
*h
)
95 dialog_repaint (h
, COLOR_NORMAL
, COLOR_HOT_NORMAL
);
98 static int listbox_callback (Dlg_head
*h
, int id
, int msg
)
110 Listbox
*create_listbox_window (int cols
, int lines
, char *title
, char *help
)
113 Listbox
*listbox
= xmalloc (sizeof (Listbox
), "create_listbox_window");
114 char* cancel_string
= _("&Cancel");
117 lines
= (lines
> LINES
-6) ? LINES
- 6 : lines
;
119 if (title
&& (cols
< (len
= strlen(title
) + 2)))
122 /* no &, but 4 spaces around button for brackets and such */
123 if (cols
< (len
= strlen(cancel_string
) + 3))
126 cols
= cols
> COLS
-6 ? COLS
-6 : cols
;
128 /* I'm not sure if this -2 is safe, should test it */
129 xpos
= (COLS
-cols
)/2;
130 ypos
= (LINES
-lines
)/2 - 2;
132 /* Create components */
133 listbox
->dlg
= create_dlg (ypos
, xpos
, lines
+6, cols
+4, dialog_colors
,
134 listbox_callback
, help
, "listbox", DLG_CENTER
|DLG_GRID
);
135 x_set_dialog_title (listbox
->dlg
, title
);
137 listbox
->list
= listbox_new (2, 2, cols
, lines
, listbox_finish
, 0, "li");
139 add_widget (listbox
->dlg
,
140 button_new (lines
+3, (cols
/2 + 2) - len
/2,
141 B_CANCEL
, NORMAL_BUTTON
, cancel_string
, 0, 0, "c"));
142 add_widget (listbox
->dlg
, listbox
->list
);
144 listbox_refresh(listbox
->dlg
);
149 /* Returns the number of the item selected */
150 int run_listbox (Listbox
*l
)
155 if (l
->dlg
->ret_value
== B_CANCEL
)
159 destroy_dlg (l
->dlg
);
167 /* {{{ Query Dialog functions */
174 static int query_callback (struct Dlg_head
*h
, int Id
, int Msg
)
176 struct text_struct
*info
;
178 info
= (struct text_struct
*) h
->data
;
183 /* designate window */
186 draw_box (h
, 1, 1, h
->lines
-2, h
->cols
-2);
187 attrset (HOT_NORMALC
);
188 dlg_move (h
, 1, (h
->cols
-strlen (info
->header
))/2);
189 addstr (info
->header
);
197 Dlg_head
*last_query_dlg
;
199 static int sel_pos
= 0;
201 /* Used to ask questions to the user */
202 int query_dialog (char *header
, char *text
, int flags
, int count
, ...)
212 static int query_colors
[4];
213 static struct text_struct pass
;
215 static char *buttonnames
[10];
218 /* set dialog colors */
219 query_colors
[0] = (flags
& D_ERROR
) ? ERROR_COLOR
: Q_UNSELECTED_COLOR
;
220 query_colors
[1] = (flags
& D_ERROR
) ? REVERSE_COLOR
: Q_SELECTED_COLOR
;
221 query_colors
[2] = (flags
& D_ERROR
) ? ERROR_COLOR
: COLOR_HOT_NORMAL
;
222 query_colors
[3] = (flags
& D_ERROR
) ? COLOR_HOT_NORMAL
: COLOR_HOT_FOCUS
;
224 if (header
== MSG_ERROR
)
225 header
= _(" Error ");
228 va_start (ap
, count
);
229 for (i
= 0; i
< count
; i
++)
231 char* cp
= va_arg (ap
, char *);
232 win_len
+= strlen (cp
) + 6;
233 if (strchr (cp
, '&') != NULL
)
239 /* count coordinates */
240 cols
= 6 + max (win_len
, max (strlen (header
), msglen (text
, &lines
)));
241 lines
+= 4 + (count
> 0 ? 2 : 0);
242 xpos
= COLS
/2 - cols
/2;
243 ypos
= LINES
/3 - (lines
-3)/2;
244 pass
.header
= header
;
248 query_dlg
= create_dlg (ypos
, xpos
, lines
, cols
, query_colors
,
249 query_callback
, "[QueryBox]", "query", DLG_NONE
);
250 x_set_dialog_title (query_dlg
, header
);
252 /* The data we need to pass to the callback */
253 query_dlg
->cols
= cols
;
254 query_dlg
->lines
= lines
;
255 query_dlg
->data
= &pass
;
257 query_dlg
->direction
= DIR_BACKWARD
;
261 cols
= (cols
-win_len
-2)/2+2;
262 va_start (ap
, count
);
263 for (i
= 0; i
< count
; i
++){
264 cur_name
= va_arg (ap
, char *);
265 xpos
= strlen (cur_name
)+6;
266 if (strchr(cur_name
, '&') != NULL
)
269 add_widget (query_dlg
, button_new
270 (lines
-3, cols
, B_USER
+i
, NORMAL_BUTTON
, cur_name
,
273 buttonnames
[i
] = cur_name
;
277 query_dlg
->initfocus
= query_dlg
->current
;
282 for (i
= count
- 1; i
>= 0; i
--)
283 add_widgetl (query_dlg
, button_new
284 (0, 0, B_USER
+i
, NORMAL_BUTTON
, buttonnames
[i
], 0, 0, NULL
),
285 i
? XV_WLAY_RIGHTOF
: XV_WLAY_CENTERROW
);
287 add_widget (query_dlg
, label_new (2, 3, text
, NULL
));
289 /* run dialog and make result */
291 switch (query_dlg
->ret_value
){
295 result
= query_dlg
->ret_value
-B_USER
;
298 /* free used memory */
299 destroy_dlg (query_dlg
);
302 add_widgetl (query_dlg
, button_new(0, 0, B_EXIT
, NORMAL_BUTTON
, _("&Ok"), 0, 0, NULL
),
305 add_widget (query_dlg
, label_new (2, 3, text
, NULL
));
307 if (flags
& D_INSERT
){
312 destroy_dlg (query_dlg
);
315 add_widget (query_dlg
, label_new (2, 3, text
, NULL
));
316 add_widget (query_dlg
, button_new(0, 0, 0, HIDDEN_BUTTON
, "-", 0, 0, NULL
));
318 last_query_dlg
= query_dlg
;
324 void query_set_sel (int new_sel
)
331 /* {{{ The message function */
333 /* To show nice messages to the users */
334 Dlg_head
*message (int error
, char *header
, char *text
, ...)
340 /* Setup the display information */
341 strcpy (buffer
, "\n");
342 va_start (args
, text
);
343 vsprintf (&buffer
[1], text
, args
);
344 strcat (buffer
, "\n");
347 query_dialog (header
, buffer
, error
, 0);
351 if (error
& D_INSERT
){
358 if (!(error
& D_INSERT
)){
371 /* {{{ The chooser routines */
373 static int remove_callback (int i
, void *data
)
375 Chooser
*c
= (Chooser
*) data
;
377 listbox_remove_current (c
->listbox
, 0);
379 dlg_select_widget (c
->dialog
, c
->listbox
);
380 dlg_select_nth_widget (c
->dialog
, 0);
382 /* Return: do not abort dialog */
386 Chooser
*new_chooser (int lines
, int cols
, char *help
, int flags
)
391 c
= (Chooser
*) xmalloc (sizeof (Chooser
), "new_chooser");
392 c
->dialog
= create_dlg (0, 0, lines
, cols
, dialog_colors
, common_dialog_callback
,
393 help
, "chooser", DLG_CENTER
| DLG_GRID
);
395 c
->dialog
->lines
= lines
;
396 c
->dialog
->cols
= cols
;
398 button_lines
= flags
& CHOOSE_EDITABLE
? 3 : 0;
400 c
->listbox
= listbox_new (1, 1, cols
-2, lines
-button_lines
,
401 listbox_finish
, 0, "listbox");
404 add_widget (c
->dialog
, button_new (lines
-button_lines
+1,
405 20, B_ENTER
, DEFPUSH_BUTTON
, _("&Remove"),
406 remove_callback
, c
, "button-remove"));
407 add_widget (c
->dialog
, button_new (lines
-button_lines
+1,
408 4, B_CANCEL
, NORMAL_BUTTON
, _("&Cancel"),
409 0, 0, "button-cancel"));
411 add_widget (c
->dialog
, c
->listbox
);
415 int run_chooser (Chooser
*c
)
418 return c
->dialog
->ret_value
;
421 void destroy_chooser (Chooser
*c
)
423 destroy_dlg (c
->dialog
);
428 /* {{{ Quick dialog routines */
430 static int quick_callback (struct Dlg_head
*h
, int id
, int Msg
)
435 attrset (COLOR_NORMAL
);
437 draw_box (h
, 1, 1, h
->lines
-2, h
->cols
-2);
439 attrset (COLOR_HOT_NORMAL
);
440 dlg_move (h
, 1,((h
->cols
-strlen (h
->data
))/2));
446 h
->ret_value
= B_ENTER
;
454 #define I18N(x) (do_int && *x ? (x = _(x)): x)
456 int quick_dialog_skip (QuickDialog
*qd
, int nskip
)
472 qd
->title
= _(qd
->title
);
477 dd
= create_dlg (0, 0, qd
->ylen
, qd
->xlen
, dialog_colors
, quick_callback
,
478 qd
->help
, qd
->class, DLG_CENTER
| DLG_TRYUP
| DLG_GRID
);
480 dd
= create_dlg (qd
->ypos
, qd
->xpos
, qd
->ylen
, qd
->xlen
, dialog_colors
,
482 qd
->help
, qd
->class, DLG_GRID
);
484 x_set_dialog_title (dd
, qd
->title
);
486 /* We pass this to the callback */
488 dd
->lines
= qd
->ylen
;
489 dd
->data
= qd
->title
;
491 for (qw
= qd
->widgets
; qw
->widget_type
; qw
++){
495 xpos
= (qd
->xlen
* qw
->relative_x
)/qw
->x_divisions
;
496 ypos
= (qd
->ylen
* qw
->relative_y
)/qw
->y_divisions
;
499 switch (qw
->widget_type
){
501 widget
= check_new (ypos
, xpos
, *qw
->result
, I18N (qw
->text
), qw
->tkname
);
505 r
= radio_new (ypos
, xpos
, qw
->hotkey_pos
, qw
->str_result
, 1, qw
->tkname
);
506 r
->pos
= r
->sel
= qw
->value
;
511 widget
= button_new (ypos
, xpos
, qw
->value
, (qw
->value
==B_ENTER
) ? DEFPUSH_BUTTON
: NORMAL_BUTTON
,
512 I18N (qw
->text
), 0, 0, qw
->tkname
);
515 /* We use the hotkey pos as the field length */
517 input
= input_new (ypos
, xpos
, INPUT_COLOR
,
518 qw
->hotkey_pos
, qw
->text
, qw
->tkname
);
519 input
->is_password
= qw
->value
== 1;
522 input
->completion_flags
|= INPUT_COMPLETE_CD
;
527 widget
= label_new (ypos
, xpos
, I18N(qw
->text
), qw
->tkname
);
532 fprintf (stderr
, "QuickWidget: unknown widget type\n");
535 qw
->the_widget
= widget
;
536 add_widgetl (dd
, widget
, qw
->layout
);
540 dd
->current
= dd
->current
->next
;
544 /* Get the data if we found something interesting */
545 if (dd
->ret_value
!= B_CANCEL
){
546 for (qw
= qd
->widgets
; qw
->widget_type
; qw
++){
547 switch (qw
->widget_type
){
549 *qw
->result
= ((WCheck
*) qw
->the_widget
)->state
& C_BOOL
;
553 *qw
->result
= ((WRadio
*) qw
->the_widget
)->sel
;
557 *qw
->str_result
= strdup (((WInput
*) qw
->the_widget
)->buffer
);
562 return_val
= dd
->ret_value
;
568 int quick_dialog (QuickDialog
*qd
)
570 return quick_dialog_skip (qd
, 0);
575 /* {{{ Input routines */
576 #define INPUT_INDEX 2
577 char *real_input_dialog_help (char *header
, char *text
, char *help
, char *def_text
)
579 QuickDialog Quick_input
;
580 QuickWidget quick_widgets
[] = {
581 { quick_button
, 6, 10, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
582 XV_WLAY_RIGHTOF
, "button-cancel" },
583 { quick_button
, 3, 10, 1, 0, N_("&Ok"), 0, B_ENTER
, 0, 0,
584 XV_WLAY_CENTERROW
, "button-ok" },
585 { quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, XV_WLAY_NEXTROW
, 0 },
586 { quick_label
, 3, 80, 2, 0, "", 0, 0, 0, 0, XV_WLAY_NEXTROW
, "label" },
593 char tk_name
[64] = "inp|";
595 /* we need a unique name for tkname because widget.c:history_tool()
596 needs a unique name for each dialog - using the header is ideal */
599 strncpy (tk_name
+ 4, header
, 59);
601 strncpy (tk_name
+ 3, header
, 60);
604 quick_widgets
[2].tkname
= tk_name
;
606 len
= max (strlen (header
), msglen (text
, &lines
)) + 4;
609 if (strncmp (text
, _("Password:"), strlen (_("Password"))) == 0){
610 quick_widgets
[INPUT_INDEX
].value
= 1;
613 quick_widgets
[INPUT_INDEX
].value
= 0;
618 * An attempt to place buttons symmetrically, based on actual i18n
619 * length of the string. It looks nicer with i18n (IMO) - alex
621 quick_widgets
[0].relative_x
= len
/2 + 4;
622 quick_widgets
[1].relative_x
=
623 len
/2 - (strlen (_(quick_widgets
[1].text
)) + 9);
624 quick_widgets
[0].x_divisions
= quick_widgets
[1].x_divisions
= len
;
625 #endif /* ENABLE_NLS */
627 Quick_input
.xlen
= len
;
628 Quick_input
.xpos
= -1;
629 Quick_input
.title
= header
;
630 Quick_input
.help
= help
;
631 Quick_input
.class = "quick_input";
632 Quick_input
.i18n
= 0;
633 quick_widgets
[INPUT_INDEX
+1].text
= text
;
634 quick_widgets
[INPUT_INDEX
].text
= def_text
;
636 for (i
= 0; i
< 4; i
++)
637 quick_widgets
[i
].y_divisions
= lines
+6;
638 Quick_input
.ylen
= lines
+ 6;
640 for (i
= 0; i
< 3; i
++)
641 quick_widgets
[i
].relative_y
+= 2 + lines
;
643 quick_widgets
[INPUT_INDEX
].str_result
= &my_str
;
645 Quick_input
.widgets
= quick_widgets
;
646 if (quick_dialog (&Quick_input
) != B_CANCEL
){
647 return *(quick_widgets
[INPUT_INDEX
].str_result
);
652 char *input_dialog (char *header
, char *text
, char *def_text
)
654 return input_dialog_help (header
, text
, "[Input Line Keys]", def_text
);
657 int input_dialog_help_2 (char *header
, char *text1
, char *text2
, char *help
, char **r1
, char **r2
)
659 QuickDialog Quick_input
;
660 QuickWidget quick_widgets
[] = {
661 { quick_button
, 6, 10, 4, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
662 XV_WLAY_DONTCARE
, "button-cancel" },
663 { quick_button
, 3, 10, 4, 0, N_("Ok")
665 XV_WLAY_DONTCARE
, "button-ok" },
666 { quick_input
, 4, 80, 4, 0, "", 58, 0, 0, 0, XV_WLAY_BELOWCLOSE
, "input-pth" },
667 { quick_label
, 3, 80, 3, 0, "", 0, 0, 0, 0, XV_WLAY_DONTCARE
, "label-pth" },
668 { quick_input
, 4, 80, 3, 0, "", 58, 0, 0, 0, XV_WLAY_BELOWCLOSE
, "input-lbl" },
669 { quick_label
, 3, 80, 2, 0, "", 0, 0, 0, 0, XV_WLAY_DONTCARE
, "label-lbl" },
675 char *my_str1
, *my_str2
;
677 len
= max (strlen (header
), msglen (text1
, &lines1
));
678 len
= max (len
, msglen (text2
, &lines2
)) + 4;
681 Quick_input
.xlen
= len
;
682 Quick_input
.xpos
= -1;
683 Quick_input
.title
= header
;
684 Quick_input
.help
= help
;
685 Quick_input
.class = "quick_input_2";
686 Quick_input
.i18n
= 0;
687 quick_widgets
[5].text
= text1
;
688 quick_widgets
[3].text
= text2
;
690 for (i
= 0; i
< 6; i
++)
691 quick_widgets
[i
].y_divisions
= lines1
+lines2
+7;
692 Quick_input
.ylen
= lines1
+ lines2
+ 7;
694 quick_widgets
[0].relative_y
+= (lines1
+ lines2
);
695 quick_widgets
[1].relative_y
+= (lines1
+ lines2
);
696 quick_widgets
[2].relative_y
+= (lines1
);
697 quick_widgets
[3].relative_y
+= (lines1
);
699 quick_widgets
[4].str_result
= &my_str1
;
700 quick_widgets
[2].str_result
= &my_str2
;
702 Quick_input
.widgets
= quick_widgets
;
703 if (quick_dialog (&Quick_input
) != B_CANCEL
){
704 *r1
= *(quick_widgets
[4].str_result
);
705 *r2
= *(quick_widgets
[2].str_result
);
711 int input_dialog_2 (char *header
, char *text1
, char *text2
, char **r1
, char **r2
)
713 return input_dialog_help_2 (header
, text1
, text2
, "[Input Line Keys]", r1
, r2
);
716 char *input_expand_dialog (char *header
, char *text
, char *def_text
)
721 result
= input_dialog (header
, text
, def_text
);
723 expanded
= tilde_expand (result
);
737 Cause emacs to enter folding mode for this file: