1 /* Widget based utility functions.
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 Authors: 1994, 1995, 1996 Miguel de Icaza
6 1994, 1995 Radek Doulik
9 2009, 2010 Andrew Borodin
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 * \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/strutil.h"
40 #include "lib/util.h" /* tilde_expand() */
41 #include "lib/widget.h"
43 /* TODO: these includes should be removed! */
44 #include "src/background.h" /* parent_call */
46 /*** global variables ****************************************************************************/
48 /*** file scope macro definitions ****************************************************************/
50 /*** file scope type declarations ****************************************************************/
52 /*** file scope variables ************************************************************************/
54 static Dlg_head
*last_query_dlg
;
56 static int sel_pos
= 0;
58 /*** file scope functions ************************************************************************/
59 /* --------------------------------------------------------------------------------------------- */
61 /** default query callback, used to reposition query */
64 default_query_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
70 int xpos
= COLS
/ 2 - h
->cols
/ 2;
71 int ypos
= LINES
/ 3 - (h
->lines
- 3) / 2;
74 dlg_set_position (h
, ypos
, xpos
, ypos
+ h
->lines
, xpos
+ h
->cols
);
79 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
83 /* --------------------------------------------------------------------------------------------- */
84 /** Create message dialog */
86 static struct Dlg_head
*
87 do_create_message (int flags
, const char *title
, const char *text
)
92 /* Add empty lines before and after the message */
93 p
= g_strconcat ("\n", text
, "\n", (char *) NULL
);
94 query_dialog (title
, p
, flags
, 0);
97 /* do resize before initing and running */
98 default_query_callback (d
, NULL
, DLG_RESIZE
, 0, NULL
);
106 /* --------------------------------------------------------------------------------------------- */
108 * Show message dialog. Dismiss it when any key is pressed.
109 * Not safe to call from background.
113 fg_message (int flags
, const char *title
, const char *text
)
117 d
= do_create_message (flags
, title
, text
);
124 /* --------------------------------------------------------------------------------------------- */
125 /** Show message box from background */
127 #ifdef WITH_BACKGROUND
129 bg_message (int dummy
, int *flags
, char *title
, const char *text
)
132 title
= g_strconcat (_("Background process:"), " ", title
, (char *) NULL
);
133 fg_message (*flags
, title
, text
);
136 #endif /* WITH_BACKGROUND */
138 /* --------------------------------------------------------------------------------------------- */
141 * Show dialog, not background safe.
143 * If the arguments "header" and "text" should be translated,
144 * that MUST be done by the caller of fg_input_dialog_help().
146 * The argument "history_name" holds the name of a section
147 * in the history file. Data entered in the input field of
148 * the dialog box will be stored there.
152 fg_input_dialog_help (const char *header
, const char *text
, const char *help
,
153 const char *history_name
, const char *def_text
)
157 QuickWidget quick_widgets
[] = {
158 /* 0 */ QUICK_BUTTON (6, 64, 1, 0, N_("&Cancel"), B_CANCEL
, NULL
),
159 /* 1 */ QUICK_BUTTON (3, 64, 1, 0, N_("&OK"), B_ENTER
, NULL
),
160 /* 2 */ QUICK_INPUT (3, 64, 0, 0, def_text
, 58, 0, NULL
, &my_str
),
161 /* 3 */ QUICK_LABEL (3, 64, 2, 0, ""),
165 int b0_len
, b1_len
, b_len
, gap
;
166 char histname
[64] = "inp|";
175 quick_widgets
[0].u
.button
.text
= _(quick_widgets
[0].u
.button
.text
);
176 quick_widgets
[1].u
.button
.text
= _(quick_widgets
[1].u
.button
.text
);
177 #endif /* ENABLE_NLS */
179 b0_len
= str_term_width1 (quick_widgets
[0].u
.button
.text
) + 3;
180 b1_len
= str_term_width1 (quick_widgets
[1].u
.button
.text
) + 5; /* default button */
181 b_len
= b0_len
+ b1_len
+ 2; /* including gap */
184 if (history_name
!= NULL
&& *history_name
!= '\0')
186 g_strlcpy (histname
+ 3, history_name
, sizeof (histname
) - 3);
187 quick_widgets
[2].u
.input
.histname
= histname
;
190 /* The special value of def_text is used to identify password boxes
191 and hide characters with "*". Don't save passwords in history! */
192 if (def_text
== INPUT_PASSWORD
)
194 quick_widgets
[2].u
.input
.flags
= 1;
196 quick_widgets
[2].u
.input
.text
= "";
200 p_text
= g_strstrip (g_strdup (text
));
201 str_msg_term_size (p_text
, &lines
, &cols
);
202 quick_widgets
[3].u
.label
.text
= p_text
;
205 len
= str_term_width1 (header
);
206 len
= max (max (len
, cols
) + 4, 64);
207 len
= min (max (len
, b_len
+ 6), COLS
);
209 /* button locations */
210 gap
= (len
- 8 - b_len
) / 3;
211 quick_widgets
[1].relative_x
= 3 + gap
;
212 quick_widgets
[0].relative_x
= quick_widgets
[1].relative_x
+ b1_len
+ gap
+ 2;
215 QuickDialog Quick_input
= {
216 len
, lines
+ 6, -1, -1, header
,
217 help
, quick_widgets
, NULL
, TRUE
220 for (i
= 0; i
< 4; i
++)
222 quick_widgets
[i
].x_divisions
= Quick_input
.xlen
;
223 quick_widgets
[i
].y_divisions
= Quick_input
.ylen
;
226 for (i
= 0; i
< 3; i
++)
227 quick_widgets
[i
].relative_y
+= 2 + lines
;
229 /* input line length */
230 quick_widgets
[2].u
.input
.len
= Quick_input
.xlen
- 6;
232 ret
= quick_dialog (&Quick_input
);
237 return (ret
!= B_CANCEL
) ? my_str
: NULL
;
240 /* --------------------------------------------------------------------------------------------- */
241 /*** public functions ****************************************************************************/
242 /* --------------------------------------------------------------------------------------------- */
244 /** Used to ask questions to the user */
246 query_dialog (const char *header
, const char *text
, int flags
, int count
, ...)
251 WButton
*defbutton
= NULL
;
257 const int *query_colors
= (flags
& D_ERROR
) ? alarm_colors
: dialog_colors
;
259 if (header
== MSG_ERROR
)
264 va_start (ap
, count
);
265 for (i
= 0; i
< count
; i
++)
267 char *cp
= va_arg (ap
, char *);
268 win_len
+= str_term_width1 (cp
) + 6;
269 if (strchr (cp
, '&') != NULL
)
275 /* count coordinates */
276 str_msg_term_size (text
, &lines
, &cols
);
277 cols
= 6 + max (win_len
, max (str_term_width1 (header
), cols
));
278 lines
+= 4 + (count
> 0 ? 2 : 0);
282 create_dlg (TRUE
, 0, 0, lines
, cols
, query_colors
, default_query_callback
,
283 "[QueryBox]", header
, DLG_NONE
);
287 cols
= (cols
- win_len
- 2) / 2 + 2;
288 va_start (ap
, count
);
289 for (i
= 0; i
< count
; i
++)
293 cur_name
= va_arg (ap
, char *);
294 xpos
= str_term_width1 (cur_name
) + 6;
295 if (strchr (cur_name
, '&') != NULL
)
298 button
= button_new (lines
- 3, cols
, B_USER
+ i
, NORMAL_BUTTON
, cur_name
, 0);
299 add_widget (query_dlg
, button
);
306 add_widget (query_dlg
, label_new (2, 3, text
));
308 /* do resize before running and selecting any widget */
309 default_query_callback (query_dlg
, NULL
, DLG_RESIZE
, 0, NULL
);
312 dlg_select_widget (defbutton
);
314 /* run dialog and make result */
315 switch (run_dlg (query_dlg
))
320 result
= query_dlg
->ret_value
- B_USER
;
323 /* free used memory */
324 destroy_dlg (query_dlg
);
328 add_widget (query_dlg
, label_new (2, 3, text
));
329 add_widget (query_dlg
, button_new (0, 0, 0, HIDDEN_BUTTON
, "-", 0));
330 last_query_dlg
= query_dlg
;
336 /* --------------------------------------------------------------------------------------------- */
339 query_set_sel (int new_sel
)
344 /* --------------------------------------------------------------------------------------------- */
346 * Create message dialog. The caller must call dlg_run_done() and
347 * destroy_dlg() to dismiss it. Not safe to call from background.
351 create_message (int flags
, const char *title
, const char *text
, ...)
357 va_start (args
, text
);
358 p
= g_strdup_vprintf (text
, args
);
361 d
= do_create_message (flags
, title
, p
);
367 /* --------------------------------------------------------------------------------------------- */
368 /** Show message box, background safe */
371 message (int flags
, const char *title
, const char *text
, ...)
378 void (*f
) (int, int *, char *, const char *);
382 p
= g_strdup_vprintf (text
, ap
);
385 if (title
== MSG_ERROR
)
388 #ifdef WITH_BACKGROUND
389 if (we_are_background
)
392 parent_call (func
.p
, NULL
, 3, sizeof (flags
), &flags
, strlen (title
), title
, strlen (p
), p
);
395 #endif /* WITH_BACKGROUND */
396 fg_message (flags
, title
, p
);
401 /* --------------------------------------------------------------------------------------------- */
403 * Show input dialog, background safe.
405 * If the arguments "header" and "text" should be translated,
406 * that MUST be done by the caller of these wrappers.
410 input_dialog_help (const char *header
, const char *text
, const char *help
,
411 const char *history_name
, const char *def_text
)
416 char *(*f
) (const char *, const char *, const char *, const char *, const char *);
418 #ifdef WITH_BACKGROUND
419 if (we_are_background
)
421 func
.f
= fg_input_dialog_help
;
422 return parent_call_string (func
.p
, 5,
423 strlen (header
), header
, strlen (text
),
424 text
, strlen (help
), help
,
425 strlen (history_name
), history_name
,
426 strlen (def_text
), def_text
);
429 #endif /* WITH_BACKGROUND */
430 return fg_input_dialog_help (header
, text
, help
, history_name
, def_text
);
433 /* --------------------------------------------------------------------------------------------- */
434 /** Show input dialog with default help, background safe */
437 input_dialog (const char *header
, const char *text
, const char *history_name
, const char *def_text
)
439 return input_dialog_help (header
, text
, "[Input Line Keys]", history_name
, def_text
);
442 /* --------------------------------------------------------------------------------------------- */
445 input_expand_dialog (const char *header
, const char *text
,
446 const char *history_name
, const char *def_text
)
451 result
= input_dialog (header
, text
, history_name
, def_text
);
454 expanded
= tilde_expand (result
);
461 /* --------------------------------------------------------------------------------------------- */