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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
47 #include "key.h" /* For mi_getch() */
48 #include "dialog.h" /* For do_refresh() */
49 #include "complete.h" /* INPUT_COMPLETE_CD */
53 /* {{{ Common dialog callback */
56 dialog_repaint (struct Dlg_head
*h
, int back
, int title_fore
)
60 space
= (h
->flags
& DLG_COMPACT
) ? 0 : 1;
64 draw_box (h
, space
, space
, h
->lines
- 2 * space
, h
->cols
- 2 * space
);
67 dlg_move (h
, space
, (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 */
89 #define listbox_refresh(h) common_dialog_repaint(h)
91 static int listbox_callback (Dlg_head
*h
, int id
, int msg
)
101 Listbox
*create_listbox_window (int cols
, int lines
, char *title
, char *help
)
104 Listbox
*listbox
= g_new (Listbox
, 1);
105 char* cancel_string
= _("&Cancel");
108 lines
= (lines
> LINES
-6) ? LINES
- 6 : lines
;
110 if (title
&& (cols
< (len
= strlen(title
) + 2)))
113 /* no &, but 4 spaces around button for brackets and such */
114 if (cols
< (len
= strlen(cancel_string
) + 3))
117 cols
= cols
> COLS
-6 ? COLS
-6 : cols
;
119 /* I'm not sure if this -2 is safe, should test it */
120 xpos
= (COLS
-cols
)/2;
121 ypos
= (LINES
-lines
)/2 - 2;
123 /* Create components */
124 listbox
->dlg
= create_dlg (ypos
, xpos
, lines
+6, cols
+4, dialog_colors
,
125 listbox_callback
, help
, "listbox", DLG_CENTER
);
126 x_set_dialog_title (listbox
->dlg
, title
);
128 listbox
->list
= listbox_new (2, 2, cols
, lines
, listbox_finish
, 0, "li");
130 add_widget (listbox
->dlg
,
131 button_new (lines
+3, (cols
/2 + 2) - len
/2,
132 B_CANCEL
, NORMAL_BUTTON
, cancel_string
, 0, 0, "c"));
133 add_widget (listbox
->dlg
, listbox
->list
);
134 listbox_refresh(listbox
->dlg
);
138 /* Returns the number of the item selected */
139 int run_listbox (Listbox
*l
)
144 if (l
->dlg
->ret_value
== B_CANCEL
)
148 destroy_dlg (l
->dlg
);
156 /* {{{ Query Dialog functions */
157 static int query_callback (struct Dlg_head
*h
, int Id
, int Msg
)
161 dialog_repaint (h
, NORMALC
, HOT_NORMALC
);
168 Dlg_head
*last_query_dlg
;
170 static int sel_pos
= 0;
172 /* Used to ask questions to the user */
173 int query_dialog (char *header
, char *text
, int flags
, int count
, ...)
183 static int query_colors
[4];
185 /* set dialog colors */
186 query_colors
[0] = (flags
& D_ERROR
) ? ERROR_COLOR
: Q_UNSELECTED_COLOR
;
187 query_colors
[1] = (flags
& D_ERROR
) ? REVERSE_COLOR
: Q_SELECTED_COLOR
;
188 query_colors
[2] = (flags
& D_ERROR
) ? ERROR_COLOR
: COLOR_HOT_NORMAL
;
189 query_colors
[3] = (flags
& D_ERROR
) ? COLOR_HOT_NORMAL
: COLOR_HOT_FOCUS
;
191 if (header
== MSG_ERROR
)
192 header
= _(" Error ");
195 va_start (ap
, count
);
196 for (i
= 0; i
< count
; i
++)
198 char* cp
= va_arg (ap
, char *);
199 win_len
+= strlen (cp
) + 6;
200 if (strchr (cp
, '&') != NULL
)
206 /* count coordinates */
207 cols
= 6 + max (win_len
, max (strlen (header
), msglen (text
, &lines
)));
208 lines
+= 4 + (count
> 0 ? 2 : 0);
209 xpos
= COLS
/2 - cols
/2;
210 ypos
= LINES
/3 - (lines
-3)/2;
213 query_dlg
= create_dlg (ypos
, xpos
, lines
, cols
, query_colors
,
214 query_callback
, "[QueryBox]", "query", DLG_BACKWARD
);
215 x_set_dialog_title (query_dlg
, header
);
219 cols
= (cols
-win_len
-2)/2+2;
220 va_start (ap
, count
);
221 for (i
= 0; i
< count
; i
++){
222 cur_name
= va_arg (ap
, char *);
223 xpos
= strlen (cur_name
)+6;
224 if (strchr(cur_name
, '&') != NULL
)
226 add_widget (query_dlg
, button_new
227 (lines
-3, cols
, B_USER
+i
, NORMAL_BUTTON
, cur_name
,
231 query_dlg
->initfocus
= query_dlg
->current
;
235 add_widget (query_dlg
, label_new (2, 3, text
, NULL
));
237 /* run dialog and make result */
239 switch (query_dlg
->ret_value
){
243 result
= query_dlg
->ret_value
-B_USER
;
246 /* free used memory */
247 destroy_dlg (query_dlg
);
249 add_widget (query_dlg
, label_new (2, 3, text
, NULL
));
250 add_widget (query_dlg
, button_new(0, 0, 0, HIDDEN_BUTTON
, "-", 0, 0, NULL
));
251 last_query_dlg
= query_dlg
;
257 void query_set_sel (int new_sel
)
264 /* {{{ The message function */
266 /* To show nice messages to the users */
267 Dlg_head
*message (int error
, char *header
, const char *text
, ...)
273 /* Setup the display information */
274 strcpy (buffer
, "\n");
275 va_start (args
, text
);
276 g_vsnprintf (&buffer
[1], sizeof (buffer
) - 2, text
, args
);
277 strcat (buffer
, "\n");
280 query_dialog (header
, buffer
, error
, 0);
284 if (!(error
& D_INSERT
)){
294 /* {{{ The chooser routines */
296 static int remove_callback (int i
, void *data
)
298 Chooser
*c
= (Chooser
*) data
;
300 listbox_remove_current (c
->listbox
, 0);
302 dlg_select_widget (c
->dialog
, c
->listbox
);
303 dlg_select_nth_widget (c
->dialog
, 0);
305 /* Return: do not abort dialog */
309 Chooser
*new_chooser (int lines
, int cols
, char *help
, int flags
)
314 c
=g_new (Chooser
, 1);
315 c
->dialog
= create_dlg (0, 0, lines
, cols
, dialog_colors
, common_dialog_callback
,
316 help
, "chooser", DLG_CENTER
);
318 c
->dialog
->lines
= lines
;
319 c
->dialog
->cols
= cols
;
321 button_lines
= flags
& CHOOSE_EDITABLE
? 3 : 0;
323 c
->listbox
= listbox_new (1, 1, cols
-2, lines
-button_lines
,
324 listbox_finish
, 0, "listbox");
327 add_widget (c
->dialog
, button_new (lines
-button_lines
+1,
328 20, B_ENTER
, DEFPUSH_BUTTON
, _("&Remove"),
329 remove_callback
, c
, "button-remove"));
330 add_widget (c
->dialog
, button_new (lines
-button_lines
+1,
331 4, B_CANCEL
, NORMAL_BUTTON
, _("&Cancel"),
332 0, 0, "button-cancel"));
334 add_widget (c
->dialog
, c
->listbox
);
338 int run_chooser (Chooser
*c
)
341 return c
->dialog
->ret_value
;
344 void destroy_chooser (Chooser
*c
)
346 destroy_dlg (c
->dialog
);
351 /* {{{ Quick dialog routines */
353 static int quick_callback (struct Dlg_head
*h
, int id
, int Msg
)
357 common_dialog_repaint (h
);
361 h
->ret_value
= B_ENTER
;
369 #define I18N(x) (do_int && *x ? (x = _(x)): x)
371 int quick_dialog_skip (QuickDialog
*qd
, int nskip
)
387 qd
->title
= _(qd
->title
);
392 dd
= create_dlg (0, 0, qd
->ylen
, qd
->xlen
, dialog_colors
, quick_callback
,
393 qd
->help
, qd
->class, DLG_CENTER
| DLG_TRYUP
);
395 dd
= create_dlg (qd
->ypos
, qd
->xpos
, qd
->ylen
, qd
->xlen
, dialog_colors
,
397 qd
->help
, qd
->class, DLG_NONE
);
399 x_set_dialog_title (dd
, qd
->title
);
401 /* We pass this to the callback */
403 dd
->lines
= qd
->ylen
;
405 for (qw
= qd
->widgets
; qw
->widget_type
; qw
++){
406 xpos
= (qd
->xlen
* qw
->relative_x
)/qw
->x_divisions
;
407 ypos
= (qd
->ylen
* qw
->relative_y
)/qw
->y_divisions
;
409 switch (qw
->widget_type
){
411 widget
= check_new (ypos
, xpos
, *qw
->result
, I18N (qw
->text
), qw
->tkname
);
415 r
= radio_new (ypos
, xpos
, qw
->hotkey_pos
, qw
->str_result
, 1, qw
->tkname
);
416 r
->pos
= r
->sel
= qw
->value
;
421 widget
= button_new (ypos
, xpos
, qw
->value
, (qw
->value
==B_ENTER
) ? DEFPUSH_BUTTON
: NORMAL_BUTTON
,
422 I18N (qw
->text
), 0, 0, qw
->tkname
);
425 /* We use the hotkey pos as the field length */
427 input
= input_new (ypos
, xpos
, INPUT_COLOR
,
428 qw
->hotkey_pos
, qw
->text
, qw
->tkname
);
429 input
->is_password
= qw
->value
== 1;
432 input
->completion_flags
|= INPUT_COMPLETE_CD
;
437 widget
= label_new (ypos
, xpos
, I18N(qw
->text
), qw
->tkname
);
442 fprintf (stderr
, "QuickWidget: unknown widget type\n");
445 qw
->the_widget
= widget
;
446 add_widget (dd
, widget
);
450 dd
->current
= dd
->current
->next
;
454 /* Get the data if we found something interesting */
455 if (dd
->ret_value
!= B_CANCEL
){
456 for (qw
= qd
->widgets
; qw
->widget_type
; qw
++){
457 switch (qw
->widget_type
){
459 *qw
->result
= ((WCheck
*) qw
->the_widget
)->state
& C_BOOL
;
463 *qw
->result
= ((WRadio
*) qw
->the_widget
)->sel
;
467 *qw
->str_result
= g_strdup (((WInput
*) qw
->the_widget
)->buffer
);
472 return_val
= dd
->ret_value
;
478 int quick_dialog (QuickDialog
*qd
)
480 return quick_dialog_skip (qd
, 0);
485 /* {{{ Input routines */
487 #define INPUT_INDEX 2
488 char *real_input_dialog_help (char *header
, char *text
, char *help
, char *def_text
)
490 QuickDialog Quick_input
;
491 QuickWidget quick_widgets
[] = {
492 { quick_button
, 6, 10, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
494 { quick_button
, 3, 10, 1, 0, N_("&Ok"), 0, B_ENTER
, 0, 0,
496 { quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, 0 },
497 { quick_label
, 3, 80, 2, 0, "", 0, 0, 0, 0, "label" },
504 char tk_name
[64] = "inp|";
506 /* we need a unique name for tkname because widget.c:history_tool()
507 needs a unique name for each dialog - using the header is ideal */
509 strncpy (tk_name
+ 3, header
, 60);
511 quick_widgets
[2].tkname
= tk_name
;
513 len
= max (strlen (header
), msglen (text
, &lines
)) + 4;
516 /* Translators should take care as "Password" or its translations
517 are used to identify password boxes and hide characters with "*" */
518 my_str
= _("Password:");
519 if (strncmp (text
, my_str
, strlen (my_str
)-1) == 0){
520 quick_widgets
[INPUT_INDEX
].value
= 1;
523 quick_widgets
[INPUT_INDEX
].value
= 0;
528 * An attempt to place buttons symmetrically, based on actual i18n
529 * length of the string. It looks nicer with i18n (IMO) - alex
531 quick_widgets
[0].relative_x
= len
/2 + 4;
532 quick_widgets
[1].relative_x
=
533 len
/2 - (strlen (_(quick_widgets
[1].text
)) + 9);
534 quick_widgets
[0].x_divisions
= quick_widgets
[1].x_divisions
= len
;
535 #endif /* ENABLE_NLS */
537 Quick_input
.xlen
= len
;
538 Quick_input
.xpos
= -1;
539 Quick_input
.title
= header
;
540 Quick_input
.help
= help
;
541 Quick_input
.class = "quick_input";
542 Quick_input
.i18n
= 0;
543 quick_widgets
[INPUT_INDEX
+1].text
= text
;
544 quick_widgets
[INPUT_INDEX
].text
= def_text
;
546 for (i
= 0; i
< 4; i
++)
547 quick_widgets
[i
].y_divisions
= lines
+6;
548 Quick_input
.ylen
= lines
+ 6;
550 for (i
= 0; i
< 3; i
++)
551 quick_widgets
[i
].relative_y
+= 2 + lines
;
553 quick_widgets
[INPUT_INDEX
].str_result
= &my_str
;
555 Quick_input
.widgets
= quick_widgets
;
556 if (quick_dialog (&Quick_input
) != B_CANCEL
){
557 return *(quick_widgets
[INPUT_INDEX
].str_result
);
562 char *input_dialog (char *header
, char *text
, char *def_text
)
564 return input_dialog_help (header
, text
, "[Input Line Keys]", def_text
);
567 char *input_expand_dialog (char *header
, char *text
, char *def_text
)
572 result
= input_dialog (header
, text
, def_text
);
574 expanded
= tilde_expand (result
);
587 Cause emacs to enter folding mode for this file: