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.
9 Miguel de Icaza, 1994, 1995, 1996
10 Radek Doulik, 1994, 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/>.
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
)
72 Dlg_head
*prev_dlg
= NULL
;
75 /* get dialog under h */
78 if (top_dlg
->data
!= (void *) h
)
79 prev_dlg
= (Dlg_head
*) top_dlg
->data
;
84 /* Top dialog is current if it is visible.
85 Get previous dialog in stack */
86 p
= g_list_next (top_dlg
);
88 prev_dlg
= (Dlg_head
*) p
->data
;
92 /* if previous dialog is not fullscreen'd -- overlap it */
93 if (prev_dlg
== NULL
|| prev_dlg
->fullscreen
)
94 ypos
= LINES
/ 3 - (h
->lines
- 3) / 2;
96 ypos
= prev_dlg
->y
+ 2;
98 xpos
= COLS
/ 2 - h
->cols
/ 2;
101 dlg_set_position (h
, ypos
, xpos
, ypos
+ h
->lines
, xpos
+ h
->cols
);
106 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
110 /* --------------------------------------------------------------------------------------------- */
111 /** Create message dialog */
113 static struct Dlg_head
*
114 do_create_message (int flags
, const char *title
, const char *text
)
119 /* Add empty lines before and after the message */
120 p
= g_strconcat ("\n", text
, "\n", (char *) NULL
);
121 query_dialog (title
, p
, flags
, 0);
124 /* do resize before initing and running */
125 default_query_callback (d
, NULL
, DLG_RESIZE
, 0, NULL
);
133 /* --------------------------------------------------------------------------------------------- */
135 * Show message dialog. Dismiss it when any key is pressed.
136 * Not safe to call from background.
140 fg_message (int flags
, const char *title
, const char *text
)
144 d
= do_create_message (flags
, title
, text
);
151 /* --------------------------------------------------------------------------------------------- */
152 /** Show message box from background */
154 #ifdef WITH_BACKGROUND
156 bg_message (int dummy
, int *flags
, char *title
, const char *text
)
159 title
= g_strconcat (_("Background process:"), " ", title
, (char *) NULL
);
160 fg_message (*flags
, title
, text
);
163 #endif /* WITH_BACKGROUND */
165 /* --------------------------------------------------------------------------------------------- */
168 * Show dialog, not background safe.
170 * If the arguments "header" and "text" should be translated,
171 * that MUST be done by the caller of fg_input_dialog_help().
173 * The argument "history_name" holds the name of a section
174 * in the history file. Data entered in the input field of
175 * the dialog box will be stored there.
179 fg_input_dialog_help (const char *header
, const char *text
, const char *help
,
180 const char *history_name
, const char *def_text
)
184 QuickWidget quick_widgets
[] = {
185 /* 0 */ QUICK_BUTTON (6, 64, 1, 0, N_("&Cancel"), B_CANCEL
, NULL
),
186 /* 1 */ QUICK_BUTTON (3, 64, 1, 0, N_("&OK"), B_ENTER
, NULL
),
187 /* 2 */ QUICK_INPUT (3, 64, 0, 0, def_text
, 58, 0, NULL
, &my_str
),
188 /* 3 */ QUICK_LABEL (3, 64, 2, 0, ""),
192 int b0_len
, b1_len
, b_len
, gap
;
193 char histname
[64] = "inp|";
202 quick_widgets
[0].u
.button
.text
= _(quick_widgets
[0].u
.button
.text
);
203 quick_widgets
[1].u
.button
.text
= _(quick_widgets
[1].u
.button
.text
);
204 #endif /* ENABLE_NLS */
206 b0_len
= str_term_width1 (quick_widgets
[0].u
.button
.text
) + 3;
207 b1_len
= str_term_width1 (quick_widgets
[1].u
.button
.text
) + 5; /* default button */
208 b_len
= b0_len
+ b1_len
+ 2; /* including gap */
211 if (history_name
!= NULL
&& *history_name
!= '\0')
213 g_strlcpy (histname
+ 3, history_name
, sizeof (histname
) - 3);
214 quick_widgets
[2].u
.input
.histname
= histname
;
217 /* The special value of def_text is used to identify password boxes
218 and hide characters with "*". Don't save passwords in history! */
219 if (def_text
== INPUT_PASSWORD
)
221 quick_widgets
[2].u
.input
.flags
= 1;
223 quick_widgets
[2].u
.input
.text
= "";
227 p_text
= g_strstrip (g_strdup (text
));
228 str_msg_term_size (p_text
, &lines
, &cols
);
229 quick_widgets
[3].u
.label
.text
= p_text
;
232 len
= str_term_width1 (header
);
233 len
= max (max (len
, cols
) + 4, 64);
234 len
= min (max (len
, b_len
+ 6), COLS
);
236 /* button locations */
237 gap
= (len
- 8 - b_len
) / 3;
238 quick_widgets
[1].relative_x
= 3 + gap
;
239 quick_widgets
[0].relative_x
= quick_widgets
[1].relative_x
+ b1_len
+ gap
+ 2;
242 QuickDialog Quick_input
= {
243 len
, lines
+ 6, -1, -1, header
,
244 help
, quick_widgets
, NULL
, TRUE
247 for (i
= 0; i
< 4; i
++)
249 quick_widgets
[i
].x_divisions
= Quick_input
.xlen
;
250 quick_widgets
[i
].y_divisions
= Quick_input
.ylen
;
253 for (i
= 0; i
< 3; i
++)
254 quick_widgets
[i
].relative_y
+= 2 + lines
;
256 /* input line length */
257 quick_widgets
[2].u
.input
.len
= Quick_input
.xlen
- 6;
259 ret
= quick_dialog (&Quick_input
);
264 return (ret
!= B_CANCEL
) ? my_str
: NULL
;
267 /* --------------------------------------------------------------------------------------------- */
269 #ifdef WITH_BACKGROUND
271 wtools_parent_call (void *routine
, gpointer ctx
, int argc
, ...)
273 ev_background_parent_call_t event_data
;
275 event_data
.routine
= routine
;
276 event_data
.ctx
= ctx
;
277 event_data
.argc
= argc
;
278 va_start (event_data
.ap
, argc
);
279 mc_event_raise (MCEVENT_GROUP_CORE
, "background_parent_call", (gpointer
) & event_data
);
280 va_end (event_data
.ap
);
281 return event_data
.ret
.i
;
284 /* --------------------------------------------------------------------------------------------- */
287 wtools_parent_call_string (void *routine
, int argc
, ...)
289 ev_background_parent_call_t event_data
;
291 event_data
.routine
= routine
;
292 event_data
.argc
= argc
;
293 va_start (event_data
.ap
, argc
);
294 mc_event_raise (MCEVENT_GROUP_CORE
, "background_parent_call_string", (gpointer
) & event_data
);
295 va_end (event_data
.ap
);
296 return event_data
.ret
.s
;
298 #endif /* WITH_BACKGROUND */
300 /* --------------------------------------------------------------------------------------------- */
301 /*** public functions ****************************************************************************/
302 /* --------------------------------------------------------------------------------------------- */
304 /** Used to ask questions to the user */
306 query_dialog (const char *header
, const char *text
, int flags
, int count
, ...)
311 WButton
*defbutton
= NULL
;
317 const int *query_colors
= (flags
& D_ERROR
) ? alarm_colors
: dialog_colors
;
319 if (header
== MSG_ERROR
)
324 va_start (ap
, count
);
325 for (i
= 0; i
< count
; i
++)
327 char *cp
= va_arg (ap
, char *);
328 win_len
+= str_term_width1 (cp
) + 6;
329 if (strchr (cp
, '&') != NULL
)
335 /* count coordinates */
336 str_msg_term_size (text
, &lines
, &cols
);
337 cols
= 6 + max (win_len
, max (str_term_width1 (header
), cols
));
338 lines
+= 4 + (count
> 0 ? 2 : 0);
342 create_dlg (TRUE
, 0, 0, lines
, cols
, query_colors
, default_query_callback
,
343 "[QueryBox]", header
, DLG_NONE
);
347 cols
= (cols
- win_len
- 2) / 2 + 2;
348 va_start (ap
, count
);
349 for (i
= 0; i
< count
; i
++)
353 cur_name
= va_arg (ap
, char *);
354 xpos
= str_term_width1 (cur_name
) + 6;
355 if (strchr (cur_name
, '&') != NULL
)
358 button
= button_new (lines
- 3, cols
, B_USER
+ i
, NORMAL_BUTTON
, cur_name
, 0);
359 add_widget (query_dlg
, button
);
366 add_widget (query_dlg
, label_new (2, 3, text
));
368 /* do resize before running and selecting any widget */
369 default_query_callback (query_dlg
, NULL
, DLG_RESIZE
, 0, NULL
);
372 dlg_select_widget (defbutton
);
374 /* run dialog and make result */
375 switch (run_dlg (query_dlg
))
380 result
= query_dlg
->ret_value
- B_USER
;
383 /* free used memory */
384 destroy_dlg (query_dlg
);
388 add_widget (query_dlg
, label_new (2, 3, text
));
389 add_widget (query_dlg
, button_new (0, 0, 0, HIDDEN_BUTTON
, "-", 0));
390 last_query_dlg
= query_dlg
;
396 /* --------------------------------------------------------------------------------------------- */
399 query_set_sel (int new_sel
)
404 /* --------------------------------------------------------------------------------------------- */
406 * Create message dialog. The caller must call dlg_run_done() and
407 * destroy_dlg() to dismiss it. Not safe to call from background.
411 create_message (int flags
, const char *title
, const char *text
, ...)
417 va_start (args
, text
);
418 p
= g_strdup_vprintf (text
, args
);
421 d
= do_create_message (flags
, title
, p
);
427 /* --------------------------------------------------------------------------------------------- */
428 /** Show message box, background safe */
431 message (int flags
, const char *title
, const char *text
, ...)
437 p
= g_strdup_vprintf (text
, ap
);
440 if (title
== MSG_ERROR
)
443 #ifdef WITH_BACKGROUND
444 if (mc_global
.we_are_background
)
449 void (*f
) (int, int *, char *, const char *);
453 wtools_parent_call (func
.p
, NULL
, 3, sizeof (flags
), &flags
, strlen (title
), title
,
457 #endif /* WITH_BACKGROUND */
458 fg_message (flags
, title
, p
);
463 /* --------------------------------------------------------------------------------------------- */
465 * Show input dialog, background safe.
467 * If the arguments "header" and "text" should be translated,
468 * that MUST be done by the caller of these wrappers.
472 input_dialog_help (const char *header
, const char *text
, const char *help
,
473 const char *history_name
, const char *def_text
)
475 #ifdef WITH_BACKGROUND
476 if (mc_global
.we_are_background
)
481 char *(*f
) (const char *, const char *, const char *, const char *, const char *);
483 func
.f
= fg_input_dialog_help
;
484 return wtools_parent_call_string (func
.p
, 5,
485 strlen (header
), header
, strlen (text
),
486 text
, strlen (help
), help
,
487 strlen (history_name
), history_name
,
488 strlen (def_text
), def_text
);
491 #endif /* WITH_BACKGROUND */
492 return fg_input_dialog_help (header
, text
, help
, history_name
, def_text
);
495 /* --------------------------------------------------------------------------------------------- */
496 /** Show input dialog with default help, background safe */
499 input_dialog (const char *header
, const char *text
, const char *history_name
, const char *def_text
)
501 return input_dialog_help (header
, text
, "[Input Line Keys]", history_name
, def_text
);
504 /* --------------------------------------------------------------------------------------------- */
507 input_expand_dialog (const char *header
, const char *text
,
508 const char *history_name
, const char *def_text
)
513 result
= input_dialog (header
, text
, history_name
, def_text
);
516 expanded
= tilde_expand (result
);
523 /* --------------------------------------------------------------------------------------------- */