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 dlg_colors_t listbox_colors
= {
66 lines
= min (lines
, LINES
- 6);
70 len
= str_term_width1 (title
) + 4;
71 cols
= max (cols
, len
);
74 cols
= min (cols
, COLS
- 6);
77 if ((center_y
< 0) || (center_x
< 0))
91 if (ypos
+ lines
>= LINES
)
92 ypos
= LINES
- lines
- space
;
96 if (xpos
+ cols
>= COLS
)
97 xpos
= COLS
- cols
- space
;
101 listbox
= g_new (Listbox
, 1);
104 create_dlg (TRUE
, ypos
, xpos
, lines
+ space
, cols
+ space
,
105 listbox_colors
, NULL
, help
, title
, DLG_REVERSE
| DLG_TRYUP
);
107 listbox
->list
= listbox_new (2, 2, lines
, cols
, FALSE
, NULL
);
108 add_widget (listbox
->dlg
, listbox
->list
);
114 create_listbox_window (int lines
, int cols
, const char *title
, const char *help
)
116 return create_listbox_window_centered (-1, -1, lines
, cols
, title
, help
);
119 /* Returns the number of the item selected */
121 run_listbox (Listbox
* l
)
125 if (run_dlg (l
->dlg
) != B_CANCEL
)
127 destroy_dlg (l
->dlg
);
132 /* default query callback, used to reposition query */
134 default_query_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
140 int xpos
= COLS
/ 2 - h
->cols
/ 2;
141 int ypos
= LINES
/ 3 - (h
->lines
- 3) / 2;
144 dlg_set_position (h
, ypos
, xpos
, ypos
+ h
->lines
, xpos
+ h
->cols
);
149 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
153 static Dlg_head
*last_query_dlg
;
155 static int sel_pos
= 0;
157 /* Used to ask questions to the user */
159 query_dialog (const char *header
, const char *text
, int flags
, int count
, ...)
164 WButton
*defbutton
= NULL
;
170 const int *query_colors
= (flags
& D_ERROR
) ? alarm_colors
: dialog_colors
;
172 if (header
== MSG_ERROR
)
177 va_start (ap
, count
);
178 for (i
= 0; i
< count
; i
++)
180 char *cp
= va_arg (ap
, char *);
181 win_len
+= str_term_width1 (cp
) + 6;
182 if (strchr (cp
, '&') != NULL
)
188 /* count coordinates */
189 str_msg_term_size (text
, &lines
, &cols
);
190 cols
= 6 + max (win_len
, max (str_term_width1 (header
), cols
));
191 lines
+= 4 + (count
> 0 ? 2 : 0);
195 create_dlg (TRUE
, 0, 0, lines
, cols
, query_colors
, default_query_callback
,
196 "[QueryBox]", header
, DLG_NONE
);
200 cols
= (cols
- win_len
- 2) / 2 + 2;
201 va_start (ap
, count
);
202 for (i
= 0; i
< count
; i
++)
206 cur_name
= va_arg (ap
, char *);
207 xpos
= str_term_width1 (cur_name
) + 6;
208 if (strchr (cur_name
, '&') != NULL
)
211 button
= button_new (lines
- 3, cols
, B_USER
+ i
, NORMAL_BUTTON
, cur_name
, 0);
212 add_widget (query_dlg
, button
);
219 add_widget (query_dlg
, label_new (2, 3, text
));
221 /* do resize before running and selecting any widget */
222 default_query_callback (query_dlg
, NULL
, DLG_RESIZE
, 0, NULL
);
225 dlg_select_widget (defbutton
);
227 /* run dialog and make result */
228 switch (run_dlg (query_dlg
))
233 result
= query_dlg
->ret_value
- B_USER
;
236 /* free used memory */
237 destroy_dlg (query_dlg
);
241 add_widget (query_dlg
, label_new (2, 3, text
));
242 add_widget (query_dlg
, button_new (0, 0, 0, HIDDEN_BUTTON
, "-", 0));
243 last_query_dlg
= query_dlg
;
250 query_set_sel (int new_sel
)
256 /* Create message dialog */
257 static struct Dlg_head
*
258 do_create_message (int flags
, const char *title
, const char *text
)
263 /* Add empty lines before and after the message */
264 p
= g_strconcat ("\n", text
, "\n", (char *) NULL
);
265 query_dialog (title
, p
, flags
, 0);
268 /* do resize before initing and running */
269 default_query_callback (d
, NULL
, DLG_RESIZE
, 0, NULL
);
279 * Create message dialog. The caller must call dlg_run_done() and
280 * destroy_dlg() to dismiss it. Not safe to call from background.
283 create_message (int flags
, const char *title
, const char *text
, ...)
289 va_start (args
, text
);
290 p
= g_strdup_vprintf (text
, args
);
293 d
= do_create_message (flags
, title
, p
);
301 * Show message dialog. Dismiss it when any key is pressed.
302 * Not safe to call from background.
305 fg_message (int flags
, const char *title
, const char *text
)
309 d
= do_create_message (flags
, title
, text
);
316 /* Show message box from background */
317 #ifdef WITH_BACKGROUND
319 bg_message (int dummy
, int *flags
, char *title
, const char *text
)
322 title
= g_strconcat (_("Background process:"), " ", title
, (char *) NULL
);
323 fg_message (*flags
, title
, text
);
326 #endif /* WITH_BACKGROUND */
329 /* Show message box, background safe */
331 message (int flags
, const char *title
, const char *text
, ...)
338 void (*f
) (int, int *, char *, const char *);
342 p
= g_strdup_vprintf (text
, ap
);
345 if (title
== MSG_ERROR
)
348 #ifdef WITH_BACKGROUND
349 if (we_are_background
)
352 parent_call (func
.p
, NULL
, 3, sizeof (flags
), &flags
, strlen (title
), title
, strlen (p
), p
);
355 #endif /* WITH_BACKGROUND */
356 fg_message (flags
, title
, p
);
362 /* {{{ Quick dialog routines */
366 quick_dialog_skip (QuickDialog
* qd
, int nskip
)
369 #define I18N(x) (x = !qd->i18n && x && *x ? _(x): x)
371 #define I18N(x) (x = x)
381 if ((qd
->xpos
== -1) || (qd
->ypos
== -1))
382 dd
= create_dlg (TRUE
, 0, 0, qd
->ylen
, qd
->xlen
,
383 dialog_colors
, qd
->callback
, qd
->help
, qd
->title
,
384 DLG_CENTER
| DLG_TRYUP
| DLG_REVERSE
);
386 dd
= create_dlg (TRUE
, qd
->ypos
, qd
->xpos
, qd
->ylen
, qd
->xlen
,
387 dialog_colors
, qd
->callback
, qd
->help
, qd
->title
, DLG_REVERSE
);
389 for (qw
= qd
->widgets
; qw
->widget_type
!= quick_end
; qw
++)
394 xpos
= (qd
->xlen
* qw
->relative_x
) / qw
->x_divisions
;
395 ypos
= (qd
->ylen
* qw
->relative_y
) / qw
->y_divisions
;
397 switch (qw
->widget_type
)
401 (Widget
*) check_new (ypos
, xpos
, *qw
->u
.checkbox
.state
,
402 I18N (qw
->u
.checkbox
.text
));
406 qw
->widget
= (Widget
*) button_new (ypos
, xpos
, qw
->u
.button
.action
,
407 (qw
->u
.button
.action
==
408 B_ENTER
) ? DEFPUSH_BUTTON
: NORMAL_BUTTON
,
409 I18N (qw
->u
.button
.text
), qw
->u
.button
.callback
);
413 in
= input_new (ypos
, xpos
, input_get_default_colors(),
414 qw
->u
.input
.len
, qw
->u
.input
.text
, qw
->u
.input
.histname
,
415 INPUT_COMPLETE_DEFAULT
);
416 in
->is_password
= (qw
->u
.input
.flags
== 1);
417 if ((qw
->u
.input
.flags
& 2) != 0)
418 in
->completion_flags
|= INPUT_COMPLETE_CD
;
419 qw
->widget
= (Widget
*) in
;
420 *qw
->u
.input
.result
= NULL
;
424 qw
->widget
= (Widget
*) label_new (ypos
, xpos
, I18N (qw
->u
.label
.text
));
428 qw
->widget
= (Widget
*) groupbox_new (ypos
, xpos
,
429 qw
->u
.groupbox
.height
,
430 qw
->u
.groupbox
.width
,
431 I18N (qw
->u
.groupbox
.title
));
439 /* create the copy of radio_items to avoid mwmory leak */
440 items
= g_new0 (char *, qw
->u
.radio
.count
+ 1);
443 for (i
= 0; i
< qw
->u
.radio
.count
; i
++)
444 items
[i
] = g_strdup (_(qw
->u
.radio
.items
[i
]));
446 for (i
= 0; i
< qw
->u
.radio
.count
; i
++)
447 items
[i
] = g_strdup (qw
->u
.radio
.items
[i
]);
449 r
= radio_new (ypos
, xpos
, qw
->u
.radio
.count
, (const char **) items
);
450 r
->pos
= r
->sel
= *qw
->u
.radio
.value
;
451 qw
->widget
= (Widget
*) r
;
458 fprintf (stderr
, "QuickWidget: unknown widget type\n");
462 if (qw
->widget
!= NULL
)
464 qw
->widget
->options
|= qw
->options
; /* FIXME: cannot reset flags, setup only */
465 add_widget (dd
, qw
->widget
);
471 dd
->current
= g_list_next (dd
->current
);
472 if (dd
->current
== NULL
)
473 dd
->current
= dd
->widgets
;
476 return_val
= run_dlg (dd
);
478 /* Get the data if we found something interesting */
479 if (return_val
!= B_CANCEL
)
481 for (qw
= qd
->widgets
; qw
->widget_type
!= quick_end
; qw
++)
483 switch (qw
->widget_type
)
486 *qw
->u
.checkbox
.state
= ((WCheck
*) qw
->widget
)->state
& C_BOOL
;
490 if ((qw
->u
.input
.flags
& 2) != 0)
491 *qw
->u
.input
.result
= tilde_expand (((WInput
*) qw
->widget
)->buffer
);
493 *qw
->u
.input
.result
= g_strdup (((WInput
*) qw
->widget
)->buffer
);
497 *qw
->u
.radio
.value
= ((WRadio
*) qw
->widget
)->sel
;
513 quick_dialog (QuickDialog
* qd
)
515 return quick_dialog_skip (qd
, 0);
520 /* {{{ Input routines */
523 * Show dialog, not background safe.
525 * If the arguments "header" and "text" should be translated,
526 * that MUST be done by the caller of fg_input_dialog_help().
528 * The argument "history_name" holds the name of a section
529 * in the history file. Data entered in the input field of
530 * the dialog box will be stored there.
534 fg_input_dialog_help (const char *header
, const char *text
, const char *help
,
535 const char *history_name
, const char *def_text
)
539 QuickWidget quick_widgets
[] = {
540 /* 0 */ QUICK_BUTTON (6, 64, 1, 0, N_("&Cancel"), B_CANCEL
, NULL
),
541 /* 1 */ QUICK_BUTTON (3, 64, 1, 0, N_("&OK"), B_ENTER
, NULL
),
542 /* 2 */ QUICK_INPUT (3, 64, 0, 0, def_text
, 58, 0, NULL
, &my_str
),
543 /* 3 */ QUICK_LABEL (3, 64, 2, 0, ""),
547 int b0_len
, b1_len
, b_len
, gap
;
548 char histname
[64] = "inp|";
557 quick_widgets
[0].u
.button
.text
= _(quick_widgets
[0].u
.button
.text
);
558 quick_widgets
[1].u
.button
.text
= _(quick_widgets
[1].u
.button
.text
);
559 #endif /* ENABLE_NLS */
561 b0_len
= str_term_width1 (quick_widgets
[0].u
.button
.text
) + 3;
562 b1_len
= str_term_width1 (quick_widgets
[1].u
.button
.text
) + 5; /* default button */
563 b_len
= b0_len
+ b1_len
+ 2; /* including gap */
566 if (history_name
!= NULL
&& *history_name
!= '\0')
568 g_strlcpy (histname
+ 3, history_name
, sizeof (histname
) - 3);
569 quick_widgets
[2].u
.input
.histname
= histname
;
572 /* The special value of def_text is used to identify password boxes
573 and hide characters with "*". Don't save passwords in history! */
574 if (def_text
== INPUT_PASSWORD
)
576 quick_widgets
[2].u
.input
.flags
= 1;
578 quick_widgets
[2].u
.input
.text
= "";
582 p_text
= g_strstrip (g_strdup (text
));
583 msglen (p_text
, &lines
, &cols
);
584 quick_widgets
[3].u
.label
.text
= p_text
;
587 len
= max (max (str_term_width1 (header
), cols
) + 4, 64);
588 len
= min (max (len
, b_len
+ 6), COLS
);
590 /* button locations */
591 gap
= (len
- 8 - b_len
) / 3;
592 quick_widgets
[1].relative_x
= 3 + gap
;
593 quick_widgets
[0].relative_x
= quick_widgets
[1].relative_x
+ b1_len
+ gap
+ 2;
596 QuickDialog Quick_input
= {
597 len
, lines
+ 6, -1, -1, header
,
598 help
, quick_widgets
, NULL
, TRUE
601 for (i
= 0; i
< 4; i
++)
603 quick_widgets
[i
].x_divisions
= Quick_input
.xlen
;
604 quick_widgets
[i
].y_divisions
= Quick_input
.ylen
;
607 for (i
= 0; i
< 3; i
++)
608 quick_widgets
[i
].relative_y
+= 2 + lines
;
610 /* input line length */
611 quick_widgets
[2].u
.input
.len
= Quick_input
.xlen
- 6;
613 ret
= quick_dialog (&Quick_input
);
618 return (ret
!= B_CANCEL
) ? my_str
: NULL
;
622 * Show input dialog, background safe.
624 * If the arguments "header" and "text" should be translated,
625 * that MUST be done by the caller of these wrappers.
628 input_dialog_help (const char *header
, const char *text
, const char *help
,
629 const char *history_name
, const char *def_text
)
634 char *(*f
) (const char *, const char *, const char *, const char *, const char *);
636 #ifdef WITH_BACKGROUND
637 if (we_are_background
)
639 func
.f
= fg_input_dialog_help
;
640 return parent_call_string (func
.p
, 5,
641 strlen (header
), header
, strlen (text
),
642 text
, strlen (help
), help
,
643 strlen (history_name
), history_name
,
644 strlen (def_text
), def_text
);
647 #endif /* WITH_BACKGROUND */
648 return fg_input_dialog_help (header
, text
, help
, history_name
, def_text
);
651 /* Show input dialog with default help, background safe */
653 input_dialog (const char *header
, const char *text
, const char *history_name
, const char *def_text
)
655 return input_dialog_help (header
, text
, "[Input Line Keys]", history_name
, def_text
);
659 input_expand_dialog (const char *header
, const char *text
,
660 const char *history_name
, const char *def_text
)
665 result
= input_dialog (header
, text
, history_name
, def_text
);
668 expanded
= tilde_expand (result
);
679 Cause emacs to enter folding mode for this file: