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 Dlg_head
*prev_dlg
= NULL
;
71 /* get dialog under h */
74 if (top_dlg
->data
!= (void *) h
)
75 prev_dlg
= (Dlg_head
*) top_dlg
->data
;
80 /* Top dialog is current if it is visible.
81 Get previous dialog in stack */
82 p
= g_list_next (top_dlg
);
84 prev_dlg
= (Dlg_head
*) p
->data
;
88 /* if previous dialog is not fullscreen'd -- overlap it */
89 if (prev_dlg
== NULL
|| prev_dlg
->fullscreen
)
90 ypos
= LINES
/ 3 - (h
->lines
- 3) / 2;
92 ypos
= prev_dlg
->y
+ 2;
94 xpos
= COLS
/ 2 - h
->cols
/ 2;
97 dlg_set_position (h
, ypos
, xpos
, ypos
+ h
->lines
, xpos
+ h
->cols
);
102 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
106 /* --------------------------------------------------------------------------------------------- */
107 /** Create message dialog */
109 static struct Dlg_head
*
110 do_create_message (int flags
, const char *title
, const char *text
)
115 /* Add empty lines before and after the message */
116 p
= g_strconcat ("\n", text
, "\n", (char *) NULL
);
117 query_dialog (title
, p
, flags
, 0);
120 /* do resize before initing and running */
121 default_query_callback (d
, NULL
, DLG_RESIZE
, 0, NULL
);
129 /* --------------------------------------------------------------------------------------------- */
131 * Show message dialog. Dismiss it when any key is pressed.
132 * Not safe to call from background.
136 fg_message (int flags
, const char *title
, const char *text
)
140 d
= do_create_message (flags
, title
, text
);
147 /* --------------------------------------------------------------------------------------------- */
148 /** Show message box from background */
150 #ifdef WITH_BACKGROUND
152 bg_message (int dummy
, int *flags
, char *title
, const char *text
)
155 title
= g_strconcat (_("Background process:"), " ", title
, (char *) NULL
);
156 fg_message (*flags
, title
, text
);
159 #endif /* WITH_BACKGROUND */
161 /* --------------------------------------------------------------------------------------------- */
164 * Show dialog, not background safe.
166 * If the arguments "header" and "text" should be translated,
167 * that MUST be done by the caller of fg_input_dialog_help().
169 * The argument "history_name" holds the name of a section
170 * in the history file. Data entered in the input field of
171 * the dialog box will be stored there.
175 fg_input_dialog_help (const char *header
, const char *text
, const char *help
,
176 const char *history_name
, const char *def_text
)
180 QuickWidget quick_widgets
[] = {
181 /* 0 */ QUICK_BUTTON (6, 64, 1, 0, N_("&Cancel"), B_CANCEL
, NULL
),
182 /* 1 */ QUICK_BUTTON (3, 64, 1, 0, N_("&OK"), B_ENTER
, NULL
),
183 /* 2 */ QUICK_INPUT (3, 64, 0, 0, def_text
, 58, 0, NULL
, &my_str
),
184 /* 3 */ QUICK_LABEL (3, 64, 2, 0, ""),
188 int b0_len
, b1_len
, b_len
, gap
;
189 char histname
[64] = "inp|";
198 quick_widgets
[0].u
.button
.text
= _(quick_widgets
[0].u
.button
.text
);
199 quick_widgets
[1].u
.button
.text
= _(quick_widgets
[1].u
.button
.text
);
200 #endif /* ENABLE_NLS */
202 b0_len
= str_term_width1 (quick_widgets
[0].u
.button
.text
) + 3;
203 b1_len
= str_term_width1 (quick_widgets
[1].u
.button
.text
) + 5; /* default button */
204 b_len
= b0_len
+ b1_len
+ 2; /* including gap */
207 if (history_name
!= NULL
&& *history_name
!= '\0')
209 g_strlcpy (histname
+ 3, history_name
, sizeof (histname
) - 3);
210 quick_widgets
[2].u
.input
.histname
= histname
;
213 /* The special value of def_text is used to identify password boxes
214 and hide characters with "*". Don't save passwords in history! */
215 if (def_text
== INPUT_PASSWORD
)
217 quick_widgets
[2].u
.input
.flags
= 1;
219 quick_widgets
[2].u
.input
.text
= "";
223 p_text
= g_strstrip (g_strdup (text
));
224 str_msg_term_size (p_text
, &lines
, &cols
);
225 quick_widgets
[3].u
.label
.text
= p_text
;
228 len
= str_term_width1 (header
);
229 len
= max (max (len
, cols
) + 4, 64);
230 len
= min (max (len
, b_len
+ 6), COLS
);
232 /* button locations */
233 gap
= (len
- 8 - b_len
) / 3;
234 quick_widgets
[1].relative_x
= 3 + gap
;
235 quick_widgets
[0].relative_x
= quick_widgets
[1].relative_x
+ b1_len
+ gap
+ 2;
238 QuickDialog Quick_input
= {
239 len
, lines
+ 6, -1, -1, header
,
240 help
, quick_widgets
, NULL
, TRUE
243 for (i
= 0; i
< 4; i
++)
245 quick_widgets
[i
].x_divisions
= Quick_input
.xlen
;
246 quick_widgets
[i
].y_divisions
= Quick_input
.ylen
;
249 for (i
= 0; i
< 3; i
++)
250 quick_widgets
[i
].relative_y
+= 2 + lines
;
252 /* input line length */
253 quick_widgets
[2].u
.input
.len
= Quick_input
.xlen
- 6;
255 ret
= quick_dialog (&Quick_input
);
260 return (ret
!= B_CANCEL
) ? my_str
: NULL
;
263 /* --------------------------------------------------------------------------------------------- */
265 #ifdef WITH_BACKGROUND
267 wtools_parent_call (void *routine
, gpointer ctx
, int argc
, ...)
269 ev_background_parent_call_t event_data
;
271 event_data
.routine
= routine
;
272 event_data
.ctx
= ctx
;
273 event_data
.argc
= argc
;
274 va_start (event_data
.ap
, argc
);
275 mc_event_raise (MCEVENT_GROUP_CORE
, "background_parent_call", (gpointer
) & event_data
);
276 va_end (event_data
.ap
);
277 return event_data
.ret
.i
;
280 /* --------------------------------------------------------------------------------------------- */
283 wtools_parent_call_string (void *routine
, int argc
, ...)
285 ev_background_parent_call_t event_data
;
287 event_data
.routine
= routine
;
288 event_data
.argc
= argc
;
289 va_start (event_data
.ap
, argc
);
290 mc_event_raise (MCEVENT_GROUP_CORE
, "background_parent_call_string", (gpointer
) & event_data
);
291 va_end (event_data
.ap
);
292 return event_data
.ret
.s
;
294 #endif /* WITH_BACKGROUND */
296 /* --------------------------------------------------------------------------------------------- */
297 /*** public functions ****************************************************************************/
298 /* --------------------------------------------------------------------------------------------- */
300 /** Used to ask questions to the user */
302 query_dialog (const char *header
, const char *text
, int flags
, int count
, ...)
307 WButton
*defbutton
= NULL
;
313 const int *query_colors
= (flags
& D_ERROR
) ? alarm_colors
: dialog_colors
;
315 if (header
== MSG_ERROR
)
320 va_start (ap
, count
);
321 for (i
= 0; i
< count
; i
++)
323 char *cp
= va_arg (ap
, char *);
324 win_len
+= str_term_width1 (cp
) + 6;
325 if (strchr (cp
, '&') != NULL
)
331 /* count coordinates */
332 str_msg_term_size (text
, &lines
, &cols
);
333 cols
= 6 + max (win_len
, max (str_term_width1 (header
), cols
));
334 lines
+= 4 + (count
> 0 ? 2 : 0);
338 create_dlg (TRUE
, 0, 0, lines
, cols
, query_colors
, default_query_callback
,
339 "[QueryBox]", header
, DLG_NONE
);
343 cols
= (cols
- win_len
- 2) / 2 + 2;
344 va_start (ap
, count
);
345 for (i
= 0; i
< count
; i
++)
349 cur_name
= va_arg (ap
, char *);
350 xpos
= str_term_width1 (cur_name
) + 6;
351 if (strchr (cur_name
, '&') != NULL
)
354 button
= button_new (lines
- 3, cols
, B_USER
+ i
, NORMAL_BUTTON
, cur_name
, 0);
355 add_widget (query_dlg
, button
);
362 add_widget (query_dlg
, label_new (2, 3, text
));
364 /* do resize before running and selecting any widget */
365 default_query_callback (query_dlg
, NULL
, DLG_RESIZE
, 0, NULL
);
368 dlg_select_widget (defbutton
);
370 /* run dialog and make result */
371 switch (run_dlg (query_dlg
))
376 result
= query_dlg
->ret_value
- B_USER
;
379 /* free used memory */
380 destroy_dlg (query_dlg
);
384 add_widget (query_dlg
, label_new (2, 3, text
));
385 add_widget (query_dlg
, button_new (0, 0, 0, HIDDEN_BUTTON
, "-", 0));
386 last_query_dlg
= query_dlg
;
392 /* --------------------------------------------------------------------------------------------- */
395 query_set_sel (int new_sel
)
400 /* --------------------------------------------------------------------------------------------- */
402 * Create message dialog. The caller must call dlg_run_done() and
403 * destroy_dlg() to dismiss it. Not safe to call from background.
407 create_message (int flags
, const char *title
, const char *text
, ...)
413 va_start (args
, text
);
414 p
= g_strdup_vprintf (text
, args
);
417 d
= do_create_message (flags
, title
, p
);
423 /* --------------------------------------------------------------------------------------------- */
424 /** Show message box, background safe */
427 message (int flags
, const char *title
, const char *text
, ...)
433 p
= g_strdup_vprintf (text
, ap
);
436 if (title
== MSG_ERROR
)
439 #ifdef WITH_BACKGROUND
440 if (mc_global
.we_are_background
)
445 void (*f
) (int, int *, char *, const char *);
449 wtools_parent_call (func
.p
, NULL
, 3, sizeof (flags
), &flags
, strlen (title
), title
,
453 #endif /* WITH_BACKGROUND */
454 fg_message (flags
, title
, p
);
459 /* --------------------------------------------------------------------------------------------- */
461 * Show input dialog, background safe.
463 * If the arguments "header" and "text" should be translated,
464 * that MUST be done by the caller of these wrappers.
468 input_dialog_help (const char *header
, const char *text
, const char *help
,
469 const char *history_name
, const char *def_text
)
471 #ifdef WITH_BACKGROUND
472 if (mc_global
.we_are_background
)
477 char *(*f
) (const char *, const char *, const char *, const char *, const char *);
479 func
.f
= fg_input_dialog_help
;
480 return wtools_parent_call_string (func
.p
, 5,
481 strlen (header
), header
, strlen (text
),
482 text
, strlen (help
), help
,
483 strlen (history_name
), history_name
,
484 strlen (def_text
), def_text
);
487 #endif /* WITH_BACKGROUND */
488 return fg_input_dialog_help (header
, text
, help
, history_name
, def_text
);
491 /* --------------------------------------------------------------------------------------------- */
492 /** Show input dialog with default help, background safe */
495 input_dialog (const char *header
, const char *text
, const char *history_name
, const char *def_text
)
497 return input_dialog_help (header
, text
, "[Input Line Keys]", history_name
, def_text
);
500 /* --------------------------------------------------------------------------------------------- */
503 input_expand_dialog (const char *header
, const char *text
,
504 const char *history_name
, const char *def_text
)
509 result
= input_dialog (header
, text
, history_name
, def_text
);
512 expanded
= tilde_expand (result
);
519 /* --------------------------------------------------------------------------------------------- */
524 #ifdef WITH_BACKGROUND
525 if (mc_global
.we_are_background
)
527 #endif /* WITH_BACKGROUND */
528 if (mc_global
.tty
.winch_flag
== FALSE
)
532 /* if winch was caugth, we should do not only redraw screen, but
533 reposition/resize all */
534 dialog_change_screen_size ();
538 /* --------------------------------------------------------------------------------------------- */