00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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 ,
00099 0 );
00100 icon_button = gtk_image_new_from_file(icon_path);
00101 gtk_box_pack_start(GTK_BOX(icon_box), icon_button,
00102 FALSE , FALSE ,
00103 3 );
00104 gtk_box_pack_start(GTK_BOX(container), icon_box,
00105 FALSE , FALSE ,
00106 3 );
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
00145 view = gtk_text_view_new();
00146
00147 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
00148 gtk_text_buffer_set_text(buffer, description, -1 );
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 , FALSE ,
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
00186
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 ,
00199 FALSE , 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 ,
00218 0 );
00219 add_icon(question, returned_box);
00220
00221 description_box = gtk_vbox_new(FALSE ,
00222 0 );
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 );
00233 gtk_box_pack_start(GTK_BOX(returned_box), description_box,
00234 TRUE , TRUE , 3 );
00235
00236 return returned_box;
00237 }
00238
00239
00240