adg: refactored _adg_read_cairo_path()
[adg.git] / src / adg / adg-table-style.c
blob53b9820eb798c7595d8c6f4226d89788456a8998
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 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.
28 * Since: 1.0
31 /**
32 * AdgTableStyle:
34 * All fields are private and should not be used directly.
35 * Use its public methods instead.
37 * Since: 1.0
38 **/
41 #include "adg-internal.h"
42 #include "adg-dress.h"
43 #include "adg-dress-builtins.h"
44 #include "adg-style.h"
46 #include "adg-table-style.h"
47 #include "adg-table-style-private.h"
50 G_DEFINE_TYPE(AdgTableStyle, adg_table_style, ADG_TYPE_STYLE)
52 enum {
53 PROP_0,
54 PROP_COLOR_DRESS,
55 PROP_GRID_DRESS,
56 PROP_FRAME_DRESS,
57 PROP_TITLE_DRESS,
58 PROP_VALUE_DRESS,
59 PROP_ROW_HEIGHT,
60 PROP_CELL_PADDING,
61 PROP_CELL_SPACING
65 static void _adg_get_property (GObject *object,
66 guint prop_id,
67 GValue *value,
68 GParamSpec *pspec);
69 static void _adg_set_property (GObject *object,
70 guint prop_id,
71 const GValue *value,
72 GParamSpec *pspec);
73 static void _adg_apply (AdgStyle *style,
74 AdgEntity *entity,
75 cairo_t *cr);
78 static void
79 adg_table_style_class_init(AdgTableStyleClass *klass)
81 GObjectClass *gobject_class;
82 AdgStyleClass *style_class;
83 GParamSpec *param;
85 gobject_class = (GObjectClass *) klass;
86 style_class = (AdgStyleClass *) klass;
88 g_type_class_add_private(klass, sizeof(AdgTableStylePrivate));
90 gobject_class->get_property = _adg_get_property;
91 gobject_class->set_property = _adg_set_property;
93 style_class->apply = _adg_apply;
95 param = adg_param_spec_dress("color-dress",
96 P_("Color Dress"),
97 P_("Fallback color dress, used when no specific dresses are selected"),
98 ADG_DRESS_COLOR_ANNOTATION,
99 G_PARAM_READWRITE);
100 g_object_class_install_property(gobject_class, PROP_COLOR_DRESS, param);
102 param = adg_param_spec_dress("grid-dress",
103 P_("Grid Dress"),
104 P_("Line dress to use while rendering the grid of the table"),
105 ADG_DRESS_LINE_GRID,
106 G_PARAM_READWRITE);
107 g_object_class_install_property(gobject_class, PROP_GRID_DRESS, param);
109 param = adg_param_spec_dress("frame-dress",
110 P_("Frame Dress"),
111 P_("Line dress to use while drawing the table frame"),
112 ADG_DRESS_LINE_FRAME,
113 G_PARAM_READWRITE);
114 g_object_class_install_property(gobject_class, PROP_FRAME_DRESS, param);
116 param = adg_param_spec_dress("title-dress",
117 P_("Title Dress"),
118 P_("Font dress to use for titles"),
119 ADG_DRESS_FONT_ANNOTATION,
120 G_PARAM_READWRITE);
121 g_object_class_install_property(gobject_class, PROP_TITLE_DRESS, param);
123 param = adg_param_spec_dress("value-dress",
124 P_("Value Dress"),
125 P_("Font dress to use for values inside the cells"),
126 ADG_DRESS_FONT_TEXT,
127 G_PARAM_READWRITE);
128 g_object_class_install_property(gobject_class, PROP_VALUE_DRESS, param);
130 param = g_param_spec_double("row-height",
131 P_("Row Height"),
132 P_("The fallback row height when not explicitely specified while creating a new row"),
133 0, G_MAXDOUBLE, 30,
134 G_PARAM_READWRITE);
135 g_object_class_install_property(gobject_class, PROP_ROW_HEIGHT, param);
137 param = g_param_spec_boxed("cell-padding",
138 P_("Cell Padding"),
139 P_("How much space from the bounding box must left inside every cell"),
140 CPML_TYPE_PAIR,
141 G_PARAM_READWRITE);
142 g_object_class_install_property(gobject_class, PROP_CELL_PADDING, param);
144 param = g_param_spec_boxed("cell-spacing",
145 P_("Cell Spacing"),
146 P_("How much space to left between the cells"),
147 CPML_TYPE_PAIR,
148 G_PARAM_READWRITE);
149 g_object_class_install_property(gobject_class, PROP_CELL_SPACING, param);
152 static void
153 adg_table_style_init(AdgTableStyle *table_style)
155 AdgTableStylePrivate *data;
157 data = G_TYPE_INSTANCE_GET_PRIVATE(table_style, ADG_TYPE_TABLE_STYLE,
158 AdgTableStylePrivate);
160 data->color_dress = ADG_DRESS_COLOR_ANNOTATION,
161 data->grid_dress = ADG_DRESS_LINE_GRID;
162 data->frame_dress = ADG_DRESS_LINE_FRAME;
163 data->title_dress = ADG_DRESS_FONT_ANNOTATION;
164 data->value_dress = ADG_DRESS_FONT_TEXT;
165 data->row_height = 30;
166 data->cell_padding.x = 3;
167 data->cell_padding.y = 3;
168 data->cell_spacing.x = 0;
169 data->cell_spacing.y = 0;
171 table_style->data = data;
174 static void
175 _adg_get_property(GObject *object, guint prop_id,
176 GValue *value, GParamSpec *pspec)
178 AdgTableStylePrivate *data = ((AdgTableStyle *) object)->data;
180 switch (prop_id) {
181 case PROP_COLOR_DRESS:
182 g_value_set_int(value, data->color_dress);
183 break;
184 case PROP_GRID_DRESS:
185 g_value_set_int(value, data->grid_dress);
186 break;
187 case PROP_FRAME_DRESS:
188 g_value_set_int(value, data->frame_dress);
189 break;
190 case PROP_TITLE_DRESS:
191 g_value_set_int(value, data->title_dress);
192 break;
193 case PROP_VALUE_DRESS:
194 g_value_set_int(value, data->value_dress);
195 break;
196 case PROP_ROW_HEIGHT:
197 g_value_set_double(value, data->row_height);
198 break;
199 case PROP_CELL_PADDING:
200 g_value_set_boxed(value, &data->cell_padding);
201 break;
202 case PROP_CELL_SPACING:
203 g_value_set_boxed(value, &data->cell_spacing);
204 break;
205 default:
206 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
207 break;
211 static void
212 _adg_set_property(GObject *object, guint prop_id,
213 const GValue *value, GParamSpec *pspec)
215 AdgTableStylePrivate *data = ((AdgTableStyle *) object)->data;
217 switch (prop_id) {
218 case PROP_COLOR_DRESS:
219 data->color_dress = g_value_get_int(value);
220 break;
221 case PROP_GRID_DRESS:
222 data->grid_dress = g_value_get_int(value);
223 break;
224 case PROP_FRAME_DRESS:
225 data->frame_dress = g_value_get_int(value);
226 break;
227 case PROP_TITLE_DRESS:
228 data->title_dress = g_value_get_int(value);
229 break;
230 case PROP_VALUE_DRESS:
231 data->value_dress = g_value_get_int(value);
232 break;
233 case PROP_ROW_HEIGHT:
234 data->row_height = g_value_get_double(value);
235 break;
236 case PROP_CELL_PADDING:
237 cpml_pair_copy(&data->cell_padding, g_value_get_boxed(value));
238 break;
239 case PROP_CELL_SPACING:
240 cpml_pair_copy(&data->cell_spacing, g_value_get_boxed(value));
241 break;
242 default:
243 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
244 break;
250 * adg_table_style_new:
252 * Constructs a new empty table style initialized with default params.
254 * Returns: (transfer full): a new table style.
256 * Since: 1.0
258 AdgTableStyle *
259 adg_table_style_new(void)
261 return g_object_new(ADG_TYPE_TABLE_STYLE, NULL);
265 * adg_table_style_set_color_dress:
266 * @table_style: an #AdgTableStyle object
267 * @dress: the new color dress
269 * Sets a new color dress on @table_style.
271 * Since: 1.0
273 void
274 adg_table_style_set_color_dress(AdgTableStyle *table_style, AdgDress dress)
276 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
277 g_object_set(table_style, "color-dress", dress, NULL);
281 * adg_table_style_get_color_dress:
282 * @table_style: an #AdgTableStyle object
284 * Gets the @table_style color dress to be used. This dress should be
285 * intended as a fallback color as it could be overriden by more
286 * specific dresses, such as a color explicitely specified on the
287 * #AdgTableStyle:value-dress.
289 * Returns: (transfer none): the color dress.
291 * Since: 1.0
293 AdgDress
294 adg_table_style_get_color_dress(AdgTableStyle *table_style)
296 AdgTableStylePrivate *data;
298 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
300 data = table_style->data;
302 return data->color_dress;
306 * adg_table_style_set_frame_dress:
307 * @table_style: an #AdgTableStyle object
308 * @dress: the new line dress
310 * Sets a new line dress on @table_style for rendering the frames.
312 * Since: 1.0
314 void
315 adg_table_style_set_frame_dress(AdgTableStyle *table_style, AdgDress dress)
317 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
318 g_object_set(table_style, "frame-dress", dress, NULL);
322 * adg_table_style_get_frame_dress:
323 * @table_style: an #AdgTableStyle object
325 * Gets the line dress to be used for rendering the frames with
326 * @table_style.
328 * Returns: (transfer none): the line dress.
330 * Since: 1.0
332 AdgDress
333 adg_table_style_get_frame_dress(AdgTableStyle *table_style)
335 AdgTableStylePrivate *data;
337 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
339 data = table_style->data;
341 return data->frame_dress;
345 * adg_table_style_set_grid_dress:
346 * @table_style: an #AdgTableStyle object
347 * @dress: the new line dress
349 * Sets a new line dress on @table_style for rendering the grids.
351 * Since: 1.0
353 void
354 adg_table_style_set_grid_dress(AdgTableStyle *table_style, AdgDress dress)
356 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
357 g_object_set(table_style, "grid-dress", dress, NULL);
361 * adg_table_style_get_grid_dress:
362 * @table_style: an #AdgTableStyle object
364 * Gets the line dress to be used for rendering the grids with
365 * @table_style.
367 * Returns: (transfer none): the line dress.
369 * Since: 1.0
371 AdgDress
372 adg_table_style_get_grid_dress(AdgTableStyle *table_style)
374 AdgTableStylePrivate *data;
376 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
378 data = table_style->data;
380 return data->grid_dress;
384 * adg_table_style_set_title_dress:
385 * @table_style: an #AdgTableStyle object
386 * @dress: the new font dress
388 * Sets a new font dress on @table_style for rendering cell titles.
390 * Since: 1.0
392 void
393 adg_table_style_set_title_dress(AdgTableStyle *table_style, AdgDress dress)
395 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
396 g_object_set(table_style, "title-dress", dress, NULL);
400 * adg_table_style_get_title_dress:
401 * @table_style: an #AdgTableStyle object
403 * Gets the font dress to be used for rendering cell titles
404 * with @table_style.
406 * Returns: (transfer none): the font dress.
408 * Since: 1.0
410 AdgDress
411 adg_table_style_get_title_dress(AdgTableStyle *table_style)
413 AdgTableStylePrivate *data;
415 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
417 data = table_style->data;
419 return data->title_dress;
423 * adg_table_style_set_value_dress:
424 * @table_style: an #AdgTableStyle object
425 * @dress: the new font dress
427 * Sets a new font dress on @table_style for rendering cell values.
429 * Since: 1.0
431 void
432 adg_table_style_set_value_dress(AdgTableStyle *table_style, AdgDress dress)
434 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
435 g_object_set(table_style, "value-dress", dress, NULL);
439 * adg_table_style_get_value_dress:
440 * @table_style: an #AdgTableStyle object
442 * Gets the font dress to be used for rendering cell values
443 * with @table_style.
445 * Returns: (transfer none): the font dress.
447 * Since: 1.0
449 AdgDress
450 adg_table_style_get_value_dress(AdgTableStyle *table_style)
452 AdgTableStylePrivate *data;
454 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
456 data = table_style->data;
458 return data->value_dress;
462 * adg_table_style_set_row_height:
463 * @table_style: an #AdgTableStyle object
464 * @height: the new row heigth fallback
466 * Sets a new #AdgTableStyle:row-height fallback. @height must
467 * be a valid row height greather than %0 or a warning will be
468 * raised and this function will fail.
470 * Since: 1.0
472 void
473 adg_table_style_set_row_height(AdgTableStyle *table_style, gdouble height)
475 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
476 g_object_set(table_style, "row-height", height, NULL);
480 * adg_table_style_get_row_height:
481 * @table_style: an #AdgTableStyle object
483 * Gets the row height fallback value.
485 * Returns: the fallback row height or %0 on errors.
487 * Since: 1.0
489 gdouble
490 adg_table_style_get_row_height(AdgTableStyle *table_style)
492 AdgTableStylePrivate *data;
494 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), 0);
496 data = table_style->data;
498 return data->row_height;
502 * adg_table_style_set_cell_padding:
503 * @table_style: an #AdgTableStyle object
504 * @padding: the new padding values
506 * Sets new #AdgTableStyle:cell-padding values.
508 * Since: 1.0
510 void
511 adg_table_style_set_cell_padding(AdgTableStyle *table_style,
512 const CpmlPair *padding)
514 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
515 g_object_set(table_style, "cell-padding", padding, NULL);
519 * adg_table_style_get_cell_padding:
520 * @table_style: an #AdgTableStyle object
522 * Gets the padding values in x and y to be left clear inside the cells.
523 * The returned pointer refers to an internal allocated struct and
524 * must not be modified or freed.
526 * The cell padding is a symmetric value, that is the padding on the
527 * left will always be equal to the padding on the right and the top
528 * will always be equal to the bottom.
530 * Returns: (transfer none): the cell padding values or %NULL on errors.
532 * Since: 1.0
534 const CpmlPair *
535 adg_table_style_get_cell_padding(AdgTableStyle *table_style)
537 AdgTableStylePrivate *data;
539 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), NULL);
541 data = table_style->data;
543 return &data->cell_padding;
547 * adg_table_style_set_cell_spacing:
548 * @table_style: an #AdgTableStyle object
549 * @spacing: the new spacing values
551 * Sets new #AdgTableStyle:cell-spacing values.
553 * Since: 1.0
555 void
556 adg_table_style_set_cell_spacing(AdgTableStyle *table_style,
557 const CpmlPair *spacing)
559 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
560 g_object_set(table_style, "cell-spacing", spacing, NULL);
564 * adg_table_style_get_cell_spacing:
565 * @table_style: an #AdgTableStyle object
567 * Gets the spacing values in x and y to be left between the cell
568 * boundary boxes. The returned pointer refers to an internal
569 * allocated struct and must not be modified or freed.
571 * The cell spacing is a symmetric value, that is the spacing on the
572 * left will always be equal to the spacing on the right and the top
573 * will always be equal to the bottom.
575 * Returns: (transfer none): the cell spacing values or %NULL on errors.
577 * Since: 1.0
579 const CpmlPair *
580 adg_table_style_get_cell_spacing(AdgTableStyle *table_style)
582 AdgTableStylePrivate *data;
584 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), NULL);
586 data = table_style->data;
588 return &data->cell_spacing;
592 static void
593 _adg_apply(AdgStyle *style, AdgEntity *entity, cairo_t *cr)
595 AdgTableStylePrivate *data = ((AdgTableStyle *) style)->data;
597 adg_entity_apply_dress(entity, data->color_dress, cr);