By default, WOP_WANT_HOTKEY option is off.
[midnight-commander.git] / lib / widget / wtools.c
bloba98d6e751897751a37ab41749014ae1750eaa187
1 /*
2 Widget based utility functions.
4 Copyright (C) 1994-2016
5 Free Software Foundation, Inc.
7 Authors:
8 Miguel de Icaza, 1994, 1995, 1996
9 Radek Doulik, 1994, 1995
10 Jakub Jelinek, 1995
11 Andrej Borsenkow, 1995
12 Andrew Borodin <aborodin@vmail.ru>, 2009-2014
14 This file is part of the Midnight Commander.
16 The Midnight Commander is free software: you can redistribute it
17 and/or modify it under the terms of the GNU General Public License as
18 published by the Free Software Foundation, either version 3 of the License,
19 or (at your option) any later version.
21 The Midnight Commander is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 /** \file wtools.c
31 * \brief Source: widget based utility functions
34 #include <config.h>
36 #include <stdarg.h>
37 #include <stdlib.h>
39 #include "lib/global.h"
40 #include "lib/tty/tty.h"
41 #include "lib/tty/key.h" /* tty_getch() */
42 #include "lib/strutil.h"
43 #include "lib/util.h" /* tilde_expand() */
44 #include "lib/widget.h"
45 #include "lib/event.h" /* mc_event_raise() */
47 /*** global variables ****************************************************************************/
49 /*** file scope macro definitions ****************************************************************/
51 /*** file scope type declarations ****************************************************************/
53 /*** file scope variables ************************************************************************/
55 static WDialog *last_query_dlg;
57 static int sel_pos = 0;
59 /* --------------------------------------------------------------------------------------------- */
60 /*** file scope functions ************************************************************************/
61 /* --------------------------------------------------------------------------------------------- */
63 /** default query callback, used to reposition query */
65 static cb_ret_t
66 query_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
68 WDialog *h = DIALOG (w);
70 switch (msg)
72 case MSG_RESIZE:
73 if ((h->flags & DLG_CENTER) == 0)
75 WDialog *prev_dlg = NULL;
76 int ypos, xpos;
78 /* get dialog under h */
79 if (top_dlg != NULL)
81 if (top_dlg->data != (void *) h)
82 prev_dlg = DIALOG (top_dlg->data);
83 else
85 GList *p;
87 /* Top dialog is current if it is visible.
88 Get previous dialog in stack */
89 p = g_list_next (top_dlg);
90 if (p != NULL)
91 prev_dlg = DIALOG (p->data);
95 /* if previous dialog is not fullscreen'd -- overlap it */
96 if (prev_dlg == NULL || prev_dlg->fullscreen)
97 ypos = LINES / 3 - (w->lines - 3) / 2;
98 else
99 ypos = WIDGET (prev_dlg)->y + 2;
101 xpos = COLS / 2 - w->cols / 2;
103 /* set position */
104 dlg_set_position (h, ypos, xpos, ypos + w->lines, xpos + w->cols);
106 return MSG_HANDLED;
108 /* fallthrough */
110 default:
111 return dlg_default_callback (w, sender, msg, parm, data);
115 /* --------------------------------------------------------------------------------------------- */
116 /** Create message dialog */
118 static WDialog *
119 do_create_message (int flags, const char *title, const char *text)
121 char *p;
122 WDialog *d;
124 /* Add empty lines before and after the message */
125 p = g_strconcat ("\n", text, "\n", (char *) NULL);
126 query_dialog (title, p, flags, 0);
127 d = last_query_dlg;
129 /* do resize before initing and running */
130 send_message (d, NULL, MSG_RESIZE, 0, NULL);
132 dlg_init (d);
133 g_free (p);
135 return d;
138 /* --------------------------------------------------------------------------------------------- */
140 * Show message dialog. Dismiss it when any key is pressed.
141 * Not safe to call from background.
144 static void
145 fg_message (int flags, const char *title, const char *text)
147 WDialog *d;
149 d = do_create_message (flags, title, text);
150 tty_getch ();
151 dlg_run_done (d);
152 dlg_destroy (d);
156 /* --------------------------------------------------------------------------------------------- */
157 /** Show message box from background */
159 #ifdef ENABLE_BACKGROUND
160 static void
161 bg_message (int dummy, int *flags, char *title, const char *text)
163 (void) dummy;
164 title = g_strconcat (_("Background process:"), " ", title, (char *) NULL);
165 fg_message (*flags, title, text);
166 g_free (title);
168 #endif /* ENABLE_BACKGROUND */
170 /* --------------------------------------------------------------------------------------------- */
173 * Show dialog, not background safe.
175 * If the arguments "header" and "text" should be translated,
176 * that MUST be done by the caller of fg_input_dialog_help().
178 * The argument "history_name" holds the name of a section
179 * in the history file. Data entered in the input field of
180 * the dialog box will be stored there.
183 static char *
184 fg_input_dialog_help (const char *header, const char *text, const char *help,
185 const char *history_name, const char *def_text, gboolean strip_password,
186 input_complete_t completion_flags)
188 char *p_text;
189 char histname[64] = "inp|";
190 gboolean is_passwd = FALSE;
191 char *my_str;
192 int ret;
194 /* label text */
195 p_text = g_strstrip (g_strdup (text));
197 /* input history */
198 if (history_name != NULL && *history_name != '\0')
199 g_strlcpy (histname + 3, history_name, sizeof (histname) - 3);
201 /* The special value of def_text is used to identify password boxes
202 and hide characters with "*". Don't save passwords in history! */
203 if (def_text == INPUT_PASSWORD)
205 is_passwd = TRUE;
206 histname[3] = '\0';
207 def_text = "";
211 quick_widget_t quick_widgets[] = {
212 /* *INDENT-OFF* */
213 QUICK_LABELED_INPUT (p_text, input_label_above, def_text, histname, &my_str,
214 NULL, is_passwd, strip_password, completion_flags),
215 QUICK_BUTTONS_OK_CANCEL,
216 QUICK_END
217 /* *INDENT-ON* */
220 quick_dialog_t qdlg = {
221 -1, -1, COLS / 2, header,
222 help, quick_widgets, NULL, NULL
225 ret = quick_dialog (&qdlg);
228 g_free (p_text);
230 return (ret != B_CANCEL) ? my_str : NULL;
233 /* --------------------------------------------------------------------------------------------- */
235 #ifdef ENABLE_BACKGROUND
236 static int
237 wtools_parent_call (void *routine, gpointer ctx, int argc, ...)
239 ev_background_parent_call_t event_data;
241 event_data.routine = routine;
242 event_data.ctx = ctx;
243 event_data.argc = argc;
244 va_start (event_data.ap, argc);
245 mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call", (gpointer) & event_data);
246 va_end (event_data.ap);
247 return event_data.ret.i;
250 /* --------------------------------------------------------------------------------------------- */
252 static char *
253 wtools_parent_call_string (void *routine, int argc, ...)
255 ev_background_parent_call_t event_data;
257 event_data.routine = routine;
258 event_data.argc = argc;
259 va_start (event_data.ap, argc);
260 mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call_string", (gpointer) & event_data);
261 va_end (event_data.ap);
262 return event_data.ret.s;
264 #endif /* ENABLE_BACKGROUND */
266 /* --------------------------------------------------------------------------------------------- */
267 /*** public functions ****************************************************************************/
268 /* --------------------------------------------------------------------------------------------- */
270 /** Used to ask questions to the user */
272 query_dialog (const char *header, const char *text, int flags, int count, ...)
274 va_list ap;
275 WDialog *query_dlg;
276 WButton *button;
277 int win_len = 0;
278 int i;
279 int result = -1;
280 int cols, lines;
281 const int *query_colors = (flags & D_ERROR) != 0 ? alarm_colors : dialog_colors;
282 dlg_flags_t dlg_flags = (flags & D_CENTER) != 0 ? (DLG_CENTER | DLG_TRYUP) : DLG_NONE;
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 dlg_create (TRUE, 0, 0, lines, cols, query_colors, query_default_callback, NULL,
308 "[QueryBox]", header, dlg_flags);
310 if (count > 0)
312 WButton *defbutton = NULL;
314 add_widget_autopos (query_dlg, label_new (2, 3, text), WPOS_KEEP_TOP | WPOS_CENTER_HORZ,
315 NULL);
316 add_widget (query_dlg, hline_new (lines - 4, -1, -1));
318 cols = (cols - win_len - 2) / 2 + 2;
319 va_start (ap, count);
320 for (i = 0; i < count; i++)
322 int xpos;
323 char *cur_name;
325 cur_name = va_arg (ap, char *);
326 xpos = str_term_width1 (cur_name) + 6;
327 if (strchr (cur_name, '&') != NULL)
328 xpos--;
330 button = button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON, cur_name, NULL);
331 add_widget (query_dlg, button);
332 cols += xpos;
333 if (i == sel_pos)
334 defbutton = button;
336 va_end (ap);
338 /* do resize before running and selecting any widget */
339 send_message (query_dlg, NULL, MSG_RESIZE, 0, NULL);
341 if (defbutton != NULL)
342 dlg_select_widget (defbutton);
344 /* run dialog and make result */
345 switch (dlg_run (query_dlg))
347 case B_CANCEL:
348 break;
349 default:
350 result = query_dlg->ret_value - B_USER;
353 /* free used memory */
354 dlg_destroy (query_dlg);
356 else
358 add_widget_autopos (query_dlg, label_new (2, 3, text), WPOS_KEEP_TOP | WPOS_CENTER_HORZ,
359 NULL);
360 add_widget (query_dlg, button_new (0, 0, 0, HIDDEN_BUTTON, "-", NULL));
361 last_query_dlg = query_dlg;
363 sel_pos = 0;
364 return result;
367 /* --------------------------------------------------------------------------------------------- */
369 void
370 query_set_sel (int new_sel)
372 sel_pos = new_sel;
375 /* --------------------------------------------------------------------------------------------- */
377 * Create message dialog. The caller must call dlg_run_done() and
378 * dlg_destroy() to dismiss it. Not safe to call from background.
381 WDialog *
382 create_message (int flags, const char *title, const char *text, ...)
384 va_list args;
385 WDialog *d;
386 char *p;
388 va_start (args, text);
389 p = g_strdup_vprintf (text, args);
390 va_end (args);
392 d = do_create_message (flags, title, p);
393 g_free (p);
395 return d;
398 /* --------------------------------------------------------------------------------------------- */
399 /** Show message box, background safe */
401 void
402 message (int flags, const char *title, const char *text, ...)
404 char *p;
405 va_list ap;
407 va_start (ap, text);
408 p = g_strdup_vprintf (text, ap);
409 va_end (ap);
411 if (title == MSG_ERROR)
412 title = _("Error");
414 #ifdef ENABLE_BACKGROUND
415 if (mc_global.we_are_background)
417 union
419 void *p;
420 void (*f) (int, int *, char *, const char *);
421 } func;
422 func.f = bg_message;
424 wtools_parent_call (func.p, NULL, 3, sizeof (flags), &flags, strlen (title), title,
425 strlen (p), p);
427 else
428 #endif /* ENABLE_BACKGROUND */
429 fg_message (flags, title, p);
431 g_free (p);
434 /* --------------------------------------------------------------------------------------------- */
435 /** Show error message box */
437 gboolean
438 mc_error_message (GError ** mcerror, int *code)
440 if (mcerror == NULL || *mcerror == NULL)
441 return FALSE;
443 if ((*mcerror)->code == 0)
444 message (D_ERROR, MSG_ERROR, "%s", (*mcerror)->message);
445 else
446 message (D_ERROR, MSG_ERROR, _("%s (%d)"), (*mcerror)->message, (*mcerror)->code);
448 if (code != NULL)
449 *code = (*mcerror)->code;
451 g_error_free (*mcerror);
452 *mcerror = NULL;
454 return TRUE;
457 /* --------------------------------------------------------------------------------------------- */
459 * Show input dialog, background safe.
461 * If the arguments "header" and "text" should be translated,
462 * that MUST be done by the caller of these wrappers.
465 char *
466 input_dialog_help (const char *header, const char *text, const char *help,
467 const char *history_name, const char *def_text, gboolean strip_password,
468 input_complete_t completion_flags)
470 #ifdef ENABLE_BACKGROUND
471 if (mc_global.we_are_background)
473 union
475 void *p;
476 char *(*f) (const char *, const char *, const char *, const char *, const char *,
477 gboolean, input_complete_t);
478 } func;
479 func.f = fg_input_dialog_help;
480 return wtools_parent_call_string (func.p, 7,
481 strlen (header), header, strlen (text),
482 text, strlen (help), help,
483 strlen (history_name), history_name,
484 strlen (def_text), def_text,
485 sizeof (gboolean), strip_password,
486 sizeof (input_complete_t), completion_flags);
488 else
489 #endif /* ENABLE_BACKGROUND */
490 return fg_input_dialog_help (header, text, help, history_name, def_text, strip_password,
491 completion_flags);
494 /* --------------------------------------------------------------------------------------------- */
495 /** Show input dialog with default help, background safe */
497 char *
498 input_dialog (const char *header, const char *text, const char *history_name, const char *def_text,
499 input_complete_t completion_flags)
501 return input_dialog_help (header, text, "[Input Line Keys]", history_name, def_text, FALSE,
502 completion_flags);
505 /* --------------------------------------------------------------------------------------------- */
507 char *
508 input_expand_dialog (const char *header, const char *text,
509 const char *history_name, const char *def_text,
510 input_complete_t completion_flags)
512 char *result;
514 result = input_dialog (header, text, history_name, def_text, completion_flags);
515 if (result)
517 char *expanded;
519 expanded = tilde_expand (result);
520 g_free (result);
521 return expanded;
523 return result;
526 /* --------------------------------------------------------------------------------------------- */
528 * Create status message window object and initialize it
530 * @param title window title
531 * @param delay initial delay to raise window in seconds
532 * @param init_cb callback to initialize user-defined part of status message
533 * @param update_cb callback to update of status message
534 * @param deinit_cb callback to deinitialize user-defined part of status message
536 * @return newly allocate status message window
539 status_msg_t *
540 status_msg_create (const char *title, double delay, status_msg_cb init_cb,
541 status_msg_update_cb update_cb, status_msg_cb deinit_cb)
543 status_msg_t *sm;
545 sm = g_try_new (status_msg_t, 1);
546 status_msg_init (sm, title, delay, init_cb, update_cb, deinit_cb);
548 return sm;
551 /* --------------------------------------------------------------------------------------------- */
553 * Destroy status message window object
555 * @param sm status message window object
558 void
559 status_msg_destroy (status_msg_t * sm)
561 status_msg_deinit (sm);
562 g_free (sm);
565 /* --------------------------------------------------------------------------------------------- */
567 * Initialize already created status message window object
569 * @param sm status message window object
570 * @param title window title
571 * @param delay initial delay to raise window in seconds
572 * @param init_cb callback to initialize user-defined part of status message
573 * @param update_cb callback to update of status message
574 * @param deinit_cb callback to deinitialize user-defined part of status message
577 void
578 status_msg_init (status_msg_t * sm, const char *title, double delay, status_msg_cb init_cb,
579 status_msg_update_cb update_cb, status_msg_cb deinit_cb)
581 guint64 start;
583 /* repaint screen to remove previous finished dialog */
584 mc_refresh ();
586 start = mc_timer_elapsed (mc_global.timer);
588 sm->dlg = dlg_create (TRUE, 0, 0, 7, MIN (MAX (40, COLS / 2), COLS), dialog_colors,
589 NULL, NULL, NULL, title, DLG_CENTER);
590 sm->start = start;
591 sm->delay = (guint64) (delay * G_USEC_PER_SEC);
592 sm->block = FALSE;
594 sm->init = init_cb;
595 sm->update = update_cb;
596 sm->deinit = deinit_cb;
598 if (sm->init != NULL)
599 sm->init (sm);
601 if (mc_time_elapsed (&start, sm->delay))
603 /* We will manage the dialog without any help, that's why we have to call dlg_init */
604 dlg_init (sm->dlg);
608 /* --------------------------------------------------------------------------------------------- */
610 * Deinitialize status message window object
612 * @param sm status message window object
615 void
616 status_msg_deinit (status_msg_t * sm)
618 if (sm == NULL)
619 return;
621 if (sm->deinit != NULL)
622 sm->deinit (sm);
624 /* close and destroy dialog */
625 dlg_run_done (sm->dlg);
626 dlg_destroy (sm->dlg);
629 /* --------------------------------------------------------------------------------------------- */
631 * Update status message window
633 * @param sm status message window object
635 * @return value of pressed key
639 status_msg_common_update (status_msg_t * sm)
641 int c;
642 Gpm_Event event;
644 if (sm == NULL)
645 return B_ENTER;
647 /* This should not happen, but... */
648 if (sm->dlg == NULL)
649 return B_ENTER;
651 if (widget_get_state (WIDGET (sm->dlg), WST_CONSTRUCT))
653 /* dialog is not shown yet */
655 /* do not change sm->start */
656 guint64 start = sm->start;
658 if (mc_time_elapsed (&start, sm->delay))
659 dlg_init (sm->dlg);
661 return B_ENTER;
664 event.x = -1; /* Don't show the GPM cursor */
665 c = tty_get_event (&event, FALSE, sm->block);
666 if (c == EV_NONE)
667 return B_ENTER;
669 /* Reinitialize by non-B_CANCEL value to avoid old values
670 after events other than selecting a button */
671 sm->dlg->ret_value = B_ENTER;
672 dlg_process_event (sm->dlg, c, &event);
674 return sm->dlg->ret_value;
677 /* --------------------------------------------------------------------------------------------- */
679 * Callback to initialize already created simple status message window object
681 * @param sm status message window object
684 void
685 simple_status_msg_init_cb (status_msg_t * sm)
687 simple_status_msg_t *ssm = SIMPLE_STATUS_MSG (sm);
688 Widget *wd = WIDGET (sm->dlg);
690 const char *b_name = N_("&Abort");
691 int b_width;
692 int wd_width, y;
693 Widget *b;
695 #ifdef ENABLE_NLS
696 b_name = _(b_name);
697 #endif
699 b_width = str_term_width1 (b_name) + 4;
700 wd_width = MAX (wd->cols, b_width + 6);
702 y = 2;
703 ssm->label = label_new (y++, 3, "");
704 add_widget_autopos (sm->dlg, ssm->label, WPOS_KEEP_TOP | WPOS_CENTER_HORZ, NULL);
705 add_widget (sm->dlg, hline_new (y++, -1, -1));
706 b = WIDGET (button_new (y++, 3, B_CANCEL, NORMAL_BUTTON, b_name, NULL));
707 add_widget_autopos (sm->dlg, b, WPOS_KEEP_TOP | WPOS_CENTER_HORZ, NULL);
709 widget_set_size (wd, wd->y, wd->x, y + 2, wd_width);
712 /* --------------------------------------------------------------------------------------------- */