Debian Codegraph @

src/modules/frontend/gtk/handlers.c

raw
1 /*****************************************************************************
2 *
3 * cdebconf - An implementation of the Debian Configuration Management
4 * System
5 *
6 * cdebconf is (c) 2000-2007 Randolph Chung and others under the following
7 * license.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *****************************************************************************/
33
34 /** @file handlers.c
35 * question handlers of the GTK+ frontend
36 *
37 * @see select_handlers.c
38 */
39
40 #include "handlers.h"
41
42 #include <string.h>
43 #include <gtk/gtk.h>
44
45 #include "frontend.h"
46 #include "question.h"
47
48 #include "cdebconf_gtk.h"
49 #include "fe_data.h"
50 #include "ui.h"
51
52 /** Update a boolean question value from the status of a toggle button.
53 *
54 * @param question question to set
55 * @param toggle_button associated toggle button
56 */
57 static void set_value_from_toggle_button(struct question * question,
58 GtkToggleButton * toggle_button)
59 {
60 gboolean check_value;
61
62 check_value = gtk_toggle_button_get_active(toggle_button);
63 question_setvalue(question, check_value ? "true" : "false");
64 }
65
66 /** The handler for boolean questions implemented as radio buttons.
67 *
68 * This handler is used when multiple questions are used in a row.
69 *
70 * @param fe cdebconf frontend
71 * @param question question being handled
72 * @param question_box container for question widgets
73 * @return DC_NOTOK if an error happened, DC_OK otherwise
74 */
75 static int handle_boolean_radio(struct frontend * fe,
76 struct question * question,
77 GtkWidget * question_box)
78 {
79 GtkWidget * radio_false;
80 GtkWidget * radio_true;
81 GtkWidget * container;
82 char * false_label;
83 char * true_label;
84 const char * value;
85
86 false_label = cdebconf_gtk_get_text(fe, "debconf/no", "No");
87 radio_false = gtk_radio_button_new_with_label(
88 NULL /* new group */, false_label);
89 g_free(false_label);
90
91 true_label = cdebconf_gtk_get_text(fe, "debconf/yes", "Yes");
92 radio_true = gtk_radio_button_new_with_label_from_widget(
93 GTK_RADIO_BUTTON(radio_false), true_label);
94 g_free(true_label);
95
96 value = question_getvalue(question, "");
97 if (NULL != value && 0 == strcmp(value, "true")) {
98 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio_true), TRUE);
99 } else {
100 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio_true), FALSE);
101 }
102
103 container = gtk_vbox_new(FALSE /* don't make children equal */,
104 DEFAULT_PADDING);
105 gtk_box_pack_start(GTK_BOX(container), radio_false,
106 FALSE /* don't expand */, FALSE /* don't fill */,
107 0 /* padding */);
108 gtk_box_pack_start(GTK_BOX(container), radio_true,
109 FALSE /* don't expand */, FALSE /* don't fill */,
110 0 /* padding */);
111 cdebconf_gtk_add_common_layout(fe, question, question_box, container);
112
113 if (cdebconf_gtk_is_first_question(question)) {
114 if (NULL != value && 0 == strcmp(value, "true")) {
115 gtk_widget_grab_focus(radio_true);
116 } else {
117 gtk_widget_grab_focus(radio_false);
118 }
119 }
120
121 cdebconf_gtk_register_setter(
122 fe, SETTER_FUNCTION(set_value_from_toggle_button), question,
123 radio_true);
124
125 return DC_OK;
126 }
127
128 /** Handler for question of boolean type.
129 *
130 * This will register the corresponding setter.
131 *
132 * @param fe cdebconf frontend
133 * @param question handled question
134 * @param question_box container for handler's widgets
135 * @return DC_OK if everything is fine, DC_NOTOK otherwise
136 */
137 int cdebconf_gtk_handle_boolean(struct frontend * fe,
138 struct question * question,
139 GtkWidget * question_box)
140 {
141 return handle_boolean_radio(fe, question, question_box);
142 }
143
144 /** Handler for "question" of note type.
145 *
146 * @param fe cdebconf frontend
147 * @param question handled question
148 * @param question_box container for handler's widgets
149 * @return DC_OK if everything is fine, DC_NOTOK otherwise
150 */
151 int cdebconf_gtk_handle_note(struct frontend * fe, struct question * question,
152 GtkWidget * question_box)
153 {
154 /* Let's use an empty container... */
155 cdebconf_gtk_add_common_layout(fe, question, question_box,
156 gtk_vbox_new(FALSE, DEFAULT_PADDING));
157 return DC_OK;
158 }
159
160 /** Handler for "question" of text type.
161 *
162 * This currently just calls cdebconf_gtk_handle_note()
163 *
164 * @param fe cdebconf frontend
165 * @param question handled question
166 * @param question_box container for handler's widgets
167 * @return DC_OK if everything is fine, DC_NOTOK otherwise
168 */
169 int cdebconf_gtk_handle_text(struct frontend * fe, struct question * question,
170 GtkWidget * question_box)
171 {
172 return cdebconf_gtk_handle_note(fe, question, question_box);
173 }
174
175 /** Setter function for handler based on GtkEntry.
176 *
177 * @param question handled question
178 * @param entry entry where the value will be read
179 * @see setter_function
180 */
181 static void set_value_from_entry(struct question * question, GtkEntry * entry)
182 {
183 question_setvalue(question, gtk_entry_get_text(entry));
184 }
185
186 /** Create a GtkAlignment widget to correctly align entry widget inside the
187 * question box.
188 *
189 * @param entry an entry widget
190 * @return a widget containing the entry
191 */
192 static GtkWidget * create_entry_alignment(GtkWidget * entry)
193 {
194 GtkWidget * alignment;
195
196 alignment = gtk_alignment_new(0.0 /* left */, 0.0 /* top */,
197 1.0 /* expand horizontally */,
198 0.0 /* no vertical expansion */);
199 gtk_container_add(GTK_CONTAINER(alignment), entry);
200 return alignment;
201 }
202
203 static void checkbox_toggled(GtkToggleButton *togglebutton, gpointer user_data)
204 {
205 gtk_entry_set_visibility(GTK_ENTRY(user_data), gtk_toggle_button_get_active(togglebutton));
206 }
207
208 /** Handler for question of password type.
209 *
210 * @param fe cdebconf frontend
211 * @param question handled question
212 * @param question_box container for handler's widgets
213 * @return DC_OK if everything is fine, DC_NOTOK otherwise
214 */
215 int cdebconf_gtk_handle_password(struct frontend * fe,
216 struct question * question,
217 GtkWidget * question_box)
218 {
219 GtkWidget * entry, * vbox, *checkbox;
220
221 /* INFO(INFO_DEBUG, "GTK_DI - gtkhandler_password() called"); */
222
223 entry = gtk_entry_new();
224 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE /* password style */);
225 gtk_entry_set_activates_default(GTK_ENTRY(entry),
226 TRUE /* activate on Enter */);
227
228
229 vbox = gtk_vbox_new(TRUE, 1);
230 checkbox = gtk_check_button_new_with_label(cdebconf_gtk_get_text(fe, "debconf/show-password", "Show Password in Clear"));
231 g_signal_connect (checkbox, "toggled", G_CALLBACK (checkbox_toggled), entry);
232 gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
233 gtk_box_pack_start(GTK_BOX(vbox), checkbox, FALSE, FALSE, 0);
234
235 cdebconf_gtk_add_common_layout(fe, question, question_box,
236 create_entry_alignment(vbox));
237
238 if (cdebconf_gtk_is_first_question(question)) {
239 gtk_widget_grab_focus(entry);
240 }
241
242 cdebconf_gtk_register_setter(fe, SETTER_FUNCTION(set_value_from_entry),
243 question, entry);
244
245 return DC_OK;
246 }
247
248 /** Handler for question of string type.
249 *
250 * @param fe cdebconf frontend
251 * @param question handled question
252 * @param question_box container for handler's widgets
253 * @return DC_OK if everything is fine, DC_NOTOK otherwise
254 */
255 int cdebconf_gtk_handle_string(struct frontend * fe,
256 struct question * question,
257 GtkWidget * question_box)
258 {
259 GtkWidget * entry;
260 const char * defval;
261
262 defval = question_getvalue(question, "");
263
264 entry = gtk_entry_new();
265 gtk_entry_set_text(GTK_ENTRY(entry), NULL != defval ? defval : "");
266 gtk_entry_set_activates_default(
267 GTK_ENTRY(entry), TRUE /* activate on Enter key */);
268
269 cdebconf_gtk_add_common_layout(fe, question, question_box,
270 create_entry_alignment(entry));
271
272 if (cdebconf_gtk_is_first_question(question)) {
273 gtk_widget_grab_focus(entry);
274 }
275
276 cdebconf_gtk_register_setter(fe, SETTER_FUNCTION(set_value_from_entry),
277 question, entry);
278
279 return DC_OK;
280 }
281
282 /* vim: et sw=4 si
283 */