1999-09-09 Federico Mena Quintero <federico@redhat.com>
[midnight-commander.git] / src / wtools.c
blobe726c68434e6a6051ad92f3d74203e879d9ebfdc
1 /* {{{ */
3 /* {{{ Copyright Notice */
5 /* Widget based utility functions.
6 Copyright (C) 1994, 1995 the Free Software Foundation
8 Authors: 1994, 1995, 1996 Miguel de Icaza
9 1994, 1995 Radek Doulik
10 1995 Jakub Jelinek
11 1995 Andrej Borsenkow
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 /* }}} */
31 /* [] = "$Id$" */
33 #include <config.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include "tty.h"
37 #include <stdarg.h>
38 #include "global.h"
39 #include "win.h"
40 #include "color.h"
41 #include "mouse.h"
42 #include "dlg.h"
43 #include "widget.h"
44 #include "menu.h"
45 #include "wtools.h"
46 #include "key.h" /* For mi_getch() */
47 #include "dialog.h" /* For do_refresh() */
48 #include "complete.h" /* INPUT_COMPLETE_CD */
49 #include "x.h"
51 /* }}} */
53 /* {{{ Common dialog callback */
54 #ifdef HAVE_X
55 void dialog_repaint (struct Dlg_head *h, int back, int title_fore)
58 #else
59 void
60 dialog_repaint (struct Dlg_head *h, int back, int title_fore)
62 attrset (back);
63 dlg_erase (h);
64 draw_box (h, 1, 1, h->lines - 2, h->cols - 2);
65 attrset (title_fore);
66 if (h->title){
67 dlg_move (h, 1, (h->cols-strlen (h->title))/2);
68 addstr (h->title);
71 #endif
72 void
73 common_dialog_repaint (struct Dlg_head *h)
75 dialog_repaint (h, COLOR_NORMAL, COLOR_HOT_NORMAL);
78 int
79 common_dialog_callback (struct Dlg_head *h, int id, int msg)
81 if (msg == DLG_DRAW)
82 common_dialog_repaint (h);
83 return 0;
86 /* }}} */
87 /* {{{ Listbox utility functions */
89 static void
90 listbox_refresh (Dlg_head *h)
92 dialog_repaint (h, COLOR_NORMAL, COLOR_HOT_NORMAL);
95 static int listbox_callback (Dlg_head *h, int id, int msg)
97 switch (msg) {
98 case DLG_DRAW:
99 #ifndef HAVE_X
100 listbox_refresh(h);
101 #endif
102 return 1;
104 return 0;
107 Listbox *create_listbox_window (int cols, int lines, char *title, char *help)
109 int xpos, ypos, len;
110 Listbox *listbox = g_new (Listbox, 1);
111 char* cancel_string = _("&Cancel");
113 /* Adjust sizes */
114 lines = (lines > LINES-6) ? LINES - 6 : lines;
116 if (title && (cols < (len = strlen(title) + 2)))
117 cols = len;
119 /* no &, but 4 spaces around button for brackets and such */
120 if (cols < (len = strlen(cancel_string) + 3))
121 cols = len;
123 cols = cols > COLS-6 ? COLS-6 : cols;
125 /* I'm not sure if this -2 is safe, should test it */
126 xpos = (COLS-cols)/2;
127 ypos = (LINES-lines)/2 - 2;
129 /* Create components */
130 listbox->dlg = create_dlg (ypos, xpos, lines+6, cols+4, dialog_colors,
131 listbox_callback, help, "listbox", DLG_CENTER|DLG_GRID);
132 x_set_dialog_title (listbox->dlg, title);
134 listbox->list = listbox_new (2, 2, cols, lines, listbox_finish, 0, "li");
136 add_widget (listbox->dlg,
137 button_new (lines+3, (cols/2 + 2) - len/2,
138 B_CANCEL, NORMAL_BUTTON, cancel_string, 0, 0, "c"));
139 add_widget (listbox->dlg, listbox->list);
140 #ifndef HAVE_X
141 listbox_refresh(listbox->dlg);
142 #endif /* !HAVE_X */
143 return listbox;
146 /* Returns the number of the item selected */
147 int run_listbox (Listbox *l)
149 int val;
151 run_dlg (l->dlg);
152 if (l->dlg->ret_value == B_CANCEL)
153 val = -1;
154 else
155 val = l->list->pos;
156 destroy_dlg (l->dlg);
157 g_free (l);
158 return val;
161 /* }}} */
164 /* {{{ Query Dialog functions */
165 #ifndef HAVE_GNOME
166 struct text_struct {
167 char *text;
168 char *header;
171 static int query_callback (struct Dlg_head *h, int Id, int Msg)
173 struct text_struct *info;
175 info = (struct text_struct *) h->data;
177 switch (Msg){
178 #ifndef HAVE_X
179 case DLG_DRAW:
180 /* designate window */
181 attrset (NORMALC);
182 dlg_erase (h);
183 draw_box (h, 1, 1, h->lines-2, h->cols-2);
184 attrset (HOT_NORMALC);
185 dlg_move (h, 1, (h->cols-strlen (info->header))/2);
186 addstr (info->header);
187 break;
188 #endif
190 return 0;
194 Dlg_head *last_query_dlg;
196 static int sel_pos = 0;
198 /* Used to ask questions to the user */
199 int query_dialog (char *header, char *text, int flags, int count, ...)
201 va_list ap;
202 Dlg_head *query_dlg;
203 int win_len = 0;
204 int i;
205 int result = -1;
206 int xpos, ypos;
207 int cols, lines;
208 char *cur_name;
209 static int query_colors [4];
210 static struct text_struct pass;
211 #ifdef HAVE_X
212 static char *buttonnames [10];
213 #endif
215 /* set dialog colors */
216 query_colors [0] = (flags & D_ERROR) ? ERROR_COLOR : Q_UNSELECTED_COLOR;
217 query_colors [1] = (flags & D_ERROR) ? REVERSE_COLOR : Q_SELECTED_COLOR;
218 query_colors [2] = (flags & D_ERROR) ? ERROR_COLOR : COLOR_HOT_NORMAL;
219 query_colors [3] = (flags & D_ERROR) ? COLOR_HOT_NORMAL : COLOR_HOT_FOCUS;
221 if (header == MSG_ERROR)
222 header = _(" Error ");
224 if (count > 0){
225 va_start (ap, count);
226 for (i = 0; i < count; i++)
228 char* cp = va_arg (ap, char *);
229 win_len += strlen (cp) + 6;
230 if (strchr (cp, '&') != NULL)
231 win_len--;
233 va_end (ap);
236 /* count coordinates */
237 cols = 6 + max (win_len, max (strlen (header), msglen (text, &lines)));
238 lines += 4 + (count > 0 ? 2 : 0);
239 xpos = COLS/2 - cols/2;
240 ypos = LINES/3 - (lines-3)/2;
241 pass.header = header;
242 pass.text = text;
244 /* prepare dialog */
245 query_dlg = create_dlg (ypos, xpos, lines, cols, query_colors,
246 query_callback, "[QueryBox]", "query", DLG_NONE);
247 x_set_dialog_title (query_dlg, header);
249 /* The data we need to pass to the callback */
250 query_dlg->cols = cols;
251 query_dlg->lines = lines;
252 query_dlg->data = &pass;
254 query_dlg->direction = DIR_BACKWARD;
256 if (count > 0){
258 cols = (cols-win_len-2)/2+2;
259 va_start (ap, count);
260 for (i = 0; i < count; i++){
261 cur_name = va_arg (ap, char *);
262 xpos = strlen (cur_name)+6;
263 if (strchr(cur_name, '&') != NULL)
264 xpos--;
265 add_widget (query_dlg, button_new
266 (lines-3, cols, B_USER+i, NORMAL_BUTTON, cur_name,
267 0, 0, NULL));
268 cols += xpos;
269 if (i == sel_pos)
270 query_dlg->initfocus = query_dlg->current;
272 va_end (ap);
274 add_widget (query_dlg, label_new (2, 3, text, NULL));
276 /* run dialog and make result */
277 run_dlg (query_dlg);
278 switch (query_dlg->ret_value){
279 case B_CANCEL:
280 break;
281 default:
282 result = query_dlg->ret_value-B_USER;
285 /* free used memory */
286 destroy_dlg (query_dlg);
287 } else {
288 #ifdef HAVE_X
289 add_widgetl (query_dlg, button_new(0, 0, B_EXIT, NORMAL_BUTTON, _("&Ok"), 0, 0, NULL),
290 XV_WLAY_CENTERROW);
292 add_widget (query_dlg, label_new (2, 3, text, NULL));
293 #ifdef HAVE_TK
294 if (flags & D_INSERT){
295 } else
296 #endif
298 run_dlg (query_dlg);
299 destroy_dlg (query_dlg);
301 #else
302 add_widget (query_dlg, label_new (2, 3, text, NULL));
303 add_widget (query_dlg, button_new(0, 0, 0, HIDDEN_BUTTON, "-", 0, 0, NULL));
304 #endif /* HAVE_X */
305 last_query_dlg = query_dlg;
307 sel_pos = 0;
308 return result;
311 void query_set_sel (int new_sel)
313 sel_pos = new_sel;
316 /* }}} */
318 /* {{{ The message function */
320 /* To show nice messages to the users */
321 Dlg_head *message (int error, char *header, char *text, ...)
323 va_list args;
324 char buffer [4096];
325 Dlg_head *d;
327 /* Setup the display information */
328 strcpy (buffer, "\n");
329 va_start (args, text);
330 g_vsnprintf (&buffer [1], sizeof (buffer), text, args);
331 strcat (buffer, "\n");
332 va_end (args);
334 query_dialog (header, buffer, error, 0);
336 d = last_query_dlg;
337 init_dlg (d);
338 if (!(error & D_INSERT)){
339 mi_getch ();
340 dlg_run_done (d);
341 destroy_dlg (d);
342 } else
343 return d;
344 return 0;
346 #endif
347 /* }}} */
349 /* {{{ The chooser routines */
351 static int remove_callback (int i, void *data)
353 Chooser *c = (Chooser *) data;
355 listbox_remove_current (c->listbox, 0);
357 dlg_select_widget (c->dialog, c->listbox);
358 dlg_select_nth_widget (c->dialog, 0);
360 /* Return: do not abort dialog */
361 return 0;
364 Chooser *new_chooser (int lines, int cols, char *help, int flags)
366 Chooser *c;
367 int button_lines;
369 c =g_new (Chooser, 1);
370 c->dialog = create_dlg (0, 0, lines, cols, dialog_colors, common_dialog_callback,
371 help, "chooser", DLG_CENTER | DLG_GRID);
373 c->dialog->lines = lines;
374 c->dialog->cols = cols;
376 button_lines = flags & CHOOSE_EDITABLE ? 3 : 0;
378 c->listbox = listbox_new (1, 1, cols-2, lines-button_lines,
379 listbox_finish, 0, "listbox");
381 if (button_lines){
382 add_widget (c->dialog, button_new (lines-button_lines+1,
383 20, B_ENTER, DEFPUSH_BUTTON, _("&Remove"),
384 remove_callback, c, "button-remove"));
385 add_widget (c->dialog, button_new (lines-button_lines+1,
386 4, B_CANCEL, NORMAL_BUTTON, _("&Cancel"),
387 0, 0, "button-cancel"));
389 add_widget (c->dialog, c->listbox);
390 return c;
393 int run_chooser (Chooser *c)
395 run_dlg (c->dialog);
396 return c->dialog->ret_value;
399 void destroy_chooser (Chooser *c)
401 destroy_dlg (c->dialog);
404 /* }}} */
406 /* {{{ Quick dialog routines */
408 static int quick_callback (struct Dlg_head *h, int id, int Msg)
410 switch (Msg){
411 #ifndef HAVE_X
412 case DLG_DRAW:
413 attrset (COLOR_NORMAL);
414 dlg_erase (h);
415 draw_box (h, 1, 1, h->lines-2, h->cols-2);
417 attrset (COLOR_HOT_NORMAL);
418 dlg_move (h, 1,((h->cols-strlen (h->data))/2));
419 addstr (h->data);
420 break;
421 #endif
422 case DLG_KEY:
423 if (id == '\n'){
424 h->ret_value = B_ENTER;
425 dlg_stop (h);
426 break;
429 return 0;
432 #define I18N(x) (do_int && *x ? (x = _(x)): x)
434 int quick_dialog_skip (QuickDialog *qd, int nskip)
436 Dlg_head *dd;
437 void *widget;
438 WRadio *r;
439 int xpos;
440 int ypos;
441 int return_val;
442 WInput *input;
443 QuickWidget *qw;
444 int do_int;
446 if (!qd->i18n){
447 qd->i18n = 1;
448 do_int = 1;
449 if (*qd->title)
450 qd->title = _(qd->title);
451 } else
452 do_int = 0;
454 if (qd->xpos == -1)
455 dd = create_dlg (0, 0, qd->ylen, qd->xlen, dialog_colors, quick_callback,
456 qd->help, qd->class, DLG_CENTER | DLG_TRYUP | DLG_GRID);
457 else
458 dd = create_dlg (qd->ypos, qd->xpos, qd->ylen, qd->xlen, dialog_colors,
459 quick_callback,
460 qd->help, qd->class, DLG_GRID);
462 x_set_dialog_title (dd, qd->title);
464 /* We pass this to the callback */
465 dd->cols = qd->xlen;
466 dd->lines = qd->ylen;
467 dd->data = qd->title;
469 for (qw = qd->widgets; qw->widget_type; qw++){
470 #ifdef HAVE_X
471 xpos = ypos = 0;
472 #else
473 xpos = (qd->xlen * qw->relative_x)/qw->x_divisions;
474 ypos = (qd->ylen * qw->relative_y)/qw->y_divisions;
475 #endif
477 switch (qw->widget_type){
478 case quick_checkbox:
479 widget = check_new (ypos, xpos, *qw->result, I18N (qw->text), qw->tkname);
480 break;
482 case quick_radio:
483 r = radio_new (ypos, xpos, qw->hotkey_pos, qw->str_result, 1, qw->tkname);
484 r->pos = r->sel = qw->value;
485 widget = r;
486 break;
488 case quick_button:
489 widget = button_new (ypos, xpos, qw->value, (qw->value==B_ENTER) ? DEFPUSH_BUTTON : NORMAL_BUTTON,
490 I18N (qw->text), 0, 0, qw->tkname);
491 break;
493 /* We use the hotkey pos as the field length */
494 case quick_input:
495 input = input_new (ypos, xpos, INPUT_COLOR,
496 qw->hotkey_pos, qw->text, qw->tkname);
497 input->is_password = qw->value == 1;
498 input->point = 0;
499 if (qw->value & 2)
500 input->completion_flags |= INPUT_COMPLETE_CD;
501 widget = input;
502 break;
504 case quick_label:
505 widget = label_new (ypos, xpos, I18N(qw->text), qw->tkname);
506 break;
508 default:
509 widget = 0;
510 fprintf (stderr, "QuickWidget: unknown widget type\n");
511 break;
513 qw->the_widget = widget;
514 add_widgetl (dd, widget, qw->layout);
517 while (nskip--)
518 dd->current = dd->current->next;
520 run_dlg (dd);
522 /* Get the data if we found something interesting */
523 if (dd->ret_value != B_CANCEL){
524 for (qw = qd->widgets; qw->widget_type; qw++){
525 switch (qw->widget_type){
526 case quick_checkbox:
527 *qw->result = ((WCheck *) qw->the_widget)->state & C_BOOL;
528 break;
530 case quick_radio:
531 *qw->result = ((WRadio *) qw->the_widget)->sel;
532 break;
534 case quick_input:
535 *qw->str_result = g_strdup (((WInput *) qw->the_widget)->buffer);
536 break;
540 return_val = dd->ret_value;
541 destroy_dlg (dd);
543 return return_val;
546 int quick_dialog (QuickDialog *qd)
548 return quick_dialog_skip (qd, 0);
551 /* }}} */
553 /* {{{ Input routines */
555 #ifndef HAVE_GNOME
556 #define INPUT_INDEX 2
557 char *real_input_dialog_help (char *header, char *text, char *help, char *def_text)
559 QuickDialog Quick_input;
560 QuickWidget quick_widgets [] = {
561 { quick_button, 6, 10, 1, 0, N_("&Cancel"), 0, B_CANCEL, 0, 0,
562 XV_WLAY_RIGHTOF, "button-cancel" },
563 { quick_button, 3, 10, 1, 0, N_("&Ok"), 0, B_ENTER, 0, 0,
564 XV_WLAY_CENTERROW, "button-ok" },
565 { quick_input, 4, 80, 0, 0, "", 58, 0, 0, 0, XV_WLAY_NEXTROW, 0 },
566 { quick_label, 3, 80, 2, 0, "", 0, 0, 0, 0, XV_WLAY_NEXTROW, "label" },
567 { 0 } };
569 int len;
570 int i;
571 int lines;
572 char *my_str;
573 char tk_name[64] = "inp|";
575 /* we need a unique name for tkname because widget.c:history_tool()
576 needs a unique name for each dialog - using the header is ideal */
578 strncpy (tk_name + 3, header, 60);
579 tk_name[63] = '\0';
580 quick_widgets[2].tkname = tk_name;
582 len = max (strlen (header), msglen (text, &lines)) + 4;
583 len = max (len, 64);
585 /* Translators should take care as "Password" or its translations
586 are used to identify password boxes and hide characters with "*" */
587 if (strncmp (text, _("Password:"), strlen (_("Password:"))-1) == 0){
588 quick_widgets [INPUT_INDEX].value = 1;
589 tk_name[3]=0;
590 } else {
591 quick_widgets [INPUT_INDEX].value = 0;
594 #ifdef ENABLE_NLS
596 * An attempt to place buttons symmetrically, based on actual i18n
597 * length of the string. It looks nicer with i18n (IMO) - alex
599 quick_widgets [0].relative_x = len/2 + 4;
600 quick_widgets [1].relative_x =
601 len/2 - (strlen (_(quick_widgets [1].text)) + 9);
602 quick_widgets [0].x_divisions = quick_widgets [1].x_divisions = len;
603 #endif /* ENABLE_NLS */
605 Quick_input.xlen = len;
606 Quick_input.xpos = -1;
607 Quick_input.title = header;
608 Quick_input.help = help;
609 Quick_input.class = "quick_input";
610 Quick_input.i18n = 0;
611 quick_widgets [INPUT_INDEX+1].text = text;
612 quick_widgets [INPUT_INDEX].text = def_text;
614 for (i = 0; i < 4; i++)
615 quick_widgets [i].y_divisions = lines+6;
616 Quick_input.ylen = lines + 6;
618 for (i = 0; i < 3; i++)
619 quick_widgets [i].relative_y += 2 + lines;
621 quick_widgets [INPUT_INDEX].str_result = &my_str;
623 Quick_input.widgets = quick_widgets;
624 if (quick_dialog (&Quick_input) != B_CANCEL){
625 return *(quick_widgets [INPUT_INDEX].str_result);
626 } else
627 return 0;
629 #endif
631 char *input_dialog (char *header, char *text, char *def_text)
633 return input_dialog_help (header, text, "[Input Line Keys]", def_text);
636 char *input_expand_dialog (char *header, char *text, char *def_text)
638 char *result;
639 char *expanded;
641 result = input_dialog (header, text, def_text);
642 if (result){
643 expanded = tilde_expand (result);
644 if (expanded){
645 g_free (result);
646 return expanded;
647 } else
648 return result;
650 return result;
653 /* }}} */
655 /* }}} */
657 Cause emacs to enter folding mode for this file:
658 Local variables:
659 end: