doc: update copyright line for 2021
[adg.git] / src / adg / adg-table-style.c
blob363df2516223c3960a92431ac5e903d47630ab81
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2021 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_WITH_PRIVATE(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 gobject_class->get_property = _adg_get_property;
88 gobject_class->set_property = _adg_set_property;
90 style_class->apply = _adg_apply;
92 param = adg_param_spec_dress("color-dress",
93 P_("Color Dress"),
94 P_("Fallback color dress, used when no specific dresses are selected"),
95 ADG_DRESS_COLOR_ANNOTATION,
96 G_PARAM_READWRITE);
97 g_object_class_install_property(gobject_class, PROP_COLOR_DRESS, param);
99 param = adg_param_spec_dress("grid-dress",
100 P_("Grid Dress"),
101 P_("Line dress to use while rendering the grid of the table"),
102 ADG_DRESS_LINE_GRID,
103 G_PARAM_READWRITE);
104 g_object_class_install_property(gobject_class, PROP_GRID_DRESS, param);
106 param = adg_param_spec_dress("frame-dress",
107 P_("Frame Dress"),
108 P_("Line dress to use while drawing the table frame"),
109 ADG_DRESS_LINE_FRAME,
110 G_PARAM_READWRITE);
111 g_object_class_install_property(gobject_class, PROP_FRAME_DRESS, param);
113 param = adg_param_spec_dress("title-dress",
114 P_("Title Dress"),
115 P_("Font dress to use for titles"),
116 ADG_DRESS_FONT_ANNOTATION,
117 G_PARAM_READWRITE);
118 g_object_class_install_property(gobject_class, PROP_TITLE_DRESS, param);
120 param = adg_param_spec_dress("value-dress",
121 P_("Value Dress"),
122 P_("Font dress to use for values inside the cells"),
123 ADG_DRESS_FONT_TEXT,
124 G_PARAM_READWRITE);
125 g_object_class_install_property(gobject_class, PROP_VALUE_DRESS, param);
127 param = g_param_spec_double("row-height",
128 P_("Row Height"),
129 P_("The fallback row height when not explicitely specified while creating a new row"),
130 0, G_MAXDOUBLE, 30,
131 G_PARAM_READWRITE);
132 g_object_class_install_property(gobject_class, PROP_ROW_HEIGHT, param);
134 param = g_param_spec_boxed("cell-padding",
135 P_("Cell Padding"),
136 P_("How much space from the bounding box must left inside every cell"),
137 CPML_TYPE_PAIR,
138 G_PARAM_READWRITE);
139 g_object_class_install_property(gobject_class, PROP_CELL_PADDING, param);
141 param = g_param_spec_boxed("cell-spacing",
142 P_("Cell Spacing"),
143 P_("How much space to left between the cells"),
144 CPML_TYPE_PAIR,
145 G_PARAM_READWRITE);
146 g_object_class_install_property(gobject_class, PROP_CELL_SPACING, param);
149 static void
150 adg_table_style_init(AdgTableStyle *table_style)
152 AdgTableStylePrivate *data = adg_table_style_get_instance_private(table_style);
154 data->color_dress = ADG_DRESS_COLOR_ANNOTATION,
155 data->grid_dress = ADG_DRESS_LINE_GRID;
156 data->frame_dress = ADG_DRESS_LINE_FRAME;
157 data->title_dress = ADG_DRESS_FONT_ANNOTATION;
158 data->value_dress = ADG_DRESS_FONT_TEXT;
159 data->row_height = 30;
160 data->cell_padding.x = 3;
161 data->cell_padding.y = 3;
162 data->cell_spacing.x = 0;
163 data->cell_spacing.y = 0;
165 table_style->data = data;
168 static void
169 _adg_get_property(GObject *object, guint prop_id,
170 GValue *value, GParamSpec *pspec)
172 AdgTableStylePrivate *data = ((AdgTableStyle *) object)->data;
174 switch (prop_id) {
175 case PROP_COLOR_DRESS:
176 g_value_set_enum(value, data->color_dress);
177 break;
178 case PROP_GRID_DRESS:
179 g_value_set_enum(value, data->grid_dress);
180 break;
181 case PROP_FRAME_DRESS:
182 g_value_set_enum(value, data->frame_dress);
183 break;
184 case PROP_TITLE_DRESS:
185 g_value_set_enum(value, data->title_dress);
186 break;
187 case PROP_VALUE_DRESS:
188 g_value_set_enum(value, data->value_dress);
189 break;
190 case PROP_ROW_HEIGHT:
191 g_value_set_double(value, data->row_height);
192 break;
193 case PROP_CELL_PADDING:
194 g_value_set_boxed(value, &data->cell_padding);
195 break;
196 case PROP_CELL_SPACING:
197 g_value_set_boxed(value, &data->cell_spacing);
198 break;
199 default:
200 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
201 break;
205 static void
206 _adg_set_property(GObject *object, guint prop_id,
207 const GValue *value, GParamSpec *pspec)
209 AdgTableStylePrivate *data = ((AdgTableStyle *) object)->data;
211 switch (prop_id) {
212 case PROP_COLOR_DRESS:
213 data->color_dress = g_value_get_enum(value);
214 break;
215 case PROP_GRID_DRESS:
216 data->grid_dress = g_value_get_enum(value);
217 break;
218 case PROP_FRAME_DRESS:
219 data->frame_dress = g_value_get_enum(value);
220 break;
221 case PROP_TITLE_DRESS:
222 data->title_dress = g_value_get_enum(value);
223 break;
224 case PROP_VALUE_DRESS:
225 data->value_dress = g_value_get_enum(value);
226 break;
227 case PROP_ROW_HEIGHT:
228 data->row_height = g_value_get_double(value);
229 break;
230 case PROP_CELL_PADDING:
231 cpml_pair_copy(&data->cell_padding, g_value_get_boxed(value));
232 break;
233 case PROP_CELL_SPACING:
234 cpml_pair_copy(&data->cell_spacing, g_value_get_boxed(value));
235 break;
236 default:
237 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
238 break;
244 * adg_table_style_new:
246 * Constructs a new empty table style initialized with default params.
248 * Returns: (transfer full): a new table style.
250 * Since: 1.0
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 * Since: 1.0
267 void
268 adg_table_style_set_color_dress(AdgTableStyle *table_style, AdgDress dress)
270 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
271 g_object_set(table_style, "color-dress", dress, NULL);
275 * adg_table_style_get_color_dress:
276 * @table_style: an #AdgTableStyle object
278 * Gets the @table_style color dress to be used. This dress should be
279 * intended as a fallback color as it could be overriden by more
280 * specific dresses, such as a color explicitely specified on the
281 * #AdgTableStyle:value-dress.
283 * Returns: (transfer none): the color dress.
285 * Since: 1.0
287 AdgDress
288 adg_table_style_get_color_dress(AdgTableStyle *table_style)
290 AdgTableStylePrivate *data;
292 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
294 data = table_style->data;
296 return data->color_dress;
300 * adg_table_style_set_frame_dress:
301 * @table_style: an #AdgTableStyle object
302 * @dress: the new line dress
304 * Sets a new line dress on @table_style for rendering the frames.
306 * Since: 1.0
308 void
309 adg_table_style_set_frame_dress(AdgTableStyle *table_style, AdgDress dress)
311 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
312 g_object_set(table_style, "frame-dress", dress, NULL);
316 * adg_table_style_get_frame_dress:
317 * @table_style: an #AdgTableStyle object
319 * Gets the line dress to be used for rendering the frames with
320 * @table_style.
322 * Returns: (transfer none): the line dress.
324 * Since: 1.0
326 AdgDress
327 adg_table_style_get_frame_dress(AdgTableStyle *table_style)
329 AdgTableStylePrivate *data;
331 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
333 data = table_style->data;
335 return data->frame_dress;
339 * adg_table_style_set_grid_dress:
340 * @table_style: an #AdgTableStyle object
341 * @dress: the new line dress
343 * Sets a new line dress on @table_style for rendering the grids.
345 * Since: 1.0
347 void
348 adg_table_style_set_grid_dress(AdgTableStyle *table_style, AdgDress dress)
350 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
351 g_object_set(table_style, "grid-dress", dress, NULL);
355 * adg_table_style_get_grid_dress:
356 * @table_style: an #AdgTableStyle object
358 * Gets the line dress to be used for rendering the grids with
359 * @table_style.
361 * Returns: (transfer none): the line dress.
363 * Since: 1.0
365 AdgDress
366 adg_table_style_get_grid_dress(AdgTableStyle *table_style)
368 AdgTableStylePrivate *data;
370 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
372 data = table_style->data;
374 return data->grid_dress;
378 * adg_table_style_set_title_dress:
379 * @table_style: an #AdgTableStyle object
380 * @dress: the new font dress
382 * Sets a new font dress on @table_style for rendering cell titles.
384 * Since: 1.0
386 void
387 adg_table_style_set_title_dress(AdgTableStyle *table_style, AdgDress dress)
389 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
390 g_object_set(table_style, "title-dress", dress, NULL);
394 * adg_table_style_get_title_dress:
395 * @table_style: an #AdgTableStyle object
397 * Gets the font dress to be used for rendering cell titles
398 * with @table_style.
400 * Returns: (transfer none): the font dress.
402 * Since: 1.0
404 AdgDress
405 adg_table_style_get_title_dress(AdgTableStyle *table_style)
407 AdgTableStylePrivate *data;
409 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
411 data = table_style->data;
413 return data->title_dress;
417 * adg_table_style_set_value_dress:
418 * @table_style: an #AdgTableStyle object
419 * @dress: the new font dress
421 * Sets a new font dress on @table_style for rendering cell values.
423 * Since: 1.0
425 void
426 adg_table_style_set_value_dress(AdgTableStyle *table_style, AdgDress dress)
428 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
429 g_object_set(table_style, "value-dress", dress, NULL);
433 * adg_table_style_get_value_dress:
434 * @table_style: an #AdgTableStyle object
436 * Gets the font dress to be used for rendering cell values
437 * with @table_style.
439 * Returns: (transfer none): the font dress.
441 * Since: 1.0
443 AdgDress
444 adg_table_style_get_value_dress(AdgTableStyle *table_style)
446 AdgTableStylePrivate *data;
448 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
450 data = table_style->data;
452 return data->value_dress;
456 * adg_table_style_set_row_height:
457 * @table_style: an #AdgTableStyle object
458 * @height: the new row heigth fallback
460 * Sets a new #AdgTableStyle:row-height fallback. @height must
461 * be a valid row height greather than 0 or a warning will be
462 * raised and this function will fail.
464 * Since: 1.0
466 void
467 adg_table_style_set_row_height(AdgTableStyle *table_style, gdouble height)
469 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
470 g_object_set(table_style, "row-height", height, NULL);
474 * adg_table_style_get_row_height:
475 * @table_style: an #AdgTableStyle object
477 * Gets the row height fallback value.
479 * Returns: the fallback row height or 0 on errors.
481 * Since: 1.0
483 gdouble
484 adg_table_style_get_row_height(AdgTableStyle *table_style)
486 AdgTableStylePrivate *data;
488 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), 0);
490 data = table_style->data;
492 return data->row_height;
496 * adg_table_style_set_cell_padding:
497 * @table_style: an #AdgTableStyle object
498 * @padding: the new padding values
500 * Sets new #AdgTableStyle:cell-padding values.
502 * Since: 1.0
504 void
505 adg_table_style_set_cell_padding(AdgTableStyle *table_style,
506 const CpmlPair *padding)
508 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
509 g_object_set(table_style, "cell-padding", padding, NULL);
513 * adg_table_style_get_cell_padding:
514 * @table_style: an #AdgTableStyle object
516 * Gets the padding values in x and y to be left clear inside the cells.
517 * The returned pointer refers to an internal allocated struct and
518 * must not be modified or freed.
520 * The cell padding is a symmetric value, that is the padding on the
521 * left will always be equal to the padding on the right and the top
522 * will always be equal to the bottom.
524 * Returns: (transfer none): the cell padding values or <constant>NULL</constant> on errors.
526 * Since: 1.0
528 const CpmlPair *
529 adg_table_style_get_cell_padding(AdgTableStyle *table_style)
531 AdgTableStylePrivate *data;
533 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), NULL);
535 data = table_style->data;
537 return &data->cell_padding;
541 * adg_table_style_set_cell_spacing:
542 * @table_style: an #AdgTableStyle object
543 * @spacing: the new spacing values
545 * Sets new #AdgTableStyle:cell-spacing values.
547 * Since: 1.0
549 void
550 adg_table_style_set_cell_spacing(AdgTableStyle *table_style,
551 const CpmlPair *spacing)
553 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
554 g_object_set(table_style, "cell-spacing", spacing, NULL);
558 * adg_table_style_get_cell_spacing:
559 * @table_style: an #AdgTableStyle object
561 * Gets the spacing values in x and y to be left between the cell
562 * boundary boxes. The returned pointer refers to an internal
563 * allocated struct and must not be modified or freed.
565 * The cell spacing is a symmetric value, that is the spacing on the
566 * left will always be equal to the spacing on the right and the top
567 * will always be equal to the bottom.
569 * Returns: (transfer none): the cell spacing values or <constant>NULL</constant> on errors.
571 * Since: 1.0
573 const CpmlPair *
574 adg_table_style_get_cell_spacing(AdgTableStyle *table_style)
576 AdgTableStylePrivate *data;
578 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), NULL);
580 data = table_style->data;
582 return &data->cell_spacing;
586 static void
587 _adg_apply(AdgStyle *style, AdgEntity *entity, cairo_t *cr)
589 AdgTableStylePrivate *data = ((AdgTableStyle *) style)->data;
591 adg_entity_apply_dress(entity, data->color_dress, cr);