Just a little correction at the it.po file.
[midnight-commander.git] / src / wtools.c
blob563c1e91e97d9715b0fc84478e8686d48ac955c8
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 #include <config.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <stdarg.h>
36 #include "global.h"
37 #include "tty.h"
38 #include "win.h"
39 #include "color.h"
40 #include "mouse.h"
41 #include "dlg.h"
42 #include "widget.h"
43 #include "menu.h"
44 #include "wtools.h"
45 #include "key.h" /* For mi_getch() */
46 #include "dialog.h" /* For do_refresh() */
47 #include "complete.h" /* INPUT_COMPLETE_CD */
49 /* }}} */
51 /* {{{ Listbox utility functions */
53 Listbox *create_listbox_window (int cols, int lines, char *title, char *help)
55 int xpos, ypos, len;
56 Listbox *listbox = g_new (Listbox, 1);
57 char* cancel_string = _("&Cancel");
59 /* Adjust sizes */
60 lines = (lines > LINES-6) ? LINES - 6 : lines;
62 if (title && (cols < (len = strlen(title) + 2)))
63 cols = len;
65 /* no &, but 4 spaces around button for brackets and such */
66 if (cols < (len = strlen(cancel_string) + 3))
67 cols = len;
69 cols = cols > COLS-6 ? COLS-6 : cols;
71 /* I'm not sure if this -2 is safe, should test it */
72 xpos = (COLS-cols)/2;
73 ypos = (LINES-lines)/2 - 2;
75 /* Create components */
76 listbox->dlg = create_dlg (ypos, xpos, lines+6, cols+4, dialog_colors,
77 NULL, help, title, DLG_CENTER);
79 listbox->list = listbox_new (2, 2, cols, lines, listbox_finish, 0, "li");
81 add_widget (listbox->dlg,
82 button_new (lines+3, (cols/2 + 2) - len/2,
83 B_CANCEL, NORMAL_BUTTON, cancel_string, 0, 0, "c"));
84 add_widget (listbox->dlg, listbox->list);
85 common_dialog_repaint (listbox->dlg);
86 return listbox;
89 /* Returns the number of the item selected */
90 int run_listbox (Listbox *l)
92 int val;
94 run_dlg (l->dlg);
95 if (l->dlg->ret_value == B_CANCEL)
96 val = -1;
97 else
98 val = l->list->pos;
99 destroy_dlg (l->dlg);
100 g_free (l);
101 return val;
104 /* }}} */
107 /* {{{ Query Dialog functions */
108 Dlg_head *last_query_dlg;
110 static int sel_pos = 0;
112 /* Used to ask questions to the user */
113 int query_dialog (char *header, char *text, int flags, int count, ...)
115 va_list ap;
116 Dlg_head *query_dlg;
117 int win_len = 0;
118 int i;
119 int result = -1;
120 int xpos, ypos;
121 int cols, lines;
122 char *cur_name;
123 static const int *query_colors;
125 /* set dialog colors */
126 if (flags & D_ERROR)
127 query_colors = alarm_colors;
128 else
129 query_colors = dialog_colors;
131 if (header == MSG_ERROR)
132 header = _("Error");
134 if (count > 0){
135 va_start (ap, count);
136 for (i = 0; i < count; i++)
138 char* cp = va_arg (ap, char *);
139 win_len += strlen (cp) + 6;
140 if (strchr (cp, '&') != NULL)
141 win_len--;
143 va_end (ap);
146 /* count coordinates */
147 cols = 6 + max (win_len, max (strlen (header), msglen (text, &lines)));
148 lines += 4 + (count > 0 ? 2 : 0);
149 xpos = COLS/2 - cols/2;
150 ypos = LINES/3 - (lines-3)/2;
152 /* prepare dialog */
153 query_dlg = create_dlg (ypos, xpos, lines, cols, query_colors, NULL,
154 "[QueryBox]", header, DLG_BACKWARD);
156 if (count > 0){
158 cols = (cols-win_len-2)/2+2;
159 va_start (ap, count);
160 for (i = 0; i < count; i++){
161 cur_name = va_arg (ap, char *);
162 xpos = strlen (cur_name)+6;
163 if (strchr(cur_name, '&') != NULL)
164 xpos--;
165 add_widget (query_dlg, button_new
166 (lines-3, cols, B_USER+i, NORMAL_BUTTON, cur_name,
167 0, 0, NULL));
168 cols += xpos;
169 if (i == sel_pos)
170 query_dlg->initfocus = query_dlg->current;
172 va_end (ap);
174 add_widget (query_dlg, label_new (2, 3, text, NULL));
176 /* run dialog and make result */
177 run_dlg (query_dlg);
178 switch (query_dlg->ret_value){
179 case B_CANCEL:
180 break;
181 default:
182 result = query_dlg->ret_value-B_USER;
185 /* free used memory */
186 destroy_dlg (query_dlg);
187 } else {
188 add_widget (query_dlg, label_new (2, 3, text, NULL));
189 add_widget (query_dlg, button_new(0, 0, 0, HIDDEN_BUTTON, "-", 0, 0, NULL));
190 last_query_dlg = query_dlg;
192 sel_pos = 0;
193 return result;
196 void query_set_sel (int new_sel)
198 sel_pos = new_sel;
201 /* }}} */
203 /* {{{ The message function */
205 /* To show nice messages to the users */
206 struct Dlg_head *
207 message (int error, char *header, const char *text, ...)
209 va_list args;
210 char buffer [4096];
211 Dlg_head *d;
213 /* Setup the display information */
214 strcpy (buffer, "\n");
215 va_start (args, text);
216 g_vsnprintf (&buffer [1], sizeof (buffer) - 2, text, args);
217 strcat (buffer, "\n");
218 va_end (args);
220 query_dialog (header, buffer, error, 0);
222 d = last_query_dlg;
223 init_dlg (d);
224 if (!(error & D_INSERT)){
225 mi_getch ();
226 dlg_run_done (d);
227 destroy_dlg (d);
228 } else
229 return d;
230 return 0;
232 /* }}} */
234 /* {{{ Quick dialog routines */
236 static int quick_callback (struct Dlg_head *h, int id, int Msg)
238 switch (Msg){
239 case DLG_DRAW:
240 common_dialog_repaint (h);
241 break;
242 case DLG_KEY:
243 if (id == '\n'){
244 h->ret_value = B_ENTER;
245 dlg_stop (h);
246 break;
249 return 0;
252 #define I18N(x) (do_int && *x ? (x = _(x)): x)
255 quick_dialog_skip (QuickDialog *qd, int nskip)
257 Dlg_head *dd;
258 void *widget;
259 WRadio *r;
260 int xpos;
261 int ypos;
262 int return_val;
263 WInput *input;
264 QuickWidget *qw;
265 int do_int;
266 int count = 0; /* number of quick widgets */
267 int curr_widget; /* number of the current quick widget */
268 Widget **widgets; /* table of corresponding widgets */
270 if (!qd->i18n) {
271 qd->i18n = 1;
272 do_int = 1;
273 if (*qd->title)
274 qd->title = _(qd->title);
275 } else
276 do_int = 0;
278 if (qd->xpos == -1)
279 dd = create_dlg (0, 0, qd->ylen, qd->xlen, dialog_colors,
280 quick_callback, qd->help, qd->title,
281 DLG_CENTER | DLG_TRYUP);
282 else
283 dd = create_dlg (qd->ypos, qd->xpos, qd->ylen, qd->xlen,
284 dialog_colors, quick_callback, qd->help,
285 qd->title, DLG_NONE);
287 /* We pass this to the callback */
288 dd->cols = qd->xlen;
289 dd->lines = qd->ylen;
291 /* Count widgets */
292 for (qw = qd->widgets; qw->widget_type; qw++) {
293 count++;
296 widgets = (Widget **) g_new (Widget *, count);
298 for (curr_widget = 0, qw = qd->widgets; qw->widget_type; qw++) {
299 xpos = (qd->xlen * qw->relative_x) / qw->x_divisions;
300 ypos = (qd->ylen * qw->relative_y) / qw->y_divisions;
302 switch (qw->widget_type) {
303 case quick_checkbox:
304 widget =
305 check_new (ypos, xpos, *qw->result, I18N (qw->text),
306 qw->tkname);
307 break;
309 case quick_radio:
310 r = radio_new (ypos, xpos, qw->hotkey_pos, qw->str_result, 1,
311 qw->tkname);
312 r->pos = r->sel = qw->value;
313 widget = r;
314 break;
316 case quick_button:
317 widget =
318 button_new (ypos, xpos, qw->value,
319 (qw->value ==
320 B_ENTER) ? DEFPUSH_BUTTON : NORMAL_BUTTON,
321 I18N (qw->text), 0, 0, qw->tkname);
322 break;
324 /* We use the hotkey pos as the field length */
325 case quick_input:
326 input = input_new (ypos, xpos, INPUT_COLOR,
327 qw->hotkey_pos, qw->text, qw->tkname);
328 input->is_password = qw->value == 1;
329 input->point = 0;
330 if (qw->value & 2)
331 input->completion_flags |= INPUT_COMPLETE_CD;
332 widget = input;
333 break;
335 case quick_label:
336 widget = label_new (ypos, xpos, I18N (qw->text), qw->tkname);
337 break;
339 default:
340 widget = 0;
341 fprintf (stderr, "QuickWidget: unknown widget type\n");
342 break;
344 widgets[curr_widget++] = widget;
345 add_widget (dd, widget);
348 while (nskip--)
349 dd->current = dd->current->next;
351 run_dlg (dd);
353 /* Get the data if we found something interesting */
354 if (dd->ret_value != B_CANCEL) {
355 for (curr_widget = 0, qw = qd->widgets; qw->widget_type; qw++) {
356 Widget *w = widgets[curr_widget++];
358 switch (qw->widget_type) {
359 case quick_checkbox:
360 *qw->result = ((WCheck *) w)->state & C_BOOL;
361 break;
363 case quick_radio:
364 *qw->result = ((WRadio *) w)->sel;
365 break;
367 case quick_input:
368 *qw->str_result = g_strdup (((WInput *) w)->buffer);
369 break;
373 return_val = dd->ret_value;
374 destroy_dlg (dd);
375 g_free (widgets);
377 return return_val;
380 int quick_dialog (QuickDialog *qd)
382 return quick_dialog_skip (qd, 0);
385 /* }}} */
387 /* {{{ Input routines */
389 #define INPUT_INDEX 2
390 char *
391 real_input_dialog_help (char *header, char *text, char *help,
392 char *def_text)
394 QuickDialog Quick_input;
395 QuickWidget quick_widgets[] = {
396 {quick_button, 6, 10, 1, 0, N_("&Cancel"), 0, B_CANCEL, 0, 0,
397 "button-cancel"},
398 {quick_button, 3, 10, 1, 0, N_("&OK"), 0, B_ENTER, 0, 0,
399 "button-ok"},
400 {quick_input, 4, 80, 0, 0, "", 58, 0, 0, 0, 0},
401 {quick_label, 4, 80, 2, 0, "", 0, 0, 0, 0, "label"},
405 int len;
406 int i;
407 int lines;
408 int ret;
409 char *my_str;
410 char tk_name[64] = "inp|";
412 /* we need a unique name for tkname because widget.c:history_tool()
413 needs a unique name for each dialog - using the header is ideal */
415 strncpy (tk_name + 3, header, 60);
416 tk_name[63] = '\0';
417 quick_widgets[2].tkname = tk_name;
419 len = max (strlen (header), msglen (text, &lines)) + 4;
420 len = max (len, 64);
422 /* The special value of def_text is used to identify password boxes
423 and hide characters with "*". Don't save passwords in history! */
424 if (def_text == INPUT_PASSWORD) {
425 quick_widgets[INPUT_INDEX].value = 1;
426 tk_name[3] = 0;
427 def_text = "";
428 } else {
429 quick_widgets[INPUT_INDEX].value = 0;
432 #ifdef ENABLE_NLS
434 * An attempt to place buttons symmetrically, based on actual i18n
435 * length of the string. It looks nicer with i18n (IMO) - alex
437 quick_widgets[0].relative_x = len / 2 + 4;
438 quick_widgets[1].relative_x =
439 len / 2 - (strlen (_(quick_widgets[1].text)) + 9);
440 quick_widgets[0].x_divisions = quick_widgets[1].x_divisions = len;
441 #endif /* ENABLE_NLS */
443 Quick_input.xlen = len;
444 Quick_input.xpos = -1;
445 Quick_input.title = header;
446 Quick_input.help = help;
447 Quick_input.i18n = 0;
448 quick_widgets[INPUT_INDEX + 1].text = g_strstrip (g_strdup (text));
449 quick_widgets[INPUT_INDEX].text = def_text;
451 for (i = 0; i < 4; i++)
452 quick_widgets[i].y_divisions = lines + 6;
453 Quick_input.ylen = lines + 6;
455 for (i = 0; i < 3; i++)
456 quick_widgets[i].relative_y += 2 + lines;
458 quick_widgets[INPUT_INDEX].str_result = &my_str;
460 Quick_input.widgets = quick_widgets;
461 ret = quick_dialog (&Quick_input);
462 g_free (quick_widgets[INPUT_INDEX + 1].text);
464 if (ret != B_CANCEL) {
465 return *(quick_widgets[INPUT_INDEX].str_result);
466 } else
467 return 0;
470 char *input_dialog (char *header, char *text, char *def_text)
472 return input_dialog_help (header, text, "[Input Line Keys]", def_text);
475 char *input_expand_dialog (char *header, char *text, char *def_text)
477 char *result;
478 char *expanded;
480 result = input_dialog (header, text, def_text);
481 if (result){
482 expanded = tilde_expand (result);
483 if (expanded){
484 g_free (result);
485 return expanded;
488 return result;
491 /* }}} */
493 /* }}} */
495 Cause emacs to enter folding mode for this file:
496 Local variables:
497 end: