[AdgTable] Implemented adg_table_row_get_height()
[adg.git] / adg / adg-table.c
blob4ab61c8f6a0d836d4843845cdff0a2232e4c25c1
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 new_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 * @new_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 new_state)
388 g_return_if_fail(ADG_IS_TABLE(table));
390 if (switch_frame(table, new_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_get_height:
522 * @row: a valid #AdgTableRow
524 * Gets the height of @row.
526 gdouble
527 adg_table_row_get_height(AdgTableRow *row)
529 g_return_val_if_fail(row != NULL, 0.);
531 return row->height;
535 * adg_table_row_extents:
536 * @row: a valid #AdgTableRow
538 * Gets the extents of @row. This function is useful only after the
539 * arrange() phase as in the other situation the extents will likely
540 * be not up to date.
542 * Returns: the extents of @row or %NULL on errors
544 const CpmlExtents *
545 adg_table_row_extents(AdgTableRow *row)
547 g_return_val_if_fail(row != NULL, NULL);
549 return &row->extents;
553 * adg_table_get_cell:
554 * @table: an #AdgTable
555 * @name: the name of a cell
557 * Gets the cell named @name inside @table. Only named cells can be
558 * retrieved by this method.
560 * Returns: the requested cell or %NULL if not found
562 AdgTableCell *
563 adg_table_get_cell(AdgTable *table, const gchar *name)
565 AdgTablePrivate *data;
567 g_return_val_if_fail(ADG_IS_TABLE(table), NULL);
569 data = table->data;
571 if (data->cell_names == NULL)
572 return NULL;
574 return g_hash_table_lookup(data->cell_names, name);
578 * adg_table_cell_new:
579 * @row: a valid #AdgTableRow
580 * @width: width of the cell
582 * Creates a new empty cell without a frame and appends it at the
583 * end of the cells yet present in @row. You can add content to the
584 * cell by using adg_table_cell_set_title() and
585 * adg_table_cell_set_value() or enable the frame with
586 * adg_table_cell_switch_frame().
588 * A positive @width value specifies the width of this cell in global
589 * space: if the width of its content (that is, either the title or the
590 * value entity) will be greater than @width, it will be rendered
591 * outside the cell boundary box, luckely overwriting the adiacent
592 * cells.
594 * Using %0 as @width means the width of the cell will be automatically
595 * adjusted to the maximum width of its content.
597 * Negative width values are not allowed: this condition will raise
598 * a warning without any further processing.
600 * Returns: the newly created cell or %NULL on errors
602 AdgTableCell *
603 adg_table_cell_new(AdgTableRow *row, gdouble width)
605 g_return_val_if_fail(row != NULL, NULL);
606 g_return_val_if_fail(width >= 0, NULL);
608 return cell_new(row, NULL, width, FALSE, NULL, NULL, NULL);
612 * adg_table_cell_new_before:
613 * @cell: a valid #AdgTableCell
614 * @width: width of the cell
616 * Creates a new cell and inserts it rigthly before the @cell cell.
617 * This works similarily and accepts the same parameters as the
618 * adg_table_cell_new() function.
620 * Returns: the newly created cell or %NULL on errors
622 AdgTableCell *
623 adg_table_cell_new_before(AdgTableCell *cell, gdouble width)
625 g_return_val_if_fail(cell != NULL, NULL);
626 g_return_val_if_fail(cell->row != NULL, NULL);
627 g_return_val_if_fail(width >= 0, NULL);
629 return cell_new(cell->row, cell, width, FALSE, NULL, NULL, NULL);
633 * adg_table_cell_new_full:
634 * @row: a valid #AdgTableRow
635 * @width: width of the cell
636 * @name: name to associate
637 * @title: title to render
638 * @value: value to render
640 * A convenient function to append a framed cell to @row with a
641 * specific title and value text. The font to use for rendering
642 * @title and @value will be picked up from the table style, so
643 * be sure to have the correct table dress set before calling
644 * this function.
646 * @row and @width have the same meanings as in adg_table_cell_new():
647 * check its documentation for details.
649 * @name is an optional identifier to univoquely access this cell
650 * by using adg_table_get_cell(). The identifier must be univoque:
651 * if there is yet a cell with the same name a warning message will
652 * be raised and the function will fail.
654 * Returns: the newly created cell or %NULL on errors
656 AdgTableCell *
657 adg_table_cell_new_full(AdgTableRow *row, gdouble width, const gchar *name,
658 const gchar *title, const gchar *value)
660 AdgTableCell *cell;
662 g_return_val_if_fail(row != NULL, NULL);
664 cell = cell_new(row, NULL, width, TRUE, name, NULL, NULL);
666 if (title != NULL)
667 adg_table_cell_set_text_title(cell, title);
669 if (value != NULL)
670 adg_table_cell_set_text_value(cell, value);
672 return cell;
676 * adg_table_cell_delete:
677 * @cell: a valid #AdgTableCell
679 * Deletes @cell removing it from the container row and freeing
680 * any resource associated to it.
682 void
683 adg_table_cell_delete(AdgTableCell *cell)
685 AdgTableRow *row;
687 g_return_if_fail(cell != NULL);
689 row = cell->row;
691 g_return_if_fail(row != NULL);
693 cell_free(cell);
694 row->cells = g_slist_remove(row->cells, cell);
698 * adg_table_cell_get_name:
699 * @cell: a valid #AdgTableCell
701 * Gets the name assigned to @cell. This function is highly inefficient
702 * as the cell names are stored in a hash table optimized to get a cell
703 * from a name. Getting the name from a cell involves a full hash table
704 * inspection.
706 * Returns: the name bound of @cell or %NULL on no name or errors
708 const gchar *
709 adg_table_cell_get_name(AdgTableCell *cell)
711 AdgTablePrivate *data;
713 g_return_val_if_fail(cell != NULL, NULL);
715 data = cell->row->table->data;
717 return g_hash_table_find(data->cell_names, value_match, cell);
721 * adg_table_cell_set_name:
722 * @cell: a valid #AdgTableCell
723 * @name: the new name of @cell
725 * Sets a new name on @cell: this will allow to access @cell by
726 * name at a later time using the adg_table_get_cell() API.
728 void
729 adg_table_cell_set_name(AdgTableCell *cell, const gchar *name)
731 AdgTablePrivate *data;
733 g_return_if_fail(cell != NULL);
735 data = cell->row->table->data;
737 cell_set_name(cell, NULL);
738 cell_set_name(cell, name);
742 * adg_table_cell_get_title:
743 * @cell: a valid #AdgTableCell
745 * Gets the current title of @cell. The returned string is owned
746 * by @cell and must not be modified or freed.
748 * Returns: the title entity or %NULL for undefined title
750 AdgEntity *
751 adg_table_cell_get_title(AdgTableCell *cell)
753 g_return_val_if_fail(cell != NULL, NULL);
755 return cell->title;
759 * adg_table_cell_set_title:
760 * @cell: a valid #AdgTableCell
761 * @title: the new title entity
763 * Sets @title as the new title entity of @cell. The top left
764 * corner of the bounding box of @title will be cohincident to
765 * the top left corner of the cell extents, taking into accounts
766 * eventual padding spaces specified by the table style.
768 * The old internal entity is unrefenrenced while the @title (if
769 * not %NULL) is refenenced with g_object_ref_sink().
771 * @title can be %NULL, in which case the old entity is removed.
773 void
774 adg_table_cell_set_title(AdgTableCell *cell, AdgEntity *title)
776 g_return_if_fail(cell != NULL);
777 g_return_if_fail(title == NULL || ADG_IS_ENTITY(title));
779 if (cell_set_title(cell, title))
780 adg_entity_invalidate((AdgEntity *) cell->row->table);
784 * adg_table_cell_set_text_title:
785 * @cell: a valid #AdgTableCell
786 * @title: a text string
788 * Convenient function to set a the title of a cell using an #AdgToyText
789 * entity with the font dress picked from #AdgTable:table-dress with
790 * a call to adg_table_style_get_title_dress().
792 void
793 adg_table_cell_set_text_title(AdgTableCell *cell, const gchar *title)
795 AdgTable *table;
796 AdgEntity *entity;
797 AdgTableStyle *table_style;
798 const AdgPair *padding;
799 AdgDress table_dress, font_dress;
800 AdgMatrix map;
802 g_return_if_fail(cell != NULL);
804 if (title == NULL)
805 adg_table_cell_set_title(cell, NULL);
807 if (cell->title != NULL) {
808 const gchar *old_title;
810 if (ADG_IS_TOY_TEXT(cell->title))
811 old_title = adg_toy_text_get_label((AdgToyText *) cell->title);
812 else
813 old_title = NULL;
815 if (adg_strcmp(title, old_title) == 0)
816 return;
819 table = cell->row->table;
820 table_dress = adg_table_get_table_dress(table);
821 table_style = (AdgTableStyle *) adg_entity_style((AdgEntity *) table,
822 table_dress);
823 padding = adg_table_style_get_cell_padding(table_style);
824 font_dress = adg_table_style_get_title_dress(table_style);
825 entity = g_object_new(ADG_TYPE_TOY_TEXT, "label", title,
826 "font-dress", font_dress, NULL);
828 cairo_matrix_init_translate(&map, padding->x, padding->y);
829 adg_entity_set_global_map(entity, &map);
831 adg_table_cell_set_title(cell, entity);
835 * adg_table_cell_get_value:
836 * @cell: a valid #AdgTableCell
838 * Gets the current value of @cell. The returned string is owned
839 * by @cell and must not be modified or freed.
841 * Returns: the value entity or %NULL for undefined value
843 AdgEntity *
844 adg_table_cell_get_value(AdgTableCell *cell)
846 g_return_val_if_fail(cell != NULL, NULL);
848 return cell->value;
852 * adg_table_cell_set_value:
853 * @cell: a valid #AdgTableCell
854 * @value: the new value entity
856 * Sets @value as the new value entity of @cell. The bottom middle
857 * point of the bounding box of @value will be cohincident to the
858 * bottom middle point of the cell extents, taking into accounts
859 * eventual padding spaces specified by the table style.
861 * The old internal entity is unrefenrenced while the @value (if
862 * not %NULL) is refenenced with g_object_ref_sink().
864 * @value can be %NULL, in which case the old entity is removed.
866 void
867 adg_table_cell_set_value(AdgTableCell *cell, AdgEntity *value)
869 g_return_if_fail(cell != NULL);
870 g_return_if_fail(value == NULL || ADG_IS_ENTITY(value));
872 if (cell_set_value(cell, value))
873 adg_entity_invalidate((AdgEntity *) cell->row->table);
877 * adg_table_cell_set_value_pos:
878 * @cell: a valid #AdgTableCell
879 * @from_factor: the alignment factor on the value entity
880 * @to_factor: the alignment factor on the cell
882 * Sets a new custom position for the value entity of @cell. The
883 * @from_factor specifies the source point (as a fraction of the
884 * value extents) while the @to_factor is the destination point
885 * (specified as a fraction of the cell extents) the source point
886 * must be moved to.
888 void
889 adg_table_cell_set_value_pos(AdgTableCell *cell, const AdgPair *from_factor,
890 const AdgPair *to_factor)
892 g_return_if_fail(cell != NULL);
893 g_return_if_fail(cell->value != NULL);
895 cell_set_value_pos(cell, from_factor, to_factor);
899 * adg_table_cell_set_text_value:
900 * @cell: a valid #AdgTableCell
901 * @value: a text string
903 * Convenient function to set a the value of a cell using an #AdgToyText
904 * entity with a value font dress picked from #AdgTable:table-dress with
905 * a call to adg_table_style_get_value_dress().
907 void
908 adg_table_cell_set_text_value(AdgTableCell *cell, const gchar *value)
910 AdgTable *table;
911 AdgEntity *entity;
912 AdgTableStyle *table_style;
913 const AdgPair *padding;
914 AdgDress table_dress, font_dress;
915 AdgMatrix map;
917 g_return_if_fail(cell != NULL);
919 if (value == NULL)
920 adg_table_cell_set_value(cell, NULL);
922 if (cell->value != NULL) {
923 const gchar *old_value;
925 if (ADG_IS_TOY_TEXT(cell->value))
926 old_value = adg_toy_text_get_label((AdgToyText *) cell->value);
927 else
928 old_value = NULL;
930 if (adg_strcmp(value, old_value) == 0)
931 return;
934 table = cell->row->table;
935 table_dress = adg_table_get_table_dress(table);
936 table_style = (AdgTableStyle *) adg_entity_style((AdgEntity *) table,
937 table_dress);
938 padding = adg_table_style_get_cell_padding(table_style);
939 font_dress = adg_table_style_get_value_dress(table_style);
940 entity = g_object_new(ADG_TYPE_TOY_TEXT, "label", value,
941 "font-dress", font_dress, NULL);
943 cairo_matrix_init_translate(&map, 0, -padding->y);
944 adg_entity_set_global_map(entity, &map);
946 adg_table_cell_set_value(cell, entity);
950 * adg_table_cell_set_width:
951 * @cell: a valid #AdgTableCell
952 * @width: the new width
954 * Sets a new width on @cell. The extents on the whole table
955 * will be invalidated, so will be recomputed in the next
956 * arrange() phase.
958 void
959 adg_table_cell_set_width(AdgTableCell *cell, gdouble width)
961 g_return_if_fail(cell != NULL);
963 cell->width = width;
965 adg_entity_invalidate((AdgEntity *) cell->row->table);
969 * adg_table_cell_has_frame:
970 * @cell: a valid #AdgTableCell
972 * Gets the frame flag of @cell.
974 * Returns: the frame flag
976 gboolean
977 adg_table_cell_has_frame(AdgTableCell *cell)
979 g_return_val_if_fail(cell != NULL, FALSE);
981 return cell->has_frame;
985 * adg_table_cell_switch_frame:
986 * @cell: a valid #AdgTableCell
987 * @new_state: the new frame state
989 * Sets the frame flag of @cell: if @new_state is %TRUE, a frame around
990 * @cell will be rendered using the #AdgTableStyle:cell-dress dress
991 * of the table style.
993 void
994 adg_table_cell_switch_frame(AdgTableCell *cell, gboolean new_state)
996 AdgTablePrivate *data;
998 g_return_if_fail(cell != NULL);
1000 if (cell->has_frame == new_state)
1001 return;
1003 data = cell->row->table->data;
1004 cell->has_frame = new_state;
1006 if (data->grid != NULL) {
1007 g_object_unref(data->grid);
1008 data->grid = NULL;
1013 * adg_table_cell_extents:
1014 * @cell: a valid #AdgTableCell
1016 * Gets the extents of @cell. This function is useful only after the
1017 * arrange() phase as in the other situation the extents will likely
1018 * be not up to date.
1020 * Returns: the extents of @cell or %NULL on errors
1022 const CpmlExtents *
1023 adg_table_cell_extents(AdgTableCell *cell)
1025 g_return_val_if_fail(cell != NULL, NULL);
1027 return &cell->extents;
1031 static void
1032 global_changed(AdgEntity *entity)
1034 if (PARENT_ENTITY_CLASS->global_changed)
1035 PARENT_ENTITY_CLASS->global_changed(entity);
1037 propagate((AdgTable *) entity, "global-changed");
1040 static void
1041 local_changed(AdgEntity *entity)
1043 if (PARENT_ENTITY_CLASS->local_changed)
1044 PARENT_ENTITY_CLASS->local_changed(entity);
1046 propagate((AdgTable *) entity, "local-changed");
1049 static void
1050 invalidate(AdgEntity *entity)
1052 propagate((AdgTable *) entity, "invalidate");
1055 static void
1056 arrange(AdgEntity *entity)
1058 AdgTable *table;
1059 AdgTablePrivate *data;
1060 CpmlExtents extents;
1061 const AdgPair *spacing;
1062 GSList *row_node;
1063 AdgTableRow *row;
1064 gdouble y;
1066 table = (AdgTable *) entity;
1067 data = table->data;
1068 cpml_extents_copy(&extents, adg_entity_get_extents(entity));
1070 /* Resolve the table style */
1071 if (data->table_style == NULL)
1072 data->table_style = (AdgTableStyle *)
1073 adg_entity_style(entity, data->table_dress);
1075 if (extents.is_defined) {
1076 if (data->grid != NULL)
1077 adg_entity_arrange((AdgEntity *) data->grid);
1078 if (data->frame != NULL)
1079 adg_entity_arrange((AdgEntity *) data->frame);
1080 return;
1083 spacing = adg_table_style_get_cell_spacing(data->table_style);
1084 extents.size.x = 0;
1085 extents.size.y = 0;
1087 for (row_node = data->rows; row_node; row_node = row_node->next) {
1088 row = row_node->data;
1090 row_arrange_size(row);
1092 if (row->extents.size.x > extents.size.x)
1093 extents.size.x = row->extents.size.x;
1094 extents.size.y += row->extents.size.y;
1097 /* TODO: update the org according to the table alignments */
1099 y = extents.org.y + spacing->y;
1100 for (row_node = data->rows; row_node; row_node = row_node->next) {
1101 row = row_node->data;
1103 row->extents.org.x = extents.org.x;
1104 row->extents.org.y = y;
1106 row_arrange(row);
1108 y += row->extents.size.y + spacing->y;
1111 extents.is_defined = TRUE;
1112 adg_entity_set_extents(entity, &extents);
1114 arrange_grid(entity);
1115 arrange_frame(entity);
1118 static void
1119 arrange_grid(AdgEntity *entity)
1121 AdgTablePrivate *data;
1122 AdgPath *path;
1123 AdgTrail *trail;
1124 GSList *row_node, *cell_node;
1125 AdgTableRow *row;
1126 AdgTableCell *cell;
1127 AdgPair pair;
1128 AdgDress dress;
1130 data = ((AdgTable *) entity)->data;
1132 if (data->grid != NULL)
1133 return;
1135 path = adg_path_new();
1136 trail = (AdgTrail *) path;
1138 for (row_node = data->rows; row_node; row_node = row_node->next) {
1139 row = row_node->data;
1141 for (cell_node = row->cells; cell_node; cell_node = cell_node->next) {
1142 cell = cell_node->data;
1144 if (!cell->has_frame)
1145 continue;
1147 cpml_pair_copy(&pair, &cell->extents.org);
1148 adg_path_move_to(path, &pair);
1149 pair.x += cell->extents.size.x;
1150 adg_path_line_to(path, &pair);
1151 pair.y += cell->extents.size.y;
1152 adg_path_line_to(path, &pair);
1153 pair.x -= cell->extents.size.x;
1154 adg_path_line_to(path, &pair);
1155 adg_path_close(path);
1159 if (!adg_trail_extents(trail)->is_defined)
1160 return;
1162 dress = adg_table_style_get_grid_dress(data->table_style);
1163 data->grid = g_object_new(ADG_TYPE_STROKE,
1164 "local-method", ADG_MIX_PARENT,
1165 "line-dress", dress,
1166 "trail", trail,
1167 "parent", entity, NULL);
1168 adg_entity_arrange((AdgEntity *) data->grid);
1171 static void
1172 arrange_frame(AdgEntity *entity)
1174 AdgTablePrivate *data;
1175 AdgPath *path;
1176 const CpmlExtents *extents;
1177 AdgPair pair;
1178 AdgDress dress;
1180 data = ((AdgTable *) entity)->data;
1182 if (data->frame != NULL || !data->has_frame)
1183 return;
1185 path = adg_path_new();
1186 extents = adg_entity_get_extents(entity);
1188 cpml_pair_copy(&pair, &extents->org);
1189 adg_path_move_to(path, &pair);
1190 pair.x += extents->size.x;
1191 adg_path_line_to(path, &pair);
1192 pair.y += extents->size.y;
1193 adg_path_line_to(path, &pair);
1194 pair.x -= extents->size.x;
1195 adg_path_line_to(path, &pair);
1196 adg_path_close(path);
1198 dress = adg_table_style_get_frame_dress(data->table_style);
1200 data->frame = g_object_new(ADG_TYPE_STROKE,
1201 "local-method", ADG_MIX_PARENT,
1202 "line-dress", dress,
1203 "trail", (AdgTrail *) path,
1204 "parent", entity, NULL);
1205 adg_entity_arrange((AdgEntity *) data->frame);
1208 static void
1209 render(AdgEntity *entity, cairo_t *cr)
1211 cairo_transform(cr, adg_entity_get_local_matrix(entity));
1213 propagate((AdgTable *) entity, "render", cr);
1216 static gboolean
1217 switch_frame(AdgTable *table, gboolean new_state)
1219 AdgTablePrivate *data = table->data;
1221 if (data->has_frame == new_state)
1222 return FALSE;
1224 data->has_frame = new_state;
1226 if (data->frame != NULL) {
1227 g_object_unref(data->frame);
1228 data->frame = NULL;
1231 return TRUE;
1234 static void
1235 propagate(AdgTable *table, const gchar *detailed_signal, ...)
1237 guint signal_id;
1238 GQuark detail = 0;
1239 va_list var_args, var_copy;
1240 AdgTablePrivate *data;
1241 GSList *row_node;
1242 AdgTableRow *row;
1243 GSList *cell_node;
1244 AdgTableCell *cell;
1245 AdgAlignment *alignment;
1247 if (!g_signal_parse_name(detailed_signal, G_TYPE_FROM_INSTANCE(table),
1248 &signal_id, &detail, FALSE)) {
1249 g_assert_not_reached();
1252 va_start(var_args, detailed_signal);
1253 data = table->data;
1255 if (data->frame != NULL) {
1256 G_VA_COPY(var_copy, var_args);
1257 g_signal_emit_valist(data->frame, signal_id, detail, var_copy);
1260 if (data->grid != NULL) {
1261 G_VA_COPY(var_copy, var_args);
1262 g_signal_emit_valist(data->grid, signal_id, detail, var_copy);
1265 for (row_node = data->rows; row_node; row_node = row_node->next) {
1266 row = row_node->data;
1268 for (cell_node = row->cells; cell_node; cell_node = cell_node->next) {
1269 cell = cell_node->data;
1271 if (cell->title != NULL) {
1272 alignment = (AdgAlignment *) adg_entity_get_parent(cell->title);
1274 G_VA_COPY(var_copy, var_args);
1275 g_signal_emit_valist(alignment, signal_id, detail, var_copy);
1278 if (cell->value != NULL) {
1279 alignment = (AdgAlignment *) adg_entity_get_parent(cell->value);
1281 G_VA_COPY(var_copy, var_args);
1282 g_signal_emit_valist(alignment, signal_id, detail, var_copy);
1287 va_end(var_args);
1290 static AdgTableRow *
1291 row_new(AdgTable *table, AdgTableRow *before_row)
1293 AdgTablePrivate *data;
1294 AdgTableRow *new_row;
1296 data = table->data;
1297 new_row = g_new(AdgTableRow, 1);
1298 new_row->table = table;
1299 new_row->cells = NULL;
1300 new_row->height = 0;
1301 new_row->extents.is_defined = FALSE;
1303 if (before_row == NULL) {
1304 data->rows = g_slist_append(data->rows, new_row);
1305 } else {
1306 GSList *before_node = g_slist_find(data->rows, before_row);
1308 /* This MUST be present, otherwise something really bad happened */
1309 g_assert(before_node != NULL);
1311 data->rows = g_slist_insert_before(data->rows, before_node, new_row);
1314 invalidate((AdgEntity *) table);
1316 return new_row;
1319 static void
1320 row_arrange_size(AdgTableRow *row)
1322 AdgTableStyle *table_style;
1323 const AdgPair *spacing;
1324 CpmlVector *size;
1325 AdgTableCell *cell;
1326 GSList *cell_node;
1328 table_style = GET_TABLE_STYLE(row->table);
1329 spacing = adg_table_style_get_cell_spacing(table_style);
1330 size = &row->extents.size;
1332 size->x = 0;
1333 if (row->height == 0)
1334 size->y = adg_table_style_get_row_height(table_style);
1335 else
1336 size->y = row->height;
1338 /* Compute the row width by summing every cell width */
1339 for (cell_node = row->cells; cell_node; cell_node = cell_node->next) {
1340 cell = cell_node->data;
1342 cell_arrange_size(cell);
1344 size->x += cell->extents.size.x + spacing->x;
1347 if (size->x > 0)
1348 size->x += spacing->x;
1351 /* Before calling this function, row->extents should be updated */
1352 static void
1353 row_arrange(AdgTableRow *row)
1355 AdgTableStyle *table_style;
1356 const AdgPair *spacing;
1357 const AdgPair *org;
1358 AdgTableCell *cell;
1359 GSList *cell_node;
1360 gdouble x;
1362 table_style = GET_TABLE_STYLE(row->table);
1363 spacing = adg_table_style_get_cell_spacing(table_style);
1364 org = &row->extents.org;
1365 x = org->x + spacing->x;
1367 for (cell_node = row->cells; cell_node; cell_node = cell_node->next) {
1368 cell = cell_node->data;
1370 cell->extents.org.x = x;
1371 cell->extents.org.y = org->y;
1373 cell_arrange(cell);
1375 x += cell->extents.size.x + spacing->x;
1378 row->extents.is_defined = TRUE;
1381 static void
1382 row_dispose(AdgTableRow *row)
1384 g_slist_foreach(row->cells, (GFunc) cell_dispose, NULL);
1387 static void
1388 row_free(AdgTableRow *row)
1390 g_slist_foreach(row->cells, (GFunc) cell_free, NULL);
1391 g_slist_free(row->cells);
1393 g_free(row);
1396 static AdgTableCell *
1397 cell_new(AdgTableRow *row, AdgTableCell *before_cell,
1398 gdouble width, gboolean has_frame,
1399 const gchar *name, AdgEntity *title, AdgEntity *value)
1401 AdgTablePrivate *data;
1402 AdgTableCell *new_cell;
1404 data = row->table->data;
1406 if (name != NULL && data->cell_names != NULL &&
1407 g_hash_table_lookup(data->cell_names, name) != NULL) {
1408 g_warning(_("%s: `%s' cell name is yet used"), G_STRLOC, name);
1409 return NULL;
1412 new_cell = g_new(AdgTableCell, 1);
1413 new_cell->row = row;
1414 new_cell->width = width;
1415 new_cell->has_frame = has_frame;
1416 new_cell->title = NULL;
1417 new_cell->value = NULL;
1418 new_cell->extents.is_defined = FALSE;
1419 new_cell->value_factor.x = 0.5;
1420 new_cell->value_factor.y = 1;
1422 cell_set_title(new_cell, title);
1423 cell_set_value(new_cell, value);
1425 if (before_cell == NULL) {
1426 row->cells = g_slist_append(row->cells, new_cell);
1427 } else {
1428 GSList *before_node = g_slist_find(row->cells, before_cell);
1430 /* This MUST be not null, otherwise something really bad happened */
1431 g_assert(before_node != NULL);
1433 row->cells = g_slist_insert_before(row->cells, before_node, new_cell);
1436 if (name != NULL)
1437 cell_set_name(new_cell, name);
1439 return new_cell;
1442 static void
1443 cell_set_name(AdgTableCell *cell, const gchar *name)
1445 AdgTablePrivate *data = cell->row->table->data;
1447 if (data->cell_names == NULL && name == NULL)
1448 return;
1450 if (data->cell_names == NULL)
1451 data->cell_names = g_hash_table_new_full(g_str_hash, g_str_equal,
1452 g_free, NULL);
1454 if (name == NULL)
1455 g_hash_table_foreach_remove(data->cell_names, value_match, cell);
1456 else
1457 g_hash_table_insert(data->cell_names, g_strdup(name), cell);
1460 static gboolean
1461 cell_set_title(AdgTableCell *cell, AdgEntity *title)
1463 AdgAlignment *alignment;
1465 if (cell->title == title)
1466 return FALSE;
1468 if (cell->title != NULL) {
1469 alignment = (AdgAlignment *) adg_entity_get_parent(cell->title);
1470 g_object_unref(alignment);
1473 cell->title = title;
1475 if (title != NULL) {
1476 alignment = adg_alignment_new_explicit(0, -1);
1477 g_object_ref_sink(alignment);
1478 adg_entity_set_parent((AdgEntity *) alignment,
1479 (AdgEntity *) cell->row->table);
1481 adg_container_add((AdgContainer *) alignment, title);
1484 return TRUE;
1487 static gboolean
1488 cell_set_value(AdgTableCell *cell, AdgEntity *value)
1490 AdgAlignment *alignment;
1492 if (cell->value == value)
1493 return FALSE;
1495 if (cell->value != NULL) {
1496 alignment = (AdgAlignment *) adg_entity_get_parent(cell->value);
1497 g_object_unref(alignment);
1500 cell->value = value;
1502 if (value != NULL) {
1503 alignment = adg_alignment_new_explicit(0.5, 0);
1504 g_object_ref_sink(alignment);
1505 adg_entity_set_parent((AdgEntity *) alignment,
1506 (AdgEntity *) cell->row->table);
1508 adg_container_add((AdgContainer *) alignment, value);
1511 return TRUE;
1514 static void
1515 cell_set_value_pos(AdgTableCell *cell,
1516 const AdgPair *from_factor, const AdgPair *to_factor)
1518 AdgAlignment *alignment;
1520 alignment = (AdgAlignment *) adg_entity_get_parent(cell->value);
1522 if (from_factor != NULL)
1523 adg_alignment_set_factor(alignment, from_factor);
1525 if (to_factor != NULL)
1526 cell->value_factor = *to_factor;
1529 static void
1530 cell_arrange_size(AdgTableCell *cell)
1532 CpmlVector *size;
1533 AdgAlignment *title_alignment, *value_alignment;
1535 size = &cell->extents.size;
1537 if (cell->title != NULL) {
1538 title_alignment = (AdgAlignment *) adg_entity_get_parent(cell->title);
1539 adg_entity_arrange((AdgEntity *) title_alignment);
1542 if (cell->value != NULL) {
1543 value_alignment = (AdgAlignment *) adg_entity_get_parent(cell->value);
1544 adg_entity_arrange((AdgEntity *) value_alignment);
1547 size->y = cell->row->extents.size.y;
1549 if (cell->width == 0) {
1550 AdgTableStyle *table_style = GET_TABLE_STYLE(cell->row->table);
1551 const CpmlExtents *extents;
1553 /* The width depends on the cell content (default = 0) */
1554 size->x = 0;
1556 if (cell->title != NULL) {
1557 extents = adg_entity_get_extents((AdgEntity *) title_alignment);
1558 size->x = extents->size.x;
1561 if (cell->value != NULL) {
1562 extents = adg_entity_get_extents((AdgEntity *) value_alignment);
1563 if (extents->size.x > size->x)
1564 size->x = extents->size.x;
1567 size->x += adg_table_style_get_cell_spacing(table_style)->x * 2;
1568 } else {
1569 size->x = cell->width;
1573 /* Before calling this function, cell->extents should be updated */
1574 static void
1575 cell_arrange(AdgTableCell *cell)
1577 CpmlExtents *extents;
1578 AdgAlignment *alignment;
1579 AdgMatrix map;
1581 extents = &cell->extents;
1583 if (cell->title != NULL) {
1584 alignment = (AdgAlignment *) adg_entity_get_parent(cell->title);
1586 cairo_matrix_init_translate(&map, extents->org.x, extents->org.y);
1587 adg_entity_set_global_map((AdgEntity *) alignment, &map);
1590 if (cell->value != NULL) {
1591 AdgPair to;
1593 alignment = (AdgAlignment *) adg_entity_get_parent(cell->value);
1594 to.x = extents->size.x * cell->value_factor.x;
1595 to.y = extents->size.y * cell->value_factor.y;
1596 cpml_pair_add(&to, &extents->org);
1598 cairo_matrix_init_translate(&map, to.x, to.y);
1599 adg_entity_set_global_map((AdgEntity *) alignment, &map);
1602 extents->is_defined = TRUE;
1605 static void
1606 cell_dispose(AdgTableCell *cell)
1608 cell_set_title(cell, NULL);
1609 cell_set_value(cell, NULL);
1612 static void
1613 cell_free(AdgTableCell *cell)
1615 cell_set_name(cell, NULL);
1616 cell_dispose(cell);
1617 g_free(cell);
1620 static gboolean
1621 value_match(gpointer key, gpointer value, gpointer user_data)
1623 return value == user_data;