Listbox window: without button, listbox only.
[midnight-commander.git] / src / wtools.c
blob783010b447f3eef6bd6553f24265db73da31f013
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 /** \file wtools.c
27 * \brief Source: widget based utility functions
30 #include <config.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <string.h>
36 #include "global.h"
38 #include "../src/tty/tty.h"
39 #include "../src/tty/color.h" /* INPUT_COLOR */
40 #include "../src/tty/key.h" /* tty_getch() */
42 #include "dialog.h"
43 #include "widget.h"
44 #include "wtools.h"
45 #include "background.h" /* parent_call */
46 #include "strutil.h"
49 Listbox *
50 create_listbox_window_delta (int delta_x, int delta_y, int cols, int lines,
51 const char *title, const char *help)
53 const int listbox_colors[4] =
55 MENU_ENTRY_COLOR,
56 MENU_SELECTED_COLOR,
57 MENU_HOT_COLOR,
58 MENU_HOTSEL_COLOR,
61 int xpos, ypos, len;
62 Listbox *listbox;
64 /* Adjust sizes */
65 lines = min (lines, LINES - 6);
67 if (title != NULL) {
68 len = str_term_width1 (title) + 4;
69 cols = max (cols, len);
72 cols = min (cols, COLS - 6);
74 /* adjust position */
75 xpos = (COLS - cols + delta_x) / 2;
76 ypos = (LINES - lines + delta_y) / 2 - 2;
78 listbox = g_new (Listbox, 1);
80 listbox->dlg =
81 create_dlg (ypos, xpos, lines + 4, cols + 4, listbox_colors, NULL,
82 help, title, DLG_REVERSE);
84 listbox->list = listbox_new (2, 2, lines, cols, NULL);
85 add_widget (listbox->dlg, listbox->list);
87 return listbox;
90 Listbox *
91 create_listbox_window (int cols, int lines, const char *title, const char *help)
93 return create_listbox_window_delta (0, 0, cols, lines, title, help);
96 /* Returns the number of the item selected */
97 int run_listbox (Listbox *l)
99 int val;
101 run_dlg (l->dlg);
102 if (l->dlg->ret_value == B_CANCEL)
103 val = -1;
104 else
105 val = l->list->pos;
106 destroy_dlg (l->dlg);
107 g_free (l);
108 return val;
112 static Dlg_head *last_query_dlg;
114 static int sel_pos = 0;
116 /* Used to ask questions to the user */
118 query_dialog (const char *header, const char *text, int flags, int count, ...)
120 va_list ap;
121 Dlg_head *query_dlg;
122 WButton *button;
123 WButton *defbutton = NULL;
124 int win_len = 0;
125 int i;
126 int result = -1;
127 int xpos, ypos;
128 int cols, lines;
129 char *cur_name;
130 static const int *query_colors;
132 /* set dialog colors */
133 if (flags & D_ERROR)
134 query_colors = alarm_colors;
135 else
136 query_colors = dialog_colors;
138 if (header == MSG_ERROR)
139 header = _("Error");
141 if (count > 0) {
142 va_start (ap, count);
143 for (i = 0; i < count; i++) {
144 char *cp = va_arg (ap, char *);
145 win_len += str_term_width1 (cp) + 6;
146 if (strchr (cp, '&') != NULL)
147 win_len--;
149 va_end (ap);
152 /* count coordinates */
153 str_msg_term_size (text, &lines, &cols);
154 cols = 6 + max (win_len, max (str_term_width1 (header), cols));
155 lines += 4 + (count > 0 ? 2 : 0);
156 xpos = COLS / 2 - cols / 2;
157 ypos = LINES / 3 - (lines - 3) / 2;
159 /* prepare dialog */
160 query_dlg =
161 create_dlg (ypos, xpos, lines, cols, query_colors, NULL,
162 "[QueryBox]", header, DLG_NONE);
164 if (count > 0) {
165 cols = (cols - win_len - 2) / 2 + 2;
166 va_start (ap, count);
167 for (i = 0; i < count; i++) {
168 cur_name = va_arg (ap, char *);
169 xpos = str_term_width1 (cur_name) + 6;
170 if (strchr (cur_name, '&') != NULL)
171 xpos--;
173 button =
174 button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON,
175 cur_name, 0);
176 add_widget (query_dlg, button);
177 cols += xpos;
178 if (i == sel_pos)
179 defbutton = button;
181 va_end (ap);
183 add_widget (query_dlg, label_new (2, 3, text));
185 if (defbutton)
186 dlg_select_widget (defbutton);
188 /* run dialog and make result */
189 run_dlg (query_dlg);
190 switch (query_dlg->ret_value) {
191 case B_CANCEL:
192 break;
193 default:
194 result = query_dlg->ret_value - B_USER;
197 /* free used memory */
198 destroy_dlg (query_dlg);
199 } else {
200 add_widget (query_dlg, label_new (2, 3, text));
201 add_widget (query_dlg,
202 button_new (0, 0, 0, HIDDEN_BUTTON, "-", 0));
203 last_query_dlg = query_dlg;
205 sel_pos = 0;
206 return result;
209 void query_set_sel (int new_sel)
211 sel_pos = new_sel;
215 /* Create message dialog */
216 static struct Dlg_head *
217 do_create_message (int flags, const char *title, const char *text)
219 char *p;
220 Dlg_head *d;
222 /* Add empty lines before and after the message */
223 p = g_strconcat ("\n", text, "\n", (char *) NULL);
224 query_dialog (title, p, flags, 0);
225 d = last_query_dlg;
226 init_dlg (d);
227 g_free (p);
229 return d;
234 * Create message dialog. The caller must call dlg_run_done() and
235 * destroy_dlg() to dismiss it. Not safe to call from background.
237 struct Dlg_head *
238 create_message (int flags, const char *title, const char *text, ...)
240 va_list args;
241 Dlg_head *d;
242 char *p;
244 va_start (args, text);
245 p = g_strdup_vprintf (text, args);
246 va_end (args);
248 d = do_create_message (flags, title, p);
249 g_free (p);
251 return d;
256 * Show message dialog. Dismiss it when any key is pressed.
257 * Not safe to call from background.
259 static void
260 fg_message (int flags, const char *title, const char *text)
262 Dlg_head *d;
264 d = do_create_message (flags, title, text);
265 tty_getch ();
266 dlg_run_done (d);
267 destroy_dlg (d);
271 /* Show message box from background */
272 #ifdef WITH_BACKGROUND
273 static void
274 bg_message (int dummy, int *flags, char *title, const char *text)
276 (void) dummy;
277 title = g_strconcat (_("Background process:"), " ", title, (char *) NULL);
278 fg_message (*flags, title, text);
279 g_free (title);
281 #endif /* WITH_BACKGROUND */
284 /* Show message box, background safe */
285 void
286 message (int flags, const char *title, const char *text, ...)
288 char *p;
289 va_list ap;
290 union {
291 void *p;
292 void (*f) (int, int *, char *, const char *);
293 } func;
295 va_start (ap, text);
296 p = g_strdup_vprintf (text, ap);
297 va_end (ap);
299 if (title == MSG_ERROR)
300 title = _("Error");
302 #ifdef WITH_BACKGROUND
303 if (we_are_background) {
304 func.f = bg_message;
305 parent_call (func.p, NULL, 3, sizeof (flags), &flags,
306 strlen (title), title, strlen (p), p);
307 } else
308 #endif /* WITH_BACKGROUND */
309 fg_message (flags, title, p);
311 g_free (p);
315 /* {{{ Quick dialog routines */
317 #define I18N(x) (do_int && *x ? (x = _(x)): x)
320 quick_dialog_skip (QuickDialog *qd, int nskip)
322 Dlg_head *dd;
324 WRadio *r;
325 int xpos;
326 int ypos;
327 int return_val;
328 WInput *input;
329 QuickWidget *qw;
330 int do_int;
332 if (!qd->i18n) {
333 qd->i18n = 1;
334 do_int = 1;
335 if (*qd->title)
336 qd->title = _(qd->title);
337 } else
338 do_int = 0;
340 if (qd->xpos == -1)
341 dd = create_dlg (0, 0, qd->ylen, qd->xlen, dialog_colors, NULL,
342 qd->help, qd->title,
343 DLG_CENTER | DLG_TRYUP | DLG_REVERSE);
344 else
345 dd = create_dlg (qd->ypos, qd->xpos, qd->ylen, qd->xlen,
346 dialog_colors, NULL, qd->help, qd->title,
347 DLG_REVERSE);
349 for (qw = qd->widgets; qw->widget_type; qw++) {
350 xpos = (qd->xlen * qw->relative_x) / qw->x_divisions;
351 ypos = (qd->ylen * qw->relative_y) / qw->y_divisions;
353 switch (qw->widget_type) {
354 case quick_checkbox:
355 qw->widget = (Widget *)check_new (ypos, xpos, *qw->result, I18N (qw->text));
356 break;
358 case quick_radio:
359 r = radio_new (ypos, xpos, qw->hotkey_pos, const_cast(const char **, qw->str_result));
360 r->pos = r->sel = qw->value;
361 qw->widget = (Widget *)r;
362 break;
364 case quick_button:
365 qw->widget = (Widget *)
366 button_new (ypos, xpos, qw->value,
367 (qw->value ==
368 B_ENTER) ? DEFPUSH_BUTTON : NORMAL_BUTTON,
369 I18N (qw->text), (bcback) qw->cb);
370 break;
372 /* We use the hotkey pos as the field length */
373 case quick_input:
374 input =
375 input_new (ypos, xpos, INPUT_COLOR, qw->hotkey_pos,
376 qw->text, qw->histname, INPUT_COMPLETE_DEFAULT);
377 input->is_password = qw->value == 1;
378 input->point = 0;
379 if (qw->value & 2)
380 input->completion_flags |= INPUT_COMPLETE_CD;
381 qw->widget = (Widget *)input;
382 break;
384 case quick_label:
385 qw->widget = (Widget *)label_new (ypos, xpos, I18N (qw->text));
386 break;
388 default:
389 qw->widget = 0;
390 fprintf (stderr, "QuickWidget: unknown widget type\n");
391 break;
393 add_widget (dd, qw->widget);
396 while (nskip--)
397 dd->current = dd->current->next;
399 run_dlg (dd);
401 /* Get the data if we found something interesting */
402 if (dd->ret_value != B_CANCEL) {
403 for (qw = qd->widgets; qw->widget_type; qw++) {
404 Widget *w = qw->widget;
406 switch (qw->widget_type) {
407 case quick_checkbox:
408 *qw->result = ((WCheck *) w)->state & C_BOOL;
409 break;
411 case quick_radio:
412 *qw->result = ((WRadio *) w)->sel;
413 break;
415 case quick_input:
416 if (qw->value & 2)
417 *qw->str_result =
418 tilde_expand (((WInput *) w)->buffer);
419 else
420 *qw->str_result = g_strdup (((WInput *) w)->buffer);
421 break;
425 return_val = dd->ret_value;
426 destroy_dlg (dd);
428 return return_val;
431 int quick_dialog (QuickDialog *qd)
433 return quick_dialog_skip (qd, 0);
436 /* }}} */
438 /* {{{ Input routines */
440 #define INPUT_INDEX 2
443 * Show dialog, not background safe.
445 * If the arguments "header" and "text" should be translated,
446 * that MUST be done by the caller of fg_input_dialog_help().
448 * The argument "history_name" holds the name of a section
449 * in the history file. Data entered in the input field of
450 * the dialog box will be stored there.
453 static char *
454 fg_input_dialog_help (const char *header, const char *text, const char *help,
455 const char *history_name, const char *def_text)
457 QuickDialog Quick_input;
458 QuickWidget quick_widgets[] = {
459 {quick_button, 6, 10, 1, 0, N_("&Cancel"), 0, B_CANCEL, 0, 0,
460 NULL, NULL, NULL},
461 {quick_button, 3, 10, 1, 0, N_("&OK"), 0, B_ENTER, 0, 0, NULL, NULL, NULL},
462 {quick_input, 4, 80, 0, 0, "", 58, 0, 0, 0, NULL, NULL, NULL},
463 {quick_label, 4, 80, 2, 0, "", 0, 0, 0, 0, NULL, NULL, NULL},
464 NULL_QuickWidget
467 int len;
468 int i;
469 int lines, cols;
470 int ret;
471 char *my_str;
472 char histname[64] = "inp|";
473 char *p_text;
475 if (history_name != NULL && *history_name != '\0') {
476 g_strlcpy (histname + 3, history_name, 61);
477 quick_widgets[2].histname = histname;
480 msglen (text, &lines, &cols);
481 len = max (str_term_width1 (header), cols) + 4;
482 len = max (len, 64);
484 /* The special value of def_text is used to identify password boxes
485 and hide characters with "*". Don't save passwords in history! */
486 if (def_text == INPUT_PASSWORD) {
487 quick_widgets[INPUT_INDEX].value = 1;
488 histname[3] = 0;
489 def_text = "";
490 } else {
491 quick_widgets[INPUT_INDEX].value = 0;
494 #ifdef ENABLE_NLS
496 * An attempt to place buttons symmetrically, based on actual i18n
497 * length of the string. It looks nicer with i18n (IMO) - alex
499 quick_widgets[0].text = _(quick_widgets[0].text);
500 quick_widgets[1].text = _(quick_widgets[1].text);
501 quick_widgets[0].relative_x = len / 2 + 4;
502 quick_widgets[1].relative_x =
503 len / 2 - (str_term_width1 (quick_widgets[1].text) + 9);
504 quick_widgets[0].x_divisions = quick_widgets[1].x_divisions = len;
505 #endif /* ENABLE_NLS */
507 Quick_input.xlen = len;
508 Quick_input.xpos = -1;
509 Quick_input.title = header;
510 Quick_input.help = help;
511 Quick_input.i18n = 1; /* The dialog is already translated. */
512 p_text = g_strstrip (g_strdup (text));
513 quick_widgets[INPUT_INDEX + 1].text = p_text;
514 quick_widgets[INPUT_INDEX].text = def_text;
516 for (i = 0; i < 4; i++)
517 quick_widgets[i].y_divisions = lines + 6;
518 Quick_input.ylen = lines + 6;
520 for (i = 0; i < 3; i++)
521 quick_widgets[i].relative_y += 2 + lines;
523 quick_widgets[INPUT_INDEX].str_result = &my_str;
525 Quick_input.widgets = quick_widgets;
526 ret = quick_dialog (&Quick_input);
527 g_free (p_text);
529 if (ret != B_CANCEL) {
530 return my_str;
531 } else
532 return 0;
536 * Show input dialog, background safe.
538 * If the arguments "header" and "text" should be translated,
539 * that MUST be done by the caller of these wrappers.
541 char *
542 input_dialog_help (const char *header, const char *text, const char *help,
543 const char *history_name, const char *def_text)
545 union {
546 void *p;
547 char * (*f) (const char *, const char *, const char *,
548 const char *, const char *);
549 } func;
550 #ifdef WITH_BACKGROUND
551 if (we_are_background)
553 func.f = fg_input_dialog_help;
554 return parent_call_string (func.p, 5,
555 strlen (header), header, strlen (text),
556 text, strlen (help), help,
557 strlen (history_name), history_name,
558 strlen (def_text), def_text);
560 else
561 #endif /* WITH_BACKGROUND */
562 return fg_input_dialog_help (header, text, help, history_name, def_text);
565 /* Show input dialog with default help, background safe */
566 char *input_dialog (const char *header, const char *text,
567 const char *history_name, const char *def_text)
569 return input_dialog_help (header, text, "[Input Line Keys]", history_name, def_text);
572 char *
573 input_expand_dialog (const char *header, const char *text,
574 const char *history_name, const char *def_text)
576 char *result;
577 char *expanded;
579 result = input_dialog (header, text, history_name, def_text);
580 if (result) {
581 expanded = tilde_expand (result);
582 g_free (result);
583 return expanded;
585 return result;
588 /* }}} */
590 /* }}} */
592 Cause emacs to enter folding mode for this file:
593 Local variables:
594 end: