corrected some file operations dialog msgs
[midnight-commander.git] / src / wtools.c
blobd67e4c66dc3c09f0f3f33ebe62985903e3150aff
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 /* {{{ Listbox utility functions */
55 Listbox *create_listbox_window (int cols, int lines, char *title, char *help)
57 int xpos, ypos, len;
58 Listbox *listbox = g_new (Listbox, 1);
59 char* cancel_string = _("&Cancel");
61 /* Adjust sizes */
62 lines = (lines > LINES-6) ? LINES - 6 : lines;
64 if (title && (cols < (len = strlen(title) + 2)))
65 cols = len;
67 /* no &, but 4 spaces around button for brackets and such */
68 if (cols < (len = strlen(cancel_string) + 3))
69 cols = len;
71 cols = cols > COLS-6 ? COLS-6 : cols;
73 /* I'm not sure if this -2 is safe, should test it */
74 xpos = (COLS-cols)/2;
75 ypos = (LINES-lines)/2 - 2;
77 /* Create components */
78 listbox->dlg = create_dlg (ypos, xpos, lines+6, cols+4, dialog_colors,
79 NULL, help, title, DLG_CENTER);
81 listbox->list = listbox_new (2, 2, cols, lines, listbox_finish, 0, "li");
83 add_widget (listbox->dlg,
84 button_new (lines+3, (cols/2 + 2) - len/2,
85 B_CANCEL, NORMAL_BUTTON, cancel_string, 0, 0, "c"));
86 add_widget (listbox->dlg, listbox->list);
87 common_dialog_repaint (listbox->dlg);
88 return listbox;
91 /* Returns the number of the item selected */
92 int run_listbox (Listbox *l)
94 int val;
96 run_dlg (l->dlg);
97 if (l->dlg->ret_value == B_CANCEL)
98 val = -1;
99 else
100 val = l->list->pos;
101 destroy_dlg (l->dlg);
102 g_free (l);
103 return val;
106 /* }}} */
109 /* {{{ Query Dialog functions */
110 Dlg_head *last_query_dlg;
112 static int sel_pos = 0;
114 /* Used to ask questions to the user */
115 int query_dialog (char *header, char *text, int flags, int count, ...)
117 va_list ap;
118 Dlg_head *query_dlg;
119 int win_len = 0;
120 int i;
121 int result = -1;
122 int xpos, ypos;
123 int cols, lines;
124 char *cur_name;
125 static const int *query_colors;
127 /* set dialog colors */
128 if (flags & D_ERROR)
129 query_colors = alarm_colors;
130 else
131 query_colors = dialog_colors;
133 if (header == MSG_ERROR)
134 header = _(" Error ");
136 if (count > 0){
137 va_start (ap, count);
138 for (i = 0; i < count; i++)
140 char* cp = va_arg (ap, char *);
141 win_len += strlen (cp) + 6;
142 if (strchr (cp, '&') != NULL)
143 win_len--;
145 va_end (ap);
148 /* count coordinates */
149 cols = 6 + max (win_len, max (strlen (header), msglen (text, &lines)));
150 lines += 4 + (count > 0 ? 2 : 0);
151 xpos = COLS/2 - cols/2;
152 ypos = LINES/3 - (lines-3)/2;
154 /* prepare dialog */
155 query_dlg = create_dlg (ypos, xpos, lines, cols, query_colors, NULL,
156 "[QueryBox]", header, DLG_BACKWARD);
158 if (count > 0){
160 cols = (cols-win_len-2)/2+2;
161 va_start (ap, count);
162 for (i = 0; i < count; i++){
163 cur_name = va_arg (ap, char *);
164 xpos = strlen (cur_name)+6;
165 if (strchr(cur_name, '&') != NULL)
166 xpos--;
167 add_widget (query_dlg, button_new
168 (lines-3, cols, B_USER+i, NORMAL_BUTTON, cur_name,
169 0, 0, NULL));
170 cols += xpos;
171 if (i == sel_pos)
172 query_dlg->initfocus = query_dlg->current;
174 va_end (ap);
176 add_widget (query_dlg, label_new (2, 3, text, NULL));
178 /* run dialog and make result */
179 run_dlg (query_dlg);
180 switch (query_dlg->ret_value){
181 case B_CANCEL:
182 break;
183 default:
184 result = query_dlg->ret_value-B_USER;
187 /* free used memory */
188 destroy_dlg (query_dlg);
189 } else {
190 add_widget (query_dlg, label_new (2, 3, text, NULL));
191 add_widget (query_dlg, button_new(0, 0, 0, HIDDEN_BUTTON, "-", 0, 0, NULL));
192 last_query_dlg = query_dlg;
194 sel_pos = 0;
195 return result;
198 void query_set_sel (int new_sel)
200 sel_pos = new_sel;
203 /* }}} */
205 /* {{{ The message function */
207 /* To show nice messages to the users */
208 Dlg_head *message (int error, char *header, const char *text, ...)
210 va_list args;
211 char buffer [4096];
212 Dlg_head *d;
214 /* Setup the display information */
215 strcpy (buffer, "\n");
216 va_start (args, text);
217 g_vsnprintf (&buffer [1], sizeof (buffer) - 2, text, args);
218 strcat (buffer, "\n");
219 va_end (args);
221 query_dialog (header, buffer, error, 0);
223 d = last_query_dlg;
224 init_dlg (d);
225 if (!(error & D_INSERT)){
226 mi_getch ();
227 dlg_run_done (d);
228 destroy_dlg (d);
229 } else
230 return d;
231 return 0;
233 /* }}} */
235 /* {{{ Quick dialog routines */
237 static int quick_callback (struct Dlg_head *h, int id, int Msg)
239 switch (Msg){
240 case DLG_DRAW:
241 common_dialog_repaint (h);
242 break;
243 case DLG_KEY:
244 if (id == '\n'){
245 h->ret_value = B_ENTER;
246 dlg_stop (h);
247 break;
250 return 0;
253 #define I18N(x) (do_int && *x ? (x = _(x)): x)
255 int 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;
267 if (!qd->i18n){
268 qd->i18n = 1;
269 do_int = 1;
270 if (*qd->title)
271 qd->title = _(qd->title);
272 } else
273 do_int = 0;
275 if (qd->xpos == -1)
276 dd = create_dlg (0, 0, qd->ylen, qd->xlen, dialog_colors, quick_callback,
277 qd->help, qd->title, DLG_CENTER | DLG_TRYUP);
278 else
279 dd = create_dlg (qd->ypos, qd->xpos, qd->ylen, qd->xlen, dialog_colors,
280 quick_callback,
281 qd->help, qd->title, DLG_NONE);
283 /* We pass this to the callback */
284 dd->cols = qd->xlen;
285 dd->lines = qd->ylen;
287 for (qw = qd->widgets; qw->widget_type; qw++){
288 xpos = (qd->xlen * qw->relative_x)/qw->x_divisions;
289 ypos = (qd->ylen * qw->relative_y)/qw->y_divisions;
291 switch (qw->widget_type){
292 case quick_checkbox:
293 widget = check_new (ypos, xpos, *qw->result, I18N (qw->text), qw->tkname);
294 break;
296 case quick_radio:
297 r = radio_new (ypos, xpos, qw->hotkey_pos, qw->str_result, 1, qw->tkname);
298 r->pos = r->sel = qw->value;
299 widget = r;
300 break;
302 case quick_button:
303 widget = button_new (ypos, xpos, qw->value, (qw->value==B_ENTER) ? DEFPUSH_BUTTON : NORMAL_BUTTON,
304 I18N (qw->text), 0, 0, qw->tkname);
305 break;
307 /* We use the hotkey pos as the field length */
308 case quick_input:
309 input = input_new (ypos, xpos, INPUT_COLOR,
310 qw->hotkey_pos, qw->text, qw->tkname);
311 input->is_password = qw->value == 1;
312 input->point = 0;
313 if (qw->value & 2)
314 input->completion_flags |= INPUT_COMPLETE_CD;
315 widget = input;
316 break;
318 case quick_label:
319 widget = label_new (ypos, xpos, I18N(qw->text), qw->tkname);
320 break;
322 default:
323 widget = 0;
324 fprintf (stderr, "QuickWidget: unknown widget type\n");
325 break;
327 qw->the_widget = widget;
328 add_widget (dd, widget);
331 while (nskip--)
332 dd->current = dd->current->next;
334 run_dlg (dd);
336 /* Get the data if we found something interesting */
337 if (dd->ret_value != B_CANCEL){
338 for (qw = qd->widgets; qw->widget_type; qw++){
339 switch (qw->widget_type){
340 case quick_checkbox:
341 *qw->result = ((WCheck *) qw->the_widget)->state & C_BOOL;
342 break;
344 case quick_radio:
345 *qw->result = ((WRadio *) qw->the_widget)->sel;
346 break;
348 case quick_input:
349 *qw->str_result = g_strdup (((WInput *) qw->the_widget)->buffer);
350 break;
354 return_val = dd->ret_value;
355 destroy_dlg (dd);
357 return return_val;
360 int quick_dialog (QuickDialog *qd)
362 return quick_dialog_skip (qd, 0);
365 /* }}} */
367 /* {{{ Input routines */
369 #define INPUT_INDEX 2
370 char *
371 real_input_dialog_help (char *header, char *text, char *help,
372 char *def_text)
374 QuickDialog Quick_input;
375 QuickWidget quick_widgets[] = {
376 {quick_button, 6, 10, 1, 0, N_("&Cancel"), 0, B_CANCEL, 0, 0,
377 "button-cancel"},
378 {quick_button, 3, 10, 1, 0, N_("&Ok"), 0, B_ENTER, 0, 0,
379 "button-ok"},
380 {quick_input, 4, 80, 0, 0, "", 58, 0, 0, 0, 0},
381 {quick_label, 4, 80, 2, 0, "", 0, 0, 0, 0, "label"},
385 int len;
386 int i;
387 int lines;
388 int ret;
389 char *my_str;
390 char tk_name[64] = "inp|";
392 /* we need a unique name for tkname because widget.c:history_tool()
393 needs a unique name for each dialog - using the header is ideal */
395 strncpy (tk_name + 3, header, 60);
396 tk_name[63] = '\0';
397 quick_widgets[2].tkname = tk_name;
399 len = max (strlen (header), msglen (text, &lines)) + 4;
400 len = max (len, 64);
402 /* The special value of def_text is used to identify password boxes
403 and hide characters with "*". Don't save passwords in history! */
404 if (def_text == INPUT_PASSWORD) {
405 quick_widgets[INPUT_INDEX].value = 1;
406 tk_name[3] = 0;
407 def_text = "";
408 } else {
409 quick_widgets[INPUT_INDEX].value = 0;
412 #ifdef ENABLE_NLS
414 * An attempt to place buttons symmetrically, based on actual i18n
415 * length of the string. It looks nicer with i18n (IMO) - alex
417 quick_widgets[0].relative_x = len / 2 + 4;
418 quick_widgets[1].relative_x =
419 len / 2 - (strlen (_(quick_widgets[1].text)) + 9);
420 quick_widgets[0].x_divisions = quick_widgets[1].x_divisions = len;
421 #endif /* ENABLE_NLS */
423 Quick_input.xlen = len;
424 Quick_input.xpos = -1;
425 Quick_input.title = header;
426 Quick_input.help = help;
427 Quick_input.i18n = 0;
428 quick_widgets[INPUT_INDEX + 1].text = g_strstrip (g_strdup (text));
429 quick_widgets[INPUT_INDEX].text = def_text;
431 for (i = 0; i < 4; i++)
432 quick_widgets[i].y_divisions = lines + 6;
433 Quick_input.ylen = lines + 6;
435 for (i = 0; i < 3; i++)
436 quick_widgets[i].relative_y += 2 + lines;
438 quick_widgets[INPUT_INDEX].str_result = &my_str;
440 Quick_input.widgets = quick_widgets;
441 ret = quick_dialog (&Quick_input);
442 g_free (quick_widgets[INPUT_INDEX + 1].text);
444 if (ret != B_CANCEL) {
445 return *(quick_widgets[INPUT_INDEX].str_result);
446 } else
447 return 0;
450 char *input_dialog (char *header, char *text, char *def_text)
452 return input_dialog_help (header, text, "[Input Line Keys]", def_text);
455 char *input_expand_dialog (char *header, char *text, char *def_text)
457 char *result;
458 char *expanded;
460 result = input_dialog (header, text, def_text);
461 if (result){
462 expanded = tilde_expand (result);
463 if (expanded){
464 g_free (result);
465 return expanded;
468 return result;
471 /* }}} */
473 /* }}} */
475 Cause emacs to enter folding mode for this file:
476 Local variables:
477 end: