Finalized renaming of AdgPositionable to AdgTranslatable
[adg.git] / adg / adg-translatable.c
blob0f85c058f533c340827cb53c5cd807e2fdb3a307
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.
21 /**
22 * SECTION:translatable
23 * @title: AdgTranslatable
24 * @short_description: Interface for translatable entities
26 * The #AdgTranslatableIface interface gives a common way to manage entities
27 * with an origin point.
28 **/
30 /**
31 * AdgTranslatableIface:
32 * @base_iface: the base interface
33 * @get_origin: returns the current origin
34 * @set_origin: sets a new origin
36 * The virtual methods @get_origin and @set_origin must be defined
37 * by all the types which implement this interface.
38 **/
40 #include "adg-translatable.h"
41 #include "adg-intl.h"
44 enum {
45 ORIGIN_MOVED,
46 LAST_SIGNAL
49 static void iface_base (AdgTranslatableIface *iface);
50 static void iface_init (AdgTranslatableIface *iface);
51 static void get_origin (AdgTranslatable *translatable,
52 AdgPoint *dest);
53 static void set_origin (AdgTranslatable *translatable,
54 const AdgPoint *origin);
56 static guint signals[LAST_SIGNAL] = { 0 };
59 GType
60 adg_translatable_get_type(void)
62 static GType translatable_type = 0;
64 if (G_UNLIKELY(translatable_type == 0)) {
65 static const GTypeInfo translatable_info = {
66 sizeof(AdgTranslatableIface),
67 (GBaseInitFunc) iface_base,
68 NULL,
69 (GClassInitFunc) iface_init,
70 NULL,
73 translatable_type = g_type_register_static(G_TYPE_INTERFACE,
74 "AdgTranslatable",
75 &translatable_info, 0);
78 return translatable_type;
81 static void
82 iface_base(AdgTranslatableIface *iface)
84 static gboolean initialized = FALSE;
85 GParamSpec *param;
86 GType param_types[1];
88 if (initialized) {
89 return;
91 initialized = TRUE;
93 param = g_param_spec_object("origin",
94 P_("Origin"),
95 P_("Origin point"),
96 ADG_TYPE_POINT,
97 G_PARAM_READWRITE);
98 g_object_interface_install_property(iface, param);
101 * AdgTranslatable::origin-moved:
102 * @translatable: an entity implementing #AdgTranslatable
103 * @old_origin: an #AdgPoint with the previous origin
105 * Emitted whenever the origin has changed.
107 param_types[0] = ADG_TYPE_POINT;
108 signals[ORIGIN_MOVED] = g_signal_newv("origin-moved", ADG_TYPE_TRANSLATABLE,
109 G_SIGNAL_RUN_FIRST,
110 NULL, NULL, NULL,
111 g_cclosure_marshal_VOID__OBJECT,
112 G_TYPE_NONE, 1, param_types);
115 static void
116 iface_init (AdgTranslatableIface *iface)
118 iface->get_origin = get_origin;
119 iface->set_origin = set_origin;
123 static void
124 get_origin(AdgTranslatable *translatable, AdgPoint *dest)
126 g_warning("AdgTranslatable::get_origin not implemented for `%s'",
127 g_type_name(G_TYPE_FROM_INSTANCE(translatable)));
130 static void
131 set_origin(AdgTranslatable *translatable, const AdgPoint *point)
133 g_warning("AdgTranslatable::set_origin not implemented for `%s'",
134 g_type_name(G_TYPE_FROM_INSTANCE(translatable)));
138 * adg_translatable_get_origin:
139 * @translatable: an entity implementing AdgTranslatable
140 * @dest: the destination #AdgPoint struct
142 * Gets the origin point of @translatable.
144 void
145 adg_translatable_get_origin(AdgTranslatable *translatable, AdgPoint *dest)
147 g_return_if_fail(ADG_IS_TRANSLATABLE(translatable));
148 g_return_if_fail(dest != NULL);
150 ADG_TRANSLATABLE_GET_IFACE(translatable)->get_origin(translatable, dest);
154 * adg_translatable_set_origin:
155 * @translatable: an entity implementing AdgTranslatable
156 * @origin: the new origin
158 * Sets the origin of @translatable to @origin.
159 * An "origin-moved" signal is emitted.
161 void
162 adg_translatable_set_origin(AdgTranslatable *translatable,
163 const AdgPoint *origin)
165 AdgTranslatableIface *iface;
166 AdgPoint old_origin;
168 g_return_if_fail(ADG_IS_TRANSLATABLE(translatable));
169 g_return_if_fail(origin != NULL);
171 iface = ADG_TRANSLATABLE_GET_IFACE(translatable);
173 iface->get_origin(translatable, &old_origin);
174 iface->set_origin(translatable, origin);
176 g_signal_emit(translatable, signals[ORIGIN_MOVED], 0, &old_origin);
180 * adg_translatable_set_origin_explicit:
181 * @translatable: an entity implementing AdgTranslatable
182 * @model_x: the new x position in model space
183 * @model_y: the new y position in model space
184 * @paper_x: the new x position in paper space
185 * @paper_y: the new y position in paper space
187 * Sets the origin of @translatable to the new coordinates. It calls
188 * adg_translatable_set_origin() internally.
190 void
191 adg_translatable_set_origin_explicit(AdgTranslatable *translatable,
192 gdouble model_x, gdouble model_y,
193 gdouble paper_x, gdouble paper_y)
195 AdgPoint origin;
197 origin.model.x = model_x;
198 origin.model.y = model_y;
199 origin.paper.x = paper_x;
200 origin.paper.y = paper_y;
202 adg_translatable_set_origin(translatable, &origin);