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 /* {{{ Listbox utility functions */
55 Listbox
*create_listbox_window (int cols
, int lines
, char *title
, char *help
)
58 Listbox
*listbox
= g_new (Listbox
, 1);
59 char* cancel_string
= _("&Cancel");
62 lines
= (lines
> LINES
-6) ? LINES
- 6 : lines
;
64 if (title
&& (cols
< (len
= strlen(title
) + 2)))
67 /* no &, but 4 spaces around button for brackets and such */
68 if (cols
< (len
= strlen(cancel_string
) + 3))
71 cols
= cols
> COLS
-6 ? COLS
-6 : cols
;
73 /* I'm not sure if this -2 is safe, should test it */
75 ypos
= (LINES
-lines
)/2 - 2;
77 /* Create components */
78 listbox
->dlg
= create_dlg (ypos
, xpos
, lines
+6, cols
+4, dialog_colors
,
79 NULL
, help
, title
, DLG_CENTER
);
81 listbox
->list
= listbox_new (2, 2, cols
, lines
, listbox_finish
, 0, "li");
83 add_widget (listbox
->dlg
,
84 button_new (lines
+3, (cols
/2 + 2) - len
/2,
85 B_CANCEL
, NORMAL_BUTTON
, cancel_string
, 0, 0, "c"));
86 add_widget (listbox
->dlg
, listbox
->list
);
87 common_dialog_repaint (listbox
->dlg
);
91 /* Returns the number of the item selected */
92 int run_listbox (Listbox
*l
)
97 if (l
->dlg
->ret_value
== B_CANCEL
)
101 destroy_dlg (l
->dlg
);
109 /* {{{ Query Dialog functions */
110 Dlg_head
*last_query_dlg
;
112 static int sel_pos
= 0;
114 /* Used to ask questions to the user */
115 int query_dialog (char *header
, char *text
, int flags
, int count
, ...)
125 static const int *query_colors
;
127 /* set dialog colors */
129 query_colors
= alarm_colors
;
131 query_colors
= dialog_colors
;
133 if (header
== MSG_ERROR
)
134 header
= _(" Error ");
137 va_start (ap
, count
);
138 for (i
= 0; i
< count
; i
++)
140 char* cp
= va_arg (ap
, char *);
141 win_len
+= strlen (cp
) + 6;
142 if (strchr (cp
, '&') != NULL
)
148 /* count coordinates */
149 cols
= 6 + max (win_len
, max (strlen (header
), msglen (text
, &lines
)));
150 lines
+= 4 + (count
> 0 ? 2 : 0);
151 xpos
= COLS
/2 - cols
/2;
152 ypos
= LINES
/3 - (lines
-3)/2;
155 query_dlg
= create_dlg (ypos
, xpos
, lines
, cols
, query_colors
, NULL
,
156 "[QueryBox]", header
, DLG_BACKWARD
);
160 cols
= (cols
-win_len
-2)/2+2;
161 va_start (ap
, count
);
162 for (i
= 0; i
< count
; i
++){
163 cur_name
= va_arg (ap
, char *);
164 xpos
= strlen (cur_name
)+6;
165 if (strchr(cur_name
, '&') != NULL
)
167 add_widget (query_dlg
, button_new
168 (lines
-3, cols
, B_USER
+i
, NORMAL_BUTTON
, cur_name
,
172 query_dlg
->initfocus
= query_dlg
->current
;
176 add_widget (query_dlg
, label_new (2, 3, text
, NULL
));
178 /* run dialog and make result */
180 switch (query_dlg
->ret_value
){
184 result
= query_dlg
->ret_value
-B_USER
;
187 /* free used memory */
188 destroy_dlg (query_dlg
);
190 add_widget (query_dlg
, label_new (2, 3, text
, NULL
));
191 add_widget (query_dlg
, button_new(0, 0, 0, HIDDEN_BUTTON
, "-", 0, 0, NULL
));
192 last_query_dlg
= query_dlg
;
198 void query_set_sel (int new_sel
)
205 /* {{{ The message function */
207 /* To show nice messages to the users */
208 Dlg_head
*message (int error
, char *header
, const char *text
, ...)
214 /* Setup the display information */
215 strcpy (buffer
, "\n");
216 va_start (args
, text
);
217 g_vsnprintf (&buffer
[1], sizeof (buffer
) - 2, text
, args
);
218 strcat (buffer
, "\n");
221 query_dialog (header
, buffer
, error
, 0);
225 if (!(error
& D_INSERT
)){
235 /* {{{ Quick dialog routines */
237 static int quick_callback (struct Dlg_head
*h
, int id
, int Msg
)
241 common_dialog_repaint (h
);
245 h
->ret_value
= B_ENTER
;
253 #define I18N(x) (do_int && *x ? (x = _(x)): x)
255 int quick_dialog_skip (QuickDialog
*qd
, int nskip
)
271 qd
->title
= _(qd
->title
);
276 dd
= create_dlg (0, 0, qd
->ylen
, qd
->xlen
, dialog_colors
, quick_callback
,
277 qd
->help
, qd
->title
, DLG_CENTER
| DLG_TRYUP
);
279 dd
= create_dlg (qd
->ypos
, qd
->xpos
, qd
->ylen
, qd
->xlen
, dialog_colors
,
281 qd
->help
, qd
->title
, DLG_NONE
);
283 /* We pass this to the callback */
285 dd
->lines
= qd
->ylen
;
287 for (qw
= qd
->widgets
; qw
->widget_type
; qw
++){
288 xpos
= (qd
->xlen
* qw
->relative_x
)/qw
->x_divisions
;
289 ypos
= (qd
->ylen
* qw
->relative_y
)/qw
->y_divisions
;
291 switch (qw
->widget_type
){
293 widget
= check_new (ypos
, xpos
, *qw
->result
, I18N (qw
->text
), qw
->tkname
);
297 r
= radio_new (ypos
, xpos
, qw
->hotkey_pos
, qw
->str_result
, 1, qw
->tkname
);
298 r
->pos
= r
->sel
= qw
->value
;
303 widget
= button_new (ypos
, xpos
, qw
->value
, (qw
->value
==B_ENTER
) ? DEFPUSH_BUTTON
: NORMAL_BUTTON
,
304 I18N (qw
->text
), 0, 0, qw
->tkname
);
307 /* We use the hotkey pos as the field length */
309 input
= input_new (ypos
, xpos
, INPUT_COLOR
,
310 qw
->hotkey_pos
, qw
->text
, qw
->tkname
);
311 input
->is_password
= qw
->value
== 1;
314 input
->completion_flags
|= INPUT_COMPLETE_CD
;
319 widget
= label_new (ypos
, xpos
, I18N(qw
->text
), qw
->tkname
);
324 fprintf (stderr
, "QuickWidget: unknown widget type\n");
327 qw
->the_widget
= widget
;
328 add_widget (dd
, widget
);
332 dd
->current
= dd
->current
->next
;
336 /* Get the data if we found something interesting */
337 if (dd
->ret_value
!= B_CANCEL
){
338 for (qw
= qd
->widgets
; qw
->widget_type
; qw
++){
339 switch (qw
->widget_type
){
341 *qw
->result
= ((WCheck
*) qw
->the_widget
)->state
& C_BOOL
;
345 *qw
->result
= ((WRadio
*) qw
->the_widget
)->sel
;
349 *qw
->str_result
= g_strdup (((WInput
*) qw
->the_widget
)->buffer
);
354 return_val
= dd
->ret_value
;
360 int quick_dialog (QuickDialog
*qd
)
362 return quick_dialog_skip (qd
, 0);
367 /* {{{ Input routines */
369 #define INPUT_INDEX 2
371 real_input_dialog_help (char *header
, char *text
, char *help
,
374 QuickDialog Quick_input
;
375 QuickWidget quick_widgets
[] = {
376 {quick_button
, 6, 10, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
378 {quick_button
, 3, 10, 1, 0, N_("&Ok"), 0, B_ENTER
, 0, 0,
380 {quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, 0},
381 {quick_label
, 4, 80, 2, 0, "", 0, 0, 0, 0, "label"},
390 char tk_name
[64] = "inp|";
392 /* we need a unique name for tkname because widget.c:history_tool()
393 needs a unique name for each dialog - using the header is ideal */
395 strncpy (tk_name
+ 3, header
, 60);
397 quick_widgets
[2].tkname
= tk_name
;
399 len
= max (strlen (header
), msglen (text
, &lines
)) + 4;
402 /* The special value of def_text is used to identify password boxes
403 and hide characters with "*". Don't save passwords in history! */
404 if (def_text
== INPUT_PASSWORD
) {
405 quick_widgets
[INPUT_INDEX
].value
= 1;
409 quick_widgets
[INPUT_INDEX
].value
= 0;
414 * An attempt to place buttons symmetrically, based on actual i18n
415 * length of the string. It looks nicer with i18n (IMO) - alex
417 quick_widgets
[0].relative_x
= len
/ 2 + 4;
418 quick_widgets
[1].relative_x
=
419 len
/ 2 - (strlen (_(quick_widgets
[1].text
)) + 9);
420 quick_widgets
[0].x_divisions
= quick_widgets
[1].x_divisions
= len
;
421 #endif /* ENABLE_NLS */
423 Quick_input
.xlen
= len
;
424 Quick_input
.xpos
= -1;
425 Quick_input
.title
= header
;
426 Quick_input
.help
= help
;
427 Quick_input
.i18n
= 0;
428 quick_widgets
[INPUT_INDEX
+ 1].text
= g_strstrip (g_strdup (text
));
429 quick_widgets
[INPUT_INDEX
].text
= def_text
;
431 for (i
= 0; i
< 4; i
++)
432 quick_widgets
[i
].y_divisions
= lines
+ 6;
433 Quick_input
.ylen
= lines
+ 6;
435 for (i
= 0; i
< 3; i
++)
436 quick_widgets
[i
].relative_y
+= 2 + lines
;
438 quick_widgets
[INPUT_INDEX
].str_result
= &my_str
;
440 Quick_input
.widgets
= quick_widgets
;
441 ret
= quick_dialog (&Quick_input
);
442 g_free (quick_widgets
[INPUT_INDEX
+ 1].text
);
444 if (ret
!= B_CANCEL
) {
445 return *(quick_widgets
[INPUT_INDEX
].str_result
);
450 char *input_dialog (char *header
, char *text
, char *def_text
)
452 return input_dialog_help (header
, text
, "[Input Line Keys]", def_text
);
455 char *input_expand_dialog (char *header
, char *text
, char *def_text
)
460 result
= input_dialog (header
, text
, def_text
);
462 expanded
= tilde_expand (result
);
475 Cause emacs to enter folding mode for this file: