changed includes from ../mhl/ to <mhl/...>
[midnight-commander.git] / src / wtools.c
blobac79993fd60597d9f746b8c72f15f7fbcf049da6
1 /* Widget based utility functions.
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007 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
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include <config.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include "global.h"
33 #include "tty.h"
34 #include "color.h" /* dialog_colors */
35 #include "dialog.h"
36 #include "widget.h"
37 #include "wtools.h"
38 #include "key.h" /* mi_getch() */
39 #include "complete.h" /* INPUT_COMPLETE_CD */
40 #include "background.h" /* parent_call */
43 Listbox *
44 create_listbox_window (int cols, int lines, const char *title, const char *help)
46 int xpos, ypos, len;
47 Listbox *listbox = g_new (Listbox, 1);
48 const char *cancel_string = _("&Cancel");
50 /* Adjust sizes */
51 lines = (lines > LINES - 6) ? LINES - 6 : lines;
53 if (title && (cols < (len = strlen (title) + 2)))
54 cols = len;
56 /* no &, but 4 spaces around button for brackets and such */
57 if (cols < (len = strlen (cancel_string) + 3))
58 cols = len;
60 cols = cols > COLS - 6 ? COLS - 6 : cols;
61 xpos = (COLS - cols) / 2;
62 ypos = (LINES - lines) / 2 - 2;
64 /* Create components */
65 listbox->dlg =
66 create_dlg (ypos, xpos, lines + 6, cols + 4, dialog_colors, NULL,
67 help, title, DLG_CENTER | DLG_REVERSE);
69 listbox->list = listbox_new (2, 2, cols, lines, 0);
71 add_widget (listbox->dlg,
72 button_new (lines + 3, (cols / 2 + 2) - len / 2, B_CANCEL,
73 NORMAL_BUTTON, cancel_string, 0));
74 add_widget (listbox->dlg, listbox->list);
76 return listbox;
79 /* Returns the number of the item selected */
80 int run_listbox (Listbox *l)
82 int val;
84 run_dlg (l->dlg);
85 if (l->dlg->ret_value == B_CANCEL)
86 val = -1;
87 else
88 val = l->list->pos;
89 destroy_dlg (l->dlg);
90 g_free (l);
91 return val;
95 static Dlg_head *last_query_dlg;
97 static int sel_pos = 0;
99 /* Used to ask questions to the user */
101 query_dialog (const char *header, const char *text, int flags, int count, ...)
103 va_list ap;
104 Dlg_head *query_dlg;
105 WButton *button;
106 WButton *defbutton = NULL;
107 int win_len = 0;
108 int i;
109 int result = -1;
110 int xpos, ypos;
111 int cols, lines;
112 char *cur_name;
113 static const int *query_colors;
115 /* set dialog colors */
116 if (flags & D_ERROR)
117 query_colors = alarm_colors;
118 else
119 query_colors = dialog_colors;
121 if (header == MSG_ERROR)
122 header = _("Error");
124 if (count > 0) {
125 va_start (ap, count);
126 for (i = 0; i < count; i++) {
127 char *cp = va_arg (ap, char *);
128 win_len += strlen (cp) + 6;
129 if (strchr (cp, '&') != NULL)
130 win_len--;
132 va_end (ap);
135 /* count coordinates */
136 msglen (text, &lines, &cols);
137 cols = 6 + max (win_len, max ((int) strlen (header), cols));
138 lines += 4 + (count > 0 ? 2 : 0);
139 xpos = COLS / 2 - cols / 2;
140 ypos = LINES / 3 - (lines - 3) / 2;
142 /* prepare dialog */
143 query_dlg =
144 create_dlg (ypos, xpos, lines, cols, query_colors, NULL,
145 "[QueryBox]", header, DLG_NONE);
147 if (count > 0) {
148 cols = (cols - win_len - 2) / 2 + 2;
149 va_start (ap, count);
150 for (i = 0; i < count; i++) {
151 cur_name = va_arg (ap, char *);
152 xpos = strlen (cur_name) + 6;
153 if (strchr (cur_name, '&') != NULL)
154 xpos--;
156 button =
157 button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON,
158 cur_name, 0);
159 add_widget (query_dlg, button);
160 cols += xpos;
161 if (i == sel_pos)
162 defbutton = button;
164 va_end (ap);
166 add_widget (query_dlg, label_new (2, 3, text));
168 if (defbutton)
169 dlg_select_widget (defbutton);
171 /* run dialog and make result */
172 run_dlg (query_dlg);
173 switch (query_dlg->ret_value) {
174 case B_CANCEL:
175 break;
176 default:
177 result = query_dlg->ret_value - B_USER;
180 /* free used memory */
181 destroy_dlg (query_dlg);
182 } else {
183 add_widget (query_dlg, label_new (2, 3, text));
184 add_widget (query_dlg,
185 button_new (0, 0, 0, HIDDEN_BUTTON, "-", 0));
186 last_query_dlg = query_dlg;
188 sel_pos = 0;
189 return result;
192 void query_set_sel (int new_sel)
194 sel_pos = new_sel;
198 /* Create message dialog */
199 static struct Dlg_head *
200 do_create_message (int flags, const char *title, const char *text)
202 char *p;
203 Dlg_head *d;
205 /* Add empty lines before and after the message */
206 p = g_strconcat ("\n", text, "\n", (char *) NULL);
207 query_dialog (title, p, flags, 0);
208 d = last_query_dlg;
209 init_dlg (d);
210 g_free (p);
212 return d;
217 * Create message dialog. The caller must call dlg_run_done() and
218 * destroy_dlg() to dismiss it. Not safe to call from background.
220 struct Dlg_head *
221 create_message (int flags, const char *title, const char *text, ...)
223 va_list args;
224 Dlg_head *d;
225 char *p;
227 va_start (args, text);
228 p = g_strdup_vprintf (text, args);
229 va_end (args);
231 d = do_create_message (flags, title, p);
232 g_free (p);
234 return d;
239 * Show message dialog. Dismiss it when any key is pressed.
240 * Not safe to call from background.
242 static void
243 fg_message (int flags, const char *title, const char *text)
245 Dlg_head *d;
247 d = do_create_message (flags, title, text);
248 mi_getch ();
249 dlg_run_done (d);
250 destroy_dlg (d);
254 /* Show message box from background */
255 #ifdef WITH_BACKGROUND
256 static void
257 bg_message (int dummy, int *flags, char *title, const char *text)
259 (void) dummy;
260 title = g_strconcat (_("Background process:"), " ", title, (char *) NULL);
261 fg_message (*flags, title, text);
262 g_free (title);
264 #endif /* WITH_BACKGROUND */
267 /* Show message box, background safe */
268 void
269 message (int flags, const char *title, const char *text, ...)
271 char *p;
272 va_list ap;
274 va_start (ap, text);
275 p = g_strdup_vprintf (text, ap);
276 va_end (ap);
278 if (title == MSG_ERROR)
279 title = _("Error");
281 #ifdef WITH_BACKGROUND
282 if (we_are_background) {
283 parent_call ((void *) bg_message, NULL, 3, sizeof (flags), &flags,
284 strlen (title), title, strlen (p), p);
285 } else
286 #endif /* WITH_BACKGROUND */
287 fg_message (flags, title, p);
289 g_free (p);
293 /* {{{ Quick dialog routines */
295 #define I18N(x) (do_int && *x ? (x = _(x)): x)
298 quick_dialog_skip (QuickDialog *qd, int nskip)
300 Dlg_head *dd;
301 void *widget;
302 WRadio *r;
303 int xpos;
304 int ypos;
305 int return_val;
306 WInput *input;
307 QuickWidget *qw;
308 int do_int;
309 int count = 0; /* number of quick widgets */
310 int curr_widget; /* number of the current quick widget */
311 Widget **widgets; /* table of corresponding widgets */
313 if (!qd->i18n) {
314 qd->i18n = 1;
315 do_int = 1;
316 if (*qd->title)
317 qd->title = _(qd->title);
318 } else
319 do_int = 0;
321 if (qd->xpos == -1)
322 dd = create_dlg (0, 0, qd->ylen, qd->xlen, dialog_colors, NULL,
323 qd->help, qd->title,
324 DLG_CENTER | DLG_TRYUP | DLG_REVERSE);
325 else
326 dd = create_dlg (qd->ypos, qd->xpos, qd->ylen, qd->xlen,
327 dialog_colors, NULL, qd->help, qd->title,
328 DLG_REVERSE);
330 /* Count widgets */
331 for (qw = qd->widgets; qw->widget_type; qw++) {
332 count++;
335 widgets = (Widget **) g_new (Widget *, count);
337 for (curr_widget = 0, qw = qd->widgets; qw->widget_type; qw++) {
338 xpos = (qd->xlen * qw->relative_x) / qw->x_divisions;
339 ypos = (qd->ylen * qw->relative_y) / qw->y_divisions;
341 switch (qw->widget_type) {
342 case quick_checkbox:
343 widget = check_new (ypos, xpos, *qw->result, I18N (qw->text));
344 break;
346 case quick_radio:
347 r = radio_new (ypos, xpos, qw->hotkey_pos, const_cast(const char **, qw->str_result));
348 r->pos = r->sel = qw->value;
349 widget = r;
350 break;
352 case quick_button:
353 widget =
354 button_new (ypos, xpos, qw->value,
355 (qw->value ==
356 B_ENTER) ? DEFPUSH_BUTTON : NORMAL_BUTTON,
357 I18N (qw->text), 0);
358 break;
360 /* We use the hotkey pos as the field length */
361 case quick_input:
362 input =
363 input_new (ypos, xpos, INPUT_COLOR, qw->hotkey_pos,
364 qw->text, qw->histname);
365 input->is_password = qw->value == 1;
366 input->point = 0;
367 if (qw->value & 2)
368 input->completion_flags |= INPUT_COMPLETE_CD;
369 widget = input;
370 break;
372 case quick_label:
373 widget = label_new (ypos, xpos, I18N (qw->text));
374 break;
376 default:
377 widget = 0;
378 fprintf (stderr, "QuickWidget: unknown widget type\n");
379 break;
381 widgets[curr_widget++] = widget;
382 add_widget (dd, widget);
385 while (nskip--)
386 dd->current = dd->current->next;
388 run_dlg (dd);
390 /* Get the data if we found something interesting */
391 if (dd->ret_value != B_CANCEL) {
392 for (curr_widget = 0, qw = qd->widgets; qw->widget_type; qw++) {
393 Widget *w = widgets[curr_widget++];
395 switch (qw->widget_type) {
396 case quick_checkbox:
397 *qw->result = ((WCheck *) w)->state & C_BOOL;
398 break;
400 case quick_radio:
401 *qw->result = ((WRadio *) w)->sel;
402 break;
404 case quick_input:
405 if (qw->value & 2)
406 *qw->str_result =
407 tilde_expand (((WInput *) w)->buffer);
408 else
409 *qw->str_result = g_strdup (((WInput *) w)->buffer);
410 break;
414 return_val = dd->ret_value;
415 destroy_dlg (dd);
416 g_free (widgets);
418 return return_val;
421 int quick_dialog (QuickDialog *qd)
423 return quick_dialog_skip (qd, 0);
426 /* }}} */
428 /* {{{ Input routines */
430 #define INPUT_INDEX 2
433 * Show dialog, not background safe.
435 * If the arguments "header" and "text" should be translated,
436 * that MUST be done by the caller of fg_input_dialog_help().
439 static char *
440 fg_input_dialog_help (const char *header, const char *text, const char *help,
441 const char *def_text)
443 QuickDialog Quick_input;
444 QuickWidget quick_widgets[] = {
445 {quick_button, 6, 10, 1, 0, N_("&Cancel"), 0, B_CANCEL, 0, 0,
446 NULL},
447 {quick_button, 3, 10, 1, 0, N_("&OK"), 0, B_ENTER, 0, 0, NULL},
448 {quick_input, 4, 80, 0, 0, "", 58, 0, 0, 0, NULL},
449 {quick_label, 4, 80, 2, 0, "", 0, 0, 0, 0, NULL},
450 NULL_QuickWidget
453 int len;
454 int i;
455 int lines, cols;
456 int ret;
457 char *my_str;
458 char histname[64] = "inp|";
459 char *p_text;
461 /* we need a unique name for histname because widget.c:history_tool()
462 needs a unique name for each dialog - using the header is ideal */
463 g_strlcpy (histname + 3, header, 61);
464 quick_widgets[2].histname = histname;
466 msglen (text, &lines, &cols);
467 len = max ((int) strlen (header), cols) + 4;
468 len = max (len, 64);
470 /* The special value of def_text is used to identify password boxes
471 and hide characters with "*". Don't save passwords in history! */
472 if (def_text == INPUT_PASSWORD) {
473 quick_widgets[INPUT_INDEX].value = 1;
474 histname[3] = 0;
475 def_text = "";
476 } else {
477 quick_widgets[INPUT_INDEX].value = 0;
480 #ifdef ENABLE_NLS
482 * An attempt to place buttons symmetrically, based on actual i18n
483 * length of the string. It looks nicer with i18n (IMO) - alex
485 quick_widgets[0].text = _(quick_widgets[0].text);
486 quick_widgets[1].text = _(quick_widgets[1].text);
487 quick_widgets[0].relative_x = len / 2 + 4;
488 quick_widgets[1].relative_x =
489 len / 2 - (strlen (quick_widgets[1].text) + 9);
490 quick_widgets[0].x_divisions = quick_widgets[1].x_divisions = len;
491 #endif /* ENABLE_NLS */
493 Quick_input.xlen = len;
494 Quick_input.xpos = -1;
495 Quick_input.title = header;
496 Quick_input.help = help;
497 Quick_input.i18n = 1; /* The dialog is already translated. */
498 p_text = g_strstrip (g_strdup (text));
499 quick_widgets[INPUT_INDEX + 1].text = p_text;
500 quick_widgets[INPUT_INDEX].text = def_text;
502 for (i = 0; i < 4; i++)
503 quick_widgets[i].y_divisions = lines + 6;
504 Quick_input.ylen = lines + 6;
506 for (i = 0; i < 3; i++)
507 quick_widgets[i].relative_y += 2 + lines;
509 quick_widgets[INPUT_INDEX].str_result = &my_str;
511 Quick_input.widgets = quick_widgets;
512 ret = quick_dialog (&Quick_input);
513 g_free (p_text);
515 if (ret != B_CANCEL) {
516 return my_str;
517 } else
518 return 0;
522 * Show input dialog, background safe.
524 * If the arguments "header" and "text" should be translated,
525 * that MUST be done by the caller of these wrappers.
527 char *
528 input_dialog_help (const char *header, const char *text, const char *help, const char *def_text)
530 #ifdef WITH_BACKGROUND
531 if (we_are_background)
532 return parent_call_string ((void *) fg_input_dialog_help, 4,
533 strlen (header), header, strlen (text),
534 text, strlen (help), help,
535 strlen (def_text), def_text);
536 else
537 #endif /* WITH_BACKGROUND */
538 return fg_input_dialog_help (header, text, help, def_text);
541 /* Show input dialog with default help, background safe */
542 char *input_dialog (const char *header, const char *text, const char *def_text)
544 return input_dialog_help (header, text, "[Input Line Keys]", def_text);
547 char *
548 input_expand_dialog (const char *header, const char *text, const char *def_text)
550 char *result;
551 char *expanded;
553 result = input_dialog (header, text, def_text);
554 if (result) {
555 expanded = tilde_expand (result);
556 g_free (result);
557 return expanded;
559 return result;
562 /* }}} */
564 /* }}} */
566 Cause emacs to enter folding mode for this file:
567 Local variables:
568 end: