3
* Copyright (C) 2004 Vivien Malerba
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License as
7
* published by the Free Software Foundation; either version 2 of the
8
* License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22
#include "mg-graph-item.h"
23
#include <libmergeant/mg-ref-base.h>
24
#include <libmergeant/mg-xml-storage.h>
28
* Main static functions
30
static void mg_graph_item_class_init (MgGraphItemClass *class);
31
static void mg_graph_item_init (MgGraphItem *graph);
32
static void mg_graph_item_dispose (GObject *object);
33
static void mg_graph_item_finalize (GObject *object);
35
static void mg_graph_item_set_property (GObject *object,
39
static void mg_graph_item_get_property (GObject *object,
44
/* XML storage interface */
45
static void mg_graph_item_xml_storage_init (MgXmlStorageIface *iface);
46
static xmlNodePtr mg_graph_item_save_to_xml (MgXmlStorage *iface, GError **error);
47
static gboolean mg_graph_item_load_from_xml (MgXmlStorage *iface, xmlNodePtr node, GError **error);
51
static void mg_graph_item_dump (MgGraphItem *graph, guint offset);
54
/* get a pointer to the parents to be able to call their destructor */
55
static GObjectClass *parent_class = NULL;
64
static gint mg_graph_item_signals[LAST_SIGNAL] = { 0 };
74
struct _MgGraphItemPrivate
76
MgRefBase *ref_object;
82
GQuark mg_graph_item_error_quark (void)
86
quark = g_quark_from_static_string ("mg_graph_item_error");
92
mg_graph_item_get_type (void)
94
static GType type = 0;
97
static const GTypeInfo info = {
98
sizeof (MgGraphItemClass),
100
(GBaseFinalizeFunc) NULL,
101
(GClassInitFunc) mg_graph_item_class_init,
104
sizeof (MgGraphItem),
106
(GInstanceInitFunc) mg_graph_item_init
109
static const GInterfaceInfo xml_storage_info = {
110
(GInterfaceInitFunc) mg_graph_item_xml_storage_init,
115
type = g_type_register_static (MG_BASE_TYPE, "MgGraphItem", &info, 0);
116
g_type_add_interface_static (type, MG_XML_STORAGE_TYPE, &xml_storage_info);
122
mg_graph_item_xml_storage_init (MgXmlStorageIface *iface)
124
iface->get_xml_id = NULL;
125
iface->save_to_xml = mg_graph_item_save_to_xml;
126
iface->load_from_xml = mg_graph_item_load_from_xml;
131
mg_graph_item_class_init (MgGraphItemClass * class)
133
GObjectClass *object_class = G_OBJECT_CLASS (class);
135
parent_class = g_type_class_peek_parent (class);
137
mg_graph_item_signals[MOVED] =
138
g_signal_new ("moved",
139
G_TYPE_FROM_CLASS (object_class),
141
G_STRUCT_OFFSET (MgGraphItemClass, moved),
143
marshal_VOID__VOID, G_TYPE_NONE, 0);
145
object_class->dispose = mg_graph_item_dispose;
146
object_class->finalize = mg_graph_item_finalize;
149
object_class->set_property = mg_graph_item_set_property;
150
object_class->get_property = mg_graph_item_get_property;
152
g_object_class_install_property (object_class, PROP_REF_OBJECT,
153
g_param_spec_pointer ("ref_object", NULL, NULL,
154
(G_PARAM_READABLE | G_PARAM_WRITABLE)));
156
/* virtual functions */
158
MG_BASE_CLASS (class)->dump = (void (*)(MgBase *, guint)) mg_graph_item_dump;
163
mg_graph_item_init (MgGraphItem *graph)
165
graph->priv = g_new0 (MgGraphItemPrivate, 1);
166
graph->priv->ref_object = NULL;
173
* @conf: a #MgConf object
174
* @ref_obj: a #MgBase object which the new item will reference, or %NULL.
176
* Creates a new #MgGraphItem object
178
* Returns: the newly created object
181
mg_graph_item_new (MgConf *conf, MgBase *ref_obj)
186
g_return_val_if_fail (conf && IS_MG_CONF (conf), NULL);
188
g_return_val_if_fail (IS_MG_BASE (ref_obj), NULL);
190
obj = g_object_new (MG_GRAPH_ITEM_TYPE, "conf", conf, NULL);
191
item = MG_GRAPH_ITEM (obj);
193
item->priv->ref_object = MG_REF_BASE (mg_ref_base_new (conf));
195
mg_ref_base_set_ref_object (item->priv->ref_object, ref_obj);
201
mg_graph_item_dispose (GObject *object)
205
g_return_if_fail (object != NULL);
206
g_return_if_fail (IS_MG_GRAPH_ITEM (object));
208
graph = MG_GRAPH_ITEM (object);
210
if (graph->priv->ref_object) {
211
g_object_unref (G_OBJECT (graph->priv->ref_object));
212
graph->priv->ref_object = NULL;
217
parent_class->dispose (object);
222
mg_graph_item_finalize (GObject * object)
226
g_return_if_fail (object != NULL);
227
g_return_if_fail (IS_MG_GRAPH_ITEM (object));
229
graph = MG_GRAPH_ITEM (object);
231
g_free (graph->priv);
236
parent_class->finalize (object);
241
mg_graph_item_set_property (GObject *object,
249
graph = MG_GRAPH_ITEM (object);
252
case PROP_REF_OBJECT:
253
g_assert (graph->priv->ref_object);
254
if (graph->priv->ref_object) {
255
ptr = g_value_get_pointer (value);
256
mg_ref_base_set_ref_object (graph->priv->ref_object, ptr);
264
mg_graph_item_get_property (GObject *object,
271
graph = MG_GRAPH_ITEM (object);
274
case PROP_REF_OBJECT:
275
g_assert (graph->priv->ref_object);
276
g_value_set_pointer (value,
277
mg_ref_base_get_ref_object (graph->priv->ref_object));
284
* mg_graph_item_set_position
285
* @item: a #MgGraphItemItem object
289
* Sets the position to be remembered for @item.
292
mg_graph_item_set_position (MgGraphItem *item, gdouble x, gdouble y)
294
g_return_if_fail (item && IS_MG_GRAPH_ITEM (item));
295
g_return_if_fail (item->priv);
297
if ((item->priv->x == x) && (item->priv->y == y))
304
g_print (">> 'MOVED' from %s::%s()\n", __FILE__, __FUNCTION__);
306
g_signal_emit (G_OBJECT (item), mg_graph_item_signals[MOVED], 0);
308
g_print ("<< 'MOVED' from %s::%s()\n", __FILE__, __FUNCTION__);
313
* mg_graph_item_get_position
314
* @item: a #MgGraphItemItem object
315
* @x: a place where to store the X part of the position, or %NULL
316
* @y: a place where to store the Y part of the position, or %NULL
318
* Get @item's position.
321
mg_graph_item_get_position (MgGraphItem *item, gdouble *x, gdouble *y)
323
g_return_if_fail (item && IS_MG_GRAPH_ITEM (item));
324
g_return_if_fail (item->priv);
334
* mg_graph_item_get_ref_object
335
* @item: a #MgGraphItem object
337
* Get the referenced #MgBase object, if it exists.
339
* Returns: the referenced object, or %NULL
342
mg_graph_item_get_ref_object (MgGraphItem *item)
344
g_return_val_if_fail (item && IS_MG_GRAPH_ITEM (item), NULL);
345
g_return_val_if_fail (item->priv, NULL);
347
return mg_ref_base_get_ref_object (item->priv->ref_object);
352
mg_graph_item_dump (MgGraphItem *item, guint offset)
357
g_return_if_fail (item && IS_MG_GRAPH_ITEM (item));
359
/* string for the offset */
360
str = g_new0 (gchar, offset+1);
361
for (i=0; i<offset; i++)
367
MgBase *base = mg_ref_base_get_ref_object (item->priv->ref_object);
368
g_print ("%s" D_COL_H1 "MgGraphItem" D_COL_NOR " for %p at (%.3f, %.3f) ", str, base,
369
item->priv->x, item->priv->y);
373
g_print ("%s" D_COL_ERR "Using finalized object %p" D_COL_NOR, str, item);
378
* MgXmlStorage interface implementation
381
mg_graph_item_save_to_xml (MgXmlStorage *iface, GError **error)
383
xmlNodePtr node = NULL;
388
g_return_val_if_fail (iface && IS_MG_GRAPH_ITEM (iface), NULL);
389
g_return_val_if_fail (MG_GRAPH_ITEM (iface)->priv, NULL);
391
item = MG_GRAPH_ITEM (iface);
393
node = xmlNewNode (NULL, "MG_GRAPH_ITEM");
395
g_assert (item->priv->ref_object);
396
base = mg_ref_base_get_ref_object (item->priv->ref_object);
398
str = mg_xml_storage_get_xml_id (MG_XML_STORAGE (base));
399
xmlSetProp (node, "object", str);
403
str = g_strdup_printf ("%.3f", item->priv->x);
404
xmlSetProp (node, "xpos", str);
407
str = g_strdup_printf ("%.3f", item->priv->y);
408
xmlSetProp (node, "ypos", str);
415
mg_graph_item_load_from_xml (MgXmlStorage *iface, xmlNodePtr node, GError **error)
420
g_return_val_if_fail (iface && IS_MG_GRAPH_ITEM (iface), FALSE);
421
g_return_val_if_fail (MG_GRAPH_ITEM (iface)->priv, FALSE);
422
g_return_val_if_fail (node, FALSE);
424
item = MG_GRAPH_ITEM (iface);
426
if (strcmp (node->name, "MG_GRAPH_ITEM")) {
429
MG_GRAPH_ITEM_XML_LOAD_ERROR,
430
_("XML Tag is not <MG_GRAPH_ITEM>"));
434
prop = xmlGetProp (node, "object");
436
g_assert (item->priv->ref_object);
437
mg_ref_base_set_ref_name (item->priv->ref_object, 0/* FIXME */, REFERENCE_BY_XML_ID, prop);
441
prop = xmlGetProp (node, "xpos");
443
item->priv->x = atof (prop);
447
prop = xmlGetProp (node, "ypos");
449
item->priv->y = atof (prop);