Nice location of red (warning and error) dialogs.
[midnight-commander.git] / lib / widget / wtools.c
blob9e9eff91dd25564a98e4bbf527a2df1b0c445a94
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 Dlg_head *prev_dlg = NULL;
71 int ypos, xpos;
73 /* get dialog under h */
74 if (top_dlg != NULL)
76 if (top_dlg->data != (void *) h)
77 prev_dlg = (Dlg_head *) top_dlg->data;
78 else
80 GList *p;
82 /* Top dialog is current if it is visible.
83 Get previous dialog in stack */
84 p = g_list_next (top_dlg);
85 if (p != NULL)
86 prev_dlg = (Dlg_head *) p->data;
90 /* if previous dialog is not fullscreen'd -- overlap it */
91 if (prev_dlg == NULL || prev_dlg->fullscreen)
92 ypos = LINES / 3 - (h->lines - 3) / 2;
93 else
94 ypos = prev_dlg->y + 2;
96 xpos = COLS / 2 - h->cols / 2;
98 /* set position */
99 dlg_set_position (h, ypos, xpos, ypos + h->lines, xpos + h->cols);
101 return MSG_HANDLED;
103 default:
104 return default_dlg_callback (h, sender, msg, parm, data);
108 /* --------------------------------------------------------------------------------------------- */
109 /** Create message dialog */
111 static struct Dlg_head *
112 do_create_message (int flags, const char *title, const char *text)
114 char *p;
115 Dlg_head *d;
117 /* Add empty lines before and after the message */
118 p = g_strconcat ("\n", text, "\n", (char *) NULL);
119 query_dialog (title, p, flags, 0);
120 d = last_query_dlg;
122 /* do resize before initing and running */
123 default_query_callback (d, NULL, DLG_RESIZE, 0, NULL);
125 init_dlg (d);
126 g_free (p);
128 return d;
131 /* --------------------------------------------------------------------------------------------- */
133 * Show message dialog. Dismiss it when any key is pressed.
134 * Not safe to call from background.
137 static void
138 fg_message (int flags, const char *title, const char *text)
140 Dlg_head *d;
142 d = do_create_message (flags, title, text);
143 tty_getch ();
144 dlg_run_done (d);
145 destroy_dlg (d);
149 /* --------------------------------------------------------------------------------------------- */
150 /** Show message box from background */
152 #ifdef WITH_BACKGROUND
153 static void
154 bg_message (int dummy, int *flags, char *title, const char *text)
156 (void) dummy;
157 title = g_strconcat (_("Background process:"), " ", title, (char *) NULL);
158 fg_message (*flags, title, text);
159 g_free (title);
161 #endif /* WITH_BACKGROUND */
163 /* --------------------------------------------------------------------------------------------- */
166 * Show dialog, not background safe.
168 * If the arguments "header" and "text" should be translated,
169 * that MUST be done by the caller of fg_input_dialog_help().
171 * The argument "history_name" holds the name of a section
172 * in the history file. Data entered in the input field of
173 * the dialog box will be stored there.
176 static char *
177 fg_input_dialog_help (const char *header, const char *text, const char *help,
178 const char *history_name, const char *def_text)
180 char *my_str;
182 QuickWidget quick_widgets[] = {
183 /* 0 */ QUICK_BUTTON (6, 64, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
184 /* 1 */ QUICK_BUTTON (3, 64, 1, 0, N_("&OK"), B_ENTER, NULL),
185 /* 2 */ QUICK_INPUT (3, 64, 0, 0, def_text, 58, 0, NULL, &my_str),
186 /* 3 */ QUICK_LABEL (3, 64, 2, 0, ""),
187 QUICK_END
190 int b0_len, b1_len, b_len, gap;
191 char histname[64] = "inp|";
192 int lines, cols;
193 int len;
194 int i;
195 char *p_text;
196 int ret;
198 /* buttons */
199 #ifdef ENABLE_NLS
200 quick_widgets[0].u.button.text = _(quick_widgets[0].u.button.text);
201 quick_widgets[1].u.button.text = _(quick_widgets[1].u.button.text);
202 #endif /* ENABLE_NLS */
204 b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
205 b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 5; /* default button */
206 b_len = b0_len + b1_len + 2; /* including gap */
208 /* input line */
209 if (history_name != NULL && *history_name != '\0')
211 g_strlcpy (histname + 3, history_name, sizeof (histname) - 3);
212 quick_widgets[2].u.input.histname = histname;
215 /* The special value of def_text is used to identify password boxes
216 and hide characters with "*". Don't save passwords in history! */
217 if (def_text == INPUT_PASSWORD)
219 quick_widgets[2].u.input.flags = 1;
220 histname[3] = '\0';
221 quick_widgets[2].u.input.text = "";
224 /* text */
225 p_text = g_strstrip (g_strdup (text));
226 str_msg_term_size (p_text, &lines, &cols);
227 quick_widgets[3].u.label.text = p_text;
229 /* dialog width */
230 len = str_term_width1 (header);
231 len = max (max (len, cols) + 4, 64);
232 len = min (max (len, b_len + 6), COLS);
234 /* button locations */
235 gap = (len - 8 - b_len) / 3;
236 quick_widgets[1].relative_x = 3 + gap;
237 quick_widgets[0].relative_x = quick_widgets[1].relative_x + b1_len + gap + 2;
240 QuickDialog Quick_input = {
241 len, lines + 6, -1, -1, header,
242 help, quick_widgets, NULL, TRUE
245 for (i = 0; i < 4; i++)
247 quick_widgets[i].x_divisions = Quick_input.xlen;
248 quick_widgets[i].y_divisions = Quick_input.ylen;
251 for (i = 0; i < 3; i++)
252 quick_widgets[i].relative_y += 2 + lines;
254 /* input line length */
255 quick_widgets[2].u.input.len = Quick_input.xlen - 6;
257 ret = quick_dialog (&Quick_input);
260 g_free (p_text);
262 return (ret != B_CANCEL) ? my_str : NULL;
265 /* --------------------------------------------------------------------------------------------- */
266 /*** public functions ****************************************************************************/
267 /* --------------------------------------------------------------------------------------------- */
269 /** Used to ask questions to the user */
271 query_dialog (const char *header, const char *text, int flags, int count, ...)
273 va_list ap;
274 Dlg_head *query_dlg;
275 WButton *button;
276 WButton *defbutton = NULL;
277 int win_len = 0;
278 int i;
279 int result = -1;
280 int cols, lines;
281 char *cur_name;
282 const int *query_colors = (flags & D_ERROR) ? alarm_colors : dialog_colors;
284 if (header == MSG_ERROR)
285 header = _("Error");
287 if (count > 0)
289 va_start (ap, count);
290 for (i = 0; i < count; i++)
292 char *cp = va_arg (ap, char *);
293 win_len += str_term_width1 (cp) + 6;
294 if (strchr (cp, '&') != NULL)
295 win_len--;
297 va_end (ap);
300 /* count coordinates */
301 str_msg_term_size (text, &lines, &cols);
302 cols = 6 + max (win_len, max (str_term_width1 (header), cols));
303 lines += 4 + (count > 0 ? 2 : 0);
305 /* prepare dialog */
306 query_dlg =
307 create_dlg (TRUE, 0, 0, lines, cols, query_colors, default_query_callback,
308 "[QueryBox]", header, DLG_NONE);
310 if (count > 0)
312 cols = (cols - win_len - 2) / 2 + 2;
313 va_start (ap, count);
314 for (i = 0; i < count; i++)
316 int xpos;
318 cur_name = va_arg (ap, char *);
319 xpos = str_term_width1 (cur_name) + 6;
320 if (strchr (cur_name, '&') != NULL)
321 xpos--;
323 button = button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON, cur_name, 0);
324 add_widget (query_dlg, button);
325 cols += xpos;
326 if (i == sel_pos)
327 defbutton = button;
329 va_end (ap);
331 add_widget (query_dlg, label_new (2, 3, text));
333 /* do resize before running and selecting any widget */
334 default_query_callback (query_dlg, NULL, DLG_RESIZE, 0, NULL);
336 if (defbutton)
337 dlg_select_widget (defbutton);
339 /* run dialog and make result */
340 switch (run_dlg (query_dlg))
342 case B_CANCEL:
343 break;
344 default:
345 result = query_dlg->ret_value - B_USER;
348 /* free used memory */
349 destroy_dlg (query_dlg);
351 else
353 add_widget (query_dlg, label_new (2, 3, text));
354 add_widget (query_dlg, button_new (0, 0, 0, HIDDEN_BUTTON, "-", 0));
355 last_query_dlg = query_dlg;
357 sel_pos = 0;
358 return result;
361 /* --------------------------------------------------------------------------------------------- */
363 void
364 query_set_sel (int new_sel)
366 sel_pos = new_sel;
369 /* --------------------------------------------------------------------------------------------- */
371 * Create message dialog. The caller must call dlg_run_done() and
372 * destroy_dlg() to dismiss it. Not safe to call from background.
375 struct Dlg_head *
376 create_message (int flags, const char *title, const char *text, ...)
378 va_list args;
379 Dlg_head *d;
380 char *p;
382 va_start (args, text);
383 p = g_strdup_vprintf (text, args);
384 va_end (args);
386 d = do_create_message (flags, title, p);
387 g_free (p);
389 return d;
392 /* --------------------------------------------------------------------------------------------- */
393 /** Show message box, background safe */
395 void
396 message (int flags, const char *title, const char *text, ...)
398 char *p;
399 va_list ap;
401 va_start (ap, text);
402 p = g_strdup_vprintf (text, ap);
403 va_end (ap);
405 if (title == MSG_ERROR)
406 title = _("Error");
408 #ifdef WITH_BACKGROUND
409 if (we_are_background)
411 union
413 void *p;
414 void (*f) (int, int *, char *, const char *);
415 } func;
416 func.f = bg_message;
417 parent_call (func.p, NULL, 3, sizeof (flags), &flags, strlen (title), title, strlen (p), p);
419 else
420 #endif /* WITH_BACKGROUND */
421 fg_message (flags, title, p);
423 g_free (p);
426 /* --------------------------------------------------------------------------------------------- */
428 * Show input dialog, background safe.
430 * If the arguments "header" and "text" should be translated,
431 * that MUST be done by the caller of these wrappers.
434 char *
435 input_dialog_help (const char *header, const char *text, const char *help,
436 const char *history_name, const char *def_text)
438 #ifdef WITH_BACKGROUND
439 if (we_are_background)
441 union
443 void *p;
444 char *(*f) (const char *, const char *, const char *, const char *, const char *);
445 } func;
446 func.f = fg_input_dialog_help;
447 return parent_call_string (func.p, 5,
448 strlen (header), header, strlen (text),
449 text, strlen (help), help,
450 strlen (history_name), history_name,
451 strlen (def_text), def_text);
453 else
454 #endif /* WITH_BACKGROUND */
455 return fg_input_dialog_help (header, text, help, history_name, def_text);
458 /* --------------------------------------------------------------------------------------------- */
459 /** Show input dialog with default help, background safe */
461 char *
462 input_dialog (const char *header, const char *text, const char *history_name, const char *def_text)
464 return input_dialog_help (header, text, "[Input Line Keys]", history_name, def_text);
467 /* --------------------------------------------------------------------------------------------- */
469 char *
470 input_expand_dialog (const char *header, const char *text,
471 const char *history_name, const char *def_text)
473 char *result;
474 char *expanded;
476 result = input_dialog (header, text, history_name, def_text);
477 if (result)
479 expanded = tilde_expand (result);
480 g_free (result);
481 return expanded;
483 return result;
486 /* --------------------------------------------------------------------------------------------- */