~ubuntu-branches/ubuntu/karmic/mergeant/karmic

« back to all changes in this revision

Viewing changes to libmergeant/graph/mg-graph-item.c

  • Committer: Bazaar Package Importer
  • Author(s): Gustavo R. Montesino
  • Date: 2007-11-29 08:44:48 UTC
  • mfrom: (2.1.4 hardy)
  • Revision ID: james.westby@ubuntu.com-20071129084448-6aon73d22bv6hzfw
Tags: 0.67-3
* Re-enable installation of the mime files in mergeant.install
* mergeant.dirs: create usr/share/mime/packages to make dh_installmime add
  the update-mime-database code snippets

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* mg-graph-item.c
2
 
 *
3
 
 * Copyright (C) 2004 Vivien Malerba
4
 
 *
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.
9
 
 *
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.
14
 
 *
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
18
 
 * USA
19
 
 */
20
 
 
21
 
#include <string.h>
22
 
#include "mg-graph-item.h"
23
 
#include <libmergeant/mg-ref-base.h>
24
 
#include <libmergeant/mg-xml-storage.h>
25
 
#include "marshal.h"
26
 
 
27
 
/* 
28
 
 * Main static functions 
29
 
 */
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);
34
 
 
35
 
static void mg_graph_item_set_property (GObject              *object,
36
 
                                        guint                 param_id,
37
 
                                        const GValue         *value,
38
 
                                        GParamSpec           *pspec);
39
 
static void mg_graph_item_get_property (GObject              *object,
40
 
                                        guint                 param_id,
41
 
                                        GValue               *value,
42
 
                                        GParamSpec           *pspec);
43
 
 
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);
48
 
 
49
 
 
50
 
#ifdef debug
51
 
static void        mg_graph_item_dump                (MgGraphItem *graph, guint offset);
52
 
#endif
53
 
 
54
 
/* get a pointer to the parents to be able to call their destructor */
55
 
static GObjectClass  *parent_class = NULL;
56
 
 
57
 
/* signals */
58
 
enum
59
 
{
60
 
        MOVED,
61
 
        LAST_SIGNAL
62
 
};
63
 
 
64
 
static gint mg_graph_item_signals[LAST_SIGNAL] = { 0 };
65
 
 
66
 
/* properties */
67
 
enum
68
 
{
69
 
        PROP_0,
70
 
        PROP_REF_OBJECT
71
 
};
72
 
 
73
 
 
74
 
struct _MgGraphItemPrivate
75
 
{
76
 
        MgRefBase    *ref_object;
77
 
        gdouble       x;
78
 
        gdouble       y;
79
 
};
80
 
 
81
 
/* module error */
82
 
GQuark mg_graph_item_error_quark (void)
83
 
{
84
 
        static GQuark quark;
85
 
        if (!quark)
86
 
                quark = g_quark_from_static_string ("mg_graph_item_error");
87
 
        return quark;
88
 
}
89
 
 
90
 
 
91
 
guint
92
 
mg_graph_item_get_type (void)
93
 
{
94
 
        static GType type = 0;
95
 
 
96
 
        if (!type) {
97
 
                static const GTypeInfo info = {
98
 
                        sizeof (MgGraphItemClass),
99
 
                        (GBaseInitFunc) NULL,
100
 
                        (GBaseFinalizeFunc) NULL,
101
 
                        (GClassInitFunc) mg_graph_item_class_init,
102
 
                        NULL,
103
 
                        NULL,
104
 
                        sizeof (MgGraphItem),
105
 
                        0,
106
 
                        (GInstanceInitFunc) mg_graph_item_init
107
 
                };
108
 
                
109
 
                static const GInterfaceInfo xml_storage_info = {
110
 
                        (GInterfaceInitFunc) mg_graph_item_xml_storage_init,
111
 
                        NULL,
112
 
                        NULL
113
 
                };
114
 
 
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);
117
 
        }
118
 
        return type;
