docs: cosmetic changes
[adg.git] / src / adg / adg-gtk-area.c
blobf694f79c91086bb32f07cbb207dbde9ae19ed72f
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011 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-gtk-area
23 * @short_description: A #GtkWidget specifically designed to contain
24 * an #AdgCanvas entity
26 * This is a #GtkDrawingArea derived object that provides an easy way
27 * to show an ADG based canvas. The associated canvas can be set
28 * directly with the adg_gtk_area_new_with_canvas() constructor
29 * function or by using adg_gtk_area_set_canvas().
31 * The minimum size of the widget will depend on the canvas content.
33 * The default implementation reacts to some mouse events: if you drag
34 * the mouse keeping the wheel pressed, the canvas will be translated
35 * (in local space by default and in global space if %SHIFT is pressed);
36 * if the mouse wheel is rotated the canvas will be scaled up or down
37 * according to the wheel direction by the factor specified in the
38 * #AdgGtkArea:factor property (again, in local space by default and
39 * in global space if %SHIFT is pressed). The adg_gtk_area_get_zoom()
40 * method could be used to retrieve the current zoom coefficient.
42 * A new transformation layer is present between the global space
43 * and the rendering: the #AdgGtkArea:render-map matrix. This
44 * transformation is applied just before the rendering and it is
45 * used to align and/or apply the zoom coefficient to the canvas
46 * without affecting the other layers. Local transformations,
47 * instead, are directly applied to the local matrix of the canvas.
49 * Since: 1.0
50 **/
53 /**
54 * AdgGtkArea:
56 * All fields are private and should not be used directly.
57 * Use its public methods instead.
59 * Since: 1.0
60 **/
63 #include "adg-internal.h"
64 #include <gtk/gtk.h>
66 #include "adg-container.h"
67 #include "adg-table.h"
68 #include "adg-title-block.h"
69 #include "adg-canvas.h"
70 #include "adg-gtk-utils.h"
72 #include "adg-gtk-area.h"
73 #include "adg-gtk-area-private.h"
75 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_gtk_area_parent_class)
76 #define _ADG_OLD_WIDGET_CLASS ((GtkWidgetClass *) adg_gtk_area_parent_class)
79 G_DEFINE_TYPE(AdgGtkArea, adg_gtk_area, GTK_TYPE_DRAWING_AREA)
81 enum {
82 PROP_0,
83 PROP_CANVAS,
84 PROP_FACTOR,
85 PROP_AUTOZOOM,
86 PROP_RENDER_MAP
89 enum {
90 CANVAS_CHANGED,
91 EXTENTS_CHANGED,
92 LAST_SIGNAL
96 static void _adg_dispose (GObject *object);
97 static void _adg_get_property (GObject *object,
98 guint prop_id,
99 GValue *value,
100 GParamSpec *pspec);
101 static void _adg_set_property (GObject *object,
102 guint prop_id,
103 const GValue *value,
104 GParamSpec *pspec);
105 static void _adg_size_request (GtkWidget *widget,
106 GtkRequisition *requisition);
107 static void _adg_size_allocate (GtkWidget *widget,
108 GtkAllocation *allocation);
109 static gboolean _adg_expose_event (GtkWidget *widget,
110 GdkEventExpose *event);
111 static gboolean _adg_scroll_event (GtkWidget *widget,
112 GdkEventScroll *event);
113 static gboolean _adg_button_press_event (GtkWidget *widget,
114 GdkEventButton *event);
115 static gboolean _adg_motion_notify_event(GtkWidget *widget,
116 GdkEventMotion *event);
117 static gboolean _adg_get_map (GtkWidget *widget,
118 gboolean local_space,
119 AdgMatrix *map,
120 AdgMatrix *inverted);
121 static void _adg_set_map (GtkWidget *widget,
122 gboolean local_space,
123 const AdgMatrix *map);
124 static void _adg_canvas_changed (AdgGtkArea *area,
125 AdgCanvas *old_canvas);
126 static const CpmlExtents *
127 _adg_get_extents (AdgGtkArea *area);
129 static guint _adg_signals[LAST_SIGNAL] = { 0 };
132 static void
133 adg_gtk_area_class_init(AdgGtkAreaClass *klass)
135 GObjectClass *gobject_class;
136 GtkWidgetClass *widget_class;
137 GParamSpec *param;
139 gobject_class = (GObjectClass *) klass;
140 widget_class = (GtkWidgetClass *) klass;
142 g_type_class_add_private(klass, sizeof(AdgGtkAreaPrivate));
144 gobject_class->dispose = _adg_dispose;
145 gobject_class->get_property = _adg_get_property;
146 gobject_class->set_property = _adg_set_property;
148 widget_class->size_request = _adg_size_request;
149 widget_class->size_allocate = _adg_size_allocate;
150 widget_class->expose_event = _adg_expose_event;
151 widget_class->scroll_event = _adg_scroll_event;
152 widget_class->button_press_event = _adg_button_press_event;
153 widget_class->motion_notify_event = _adg_motion_notify_event;
155 klass->canvas_changed = _adg_canvas_changed;
157 param = g_param_spec_object("canvas",
158 P_("Canvas"),
159 P_("The canvas to be shown"),
160 ADG_TYPE_CANVAS,
161 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
162 g_object_class_install_property(gobject_class, PROP_CANVAS, param);
164 param = g_param_spec_double("factor",
165 P_("Factor"),
166 P_("The factor to use while zooming in and out (usually with the mouse wheel)"),
167 1., G_MAXDOUBLE, 1.05,
168 G_PARAM_READWRITE);
169 g_object_class_install_property(gobject_class, PROP_FACTOR, param);
171 param = g_param_spec_boolean("autozoom",
172 P_("Autozoom"),
173 P_("When enabled, automatically adjust the zoom in global space at every size allocation"),
174 FALSE,
175 G_PARAM_READWRITE);
176 g_object_class_install_property(gobject_class, PROP_AUTOZOOM, param);
178 param = g_param_spec_boxed("render-map",
179 P_("Render Map"),
180 P_("The transformation to be applied on the canvas before rendering it"),
181 ADG_TYPE_MATRIX,
182 G_PARAM_READWRITE);
183 g_object_class_install_property(gobject_class, PROP_RENDER_MAP, param);
186 * AdgGtkArea::canvas-changed:
187 * @area: an #AdgGtkArea
188 * @old_canvas: the old #AdgCanvas object
190 * Emitted when the #AdgGtkArea has a new canvas. If the new canvas
191 * is the same as the old one, the signal is not emitted.
193 * Since: 1.0
195 _adg_signals[CANVAS_CHANGED] =
196 g_signal_new("canvas-changed", ADG_GTK_TYPE_AREA,
197 G_SIGNAL_RUN_LAST|G_SIGNAL_NO_RECURSE,
198 G_STRUCT_OFFSET(AdgGtkAreaClass, canvas_changed),
199 NULL, NULL,
200 adg_marshal_VOID__OBJECT,
201 G_TYPE_NONE, 1, ADG_TYPE_CANVAS);
204 * AdgGtkArea::extents-changed:
205 * @area: an #AdgGtkArea
206 * @old_extents: the old #CpmlExtents struct
208 * Emitted when the extents of @area have been changed.
209 * The old extents are always compared to the new ones,
210 * so when the extents are recalculated but the result
211 * is the same the signal is not emitted.
213 * The extents of #AdgGtkArea are subject to the render
214 * map, so changing the #AdgGtkArea:render-map property
215 * will emit this signal too.
217 * Since: 1.0
219 _adg_signals[EXTENTS_CHANGED] =
220 g_signal_new("extents-changed", ADG_GTK_TYPE_AREA,
221 G_SIGNAL_RUN_LAST|G_SIGNAL_NO_RECURSE,
222 G_STRUCT_OFFSET(AdgGtkAreaClass, extents_changed),
223 NULL, NULL,
224 adg_marshal_VOID__POINTER,
225 G_TYPE_NONE, 1, G_TYPE_POINTER);
228 static void
229 adg_gtk_area_init(AdgGtkArea *area)
231 AdgGtkAreaPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(area,
232 ADG_GTK_TYPE_AREA,
233 AdgGtkAreaPrivate);
235 data->canvas = NULL;
236 data->factor = 1.05;
237 data->autozoom = FALSE;
238 cairo_matrix_init_identity(&data->render_map);
240 data->initialized = FALSE;
241 data->x_event = 0;
242 data->y_event = 0;
244 area->data = data;
246 /* Enable GDK events to catch wheel rotation and drag */
247 gtk_widget_add_events((GtkWidget *) area,
248 GDK_BUTTON_PRESS_MASK |
249 GDK_BUTTON2_MOTION_MASK |
250 GDK_SCROLL_MASK);
253 static void
254 _adg_dispose(GObject *object)
256 AdgGtkAreaPrivate *data = ((AdgGtkArea *) object)->data;
258 if (data->canvas) {
259 g_object_unref(data->canvas);
260 data->canvas = NULL;
263 if (_ADG_OLD_OBJECT_CLASS->dispose)
264 _ADG_OLD_OBJECT_CLASS->dispose(object);
267 static void
268 _adg_get_property(GObject *object, guint prop_id,
269 GValue *value, GParamSpec *pspec)
271 AdgGtkAreaPrivate *data = ((AdgGtkArea *) object)->data;
273 switch (prop_id) {
274 case PROP_CANVAS:
275 g_value_set_object(value, data->canvas);
276 break;
277 case PROP_FACTOR:
278 g_value_set_double(value, data->factor);
279 break;
280 case PROP_AUTOZOOM:
281 g_value_set_boolean(value, data->autozoom);
282 break;
283 case PROP_RENDER_MAP:
284 g_value_set_boxed(value, &data->render_map);
285 break;
286 default:
287 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
288 break;
292 static void
293 _adg_set_property(GObject *object, guint prop_id,
294 const GValue *value, GParamSpec *pspec)
296 AdgGtkArea *area;
297 AdgGtkAreaPrivate *data;
298 AdgCanvas *new_canvas, *old_canvas;
300 area = (AdgGtkArea *) object;
301 data = area->data;
303 switch (prop_id) {
304 case PROP_CANVAS:
305 new_canvas = g_value_get_object(value);
306 old_canvas = data->canvas;
307 if (new_canvas != old_canvas) {
308 if (new_canvas != NULL)
309 g_object_ref(new_canvas);
310 if (old_canvas != NULL)
311 g_object_unref(old_canvas);
312 data->canvas = new_canvas;
313 g_signal_emit(area, _adg_signals[CANVAS_CHANGED], 0, old_canvas);
315 break;
316 case PROP_FACTOR:
317 data->factor = g_value_get_double(value);
318 break;
319 case PROP_AUTOZOOM:
320 data->autozoom = g_value_get_boolean(value);
321 break;
322 case PROP_RENDER_MAP:
323 adg_matrix_copy(&data->render_map, g_value_get_boxed(value));
324 break;
325 default:
326 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
327 break;
333 * adg_gtk_area_new:
335 * Creates a new empty #AdgGtkArea. The widget is useful only after
336 * an #AdgCanvas has been added either using the #AdgGtkArea:canvas
337 * property or with adg_gtk_area_set_canvas().
339 * Returns: the newly created widget
341 * Since: 1.0
343 GtkWidget *
344 adg_gtk_area_new(void)
346 return g_object_new(ADG_GTK_TYPE_AREA, NULL);
350 * adg_gtk_area_new_with_canvas:
351 * @canvas: the #AdgCanvas shown by this widget
353 * Creates a new #AdgGtkArea and sets the #AdgGtkArea:canvas property
354 * to @canvas.
356 * Returns: the newly created widget
358 * Since: 1.0
360 GtkWidget *
361 adg_gtk_area_new_with_canvas(AdgCanvas *canvas)
363 g_return_val_if_fail(ADG_IS_CANVAS(canvas), NULL);
365 return g_object_new(ADG_GTK_TYPE_AREA, "canvas", canvas, NULL);
369 * adg_gtk_area_set_canvas:
370 * @area: an #AdgGtkArea
371 * @canvas: the new #AdgCanvas
373 * Sets a new canvas on @area. The old canvas, if presents, is
374 * unreferenced.
376 * Since: 1.0
378 void
379 adg_gtk_area_set_canvas(AdgGtkArea *area, AdgCanvas *canvas)
381 g_return_if_fail(ADG_GTK_IS_AREA(area));
382 g_object_set(area, "canvas", canvas, NULL);
386 * adg_gtk_area_get_canvas:
387 * @area: an #AdgGtkArea
389 * Gets the canvas associated to @area.
391 * Returns: the requested #AdgCanvas object or %NULL on errors
393 * Since: 1.0
395 AdgCanvas *
396 adg_gtk_area_get_canvas(AdgGtkArea *area)
398 AdgGtkAreaPrivate *data;
400 g_return_val_if_fail(ADG_GTK_IS_AREA(area), NULL);
402 data = area->data;
404 return data->canvas;
408 * adg_gtk_area_set_render_map:
409 * @area: an #AdgGtkArea object
410 * @map: the new map
412 * Sets the new render transformation of @area to @map:
413 * the old map is discarded. If @map is %NULL, the render
414 * map is left unchanged.
416 * <note><para>
417 * The render map is an implementation detail and this function
418 * is expected to be used only by #AdgGtkArea derived objects.
419 * </para></note>
421 * Since: 1.0
423 void
424 adg_gtk_area_set_render_map(AdgGtkArea *area, const AdgMatrix *map)
426 g_return_if_fail(ADG_GTK_IS_AREA(area));
427 g_object_set(area, "render-map", map, NULL);
431 * adg_gtk_area_transform_render_map:
432 * @area: an #AdgGtkArea object
433 * @transformation: the transformation to apply
434 * @mode: how @transformation should be applied
436 * Convenient function to change the render map of @area by
437 * applying @tranformation using the @mode operator. This is
438 * logically equivalent to the following:
440 * |[
441 * AdgMatrix map;
442 * adg_matrix_copy(&map, adg_gtk_area_get_render_map(area));
443 * adg_matrix_transform(&map, transformation, mode);
444 * adg_gtk_area_set_render_map(area, &map);
445 * ]|
447 * <note><para>
448 * The render map is an implementation detail and this function
449 * is expected to be used only by #AdgGtkArea derived objects.
450 * </para></note>
452 * Since: 1.0
454 void
455 adg_gtk_area_transform_render_map(AdgGtkArea *area,
456 const AdgMatrix *transformation,
457 AdgTransformMode mode)
459 AdgGtkAreaPrivate *data;
460 AdgMatrix map;
462 g_return_if_fail(ADG_GTK_IS_AREA(area));
463 g_return_if_fail(transformation != NULL);
465 data = area->data;
467 adg_matrix_copy(&map, &data->render_map);
468 adg_matrix_transform(&map, transformation, mode);
470 g_object_set(area, "render-map", &map, NULL);
474 * adg_gtk_area_get_render_map:
475 * @area: an #AdgGtkArea object
477 * Gets the render map.
479 * Returns: the requested map or %NULL on errors
481 * Since: 1.0
483 const AdgMatrix *
484 adg_gtk_area_get_render_map(AdgGtkArea *area)
486 AdgGtkAreaPrivate *data;
488 g_return_val_if_fail(ADG_GTK_IS_AREA(area), NULL);
490 data = area->data;
492 return &data->render_map;
496 * adg_gtk_area_get_extents:
497 * @area: an #AdgGtkArea
499 * Gets the extents of the canvas bound to @area. The returned
500 * struct is owned by @area and should not modified or freed.
502 * The extents of an #AdgGtkArea instance are the extents of
503 * its canvas (as returned by adg_entity_get_extents()) with
504 * the margins added to it and the #AdgGtkArea:render-map
505 * transformation applied.
507 * If @area does not have any canvas associated to it or the
508 * canvas is invalid or empty, an undefined #CpmlExtents
509 * struct will be returned.
511 * The canvas will be updated, meaning adg_entity_arrange()
512 * is called before the extents computation.
514 * Returns: the extents of the @area canvas or %NULL on errors
516 * Since: 1.0
518 const CpmlExtents *
519 adg_gtk_area_get_extents(AdgGtkArea *area)
521 g_return_val_if_fail(ADG_GTK_IS_AREA(area), NULL);
523 return _adg_get_extents(area);
527 * adg_gtk_area_get_zoom:
528 * @area: an #AdgGtkArea
530 * Gets the last zoom coefficient applied on the canvas of @area.
531 * If the #AdgGtkArea:autozoom property is %FALSE, the value
532 * returned should be always %1.
534 * Returns: the current zoom coefficient
536 * Since: 1.0
538 gdouble
539 adg_gtk_area_get_zoom(AdgGtkArea *area)
541 AdgGtkAreaPrivate *data;
543 g_return_val_if_fail(ADG_GTK_IS_AREA(area), 0.);
545 data = area->data;
546 return data->render_map.xx;
550 * adg_gtk_area_set_factor:
551 * @area: an #AdgGtkArea
552 * @factor: the new zoom factor
554 * Sets a new zoom factor to @area. If the factor is less than
555 * 1, it will be clamped to 1.
557 * Since: 1.0
559 void
560 adg_gtk_area_set_factor(AdgGtkArea *area, gdouble factor)
562 g_return_if_fail(ADG_GTK_IS_AREA(area));
563 g_object_set(area, "factor", factor, NULL);
567 * adg_gtk_area_get_factor:
568 * @area: an #AdgGtkArea
570 * Gets the zoom factor associated to @area. The zoom factor is
571 * directly used to zoom in (that is, the default zoom factor of
572 * 1.05 will zoom of 5% every iteration) and it is reversed while
573 * zooming out (that is, the default factor will be 1/1.05).
575 * Returns: the requested zoom factor or 0 on error
577 * Since: 1.0
579 gdouble
580 adg_gtk_area_get_factor(AdgGtkArea *area)
582 AdgGtkAreaPrivate *data;
584 g_return_val_if_fail(ADG_GTK_IS_AREA(area), 0.);
586 data = area->data;
587 return data->factor;
591 * adg_gtk_area_switch_autozoom:
592 * @area: an #AdgGtkArea
593 * @state: the new autozoom state
595 * Sets the #AdgGtkArea:autozoom property of @area to @state. When the
596 * autozoom feature is enabled, @area reacts to any size allocation
597 * by adjusting its zoom coefficient in global space. This means the
598 * drawing will fill the available space (keeping its aspect ratio)
599 * when resizing the window.
601 * Since: 1.0
603 void
604 adg_gtk_area_switch_autozoom(AdgGtkArea *area, gboolean state)
606 g_return_if_fail(ADG_GTK_IS_AREA(area));
607 g_object_set(area, "autozoom", state, NULL);
611 * adg_gtk_area_has_autozoom:
612 * @area: an #AdgGtkArea
614 * Gets the current state of the #AdgGtkArea:autozoom property on
615 * the @area object.
617 * Returns: the current autozoom state
619 * Since: 1.0
621 gboolean
622 adg_gtk_area_has_autozoom(AdgGtkArea *area)
624 AdgGtkAreaPrivate *data;
626 g_return_val_if_fail(ADG_GTK_IS_AREA(area), FALSE);
628 data = area->data;
629 return data->autozoom;
633 * adg_gtk_area_canvas_changed:
634 * @area: an #AdgGtkArea
635 * @canvas: the old canvas bound to @area
637 * Emits the #AdgGtkArea::canvas-changed signal on @area.
639 * Since: 1.0
641 void
642 adg_gtk_area_canvas_changed(AdgGtkArea *area, AdgCanvas *old_canvas)
644 g_return_if_fail(ADG_GTK_IS_AREA(area));
646 g_signal_emit(area, _adg_signals[CANVAS_CHANGED], 0, old_canvas);
650 * adg_gtk_area_extents_changed:
651 * @area: an #AdgGtkArea
652 * @old_extents: the old extents of @area
654 * Emits the #AdgGtkArea::extents-changed signal on @area.
656 * Since: 1.0
658 void
659 adg_gtk_area_extents_changed(AdgGtkArea *area, const CpmlExtents *old_extents)
661 g_return_if_fail(ADG_GTK_IS_AREA(area));
663 g_signal_emit(area, _adg_signals[EXTENTS_CHANGED], 0, old_extents);
667 static void
668 _adg_size_request(GtkWidget *widget, GtkRequisition *requisition)
670 AdgGtkArea *area;
671 const CpmlExtents *extents;
673 area = (AdgGtkArea *) widget;
674 extents = _adg_get_extents(area);
676 if (extents->is_defined) {
677 requisition->width = extents->size.x;
678 requisition->height = extents->size.y;
683 * _adg_size_allocate:
684 * @widget: an #AdgGtkArea widget
685 * @allocation: the new allocation struct
687 * Scales the drawing according to the new allocation if
688 * #AdgGtkArea:autozoom is %TRUE.
690 * TODO: the current implementation initially centers the canvas
691 * on the allocation space. Further allocations (due to a
692 * window resizing, for example) use the top/left corner of the
693 * canvas as reference point. Plan different policies for either
694 * those situations.
696 * Since: 1.0
698 static void
699 _adg_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
701 AdgGtkArea *area;
702 AdgGtkAreaPrivate *data;
703 const CpmlExtents *sheet;
704 CpmlVector size;
705 gdouble factor;
707 if (_ADG_OLD_WIDGET_CLASS->size_allocate)
708 _ADG_OLD_WIDGET_CLASS->size_allocate(widget, allocation);
710 area = (AdgGtkArea *) widget;
711 data = area->data;
713 sheet = _adg_get_extents(area);
714 if (!sheet->is_defined || sheet->size.x <= 0 || sheet->size.y <= 0)
715 return;
717 size.x = allocation->width;
718 size.y = allocation->height;
720 if (data->autozoom) {
721 /* Adjust the zoom according to the allocation and sheet size */
722 factor = MIN(size.x / sheet->size.x, size.y / sheet->size.y);
723 } else if (!data->initialized) {
724 /* First allocation with autozoom off: keep the current zoom */
725 factor = 1;
726 } else {
727 /* Not the first allocation and autozoom off: keep the old map */
728 return;
731 if (!data->initialized) {
732 /* TODO: plan different attachment policies other than centering */
733 cairo_matrix_init_translate(&data->render_map,
734 (size.x - sheet->size.x) / 2 - sheet->org.x,
735 (size.y - sheet->size.y) / 2 - sheet->org.y);
736 data->initialized = TRUE;
739 /* TODO: plan other reference points other than left/top (x0, y0) */
740 data->render_map.x0 *= factor;
741 data->render_map.y0 *= factor;
742 data->render_map.xx *= factor;
743 data->render_map.yy *= factor;
746 static gboolean
747 _adg_expose_event(GtkWidget *widget, GdkEventExpose *event)
749 AdgGtkAreaPrivate *data;
750 AdgCanvas *canvas;
752 data = ((AdgGtkArea *) widget)->data;
753 canvas = data->canvas;
755 if (canvas != NULL && event->window != NULL) {
756 cairo_t *cr = gdk_cairo_create(event->window);
757 cairo_transform(cr, &data->render_map);
758 adg_entity_render((AdgEntity *) canvas, cr);
759 cairo_destroy(cr);
762 if (_ADG_OLD_WIDGET_CLASS->expose_event == NULL)
763 return FALSE;
765 return _ADG_OLD_WIDGET_CLASS->expose_event(widget, event);
768 static gboolean
769 _adg_scroll_event(GtkWidget *widget, GdkEventScroll *event)
771 gboolean zoom_in, zoom_out, local_space, global_space;
772 AdgMatrix map, inverted;
773 AdgGtkAreaPrivate *data;
774 double factor, x, y;
776 zoom_in = event->direction == GDK_SCROLL_UP;
777 zoom_out = event->direction == GDK_SCROLL_DOWN;
778 local_space = (event->state & ADG_GTK_MODIFIERS) == 0;
779 global_space = (event->state & ADG_GTK_MODIFIERS) == GDK_SHIFT_MASK;
781 if ((zoom_in || zoom_out) && (local_space || global_space) &&
782 _adg_get_map(widget, local_space, &map, &inverted)) {
783 data = ((AdgGtkArea *) widget)->data;
784 factor = zoom_in ? data->factor : 1. / data->factor;
785 x = event->x;
786 y = event->y;
788 cairo_matrix_transform_point(&inverted, &x, &y);
789 cairo_matrix_scale(&map, factor, factor);
790 cairo_matrix_translate(&map, x/factor - x, y/factor - y);
792 _adg_set_map(widget, local_space, &map);
794 gtk_widget_queue_draw(widget);
796 /* Avoid to chain up the default handler:
797 * this event has been grabbed by this function */
798 return TRUE;
801 if (_ADG_OLD_WIDGET_CLASS->scroll_event == NULL)
802 return FALSE;
804 return _ADG_OLD_WIDGET_CLASS->scroll_event(widget, event);
807 static gboolean
808 _adg_button_press_event(GtkWidget *widget, GdkEventButton *event)
810 AdgGtkAreaPrivate *data = ((AdgGtkArea *) widget)->data;
812 if (event->type == GDK_BUTTON_PRESS && event->button == 2) {
813 /* Remember the starting coordinates of a (probable) translation */
814 data->x_event = event->x;
815 data->y_event = event->y;
818 if (_ADG_OLD_WIDGET_CLASS->button_press_event == NULL)
819 return FALSE;
821 return _ADG_OLD_WIDGET_CLASS->button_press_event(widget, event);
824 static gboolean
825 _adg_motion_notify_event(GtkWidget *widget, GdkEventMotion *event)
827 gboolean translating, local_space, global_space;
828 AdgMatrix map, inverted;
829 AdgGtkAreaPrivate *data;
830 double x, y;
832 translating = (event->state & GDK_BUTTON2_MASK) == GDK_BUTTON2_MASK;
833 local_space = (event->state & ADG_GTK_MODIFIERS) == 0;
834 global_space = (event->state & ADG_GTK_MODIFIERS) == GDK_SHIFT_MASK;
836 if (translating && (local_space || global_space) &&
837 _adg_get_map(widget, local_space, &map, &inverted)) {
838 data = ((AdgGtkArea *) widget)->data;
839 x = event->x - data->x_event;
840 y = event->y - data->y_event;
842 cairo_matrix_transform_distance(&inverted, &x, &y);
843 cairo_matrix_translate(&map, x, y);
844 data->x_event = event->x;
845 data->y_event = event->y;
847 _adg_set_map(widget, local_space, &map);
849 gtk_widget_queue_draw(widget);
851 /* Avoid to chain up the default handler:
852 * this event has been grabbed by this function */
853 return TRUE;
856 if (_ADG_OLD_WIDGET_CLASS->motion_notify_event == NULL)
857 return FALSE;
859 return _ADG_OLD_WIDGET_CLASS->motion_notify_event(widget, event);
862 static gboolean
863 _adg_get_map(GtkWidget *widget, gboolean local_space,
864 AdgMatrix *map, AdgMatrix *inverted)
866 AdgGtkAreaPrivate *data;
867 AdgEntity *entity;
869 data = ((AdgGtkArea *) widget)->data;
870 entity = (AdgEntity *) data->canvas;
871 if (entity == NULL)
872 return FALSE;
874 if (local_space) {
875 adg_matrix_copy(map, adg_entity_get_local_map(entity));
877 /* The inverted map is subject to the global matrix */
878 adg_matrix_copy(inverted, adg_entity_get_global_matrix(entity));
879 adg_matrix_transform(inverted, map, ADG_TRANSFORM_BEFORE);
880 } else {
881 adg_matrix_copy(map, adg_entity_get_global_map(entity));
882 adg_matrix_copy(inverted, map);
885 return cairo_matrix_invert(inverted) == CAIRO_STATUS_SUCCESS;
888 static void
889 _adg_set_map(GtkWidget *widget, gboolean local_space, const AdgMatrix *map)
891 AdgGtkAreaPrivate *data;
892 AdgEntity *entity;
894 data = ((AdgGtkArea *) widget)->data;
895 entity = (AdgEntity *) data->canvas;
897 if (entity == NULL)
898 return;
900 if (local_space) {
901 /* TODO: this forcibly overwrites any local transformation */
902 adg_entity_set_local_map(entity, map);
903 } else {
904 adg_matrix_transform(&data->render_map, map, ADG_TRANSFORM_BEFORE);
907 /* This will emit the extents-changed signal when applicable */
908 _adg_get_extents((AdgGtkArea *) widget);
911 static void
912 _adg_canvas_changed(AdgGtkArea *area, AdgCanvas *old_canvas)
914 AdgGtkAreaPrivate *data = area->data;
915 data->initialized = FALSE;
918 static const CpmlExtents *
919 _adg_get_extents(AdgGtkArea *area)
921 AdgGtkAreaPrivate *data;
922 AdgCanvas *canvas;
923 CpmlExtents old_extents;
925 data = area->data;
926 old_extents = data->extents;
927 data->extents.is_defined = FALSE;
928 canvas = data->canvas;
930 if (ADG_IS_CANVAS(canvas)) {
931 const CpmlExtents *extents;
932 AdgEntity *entity;
934 entity = (AdgEntity *) canvas;
936 adg_entity_arrange(entity);
937 extents = adg_entity_get_extents(entity);
939 if (extents != NULL) {
940 data->extents = *extents;
941 adg_canvas_apply_margins(canvas, &data->extents);
942 cpml_extents_transform(&data->extents, &data->render_map);
946 if (!cpml_extents_equal(&data->extents, &old_extents))
947 g_signal_emit(area, _adg_signals[EXTENTS_CHANGED], 0, &old_extents);
949 return &data->extents;