1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012 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.
23 * @short_description: The drawing container
25 * The canvas is the toplevel entity of an ADG drawing. It can be
26 * bound to a GTK+ widget, such as #AdgGtkArea, or manually rendered
27 * to a custom surface.
29 * Tipically, the canvas contains the description and properties of
30 * the media used, such as such as size (if relevant), margins,
31 * border and paddings. This approach clearly follows the block model
32 * of the CSS specifications level 2.
34 * The paddings specify the distance between the entities contained
35 * by the canvas and the border. The margins specify the distance
36 * between the canvas border and the media extents.
38 * The canvas (hence the media) size can be explicitely specified
39 * by directly writing to the #AdgCanvas:size property or using any
40 * valid setter, such as adg_canvas_set_size(),
41 * adg_canvas_set_size_explicit() or the convenient
42 * adg_canvas_set_paper() GTK+ wrapper. You can also set explicitely
43 * only one dimension and let the other one be computed automatically.
44 * This is done by using the special value %0 that specifies a side
45 * must be autocalculated.
47 * By default either width and height must be autocalculated (are
48 * set to %0), so the arrange() phase on the canvas is performed.
49 * Margins and paddings are then added to the extents to get the
50 * border coordinates and the final bounding box.
52 * When the size is explicitely set, instead, the final bounding
53 * box is forcibly set to this value without taking the canvas
54 * extents into account. The margins are then subtracted to get
55 * the coordinates of the border. In this case, the paddings are
64 * All fields are private and should not be used directly.
65 * Use its public methods instead.
71 #include "adg-internal.h"
72 #if GTK3_ENABLED || GTK2_ENABLED
76 #include "adg-container.h"
77 #include "adg-table.h"
78 #include "adg-title-block.h"
79 #include "adg-style.h"
80 #include "adg-color-style.h"
81 #include "adg-dress.h"
82 #include "adg-dress-builtins.h"
84 #include "adg-canvas.h"
85 #include "adg-canvas-private.h"
88 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_canvas_parent_class)
89 #define _ADG_OLD_ENTITY_CLASS ((AdgEntityClass *) adg_canvas_parent_class)
92 G_DEFINE_TYPE(AdgCanvas
, adg_canvas
, ADG_TYPE_CONTAINER
)
98 PROP_BACKGROUND_DRESS
,
113 static void _adg_dispose (GObject
*object
);
114 static void _adg_get_property (GObject
*object
,
118 static void _adg_set_property (GObject
*object
,
122 static void _adg_global_changed (AdgEntity
*entity
);
123 static void _adg_local_changed (AdgEntity
*entity
);
124 static void _adg_invalidate (AdgEntity
*entity
);
125 static void _adg_arrange (AdgEntity
*entity
);
126 static void _adg_render (AdgEntity
*entity
,
131 adg_canvas_class_init(AdgCanvasClass
*klass
)
133 GObjectClass
*gobject_class
;
134 AdgEntityClass
*entity_class
;
137 gobject_class
= (GObjectClass
*) klass
;
138 entity_class
= (AdgEntityClass
*) klass
;
140 g_type_class_add_private(klass
, sizeof(AdgCanvasPrivate
));
142 gobject_class
->dispose
= _adg_dispose
;
143 gobject_class
->get_property
= _adg_get_property
;
144 gobject_class
->set_property
= _adg_set_property
;
146 entity_class
->global_changed
= _adg_global_changed
;
147 entity_class
->local_changed
= _adg_local_changed
;
148 entity_class
->invalidate
= _adg_invalidate
;
149 entity_class
->arrange
= _adg_arrange
;
150 entity_class
->render
= _adg_render
;
152 param
= g_param_spec_boxed("size",
154 P_("The size set on this canvas: use 0 to have an automatic dimension based on the canvas extents"),
157 g_object_class_install_property(gobject_class
, PROP_SIZE
, param
);
159 param
= g_param_spec_boxed("scales",
161 P_("List of scales to be tested while autoscaling the drawing"),
164 g_object_class_install_property(gobject_class
, PROP_SCALES
, param
);
166 param
= adg_param_spec_dress("background-dress",
167 P_("Background Dress"),
168 P_("The color dress to use for the canvas background"),
169 ADG_DRESS_COLOR_BACKGROUND
,
171 g_object_class_install_property(gobject_class
, PROP_BACKGROUND_DRESS
, param
);
173 param
= adg_param_spec_dress("frame-dress",
175 P_("Line dress to use while drawing the frame around the canvas"),
176 ADG_DRESS_LINE_FRAME
,
178 g_object_class_install_property(gobject_class
, PROP_FRAME_DRESS
, param
);
180 param
= g_param_spec_object("title-block",
182 P_("The title block to assign to this canvas"),
183 ADG_TYPE_TITLE_BLOCK
,
185 g_object_class_install_property(gobject_class
, PROP_TITLE_BLOCK
, param
);
187 param
= g_param_spec_double("top-margin",
189 P_("The margin (in global space) to leave above the frame"),
190 -G_MAXDOUBLE
, G_MAXDOUBLE
, 15,
192 g_object_class_install_property(gobject_class
, PROP_TOP_MARGIN
, param
);
194 param
= g_param_spec_double("right-margin",
196 P_("The margin (in global space) to leave empty at the right of the frame"),
197 -G_MAXDOUBLE
, G_MAXDOUBLE
, 15,
199 g_object_class_install_property(gobject_class
, PROP_RIGHT_MARGIN
, param
);
201 param
= g_param_spec_double("bottom-margin",
203 P_("The margin (in global space) to leave empty below the frame"),
204 -G_MAXDOUBLE
, G_MAXDOUBLE
, 15,
206 g_object_class_install_property(gobject_class
, PROP_BOTTOM_MARGIN
, param
);
208 param
= g_param_spec_double("left-margin",
210 P_("The margin (in global space) to leave empty at the left of the frame"),
211 -G_MAXDOUBLE
, G_MAXDOUBLE
, 15,
213 g_object_class_install_property(gobject_class
, PROP_LEFT_MARGIN
, param
);
215 param
= g_param_spec_boolean("has-frame",
216 P_("Has Frame Flag"),
217 P_("If enabled, a frame using the frame dress will be drawn around the canvas extents, taking into account the margins"),
220 g_object_class_install_property(gobject_class
, PROP_HAS_FRAME
, param
);
222 param
= g_param_spec_double("top-padding",
224 P_("The padding (in global space) to leave empty above between the drawing and the frame"),
225 -G_MAXDOUBLE
, G_MAXDOUBLE
, 15,
227 g_object_class_install_property(gobject_class
, PROP_TOP_PADDING
, param
);
229 param
= g_param_spec_double("right-padding",
231 P_("The padding (in global space) to leave empty at the right between the drawing and the frame"),
232 -G_MAXDOUBLE
, G_MAXDOUBLE
, 15,
234 g_object_class_install_property(gobject_class
, PROP_RIGHT_PADDING
, param
);
236 param
= g_param_spec_double("bottom-padding",
237 P_("Bottom Padding"),
238 P_("The padding (in global space) to leave empty below between the drawing and the frame"),
239 -G_MAXDOUBLE
, G_MAXDOUBLE
, 15,
241 g_object_class_install_property(gobject_class
, PROP_BOTTOM_PADDING
, param
);
243 param
= g_param_spec_double("left-padding",
245 P_("The padding (in global space) to leave empty at the left between the drawing and the frame"),
246 -G_MAXDOUBLE
, G_MAXDOUBLE
, 15,
248 g_object_class_install_property(gobject_class
, PROP_LEFT_PADDING
, param
);
252 adg_canvas_init(AdgCanvas
*canvas
)
254 AdgCanvasPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(canvas
,
261 data
->background_dress
= ADG_DRESS_COLOR_BACKGROUND
;
262 data
->frame_dress
= ADG_DRESS_LINE_FRAME
;
263 data
->title_block
= NULL
;
264 data
->top_margin
= 15;
265 data
->right_margin
= 15;
266 data
->bottom_margin
= 15;
267 data
->left_margin
= 15;
268 data
->has_frame
= TRUE
;
269 data
->top_padding
= 15;
270 data
->right_padding
= 15;
271 data
->bottom_padding
= 15;
272 data
->left_padding
= 15;
278 _adg_dispose(GObject
*object
)
281 AdgCanvasPrivate
*data
;
283 canvas
= (AdgCanvas
*) object
;
286 if (data
->title_block
) {
287 g_object_unref(data
->title_block
);
288 data
->title_block
= NULL
;
291 if (data
->scales
!= NULL
) {
292 g_strfreev(data
->scales
);
296 if (_ADG_OLD_OBJECT_CLASS
->dispose
)
297 _ADG_OLD_OBJECT_CLASS
->dispose(object
);
302 _adg_get_property(GObject
*object
, guint prop_id
,
303 GValue
*value
, GParamSpec
*pspec
)
305 AdgCanvasPrivate
*data
= ((AdgCanvas
*) object
)->data
;
309 g_value_set_boxed(value
, &data
->size
);
312 g_value_set_boxed(value
, data
->scales
);
314 case PROP_BACKGROUND_DRESS
:
315 g_value_set_int(value
, data
->background_dress
);
317 case PROP_FRAME_DRESS
:
318 g_value_set_int(value
, data
->frame_dress
);
320 case PROP_TITLE_BLOCK
:
321 g_value_set_object(value
, data
->title_block
);
323 case PROP_TOP_MARGIN
:
324 g_value_set_double(value
, data
->top_margin
);
326 case PROP_RIGHT_MARGIN
:
327 g_value_set_double(value
, data
->right_margin
);
329 case PROP_BOTTOM_MARGIN
:
330 g_value_set_double(value
, data
->bottom_margin
);
332 case PROP_LEFT_MARGIN
:
333 g_value_set_double(value
, data
->left_margin
);
336 g_value_set_boolean(value
, data
->has_frame
);
338 case PROP_TOP_PADDING
:
339 g_value_set_double(value
, data
->top_padding
);
341 case PROP_RIGHT_PADDING
:
342 g_value_set_double(value
, data
->right_padding
);
344 case PROP_BOTTOM_PADDING
:
345 g_value_set_double(value
, data
->bottom_padding
);
347 case PROP_LEFT_PADDING
:
348 g_value_set_double(value
, data
->left_padding
);
351 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
357 _adg_set_property(GObject
*object
, guint prop_id
,
358 const GValue
*value
, GParamSpec
*pspec
)
361 AdgCanvasPrivate
*data
;
362 AdgTitleBlock
*title_block
;
364 canvas
= (AdgCanvas
*) object
;
369 cpml_pair_copy(&data
->size
, g_value_get_boxed(value
));
372 g_strfreev(data
->scales
);
373 data
->scales
= g_value_dup_boxed(value
);
375 case PROP_BACKGROUND_DRESS
:
376 data
->background_dress
= g_value_get_int(value
);
378 case PROP_FRAME_DRESS
:
379 data
->frame_dress
= g_value_get_int(value
);
381 case PROP_TITLE_BLOCK
:
382 title_block
= g_value_get_object(value
);
384 g_object_ref(title_block
);
385 adg_entity_set_parent((AdgEntity
*) title_block
,
386 (AdgEntity
*) canvas
);
388 if (data
->title_block
)
389 g_object_unref(data
->title_block
);
390 data
->title_block
= title_block
;
392 case PROP_TOP_MARGIN
:
393 data
->top_margin
= g_value_get_double(value
);
395 case PROP_RIGHT_MARGIN
:
396 data
->right_margin
= g_value_get_double(value
);
398 case PROP_BOTTOM_MARGIN
:
399 data
->bottom_margin
= g_value_get_double(value
);
401 case PROP_LEFT_MARGIN
:
402 data
->left_margin
= g_value_get_double(value
);
405 data
->has_frame
= g_value_get_boolean(value
);
407 case PROP_TOP_PADDING
:
408 data
->top_padding
= g_value_get_double(value
);
410 case PROP_RIGHT_PADDING
:
411 data
->right_padding
= g_value_get_double(value
);
413 case PROP_BOTTOM_PADDING
:
414 data
->bottom_padding
= g_value_get_double(value
);
416 case PROP_LEFT_PADDING
:
417 data
->left_padding
= g_value_get_double(value
);
420 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
429 * Creates a new empty canvas object.
431 * Returns: (transfer full): the newly created canvas.
438 /* Some common ISO scales for presetting AdgCanvas:scales property */
439 const gchar
*scales
[] = {
440 "10:1", "5:1", "3:1", "2:1", "1:1", "1:2", "1:3", "1:5", "1:10",
444 return g_object_new(ADG_TYPE_CANVAS
, "scales", scales
, NULL
);
448 * adg_canvas_set_size:
449 * @canvas: an #AdgCanvas
450 * @size: the new size for the canvas
452 * Sets a specific size on @canvas. The x and/or y
453 * components of the returned #CpmlPair could be %0, in which
454 * case the size returned by adg_entity_get_extents() on
455 * @canvas will be used instead.
460 adg_canvas_set_size(AdgCanvas
*canvas
, const CpmlPair
*size
)
462 g_return_if_fail(ADG_IS_CANVAS(canvas
));
463 g_return_if_fail(size
!= NULL
);
465 g_object_set(canvas
, "size", size
, NULL
);
469 * adg_canvas_set_size_explicit:
470 * @canvas: an #AdgCanvas
471 * @x: the new width of the canvas or %0 to reset
472 * @y: the new height of the canvas or %0 to reset
474 * A convenient function to set the size of @canvas using
475 * explicit coordinates. Check adg_canvas_set_size() for
481 adg_canvas_set_size_explicit(AdgCanvas
*canvas
, gdouble x
, gdouble y
)
488 adg_canvas_set_size(canvas
, &size
);
492 * adg_canvas_get_size:
493 * @canvas: an #AdgCanvas
495 * Gets the specific size set on @canvas. The x and/or y
496 * components of the returned #CpmlPair could be %0, in which
497 * case the size returned by adg_entity_get_extents() on
498 * @canvas will be used instead.
500 * Returns: the explicit size set on this canvas or %NULL on errors.
505 adg_canvas_get_size(AdgCanvas
*canvas
)
507 AdgCanvasPrivate
*data
;
509 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), NULL
);
516 * adg_canvas_set_scales:
517 * @canvas: an #AdgCanvas
518 * @...: %NULL terminated list of strings
520 * Sets the scales allowed by @canvas. Every scale identifies a
521 * specific factor to be applied to the local matrix of @canvas.
522 * When adg_canvas_autoscale() will be called, the greatest
523 * scale that can render every entity inside a box of
524 * #AdgCanvas:size dimensions will be applied. The drawing will
525 * be centered inside that box.
527 * Every scale should be expressed with a string in the form of
528 * "x:y", where x and y are positive integers that identifies
529 * numerator an denominator of a fraction. That string itself
530 * will be put into the title block when used.
535 adg_canvas_set_scales(AdgCanvas
*canvas
, ...)
539 va_start(var_args
, canvas
);
540 adg_canvas_set_scales_valist(canvas
, var_args
);
545 * adg_canvas_set_scales_valist:
546 * @canvas: an #AdgCanvas
547 * @var_args: %NULL terminated list of strings
549 * Vararg variant of adg_canvas_set_scales().
554 adg_canvas_set_scales_valist(AdgCanvas
*canvas
, va_list var_args
)
560 g_return_if_fail(ADG_IS_CANVAS(canvas
));
564 while ((scale
= va_arg(var_args
, const gchar
*)) != NULL
) {
566 scales
= g_realloc(scales
, sizeof(const gchar
*) * (n
+ 1));
567 scales
[n
-1] = g_strdup(scale
);
571 g_object_set(canvas
, "scales", scales
, NULL
);
576 * adg_canvas_set_scales_array:
577 * @canvas: an #AdgCanvas
578 * @scales: (in) (array zero-terminated=1) (allow-none): %NULL terminated array of scales
580 * Array variant of adg_canvas_set_scales().
582 * Rename to: adg_canvas_set_scales
586 adg_canvas_set_scales_array(AdgCanvas
*canvas
, gchar
**scales
)
588 g_return_if_fail(ADG_IS_CANVAS(canvas
));
589 g_object_set(canvas
, "scales", scales
, NULL
);
593 * adg_canvas_get_scales:
594 * @canvas: an #AdgCanvas
596 * Gets the list of scales set on @canvas: check adg_canvas_set_scales()
597 * to get an idea of what scales are supposed to be.
599 * If no scales are set, %NULL is returned.
601 * Returns: (element-type utf8) (transfer none): the %NULL terminated list of valid scales.
606 adg_canvas_get_scales(AdgCanvas
*canvas
)
608 AdgCanvasPrivate
*data
;
610 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), NULL
);
617 * adg_canvas_autoscale:
618 * @canvas: an #AdgCanvas
620 * Applies one scale per time, in the order they have been provided
621 * in the adg_canvas_set_scales() call, until the drawing can be
622 * entirely contained into the current paper. If successful, the
623 * scale of the title block is changed accordingly and the drawing
624 * is centered inside the paper.
629 adg_canvas_autoscale(AdgCanvas
*canvas
)
631 AdgCanvasPrivate
*data
;
635 const CpmlExtents
*extents
;
636 AdgTitleBlock
*title_block
;
639 g_return_if_fail(ADG_IS_CANVAS(canvas
));
640 g_return_if_fail(_ADG_OLD_ENTITY_CLASS
->arrange
!= NULL
);
643 entity
= (AdgEntity
*) canvas
;
644 title_block
= adg_canvas_get_title_block(canvas
);
646 for (p_scale
= data
->scales
; *p_scale
!= NULL
; ++p_scale
) {
647 const gchar
*scale
= *p_scale
;
648 gdouble factor
= adg_scale_factor(scale
);
652 cairo_matrix_init_scale(&map
, factor
, factor
);
653 adg_entity_set_local_map(entity
, &map
);
654 adg_entity_local_changed(entity
);
656 /* Arrange the entities inside the canvas, but not the canvas itself,
657 * just to get the bounding box of the drawing without the paper */
658 _ADG_OLD_ENTITY_CLASS
->arrange(entity
);
659 extents
= adg_entity_get_extents(entity
);
661 /* Just in case @canvas is emtpy */
662 if (! extents
->is_defined
)
665 if (title_block
!= NULL
)
666 adg_title_block_set_scale(title_block
, scale
);
668 /* Bail out if paper size is not specified or invalid */
669 if (data
->size
.x
<= 0 || data
->size
.y
<= 0)
672 /* If the drawing extents are fully contained inside the paper size,
673 * center the drawing in the paper and bail out */
674 delta
.x
= data
->size
.x
- extents
->size
.x
;
675 delta
.y
= data
->size
.y
- extents
->size
.y
;
676 if (delta
.x
>= 0 && delta
.y
>= 0) {
678 cairo_matrix_init_translate(&transform
,
679 delta
.x
/ 2 - extents
->org
.x
,
680 delta
.y
/ 2 - extents
->org
.y
);
681 adg_entity_transform_local_map(entity
, &transform
,
682 ADG_TRANSFORM_AFTER
);
689 * adg_canvas_set_background_dress:
690 * @canvas: an #AdgCanvas
691 * @dress: the new #AdgDress to use
693 * Sets a new background dress for rendering @canvas: the new
694 * dress must be a color dress.
699 adg_canvas_set_background_dress(AdgCanvas
*canvas
, AdgDress dress
)
701 g_return_if_fail(ADG_IS_CANVAS(canvas
));
702 g_object_set(canvas
, "background-dress", dress
, NULL
);
706 * adg_canvas_get_background_dress:
707 * @canvas: an #AdgCanvas
709 * Gets the background dress to be used in rendering @canvas.
711 * Returns: (transfer none): the current background dress.
716 adg_canvas_get_background_dress(AdgCanvas
*canvas
)
718 AdgCanvasPrivate
*data
;
720 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), ADG_DRESS_UNDEFINED
);
724 return data
->background_dress
;
728 * adg_canvas_set_frame_dress:
729 * @canvas: an #AdgCanvas
730 * @dress: the new #AdgDress to use
732 * Sets the #AdgCanvas:frame-dress property of @canvas to @dress:
733 * the new dress must be a line dress.
738 adg_canvas_set_frame_dress(AdgCanvas
*canvas
, AdgDress dress
)
740 g_return_if_fail(ADG_IS_CANVAS(canvas
));
741 g_object_set(canvas
, "frame-dress", dress
, NULL
);
745 * adg_canvas_get_frame_dress:
746 * @canvas: an #AdgCanvas
748 * Gets the frame dress to be used in rendering the border of @canvas.
750 * Returns: (transfer none): the current frame dress.
755 adg_canvas_get_frame_dress(AdgCanvas
*canvas
)
757 AdgCanvasPrivate
*data
;
759 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), ADG_DRESS_UNDEFINED
);
762 return data
->frame_dress
;
766 * adg_canvas_set_title_block:
767 * @canvas: an #AdgCanvas
768 * @title_block: a title block
770 * Sets the #AdgCanvas:title-block property of @canvas to @title_block.
772 * Although a title block entity could be added to @canvas in the usual
773 * way, that is using the adg_container_add() method, assigning a title
774 * block with adg_canvas_set_title_block() is somewhat different:
776 * - @title_block will be automatically attached to the bottom right
777 * corner of to the @canvas frame (this could be accomplished in the
778 * usual way too, by resetting the right and bottom paddings);
779 * - the @title_block boundary box is not taken into account while
780 * computing the extents of @canvas.
785 adg_canvas_set_title_block(AdgCanvas
*canvas
, AdgTitleBlock
*title_block
)
787 g_return_if_fail(ADG_IS_CANVAS(canvas
));
788 g_return_if_fail(title_block
== NULL
|| ADG_IS_TITLE_BLOCK(title_block
));
789 g_object_set(canvas
, "title-block", title_block
, NULL
);
793 * adg_canvas_get_title_block:
794 * @canvas: an #AdgCanvas
796 * Gets the #AdgTitleBlock object of @canvas: check
797 * adg_canvas_set_title_block() for details.
799 * The returned entity is owned by @canvas and should not be
802 * Returns: (transfer none): the title block object or %NULL.
807 adg_canvas_get_title_block(AdgCanvas
*canvas
)
809 AdgCanvasPrivate
*data
;
811 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), NULL
);
814 return data
->title_block
;
818 * adg_canvas_set_top_margin:
819 * @canvas: an #AdgCanvas
820 * @value: the new margin, in global space
822 * Changes the top margin of @canvas by setting #AdgCanvas:top-margin
823 * to @value. Negative values are allowed.
828 adg_canvas_set_top_margin(AdgCanvas
*canvas
, gdouble value
)
830 g_return_if_fail(ADG_IS_CANVAS(canvas
));
831 g_object_set(canvas
, "top-margin", value
, NULL
);
835 * adg_canvas_get_top_margin:
836 * @canvas: an #AdgCanvas
838 * Gets the top margin (in global space) of @canvas.
840 * Returns: the requested margin or %0 on error.
845 adg_canvas_get_top_margin(AdgCanvas
*canvas
)
847 AdgCanvasPrivate
*data
;
849 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), 0.);
852 return data
->top_margin
;
856 * adg_canvas_set_right_margin:
857 * @canvas: an #AdgCanvas
858 * @value: the new margin, in global space
860 * Changes the right margin of @canvas by setting #AdgCanvas:right-margin
861 * to @value. Negative values are allowed.
866 adg_canvas_set_right_margin(AdgCanvas
*canvas
, gdouble value
)
868 g_return_if_fail(ADG_IS_CANVAS(canvas
));
869 g_object_set(canvas
, "right-margin", value
, NULL
);
873 * adg_canvas_get_right_margin:
874 * @canvas: an #AdgCanvas
876 * Gets the right margin (in global space) of @canvas.
878 * Returns: the requested margin or %0 on error.
883 adg_canvas_get_right_margin(AdgCanvas
*canvas
)
885 AdgCanvasPrivate
*data
;
887 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), 0.);
890 return data
->right_margin
;
895 * adg_canvas_set_bottom_margin:
896 * @canvas: an #AdgCanvas
897 * @value: the new margin, in global space
899 * Changes the bottom margin of @canvas by setting #AdgCanvas:bottom-margin
900 * to @value. Negative values are allowed.
905 adg_canvas_set_bottom_margin(AdgCanvas
*canvas
, gdouble value
)
907 g_return_if_fail(ADG_IS_CANVAS(canvas
));
908 g_object_set(canvas
, "bottom-margin", value
, NULL
);
912 * adg_canvas_get_bottom_margin:
913 * @canvas: an #AdgCanvas
915 * Gets the bottom margin (in global space) of @canvas.
917 * Returns: the requested margin or %0 on error.
922 adg_canvas_get_bottom_margin(AdgCanvas
*canvas
)
924 AdgCanvasPrivate
*data
;
926 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), 0.);
929 return data
->bottom_margin
;
933 * adg_canvas_set_left_margin:
934 * @canvas: an #AdgCanvas
935 * @value: the new margin, in global space
937 * Changes the left margin of @canvas by setting #AdgCanvas:left-margin
938 * to @value. Negative values are allowed.
943 adg_canvas_set_left_margin(AdgCanvas
*canvas
, gdouble value
)
945 g_return_if_fail(ADG_IS_CANVAS(canvas
));
946 g_object_set(canvas
, "left-margin", value
, NULL
);
950 * adg_canvas_get_left_margin:
951 * @canvas: an #AdgCanvas
953 * Gets the left margin (in global space) of @canvas.
955 * Returns: the requested margin or %0 on error.
960 adg_canvas_get_left_margin(AdgCanvas
*canvas
)
962 AdgCanvasPrivate
*data
;
964 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), 0.);
967 return data
->left_margin
;
971 * adg_canvas_set_margins:
972 * @canvas: an #AdgCanvas
973 * @top: top margin, in global space
974 * @right: right margin, in global space
975 * @bottom: bottom margin, in global space
976 * @left: left margin, in global space
978 * Convenient function to set all the margins at once.
983 adg_canvas_set_margins(AdgCanvas
*canvas
, gdouble top
, gdouble right
,
984 gdouble bottom
, gdouble left
)
986 g_return_if_fail(ADG_IS_CANVAS(canvas
));
987 g_object_set(canvas
, "top-margin", top
, "right-margin", right
,
988 "bottom-margin", bottom
, "left-margin", left
, NULL
);
992 * adg_canvas_apply_margins:
993 * @canvas: an #AdgCanvas
994 * @extents: where apply the margins
996 * A convenient function to apply the margins of @canvas to the
997 * arbitrary #CpmlExtents struct @extents.
1002 adg_canvas_apply_margins(AdgCanvas
*canvas
, CpmlExtents
*extents
)
1004 AdgCanvasPrivate
*data
;
1006 g_return_if_fail(ADG_IS_CANVAS(canvas
));
1008 data
= canvas
->data
;
1010 extents
->org
.x
-= data
->left_margin
;
1011 extents
->org
.y
-= data
->top_margin
;
1012 extents
->size
.x
+= data
->left_margin
+ data
->right_margin
;
1013 extents
->size
.y
+= data
->top_margin
+ data
->bottom_margin
;
1017 * adg_canvas_switch_frame:
1018 * @canvas: an #AdgCanvas
1019 * @new_state: the new flag status
1021 * Sets a new status on the #AdgCanvas:has-frame property: %TRUE
1022 * means a border around the canvas extents (less the margins)
1023 * should be rendered.
1028 adg_canvas_switch_frame(AdgCanvas
*canvas
, gboolean new_state
)
1030 g_return_if_fail(ADG_IS_CANVAS(canvas
));
1031 g_object_set(canvas
, "has-frame", new_state
, NULL
);
1035 * adg_canvas_has_frame:
1036 * @canvas: an #AdgCanvas
1038 * Gets the current status of the #AdgCanvas:has-frame property,
1039 * that is whether a border around the canvas extents (less the
1040 * margins) should be rendered (%TRUE) or not (%FALSE).
1042 * Returns: the current status of the frame flag.
1047 adg_canvas_has_frame(AdgCanvas
*canvas
)
1049 AdgCanvasPrivate
*data
;
1051 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), FALSE
);
1053 data
= canvas
->data
;
1054 return data
->has_frame
;
1058 * adg_canvas_set_top_padding:
1059 * @canvas: an #AdgCanvas
1060 * @value: the new padding, in global space
1062 * Changes the top padding of @canvas by setting #AdgCanvas:top-padding
1063 * to @value. Negative values are allowed.
1068 adg_canvas_set_top_padding(AdgCanvas
*canvas
, gdouble value
)
1070 g_return_if_fail(ADG_IS_CANVAS(canvas
));
1071 g_object_set(canvas
, "top-padding", value
, NULL
);
1075 * adg_canvas_get_top_padding:
1076 * @canvas: an #AdgCanvas
1078 * Gets the top padding (in global space) of @canvas.
1080 * Returns: the requested padding or %0 on error.
1085 adg_canvas_get_top_padding(AdgCanvas
*canvas
)
1087 AdgCanvasPrivate
*data
;
1089 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), 0.);
1091 data
= canvas
->data
;
1092 return data
->top_padding
;
1096 * adg_canvas_set_right_padding:
1097 * @canvas: an #AdgCanvas
1098 * @value: the new padding, in global space
1100 * Changes the right padding of @canvas by setting #AdgCanvas:right-padding
1101 * to @value. Negative values are allowed.
1106 adg_canvas_set_right_padding(AdgCanvas
*canvas
, gdouble value
)
1108 g_return_if_fail(ADG_IS_CANVAS(canvas
));
1109 g_object_set(canvas
, "right-padding", value
, NULL
);
1113 * adg_canvas_get_right_padding:
1114 * @canvas: an #AdgCanvas
1116 * Gets the right padding (in global space) of @canvas.
1118 * Returns: the requested padding or %0 on error.
1123 adg_canvas_get_right_padding(AdgCanvas
*canvas
)
1125 AdgCanvasPrivate
*data
;
1127 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), 0.);
1129 data
= canvas
->data
;
1130 return data
->right_padding
;
1135 * adg_canvas_set_bottom_padding:
1136 * @canvas: an #AdgCanvas
1137 * @value: the new padding, in global space
1139 * Changes the bottom padding of @canvas by setting #AdgCanvas:bottom-padding
1140 * to @value. Negative values are allowed.
1145 adg_canvas_set_bottom_padding(AdgCanvas
*canvas
, gdouble value
)
1147 g_return_if_fail(ADG_IS_CANVAS(canvas
));
1148 g_object_set(canvas
, "bottom-padding", value
, NULL
);
1152 * adg_canvas_get_bottom_padding:
1153 * @canvas: an #AdgCanvas
1155 * Gets the bottom padding (in global space) of @canvas.
1157 * Returns: the requested padding or %0 on error.
1162 adg_canvas_get_bottom_padding(AdgCanvas
*canvas
)
1164 AdgCanvasPrivate
*data
;
1166 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), 0.);
1168 data
= canvas
->data
;
1169 return data
->bottom_padding
;
1173 * adg_canvas_set_left_padding:
1174 * @canvas: an #AdgCanvas
1175 * @value: the new padding, in global space
1177 * Changes the left padding of @canvas by setting #AdgCanvas:left-padding
1178 * to @value. Negative values are allowed.
1183 adg_canvas_set_left_padding(AdgCanvas
*canvas
, gdouble value
)
1185 g_return_if_fail(ADG_IS_CANVAS(canvas
));
1186 g_object_set(canvas
, "left-padding", value
, NULL
);
1190 * adg_canvas_get_left_padding:
1191 * @canvas: an #AdgCanvas
1193 * Gets the left padding (in global space) of @canvas.
1195 * Returns: the requested padding or %0 on error.
1200 adg_canvas_get_left_padding(AdgCanvas
*canvas
)
1202 AdgCanvasPrivate
*data
;
1204 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), 0.);
1206 data
= canvas
->data
;
1207 return data
->left_padding
;
1211 * adg_canvas_set_paddings:
1212 * @canvas: an #AdgCanvas
1213 * @top: top padding, in global space
1214 * @right: right padding, in global space
1215 * @bottom: bottom padding, in global space
1216 * @left: left padding, in global space
1218 * Convenient function to set all the paddings at once.
1223 adg_canvas_set_paddings(AdgCanvas
*canvas
, gdouble top
, gdouble right
,
1224 gdouble bottom
, gdouble left
)
1226 g_return_if_fail(ADG_IS_CANVAS(canvas
));
1227 g_object_set(canvas
, "top-padding", top
, "right-padding", right
,
1228 "bottom-padding", bottom
, "left-padding", left
, NULL
);
1233 _adg_global_changed(AdgEntity
*entity
)
1235 AdgCanvasPrivate
*data
= ((AdgCanvas
*) entity
)->data
;
1237 if (_ADG_OLD_ENTITY_CLASS
->global_changed
)
1238 _ADG_OLD_ENTITY_CLASS
->global_changed(entity
);
1240 if (data
->title_block
)
1241 adg_entity_global_changed((AdgEntity
*) data
->title_block
);
1245 _adg_local_changed(AdgEntity
*entity
)
1247 AdgCanvasPrivate
*data
= ((AdgCanvas
*) entity
)->data
;
1249 if (_ADG_OLD_ENTITY_CLASS
->local_changed
)
1250 _ADG_OLD_ENTITY_CLASS
->local_changed(entity
);
1252 if (data
->title_block
)
1253 adg_entity_local_changed((AdgEntity
*) data
->title_block
);
1257 _adg_invalidate(AdgEntity
*entity
)
1259 AdgCanvasPrivate
*data
= ((AdgCanvas
*) entity
)->data
;
1261 if (_ADG_OLD_ENTITY_CLASS
->invalidate
)
1262 _ADG_OLD_ENTITY_CLASS
->invalidate(entity
);
1264 if (data
->title_block
)
1265 adg_entity_invalidate((AdgEntity
*) data
->title_block
);
1269 _adg_arrange(AdgEntity
*entity
)
1271 AdgCanvasPrivate
*data
;
1272 CpmlExtents extents
;
1274 if (_ADG_OLD_ENTITY_CLASS
->arrange
)
1275 _ADG_OLD_ENTITY_CLASS
->arrange(entity
);
1277 cpml_extents_copy(&extents
, adg_entity_get_extents(entity
));
1279 /* The extents should be defined, otherwise there is no drawing */
1280 g_return_if_fail(extents
.is_defined
);
1282 data
= ((AdgCanvas
*) entity
)->data
;
1284 if (data
->size
.x
> 0 || data
->size
.y
> 0) {
1285 const AdgMatrix
*global
= adg_entity_get_global_matrix(entity
);
1290 paper
.size
.x
= data
->size
.x
;
1291 paper
.size
.y
= data
->size
.y
;
1293 cairo_matrix_transform_point(global
, &paper
.org
.x
, &paper
.org
.y
);
1294 cairo_matrix_transform_distance(global
, &paper
.size
.x
, &paper
.size
.y
);
1296 if (data
->size
.x
> 0) {
1297 extents
.org
.x
= paper
.org
.x
;
1298 extents
.size
.x
= paper
.size
.x
;
1300 if (data
->size
.y
> 0) {
1301 extents
.org
.y
= paper
.org
.y
;
1302 extents
.size
.y
= paper
.size
.y
;
1306 if (data
->size
.x
== 0) {
1307 extents
.org
.x
-= data
->left_padding
;
1308 extents
.size
.x
+= data
->left_padding
+ data
->right_padding
;
1310 if (data
->size
.y
== 0) {
1311 extents
.org
.y
-= data
->top_padding
;
1312 extents
.size
.y
+= data
->top_padding
+ data
->bottom_padding
;
1315 /* Impose the new extents */
1316 adg_entity_set_extents(entity
, &extents
);
1318 if (data
->title_block
) {
1319 AdgEntity
*title_block_entity
;
1320 const CpmlExtents
*title_block_extents
;
1323 title_block_entity
= (AdgEntity
*) data
->title_block
;
1324 adg_entity_arrange(title_block_entity
);
1325 title_block_extents
= adg_entity_get_extents(title_block_entity
);
1327 shift
.x
= extents
.org
.x
+ extents
.size
.x
- title_block_extents
->org
.x
1328 - title_block_extents
->size
.x
;
1329 shift
.y
= extents
.org
.y
+ extents
.size
.y
- title_block_extents
->org
.y
1330 - title_block_extents
->size
.y
;
1332 /* The following block could be optimized by skipping tiny shift,
1333 * usually left by rounding errors */
1334 if (shift
.x
!= 0 || shift
.y
!= 0) {
1335 AdgMatrix unglobal
, map
;
1336 adg_matrix_copy(&unglobal
, adg_entity_get_global_matrix(entity
));
1337 cairo_matrix_invert(&unglobal
);
1339 cairo_matrix_transform_distance(&unglobal
, &shift
.x
, &shift
.y
);
1340 cairo_matrix_init_translate(&map
, shift
.x
, shift
.y
);
1341 adg_entity_transform_global_map(title_block_entity
, &map
,
1342 ADG_TRANSFORM_AFTER
);
1344 adg_entity_global_changed(title_block_entity
);
1345 adg_entity_arrange(title_block_entity
);
1351 _adg_render(AdgEntity
*entity
, cairo_t
*cr
)
1353 AdgCanvasPrivate
*data
;
1354 const CpmlExtents
*extents
;
1356 data
= ((AdgCanvas
*) entity
)->data
;
1357 extents
= adg_entity_get_extents(entity
);
1361 /* Background fill */
1362 cairo_rectangle(cr
, extents
->org
.x
- data
->left_margin
,
1363 extents
->org
.y
- data
->top_margin
,
1364 extents
->size
.x
+ data
->left_margin
+ data
->right_margin
,
1365 extents
->size
.y
+ data
->top_margin
+ data
->bottom_margin
);
1366 adg_entity_apply_dress(entity
, data
->background_dress
, cr
);
1370 if (data
->has_frame
) {
1371 cairo_rectangle(cr
, extents
->org
.x
, extents
->org
.y
,
1372 extents
->size
.x
, extents
->size
.y
);
1373 cairo_transform(cr
, adg_entity_get_global_matrix(entity
));
1374 adg_entity_apply_dress(entity
, data
->frame_dress
, cr
);
1380 if (data
->title_block
)
1381 adg_entity_render((AdgEntity
*) data
->title_block
, cr
);
1383 if (_ADG_OLD_ENTITY_CLASS
->render
)
1384 _ADG_OLD_ENTITY_CLASS
->render(entity
, cr
);
1388 #if GTK3_ENABLED || GTK2_ENABLED
1391 * adg_canvas_set_paper:
1392 * @canvas: an #AdgCanvas
1393 * @paper_name: a paper name
1394 * @orientation: the page orientation
1396 * A convenient function to set the size of @canvas using a
1397 * @paper_name and an @orientation value. This should be a
1398 * PWG 5101.1-2002 paper name and it will be passed as is to
1399 * gtk_paper_size_new(), so use any valid name accepted by
1402 * To reset this size, you could use adg_canvas_set_size() with a
1403 * %NULL size: in this way the size will match the boundary boxes
1404 * of the entities contained by the canvas.
1406 * Furthermore, the margins will be set to their default values,
1407 * that is the margins returned by the #GtkPaperSize API.
1408 * If you want to use your own margins on a named paper size,
1409 * set them <emphasis>after</emphasis> the call to this function.
1414 adg_canvas_set_paper(AdgCanvas
*canvas
, const gchar
*paper_name
,
1415 GtkPageOrientation orientation
)
1417 GtkPageSetup
*page_setup
;
1418 GtkPaperSize
*paper_size
;
1420 g_return_if_fail(ADG_IS_CANVAS(canvas
));
1421 g_return_if_fail(paper_name
!= NULL
);
1423 page_setup
= gtk_page_setup_new();
1424 paper_size
= gtk_paper_size_new(paper_name
);
1426 gtk_page_setup_set_paper_size(page_setup
, paper_size
);
1427 gtk_page_setup_set_orientation(page_setup
, orientation
);
1428 gtk_paper_size_free(paper_size
);
1430 adg_canvas_set_page_setup(canvas
, page_setup
);
1431 g_object_unref(page_setup
);
1435 * adg_canvas_set_page_setup:
1436 * @canvas: an #AdgCanvas
1437 * @page_setup: the page setup
1439 * A convenient function to setup the page of @canvas so it can
1440 * also be subsequentially used for printing. It is allowed to
1441 * pass %NULL for @page_setup to unset the setup data from @canvas.
1443 * A reference to @page_setup is added, so there is no need to keep
1444 * alive this object outside this function. The @page_setup pointer
1445 * is stored in the associative key %_adg_page_setup and can be
1446 * retrieved at any time with:
1449 * page_setup = g_object_get_data(G_OBJECT(canvas), "_adg_page_setup");
1452 * The size and margins provided by @page_setup are used to set the
1453 * size and margins of @canvas much in the same way as what
1454 * adg_canvas_set_paper() does. This means if you set a page and
1455 * then unset it, the canvas will retain size and margins of the
1456 * original page although @page_setup will not be used for printing.
1457 * You must unset the size with adg_canvas_set_size() with a %NULL size.
1460 * // By default, canvas does not have an explicit size
1461 * adg_canvas_set_page_setup(canvas, a4);
1462 * // Here canvas has the size and margins specified by a4
1463 * adg_canvas_set_page_setup(canvas, NULL);
1464 * // Now the only difference is that canvas is no more bound
1465 * // to the a4 page setup, so the following will return NULL:
1466 * page_setup = g_object_get_data(G_OBJECT(canvas), "_adg_page_setup");
1467 * // To restore the original status and have an autocomputed size:
1468 * adg_canvas_set_size(canvas, NULL);
1474 adg_canvas_set_page_setup(AdgCanvas
*canvas
, GtkPageSetup
*page_setup
)
1476 gdouble top
, right
, bottom
, left
;
1479 g_return_if_fail(ADG_IS_CANVAS(canvas
));
1481 if (page_setup
== NULL
) {
1482 /* By convention, NULL resets the default page but
1483 * does not change any other property */
1484 g_object_set_data((GObject
*) canvas
, "_adg_page_setup", NULL
);
1488 g_return_if_fail(GTK_IS_PAGE_SETUP(page_setup
));
1490 top
= gtk_page_setup_get_top_margin(page_setup
, GTK_UNIT_POINTS
);
1491 right
= gtk_page_setup_get_right_margin(page_setup
, GTK_UNIT_POINTS
);
1492 bottom
= gtk_page_setup_get_bottom_margin(page_setup
, GTK_UNIT_POINTS
);
1493 left
= gtk_page_setup_get_left_margin(page_setup
, GTK_UNIT_POINTS
);
1494 size
.x
= gtk_page_setup_get_page_width(page_setup
, GTK_UNIT_POINTS
);
1495 size
.y
= gtk_page_setup_get_page_height(page_setup
, GTK_UNIT_POINTS
);
1497 adg_canvas_set_size(canvas
, &size
);
1498 adg_canvas_set_margins(canvas
, top
, right
, bottom
, left
);
1500 g_object_ref(page_setup
);
1501 g_object_set_data_full((GObject
*) canvas
, "_adg_page_setup",
1502 page_setup
, g_object_unref
);