*** empty log message ***
[midnight-commander.git] / src / wtools.c
blob8a50601e58f97db70cf573e1d2ecde44d4ca9504
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /* }}} */
31 /* [] = "$Id$" */
33 #include <config.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <stdarg.h>
38 #include "global.h"
39 #include "tty.h"
40 #include "win.h"
41 #include "color.h"
42 #include "mouse.h"
43 #include "dlg.h"
44 #include "widget.h"
45 #include "menu.h"
46 #include "wtools.h"
47 #include "key.h" /* For mi_getch() */
48 #include "dialog.h" /* For do_refresh() */
49 #include "complete.h" /* INPUT_COMPLETE_CD */
51 /* }}} */
53 /* {{{ Common dialog callback */
55 void
56 dialog_repaint (struct Dlg_head *h, int back, int title_fore)
58 #ifndef HAVE_X
59 attrset (back);
60 dlg_erase (h);
61 draw_box (h, 1, 1, h->lines - 2, h->cols - 2);
62 attrset (title_fore);
63 if (h->title){
64 dlg_move (h, 1, (h->cols-strlen (h->title))/2);
65 addstr (h->title);
67 #endif
70 void
71 common_dialog_repaint (struct Dlg_head *h)
73 dialog_repaint (h, COLOR_NORMAL, COLOR_HOT_NORMAL);
76 int
77 common_dialog_callback (struct Dlg_head *h, int id, int msg)
79 if (msg == DLG_DRAW)
80 common_dialog_repaint (h);
81 return 0;
84 /* }}} */
85 /* {{{ Listbox utility functions */
87 #ifdef HAVE_X
88 #define listbox_refresh(h)
89 #else
90 #define listbox_refresh(h) common_dialog_repaint(h)
91 #endif
93 static int listbox_callback (Dlg_head *h, int id, int msg)
95 switch (msg) {
96 case DLG_DRAW:
97 listbox_refresh(h);
98 return 1;
100 return 0;
103 Listbox *create_listbox_window (int cols, int lines, char *title, char *help)
105 int xpos, ypos, len;
106 Listbox *listbox = g_new (Listbox, 1);
107 char* cancel_string = _("&Cancel");
109 /* Adjust sizes */
110 lines = (lines > LINES-6) ? LINES - 6 : lines;
112 if (title && (cols < (len = strlen(title) + 2)))
113 cols = len;
115 /* no &, but 4 spaces around button for brackets and such */
116 if (cols < (len = strlen(cancel_string) + 3))
117 cols = len;
119 cols = cols > COLS-6 ? COLS-6 : cols;
121 /* I'm not sure if this -2 is safe, should test it */
122 xpos = (COLS-cols)/2;
123 ypos = (LINES-lines)/2 - 2;
125 /* Create components */
126 listbox->dlg = create_dlg (ypos, xpos, lines+6, cols+4, dialog_colors,
127 listbox_callback, help, "listbox", DLG_CENTER|DLG_GRID);
128 x_set_dialog_title (listbox->dlg, title);
130 listbox->list = listbox_new (2, 2, cols, lines, listbox_finish, 0, "li");
132 add_widget (listbox->dlg,
133 button_new (lines+3, (cols/2 + 2) - len/2,
134 B_CANCEL, NORMAL_BUTTON, cancel_string, 0, 0, "c"));
135 add_widget (listbox->dlg, listbox->list);
136 listbox_refresh(listbox->dlg);
137 return listbox;
140 /* Returns the number of the item selected */
141 int run_listbox (Listbox *l)
143 int val;
145 run_dlg (l->dlg);
146 if (l->dlg->ret_value == B_CANCEL)
147 val = -1;
148 else
149 val = l->list->pos;
150 destroy_dlg (l->dlg);
151 g_free (l);
152 return val;
155 /* }}} */
158 /* {{{ Query Dialog functions */
159 #ifndef HAVE_GNOME
160 struct text_struct {
161 char *text;
162 char *header;
165 static int query_callback (struct Dlg_head *h, int Id, int Msg)
167 #ifndef HAVE_X
168 struct text_struct *info;
170 info = (struct text_struct *) h->data;
172 switch (Msg){
173 case DLG_DRAW:
174 /* designate window */
175 attrset (NORMALC);
176 dlg_erase (h);
177 draw_box (h, 1, 1, h->lines-2, h->cols-2);
178 attrset (HOT_NORMALC);
179 dlg_move (h, 1, (h->cols-strlen (info->header))/2);
180 addstr (info->header);
181 break;
183 #endif
184 return 0;
188 Dlg_head *last_query_dlg;
190 static int sel_pos = 0;
192 /* Used to ask questions to the user */
193 int query_dialog (char *header, char *text, int flags, int count, ...)
195 va_list ap;
196 Dlg_head *query_dlg;
197 int win_len = 0;
198 int i;
199 int result = -1;
200 int xpos, ypos;
201 int cols, lines;
202 char *cur_name;
203 static int query_colors [4];
204 static struct text_struct pass;
205 #ifdef HAVE_X
206 static char *buttonnames [10];
207 #endif
209 /* set dialog colors */
210 query_colors [0] = (flags & D_ERROR) ? ERROR_COLOR : Q_UNSELECTED_COLOR;
211 query_colors [1] = (flags & D_ERROR) ? REVERSE_COLOR : Q_SELECTED_COLOR;
212 query_colors [2] = (flags & D_ERROR) ? ERROR_COLOR : COLOR_HOT_NORMAL;
213 query_colors [3] = (flags & D_ERROR) ? COLOR_HOT_NORMAL : COLOR_HOT_FOCUS;
215 if (header == MSG_ERROR)
216 header = _(" Error ");
218 if (count > 0){
219 va_start (ap, count);
220 for (i = 0; i < count; i++)
222 char* cp = va_arg (ap, char *);
223 win_len += strlen (cp) + 6;
224 if (strchr (cp, '&') != NULL)
225 win_len--;
227 va_end (ap);
230 /* count coordinates */
231 cols = 6 + max (win_len, max (strlen (header), msglen (text, &lines)));
232 lines += 4 + (count > 0 ? 2 : 0);
233 xpos = COLS/2 - cols/2;
234 ypos = LINES/3 - (lines-3)/2;
235 pass.header = header;
236 pass.text = text;
238 /* prepare dialog */
239 query_dlg = create_dlg (ypos, xpos, lines, cols, query_colors,
240 query_callback, "[QueryBox]", "query", DLG_NONE);
241 x_set_dialog_title (query_dlg, header);
243 /* The data we need to pass to the callback */
244 query_dlg->cols = cols;
245 query_dlg->lines = lines;
246 query_dlg->data = &pass;
248 query_dlg->direction = DIR_BACKWARD;
250 if (count > 0){
252 cols = (cols-win_len-2)/2+2;
253 va_start (ap, count);
254 for (i = 0; i < count; i++){
255 cur_name = va_arg (ap, char *);
256 xpos = strlen (cur_name)+6;
257 if (strchr(cur_name, '&') != NULL)
258 xpos--;
259 add_widget (query_dlg, button_new
260 (lines-3, cols, B_USER+i, NORMAL_BUTTON, cur_name,
261 0, 0, NULL));
262 cols += xpos;
263 if (i == sel_pos)
264 query_dlg->initfocus = query_dlg->current;
266 va_end (ap);
268 add_widget (query_dlg, label_new (2, 3, text, NULL));
270 /* run dialog and make result */
271 run_dlg (query_dlg);
272 switch (query_dlg->ret_value){
273 case B_CANCEL:
274 break;
275 default:
276 result = query_dlg->ret_value-B_USER;
279 /* free used memory */
280 destroy_dlg (query_dlg);
281 } else {
282 #ifdef HAVE_X
283 add_widget (query_dlg, button_new(0, 0, B_EXIT, NORMAL_BUTTON,
284 _("&Ok"), 0, 0, NULL));
286 add_widget (query_dlg, label_new (2, 3, text, NULL));
287 run_dlg (query_dlg);
288 destroy_dlg (query_dlg);
289 #else
290 add_widget (query_dlg, label_new (2, 3, text, NULL));
291 add_widget (query_dlg, button_new(0, 0, 0, HIDDEN_BUTTON, "-", 0, 0, NULL));
292 #endif /* HAVE_X */
293 last_query_dlg = query_dlg;
295 sel_pos = 0;
296 return result;
299 void query_set_sel (int new_sel)
301 sel_pos = new_sel;
304 /* }}} */
306 /* {{{ The message function */
308 /* To show nice messages to the users */
309 Dlg_head *message (int error, char *header, const char *text, ...)
311 va_list args;
312 char buffer [4096];
313 Dlg_head *d;
315 /* Setup the display information */
316 strcpy (buffer, "\n");
317 va_start (args, text);
318 g_vsnprintf (&buffer [1], sizeof (buffer) - 1, text, args);
319 strcat (buffer, "\n");
320 va_end (args);
322 query_dialog (header, buffer, error, 0);
324 d = last_query_dlg;
325 init_dlg (d);
326 if (!(error & D_INSERT)){
327 mi_getch ();
328 dlg_run_done (d);
329 destroy_dlg (d);
330 } else
331 return d;
332 return 0;
334 #endif
335 /* }}} */
337 /* {{{ The chooser routines */
339 static int remove_callback (int i, void *data)
341 Chooser *c = (Chooser *) data;
343 listbox_remove_current (c->listbox, 0);
345 dlg_select_widget (c->dialog, c->listbox);
346 dlg_select_nth_widget (c->dialog, 0);
348 /* Return: do not abort dialog */
349 return 0;
352 Chooser *new_chooser (int lines, int cols, char *help, int flags)
354 Chooser *c;
355 int button_lines;
357 c =g_new (Chooser, 1);
358 c->dialog = create_dlg (0, 0, lines, cols, dialog_colors, common_dialog_callback,
359 help, "chooser", DLG_CENTER | DLG_GRID);
361 c->dialog->lines = lines;
362 c->dialog->cols = cols;
364 button_lines = flags & CHOOSE_EDITABLE ? 3 : 0;
366 c->listbox = listbox_new (1, 1, cols-2, lines-button_lines,
367 listbox_finish, 0, "listbox");
369 if (button_lines){
370 add_widget (c->dialog, button_new (lines-button_lines+1,
371 20, B_ENTER, DEFPUSH_BUTTON, _("&Remove"),
372 remove_callback, c, "button-remove"));
373 add_widget (c->dialog, button_new (lines-button_lines+1,
374 4, B_CANCEL, NORMAL_BUTTON, _("&Cancel"),
375 0, 0, "button-cancel"));
377 add_widget (c->dialog, c->listbox);
378 return c;
381 int run_chooser (Chooser *c)
383 run_dlg (c->dialog);
384 return c->dialog->ret_value;
387 void destroy_chooser (Chooser *c)
389 destroy_dlg (c->dialog);
392 /* }}} */
394 /* {{{ Quick dialog routines */
396 #ifndef HAVE_X
398 static int quick_callback (struct Dlg_head *h, int id, int Msg)
400 switch (Msg){
401 case DLG_DRAW:
402 attrset (COLOR_NORMAL);
403 dlg_erase (h);
404 draw_box (h, 1, 1, h->lines-2, h->cols-2);
406 attrset (COLOR_HOT_NORMAL);
407 dlg_move (h, 1,((h->cols-strlen (h->data))/2));
408 addstr (h->data);
409 break;
410 case DLG_KEY:
411 if (id == '\n'){
412 h->ret_value = B_ENTER;
413 dlg_stop (h);
414 break;
417 return 0;
420 #define I18N(x) (do_int && *x ? (x = _(x)): x)
422 int quick_dialog_skip (QuickDialog *qd, int nskip)
424 Dlg_head *dd;
425 void *widget;
426 WRadio *r;
427 int xpos;
428 int ypos;
429 int return_val;
430 WInput *input;
431 QuickWidget *qw;
432 int do_int;
434 if (!qd->i18n){
435 qd->i18n = 1;
436 do_int = 1;
437 if (*qd->title)
438 qd->title = _(qd->title);
439 } else
440 do_int = 0;
442 if (qd->xpos == -1)
443 dd = create_dlg (0, 0, qd->ylen, qd->xlen, dialog_colors, quick_callback,
444 qd->help, qd->class, DLG_CENTER | DLG_TRYUP | DLG_GRID);
445 else
446 dd = create_dlg (qd->ypos, qd->xpos, qd->ylen, qd->xlen, dialog_colors,
447 quick_callback,
448 qd->help, qd->class, DLG_GRID);
450 x_set_dialog_title (dd, qd->title);
452 /* We pass this to the callback */
453 dd->cols = qd->xlen;
454 dd->lines = qd->ylen;
455 dd->data = qd->title;
457 for (qw = qd->widgets; qw->widget_type; qw++){
458 xpos = (qd->xlen * qw->relative_x)/qw->x_divisions;
459 ypos = (qd->ylen * qw->relative_y)/qw->y_divisions;
461 switch (qw->widget_type){
462 case quick_checkbox:
463 widget = check_new (ypos, xpos, *qw->result, I18N (qw->text), qw->tkname);
464 break;
466 case quick_radio:
467 r = radio_new (ypos, xpos, qw->hotkey_pos, qw->str_result, 1, qw->tkname);
468 r->pos = r->sel = qw->value;
469 widget = r;
470 break;
472 case quick_button:
473 widget = button_new (ypos, xpos, qw->value, (qw->value==B_ENTER) ? DEFPUSH_BUTTON : NORMAL_BUTTON,
474 I18N (qw->text), 0, 0, qw->tkname);
475 break;
477 /* We use the hotkey pos as the field length */
478 case quick_input:
479 input = input_new (ypos, xpos, INPUT_COLOR,
480 qw->hotkey_pos, qw->text, qw->tkname);
481 input->is_password = qw->value == 1;
482 input->point = 0;
483 if (qw->value & 2)
484 input->completion_flags |= INPUT_COMPLETE_CD;
485 widget = input;
486 break;
488 case quick_label:
489 widget = label_new (ypos, xpos, I18N(qw->text), qw->tkname);
490 break;
492 default:
493 widget = 0;
494 fprintf (stderr, "QuickWidget: unknown widget type\n");
495 break;
497 qw->the_widget = widget;
498 add_widget (dd, widget);
501 while (nskip--)
502 dd->current = dd->current->next;
504 run_dlg (dd);
506 /* Get the data if we found something interesting */
507 if (dd->ret_value != B_CANCEL){
508 for (qw = qd->widgets; qw->widget_type; qw++){
509 switch (qw->widget_type){
510 case quick_checkbox:
511 *qw->result = ((WCheck *) qw->the_widget)->state & C_BOOL;
512 break;
514 case quick_radio:
515 *qw->result = ((WRadio *) qw->the_widget)->sel;
516 break;
518 case quick_input:
519 *qw->str_result = g_strdup (((WInput *) qw->the_widget)->buffer);
520 break;
524 return_val = dd->ret_value;
525 destroy_dlg (dd);
527 return return_val;
530 int quick_dialog (QuickDialog *qd)
532 return quick_dialog_skip (qd, 0);
535 /* }}} */
537 /* {{{ Input routines */
539 #define INPUT_INDEX 2
540 char *real_input_dialog_help (char *header, char *text, char *help, char *def_text)
542 QuickDialog Quick_input;
543 QuickWidget quick_widgets [] = {
544 { quick_button, 6, 10, 1, 0, N_("&Cancel"), 0, B_CANCEL, 0, 0,
545 "button-cancel" },
546 { quick_button, 3, 10, 1, 0, N_("&Ok"), 0, B_ENTER, 0, 0,
547 "button-ok" },
548 { quick_input, 4, 80, 0, 0, "", 58, 0, 0, 0, 0 },
549 { quick_label, 3, 80, 2, 0, "", 0, 0, 0, 0, "label" },
550 { 0 } };
552 int len;
553 int i;
554 int lines;
555 char *my_str;
556 char tk_name[64] = "inp|";
558 /* we need a unique name for tkname because widget.c:history_tool()
559 needs a unique name for each dialog - using the header is ideal */
561 strncpy (tk_name + 3, header, 60);
562 tk_name[63] = '\0';
563 quick_widgets[2].tkname = tk_name;
565 len = max (strlen (header), msglen (text, &lines)) + 4;
566 len = max (len, 64);
568 /* Translators should take care as "Password" or its translations
569 are used to identify password boxes and hide characters with "*" */
570 my_str = _("Password:");
571 if (strncmp (text, my_str, strlen (my_str)-1) == 0){
572 quick_widgets [INPUT_INDEX].value = 1;
573 tk_name[3]=0;
574 } else {
575 quick_widgets [INPUT_INDEX].value = 0;
578 #ifdef ENABLE_NLS
580 * An attempt to place buttons symmetrically, based on actual i18n
581 * length of the string. It looks nicer with i18n (IMO) - alex
583 quick_widgets [0].relative_x = len/2 + 4;
584 quick_widgets [1].relative_x =
585 len/2 - (strlen (_(quick_widgets [1].text)) + 9);
586 quick_widgets [0].x_divisions = quick_widgets [1].x_divisions = len;
587 #endif /* ENABLE_NLS */
589 Quick_input.xlen = len;
590 Quick_input.xpos = -1;
591 Quick_input.title = header;
592 Quick_input.help = help;
593 Quick_input.class = "quick_input";
594 Quick_input.i18n = 0;
595 quick_widgets [INPUT_INDEX+1].text = text;
596 quick_widgets [INPUT_INDEX].text = def_text;
598 for (i = 0; i < 4; i++)
599 quick_widgets [i].y_divisions = lines+6;
600 Quick_input.ylen = lines + 6;
602 for (i = 0; i < 3; i++)
603 quick_widgets [i].relative_y += 2 + lines;
605 quick_widgets [INPUT_INDEX].str_result = &my_str;
607 Quick_input.widgets = quick_widgets;
608 if (quick_dialog (&Quick_input) != B_CANCEL){
609 return *(quick_widgets [INPUT_INDEX].str_result);
610 } else
611 return 0;
613 #endif /* !HAVE_X */
615 char *input_dialog (char *header, char *text, char *def_text)
617 return input_dialog_help (header, text, "[Input Line Keys]", def_text);
620 char *input_expand_dialog (char *header, char *text, char *def_text)
622 char *result;
623 char *expanded;
625 result = input_dialog (header, text, def_text);
626 if (result){
627 expanded = tilde_expand (result);
628 if (expanded){
629 g_free (result);
630 return expanded;
633 return result;
636 /* }}} */
638 /* }}} */
640 Cause emacs to enter folding mode for this file:
641 Local variables:
642 end: