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
] = {
65 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))
90 if (ypos
+ lines
>= LINES
)
91 ypos
= LINES
- lines
- space
;
95 if (xpos
+ cols
>= COLS
)
96 xpos
= COLS
- cols
- space
;
100 listbox
= g_new (Listbox
, 1);
103 create_dlg (TRUE
, ypos
, xpos
, lines
+ space
, cols
+ space
,
104 listbox_colors
, NULL
, help
, title
, DLG_REVERSE
| DLG_TRYUP
);
106 listbox
->list
= listbox_new (2, 2, lines
, cols
, FALSE
, NULL
);
107 add_widget (listbox
->dlg
, listbox
->list
);
113 create_listbox_window (int lines
, int cols
, const char *title
, const char *help
)
115 return create_listbox_window_centered (-1, -1, lines
, cols
, title
, help
);
118 /* Returns the number of the item selected */
120 run_listbox (Listbox
* l
)
124 if (run_dlg (l
->dlg
) != B_CANCEL
)
126 destroy_dlg (l
->dlg
);
131 /* default query callback, used to reposition query */
133 default_query_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
139 int xpos
= COLS
/ 2 - h
->cols
/ 2;
140 int ypos
= LINES
/ 3 - (h
->lines
- 3) / 2;
143 dlg_set_position (h
, ypos
, xpos
, ypos
+ h
->lines
, xpos
+ h
->cols
);
148 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
152 static Dlg_head
*last_query_dlg
;
154 static int sel_pos
= 0;
156 /* Used to ask questions to the user */
158 query_dialog (const char *header
, const char *text
, int flags
, int count
, ...)
163 WButton
*defbutton
= NULL
;
169 const int *query_colors
= (flags
& D_ERROR
) ? alarm_colors
: dialog_colors
;
171 if (header
== MSG_ERROR
)
176 va_start (ap
, count
);
177 for (i
= 0; i
< count
; i
++)
179 char *cp
= va_arg (ap
, char *);
180 win_len
+= str_term_width1 (cp
) + 6;
181 if (strchr (cp
, '&') != NULL
)
187 /* count coordinates */
188 str_msg_term_size (text
, &lines
, &cols
);
189 cols
= 6 + max (win_len
, max (str_term_width1 (header
), cols
));
190 lines
+= 4 + (count
> 0 ? 2 : 0);
194 create_dlg (TRUE
, 0, 0, lines
, cols
, query_colors
, default_query_callback
,
195 "[QueryBox]", header
, DLG_NONE
);
199 cols
= (cols
- win_len
- 2) / 2 + 2;
200 va_start (ap
, count
);
201 for (i
= 0; i
< count
; i
++)
205 cur_name
= va_arg (ap
, char *);
206 xpos
= str_term_width1 (cur_name
) + 6;
207 if (strchr (cur_name
, '&') != NULL
)
210 button
= button_new (lines
- 3, cols
, B_USER
+ i
, NORMAL_BUTTON
, cur_name
, 0);
211 add_widget (query_dlg
, button
);
218 add_widget (query_dlg
, label_new (2, 3, text
));
220 /* do resize before running and selecting any widget */
221 default_query_callback (query_dlg
, NULL
, DLG_RESIZE
, 0, NULL
);
224 dlg_select_widget (defbutton
);
226 /* run dialog and make result */
227 switch (run_dlg (query_dlg
))
232 result
= query_dlg
->ret_value
- B_USER
;
235 /* free used memory */
236 destroy_dlg (query_dlg
);
240 add_widget (query_dlg
, label_new (2, 3, text
));
241 add_widget (query_dlg
, button_new (0, 0, 0, HIDDEN_BUTTON
, "-", 0));
242 last_query_dlg
= query_dlg
;
249 query_set_sel (int new_sel
)
255 /* Create message dialog */
256 static struct Dlg_head
*
257 do_create_message (int flags
, const char *title
, const char *text
)
262 /* Add empty lines before and after the message */
263 p
= g_strconcat ("\n", text
, "\n", (char *) NULL
);
264 query_dialog (title
, p
, flags
, 0);
267 /* do resize before initing and running */
268 default_query_callback (d
, NULL
, DLG_RESIZE
, 0, NULL
);
278 * Create message dialog. The caller must call dlg_run_done() and
279 * destroy_dlg() to dismiss it. Not safe to call from background.
282 create_message (int flags
, const char *title
, const char *text
, ...)
288 va_start (args
, text
);
289 p
= g_strdup_vprintf (text
, args
);
292 d
= do_create_message (flags
, title
, p
);
300 * Show message dialog. Dismiss it when any key is pressed.
301 * Not safe to call from background.
304 fg_message (int flags
, const char *title
, const char *text
)
308 d
= do_create_message (flags
, title
, text
);
315 /* Show message box from background */
316 #ifdef WITH_BACKGROUND
318 bg_message (int dummy
, int *flags
, char *title
, const char *text
)
321 title
= g_strconcat (_("Background process:"), " ", title
, (char *) NULL
);
322 fg_message (*flags
, title
, text
);
325 #endif /* WITH_BACKGROUND */
328 /* Show message box, background safe */
330 message (int flags
, const char *title
, const char *text
, ...)
337 void (*f
) (int, int *, char *, const char *);
341 p
= g_strdup_vprintf (text
, ap
);
344 if (title
== MSG_ERROR
)
347 #ifdef WITH_BACKGROUND
348 if (we_are_background
)
351 parent_call (func
.p
, NULL
, 3, sizeof (flags
), &flags
, strlen (title
), title
, strlen (p
), p
);
354 #endif /* WITH_BACKGROUND */
355 fg_message (flags
, title
, p
);
361 /* {{{ Quick dialog routines */
365 quick_dialog_skip (QuickDialog
* qd
, int nskip
)
368 #define I18N(x) (x = !qd->i18n && x && *x ? _(x): x)
370 #define I18N(x) (x = x)
378 const int input_colors
[3] = {
380 INPUT_UNCHANGED_COLOR
,
386 if ((qd
->xpos
== -1) || (qd
->ypos
== -1))
387 dd
= create_dlg (TRUE
, 0, 0, qd
->ylen
, qd
->xlen
,
388 dialog_colors
, qd
->callback
, qd
->help
, qd
->title
,
389 DLG_CENTER
| DLG_TRYUP
| DLG_REVERSE
);
391 dd
= create_dlg (TRUE
, qd
->ypos
, qd
->xpos
, qd
->ylen
, qd
->xlen
,
392 dialog_colors
, qd
->callback
, qd
->help
, qd
->title
, DLG_REVERSE
);
394 for (qw
= qd
->widgets
; qw
->widget_type
!= quick_end
; qw
++)
399 xpos
= (qd
->xlen
* qw
->relative_x
) / qw
->x_divisions
;
400 ypos
= (qd
->ylen
* qw
->relative_y
) / qw
->y_divisions
;
402 switch (qw
->widget_type
)
406 (Widget
*) check_new (ypos
, xpos
, *qw
->u
.checkbox
.state
,
407 I18N (qw
->u
.checkbox
.text
));
411 qw
->widget
= (Widget
*) button_new (ypos
, xpos
, qw
->u
.button
.action
,
412 (qw
->u
.button
.action
==
413 B_ENTER
) ? DEFPUSH_BUTTON
: NORMAL_BUTTON
,
414 I18N (qw
->u
.button
.text
), qw
->u
.button
.callback
);
418 in
= input_new (ypos
, xpos
, (int *) input_colors
,
419 qw
->u
.input
.len
, qw
->u
.input
.text
, qw
->u
.input
.histname
,
420 INPUT_COMPLETE_DEFAULT
);
421 in
->is_password
= (qw
->u
.input
.flags
== 1);
422 if ((qw
->u
.input
.flags
& 2) != 0)
423 in
->completion_flags
|= INPUT_COMPLETE_CD
;
424 qw
->widget
= (Widget
*) in
;
425 *qw
->u
.input
.result
= NULL
;
429 qw
->widget
= (Widget
*) label_new (ypos
, xpos
, I18N (qw
->u
.label
.text
));
433 qw
->widget
= (Widget
*) groupbox_new (ypos
, xpos
,
434 qw
->u
.groupbox
.height
,
435 qw
->u
.groupbox
.width
,
436 I18N (qw
->u
.groupbox
.title
));
444 /* create the copy of radio_items to avoid mwmory leak */
445 items
= g_new0 (char *, qw
->u
.radio
.count
+ 1);
448 for (i
= 0; i
< qw
->u
.radio
.count
; i
++)
449 items
[i
] = g_strdup (_(qw
->u
.radio
.items
[i
]));
451 for (i
= 0; i
< qw
->u
.radio
.count
; i
++)
452 items
[i
] = g_strdup (qw
->u
.radio
.items
[i
]);
454 r
= radio_new (ypos
, xpos
, qw
->u
.radio
.count
, (const char **) items
);
455 r
->pos
= r
->sel
= *qw
->u
.radio
.value
;
456 qw
->widget
= (Widget
*) r
;
463 fprintf (stderr
, "QuickWidget: unknown widget type\n");
467 if (qw
->widget
!= NULL
)
469 qw
->widget
->options
|= qw
->options
; /* FIXME: cannot reset flags, setup only */
470 add_widget (dd
, qw
->widget
);
476 dd
->current
= g_list_next (dd
->current
);
477 if (dd
->current
== NULL
)
478 dd
->current
= dd
->widgets
;
481 return_val
= run_dlg (dd
);
483 /* Get the data if we found something interesting */
484 if (return_val
!= B_CANCEL
)
486 for (qw
= qd
->widgets
; qw
->widget_type
!= quick_end
; qw
++)
488 switch (qw
->widget_type
)
491 *qw
->u
.checkbox
.state
= ((WCheck
*) qw
->widget
)->state
& C_BOOL
;
495 if ((qw
->u
.input
.flags
& 2) != 0)
496 *qw
->u
.input
.result
= tilde_expand (((WInput
*) qw
->widget
)->buffer
);
498 *qw
->u
.input
.result
= g_strdup (((WInput
*) qw
->widget
)->buffer
);
502 *qw
->u
.radio
.value
= ((WRadio
*) qw
->widget
)->sel
;
518 quick_dialog (QuickDialog
* qd
)
520 return quick_dialog_skip (qd
, 0);
525 /* {{{ Input routines */
528 * Show dialog, not background safe.
530 * If the arguments "header" and "text" should be translated,
531 * that MUST be done by the caller of fg_input_dialog_help().
533 * The argument "history_name" holds the name of a section
534 * in the history file. Data entered in the input field of
535 * the dialog box will be stored there.
539 fg_input_dialog_help (const char *header
, const char *text
, const char *help
,
540 const char *history_name
, const char *def_text
)
544 QuickWidget quick_widgets
[] = {
545 /* 0 */ QUICK_BUTTON (6, 64, 1, 0, N_("&Cancel"), B_CANCEL
, NULL
),
546 /* 1 */ QUICK_BUTTON (3, 64, 1, 0, N_("&OK"), B_ENTER
, NULL
),
547 /* 2 */ QUICK_INPUT (3, 64, 0, 0, def_text
, 58, 0, NULL
, &my_str
),
548 /* 3 */ QUICK_LABEL (3, 64, 2, 0, ""),
552 int b0_len
, b1_len
, b_len
, gap
;
553 char histname
[64] = "inp|";
562 quick_widgets
[0].u
.button
.text
= _(quick_widgets
[0].u
.button
.text
);
563 quick_widgets
[1].u
.button
.text
= _(quick_widgets
[1].u
.button
.text
);
564 #endif /* ENABLE_NLS */
566 b0_len
= str_term_width1 (quick_widgets
[0].u
.button
.text
) + 3;
567 b1_len
= str_term_width1 (quick_widgets
[1].u
.button
.text
) + 5; /* default button */
568 b_len
= b0_len
+ b1_len
+ 2; /* including gap */
571 if (history_name
!= NULL
&& *history_name
!= '\0')
573 g_strlcpy (histname
+ 3, history_name
, sizeof (histname
) - 3);
574 quick_widgets
[2].u
.input
.histname
= histname
;
577 /* The special value of def_text is used to identify password boxes
578 and hide characters with "*". Don't save passwords in history! */
579 if (def_text
== INPUT_PASSWORD
)
581 quick_widgets
[2].u
.input
.flags
= 1;
583 quick_widgets
[2].u
.input
.text
= "";
587 p_text
= g_strstrip (g_strdup (text
));
588 msglen (p_text
, &lines
, &cols
);
589 quick_widgets
[3].u
.label
.text
= p_text
;
592 len
= max (max (str_term_width1 (header
), cols
) + 4, 64);
593 len
= min (max (len
, b_len
+ 6), COLS
);
595 /* button locations */
596 gap
= (len
- 8 - b_len
) / 3;
597 quick_widgets
[1].relative_x
= 3 + gap
;
598 quick_widgets
[0].relative_x
= quick_widgets
[1].relative_x
+ b1_len
+ gap
+ 2;
601 QuickDialog Quick_input
= {
602 len
, lines
+ 6, -1, -1, header
,
603 help
, quick_widgets
, NULL
, TRUE
606 for (i
= 0; i
< 4; i
++)
608 quick_widgets
[i
].x_divisions
= Quick_input
.xlen
;
609 quick_widgets
[i
].y_divisions
= Quick_input
.ylen
;
612 for (i
= 0; i
< 3; i
++)
613 quick_widgets
[i
].relative_y
+= 2 + lines
;
615 /* input line length */
616 quick_widgets
[2].u
.input
.len
= Quick_input
.xlen
- 6;
618 ret
= quick_dialog (&Quick_input
);
623 return (ret
!= B_CANCEL
) ? my_str
: NULL
;
627 * Show input dialog, background safe.
629 * If the arguments "header" and "text" should be translated,
630 * that MUST be done by the caller of these wrappers.
633 input_dialog_help (const char *header
, const char *text
, const char *help
,
634 const char *history_name
, const char *def_text
)
639 char *(*f
) (const char *, const char *, const char *, const char *, const char *);
641 #ifdef WITH_BACKGROUND
642 if (we_are_background
)
644 func
.f
= fg_input_dialog_help
;
645 return parent_call_string (func
.p
, 5,
646 strlen (header
), header
, strlen (text
),
647 text
, strlen (help
), help
,
648 strlen (history_name
), history_name
,
649 strlen (def_text
), def_text
);
652 #endif /* WITH_BACKGROUND */
653 return fg_input_dialog_help (header
, text
, help
, history_name
, def_text
);
656 /* Show input dialog with default help, background safe */
658 input_dialog (const char *header
, const char *text
, const char *history_name
, const char *def_text
)
660 return input_dialog_help (header
, text
, "[Input Line Keys]", history_name
, def_text
);
664 input_expand_dialog (const char *header
, const char *text
,
665 const char *history_name
, const char *def_text
)
670 result
= input_dialog (header
, text
, history_name
, def_text
);
673 expanded
= tilde_expand (result
);
684 Cause emacs to enter folding mode for this file: