[AdgWidget] Reordered methods
[adg.git] / adg / adg-table-style.c
blob8358cbc02159685b6181ed4213a1dc16af80bc38
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009 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-table-style.h"
38 #include "adg-table-style-private.h"
39 #include "adg-dress-builtins.h"
40 #include "adg-font-style.h"
41 #include "adg-line-style.h"
42 #include "adg-intl.h"
43 #include "adg-util.h"
46 enum {
47 PROP_0,
48 PROP_COLOR_DRESS,
49 PROP_GRID_DRESS,
50 PROP_FRAME_DRESS,
51 PROP_TITLE_DRESS,
52 PROP_VALUE_DRESS,
53 PROP_ROW_HEIGHT,
54 PROP_CELL_PADDING,
55 PROP_CELL_SPACING
59 static void get_property (GObject *object,
60 guint prop_id,
61 GValue *value,
62 GParamSpec *pspec);
63 static void set_property (GObject *object,
64 guint prop_id,
65 const GValue *value,
66 GParamSpec *pspec);
67 static void apply (AdgStyle *style,
68 AdgEntity *entity,
69 cairo_t *cr);
72 G_DEFINE_TYPE(AdgTableStyle, adg_table_style, ADG_TYPE_STYLE);
75 static void
76 adg_table_style_class_init(AdgTableStyleClass *klass)
78 GObjectClass *gobject_class;
79 AdgStyleClass *style_class;
80 GParamSpec *param;
82 gobject_class = (GObjectClass *) klass;
83 style_class = (AdgStyleClass *) klass;
85 g_type_class_add_private(klass, sizeof(AdgTableStylePrivate));
87 gobject_class->get_property = get_property;
88 gobject_class->set_property = set_property;
90 style_class->apply = apply;
92 param = adg_param_spec_dress("color-dress",
93 P_("Color Dress"),
94 P_("Color dress for the whole tableension"),
95 ADG_DRESS_COLOR,
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_TEXT_LIMIT,
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_TEXT_VALUE,
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 ADG_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 ADG_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;
154 data = G_TYPE_INSTANCE_GET_PRIVATE(table_style, ADG_TYPE_TABLE_STYLE,
155 AdgTableStylePrivate);
157 data->color_dress = ADG_DRESS_COLOR;
158 data->grid_dress = ADG_DRESS_LINE_GRID;
159 data->frame_dress = ADG_DRESS_LINE_FRAME;
160 data->title_dress = ADG_DRESS_TEXT_LIMIT;
161 data->value_dress = ADG_DRESS_TEXT_VALUE;
162 data->row_height = 30;
163 data->cell_padding.x = 5;
164 data->cell_padding.y = 5;
165 data->cell_spacing.x = 0;
166 data->cell_spacing.y = 0;
168 table_style->data = data;
171 static void
172 get_property(GObject *object, guint prop_id, 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 set_property(GObject *object,
209 guint prop_id, const GValue *value, GParamSpec *pspec)
211 AdgTableStylePrivate *data = ((AdgTableStyle *) object)->data;
213 switch (prop_id) {
214 case PROP_COLOR_DRESS:
215 adg_dress_set(&data->color_dress, g_value_get_int(value));
216 break;
217 case PROP_GRID_DRESS:
218 adg_dress_set(&data->grid_dress, g_value_get_int(value));
219 break;
220 case PROP_FRAME_DRESS:
221 adg_dress_set(&data->frame_dress, g_value_get_int(value));
222 break;
223 case PROP_TITLE_DRESS:
224 adg_dress_set(&data->title_dress, g_value_get_int(value));
225 break;
226 case PROP_VALUE_DRESS:
227 adg_dress_set(&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 cpml_pair_copy(&data->cell_padding, g_value_get_boxed(value));
234 break;
235 case PROP_CELL_SPACING:
236 cpml_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 tableension style initialized with default params.
250 * Returns: a new tableension style
252 AdgStyle *
253 adg_table_style_new(void)
255 return g_object_new(ADG_TYPE_TABLE_STYLE, NULL);
259 * adg_table_style_get_color_dress:
260 * @table_style: an #AdgTableStyle object
262 * Gets the @table_style color dress to be used. This dress should be
263 * intended as a fallback color as it could be overriden by more
264 * specific dresses, such as a color explicitely specified on the
265 * #AdgTableStyle:value-dress.
267 * Returns: the color dress
269 AdgDress
270 adg_table_style_get_color_dress(AdgTableStyle *table_style)
272 AdgTableStylePrivate *data;
274 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
276 data = table_style->data;
278 return data->color_dress;
282 * adg_table_style_set_color_dress:
283 * @table_style: an #AdgTableStyle object
284 * @dress: the new color dress
286 * Sets a new color dress on @table_style.
288 void
289 adg_table_style_set_color_dress(AdgTableStyle *table_style, AdgDress dress)
291 AdgTableStylePrivate *data;
293 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
295 data = table_style->data;
297 if (adg_dress_set(&data->color_dress, dress))
298 g_object_notify((GObject *) table_style, "color-dress");
302 * adg_table_style_get_frame_dress:
303 * @table_style: an #AdgTableStyle object
305 * Gets the line dress to be used for rendering the frames with
306 * @table_style.
308 * Returns: the line dress
310 AdgDress
311 adg_table_style_get_frame_dress(AdgTableStyle *table_style)
313 AdgTableStylePrivate *data;
315 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
317 data = table_style->data;
319 return data->frame_dress;
323 * adg_table_style_set_frame_dress:
324 * @table_style: an #AdgTableStyle object
325 * @dress: the new line dress
327 * Sets a new line dress on @table_style for rendering the frames.
329 void
330 adg_table_style_set_frame_dress(AdgTableStyle *table_style, AdgDress dress)
332 AdgTableStylePrivate *data;
334 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
336 data = table_style->data;
338 if (adg_dress_set(&data->frame_dress, dress))
339 g_object_notify((GObject *) table_style, "frame-dress");
343 * adg_table_style_get_grid_dress:
344 * @table_style: an #AdgTableStyle object
346 * Gets the line dress to be used for rendering the grids with
347 * @table_style.
349 * Returns: the line dress
351 AdgDress
352 adg_table_style_get_grid_dress(AdgTableStyle *table_style)
354 AdgTableStylePrivate *data;
356 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
358 data = table_style->data;
360 return data->grid_dress;
364 * adg_table_style_set_grid_dress:
365 * @table_style: an #AdgTableStyle object
366 * @dress: the new line dress
368 * Sets a new line dress on @table_style for rendering the grids.
370 void
371 adg_table_style_set_grid_dress(AdgTableStyle *table_style, AdgDress dress)
373 AdgTableStylePrivate *data;
375 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
377 data = table_style->data;
379 if (adg_dress_set(&data->grid_dress, dress))
380 g_object_notify((GObject *) table_style, "grid-dress");
384 * adg_table_style_get_title_dress:
385 * @table_style: an #AdgTableStyle object
387 * Gets the font dress to be used for rendering cell titles
388 * with @table_style.
390 * Returns: the font dress
392 AdgDress
393 adg_table_style_get_title_dress(AdgTableStyle *table_style)
395 AdgTableStylePrivate *data;
397 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
399 data = table_style->data;
401 return data->title_dress;
405 * adg_table_style_set_title_dress:
406 * @table_style: an #AdgTableStyle object
407 * @dress: the new font dress
409 * Sets a new font dress on @table_style for rendering cell titles.
411 void
412 adg_table_style_set_title_dress(AdgTableStyle *table_style, AdgDress dress)
414 AdgTableStylePrivate *data;
416 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
418 data = table_style->data;
420 if (adg_dress_set(&data->title_dress, dress))
421 g_object_notify((GObject *) table_style, "title-dress");
425 * adg_table_style_get_value_dress:
426 * @table_style: an #AdgTableStyle object
428 * Gets the font dress to be used for rendering cell values
429 * with @table_style.
431 * Returns: the font dress
433 AdgDress
434 adg_table_style_get_value_dress(AdgTableStyle *table_style)
436 AdgTableStylePrivate *data;
438 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), ADG_DRESS_UNDEFINED);
440 data = table_style->data;
442 return data->value_dress;
446 * adg_table_style_set_value_dress:
447 * @table_style: an #AdgTableStyle object
448 * @dress: the new font dress
450 * Sets a new font dress on @table_style for rendering cell values.
452 void
453 adg_table_style_set_value_dress(AdgTableStyle *table_style, AdgDress dress)
455 AdgTableStylePrivate *data;
457 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
459 data = table_style->data;
461 if (adg_dress_set(&data->value_dress, dress))
462 g_object_notify((GObject *) table_style, "value-dress");
466 * adg_table_style_get_row_height:
467 * @table_style: an #AdgTableStyle object
469 * Gets the row height fallback value.
471 * Returns: the fallback row height or %0 on errors
473 gdouble
474 adg_table_style_get_row_height(AdgTableStyle *table_style)
476 AdgTableStylePrivate *data;
478 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), 0);
480 data = table_style->data;
482 return data->row_height;
486 * adg_table_style_set_row_height:
487 * @table_style: an #AdgTableStyle object
488 * @row_heigth: the new row heigth fallback
490 * Sets a new #AdgTableStyle:row-height fallback. @row_height must
491 * be a valid row height greather than %0 or a warning will be
492 * raised and this function will fail.
494 void
495 adg_table_style_set_row_height(AdgTableStyle *table_style, gdouble row_height)
497 AdgTableStylePrivate *data;
499 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
500 g_return_if_fail(row_height > 0);
502 data = table_style->data;
504 if (row_height == data->row_height)
505 return;
507 data->row_height = row_height;
509 g_object_notify((GObject *) table_style, "row-height");
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: the cell padding values or %NULL on errors
526 const AdgPair *
527 adg_table_style_get_cell_padding(AdgTableStyle *table_style)
529 AdgTableStylePrivate *data;
531 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), NULL);
533 data = table_style->data;
535 return &data->cell_padding;
539 * adg_table_style_set_cell_padding:
540 * @table_style: an #AdgTableStyle object
541 * @cell_padding: the new padding values
543 * Sets new #AdgTableStyle:cell-padding values.
545 void
546 adg_table_style_set_cell_padding(AdgTableStyle *table_style,
547 const AdgPair *cell_padding)
549 AdgTableStylePrivate *data;
551 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
552 g_return_if_fail(cell_padding != NULL);
554 data = table_style->data;
555 cpml_pair_copy(&data->cell_padding, cell_padding);
557 g_object_notify((GObject *) table_style, "cell-padding");
561 * adg_table_style_get_cell_spacing:
562 * @table_style: an #AdgTableStyle object
564 * Gets the spacing values in x and y to be left between the cell
565 * boundary boxes. The returned pointer refers to an internal
566 * allocated struct and must not be modified or freed.
568 * The cell spacing is a symmetric value, that is the spacing on the
569 * left will always be equal to the spacing on the right and the top
570 * will always be equal to the bottom.
572 * Returns: the cell spacing values or %NULL on errors
574 const AdgPair *
575 adg_table_style_get_cell_spacing(AdgTableStyle *table_style)
577 AdgTableStylePrivate *data;
579 g_return_val_if_fail(ADG_IS_TABLE_STYLE(table_style), NULL);
581 data = table_style->data;
583 return &data->cell_spacing;
587 * adg_table_style_set_cell_spacing:
588 * @table_style: an #AdgTableStyle object
589 * @cell_spacing: the new spacing values
591 * Sets new #AdgTableStyle:cell-spacing values.
593 void
594 adg_table_style_set_cell_spacing(AdgTableStyle *table_style,
595 const AdgPair *cell_spacing)
597 AdgTableStylePrivate *data;
599 g_return_if_fail(ADG_IS_TABLE_STYLE(table_style));
600 g_return_if_fail(cell_spacing != NULL);
602 data = table_style->data;
603 cpml_pair_copy(&data->cell_spacing, cell_spacing);
605 g_object_notify((GObject *) table_style, "cell-spacing");
609 static void
610 apply(AdgStyle *style, AdgEntity *entity, cairo_t *cr)
612 AdgTableStylePrivate *data = ((AdgTableStyle *) style)->data;
614 adg_entity_apply_dress(entity, data->color_dress, cr);