fixed relative symlink operations. Symlink now stay relative
[midnight-commander.git] / lib / widget / quick.c
blob5be6df1ba231cdff59ad9c742117c4d967392095
1 /* Widget based utility functions.
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 Authors: 1994, 1995, 1996 Miguel de Icaza
6 1994, 1995 Radek Doulik
7 1995 Jakub Jelinek
8 1995 Andrej Borsenkow
9 2009, 2010 Andrew Borodin
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 /** \file quick.c
28 * \brief Source: quick dialog engine
31 #include <config.h>
33 #include <stdlib.h>
34 #include <stdio.h> /* fprintf() */
36 #include "lib/global.h"
37 #include "lib/util.h" /* tilde_expand() */
38 #include "lib/widget.h"
40 /*** global variables ****************************************************************************/
42 /*** file scope macro definitions ****************************************************************/
44 /*** file scope type declarations ****************************************************************/
46 /*** file scope variables ************************************************************************/
48 /*** file scope functions ************************************************************************/
50 /* --------------------------------------------------------------------------------------------- */
51 /*** public functions ****************************************************************************/
52 /* --------------------------------------------------------------------------------------------- */
54 int
55 quick_dialog_skip (QuickDialog * qd, int nskip)
57 #ifdef ENABLE_NLS
58 #define I18N(x) (x = !qd->i18n && x != NULL && *x != '\0' ? _(x): x)
59 #else
60 #define I18N(x) (x = x)
61 #endif
62 Dlg_head *dd;
63 QuickWidget *qw;
64 WInput *in;
65 WRadio *r;
66 int return_val;
68 I18N (qd->title);
70 if ((qd->xpos == -1) || (qd->ypos == -1))
71 dd = create_dlg (TRUE, 0, 0, qd->ylen, qd->xlen,
72 dialog_colors, qd->callback, qd->help, qd->title,
73 DLG_CENTER | DLG_TRYUP | DLG_REVERSE);
74 else
75 dd = create_dlg (TRUE, qd->ypos, qd->xpos, qd->ylen, qd->xlen,
76 dialog_colors, qd->callback, qd->help, qd->title, DLG_REVERSE);
78 for (qw = qd->widgets; qw->widget_type != quick_end; qw++)
80 int xpos;
81 int ypos;
83 xpos = (qd->xlen * qw->relative_x) / qw->x_divisions;
84 ypos = (qd->ylen * qw->relative_y) / qw->y_divisions;
86 switch (qw->widget_type)
88 case quick_checkbox:
89 qw->widget =
90 (Widget *) check_new (ypos, xpos, *qw->u.checkbox.state,
91 I18N (qw->u.checkbox.text));
92 break;
94 case quick_button:
95 qw->widget = (Widget *) button_new (ypos, xpos, qw->u.button.action,
96 (qw->u.button.action ==
97 B_ENTER) ? DEFPUSH_BUTTON : NORMAL_BUTTON,
98 I18N (qw->u.button.text), qw->u.button.callback);
99 break;
101 case quick_input:
102 in = input_new (ypos, xpos, input_get_default_colors (),
103 qw->u.input.len, qw->u.input.text, qw->u.input.histname,
104 INPUT_COMPLETE_DEFAULT);
105 in->is_password = (qw->u.input.flags == 1);
106 if ((qw->u.input.flags & 2) != 0)
107 in->completion_flags |= INPUT_COMPLETE_CD;
108 qw->widget = (Widget *) in;
109 *qw->u.input.result = NULL;
110 break;
112 case quick_label:
113 qw->widget = (Widget *) label_new (ypos, xpos, I18N (qw->u.label.text));
114 break;
116 case quick_groupbox:
117 qw->widget = (Widget *) groupbox_new (ypos, xpos,
118 qw->u.groupbox.height,
119 qw->u.groupbox.width,
120 I18N (qw->u.groupbox.title));
121 break;
123 case quick_radio:
125 int i;
126 char **items = NULL;
128 /* create the copy of radio_items to avoid mwmory leak */
129 items = g_new0 (char *, qw->u.radio.count + 1);
131 if (!qd->i18n)
132 for (i = 0; i < qw->u.radio.count; i++)
133 items[i] = g_strdup (_(qw->u.radio.items[i]));
134 else
135 for (i = 0; i < qw->u.radio.count; i++)
136 items[i] = g_strdup (qw->u.radio.items[i]);
138 r = radio_new (ypos, xpos, qw->u.radio.count, (const char **) items);
139 r->pos = r->sel = *qw->u.radio.value;
140 qw->widget = (Widget *) r;
141 g_strfreev (items);
142 break;
145 default:
146 qw->widget = NULL;
147 fprintf (stderr, "QuickWidget: unknown widget type\n");
148 break;
151 if (qw->widget != NULL)
153 qw->widget->options |= qw->options; /* FIXME: cannot reset flags, setup only */
154 add_widget (dd, qw->widget);
158 while (nskip-- != 0)
160 dd->current = g_list_next (dd->current);
161 if (dd->current == NULL)
162 dd->current = dd->widgets;
165 return_val = run_dlg (dd);
167 /* Get the data if we found something interesting */
168 if (return_val != B_CANCEL)
170 for (qw = qd->widgets; qw->widget_type != quick_end; qw++)
172 switch (qw->widget_type)
174 case quick_checkbox:
175 *qw->u.checkbox.state = ((WCheck *) qw->widget)->state & C_BOOL;
176 break;
178 case quick_input:
179 if ((qw->u.input.flags & 2) != 0)
180 *qw->u.input.result = tilde_expand (((WInput *) qw->widget)->buffer);
181 else
182 *qw->u.input.result = g_strdup (((WInput *) qw->widget)->buffer);
183 break;
185 case quick_radio:
186 *qw->u.radio.value = ((WRadio *) qw->widget)->sel;
187 break;
189 default:
190 break;
195 destroy_dlg (dd);
197 return return_val;
198 #undef I18N
201 /* --------------------------------------------------------------------------------------------- */