1 /* Widget based utility functions.
2 Copyright (C) 1994, 1995 the Free Software Foundation
4 Authors: 1994, 1995, 1996 Miguel de Icaza
5 1994, 1995 Radek Doulik
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #include "color.h" /* dialog_colors */
36 #include "key.h" /* mi_getch() */
37 #include "complete.h" /* INPUT_COMPLETE_CD */
38 #include "background.h" /* parent_call */
42 create_listbox_window (int cols
, int lines
, const char *title
, const char *help
)
45 Listbox
*listbox
= g_new (Listbox
, 1);
46 const char *cancel_string
= _("&Cancel");
49 lines
= (lines
> LINES
- 6) ? LINES
- 6 : lines
;
51 if (title
&& (cols
< (len
= strlen (title
) + 2)))
54 /* no &, but 4 spaces around button for brackets and such */
55 if (cols
< (len
= strlen (cancel_string
) + 3))
58 cols
= cols
> COLS
- 6 ? COLS
- 6 : cols
;
59 xpos
= (COLS
- cols
) / 2;
60 ypos
= (LINES
- lines
) / 2 - 2;
62 /* Create components */
64 create_dlg (ypos
, xpos
, lines
+ 6, cols
+ 4, dialog_colors
, NULL
,
65 help
, title
, DLG_CENTER
| DLG_REVERSE
);
67 listbox
->list
= listbox_new (2, 2, cols
, lines
, 0);
69 add_widget (listbox
->dlg
,
70 button_new (lines
+ 3, (cols
/ 2 + 2) - len
/ 2, B_CANCEL
,
71 NORMAL_BUTTON
, const_cast(char *, cancel_string
), 0));
72 add_widget (listbox
->dlg
, listbox
->list
);
77 /* Returns the number of the item selected */
78 int run_listbox (Listbox
*l
)
83 if (l
->dlg
->ret_value
== B_CANCEL
)
93 static Dlg_head
*last_query_dlg
;
95 static int sel_pos
= 0;
97 /* Used to ask questions to the user */
99 query_dialog (const char *header
, const char *text
, int flags
, int count
, ...)
104 WButton
*defbutton
= NULL
;
111 static const int *query_colors
;
113 /* set dialog colors */
115 query_colors
= alarm_colors
;
117 query_colors
= dialog_colors
;
119 if (header
== MSG_ERROR
)
123 va_start (ap
, count
);
124 for (i
= 0; i
< count
; i
++) {
125 char *cp
= va_arg (ap
, char *);
126 win_len
+= strlen (cp
) + 6;
127 if (strchr (cp
, '&') != NULL
)
133 /* count coordinates */
134 cols
= 6 + max (win_len
, max ((int) strlen (header
), msglen (text
, &lines
)));
135 lines
+= 4 + (count
> 0 ? 2 : 0);
136 xpos
= COLS
/ 2 - cols
/ 2;
137 ypos
= LINES
/ 3 - (lines
- 3) / 2;
141 create_dlg (ypos
, xpos
, lines
, cols
, query_colors
, NULL
,
142 "[QueryBox]", header
, DLG_NONE
);
145 cols
= (cols
- win_len
- 2) / 2 + 2;
146 va_start (ap
, count
);
147 for (i
= 0; i
< count
; i
++) {
148 cur_name
= va_arg (ap
, char *);
149 xpos
= strlen (cur_name
) + 6;
150 if (strchr (cur_name
, '&') != NULL
)
154 button_new (lines
- 3, cols
, B_USER
+ i
, NORMAL_BUTTON
,
156 add_widget (query_dlg
, button
);
163 add_widget (query_dlg
, label_new (2, 3, text
));
166 dlg_select_widget (query_dlg
, defbutton
);
168 /* run dialog and make result */
170 switch (query_dlg
->ret_value
) {
174 result
= query_dlg
->ret_value
- B_USER
;
177 /* free used memory */
178 destroy_dlg (query_dlg
);
180 add_widget (query_dlg
, label_new (2, 3, text
));
181 add_widget (query_dlg
,
182 button_new (0, 0, 0, HIDDEN_BUTTON
, "-", 0));
183 last_query_dlg
= query_dlg
;
189 void query_set_sel (int new_sel
)
195 /* Create message dialog */
196 static struct Dlg_head
*
197 do_create_message (int flags
, const char *title
, const char *text
)
202 /* Add empty lines before and after the message */
203 p
= g_strconcat ("\n", text
, "\n", (char *) NULL
);
204 query_dialog (title
, p
, flags
, 0);
214 * Create message dialog. The caller must call dlg_run_done() and
215 * destroy_dlg() to dismiss it. Not safe to call from background.
218 create_message (int flags
, const char *title
, const char *text
, ...)
224 va_start (args
, text
);
225 p
= g_strdup_vprintf (text
, args
);
228 d
= do_create_message (flags
, title
, p
);
236 * Show message dialog. Dismiss it when any key is pressed.
237 * Not safe to call from background.
240 fg_message (int flags
, const char *title
, const char *text
)
244 d
= do_create_message (flags
, title
, text
);
251 /* Show message box from background */
252 #ifdef WITH_BACKGROUND
254 bg_message (int dummy
, int *flags
, char *title
, const char *text
)
256 title
= g_strconcat (_("Background process:"), " ", title
, (char *) NULL
);
257 fg_message (*flags
, title
, text
);
260 #endif /* WITH_BACKGROUND */
263 /* Show message box, background safe */
265 message (int flags
, const char *title
, const char *text
, ...)
271 p
= g_strdup_vprintf (text
, ap
);
274 if (title
== MSG_ERROR
)
277 #ifdef WITH_BACKGROUND
278 if (we_are_background
) {
279 parent_call ((void *) bg_message
, NULL
, 3, sizeof (flags
), &flags
,
280 strlen (title
), title
, strlen (p
), p
);
282 #endif /* WITH_BACKGROUND */
283 fg_message (flags
, title
, p
);
289 /* {{{ Quick dialog routines */
291 #define I18N(x) (do_int && *x ? (x = _(x)): x)
294 quick_dialog_skip (QuickDialog
*qd
, int nskip
)
305 int count
= 0; /* number of quick widgets */
306 int curr_widget
; /* number of the current quick widget */
307 Widget
**widgets
; /* table of corresponding widgets */
313 qd
->title
= _(qd
->title
);
318 dd
= create_dlg (0, 0, qd
->ylen
, qd
->xlen
, dialog_colors
, NULL
,
320 DLG_CENTER
| DLG_TRYUP
| DLG_REVERSE
);
322 dd
= create_dlg (qd
->ypos
, qd
->xpos
, qd
->ylen
, qd
->xlen
,
323 dialog_colors
, NULL
, qd
->help
, qd
->title
,
326 /* We pass this to the callback */
328 dd
->lines
= qd
->ylen
;
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
), 1);
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
432 /* Show dialog, not background safe */
434 fg_input_dialog_help (const char *header
, const char *text
, const char *help
,
435 const char *def_text
)
437 QuickDialog Quick_input
;
438 QuickWidget quick_widgets
[] = {
439 {quick_button
, 6, 10, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
441 {quick_button
, 3, 10, 1, 0, N_("&OK"), 0, B_ENTER
, 0, 0, NULL
},
442 {quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, NULL
},
443 {quick_label
, 4, 80, 2, 0, "", 0, 0, 0, 0, NULL
},
452 char histname
[64] = "inp|";
455 /* we need a unique name for histname because widget.c:history_tool()
456 needs a unique name for each dialog - using the header is ideal */
457 g_strlcpy (histname
+ 3, header
, 61);
458 quick_widgets
[2].histname
= histname
;
460 len
= max ((int) strlen (header
), msglen (text
, &lines
)) + 4;
463 /* The special value of def_text is used to identify password boxes
464 and hide characters with "*". Don't save passwords in history! */
465 if (def_text
== INPUT_PASSWORD
) {
466 quick_widgets
[INPUT_INDEX
].value
= 1;
470 quick_widgets
[INPUT_INDEX
].value
= 0;
475 * An attempt to place buttons symmetrically, based on actual i18n
476 * length of the string. It looks nicer with i18n (IMO) - alex
478 quick_widgets
[0].relative_x
= len
/ 2 + 4;
479 quick_widgets
[1].relative_x
=
480 len
/ 2 - (strlen (_(quick_widgets
[1].text
)) + 9);
481 quick_widgets
[0].x_divisions
= quick_widgets
[1].x_divisions
= len
;
482 #endif /* ENABLE_NLS */
484 Quick_input
.xlen
= len
;
485 Quick_input
.xpos
= -1;
486 Quick_input
.title
= header
;
487 Quick_input
.help
= help
;
488 Quick_input
.i18n
= 0;
489 p_text
= g_strstrip (g_strdup (text
));
490 quick_widgets
[INPUT_INDEX
+ 1].text
= p_text
;
491 quick_widgets
[INPUT_INDEX
].text
= def_text
;
493 for (i
= 0; i
< 4; i
++)
494 quick_widgets
[i
].y_divisions
= lines
+ 6;
495 Quick_input
.ylen
= lines
+ 6;
497 for (i
= 0; i
< 3; i
++)
498 quick_widgets
[i
].relative_y
+= 2 + lines
;
500 quick_widgets
[INPUT_INDEX
].str_result
= &my_str
;
502 Quick_input
.widgets
= quick_widgets
;
503 ret
= quick_dialog (&Quick_input
);
506 if (ret
!= B_CANCEL
) {
512 /* Show input dialog, background safe */
514 input_dialog_help (const char *header
, const char *text
, const char *help
, const char *def_text
)
516 #ifdef WITH_BACKGROUND
517 if (we_are_background
)
518 return parent_call_string ((void *) fg_input_dialog_help
, 4,
519 strlen (header
), header
, strlen (text
),
520 text
, strlen (help
), help
,
521 strlen (def_text
), def_text
);
523 #endif /* WITH_BACKGROUND */
524 return fg_input_dialog_help (header
, text
, help
, def_text
);
527 /* Show input dialog with default help, background safe */
528 char *input_dialog (const char *header
, const char *text
, const char *def_text
)
530 return input_dialog_help (header
, text
, "[Input Line Keys]", def_text
);
534 input_expand_dialog (const char *header
, const char *text
, const char *def_text
)
539 result
= input_dialog (header
, text
, def_text
);
541 expanded
= tilde_expand (result
);
552 Cause emacs to enter folding mode for this file: