[AdgPositionable] Implemented adg_positionable_set_org_explicit()
[adg.git] / adg / adg-positionable.c
blob4b1596d14d56745d0cfa0a091727d43a80933ee9
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2008, 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:positionable
23 * @title: AdgPositionable
24 * @short_description: Interface for positionable entities
26 * The #AdgPositionableIface interface gives a common way to manage entities
27 * with an origin point.
28 **/
30 /**
31 * AdgPositionableIface:
32 * @base_iface: the base interface
33 * @org: the origin point accessor
35 * The virtual methods @org must be defined by all the types which
36 * implement this interface.
37 **/
39 #include "adg-positionable.h"
40 #include "adg-intl.h"
43 enum {
44 ORG_MOVED,
45 LAST_SIGNAL
48 static void iface_base (AdgPositionableIface *iface);
49 static void iface_init (AdgPositionableIface *iface);
50 static AdgPoint *org (AdgPositionable *positionable);
53 static guint signals[LAST_SIGNAL] = { 0 };
56 GType
57 adg_positionable_get_type(void)
59 static GType positionable_type = 0;
61 if (G_UNLIKELY(positionable_type == 0)) {
62 static const GTypeInfo positionable_info = {
63 sizeof(AdgPositionableIface),
64 (GBaseInitFunc) iface_base,
65 NULL,
66 (GClassInitFunc) iface_init,
67 NULL,
70 positionable_type = g_type_register_static(G_TYPE_INTERFACE,
71 "AdgPositionable",
72 &positionable_info, 0);
75 return positionable_type;
78 static void
79 iface_base(AdgPositionableIface *iface)
81 static gboolean initialized = FALSE;
82 GParamSpec *param;
83 GType param_types[1];
85 if (initialized) {
86 return;
88 initialized = TRUE;
90 param = g_param_spec_object("org",
91 P_("Org"),
92 P_("Origin point"),
93 ADG_TYPE_POINT,
94 G_PARAM_READWRITE);
95 g_object_interface_install_property(iface, param);
97 /**
98 * AdgPositionable::org-moved:
99 * @positionable: an entity implementing #AdgPositionable
101 * Emitted whenever the origin has changed.
103 param_types[0] = ADG_TYPE_POINT;
104 signals[ORG_MOVED] = g_signal_newv("org-moved", ADG_TYPE_POSITIONABLE,
105 G_SIGNAL_RUN_FIRST,
106 NULL, NULL, NULL,
107 g_cclosure_marshal_VOID__OBJECT,
108 G_TYPE_NONE, 1, param_types);
111 static void
112 iface_init (AdgPositionableIface *iface)
114 iface->org = org;
118 static AdgPoint *
119 org(AdgPositionable *positionable)
121 g_warning("AdgPositionable::org not implemented for `%s'",
122 g_type_name(G_TYPE_FROM_INSTANCE(positionable)));
123 return NULL;
127 * adg_positionable_get_org:
128 * @positionable: an entity implementing AdgPositionable
130 * Gets the origin point of @positionable.
132 * Return value: A pointer to the internal origin point
134 const AdgPoint *
135 adg_positionable_get_org(AdgPositionable *positionable)
137 g_return_val_if_fail(ADG_IS_POSITIONABLE(positionable), NULL);
139 return ADG_POSITIONABLE_GET_IFACE(positionable)->org(positionable);
143 * adg_positionable_set_org:
144 * @positionable: an entity implementing AdgPositionable
145 * @org: the new origin
147 * Sets the origin of @positionable to @org. An "org-moved" signal is emitted.
149 void
150 adg_positionable_set_org(AdgPositionable *positionable, const AdgPoint *org)
152 AdgPoint *current_org;
153 AdgPoint old_org;
155 g_return_if_fail(ADG_IS_POSITIONABLE(positionable));
157 current_org = ADG_POSITIONABLE_GET_IFACE(positionable)->org(positionable);
159 adg_point_copy(current_org, &old_org);
160 adg_point_copy(org, current_org);
161 g_signal_emit(positionable, signals[ORG_MOVED], 0, &old_org);
165 * adg_positionable_set_org_explicit:
166 * @positionable: an entity implementing AdgPositionable
167 * @model_x: the new x position in model space
168 * @model_y: the new y position in model space
169 * @paper_x: the new x position in paper space
170 * @paper_y: the new y position in paper space
172 * Sets the origin of @positionable to the new coordinates. It calls
173 * adg_positionable_set_org() internally.
175 void
176 adg_positionable_set_org_explicit(AdgPositionable *positionable,
177 gdouble model_x, gdouble model_y,
178 gdouble paper_x, gdouble paper_y)
180 AdgPoint org;
182 org.model.x = model_x;
183 org.model.y = model_y;
184 org.paper.x = paper_x;
185 org.paper.y = paper_y;
187 adg_positionable_set_org(positionable, &org);