*** empty log message ***
[midnight-commander.git] / src / wtools.c
blob867b9510f52c62412830f129864bbb80dae6e45a
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 int space;
60 space = (h->flags & DLG_COMPACT) ? 0 : 1;
62 attrset (back);
63 dlg_erase (h);
64 draw_box (h, space, space, h->lines - 2 * space, h->cols - 2 * space);
65 attrset (title_fore);
66 if (h->title) {
67 dlg_move (h, space, (h->cols - strlen (h->title)) / 2);
68 addstr (h->title);
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 #define listbox_refresh(h) common_dialog_repaint(h)
91 static int listbox_callback (Dlg_head *h, int id, int msg)
93 switch (msg) {
94 case DLG_DRAW:
95 listbox_refresh(h);
96 return 1;
98 return 0;
101 Listbox *create_listbox_window (int cols, int lines, char *title, char *help)
103 int xpos, ypos, len;
104 Listbox *listbox = g_new (Listbox, 1);
105 char* cancel_string = _("&Cancel");
107 /* Adjust sizes */
108 lines = (lines > LINES-6) ? LINES - 6 : lines;
110 if (title && (cols < (len = strlen(title) + 2)))
111 cols = len;
113 /* no &, but 4 spaces around button for brackets and such */
114 if (cols < (len = strlen(cancel_string) + 3))
115 cols = len;
117 cols = cols > COLS-6 ? COLS-6 : cols;
119 /* I'm not sure if this -2 is safe, should test it */
120 xpos = (COLS-cols)/2;
121 ypos = (LINES-lines)/2 - 2;
123 /* Create components */
124 listbox->dlg = create_dlg (ypos, xpos, lines+6, cols+4, dialog_colors,
125 listbox_callback, help, "listbox", DLG_CENTER);
126 x_set_dialog_title (listbox->dlg, title);
128 listbox->list = listbox_new (2, 2, cols, lines, listbox_finish, 0, "li");
130 add_widget (listbox->dlg,
131 button_new (lines+3, (cols/2 + 2) - len/2,
132 B_CANCEL, NORMAL_BUTTON, cancel_string, 0, 0, "c"));
133 add_widget (listbox->dlg, listbox->list);
134 listbox_refresh(listbox->dlg);
135 return listbox;
138 /* Returns the number of the item selected */
139 int run_listbox (Listbox *l)
141 int val;
143 run_dlg (l->dlg);
144 if (l->dlg->ret_value == B_CANCEL)
145 val = -1;
146 else
147 val = l->list->pos;
148 destroy_dlg (l->dlg);
149 g_free (l);
150 return val;
153 /* }}} */
156 /* {{{ Query Dialog functions */
157 static int query_callback (struct Dlg_head *h, int Id, int Msg)
159 switch (Msg){
160 case DLG_DRAW:
161 dialog_repaint (h, NORMALC, HOT_NORMALC);
162 break;
164 return 0;
168 Dlg_head *last_query_dlg;
170 static int sel_pos = 0;
172 /* Used to ask questions to the user */
173 int query_dialog (char *header, char *text, int flags, int count, ...)
175 va_list ap;
176 Dlg_head *query_dlg;
177 int win_len = 0;
178 int i;
179 int result = -1;
180 int xpos, ypos;
181 int cols, lines;
182 char *cur_name;
183 static int query_colors [4];
185 /* set dialog colors */
186 query_colors [0] = (flags & D_ERROR) ? ERROR_COLOR : Q_UNSELECTED_COLOR;
187 query_colors [1] = (flags & D_ERROR) ? REVERSE_COLOR : Q_SELECTED_COLOR;
188 query_colors [2] = (flags & D_ERROR) ? ERROR_COLOR : COLOR_HOT_NORMAL;
189 query_colors [3] = (flags & D_ERROR) ? COLOR_HOT_NORMAL : COLOR_HOT_FOCUS;
191 if (header == MSG_ERROR)
192 header = _(" Error ");
194 if (count > 0){
195 va_start (ap, count);
196 for (i = 0; i < count; i++)
198 char* cp = va_arg (ap, char *);
199 win_len += strlen (cp) + 6;
200 if (strchr (cp, '&') != NULL)
201 win_len--;
203 va_end (ap);
206 /* count coordinates */
207 cols = 6 + max (win_len, max (strlen (header), msglen (text, &lines)));
208 lines += 4 + (count > 0 ? 2 : 0);
209 xpos = COLS/2 - cols/2;
210 ypos = LINES/3 - (lines-3)/2;
212 /* prepare dialog */
213 query_dlg = create_dlg (ypos, xpos, lines, cols, query_colors,
214 query_callback, "[QueryBox]", "query", DLG_BACKWARD);
215 x_set_dialog_title (query_dlg, header);
217 if (count > 0){
219 cols = (cols-win_len-2)/2+2;
220 va_start (ap, count);
221 for (i = 0; i < count; i++){
222 cur_name = va_arg (ap, char *);
223 xpos = strlen (cur_name)+6;
224 if (strchr(cur_name, '&') != NULL)
225 xpos--;
226 add_widget (query_dlg, button_new
227 (lines-3, cols, B_USER+i, NORMAL_BUTTON, cur_name,
228 0, 0, NULL));
229 cols += xpos;
230 if (i == sel_pos)
231 query_dlg->initfocus = query_dlg->current;
233 va_end (ap);
235 add_widget (query_dlg, label_new (2, 3, text, NULL));
237 /* run dialog and make result */
238 run_dlg (query_dlg);
239 switch (query_dlg->ret_value){
240 case B_CANCEL:
241 break;
242 default:
243 result = query_dlg->ret_value-B_USER;
246 /* free used memory */
247 destroy_dlg (query_dlg);
248 } else {
249 add_widget (query_dlg, label_new (2, 3, text, NULL));
250 add_widget (query_dlg, button_new(0, 0, 0, HIDDEN_BUTTON, "-", 0, 0, NULL));
251 last_query_dlg = query_dlg;
253 sel_pos = 0;
254 return result;
257 void query_set_sel (int new_sel)
259 sel_pos = new_sel;
262 /* }}} */
264 /* {{{ The message function */
266 /* To show nice messages to the users */
267 Dlg_head *message (int error, char *header, const char *text, ...)
269 va_list args;
270 char buffer [4096];
271 Dlg_head *d;
273 /* Setup the display information */
274 strcpy (buffer, "\n");
275 va_start (args, text);
276 g_vsnprintf (&buffer [1], sizeof (buffer) - 2, text, args);
277 strcat (buffer, "\n");
278 va_end (args);
280 query_dialog (header, buffer, error, 0);
282 d = last_query_dlg;
283 init_dlg (d);
284 if (!(error & D_INSERT)){
285 mi_getch ();
286 dlg_run_done (d);
287 destroy_dlg (d);
288 } else
289 return d;
290 return 0;
292 /* }}} */
294 /* {{{ The chooser routines */
296 static int remove_callback (int i, void *data)
298 Chooser *c = (Chooser *) data;
300 listbox_remove_current (c->listbox, 0);
302 dlg_select_widget (c->dialog, c->listbox);
303 dlg_select_nth_widget (c->dialog, 0);
305 /* Return: do not abort dialog */
306 return 0;
309 Chooser *new_chooser (int lines, int cols, char *help, int flags)
311 Chooser *c;
312 int button_lines;
314 c =g_new (Chooser, 1);
315 c->dialog = create_dlg (0, 0, lines, cols, dialog_colors, common_dialog_callback,
316 help, "chooser", DLG_CENTER);
318 c->dialog->lines = lines;
319 c->dialog->cols = cols;
321 button_lines = flags & CHOOSE_EDITABLE ? 3 : 0;
323 c->listbox = listbox_new (1, 1, cols-2, lines-button_lines,
324 listbox_finish, 0, "listbox");
326 if (button_lines){
327 add_widget (c->dialog, button_new (lines-button_lines+1,
328 20, B_ENTER, DEFPUSH_BUTTON, _("&Remove"),
329 remove_callback, c, "button-remove"));
330 add_widget (c->dialog, button_new (lines-button_lines+1,
331 4, B_CANCEL, NORMAL_BUTTON, _("&Cancel"),
332 0, 0, "button-cancel"));
334 add_widget (c->dialog, c->listbox);
335 return c;
338 int run_chooser (Chooser *c)
340 run_dlg (c->dialog);
341 return c->dialog->ret_value;
344 void destroy_chooser (Chooser *c)
346 destroy_dlg (c->dialog);
349 /* }}} */
351 /* {{{ Quick dialog routines */
353 static int quick_callback (struct Dlg_head *h, int id, int Msg)
355 switch (Msg){
356 case DLG_DRAW:
357 common_dialog_repaint (h);
358 break;
359 case DLG_KEY:
360 if (id == '\n'){
361 h->ret_value = B_ENTER;
362 dlg_stop (h);
363 break;
366 return 0;
369 #define I18N(x) (do_int && *x ? (x = _(x)): x)
371 int quick_dialog_skip (QuickDialog *qd, int nskip)
373 Dlg_head *dd;
374 void *widget;
375 WRadio *r;
376 int xpos;
377 int ypos;
378 int return_val;
379 WInput *input;
380 QuickWidget *qw;
381 int do_int;
383 if (!qd->i18n){
384 qd->i18n = 1;
385 do_int = 1;
386 if (*qd->title)
387 qd->title = _(qd->title);
388 } else
389 do_int = 0;
391 if (qd->xpos == -1)
392 dd = create_dlg (0, 0, qd->ylen, qd->xlen, dialog_colors, quick_callback,
393 qd->help, qd->class, DLG_CENTER | DLG_TRYUP);
394 else
395 dd = create_dlg (qd->ypos, qd->xpos, qd->ylen, qd->xlen, dialog_colors,
396 quick_callback,
397 qd->help, qd->class, DLG_NONE);
399 x_set_dialog_title (dd, qd->title);
401 /* We pass this to the callback */
402 dd->cols = qd->xlen;
403 dd->lines = qd->ylen;
405 for (qw = qd->widgets; qw->widget_type; qw++){
406 xpos = (qd->xlen * qw->relative_x)/qw->x_divisions;
407 ypos = (qd->ylen * qw->relative_y)/qw->y_divisions;
409 switch (qw->widget_type){
410 case quick_checkbox:
411 widget = check_new (ypos, xpos, *qw->result, I18N (qw->text), qw->tkname);
412 break;
414 case quick_radio:
415 r = radio_new (ypos, xpos, qw->hotkey_pos, qw->str_result, 1, qw->tkname);
416 r->pos = r->sel = qw->value;
417 widget = r;
418 break;
420 case quick_button:
421 widget = button_new (ypos, xpos, qw->value, (qw->value==B_ENTER) ? DEFPUSH_BUTTON : NORMAL_BUTTON,
422 I18N (qw->text), 0, 0, qw->tkname);
423 break;
425 /* We use the hotkey pos as the field length */
426 case quick_input:
427 input = input_new (ypos, xpos, INPUT_COLOR,
428 qw->hotkey_pos, qw->text, qw->tkname);
429 input->is_password = qw->value == 1;
430 input->point = 0;
431 if (qw->value & 2)
432 input->completion_flags |= INPUT_COMPLETE_CD;
433 widget = input;
434 break;
436 case quick_label:
437 widget = label_new (ypos, xpos, I18N(qw->text), qw->tkname);
438 break;
440 default:
441 widget = 0;
442 fprintf (stderr, "QuickWidget: unknown widget type\n");
443 break;
445 qw->the_widget = widget;
446 add_widget (dd, widget);
449 while (nskip--)
450 dd->current = dd->current->next;
452 run_dlg (dd);
454 /* Get the data if we found something interesting */
455 if (dd->ret_value != B_CANCEL){
456 for (qw = qd->widgets; qw->widget_type; qw++){
457 switch (qw->widget_type){
458 case quick_checkbox:
459 *qw->result = ((WCheck *) qw->the_widget)->state & C_BOOL;
460 break;
462 case quick_radio:
463 *qw->result = ((WRadio *) qw->the_widget)->sel;
464 break;
466 case quick_input:
467 *qw->str_result = g_strdup (((WInput *) qw->the_widget)->buffer);
468 break;
472 return_val = dd->ret_value;
473 destroy_dlg (dd);
475 return return_val;
478 int quick_dialog (QuickDialog *qd)
480 return quick_dialog_skip (qd, 0);
483 /* }}} */
485 /* {{{ Input routines */
487 #define INPUT_INDEX 2
488 char *real_input_dialog_help (char *header, char *text, char *help, char *def_text)
490 QuickDialog Quick_input;
491 QuickWidget quick_widgets [] = {
492 { quick_button, 6, 10, 1, 0, N_("&Cancel"), 0, B_CANCEL, 0, 0,
493 "button-cancel" },
494 { quick_button, 3, 10, 1, 0, N_("&Ok"), 0, B_ENTER, 0, 0,
495 "button-ok" },
496 { quick_input, 4, 80, 0, 0, "", 58, 0, 0, 0, 0 },
497 { quick_label, 3, 80, 2, 0, "", 0, 0, 0, 0, "label" },
498 { 0 } };
500 int len;
501 int i;
502 int lines;
503 char *my_str;
504 char tk_name[64] = "inp|";
506 /* we need a unique name for tkname because widget.c:history_tool()
507 needs a unique name for each dialog - using the header is ideal */
509 strncpy (tk_name + 3, header, 60);
510 tk_name[63] = '\0';
511 quick_widgets[2].tkname = tk_name;
513 len = max (strlen (header), msglen (text, &lines)) + 4;
514 len = max (len, 64);
516 /* Translators should take care as "Password" or its translations
517 are used to identify password boxes and hide characters with "*" */
518 my_str = _("Password:");
519 if (strncmp (text, my_str, strlen (my_str)-1) == 0){
520 quick_widgets [INPUT_INDEX].value = 1;
521 tk_name[3]=0;
522 } else {
523 quick_widgets [INPUT_INDEX].value = 0;
526 #ifdef ENABLE_NLS
528 * An attempt to place buttons symmetrically, based on actual i18n
529 * length of the string. It looks nicer with i18n (IMO) - alex
531 quick_widgets [0].relative_x = len/2 + 4;
532 quick_widgets [1].relative_x =
533 len/2 - (strlen (_(quick_widgets [1].text)) + 9);
534 quick_widgets [0].x_divisions = quick_widgets [1].x_divisions = len;
535 #endif /* ENABLE_NLS */
537 Quick_input.xlen = len;
538 Quick_input.xpos = -1;
539 Quick_input.title = header;
540 Quick_input.help = help;
541 Quick_input.class = "quick_input";
542 Quick_input.i18n = 0;
543 quick_widgets [INPUT_INDEX+1].text = text;
544 quick_widgets [INPUT_INDEX].text = def_text;
546 for (i = 0; i < 4; i++)
547 quick_widgets [i].y_divisions = lines+6;
548 Quick_input.ylen = lines + 6;
550 for (i = 0; i < 3; i++)
551 quick_widgets [i].relative_y += 2 + lines;
553 quick_widgets [INPUT_INDEX].str_result = &my_str;
555 Quick_input.widgets = quick_widgets;
556 if (quick_dialog (&Quick_input) != B_CANCEL){
557 return *(quick_widgets [INPUT_INDEX].str_result);
558 } else
559 return 0;
562 char *input_dialog (char *header, char *text, char *def_text)
564 return input_dialog_help (header, text, "[Input Line Keys]", def_text);
567 char *input_expand_dialog (char *header, char *text, char *def_text)
569 char *result;
570 char *expanded;
572 result = input_dialog (header, text, def_text);
573 if (result){
574 expanded = tilde_expand (result);
575 if (expanded){
576 g_free (result);
577 return expanded;
580 return result;
583 /* }}} */
585 /* }}} */
587 Cause emacs to enter folding mode for this file:
588 Local variables:
589 end: