[AdgText] Using logical_rect instead of ink_rect for extents
[adg.git] / src / adg / adg-table-style.c
blobca2d8991afbb5c783988285c1565036872a58b99
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 /**
22 * SECTION:adg-table-style
23 * @short_description: Customization of table rendering
25 * Contains parameters on how to build tables such as the lines to
26 * use for frames and grids and the font dresses for titles or values.
29 /**
30 * AdgTableStyle:
32 * All fields are private and should not be used directly.
33 * Use its public methods instead.
34 **/
37 #include "adg-internal.h"
38 #include "adg-dress.h"
39 #include "adg-dress-builtins.h"
40 #include "adg-style.h"
42 #include "adg-table-style.h"
43 #include "adg-table-style-private.h"
46 G_DEFINE_TYPE(AdgTableStyle, adg_table_style, ADG_TYPE_STYLE)
48 enum {
49 PROP_0,
50 PROP_COLOR_DRESS,
51 PROP_GRID_DRESS,
52 PROP_FRAME_DRESS,
53 PROP_TITLE_DRESS,
54 PROP_VALUE_DRESS,
55 PROP_ROW_HEIGHT,
56 PROP_CELL_PADDING,
57 PROP_CELL_SPACING
61 static void _adg_get_property (GObject *object,
62 guint prop_id,
63 GValue *value,
64 GParamSpec *pspec);
65 static void _adg_set_property (GObject *object,
66 guint prop_id,
67 const GValue *value,
68 GParamSpec *pspec);
69 static void _adg_apply (AdgStyle *style,
70 AdgEntity *entity,
71 cairo_t *cr);
74 static void
75 adg_table_style_class_init(AdgTableStyleClass *klass)
77 GObjectClass *gobject_class;
78 AdgStyleClass *style_class;
79 GParamSpec *param;
81 gobject_class = (GObjectClass *) klass;
82 style_class = (AdgStyleClass *) klass;
84 g_type_class_add_private(klass, sizeof(AdgTableStylePrivate));
86 gobject_class->get_property = _adg_get_property;
87 gobject_class->set_property = _adg_set_property;
89 style_class->apply = _adg_apply;
91 param = adg_param_spec_dress("color-dress",
92 P_("Color Dress"),
93 P_("Fallback color dress, used when no specific dresses are selected"),
94 ADG_DRESS_COLOR_ANNOTATION,
95 G_PARAM_READWRITE);
96 g_object_class_install_property(gobject_class, PROP_COLOR_DRESS, param);
98 param = adg_param_spec_dress("grid-dress",
99 P_("Grid Dress"),
100 P_("Line dress to use while rendering the grid of the table"),
101 ADG_DRESS_LINE_GRID,
102 G_PARAM_READWRITE);
103 g_object_class_install_property(gobject_class, PROP_GRID_DRESS, param);
105 param = adg_param_spec_dress("frame-dress",
106 P_("Frame Dress"),
107 P_("Line dress to use while drawing the table frame"),
108 ADG_DRESS_LINE_FRAME,
109 G_PARAM_READWRITE);
110 g_object_class_install_property(gobject_class, PROP_FRAME_DRESS, param);
112 param = adg_param_spec_dress("title-dress",
113 P_("Title Dress"),
114 P_("Font dress to use for titles"),
115 ADG_DRESS_FONT_ANNOTATION,
116 G_PARAM_READWRITE);
117 g_object_class_install_property(gobject_class, PROP_TITLE_DRESS, param);
119 param = adg_param_spec_dress("value-dress",
120 P_("Value Dress"),
121 P_("Font dress to use for values inside the cells"),
122 ADG_DRESS_FONT_TEXT,
123 G_PARAM_READWRITE);
124 g_object_class_install_property(gobject_class, PROP_VALUE_DRESS, param);
126 param = g_param_spec_double("row-height",
127 P_("Row Height"),
128 P_("The fallback row height when not explicitely specified while creating a new row"),
129 0, G_MAXDOUBLE, 30,
130 G_PARAM_READWRITE);
131 g_object_class_install_property(gobject_class, PROP_ROW_HEIGHT, param);
133 param = g_param_spec_boxed("cell-padding",
134 P_("Cell Padding"),
135 P_("How much space from the bounding box must left inside every cell"),
136 ADG_TYPE_PAIR,
137 G_PARAM_READWRITE);
138 g_object_class_install_property(gobject_class, PROP_CELL_PADDING, param);
140 param = g_param_spec_boxed("cell-spacing",
141 P_("Cell Spacing"),
142 P_("How much space to left between the cells"),
143 ADG_TYPE_PAIR,
144 G_PARAM_READWRITE);
145 g_object_class_install_property(gobject_class, PROP_CELL_SPACING, param);
148 static void
149 adg_table_style_init(AdgTableStyle *table_style)
151 AdgTableStylePrivate *data;
153 data = G_TYPE_INSTANCE_GET_PRIVATE(table_style, ADG_TYPE_TABLE_STYLE,
154 AdgTableStylePrivate);
156 data->color_dress = ADG_DRESS_COLOR_ANNOTATION,
157 data->grid_dress = ADG_DRESS_LINE_GRID;
158 data->frame_dress = ADG_DRESS_LINE_FRAME;
159 data->title_dress = ADG_DRESS_FONT_ANNOTATION;
160 data->value_dress = ADG_DRESS_FONT_TEXT;
161 data->row_height = 30;
162 data->cell_padding.x = 3;
163 data->cell_padding.y = 3;
164 data->cell_spacing.x = 0;
165 data->cell_spacing.y = 0;
167 table_style->data = data;
170 static void
171 _adg_get_property(GObject *object, guint prop_id,
172 GValue *value, GParamSpec *pspec)
174 AdgTableStylePrivate *data = ((AdgTableStyle *) object)->data;
176 switch (prop_id) {
177 case PROP_COLOR_DRESS:
178 g_value_set_int(value, data->color_dress);
179 break;
180 case PROP_GRID_DRESS:
181 g_value_set_int(value, data->grid_dress);
182 break;
183 case PROP_FRAME_DRESS:
184 g_value_set_int(value, data->frame_dress);
185 break;
186 case PROP_TITLE_DRESS:
187 g_value_set_int(value, data->title_dress);
188 break;
189 case PROP_VALUE_DRESS:
190 g_value_set_int(value, data->value_dress);
191 break;
192 case PROP_ROW_HEIGHT:
193 g_value_set_double(value, data->row_height);
194 break;
195 case PROP_CELL_PADDING:
196 g_value_set_boxed(value, &data->cell_padding);
197 break;
198 case PROP_CELL_SPACING:
199 g_value_set_boxed(value, &data->cell_spacing);
200 break;
201 default:
202 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
203 break;
207 static void
208 _adg_set_property(GObject *object, guint prop_id,
209 const GValue *value, GParamSpec *pspec)
211 AdgTableStylePrivate *data = ((AdgTableStyle *) object)->data;
213 switch (prop_id) {
214 case PROP_COLOR_DRESS:
215 data->color_dress = g_value_get_int(value);
216 break;
217 case PROP_GRID_DRESS:
218 data->grid_dress = g_value_get_int(value);
219 break;
220 case PROP_FRAME_DRESS:
221 data->frame_dress = g_value_get_int(value);
222 break;
223 case PROP_TITLE_DRESS:
224 data->title_dress = g_value_get_int(value);
225 break;
226 case PROP_VALUE_DRESS:
227 data->value_dress = g_value_get_int(value);
228 break;
229 case PROP_ROW_HEIGHT:
230 data->row_height = g_value_get_double(value);
231 break;
232 case PROP_CELL_PADDING:
233 adg_pair_copy(&data->cell_padding, g_value_get_boxed(value));
234 break;
235 case PROP_CELL_SPACING:
236 adg_pair_copy(&data->cell_spacing, g_value_get_boxed(value));
237 break;
238 default:
239 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
240 break;
246 * adg_table_style_new:
248 * Constructs a new empty table style initialized with default params.
250 * Returns: a new table style
252 AdgTableStyle *
253 adg_table_style_new(void)
255 return g_object_new(ADG_TYPE_TABLE_STYLE, NULL);
259 * adg_table_style_set_color_dress:
260 * @table_style: an #AdgTableStyle object
261 * @dress: the new color dress
263 * Sets a new color dress on @table_style.
265 void
266 adg_table_style_set_color_dress(AdgTableStyle *table_style, AdgDress dress)
268 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
269 g_object_set(table_style, "color-dress", dress, NULL);
273 * adg_table_style_get_color_dress:
274 * @table_style: an #AdgTableStyle object
276 * Gets the @table_style color dress to be used. This dress should be
277 * intended as a fallback color as it could be overriden by more
278 * specific dresses, such as a color explicitely specified on the
279 * #AdgTableStyle:value-dress.
281 * Returns: the color dress
283 AdgDress
284 adg_table_style_get_color_dress(AdgTableStyle *table_style)
286 AdgTableStylePrivate *data;
288 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
290 data = table_style->data;
292 return data->color_dress;
296 * adg_table_style_set_frame_dress:
297 * @table_style: an #AdgTableStyle object
298 * @dress: the new line dress
300 * Sets a new line dress on @table_style for rendering the frames.
302 void
303 adg_table_style_set_frame_dress(AdgTableStyle *table_style, AdgDress dress)
305 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
306 g_object_set(table_style, "frame-dress", dress, NULL);
310 * adg_table_style_get_frame_dress:
311 * @table_style: an #AdgTableStyle object
313 * Gets the line dress to be used for rendering the frames with
314 * @table_style.
316 * Returns: the line dress
318 AdgDress
319 adg_table_style_get_frame_dress(AdgTableStyle *table_style)
321 AdgTableStylePrivate *data;
323 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
325 data = table_style->data;
327 return data->frame_dress;
331 * adg_table_style_set_grid_dress:
332 * @table_style: an #AdgTableStyle object
333 * @dress: the new line dress
335 * Sets a new line dress on @table_style for rendering the grids.
337 void
338 adg_table_style_set_grid_dress(AdgTableStyle *table_style, AdgDress dress)
340 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
341 g_object_set(table_style, "grid-dress", dress, NULL);
345 * adg_table_style_get_grid_dress:
346 * @table_style: an #AdgTableStyle object
348 * Gets the line dress to be used for rendering the grids with
349 * @table_style.
351 * Returns: the line dress
353 AdgDress
354 adg_table_style_get_grid_dress(AdgTableStyle *table_style)
356 AdgTableStylePrivate *data;
358 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
360 data = table_style->data;
362 return data->grid_dress;
366 * adg_table_style_set_title_dress:
367 * @table_style: an #AdgTableStyle object
368 * @dress: the new font dress
370 * Sets a new font dress on @table_style for rendering cell titles.
372 void
373 adg_table_style_set_title_dress(AdgTableStyle *table_style, AdgDress dress)
375 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
376 g_object_set(table_style, "title-dress", dress, NULL);
380 * adg_table_style_get_title_dress:
381 * @table_style: an #AdgTableStyle object
383 * Gets the font dress to be used for rendering cell titles
384 * with @table_style.
386 * Returns: the font dress
388 AdgDress
389 adg_table_style_get_title_dress(AdgTableStyle *table_style)
391 AdgTableStylePrivate *data;
393 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
395 data = table_style->data;
397 return data->title_dress;
401 * adg_table_style_set_value_dress:
402 * @table_style: an #AdgTableStyle object
403 * @dress: the new font dress
405 * Sets a new font dress on @table_style for rendering cell values.
407 void
408 adg_table_style_set_value_dress(AdgTableStyle *table_style, AdgDress dress)
410 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
411 g_object_set(table_style, "value-dress", dress, NULL);
415 * adg_table_style_get_value_dress:
416 * @table_style: an #AdgTableStyle object
418 * Gets the font dress to be used for rendering cell values
419 * with @table_style.
421 * Returns: the font dress
423 AdgDress
424 adg_table_style_get_value_dress(AdgTableStyle *table_style)
426 AdgTableStylePrivate *data;
428 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
430 data = table_style->data;
432 return data->value_dress;
436 * adg_table_style_set_row_height:
437 * @table_style: an #AdgTableStyle object
438 * @heigth: the new row heigth fallback
440 * Sets a new #AdgTableStyle:row-height fallback. @height must
441 * be a valid row height greather than %0 or a warning will be
442 * raised and this function will fail.
444 void
445 adg_table_style_set_row_height(AdgTableStyle *table_style, gdouble height)
447 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
448 g_object_set(table_style, "row-height", height, NULL);
452 * adg_table_style_get_row_height:
453 * @table_style: an #AdgTableStyle object
455 * Gets the row height fallback value.
457 * Returns: the fallback row height or %0 on errors
459 gdouble
460 adg_table_style_get_row_height(AdgTableStyle *table_style)
462 AdgTableStylePrivate *data;
464 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), 0);
466 data = table_style->data;
468 return data->row_height;
472 * adg_table_style_set_cell_padding:
473 * @table_style: an #AdgTableStyle object
474 * @padding: the new padding values
476 * Sets new #AdgTableStyle:cell-padding values.
478 void
479 adg_table_style_set_cell_padding(AdgTableStyle *table_style,
480 const AdgPair *padding)
482 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
483 g_object_set(table_style, "cell-padding", padding, NULL);
487 * adg_table_style_get_cell_padding:
488 * @table_style: an #AdgTableStyle object
490 * Gets the padding values in x and y to be left clear inside the cells.
491 * The returned pointer refers to an internal allocated struct and
492 * must not be modified or freed.
494 * The cell padding is a symmetric value, that is the padding on the
495 * left will always be equal to the padding on the right and the top
496 * will always be equal to the bottom.
498 * Returns: the cell padding values or %NULL on errors
500 const AdgPair *
501 adg_table_style_get_cell_padding(AdgTableStyle *table_style)
503 AdgTableStylePrivate *data;
505 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), NULL);
507 data = table_style->data;
509 return &data->cell_padding;
513 * adg_table_style_set_cell_spacing:
514 * @table_style: an #AdgTableStyle object
515 * @spacing: the new spacing values
517 * Sets new #AdgTableStyle:cell-spacing values.
519 void
520 adg_table_style_set_cell_spacing(AdgTableStyle *table_style,
521 const AdgPair *spacing)
523 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
524 g_object_set(table_style, "cell-spacing", spacing, NULL);
528 * adg_table_style_get_cell_spacing:
529 * @table_style: an #AdgTableStyle object
531 * Gets the spacing values in x and y to be left between the cell
532 * boundary boxes. The returned pointer refers to an internal
533 * allocated struct and must not be modified or freed.
535 * The cell spacing is a symmetric value, that is the spacing on the
536 * left will always be equal to the spacing on the right and the top
537 * will always be equal to the bottom.
539 * Returns: the cell spacing values or %NULL on errors
541 const AdgPair *
542 adg_table_style_get_cell_spacing(AdgTableStyle *table_style)
544 AdgTableStylePrivate *data;
546 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), NULL);
548 data = table_style->data;
550 return &data->cell_spacing;
554 static void
555 _adg_apply(AdgStyle *style, AdgEntity *entity, cairo_t *cr)
557 AdgTableStylePrivate *data = ((AdgTableStyle *) style)->data;
559 adg_entity_apply_dress(entity, data->color_dress, cr);