2 Widget based utility functions.
4 Copyright (C) 1994-2024
5 Free Software Foundation, Inc.
8 Miguel de Icaza, 1994, 1995, 1996
9 Radek Doulik, 1994, 1995
11 Andrej Borsenkow, 1995
12 Andrew Borodin <aborodin@vmail.ru>, 2009-2022
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/>.
31 * \brief Source: widget based utility functions
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 /*** forward declarations (file scope functions) *************************************************/
55 /*** file scope variables ************************************************************************/
57 static WDialog
*last_query_dlg
;
59 static int sel_pos
= 0;
61 /* --------------------------------------------------------------------------------------------- */
62 /*** file scope functions ************************************************************************/
63 /* --------------------------------------------------------------------------------------------- */
65 /** default query callback, used to reposition query */
68 query_default_callback (Widget
*w
, Widget
*sender
, widget_msg_t msg
, int parm
, void *data
)
70 WDialog
*h
= DIALOG (w
);
75 if ((w
->pos_flags
& WPOS_CENTER
) == 0)
77 WDialog
*prev_dlg
= NULL
;
81 /* get dialog under h */
84 if (top_dlg
->data
!= (void *) h
)
85 prev_dlg
= DIALOG (top_dlg
->data
);
90 /* Top dialog is current if it is visible.
91 Get previous dialog in stack */
92 p
= g_list_next (top_dlg
);
94 prev_dlg
= DIALOG (p
->data
);
98 /* if previous dialog is not fullscreen'd -- overlap it */
99 if (prev_dlg
== NULL
|| (WIDGET (prev_dlg
)->pos_flags
& WPOS_FULLSCREEN
) != 0)
100 ypos
= LINES
/ 3 - (w
->rect
.lines
- 3) / 2;
102 ypos
= WIDGET (prev_dlg
)->rect
.y
+ 2;
104 /* if dialog is too high, place it centered */
105 if (ypos
+ w
->rect
.lines
< LINES
/ 2)
106 w
->pos_flags
|= WPOS_CENTER
;
108 xpos
= COLS
/ 2 - w
->rect
.cols
/ 2;
111 rect_init (&r
, ypos
, xpos
, w
->rect
.lines
, w
->rect
.cols
);
113 return dlg_default_callback (w
, NULL
, MSG_RESIZE
, 0, &r
);
118 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
122 /* --------------------------------------------------------------------------------------------- */
123 /** Create message dialog */
126 do_create_message (int flags
, const char *title
, const char *text
)
131 /* Add empty lines before and after the message */
132 p
= g_strconcat ("\n", text
, "\n", (char *) NULL
);
133 query_dialog (title
, p
, flags
, 0);
136 /* do resize before initing and running */
137 send_message (d
, NULL
, MSG_RESIZE
, 0, NULL
);
145 /* --------------------------------------------------------------------------------------------- */
147 * Show message dialog. Dismiss it when any key is pressed.
148 * Not safe to call from background.
152 fg_message (int flags
, const char *title
, const char *text
)
156 d
= do_create_message (flags
, title
, text
);
159 widget_destroy (WIDGET (d
));
162 /* --------------------------------------------------------------------------------------------- */
163 /** Show message box from background */
165 #ifdef ENABLE_BACKGROUND
167 bg_message (int dummy
, int *flags
, char *title
, const char *text
)
170 title
= g_strconcat (_("Background process:"), " ", title
, (char *) NULL
);
171 fg_message (*flags
, title
, text
);
174 #endif /* ENABLE_BACKGROUND */
176 /* --------------------------------------------------------------------------------------------- */
179 * Show dialog, not background safe.
181 * If the arguments "header" and "text" should be translated,
182 * that MUST be done by the caller of fg_input_dialog_help().
184 * The argument "history_name" holds the name of a section
185 * in the history file. Data entered in the input field of
186 * the dialog box will be stored there.
190 fg_input_dialog_help (const char *header
, const char *text
, const char *help
,
191 const char *history_name
, const char *def_text
, gboolean strip_password
,
192 input_complete_t completion_flags
)
195 char histname
[64] = "inp|";
196 gboolean is_passwd
= FALSE
;
201 p_text
= g_strstrip (g_strdup (text
));
204 if (history_name
!= NULL
&& *history_name
!= '\0')
205 g_strlcpy (histname
+ 3, history_name
, sizeof (histname
) - 3);
207 /* The special value of def_text is used to identify password boxes
208 and hide characters with "*". Don't save passwords in history! */
209 if (def_text
== INPUT_PASSWORD
)
217 quick_widget_t quick_widgets
[] = {
219 QUICK_LABELED_INPUT (p_text
, input_label_above
, def_text
, histname
, &my_str
,
220 NULL
, is_passwd
, strip_password
, completion_flags
),
221 QUICK_BUTTONS_OK_CANCEL
,
226 WRect r
= { -1, -1, 0, COLS
/ 2 };
228 quick_dialog_t qdlg
= {
230 quick_widgets
, NULL
, NULL
233 ret
= quick_dialog (&qdlg
);
238 return (ret
!= B_CANCEL
) ? my_str
: NULL
;
241 /* --------------------------------------------------------------------------------------------- */
243 #ifdef ENABLE_BACKGROUND
245 wtools_parent_call (void *routine
, gpointer ctx
, int argc
, ...)
247 ev_background_parent_call_t event_data
;
249 event_data
.routine
= routine
;
250 event_data
.ctx
= ctx
;
251 event_data
.argc
= argc
;
252 va_start (event_data
.ap
, argc
);
253 mc_event_raise (MCEVENT_GROUP_CORE
, "background_parent_call", (gpointer
) & event_data
);
254 va_end (event_data
.ap
);
255 return event_data
.ret
.i
;
258 /* --------------------------------------------------------------------------------------------- */
261 wtools_parent_call_string (void *routine
, int argc
, ...)
263 ev_background_parent_call_t event_data
;
265 event_data
.routine
= routine
;
266 event_data
.argc
= argc
;
267 va_start (event_data
.ap
, argc
);
268 mc_event_raise (MCEVENT_GROUP_CORE
, "background_parent_call_string", (gpointer
) & event_data
);
269 va_end (event_data
.ap
);
270 return event_data
.ret
.s
;
272 #endif /* ENABLE_BACKGROUND */
274 /* --------------------------------------------------------------------------------------------- */
275 /*** public functions ****************************************************************************/
276 /* --------------------------------------------------------------------------------------------- */
278 /** Used to ask questions to the user */
280 query_dialog (const char *header
, const char *text
, int flags
, int count
, ...)
290 const int *query_colors
= (flags
& D_ERROR
) != 0 ? alarm_colors
: dialog_colors
;
291 widget_pos_flags_t pos_flags
=
292 (flags
& D_CENTER
) != 0 ? (WPOS_CENTER
| WPOS_TRYUP
) : WPOS_KEEP_DEFAULT
;
294 if (header
== MSG_ERROR
)
299 va_start (ap
, count
);
300 for (i
= 0; i
< count
; i
++)
302 char *cp
= va_arg (ap
, char *);
304 win_len
+= str_term_width1 (cp
) + 6;
305 if (strchr (cp
, '&') != NULL
)
311 /* count coordinates */
312 str_msg_term_size (text
, &lines
, &cols
);
313 cols
= 6 + MAX (win_len
, MAX (str_term_width1 (header
), cols
));
314 lines
+= 4 + (count
> 0 ? 2 : 0);
318 dlg_create (TRUE
, 0, 0, lines
, cols
, pos_flags
, FALSE
, query_colors
, query_default_callback
,
319 NULL
, "[QueryBox]", header
);
320 g
= GROUP (query_dlg
);
324 WButton
*defbutton
= NULL
;
326 group_add_widget_autopos (g
, label_new (2, 3, text
), WPOS_KEEP_TOP
| WPOS_CENTER_HORZ
,
328 group_add_widget (g
, hline_new (lines
- 4, -1, -1));
330 cols
= (cols
- win_len
- 2) / 2 + 2;
331 va_start (ap
, count
);
332 for (i
= 0; i
< count
; i
++)
337 cur_name
= va_arg (ap
, char *);
338 xpos
= str_term_width1 (cur_name
) + 6;
339 if (strchr (cur_name
, '&') != NULL
)
342 button
= button_new (lines
- 3, cols
, B_USER
+ i
, NORMAL_BUTTON
, cur_name
, NULL
);
343 group_add_widget (g
, button
);
350 /* do resize before running and selecting any widget */
351 send_message (query_dlg
, NULL
, MSG_RESIZE
, 0, NULL
);
353 if (defbutton
!= NULL
)
354 widget_select (WIDGET (defbutton
));
356 /* run dialog and make result */
357 switch (dlg_run (query_dlg
))
362 result
= query_dlg
->ret_value
- B_USER
;
365 /* free used memory */
366 widget_destroy (WIDGET (query_dlg
));
370 group_add_widget_autopos (g
, label_new (2, 3, text
), WPOS_KEEP_TOP
| WPOS_CENTER_HORZ
,
372 group_add_widget (g
, button_new (0, 0, 0, HIDDEN_BUTTON
, "-", NULL
));
373 last_query_dlg
= query_dlg
;
379 /* --------------------------------------------------------------------------------------------- */
382 query_set_sel (int new_sel
)
387 /* --------------------------------------------------------------------------------------------- */
389 * Create message dialog. The caller must call dlg_run_done() and
390 * widget_destroy() to dismiss it. Not safe to call from background.
394 create_message (int flags
, const char *title
, const char *text
, ...)
400 va_start (args
, text
);
401 p
= g_strdup_vprintf (text
, args
);
404 d
= do_create_message (flags
, title
, p
);
410 /* --------------------------------------------------------------------------------------------- */
411 /** Show message box, background safe */
414 message (int flags
, const char *title
, const char *text
, ...)
420 p
= g_strdup_vprintf (text
, ap
);
423 if (title
== MSG_ERROR
)
426 #ifdef ENABLE_BACKGROUND
427 if (mc_global
.we_are_background
)
432 void (*f
) (int, int *, char *, const char *);
436 wtools_parent_call (func
.p
, NULL
, 3, sizeof (flags
), &flags
, strlen (title
), title
,
440 #endif /* ENABLE_BACKGROUND */
441 fg_message (flags
, title
, p
);
446 /* --------------------------------------------------------------------------------------------- */
447 /** Show error message box */
450 mc_error_message (GError
**mcerror
, int *code
)
452 if (mcerror
== NULL
|| *mcerror
== NULL
)
455 if ((*mcerror
)->code
== 0)
456 message (D_ERROR
, MSG_ERROR
, "%s", (*mcerror
)->message
);
458 message (D_ERROR
, MSG_ERROR
, _("%s (%d)"), (*mcerror
)->message
, (*mcerror
)->code
);
461 *code
= (*mcerror
)->code
;
463 g_error_free (*mcerror
);
469 /* --------------------------------------------------------------------------------------------- */
471 * Show input dialog, background safe.
473 * If the arguments "header" and "text" should be translated,
474 * that MUST be done by the caller of these wrappers.
478 input_dialog_help (const char *header
, const char *text
, const char *help
,
479 const char *history_name
, const char *def_text
, gboolean strip_password
,
480 input_complete_t completion_flags
)
482 #ifdef ENABLE_BACKGROUND
483 if (mc_global
.we_are_background
)
488 char *(*f
) (const char *, const char *, const char *, const char *, const char *,
489 gboolean
, input_complete_t
);
491 func
.f
= fg_input_dialog_help
;
492 return wtools_parent_call_string (func
.p
, 7,
493 strlen (header
), header
, strlen (text
),
494 text
, strlen (help
), help
,
495 strlen (history_name
), history_name
,
496 strlen (def_text
), def_text
,
497 sizeof (gboolean
), strip_password
,
498 sizeof (input_complete_t
), completion_flags
);
501 #endif /* ENABLE_BACKGROUND */
502 return fg_input_dialog_help (header
, text
, help
, history_name
, def_text
, strip_password
,
506 /* --------------------------------------------------------------------------------------------- */
507 /** Show input dialog with default help, background safe */
510 input_dialog (const char *header
, const char *text
, const char *history_name
, const char *def_text
,
511 input_complete_t completion_flags
)
513 return input_dialog_help (header
, text
, "[Input Line Keys]", history_name
, def_text
, FALSE
,
517 /* --------------------------------------------------------------------------------------------- */
520 input_expand_dialog (const char *header
, const char *text
,
521 const char *history_name
, const char *def_text
,
522 input_complete_t completion_flags
)
526 result
= input_dialog (header
, text
, history_name
, def_text
, completion_flags
);
531 expanded
= tilde_expand (result
);
538 /* --------------------------------------------------------------------------------------------- */
540 * Create status message window object and initialize it
542 * @param title window title
543 * @param delay initial delay to raise window in seconds
544 * @param init_cb callback to initialize user-defined part of status message
545 * @param update_cb callback to update of status message
546 * @param deinit_cb callback to deinitialize user-defined part of status message
548 * @return newly allocate status message window
552 status_msg_create (const char *title
, double delay
, status_msg_cb init_cb
,
553 status_msg_update_cb update_cb
, status_msg_cb deinit_cb
)
557 sm
= g_try_new (status_msg_t
, 1);
558 status_msg_init (sm
, title
, delay
, init_cb
, update_cb
, deinit_cb
);
563 /* --------------------------------------------------------------------------------------------- */
565 * Destroy status message window object
567 * @param sm status message window object
571 status_msg_destroy (status_msg_t
*sm
)
573 status_msg_deinit (sm
);
577 /* --------------------------------------------------------------------------------------------- */
579 * Initialize already created status message window object
581 * @param sm status message window object
582 * @param title window title
583 * @param delay initial delay to raise window in seconds
584 * @param init_cb callback to initialize user-defined part of status message
585 * @param update_cb callback to update of status message
586 * @param deinit_cb callback to deinitialize user-defined part of status message
590 status_msg_init (status_msg_t
*sm
, const char *title
, double delay
, status_msg_cb init_cb
,
591 status_msg_update_cb update_cb
, status_msg_cb deinit_cb
)
595 /* repaint screen to remove previous finished dialog */
598 start
= g_get_monotonic_time ();
600 sm
->dlg
= dlg_create (TRUE
, 0, 0, 7, MIN (MAX (40, COLS
/ 2), COLS
), WPOS_CENTER
, FALSE
,
601 dialog_colors
, NULL
, NULL
, NULL
, title
);
603 sm
->delay
= (gint64
) (delay
* G_USEC_PER_SEC
);
607 sm
->update
= update_cb
;
608 sm
->deinit
= deinit_cb
;
610 if (sm
->init
!= NULL
)
613 if (mc_time_elapsed (&start
, sm
->delay
))
615 /* We will manage the dialog without any help, that's why we have to call dlg_init */
620 /* --------------------------------------------------------------------------------------------- */
622 * Deinitialize status message window object
624 * @param sm status message window object
628 status_msg_deinit (status_msg_t
*sm
)
633 if (sm
->deinit
!= NULL
)
636 /* close and destroy dialog */
637 dlg_run_done (sm
->dlg
);
638 widget_destroy (WIDGET (sm
->dlg
));
641 /* --------------------------------------------------------------------------------------------- */
643 * Update status message window
645 * @param sm status message window object
647 * @return value of pressed key
651 status_msg_common_update (status_msg_t
*sm
)
659 /* This should not happen, but... */
663 if (widget_get_state (WIDGET (sm
->dlg
), WST_CONSTRUCT
))
665 /* dialog is not shown yet */
667 /* do not change sm->start */
668 gint64 start
= sm
->start
;
670 if (mc_time_elapsed (&start
, sm
->delay
))
676 event
.x
= -1; /* Don't show the GPM cursor */
677 c
= tty_get_event (&event
, FALSE
, sm
->block
);
681 /* Reinitialize by non-B_CANCEL value to avoid old values
682 after events other than selecting a button */
683 sm
->dlg
->ret_value
= B_ENTER
;
684 dlg_process_event (sm
->dlg
, c
, &event
);
686 return sm
->dlg
->ret_value
;
689 /* --------------------------------------------------------------------------------------------- */
691 * Callback to initialize already created simple status message window object
693 * @param sm status message window object
697 simple_status_msg_init_cb (status_msg_t
*sm
)
699 simple_status_msg_t
*ssm
= SIMPLE_STATUS_MSG (sm
);
700 Widget
*wd
= WIDGET (sm
->dlg
);
701 WGroup
*wg
= GROUP (sm
->dlg
);
704 const char *b_name
= N_("&Abort");
713 b_width
= str_term_width1 (b_name
) + 4;
714 wd_width
= MAX (wd
->rect
.cols
, b_width
+ 6);
717 ssm
->label
= label_new (y
++, 3, NULL
);
718 group_add_widget_autopos (wg
, ssm
->label
, WPOS_KEEP_TOP
| WPOS_CENTER_HORZ
, NULL
);
719 group_add_widget (wg
, hline_new (y
++, -1, -1));
720 b
= WIDGET (button_new (y
++, 3, B_CANCEL
, NORMAL_BUTTON
, b_name
, NULL
));
721 group_add_widget_autopos (wg
, b
, WPOS_KEEP_TOP
| WPOS_CENTER_HORZ
, NULL
);
726 widget_set_size_rect (wd
, &r
);
729 /* --------------------------------------------------------------------------------------------- */