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.
45 #include "key.h" /* For mi_getch() */
46 #include "dialog.h" /* For do_refresh() */
47 #include "complete.h" /* INPUT_COMPLETE_CD */
51 /* {{{ Listbox utility functions */
53 Listbox
*create_listbox_window (int cols
, int lines
, char *title
, char *help
)
56 Listbox
*listbox
= g_new (Listbox
, 1);
57 char* cancel_string
= _("&Cancel");
60 lines
= (lines
> LINES
-6) ? LINES
- 6 : lines
;
62 if (title
&& (cols
< (len
= strlen(title
) + 2)))
65 /* no &, but 4 spaces around button for brackets and such */
66 if (cols
< (len
= strlen(cancel_string
) + 3))
69 cols
= cols
> COLS
-6 ? COLS
-6 : cols
;
71 /* I'm not sure if this -2 is safe, should test it */
73 ypos
= (LINES
-lines
)/2 - 2;
75 /* Create components */
76 listbox
->dlg
= create_dlg (ypos
, xpos
, lines
+6, cols
+4, dialog_colors
,
77 NULL
, help
, title
, DLG_CENTER
);
79 listbox
->list
= listbox_new (2, 2, cols
, lines
, listbox_finish
, 0, "li");
81 add_widget (listbox
->dlg
,
82 button_new (lines
+3, (cols
/2 + 2) - len
/2,
83 B_CANCEL
, NORMAL_BUTTON
, cancel_string
, 0, 0, "c"));
84 add_widget (listbox
->dlg
, listbox
->list
);
85 common_dialog_repaint (listbox
->dlg
);
89 /* Returns the number of the item selected */
90 int run_listbox (Listbox
*l
)
95 if (l
->dlg
->ret_value
== B_CANCEL
)
107 /* {{{ Query Dialog functions */
108 Dlg_head
*last_query_dlg
;
110 static int sel_pos
= 0;
112 /* Used to ask questions to the user */
113 int query_dialog (char *header
, char *text
, int flags
, int count
, ...)
123 static const int *query_colors
;
125 /* set dialog colors */
127 query_colors
= alarm_colors
;
129 query_colors
= dialog_colors
;
131 if (header
== MSG_ERROR
)
135 va_start (ap
, count
);
136 for (i
= 0; i
< count
; i
++)
138 char* cp
= va_arg (ap
, char *);
139 win_len
+= strlen (cp
) + 6;
140 if (strchr (cp
, '&') != NULL
)
146 /* count coordinates */
147 cols
= 6 + max (win_len
, max (strlen (header
), msglen (text
, &lines
)));
148 lines
+= 4 + (count
> 0 ? 2 : 0);
149 xpos
= COLS
/2 - cols
/2;
150 ypos
= LINES
/3 - (lines
-3)/2;
153 query_dlg
= create_dlg (ypos
, xpos
, lines
, cols
, query_colors
, NULL
,
154 "[QueryBox]", header
, DLG_BACKWARD
);
158 cols
= (cols
-win_len
-2)/2+2;
159 va_start (ap
, count
);
160 for (i
= 0; i
< count
; i
++){
161 cur_name
= va_arg (ap
, char *);
162 xpos
= strlen (cur_name
)+6;
163 if (strchr(cur_name
, '&') != NULL
)
165 add_widget (query_dlg
, button_new
166 (lines
-3, cols
, B_USER
+i
, NORMAL_BUTTON
, cur_name
,
170 query_dlg
->initfocus
= query_dlg
->current
;
174 add_widget (query_dlg
, label_new (2, 3, text
, NULL
));
176 /* run dialog and make result */
178 switch (query_dlg
->ret_value
){
182 result
= query_dlg
->ret_value
-B_USER
;
185 /* free used memory */
186 destroy_dlg (query_dlg
);
188 add_widget (query_dlg
, label_new (2, 3, text
, NULL
));
189 add_widget (query_dlg
, button_new(0, 0, 0, HIDDEN_BUTTON
, "-", 0, 0, NULL
));
190 last_query_dlg
= query_dlg
;
196 void query_set_sel (int new_sel
)
203 /* {{{ The message function */
205 /* To show nice messages to the users */
207 message (int error
, char *header
, const char *text
, ...)
213 /* Setup the display information */
214 strcpy (buffer
, "\n");
215 va_start (args
, text
);
216 g_vsnprintf (&buffer
[1], sizeof (buffer
) - 2, text
, args
);
217 strcat (buffer
, "\n");
220 query_dialog (header
, buffer
, error
, 0);
224 if (!(error
& D_INSERT
)){
234 /* {{{ Quick dialog routines */
236 static int quick_callback (struct Dlg_head
*h
, int id
, int Msg
)
240 common_dialog_repaint (h
);
244 h
->ret_value
= B_ENTER
;
252 #define I18N(x) (do_int && *x ? (x = _(x)): x)
255 quick_dialog_skip (QuickDialog
*qd
, int nskip
)
266 int count
= 0; /* number of quick widgets */
267 int curr_widget
; /* number of the current quick widget */
268 Widget
**widgets
; /* table of corresponding widgets */
274 qd
->title
= _(qd
->title
);
279 dd
= create_dlg (0, 0, qd
->ylen
, qd
->xlen
, dialog_colors
,
280 quick_callback
, qd
->help
, qd
->title
,
281 DLG_CENTER
| DLG_TRYUP
);
283 dd
= create_dlg (qd
->ypos
, qd
->xpos
, qd
->ylen
, qd
->xlen
,
284 dialog_colors
, quick_callback
, qd
->help
,
285 qd
->title
, DLG_NONE
);
287 /* We pass this to the callback */
289 dd
->lines
= qd
->ylen
;
292 for (qw
= qd
->widgets
; qw
->widget_type
; qw
++) {
296 widgets
= (Widget
**) g_new (Widget
*, count
);
298 for (curr_widget
= 0, qw
= qd
->widgets
; qw
->widget_type
; qw
++) {
299 xpos
= (qd
->xlen
* qw
->relative_x
) / qw
->x_divisions
;
300 ypos
= (qd
->ylen
* qw
->relative_y
) / qw
->y_divisions
;
302 switch (qw
->widget_type
) {
305 check_new (ypos
, xpos
, *qw
->result
, I18N (qw
->text
),
310 r
= radio_new (ypos
, xpos
, qw
->hotkey_pos
, qw
->str_result
, 1,
312 r
->pos
= r
->sel
= qw
->value
;
318 button_new (ypos
, xpos
, qw
->value
,
320 B_ENTER
) ? DEFPUSH_BUTTON
: NORMAL_BUTTON
,
321 I18N (qw
->text
), 0, 0, qw
->tkname
);
324 /* We use the hotkey pos as the field length */
326 input
= input_new (ypos
, xpos
, INPUT_COLOR
,
327 qw
->hotkey_pos
, qw
->text
, qw
->tkname
);
328 input
->is_password
= qw
->value
== 1;
331 input
->completion_flags
|= INPUT_COMPLETE_CD
;
336 widget
= label_new (ypos
, xpos
, I18N (qw
->text
), qw
->tkname
);
341 fprintf (stderr
, "QuickWidget: unknown widget type\n");
344 widgets
[curr_widget
++] = widget
;
345 add_widget (dd
, widget
);
349 dd
->current
= dd
->current
->next
;
353 /* Get the data if we found something interesting */
354 if (dd
->ret_value
!= B_CANCEL
) {
355 for (curr_widget
= 0, qw
= qd
->widgets
; qw
->widget_type
; qw
++) {
356 Widget
*w
= widgets
[curr_widget
++];
358 switch (qw
->widget_type
) {
360 *qw
->result
= ((WCheck
*) w
)->state
& C_BOOL
;
364 *qw
->result
= ((WRadio
*) w
)->sel
;
368 *qw
->str_result
= g_strdup (((WInput
*) w
)->buffer
);
373 return_val
= dd
->ret_value
;
380 int quick_dialog (QuickDialog
*qd
)
382 return quick_dialog_skip (qd
, 0);
387 /* {{{ Input routines */
389 #define INPUT_INDEX 2
391 real_input_dialog_help (char *header
, char *text
, char *help
,
394 QuickDialog Quick_input
;
395 QuickWidget quick_widgets
[] = {
396 {quick_button
, 6, 10, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
398 {quick_button
, 3, 10, 1, 0, N_("&OK"), 0, B_ENTER
, 0, 0,
400 {quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, 0},
401 {quick_label
, 4, 80, 2, 0, "", 0, 0, 0, 0, "label"},
410 char tk_name
[64] = "inp|";
412 /* we need a unique name for tkname because widget.c:history_tool()
413 needs a unique name for each dialog - using the header is ideal */
415 strncpy (tk_name
+ 3, header
, 60);
417 quick_widgets
[2].tkname
= tk_name
;
419 len
= max (strlen (header
), msglen (text
, &lines
)) + 4;
422 /* The special value of def_text is used to identify password boxes
423 and hide characters with "*". Don't save passwords in history! */
424 if (def_text
== INPUT_PASSWORD
) {
425 quick_widgets
[INPUT_INDEX
].value
= 1;
429 quick_widgets
[INPUT_INDEX
].value
= 0;
434 * An attempt to place buttons symmetrically, based on actual i18n
435 * length of the string. It looks nicer with i18n (IMO) - alex
437 quick_widgets
[0].relative_x
= len
/ 2 + 4;
438 quick_widgets
[1].relative_x
=
439 len
/ 2 - (strlen (_(quick_widgets
[1].text
)) + 9);
440 quick_widgets
[0].x_divisions
= quick_widgets
[1].x_divisions
= len
;
441 #endif /* ENABLE_NLS */
443 Quick_input
.xlen
= len
;
444 Quick_input
.xpos
= -1;
445 Quick_input
.title
= header
;
446 Quick_input
.help
= help
;
447 Quick_input
.i18n
= 0;
448 quick_widgets
[INPUT_INDEX
+ 1].text
= g_strstrip (g_strdup (text
));
449 quick_widgets
[INPUT_INDEX
].text
= def_text
;
451 for (i
= 0; i
< 4; i
++)
452 quick_widgets
[i
].y_divisions
= lines
+ 6;
453 Quick_input
.ylen
= lines
+ 6;
455 for (i
= 0; i
< 3; i
++)
456 quick_widgets
[i
].relative_y
+= 2 + lines
;
458 quick_widgets
[INPUT_INDEX
].str_result
= &my_str
;
460 Quick_input
.widgets
= quick_widgets
;
461 ret
= quick_dialog (&Quick_input
);
462 g_free (quick_widgets
[INPUT_INDEX
+ 1].text
);
464 if (ret
!= B_CANCEL
) {
465 return *(quick_widgets
[INPUT_INDEX
].str_result
);
470 char *input_dialog (char *header
, char *text
, char *def_text
)
472 return input_dialog_help (header
, text
, "[Input Line Keys]", def_text
);
475 char *input_expand_dialog (char *header
, char *text
, char *def_text
)
480 result
= input_dialog (header
, text
, def_text
);
482 expanded
= tilde_expand (result
);
495 Cause emacs to enter folding mode for this file: