Debian Codegraph @

src/modules/frontend/gtk/descriptions.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 descriptions.c
35 * tools to display question descriptions for the GTK+ frontend of cdebconf
36 */
37
38 #include "descriptions.h"
39
40 #include <string.h>
41 #include <gtk/gtk.h>
42
43 #include "frontend.h"
44 #include "question.h"
45 #include "template.h"
46
47 #include "cdebconf_gtk.h"
48 #include "fe_data.h"
49
50 /** A mapping between question type and icons for descriptions. */
51 static const struct {
52 /** question type */
53 const char * type;
54 /** full path to the associated icon */
55 const char * path;
56 } icon_mapping[] = {
57 { "note", BASE_IMAGE_PATH "/note_icon.png" },
58 { "error", BASE_IMAGE_PATH "/warning_icon.png" },
59 { NULL, NULL }
60 };
61
62 /** Get the full path to an icon associated to a question type.
63 *
64 * @param question the question
65 * @return a statically allocated path to an icon or NULL if no icon is
66 * associated with the question
67 */
68 static const char * get_icon_path(struct question * question)
69 {
70 int i;
71
72 for (i = 0; NULL != icon_mapping[i].type; i++) {
73 if (0 == strcmp(question->template->type, icon_mapping[i].type)) {
74 return icon_mapping[i].path;
75 }
76 }
77 return NULL;
78 }
79
80 /** Add an icon for a given question in the given container.
81 *
82 * @param question the question
83 * @param container the container where the icon will be added
84 */
85 static void add_icon(struct question * question, GtkWidget * container)
86 {
87 const char * icon_path;
88 GtkWidget * icon_box;
89 GtkWidget * icon_button = NULL;
90
91 icon_path = get_icon_path(question);
92 if (NULL == icon_path) {
93 return;
94 }
95
96 icon_box = gtk_vbox_new(FALSE /* don't make children equal */,
97 0 /* no spacing */);
98 icon_button = gtk_image_new_from_file(icon_path);
99 gtk_box_pack_start(GTK_BOX(icon_box), icon_button,
100 FALSE /* don't expand */, FALSE /* don't fill */,
101 3 /* padding */);
102 gtk_box_pack_start(GTK_BOX(container), icon_box,
103 FALSE /* don't expand */, FALSE /* don't fill */,
104 3 /* padding */);
105 }
106
107 /** Get the background color for the main window of the frontend.
108 *
109 * @param fe cdebconf frontend
110 * @return the background color of the main window
111 */
112 static GdkColor * get_background_color(struct frontend * fe)
113 {
114 struct frontend_data * fe_data = fe->data;
115 GtkStyle * style;
116
117 style = gtk_widget_get_style(fe_data->window);
118 return style->bg;
119 }
120
121 /** margin around descriptions */
122 #define DESCRIPTION_MARGIN 4
123 /** vertical padding around descriptions */
124 #define DESCRIPTION_VPADDING 3
125
126 /** Make sure text views for the description are at least 300px wide.
127 *
128 * This works around Pango >= 1.44 sometimes behaving unexpectedly
129 * when asked to fit text into too small a space, leading to GTK 2 going
130 * into an infinite loop of redoing the layout (see #988787).
131 */
132 static void description_size_request_cb(GtkWidget * view,
133 GtkRequisition * requisition,
134 gpointer user_data)
135 {
136 requisition->width = MAX(requisition->width, 300);
137 }
138
139 /** Add a description to a given container.
140 *
141 * @param fe cdebconf frontend
142 * @param question the question from which the description will be read
143 * @param container the container where the description will be added
144 */
145 static void add_description(struct frontend * fe, struct question * question,
146 GtkWidget * container)
147 {
148 GtkWidget * view;
149 GtkTextBuffer * buffer;
150 GtkTextIter start;
151 GtkTextIter end;
152 char * description;
153
154 description = q_get_description(fe, question);
155 view = gtk_text_view_new();
156 g_signal_connect_after(view, "size-request",
157 G_CALLBACK(description_size_request_cb), NULL);
158 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
159 gtk_text_buffer_set_text(buffer, description, -1 /* until '\0' */);
160 g_free(description);
161 gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
162 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
163 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD_CHAR);
164 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(view), DESCRIPTION_MARGIN);
165 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(view), DESCRIPTION_MARGIN);
166 gtk_text_buffer_create_tag(buffer, "italic", "style",
167 PANGO_STYLE_ITALIC, NULL);
168 gtk_text_buffer_get_start_iter(buffer, &start);
169 gtk_text_buffer_get_end_iter(buffer, &end);
170 gtk_text_buffer_apply_tag_by_name(buffer, "italic", &start, &end);
171 gtk_widget_modify_base(view, GTK_STATE_NORMAL, get_background_color(fe));
172 gtk_box_pack_start(GTK_BOX(container), view,
173 FALSE /* don't expand */, FALSE /* don't fill */,
174 DESCRIPTION_VPADDING);
175 }
176
177 /** vertical padding around extendend description */
178 #define EXT_DESCRIPTION_VPADDING 2
179
180 /** Add an extendend description to a given container.
181 *
182 * @param fe cdebconf frontend
183 * @param question the question from which the extendend description will be
184 * read
185 * @param container the container where the extendend description will be
186 * added
187 */
188 static void add_extended_description(struct frontend * fe,
189 struct question * question,
190 GtkWidget * container)
191 {
192 GtkTextBuffer * buffer;
193 GtkWidget * view;
194 char * ext_description;
195
196 /* here is created the question's extended description, but only
197 * if the question's extended description actually exists
198 */
199 ext_description = q_get_extended_description(fe, question);
200 if ('\0' != ext_description[0]) {
201 view = gtk_text_view_new();
202 g_signal_connect_after(view, "size-request",
203 G_CALLBACK(description_size_request_cb), NULL);
204 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
205 gtk_text_buffer_set_text(buffer, ext_description, -1);
206 gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
207 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
208 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD_CHAR);
209 gtk_widget_modify_base(view, GTK_STATE_NORMAL,
210 get_background_color(fe));
211 gtk_box_pack_start(GTK_BOX(container), view, FALSE /* don't expand */,
212 FALSE /* don't fill */, EXT_DESCRIPTION_VPADDING);
213 }
214 g_free(ext_description);
215 }
216
217 /** Return a new widget with description and extendend description of a
218 * given question.
219 *
220 * @param fe cdebconf frontend
221 * @param question question that will be looked upon
222 * @return a newly created widget or NULL on failure
223 */
224 GtkWidget * cdebconf_gtk_create_description(struct frontend * fe,
225 struct question * question)
226 {
227 GtkWidget * returned_box;
228 GtkWidget * description_box;
229
230 returned_box = gtk_hbox_new(FALSE /* don't make children equal */,
231 0 /* no spacing */);
232 add_icon(question, returned_box);
233
234 description_box = gtk_vbox_new(FALSE /* don't make children equal */,
235 0 /* no spacing */);
236 if (0 == strcmp(question->template->type, "note") ||
237 0 == strcmp(question->template->type, "error")) {
238 add_description(fe, question, description_box);
239 add_extended_description(fe, question, description_box);
240 } else {
241 add_extended_description(fe, question, description_box);
242 add_description(fe, question, description_box);
243 }
244 gtk_container_set_focus_chain(GTK_CONTAINER(description_box),
245 NULL /* empty list */);
246 gtk_box_pack_start(GTK_BOX(returned_box), description_box,
247 TRUE /* expand */, TRUE /* fill */, 3 /* padding */);
248
249 return returned_box;
250 }
251
252 /* vim: et sw=4 si
253 */