handlers.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002  *
00003  * cdebconf - An implementation of the Debian Configuration Management
00004  *            System
00005  *
00006  * $Id$
00007  *
00008  * cdebconf is (c) 2000-2007 Randolph Chung and others under the following
00009  * license.
00010  *
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions
00013  * are met:
00014  *
00015  * 1. Redistributions of source code must retain the above copyright
00016  * notice, this list of conditions and the following disclaimer.
00017  *
00018  * 2. Redistributions in binary form must reproduce the above copyright
00019  * notice, this list of conditions and the following disclaimer in the
00020  * documentation and/or other materials provided with the distribution.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  *
00034  *****************************************************************************/
00035 
00042 #include "handlers.h"
00043 
00044 #include <string.h>
00045 #include <gtk/gtk.h>
00046 
00047 #include "frontend.h"
00048 #include "question.h"
00049 
00050 #include "fe_gtk.h"
00051 #include "fe_data.h"
00052 #include "ui.h"
00053 
00059 static void set_value_from_toggle_button(struct question * question,
00060                                          GtkToggleButton * toggle_button)
00061 {
00062     gboolean check_value;
00063 
00064     check_value = gtk_toggle_button_get_active(toggle_button);
00065     question_setvalue(question, check_value ? "true" : "false");
00066 }
00067 
00077 static int handle_boolean_radio(struct frontend * fe,
00078                                 struct question * question,
00079                                 GtkWidget * question_box)
00080 {
00081     GtkWidget * radio_false;
00082     GtkWidget * radio_true;
00083     GtkWidget * container;
00084     char * false_label;
00085     char * true_label;
00086     
00087     false_label = fe_gtk_get_text(fe, "debconf/no", "No");
00088     radio_false = gtk_radio_button_new_with_label(
00089         NULL /* new group */, false_label);
00090     g_free(false_label);
00091 
00092     true_label = fe_gtk_get_text(fe, "debconf/yes", "Yes");
00093     radio_true = gtk_radio_button_new_with_label_from_widget(
00094         GTK_RADIO_BUTTON(radio_false), true_label);
00095     g_free(true_label);
00096 
00097     if (0 == strcmp(question_getvalue(question, ""), "true")) {
00098         /* XXX: only one needed? */
00099         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio_false), FALSE);
00100         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio_true), TRUE);
00101     } else {
00102         /* XXX: only one needed? */
00103         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio_false), TRUE);
00104         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio_true), FALSE);
00105     }
00106 
00107     container = gtk_vbox_new(FALSE /* don't make children equal */,
00108                              DEFAULT_PADDING);
00109     gtk_box_pack_start(GTK_BOX(container), radio_false,
00110                        FALSE /* don't expand */, FALSE /* don't fill */,
00111                        0 /* padding */);
00112     gtk_box_pack_start(GTK_BOX(container), radio_true,
00113                        FALSE /* don't expand */, FALSE /* don't fill */,
00114                        0 /* padding */);
00115     fe_gtk_add_common_layout(fe, question, question_box, container);
00116 
00117     if (fe_gtk_is_first_question(question)) {
00118         if (0 == strcmp(question_getvalue(question, ""), "true")) {
00119             gtk_widget_grab_focus(radio_true);
00120         } else {
00121             gtk_widget_grab_focus(radio_false);
00122         }
00123     }
00124 
00125     fe_gtk_register_setter(fe, SETTER_FUNCTION(set_value_from_toggle_button),
00126                            question, radio_true);
00127 
00128     return DC_OK;
00129 }
00130 
00139 static void handle_false_button(GtkWidget * widget, struct frontend * fe)
00140 {
00141     struct question * question = fe->questions;
00142 
00143     question_setvalue(question, "false");
00144     fe_gtk_set_answer_ok(fe);
00145 }
00146 
00155 static void handle_true_button(GtkWidget * widget, struct frontend * fe)
00156 {
00157     struct question * question = fe->questions;
00158 
00159     question_setvalue(question, "true");
00160     fe_gtk_set_answer_ok(fe);
00161 }
00162 
00173 static int handle_boolean_buttons(struct frontend * fe,
00174                                   struct question * question,
00175                                   GtkWidget * question_box)
00176 {
00177     GtkWidget * false_button;
00178     GtkWidget * true_button;
00179     char * label;
00180 
00181     /* show the descriptions */
00182     fe_gtk_add_common_layout(fe, question, question_box,
00183                              gtk_vbox_new(0, FALSE));
00184 
00185     label = fe_gtk_get_text(fe, "debconf/no", "_No");
00186     /* check NULL! */
00187     false_button = gtk_button_new_with_mnemonic(label);
00188     g_free(label);
00189 
00190     label = fe_gtk_get_text(fe, "debconf/yes", "_Yes");
00191     /* check NULL! */
00192     true_button = gtk_button_new_with_mnemonic(label);
00193     g_free(label);
00194 
00195     g_signal_connect(G_OBJECT(false_button), "clicked",
00196                      G_CALLBACK(handle_false_button), fe);
00197     g_signal_connect(G_OBJECT(true_button), "clicked",
00198                      G_CALLBACK(handle_true_button), fe);
00199     fe_gtk_add_button(fe, false_button);
00200     fe_gtk_add_button(fe, true_button);
00201 
00202     if (0 == strcmp(question_getvalue(question, ""), "true")) {
00203         GTK_WIDGET_SET_FLAGS(true_button, GTK_CAN_DEFAULT);
00204         gtk_widget_grab_default(true_button);
00205     } else {
00206         GTK_WIDGET_SET_FLAGS(false_button, GTK_CAN_DEFAULT);
00207         gtk_widget_grab_default(false_button);
00208     }
00209     return DC_OK;
00210 }
00211 
00221 int fe_gtk_handle_boolean(struct frontend * fe, struct question * question,
00222                           GtkWidget * question_box)
00223 {
00224     if (IS_QUESTION_SINGLE(question)) {
00225         return handle_boolean_buttons(fe, question, question_box);
00226     } else {
00227         return handle_boolean_radio(fe, question, question_box);
00228     }
00229 }
00230 
00238 int fe_gtk_handle_note(struct frontend * fe, struct question * question,
00239                        GtkWidget * question_box)
00240 {
00241     /* Let's use an empty container... */
00242     fe_gtk_add_common_layout(fe, question, question_box,
00243                              gtk_vbox_new(FALSE, DEFAULT_PADDING));
00244     return DC_OK;
00245 }
00246 
00256 int fe_gtk_handle_text(struct frontend * fe, struct question * question,
00257                        GtkWidget * question_box)
00258 {
00259     return fe_gtk_handle_note(fe, question, question_box);
00260 }
00261 
00268 static void set_value_from_entry(struct question * question, GtkEntry * entry)
00269 {
00270     question_setvalue(question, gtk_entry_get_text(entry));
00271 }
00272 
00279 static GtkWidget * create_entry_alignment(GtkWidget * entry)
00280 {
00281     GtkWidget * alignment;
00282 
00283     /* check NULL! */
00284     alignment = gtk_alignment_new(0.0 /* left */, 0.0 /* top */,
00285                                   1.0 /* expand horizontally */,
00286                                   0.0 /* no vertical expansion */);
00287     gtk_container_add(GTK_CONTAINER(alignment), entry);
00288     return alignment;
00289 }
00290 
00298 int fe_gtk_handle_password(struct frontend * fe, struct question * question,
00299                            GtkWidget * question_box)
00300 {
00301     GtkWidget * entry;
00302 
00303     /* INFO(INFO_DEBUG, "GTK_DI - gtkhandler_password() called"); */
00304 
00305     entry = gtk_entry_new();
00306     gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE /* password style */);
00307     gtk_entry_set_activates_default(GTK_ENTRY(entry),
00308                                     TRUE /* activate on Enter */);
00309 
00310     fe_gtk_add_common_layout(fe, question, question_box,
00311                              create_entry_alignment(entry));
00312 
00313     if (fe_gtk_is_first_question(question)) {
00314         gtk_widget_grab_focus(entry);
00315     }
00316 
00317     fe_gtk_register_setter(fe, SETTER_FUNCTION(set_value_from_entry),
00318                            question, entry);
00319 
00320     return DC_OK;
00321 }
00322 
00330 int fe_gtk_handle_string(struct frontend * fe, struct question * question,
00331                          GtkWidget * question_box)
00332 {
00333     GtkWidget * entry;
00334     const char * defval;
00335    
00336     defval = question_getvalue(question, "");
00337 
00338     entry = gtk_entry_new();
00339     gtk_entry_set_text(GTK_ENTRY(entry), NULL != defval ? defval : "");
00340     gtk_entry_set_activates_default(
00341         GTK_ENTRY(entry), TRUE /* activate on Enter key */);
00342 
00343     fe_gtk_add_common_layout(fe, question, question_box,
00344                              create_entry_alignment(entry));
00345 
00346     if (fe_gtk_is_first_question(question)) {
00347         gtk_widget_grab_focus(entry);
00348     }
00349 
00350     fe_gtk_register_setter(fe, SETTER_FUNCTION(set_value_from_entry),
00351                            question, entry);
00352 
00353     return DC_OK;
00354 }
00355 
00356 

Generated on Sat Jul 7 23:41:41 2007 for fe_gtk by  doxygen 1.5.1