Ticket #2760: The password for session ftp remains in input history
[midnight-commander.git] / lib / widget / quick.c
blobc6b415a23cbd2c8b268bc1f4033e4e268a15e09e
1 /*
2 Widget based utility functions.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2008, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 Authors:
9 Miguel de Icaza, 1994, 1995, 1996
10 Radek Doulik, 1994, 1995
11 Jakub Jelinek, 1995
12 Andrej Borsenkow, 1995
13 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
15 This file is part of the Midnight Commander.
17 The Midnight Commander is free software: you can redistribute it
18 and/or modify it under the terms of the GNU General Public License as
19 published by the Free Software Foundation, either version 3 of the License,
20 or (at your option) any later version.
22 The Midnight Commander is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 /** \file quick.c
32 * \brief Source: quick dialog engine
35 #include <config.h>
37 #include <stdlib.h>
38 #include <stdio.h> /* fprintf() */
40 #include "lib/global.h"
41 #include "lib/util.h" /* tilde_expand() */
42 #include "lib/widget.h"
44 /*** global variables ****************************************************************************/
46 /*** file scope macro definitions ****************************************************************/
48 /*** file scope type declarations ****************************************************************/
50 /*** file scope variables ************************************************************************/
52 /*** file scope functions ************************************************************************/
54 /* --------------------------------------------------------------------------------------------- */
55 /*** public functions ****************************************************************************/
56 /* --------------------------------------------------------------------------------------------- */
58 int
59 quick_dialog_skip (QuickDialog * qd, int nskip)
61 #ifdef ENABLE_NLS
62 #define I18N(x) (x = !qd->i18n && x != NULL && *x != '\0' ? _(x): x)
63 #else
64 #define I18N(x) (x = x)
65 #endif
66 Dlg_head *dd;
67 QuickWidget *qw;
68 WInput *in;
69 WRadio *r;
70 int return_val;
72 I18N (qd->title);
74 if ((qd->xpos == -1) || (qd->ypos == -1))
75 dd = create_dlg (TRUE, 0, 0, qd->ylen, qd->xlen,
76 dialog_colors, qd->callback, qd->help, qd->title,
77 DLG_CENTER | DLG_TRYUP | DLG_REVERSE);
78 else
79 dd = create_dlg (TRUE, qd->ypos, qd->xpos, qd->ylen, qd->xlen,
80 dialog_colors, qd->callback, qd->help, qd->title, DLG_REVERSE);
82 for (qw = qd->widgets; qw->widget_type != quick_end; qw++)
84 int xpos;
85 int ypos;
87 xpos = (qd->xlen * qw->relative_x) / qw->x_divisions;
88 ypos = (qd->ylen * qw->relative_y) / qw->y_divisions;
90 switch (qw->widget_type)
92 case quick_checkbox:
93 qw->widget =
94 (Widget *) check_new (ypos, xpos, *qw->u.checkbox.state,
95 I18N (qw->u.checkbox.text));
96 break;
98 case quick_button:
99 qw->widget = (Widget *) button_new (ypos, xpos, qw->u.button.action,
100 (qw->u.button.action ==
101 B_ENTER) ? DEFPUSH_BUTTON : NORMAL_BUTTON,
102 I18N (qw->u.button.text), qw->u.button.callback);
103 break;
105 case quick_input:
106 in = input_new (ypos, xpos, input_get_default_colors (),
107 qw->u.input.len, qw->u.input.text, qw->u.input.histname,
108 INPUT_COMPLETE_DEFAULT);
109 in->is_password = (qw->u.input.flags == 1);
110 if ((qw->u.input.flags & 2) != 0)
111 in->completion_flags |= INPUT_COMPLETE_CD;
112 if ((qw->u.input.flags & 4) != 0)
113 in->strip_password = TRUE;
114 qw->widget = (Widget *) in;
115 *qw->u.input.result = NULL;
116 break;
118 case quick_label:
119 qw->widget = (Widget *) label_new (ypos, xpos, I18N (qw->u.label.text));
120 break;
122 case quick_groupbox:
123 qw->widget = (Widget *) groupbox_new (ypos, xpos,
124 qw->u.groupbox.height,
125 qw->u.groupbox.width,
126 I18N (qw->u.groupbox.title));
127 break;
129 case quick_radio:
131 int i;
132 char **items = NULL;
134 /* create the copy of radio_items to avoid mwmory leak */
135 items = g_new0 (char *, qw->u.radio.count + 1);
137 if (!qd->i18n)
138 for (i = 0; i < qw->u.radio.count; i++)
139 items[i] = g_strdup (_(qw->u.radio.items[i]));
140 else
141 for (i = 0; i < qw->u.radio.count; i++)
142 items[i] = g_strdup (qw->u.radio.items[i]);
144 r = radio_new (ypos, xpos, qw->u.radio.count, (const char **) items);
145 r->pos = r->sel = *qw->u.radio.value;
146 qw->widget = (Widget *) r;
147 g_strfreev (items);
148 break;
151 default:
152 qw->widget = NULL;
153 fprintf (stderr, "QuickWidget: unknown widget type\n");
154 break;
157 if (qw->widget != NULL)
159 qw->widget->options |= qw->options; /* FIXME: cannot reset flags, setup only */
160 add_widget (dd, qw->widget);
164 while (nskip-- != 0)
166 dd->current = g_list_next (dd->current);
167 if (dd->current == NULL)
168 dd->current = dd->widgets;
171 return_val = run_dlg (dd);
173 /* Get the data if we found something interesting */
174 if (return_val != B_CANCEL)
176 for (qw = qd->widgets; qw->widget_type != quick_end; qw++)
178 switch (qw->widget_type)
180 case quick_checkbox:
181 *qw->u.checkbox.state = ((WCheck *) qw->widget)->state & C_BOOL;
182 break;
184 case quick_input:
185 if ((qw->u.input.flags & 2) != 0)
186 *qw->u.input.result = tilde_expand (((WInput *) qw->widget)->buffer);
187 else
188 *qw->u.input.result = g_strdup (((WInput *) qw->widget)->buffer);
189 break;
191 case quick_radio:
192 *qw->u.radio.value = ((WRadio *) qw->widget)->sel;
193 break;
195 default:
196 break;
201 destroy_dlg (dd);
203 return return_val;
204 #undef I18N
207 /* --------------------------------------------------------------------------------------------- */