Ticket 1551: Update GPL version from 2 to 3
[midnight-commander.git] / lib / widget / wtools.c
blobdccacc3d9323560a84afa5913256802ff75c8949
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:
72 Dlg_head *prev_dlg = NULL;
73 int ypos, xpos;
75 /* get dialog under h */
76 if (top_dlg != NULL)
78 if (top_dlg->data != (void *) h)
79 prev_dlg = (Dlg_head *) top_dlg->data;
80 else
82 GList *p;
84 /* Top dialog is current if it is visible.
85 Get previous dialog in stack */
86 p = g_list_next (top_dlg);
87 if (p != NULL)
88 prev_dlg = (Dlg_head *) p->data;
92 /* if previous dialog is not fullscreen'd -- overlap it */
93 if (prev_dlg == NULL || prev_dlg->fullscreen)
94 ypos = LINES / 3 - (h->lines - 3) / 2;
95 else
96 ypos = prev_dlg->y + 2;
98 xpos = COLS / 2 - h->cols / 2;
100 /* set position */
101 dlg_set_position (h, ypos, xpos, ypos + h->lines, xpos + h->cols);
103 return MSG_HANDLED;
105 default:
106 return default_dlg_callback (h, sender, msg, parm, data);
110 /* --------------------------------------------------------------------------------------------- */
111 /** Create message dialog */
113 static struct Dlg_head *
114 do_create_message (int flags, const char *title, const char *text)
116 char *p;
117 Dlg_head *d;
119 /* Add empty lines before and after the message */
120 p = g_strconcat ("\n", text, "\n", (char *) NULL);
121 query_dialog (title, p, flags, 0);
122 d = last_query_dlg;
124 /* do resize before initing and running */
125 default_query_callback (d, NULL, DLG_RESIZE, 0, NULL);
127 init_dlg (d);
128 g_free (p);
130 return d;
133 /* --------------------------------------------------------------------------------------------- */
135 * Show message dialog. Dismiss it when any key is pressed.
136 * Not safe to call from background.
139 static void
140 fg_message (int flags, const char *title, const char *text)
142 Dlg_head *d;
144 d = do_create_message (flags, title, text);
145 tty_getch ();
146 dlg_run_done (d);
147 destroy_dlg (d);
151 /* --------------------------------------------------------------------------------------------- */
152 /** Show message box from background */
154 #ifdef WITH_BACKGROUND
155 static void
156 bg_message (int dummy, int *flags, char *title, const char *text)
158 (void) dummy;
159 title = g_strconcat (_("Background process:"), " ", title, (char *) NULL);
160 fg_message (*flags, title, text);
161 g_free (title);
163 #endif /* WITH_BACKGROUND */
165 /* --------------------------------------------------------------------------------------------- */
168 * Show dialog, not background safe.
170 * If the arguments "header" and "text" should be translated,
171 * that MUST be done by the caller of fg_input_dialog_help().
173 * The argument "history_name" holds the name of a section
174 * in the history file. Data entered in the input field of
175 * the dialog box will be stored there.
178 static char *
179 fg_input_dialog_help (const char *header, const char *text, const char *help,
180 const char *history_name, const char *def_text)
182 char *my_str;
184 QuickWidget quick_widgets[] = {
185 /* 0 */ QUICK_BUTTON (6, 64, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
186 /* 1 */ QUICK_BUTTON (3, 64, 1, 0, N_("&OK"), B_ENTER, NULL),
187 /* 2 */ QUICK_INPUT (3, 64, 0, 0, def_text, 58, 0, NULL, &my_str),
188 /* 3 */ QUICK_LABEL (3, 64, 2, 0, ""),
189 QUICK_END
192 int b0_len, b1_len, b_len, gap;
193 char histname[64] = "inp|";
194 int lines, cols;
195 int len;
196 int i;
197 char *p_text;
198 int ret;
200 /* buttons */
201 #ifdef ENABLE_NLS
202 quick_widgets[0].u.button.text = _(quick_widgets[0].u.button.text);
203 quick_widgets[1].u.button.text = _(quick_widgets[1].u.button.text);
204 #endif /* ENABLE_NLS */
206 b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
207 b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 5; /* default button */
208 b_len = b0_len + b1_len + 2; /* including gap */
210 /* input line */
211 if (history_name != NULL && *history_name != '\0')
213 g_strlcpy (histname + 3, history_name, sizeof (histname) - 3);
214 quick_widgets[2].u.input.histname = histname;
217 /* The special value of def_text is used to identify password boxes
218 and hide characters with "*". Don't save passwords in history! */
219 if (def_text == INPUT_PASSWORD)
221 quick_widgets[2].u.input.flags = 1;
222 histname[3] = '\0';
223 quick_widgets[2].u.input.text = "";
226 /* text */
227 p_text = g_strstrip (g_strdup (text));
228 str_msg_term_size (p_text, &lines, &cols);
229 quick_widgets[3].u.label.text = p_text;
231 /* dialog width */
232 len = str_term_width1 (header);
233 len = max (max (len, cols) + 4, 64);
234 len = min (max (len, b_len + 6), COLS);
236 /* button locations */
237 gap = (len - 8 - b_len) / 3;
238 quick_widgets[1].relative_x = 3 + gap;
239 quick_widgets[0].relative_x = quick_widgets[1].relative_x + b1_len + gap + 2;
242 QuickDialog Quick_input = {
243 len, lines + 6, -1, -1, header,
244 help, quick_widgets, NULL, TRUE
247 for (i = 0; i < 4; i++)
249 quick_widgets[i].x_divisions = Quick_input.xlen;
250 quick_widgets[i].y_divisions = Quick_input.ylen;
253 for (i = 0; i < 3; i++)
254 quick_widgets[i].relative_y += 2 + lines;
256 /* input line length */
257 quick_widgets[2].u.input.len = Quick_input.xlen - 6;
259 ret = quick_dialog (&Quick_input);
262 g_free (p_text);
264 return (ret != B_CANCEL) ? my_str : NULL;
267 /* --------------------------------------------------------------------------------------------- */
269 #ifdef WITH_BACKGROUND
270 static int
271 wtools_parent_call (void *routine, gpointer ctx, int argc, ...)
273 ev_background_parent_call_t event_data;
275 event_data.routine = routine;
276 event_data.ctx = ctx;
277 event_data.argc = argc;
278 va_start (event_data.ap, argc);
279 mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call", (gpointer) & event_data);
280 va_end (event_data.ap);
281 return event_data.ret.i;
284 /* --------------------------------------------------------------------------------------------- */
286 static char *
287 wtools_parent_call_string (void *routine, int argc, ...)
289 ev_background_parent_call_t event_data;
291 event_data.routine = routine;
292 event_data.argc = argc;
293 va_start (event_data.ap, argc);
294 mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call_string", (gpointer) & event_data);
295 va_end (event_data.ap);
296 return event_data.ret.s;
298 #endif /* WITH_BACKGROUND */
300 /* --------------------------------------------------------------------------------------------- */
301 /*** public functions ****************************************************************************/
302 /* --------------------------------------------------------------------------------------------- */
304 /** Used to ask questions to the user */
306 query_dialog (const char *header, const char *text, int flags, int count, ...)
308 va_list ap;
309 Dlg_head *query_dlg;
310 WButton *button;
311 WButton *defbutton = NULL;
312 int win_len = 0;
313 int i;
314 int result = -1;
315 int cols, lines;
316 char *cur_name;
317 const int *query_colors = (flags & D_ERROR) ? alarm_colors : dialog_colors;
319 if (header == MSG_ERROR)
320 header = _("Error");
322 if (count > 0)
324 va_start (ap, count);
325 for (i = 0; i < count; i++)
327 char *cp = va_arg (ap, char *);
328 win_len += str_term_width1 (cp) + 6;
329 if (strchr (cp, '&') != NULL)
330 win_len--;
332 va_end (ap);
335 /* count coordinates */
336 str_msg_term_size (text, &lines, &cols);
337 cols = 6 + max (win_len, max (str_term_width1 (header), cols));
338 lines += 4 + (count > 0 ? 2 : 0);
340 /* prepare dialog */
341 query_dlg =
342 create_dlg (TRUE, 0, 0, lines, cols, query_colors, default_query_callback,
343 "[QueryBox]", header, DLG_NONE);
345 if (count > 0)
347 cols = (cols - win_len - 2) / 2 + 2;
348 va_start (ap, count);
349 for (i = 0; i < count; i++)
351 int xpos;
353 cur_name = va_arg (ap, char *);
354 xpos = str_term_width1 (cur_name) + 6;
355 if (strchr (cur_name, '&') != NULL)
356 xpos--;
358 button = button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON, cur_name, 0);
359 add_widget (query_dlg, button);
360 cols += xpos;
361 if (i == sel_pos)
362 defbutton = button;
364 va_end (ap);
366 add_widget (query_dlg, label_new (2, 3, text));
368 /* do resize before running and selecting any widget */
369 default_query_callback (query_dlg, NULL, DLG_RESIZE, 0, NULL);
371 if (defbutton)
372 dlg_select_widget (defbutton);
374 /* run dialog and make result */
375 switch (run_dlg (query_dlg))
377 case B_CANCEL:
378 break;
379 default:
380 result = query_dlg->ret_value - B_USER;
383 /* free used memory */
384 destroy_dlg (query_dlg);
386 else
388 add_widget (query_dlg, label_new (2, 3, text));
389 add_widget (query_dlg, button_new (0, 0, 0, HIDDEN_BUTTON, "-", 0));
390 last_query_dlg = query_dlg;
392 sel_pos = 0;
393 return result;
396 /* --------------------------------------------------------------------------------------------- */
398 void
399 query_set_sel (int new_sel)
401 sel_pos = new_sel;
404 /* --------------------------------------------------------------------------------------------- */
406 * Create message dialog. The caller must call dlg_run_done() and
407 * destroy_dlg() to dismiss it. Not safe to call from background.
410 struct Dlg_head *
411 create_message (int flags, const char *title, const char *text, ...)
413 va_list args;
414 Dlg_head *d;
415 char *p;
417 va_start (args, text);
418 p = g_strdup_vprintf (text, args);
419 va_end (args);
421 d = do_create_message (flags, title, p);
422 g_free (p);
424 return d;
427 /* --------------------------------------------------------------------------------------------- */
428 /** Show message box, background safe */
430 void
431 message (int flags, const char *title, const char *text, ...)
433 char *p;
434 va_list ap;
436 va_start (ap, text);
437 p = g_strdup_vprintf (text, ap);
438 va_end (ap);
440 if (title == MSG_ERROR)
441 title = _("Error");
443 #ifdef WITH_BACKGROUND
444 if (mc_global.we_are_background)
446 union
448 void *p;
449 void (*f) (int, int *, char *, const char *);
450 } func;
451 func.f = bg_message;
453 wtools_parent_call (func.p, NULL, 3, sizeof (flags), &flags, strlen (title), title,
454 strlen (p), p);
456 else
457 #endif /* WITH_BACKGROUND */
458 fg_message (flags, title, p);
460 g_free (p);
463 /* --------------------------------------------------------------------------------------------- */
465 * Show input dialog, background safe.
467 * If the arguments "header" and "text" should be translated,
468 * that MUST be done by the caller of these wrappers.
471 char *
472 input_dialog_help (const char *header, const char *text, const char *help,
473 const char *history_name, const char *def_text)
475 #ifdef WITH_BACKGROUND
476 if (mc_global.we_are_background)
478 union
480 void *p;
481 char *(*f) (const char *, const char *, const char *, const char *, const char *);
482 } func;
483 func.f = fg_input_dialog_help;
484 return wtools_parent_call_string (func.p, 5,
485 strlen (header), header, strlen (text),
486 text, strlen (help), help,
487 strlen (history_name), history_name,
488 strlen (def_text), def_text);
490 else
491 #endif /* WITH_BACKGROUND */
492 return fg_input_dialog_help (header, text, help, history_name, def_text);
495 /* --------------------------------------------------------------------------------------------- */
496 /** Show input dialog with default help, background safe */
498 char *
499 input_dialog (const char *header, const char *text, const char *history_name, const char *def_text)
501 return input_dialog_help (header, text, "[Input Line Keys]", history_name, def_text);
504 /* --------------------------------------------------------------------------------------------- */
506 char *
507 input_expand_dialog (const char *header, const char *text,
508 const char *history_name, const char *def_text)
510 char *result;
511 char *expanded;
513 result = input_dialog (header, text, history_name, def_text);
514 if (result)
516 expanded = tilde_expand (result);
517 g_free (result);
518 return expanded;
520 return result;
523 /* --------------------------------------------------------------------------------------------- */