119
 
}
120
 
 
121
 
static void
122
 
mg_graph_item_xml_storage_init (MgXmlStorageIface *iface)
123
 
{
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;
127
 
}
128
 
 
129
 
 
130
 
static void
131
 
mg_graph_item_class_init (MgGraphItemClass * class)
132
 
{
133
 
        GObjectClass   *object_class = G_OBJECT_CLASS (class);
134
 
 
135
 
        parent_class = g_type_class_peek_parent (class);
136
 
        
137
 
        mg_graph_item_signals[MOVED] =
138
 
                g_signal_new ("moved",
139
 
                              G_TYPE_FROM_CLASS (object_class),
140
 
                              G_SIGNAL_RUN_FIRST,
141
 
                              G_STRUCT_OFFSET (MgGraphItemClass, moved),
142
 
                              NULL, NULL,
143
 
                              marshal_VOID__VOID, G_TYPE_NONE, 0);
144
 
 
145
 
        object_class->dispose = mg_graph_item_dispose;
146
 
        object_class->finalize = mg_graph_item_finalize;
147
 
 
148
 
        /* Properties */
149
 
        object_class->set_property = mg_graph_item_set_property;
150
 
        object_class->get_property = mg_graph_item_get_property;
151
 
 
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)));
155
 
 
156
 
        /* virtual functions */
157
 
#ifdef debug
158
 
        MG_BASE_CLASS (class)->dump = (void (*)(MgBase *, guint)) mg_graph_item_dump;
159
 
#endif
160
 
}
161
 
 
162
 
static void
163
 
mg_graph_item_init (MgGraphItem *graph)
164
 
{
165
 
        graph->priv = g_new0 (MgGraphItemPrivate, 1);
166
 
        graph->priv->ref_object = NULL;
167
 
        graph->priv->x = 0.;
168
 
        graph->priv->y = 0.;
169
 
}
170
 
 
171
 
/**
172
 
 * mg_graph_item_new
173
 
 * @conf: a #MgConf object
174
 
 * @ref_obj: a #MgBase object which the new item will reference, or %NULL.
175
 
 *
176
 
 * Creates a new #MgGraphItem object
177
 
 *
178
 
 * Returns: the newly created object
179
 
 */
180
 
GObject   *
181
 
mg_graph_item_new (MgConf *conf, MgBase *ref_obj)
182
 
{
183
 
        GObject *obj = NULL;
184
 
        MgGraphItem *item;
185
 
 
186
 
        g_return_val_if_fail (conf && IS_MG_CONF (conf), NULL);
187
 
        if (ref_obj)
188
 
                g_return_val_if_fail (IS_MG_BASE (ref_obj), NULL);
189
 
 
190
 
        obj = g_object_new (MG_GRAPH_ITEM_TYPE, "conf", conf, NULL);
191
 
        item = MG_GRAPH_ITEM (obj);
192
 
 
193
 
        item->priv->ref_object = MG_REF_BASE (mg_ref_base_new (conf));
194
 
        if (ref_obj)
195
 
                mg_ref_base_set_ref_object (item->priv->ref_object, ref_obj);
196
 
 
197
 
        return obj;
198
 
}
199
 
 
200
 
static void
201
 
mg_graph_item_dispose (GObject *object)
202
 
{
203
 
        MgGraphItem *graph;
204
 
 
205
 
        g_return_if_fail (object != NULL);
206
 
        g_return_if_fail (IS_MG_GRAPH_ITEM (object));
207
 
 
208
 
        graph = MG_GRAPH_ITEM (object);
209
 
        if (graph->priv) {              
210
 
                if (graph->priv->ref_object) {
211
 
                        g_object_unref (G_OBJECT (graph->priv->ref_object));
212
 
                        graph->priv->ref_object = NULL;
213
 
                }
214
 
        }
215
 
 
216
 
        /* parent class */
217
 
        parent_class->dispose (object);
218
 
}
219
 
 
220
 
 
221
 
