adg: split AdgParamDress from AdgDress
[adg.git] / src / adg / adg-table-style.c
blob4c10c410ac0f26db2a3a735b656b0f888c612555
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
27 * Since: 1.0
30 /**
31 * AdgTableStyle:
33 * All fields are private and should not be used directly.
34 * Use its public methods instead.
36 * Since: 1.0
37 **/
40 #include "adg-internal.h"
41 #include "adg-style.h"
42 #include "adg-dress.h"
43 #include "adg-param-dress.h"
45 #include "adg-table-style.h"
46 #include "adg-table-style-private.h"
49 G_DEFINE_TYPE(AdgTableStyle, adg_table_style, ADG_TYPE_STYLE)
51 enum {
52 PROP_0,
53 PROP_COLOR_DRESS,
54 PROP_GRID_DRESS,
55 PROP_FRAME_DRESS,
56 PROP_TITLE_DRESS,
57 PROP_VALUE_DRESS,
58 PROP_ROW_HEIGHT,
59 PROP_CELL_PADDING,
60 PROP_CELL_SPACING
64 static void _adg_get_property (GObject *object,
65 guint prop_id,
66 GValue *value,
67 GParamSpec *pspec);
68 static void _adg_set_property (GObject *object,
69 guint prop_id,
70 const GValue *value,
71 GParamSpec *pspec);
72 static void _adg_apply (AdgStyle *style,
73 AdgEntity *entity,
74 cairo_t *cr);
77 static void
78 adg_table_style_class_init(AdgTableStyleClass *klass)
80 GObjectClass *gobject_class;
81 AdgStyleClass *style_class;
82 GParamSpec *param;
84 gobject_class = (GObjectClass *) klass;
85 style_class = (AdgStyleClass *) klass;
87 g_type_class_add_private(klass, sizeof(AdgTableStylePrivate));
89 gobject_class->get_property = _adg_get_property;
90 gobject_class->set_property = _adg_set_property;
92 style_class->apply = _adg_apply;
94 param = adg_param_spec_dress("color-dress",
95 P_("Color Dress"),
96 P_("Fallback color dress, used when no specific dresses are selected"),
97 ADG_DRESS_COLOR_ANNOTATION,
98 G_PARAM_READWRITE);
99 g_object_class_install_property(gobject_class, PROP_COLOR_DRESS, param);
101 param = adg_param_spec_dress("grid-dress",
102 P_("Grid Dress"),
103 P_("Line dress to use while rendering the grid of the table"),
104 ADG_DRESS_LINE_GRID,
105 G_PARAM_READWRITE);
106 g_object_class_install_property(gobject_class, PROP_GRID_DRESS, param);
108 param = adg_param_spec_dress("frame-dress",
109 P_("Frame Dress"),
110 P_("Line dress to use while drawing the table frame"),
111 ADG_DRESS_LINE_FRAME,
112 G_PARAM_READWRITE);
113 g_object_class_install_property(gobject_class, PROP_FRAME_DRESS, param);
115 param = adg_param_spec_dress("title-dress",
116 P_("Title Dress"),
117 P_("Font dress to use for titles"),
118 ADG_DRESS_FONT_ANNOTATION,
119 G_PARAM_READWRITE);
120 g_object_class_install_property(gobject_class, PROP_TITLE_DRESS, param);
122 param = adg_param_spec_dress("value-dress",
123 P_("Value Dress"),
124 P_("Font dress to use for values inside the cells"),
125 ADG_DRESS_FONT_TEXT,
126 G_PARAM_READWRITE);
127 g_object_class_install_property(gobject_class, PROP_VALUE_DRESS, param);
129 param = g_param_spec_double("row-height",
130 P_("Row Height"),
131 P_("The fallback row height when not explicitely specified while creating a new row"),
132 0, G_MAXDOUBLE, 30,
133 G_PARAM_READWRITE);
134 g_object_class_install_property(gobject_class, PROP_ROW_HEIGHT, param);
136 param = g_param_spec_boxed("cell-padding",
137 P_("Cell Padding"),
138 P_("How much space from the bounding box must left inside every cell"),
139 CPML_TYPE_PAIR,
140 G_PARAM_READWRITE);
141 g_object_class_install_property(gobject_class, PROP_CELL_PADDING, param);
143 param = g_param_spec_boxed("cell-spacing",
144 P_("Cell Spacing"),
145 P_("How much space to left between the cells"),
146 CPML_TYPE_PAIR,
147 G_PARAM_READWRITE);
148 g_object_class_install_property(gobject_class, PROP_CELL_SPACING, param);
151 static void
152 adg_table_style_init(AdgTableStyle *table_style)
154 AdgTableStylePrivate *data;
156 data = G_TYPE_INSTANCE_GET_PRIVATE(table_style, ADG_TYPE_TABLE_STYLE,
157 AdgTableStylePrivate);
159 data->color_dress = ADG_DRESS_COLOR_ANNOTATION,
160 data->grid_dress = ADG_DRESS_LINE_GRID;
161 data->frame_dress = ADG_DRESS_LINE_FRAME;
162 data->title_dress = ADG_DRESS_FONT_ANNOTATION;
163 data->value_dress = ADG_DRESS_FONT_TEXT;
164 data->row_height = 30;
165 data->cell_padding.x = 3;
166 data->cell_padding.y = 3;
167 data->cell_spacing.x = 0;
168 data->cell_spacing.y = 0;
170 table_style->data = data;
173 static void
174 _adg_get_property(GObject *object, guint prop_id,
175 GValue *value, GParamSpec *pspec)
177 AdgTableStylePrivate *data = ((AdgTableStyle *) object)->data;
179 switch (prop_id) {
180 case PROP_COLOR_DRESS:
181 g_value_set_enum(value, data->color_dress);
182 break;
183 case PROP_GRID_DRESS:
184 g_value_set_enum(value, data->grid_dress);
185 break;
186 case PROP_FRAME_DRESS:
187 g_value_set_enum(value, data->frame_dress);
188 break;
189 case PROP_TITLE_DRESS:
190 g_value_set_enum(value, data->title_dress);
191 break;
192 case PROP_VALUE_DRESS:
193 g_value_set_enum(value, data->value_dress);
194 break;
195 case PROP_ROW_HEIGHT:
196 g_value_set_double(value, data->row_height);
197 break;
198 case PROP_CELL_PADDING:
199 g_value_set_boxed(value, &data->cell_padding);
200 break;
201 case PROP_CELL_SPACING:
202 g_value_set_boxed(value, &data->cell_spacing);
203 break;
204 default:
205 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
206 break;
210 static void
211 _adg_set_property(GObject *object, guint prop_id,
212 const GValue *value, GParamSpec *pspec)
214 AdgTableStylePrivate *data = ((AdgTableStyle *) object)->data;
216 switch (prop_id) {
217 case PROP_COLOR_DRESS:
218 data->color_dress = g_value_get_enum(value);
219 break;
220 case PROP_GRID_DRESS:
221 data->grid_dress = g_value_get_enum(value);
222 break;
223 case PROP_FRAME_DRESS:
224 data->frame_dress = g_value_get_enum(value);
225 break;
226 case PROP_TITLE_DRESS:
227 data->title_dress = g_value_get_enum(value);
228 break;
229 case PROP_VALUE_DRESS:
230 data->value_dress = g_value_get_enum(value);
231 break;
232 case PROP_ROW_HEIGHT:
233 data->row_height = g_value_get_double(value);
234 break;
235 case PROP_CELL_PADDING:
236 cpml_pair_copy(&data->cell_padding, g_value_get_boxed(value));
237 break;
238 case PROP_CELL_SPACING:
239 cpml_pair_copy(&data->cell_spacing, g_value_get_boxed(value));
240 break;
241 default:
242 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
243 break;
249 * adg_table_style_new:
251 * Constructs a new empty table style initialized with default params.
253 * Returns: (transfer full): a new table style.
255 * Since: 1.0
257 AdgTableStyle *
258 adg_table_style_new(void)
260 return g_object_new(ADG_TYPE_TABLE_STYLE, NULL);
264 * adg_table_style_set_color_dress:
265 * @table_style: an #AdgTableStyle object
266 * @dress: the new color dress
268 * Sets a new color dress on @table_style.
270 * Since: 1.0
272 void
273 adg_table_style_set_color_dress(AdgTableStyle *table_style, AdgDress dress)
275 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
276 g_object_set(table_style, "color-dress", dress, NULL);
280 * adg_table_style_get_color_dress:
281 * @table_style: an #AdgTableStyle object
283 * Gets the @table_style color dress to be used. This dress should be
284 * intended as a fallback color as it could be overriden by more
285 * specific dresses, such as a color explicitely specified on the
286 * #AdgTableStyle:value-dress.
288 * Returns: (transfer none): the color dress.
290 * Since: 1.0
292 AdgDress
293 adg_table_style_get_color_dress(AdgTableStyle *table_style)
295 AdgTableStylePrivate *data;
297 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
299 data = table_style->data;
301 return data->color_dress;
305 * adg_table_style_set_frame_dress:
306 * @table_style: an #AdgTableStyle object
307 * @dress: the new line dress
309 * Sets a new line dress on @table_style for rendering the frames.
311 * Since: 1.0
313 void
314 adg_table_style_set_frame_dress(AdgTableStyle *table_style, AdgDress dress)
316 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
317 g_object_set(table_style, "frame-dress", dress, NULL);
321 * adg_table_style_get_frame_dress:
322 * @table_style: an #AdgTableStyle object
324 * Gets the line dress to be used for rendering the frames with
325 * @table_style.
327 * Returns: (transfer none): the line dress.
329 * Since: 1.0
331 AdgDress
332 adg_table_style_get_frame_dress(AdgTableStyle *table_style)
334 AdgTableStylePrivate *data;
336 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
338 data = table_style->data;
340 return data->frame_dress;
344 * adg_table_style_set_grid_dress:
345 * @table_style: an #AdgTableStyle object
346 * @dress: the new line dress
348 * Sets a new line dress on @table_style for rendering the grids.
350 * Since: 1.0
352 void
353 adg_table_style_set_grid_dress(AdgTableStyle *table_style, AdgDress dress)
355 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
356 g_object_set(table_style, "grid-dress", dress, NULL);
360 * adg_table_style_get_grid_dress:
361 * @table_style: an #AdgTableStyle object
363 * Gets the line dress to be used for rendering the grids with
364 * @table_style.
366 * Returns: (transfer none): the line dress.
368 * Since: 1.0
370 AdgDress
371 adg_table_style_get_grid_dress(AdgTableStyle *table_style)
373 AdgTableStylePrivate *data;
375 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
377 data = table_style->data;
379 return data->grid_dress;
383 * adg_table_style_set_title_dress:
384 * @table_style: an #AdgTableStyle object
385 * @dress: the new font dress
387 * Sets a new font dress on @table_style for rendering cell titles.
389 * Since: 1.0
391 void
392 adg_table_style_set_title_dress(AdgTableStyle *table_style, AdgDress dress)
394 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
395 g_object_set(table_style, "title-dress", dress, NULL);
399 * adg_table_style_get_title_dress:
400 * @table_style: an #AdgTableStyle object
402 * Gets the font dress to be used for rendering cell titles
403 * with @table_style.
405 * Returns: (transfer none): the font dress.
407 * Since: 1.0
409 AdgDress
410 adg_table_style_get_title_dress(AdgTableStyle *table_style)
412 AdgTableStylePrivate *data;
414 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
416 data = table_style->data;
418 return data->title_dress;
422 * adg_table_style_set_value_dress:
423 * @table_style: an #AdgTableStyle object
424 * @dress: the new font dress
426 * Sets a new font dress on @table_style for rendering cell values.
428 * Since: 1.0
430 void
431 adg_table_style_set_value_dress(AdgTableStyle *table_style, AdgDress dress)
433 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
434 g_object_set(table_style, "value-dress", dress, NULL);
438 * adg_table_style_get_value_dress:
439 * @table_style: an #AdgTableStyle object
441 * Gets the font dress to be used for rendering cell values
442 * with @table_style.
444 * Returns: (transfer none): the font dress.
446 * Since: 1.0
448 AdgDress
449 adg_table_style_get_value_dress(AdgTableStyle *table_style)
451 AdgTableStylePrivate *data;
453 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
455 data = table_style->data;
457 return data->value_dress;
461 * adg_table_style_set_row_height:
462 * @table_style: an #AdgTableStyle object
463 * @height: the new row heigth fallback
465 * Sets a new #AdgTableStyle:row-height fallback. @height must
466 * be a valid row height greather than %0 or a warning will be
467 * raised and this function will fail.
469 * Since: 1.0
471 void
472 adg_table_style_set_row_height(AdgTableStyle *table_style, gdouble height)
474 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
475 g_object_set(table_style, "row-height", height, NULL);
479 * adg_table_style_get_row_height:
480 * @table_style: an #AdgTableStyle object
482 * Gets the row height fallback value.
484 * Returns: the fallback row height or %0 on errors.
486 * Since: 1.0
488 gdouble
489 adg_table_style_get_row_height(AdgTableStyle *table_style)
491 AdgTableStylePrivate *data;
493 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), 0);
495 data = table_style->data;
497 return data->row_height;
501 * adg_table_style_set_cell_padding:
502 * @table_style: an #AdgTableStyle object
503 * @padding: the new padding values
505 * Sets new #AdgTableStyle:cell-padding values.
507 * Since: 1.0
509 void
510 adg_table_style_set_cell_padding(AdgTableStyle *table_style,
511 const CpmlPair *padding)
513 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
514 g_object_set(table_style, "cell-padding", padding, NULL);
518 * adg_table_style_get_cell_padding:
519 * @table_style: an #AdgTableStyle object
521 * Gets the padding values in x and y to be left clear inside the cells.
522 * The returned pointer refers to an internal allocated struct and
523 * must not be modified or freed.
525 * The cell padding is a symmetric value, that is the padding on the
526 * left will always be equal to the padding on the right and the top
527 * will always be equal to the bottom.
529 * Returns: (transfer none): the cell padding values or %NULL on errors.
531 * Since: 1.0
533 const CpmlPair *
534 adg_table_style_get_cell_padding(AdgTableStyle *table_style)
536 AdgTableStylePrivate *data;
538 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), NULL);
540 data = table_style->data;
542 return &data->cell_padding;
546 * adg_table_style_set_cell_spacing:
547 * @table_style: an #AdgTableStyle object
548 * @spacing: the new spacing values
550 * Sets new #AdgTableStyle:cell-spacing values.
552 * Since: 1.0
554 void
555 adg_table_style_set_cell_spacing(AdgTableStyle *table_style,
556 const CpmlPair *spacing)
558 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
559 g_object_set(table_style, "cell-spacing", spacing, NULL);
563 * adg_table_style_get_cell_spacing:
564 * @table_style: an #AdgTableStyle object
566 * Gets the spacing values in x and y to be left between the cell
567 * boundary boxes. The returned pointer refers to an internal
568 * allocated struct and must not be modified or freed.
570 * The cell spacing is a symmetric value, that is the spacing on the
571 * left will always be equal to the spacing on the right and the top
572 * will always be equal to the bottom.
574 * Returns: (transfer none): the cell spacing values or %NULL on errors.
576 * Since: 1.0
578 const CpmlPair *
579 adg_table_style_get_cell_spacing(AdgTableStyle *table_style)
581 AdgTableStylePrivate *data;
583 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), NULL);
585 data = table_style->data;
587 return &data->cell_spacing;
591 static void
592 _adg_apply(AdgStyle *style, AdgEntity *entity, cairo_t *cr)
594 AdgTableStylePrivate *data = ((AdgTableStyle *) style)->data;
596 adg_entity_apply_dress(entity, data->color_dress, cr);