Ticket #2170: Color collisions
[midnight-commander.git] / src / wtools.c
bloba0219c4751c10b4a8c97801ac0d8edf03f8afe36
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 dlg_colors_t listbox_colors = {
53 MENU_ENTRY_COLOR,
54 MENU_SELECTED_COLOR,
55 MENU_HOT_COLOR,
56 MENU_HOTSEL_COLOR,
57 COLOR_TITLE
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)
70 len = str_term_width1 (title) + 4;
71 cols = max (cols, len);
74 cols = min (cols, COLS - 6);
76 /* adjust position */
77 if ((center_y < 0) || (center_x < 0))
79 ypos = LINES / 2;
80 xpos = COLS / 2;
82 else
84 ypos = center_y;
85 xpos = center_x;
88 ypos -= lines / 2;
89 xpos -= cols / 2;
91 if (ypos + lines >= LINES)
92 ypos = LINES - lines - space;
93 if (ypos < 0)
94 ypos = 0;
96 if (xpos + cols >= COLS)
97 xpos = COLS - cols - space;
98 if (xpos < 0)
99 xpos = 0;
101 listbox = g_new (Listbox, 1);
103 listbox->dlg =
104 create_dlg (TRUE, ypos, xpos, lines + space, cols + space,
105 listbox_colors, NULL, help, title, DLG_REVERSE | DLG_TRYUP);
107 listbox->list = listbox_new (2, 2, lines, cols, FALSE, NULL);
108 add_widget (listbox->dlg, listbox->list);
110 return listbox;
113 Listbox *
114 create_listbox_window (int lines, int cols, const char *title, const char *help)
116 return create_listbox_window_centered (-1, -1, lines, cols, title, help);
119 /* Returns the number of the item selected */
121 run_listbox (Listbox * l)
123 int val = -1;
125 if (run_dlg (l->dlg) != B_CANCEL)
126 val = l->list->pos;
127 destroy_dlg (l->dlg);
128 g_free (l);
129 return val;
132 /* default query callback, used to reposition query */
133 static cb_ret_t
134 default_query_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
136 switch (msg)
138 case DLG_RESIZE:
140 int xpos = COLS / 2 - h->cols / 2;
141 int ypos = LINES / 3 - (h->lines - 3) / 2;
143 /* set position */
144 dlg_set_position (h, ypos, xpos, ypos + h->lines, xpos + h->cols);
146 return MSG_HANDLED;
148 default:
149 return default_dlg_callback (h, sender, msg, parm, data);
153 static Dlg_head *last_query_dlg;
155 static int sel_pos = 0;
157 /* Used to ask questions to the user */
159 query_dialog (const char *header, const char *text, int flags, int count, ...)
161 va_list ap;
162 Dlg_head *query_dlg;
163 WButton *button;
164 WButton *defbutton = NULL;
165 int win_len = 0;
166 int i;
167 int result = -1;
168 int cols, lines;
169 char *cur_name;
170 const int *query_colors = (flags & D_ERROR) ? alarm_colors : dialog_colors;
172 if (header == MSG_ERROR)
173 header = _("Error");
175 if (count > 0)
177 va_start (ap, count);
178 for (i = 0; i < count; i++)
180 char *cp = va_arg (ap, char *);
181 win_len += str_term_width1 (cp) + 6;
182 if (strchr (cp, '&') != NULL)
183 win_len--;
185 va_end (ap);
188 /* count coordinates */
189 str_msg_term_size (text, &lines, &cols);
190 cols = 6 + max (win_len, max (str_term_width1 (header), cols));
191 lines += 4 + (count > 0 ? 2 : 0);
193 /* prepare dialog */
194 query_dlg =
195 create_dlg (TRUE, 0, 0, lines, cols, query_colors, default_query_callback,
196 "[QueryBox]", header, DLG_NONE);
198 if (count > 0)
200 cols = (cols - win_len - 2) / 2 + 2;
201 va_start (ap, count);
202 for (i = 0; i < count; i++)
204 int xpos;
206 cur_name = va_arg (ap, char *);
207 xpos = str_term_width1 (cur_name) + 6;
208 if (strchr (cur_name, '&') != NULL)
209 xpos--;
211 button = button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON, cur_name, 0);
212 add_widget (query_dlg, button);
213 cols += xpos;
214 if (i == sel_pos)
215 defbutton = button;
217 va_end (ap);
219 add_widget (query_dlg, label_new (2, 3, text));
221 /* do resize before running and selecting any widget */
222 default_query_callback (query_dlg, NULL, DLG_RESIZE, 0, NULL);
224 if (defbutton)
225 dlg_select_widget (defbutton);
227 /* run dialog and make result */
228 switch (run_dlg (query_dlg))
230 case B_CANCEL:
231 break;
232 default:
233 result = query_dlg->ret_value - B_USER;
236 /* free used memory */
237 destroy_dlg (query_dlg);
239 else
241 add_widget (query_dlg, label_new (2, 3, text));
242 add_widget (query_dlg, button_new (0, 0, 0, HIDDEN_BUTTON, "-", 0));
243 last_query_dlg = query_dlg;
245 sel_pos = 0;
246 return result;
249 void
250 query_set_sel (int new_sel)
252 sel_pos = new_sel;
256 /* Create message dialog */
257 static struct Dlg_head *
258 do_create_message (int flags, const char *title, const char *text)
260 char *p;
261 Dlg_head *d;
263 /* Add empty lines before and after the message */
264 p = g_strconcat ("\n", text, "\n", (char *) NULL);
265 query_dialog (title, p, flags, 0);
266 d = last_query_dlg;
268 /* do resize before initing and running */
269 default_query_callback (d, NULL, DLG_RESIZE, 0, NULL);
271 init_dlg (d);
272 g_free (p);
274 return d;
279 * Create message dialog. The caller must call dlg_run_done() and
280 * destroy_dlg() to dismiss it. Not safe to call from background.
282 struct Dlg_head *
283 create_message (int flags, const char *title, const char *text, ...)
285 va_list args;
286 Dlg_head *d;
287 char *p;
289 va_start (args, text);
290 p = g_strdup_vprintf (text, args);
291 va_end (args);
293 d = do_create_message (flags, title, p);
294 g_free (p);
296 return d;
301 * Show message dialog. Dismiss it when any key is pressed.
302 * Not safe to call from background.
304 static void
305 fg_message (int flags, const char *title, const char *text)
307 Dlg_head *d;
309 d = do_create_message (flags, title, text);
310 tty_getch ();
311 dlg_run_done (d);
312 destroy_dlg (d);
316 /* Show message box from background */
317 #ifdef WITH_BACKGROUND
318 static void
319 bg_message (int dummy, int *flags, char *title, const char *text)
321 (void) dummy;
322 title = g_strconcat (_("Background process:"), " ", title, (char *) NULL);
323 fg_message (*flags, title, text);
324 g_free (title);
326 #endif /* WITH_BACKGROUND */
329 /* Show message box, background safe */
330 void
331 message (int flags, const char *title, const char *text, ...)
333 char *p;
334 va_list ap;
335 union
337 void *p;
338 void (*f) (int, int *, char *, const char *);
339 } func;
341 va_start (ap, text);
342 p = g_strdup_vprintf (text, ap);
343 va_end (ap);
345 if (title == MSG_ERROR)
346 title = _("Error");
348 #ifdef WITH_BACKGROUND
349 if (we_are_background)
351 func.f = bg_message;
352 parent_call (func.p, NULL, 3, sizeof (flags), &flags, strlen (title), title, strlen (p), p);
354 else
355 #endif /* WITH_BACKGROUND */
356 fg_message (flags, title, p);
358 g_free (p);
362 /* {{{ Quick dialog routines */
366 quick_dialog_skip (QuickDialog * qd, int nskip)
368 #ifdef ENABLE_NLS
369 #define I18N(x) (x = !qd->i18n && x && *x ? _(x): x)
370 #else
371 #define I18N(x) (x = x)
372 #endif
373 Dlg_head *dd;
374 QuickWidget *qw;
375 WInput *in;
376 WRadio *r;
377 int return_val;
379 I18N (qd->title);
381 if ((qd->xpos == -1) || (qd->ypos == -1))
382 dd = create_dlg (TRUE, 0, 0, qd->ylen, qd->xlen,
383 dialog_colors, qd->callback, qd->help, qd->title,
384 DLG_CENTER | DLG_TRYUP | DLG_REVERSE);
385 else
386 dd = create_dlg (TRUE, qd->ypos, qd->xpos, qd->ylen, qd->xlen,
387 dialog_colors, qd->callback, qd->help, qd->title, DLG_REVERSE);
389 for (qw = qd->widgets; qw->widget_type != quick_end; qw++)
391 int xpos;
392 int ypos;
394 xpos = (qd->xlen * qw->relative_x) / qw->x_divisions;
395 ypos = (qd->ylen * qw->relative_y) / qw->y_divisions;
397 switch (qw->widget_type)
399 case quick_checkbox:
400 qw->widget =
401 (Widget *) check_new (ypos, xpos, *qw->u.checkbox.state,
402 I18N (qw->u.checkbox.text));
403 break;
405 case quick_button:
406 qw->widget = (Widget *) button_new (ypos, xpos, qw->u.button.action,
407 (qw->u.button.action ==
408 B_ENTER) ? DEFPUSH_BUTTON : NORMAL_BUTTON,
409 I18N (qw->u.button.text), qw->u.button.callback);
410 break;
412 case quick_input:
413 in = input_new (ypos, xpos, input_get_default_colors(),
414 qw->u.input.len, qw->u.input.text, qw->u.input.histname,
415 INPUT_COMPLETE_DEFAULT);
416 in->is_password = (qw->u.input.flags == 1);
417 if ((qw->u.input.flags & 2) != 0)
418 in->completion_flags |= INPUT_COMPLETE_CD;
419 qw->widget = (Widget *) in;
420 *qw->u.input.result = NULL;
421 break;
423 case quick_label:
424 qw->widget = (Widget *) label_new (ypos, xpos, I18N (qw->u.label.text));
425 break;
427 case quick_groupbox:
428 qw->widget = (Widget *) groupbox_new (ypos, xpos,
429 qw->u.groupbox.height,
430 qw->u.groupbox.width,
431 I18N (qw->u.groupbox.title));
432 break;
434 case quick_radio:
436 int i;
437 char **items = NULL;
439 /* create the copy of radio_items to avoid mwmory leak */
440 items = g_new0 (char *, qw->u.radio.count + 1);
442 if (!qd->i18n)
443 for (i = 0; i < qw->u.radio.count; i++)
444 items[i] = g_strdup (_(qw->u.radio.items[i]));
445 else
446 for (i = 0; i < qw->u.radio.count; i++)
447 items[i] = g_strdup (qw->u.radio.items[i]);
449 r = radio_new (ypos, xpos, qw->u.radio.count, (const char **) items);
450 r->pos = r->sel = *qw->u.radio.value;
451 qw->widget = (Widget *) r;
452 g_strfreev (items);
453 break;
456 default:
457 qw->widget = NULL;
458 fprintf (stderr, "QuickWidget: unknown widget type\n");
459 break;
462 if (qw->widget != NULL)
464 qw->widget->options |= qw->options; /* FIXME: cannot reset flags, setup only */
465 add_widget (dd, qw->widget);
469 while (nskip-- != 0)
471 dd->current = g_list_next (dd->current);
472 if (dd->current == NULL)
473 dd->current = dd->widgets;
476 return_val = run_dlg (dd);
478 /* Get the data if we found something interesting */
479 if (return_val != B_CANCEL)
481 for (qw = qd->widgets; qw->widget_type != quick_end; qw++)
483 switch (qw->widget_type)
485 case quick_checkbox:
486 *qw->u.checkbox.state = ((WCheck *) qw->widget)->state & C_BOOL;
487 break;
489 case quick_input:
490 if ((qw->u.input.flags & 2) != 0)
491 *qw->u.input.result = tilde_expand (((WInput *) qw->widget)->buffer);
492 else
493 *qw->u.input.result = g_strdup (((WInput *) qw->widget)->buffer);
494 break;
496 case quick_radio:
497 *qw->u.radio.value = ((WRadio *) qw->widget)->sel;
498 break;
500 default:
501 break;
506 destroy_dlg (dd);
508 return return_val;
509 #undef I18N
513 quick_dialog (QuickDialog * qd)
515 return quick_dialog_skip (qd, 0);
518 /* }}} */
520 /* {{{ Input routines */
523 * Show dialog, not background safe.
525 * If the arguments "header" and "text" should be translated,
526 * that MUST be done by the caller of fg_input_dialog_help().
528 * The argument "history_name" holds the name of a section
529 * in the history file. Data entered in the input field of
530 * the dialog box will be stored there.
533 static char *
534 fg_input_dialog_help (const char *header, const char *text, const char *help,
535 const char *history_name, const char *def_text)
537 char *my_str;
539 QuickWidget quick_widgets[] = {
540 /* 0 */ QUICK_BUTTON (6, 64, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
541 /* 1 */ QUICK_BUTTON (3, 64, 1, 0, N_("&OK"), B_ENTER, NULL),
542 /* 2 */ QUICK_INPUT (3, 64, 0, 0, def_text, 58, 0, NULL, &my_str),
543 /* 3 */ QUICK_LABEL (3, 64, 2, 0, ""),
544 QUICK_END
547 int b0_len, b1_len, b_len, gap;
548 char histname[64] = "inp|";
549 int lines, cols;
550 int len;
551 int i;
552 char *p_text;
553 int ret;
555 /* buttons */
556 #ifdef ENABLE_NLS
557 quick_widgets[0].u.button.text = _(quick_widgets[0].u.button.text);
558 quick_widgets[1].u.button.text = _(quick_widgets[1].u.button.text);
559 #endif /* ENABLE_NLS */
561 b0_len = str_term_width1 (quick_widgets[0].u.button.text) + 3;
562 b1_len = str_term_width1 (quick_widgets[1].u.button.text) + 5; /* default button */
563 b_len = b0_len + b1_len + 2; /* including gap */
565 /* input line */
566 if (history_name != NULL && *history_name != '\0')
568 g_strlcpy (histname + 3, history_name, sizeof (histname) - 3);
569 quick_widgets[2].u.input.histname = histname;
572 /* The special value of def_text is used to identify password boxes
573 and hide characters with "*". Don't save passwords in history! */
574 if (def_text == INPUT_PASSWORD)
576 quick_widgets[2].u.input.flags = 1;
577 histname[3] = '\0';
578 quick_widgets[2].u.input.text = "";
581 /* text */
582 p_text = g_strstrip (g_strdup (text));
583 msglen (p_text, &lines, &cols);
584 quick_widgets[3].u.label.text = p_text;
586 /* dialog width */
587 len = max (max (str_term_width1 (header), cols) + 4, 64);
588 len = min (max (len, b_len + 6), COLS);
590 /* button locations */
591 gap = (len - 8 - b_len) / 3;
592 quick_widgets[1].relative_x = 3 + gap;
593 quick_widgets[0].relative_x = quick_widgets[1].relative_x + b1_len + gap + 2;
596 QuickDialog Quick_input = {
597 len, lines + 6, -1, -1, header,
598 help, quick_widgets, NULL, TRUE
601 for (i = 0; i < 4; i++)
603 quick_widgets[i].x_divisions = Quick_input.xlen;
604 quick_widgets[i].y_divisions = Quick_input.ylen;
607 for (i = 0; i < 3; i++)
608 quick_widgets[i].relative_y += 2 + lines;
610 /* input line length */
611 quick_widgets[2].u.input.len = Quick_input.xlen - 6;
613 ret = quick_dialog (&Quick_input);
616 g_free (p_text);
618 return (ret != B_CANCEL) ? my_str : NULL;
622 * Show input dialog, background safe.
624 * If the arguments "header" and "text" should be translated,
625 * that MUST be done by the caller of these wrappers.
627 char *
628 input_dialog_help (const char *header, const char *text, const char *help,
629 const char *history_name, const char *def_text)
631 union
633 void *p;
634 char *(*f) (const char *, const char *, const char *, const char *, const char *);
635 } func;
636 #ifdef WITH_BACKGROUND
637 if (we_are_background)
639 func.f = fg_input_dialog_help;
640 return parent_call_string (func.p, 5,
641 strlen (header), header, strlen (text),
642 text, strlen (help), help,
643 strlen (history_name), history_name,
644 strlen (def_text), def_text);
646 else
647 #endif /* WITH_BACKGROUND */
648 return fg_input_dialog_help (header, text, help, history_name, def_text);
651 /* Show input dialog with default help, background safe */
652 char *
653 input_dialog (const char *header, const char *text, const char *history_name, const char *def_text)
655 return input_dialog_help (header, text, "[Input Line Keys]", history_name, def_text);
658 char *
659 input_expand_dialog (const char *header, const char *text,
660 const char *history_name, const char *def_text)
662 char *result;
663 char *expanded;
665 result = input_dialog (header, text, history_name, def_text);
666 if (result)
668 expanded = tilde_expand (result);
669 g_free (result);
670 return expanded;
672 return result;
675 /* }}} */
677 /* }}} */
679 Cause emacs to enter folding mode for this file:
680 Local variables:
681 end: