descriptions.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 
00040 #include "descriptions.h"
00041 
00042 #include <string.h>
00043 #include <gtk/gtk.h>
00044 
00045 #include "frontend.h"
00046 #include "question.h"
00047 #include "template.h"
00048 
00049 #include "fe_gtk.h"
00050 #include "fe_data.h"
00051 
00053 static const struct {
00055     const char * type;
00057     const char * path;
00058 } icon_mapping[] = {
00059     { "note",  BASE_IMAGE_PATH "/note_icon.png" },
00060     { "error", BASE_IMAGE_PATH "/warning_icon.png" },
00061     { NULL,    NULL }
00062 };
00063 
00070 static const char * get_icon_path(struct question * question)
00071 {
00072     int i;
00073 
00074     for (i = 0; NULL != icon_mapping[i].type; i++) {
00075         if (0 == strcmp(question->template->type, icon_mapping[i].type)) {
00076             return icon_mapping[i].path;
00077         }
00078     }
00079     return NULL;
00080 }
00081 
00087 static void add_icon(struct question * question, GtkWidget * container)
00088 {
00089     const char * icon_path;
00090     GtkWidget * icon_box;
00091     GtkWidget * icon_button = NULL;
00092 
00093     icon_path = get_icon_path(question);
00094     if (NULL == icon_path) {
00095         return;
00096     }
00097 
00098     icon_box = gtk_vbox_new(FALSE /* don't make children equal */,
00099                             0 /* no spacing */);
00100     icon_button = gtk_image_new_from_file(icon_path);
00101     gtk_box_pack_start(GTK_BOX(icon_box), icon_button,
00102                        FALSE /* don't expand */, FALSE /* don't fill */,
00103                        3 /* padding */);
00104     gtk_box_pack_start(GTK_BOX(container), icon_box,
00105                        FALSE /* don't expand */, FALSE /* don't fill */,
00106                        3 /* padding */);
00107 }
00108 
00114 static GdkColor * get_background_color(struct frontend * fe)
00115 {
00116     struct frontend_data * fe_data = fe->data;
00117     GtkStyle * style;
00118 
00119     style = gtk_widget_get_style(fe_data->window);
00120     return style->bg;
00121 }
00122 
00124 #define DESCRIPTION_MARGIN 4
00125 
00126 #define DESCRIPTION_VPADDING 3
00127 
00134 static void add_description(struct frontend * fe, struct question * question,
00135                             GtkWidget * container)
00136 {
00137     GtkWidget * view;
00138     GtkTextBuffer * buffer;
00139     GtkTextIter start;
00140     GtkTextIter end;
00141     char * description;
00142 
00143     description = q_get_description(question); 
00144     /* XXX: check NULL! */
00145     view = gtk_text_view_new();
00146     /* XXX: check NULL! */
00147     buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
00148     gtk_text_buffer_set_text(buffer, description, -1 /* until '\0' */);
00149     g_free(description);
00150     gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
00151     gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
00152     gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD_CHAR);
00153     gtk_text_view_set_left_margin(GTK_TEXT_VIEW(view), DESCRIPTION_MARGIN);
00154     gtk_text_view_set_right_margin(GTK_TEXT_VIEW(view), DESCRIPTION_MARGIN);
00155     gtk_text_buffer_create_tag(buffer, "italic", "style",
00156                                PANGO_STYLE_ITALIC, NULL);
00157     gtk_text_buffer_get_start_iter(buffer, &start);
00158     gtk_text_buffer_get_end_iter(buffer, &end);
00159     gtk_text_buffer_apply_tag_by_name(buffer, "italic", &start, &end);
00160     gtk_widget_modify_base(view, GTK_STATE_NORMAL, get_background_color(fe));
00161     gtk_box_pack_start(GTK_BOX(container), view,
00162                        FALSE /* don't expand */, FALSE /* don't fill */,
00163                        DESCRIPTION_VPADDING);
00164 }
00165 
00167 #define EXT_DESCRIPTION_VPADDING 2
00168 
00177 static void add_extended_description(struct frontend * fe,
00178                                      struct question * question,
00179                                      GtkWidget * container)
00180 {
00181     GtkTextBuffer * buffer;
00182     GtkWidget * view;
00183     char * ext_description;
00184 
00185     /* here is created the question's extended description, but only
00186      * if the question's extended description actually exists
00187      */
00188     ext_description = q_get_extended_description(question);
00189     if ('\0' != ext_description[0]) {
00190         view = gtk_text_view_new();
00191         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
00192         gtk_text_buffer_set_text(buffer, ext_description, -1);
00193         gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
00194         gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
00195         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD_CHAR);
00196         gtk_widget_modify_base(view, GTK_STATE_NORMAL,
00197                                get_background_color(fe));
00198         gtk_box_pack_start(GTK_BOX(container), view, FALSE /* don't expand */,
00199                            FALSE /* don't fill */, EXT_DESCRIPTION_VPADDING);
00200     }
00201     g_free(ext_description);
00202 }
00203 
00211 GtkWidget * fe_gtk_create_description(struct frontend * fe,
00212                                       struct question * question)
00213 {
00214     GtkWidget * returned_box;
00215     GtkWidget * description_box;
00216 
00217     returned_box = gtk_hbox_new(FALSE /* don't make children equal */,
00218                                 0 /* no spacing */);
00219     add_icon(question, returned_box);
00220 
00221     description_box = gtk_vbox_new(FALSE /* don't make children equal */,
00222                                    0 /* no spacing */);
00223     if (0 == strcmp(question->template->type, "note") ||
00224         0 == strcmp(question->template->type, "error")) {
00225         add_description(fe, question, description_box);
00226         add_extended_description(fe, question, description_box);
00227     } else {
00228         add_extended_description(fe, question, description_box);
00229         add_description(fe, question, description_box);
00230     }
00231     gtk_container_set_focus_chain(GTK_CONTAINER(description_box),
00232                                   NULL /* empty list */);
00233     gtk_box_pack_start(GTK_BOX(returned_box), description_box,
00234                        TRUE /* expand */, TRUE /* fill */, 3 /* padding */);
00235 
00236     return returned_box;
00237 }
00238 
00239 /* vim: et sw=4 si
00240  */ 

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