1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 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-param-dress.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
,
257 const gchar
*scales
[] = {
258 "10:1", "5:1", "3:1", "2:1", "1:1", "1:2", "1:3", "1:5", "1:10",
265 data
->scales
= g_strdupv((gchar
**) scales
);
266 data
->background_dress
= ADG_DRESS_COLOR_BACKGROUND
;
267 data
->frame_dress
= ADG_DRESS_LINE_FRAME
;
268 data
->title_block
= NULL
;
269 data
->top_margin
= 15;
270 data
->right_margin
= 15;
271 data
->bottom_margin
= 15;
272 data
->left_margin
= 15;
273 data
->has_frame
= TRUE
;
274 data
->top_padding
= 15;
275 data
->right_padding
= 15;
276 data
->bottom_padding
= 15;
277 data
->left_padding
= 15;
283 _adg_dispose(GObject
*object
)
286 AdgCanvasPrivate
*data
;
288 canvas
= (AdgCanvas
*) object
;
291 if (data
->title_block
) {
292 g_object_unref(data
->title_block
);
293 data
->title_block
= NULL
;
296 if (data
->scales
!= NULL
) {
297 g_strfreev(data
->scales
);
301 if (_ADG_OLD_OBJECT_CLASS
->dispose
)
302 _ADG_OLD_OBJECT_CLASS
->dispose(object
);
307 _adg_get_property(GObject
*object
, guint prop_id
,
308 GValue
*value
, GParamSpec
*pspec
)
310 AdgCanvasPrivate
*data
= ((AdgCanvas
*) object
)->data
;
314 g_value_set_boxed(value
, &data
->size
);
317 g_value_set_boxed(value
, data
->scales
);
319 case PROP_BACKGROUND_DRESS
:
320 g_value_set_enum(value
, data
->background_dress
);
322 case PROP_FRAME_DRESS
:
323 g_value_set_enum(value
, data
->frame_dress
);
325 case PROP_TITLE_BLOCK
:
326 g_value_set_object(value
, data
->title_block
);
328 case PROP_TOP_MARGIN
:
329 g_value_set_double(value
, data
->top_margin
);
331 case PROP_RIGHT_MARGIN
:
332 g_value_set_double(value
, data
->right_margin
);
334 case PROP_BOTTOM_MARGIN
:
335 g_value_set_double(value
, data
->bottom_margin
);
337 case PROP_LEFT_MARGIN
:
338 g_value_set_double(value
, data
->left_margin
);
341 g_value_set_boolean(value
, data
->has_frame
);
343 case PROP_TOP_PADDING
:
344 g_value_set_double(value
, data
->top_padding
);
346 case PROP_RIGHT_PADDING
:
347 g_value_set_double(value
, data
->right_padding
);
349 case PROP_BOTTOM_PADDING
:
350 g_value_set_double(value
, data
->bottom_padding
);
352 case PROP_LEFT_PADDING
:
353 g_value_set_double(value
, data
->left_padding
);
356 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
362 _adg_set_property(GObject
*object
, guint prop_id
,
363 const GValue
*value
, GParamSpec
*pspec
)
366 AdgCanvasPrivate
*data
;
367 AdgTitleBlock
*title_block
;
369 canvas
= (AdgCanvas
*) object
;
374 cpml_pair_copy(&data
->size
, g_value_get_boxed(value
));
377 g_strfreev(data
->scales
);
378 data
->scales
= g_value_dup_boxed(value
);
380 case PROP_BACKGROUND_DRESS
:
381 data
->background_dress
= g_value_get_enum(value
);
383 case PROP_FRAME_DRESS
:
384 data
->frame_dress
= g_value_get_enum(value
);
386 case PROP_TITLE_BLOCK
:
387 title_block
= g_value_get_object(value
);
389 g_object_ref(title_block
);
390 adg_entity_set_parent((AdgEntity
*) title_block
,
391 (AdgEntity
*) canvas
);
393 if (data
->title_block
)
394 g_object_unref(data
->title_block
);
395 data
->title_block
= title_block
;
397 case PROP_TOP_MARGIN
:
398 data
->top_margin
= g_value_get_double(value
);
400 case PROP_RIGHT_MARGIN
:
401 data
->right_margin
= g_value_get_double(value
);
403 case PROP_BOTTOM_MARGIN
:
404 data
->bottom_margin
= g_value_get_double(value
);
406 case PROP_LEFT_MARGIN
:
407 data
->left_margin
= g_value_get_double(value
);
410 data
->has_frame
= g_value_get_boolean(value
);
412 case PROP_TOP_PADDING
:
413 data
->top_padding
= g_value_get_double(value
);
415 case PROP_RIGHT_PADDING
:
416 data
->right_padding
= g_value_get_double(value
);
418 case PROP_BOTTOM_PADDING
:
419 data
->bottom_padding
= g_value_get_double(value
);
421 case PROP_LEFT_PADDING
:
422 data
->left_padding
= g_value_get_double(value
);
425 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
434 * Creates a new empty canvas object.
436 * Returns: (transfer full): the newly created canvas.
443 return g_object_new(ADG_TYPE_CANVAS
, NULL
);
447 * adg_canvas_set_size:
448 * @canvas: an #AdgCanvas
449 * @size: (transfer none): the new size for the canvas
451 * Sets a specific size on @canvas. The x and/or y
452 * component of @size can be set to %0, in which case
453 * the exact value will be autocalculated, that is the
454 * size component returned by adg_entity_get_extents()
455 * on @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 @canvas
498 * will be used instead.
500 * Returns: (transfer none): 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: (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
587 adg_canvas_set_scales_array(AdgCanvas
*canvas
, gchar
**scales
)
589 g_return_if_fail(ADG_IS_CANVAS(canvas
));
590 g_object_set(canvas
, "scales", scales
, NULL
);
594 * adg_canvas_get_scales:
595 * @canvas: an #AdgCanvas
597 * Gets the list of scales set on @canvas: check adg_canvas_set_scales()
598 * to get an idea of what scales are supposed to be.
600 * If no scales are set, %NULL is returned.
602 * Returns: (element-type utf8) (transfer none): the %NULL terminated list of valid scales.
607 adg_canvas_get_scales(AdgCanvas
*canvas
)
609 AdgCanvasPrivate
*data
;
611 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), NULL
);
618 * adg_canvas_autoscale:
619 * @canvas: an #AdgCanvas
621 * Applies one scale per time, in the order they have been provided
622 * in the adg_canvas_set_scales() call, until the drawing can be
623 * entirely contained into the current paper. If successful, the
624 * scale of the title block is changed accordingly and the drawing
625 * is centered inside the paper.
630 adg_canvas_autoscale(AdgCanvas
*canvas
)
632 AdgCanvasPrivate
*data
;
636 const CpmlExtents
*extents
;
637 AdgTitleBlock
*title_block
;
640 g_return_if_fail(ADG_IS_CANVAS(canvas
));
641 g_return_if_fail(_ADG_OLD_ENTITY_CLASS
->arrange
!= NULL
);
644 entity
= (AdgEntity
*) canvas
;
645 title_block
= data
->title_block
;
647 for (p_scale
= data
->scales
; p_scale
!= NULL
&& *p_scale
!= NULL
; ++p_scale
) {
648 const gchar
*scale
= *p_scale
;
649 gdouble factor
= adg_scale_factor(scale
);
653 cairo_matrix_init_scale(&map
, factor
, factor
);
654 adg_entity_set_local_map(entity
, &map
);
655 adg_entity_local_changed(entity
);
657 /* Arrange the entities inside the canvas, but not the canvas itself,
658 * just to get the bounding box of the drawing without the paper */
659 _ADG_OLD_ENTITY_CLASS
->arrange(entity
);
660 extents
= adg_entity_get_extents(entity
);
662 /* Just in case @canvas is emtpy */
663 if (! extents
->is_defined
)
666 if (title_block
!= NULL
)
667 adg_title_block_set_scale(title_block
, scale
);
669 /* Bail out if paper size is not specified or invalid */
670 if (data
->size
.x
<= 0 || data
->size
.y
<= 0)
673 /* If the drawing extents are fully contained inside the paper size,
674 * center the drawing in the paper and bail out */
675 delta
.x
= data
->size
.x
- extents
->size
.x
;
676 delta
.y
= data
->size
.y
- extents
->size
.y
;
677 if (delta
.x
>= 0 && delta
.y
>= 0) {
678 cairo_matrix_t transform
;
679 cairo_matrix_init_translate(&transform
,
680 delta
.x
/ 2 - extents
->org
.x
,
681 delta
.y
/ 2 - extents
->org
.y
);
682 adg_entity_transform_local_map(entity
, &transform
,
683 ADG_TRANSFORM_AFTER
);
690 * adg_canvas_set_background_dress:
691 * @canvas: an #AdgCanvas
692 * @dress: the new #AdgDress to use
694 * Sets a new background dress for rendering @canvas: the new
695 * dress must be a color dress.
700 adg_canvas_set_background_dress(AdgCanvas
*canvas
, AdgDress dress
)
702 g_return_if_fail(ADG_IS_CANVAS(canvas
));
703 g_object_set(canvas
, "background-dress", dress
, NULL
);
707 * adg_canvas_get_background_dress:
708 * @canvas: an #AdgCanvas
710 * Gets the background dress to be used in rendering @canvas.
712 * Returns: the current background dress.
717 adg_canvas_get_background_dress(AdgCanvas
*canvas
)
719 AdgCanvasPrivate
*data
;
721 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), ADG_DRESS_UNDEFINED
);
725 return data
->background_dress
;
729 * adg_canvas_set_frame_dress:
730 * @canvas: an #AdgCanvas
731 * @dress: the new #AdgDress to use
733 * Sets the #AdgCanvas:frame-dress property of @canvas to @dress:
734 * the new dress must be a line dress.
739 adg_canvas_set_frame_dress(AdgCanvas
*canvas
, AdgDress dress
)
741 g_return_if_fail(ADG_IS_CANVAS(canvas
));
742 g_object_set(canvas
, "frame-dress", dress
, NULL
);
746 * adg_canvas_get_frame_dress:
747 * @canvas: an #AdgCanvas
749 * Gets the frame dress to be used in rendering the border of @canvas.
751 * Returns: the current frame dress.
756 adg_canvas_get_frame_dress(AdgCanvas
*canvas
)
758 AdgCanvasPrivate
*data
;
760 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), ADG_DRESS_UNDEFINED
);
763 return data
->frame_dress
;
767 * adg_canvas_set_title_block:
768 * @canvas: an #AdgCanvas
769 * @title_block: (transfer full): a title block
771 * Sets the #AdgCanvas:title-block property of @canvas to @title_block.
773 * Although a title block entity could be added to @canvas in the usual
774 * way, that is using the adg_container_add() method, assigning a title
775 * block with adg_canvas_set_title_block() is somewhat different:
777 * - @title_block will be automatically attached to the bottom right
778 * corner of to the @canvas frame (this could be accomplished in the
779 * usual way too, by resetting the right and bottom paddings);
780 * - the @title_block boundary box is not taken into account while
781 * computing the extents of @canvas.
786 adg_canvas_set_title_block(AdgCanvas
*canvas
,
787 AdgTitleBlock
*title_block
)
789 g_return_if_fail(ADG_IS_CANVAS(canvas
));
790 g_return_if_fail(title_block
== NULL
|| ADG_IS_TITLE_BLOCK(title_block
));
791 g_object_set(canvas
, "title-block", title_block
, NULL
);
795 * adg_canvas_get_title_block:
796 * @canvas: an #AdgCanvas
798 * Gets the #AdgTitleBlock object of @canvas: check
799 * adg_canvas_set_title_block() for details.
801 * The returned entity is owned by @canvas and should not be
804 * Returns: (transfer none): the title block object or %NULL.
809 adg_canvas_get_title_block(AdgCanvas
*canvas
)
811 AdgCanvasPrivate
*data
;
813 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), NULL
);
816 return data
->title_block
;
820 * adg_canvas_set_top_margin:
821 * @canvas: an #AdgCanvas
822 * @value: the new margin, in global space
824 * Changes the top margin of @canvas by setting #AdgCanvas:top-margin
825 * to @value. Negative values are allowed.
830 adg_canvas_set_top_margin(AdgCanvas
*canvas
, gdouble value
)
832 g_return_if_fail(ADG_IS_CANVAS(canvas
));
833 g_object_set(canvas
, "top-margin", value
, NULL
);
837 * adg_canvas_get_top_margin:
838 * @canvas: an #AdgCanvas
840 * Gets the top margin (in global space) of @canvas.
842 * Returns: the requested margin or %0 on error.
847 adg_canvas_get_top_margin(AdgCanvas
*canvas
)
849 AdgCanvasPrivate
*data
;
851 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), 0.);
854 return data
->top_margin
;
858 * adg_canvas_set_right_margin:
859 * @canvas: an #AdgCanvas
860 * @value: the new margin, in global space
862 * Changes the right margin of @canvas by setting
863 * #AdgCanvas:right-margin to @value. Negative values are allowed.
868 adg_canvas_set_right_margin(AdgCanvas
*canvas
, gdouble value
)
870 g_return_if_fail(ADG_IS_CANVAS(canvas
));
871 g_object_set(canvas
, "right-margin", value
, NULL
);
875 * adg_canvas_get_right_margin:
876 * @canvas: an #AdgCanvas
878 * Gets the right margin (in global space) of @canvas.
880 * Returns: the requested margin or %0 on error.
885 adg_canvas_get_right_margin(AdgCanvas
*canvas
)
887 AdgCanvasPrivate
*data
;
889 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), 0.);
892 return data
->right_margin
;
896 * adg_canvas_set_bottom_margin:
897 * @canvas: an #AdgCanvas
898 * @value: the new margin, in global space
900 * Changes the bottom margin of @canvas by setting
901 * #AdgCanvas:bottom-margin to @value. Negative values are allowed.
906 adg_canvas_set_bottom_margin(AdgCanvas
*canvas
, gdouble value
)
908 g_return_if_fail(ADG_IS_CANVAS(canvas
));
909 g_object_set(canvas
, "bottom-margin", value
, NULL
);
913 * adg_canvas_get_bottom_margin:
914 * @canvas: an #AdgCanvas
916 * Gets the bottom margin (in global space) of @canvas.
918 * Returns: the requested margin or %0 on error.
923 adg_canvas_get_bottom_margin(AdgCanvas
*canvas
)
925 AdgCanvasPrivate
*data
;
927 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), 0.);
930 return data
->bottom_margin
;
934 * adg_canvas_set_left_margin:
935 * @canvas: an #AdgCanvas
936 * @value: the new margin, in global space
938 * Changes the left margin of @canvas by setting
939 * #AdgCanvas:left-margin to @value. Negative values are allowed.
944 adg_canvas_set_left_margin(AdgCanvas
*canvas
, gdouble value
)
946 g_return_if_fail(ADG_IS_CANVAS(canvas
));
947 g_object_set(canvas
, "left-margin", value
, NULL
);
951 * adg_canvas_get_left_margin:
952 * @canvas: an #AdgCanvas
954 * Gets the left margin (in global space) of @canvas.
956 * Returns: the requested margin or %0 on error.
961 adg_canvas_get_left_margin(AdgCanvas
*canvas
)
963 AdgCanvasPrivate
*data
;
965 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), 0.);
968 return data
->left_margin
;
972 * adg_canvas_set_margins:
973 * @canvas: an #AdgCanvas
974 * @top: top margin, in global space
975 * @right: right margin, in global space
976 * @bottom: bottom margin, in global space
977 * @left: left margin, in global space
979 * Convenient function to set all the margins at once.
984 adg_canvas_set_margins(AdgCanvas
*canvas
,
985 gdouble top
, gdouble right
,
986 gdouble bottom
, gdouble left
)
988 g_return_if_fail(ADG_IS_CANVAS(canvas
));
989 g_object_set(canvas
, "top-margin", top
, "right-margin", right
,
990 "bottom-margin", bottom
, "left-margin", left
, NULL
);
994 * adg_canvas_apply_margins:
995 * @canvas: an #AdgCanvas
996 * @extents: where apply the margins
998 * A convenient function to apply the margins of @canvas to the
999 * arbitrary #CpmlExtents struct @extents.
1004 adg_canvas_apply_margins(AdgCanvas
*canvas
, CpmlExtents
*extents
)
1006 AdgCanvasPrivate
*data
;
1008 g_return_if_fail(ADG_IS_CANVAS(canvas
));
1010 data
= canvas
->data
;
1012 extents
->org
.x
-= data
->left_margin
;
1013 extents
->org
.y
-= data
->top_margin
;
1014 extents
->size
.x
+= data
->left_margin
+ data
->right_margin
;
1015 extents
->size
.y
+= data
->top_margin
+ data
->bottom_margin
;
1019 * adg_canvas_switch_frame:
1020 * @canvas: an #AdgCanvas
1021 * @new_state: the new flag status
1023 * Sets a new status on the #AdgCanvas:has-frame property: %TRUE
1024 * means a border around the canvas extents (less the margins)
1025 * should be rendered.
1030 adg_canvas_switch_frame(AdgCanvas
*canvas
, gboolean new_state
)
1032 g_return_if_fail(ADG_IS_CANVAS(canvas
));
1033 g_object_set(canvas
, "has-frame", new_state
, NULL
);
1037 * adg_canvas_has_frame:
1038 * @canvas: an #AdgCanvas
1040 * Gets the current status of the #AdgCanvas:has-frame property,
1041 * that is whether a border around the canvas extents (less the
1042 * margins) should be rendered (%TRUE) or not (%FALSE).
1044 * Returns: the current status of the frame flag.
1049 adg_canvas_has_frame(AdgCanvas
*canvas
)
1051 AdgCanvasPrivate
*data
;
1053 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), FALSE
);
1055 data
= canvas
->data
;
1056 return data
->has_frame
;
1060 * adg_canvas_set_top_padding:
1061 * @canvas: an #AdgCanvas
1062 * @value: the new padding, in global space
1064 * Changes the top padding of @canvas by setting
1065 * #AdgCanvas:top-padding to @value. Negative values are allowed.
1070 adg_canvas_set_top_padding(AdgCanvas
*canvas
, gdouble value
)
1072 g_return_if_fail(ADG_IS_CANVAS(canvas
));
1073 g_object_set(canvas
, "top-padding", value
, NULL
);
1077 * adg_canvas_get_top_padding:
1078 * @canvas: an #AdgCanvas
1080 * Gets the top padding (in global space) of @canvas.
1082 * Returns: the requested padding or %0 on error.
1087 adg_canvas_get_top_padding(AdgCanvas
*canvas
)
1089 AdgCanvasPrivate
*data
;
1091 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), 0.);
1093 data
= canvas
->data
;
1094 return data
->top_padding
;
1098 * adg_canvas_set_right_padding:
1099 * @canvas: an #AdgCanvas
1100 * @value: the new padding, in global space
1102 * Changes the right padding of @canvas by setting #AdgCanvas:right-padding
1103 * to @value. Negative values are allowed.
1108 adg_canvas_set_right_padding(AdgCanvas
*canvas
, gdouble value
)
1110 g_return_if_fail(ADG_IS_CANVAS(canvas
));
1111 g_object_set(canvas
, "right-padding", value
, NULL
);
1115 * adg_canvas_get_right_padding:
1116 * @canvas: an #AdgCanvas
1118 * Gets the right padding (in global space) of @canvas.
1120 * Returns: the requested padding or %0 on error.
1125 adg_canvas_get_right_padding(AdgCanvas
*canvas
)
1127 AdgCanvasPrivate
*data
;
1129 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), 0.);
1131 data
= canvas
->data
;
1132 return data
->right_padding
;
1137 * adg_canvas_set_bottom_padding:
1138 * @canvas: an #AdgCanvas
1139 * @value: the new padding, in global space
1141 * Changes the bottom padding of @canvas by setting
1142 * #AdgCanvas:bottom-padding to @value. Negative values are allowed.
1147 adg_canvas_set_bottom_padding(AdgCanvas
*canvas
, gdouble value
)
1149 g_return_if_fail(ADG_IS_CANVAS(canvas
));
1150 g_object_set(canvas
, "bottom-padding", value
, NULL
);
1154 * adg_canvas_get_bottom_padding:
1155 * @canvas: an #AdgCanvas
1157 * Gets the bottom padding (in global space) of @canvas.
1159 * Returns: the requested padding or %0 on error.
1164 adg_canvas_get_bottom_padding(AdgCanvas
*canvas
)
1166 AdgCanvasPrivate
*data
;
1168 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), 0.);
1170 data
= canvas
->data
;
1171 return data
->bottom_padding
;
1175 * adg_canvas_set_left_padding:
1176 * @canvas: an #AdgCanvas
1177 * @value: the new padding, in global space
1179 * Changes the left padding of @canvas by setting
1180 * #AdgCanvas:left-padding to @value. Negative values are allowed.
1185 adg_canvas_set_left_padding(AdgCanvas
*canvas
, gdouble value
)
1187 g_return_if_fail(ADG_IS_CANVAS(canvas
));
1188 g_object_set(canvas
, "left-padding", value
, NULL
);
1192 * adg_canvas_get_left_padding:
1193 * @canvas: an #AdgCanvas
1195 * Gets the left padding (in global space) of @canvas.
1197 * Returns: the requested padding or %0 on error.
1202 adg_canvas_get_left_padding(AdgCanvas
*canvas
)
1204 AdgCanvasPrivate
*data
;
1206 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), 0.);
1208 data
= canvas
->data
;
1209 return data
->left_padding
;
1213 * adg_canvas_set_paddings:
1214 * @canvas: an #AdgCanvas
1215 * @top: top padding, in global space
1216 * @right: right padding, in global space
1217 * @bottom: bottom padding, in global space
1218 * @left: left padding, in global space
1220 * Convenient function to set all the paddings at once.
1225 adg_canvas_set_paddings(AdgCanvas
*canvas
,
1226 gdouble top
, gdouble right
,
1227 gdouble bottom
, gdouble left
)
1229 g_return_if_fail(ADG_IS_CANVAS(canvas
));
1230 g_object_set(canvas
, "top-padding", top
, "right-padding", right
,
1231 "bottom-padding", bottom
, "left-padding", left
, NULL
);
1236 _adg_global_changed(AdgEntity
*entity
)
1238 AdgCanvasPrivate
*data
= ((AdgCanvas
*) entity
)->data
;
1240 if (_ADG_OLD_ENTITY_CLASS
->global_changed
)
1241 _ADG_OLD_ENTITY_CLASS
->global_changed(entity
);
1243 if (data
->title_block
)
1244 adg_entity_global_changed((AdgEntity
*) data
->title_block
);
1248 _adg_local_changed(AdgEntity
*entity
)
1250 AdgCanvasPrivate
*data
= ((AdgCanvas
*) entity
)->data
;
1251 AdgTitleBlock
*title_block
= data
->title_block
;
1253 if (_ADG_OLD_ENTITY_CLASS
->local_changed
)
1254 _ADG_OLD_ENTITY_CLASS
->local_changed(entity
);
1256 if (title_block
!= NULL
) {
1257 const gchar
*scale
= adg_title_block_get_scale(title_block
);
1259 if (scale
!= NULL
&& scale
[0] != '\0') {
1260 const cairo_matrix_t
*map
= adg_entity_get_local_map(entity
);
1261 gdouble factor
= adg_scale_factor(scale
);
1263 if (map
->xx
!= factor
|| map
->yy
!= factor
)
1264 adg_title_block_set_scale(title_block
, "---");
1267 adg_entity_local_changed((AdgEntity
*) title_block
);
1272 _adg_invalidate(AdgEntity
*entity
)
1274 AdgCanvasPrivate
*data
= ((AdgCanvas
*) entity
)->data
;
1276 if (_ADG_OLD_ENTITY_CLASS
->invalidate
)
1277 _ADG_OLD_ENTITY_CLASS
->invalidate(entity
);
1279 if (data
->title_block
)
1280 adg_entity_invalidate((AdgEntity
*) data
->title_block
);
1284 _adg_arrange(AdgEntity
*entity
)
1286 AdgCanvasPrivate
*data
;
1287 CpmlExtents extents
;
1289 if (_ADG_OLD_ENTITY_CLASS
->arrange
)
1290 _ADG_OLD_ENTITY_CLASS
->arrange(entity
);
1292 cpml_extents_copy(&extents
, adg_entity_get_extents(entity
));
1294 /* The extents should be defined, otherwise there is no drawing */
1295 g_return_if_fail(extents
.is_defined
);
1297 data
= ((AdgCanvas
*) entity
)->data
;
1299 if (data
->size
.x
> 0 || data
->size
.y
> 0) {
1300 const cairo_matrix_t
*global
= adg_entity_get_global_matrix(entity
);
1305 paper
.size
.x
= data
->size
.x
;
1306 paper
.size
.y
= data
->size
.y
;
1308 cairo_matrix_transform_point(global
, &paper
.org
.x
, &paper
.org
.y
);
1309 cairo_matrix_transform_distance(global
, &paper
.size
.x
, &paper
.size
.y
);
1311 if (data
->size
.x
> 0) {
1312 extents
.org
.x
= paper
.org
.x
;
1313 extents
.size
.x
= paper
.size
.x
;
1315 if (data
->size
.y
> 0) {
1316 extents
.org
.y
= paper
.org
.y
;
1317 extents
.size
.y
= paper
.size
.y
;
1321 if (data
->size
.x
== 0) {
1322 extents
.org
.x
-= data
->left_padding
;
1323 extents
.size
.x
+= data
->left_padding
+ data
->right_padding
;
1325 if (data
->size
.y
== 0) {
1326 extents
.org
.y
-= data
->top_padding
;
1327 extents
.size
.y
+= data
->top_padding
+ data
->bottom_padding
;
1330 /* Impose the new extents */
1331 adg_entity_set_extents(entity
, &extents
);
1333 if (data
->title_block
) {
1334 AdgEntity
*title_block_entity
;
1335 const CpmlExtents
*title_block_extents
;
1338 title_block_entity
= (AdgEntity
*) data
->title_block
;
1339 adg_entity_arrange(title_block_entity
);
1340 title_block_extents
= adg_entity_get_extents(title_block_entity
);
1342 shift
.x
= extents
.org
.x
+ extents
.size
.x
- title_block_extents
->org
.x
1343 - title_block_extents
->size
.x
;
1344 shift
.y
= extents
.org
.y
+ extents
.size
.y
- title_block_extents
->org
.y
1345 - title_block_extents
->size
.y
;
1347 /* The following block could be optimized by skipping tiny shift,
1348 * usually left by rounding errors */
1349 if (shift
.x
!= 0 || shift
.y
!= 0) {
1350 cairo_matrix_t unglobal
, map
;
1351 adg_matrix_copy(&unglobal
, adg_entity_get_global_matrix(entity
));
1352 cairo_matrix_invert(&unglobal
);
1354 cairo_matrix_transform_distance(&unglobal
, &shift
.x
, &shift
.y
);
1355 cairo_matrix_init_translate(&map
, shift
.x
, shift
.y
);
1356 adg_entity_transform_global_map(title_block_entity
, &map
,
1357 ADG_TRANSFORM_AFTER
);
1359 adg_entity_global_changed(title_block_entity
);
1360 adg_entity_arrange(title_block_entity
);
1366 _adg_render(AdgEntity
*entity
, cairo_t
*cr
)
1368 AdgCanvasPrivate
*data
;
1369 const CpmlExtents
*extents
;
1371 data
= ((AdgCanvas
*) entity
)->data
;
1372 extents
= adg_entity_get_extents(entity
);
1376 /* Background fill */
1377 cairo_rectangle(cr
, extents
->org
.x
- data
->left_margin
,
1378 extents
->org
.y
- data
->top_margin
,
1379 extents
->size
.x
+ data
->left_margin
+ data
->right_margin
,
1380 extents
->size
.y
+ data
->top_margin
+ data
->bottom_margin
);
1381 adg_entity_apply_dress(entity
, data
->background_dress
, cr
);
1385 if (data
->has_frame
) {
1386 cairo_rectangle(cr
, extents
->org
.x
, extents
->org
.y
,
1387 extents
->size
.x
, extents
->size
.y
);
1388 cairo_transform(cr
, adg_entity_get_global_matrix(entity
));
1389 adg_entity_apply_dress(entity
, data
->frame_dress
, cr
);
1395 if (data
->title_block
)
1396 adg_entity_render((AdgEntity
*) data
->title_block
, cr
);
1398 if (_ADG_OLD_ENTITY_CLASS
->render
)
1399 _ADG_OLD_ENTITY_CLASS
->render(entity
, cr
);
1403 #if GTK3_ENABLED || GTK2_ENABLED
1406 * adg_canvas_set_paper:
1407 * @canvas: an #AdgCanvas
1408 * @paper_name: a paper name
1409 * @orientation: the page orientation
1411 * A convenient function to set the size of @canvas using a
1412 * @paper_name and an @orientation value. This should be a
1413 * PWG 5101.1-2002 paper name and it will be passed as is to
1414 * gtk_paper_size_new(), so use any valid name accepted by
1417 * To reset this size, you could use adg_canvas_set_size() with a
1418 * %NULL size: in this way the size will match the boundary boxes
1419 * of the entities contained by the canvas.
1421 * Furthermore, the margins will be set to their default values,
1422 * that is the margins returned by the #GtkPaperSize API.
1423 * If you want to use your own margins on a named paper size,
1424 * set them <emphasis>after</emphasis> the call to this function.
1429 adg_canvas_set_paper(AdgCanvas
*canvas
,
1430 const gchar
*paper_name
,
1431 GtkPageOrientation orientation
)
1433 GtkPageSetup
*page_setup
;
1434 GtkPaperSize
*paper_size
;
1436 g_return_if_fail(ADG_IS_CANVAS(canvas
));
1437 g_return_if_fail(paper_name
!= NULL
);
1439 page_setup
= gtk_page_setup_new();
1440 paper_size
= gtk_paper_size_new(paper_name
);
1442 gtk_page_setup_set_paper_size(page_setup
, paper_size
);
1443 gtk_page_setup_set_orientation(page_setup
, orientation
);
1444 gtk_paper_size_free(paper_size
);
1446 adg_canvas_set_page_setup(canvas
, page_setup
);
1447 g_object_unref(page_setup
);
1451 * adg_canvas_set_page_setup:
1452 * @canvas: an #AdgCanvas
1453 * @page_setup: (allow-none) (transfer none): the page setup
1455 * A convenient function to setup the page of @canvas so it can
1456 * also be subsequentially used for printing. It is allowed to
1457 * pass %NULL for @page_setup to unset the setup data from @canvas.
1459 * A reference to @page_setup is added, so there is no need to keep
1460 * alive this object outside this function. The internal @page_setup
1461 * pointer is stored in the associative key %_adg_page_setup and
1462 * can be retrieved at any time with adg_canvas_get_page_setup().
1464 * The size and margins provided by @page_setup are used to set the
1465 * size and margins of @canvas much in the same way as what
1466 * adg_canvas_set_paper() does. This means if you set a page and
1467 * then unset it, the canvas will retain size and margins of the
1468 * original page although @page_setup will not be used for printing.
1469 * You must unset the size with adg_canvas_set_size() with a
1472 * <informalexample><programlisting language="C">
1473 * // By default, canvas does not have an explicit size
1474 * adg_canvas_set_page_setup(canvas, a4);
1475 * // Here canvas has the size and margins specified by a4
1476 * adg_canvas_set_page_setup(canvas, NULL);
1477 * // Now the only difference is that canvas is no more bound
1478 * // to the a4 page setup, so the following will return NULL:
1479 * page_setup = adg_canvas_get_page_setup(canvas);
1480 * // To restore the original status and have an autocomputed size:
1481 * adg_canvas_set_size(canvas, NULL);
1482 * </programlisting></informalexample>
1487 adg_canvas_set_page_setup(AdgCanvas
*canvas
, GtkPageSetup
*page_setup
)
1489 gdouble top
, right
, bottom
, left
;
1492 g_return_if_fail(ADG_IS_CANVAS(canvas
));
1494 if (page_setup
== NULL
) {
1495 /* By convention, NULL resets the default page but
1496 * does not change any other property */
1497 g_object_set_data((GObject
*) canvas
, "_adg_page_setup", NULL
);
1501 g_return_if_fail(GTK_IS_PAGE_SETUP(page_setup
));
1503 top
= gtk_page_setup_get_top_margin(page_setup
, GTK_UNIT_POINTS
);
1504 right
= gtk_page_setup_get_right_margin(page_setup
, GTK_UNIT_POINTS
);
1505 bottom
= gtk_page_setup_get_bottom_margin(page_setup
, GTK_UNIT_POINTS
);
1506 left
= gtk_page_setup_get_left_margin(page_setup
, GTK_UNIT_POINTS
);
1507 size
.x
= gtk_page_setup_get_page_width(page_setup
, GTK_UNIT_POINTS
);
1508 size
.y
= gtk_page_setup_get_page_height(page_setup
, GTK_UNIT_POINTS
);
1510 adg_canvas_set_size(canvas
, &size
);
1511 adg_canvas_set_margins(canvas
, top
, right
, bottom
, left
);
1513 g_object_ref(page_setup
);
1514 g_object_set_data_full((GObject
*) canvas
, "_adg_page_setup",
1515 page_setup
, g_object_unref
);
1519 * adg_canvas_get_page_setup:
1520 * @canvas: an #AdgCanvas
1522 * If adg_canvas_set_page_setup() is called, a #GtkPageSetup object
1523 * is created and bound to @canvas. This metho returns a pointer
1524 * to that internal object or %NULL if adg_canvas_set_page_setup()
1525 * has not been called before.
1527 * Returns: (allow-none) (transfer none): the #GtkPageSetup with size and margins of @canvas of %NULL on no setup found or errors.
1532 adg_canvas_get_page_setup(AdgCanvas
*canvas
)
1534 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), NULL
);
1535 return g_object_get_data((GObject
*) canvas
, "_adg_page_setup");