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
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.
28 * \brief Source: widget based utility functions
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 */
62 default_query_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
68 int xpos
= COLS
/ 2 - h
->cols
/ 2;
69 int ypos
= LINES
/ 3 - (h
->lines
- 3) / 2;
72 dlg_set_position (h
, ypos
, xpos
, ypos
+ h
->lines
, xpos
+ h
->cols
);
77 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
81 /* --------------------------------------------------------------------------------------------- */
82 /** Create message dialog */
84 static struct Dlg_head
*
85 do_create_message (int flags
, const char *title
, const char *text
)
90 /* Add empty lines before and after the message */
91 p
= g_strconcat ("\n", text
, "\n", (char *) NULL
);
92 query_dialog (title
, p
, flags
, 0);
95 /* do resize before initing and running */
96 default_query_callback (d
, NULL
, DLG_RESIZE
, 0, NULL
);
104 /* --------------------------------------------------------------------------------------------- */
106 * Show message dialog. Dismiss it when any key is pressed.
107 * Not safe to call from background.
111 fg_message (int flags
, const char *title
, const char *text
)
115 d
= do_create_message (flags
, title
, text
);
122 /* --------------------------------------------------------------------------------------------- */
123 /** Show message box from background */
125 #ifdef WITH_BACKGROUND
127 bg_message (int dummy
, int *flags
, char *title
, const char *text
)
130 title
= g_strconcat (_("Background process:"), " ", title
, (char *) NULL
);
131 fg_message (*flags
, title
, text
);
134 #endif /* WITH_BACKGROUND */
136 /* --------------------------------------------------------------------------------------------- */
139 * Show dialog, not background safe.
141 * If the arguments "header" and "text" should be translated,
142 * that MUST be done by the caller of fg_input_dialog_help().
144 * The argument "history_name" holds the name of a section
145 * in the history file. Data entered in the input field of
146 * the dialog box will be stored there.
150 fg_input_dialog_help (const char *header
, const char *text
, const char *help
,
151 const char *history_name
, const char *def_text
)
155 QuickWidget quick_widgets
[] = {
156 /* 0 */ QUICK_BUTTON (6, 64, 1, 0, N_("&Cancel"), B_CANCEL
, NULL
),
157 /* 1 */ QUICK_BUTTON (3, 64, 1, 0, N_("&OK"), B_ENTER
, NULL
),
158 /* 2 */ QUICK_INPUT (3, 64, 0, 0, def_text
, 58, 0, NULL
, &my_str
),
159 /* 3 */ QUICK_LABEL (3, 64, 2, 0, ""),
163 int b0_len
, b1_len
, b_len
, gap
;
164 char histname
[64] = "inp|";
173 quick_widgets
[0].u
.button
.text
= _(quick_widgets
[0].u
.button
.text
);
174 quick_widgets
[1].u
.button
.text
= _(quick_widgets
[1].u
.button
.text
);
175 #endif /* ENABLE_NLS */
177 b0_len
= str_term_width1 (quick_widgets
[0].u
.button
.text
) + 3;
178 b1_len
= str_term_width1 (quick_widgets
[1].u
.button
.text
) + 5; /* default button */
179 b_len
= b0_len
+ b1_len
+ 2; /* including gap */
182 if (history_name
!= NULL
&& *history_name
!= '\0')
184 g_strlcpy (histname
+ 3, history_name
, sizeof (histname
) - 3);
185 quick_widgets
[2].u
.input
.histname
= histname
;
188 /* The special value of def_text is used to identify password boxes
189 and hide characters with "*". Don't save passwords in history! */
190 if (def_text
== INPUT_PASSWORD
)
192 quick_widgets
[2].u
.input
.flags
= 1;
194 quick_widgets
[2].u
.input
.text
= "";
198 p_text
= g_strstrip (g_strdup (text
));
199 str_msg_term_size (p_text
, &lines
, &cols
);
200 quick_widgets
[3].u
.label
.text
= p_text
;
203 len
= str_term_width1 (header
);
204 len
= max (max (len
, cols
) + 4, 64);
205 len
= min (max (len
, b_len
+ 6), COLS
);
207 /* button locations */
208 gap
= (len
- 8 - b_len
) / 3;
209 quick_widgets
[1].relative_x
= 3 + gap
;
210 quick_widgets
[0].relative_x
= quick_widgets
[1].relative_x
+ b1_len
+ gap
+ 2;
213 QuickDialog Quick_input
= {
214 len
, lines
+ 6, -1, -1, header
,
215 help
, quick_widgets
, NULL
, TRUE
218 for (i
= 0; i
< 4; i
++)
220 quick_widgets
[i
].x_divisions
= Quick_input
.xlen
;
221 quick_widgets
[i
].y_divisions
= Quick_input
.ylen
;
224 for (i
= 0; i
< 3; i
++)
225 quick_widgets
[i
].relative_y
+= 2 + lines
;
227 /* input line length */
228 quick_widgets
[2].u
.input
.len
= Quick_input
.xlen
- 6;
230 ret
= quick_dialog (&Quick_input
);
235 return (ret
!= B_CANCEL
) ? my_str
: NULL
;
238 /* --------------------------------------------------------------------------------------------- */
240 #ifdef WITH_BACKGROUND
242 wtools_parent_call (void *routine
, gpointer ctx
, int argc
, ...)
244 ev_background_parent_call_t event_data
;
246 event_data
.routine
= routine
;
247 event_data
.ctx
= ctx
;
248 event_data
.argc
= argc
;
249 va_start (event_data
.ap
, argc
);
250 mc_event_raise (MCEVENT_GROUP_CORE
, "background_parent_call", (gpointer
) & event_data
);
251 va_end (event_data
.ap
);
252 return event_data
.ret
.i
;
255 /* --------------------------------------------------------------------------------------------- */
258 wtools_parent_call_string (void *routine
, int argc
, ...)
260 ev_background_parent_call_t event_data
;
262 event_data
.routine
= routine
;
263 event_data
.argc
= argc
;
264 va_start (event_data
.ap
, argc
);
265 mc_event_raise (MCEVENT_GROUP_CORE
, "background_parent_call_string", (gpointer
) & event_data
);
266 va_end (event_data
.ap
);
267 return event_data
.ret
.s
;
269 #endif /* WITH_BACKGROUND */
271 /* --------------------------------------------------------------------------------------------- */
272 /*** public functions ****************************************************************************/
273 /* --------------------------------------------------------------------------------------------- */
275 /** Used to ask questions to the user */
277 query_dialog (const char *header
, const char *text
, int flags
, int count
, ...)
282 WButton
*defbutton
= NULL
;
288 const int *query_colors
= (flags
& D_ERROR
) ? alarm_colors
: dialog_colors
;
290 if (header
== MSG_ERROR
)
295 va_start (ap
, count
);
296 for (i
= 0; i
< count
; i
++)
298 char *cp
= va_arg (ap
, char *);
299 win_len
+= str_term_width1 (cp
) + 6;
300 if (strchr (cp
, '&') != NULL
)
306 /* count coordinates */
307 str_msg_term_size (text
, &lines
, &cols
);
308 cols
= 6 + max (win_len
, max (str_term_width1 (header
), cols
));
309 lines
+= 4 + (count
> 0 ? 2 : 0);
313 create_dlg (TRUE
, 0, 0, lines
, cols
, query_colors
, default_query_callback
,
314 "[QueryBox]", header
, DLG_NONE
);
318 cols
= (cols
- win_len
- 2) / 2 + 2;
319 va_start (ap
, count
);
320 for (i
= 0; i
< count
; i
++)
324 cur_name
= va_arg (ap
, char *);
325 xpos
= str_term_width1 (cur_name
) + 6;
326 if (strchr (cur_name
, '&') != NULL
)
329 button
= button_new (lines
- 3, cols
, B_USER
+ i
, NORMAL_BUTTON
, cur_name
, 0);
330 add_widget (query_dlg
, button
);
337 add_widget (query_dlg
, label_new (2, 3, text
));
339 /* do resize before running and selecting any widget */
340 default_query_callback (query_dlg
, NULL
, DLG_RESIZE
, 0, NULL
);
343 dlg_select_widget (defbutton
);
345 /* run dialog and make result */
346 switch (run_dlg (query_dlg
))
351 result
= query_dlg
->ret_value
- B_USER
;
354 /* free used memory */
355 destroy_dlg (query_dlg
);
359 add_widget (query_dlg
, label_new (2, 3, text
));
360 add_widget (query_dlg
, button_new (0, 0, 0, HIDDEN_BUTTON
, "-", 0));
361 last_query_dlg
= query_dlg
;
367 /* --------------------------------------------------------------------------------------------- */
370 query_set_sel (int new_sel
)
375 /* --------------------------------------------------------------------------------------------- */
377 * Create message dialog. The caller must call dlg_run_done() and
378 * destroy_dlg() to dismiss it. Not safe to call from background.
382 create_message (int flags
, const char *title
, const char *text
, ...)
388 va_start (args
, text
);
389 p
= g_strdup_vprintf (text
, args
);
392 d
= do_create_message (flags
, title
, p
);
398 /* --------------------------------------------------------------------------------------------- */
399 /** Show message box, background safe */
402 message (int flags
, const char *title
, const char *text
, ...)
408 p
= g_strdup_vprintf (text
, ap
);
411 if (title
== MSG_ERROR
)
414 #ifdef WITH_BACKGROUND
415 if (mc_global
.we_are_background
)
420 void (*f
) (int, int *, char *, const char *);
424 wtools_parent_call (func
.p
, NULL
, 3, sizeof (flags
), &flags
, strlen (title
), title
,
428 #endif /* WITH_BACKGROUND */
429 fg_message (flags
, title
, p
);
434 /* --------------------------------------------------------------------------------------------- */
436 * Show input dialog, background safe.
438 * If the arguments "header" and "text" should be translated,
439 * that MUST be done by the caller of these wrappers.
443 input_dialog_help (const char *header
, const char *text
, const char *help
,
444 const char *history_name
, const char *def_text
)
446 #ifdef WITH_BACKGROUND
447 if (mc_global
.we_are_background
)
452 char *(*f
) (const char *, const char *, const char *, const char *, const char *);
454 func
.f
= fg_input_dialog_help
;
455 return wtools_parent_call_string (func
.p
, 5,
456 strlen (header
), header
, strlen (text
),
457 text
, strlen (help
), help
,
458 strlen (history_name
), history_name
,
459 strlen (def_text
), def_text
);
462 #endif /* WITH_BACKGROUND */
463 return fg_input_dialog_help (header
, text
, help
, history_name
, def_text
);
466 /* --------------------------------------------------------------------------------------------- */
467 /** Show input dialog with default help, background safe */
470 input_dialog (const char *header
, const char *text
, const char *history_name
, const char *def_text
)
472 return input_dialog_help (header
, text
, "[Input Line Keys]", history_name
, def_text
);
475 /* --------------------------------------------------------------------------------------------- */
478 input_expand_dialog (const char *header
, const char *text
,
479 const char *history_name
, const char *def_text
)
484 result
= input_dialog (header
, text
, history_name
, def_text
);
487 expanded
= tilde_expand (result
);
494 /* --------------------------------------------------------------------------------------------- */
499 #ifdef WITH_BACKGROUND
500 if (mc_global
.we_are_background
)
502 #endif /* WITH_BACKGROUND */
503 if (mc_global
.tty
.winch_flag
== FALSE
)
507 /* if winch was caugth, we should do not only redraw screen, but
508 reposition/resize all */
509 dialog_change_screen_size ();
513 /* --------------------------------------------------------------------------------------------- */