Ticket #2919: widget system improvements and unifications.
[midnight-commander.git] / lib / widget / wtools.c
blobf56572880c88a0f3c54d79519dbfe6ae5f0b4d05
1 /*
2 Widget based utility functions.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2008, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 Authors:
9 Miguel de Icaza, 1994, 1995, 1996
10 Radek Doulik, 1994, 1995
11 Jakub Jelinek, 1995
12 Andrej Borsenkow, 1995
13 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
15 This file is part of the Midnight Commander.
17 The Midnight Commander is free software: you can redistribute it
18 and/or modify it under the terms of the GNU General Public License as
19 published by the Free Software Foundation, either version 3 of the License,
20 or (at your option) any later version.
22 The Midnight Commander is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 /** \file wtools.c
32 * \brief Source: widget based utility functions
35 #include <config.h>
37 #include <stdarg.h>
38 #include <stdlib.h>
40 #include "lib/global.h"
41 #include "lib/tty/tty.h"
42 #include "lib/tty/key.h" /* tty_getch() */
43 #include "lib/strutil.h"
44 #include "lib/util.h" /* tilde_expand() */
45 #include "lib/widget.h"
46 #include "lib/event.h" /* mc_event_raise() */
48 /*** global variables ****************************************************************************/
50 /*** file scope macro definitions ****************************************************************/
52 /*** file scope type declarations ****************************************************************/
54 /*** file scope variables ************************************************************************/
56 static Dlg_head *last_query_dlg;
58 static int sel_pos = 0;
60 /*** file scope functions ************************************************************************/
61 /* --------------------------------------------------------------------------------------------- */
63 /** default query callback, used to reposition query */
65 static cb_ret_t
66 default_query_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
68 switch (msg)
70 case DLG_RESIZE:
71 if ((h->flags & DLG_CENTER) == 0)
73 Widget *wh = WIDGET (h);
74 Dlg_head *prev_dlg = NULL;
75 int ypos, xpos;
77 /* get dialog under h */
78 if (top_dlg != NULL)
80 if (top_dlg->data != (void *) h)
81 prev_dlg = (Dlg_head *) top_dlg->data;
82 else
84 GList *p;
86 /* Top dialog is current if it is visible.
87 Get previous dialog in stack */
88 p = g_list_next (top_dlg);
89 if (p != NULL)
90 prev_dlg = (Dlg_head *) p->data;
94 /* if previous dialog is not fullscreen'd -- overlap it */
95 if (prev_dlg == NULL || prev_dlg->fullscreen)
96 ypos = LINES / 3 - (wh->lines - 3) / 2;
97 else
98 ypos = WIDGET (prev_dlg)->y + 2;
100 xpos = COLS / 2 - wh->cols / 2;
102 /* set position */
103 dlg_set_position (h, ypos, xpos, ypos + wh->lines, xpos + wh->cols);
105 return MSG_HANDLED;
107 /* fallthrough */
109 default:
110 return default_dlg_callback (h, sender, msg, parm, data);
114 /* --------------------------------------------------------------------------------------------- */
115 /** Create message dialog */
117 static struct Dlg_head *
118 do_create_message (int flags, const char *title, const char *text)
120 char *p;
121 Dlg_head *d;
123 /* Add empty lines before and after the message */
124 p = g_strconcat ("\n", text, "\n", (char *) NULL);
125 query_dialog (title, p, flags, 0);
126 d = last_query_dlg;
128 /* do resize before initing and running */
129 default_query_callback (d, NULL, DLG_RESIZE, 0, NULL);
131 init_dlg (d);
132 g_free (p);
134 return d;
137 /* --------------------------------------------------------------------------------------------- */
139 * Show message dialog. Dismiss it when any key is pressed.
140 * Not safe to call from background.
143 static void
144 fg_message (int flags, const char *title, const char *text)
146 Dlg_head *d;
148 d = do_create_message (flags, title, text);
149 tty_getch ();
150 dlg_run_done (d);
151 destroy_dlg (d);
155 /* --------------------------------------------------------------------------------------------- */
156 /** Show message box from background */
158 #ifdef ENABLE_BACKGROUND
159 static void
160 bg_message (int dummy, int *flags, char *title, const char *text)
162 (void) dummy;
163 title = g_strconcat (_("Background process:"), " ", title, (char *) NULL);
164 fg_message (*flags, title, text);
165 g_free (title);
167 #endif /* ENABLE_BACKGROUND */
169 /* --------------------------------------------------------------------------------------------- */
172 * Show dialog, not background safe.
174 * If the arguments "header" and "text" should be translated,
175 * that MUST be done by the caller of fg_input_dialog_help().
177 * The argument "history_name" holds the name of a section
178 * in the history file. Data entered in the input field of
179 * the dialog box will be stored there.
182 static char *
183 fg_input_dialog_help (const char *header, const char *text, const char *help,
184 const char *history_name, const char *def_text, gboolean strip_password)
186 char *my_str;
187 int flags = (strip_password) ? 4 : 0;
188 QuickWidget quick_widgets[] = {
189 /* 0 */ QUICK_BUTTON (6, 64, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
190 /* 1 */ QUICK_BUTTON (3, 64, 1, 0, N_("&OK"), B_ENTER, NULL),
191 /* 2 */ QUICK_INPUT (3, 64, 0, 0, def_text, 58, flags, NULL, &my_str),
192 /* 3 */ QUICK_LABEL (3, 64, 2, 0, ""),
193 QUICK_END
196 int b0_len, b1_len, b_len, gap;
197 char histname[64] = "inp|";
198 int lines, cols;
199 int len;
200 int i;
201 char *p_text;
202 int ret;
204 /* buttons */
205 #ifdef ENABLE_NLS
206 quick_widgets[0].u.button.text = _(quick_widgets[0].u.button.text);
207 quick_widgets[1].u.button.text = _(quick_widgets[1].u.button.text);
208 #endif /* ENABLE_NLS */
210 b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
211 b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 5; /* default button */
212 b_len = b0_len + b1_len + 2; /* including gap */
214 /* input line */
215 if (history_name != NULL && *history_name != '\0')
217 g_strlcpy (histname + 3, history_name, sizeof (histname) - 3);
218 quick_widgets[2].u.input.histname = histname;
221 /* The special value of def_text is used to identify password boxes
222 and hide characters with "*". Don't save passwords in history! */
223 if (def_text == INPUT_PASSWORD)
225 quick_widgets[2].u.input.flags = 1;
226 histname[3] = '\0';
227 quick_widgets[2].u.input.text = "";
230 /* text */
231 p_text = g_strstrip (g_strdup (text));
232 str_msg_term_size (p_text, &lines, &cols);
233 quick_widgets[3].u.label.text = p_text;
235 /* dialog width */
236 len = str_term_width1 (header);
237 len = max (max (len, cols) + 4, 64);
238 len = min (max (len, b_len + 6), COLS);
240 /* button locations */
241 gap = (len - 8 - b_len) / 3;
242 quick_widgets[1].relative_x = 3 + gap;
243 quick_widgets[0].relative_x = quick_widgets[1].relative_x + b1_len + gap + 2;
246 QuickDialog Quick_input = {
247 len, lines + 6, -1, -1, header,
248 help, quick_widgets, NULL, NULL, TRUE
251 for (i = 0; i < 4; i++)
253 quick_widgets[i].x_divisions = Quick_input.xlen;
254 quick_widgets[i].y_divisions = Quick_input.ylen;
257 for (i = 0; i < 3; i++)
258 quick_widgets[i].relative_y += 2 + lines;
260 /* input line length */
261 quick_widgets[2].u.input.len = Quick_input.xlen - 6;
263 ret = quick_dialog (&Quick_input);
266 g_free (p_text);
268 return (ret != B_CANCEL) ? my_str : NULL;
271 /* --------------------------------------------------------------------------------------------- */
273 #ifdef ENABLE_BACKGROUND
274 static int
275 wtools_parent_call (void *routine, gpointer ctx, int argc, ...)
277 ev_background_parent_call_t event_data;
279 event_data.routine = routine;
280 event_data.ctx = ctx;
281 event_data.argc = argc;
282 va_start (event_data.ap, argc);
283 mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call", (gpointer) & event_data);
284 va_end (event_data.ap);
285 return event_data.ret.i;
288 /* --------------------------------------------------------------------------------------------- */
290 static char *
291 wtools_parent_call_string (void *routine, int argc, ...)
293 ev_background_parent_call_t event_data;
295 event_data.routine = routine;
296 event_data.argc = argc;
297 va_start (event_data.ap, argc);
298 mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call_string", (gpointer) & event_data);
299 va_end (event_data.ap);
300 return event_data.ret.s;
302 #endif /* ENABLE_BACKGROUND */
304 /* --------------------------------------------------------------------------------------------- */
305 /*** public functions ****************************************************************************/
306 /* --------------------------------------------------------------------------------------------- */
308 /** Used to ask questions to the user */
310 query_dialog (const char *header, const char *text, int flags, int count, ...)
312 va_list ap;
313 Dlg_head *query_dlg;
314 WButton *button;
315 WButton *defbutton = NULL;
316 int win_len = 0;
317 int i;
318 int result = -1;
319 int cols, lines;
320 char *cur_name;
321 const int *query_colors = (flags & D_ERROR) != 0 ? alarm_colors : dialog_colors;
322 dlg_flags_t dlg_flags = (flags & D_CENTER) != 0 ? (DLG_CENTER | DLG_TRYUP) : DLG_NONE;
324 if (header == MSG_ERROR)
325 header = _("Error");
327 if (count > 0)
329 va_start (ap, count);
330 for (i = 0; i < count; i++)
332 char *cp = va_arg (ap, char *);
333 win_len += str_term_width1 (cp) + 6;
334 if (strchr (cp, '&') != NULL)
335 win_len--;
337 va_end (ap);
340 /* count coordinates */
341 str_msg_term_size (text, &lines, &cols);
342 cols = 6 + max (win_len, max (str_term_width1 (header), cols));
343 lines += 4 + (count > 0 ? 2 : 0);
345 /* prepare dialog */
346 query_dlg =
347 create_dlg (TRUE, 0, 0, lines, cols, query_colors, default_query_callback, NULL,
348 "[QueryBox]", header, dlg_flags);
350 if (count > 0)
352 cols = (cols - win_len - 2) / 2 + 2;
353 va_start (ap, count);
354 for (i = 0; i < count; i++)
356 int xpos;
358 cur_name = va_arg (ap, char *);
359 xpos = str_term_width1 (cur_name) + 6;
360 if (strchr (cur_name, '&') != NULL)
361 xpos--;
363 button = button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON, cur_name, 0);
364 add_widget (query_dlg, button);
365 cols += xpos;
366 if (i == sel_pos)
367 defbutton = button;
369 va_end (ap);
371 add_widget (query_dlg, label_new (2, 3, text));
373 /* do resize before running and selecting any widget */
374 default_query_callback (query_dlg, NULL, DLG_RESIZE, 0, NULL);
376 if (defbutton)
377 dlg_select_widget (defbutton);
379 /* run dialog and make result */
380 switch (run_dlg (query_dlg))
382 case B_CANCEL:
383 break;
384 default:
385 result = query_dlg->ret_value - B_USER;
388 /* free used memory */
389 destroy_dlg (query_dlg);
391 else
393 add_widget (query_dlg, label_new (2, 3, text));
394 add_widget (query_dlg, button_new (0, 0, 0, HIDDEN_BUTTON, "-", 0));
395 last_query_dlg = query_dlg;
397 sel_pos = 0;
398 return result;
401 /* --------------------------------------------------------------------------------------------- */
403 void
404 query_set_sel (int new_sel)
406 sel_pos = new_sel;
409 /* --------------------------------------------------------------------------------------------- */
411 * Create message dialog. The caller must call dlg_run_done() and
412 * destroy_dlg() to dismiss it. Not safe to call from background.
415 struct Dlg_head *
416 create_message (int flags, const char *title, const char *text, ...)
418 va_list args;
419 Dlg_head *d;
420 char *p;
422 va_start (args, text);
423 p = g_strdup_vprintf (text, args);
424 va_end (args);
426 d = do_create_message (flags, title, p);
427 g_free (p);
429 return d;
432 /* --------------------------------------------------------------------------------------------- */
433 /** Show message box, background safe */
435 void
436 message (int flags, const char *title, const char *text, ...)
438 char *p;
439 va_list ap;
441 va_start (ap, text);
442 p = g_strdup_vprintf (text, ap);
443 va_end (ap);
445 if (title == MSG_ERROR)
446 title = _("Error");
448 #ifdef ENABLE_BACKGROUND
449 if (mc_global.we_are_background)
451 union
453 void *p;
454 void (*f) (int, int *, char *, const char *);
455 } func;
456 func.f = bg_message;
458 wtools_parent_call (func.p, NULL, 3, sizeof (flags), &flags, strlen (title), title,
459 strlen (p), p);
461 else
462 #endif /* ENABLE_BACKGROUND */
463 fg_message (flags, title, p);
465 g_free (p);
468 /* --------------------------------------------------------------------------------------------- */
470 * Show input dialog, background safe.
472 * If the arguments "header" and "text" should be translated,
473 * that MUST be done by the caller of these wrappers.
476 char *
477 input_dialog_help (const char *header, const char *text, const char *help,
478 const char *history_name, const char *def_text, gboolean strip_password)
480 #ifdef ENABLE_BACKGROUND
481 if (mc_global.we_are_background)
483 union
485 void *p;
486 char *(*f) (const char *, const char *, const char *, const char *, const char *,
487 gboolean);
488 } func;
489 func.f = fg_input_dialog_help;
490 return wtools_parent_call_string (func.p, 6,
491 strlen (header), header, strlen (text),
492 text, strlen (help), help,
493 strlen (history_name), history_name,
494 strlen (def_text), def_text,
495 sizeof (gboolean), strip_password);
497 else
498 #endif /* ENABLE_BACKGROUND */
499 return fg_input_dialog_help (header, text, help, history_name, def_text, strip_password);
502 /* --------------------------------------------------------------------------------------------- */
503 /** Show input dialog with default help, background safe */
505 char *
506 input_dialog (const char *header, const char *text, const char *history_name, const char *def_text)
508 return input_dialog_help (header, text, "[Input Line Keys]", history_name, def_text, FALSE);
511 /* --------------------------------------------------------------------------------------------- */
513 char *
514 input_expand_dialog (const char *header, const char *text,
515 const char *history_name, const char *def_text)
517 char *result;
518 char *expanded;
520 result = input_dialog (header, text, history_name, def_text);
521 if (result)
523 expanded = tilde_expand (result);
524 g_free (result);
525 return expanded;
527 return result;
530 /* --------------------------------------------------------------------------------------------- */