Ticket #2115: fixed button location in common input dialog.
[free-mc.git] / src / wtools.c
blobf420b7db4b3896ed8970bb6548eceb53a1224213
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 "lib/global.h"
37 #include "lib/tty/tty.h"
38 #include "lib/tty/key.h" /* tty_getch() */
39 #include "lib/skin.h" /* INPUT_COLOR */
40 #include "lib/strutil.h"
42 #include "dialog.h"
43 #include "widget.h"
44 #include "wtools.h"
45 #include "background.h" /* parent_call */
48 Listbox *
49 create_listbox_window_centered (int center_y, int center_x, int lines, int cols,
50 const char *title, const char *help)
52 const int listbox_colors[DLG_COLOR_NUM] =
54 MENU_ENTRY_COLOR,
55 MENU_SELECTED_COLOR,
56 MENU_HOT_COLOR,
57 MENU_HOTSEL_COLOR,
60 const int space = 4;
62 int xpos, ypos, len;
63 Listbox *listbox;
65 /* Adjust sizes */
66 lines = min (lines, LINES - 6);
68 if (title != NULL) {
69 len = str_term_width1 (title) + 4;
70 cols = max (cols, len);
73 cols = min (cols, COLS - 6);
75 /* adjust position */
76 if ((center_y < 0) || (center_x < 0)) {
77 ypos = LINES/2;
78 xpos = COLS/2;
79 } else {
80 ypos = center_y;
81 xpos = center_x;
84 ypos -= lines/2;
85 xpos -= cols/2;
87 if (ypos + lines >= LINES)
88 ypos = LINES - lines - space;
89 if (ypos < 0)
90 ypos = 0;
92 if (xpos + cols >= COLS)
93 xpos = COLS - cols - space;
94 if (xpos < 0)
95 xpos = 0;
97 listbox = g_new (Listbox, 1);
99 listbox->dlg =
100 create_dlg (TRUE, ypos, xpos, lines + space, cols + space,
101 listbox_colors, NULL,
102 help, title, DLG_REVERSE | DLG_TRYUP);
104 listbox->list = listbox_new (2, 2, lines, cols, FALSE, NULL);
105 add_widget (listbox->dlg, listbox->list);
107 return listbox;
110 Listbox *
111 create_listbox_window (int lines, int cols, const char *title, const char *help)
113 return create_listbox_window_centered (-1, -1, lines, cols, title, help);
116 /* Returns the number of the item selected */
118 run_listbox (Listbox *l)
120 int val = -1;
122 if (run_dlg (l->dlg) != B_CANCEL)
123 val = l->list->pos;
124 destroy_dlg (l->dlg);
125 g_free (l);
126 return val;
129 /* default query callback, used to reposition query */
130 static cb_ret_t
131 default_query_callback (Dlg_head *h, Widget *sender,
132 dlg_msg_t msg, int parm, void *data)
134 switch (msg) {
135 case DLG_RESIZE:
137 int xpos = COLS / 2 - h->cols / 2;
138 int ypos = LINES / 3 - (h->lines - 3) / 2;
140 /* set position */
141 dlg_set_position (h, ypos, xpos, ypos + h->lines, xpos + h->cols);
143 return MSG_HANDLED;
145 default:
146 return default_dlg_callback (h, sender, msg, parm, data);
150 static Dlg_head *last_query_dlg;
152 static int sel_pos = 0;
154 /* Used to ask questions to the user */
156 query_dialog (const char *header, const char *text, int flags, int count, ...)
158 va_list ap;
159 Dlg_head *query_dlg;
160 WButton *button;
161 WButton *defbutton = NULL;
162 int win_len = 0;
163 int i;
164 int result = -1;
165 int cols, lines;
166 char *cur_name;
167 const int *query_colors = (flags & D_ERROR) ?
168 alarm_colors : dialog_colors;
170 if (header == MSG_ERROR)
171 header = _("Error");
173 if (count > 0) {
174 va_start (ap, count);
175 for (i = 0; i < count; i++) {
176 char *cp = va_arg (ap, char *);
177 win_len += str_term_width1 (cp) + 6;
178 if (strchr (cp, '&') != NULL)
179 win_len--;
181 va_end (ap);
184 /* count coordinates */
185 str_msg_term_size (text, &lines, &cols);
186 cols = 6 + max (win_len, max (str_term_width1 (header), cols));
187 lines += 4 + (count > 0 ? 2 : 0);
189 /* prepare dialog */
190 query_dlg =
191 create_dlg (TRUE, 0, 0, lines, cols, query_colors, default_query_callback,
192 "[QueryBox]", header, DLG_NONE);
194 if (count > 0) {
195 cols = (cols - win_len - 2) / 2 + 2;
196 va_start (ap, count);
197 for (i = 0; i < count; i++) {
198 int xpos;
200 cur_name = va_arg (ap, char *);
201 xpos = str_term_width1 (cur_name) + 6;
202 if (strchr (cur_name, '&') != NULL)
203 xpos--;
205 button =
206 button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON,
207 cur_name, 0);
208 add_widget (query_dlg, button);
209 cols += xpos;
210 if (i == sel_pos)
211 defbutton = button;
213 va_end (ap);
215 add_widget (query_dlg, label_new (2, 3, text));
217 /* do resize before running and selecting any widget */
218 default_query_callback (query_dlg, NULL, DLG_RESIZE, 0, NULL);
220 if (defbutton)
221 dlg_select_widget (defbutton);
223 /* run dialog and make result */
224 switch (run_dlg (query_dlg)) {
225 case B_CANCEL:
226 break;
227 default:
228 result = query_dlg->ret_value - B_USER;
231 /* free used memory */
232 destroy_dlg (query_dlg);
233 } else {
234 add_widget (query_dlg, label_new (2, 3, text));
235 add_widget (query_dlg,
236 button_new (0, 0, 0, HIDDEN_BUTTON, "-", 0));
237 last_query_dlg = query_dlg;
239 sel_pos = 0;
240 return result;
243 void query_set_sel (int new_sel)
245 sel_pos = new_sel;
249 /* Create message dialog */
250 static struct Dlg_head *
251 do_create_message (int flags, const char *title, const char *text)
253 char *p;
254 Dlg_head *d;
256 /* Add empty lines before and after the message */
257 p = g_strconcat ("\n", text, "\n", (char *) NULL);
258 query_dialog (title, p, flags, 0);
259 d = last_query_dlg;
261 /* do resize before initing and running */
262 default_query_callback (d, NULL, DLG_RESIZE, 0, NULL);
264 init_dlg (d);
265 g_free (p);
267 return d;
272 * Create message dialog. The caller must call dlg_run_done() and
273 * destroy_dlg() to dismiss it. Not safe to call from background.
275 struct Dlg_head *
276 create_message (int flags, const char *title, const char *text, ...)
278 va_list args;
279 Dlg_head *d;
280 char *p;
282 va_start (args, text);
283 p = g_strdup_vprintf (text, args);
284 va_end (args);
286 d = do_create_message (flags, title, p);
287 g_free (p);
289 return d;
294 * Show message dialog. Dismiss it when any key is pressed.
295 * Not safe to call from background.
297 static void
298 fg_message (int flags, const char *title, const char *text)
300 Dlg_head *d;
302 d = do_create_message (flags, title, text);
303 tty_getch ();
304 dlg_run_done (d);
305 destroy_dlg (d);
309 /* Show message box from background */
310 #ifdef WITH_BACKGROUND
311 static void
312 bg_message (int dummy, int *flags, char *title, const char *text)
314 (void) dummy;
315 title = g_strconcat (_("Background process:"), " ", title, (char *) NULL);
316 fg_message (*flags, title, text);
317 g_free (title);
319 #endif /* WITH_BACKGROUND */
322 /* Show message box, background safe */
323 void
324 message (int flags, const char *title, const char *text, ...)
326 char *p;
327 va_list ap;
328 union {
329 void *p;
330 void (*f) (int, int *, char *, const char *);
331 } func;
333 va_start (ap, text);
334 p = g_strdup_vprintf (text, ap);
335 va_end (ap);
337 if (title == MSG_ERROR)
338 title = _("Error");
340 #ifdef WITH_BACKGROUND
341 if (we_are_background) {
342 func.f = bg_message;
343 parent_call (func.p, NULL, 3, sizeof (flags), &flags,
344 strlen (title), title, strlen (p), p);
345 } else
346 #endif /* WITH_BACKGROUND */
347 fg_message (flags, title, p);
349 g_free (p);
353 /* {{{ Quick dialog routines */
357 quick_dialog_skip (QuickDialog *qd, int nskip)
359 #ifdef ENABLE_NLS
360 #define I18N(x) (x = !qd->i18n && x && *x ? _(x): x)
361 #else
362 #define I18N(x) (x = x)
363 #endif
364 Dlg_head *dd;
365 QuickWidget *qw;
366 WInput *in;
367 WRadio *r;
368 int return_val;
370 const int input_colors[3] =
372 INPUT_COLOR,
373 INPUT_UNCHANGED_COLOR,
374 INPUT_MARK_COLOR
377 I18N (qd->title);
379 if ((qd->xpos == -1) || (qd->ypos == -1))
380 dd = create_dlg (TRUE, 0, 0, qd->ylen, qd->xlen,
381 dialog_colors, NULL, qd->help, qd->title,
382 DLG_CENTER | DLG_TRYUP | DLG_REVERSE);
383 else
384 dd = create_dlg (TRUE, qd->ypos, qd->xpos, qd->ylen, qd->xlen,
385 dialog_colors, NULL, qd->help, qd->title,
386 DLG_REVERSE);
388 for (qw = qd->widgets; qw->widget_type != quick_end; qw++) {
389 int xpos;
390 int ypos;
392 xpos = (qd->xlen * qw->relative_x) / qw->x_divisions;
393 ypos = (qd->ylen * qw->relative_y) / qw->y_divisions;
395 switch (qw->widget_type) {
396 case quick_checkbox:
397 qw->widget = (Widget *) check_new (ypos, xpos, *qw->u.checkbox.state, I18N (qw->u.checkbox.text));
398 break;
400 case quick_button:
401 qw->widget = (Widget *) button_new (ypos, xpos, qw->u.button.action,
402 (qw->u.button.action == B_ENTER) ? DEFPUSH_BUTTON : NORMAL_BUTTON,
403 I18N (qw->u.button.text), qw->u.button.callback);
404 break;
406 case quick_input:
407 in = input_new (ypos, xpos, (int *) input_colors,
408 qw->u.input.len, qw->u.input.text, qw->u.input.histname,
409 INPUT_COMPLETE_DEFAULT);
410 in->is_password = (qw->u.input.flags == 1);
411 if ((qw->u.input.flags & 2) != 0)
412 in->completion_flags |= INPUT_COMPLETE_CD;
413 qw->widget = (Widget *) in;
414 *qw->u.input.result = NULL;
415 break;
417 case quick_label:
418 qw->widget = (Widget *) label_new (ypos, xpos, I18N (qw->u.label.text));
419 break;
421 case quick_groupbox:
422 qw->widget = (Widget *) groupbox_new (ypos, xpos,
423 qw->u.groupbox.height,
424 qw->u.groupbox.width,
425 I18N (qw->u.groupbox.title));
426 break;
428 case quick_radio:
430 int i;
431 char **items = NULL;
433 /* create the copy of radio_items to avoid mwmory leak */
434 items = g_new0 (char *, qw->u.radio.count + 1);
436 if (!qd->i18n)
437 for (i = 0; i < qw->u.radio.count; i++)
438 items[i] = g_strdup (_(qw->u.radio.items[i]));
439 else
440 for (i = 0; i < qw->u.radio.count; i++)
441 items[i] = g_strdup (qw->u.radio.items[i]);
443 r = radio_new (ypos, xpos, qw->u.radio.count, (const char **) items);
444 r->pos = r->sel = *qw->u.radio.value;
445 qw->widget = (Widget *) r;
446 g_strfreev (items);
447 break;
450 default:
451 qw->widget = NULL;
452 fprintf (stderr, "QuickWidget: unknown widget type\n");
453 break;
456 add_widget (dd, qw->widget);
459 while (nskip-- != 0)
460 dd->current = dd->current->next;
462 return_val = run_dlg (dd);
464 /* Get the data if we found something interesting */
465 if (return_val != B_CANCEL) {
466 for (qw = qd->widgets; qw->widget_type != quick_end; qw++) {
467 switch (qw->widget_type) {
468 case quick_checkbox:
469 *qw->u.checkbox.state = ((WCheck *) qw->widget)->state & C_BOOL;
470 break;
472 case quick_input:
473 if ((qw->u.input.flags & 2) != 0)
474 *qw->u.input.result = tilde_expand (((WInput *) qw->widget)->buffer);
475 else
476 *qw->u.input.result = g_strdup (((WInput *) qw->widget)->buffer);
477 break;
479 case quick_radio:
480 *qw->u.radio.value = ((WRadio *) qw->widget)->sel;
481 break;
483 default:
484 break;
489 destroy_dlg (dd);
491 return return_val;
492 #undef I18N
496 quick_dialog (QuickDialog *qd)
498 return quick_dialog_skip (qd, 0);
501 /* }}} */
503 /* {{{ Input routines */
506 * Show dialog, not background safe.
508 * If the arguments "header" and "text" should be translated,
509 * that MUST be done by the caller of fg_input_dialog_help().
511 * The argument "history_name" holds the name of a section
512 * in the history file. Data entered in the input field of
513 * the dialog box will be stored there.
516 static char *
517 fg_input_dialog_help (const char *header, const char *text, const char *help,
518 const char *history_name, const char *def_text)
520 char *my_str;
522 QuickWidget quick_widgets[] = {
523 /* 0 */ QUICK_BUTTON (6, 64, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
524 /* 1 */ QUICK_BUTTON (3, 64, 1, 0, N_("&OK"), B_ENTER, NULL),
525 /* 2 */ QUICK_INPUT (3, 64, 0, 0, def_text, 58, 0, NULL, &my_str),
526 /* 3 */ QUICK_LABEL (3, 64, 2, 0, ""),
527 QUICK_END
530 int b0_len, b1_len, b_len, gap;
531 char histname [64] = "inp|";
532 int lines, cols;
533 int len;
534 int i;
535 char *p_text;
536 int ret;
538 /* buttons */
539 #ifdef ENABLE_NLS
540 quick_widgets[0].u.button.text = _(quick_widgets[0].u.button.text);
541 quick_widgets[1].u.button.text = _(quick_widgets[1].u.button.text);
542 #endif /* ENABLE_NLS */
544 b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
545 b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 5; /* default button */
546 b_len = b0_len + b1_len + 2; /* including gap */
548 /* input line */
549 if (history_name != NULL && *history_name != '\0') {
550 g_strlcpy (histname + 3, history_name, sizeof (histname) - 3);
551 quick_widgets[2].u.input.histname = histname;
554 /* The special value of def_text is used to identify password boxes
555 and hide characters with "*". Don't save passwords in history! */
556 if (def_text == INPUT_PASSWORD) {
557 quick_widgets[2].u.input.flags = 1;
558 histname[3] = '\0';
559 quick_widgets[2].u.input.text = "";
562 /* text */
563 p_text = g_strstrip (g_strdup (text));
564 msglen (p_text, &lines, &cols);
565 quick_widgets[3].u.label.text = p_text;
567 /* dialog width */
568 len = max (max (str_term_width1 (header), cols) + 4, 64);
569 len = min (max (len, b_len + 6), COLS);
571 /* button locations */
572 gap = (len - 8 - b_len)/3;
573 quick_widgets[1].relative_x = 3 + gap;
574 quick_widgets[0].relative_x = quick_widgets[1].relative_x + b1_len + gap + 2;
577 QuickDialog Quick_input =
579 len, lines + 6, -1, -1, header,
580 help, quick_widgets, TRUE
583 for (i = 0; i < 4; i++) {
584 quick_widgets[i].x_divisions = Quick_input.xlen;
585 quick_widgets[i].y_divisions = Quick_input.ylen;
588 for (i = 0; i < 3; i++)
589 quick_widgets[i].relative_y += 2 + lines;
591 /* input line length */
592 quick_widgets[2].u.input.len = Quick_input.xlen - 6;
594 ret = quick_dialog (&Quick_input);
597 g_free (p_text);
599 return (ret != B_CANCEL) ? my_str : NULL;
603 * Show input dialog, background safe.
605 * If the arguments "header" and "text" should be translated,
606 * that MUST be done by the caller of these wrappers.
608 char *
609 input_dialog_help (const char *header, const char *text, const char *help,
610 const char *history_name, const char *def_text)
612 union {
613 void *p;
614 char * (*f) (const char *, const char *, const char *,
615 const char *, const char *);
616 } func;
617 #ifdef WITH_BACKGROUND
618 if (we_are_background)
620 func.f = fg_input_dialog_help;
621 return parent_call_string (func.p, 5,
622 strlen (header), header, strlen (text),
623 text, strlen (help), help,
624 strlen (history_name), history_name,
625 strlen (def_text), def_text);
627 else
628 #endif /* WITH_BACKGROUND */
629 return fg_input_dialog_help (header, text, help, history_name, def_text);
632 /* Show input dialog with default help, background safe */
633 char *input_dialog (const char *header, const char *text,
634 const char *history_name, const char *def_text)
636 return input_dialog_help (header, text, "[Input Line Keys]", history_name, def_text);
639 char *
640 input_expand_dialog (const char *header, const char *text,
641 const char *history_name, const char *def_text)
643 char *result;
644 char *expanded;
646 result = input_dialog (header, text, history_name, def_text);
647 if (result) {
648 expanded = tilde_expand (result);
649 g_free (result);
650 return expanded;
652 return result;
655 /* }}} */
657 /* }}} */
659 Cause emacs to enter folding mode for this file:
660 Local variables:
661 end: