tests: additional checks on container types
[adg.git] / src / adg / adg-table-cell.c
blob0fbe9e7480af25a5e7ae9a781a529cab136d471e
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2015 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-cell
23 * @Section_Id:AdgTableCell
24 * @title: AdgTableCell
25 * @short_description: A single cell of a table
27 * The #AdgTableCell is a boxed type, the basic component of an
28 * #AdgTable entity. It must be added to an #AdgTableRow that,
29 * in cascade, will be added to an #AdgTable entity.
31 * Any cell can be filled with a title and a value: the font to be
32 * used will be picked up from the #AdgTableStyle got by resolving
33 * the #AdgTable:table-dress property.
35 * The default title is placed at the upper left corner of the cell
36 * while the value is centered up to the bottom edge of the cell.
37 * Anyway the text positioning can be customized by using the
38 * adg_table_cell_set_value_pos() method.
40 * Some convenient functions to easily create title and value entities
41 * with plain text are provided: adg_table_cell_new_full(),
42 * adg_table_cell_set_text_title() and adg_table_cell_set_text_value().
43 * When using these methods keep in mind the underlying #AdgToyText
44 * entities will be displaced accordingly to the
45 * #AdgTableStyle:cell-padding value, not used when setting the
46 * entities throught other APIs.
48 * Since: 1.0
49 **/
51 /**
52 * AdgTableCell:
54 * An opaque structure referring to the cell of an #AdgTableRow.
55 * Any row can have an unlimited number of cells.
57 * Since: 1.0
58 **/
61 #include "adg-internal.h"
62 #include "adg-text-internal.h"
64 #include "adg-container.h"
65 #include "adg-alignment.h"
66 #include "adg-textual.h"
67 #include "adg-style.h"
68 #include "adg-table-style.h"
70 #include "adg-table.h"
71 #include "adg-table-row.h"
72 #include "adg-table-cell.h"
75 struct _AdgTableCell {
76 AdgTableRow *row;
77 gdouble width;
78 gboolean has_frame;
79 AdgEntity *title;
80 AdgEntity *value;
81 CpmlPair value_factor;
82 CpmlExtents extents;
86 static AdgTableCell * _adg_cell_new (void);
87 static void _adg_cell_invalidate (AdgTableCell *table_cell);
88 static gboolean _adg_cell_set_title (AdgTableCell *table_cell,
89 AdgEntity *title);
90 static gboolean _adg_cell_set_value (AdgTableCell *table_cell,
91 AdgEntity *value);
92 static void _adg_cell_set_value_pos (AdgTableCell *table_cell,
93 const CpmlPair *from_factor,
94 const CpmlPair *to_factor);
97 GType
98 adg_table_cell_get_type(void)
100 static GType cell_type = 0;
102 if (G_UNLIKELY(cell_type == 0))
103 cell_type = g_boxed_type_register_static("AdgTableCell",
104 (GBoxedCopyFunc) adg_table_cell_dup,
105 (GBoxedFreeFunc) adg_table_cell_free);
107 return cell_type;
112 * adg_table_cell_dup:
113 * @src: an #AdgTableCell structure
115 * Duplicates @src. The returned duplicate should be freed
116 * with adg_table_cell_free() when no longer needed.
118 * Returns: (transfer full): a duplicate of @src.
120 * Since: 1.0
122 AdgTableCell *
123 adg_table_cell_dup(const AdgTableCell *src)
125 g_return_val_if_fail(src != NULL, NULL);
126 return g_memdup(src, sizeof(AdgTableCell));
130 * adg_table_cell_new:
131 * @table_row: a valid #AdgTableRow
133 * Creates a new empty cell without a frame and appends it at the
134 * end of the cells yet present in @table_row. You can add content
135 * to the cell by using adg_table_cell_set_title() and
136 * adg_table_cell_set_value() or enable the frame with
137 * adg_table_cell_switch_frame().
139 * Returns: (transfer full): the newly created cell or <constant>NULL</constant> on errors.
141 * Since: 1.0
143 AdgTableCell *
144 adg_table_cell_new(AdgTableRow *table_row)
146 AdgTableCell *table_cell;
148 g_return_val_if_fail(table_row != NULL, NULL);
150 table_cell = _adg_cell_new();
151 table_cell->row = table_row;
153 adg_table_row_insert(table_row, table_cell, NULL);
154 _adg_cell_invalidate(table_cell);
156 return table_cell;
160 * adg_table_cell_new_before:
161 * @before_cell: a valid #AdgTableCell
163 * Creates a new cell and inserts it rigthly before the @table_cell cell.
165 * Returns: (transfer full): the newly created cell or <constant>NULL</constant> on errors.
167 * Since: 1.0
169 AdgTableCell *
170 adg_table_cell_new_before(AdgTableCell *before_cell)
172 AdgTableRow *table_row;
173 AdgTableCell *table_cell;
175 g_return_val_if_fail(before_cell != NULL, NULL);
177 table_row = before_cell->row;
178 g_return_val_if_fail(table_row != NULL, NULL);
180 table_cell = _adg_cell_new();
181 table_cell->row = table_row;
183 adg_table_row_insert(table_row, table_cell, NULL);
184 _adg_cell_invalidate(table_cell);
186 return table_cell;
190 * adg_table_cell_new_with_width:
191 * @table_row: a valid #AdgTableRow
192 * @width: the cell width
194 * A convenient wrapper to adg_table_cell_new() that allows to
195 * specify the @width of @table_row all at once.
197 * Returns: (transfer full): the newly created cell or <constant>NULL</constant> on errors.
199 * Since: 1.0
201 AdgTableCell *
202 adg_table_cell_new_with_width(AdgTableRow *table_row, gdouble width)
204 AdgTableCell *table_cell = adg_table_cell_new(table_row);
206 if (table_cell != NULL)
207 adg_table_cell_set_width(table_cell, width);
209 return table_cell;
213 * adg_table_cell_new_full:
214 * @table_row: a valid #AdgTableRow
215 * @width: the cell width
216 * @name: (allow-none): the name to bound to this cell
217 * @title: (allow-none): the title text
218 * @has_frame: whether to draw or not the frame
220 * A convenient function to add a cell and specifies some common
221 * used properties at once.
223 * If @name is <constant>NULL</constant>, the created cell will
224 * not be a named cell. Check adg_table_set_cell() for further
225 * details on what a named cell is supposed to be.
227 * @title can be <constant>NULL</constant>, in which case no
228 * title entity will be created.
230 * Returns: (transfer full): the newly created cell or <constant>NULL</constant> on errors.
232 * Since: 1.0
234 AdgTableCell *
235 adg_table_cell_new_full(AdgTableRow *table_row, gdouble width,
236 const gchar *name, const gchar *title,
237 gboolean has_frame)
239 AdgTableCell *table_cell = adg_table_cell_new(table_row);
241 if (table_cell == NULL)
242 return NULL;
244 adg_table_cell_set_width(table_cell, width);
245 adg_table_cell_switch_frame(table_cell, has_frame);
247 if (title) {
248 adg_table_cell_set_text_title(table_cell, title);
250 if (name) {
251 AdgTable *table = adg_table_row_get_table(table_row);
252 adg_table_set_cell(table, name, table_cell);
255 return table_cell;
259 * adg_table_cell_dispose:
260 * @table_cell: a valid #AdgTableCell struct
262 * Disposes @table_cell.
264 * Since: 1.0
266 void
267 adg_table_cell_dispose(AdgTableCell *table_cell)
269 _adg_cell_set_title(table_cell, NULL);
270 _adg_cell_set_value(table_cell, NULL);
274 * adg_table_cell_free:
275 * @table_cell: an #AdgTableCell structure
277 * Releases all the memory allocated by @table_cell, itself included.
279 * Since: 1.0
281 void
282 adg_table_cell_free(AdgTableCell *table_cell)
284 AdgTableRow *table_row = table_cell->row;
286 if (table_cell->row != NULL) {
287 AdgTable *table = adg_table_row_get_table(table_row);
288 adg_table_row_remove(table_row, table_cell);
289 if (table)
290 adg_table_set_cell(table, NULL, table_cell);
293 adg_table_cell_dispose(table_cell);
294 g_free(table_cell);
298 * adg_table_cell_get_row:
299 * @table_cell: a valid #AdgTableCell
301 * Gets the row container of @table_cell. The returned #AdgTableRow
302 * is owned by @table_cell and must not be modified or freed.
304 * Returns: (transfer none): the container row.
306 * Since: 1.0
308 AdgTableRow *
309 adg_table_cell_get_row(AdgTableCell *table_cell)
311 g_return_val_if_fail(table_cell != NULL, NULL);
312 return table_cell->row;
316 * adg_table_cell_get_table:
317 * @table_cell: a valid #AdgTableCell
319 * A convenient function that gets the table that contains
320 * @table_cell. The returned #AdgTable is owned by @table_cell
321 * and must not be modified or freed.
323 * Returns: (transfer none): the container table.
325 * Since: 1.0
327 AdgTable *
328 adg_table_cell_get_table(AdgTableCell *table_cell)
330 AdgTableRow *table_row = adg_table_cell_get_row(table_cell);
331 if (table_row == NULL)
332 return NULL;
334 return adg_table_row_get_table(table_row);
338 * adg_table_cell_set_title:
339 * @table_cell: a valid #AdgTableCell
340 * @title: the new title entity
342 * Sets @title as the new title entity of @table_cell. The top left
343 * corner of the bounding box of @title will be cohincident to
344 * the top left corner of the cell extents, taking into accounts
345 * eventual padding spaces specified by the table style.
347 * The old internal entity is unrefenrenced while the @title (if
348 * not <constant>NULL</constant>) is refenenced with
349 * g_object_ref_sink().
351 * @title can be <constant>NULL</constant>, in which case the old
352 * entity is removed.
354 * Since: 1.0
356 void
357 adg_table_cell_set_title(AdgTableCell *table_cell, AdgEntity *title)
359 g_return_if_fail(table_cell != NULL);
360 g_return_if_fail(title == NULL || ADG_IS_ENTITY(title));
362 if (_adg_cell_set_title(table_cell, title))
363 _adg_cell_invalidate(table_cell);
367 * adg_table_cell_set_text_title:
368 * @table_cell: a valid #AdgTableCell
369 * @title: a text string
371 * Convenient function to set a the title of a cell using an #AdgToyText
372 * entity with the font dress picked from #AdgTable:table-dress with
373 * a call to adg_table_style_get_title_dress().
375 * Since: 1.0
377 void
378 adg_table_cell_set_text_title(AdgTableCell *table_cell, const gchar *title)
380 AdgTable *table;
381 AdgEntity *entity;
382 AdgTableStyle *table_style;
383 const CpmlPair *padding;
384 AdgDress table_dress, font_dress;
385 cairo_matrix_t map;
387 g_return_if_fail(table_cell != NULL);
389 if (title == NULL)
390 adg_table_cell_set_title(table_cell, NULL);
392 if (table_cell->title) {
393 gchar *old_title;
394 gboolean unchanged;
396 if (ADG_IS_TEXTUAL(table_cell->title))
397 old_title = adg_textual_dup_text((AdgTextual *) table_cell->title);
398 else
399 old_title = NULL;
401 unchanged = g_strcmp0(title, old_title) == 0;
402 g_free(old_title);
404 if (unchanged)
405 return;
408 table = adg_table_cell_get_table(table_cell);
409 table_dress = adg_table_get_table_dress(table);
410 table_style = (AdgTableStyle *) adg_entity_style((AdgEntity *) table,
411 table_dress);
412 padding = adg_table_style_get_cell_padding(table_style);
413 font_dress = adg_table_style_get_title_dress(table_style);
414 entity = g_object_new(ADG_TYPE_BEST_TEXT, "text", title,
415 "font-dress", font_dress, NULL);
417 cairo_matrix_init_translate(&map, padding->x, padding->y);
418 adg_entity_set_global_map(entity, &map);
420 adg_table_cell_set_title(table_cell, entity);
424 * adg_table_cell_title:
425 * @table_cell: a valid #AdgTableCell
427 * Gets the current title of @table_cell. The returned string is owned
428 * by @table_cell and must not be modified or freed.
430 * Returns: (transfer none): the title entity or <constant>NULL</constant> for undefined title.
432 * Since: 1.0
434 AdgEntity *
435 adg_table_cell_title(AdgTableCell *table_cell)
437 g_return_val_if_fail(table_cell != NULL, NULL);
439 return table_cell->title;
443 * adg_table_cell_set_value:
444 * @table_cell: a valid #AdgTableCell
445 * @value: the new value entity
447 * Sets @value as the new value entity of @table_cell. The bottom middle
448 * point of the bounding box of @value will be cohincident to the
449 * bottom middle point of the cell extents, taking into accounts
450 * eventual padding spaces specified by the table style.
452 * The old internal entity is unrefenrenced while the @value (if
453 * not <constant>NULL</constant>) is referenced with
454 * g_object_ref_sink().
456 * @value can be <constant>NULL</constant>, in which case the old entity is removed.
458 * Since: 1.0
460 void
461 adg_table_cell_set_value(AdgTableCell *table_cell, AdgEntity *value)
463 g_return_if_fail(table_cell != NULL);
464 g_return_if_fail(value == NULL || ADG_IS_ENTITY(value));
466 if (_adg_cell_set_value(table_cell, value))
467 _adg_cell_invalidate(table_cell);
471 * adg_table_cell_set_text_value:
472 * @table_cell: a valid #AdgTableCell
473 * @value: a text string
475 * Convenient function to set a the value of a cell using an #AdgToyText
476 * entity with a value font dress picked from #AdgTable:table-dress with
477 * a call to adg_table_style_get_value_dress().
479 * Since: 1.0
481 void
482 adg_table_cell_set_text_value(AdgTableCell *table_cell, const gchar *value)
484 AdgTable *table;
485 AdgEntity *entity;
486 AdgTableStyle *table_style;
487 const CpmlPair *padding;
488 AdgDress table_dress, font_dress;
489 cairo_matrix_t map;
491 g_return_if_fail(table_cell != NULL);
493 if (value == NULL)
494 adg_table_cell_set_value(table_cell, NULL);
496 if (table_cell->value) {
497 gchar *old_value;
498 gboolean unchanged;
500 if (ADG_IS_TEXTUAL(table_cell->value))
501 old_value = adg_textual_dup_text((AdgTextual *) table_cell->value);
502 else
503 old_value = NULL;
505 unchanged = g_strcmp0(value, old_value) == 0;
506 g_free(old_value);
507 if (unchanged)
508 return;
511 table = adg_table_cell_get_table(table_cell);
512 table_dress = adg_table_get_table_dress(table);
513 table_style = (AdgTableStyle *) adg_entity_style((AdgEntity *) table,
514 table_dress);
515 padding = adg_table_style_get_cell_padding(table_style);
516 font_dress = adg_table_style_get_value_dress(table_style);
517 entity = g_object_new(ADG_TYPE_BEST_TEXT, "text", value,
518 "font-dress", font_dress, NULL);
520 cairo_matrix_init_translate(&map, 0, -padding->y);
521 adg_entity_set_global_map(entity, &map);
523 adg_table_cell_set_value(table_cell, entity);
527 * adg_table_cell_value:
528 * @table_cell: a valid #AdgTableCell
530 * Gets the current value of @table_cell. The returned string is owned
531 * by @table_cell and must not be modified or freed.
533 * Returns: (transfer none): the value entity or <constant>NULL</constant> for undefined value.
535 * Since: 1.0
537 AdgEntity *
538 adg_table_cell_value(AdgTableCell *table_cell)
540 g_return_val_if_fail(table_cell != NULL, NULL);
542 return table_cell->value;
546 * adg_table_cell_set_value_pos:
547 * @table_cell: a valid #AdgTableCell
548 * @from_factor: the alignment factor on the value entity
549 * @to_factor: the alignment factor on the cell
551 * Sets a new custom position for the value entity of @table_cell. The
552 * @from_factor specifies the source point (as a fraction of the
553 * value extents) while the @to_factor is the destination point
554 * (specified as a fraction of the cell extents) the source point
555 * must be moved to.
557 * Since: 1.0
559 void
560 adg_table_cell_set_value_pos(AdgTableCell *table_cell,
561 const CpmlPair *from_factor,
562 const CpmlPair *to_factor)
564 g_return_if_fail(table_cell != NULL);
565 _adg_cell_set_value_pos(table_cell, from_factor, to_factor);
569 * adg_table_cell_set_value_pos_explicit:
570 * @table_cell: a valid #AdgTableCell
571 * @from_x: the x alignment factor on the entity
572 * @from_y: the y alignment factor on the entity
573 * @to_x: the x alignment factor on the cell
574 * @to_y: the y alignment factor on the cell
576 * A convenient wrapper around adg_table_cell_set_value_pos()
577 * that uses explicit factors instead of #CpmlPair.
579 * Since: 1.0
581 void
582 adg_table_cell_set_value_pos_explicit(AdgTableCell *table_cell,
583 gdouble from_x, gdouble from_y,
584 gdouble to_x, gdouble to_y)
586 CpmlPair from, to;
588 from.x = from_x;
589 from.y = from_y;
590 to.x = to_x;
591 to.y = to_y;
593 adg_table_cell_set_value_pos(table_cell, &from, &to);
597 * adg_table_cell_set_width:
598 * @table_cell: a valid #AdgTableCell
599 * @width: the new width
601 * Sets a new width on @table_cell. The extents on the whole table
602 * will be invalidated, so will be recomputed in the next
603 * arrange() phase.
605 * A positive @width value specifies the width of this cell in global
606 * space: if the width of its content (that is, either the title or the
607 * value entity) will be greater than @width, it will be rendered
608 * outside the cell boundary box, luckely overwriting the adiacent
609 * cells.
611 * Using 0 as @width means the width of the cell will be automatically
612 * adjusted to the maximum width of its content.
614 * Negative width values are not allowed: this condition will raise
615 * a warning without any further processing.
617 * Since: 1.0
619 void
620 adg_table_cell_set_width(AdgTableCell *table_cell, gdouble width)
622 g_return_if_fail(table_cell != NULL);
623 g_return_if_fail(width >= 0);
625 if (table_cell->width != width) {
626 table_cell->width = width;
627 _adg_cell_invalidate(table_cell);
632 * adg_table_cell_get_width:
633 * @table_cell: a valid #AdgTableCell
635 * Gets the width of @table_cell.
637 * Returns: the requested width or 0 on errors.
639 * Since: 1.0
641 gdouble
642 adg_table_cell_get_width(AdgTableCell *table_cell)
644 g_return_val_if_fail(table_cell != NULL, 0.);
646 return table_cell->width;
650 * adg_table_cell_switch_frame:
651 * @table_cell: a valid #AdgTableCell
652 * @has_frame: whether to draw or not the frame
654 * Sets the frame flag of @table_cell: if @has_frame is
655 * <constant>TRUE</constant>, a frame around @table_cell
656 * will be rendered using the #AdgTableStyle:frame-dress
657 * dress of the table style.
659 * Since: 1.0
661 void
662 adg_table_cell_switch_frame(AdgTableCell *table_cell, gboolean has_frame)
664 g_return_if_fail(table_cell != NULL);
666 if (table_cell->has_frame != has_frame) {
667 AdgTable *table = adg_table_cell_get_table(table_cell);
668 table_cell->has_frame = has_frame;
669 adg_table_invalidate_grid(table);
674 * adg_table_cell_has_frame:
675 * @table_cell: a valid #AdgTableCell
677 * Gets the frame flag of @table_cell.
679 * Returns: the frame flag.
681 * Since: 1.0
683 gboolean
684 adg_table_cell_has_frame(AdgTableCell *table_cell)
686 g_return_val_if_fail(table_cell != NULL, FALSE);
688 return table_cell->has_frame;
692 * adg_table_cell_get_extents:
693 * @table_cell: a valid #AdgTableCell
695 * Gets the extents of @table_cell. This function is useful only after the
696 * arrange() phase as in the other situation the extents will likely
697 * be not up to date.
699 * Returns: the extents of @table_cell or <constant>NULL</constant> on errors.
701 * Since: 1.0
703 const CpmlExtents *
704 adg_table_cell_get_extents(AdgTableCell *table_cell)
706 g_return_val_if_fail(table_cell != NULL, NULL);
708 return &table_cell->extents;
712 * adg_table_cell_size_request:
713 * @table_cell: a valid #AdgTableCell
714 * @row_extents: the extents of the container #AdgTableRow
716 * Computes the minimum space needed to properly render @table_cell
717 * and updates the size component of the internal #CpmlExtents struct,
718 * returning it to the caller. The returned #CpmlPair is owned by
719 * @table_cell and should not be modified or freed.
721 * Returns: (transfer none): the minimum size required.
723 * Since: 1.0
725 const CpmlPair *
726 adg_table_cell_size_request(AdgTableCell *table_cell,
727 const CpmlExtents *row_extents)
729 CpmlVector *size;
730 AdgAlignment *title_alignment;
731 AdgAlignment *value_alignment;
732 AdgTable *table;
734 size = &table_cell->extents.size;
736 if (table_cell->title) {
737 title_alignment = (AdgAlignment *) adg_entity_get_parent(table_cell->title);
738 adg_entity_arrange((AdgEntity *) title_alignment);
739 } else {
740 title_alignment = NULL;
743 if (table_cell->value) {
744 value_alignment = (AdgAlignment *) adg_entity_get_parent(table_cell->value);
745 adg_entity_arrange((AdgEntity *) value_alignment);
746 } else {
747 value_alignment = NULL;
750 table = adg_table_cell_get_table(table_cell);
751 size->y = row_extents->size.y;
753 if (table_cell->width == 0) {
754 AdgTableStyle *table_style = (AdgTableStyle *) adg_table_get_table_style(table);
755 const CpmlExtents *extents;
757 /* The width depends on the cell content (default = 0) */
758 size->x = 0;
760 if (title_alignment) {
761 extents = adg_entity_get_extents((AdgEntity *) title_alignment);
762 size->x = extents->size.x;
765 if (value_alignment) {
766 extents = adg_entity_get_extents((AdgEntity *) value_alignment);
767 if (extents->size.x > size->x)
768 size->x = extents->size.x;
771 size->x += adg_table_style_get_cell_spacing(table_style)->x * 2;
772 } else {
773 size->x = table_cell->width;
776 return size;
780 * adg_table_cell_arrange:
781 * @table_cell: an #AdgTableCell
782 * @layout: the new extents to use
784 * Rearranges the underlying #AdgTableCell owned by @table_cell using
785 * the new extents provided in @layout. If the x or y size component
786 * of @layout is negative, the value holded by the internal extents
787 * struct is not overriden.
789 * <note><para>
790 * table_cell->extents must be up to date if @layout->size.x or
791 * @layout->size.y is negative in order to have a valid size.
792 * </para></note>
794 * Returns: the extents of @table_cell or <constant>NULL</constant> on errors.
796 * Since: 1.0
798 const CpmlExtents *
799 adg_table_cell_arrange(AdgTableCell *table_cell, const CpmlExtents *layout)
801 CpmlExtents *extents;
802 AdgAlignment *alignment;
803 cairo_matrix_t map;
805 /* Set the new extents */
806 extents = &table_cell->extents;
807 extents->org = layout->org;
808 if (layout->size.x > 0)
809 extents->size.x = layout->size.x;
810 if (layout->size.y > 0)
811 extents->size.y = layout->size.y;
812 extents->is_defined = TRUE;
814 if (table_cell->title) {
815 alignment = (AdgAlignment *) adg_entity_get_parent(table_cell->title);
817 cairo_matrix_init_translate(&map, extents->org.x, extents->org.y);
818 adg_entity_set_global_map((AdgEntity *) alignment, &map);
821 if (table_cell->value) {
822 CpmlPair to;
824 alignment = (AdgAlignment *) adg_entity_get_parent(table_cell->value);
825 to.x = extents->size.x * table_cell->value_factor.x + extents->org.x;
826 to.y = extents->size.y * table_cell->value_factor.y + extents->org.y;
828 cairo_matrix_init_translate(&map, to.x, to.y);
829 adg_entity_set_global_map((AdgEntity *) alignment, &map);
832 return extents;
836 static AdgTableCell *
837 _adg_cell_new(void)
839 AdgTableCell *table_cell;
841 table_cell = g_new(AdgTableCell, 1);
842 table_cell->row = NULL;
843 table_cell->width = 0.;
844 table_cell->has_frame = FALSE;
845 table_cell->title = NULL;
846 table_cell->value = NULL;
847 table_cell->extents.is_defined = FALSE;
848 table_cell->value_factor.x = 0.5;
849 table_cell->value_factor.y = 1;
851 return table_cell;
854 static void
855 _adg_cell_invalidate(AdgTableCell *table_cell)
857 AdgTable *table = adg_table_cell_get_table(table_cell);
859 if (table)
860 adg_entity_invalidate((AdgEntity *) table);
863 static gboolean
864 _adg_cell_set_title(AdgTableCell *table_cell, AdgEntity *title)
866 AdgEntity *alignment;
868 if (table_cell->title == title)
869 return FALSE;
871 if (table_cell->title) {
872 alignment = adg_entity_get_parent(table_cell->title);
873 g_object_unref(alignment);
876 table_cell->title = title;
878 if (title) {
879 AdgEntity *table = (AdgEntity *) adg_table_cell_get_table(table_cell);
880 alignment = (AdgEntity *) adg_alignment_new_explicit(0, -1);
881 g_object_ref_sink(alignment);
882 adg_entity_set_parent(alignment, table);
883 adg_container_add((AdgContainer *) alignment, title);
886 return TRUE;
889 static gboolean
890 _adg_cell_set_value(AdgTableCell *table_cell, AdgEntity *value)
892 AdgEntity *alignment;
894 if (table_cell->value == value)
895 return FALSE;
897 if (value) {
898 AdgEntity *table = (AdgEntity *) adg_table_cell_get_table(table_cell);
899 alignment = (AdgEntity *) adg_alignment_new_explicit(0.5, 0);
900 g_object_ref_sink(alignment);
901 adg_entity_set_parent(alignment, table);
902 adg_container_add((AdgContainer *) alignment, value);
905 if (table_cell->value) {
906 alignment = adg_entity_get_parent(table_cell->value);
907 adg_container_remove((AdgContainer *) alignment, table_cell->value);
908 g_object_unref(alignment);
911 table_cell->value = value;
912 return TRUE;
915 static void
916 _adg_cell_set_value_pos(AdgTableCell *table_cell,
917 const CpmlPair *from_factor, const CpmlPair *to_factor)
919 AdgAlignment *alignment;
921 if (table_cell->value == NULL)
922 return;
924 alignment = (AdgAlignment *) adg_entity_get_parent(table_cell->value);
926 if (from_factor)
927 adg_alignment_set_factor(alignment, from_factor);
929 if (to_factor)
930 table_cell->value_factor = *to_factor;