static void
222
 
mg_graph_item_finalize (GObject   * object)
223
 
{
224
 
        MgGraphItem *graph;
225
 
 
226
 
        g_return_if_fail (object != NULL);
227
 
        g_return_if_fail (IS_MG_GRAPH_ITEM (object));
228
 
 
229
 
        graph = MG_GRAPH_ITEM (object);
230
 
        if (graph->priv) {
231
 
                g_free (graph->priv);
232
 
                graph->priv = NULL;
233
 
        }
234
 
 
235
 
        /* parent class */
236
 
        parent_class->finalize (object);
237
 
}
238
 
 
239
 
 
240
 
static void 
241
 
mg_graph_item_set_property (GObject              *object,
242
 
                           guint                 param_id,
243
 
                           const GValue         *value,
244
 
                           GParamSpec           *pspec)
245
 
{
246
 
        MgGraphItem *graph;
247
 
        gpointer ptr;
248
 
 
249
 
        graph = MG_GRAPH_ITEM (object);
250
 
        if (graph->priv) {
251
 
                switch (param_id) {
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);
257
 
                        }
258
 
                        break;
259
 
                }
260
 
        }
261
 
}
262
 
 
263
 
static void
264
 
mg_graph_item_get_property (GObject              *object,
265
 
                           guint                 param_id,
266
 
                           GValue               *value,
267
 
                           GParamSpec           *pspec)
268
 
{
269
 
        MgGraphItem *graph;
270
 
 
271
 
        graph = MG_GRAPH_ITEM (object);
272
 
        if (graph->priv) {
273
 
                switch (param_id) {
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));
278
 
                        break;
279
 
                }
280
 
        }
281
 
}
282
 
 
283
 
/**
284
 
 * mg_graph_item_set_position
285
 
 * @item: a #MgGraphItemItem object
286
 
 * @x:
287
 
 * @y:
288
 
 *
289
 
 * Sets the position to be remembered for @item.
290
 
 */
291
 
void
292
 
mg_graph_item_set_position (MgGraphItem *item, gdouble x, gdouble y)
293
 
{
294
 
        g_return_if_fail (item && IS_MG_GRAPH_ITEM (item));
295
 
        g_return_if_fail (item->priv);
296
 
        
297
 
        if ((item->priv->x == x) && (item->priv->y == y))
298
 
                return;
299
 
 
300
 
        item->priv->x = x;
301
 
        item->priv->y = y;
302
 
        
303
 
#ifdef debug_signal
304
 
        g_print (">> 'MOVED' from %s::%s()\n", __FILE__, __FUNCTION__);
305
 
#endif
306
 
        g_signal_emit (G_OBJECT (item), mg_graph_item_signals[MOVED], 0);
307
 
#ifdef debug_signal
308
 
        g_print ("<< 'MOVED' from %s::%s()\n", __FILE__, __FUNCTION__);
309
 
#endif
310
 
}
311
 
 
312
 
/**
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
317
 
 *
318
 
 * Get @item's position.
319
 
 */
320
 
void
321
 
mg_graph_item_get_position (MgGraphItem *item, gdouble *x, gdouble *y)
322
 
{
323
 
        g_return_if_fail (item && IS_MG_GRAPH_ITEM (item));
324
 
        g_return_if_fail (item->priv);
325
 
        
326
 
        if (x)
327
 
                *x = item->priv->x;
328
 
        if (y)
329
 
                *y = item->priv->y;
330
 
}
331
 
 
332
 
 
333
 
/**
334
 
 * mg_graph_item_get_ref_object
335
 
 * @item: a #MgGraphItem object
336
 
 *
337
 
 * Get the referenced #MgBase object, if it exists.
338
 
 *
339
 
 * Returns: the referenced object, or %NULL
340
 
 */
341
 
MgBase *
342
 
mg_graph_item_get_ref_object (MgGraphItem *item)
343
 
