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, 2012
6 The Free Software Foundation, Inc.
9 Miguel de Icaza, 1994, 1995, 1996
10 Radek Doulik, 1994, 1995
12 Andrej Borsenkow, 1995
13 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2012
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/>.
32 * \brief Source: widget based utility functions
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 */
66 default_query_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
71 if ((h
->flags
& DLG_CENTER
) == 0)
73 Widget
*wh
= WIDGET (h
);
74 Dlg_head
*prev_dlg
= NULL
;
77 /* get dialog under h */
80 if (top_dlg
->data
!= (void *) h
)
81 prev_dlg
= (Dlg_head
*) top_dlg
->data
;
86 /* Top dialog is current if it is visible.
87 Get previous dialog in stack */
88 p
= g_list_next (top_dlg
);
90 prev_dlg
= (Dlg_head
*) p
->data
;
94 /* if previous dialog is not fullscreen'd -- overlap it */
95 if (prev_dlg
== NULL
|| prev_dlg
->fullscreen
)
96 ypos
= LINES
/ 3 - (wh
->lines
- 3) / 2;
98 ypos
= WIDGET (prev_dlg
)->y
+ 2;
100 xpos
= COLS
/ 2 - wh
->cols
/ 2;
103 dlg_set_position (h
, ypos
, xpos
, ypos
+ wh
->lines
, xpos
+ wh
->cols
);
110 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
114 /* --------------------------------------------------------------------------------------------- */
115 /** Create message dialog */
117 static struct Dlg_head
*
118 do_create_message (int flags
, const char *title
, const char *text
)
123 /* Add empty lines before and after the message */
124 p
= g_strconcat ("\n", text
, "\n", (char *) NULL
);
125 query_dialog (title
, p
, flags
, 0);
128 /* do resize before initing and running */
129 default_query_callback (d
, NULL
, DLG_RESIZE
, 0, NULL
);
137 /* --------------------------------------------------------------------------------------------- */
139 * Show message dialog. Dismiss it when any key is pressed.
140 * Not safe to call from background.
144 fg_message (int flags
, const char *title
, const char *text
)
148 d
= do_create_message (flags
, title
, text
);
155 /* --------------------------------------------------------------------------------------------- */
156 /** Show message box from background */
158 #ifdef ENABLE_BACKGROUND
160 bg_message (int dummy
, int *flags
, char *title
, const char *text
)
163 title
= g_strconcat (_("Background process:"), " ", title
, (char *) NULL
);
164 fg_message (*flags
, title
, text
);
167 #endif /* ENABLE_BACKGROUND */
169 /* --------------------------------------------------------------------------------------------- */
172 * Show dialog, not background safe.
174 * If the arguments "header" and "text" should be translated,
175 * that MUST be done by the caller of fg_input_dialog_help().
177 * The argument "history_name" holds the name of a section
178 * in the history file. Data entered in the input field of
179 * the dialog box will be stored there.
183 fg_input_dialog_help (const char *header
, const char *text
, const char *help
,
184 const char *history_name
, const char *def_text
, gboolean strip_password
)
187 char histname
[64] = "inp|";
188 int flags
= strip_password
? 4 : 0;
193 p_text
= g_strstrip (g_strdup (text
));
196 if (history_name
!= NULL
&& *history_name
!= '\0')
197 g_strlcpy (histname
+ 3, history_name
, sizeof (histname
) - 3);
199 /* The special value of def_text is used to identify password boxes
200 and hide characters with "*". Don't save passwords in history! */
201 if (def_text
== INPUT_PASSWORD
)
209 quick_widget_t quick_widgets
[] = {
211 QUICK2_LABELED_INPUT (p_text
, input_label_above
, def_text
, flags
, histname
, &my_str
,
213 QUICK2_START_BUTTONS (TRUE
, TRUE
),
214 QUICK2_BUTTON (N_("&OK"), B_ENTER
, NULL
, NULL
),
215 QUICK2_BUTTON (N_("&Cancel"), B_CANCEL
, NULL
, NULL
),
220 quick_dialog_t qdlg
= {
221 -1, -1, COLS
/ 2, header
,
222 help
, quick_widgets
, NULL
, NULL
225 ret
= quick2_dialog (&qdlg
);
230 return (ret
!= B_CANCEL
) ? my_str
: NULL
;
233 /* --------------------------------------------------------------------------------------------- */
235 #ifdef ENABLE_BACKGROUND
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 /* --------------------------------------------------------------------------------------------- */
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
, ...)
277 WButton
*defbutton
= NULL
;
283 const int *query_colors
= (flags
& D_ERROR
) != 0 ? alarm_colors
: dialog_colors
;
284 dlg_flags_t dlg_flags
= (flags
& D_CENTER
) != 0 ? (DLG_CENTER
| DLG_TRYUP
) : DLG_NONE
;
286 if (header
== MSG_ERROR
)
291 va_start (ap
, count
);
292 for (i
= 0; i
< count
; i
++)
294 char *cp
= va_arg (ap
, char *);
295 win_len
+= str_term_width1 (cp
) + 6;
296 if (strchr (cp
, '&') != NULL
)
302 /* count coordinates */
303 str_msg_term_size (text
, &lines
, &cols
);
304 cols
= 6 + max (win_len
, max (str_term_width1 (header
), cols
));
305 lines
+= 4 + (count
> 0 ? 2 : 0);
309 create_dlg (TRUE
, 0, 0, lines
, cols
, query_colors
, default_query_callback
, NULL
,
310 "[QueryBox]", header
, dlg_flags
);
314 cols
= (cols
- win_len
- 2) / 2 + 2;
315 va_start (ap
, count
);
316 for (i
= 0; i
< count
; i
++)
320 cur_name
= va_arg (ap
, char *);
321 xpos
= str_term_width1 (cur_name
) + 6;
322 if (strchr (cur_name
, '&') != NULL
)
325 button
= button_new (lines
- 3, cols
, B_USER
+ i
, NORMAL_BUTTON
, cur_name
, 0);
326 add_widget (query_dlg
, button
);
333 add_widget (query_dlg
, label_new (2, 3, text
));
335 /* do resize before running and selecting any widget */
336 default_query_callback (query_dlg
, NULL
, DLG_RESIZE
, 0, NULL
);
339 dlg_select_widget (defbutton
);
341 /* run dialog and make result */
342 switch (run_dlg (query_dlg
))
347 result
= query_dlg
->ret_value
- B_USER
;
350 /* free used memory */
351 destroy_dlg (query_dlg
);
355 add_widget (query_dlg
, label_new (2, 3, text
));
356 add_widget (query_dlg
, button_new (0, 0, 0, HIDDEN_BUTTON
, "-", 0));
357 last_query_dlg
= query_dlg
;
363 /* --------------------------------------------------------------------------------------------- */
366 query_set_sel (int new_sel
)
371 /* --------------------------------------------------------------------------------------------- */
373 * Create message dialog. The caller must call dlg_run_done() and
374 * destroy_dlg() to dismiss it. Not safe to call from background.
378 create_message (int flags
, const char *title
, const char *text
, ...)
384 va_start (args
, text
);
385 p
= g_strdup_vprintf (text
, args
);
388 d
= do_create_message (flags
, title
, p
);
394 /* --------------------------------------------------------------------------------------------- */
395 /** Show message box, background safe */
398 message (int flags
, const char *title
, const char *text
, ...)
404 p
= g_strdup_vprintf (text
, ap
);
407 if (title
== MSG_ERROR
)
410 #ifdef ENABLE_BACKGROUND
411 if (mc_global
.we_are_background
)
416 void (*f
) (int, int *, char *, const char *);
420 wtools_parent_call (func
.p
, NULL
, 3, sizeof (flags
), &flags
, strlen (title
), title
,
424 #endif /* ENABLE_BACKGROUND */
425 fg_message (flags
, title
, p
);
430 /* --------------------------------------------------------------------------------------------- */
432 * Show input dialog, background safe.
434 * If the arguments "header" and "text" should be translated,
435 * that MUST be done by the caller of these wrappers.
439 input_dialog_help (const char *header
, const char *text
, const char *help
,
440 const char *history_name
, const char *def_text
, gboolean strip_password
)
442 #ifdef ENABLE_BACKGROUND
443 if (mc_global
.we_are_background
)
448 char *(*f
) (const char *, const char *, const char *, const char *, const char *,
451 func
.f
= fg_input_dialog_help
;
452 return wtools_parent_call_string (func
.p
, 6,
453 strlen (header
), header
, strlen (text
),
454 text
, strlen (help
), help
,
455 strlen (history_name
), history_name
,
456 strlen (def_text
), def_text
,
457 sizeof (gboolean
), strip_password
);
460 #endif /* ENABLE_BACKGROUND */
461 return fg_input_dialog_help (header
, text
, help
, history_name
, def_text
, strip_password
);
464 /* --------------------------------------------------------------------------------------------- */
465 /** Show input dialog with default help, background safe */
468 input_dialog (const char *header
, const char *text
, const char *history_name
, const char *def_text
)
470 return input_dialog_help (header
, text
, "[Input Line Keys]", history_name
, def_text
, FALSE
);
473 /* --------------------------------------------------------------------------------------------- */
476 input_expand_dialog (const char *header
, const char *text
,
477 const char *history_name
, const char *def_text
)
482 result
= input_dialog (header
, text
, history_name
, def_text
);
485 expanded
= tilde_expand (result
);
492 /* --------------------------------------------------------------------------------------------- */