[AdgColorStyle] s/_get_rgb/_put_rgb/
[adg.git] / adg / adg-table.c
blobe358221f411de9effa99eba286e4f16506ee3c75
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
23 * @short_description: A tabular entity
25 * The #AdgTable is the entity to be used for rendering data arranged
26 * in tabular evironments.
28 * To define a table, you should add to its internal model any number
29 * of row using adg_table_row_new() or adg_table_row_new_before().
31 * Every row could be segmented with different cells by using
32 * adg_table_cell_new() or adg_table_cell_new_before(). Any cell can
33 * be filled with a title and a value: the font to be used will be
34 * picked up from the #AdgTableStyle got by resolving the
35 * #AdgTable:table-dress property.
37 * The default title is placed at the upper left corner of the cell
38 * while the value is centered up to the bottom edge of the cell.
39 * Anyway, the value position can be customized by using the
40 * adg_table_cell_set_value_pos() method. Anyway, both entities react
41 * to the common map displacements.
43 * Some convenient functions to easily create title and value entities
44 * with plain text are provided: adg_table_cell_new_full(),
45 * adg_table_cell_set_text_title() and adg_table_cell_set_text_value().
46 * When using these methods keep in mind the underlying #AdgToyText
47 * entities will be displaced accordingly to the
48 * #AdgTableStyle:cell-padding value (not used when setting the
49 * entities throught other APIs).
50 **/
52 /**
53 * AdgTable:
55 * All fields are private and should not be used directly.
56 * Use its public methods instead.
57 **/
59 /**
60 * AdgTableRow:
62 * An opaque structure referring to a row of an #AdgTable. Any
63 * table can have an unlimited number of rows.
64 **/
66 /**
67 * AdgTableCell:
69 * An opaque structure referring to the cell of an #AdgTableRow.
70 * Any row can have an unlimited number of cells.
71 **/
74 #include "adg-table.h"
75 #include "adg-table-private.h"
76 #include "adg-alignment.h"
77 #include "adg-dress-builtins.h"
78 #include "adg-path.h"
79 #include "adg-line-style.h"
80 #include "adg-toy-text.h"
81 #include "adg-intl.h"
83 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_table_parent_class)
84 #define PARENT_ENTITY_CLASS ((AdgEntityClass *) adg_table_parent_class)
87 enum {
88 PROP_0,
89 PROP_TABLE_DRESS,
90 PROP_HAS_FRAME
93 static void dispose (GObject *object);
94 static void finalize (GObject *object);
95 static void get_property (GObject *object,
96 guint param_id,
97 GValue *value,
98 GParamSpec *pspec);
99 static void set_property (GObject *object,
100 guint param_id,
101 const GValue *value,
102 GParamSpec *pspec);
103 static void global_changed (AdgEntity *entity);
104 static void local_changed (AdgEntity *entity);
105 static void invalidate (AdgEntity *entity);
106 static void arrange (AdgEntity *entity);
107 static void arrange_grid (AdgEntity *entity);
108 static void arrange_frame (AdgEntity *entity);
109 static void render (AdgEntity *entity,
110 cairo_t *cr);
111 static gboolean switch_frame (AdgTable *table,
112 gboolean state);
113 static void propagate (AdgTable *table,
114 const gchar *detailed_signal,
115 ...);
116 static AdgTableRow * row_new (AdgTable *table,
117 AdgTableRow *before_row);
118 static void row_arrange_size (AdgTableRow *row);
119 static void row_arrange (AdgTableRow *row);
120 static void row_dispose (AdgTableRow *row);
121 static void row_free (AdgTableRow *row);
122 static AdgTableCell * cell_new (AdgTableRow *row,
123 AdgTableCell *before_cell,
124 gdouble width,
125 gboolean has_frame,
126 const gchar *name,
127 AdgEntity *title,
128 AdgEntity *value);
129 static void cell_set_name (AdgTableCell *cell,
130 const gchar *name);
131 static gboolean cell_set_title (AdgTableCell *cell,
132 AdgEntity *title);
133 static gboolean cell_set_value (AdgTableCell *cell,
134 AdgEntity *value);
135 static void cell_set_value_pos (AdgTableCell *cell,
136 const AdgPair *from_factor,
137 const AdgPair *to_factor);
138 static void cell_arrange_size (AdgTableCell *cell);
139 static void cell_arrange (AdgTableCell *cell);
140 static void cell_dispose (AdgTableCell *cell);
141 static void cell_free (AdgTableCell *cell);
142 static gboolean value_match (gpointer key,
143 gpointer value,
144 gpointer user_data);
147 G_DEFINE_TYPE(AdgTable, adg_table, ADG_TYPE_ENTITY);
150 static void
151 adg_table_class_init(AdgTableClass *klass)
153 GObjectClass *gobject_class;
154 AdgEntityClass *entity_class;
155 GParamSpec *param;
157 gobject_class = (GObjectClass *) klass;
158 entity_class = (AdgEntityClass *) klass;
160 g_type_class_add_private(klass, sizeof(AdgTablePrivate));
162 gobject_class->dispose = dispose;
163 gobject_class->finalize = finalize;
164 gobject_class->get_property = get_property;
165 gobject_class->set_property = set_property;
167 entity_class->global_changed = global_changed;
168 entity_class->local_changed = local_changed;
169 entity_class->invalidate = invalidate;
170 entity_class->arrange = arrange;
171 entity_class->render = render;
173 param = adg_param_spec_dress("table-dress",
174 P_("Table Dress"),
175 P_("The dress to use for stroking this entity"),
176 ADG_DRESS_TABLE,
177 G_PARAM_READWRITE);
178 g_object_class_install_property(gobject_class, PROP_TABLE_DRESS, param);
180 param = g_param_spec_boolean("has-frame",
181 P_("Has Frame Flag"),
182 P_("If enabled, a frame using the proper dress found in this table style will be drawn around the table extents"),
183 TRUE,
184 G_PARAM_READWRITE);
185 g_object_class_install_property(gobject_class, PROP_HAS_FRAME, param);
188 static void
189 adg_table_init(AdgTable *table)
191 AdgTablePrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(table,
192 ADG_TYPE_TABLE,
193 AdgTablePrivate);
195 data->table_dress = ADG_DRESS_TABLE;
196 data->has_frame = TRUE;
198 data->table_style = NULL;
199 data->grid = NULL;
200 data->frame = NULL;
201 data->rows = NULL;
202 data->cell_names = NULL;
204 table->data = data;
207 static void
208 dispose(GObject *object)
210 AdgTablePrivate *data = ((AdgTable *) object)->data;
212 if (data->grid != NULL) {
213 g_object_unref(data->grid);
214 data->grid = NULL;
217 if (data->frame != NULL) {
218 g_object_unref(data->frame);
219 data->frame = NULL;
222 /* The rows finalization will happen in the finalize() method */
223 if (data->rows != NULL)
224 g_slist_foreach(data->rows, (GFunc) row_dispose, NULL);
226 if (PARENT_OBJECT_CLASS->dispose)
227 PARENT_OBJECT_CLASS->dispose(object);
230 static void
231 finalize(GObject *object)
233 AdgTable *table;
234 AdgTablePrivate *data;
236 table = (AdgTable *) object;
237 data = table->data;
239 if (data->rows != NULL) {
240 g_slist_foreach(data->rows, (GFunc) row_free, NULL);
241 g_slist_free(data->rows);
244 if (data->cell_names != NULL)
245 g_hash_table_destroy(data->cell_names);
247 if (PARENT_OBJECT_CLASS->finalize)
248 PARENT_OBJECT_CLASS->finalize(object);
251 static void
252 get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
254 AdgTablePrivate *data = ((AdgTable *) object)->data;
256 switch (prop_id) {
257 case PROP_TABLE_DRESS:
258 g_value_set_int(value, data->table_dress);
259 break;
260 case PROP_HAS_FRAME:
261 g_value_set_boolean(value, data->has_frame);
262 break;
263 default:
264 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
265 break;
269 static void
270 set_property(GObject *object, guint prop_id,
271 const GValue *value, GParamSpec *pspec)
273 AdgTable *table;
274 AdgTablePrivate *data;
276 table = (AdgTable *) object;
277 data = table->data;
279 switch (prop_id) {
280 case PROP_TABLE_DRESS:
281 adg_dress_set(&data->table_dress, g_value_get_int(value));
282 break;
283 case PROP_HAS_FRAME:
284 switch_frame(table, g_value_get_boolean(value));
285 break;
286 default:
287 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
288 break;
294 * adg_table_new:
296 * Creates a new empty table entity. The #AdgEntity:local-method
297 * property is set by default to #ADG_MIX_DISABLED, that is the
298 * table is not subject to any local transformations.
300 * Returns: the newly created table entity
302 AdgTable *
303 adg_table_new(void)
305 return g_object_new(ADG_TYPE_TABLE,
306 "local-method", ADG_MIX_DISABLED, NULL);
310 * adg_table_get_table_dress:
311 * @table: an #AdgTable
313 * Gets the table dress to be used in rendering @table.
315 * Returns: the current table dress
317 AdgDress
318 adg_table_get_table_dress(AdgTable *table)
320 AdgTablePrivate *data;
322 g_return_val_if_fail(ADG_IS_TABLE(table), ADG_DRESS_UNDEFINED);
324 data = table->data;
326 return data->table_dress;
330 * adg_table_set_table_dress:
331 * @table: an #AdgTable
332 * @dress: the new #AdgDress to use
334 * Sets a new table dress for rendering @table. The new dress
335 * must be related to the original dress for this property:
336 * you cannot set a dress used for line styles to a dress
337 * managing fonts.
339 * The check is done by calling adg_dress_are_related() with
340 * @dress and the previous dress as arguments. Check out its
341 * documentation for details on what is a related dress.
343 void
344 adg_table_set_table_dress(AdgTable *table, AdgDress dress)
346 AdgTablePrivate *data;
348 g_return_if_fail(ADG_IS_TABLE(table));
350 data = table->data;
352 if (adg_dress_set(&data->table_dress, dress))
353 g_object_notify((GObject *) table, "table-dress");
357 * adg_table_has_frame:
358 * @table: an #AdgTable
360 * Returns the state of the #AdgTable:has-frame property.
362 * Returns: the current state
364 gboolean
365 adg_table_has_frame(AdgTable *table)
367 AdgTablePrivate *data;
369 g_return_val_if_fail(ADG_IS_TABLE(table), FALSE);
371 data = table->data;
373 return data->has_frame;
377 * adg_table_switch_frame:
378 * @table: an #AdgTable
379 * @state: the new state of the frame
381 * Sets the #AdgTable:has-frame property: %TRUE will draw a
382 * frame around the whole table using the #AdgTableStyle:frame-dress
383 * dress of the table style.
385 void
386 adg_table_switch_frame(AdgTable *table, gboolean state)
388 g_return_if_fail(ADG_IS_TABLE(table));
390 if (switch_frame(table, state))
391 g_object_notify((GObject *) table, "has-frame");
395 * adg_table_get_n_rows:
396 * @table: an #AdgTable
398 * Gets the number of rows stored in @table.
400 * Returns: the number of rows or %0 on empty @table or errors
402 guint
403 adg_table_get_n_rows(AdgTable *table)
405 AdgTablePrivate *data;
407 g_return_val_if_fail(ADG_IS_TABLE(table), 0);
409 data = table->data;
411 if (data->rows == NULL)
412 return 0;
414 return g_slist_length(data->rows);
418 * adg_table_row_new:
419 * @table: an #AdgTable
421 * Creates a new empty row and appends it at the end of the rows
422 * yet present in @table. By default, the height of this new
423 * row will be the fallback value provided by the table style:
424 * you can override it by using adg_table_row_set_height().
426 * Returns: the newly created row or %NULL on errors
428 AdgTableRow *
429 adg_table_row_new(AdgTable *table)
431 g_return_val_if_fail(ADG_IS_TABLE(table), NULL);
433 return row_new(table, NULL);
437 * adg_table_row_new_before:
438 * @row: a valid #AdgTableRow
440 * Creates a new empty row with default height and inserts it
441 * just before @row.
443 * Returns: the newly created row or %NULL on errors
445 AdgTableRow *
446 adg_table_row_new_before(AdgTableRow *row)
448 g_return_val_if_fail(row != NULL, NULL);
449 g_return_val_if_fail(ADG_IS_TABLE(row->table), NULL);
451 return row_new(row->table, row);
455 * adg_table_row_delete:
456 * @row: a valid #AdgTableRow
458 * Removes @row from its owner table and frees every resources allocated
459 * by it. This means also the eventual cells owned by @row will be freed.
461 void
462 adg_table_row_delete(AdgTableRow *row)
464 AdgTable *table;
465 AdgTablePrivate *data;
467 g_return_if_fail(row != NULL);
469 table = row->table;
471 g_return_if_fail(ADG_IS_TABLE(table));
473 data = table->data;
475 g_slist_foreach(row->cells, (GFunc) cell_free, NULL);
476 g_slist_free(row->cells);
477 data->rows = g_slist_remove(data->rows, row);
479 g_free(row);
483 * adg_table_row_get_n_cells:
484 * @row: a valid #AdgTableRow
486 * Gets the number of cells stored in @row.
488 * Returns: the number of cells or %0 on empty row or errors
490 guint
491 adg_table_row_get_n_cells(const AdgTableRow *row)
493 g_return_val_if_fail(row != NULL, 0);
495 if (row->cells == NULL)
496 return 0;
498 return g_slist_length(row->cells);
502 * adg_table_row_set_height:
503 * @row: a valid #AdgTableRow
504 * @height: the new height
506 * Sets a new height on @row. The extents will be invalidated to
507 * recompute the whole layout of the table. Specifying %0 in
508 * @height will use the default height set in the table style.
510 void
511 adg_table_row_set_height(AdgTableRow *row, gdouble height)
513 g_return_if_fail(row != NULL);
515 row->height = height;
517 adg_entity_invalidate((AdgEntity *) row->table);
521 * adg_table_row_extents:
522 * @row: a valid #AdgTableRow
524 * Gets the extents of @row. This function is useful only after the
525 * arrange() phase as in the other situation the extents will likely
526 * be not up to date.
528 * Returns: the extents of @row or %NULL on errors
530 const CpmlExtents *
531 adg_table_row_extents(AdgTableRow *row)
533 g_return_val_if_fail(row != NULL, NULL);
535 return &row->extents;
539 * adg_table_get_cell:
540 * @table: an #AdgTable
541 * @name: the name of a cell
543 * Gets the cell named @name inside @table. Only named cells can be
544 * retrieved by this method.
546 * Returns: the requested cell or %NULL if not found
548 AdgTableCell *
549 adg_table_get_cell(AdgTable *table, const gchar *name)
551 AdgTablePrivate *data;
553 g_return_val_if_fail(ADG_IS_TABLE(table), NULL);
555 data = table->data;
557 if (data->cell_names == NULL)
558 return NULL;
560 return g_hash_table_lookup(data->cell_names, name);
564 * adg_table_cell_new:
565 * @row: a valid #AdgTableRow
566 * @width: width of the cell
568 * Creates a new empty cell without a frame and appends it at the
569 * end of the cells yet present in @row. You can add content to the
570 * cell by using adg_table_cell_set_title() and
571 * adg_table_cell_set_value() or enable the frame with
572 * adg_table_cell_switch_frame().
574 * A positive @width value specifies the width of this cell in global
575 * space: if the width of its content (that is, either the title or the
576 * value entity) will be greater than @width, it will be rendered
577 * outside the cell boundary box, luckely overwriting the adiacent
578 * cells.
580 * Using %0 as @width means the width of the cell will be automatically
581 * adjusted to the maximum width of its content.
583 * Negative width values are not allowed: this condition will raise
584 * a warning without any further processing.
586 * Returns: the newly created cell or %NULL on errors
588 AdgTableCell *
589 adg_table_cell_new(AdgTableRow *row, gdouble width)
591 g_return_val_if_fail(row != NULL, NULL);
592 g_return_val_if_fail(width >= 0, NULL);
594 return cell_new(row, NULL, width, FALSE, NULL, NULL, NULL);
598 * adg_table_cell_new_before:
599 * @cell: a valid #AdgTableCell
600 * @width: width of the cell
602 * Creates a new cell and inserts it rigthly before the @cell cell.
603 * This works similarily and accepts the same parameters as the
604 * adg_table_cell_new() function.
606 * Returns: the newly created cell or %NULL on errors
608 AdgTableCell *
609 adg_table_cell_new_before(AdgTableCell *cell, gdouble width)
611 g_return_val_if_fail(cell != NULL, NULL);
612 g_return_val_if_fail(cell->row != NULL, NULL);
613 g_return_val_if_fail(width >= 0, NULL);
615 return cell_new(cell->row, cell, width, FALSE, NULL, NULL, NULL);
619 * adg_table_cell_new_full:
620 * @row: a valid #AdgTableRow
621 * @width: width of the cell
622 * @name: name to associate
623 * @title: title to render
624 * @value: value to render
626 * A convenient function to append a framed cell to @row with a
627 * specific title and value text. The font to use for rendering
628 * @title and @value will be picked up from the table style, so
629 * be sure to have the correct table dress set before calling
630 * this function.
632 * @row and @width have the same meanings as in adg_table_cell_new():
633 * check its documentation for details.
635 * @name is an optional identifier to univoquely access this cell
636 * by using adg_table_get_cell(). The identifier must be univoque:
637 * if there is yet a cell with the same name a warning message will
638 * be raised and the function will fail.
640 * Returns: the newly created cell or %NULL on errors
642 AdgTableCell *
643 adg_table_cell_new_full(AdgTableRow *row, gdouble width, const gchar *name,
644 const gchar *title, const gchar *value)
646 AdgTableCell *cell;
648 g_return_val_if_fail(row != NULL, NULL);
650 cell = cell_new(row, NULL, width, TRUE, name, NULL, NULL);
652 if (title != NULL)
653 adg_table_cell_set_text_title(cell, title);
655 if (value != NULL)
656 adg_table_cell_set_text_value(cell, value);
658 return cell;
662 * adg_table_cell_delete:
663 * @cell: a valid #AdgTableCell
665 * Deletes @cell removing it from the container row and freeing
666 * any resource associated to it.
668 void
669 adg_table_cell_delete(AdgTableCell *cell)
671 AdgTableRow *row;
673 g_return_if_fail(cell != NULL);
675 row = cell->row;
677 g_return_if_fail(row != NULL);
679 cell_free(cell);
680 row->cells = g_slist_remove(row->cells, cell);
684 * adg_table_cell_get_name:
685 * @cell: a valid #AdgTableCell
687 * Gets the name assigned to @cell. This function is highly inefficient
688 * as the cell names are stored in a hash table optimized to get a cell
689 * from a name. Getting the name from a cell involves a full hash table
690 * inspection.
692 * Returns: the name bound of @cell or %NULL on no name or errors
694 const gchar *
695 adg_table_cell_get_name(AdgTableCell *cell)
697 AdgTablePrivate *data;
699 g_return_val_if_fail(cell != NULL, NULL);
701 data = cell->row->table->data;
703 return g_hash_table_find(data->cell_names, value_match, cell);
707 * adg_table_cell_set_name:
708 * @cell: a valid #AdgTableCell
709 * @name: the new name of @cell
711 * Sets a new name on @cell: this will allow to access @cell by
712 * name at a later time using the adg_table_get_cell() API.
714 void
715 adg_table_cell_set_name(AdgTableCell *cell, const gchar *name)
717 AdgTablePrivate *data;
719 g_return_if_fail(cell != NULL);
721 data = cell->row->table->data;
723 cell_set_name(cell, NULL);
724 cell_set_name(cell, name);
728 * adg_table_cell_get_title:
729 * @cell: a valid #AdgTableCell
731 * Gets the current title of @cell. The returned string is owned
732 * by @cell and must not be modified or freed.
734 * Returns: the title entity or %NULL for undefined title
736 AdgEntity *
737 adg_table_cell_get_title(AdgTableCell *cell)
739 g_return_val_if_fail(cell != NULL, NULL);
741 return cell->title;
745 * adg_table_cell_set_title:
746 * @cell: a valid #AdgTableCell
747 * @title: the new title entity
749 * Sets @title as the new title entity of @cell. The top left
750 * corner of the bounding box of @title will be cohincident to
751 * the top left corner of the cell extents, taking into accounts
752 * eventual padding spaces specified by the table style.
754 * The old internal entity is unrefenrenced while the @title (if
755 * not %NULL) is refenenced with g_object_ref_sink().
757 * @title can be %NULL, in which case the old entity is removed.
759 void
760 adg_table_cell_set_title(AdgTableCell *cell, AdgEntity *title)
762 g_return_if_fail(cell != NULL);
763 g_return_if_fail(title == NULL || ADG_IS_ENTITY(title));
765 if (cell_set_title(cell, title))
766 adg_entity_invalidate((AdgEntity *) cell->row->table);
770 * adg_table_cell_set_text_title:
771 * @cell: a valid #AdgTableCell
772 * @title: a text string
774 * Convenient function to set a the title of a cell using an #AdgToyText
775 * entity with the font dress picked from #AdgTable:table-dress with
776 * a call to adg_table_style_get_title_dress().
778 void
779 adg_table_cell_set_text_title(AdgTableCell *cell, const gchar *title)
781 AdgTable *table;
782 AdgEntity *entity;
783 AdgTableStyle *table_style;
784 const AdgPair *padding;
785 AdgDress table_dress, font_dress;
786 AdgMatrix map;
788 g_return_if_fail(cell != NULL);
790 if (title == NULL)
791 adg_table_cell_set_title(cell, NULL);
793 if (cell->title != NULL) {
794 const gchar *old_title;
796 if (ADG_IS_TOY_TEXT(cell->title))
797 old_title = adg_toy_text_get_label((AdgToyText *) cell->title);
798 else
799 old_title = NULL;
801 if (adg_strcmp(title, old_title) == 0)
802 return;
805 table = cell->row->table;
806 table_dress = adg_table_get_table_dress(table);
807 table_style = (AdgTableStyle *) adg_entity_style((AdgEntity *) table,
808 table_dress);
809 padding = adg_table_style_get_cell_padding(table_style);
810 font_dress = adg_table_style_get_title_dress(table_style);
811 entity = g_object_new(ADG_TYPE_TOY_TEXT, "label", title,
812 "font-dress", font_dress, NULL);
814 cairo_matrix_init_translate(&map, padding->x, padding->y);
815 adg_entity_set_global_map(entity, &map);
817 adg_table_cell_set_title(cell, entity);
821 * adg_table_cell_get_value:
822 * @cell: a valid #AdgTableCell
824 * Gets the current value of @cell. The returned string is owned
825 * by @cell and must not be modified or freed.
827 * Returns: the value entity or %NULL for undefined value
829 AdgEntity *
830 adg_table_cell_get_value(AdgTableCell *cell)
832 g_return_val_if_fail(cell != NULL, NULL);
834 return cell->value;
838 * adg_table_cell_set_value:
839 * @cell: a valid #AdgTableCell
840 * @value: the new value entity
842 * Sets @value as the new value entity of @cell. The bottom middle
843 * point of the bounding box of @value will be cohincident to the
844 * bottom middle point of the cell extents, taking into accounts
845 * eventual padding spaces specified by the table style.
847 * The old internal entity is unrefenrenced while the @value (if
848 * not %NULL) is refenenced with g_object_ref_sink().
850 * @value can be %NULL, in which case the old entity is removed.
852 void
853 adg_table_cell_set_value(AdgTableCell *cell, AdgEntity *value)
855 g_return_if_fail(cell != NULL);
856 g_return_if_fail(value == NULL || ADG_IS_ENTITY(value));
858 if (cell_set_value(cell, value))
859 adg_entity_invalidate((AdgEntity *) cell->row->table);
863 * adg_table_cell_set_value_pos:
864 * @cell: a valid #AdgTableCell
865 * @from_factor: the alignment factor on the value entity
866 * @to_factor: the alignment factor on the cell
868 * Sets a new custom position for the value entity of @cell. The
869 * @from_factor specifies the source point (as a fraction of the
870 * value extents) while the @to_factor is the destination point
871 * (specified as a fraction of the cell extents) the source point
872 * must be moved to.
874 void
875 adg_table_cell_set_value_pos(AdgTableCell *cell, const AdgPair *from_factor,
876 const AdgPair *to_factor)
878 g_return_if_fail(cell != NULL);
879 g_return_if_fail(cell->value != NULL);
881 cell_set_value_pos(cell, from_factor, to_factor);
885 * adg_table_cell_set_text_value:
886 * @cell: a valid #AdgTableCell
887 * @value: a text string
889 * Convenient function to set a the value of a cell using an #AdgToyText
890 * entity with a value font dress picked from #AdgTable:table-dress with
891 * a call to adg_table_style_get_value_dress().
893 void
894 adg_table_cell_set_text_value(AdgTableCell *cell, const gchar *value)
896 AdgTable *table;
897 AdgEntity *entity;
898 AdgTableStyle *table_style;
899 const AdgPair *padding;
900 AdgDress table_dress, font_dress;
901 AdgMatrix map;
903 g_return_if_fail(cell != NULL);
905 if (value == NULL)
906 adg_table_cell_set_value(cell, NULL);
908 if (cell->value != NULL) {
909 const gchar *old_value;
911 if (ADG_IS_TOY_TEXT(cell->value))
912 old_value = adg_toy_text_get_label((AdgToyText *) cell->value);
913 else
914 old_value = NULL;
916 if (adg_strcmp(value, old_value) == 0)
917 return;
920 table = cell->row->table;
921 table_dress = adg_table_get_table_dress(table);
922 table_style = (AdgTableStyle *) adg_entity_style((AdgEntity *) table,
923 table_dress);
924 padding = adg_table_style_get_cell_padding(table_style);
925 font_dress = adg_table_style_get_value_dress(table_style);
926 entity = g_object_new(ADG_TYPE_TOY_TEXT, "label", value,
927 "font-dress", font_dress, NULL);
929 cairo_matrix_init_translate(&map, 0, -padding->y);
930 adg_entity_set_global_map(entity, &map);
932 adg_table_cell_set_value(cell, entity);
936 * adg_table_cell_set_width:
937 * @cell: a valid #AdgTableCell
938 * @width: the new width
940 * Sets a new width on @cell. The extents on the whole table
941 * will be invalidated, so will be recomputed in the next
942 * arrange() phase.
944 void
945 adg_table_cell_set_width(AdgTableCell *cell, gdouble width)
947 g_return_if_fail(cell != NULL);
949 cell->width = width;
951 adg_entity_invalidate((AdgEntity *) cell->row->table);
955 * adg_table_cell_has_frame:
956 * @cell: a valid #AdgTableCell
958 * Gets the frame flag of @cell.
960 * Returns: the frame flag
962 gboolean
963 adg_table_cell_has_frame(AdgTableCell *cell)
965 g_return_val_if_fail(cell != NULL, FALSE);
967 return cell->has_frame;
971 * adg_table_cell_switch_frame:
972 * @cell: a valid #AdgTableCell
973 * @state: the new frame state
975 * Sets the frame flag of @cell: if @state is %TRUE, a frame around
976 * @cell will be rendered using the #AdgTableStyle:cell-dress dress
977 * of the table style.
979 void
980 adg_table_cell_switch_frame(AdgTableCell *cell, gboolean state)
982 AdgTablePrivate *data;
984 g_return_if_fail(cell != NULL);
986 if (cell->has_frame == state)
987 return;
989 data = cell->row->table->data;
990 cell->has_frame = state;
992 if (data->grid != NULL) {
993 g_object_unref(data->grid);
994 data->grid = NULL;
999 * adg_table_cell_extents:
1000 * @cell: a valid #AdgTableCell
1002 * Gets the extents of @cell. This function is useful only after the
1003 * arrange() phase as in the other situation the extents will likely
1004 * be not up to date.
1006 * Returns: the extents of @cell or %NULL on errors
1008 const CpmlExtents *
1009 adg_table_cell_extents(AdgTableCell *cell)
1011 g_return_val_if_fail(cell != NULL, NULL);
1013 return &cell->extents;
1017 static void
1018 global_changed(AdgEntity *entity)
1020 if (PARENT_ENTITY_CLASS->global_changed)
1021 PARENT_ENTITY_CLASS->global_changed(entity);
1023 propagate((AdgTable *) entity, "global-changed");
1026 static void
1027 local_changed(AdgEntity *entity)
1029 if (PARENT_ENTITY_CLASS->local_changed)
1030 PARENT_ENTITY_CLASS->local_changed(entity);
1032 propagate((AdgTable *) entity, "local-changed");
1035 static void
1036 invalidate(AdgEntity *entity)
1038 propagate((AdgTable *) entity, "invalidate");
1041 static void
1042 arrange(AdgEntity *entity)
1044 AdgTable *table;
1045 AdgTablePrivate *data;
1046 CpmlExtents extents;
1047 const AdgPair *spacing;
1048 GSList *row_node;
1049 AdgTableRow *row;
1050 gdouble y;
1052 table = (AdgTable *) entity;
1053 data = table->data;
1054 cpml_extents_copy(&extents, adg_entity_get_extents(entity));
1056 /* Resolve the table style */
1057 if (data->table_style == NULL)
1058 data->table_style = (AdgTableStyle *)
1059 adg_entity_style(entity, data->table_dress);
1061 if (extents.is_defined) {
1062 if (data->grid != NULL)
1063 adg_entity_arrange((AdgEntity *) data->grid);
1064 if (data->frame != NULL)
1065 adg_entity_arrange((AdgEntity *) data->frame);
1066 return;
1069 spacing = adg_table_style_get_cell_spacing(data->table_style);
1070 extents.size.x = 0;
1071 extents.size.y = 0;
1073 for (row_node = data->rows; row_node; row_node = row_node->next) {
1074 row = row_node->data;
1076 row_arrange_size(row);
1078 if (row->extents.size.x > extents.size.x)
1079 extents.size.x = row->extents.size.x;
1080 extents.size.y += row->extents.size.y;
1083 /* TODO: update the org according to the table alignments */
1085 y = extents.org.y + spacing->y;
1086 for (row_node = data->rows; row_node; row_node = row_node->next) {
1087 row = row_node->data;
1089 row->extents.org.x = extents.org.x;
1090 row->extents.org.y = y;
1092 row_arrange(row);
1094 y += row->extents.size.y + spacing->y;
1097 extents.is_defined = TRUE;
1098 adg_entity_set_extents(entity, &extents);
1100 arrange_grid(entity);
1101 arrange_frame(entity);
1104 static void
1105 arrange_grid(AdgEntity *entity)
1107 AdgTablePrivate *data;
1108 AdgPath *path;
1109 AdgTrail *trail;
1110 GSList *row_node, *cell_node;
1111 AdgTableRow *row;
1112 AdgTableCell *cell;
1113 AdgPair pair;
1114 AdgDress dress;
1116 data = ((AdgTable *) entity)->data;
1118 if (data->grid != NULL)
1119 return;
1121 path = adg_path_new();
1122 trail = (AdgTrail *) path;
1124 for (row_node = data->rows; row_node; row_node = row_node->next) {
1125 row = row_node->data;
1127 for (cell_node = row->cells; cell_node; cell_node = cell_node->next) {
1128 cell = cell_node->data;
1130 if (!cell->has_frame)
1131 continue;
1133 cpml_pair_copy(&pair, &cell->extents.org);
1134 adg_path_move_to(path, &pair);
1135 pair.x += cell->extents.size.x;
1136 adg_path_line_to(path, &pair);
1137 pair.y += cell->extents.size.y;
1138 adg_path_line_to(path, &pair);
1139 pair.x -= cell->extents.size.x;
1140 adg_path_line_to(path, &pair);
1141 adg_path_close(path);
1145 if (!adg_trail_extents(trail)->is_defined)
1146 return;
1148 dress = adg_table_style_get_grid_dress(data->table_style);
1149 data->grid = g_object_new(ADG_TYPE_STROKE,
1150 "local-method", ADG_MIX_PARENT,
1151 "line-dress", dress,
1152 "trail", trail,
1153 "parent", entity, NULL);
1154 adg_entity_arrange((AdgEntity *) data->grid);
1157 static void
1158 arrange_frame(AdgEntity *entity)
1160 AdgTablePrivate *data;
1161 AdgPath *path;
1162 const CpmlExtents *extents;
1163 AdgPair pair;
1164 AdgDress dress;
1166 data = ((AdgTable *) entity)->data;
1168 if (data->frame != NULL || !data->has_frame)
1169 return;
1171 path = adg_path_new();
1172 extents = adg_entity_get_extents(entity);
1174 cpml_pair_copy(&pair, &extents->org);
1175 adg_path_move_to(path, &pair);
1176 pair.x += extents->size.x;
1177 adg_path_line_to(path, &pair);
1178 pair.y += extents->size.y;
1179 adg_path_line_to(path, &pair);
1180 pair.x -= extents->size.x;
1181 adg_path_line_to(path, &pair);
1182 adg_path_close(path);
1184 dress = adg_table_style_get_frame_dress(data->table_style);
1186 data->frame = g_object_new(ADG_TYPE_STROKE,
1187 "local-method", ADG_MIX_PARENT,
1188 "line-dress", dress,
1189 "trail", (AdgTrail *) path,
1190 "parent", entity, NULL);
1191 adg_entity_arrange((AdgEntity *) data->frame);
1194 static void
1195 render(AdgEntity *entity, cairo_t *cr)
1197 cairo_transform(cr, adg_entity_get_local_matrix(entity));
1199 propagate((AdgTable *) entity, "render", cr);
1202 static gboolean
1203 switch_frame(AdgTable *table, gboolean state)
1205 AdgTablePrivate *data = table->data;
1207 if (data->has_frame == state)
1208 return FALSE;
1210 data->has_frame = state;
1212 if (data->frame != NULL) {
1213 g_object_unref(data->frame);
1214 data->frame = NULL;
1217 return TRUE;
1220 static void
1221 propagate(AdgTable *table, const gchar *detailed_signal, ...)
1223 guint signal_id;
1224 GQuark detail = 0;
1225 va_list var_args, var_copy;
1226 AdgTablePrivate *data;
1227 GSList *row_node;
1228 AdgTableRow *row;
1229 GSList *cell_node;
1230 AdgTableCell *cell;
1231 AdgAlignment *alignment;
1233 if (!g_signal_parse_name(detailed_signal, G_TYPE_FROM_INSTANCE(table),
1234 &signal_id, &detail, FALSE)) {
1235 g_assert_not_reached();
1238 va_start(var_args, detailed_signal);
1239 data = table->data;
1241 if (data->frame != NULL) {
1242 G_VA_COPY(var_copy, var_args);
1243 g_signal_emit_valist(data->frame, signal_id, detail, var_copy);
1246 if (data->grid != NULL) {
1247 G_VA_COPY(var_copy, var_args);
1248 g_signal_emit_valist(data->grid, signal_id, detail, var_copy);
1251 for (row_node = data->rows; row_node; row_node = row_node->next) {
1252 row = row_node->data;
1254 for (cell_node = row->cells; cell_node; cell_node = cell_node->next) {
1255 cell = cell_node->data;
1257 if (cell->title != NULL) {
1258 alignment = (AdgAlignment *) adg_entity_get_parent(cell->title);
1260 G_VA_COPY(var_copy, var_args);
1261 g_signal_emit_valist(alignment, signal_id, detail, var_copy);
1264 if (cell->value != NULL) {
1265 alignment = (AdgAlignment *) adg_entity_get_parent(cell->value);
1267 G_VA_COPY(var_copy, var_args);
1268 g_signal_emit_valist(alignment, signal_id, detail, var_copy);
1273 va_end(var_args);
1276 static AdgTableRow *
1277 row_new(AdgTable *table, AdgTableRow *before_row)
1279 AdgTablePrivate *data;
1280 AdgTableRow *new_row;
1282 data = table->data;
1283 new_row = g_new(AdgTableRow, 1);
1284 new_row->table = table;
1285 new_row->cells = NULL;
1286 new_row->height = 0;
1287 new_row->extents.is_defined = FALSE;
1289 if (before_row == NULL) {
1290 data->rows = g_slist_append(data->rows, new_row);
1291 } else {
1292 GSList *before_node = g_slist_find(data->rows, before_row);
1294 /* This MUST be present, otherwise something really bad happened */
1295 g_assert(before_node != NULL);
1297 data->rows = g_slist_insert_before(data->rows, before_node, new_row);
1300 invalidate((AdgEntity *) table);
1302 return new_row;
1305 static void
1306 row_arrange_size(AdgTableRow *row)
1308 AdgTableStyle *table_style;
1309 const AdgPair *spacing;
1310 CpmlVector *size;
1311 AdgTableCell *cell;
1312 GSList *cell_node;
1314 table_style = GET_TABLE_STYLE(row->table);
1315 spacing = adg_table_style_get_cell_spacing(table_style);
1316 size = &row->extents.size;
1318 size->x = 0;
1319 if (row->height == 0)
1320 size->y = adg_table_style_get_row_height(table_style);
1321 else
1322 size->y = row->height;
1324 /* Compute the row width by summing every cell width */
1325 for (cell_node = row->cells; cell_node; cell_node = cell_node->next) {
1326 cell = cell_node->data;
1328 cell_arrange_size(cell);
1330 size->x += cell->extents.size.x + spacing->x;
1333 if (size->x > 0)
1334 size->x += spacing->x;
1337 /* Before calling this function, row->extents should be updated */
1338 static void
1339 row_arrange(AdgTableRow *row)
1341 AdgTableStyle *table_style;
1342 const AdgPair *spacing;
1343 const AdgPair *org;
1344 AdgTableCell *cell;
1345 GSList *cell_node;
1346 gdouble x;
1348 table_style = GET_TABLE_STYLE(row->table);
1349 spacing = adg_table_style_get_cell_spacing(table_style);
1350 org = &row->extents.org;
1351 x = org->x + spacing->x;
1353 for (cell_node = row->cells; cell_node; cell_node = cell_node->next) {
1354 cell = cell_node->data;
1356 cell->extents.org.x = x;
1357 cell->extents.org.y = org->y;
1359 cell_arrange(cell);
1361 x += cell->extents.size.x + spacing->x;
1364 row->extents.is_defined = TRUE;
1367 static void
1368 row_dispose(AdgTableRow *row)
1370 g_slist_foreach(row->cells, (GFunc) cell_dispose, NULL);
1373 static void
1374 row_free(AdgTableRow *row)
1376 g_slist_foreach(row->cells, (GFunc) cell_free, NULL);
1377 g_slist_free(row->cells);
1379 g_free(row);
1382 static AdgTableCell *
1383 cell_new(AdgTableRow *row, AdgTableCell *before_cell,
1384 gdouble width, gboolean has_frame,
1385 const gchar *name, AdgEntity *title, AdgEntity *value)
1387 AdgTablePrivate *data;
1388 AdgTableCell *new_cell;
1390 data = row->table->data;
1392 if (name != NULL && data->cell_names != NULL &&
1393 g_hash_table_lookup(data->cell_names, name) != NULL) {
1394 g_warning(_("%s: `%s' cell name is yet used"), G_STRLOC, name);
1395 return NULL;
1398 new_cell = g_new(AdgTableCell, 1);
1399 new_cell->row = row;
1400 new_cell->width = width;
1401 new_cell->has_frame = has_frame;
1402 new_cell->title = NULL;
1403 new_cell->value = NULL;
1404 new_cell->extents.is_defined = FALSE;
1405 new_cell->value_factor.x = 0.5;
1406 new_cell->value_factor.y = 1;
1408 cell_set_title(new_cell, title);
1409 cell_set_value(new_cell, value);
1411 if (before_cell == NULL) {
1412 row->cells = g_slist_append(row->cells, new_cell);
1413 } else {
1414 GSList *before_node = g_slist_find(row->cells, before_cell);
1416 /* This MUST be not null, otherwise something really bad happened */
1417 g_assert(before_node != NULL);
1419 row->cells = g_slist_insert_before(row->cells, before_node, new_cell);
1422 if (name != NULL)
1423 cell_set_name(new_cell, name);
1425 return new_cell;
1428 static void
1429 cell_set_name(AdgTableCell *cell, const gchar *name)
1431 AdgTablePrivate *data = cell->row->table->data;
1433 if (data->cell_names == NULL && name == NULL)
1434 return;
1436 if (data->cell_names == NULL)
1437 data->cell_names = g_hash_table_new_full(g_str_hash, g_str_equal,
1438 g_free, NULL);
1440 if (name == NULL)
1441 g_hash_table_foreach_remove(data->cell_names, value_match, cell);
1442 else
1443 g_hash_table_insert(data->cell_names, g_strdup(name), cell);
1446 static gboolean
1447 cell_set_title(AdgTableCell *cell, AdgEntity *title)
1449 AdgAlignment *alignment;
1451 if (cell->title == title)
1452 return FALSE;
1454 if (cell->title != NULL) {
1455 alignment = (AdgAlignment *) adg_entity_get_parent(cell->title);
1456 g_object_unref(alignment);
1459 cell->title = title;
1461 if (title != NULL) {
1462 alignment = adg_alignment_new_explicit(0, -1);
1463 g_object_ref_sink(alignment);
1464 adg_entity_set_parent((AdgEntity *) alignment,
1465 (AdgEntity *) cell->row->table);
1467 adg_container_add((AdgContainer *) alignment, title);
1470 return TRUE;
1473 static gboolean
1474 cell_set_value(AdgTableCell *cell, AdgEntity *value)
1476 AdgAlignment *alignment;
1478 if (cell->value == value)
1479 return FALSE;
1481 if (cell->value != NULL) {
1482 alignment = (AdgAlignment *) adg_entity_get_parent(cell->value);
1483 g_object_unref(alignment);
1486 cell->value = value;
1488 if (value != NULL) {
1489 alignment = adg_alignment_new_explicit(0.5, 0);
1490 g_object_ref_sink(alignment);
1491 adg_entity_set_parent((AdgEntity *) alignment,
1492 (AdgEntity *) cell->row->table);
1494 adg_container_add((AdgContainer *) alignment, value);
1497 return TRUE;
1500 static void
1501 cell_set_value_pos(AdgTableCell *cell,
1502 const AdgPair *from_factor, const AdgPair *to_factor)
1504 AdgAlignment *alignment;
1506 alignment = (AdgAlignment *) adg_entity_get_parent(cell->value);
1508 if (from_factor != NULL)
1509 adg_alignment_set_factor(alignment, from_factor);
1511 if (to_factor != NULL)
1512 cell->value_factor = *to_factor;
1515 static void
1516 cell_arrange_size(AdgTableCell *cell)
1518 CpmlVector *size;
1519 AdgAlignment *title_alignment, *value_alignment;
1521 size = &cell->extents.size;
1523 if (cell->title != NULL) {
1524 title_alignment = (AdgAlignment *) adg_entity_get_parent(cell->title);
1525 adg_entity_arrange((AdgEntity *) title_alignment);
1528 if (cell->value != NULL) {
1529 value_alignment = (AdgAlignment *) adg_entity_get_parent(cell->value);
1530 adg_entity_arrange((AdgEntity *) value_alignment);
1533 size->y = cell->row->extents.size.y;
1535 if (cell->width == 0) {
1536 AdgTableStyle *table_style = GET_TABLE_STYLE(cell->row->table);
1537 const CpmlExtents *extents;
1539 /* The width depends on the cell content (default = 0) */
1540 size->x = 0;
1542 if (cell->title != NULL) {
1543 extents = adg_entity_get_extents((AdgEntity *) title_alignment);
1544 size->x = extents->size.x;
1547 if (cell->value != NULL) {
1548 extents = adg_entity_get_extents((AdgEntity *) value_alignment);
1549 if (extents->size.x > size->x)
1550 size->x = extents->size.x;
1553 size->x += adg_table_style_get_cell_spacing(table_style)->x * 2;
1554 } else {
1555 size->x = cell->width;
1559 /* Before calling this function, cell->extents should be updated */
1560 static void
1561 cell_arrange(AdgTableCell *cell)
1563 CpmlExtents *extents;
1564 AdgAlignment *alignment;
1565 AdgMatrix map;
1567 extents = &cell->extents;
1569 if (cell->title != NULL) {
1570 alignment = (AdgAlignment *) adg_entity_get_parent(cell->title);
1572 cairo_matrix_init_translate(&map, extents->org.x, extents->org.y);
1573 adg_entity_set_global_map((AdgEntity *) alignment, &map);
1576 if (cell->value != NULL) {
1577 AdgPair to;
1579 alignment = (AdgAlignment *) adg_entity_get_parent(cell->value);
1580 to.x = extents->size.x * cell->value_factor.x;
1581 to.y = extents->size.y * cell->value_factor.y;
1582 cpml_pair_add(&to, &extents->org);
1584 cairo_matrix_init_translate(&map, to.x, to.y);
1585 adg_entity_set_global_map((AdgEntity *) alignment, &map);
1588 extents->is_defined = TRUE;
1591 static void
1592 cell_dispose(AdgTableCell *cell)
1594 cell_set_title(cell, NULL);
1595 cell_set_value(cell, NULL);
1598 static void
1599 cell_free(AdgTableCell *cell)
1601 cell_set_name(cell, NULL);
1602 cell_dispose(cell);
1603 g_free(cell);
1606 static gboolean
1607 value_match(gpointer key, gpointer value, gpointer user_data)
1609 return value == user_data;