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
36 #include "lib/global.h"
37 #include "lib/tty/tty.h"
38 #include "lib/tty/key.h" /* tty_getch() */
39 #include "lib/skin.h" /* INPUT_COLOR */
40 #include "lib/strutil.h"
45 #include "background.h" /* parent_call */
49 create_listbox_window_centered (int center_y
, int center_x
, int lines
, int cols
,
50 const char *title
, const char *help
)
52 const int listbox_colors
[DLG_COLOR_NUM
] =
66 lines
= min (lines
, LINES
- 6);
69 len
= str_term_width1 (title
) + 4;
70 cols
= max (cols
, len
);
73 cols
= min (cols
, COLS
- 6);
76 if ((center_y
< 0) || (center_x
< 0)) {
87 if (ypos
+ lines
>= LINES
)
88 ypos
= LINES
- lines
- space
;
92 if (xpos
+ cols
>= COLS
)
93 xpos
= COLS
- cols
- space
;
97 listbox
= g_new (Listbox
, 1);
100 create_dlg (ypos
, xpos
, lines
+ space
, cols
+ space
,
101 listbox_colors
, NULL
,
102 help
, title
, DLG_REVERSE
| DLG_TRYUP
);
104 listbox
->list
= listbox_new (2, 2, lines
, cols
, FALSE
, NULL
);
105 add_widget (listbox
->dlg
, listbox
->list
);
111 create_listbox_window (int lines
, int cols
, const char *title
, const char *help
)
113 return create_listbox_window_centered (-1, -1, lines
, cols
, title
, help
);
116 /* Returns the number of the item selected */
118 run_listbox (Listbox
*l
)
122 if (run_dlg (l
->dlg
) != B_CANCEL
)
124 destroy_dlg (l
->dlg
);
129 /* default query callback, used to reposition query */
131 default_query_callback (Dlg_head
*h
, Widget
*sender
,
132 dlg_msg_t msg
, int parm
, void *data
)
137 int xpos
= COLS
/ 2 - h
->cols
/ 2;
138 int ypos
= LINES
/ 3 - (h
->lines
- 3) / 2;
141 dlg_set_position (h
, ypos
, xpos
, ypos
+ h
->lines
, xpos
+ h
->cols
);
146 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
150 static Dlg_head
*last_query_dlg
;
152 static int sel_pos
= 0;
154 /* Used to ask questions to the user */
156 query_dialog (const char *header
, const char *text
, int flags
, int count
, ...)
161 WButton
*defbutton
= NULL
;
167 const int *query_colors
= (flags
& D_ERROR
) ?
168 alarm_colors
: dialog_colors
;
170 if (header
== MSG_ERROR
)
174 va_start (ap
, count
);
175 for (i
= 0; i
< count
; i
++) {
176 char *cp
= va_arg (ap
, char *);
177 win_len
+= str_term_width1 (cp
) + 6;
178 if (strchr (cp
, '&') != NULL
)
184 /* count coordinates */
185 str_msg_term_size (text
, &lines
, &cols
);
186 cols
= 6 + max (win_len
, max (str_term_width1 (header
), cols
));
187 lines
+= 4 + (count
> 0 ? 2 : 0);
191 create_dlg (0, 0, lines
, cols
, query_colors
, default_query_callback
,
192 "[QueryBox]", header
, DLG_NONE
);
195 cols
= (cols
- win_len
- 2) / 2 + 2;
196 va_start (ap
, count
);
197 for (i
= 0; i
< count
; i
++) {
200 cur_name
= va_arg (ap
, char *);
201 xpos
= str_term_width1 (cur_name
) + 6;
202 if (strchr (cur_name
, '&') != NULL
)
206 button_new (lines
- 3, cols
, B_USER
+ i
, NORMAL_BUTTON
,
208 add_widget (query_dlg
, button
);
215 add_widget (query_dlg
, label_new (2, 3, text
));
217 /* do resize before running and selecting any widget */
218 default_query_callback (query_dlg
, NULL
, DLG_RESIZE
, 0, NULL
);
221 dlg_select_widget (defbutton
);
223 /* run dialog and make result */
224 switch (run_dlg (query_dlg
)) {
228 result
= query_dlg
->ret_value
- B_USER
;
231 /* free used memory */
232 destroy_dlg (query_dlg
);
234 add_widget (query_dlg
, label_new (2, 3, text
));
235 add_widget (query_dlg
,
236 button_new (0, 0, 0, HIDDEN_BUTTON
, "-", 0));
237 last_query_dlg
= query_dlg
;
243 void query_set_sel (int new_sel
)
249 /* Create message dialog */
250 static struct Dlg_head
*
251 do_create_message (int flags
, const char *title
, const char *text
)
256 /* Add empty lines before and after the message */
257 p
= g_strconcat ("\n", text
, "\n", (char *) NULL
);
258 query_dialog (title
, p
, flags
, 0);
261 /* do resize before initing and running */
262 default_query_callback (d
, NULL
, DLG_RESIZE
, 0, NULL
);
272 * Create message dialog. The caller must call dlg_run_done() and
273 * destroy_dlg() to dismiss it. Not safe to call from background.
276 create_message (int flags
, const char *title
, const char *text
, ...)
282 va_start (args
, text
);
283 p
= g_strdup_vprintf (text
, args
);
286 d
= do_create_message (flags
, title
, p
);
294 * Show message dialog. Dismiss it when any key is pressed.
295 * Not safe to call from background.
298 fg_message (int flags
, const char *title
, const char *text
)
302 d
= do_create_message (flags
, title
, text
);
309 /* Show message box from background */
310 #ifdef WITH_BACKGROUND
312 bg_message (int dummy
, int *flags
, char *title
, const char *text
)
315 title
= g_strconcat (_("Background process:"), " ", title
, (char *) NULL
);
316 fg_message (*flags
, title
, text
);
319 #endif /* WITH_BACKGROUND */
322 /* Show message box, background safe */
324 message (int flags
, const char *title
, const char *text
, ...)
330 void (*f
) (int, int *, char *, const char *);
334 p
= g_strdup_vprintf (text
, ap
);
337 if (title
== MSG_ERROR
)
340 #ifdef WITH_BACKGROUND
341 if (we_are_background
) {
343 parent_call (func
.p
, NULL
, 3, sizeof (flags
), &flags
,
344 strlen (title
), title
, strlen (p
), p
);
346 #endif /* WITH_BACKGROUND */
347 fg_message (flags
, title
, p
);
353 /* {{{ Quick dialog routines */
357 quick_dialog_skip (QuickDialog
*qd
, int nskip
)
360 #define I18N(x) (x = !qd->i18n && x && *x ? _(x): x)
362 #define I18N(x) (x = x)
372 if ((qd
->xpos
== -1) || (qd
->ypos
== -1))
373 dd
= create_dlg (0, 0, qd
->ylen
, qd
->xlen
,
374 dialog_colors
, NULL
, qd
->help
, qd
->title
,
375 DLG_CENTER
| DLG_TRYUP
| DLG_REVERSE
);
377 dd
= create_dlg (qd
->ypos
, qd
->xpos
, qd
->ylen
, qd
->xlen
,
378 dialog_colors
, NULL
, qd
->help
, qd
->title
,
381 for (qw
= qd
->widgets
; qw
->widget_type
!= quick_end
; qw
++) {
385 xpos
= (qd
->xlen
* qw
->relative_x
) / qw
->x_divisions
;
386 ypos
= (qd
->ylen
* qw
->relative_y
) / qw
->y_divisions
;
388 switch (qw
->widget_type
) {
390 qw
->widget
= (Widget
*) check_new (ypos
, xpos
, *qw
->u
.checkbox
.state
, I18N (qw
->u
.checkbox
.text
));
394 qw
->widget
= (Widget
*) button_new (ypos
, xpos
, qw
->u
.button
.action
,
395 (qw
->u
.button
.action
== B_ENTER
) ? DEFPUSH_BUTTON
: NORMAL_BUTTON
,
396 I18N (qw
->u
.button
.text
), qw
->u
.button
.callback
);
400 in
= input_new (ypos
, xpos
, INPUT_COLOR
, qw
->u
.input
.len
,
401 qw
->u
.input
.text
, qw
->u
.input
.histname
, INPUT_COMPLETE_DEFAULT
);
402 in
->is_password
= (qw
->u
.input
.flags
== 1);
404 if ((qw
->u
.input
.flags
& 2) != 0)
405 in
->completion_flags
|= INPUT_COMPLETE_CD
;
406 qw
->widget
= (Widget
*) in
;
407 *qw
->u
.input
.result
= NULL
;
411 qw
->widget
= (Widget
*) label_new (ypos
, xpos
, I18N (qw
->u
.label
.text
));
419 /* create the copy of radio_items to avoid mwmory leak */
420 items
= g_new0 (char *, qw
->u
.radio
.count
+ 1);
423 for (i
= 0; i
< qw
->u
.radio
.count
; i
++)
424 items
[i
] = g_strdup (_(qw
->u
.radio
.items
[i
]));
426 for (i
= 0; i
< qw
->u
.radio
.count
; i
++)
427 items
[i
] = g_strdup (qw
->u
.radio
.items
[i
]);
429 r
= radio_new (ypos
, xpos
, qw
->u
.radio
.count
, (const char **) items
);
430 r
->pos
= r
->sel
= *qw
->u
.radio
.value
;
431 qw
->widget
= (Widget
*) r
;
438 fprintf (stderr
, "QuickWidget: unknown widget type\n");
442 add_widget (dd
, qw
->widget
);
446 dd
->current
= dd
->current
->next
;
448 return_val
= run_dlg (dd
);
450 /* Get the data if we found something interesting */
451 if (return_val
!= B_CANCEL
) {
452 for (qw
= qd
->widgets
; qw
->widget_type
!= quick_end
; qw
++) {
453 switch (qw
->widget_type
) {
455 *qw
->u
.checkbox
.state
= ((WCheck
*) qw
->widget
)->state
& C_BOOL
;
459 if ((qw
->u
.input
.flags
& 2) != 0)
460 *qw
->u
.input
.result
= tilde_expand (((WInput
*) qw
->widget
)->buffer
);
462 *qw
->u
.input
.result
= g_strdup (((WInput
*) qw
->widget
)->buffer
);
466 *qw
->u
.radio
.value
= ((WRadio
*) qw
->widget
)->sel
;
482 quick_dialog (QuickDialog
*qd
)
484 return quick_dialog_skip (qd
, 0);
489 /* {{{ Input routines */
492 * Show dialog, not background safe.
494 * If the arguments "header" and "text" should be translated,
495 * that MUST be done by the caller of fg_input_dialog_help().
497 * The argument "history_name" holds the name of a section
498 * in the history file. Data entered in the input field of
499 * the dialog box will be stored there.
503 fg_input_dialog_help (const char *header
, const char *text
, const char *help
,
504 const char *history_name
, const char *def_text
)
508 QuickWidget quick_widgets
[] = {
509 /* 0 */ QUICK_BUTTON (6, 10, 1, 0, N_("&Cancel"), B_CANCEL
, NULL
),
510 /* 1 */ QUICK_BUTTON (3, 10, 1, 0, N_("&OK"), B_ENTER
, NULL
),
511 /* 2 */ QUICK_INPUT (3, 80, 0, 0, def_text
, 58, 0, NULL
, &my_str
),
512 /* 3 */ QUICK_LABEL (3, 80, 2, 0, ""),
516 char histname
[64] = "inp|";
523 if (history_name
!= NULL
&& *history_name
!= '\0') {
524 g_strlcpy (histname
+ 3, history_name
, sizeof (histname
) - 3);
525 quick_widgets
[2].u
.input
.histname
= histname
;
528 msglen (text
, &lines
, &cols
);
529 len
= max (max (str_term_width1 (header
), cols
) + 4, 64);
531 /* The special value of def_text is used to identify password boxes
532 and hide characters with "*". Don't save passwords in history! */
533 if (def_text
== INPUT_PASSWORD
) {
534 quick_widgets
[2].u
.input
.flags
= 1;
536 quick_widgets
[2].u
.input
.text
= "";
541 * An attempt to place buttons symmetrically, based on actual i18n
542 * length of the string. It looks nicer with i18n (IMO) - alex
544 quick_widgets
[0].u
.button
.text
= _(quick_widgets
[0].u
.button
.text
);
545 quick_widgets
[1].u
.button
.text
= _(quick_widgets
[1].u
.button
.text
);
546 quick_widgets
[0].relative_x
= len
/ 2 + 4;
547 quick_widgets
[1].relative_x
=
548 len
/ 2 - (str_term_width1 (quick_widgets
[1].u
.button
.text
) + 9);
549 quick_widgets
[0].x_divisions
= quick_widgets
[1].x_divisions
= len
;
550 #endif /* ENABLE_NLS */
552 p_text
= g_strstrip (g_strdup (text
));
553 quick_widgets
[3].u
.label
.text
= p_text
;
556 QuickDialog Quick_input
=
558 len
, lines
+ 6, -1, -1, header
,
559 help
, quick_widgets
, TRUE
562 for (i
= 0; i
< 4; i
++) {
563 quick_widgets
[i
].x_divisions
= Quick_input
.xlen
;
564 quick_widgets
[i
].y_divisions
= Quick_input
.ylen
;
567 for (i
= 0; i
< 3; i
++)
568 quick_widgets
[i
].relative_y
+= 2 + lines
;
570 /* input line length */
571 quick_widgets
[2].u
.input
.len
= Quick_input
.xlen
- 6;
573 ret
= quick_dialog (&Quick_input
);
578 return (ret
!= B_CANCEL
) ? my_str
: NULL
;
582 * Show input dialog, background safe.
584 * If the arguments "header" and "text" should be translated,
585 * that MUST be done by the caller of these wrappers.
588 input_dialog_help (const char *header
, const char *text
, const char *help
,
589 const char *history_name
, const char *def_text
)
593 char * (*f
) (const char *, const char *, const char *,
594 const char *, const char *);
596 #ifdef WITH_BACKGROUND
597 if (we_are_background
)
599 func
.f
= fg_input_dialog_help
;
600 return parent_call_string (func
.p
, 5,
601 strlen (header
), header
, strlen (text
),
602 text
, strlen (help
), help
,
603 strlen (history_name
), history_name
,
604 strlen (def_text
), def_text
);
607 #endif /* WITH_BACKGROUND */
608 return fg_input_dialog_help (header
, text
, help
, history_name
, def_text
);
611 /* Show input dialog with default help, background safe */
612 char *input_dialog (const char *header
, const char *text
,
613 const char *history_name
, const char *def_text
)
615 return input_dialog_help (header
, text
, "[Input Line Keys]", history_name
, def_text
);
619 input_expand_dialog (const char *header
, const char *text
,
620 const char *history_name
, const char *def_text
)
625 result
= input_dialog (header
, text
, history_name
, def_text
);
627 expanded
= tilde_expand (result
);
638 Cause emacs to enter folding mode for this file: