1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
24 * @short_description: A #GtkWidget specifically designed to contain
25 * an #AdgCanvas entity
27 * This is a #GtkDrawingArea derived object that provides an easy way
28 * to show an ADG based canvas. Its default implementation reacts to
29 * some mouse events: if you drag the mouse by keepeng the wheel pressed
30 * the canvas is translated by translating its model transformation;
31 * if the mouse wheel is rotated, the model transformation of the canvas
32 * is scaled up or down (accordling to the wheel direction) by a factor
33 * specified by the #AdgWidget:factor property.
39 * All fields are private and should not be used directly.
40 * Use its public methods instead.
44 #include "adg-widget.h"
45 #include "adg-widget-private.h"
61 static void dispose (GObject
*object
);
62 static void get_property (GObject
*object
,
66 static void set_property (GObject
*object
,
70 static void set_canvas (AdgWidget
*widget
,
72 static gboolean
expose_event (GtkWidget
*widget
,
73 GdkEventExpose
*event
);
74 static gboolean
scroll_event (GtkWidget
*widget
,
75 GdkEventScroll
*event
);
76 static gboolean
button_press_event (GtkWidget
*widget
,
77 GdkEventButton
*event
);
78 static gboolean
motion_notify_event (GtkWidget
*widget
,
79 GdkEventMotion
*event
);
80 static gboolean
get_transformation (GtkWidget
*widget
,
83 static void set_transformation (GtkWidget
*widget
,
84 const AdgMatrix
*model
);
86 static guint signals
[LAST_SIGNAL
] = { 0 };
89 G_DEFINE_TYPE(AdgWidget
, adg_widget
, GTK_TYPE_DRAWING_AREA
);
93 adg_widget_class_init(AdgWidgetClass
*klass
)
95 GObjectClass
*gobject_class
;
96 GtkWidgetClass
*widget_class
;
99 gobject_class
= (GObjectClass
*) klass
;
100 widget_class
= (GtkWidgetClass
*) klass
;
102 g_type_class_add_private(klass
, sizeof(AdgWidgetPrivate
));
104 gobject_class
->dispose
= dispose
;
105 gobject_class
->get_property
= get_property
;
106 gobject_class
->set_property
= set_property
;
108 widget_class
->expose_event
= expose_event
;
109 widget_class
->scroll_event
= scroll_event
;
110 widget_class
->button_press_event
= button_press_event
;
111 widget_class
->motion_notify_event
= motion_notify_event
;
113 param
= g_param_spec_object("canvas",
115 P_("The canvas to be shown by this widget"),
117 G_PARAM_CONSTRUCT
|G_PARAM_READWRITE
);
118 g_object_class_install_property(gobject_class
, PROP_CANVAS
, param
);
120 param
= g_param_spec_double("factor",
122 P_("The factor used in zooming in and out"),
123 1., G_MAXDOUBLE
, 1.05,
124 G_PARAM_CONSTRUCT
|G_PARAM_READWRITE
);
125 g_object_class_install_property(gobject_class
, PROP_FACTOR
, param
);
128 * AdgWidget::canvas-changed:
129 * @widget: an #AdgWidget
131 * Emitted when the widget has a new canvas.
133 signals
[CANVAS_CHANGED
] = g_signal_new("canvas-changed", ADG_TYPE_WIDGET
,
134 G_SIGNAL_RUN_LAST
|G_SIGNAL_NO_RECURSE
,
135 G_STRUCT_OFFSET(AdgWidgetClass
, canvas_changed
),
137 g_cclosure_marshal_VOID__VOID
,
142 adg_widget_init(AdgWidget
*widget
)
144 AdgWidgetPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(widget
,
155 /* Enable GDK events to catch wheel rotation and drag */
156 gtk_widget_add_events((GtkWidget
*) widget
,
157 GDK_BUTTON2_MOTION_MASK
|GDK_SCROLL_MASK
);
161 dispose(GObject
*object
)
163 AdgWidgetPrivate
*data
= ((AdgWidget
*) object
)->data
;
165 if (data
->canvas
!= NULL
) {
166 g_object_unref(data
->canvas
);
170 G_OBJECT_CLASS(adg_widget_parent_class
)->dispose(object
);
174 get_property(GObject
*object
, guint prop_id
, GValue
*value
, GParamSpec
*pspec
)
176 AdgWidgetPrivate
*data
= ((AdgWidget
*) object
)->data
;
181 g_value_set_object(value
, data
->canvas
);
185 g_value_set_double(value
, data
->factor
);
189 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
195 set_property(GObject
*object
,
196 guint prop_id
, const GValue
*value
, GParamSpec
*pspec
)
199 AdgWidgetPrivate
*data
;
201 widget
= (AdgWidget
*) object
;
207 set_canvas(widget
, g_value_get_object(value
));
211 data
->factor
= g_value_get_double(value
);
215 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
223 * @path: the #AdgPath to stroke
225 * Creates a new #AdgWidget.
227 * Return value: the newly created widget
230 adg_widget_new(AdgCanvas
*canvas
)
232 g_return_val_if_fail(ADG_IS_CANVAS(canvas
), NULL
);
234 return (GtkWidget
*) g_object_new(ADG_TYPE_WIDGET
, "canvas", canvas
, NULL
);
239 * adg_widget_get_canvas:
240 * @widget: an #AdgWidget
242 * Gets the canvas associated to @widget.
244 * Return value: the requested #AdgCanvas object or %NULL on errors
247 adg_widget_get_canvas(AdgWidget
*widget
)
249 AdgWidgetPrivate
*data
;
251 g_return_val_if_fail(ADG_IS_WIDGET(widget
), NULL
);
259 * adg_widget_set_canvas:
260 * @widget: an #AdgWidget
261 * @canvas: the new #AdgCanvas
263 * Sets a new canvas on @widget. The old canvas, if presents, is
267 adg_widget_set_canvas(AdgWidget
*widget
, AdgCanvas
*canvas
)
269 g_return_if_fail(ADG_IS_WIDGET(widget
));
271 set_canvas(widget
, canvas
);
273 g_object_notify((GObject
*) widget
, "canvas");
277 * adg_widget_get_factor:
278 * @widget: an #AdgWidget
280 * Gets the zoom factor associated to @widget. The zoom factor is
281 * directly used to zoom in (that is, the default zoom factor of
282 * 1.05 will zoom of 5% every iteration) and it is reversed while
283 * zooming out (that is, the default factor will use 1/1.05).
285 * Return value: the requested zoom factor or 0 on error
288 adg_widget_get_factor(AdgWidget
*widget
)
290 AdgWidgetPrivate
*data
;
292 g_return_val_if_fail(ADG_IS_WIDGET(widget
), 0.);
300 * adg_widget_set_factor:
301 * @widget: an #AdgWidget
302 * @factor: the new zoom factor
304 * Sets a new zoom factor to @widget. If the factor is less than
305 * 1, it will be clamped to 1.
308 adg_widget_set_factor(AdgWidget
*widget
, gdouble factor
)
310 AdgWidgetPrivate
*data
;
312 g_return_if_fail(ADG_IS_WIDGET(widget
));
315 data
->factor
= CLAMP(factor
, 1., G_MAXDOUBLE
);
317 g_object_notify((GObject
*) widget
, "factor");
322 set_canvas(AdgWidget
*widget
, AdgCanvas
*canvas
)
324 AdgWidgetPrivate
*data
= widget
->data
;
326 if (data
->canvas
!= NULL
)
327 g_object_unref(data
->canvas
);
329 data
->canvas
= canvas
;
332 g_object_ref(data
->canvas
);
334 g_signal_emit(widget
, signals
[CANVAS_CHANGED
], 0);
338 expose_event(GtkWidget
*widget
, GdkEventExpose
*event
)
340 AdgWidgetPrivate
*data
;
342 GtkWidgetClass
*parent_class
;
344 data
= ((AdgWidget
*) widget
)->data
;
345 canvas
= data
->canvas
;
346 parent_class
= (GtkWidgetClass
*) adg_widget_parent_class
;
348 if (canvas
!= NULL
) {
349 cairo_t
*cr
= gdk_cairo_create(widget
->window
);
350 adg_entity_render((AdgEntity
*) canvas
, cr
);
354 if (parent_class
->expose_event
== NULL
)
357 return parent_class
->expose_event(widget
, event
);
361 scroll_event(GtkWidget
*widget
, GdkEventScroll
*event
)
363 GtkWidgetClass
*parent_class
;
364 AdgWidgetPrivate
*data
;
365 AdgMatrix model
, inverted
;
367 parent_class
= (GtkWidgetClass
*) adg_widget_parent_class
;
368 data
= ((AdgWidget
*) widget
)->data
;
370 if ((event
->direction
== GDK_SCROLL_UP
||
371 event
->direction
== GDK_SCROLL_DOWN
) &&
372 get_transformation(widget
, &model
, &inverted
)) {
375 if (event
->direction
== GDK_SCROLL_UP
) {
376 factor
= data
->factor
;
378 factor
= 1. / data
->factor
;
384 cairo_matrix_transform_point(&inverted
, &x
, &y
);
386 cairo_matrix_scale(&model
, factor
, factor
);
387 cairo_matrix_translate(&model
, x
/factor
- x
, y
/factor
- y
);
389 set_transformation(widget
, &model
);
391 gtk_widget_queue_draw(widget
);
394 if (parent_class
->scroll_event
== NULL
)
397 return parent_class
->scroll_event(widget
, event
);
401 button_press_event(GtkWidget
*widget
, GdkEventButton
*event
)
403 GtkWidgetClass
*parent_class
;
404 AdgWidgetPrivate
*data
;
406 parent_class
= (GtkWidgetClass
*) adg_widget_parent_class
;
407 data
= ((AdgWidget
*) widget
)->data
;
409 if (event
->type
== GDK_BUTTON_PRESS
&& event
->button
== 2) {
410 data
->x_event
= event
->x
;
411 data
->y_event
= event
->y
;
414 if (parent_class
->button_press_event
== NULL
)
417 return parent_class
->button_press_event(widget
, event
);
421 motion_notify_event(GtkWidget
*widget
, GdkEventMotion
*event
)
423 GtkWidgetClass
*parent_class
;
424 AdgWidgetPrivate
*data
;
425 AdgMatrix model
, inverted
;
427 parent_class
= (GtkWidgetClass
*) adg_widget_parent_class
;
428 data
= ((AdgWidget
*) widget
)->data
;
430 if ((event
->state
& GDK_BUTTON2_MASK
) > 0 &&
431 get_transformation(widget
, &model
, &inverted
)) {
434 x
= event
->x
- data
->x_event
;
435 y
= event
->y
- data
->y_event
;
437 cairo_matrix_transform_distance(&inverted
, &x
, &y
);
438 cairo_matrix_translate(&model
, x
, y
);
439 data
->x_event
= event
->x
;
440 data
->y_event
= event
->y
;
442 set_transformation(widget
, &model
);
444 gtk_widget_queue_draw(widget
);
447 if (parent_class
->motion_notify_event
== NULL
)
450 return parent_class
->motion_notify_event(widget
, event
);
454 get_transformation(GtkWidget
*widget
, AdgMatrix
*model
, AdgMatrix
*inverted
)
456 AdgWidgetPrivate
*data
;
458 const AdgMatrix
*src
;
460 data
= ((AdgWidget
*) widget
)->data
;
461 canvas
= data
->canvas
;
465 src
= adg_container_get_model_transformation((AdgContainer
*) canvas
);
467 adg_matrix_copy(model
, src
);
468 adg_matrix_copy(inverted
, src
);
469 return cairo_matrix_invert(inverted
) == CAIRO_STATUS_SUCCESS
;
473 set_transformation(GtkWidget
*widget
, const AdgMatrix
*model
)
475 AdgWidgetPrivate
*data
;
478 data
= ((AdgWidget
*) widget
)->data
;
479 canvas
= data
->canvas
;
482 adg_container_set_model_transformation((AdgContainer
*) canvas
, model
);