Ticket #2598: u7z: Improve handling of missing p7zip binaries.
[midnight-commander.git] / lib / widget / wtools.c
blob380642935809e833a6b85b592fdb08a56aebf1f1
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"
42 #include "lib/event.h" /* mc_event_raise() */
44 /*** global variables ****************************************************************************/
46 /*** file scope macro definitions ****************************************************************/
48 /*** file scope type declarations ****************************************************************/
50 /*** file scope variables ************************************************************************/
52 static Dlg_head *last_query_dlg;
54 static int sel_pos = 0;
56 /*** file scope functions ************************************************************************/
57 /* --------------------------------------------------------------------------------------------- */
59 /** default query callback, used to reposition query */
61 static cb_ret_t
62 default_query_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
64 switch (msg)
66 case DLG_RESIZE:
68 Dlg_head *prev_dlg = NULL;
69 int ypos, xpos;
71 /* get dialog under h */
72 if (top_dlg != NULL)
74 if (top_dlg->data != (void *) h)
75 prev_dlg = (Dlg_head *) top_dlg->data;
76 else
78 GList *p;
80 /* Top dialog is current if it is visible.
81 Get previous dialog in stack */
82 p = g_list_next (top_dlg);
83 if (p != NULL)
84 prev_dlg = (Dlg_head *) p->data;
88 /* if previous dialog is not fullscreen'd -- overlap it */
89 if (prev_dlg == NULL || prev_dlg->fullscreen)
90 ypos = LINES / 3 - (h->lines - 3) / 2;
91 else
92 ypos = prev_dlg->y + 2;
94 xpos = COLS / 2 - h->cols / 2;
96 /* set position */
97 dlg_set_position (h, ypos, xpos, ypos + h->lines, xpos + h->cols);
99 return MSG_HANDLED;
101 default:
102 return default_dlg_callback (h, sender, msg, parm, data);
106 /* --------------------------------------------------------------------------------------------- */
107 /** Create message dialog */
109 static struct Dlg_head *
110 do_create_message (int flags, const char *title, const char *text)
112 char *p;
113 Dlg_head *d;
115 /* Add empty lines before and after the message */
116 p = g_strconcat ("\n", text, "\n", (char *) NULL);
117 query_dialog (title, p, flags, 0);
118 d = last_query_dlg;
120 /* do resize before initing and running */
121 default_query_callback (d, NULL, DLG_RESIZE, 0, NULL);
123 init_dlg (d);
124 g_free (p);
126 return d;
129 /* --------------------------------------------------------------------------------------------- */
131 * Show message dialog. Dismiss it when any key is pressed.
132 * Not safe to call from background.
135 static void
136 fg_message (int flags, const char *title, const char *text)
138 Dlg_head *d;
140 d = do_create_message (flags, title, text);
141 tty_getch ();
142 dlg_run_done (d);
143 destroy_dlg (d);
147 /* --------------------------------------------------------------------------------------------- */
148 /** Show message box from background */
150 #ifdef WITH_BACKGROUND
151 static void
152 bg_message (int dummy, int *flags, char *title, const char *text)
154 (void) dummy;
155 title = g_strconcat (_("Background process:"), " ", title, (char *) NULL);
156 fg_message (*flags, title, text);
157 g_free (title);
159 #endif /* WITH_BACKGROUND */
161 /* --------------------------------------------------------------------------------------------- */
164 * Show dialog, not background safe.
166 * If the arguments "header" and "text" should be translated,
167 * that MUST be done by the caller of fg_input_dialog_help().
169 * The argument "history_name" holds the name of a section
170 * in the history file. Data entered in the input field of
171 * the dialog box will be stored there.
174 static char *
175 fg_input_dialog_help (const char *header, const char *text, const char *help,
176 const char *history_name, const char *def_text)
178 char *my_str;
180 QuickWidget quick_widgets[] = {
181 /* 0 */ QUICK_BUTTON (6, 64, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
182 /* 1 */ QUICK_BUTTON (3, 64, 1, 0, N_("&OK"), B_ENTER, NULL),
183 /* 2 */ QUICK_INPUT (3, 64, 0, 0, def_text, 58, 0, NULL, &my_str),
184 /* 3 */ QUICK_LABEL (3, 64, 2, 0, ""),
185 QUICK_END
188 int b0_len, b1_len, b_len, gap;
189 char histname[64] = "inp|";
190 int lines, cols;
191 int len;
192 int i;
193 char *p_text;
194 int ret;
196 /* buttons */
197 #ifdef ENABLE_NLS
198 quick_widgets[0].u.button.text = _(quick_widgets[0].u.button.text);
199 quick_widgets[1].u.button.text = _(quick_widgets[1].u.button.text);
200 #endif /* ENABLE_NLS */
202 b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
203 b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 5; /* default button */
204 b_len = b0_len + b1_len + 2; /* including gap */
206 /* input line */
207 if (history_name != NULL && *history_name != '\0')
209 g_strlcpy (histname + 3, history_name, sizeof (histname) - 3);
210 quick_widgets[2].u.input.histname = histname;
213 /* The special value of def_text is used to identify password boxes
214 and hide characters with "*". Don't save passwords in history! */
215 if (def_text == INPUT_PASSWORD)
217 quick_widgets[2].u.input.flags = 1;
218 histname[3] = '\0';
219 quick_widgets[2].u.input.text = "";
222 /* text */
223 p_text = g_strstrip (g_strdup (text));
224 str_msg_term_size (p_text, &lines, &cols);
225 quick_widgets[3].u.label.text = p_text;
227 /* dialog width */
228 len = str_term_width1 (header);
229 len = max (max (len, cols) + 4, 64);
230 len = min (max (len, b_len + 6), COLS);
232 /* button locations */
233 gap = (len - 8 - b_len) / 3;
234 quick_widgets[1].relative_x = 3 + gap;
235 quick_widgets[0].relative_x = quick_widgets[1].relative_x + b1_len + gap + 2;
238 QuickDialog Quick_input = {
239 len, lines + 6, -1, -1, header,
240 help, quick_widgets, NULL, TRUE
243 for (i = 0; i < 4; i++)
245 quick_widgets[i].x_divisions = Quick_input.xlen;
246 quick_widgets[i].y_divisions = Quick_input.ylen;
249 for (i = 0; i < 3; i++)
250 quick_widgets[i].relative_y += 2 + lines;
252 /* input line length */
253 quick_widgets[2].u.input.len = Quick_input.xlen - 6;
255 ret = quick_dialog (&Quick_input);
258 g_free (p_text);
260 return (ret != B_CANCEL) ? my_str : NULL;
263 /* --------------------------------------------------------------------------------------------- */
265 #ifdef WITH_BACKGROUND
266 static int
267 wtools_parent_call (void *routine, gpointer ctx, int argc, ...)
269 ev_background_parent_call_t event_data;
271 event_data.routine = routine;
272 event_data.ctx = ctx;
273 event_data.argc = argc;
274 va_start (event_data.ap, argc);
275 mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call", (gpointer) & event_data);
276 va_end (event_data.ap);
277 return event_data.ret.i;
280 /* --------------------------------------------------------------------------------------------- */
282 static char *
283 wtools_parent_call_string (void *routine, int argc, ...)
285 ev_background_parent_call_t event_data;
287 event_data.routine = routine;
288 event_data.argc = argc;
289 va_start (event_data.ap, argc);
290 mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call_string", (gpointer) & event_data);
291 va_end (event_data.ap);
292 return event_data.ret.s;
294 #endif /* WITH_BACKGROUND */
296 /* --------------------------------------------------------------------------------------------- */
297 /*** public functions ****************************************************************************/
298 /* --------------------------------------------------------------------------------------------- */
300 /** Used to ask questions to the user */
302 query_dialog (const char *header, const char *text, int flags, int count, ...)
304 va_list ap;
305 Dlg_head *query_dlg;
306 WButton *button;
307 WButton *defbutton = NULL;
308 int win_len = 0;
309 int i;
310 int result = -1;
311 int cols, lines;
312 char *cur_name;
313 const int *query_colors = (flags & D_ERROR) ? alarm_colors : dialog_colors;
315 if (header == MSG_ERROR)
316 header = _("Error");
318 if (count > 0)
320 va_start (ap, count);
321 for (i = 0; i < count; i++)
323 char *cp = va_arg (ap, char *);
324 win_len += str_term_width1 (cp) + 6;
325 if (strchr (cp, '&') != NULL)
326 win_len--;
328 va_end (ap);
331 /* count coordinates */
332 str_msg_term_size (text, &lines, &cols);
333 cols = 6 + max (win_len, max (str_term_width1 (header), cols));
334 lines += 4 + (count > 0 ? 2 : 0);
336 /* prepare dialog */
337 query_dlg =
338 create_dlg (TRUE, 0, 0, lines, cols, query_colors, default_query_callback,
339 "[QueryBox]", header, DLG_NONE);
341 if (count > 0)
343 cols = (cols - win_len - 2) / 2 + 2;
344 va_start (ap, count);
345 for (i = 0; i < count; i++)
347 int xpos;
349 cur_name = va_arg (ap, char *);
350 xpos = str_term_width1 (cur_name) + 6;
351 if (strchr (cur_name, '&') != NULL)
352 xpos--;
354 button = button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON, cur_name, 0);
355 add_widget (query_dlg, button);
356 cols += xpos;
357 if (i == sel_pos)
358 defbutton = button;
360 va_end (ap);
362 add_widget (query_dlg, label_new (2, 3, text));
364 /* do resize before running and selecting any widget */
365 default_query_callback (query_dlg, NULL, DLG_RESIZE, 0, NULL);
367 if (defbutton)
368 dlg_select_widget (defbutton);
370 /* run dialog and make result */
371 switch (run_dlg (query_dlg))
373 case B_CANCEL:
374 break;
375 default:
376 result = query_dlg->ret_value - B_USER;
379 /* free used memory */
380 destroy_dlg (query_dlg);
382 else
384 add_widget (query_dlg, label_new (2, 3, text));
385 add_widget (query_dlg, button_new (0, 0, 0, HIDDEN_BUTTON, "-", 0));
386 last_query_dlg = query_dlg;
388 sel_pos = 0;
389 return result;
392 /* --------------------------------------------------------------------------------------------- */
394 void
395 query_set_sel (int new_sel)
397 sel_pos = new_sel;
400 /* --------------------------------------------------------------------------------------------- */
402 * Create message dialog. The caller must call dlg_run_done() and
403 * destroy_dlg() to dismiss it. Not safe to call from background.
406 struct Dlg_head *
407 create_message (int flags, const char *title, const char *text, ...)
409 va_list args;
410 Dlg_head *d;
411 char *p;
413 va_start (args, text);
414 p = g_strdup_vprintf (text, args);
415 va_end (args);
417 d = do_create_message (flags, title, p);
418 g_free (p);
420 return d;
423 /* --------------------------------------------------------------------------------------------- */
424 /** Show message box, background safe */
426 void
427 message (int flags, const char *title, const char *text, ...)
429 char *p;
430 va_list ap;
432 va_start (ap, text);
433 p = g_strdup_vprintf (text, ap);
434 va_end (ap);
436 if (title == MSG_ERROR)
437 title = _("Error");
439 #ifdef WITH_BACKGROUND
440 if (mc_global.we_are_background)
442 union
444 void *p;
445 void (*f) (int, int *, char *, const char *);
446 } func;
447 func.f = bg_message;
449 wtools_parent_call (func.p, NULL, 3, sizeof (flags), &flags, strlen (title), title,
450 strlen (p), p);
452 else
453 #endif /* WITH_BACKGROUND */
454 fg_message (flags, title, p);
456 g_free (p);
459 /* --------------------------------------------------------------------------------------------- */
461 * Show input dialog, background safe.
463 * If the arguments "header" and "text" should be translated,
464 * that MUST be done by the caller of these wrappers.
467 char *
468 input_dialog_help (const char *header, const char *text, const char *help,
469 const char *history_name, const char *def_text)
471 #ifdef WITH_BACKGROUND
472 if (mc_global.we_are_background)
474 union
476 void *p;
477 char *(*f) (const char *, const char *, const char *, const char *, const char *);
478 } func;
479 func.f = fg_input_dialog_help;
480 return wtools_parent_call_string (func.p, 5,
481 strlen (header), header, strlen (text),
482 text, strlen (help), help,
483 strlen (history_name), history_name,
484 strlen (def_text), def_text);
486 else
487 #endif /* WITH_BACKGROUND */
488 return fg_input_dialog_help (header, text, help, history_name, def_text);
491 /* --------------------------------------------------------------------------------------------- */
492 /** Show input dialog with default help, background safe */
494 char *
495 input_dialog (const char *header, const char *text, const char *history_name, const char *def_text)
497 return input_dialog_help (header, text, "[Input Line Keys]", history_name, def_text);
500 /* --------------------------------------------------------------------------------------------- */
502 char *
503 input_expand_dialog (const char *header, const char *text,
504 const char *history_name, const char *def_text)
506 char *result;
507 char *expanded;
509 result = input_dialog (header, text, history_name, def_text);
510 if (result)
512 expanded = tilde_expand (result);
513 g_free (result);
514 return expanded;
516 return result;
519 /* --------------------------------------------------------------------------------------------- */
521 void
522 mc_refresh (void)
524 #ifdef WITH_BACKGROUND
525 if (mc_global.we_are_background)
526 return;
527 #endif /* WITH_BACKGROUND */
528 if (mc_global.tty.winch_flag == FALSE)
529 tty_refresh ();
530 else
532 /* if winch was caugth, we should do not only redraw screen, but
533 reposition/resize all */
534 dialog_change_screen_size ();
538 /* --------------------------------------------------------------------------------------------- */