Merge branch '2087_standalone_mcedit_crash'
[kaloumi3.git] / src / wtools.c
blob6bc6e389829dd485fbcdbdbc7e69b6523d96df79
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 (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 (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 I18N (qd->title);
372 if ((qd->xpos == -1) || (qd->ypos == -1))
373 dd = create_dlg (0, 0, qd->ylen, qd->xlen,
374 dialog_colors, NULL, qd->help, qd->title,
375 DLG_CENTER | DLG_TRYUP | DLG_REVERSE);
376 else
377 dd = create_dlg (qd->ypos, qd->xpos, qd->ylen, qd->xlen,
378 dialog_colors, NULL, qd->help, qd->title,
379 DLG_REVERSE);
381 for (qw = qd->widgets; qw->widget_type != quick_end; qw++) {
382 int xpos;
383 int ypos;
385 xpos = (qd->xlen * qw->relative_x) / qw->x_divisions;
386 ypos = (qd->ylen * qw->relative_y) / qw->y_divisions;
388 switch (qw->widget_type) {
389 case quick_checkbox:
390 qw->widget = (Widget *) check_new (ypos, xpos, *qw->u.checkbox.state, I18N (qw->u.checkbox.text));
391 break;
393 case quick_button:
394 qw->widget = (Widget *) button_new (ypos, xpos, qw->u.button.action,
395 (qw->u.button.action == B_ENTER) ? DEFPUSH_BUTTON : NORMAL_BUTTON,
396 I18N (qw->u.button.text), qw->u.button.callback);
397 break;
399 case quick_input:
400 in = input_new (ypos, xpos, INPUT_COLOR, qw->u.input.len,
401 qw->u.input.text, qw->u.input.histname, INPUT_COMPLETE_DEFAULT);
402 in->is_password = (qw->u.input.flags == 1);
403 in->point = 0;
404 if ((qw->u.input.flags & 2) != 0)
405 in->completion_flags |= INPUT_COMPLETE_CD;
406 qw->widget = (Widget *) in;
407 *qw->u.input.result = NULL;
408 break;
410 case quick_label:
411 qw->widget = (Widget *) label_new (ypos, xpos, I18N (qw->u.label.text));
412 break;
414 case quick_radio:
416 int i;
417 char **items = NULL;
419 /* create the copy of radio_items to avoid mwmory leak */
420 items = g_new0 (char *, qw->u.radio.count + 1);
422 if (!qd->i18n)
423 for (i = 0; i < qw->u.radio.count; i++)
424 items[i] = g_strdup (_(qw->u.radio.items[i]));
425 else
426 for (i = 0; i < qw->u.radio.count; i++)
427 items[i] = g_strdup (qw->u.radio.items[i]);
429 r = radio_new (ypos, xpos, qw->u.radio.count, (const char **) items);
430 r->pos = r->sel = *qw->u.radio.value;
431 qw->widget = (Widget *) r;
432 g_strfreev (items);
433 break;
436 default:
437 qw->widget = NULL;
438 fprintf (stderr, "QuickWidget: unknown widget type\n");
439 break;
442 add_widget (dd, qw->widget);
445 while (nskip-- != 0)
446 dd->current = dd->current->next;
448 return_val = run_dlg (dd);
450 /* Get the data if we found something interesting */
451 if (return_val != B_CANCEL) {
452 for (qw = qd->widgets; qw->widget_type != quick_end; qw++) {
453 switch (qw->widget_type) {
454 case quick_checkbox:
455 *qw->u.checkbox.state = ((WCheck *) qw->widget)->state & C_BOOL;
456 break;
458 case quick_input:
459 if ((qw->u.input.flags & 2) != 0)
460 *qw->u.input.result = tilde_expand (((WInput *) qw->widget)->buffer);
461 else
462 *qw->u.input.result = g_strdup (((WInput *) qw->widget)->buffer);
463 break;
465 case quick_radio:
466 *qw->u.radio.value = ((WRadio *) qw->widget)->sel;
467 break;
469 default:
470 break;
475 destroy_dlg (dd);
477 return return_val;
478 #undef I18N
482 quick_dialog (QuickDialog *qd)
484 return quick_dialog_skip (qd, 0);
487 /* }}} */
489 /* {{{ Input routines */
492 * Show dialog, not background safe.
494 * If the arguments "header" and "text" should be translated,
495 * that MUST be done by the caller of fg_input_dialog_help().
497 * The argument "history_name" holds the name of a section
498 * in the history file. Data entered in the input field of
499 * the dialog box will be stored there.
502 static char *
503 fg_input_dialog_help (const char *header, const char *text, const char *help,
504 const char *history_name, const char *def_text)
506 char *my_str;
508 QuickWidget quick_widgets[] = {
509 /* 0 */ QUICK_BUTTON (6, 10, 1, 0, N_("&Cancel"), B_CANCEL, NULL),
510 /* 1 */ QUICK_BUTTON (3, 10, 1, 0, N_("&OK"), B_ENTER, NULL),
511 /* 2 */ QUICK_INPUT (3, 80, 0, 0, def_text, 58, 0, NULL, &my_str),
512 /* 3 */ QUICK_LABEL (3, 80, 2, 0, ""),
513 QUICK_END
516 char histname [64] = "inp|";
517 int lines, cols;
518 int len;
519 int i;
520 char *p_text;
521 int ret;
523 if (history_name != NULL && *history_name != '\0') {
524 g_strlcpy (histname + 3, history_name, sizeof (histname) - 3);
525 quick_widgets[2].u.input.histname = histname;
528 msglen (text, &lines, &cols);
529 len = max (max (str_term_width1 (header), cols) + 4, 64);
531 /* The special value of def_text is used to identify password boxes
532 and hide characters with "*". Don't save passwords in history! */
533 if (def_text == INPUT_PASSWORD) {
534 quick_widgets[2].u.input.flags = 1;
535 histname[3] = '\0';
536 quick_widgets[2].u.input.text = "";
539 #ifdef ENABLE_NLS
541 * An attempt to place buttons symmetrically, based on actual i18n
542 * length of the string. It looks nicer with i18n (IMO) - alex
544 quick_widgets[0].u.button.text = _(quick_widgets[0].u.button.text);
545 quick_widgets[1].u.button.text = _(quick_widgets[1].u.button.text);
546 quick_widgets[0].relative_x = len / 2 + 4;
547 quick_widgets[1].relative_x =
548 len / 2 - (str_term_width1 (quick_widgets[1].u.button.text) + 9);
549 quick_widgets[0].x_divisions = quick_widgets[1].x_divisions = len;
550 #endif /* ENABLE_NLS */
552 p_text = g_strstrip (g_strdup (text));
553 quick_widgets[3].u.label.text = p_text;
556 QuickDialog Quick_input =
558 len, lines + 6, -1, -1, header,
559 help, quick_widgets, TRUE
562 for (i = 0; i < 4; i++) {
563 quick_widgets[i].x_divisions = Quick_input.xlen;
564 quick_widgets[i].y_divisions = Quick_input.ylen;
567 for (i = 0; i < 3; i++)
568 quick_widgets[i].relative_y += 2 + lines;
570 /* input line length */
571 quick_widgets[2].u.input.len = Quick_input.xlen - 6;
573 ret = quick_dialog (&Quick_input);
576 g_free (p_text);
578 return (ret != B_CANCEL) ? my_str : NULL;
582 * Show input dialog, background safe.
584 * If the arguments "header" and "text" should be translated,
585 * that MUST be done by the caller of these wrappers.
587 char *
588 input_dialog_help (const char *header, const char *text, const char *help,
589 const char *history_name, const char *def_text)
591 union {
592 void *p;
593 char * (*f) (const char *, const char *, const char *,
594 const char *, const char *);
595 } func;
596 #ifdef WITH_BACKGROUND
597 if (we_are_background)
599 func.f = fg_input_dialog_help;
600 return parent_call_string (func.p, 5,
601 strlen (header), header, strlen (text),
602 text, strlen (help), help,
603 strlen (history_name), history_name,
604 strlen (def_text), def_text);
606 else
607 #endif /* WITH_BACKGROUND */
608 return fg_input_dialog_help (header, text, help, history_name, def_text);
611 /* Show input dialog with default help, background safe */
612 char *input_dialog (const char *header, const char *text,
613 const char *history_name, const char *def_text)
615 return input_dialog_help (header, text, "[Input Line Keys]", history_name, def_text);
618 char *
619 input_expand_dialog (const char *header, const char *text,
620 const char *history_name, const char *def_text)
622 char *result;
623 char *expanded;
625 result = input_dialog (header, text, history_name, def_text);
626 if (result) {
627 expanded = tilde_expand (result);
628 g_free (result);
629 return expanded;
631 return result;
634 /* }}} */
636 /* }}} */
638 Cause emacs to enter folding mode for this file:
639 Local variables:
640 end: