Added D_CENTER flag to query dialog flags
[midnight-commander.git] / lib / widget / wtools.c
blob1b8a47f9f4d8ca688f17bf345100d1d3e0097fc1
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 Dlg_head *prev_dlg = NULL;
74 int ypos, xpos;
76 /* get dialog under h */
77 if (top_dlg != NULL)
79 if (top_dlg->data != (void *) h)
80 prev_dlg = (Dlg_head *) top_dlg->data;
81 else
83 GList *p;
85 /* Top dialog is current if it is visible.
86 Get previous dialog in stack */
87 p = g_list_next (top_dlg);
88 if (p != NULL)
89 prev_dlg = (Dlg_head *) p->data;
93 /* if previous dialog is not fullscreen'd -- overlap it */
94 if (prev_dlg == NULL || prev_dlg->fullscreen)
95 ypos = LINES / 3 - (h->lines - 3) / 2;
96 else
97 ypos = prev_dlg->y + 2;
99 xpos = COLS / 2 - h->cols / 2;
101 /* set position */
102 dlg_set_position (h, ypos, xpos, ypos + h->lines, xpos + h->cols);
104 return MSG_HANDLED;
106 /* fallthrough */
108 default:
109 return default_dlg_callback (h, sender, msg, parm, data);
113 /* --------------------------------------------------------------------------------------------- */
114 /** Create message dialog */
116 static struct Dlg_head *
117 do_create_message (int flags, const char *title, const char *text)
119 char *p;
120 Dlg_head *d;
122 /* Add empty lines before and after the message */
123 p = g_strconcat ("\n", text, "\n", (char *) NULL);
124 query_dialog (title, p, flags, 0);
125 d = last_query_dlg;
127 /* do resize before initing and running */
128 default_query_callback (d, NULL, DLG_RESIZE, 0, NULL);
130 init_dlg (d);
131 g_free (p);
133 return d;
136 /* --------------------------------------------------------------------------------------------- */
138 * Show message dialog. Dismiss it when any key is pressed.
139 * Not safe to call from background.
142 static void
143 fg_message (int flags, const char *title, const char *text)
145 Dlg_head *d;
147 d = do_create_message (flags, title, text);
148 tty_getch ();
149 dlg_run_done (d);
150 destroy_dlg (d);
154 /* --------------------------------------------------------------------------------------------- */
155 /** Show message box from background */
157 #ifdef WITH_BACKGROUND
158 static void
159 bg_message (int dummy, int *flags, char *title, const char *text)
161 (void) dummy;
162 title = g_strconcat (_("Background process:"), " ", title, (char *) NULL);
163 fg_message (*flags, title, text);
164 g_free (title);
166 #endif /* WITH_BACKGROUND */
168 /* --------------------------------------------------------------------------------------------- */
171 * Show dialog, not background safe.
173 * If the arguments "header" and "text" should be translated,
174 * that MUST be done by the caller of fg_input_dialog_help().
176 * The argument "history_name" holds the name of a section
177 * in the history file. Data entered in the input field of
178 * the dialog box will be stored there.
181 static char *
182 fg_input_dialog_help (const char *header, const char *text, const char *help,
183 const char *history_name, const char *def_text)
185 char *my_str;
187 QuickWidget quick_widgets[] = {
188 /* 0 */ QUICK_BUTTON (6, 64, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
189 /* 1 */ QUICK_BUTTON (3, 64, 1, 0, N_("&OK"), B_ENTER, NULL),
190 /* 2 */ QUICK_INPUT (3, 64, 0, 0, def_text, 58, 0, NULL, &my_str),
191 /* 3 */ QUICK_LABEL (3, 64, 2, 0, ""),
192 QUICK_END
195 int b0_len, b1_len, b_len, gap;
196 char histname[64] = "inp|";
197 int lines, cols;
198 int len;
199 int i;
200 char *p_text;
201 int ret;
203 /* buttons */
204 #ifdef ENABLE_NLS
205 quick_widgets[0].u.button.text = _(quick_widgets[0].u.button.text);
206 quick_widgets[1].u.button.text = _(quick_widgets[1].u.button.text);
207 #endif /* ENABLE_NLS */
209 b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
210 b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 5; /* default button */
211 b_len = b0_len + b1_len + 2; /* including gap */
213 /* input line */
214 if (history_name != NULL && *history_name != '\0')
216 g_strlcpy (histname + 3, history_name, sizeof (histname) - 3);
217 quick_widgets[2].u.input.histname = histname;
220 /* The special value of def_text is used to identify password boxes
221 and hide characters with "*". Don't save passwords in history! */
222 if (def_text == INPUT_PASSWORD)
224 quick_widgets[2].u.input.flags = 1;
225 histname[3] = '\0';
226 quick_widgets[2].u.input.text = "";
229 /* text */
230 p_text = g_strstrip (g_strdup (text));
231 str_msg_term_size (p_text, &lines, &cols);
232 quick_widgets[3].u.label.text = p_text;
234 /* dialog width */
235 len = str_term_width1 (header);
236 len = max (max (len, cols) + 4, 64);
237 len = min (max (len, b_len + 6), COLS);
239 /* button locations */
240 gap = (len - 8 - b_len) / 3;
241 quick_widgets[1].relative_x = 3 + gap;
242 quick_widgets[0].relative_x = quick_widgets[1].relative_x + b1_len + gap + 2;
245 QuickDialog Quick_input = {
246 len, lines + 6, -1, -1, header,
247 help, quick_widgets, NULL, TRUE
250 for (i = 0; i < 4; i++)
252 quick_widgets[i].x_divisions = Quick_input.xlen;
253 quick_widgets[i].y_divisions = Quick_input.ylen;
256 for (i = 0; i < 3; i++)
257 quick_widgets[i].relative_y += 2 + lines;
259 /* input line length */
260 quick_widgets[2].u.input.len = Quick_input.xlen - 6;
262 ret = quick_dialog (&Quick_input);
265 g_free (p_text);
267 return (ret != B_CANCEL) ? my_str : NULL;
270 /* --------------------------------------------------------------------------------------------- */
272 #ifdef WITH_BACKGROUND
273 static int
274 wtools_parent_call (void *routine, gpointer ctx, int argc, ...)
276 ev_background_parent_call_t event_data;
278 event_data.routine = routine;
279 event_data.ctx = ctx;
280 event_data.argc = argc;
281 va_start (event_data.ap, argc);
282 mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call", (gpointer) & event_data);
283 va_end (event_data.ap);
284 return event_data.ret.i;
287 /* --------------------------------------------------------------------------------------------- */
289 static char *
290 wtools_parent_call_string (void *routine, int argc, ...)
292 ev_background_parent_call_t event_data;
294 event_data.routine = routine;
295 event_data.argc = argc;
296 va_start (event_data.ap, argc);
297 mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call_string", (gpointer) & event_data);
298 va_end (event_data.ap);
299 return event_data.ret.s;
301 #endif /* WITH_BACKGROUND */
303 /* --------------------------------------------------------------------------------------------- */
304 /*** public functions ****************************************************************************/
305 /* --------------------------------------------------------------------------------------------- */
307 /** Used to ask questions to the user */
309 query_dialog (const char *header, const char *text, int flags, int count, ...)
311 va_list ap;
312 Dlg_head *query_dlg;
313 WButton *button;
314 WButton *defbutton = NULL;
315 int win_len = 0;
316 int i;
317 int result = -1;
318 int cols, lines;
319 char *cur_name;
320 const int *query_colors = (flags & D_ERROR) != 0 ? alarm_colors : dialog_colors;
321 dlg_flags_t dlg_flags = (flags & D_CENTER) != 0 ? (DLG_CENTER | DLG_TRYUP) : DLG_NONE;
323 if (header == MSG_ERROR)
324 header = _("Error");
326 if (count > 0)
328 va_start (ap, count);
329 for (i = 0; i < count; i++)
331 char *cp = va_arg (ap, char *);
332 win_len += str_term_width1 (cp) + 6;
333 if (strchr (cp, '&') != NULL)
334 win_len--;
336 va_end (ap);
339 /* count coordinates */
340 str_msg_term_size (text, &lines, &cols);
341 cols = 6 + max (win_len, max (str_term_width1 (header), cols));
342 lines += 4 + (count > 0 ? 2 : 0);
344 /* prepare dialog */
345 query_dlg =
346 create_dlg (TRUE, 0, 0, lines, cols, query_colors, default_query_callback,
347 "[QueryBox]", header, dlg_flags);
349 if (count > 0)
351 cols = (cols - win_len - 2) / 2 + 2;
352 va_start (ap, count);
353 for (i = 0; i < count; i++)
355 int xpos;
357 cur_name = va_arg (ap, char *);
358 xpos = str_term_width1 (cur_name) + 6;
359 if (strchr (cur_name, '&') != NULL)
360 xpos--;
362 button = button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON, cur_name, 0);
363 add_widget (query_dlg, button);
364 cols += xpos;
365 if (i == sel_pos)
366 defbutton = button;
368 va_end (ap);
370 add_widget (query_dlg, label_new (2, 3, text));
372 /* do resize before running and selecting any widget */
373 default_query_callback (query_dlg, NULL, DLG_RESIZE, 0, NULL);
375 if (defbutton)
376 dlg_select_widget (defbutton);
378 /* run dialog and make result */
379 switch (run_dlg (query_dlg))
381 case B_CANCEL:
382 break;
383 default:
384 result = query_dlg->ret_value - B_USER;
387 /* free used memory */
388 destroy_dlg (query_dlg);
390 else
392 add_widget (query_dlg, label_new (2, 3, text));
393 add_widget (query_dlg, button_new (0, 0, 0, HIDDEN_BUTTON, "-", 0));
394 last_query_dlg = query_dlg;
396 sel_pos = 0;
397 return result;
400 /* --------------------------------------------------------------------------------------------- */
402 void
403 query_set_sel (int new_sel)
405 sel_pos = new_sel;
408 /* --------------------------------------------------------------------------------------------- */
410 * Create message dialog. The caller must call dlg_run_done() and
411 * destroy_dlg() to dismiss it. Not safe to call from background.
414 struct Dlg_head *
415 create_message (int flags, const char *title, const char *text, ...)
417 va_list args;
418 Dlg_head *d;
419 char *p;
421 va_start (args, text);
422 p = g_strdup_vprintf (text, args);
423 va_end (args);
425 d = do_create_message (flags, title, p);
426 g_free (p);
428 return d;
431 /* --------------------------------------------------------------------------------------------- */
432 /** Show message box, background safe */
434 void
435 message (int flags, const char *title, const char *text, ...)
437 char *p;
438 va_list ap;
440 va_start (ap, text);
441 p = g_strdup_vprintf (text, ap);
442 va_end (ap);
444 if (title == MSG_ERROR)
445 title = _("Error");
447 #ifdef WITH_BACKGROUND
448 if (mc_global.we_are_background)
450 union
452 void *p;
453 void (*f) (int, int *, char *, const char *);
454 } func;
455 func.f = bg_message;
457 wtools_parent_call (func.p, NULL, 3, sizeof (flags), &flags, strlen (title), title,
458 strlen (p), p);
460 else
461 #endif /* WITH_BACKGROUND */
462 fg_message (flags, title, p);
464 g_free (p);
467 /* --------------------------------------------------------------------------------------------- */
469 * Show input dialog, background safe.
471 * If the arguments "header" and "text" should be translated,
472 * that MUST be done by the caller of these wrappers.
475 char *
476 input_dialog_help (const char *header, const char *text, const char *help,
477 const char *history_name, const char *def_text)
479 #ifdef WITH_BACKGROUND
480 if (mc_global.we_are_background)
482 union
484 void *p;
485 char *(*f) (const char *, const char *, const char *, const char *, const char *);
486 } func;
487 func.f = fg_input_dialog_help;
488 return wtools_parent_call_string (func.p, 5,
489 strlen (header), header, strlen (text),
490 text, strlen (help), help,
491 strlen (history_name), history_name,
492 strlen (def_text), def_text);
494 else
495 #endif /* WITH_BACKGROUND */
496 return fg_input_dialog_help (header, text, help, history_name, def_text);
499 /* --------------------------------------------------------------------------------------------- */
500 /** Show input dialog with default help, background safe */
502 char *
503 input_dialog (const char *header, const char *text, const char *history_name, const char *def_text)
505 return input_dialog_help (header, text, "[Input Line Keys]", history_name, def_text);
508 /* --------------------------------------------------------------------------------------------- */
510 char *
511 input_expand_dialog (const char *header, const char *text,
512 const char *history_name, const char *def_text)
514 char *result;
515 char *expanded;
517 result = input_dialog (header, text, history_name, def_text);
518 if (result)
520 expanded = tilde_expand (result);
521 g_free (result);
522 return expanded;
524 return result;
527 /* --------------------------------------------------------------------------------------------- */