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.
46 #include "key.h" /* For mi_getch() */
47 #include "dialog.h" /* For do_refresh() */
48 #include "complete.h" /* INPUT_COMPLETE_CD */
53 /* {{{ Common dialog callback */
55 void dialog_repaint (struct Dlg_head
*h
, int back
, int title_fore
)
60 dialog_repaint (struct Dlg_head
*h
, int back
, int title_fore
)
64 draw_box (h
, 1, 1, h
->lines
- 2, h
->cols
- 2);
67 dlg_move (h
, 1, (h
->cols
-strlen (h
->title
))/2);
73 common_dialog_repaint (struct Dlg_head
*h
)
75 dialog_repaint (h
, COLOR_NORMAL
, COLOR_HOT_NORMAL
);
79 common_dialog_callback (struct Dlg_head
*h
, int id
, int msg
)
82 common_dialog_repaint (h
);
87 /* {{{ Listbox utility functions */
90 listbox_refresh (Dlg_head
*h
)
92 dialog_repaint (h
, COLOR_NORMAL
, COLOR_HOT_NORMAL
);
95 static int listbox_callback (Dlg_head
*h
, int id
, int msg
)
107 Listbox
*create_listbox_window (int cols
, int lines
, char *title
, char *help
)
110 Listbox
*listbox
= g_new (Listbox
, 1);
111 char* cancel_string
= _("&Cancel");
114 lines
= (lines
> LINES
-6) ? LINES
- 6 : lines
;
116 if (title
&& (cols
< (len
= strlen(title
) + 2)))
119 /* no &, but 4 spaces around button for brackets and such */
120 if (cols
< (len
= strlen(cancel_string
) + 3))
123 cols
= cols
> COLS
-6 ? COLS
-6 : cols
;
125 /* I'm not sure if this -2 is safe, should test it */
126 xpos
= (COLS
-cols
)/2;
127 ypos
= (LINES
-lines
)/2 - 2;
129 /* Create components */
130 listbox
->dlg
= create_dlg (ypos
, xpos
, lines
+6, cols
+4, dialog_colors
,
131 listbox_callback
, help
, "listbox", DLG_CENTER
|DLG_GRID
);
132 x_set_dialog_title (listbox
->dlg
, title
);
134 listbox
->list
= listbox_new (2, 2, cols
, lines
, listbox_finish
, 0, "li");
136 add_widget (listbox
->dlg
,
137 button_new (lines
+3, (cols
/2 + 2) - len
/2,
138 B_CANCEL
, NORMAL_BUTTON
, cancel_string
, 0, 0, "c"));
139 add_widget (listbox
->dlg
, listbox
->list
);
141 listbox_refresh(listbox
->dlg
);
146 /* Returns the number of the item selected */
147 int run_listbox (Listbox
*l
)
152 if (l
->dlg
->ret_value
== B_CANCEL
)
156 destroy_dlg (l
->dlg
);
164 /* {{{ Query Dialog functions */
171 static int query_callback (struct Dlg_head
*h
, int Id
, int Msg
)
173 struct text_struct
*info
;
175 info
= (struct text_struct
*) h
->data
;
180 /* designate window */
183 draw_box (h
, 1, 1, h
->lines
-2, h
->cols
-2);
184 attrset (HOT_NORMALC
);
185 dlg_move (h
, 1, (h
->cols
-strlen (info
->header
))/2);
186 addstr (info
->header
);
194 Dlg_head
*last_query_dlg
;
196 static int sel_pos
= 0;
198 /* Used to ask questions to the user */
199 int query_dialog (char *header
, char *text
, int flags
, int count
, ...)
209 static int query_colors
[4];
210 static struct text_struct pass
;
212 static char *buttonnames
[10];
215 /* set dialog colors */
216 query_colors
[0] = (flags
& D_ERROR
) ? ERROR_COLOR
: Q_UNSELECTED_COLOR
;
217 query_colors
[1] = (flags
& D_ERROR
) ? REVERSE_COLOR
: Q_SELECTED_COLOR
;
218 query_colors
[2] = (flags
& D_ERROR
) ? ERROR_COLOR
: COLOR_HOT_NORMAL
;
219 query_colors
[3] = (flags
& D_ERROR
) ? COLOR_HOT_NORMAL
: COLOR_HOT_FOCUS
;
221 if (header
== MSG_ERROR
)
222 header
= _(" Error ");
225 va_start (ap
, count
);
226 for (i
= 0; i
< count
; i
++)
228 char* cp
= va_arg (ap
, char *);
229 win_len
+= strlen (cp
) + 6;
230 if (strchr (cp
, '&') != NULL
)
236 /* count coordinates */
237 cols
= 6 + max (win_len
, max (strlen (header
), msglen (text
, &lines
)));
238 lines
+= 4 + (count
> 0 ? 2 : 0);
239 xpos
= COLS
/2 - cols
/2;
240 ypos
= LINES
/3 - (lines
-3)/2;
241 pass
.header
= header
;
245 query_dlg
= create_dlg (ypos
, xpos
, lines
, cols
, query_colors
,
246 query_callback
, "[QueryBox]", "query", DLG_NONE
);
247 x_set_dialog_title (query_dlg
, header
);
249 /* The data we need to pass to the callback */
250 query_dlg
->cols
= cols
;
251 query_dlg
->lines
= lines
;
252 query_dlg
->data
= &pass
;
254 query_dlg
->direction
= DIR_BACKWARD
;
258 cols
= (cols
-win_len
-2)/2+2;
259 va_start (ap
, count
);
260 for (i
= 0; i
< count
; i
++){
261 cur_name
= va_arg (ap
, char *);
262 xpos
= strlen (cur_name
)+6;
263 if (strchr(cur_name
, '&') != NULL
)
265 add_widget (query_dlg
, button_new
266 (lines
-3, cols
, B_USER
+i
, NORMAL_BUTTON
, cur_name
,
270 query_dlg
->initfocus
= query_dlg
->current
;
274 add_widget (query_dlg
, label_new (2, 3, text
, NULL
));
276 /* run dialog and make result */
278 switch (query_dlg
->ret_value
){
282 result
= query_dlg
->ret_value
-B_USER
;
285 /* free used memory */
286 destroy_dlg (query_dlg
);
289 add_widgetl (query_dlg
, button_new(0, 0, B_EXIT
, NORMAL_BUTTON
, _("&Ok"), 0, 0, NULL
),
292 add_widget (query_dlg
, label_new (2, 3, text
, NULL
));
294 if (flags
& D_INSERT
){
299 destroy_dlg (query_dlg
);
302 add_widget (query_dlg
, label_new (2, 3, text
, NULL
));
303 add_widget (query_dlg
, button_new(0, 0, 0, HIDDEN_BUTTON
, "-", 0, 0, NULL
));
305 last_query_dlg
= query_dlg
;
311 void query_set_sel (int new_sel
)
318 /* {{{ The message function */
320 /* To show nice messages to the users */
321 Dlg_head
*message (int error
, char *header
, char *text
, ...)
327 /* Setup the display information */
328 strcpy (buffer
, "\n");
329 va_start (args
, text
);
330 g_vsnprintf (&buffer
[1], sizeof (buffer
), text
, args
);
331 strcat (buffer
, "\n");
334 query_dialog (header
, buffer
, error
, 0);
338 if (!(error
& D_INSERT
)){
349 /* {{{ The chooser routines */
351 static int remove_callback (int i
, void *data
)
353 Chooser
*c
= (Chooser
*) data
;
355 listbox_remove_current (c
->listbox
, 0);
357 dlg_select_widget (c
->dialog
, c
->listbox
);
358 dlg_select_nth_widget (c
->dialog
, 0);
360 /* Return: do not abort dialog */
364 Chooser
*new_chooser (int lines
, int cols
, char *help
, int flags
)
369 c
=g_new (Chooser
, 1);
370 c
->dialog
= create_dlg (0, 0, lines
, cols
, dialog_colors
, common_dialog_callback
,
371 help
, "chooser", DLG_CENTER
| DLG_GRID
);
373 c
->dialog
->lines
= lines
;
374 c
->dialog
->cols
= cols
;
376 button_lines
= flags
& CHOOSE_EDITABLE
? 3 : 0;
378 c
->listbox
= listbox_new (1, 1, cols
-2, lines
-button_lines
,
379 listbox_finish
, 0, "listbox");
382 add_widget (c
->dialog
, button_new (lines
-button_lines
+1,
383 20, B_ENTER
, DEFPUSH_BUTTON
, _("&Remove"),
384 remove_callback
, c
, "button-remove"));
385 add_widget (c
->dialog
, button_new (lines
-button_lines
+1,
386 4, B_CANCEL
, NORMAL_BUTTON
, _("&Cancel"),
387 0, 0, "button-cancel"));
389 add_widget (c
->dialog
, c
->listbox
);
393 int run_chooser (Chooser
*c
)
396 return c
->dialog
->ret_value
;
399 void destroy_chooser (Chooser
*c
)
401 destroy_dlg (c
->dialog
);
406 /* {{{ Quick dialog routines */
408 static int quick_callback (struct Dlg_head
*h
, int id
, int Msg
)
413 attrset (COLOR_NORMAL
);
415 draw_box (h
, 1, 1, h
->lines
-2, h
->cols
-2);
417 attrset (COLOR_HOT_NORMAL
);
418 dlg_move (h
, 1,((h
->cols
-strlen (h
->data
))/2));
424 h
->ret_value
= B_ENTER
;
432 #define I18N(x) (do_int && *x ? (x = _(x)): x)
434 int quick_dialog_skip (QuickDialog
*qd
, int nskip
)
450 qd
->title
= _(qd
->title
);
455 dd
= create_dlg (0, 0, qd
->ylen
, qd
->xlen
, dialog_colors
, quick_callback
,
456 qd
->help
, qd
->class, DLG_CENTER
| DLG_TRYUP
| DLG_GRID
);
458 dd
= create_dlg (qd
->ypos
, qd
->xpos
, qd
->ylen
, qd
->xlen
, dialog_colors
,
460 qd
->help
, qd
->class, DLG_GRID
);
462 x_set_dialog_title (dd
, qd
->title
);
464 /* We pass this to the callback */
466 dd
->lines
= qd
->ylen
;
467 dd
->data
= qd
->title
;
469 for (qw
= qd
->widgets
; qw
->widget_type
; qw
++){
473 xpos
= (qd
->xlen
* qw
->relative_x
)/qw
->x_divisions
;
474 ypos
= (qd
->ylen
* qw
->relative_y
)/qw
->y_divisions
;
477 switch (qw
->widget_type
){
479 widget
= check_new (ypos
, xpos
, *qw
->result
, I18N (qw
->text
), qw
->tkname
);
483 r
= radio_new (ypos
, xpos
, qw
->hotkey_pos
, qw
->str_result
, 1, qw
->tkname
);
484 r
->pos
= r
->sel
= qw
->value
;
489 widget
= button_new (ypos
, xpos
, qw
->value
, (qw
->value
==B_ENTER
) ? DEFPUSH_BUTTON
: NORMAL_BUTTON
,
490 I18N (qw
->text
), 0, 0, qw
->tkname
);
493 /* We use the hotkey pos as the field length */
495 input
= input_new (ypos
, xpos
, INPUT_COLOR
,
496 qw
->hotkey_pos
, qw
->text
, qw
->tkname
);
497 input
->is_password
= qw
->value
== 1;
500 input
->completion_flags
|= INPUT_COMPLETE_CD
;
505 widget
= label_new (ypos
, xpos
, I18N(qw
->text
), qw
->tkname
);
510 fprintf (stderr
, "QuickWidget: unknown widget type\n");
513 qw
->the_widget
= widget
;
514 add_widgetl (dd
, widget
, qw
->layout
);
518 dd
->current
= dd
->current
->next
;
522 /* Get the data if we found something interesting */
523 if (dd
->ret_value
!= B_CANCEL
){
524 for (qw
= qd
->widgets
; qw
->widget_type
; qw
++){
525 switch (qw
->widget_type
){
527 *qw
->result
= ((WCheck
*) qw
->the_widget
)->state
& C_BOOL
;
531 *qw
->result
= ((WRadio
*) qw
->the_widget
)->sel
;
535 *qw
->str_result
= g_strdup (((WInput
*) qw
->the_widget
)->buffer
);
540 return_val
= dd
->ret_value
;
546 int quick_dialog (QuickDialog
*qd
)
548 return quick_dialog_skip (qd
, 0);
553 /* {{{ Input routines */
556 #define INPUT_INDEX 2
557 char *real_input_dialog_help (char *header
, char *text
, char *help
, char *def_text
)
559 QuickDialog Quick_input
;
560 QuickWidget quick_widgets
[] = {
561 { quick_button
, 6, 10, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
562 XV_WLAY_RIGHTOF
, "button-cancel" },
563 { quick_button
, 3, 10, 1, 0, N_("&Ok"), 0, B_ENTER
, 0, 0,
564 XV_WLAY_CENTERROW
, "button-ok" },
565 { quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, XV_WLAY_NEXTROW
, 0 },
566 { quick_label
, 3, 80, 2, 0, "", 0, 0, 0, 0, XV_WLAY_NEXTROW
, "label" },
573 char tk_name
[64] = "inp|";
575 /* we need a unique name for tkname because widget.c:history_tool()
576 needs a unique name for each dialog - using the header is ideal */
578 strncpy (tk_name
+ 3, header
, 60);
580 quick_widgets
[2].tkname
= tk_name
;
582 len
= max (strlen (header
), msglen (text
, &lines
)) + 4;
585 /* Translators should take care as "Password" or its translations
586 are used to identify password boxes and hide characters with "*" */
587 if (strncmp (text
, _("Password:"), strlen (_("Password:"))-1) == 0){
588 quick_widgets
[INPUT_INDEX
].value
= 1;
591 quick_widgets
[INPUT_INDEX
].value
= 0;
596 * An attempt to place buttons symmetrically, based on actual i18n
597 * length of the string. It looks nicer with i18n (IMO) - alex
599 quick_widgets
[0].relative_x
= len
/2 + 4;
600 quick_widgets
[1].relative_x
=
601 len
/2 - (strlen (_(quick_widgets
[1].text
)) + 9);
602 quick_widgets
[0].x_divisions
= quick_widgets
[1].x_divisions
= len
;
603 #endif /* ENABLE_NLS */
605 Quick_input
.xlen
= len
;
606 Quick_input
.xpos
= -1;
607 Quick_input
.title
= header
;
608 Quick_input
.help
= help
;
609 Quick_input
.class = "quick_input";
610 Quick_input
.i18n
= 0;
611 quick_widgets
[INPUT_INDEX
+1].text
= text
;
612 quick_widgets
[INPUT_INDEX
].text
= def_text
;
614 for (i
= 0; i
< 4; i
++)
615 quick_widgets
[i
].y_divisions
= lines
+6;
616 Quick_input
.ylen
= lines
+ 6;
618 for (i
= 0; i
< 3; i
++)
619 quick_widgets
[i
].relative_y
+= 2 + lines
;
621 quick_widgets
[INPUT_INDEX
].str_result
= &my_str
;
623 Quick_input
.widgets
= quick_widgets
;
624 if (quick_dialog (&Quick_input
) != B_CANCEL
){
625 return *(quick_widgets
[INPUT_INDEX
].str_result
);
631 char *input_dialog (char *header
, char *text
, char *def_text
)
633 return input_dialog_help (header
, text
, "[Input Line Keys]", def_text
);
636 char *input_expand_dialog (char *header
, char *text
, char *def_text
)
641 result
= input_dialog (header
, text
, def_text
);
643 expanded
= tilde_expand (result
);
657 Cause emacs to enter folding mode for this file: