[AdgTable] s/_cell_get_value/_cell_value/
[adg.git] / adg / adg-table.c
blobbda78b03d49e5771b554ccc01706c1074c9014ea
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 * Returns: the requested height or %0 on errors
528 gdouble
529 adg_table_row_get_height(AdgTableRow *row)
531 g_return_val_if_fail(row != NULL, 0.);
533 return row->height;
537 * adg_table_row_extents:
538 * @row: a valid #AdgTableRow
540 * Gets the extents of @row. This function is useful only after the
541 * arrange() phase as in the other situation the extents will likely
542 * be not up to date.
544 * Returns: the extents of @row or %NULL on errors
546 const CpmlExtents *
547 adg_table_row_extents(AdgTableRow *row)
549 g_return_val_if_fail(row != NULL, NULL);
551 return &row->extents;
555 * adg_table_cell:
556 * @table: an #AdgTable
557 * @name: the name of a cell
559 * Gets the cell named @name inside @table. Only named cells can be
560 * retrieved by this method.
562 * Returns: the requested cell or %NULL if not found
564 AdgTableCell *
565 adg_table_cell(AdgTable *table, const gchar *name)
567 AdgTablePrivate *data;
569 g_return_val_if_fail(ADG_IS_TABLE(table), NULL);
571 data = table->data;
573 if (data->cell_names == NULL)
574 return NULL;
576 return g_hash_table_lookup(data->cell_names, name);
580 * adg_table_cell_new:
581 * @row: a valid #AdgTableRow
582 * @width: width of the cell
584 * Creates a new empty cell without a frame and appends it at the
585 * end of the cells yet present in @row. You can add content to the
586 * cell by using adg_table_cell_set_title() and
587 * adg_table_cell_set_value() or enable the frame with
588 * adg_table_cell_switch_frame().
590 * A positive @width value specifies the width of this cell in global
591 * space: if the width of its content (that is, either the title or the
592 * value entity) will be greater than @width, it will be rendered
593 * outside the cell boundary box, luckely overwriting the adiacent
594 * cells.
596 * Using %0 as @width means the width of the cell will be automatically
597 * adjusted to the maximum width of its content.
599 * Negative width values are not allowed: this condition will raise
600 * a warning without any further processing.
602 * Returns: the newly created cell or %NULL on errors
604 AdgTableCell *
605 adg_table_cell_new(AdgTableRow *row, gdouble width)
607 g_return_val_if_fail(row != NULL, NULL);
608 g_return_val_if_fail(width >= 0, NULL);
610 return cell_new(row, NULL, width, FALSE, NULL, NULL, NULL);
614 * adg_table_cell_new_before:
615 * @cell: a valid #AdgTableCell
616 * @width: width of the cell
618 * Creates a new cell and inserts it rigthly before the @cell cell.
619 * This works similarily and accepts the same parameters as the
620 * adg_table_cell_new() function.
622 * Returns: the newly created cell or %NULL on errors
624 AdgTableCell *
625 adg_table_cell_new_before(AdgTableCell *cell, gdouble width)
627 g_return_val_if_fail(cell != NULL, NULL);
628 g_return_val_if_fail(cell->row != NULL, NULL);
629 g_return_val_if_fail(width >= 0, NULL);
631 return cell_new(cell->row, cell, width, FALSE, NULL, NULL, NULL);
635 * adg_table_cell_new_full:
636 * @row: a valid #AdgTableRow
637 * @width: width of the cell
638 * @name: name to associate
639 * @title: title to render
640 * @value: value to render
642 * A convenient function to append a framed cell to @row with a
643 * specific title and value text. The font to use for rendering
644 * @title and @value will be picked up from the table style, so
645 * be sure to have the correct table dress set before calling
646 * this function.
648 * @row and @width have the same meanings as in adg_table_cell_new():
649 * check its documentation for details.
651 * @name is an optional identifier to univoquely access this cell
652 * by using adg_table_cell(). The identifier must be univoque:
653 * if there is yet a cell with the same name a warning message will
654 * be raised and the function will fail.
656 * Returns: the newly created cell or %NULL on errors
658 AdgTableCell *
659 adg_table_cell_new_full(AdgTableRow *row, gdouble width, const gchar *name,
660 const gchar *title, const gchar *value)
662 AdgTableCell *cell;
664 g_return_val_if_fail(row != NULL, NULL);
666 cell = cell_new(row, NULL, width, TRUE, name, NULL, NULL);
668 if (title != NULL)
669 adg_table_cell_set_text_title(cell, title);
671 if (value != NULL)
672 adg_table_cell_set_text_value(cell, value);
674 return cell;
678 * adg_table_cell_delete:
679 * @cell: a valid #AdgTableCell
681 * Deletes @cell removing it from the container row and freeing
682 * any resource associated to it.
684 void
685 adg_table_cell_delete(AdgTableCell *cell)
687 AdgTableRow *row;
689 g_return_if_fail(cell != NULL);
691 row = cell->row;
693 g_return_if_fail(row != NULL);
695 cell_free(cell);
696 row->cells = g_slist_remove(row->cells, cell);
700 * adg_table_cell_get_name:
701 * @cell: a valid #AdgTableCell
703 * Gets the name assigned to @cell. This function is highly inefficient
704 * as the cell names are stored in a hash table optimized to get a cell
705 * from a name. Getting the name from a cell involves a full hash table
706 * inspection.
708 * Returns: the name bound of @cell or %NULL on no name or errors
710 const gchar *
711 adg_table_cell_get_name(AdgTableCell *cell)
713 AdgTablePrivate *data;
715 g_return_val_if_fail(cell != NULL, NULL);
717 data = cell->row->table->data;
719 return g_hash_table_find(data->cell_names, value_match, cell);
723 * adg_table_cell_set_name:
724 * @cell: a valid #AdgTableCell
725 * @name: the new name of @cell
727 * Sets a new name on @cell: this will allow to access @cell by
728 * name at a later time using the adg_table_cell() API.
730 void
731 adg_table_cell_set_name(AdgTableCell *cell, const gchar *name)
733 AdgTablePrivate *data;
735 g_return_if_fail(cell != NULL);
737 data = cell->row->table->data;
739 cell_set_name(cell, NULL);
740 cell_set_name(cell, name);
744 * adg_table_cell_get_title:
745 * @cell: a valid #AdgTableCell
747 * Gets the current title of @cell. The returned string is owned
748 * by @cell and must not be modified or freed.
750 * Returns: the title entity or %NULL for undefined title
752 AdgEntity *
753 adg_table_cell_get_title(AdgTableCell *cell)
755 g_return_val_if_fail(cell != NULL, NULL);
757 return cell->title;
761 * adg_table_cell_set_title:
762 * @cell: a valid #AdgTableCell
763 * @title: the new title entity
765 * Sets @title as the new title entity of @cell. The top left
766 * corner of the bounding box of @title will be cohincident to
767 * the top left corner of the cell extents, taking into accounts
768 * eventual padding spaces specified by the table style.
770 * The old internal entity is unrefenrenced while the @title (if
771 * not %NULL) is refenenced with g_object_ref_sink().
773 * @title can be %NULL, in which case the old entity is removed.
775 void
776 adg_table_cell_set_title(AdgTableCell *cell, AdgEntity *title)
778 g_return_if_fail(cell != NULL);
779 g_return_if_fail(title == NULL || ADG_IS_ENTITY(title));
781 if (cell_set_title(cell, title))
782 adg_entity_invalidate((AdgEntity *) cell->row->table);
786 * adg_table_cell_set_text_title:
787 * @cell: a valid #AdgTableCell
788 * @title: a text string
790 * Convenient function to set a the title of a cell using an #AdgToyText
791 * entity with the font dress picked from #AdgTable:table-dress with
792 * a call to adg_table_style_get_title_dress().
794 void
795 adg_table_cell_set_text_title(AdgTableCell *cell, const gchar *title)
797 AdgTable *table;
798 AdgEntity *entity;
799 AdgTableStyle *table_style;
800 const AdgPair *padding;
801 AdgDress table_dress, font_dress;
802 AdgMatrix map;
804 g_return_if_fail(cell != NULL);
806 if (title == NULL)
807 adg_table_cell_set_title(cell, NULL);
809 if (cell->title != NULL) {
810 const gchar *old_title;
812 if (ADG_IS_TOY_TEXT(cell->title))
813 old_title = adg_toy_text_get_label((AdgToyText *) cell->title);
814 else
815 old_title = NULL;
817 if (adg_strcmp(title, old_title) == 0)
818 return;
821 table = cell->row->table;
822 table_dress = adg_table_get_table_dress(table);
823 table_style = (AdgTableStyle *) adg_entity_style((AdgEntity *) table,
824 table_dress);
825 padding = adg_table_style_get_cell_padding(table_style);
826 font_dress = adg_table_style_get_title_dress(table_style);
827 entity = g_object_new(ADG_TYPE_TOY_TEXT, "label", title,
828 "font-dress", font_dress, NULL);
830 cairo_matrix_init_translate(&map, padding->x, padding->y);
831 adg_entity_set_global_map(entity, &map);
833 adg_table_cell_set_title(cell, entity);
837 * adg_table_cell_set_value:
838 * @cell: a valid #AdgTableCell
839 * @value: the new value entity
841 * Sets @value as the new value entity of @cell. The bottom middle
842 * point of the bounding box of @value will be cohincident to the
843 * bottom middle point of the cell extents, taking into accounts
844 * eventual padding spaces specified by the table style.
846 * The old internal entity is unrefenrenced while the @value (if
847 * not %NULL) is refenenced with g_object_ref_sink().
849 * @value can be %NULL, in which case the old entity is removed.
851 void
852 adg_table_cell_set_value(AdgTableCell *cell, AdgEntity *value)
854 g_return_if_fail(cell != NULL);
855 g_return_if_fail(value == NULL || ADG_IS_ENTITY(value));
857 if (cell_set_value(cell, value))
858 adg_entity_invalidate((AdgEntity *) cell->row->table);
862 * adg_table_cell_set_text_value:
863 * @cell: a valid #AdgTableCell
864 * @value: a text string
866 * Convenient function to set a the value of a cell using an #AdgToyText
867 * entity with a value font dress picked from #AdgTable:table-dress with
868 * a call to adg_table_style_get_value_dress().
870 void
871 adg_table_cell_set_text_value(AdgTableCell *cell, const gchar *value)
873 AdgTable *table;
874 AdgEntity *entity;
875 AdgTableStyle *table_style;
876 const AdgPair *padding;
877 AdgDress table_dress, font_dress;
878 AdgMatrix map;
880 g_return_if_fail(cell != NULL);
882 if (value == NULL)
883 adg_table_cell_set_value(cell, NULL);
885 if (cell->value != NULL) {
886 const gchar *old_value;
888 if (ADG_IS_TOY_TEXT(cell->value))
889 old_value = adg_toy_text_get_label((AdgToyText *) cell->value);
890 else
891 old_value = NULL;
893 if (adg_strcmp(value, old_value) == 0)
894 return;
897 table = cell->row->table;
898 table_dress = adg_table_get_table_dress(table);
899 table_style = (AdgTableStyle *) adg_entity_style((AdgEntity *) table,
900 table_dress);
901 padding = adg_table_style_get_cell_padding(table_style);
902 font_dress = adg_table_style_get_value_dress(table_style);
903 entity = g_object_new(ADG_TYPE_TOY_TEXT, "label", value,
904 "font-dress", font_dress, NULL);
906 cairo_matrix_init_translate(&map, 0, -padding->y);
907 adg_entity_set_global_map(entity, &map);
909 adg_table_cell_set_value(cell, entity);
913 * adg_table_cell_value:
914 * @cell: a valid #AdgTableCell
916 * Gets the current value of @cell. The returned string is owned
917 * by @cell and must not be modified or freed.
919 * Returns: the value entity or %NULL for undefined value
921 AdgEntity *
922 adg_table_cell_value(AdgTableCell *cell)
924 g_return_val_if_fail(cell != NULL, NULL);
926 return cell->value;
930 * adg_table_cell_set_value_pos:
931 * @cell: a valid #AdgTableCell
932 * @from_factor: the alignment factor on the value entity
933 * @to_factor: the alignment factor on the cell
935 * Sets a new custom position for the value entity of @cell. The
936 * @from_factor specifies the source point (as a fraction of the
937 * value extents) while the @to_factor is the destination point
938 * (specified as a fraction of the cell extents) the source point
939 * must be moved to.
941 void
942 adg_table_cell_set_value_pos(AdgTableCell *cell, const AdgPair *from_factor,
943 const AdgPair *to_factor)
945 g_return_if_fail(cell != NULL);
946 g_return_if_fail(cell->value != NULL);
948 cell_set_value_pos(cell, from_factor, to_factor);
952 * adg_table_cell_set_width:
953 * @cell: a valid #AdgTableCell
954 * @width: the new width
956 * Sets a new width on @cell. The extents on the whole table
957 * will be invalidated, so will be recomputed in the next
958 * arrange() phase.
960 void
961 adg_table_cell_set_width(AdgTableCell *cell, gdouble width)
963 g_return_if_fail(cell != NULL);
965 cell->width = width;
967 adg_entity_invalidate((AdgEntity *) cell->row->table);
971 * adg_table_cell_get_width:
972 * @cell: a valid #AdgTableCell
974 * Gets the width of @cell.
976 * Returns: the requested width or %0 on errors
978 gdouble
979 adg_table_cell_get_width(AdgTableCell *cell)
981 g_return_val_if_fail(cell != NULL, 0.);
983 return cell->width;
987 * adg_table_cell_has_frame:
988 * @cell: a valid #AdgTableCell
990 * Gets the frame flag of @cell.
992 * Returns: the frame flag
994 gboolean
995 adg_table_cell_has_frame(AdgTableCell *cell)
997 g_return_val_if_fail(cell != NULL, FALSE);
999 return cell->has_frame;
1003 * adg_table_cell_switch_frame:
1004 * @cell: a valid #AdgTableCell
1005 * @new_state: the new frame state
1007 * Sets the frame flag of @cell: if @new_state is %TRUE, a frame around
1008 * @cell will be rendered using the #AdgTableStyle:cell-dress dress
1009 * of the table style.
1011 void
1012 adg_table_cell_switch_frame(AdgTableCell *cell, gboolean new_state)
1014 AdgTablePrivate *data;
1016 g_return_if_fail(cell != NULL);
1018 if (cell->has_frame == new_state)
1019 return;
1021 data = cell->row->table->data;
1022 cell->has_frame = new_state;
1024 if (data->grid != NULL) {
1025 g_object_unref(data->grid);
1026 data->grid = NULL;
1031 * adg_table_cell_extents:
1032 * @cell: a valid #AdgTableCell
1034 * Gets the extents of @cell. This function is useful only after the
1035 * arrange() phase as in the other situation the extents will likely
1036 * be not up to date.
1038 * Returns: the extents of @cell or %NULL on errors
1040 const CpmlExtents *
1041 adg_table_cell_extents(AdgTableCell *cell)
1043 g_return_val_if_fail(cell != NULL, NULL);
1045 return &cell->extents;
1049 static void
1050 global_changed(AdgEntity *entity)
1052 if (PARENT_ENTITY_CLASS->global_changed)
1053 PARENT_ENTITY_CLASS->global_changed(entity);
1055 propagate((AdgTable *) entity, "global-changed");
1058 static void
1059 local_changed(AdgEntity *entity)
1061 if (PARENT_ENTITY_CLASS->local_changed)
1062 PARENT_ENTITY_CLASS->local_changed(entity);
1064 propagate((AdgTable *) entity, "local-changed");
1067 static void
1068 invalidate(AdgEntity *entity)
1070 propagate((AdgTable *) entity, "invalidate");
1073 static void
1074 arrange(AdgEntity *entity)
1076 AdgTable *table;
1077 AdgTablePrivate *data;
1078 CpmlExtents extents;
1079 const AdgPair *spacing;
1080 GSList *row_node;
1081 AdgTableRow *row;
1082 gdouble y;
1084 table = (AdgTable *) entity;
1085 data = table->data;
1086 cpml_extents_copy(&extents, adg_entity_get_extents(entity));
1088 /* Resolve the table style */
1089 if (data->table_style == NULL)
1090 data->table_style = (AdgTableStyle *)
1091 adg_entity_style(entity, data->table_dress);
1093 if (extents.is_defined) {
1094 if (data->grid != NULL)
1095 adg_entity_arrange((AdgEntity *) data->grid);
1096 if (data->frame != NULL)
1097 adg_entity_arrange((AdgEntity *) data->frame);
1098 return;
1101 spacing = adg_table_style_get_cell_spacing(data->table_style);
1102 extents.size.x = 0;
1103 extents.size.y = 0;
1105 for (row_node = data->rows; row_node; row_node = row_node->next) {
1106 row = row_node->data;
1108 row_arrange_size(row);
1110 if (row->extents.size.x > extents.size.x)
1111 extents.size.x = row->extents.size.x;
1112 extents.size.y += row->extents.size.y;
1115 /* TODO: update the org according to the table alignments */
1117 y = extents.org.y + spacing->y;
1118 for (row_node = data->rows; row_node; row_node = row_node->next) {
1119 row = row_node->data;
1121 row->extents.org.x = extents.org.x;
1122 row->extents.org.y = y;
1124 row_arrange(row);
1126 y += row->extents.size.y + spacing->y;
1129 extents.is_defined = TRUE;
1130 adg_entity_set_extents(entity, &extents);
1132 arrange_grid(entity);
1133 arrange_frame(entity);
1136 static void
1137 arrange_grid(AdgEntity *entity)
1139 AdgTablePrivate *data;
1140 AdgPath *path;
1141 AdgTrail *trail;
1142 GSList *row_node, *cell_node;
1143 AdgTableRow *row;
1144 AdgTableCell *cell;
1145 AdgPair pair;
1146 AdgDress dress;
1148 data = ((AdgTable *) entity)->data;
1150 if (data->grid != NULL)
1151 return;
1153 path = adg_path_new();
1154 trail = (AdgTrail *) path;
1156 for (row_node = data->rows; row_node; row_node = row_node->next) {
1157 row = row_node->data;
1159 for (cell_node = row->cells; cell_node; cell_node = cell_node->next) {
1160 cell = cell_node->data;
1162 if (!cell->has_frame)
1163 continue;
1165 cpml_pair_copy(&pair, &cell->extents.org);
1166 adg_path_move_to(path, &pair);
1167 pair.x += cell->extents.size.x;
1168 adg_path_line_to(path, &pair);
1169 pair.y += cell->extents.size.y;
1170 adg_path_line_to(path, &pair);
1171 pair.x -= cell->extents.size.x;
1172 adg_path_line_to(path, &pair);
1173 adg_path_close(path);
1177 if (!adg_trail_extents(trail)->is_defined)
1178 return;
1180 dress = adg_table_style_get_grid_dress(data->table_style);
1181 data->grid = g_object_new(ADG_TYPE_STROKE,
1182 "local-method", ADG_MIX_PARENT,
1183 "line-dress", dress,
1184 "trail", trail,
1185 "parent", entity, NULL);
1186 adg_entity_arrange((AdgEntity *) data->grid);
1189 static void
1190 arrange_frame(AdgEntity *entity)
1192 AdgTablePrivate *data;
1193 AdgPath *path;
1194 const CpmlExtents *extents;
1195 AdgPair pair;
1196 AdgDress dress;
1198 data = ((AdgTable *) entity)->data;
1200 if (data->frame != NULL || !data->has_frame)
1201 return;
1203 path = adg_path_new();
1204 extents = adg_entity_get_extents(entity);
1206 cpml_pair_copy(&pair, &extents->org);
1207 adg_path_move_to(path, &pair);
1208 pair.x += extents->size.x;
1209 adg_path_line_to(path, &pair);
1210 pair.y += extents->size.y;
1211 adg_path_line_to(path, &pair);
1212 pair.x -= extents->size.x;
1213 adg_path_line_to(path, &pair);
1214 adg_path_close(path);
1216 dress = adg_table_style_get_frame_dress(data->table_style);
1218 data->frame = g_object_new(ADG_TYPE_STROKE,
1219 "local-method", ADG_MIX_PARENT,
1220 "line-dress", dress,
1221 "trail", (AdgTrail *) path,
1222 "parent", entity, NULL);
1223 adg_entity_arrange((AdgEntity *) data->frame);
1226 static void
1227 render(AdgEntity *entity, cairo_t *cr)
1229 cairo_transform(cr, adg_entity_get_local_matrix(entity));
1231 propagate((AdgTable *) entity, "render", cr);
1234 static gboolean
1235 switch_frame(AdgTable *table, gboolean new_state)
1237 AdgTablePrivate *data = table->data;
1239 if (data->has_frame == new_state)
1240 return FALSE;
1242 data->has_frame = new_state;
1244 if (data->frame != NULL) {
1245 g_object_unref(data->frame);
1246 data->frame = NULL;
1249 return TRUE;
1252 static void
1253 propagate(AdgTable *table, const gchar *detailed_signal, ...)
1255 guint signal_id;
1256 GQuark detail = 0;
1257 va_list var_args, var_copy;
1258 AdgTablePrivate *data;
1259 GSList *row_node;
1260 AdgTableRow *row;
1261 GSList *cell_node;
1262 AdgTableCell *cell;
1263 AdgAlignment *alignment;
1265 if (!g_signal_parse_name(detailed_signal, G_TYPE_FROM_INSTANCE(table),
1266 &signal_id, &detail, FALSE)) {
1267 g_assert_not_reached();
1270 va_start(var_args, detailed_signal);
1271 data = table->data;
1273 if (data->frame != NULL) {
1274 G_VA_COPY(var_copy, var_args);
1275 g_signal_emit_valist(data->frame, signal_id, detail, var_copy);
1278 if (data->grid != NULL) {
1279 G_VA_COPY(var_copy, var_args);
1280 g_signal_emit_valist(data->grid, signal_id, detail, var_copy);
1283 for (row_node = data->rows; row_node; row_node = row_node->next) {
1284 row = row_node->data;
1286 for (cell_node = row->cells; cell_node; cell_node = cell_node->next) {
1287 cell = cell_node->data;
1289 if (cell->title != NULL) {
1290 alignment = (AdgAlignment *) adg_entity_get_parent(cell->title);
1292 G_VA_COPY(var_copy, var_args);
1293 g_signal_emit_valist(alignment, signal_id, detail, var_copy);
1296 if (cell->value != NULL) {
1297 alignment = (AdgAlignment *) adg_entity_get_parent(cell->value);
1299 G_VA_COPY(var_copy, var_args);
1300 g_signal_emit_valist(alignment, signal_id, detail, var_copy);
1305 va_end(var_args);
1308 static AdgTableRow *
1309 row_new(AdgTable *table, AdgTableRow *before_row)
1311 AdgTablePrivate *data;
1312 AdgTableRow *new_row;
1314 data = table->data;
1315 new_row = g_new(AdgTableRow, 1);
1316 new_row->table = table;
1317 new_row->cells = NULL;
1318 new_row->height = 0;
1319 new_row->extents.is_defined = FALSE;
1321 if (before_row == NULL) {
1322 data->rows = g_slist_append(data->rows, new_row);
1323 } else {
1324 GSList *before_node = g_slist_find(data->rows, before_row);
1326 /* This MUST be present, otherwise something really bad happened */
1327 g_assert(before_node != NULL);
1329 data->rows = g_slist_insert_before(data->rows, before_node, new_row);
1332 invalidate((AdgEntity *) table);
1334 return new_row;
1337 static void
1338 row_arrange_size(AdgTableRow *row)
1340 AdgTableStyle *table_style;
1341 const AdgPair *spacing;
1342 CpmlVector *size;
1343 AdgTableCell *cell;
1344 GSList *cell_node;
1346 table_style = GET_TABLE_STYLE(row->table);
1347 spacing = adg_table_style_get_cell_spacing(table_style);
1348 size = &row->extents.size;
1350 size->x = 0;
1351 if (row->height == 0)
1352 size->y = adg_table_style_get_row_height(table_style);
1353 else
1354 size->y = row->height;
1356 /* Compute the row width by summing every cell width */
1357 for (cell_node = row->cells; cell_node; cell_node = cell_node->next) {
1358 cell = cell_node->data;
1360 cell_arrange_size(cell);
1362 size->x += cell->extents.size.x + spacing->x;
1365 if (size->x > 0)
1366 size->x += spacing->x;
1369 /* Before calling this function, row->extents should be updated */
1370 static void
1371 row_arrange(AdgTableRow *row)
1373 AdgTableStyle *table_style;
1374 const AdgPair *spacing;
1375 const AdgPair *org;
1376 AdgTableCell *cell;
1377 GSList *cell_node;
1378 gdouble x;
1380 table_style = GET_TABLE_STYLE(row->table);
1381 spacing = adg_table_style_get_cell_spacing(table_style);
1382 org = &row->extents.org;
1383 x = org->x + spacing->x;
1385 for (cell_node = row->cells; cell_node; cell_node = cell_node->next) {
1386 cell = cell_node->data;
1388 cell->extents.org.x = x;
1389 cell->extents.org.y = org->y;
1391 cell_arrange(cell);
1393 x += cell->extents.size.x + spacing->x;
1396 row->extents.is_defined = TRUE;
1399 static void
1400 row_dispose(AdgTableRow *row)
1402 g_slist_foreach(row->cells, (GFunc) cell_dispose, NULL);
1405 static void
1406 row_free(AdgTableRow *row)
1408 g_slist_foreach(row->cells, (GFunc) cell_free, NULL);
1409 g_slist_free(row->cells);
1411 g_free(row);
1414 static AdgTableCell *
1415 cell_new(AdgTableRow *row, AdgTableCell *before_cell,
1416 gdouble width, gboolean has_frame,
1417 const gchar *name, AdgEntity *title, AdgEntity *value)
1419 AdgTablePrivate *data;
1420 AdgTableCell *new_cell;
1422 data = row->table->data;
1424 if (name != NULL && data->cell_names != NULL &&
1425 g_hash_table_lookup(data->cell_names, name) != NULL) {
1426 g_warning(_("%s: `%s' cell name is yet used"), G_STRLOC, name);
1427 return NULL;
1430 new_cell = g_new(AdgTableCell, 1);
1431 new_cell->row = row;
1432 new_cell->width = width;
1433 new_cell->has_frame = has_frame;
1434 new_cell->title = NULL;
1435 new_cell->value = NULL;
1436 new_cell->extents.is_defined = FALSE;
1437 new_cell->value_factor.x = 0.5;
1438 new_cell->value_factor.y = 1;
1440 cell_set_title(new_cell, title);
1441 cell_set_value(new_cell, value);
1443 if (before_cell == NULL) {
1444 row->cells = g_slist_append(row->cells, new_cell);
1445 } else {
1446 GSList *before_node = g_slist_find(row->cells, before_cell);
1448 /* This MUST be not null, otherwise something really bad happened */
1449 g_assert(before_node != NULL);
1451 row->cells = g_slist_insert_before(row->cells, before_node, new_cell);
1454 if (name != NULL)
1455 cell_set_name(new_cell, name);
1457 return new_cell;
1460 static void
1461 cell_set_name(AdgTableCell *cell, const gchar *name)
1463 AdgTablePrivate *data = cell->row->table->data;
1465 if (data->cell_names == NULL && name == NULL)
1466 return;
1468 if (data->cell_names == NULL)
1469 data->cell_names = g_hash_table_new_full(g_str_hash, g_str_equal,
1470 g_free, NULL);
1472 if (name == NULL)
1473 g_hash_table_foreach_remove(data->cell_names, value_match, cell);
1474 else
1475 g_hash_table_insert(data->cell_names, g_strdup(name), cell);
1478 static gboolean
1479 cell_set_title(AdgTableCell *cell, AdgEntity *title)
1481 AdgAlignment *alignment;
1483 if (cell->title == title)
1484 return FALSE;
1486 if (cell->title != NULL) {
1487 alignment = (AdgAlignment *) adg_entity_get_parent(cell->title);
1488 g_object_unref(alignment);
1491 cell->title = title;
1493 if (title != NULL) {
1494 alignment = adg_alignment_new_explicit(0, -1);
1495 g_object_ref_sink(alignment);
1496 adg_entity_set_parent((AdgEntity *) alignment,
1497 (AdgEntity *) cell->row->table);
1499 adg_container_add((AdgContainer *) alignment, title);
1502 return TRUE;
1505 static gboolean
1506 cell_set_value(AdgTableCell *cell, AdgEntity *value)
1508 AdgAlignment *alignment;
1510 if (cell->value == value)
1511 return FALSE;
1513 if (cell->value != NULL) {
1514 alignment = (AdgAlignment *) adg_entity_get_parent(cell->value);
1515 g_object_unref(alignment);
1518 cell->value = value;
1520 if (value != NULL) {
1521 alignment = adg_alignment_new_explicit(0.5, 0);
1522 g_object_ref_sink(alignment);
1523 adg_entity_set_parent((AdgEntity *) alignment,
1524 (AdgEntity *) cell->row->table);
1526 adg_container_add((AdgContainer *) alignment, value);
1529 return TRUE;
1532 static void
1533 cell_set_value_pos(AdgTableCell *cell,
1534 const AdgPair *from_factor, const AdgPair *to_factor)
1536 AdgAlignment *alignment;
1538 alignment = (AdgAlignment *) adg_entity_get_parent(cell->value);
1540 if (from_factor != NULL)
1541 adg_alignment_set_factor(alignment, from_factor);
1543 if (to_factor != NULL)
1544 cell->value_factor = *to_factor;
1547 static void
1548 cell_arrange_size(AdgTableCell *cell)
1550 CpmlVector *size;
1551 AdgAlignment *title_alignment, *value_alignment;
1553 size = &cell->extents.size;
1555 if (cell->title != NULL) {
1556 title_alignment = (AdgAlignment *) adg_entity_get_parent(cell->title);
1557 adg_entity_arrange((AdgEntity *) title_alignment);
1560 if (cell->value != NULL) {
1561 value_alignment = (AdgAlignment *) adg_entity_get_parent(cell->value);
1562 adg_entity_arrange((AdgEntity *) value_alignment);
1565 size->y = cell->row->extents.size.y;
1567 if (cell->width == 0) {
1568 AdgTableStyle *table_style = GET_TABLE_STYLE(cell->row->table);
1569 const CpmlExtents *extents;
1571 /* The width depends on the cell content (default = 0) */
1572 size->x = 0;
1574 if (cell->title != NULL) {
1575 extents = adg_entity_get_extents((AdgEntity *) title_alignment);
1576 size->x = extents->size.x;
1579 if (cell->value != NULL) {
1580 extents = adg_entity_get_extents((AdgEntity *) value_alignment);
1581 if (extents->size.x > size->x)
1582 size->x = extents->size.x;
1585 size->x += adg_table_style_get_cell_spacing(table_style)->x * 2;
1586 } else {
1587 size->x = cell->width;
1591 /* Before calling this function, cell->extents should be updated */
1592 static void
1593 cell_arrange(AdgTableCell *cell)
1595 CpmlExtents *extents;
1596 AdgAlignment *alignment;
1597 AdgMatrix map;
1599 extents = &cell->extents;
1601 if (cell->title != NULL) {
1602 alignment = (AdgAlignment *) adg_entity_get_parent(cell->title);
1604 cairo_matrix_init_translate(&map, extents->org.x, extents->org.y);
1605 adg_entity_set_global_map((AdgEntity *) alignment, &map);
1608 if (cell->value != NULL) {
1609 AdgPair to;
1611 alignment = (AdgAlignment *) adg_entity_get_parent(cell->value);
1612 to.x = extents->size.x * cell->value_factor.x;
1613 to.y = extents->size.y * cell->value_factor.y;
1614 cpml_pair_add(&to, &extents->org);
1616 cairo_matrix_init_translate(&map, to.x, to.y);
1617 adg_entity_set_global_map((AdgEntity *) alignment, &map);
1620 extents->is_defined = TRUE;
1623 static void
1624 cell_dispose(AdgTableCell *cell)
1626 cell_set_title(cell, NULL);
1627 cell_set_value(cell, NULL);
1630 static void
1631 cell_free(AdgTableCell *cell)
1633 cell_set_name(cell, NULL);
1634 cell_dispose(cell);
1635 g_free(cell);
1638 static gboolean
1639 value_match(gpointer key, gpointer value, gpointer user_data)
1641 return value == user_data;