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.
27 * \brief Source: widget based utility functions
38 #include "../src/tty/tty.h"
39 #include "../src/tty/color.h" /* INPUT_COLOR */
40 #include "../src/tty/key.h" /* tty_getch() */
45 #include "background.h" /* parent_call */
50 create_listbox_window_delta (int delta_x
, int delta_y
, int cols
, int lines
,
51 const char *title
, const char *help
)
53 const int listbox_colors
[4] =
65 lines
= min (lines
, LINES
- 6);
68 len
= str_term_width1 (title
) + 4;
69 cols
= max (cols
, len
);
72 cols
= min (cols
, COLS
- 6);
75 xpos
= (COLS
- cols
+ delta_x
) / 2;
76 ypos
= (LINES
- lines
+ delta_y
) / 2 - 2;
78 listbox
= g_new (Listbox
, 1);
81 create_dlg (ypos
, xpos
, lines
+ 4, cols
+ 4, listbox_colors
, NULL
,
82 help
, title
, DLG_REVERSE
);
84 listbox
->list
= listbox_new (2, 2, lines
, cols
, NULL
);
85 add_widget (listbox
->dlg
, listbox
->list
);
91 create_listbox_window (int cols
, int lines
, const char *title
, const char *help
)
93 return create_listbox_window_delta (0, 0, cols
, lines
, title
, help
);
96 /* Returns the number of the item selected */
97 int run_listbox (Listbox
*l
)
102 if (l
->dlg
->ret_value
== B_CANCEL
)
106 destroy_dlg (l
->dlg
);
112 static Dlg_head
*last_query_dlg
;
114 static int sel_pos
= 0;
116 /* Used to ask questions to the user */
118 query_dialog (const char *header
, const char *text
, int flags
, int count
, ...)
123 WButton
*defbutton
= NULL
;
130 static const int *query_colors
;
132 /* set dialog colors */
134 query_colors
= alarm_colors
;
136 query_colors
= dialog_colors
;
138 if (header
== MSG_ERROR
)
142 va_start (ap
, count
);
143 for (i
= 0; i
< count
; i
++) {
144 char *cp
= va_arg (ap
, char *);
145 win_len
+= str_term_width1 (cp
) + 6;
146 if (strchr (cp
, '&') != NULL
)
152 /* count coordinates */
153 str_msg_term_size (text
, &lines
, &cols
);
154 cols
= 6 + max (win_len
, max (str_term_width1 (header
), cols
));
155 lines
+= 4 + (count
> 0 ? 2 : 0);
156 xpos
= COLS
/ 2 - cols
/ 2;
157 ypos
= LINES
/ 3 - (lines
- 3) / 2;
161 create_dlg (ypos
, xpos
, lines
, cols
, query_colors
, NULL
,
162 "[QueryBox]", header
, DLG_NONE
);
165 cols
= (cols
- win_len
- 2) / 2 + 2;
166 va_start (ap
, count
);
167 for (i
= 0; i
< count
; i
++) {
168 cur_name
= va_arg (ap
, char *);
169 xpos
= str_term_width1 (cur_name
) + 6;
170 if (strchr (cur_name
, '&') != NULL
)
174 button_new (lines
- 3, cols
, B_USER
+ i
, NORMAL_BUTTON
,
176 add_widget (query_dlg
, button
);
183 add_widget (query_dlg
, label_new (2, 3, text
));
186 dlg_select_widget (defbutton
);
188 /* run dialog and make result */
190 switch (query_dlg
->ret_value
) {
194 result
= query_dlg
->ret_value
- B_USER
;
197 /* free used memory */
198 destroy_dlg (query_dlg
);
200 add_widget (query_dlg
, label_new (2, 3, text
));
201 add_widget (query_dlg
,
202 button_new (0, 0, 0, HIDDEN_BUTTON
, "-", 0));
203 last_query_dlg
= query_dlg
;
209 void query_set_sel (int new_sel
)
215 /* Create message dialog */
216 static struct Dlg_head
*
217 do_create_message (int flags
, const char *title
, const char *text
)
222 /* Add empty lines before and after the message */
223 p
= g_strconcat ("\n", text
, "\n", (char *) NULL
);
224 query_dialog (title
, p
, flags
, 0);
234 * Create message dialog. The caller must call dlg_run_done() and
235 * destroy_dlg() to dismiss it. Not safe to call from background.
238 create_message (int flags
, const char *title
, const char *text
, ...)
244 va_start (args
, text
);
245 p
= g_strdup_vprintf (text
, args
);
248 d
= do_create_message (flags
, title
, p
);
256 * Show message dialog. Dismiss it when any key is pressed.
257 * Not safe to call from background.
260 fg_message (int flags
, const char *title
, const char *text
)
264 d
= do_create_message (flags
, title
, text
);
271 /* Show message box from background */
272 #ifdef WITH_BACKGROUND
274 bg_message (int dummy
, int *flags
, char *title
, const char *text
)
277 title
= g_strconcat (_("Background process:"), " ", title
, (char *) NULL
);
278 fg_message (*flags
, title
, text
);
281 #endif /* WITH_BACKGROUND */
284 /* Show message box, background safe */
286 message (int flags
, const char *title
, const char *text
, ...)
292 void (*f
) (int, int *, char *, const char *);
296 p
= g_strdup_vprintf (text
, ap
);
299 if (title
== MSG_ERROR
)
302 #ifdef WITH_BACKGROUND
303 if (we_are_background
) {
305 parent_call (func
.p
, NULL
, 3, sizeof (flags
), &flags
,
306 strlen (title
), title
, strlen (p
), p
);
308 #endif /* WITH_BACKGROUND */
309 fg_message (flags
, title
, p
);
315 /* {{{ Quick dialog routines */
317 #define I18N(x) (do_int && *x ? (x = _(x)): x)
320 quick_dialog_skip (QuickDialog
*qd
, int nskip
)
336 qd
->title
= _(qd
->title
);
341 dd
= create_dlg (0, 0, qd
->ylen
, qd
->xlen
, dialog_colors
, NULL
,
343 DLG_CENTER
| DLG_TRYUP
| DLG_REVERSE
);
345 dd
= create_dlg (qd
->ypos
, qd
->xpos
, qd
->ylen
, qd
->xlen
,
346 dialog_colors
, NULL
, qd
->help
, qd
->title
,
349 for (qw
= qd
->widgets
; qw
->widget_type
; qw
++) {
350 xpos
= (qd
->xlen
* qw
->relative_x
) / qw
->x_divisions
;
351 ypos
= (qd
->ylen
* qw
->relative_y
) / qw
->y_divisions
;
353 switch (qw
->widget_type
) {
355 qw
->widget
= (Widget
*)check_new (ypos
, xpos
, *qw
->result
, I18N (qw
->text
));
359 r
= radio_new (ypos
, xpos
, qw
->hotkey_pos
, const_cast(const char **, qw
->str_result
));
360 r
->pos
= r
->sel
= qw
->value
;
361 qw
->widget
= (Widget
*)r
;
365 qw
->widget
= (Widget
*)
366 button_new (ypos
, xpos
, qw
->value
,
368 B_ENTER
) ? DEFPUSH_BUTTON
: NORMAL_BUTTON
,
369 I18N (qw
->text
), (bcback
) qw
->cb
);
372 /* We use the hotkey pos as the field length */
375 input_new (ypos
, xpos
, INPUT_COLOR
, qw
->hotkey_pos
,
376 qw
->text
, qw
->histname
, INPUT_COMPLETE_DEFAULT
);
377 input
->is_password
= qw
->value
== 1;
380 input
->completion_flags
|= INPUT_COMPLETE_CD
;
381 qw
->widget
= (Widget
*)input
;
385 qw
->widget
= (Widget
*)label_new (ypos
, xpos
, I18N (qw
->text
));
390 fprintf (stderr
, "QuickWidget: unknown widget type\n");
393 add_widget (dd
, qw
->widget
);
397 dd
->current
= dd
->current
->next
;
401 /* Get the data if we found something interesting */
402 if (dd
->ret_value
!= B_CANCEL
) {
403 for (qw
= qd
->widgets
; qw
->widget_type
; qw
++) {
404 Widget
*w
= qw
->widget
;
406 switch (qw
->widget_type
) {
408 *qw
->result
= ((WCheck
*) w
)->state
& C_BOOL
;
412 *qw
->result
= ((WRadio
*) w
)->sel
;
418 tilde_expand (((WInput
*) w
)->buffer
);
420 *qw
->str_result
= g_strdup (((WInput
*) w
)->buffer
);
425 return_val
= dd
->ret_value
;
431 int quick_dialog (QuickDialog
*qd
)
433 return quick_dialog_skip (qd
, 0);
438 /* {{{ Input routines */
440 #define INPUT_INDEX 2
443 * Show dialog, not background safe.
445 * If the arguments "header" and "text" should be translated,
446 * that MUST be done by the caller of fg_input_dialog_help().
448 * The argument "history_name" holds the name of a section
449 * in the history file. Data entered in the input field of
450 * the dialog box will be stored there.
454 fg_input_dialog_help (const char *header
, const char *text
, const char *help
,
455 const char *history_name
, const char *def_text
)
457 QuickDialog Quick_input
;
458 QuickWidget quick_widgets
[] = {
459 {quick_button
, 6, 10, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
461 {quick_button
, 3, 10, 1, 0, N_("&OK"), 0, B_ENTER
, 0, 0, NULL
, NULL
, NULL
},
462 {quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, NULL
, NULL
, NULL
},
463 {quick_label
, 4, 80, 2, 0, "", 0, 0, 0, 0, NULL
, NULL
, NULL
},
472 char histname
[64] = "inp|";
475 if (history_name
!= NULL
&& *history_name
!= '\0') {
476 g_strlcpy (histname
+ 3, history_name
, 61);
477 quick_widgets
[2].histname
= histname
;
480 msglen (text
, &lines
, &cols
);
481 len
= max (str_term_width1 (header
), cols
) + 4;
484 /* The special value of def_text is used to identify password boxes
485 and hide characters with "*". Don't save passwords in history! */
486 if (def_text
== INPUT_PASSWORD
) {
487 quick_widgets
[INPUT_INDEX
].value
= 1;
491 quick_widgets
[INPUT_INDEX
].value
= 0;
496 * An attempt to place buttons symmetrically, based on actual i18n
497 * length of the string. It looks nicer with i18n (IMO) - alex
499 quick_widgets
[0].text
= _(quick_widgets
[0].text
);
500 quick_widgets
[1].text
= _(quick_widgets
[1].text
);
501 quick_widgets
[0].relative_x
= len
/ 2 + 4;
502 quick_widgets
[1].relative_x
=
503 len
/ 2 - (str_term_width1 (quick_widgets
[1].text
) + 9);
504 quick_widgets
[0].x_divisions
= quick_widgets
[1].x_divisions
= len
;
505 #endif /* ENABLE_NLS */
507 Quick_input
.xlen
= len
;
508 Quick_input
.xpos
= -1;
509 Quick_input
.title
= header
;
510 Quick_input
.help
= help
;
511 Quick_input
.i18n
= 1; /* The dialog is already translated. */
512 p_text
= g_strstrip (g_strdup (text
));
513 quick_widgets
[INPUT_INDEX
+ 1].text
= p_text
;
514 quick_widgets
[INPUT_INDEX
].text
= def_text
;
516 for (i
= 0; i
< 4; i
++)
517 quick_widgets
[i
].y_divisions
= lines
+ 6;
518 Quick_input
.ylen
= lines
+ 6;
520 for (i
= 0; i
< 3; i
++)
521 quick_widgets
[i
].relative_y
+= 2 + lines
;
523 quick_widgets
[INPUT_INDEX
].str_result
= &my_str
;
525 Quick_input
.widgets
= quick_widgets
;
526 ret
= quick_dialog (&Quick_input
);
529 if (ret
!= B_CANCEL
) {
536 * Show input dialog, background safe.
538 * If the arguments "header" and "text" should be translated,
539 * that MUST be done by the caller of these wrappers.
542 input_dialog_help (const char *header
, const char *text
, const char *help
,
543 const char *history_name
, const char *def_text
)
547 char * (*f
) (const char *, const char *, const char *,
548 const char *, const char *);
550 #ifdef WITH_BACKGROUND
551 if (we_are_background
)
553 func
.f
= fg_input_dialog_help
;
554 return parent_call_string (func
.p
, 5,
555 strlen (header
), header
, strlen (text
),
556 text
, strlen (help
), help
,
557 strlen (history_name
), history_name
,
558 strlen (def_text
), def_text
);
561 #endif /* WITH_BACKGROUND */
562 return fg_input_dialog_help (header
, text
, help
, history_name
, def_text
);
565 /* Show input dialog with default help, background safe */
566 char *input_dialog (const char *header
, const char *text
,
567 const char *history_name
, const char *def_text
)
569 return input_dialog_help (header
, text
, "[Input Line Keys]", history_name
, def_text
);
573 input_expand_dialog (const char *header
, const char *text
,
574 const char *history_name
, const char *def_text
)
579 result
= input_dialog (header
, text
, history_name
, def_text
);
581 expanded
= tilde_expand (result
);
592 Cause emacs to enter folding mode for this file: