fixed relative symlink operations. Symlink now stay relative
[midnight-commander.git] / lib / widget / wtools.c
blobe7ef931477048086098ee7a39942fa4d71f8f1a8
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
7 1995 Jakub Jelinek
8 1995 Andrej Borsenkow
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.
27 /** \file wtools.c
28 * \brief Source: widget based utility functions
31 #include <config.h>
33 #include <stdarg.h>
34 #include <stdlib.h>
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 */
61 static cb_ret_t
62 default_query_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
64 switch (msg)
66 case DLG_RESIZE:
68 int xpos = COLS / 2 - h->cols / 2;
69 int ypos = LINES / 3 - (h->lines - 3) / 2;
71 /* set position */
72 dlg_set_position (h, ypos, xpos, ypos + h->lines, xpos + h->cols);
74 return MSG_HANDLED;
76 default:
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)
87 char *p;
88 Dlg_head *d;
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);
93 d = last_query_dlg;
95 /* do resize before initing and running */
96 default_query_callback (d, NULL, DLG_RESIZE, 0, NULL);
98 init_dlg (d);
99 g_free (p);
101 return d;
104 /* --------------------------------------------------------------------------------------------- */
106 * Show message dialog. Dismiss it when any key is pressed.
107 * Not safe to call from background.
110 static void
111 fg_message (int flags, const char *title, const char *text)
113 Dlg_head *d;
115 d = do_create_message (flags, title, text);
116 tty_getch ();
117 dlg_run_done (d);
118 destroy_dlg (d);
122 /* --------------------------------------------------------------------------------------------- */
123 /** Show message box from background */
125 #ifdef WITH_BACKGROUND
126 static void
127 bg_message (int dummy, int *flags, char *title, const char *text)
129 (void) dummy;
130 title = g_strconcat (_("Background process:"), " ", title, (char *) NULL);
131 fg_message (*flags, title, text);
132 g_free (title);
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.
149 static char *
150 fg_input_dialog_help (const char *header, const char *text, const char *help,
151 const char *history_name, const char *def_text)
153 char *my_str;
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, ""),
160 QUICK_END
163 int b0_len, b1_len, b_len, gap;
164 char histname[64] = "inp|";
165 int lines, cols;
166 int len;
167 int i;
168 char *p_text;
169 int ret;
171 /* buttons */
172 #ifdef ENABLE_NLS
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 */
181 /* input line */
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;
193 histname[3] = '\0';
194 quick_widgets[2].u.input.text = "";
197 /* 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;
202 /* dialog width */
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);
233 g_free (p_text);
235 return (ret != B_CANCEL) ? my_str : NULL;
238 /* --------------------------------------------------------------------------------------------- */
240 #ifdef WITH_BACKGROUND
241 static int
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 /* --------------------------------------------------------------------------------------------- */
257 static char *
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, ...)
279 va_list ap;
280 Dlg_head *query_dlg;
281 WButton *button;
282 WButton *defbutton = NULL;
283 int win_len = 0;
284 int i;
285 int result = -1;
286 int cols, lines;
287 char *cur_name;
288 const int *query_colors = (flags & D_ERROR) ? alarm_colors : dialog_colors;
290 if (header == MSG_ERROR)
291 header = _("Error");
293 if (count > 0)
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)
301 win_len--;
303 va_end (ap);
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);
311 /* prepare dialog */
312 query_dlg =
313 create_dlg (TRUE, 0, 0, lines, cols, query_colors, default_query_callback,
314 "[QueryBox]", header, DLG_NONE);
316 if (count > 0)
318 cols = (cols - win_len - 2) / 2 + 2;
319 va_start (ap, count);
320 for (i = 0; i < count; i++)
322 int xpos;
324 cur_name = va_arg (ap, char *);
325 xpos = str_term_width1 (cur_name) + 6;
326 if (strchr (cur_name, '&') != NULL)
327 xpos--;
329 button = button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON, cur_name, 0);
330 add_widget (query_dlg, button);
331 cols += xpos;
332 if (i == sel_pos)
333 defbutton = button;
335 va_end (ap);
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);
342 if (defbutton)
343 dlg_select_widget (defbutton);
345 /* run dialog and make result */
346 switch (run_dlg (query_dlg))
348 case B_CANCEL:
349 break;
350 default:
351 result = query_dlg->ret_value - B_USER;
354 /* free used memory */
355 destroy_dlg (query_dlg);
357 else
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;
363 sel_pos = 0;
364 return result;
367 /* --------------------------------------------------------------------------------------------- */
369 void
370 query_set_sel (int new_sel)
372 sel_pos = 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.
381 struct Dlg_head *
382 create_message (int flags, const char *title, const char *text, ...)
384 va_list args;
385 Dlg_head *d;
386 char *p;
388 va_start (args, text);
389 p = g_strdup_vprintf (text, args);
390 va_end (args);
392 d = do_create_message (flags, title, p);
393 g_free (p);
395 return d;
398 /* --------------------------------------------------------------------------------------------- */
399 /** Show message box, background safe */
401 void
402 message (int flags, const char *title, const char *text, ...)
404 char *p;
405 va_list ap;
407 va_start (ap, text);
408 p = g_strdup_vprintf (text, ap);
409 va_end (ap);
411 if (title == MSG_ERROR)
412 title = _("Error");
414 #ifdef WITH_BACKGROUND
415 if (mc_global.we_are_background)
417 union
419 void *p;
420 void (*f) (int, int *, char *, const char *);
421 } func;
422 func.f = bg_message;
424 wtools_parent_call (func.p, NULL, 3, sizeof (flags), &flags, strlen (title), title,
425 strlen (p), p);
427 else
428 #endif /* WITH_BACKGROUND */
429 fg_message (flags, title, p);
431 g_free (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.
442 char *
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)
449 union
451 void *p;
452 char *(*f) (const char *, const char *, const char *, const char *, const char *);
453 } func;
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);
461 else
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 */
469 char *
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 /* --------------------------------------------------------------------------------------------- */
477 char *
478 input_expand_dialog (const char *header, const char *text,
479 const char *history_name, const char *def_text)
481 char *result;
482 char *expanded;
484 result = input_dialog (header, text, history_name, def_text);
485 if (result)
487 expanded = tilde_expand (result);
488 g_free (result);
489 return expanded;
491 return result;
494 /* --------------------------------------------------------------------------------------------- */
496 void
497 mc_refresh (void)
499 #ifdef WITH_BACKGROUND
500 if (mc_global.we_are_background)
501 return;
502 #endif /* WITH_BACKGROUND */
503 if (mc_global.tty.winch_flag == FALSE)
504 tty_refresh ();
505 else
507 /* if winch was caugth, we should do not only redraw screen, but
508 reposition/resize all */
509 dialog_change_screen_size ();
513 /* --------------------------------------------------------------------------------------------- */