Debian Codegraph @

src/modules/frontend/gtk/align_text_renderer.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 align_text_renderer.c
35 * GtkCellRenderer for text aligned with tab stops
36 */
37
38 #include "align_text_renderer.h"
39
40 #include "cdebconf_gtk.h"
41
42 enum {
43 PROP_0,
44
45 PROP_TEXT
46 };
47
48 static gpointer cdebconf_gtk_align_text_renderer_parent_class;
49
50 static void cdebconf_gtk_align_text_renderer_init(AlignTextRenderer * renderer,
51 gpointer klass)
52 {
53 gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 0.0, 0.5);
54 gtk_cell_renderer_set_padding(GTK_CELL_RENDERER(renderer), 2, 2);
55 renderer->tab_array = NULL;
56 renderer->align_center_glyph = 0;
57 renderer->align_right_glyph = 0;
58 }
59
60 static void align_text_renderer_finalize(GObject * object)
61 {
62 AlignTextRenderer * renderer = ALIGN_TEXT_RENDERER(object);
63
64 if (NULL != renderer->text) {
65 g_free(renderer->text);
66 }
67 if (NULL != renderer->tab_array) {
68 pango_tab_array_free(renderer->tab_array);
69 }
70
71 (* G_OBJECT_CLASS(
72 cdebconf_gtk_align_text_renderer_parent_class)->finalize)(object);
73 }
74
75 static void align_text_renderer_get_property(
76 GObject * object, guint param_id, GValue * value,
77 GParamSpec * param_spec)
78 {
79 AlignTextRenderer * renderer = ALIGN_TEXT_RENDERER(object);
80
81 switch (param_id) {
82 case PROP_TEXT:
83 g_value_set_string(value, renderer->text);
84 break;
85
86 default:
87 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, param_spec);
88 break;
89 }
90 }
91
92 static void align_text_renderer_set_property(
93 GObject * object, guint param_id, const GValue * value,
94 GParamSpec * param_spec)
95 {
96 AlignTextRenderer * renderer = ALIGN_TEXT_RENDERER(object);
97
98 switch (param_id) {
99 case PROP_TEXT:
100 if (NULL != renderer->text) {
101 g_free(renderer->text);
102 }
103 renderer->text = g_strdup(g_value_get_string(value));
104 g_object_notify(object, "text");
105 break;
106
107 default:
108 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, param_spec);
109 break;
110 }
111 }
112
113 void cdebconf_gtk_align_text_renderer_set_tab_array(
114 AlignTextRenderer * renderer, PangoTabArray * tab_array)
115 {
116 if (NULL != renderer->tab_array) {
117 pango_tab_array_free(renderer->tab_array);
118 }
119 renderer->tab_array = pango_tab_array_copy(tab_array);
120 }
121
122 GtkCellRenderer * cdebconf_gtk_align_text_renderer_new(void)
123 {
124 return g_object_new(TYPE_ALIGN_TEXT_RENDERER, NULL);
125 }
126
127 static void handle_right_alignment(AlignTextRenderer * renderer,
128 PangoLayout * layout,
129 const GdkRectangle * cell_area)
130 {
131 PangoLayoutIter * iter;
132 PangoLayoutRun * run;
133 PangoGlyphInfo * current;
134 PangoGlyphInfo * last_spacer;
135 enum {
136 ALIGN_LEFT = 0,
137 ALIGN_CENTER,
138 ALIGN_RIGHT
139 } align_mode;
140 PangoRectangle rect;
141 int i;
142
143 last_spacer = NULL;
144 align_mode = ALIGN_LEFT;
145 iter = pango_layout_get_iter(layout);
146 do {
147 if (NULL != (run = pango_layout_iter_get_run(iter))) {
148 for (i = 0; i < run->glyphs->num_glyphs; i++) {
149 current = &run->glyphs->glyphs[i];
150 if (PANGO_GLYPH_EMPTY == current->glyph) {
151 if (NULL != last_spacer) {
152 if (ALIGN_CENTER == align_mode) {
153 last_spacer->geometry.width +=
154 (current->geometry.width
155 - last_spacer->geometry.width
156 - COLUMN_SPACING) / 2;
157 current->geometry.width =
158 COLUMN_SPACING + last_spacer->geometry.width;
159 } else if (ALIGN_RIGHT == align_mode) {
160 last_spacer->geometry.width +=
161 current->geometry.width - COLUMN_SPACING;
162 current->geometry.width = COLUMN_SPACING;
163 }
164 last_spacer = NULL;
165 align_mode = ALIGN_LEFT;
166 }
167 } else if (renderer->align_center_glyph == current->glyph) {
168 align_mode = ALIGN_CENTER;
169 current->glyph = PANGO_GLYPH_EMPTY;
170 last_spacer = current;
171 } else if (renderer->align_right_glyph == current->glyph) {
172 align_mode = ALIGN_RIGHT;
173 current->glyph = PANGO_GLYPH_EMPTY;
174 last_spacer = current;
175 }
176 }
177 }
178 } while (pango_layout_iter_next_run(iter));
179 if (NULL != cell_area && NULL != last_spacer && ALIGN_LEFT != align_mode) {
180 pango_layout_get_pixel_extents(layout, NULL, &rect);
181 if (ALIGN_CENTER == align_mode) {
182 last_spacer->geometry.width +=
183 PANGO_SCALE * ((cell_area->width - rect.width) / 2);
184 } else if (ALIGN_RIGHT == align_mode) {
185 last_spacer->geometry.width +=
186 PANGO_SCALE * (cell_area->width - rect.width);
187 }
188 }
189 pango_layout_iter_free(iter);
190 }
191
192 static PangoLayout * get_layout(AlignTextRenderer * renderer,
193 GtkWidget * widget,
194 const GdkRectangle * cell_area)
195 {
196 PangoLayout * layout;
197
198 layout = gtk_widget_create_pango_layout(widget, renderer->text);
199 pango_layout_set_width(layout, -1);
200 pango_layout_set_wrap(layout, PANGO_WRAP_CHAR);
201 pango_layout_set_tabs(layout, renderer->tab_array);
202 handle_right_alignment(renderer, layout, cell_area);
203 return layout;
204 }
205
206 static void align_text_renderer_get_size(
207 #if GTK_CHECK_VERSION(3,0,0)
208 GtkCellRenderer * cell, GtkWidget * widget, const GdkRectangle * cell_area,
209 #else
210 GtkCellRenderer * cell, GtkWidget * widget, GdkRectangle * cell_area,
211 #endif
212 gint * x_offset, gint * y_offset, gint * width, gint * height)
213 {
214 AlignTextRenderer * renderer = ALIGN_TEXT_RENDERER(cell);
215 PangoLayout * layout;
216 PangoRectangle rect;
217 gint xpad;
218 gint ypad;
219 gfloat xalign;
220 gfloat yalign;
221
222 layout = get_layout(renderer, widget, cell_area);
223 pango_layout_get_pixel_extents(layout, NULL, &rect);
224 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
225 gtk_cell_renderer_get_alignment(cell, &xalign, &yalign);
226 if (NULL != height) {
227 *height = ypad * 2 + rect.height;
228 }
229 if (NULL != width) {
230 *width = xpad * 2 + rect.x + rect.width;
231 }
232
233 if (NULL != cell_area) {
234 if (NULL != x_offset) {
235 *x_offset = cell_area->width - rect.x - rect.width -
236 (2 * xpad);
237 if (GTK_TEXT_DIR_RTL == gtk_widget_get_direction(widget)) {
238 *x_offset *= 1.0 - xalign;
239 } else {
240 *x_offset *= xalign;
241 }
242 }
243 if (NULL != y_offset) {
244 *y_offset = cell_area->height - rect.height - (2 * ypad);
245 *y_offset = MAX(*y_offset * yalign, 0);
246 }
247 }
248 }
249
250 static GtkStateType get_state(GtkCellRenderer * cell, GtkWidget * widget,
251 GtkCellRendererState flags)
252 {
253 if (!gtk_cell_renderer_get_sensitive(cell)) {
254 return GTK_STATE_INSENSITIVE;
255 }
256 if (GTK_CELL_RENDERER_SELECTED == (flags & GTK_CELL_RENDERER_SELECTED)) {
257 return gtk_widget_has_focus(widget) ?
258 GTK_STATE_SELECTED : GTK_STATE_ACTIVE;
259 }
260 if (GTK_CELL_RENDERER_PRELIT == (flags & GTK_CELL_RENDERER_PRELIT) &&
261 GTK_STATE_PRELIGHT == gtk_widget_get_state(widget)) {
262 return GTK_STATE_PRELIGHT;
263 }
264 if (GTK_STATE_INSENSITIVE == gtk_widget_get_state(widget)) {
265 return GTK_STATE_INSENSITIVE;
266 }
267 return GTK_STATE_NORMAL;
268 }
269
270 static PangoGlyph find_glyph(GtkWidget * widget, const gchar * str)
271 {
272 PangoLayout * layout;
273 PangoLayoutIter * iter;
274 PangoLayoutRun * run;
275 PangoGlyph glyph = PANGO_GET_UNKNOWN_GLYPH(PANGO_GLYPH_EMPTY);
276
277 layout = gtk_widget_create_pango_layout(widget, str);
278 iter = pango_layout_get_iter(layout);
279 if (NULL != (run = pango_layout_iter_get_run(iter))) {
280 if (1 >= run->glyphs->num_glyphs) {
281 glyph = run->glyphs->glyphs[0].glyph;
282 }
283 }
284 pango_layout_iter_free(iter);
285 return glyph;
286 }
287
288 static void find_renderer_glyphs(AlignTextRenderer * renderer, GtkWidget * widget)
289 {
290 if (0 == renderer->align_center_glyph) {
291 renderer->align_center_glyph = find_glyph(widget, ALIGN_CENTER_STRING);
292 }
293 if (0 == renderer->align_right_glyph) {
294 renderer->align_right_glyph = find_glyph(widget, ALIGN_RIGHT_STRING);
295 }
296 }
297
298 static void align_text_renderer_render(
299 #if GTK_CHECK_VERSION(3,0,0)
300 GtkCellRenderer * cell, cairo_t *cr, GtkWidget * widget,
301 const GdkRectangle * background_area, const GdkRectangle * cell_area,
302 GtkCellRendererState flags)
303 #else
304 GtkCellRenderer * cell, GdkWindow * window, GtkWidget * widget,
305 GdkRectangle * background_area, GdkRectangle * cell_area,
306 GdkRectangle * expose_area, guint flags)
307 #endif
308 {
309 AlignTextRenderer * renderer = ALIGN_TEXT_RENDERER(cell);
310 PangoLayout * layout;
311 GtkStateType state;
312 gint x_offset = 0;
313 gint y_offset = 0;
314 gint x_padding = 0;
315 gint y_padding = 0;
316
317 find_renderer_glyphs(renderer, widget);
318 layout = get_layout(renderer, widget, cell_area);
319 align_text_renderer_get_size(cell, widget, cell_area, &x_offset,
320 &y_offset, NULL, NULL);
321
322 state = get_state(cell, widget, flags);
323 gtk_cell_renderer_get_padding(cell, &x_padding, &y_padding);
324 #if GTK_CHECK_VERSION(3,0,0)
325 gtk_paint_layout(gtk_widget_get_style(widget), cr, state, TRUE,
326 #else
327 gtk_paint_layout(gtk_widget_get_style(widget), window, state, TRUE, expose_area,
328 #endif
329 widget, "align_text_renderer",
330 cell_area->x + x_offset + x_padding,
331 cell_area->y + y_offset + y_padding,
332 layout);
333 }
334
335 static void cdebconf_gtk_align_text_renderer_class_init(
336 AlignTextRendererClass * klass,
337 gpointer class_data)
338 {
339 GObjectClass * object_class = G_OBJECT_CLASS(klass);
340 GtkCellRendererClass * cell_class = GTK_CELL_RENDERER_CLASS(klass);
341
342 cdebconf_gtk_align_text_renderer_parent_class =
343 g_type_class_peek_parent (klass);
344 object_class->finalize = align_text_renderer_finalize;
345 object_class->get_property = align_text_renderer_get_property;
346 object_class->set_property = align_text_renderer_set_property;
347
348 cell_class->get_size = align_text_renderer_get_size;
349 cell_class->render = align_text_renderer_render;
350
351 klass->set_tab_array = cdebconf_gtk_align_text_renderer_set_tab_array;
352
353 g_object_class_install_property(
354 object_class, PROP_TEXT,
355 g_param_spec_string("text", "Text", "Text to render", NULL,
356 G_PARAM_READWRITE));
357 }
358
359 GType cdebconf_gtk_align_text_renderer_get_type(void)
360 {
361 static const GTypeInfo align_text_renderer_info = {
362 sizeof (AlignTextRendererClass),
363 NULL, /* base_init */
364 NULL, /* base_finalize */
365 (GClassInitFunc) cdebconf_gtk_align_text_renderer_class_init,
366 NULL, /* class_finalize */
367 NULL, /* class_data */
368 sizeof (AlignTextRenderer),
369 0, /* n_preallocs */
370 (GInstanceInitFunc) cdebconf_gtk_align_text_renderer_init,
371 NULL
372 };
373 static GType align_text_renderer_type = 0;
374
375 if (align_text_renderer_type) {
376 return align_text_renderer_type;
377 }
378
379 /* Derive from GtkCellRenderer */
380 align_text_renderer_type = g_type_register_static(
381 GTK_TYPE_CELL_RENDERER, "AlignTextRenderer",
382 &align_text_renderer_info, 0);
383 return align_text_renderer_type;
384 }
385
386 /* vim: et sw=4 si
387 */