{
344
 
        g_return_val_if_fail (item && IS_MG_GRAPH_ITEM (item), NULL);
345
 
        g_return_val_if_fail (item->priv, NULL);
346
 
        
347
 
        return mg_ref_base_get_ref_object (item->priv->ref_object);
348
 
}
349
 
 
350
 
#ifdef debug
351
 
static void
352
 
mg_graph_item_dump (MgGraphItem *item, guint offset)
353
 
{
354
 
        gchar *str;
355
 
        gint i;
356
 
 
357
 
        g_return_if_fail (item && IS_MG_GRAPH_ITEM (item));
358
 
        
359
 
        /* string for the offset */
360
 
        str = g_new0 (gchar, offset+1);
361
 
        for (i=0; i<offset; i++)
362
 
                str[i] = ' ';
363
 
        str[offset] = 0;
364
 
 
365
 
        /* dump */
366
 
        if (item->priv) {
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);
370
 
                
371
 
        }
372
 
        else
373
 
                g_print ("%s" D_COL_ERR "Using finalized object %p" D_COL_NOR, str, item);
374
 
}
375
 
#endif
376
 
 
377
 
/* 
378
 
 * MgXmlStorage interface implementation
379
 
 */
380
 
static xmlNodePtr
381
 
mg_graph_item_save_to_xml (MgXmlStorage *iface, GError **error)
382
 
{
383
 
        xmlNodePtr node = NULL;
384
 
        MgGraphItem *item;
385
 
        gchar *str;
386
 
        MgBase *base;
387
 
 
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);
390
 
 
391
 
        item = MG_GRAPH_ITEM (iface);
392
 
 
393
 
        node = xmlNewNode (NULL, "MG_GRAPH_ITEM");
394
 
 
395
 
        g_assert (item->priv->ref_object);
396
 
        base = mg_ref_base_get_ref_object (item->priv->ref_object);
397
 
        if (base) {
398
 
                str = mg_xml_storage_get_xml_id (MG_XML_STORAGE (base));
399
 
                xmlSetProp (node, "object", str);
400
 
                g_free (str);
401
 
        }
402
 
        
403
 
        str = g_strdup_printf ("%.3f", item->priv->x);
404
 
        xmlSetProp (node, "xpos", str);
405
 
        g_free (str);
406
 
 
407
 
        str = g_strdup_printf ("%.3f", item->priv->y);
408
 
        xmlSetProp (node, "ypos", str);
409
 
        g_free (str);
410
 
 
411
 
        return node;
412
 
}
413
 
 
414
 
static gboolean
415
 
mg_graph_item_load_from_xml (MgXmlStorage *iface, xmlNodePtr node, GError **error)
416
 
{
417
 
        MgGraphItem *item;
418
 
        gchar *prop;
419
 
 
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);
423
 
 
424
 
        item = MG_GRAPH_ITEM (iface);
425
 
 
426
 
        if (strcmp (node->name, "MG_GRAPH_ITEM")) {
427
 
                g_set_error (error,
428
 
                             MG_GRAPH_ITEM_ERROR,
429
 
                             MG_GRAPH_ITEM_XML_LOAD_ERROR,
430
 
                             _("XML Tag is not <MG_GRAPH_ITEM>"));
431
 
                return FALSE;
432
 
        }
433
 
 
434
 
        prop = xmlGetProp (node, "object");
435
 
        if (prop) {
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);
438
 
                g_free (prop);
439
 
        }
440
 
 
441
 
        prop = xmlGetProp (node, "xpos");
442
 
        if (prop) {
443
 
                item->priv->x = atof (prop);
444
 
                g_free (prop);
445
 
        }
446
 
 
447
 
        prop = xmlGetProp (node, "ypos");
448
 
        if (prop) {
449
 
                item->priv->y = atof (prop);
450
 
                g_free (prop);
451
 
        }
452
 
 
453
 
        return TRUE;
454
 
}
455