1 /* Widget based utility functions.
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007 Free Software Foundation, Inc.
5 Authors: 1994, 1995, 1996 Miguel de Icaza
6 1994, 1995 Radek Doulik
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
34 #include "color.h" /* dialog_colors */
38 #include "key.h" /* mi_getch() */
39 #include "complete.h" /* INPUT_COMPLETE_CD */
40 #include "background.h" /* parent_call */
44 create_listbox_window (int cols
, int lines
, const char *title
, const char *help
)
47 Listbox
*listbox
= g_new (Listbox
, 1);
48 const char *cancel_string
= _("&Cancel");
51 lines
= (lines
> LINES
- 6) ? LINES
- 6 : lines
;
53 if (title
&& (cols
< (len
= strlen (title
) + 2)))
56 /* no &, but 4 spaces around button for brackets and such */
57 if (cols
< (len
= strlen (cancel_string
) + 3))
60 cols
= cols
> COLS
- 6 ? COLS
- 6 : cols
;
61 xpos
= (COLS
- cols
) / 2;
62 ypos
= (LINES
- lines
) / 2 - 2;
64 /* Create components */
66 create_dlg (ypos
, xpos
, lines
+ 6, cols
+ 4, dialog_colors
, NULL
,
67 help
, title
, DLG_CENTER
| DLG_REVERSE
);
69 listbox
->list
= listbox_new (2, 2, cols
, lines
, 0);
71 add_widget (listbox
->dlg
,
72 button_new (lines
+ 3, (cols
/ 2 + 2) - len
/ 2, B_CANCEL
,
73 NORMAL_BUTTON
, cancel_string
, 0));
74 add_widget (listbox
->dlg
, listbox
->list
);
79 /* Returns the number of the item selected */
80 int run_listbox (Listbox
*l
)
85 if (l
->dlg
->ret_value
== B_CANCEL
)
95 static Dlg_head
*last_query_dlg
;
97 static int sel_pos
= 0;
99 /* Used to ask questions to the user */
101 query_dialog (const char *header
, const char *text
, int flags
, int count
, ...)
106 WButton
*defbutton
= NULL
;
113 static const int *query_colors
;
115 /* set dialog colors */
117 query_colors
= alarm_colors
;
119 query_colors
= dialog_colors
;
121 if (header
== MSG_ERROR
)
125 va_start (ap
, count
);
126 for (i
= 0; i
< count
; i
++) {
127 char *cp
= va_arg (ap
, char *);
128 win_len
+= strlen (cp
) + 6;
129 if (strchr (cp
, '&') != NULL
)
135 /* count coordinates */
136 msglen (text
, &lines
, &cols
);
137 cols
= 6 + max (win_len
, max ((int) strlen (header
), cols
));
138 lines
+= 4 + (count
> 0 ? 2 : 0);
139 xpos
= COLS
/ 2 - cols
/ 2;
140 ypos
= LINES
/ 3 - (lines
- 3) / 2;
144 create_dlg (ypos
, xpos
, lines
, cols
, query_colors
, NULL
,
145 "[QueryBox]", header
, DLG_NONE
);
148 cols
= (cols
- win_len
- 2) / 2 + 2;
149 va_start (ap
, count
);
150 for (i
= 0; i
< count
; i
++) {
151 cur_name
= va_arg (ap
, char *);
152 xpos
= strlen (cur_name
) + 6;
153 if (strchr (cur_name
, '&') != NULL
)
157 button_new (lines
- 3, cols
, B_USER
+ i
, NORMAL_BUTTON
,
159 add_widget (query_dlg
, button
);
166 add_widget (query_dlg
, label_new (2, 3, text
));
169 dlg_select_widget (defbutton
);
171 /* run dialog and make result */
173 switch (query_dlg
->ret_value
) {
177 result
= query_dlg
->ret_value
- B_USER
;
180 /* free used memory */
181 destroy_dlg (query_dlg
);
183 add_widget (query_dlg
, label_new (2, 3, text
));
184 add_widget (query_dlg
,
185 button_new (0, 0, 0, HIDDEN_BUTTON
, "-", 0));
186 last_query_dlg
= query_dlg
;
192 void query_set_sel (int new_sel
)
198 /* Create message dialog */
199 static struct Dlg_head
*
200 do_create_message (int flags
, const char *title
, const char *text
)
205 /* Add empty lines before and after the message */
206 p
= g_strconcat ("\n", text
, "\n", (char *) NULL
);
207 query_dialog (title
, p
, flags
, 0);
217 * Create message dialog. The caller must call dlg_run_done() and
218 * destroy_dlg() to dismiss it. Not safe to call from background.
221 create_message (int flags
, const char *title
, const char *text
, ...)
227 va_start (args
, text
);
228 p
= g_strdup_vprintf (text
, args
);
231 d
= do_create_message (flags
, title
, p
);
239 * Show message dialog. Dismiss it when any key is pressed.
240 * Not safe to call from background.
243 fg_message (int flags
, const char *title
, const char *text
)
247 d
= do_create_message (flags
, title
, text
);
254 /* Show message box from background */
255 #ifdef WITH_BACKGROUND
257 bg_message (int dummy
, int *flags
, char *title
, const char *text
)
260 title
= g_strconcat (_("Background process:"), " ", title
, (char *) NULL
);
261 fg_message (*flags
, title
, text
);
264 #endif /* WITH_BACKGROUND */
267 /* Show message box, background safe */
269 message (int flags
, const char *title
, const char *text
, ...)
275 p
= g_strdup_vprintf (text
, ap
);
278 if (title
== MSG_ERROR
)
281 #ifdef WITH_BACKGROUND
282 if (we_are_background
) {
283 parent_call ((void *) bg_message
, NULL
, 3, sizeof (flags
), &flags
,
284 strlen (title
), title
, strlen (p
), p
);
286 #endif /* WITH_BACKGROUND */
287 fg_message (flags
, title
, p
);
293 /* {{{ Quick dialog routines */
295 #define I18N(x) (do_int && *x ? (x = _(x)): x)
298 quick_dialog_skip (QuickDialog
*qd
, int nskip
)
309 int count
= 0; /* number of quick widgets */
310 int curr_widget
; /* number of the current quick widget */
311 Widget
**widgets
; /* table of corresponding widgets */
317 qd
->title
= _(qd
->title
);
322 dd
= create_dlg (0, 0, qd
->ylen
, qd
->xlen
, dialog_colors
, NULL
,
324 DLG_CENTER
| DLG_TRYUP
| DLG_REVERSE
);
326 dd
= create_dlg (qd
->ypos
, qd
->xpos
, qd
->ylen
, qd
->xlen
,
327 dialog_colors
, NULL
, qd
->help
, qd
->title
,
331 for (qw
= qd
->widgets
; qw
->widget_type
; qw
++) {
335 widgets
= (Widget
**) g_new (Widget
*, count
);
337 for (curr_widget
= 0, qw
= qd
->widgets
; qw
->widget_type
; qw
++) {
338 xpos
= (qd
->xlen
* qw
->relative_x
) / qw
->x_divisions
;
339 ypos
= (qd
->ylen
* qw
->relative_y
) / qw
->y_divisions
;
341 switch (qw
->widget_type
) {
343 widget
= check_new (ypos
, xpos
, *qw
->result
, I18N (qw
->text
));
347 r
= radio_new (ypos
, xpos
, qw
->hotkey_pos
, const_cast(const char **, qw
->str_result
));
348 r
->pos
= r
->sel
= qw
->value
;
354 button_new (ypos
, xpos
, qw
->value
,
356 B_ENTER
) ? DEFPUSH_BUTTON
: NORMAL_BUTTON
,
360 /* We use the hotkey pos as the field length */
363 input_new (ypos
, xpos
, INPUT_COLOR
, qw
->hotkey_pos
,
364 qw
->text
, qw
->histname
);
365 input
->is_password
= qw
->value
== 1;
368 input
->completion_flags
|= INPUT_COMPLETE_CD
;
373 widget
= label_new (ypos
, xpos
, I18N (qw
->text
));
378 fprintf (stderr
, "QuickWidget: unknown widget type\n");
381 widgets
[curr_widget
++] = widget
;
382 add_widget (dd
, widget
);
386 dd
->current
= dd
->current
->next
;
390 /* Get the data if we found something interesting */
391 if (dd
->ret_value
!= B_CANCEL
) {
392 for (curr_widget
= 0, qw
= qd
->widgets
; qw
->widget_type
; qw
++) {
393 Widget
*w
= widgets
[curr_widget
++];
395 switch (qw
->widget_type
) {
397 *qw
->result
= ((WCheck
*) w
)->state
& C_BOOL
;
401 *qw
->result
= ((WRadio
*) w
)->sel
;
407 tilde_expand (((WInput
*) w
)->buffer
);
409 *qw
->str_result
= g_strdup (((WInput
*) w
)->buffer
);
414 return_val
= dd
->ret_value
;
421 int quick_dialog (QuickDialog
*qd
)
423 return quick_dialog_skip (qd
, 0);
428 /* {{{ Input routines */
430 #define INPUT_INDEX 2
433 * Show dialog, not background safe.
435 * If the arguments "header" and "text" should be translated,
436 * that MUST be done by the caller of fg_input_dialog_help().
438 * The argument "history_name" holds the name of a section
439 * in the history file. Data entered in the input field of
440 * the dialog box will be stored there.
444 fg_input_dialog_help (const char *header
, const char *text
, const char *help
,
445 const char *history_name
, const char *def_text
)
447 QuickDialog Quick_input
;
448 QuickWidget quick_widgets
[] = {
449 {quick_button
, 6, 10, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
451 {quick_button
, 3, 10, 1, 0, N_("&OK"), 0, B_ENTER
, 0, 0, NULL
},
452 {quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, NULL
},
453 {quick_label
, 4, 80, 2, 0, "", 0, 0, 0, 0, NULL
},
462 char histname
[64] = "inp|";
465 if (history_name
!= NULL
&& *history_name
!= '\0') {
466 g_strlcpy (histname
+ 3, history_name
, 61);
467 quick_widgets
[2].histname
= histname
;
470 msglen (text
, &lines
, &cols
);
471 len
= max ((int) strlen (header
), cols
) + 4;
474 /* The special value of def_text is used to identify password boxes
475 and hide characters with "*". Don't save passwords in history! */
476 if (def_text
== INPUT_PASSWORD
) {
477 quick_widgets
[INPUT_INDEX
].value
= 1;
481 quick_widgets
[INPUT_INDEX
].value
= 0;
486 * An attempt to place buttons symmetrically, based on actual i18n
487 * length of the string. It looks nicer with i18n (IMO) - alex
489 quick_widgets
[0].text
= _(quick_widgets
[0].text
);
490 quick_widgets
[1].text
= _(quick_widgets
[1].text
);
491 quick_widgets
[0].relative_x
= len
/ 2 + 4;
492 quick_widgets
[1].relative_x
=
493 len
/ 2 - (strlen (quick_widgets
[1].text
) + 9);
494 quick_widgets
[0].x_divisions
= quick_widgets
[1].x_divisions
= len
;
495 #endif /* ENABLE_NLS */
497 Quick_input
.xlen
= len
;
498 Quick_input
.xpos
= -1;
499 Quick_input
.title
= header
;
500 Quick_input
.help
= help
;
501 Quick_input
.i18n
= 1; /* The dialog is already translated. */
502 p_text
= g_strstrip (g_strdup (text
));
503 quick_widgets
[INPUT_INDEX
+ 1].text
= p_text
;
504 quick_widgets
[INPUT_INDEX
].text
= def_text
;
506 for (i
= 0; i
< 4; i
++)
507 quick_widgets
[i
].y_divisions
= lines
+ 6;
508 Quick_input
.ylen
= lines
+ 6;
510 for (i
= 0; i
< 3; i
++)
511 quick_widgets
[i
].relative_y
+= 2 + lines
;
513 quick_widgets
[INPUT_INDEX
].str_result
= &my_str
;
515 Quick_input
.widgets
= quick_widgets
;
516 ret
= quick_dialog (&Quick_input
);
519 if (ret
!= B_CANCEL
) {
526 * Show input dialog, background safe.
528 * If the arguments "header" and "text" should be translated,
529 * that MUST be done by the caller of these wrappers.
532 input_dialog_help (const char *header
, const char *text
, const char *help
,
533 const char *history_name
, const char *def_text
)
535 #ifdef WITH_BACKGROUND
536 if (we_are_background
)
537 return parent_call_string ((void *) fg_input_dialog_help
, 5,
538 strlen (header
), header
, strlen (text
),
539 text
, strlen (help
), help
,
540 strlen (history_name
), history_name
,
541 strlen (def_text
), def_text
);
543 #endif /* WITH_BACKGROUND */
544 return fg_input_dialog_help (header
, text
, help
, history_name
, def_text
);
547 /* Show input dialog with default help, background safe */
548 char *input_dialog (const char *header
, const char *text
,
549 const char *history_name
, const char *def_text
)
551 return input_dialog_help (header
, text
, "[Input Line Keys]", history_name
, def_text
);
555 input_expand_dialog (const char *header
, const char *text
,
556 const char *history_name
, const char *def_text
)
561 result
= input_dialog (header
, text
, history_name
, def_text
);
563 expanded
= tilde_expand (result
);
574 Cause emacs to enter folding mode for this file: