Merge branch '2447_prepare_to_4.7.5'
[pantumic.git] / lib / widget / wtools.c
blob9054d7c2fc49e79c15506df4b1f61f73c5227548
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
7 1995 Jakub Jelinek
8 1995 Andrej Borsenkow
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.
27 /** \file wtools.c
28 * \brief Source: widget based utility functions
31 #include <config.h>
33 #include <stdarg.h>
34 #include <stdlib.h>
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 */
63 static cb_ret_t
64 default_query_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
66 switch (msg)
68 case DLG_RESIZE:
70 int xpos = COLS / 2 - h->cols / 2;
71 int ypos = LINES / 3 - (h->lines - 3) / 2;
73 /* set position */
74 dlg_set_position (h, ypos, xpos, ypos + h->lines, xpos + h->cols);
76 return MSG_HANDLED;
78 default:
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)
89 char *p;
90 Dlg_head *d;
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);
95 d = last_query_dlg;
97 /* do resize before initing and running */
98 default_query_callback (d, NULL, DLG_RESIZE, 0, NULL);
100 init_dlg (d);
101 g_free (p);
103 return d;
106 /* --------------------------------------------------------------------------------------------- */
108 * Show message dialog. Dismiss it when any key is pressed.
109 * Not safe to call from background.
112 static void
113 fg_message (int flags, const char *title, const char *text)
115 Dlg_head *d;
117 d = do_create_message (flags, title, text);
118 tty_getch ();
119 dlg_run_done (d);
120 destroy_dlg (d);
124 /* --------------------------------------------------------------------------------------------- */
125 /** Show message box from background */
127 #ifdef WITH_BACKGROUND
128 static void
129 bg_message (int dummy, int *flags, char *title, const char *text)
131 (void) dummy;
132 title = g_strconcat (_("Background process:"), " ", title, (char *) NULL);
133 fg_message (*flags, title, text);
134 g_free (title);
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.
151 static char *
152 fg_input_dialog_help (const char *header, const char *text, const char *help,
153 const char *history_name, const char *def_text)
155 char *my_str;
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, ""),
162 QUICK_END
165 int b0_len, b1_len, b_len, gap;
166 char histname[64] = "inp|";
167 int lines, cols;
168 int len;
169 int i;
170 char *p_text;
171 int ret;
173 /* buttons */
174 #ifdef ENABLE_NLS
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 */
183 /* input line */
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;
195 histname[3] = '\0';
196 quick_widgets[2].u.input.text = "";
199 /* 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;
204 /* dialog width */
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);
235 g_free (p_text);
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, ...)
248 va_list ap;
249 Dlg_head *query_dlg;
250 WButton *button;
251 WButton *defbutton = NULL;
252 int win_len = 0;
253 int i;
254 int result = -1;
255 int cols, lines;
256 char *cur_name;
257 const int *query_colors = (flags & D_ERROR) ? alarm_colors : dialog_colors;
259 if (header == MSG_ERROR)
260 header = _("Error");
262 if (count > 0)
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)
270 win_len--;
272 va_end (ap);
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);
280 /* prepare dialog */
281 query_dlg =
282 create_dlg (TRUE, 0, 0, lines, cols, query_colors, default_query_callback,
283 "[QueryBox]", header, DLG_NONE);
285 if (count > 0)
287 cols = (cols - win_len - 2) / 2 + 2;
288 va_start (ap, count);
289 for (i = 0; i < count; i++)
291 int xpos;
293 cur_name = va_arg (ap, char *);
294 xpos = str_term_width1 (cur_name) + 6;
295 if (strchr (cur_name, '&') != NULL)
296 xpos--;
298 button = button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON, cur_name, 0);
299 add_widget (query_dlg, button);
300 cols += xpos;
301 if (i == sel_pos)
302 defbutton = button;
304 va_end (ap);
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);
311 if (defbutton)
312 dlg_select_widget (defbutton);
314 /* run dialog and make result */
315 switch (run_dlg (query_dlg))
317 case B_CANCEL:
318 break;
319 default:
320 result = query_dlg->ret_value - B_USER;
323 /* free used memory */
324 destroy_dlg (query_dlg);
326 else
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;
332 sel_pos = 0;
333 return result;
336 /* --------------------------------------------------------------------------------------------- */
338 void
339 query_set_sel (int new_sel)
341 sel_pos = 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.
350 struct Dlg_head *
351 create_message (int flags, const char *title, const char *text, ...)
353 va_list args;
354 Dlg_head *d;
355 char *p;
357 va_start (args, text);
358 p = g_strdup_vprintf (text, args);
359 va_end (args);
361 d = do_create_message (flags, title, p);
362 g_free (p);
364 return d;
367 /* --------------------------------------------------------------------------------------------- */
368 /** Show message box, background safe */
370 void
371 message (int flags, const char *title, const char *text, ...)
373 char *p;
374 va_list ap;
375 union
377 void *p;
378 void (*f) (int, int *, char *, const char *);
379 } func;
381 va_start (ap, text);
382 p = g_strdup_vprintf (text, ap);
383 va_end (ap);
385 if (title == MSG_ERROR)
386 title = _("Error");
388 #ifdef WITH_BACKGROUND
389 if (we_are_background)
391 func.f = bg_message;
392 parent_call (func.p, NULL, 3, sizeof (flags), &flags, strlen (title), title, strlen (p), p);
394 else
395 #endif /* WITH_BACKGROUND */
396 fg_message (flags, title, p);
398 g_free (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.
409 char *
410 input_dialog_help (const char *header, const char *text, const char *help,
411 const char *history_name, const char *def_text)
413 union
415 void *p;
416 char *(*f) (const char *, const char *, const char *, const char *, const char *);
417 } func;
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);
428 else
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 */
436 char *
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 /* --------------------------------------------------------------------------------------------- */
444 char *
445 input_expand_dialog (const char *header, const char *text,
446 const char *history_name, const char *def_text)
448 char *result;
449 char *expanded;
451 result = input_dialog (header, text, history_name, def_text);
452 if (result)
454 expanded = tilde_expand (result);
455 g_free (result);
456 return expanded;
458 return result;
461 /* --------------------------------------------------------------------------------------------- */