gobject + diacanvas = i really please for help here



Hello

 First of all, sorry for my poor English.

 I want write an application. I try, but I create a monster not a
program. So I decide to start once again. When I was writing first
version of it, I put every object that I created to tree of widget.
Purpose for that was that: if action had place and specific widget (and
his children) was target, he was found in widget tree and after reading
associate properties with hem I know what I can do with him. And That
was Wrong way to do it. I know, I lost much time. And now I want to
really understood what magic happens under the hood.

 I want build my application over diacanvas2 lib.  I what every object 
on canvas has his own properties. Of course there can be one group of
boxes, and another group of boxes with for the program will be complete
different. I look into demo in diacanvas2 package.  There is such
function like add_box:

add_box (DiaCanvasView *view)
{
        DiaTool *tool = dia_placement_tool_new (DIA_TYPE_CANVAS_BOX,
                                                "parent",
view->canvas->root,
                                                "width", 0.0,
                                                "height", 0.0,
                                                "bg_color", 0xFF8FFF55,
                                                NULL);
        g_object_set (view, "tool", tool, NULL);

        /* First let the tool create the new item... Then unset it. */
        g_signal_connect (tool, "button_release_event",
                                G_CALLBACK (cb_unset_tool), view);

        g_object_unref (tool);
}

 Lets focus on the first function: dia_placement_tool_new. As we can see
in dia-placement-tool.c this function use
GParamSpec *pspec = g_object_class_find_property (class, name); to
search given properties and set some value. From these properties:
"parent","width", "height", "bg_color" (or fill_color) dia-canvas-box
have the last one. I have two ways and one of them is bad.
THE BAD WAY:
Copy the *-box file, add add property like gchar *name;  (that is
working of course)
The good way:
Create child of widget dia-canavas-box. And here is the first place
where I really need help.  My widget is in  attachment. But something
was wrong. When I use it to create widget in diacanvas using add_box and
DIA_TYPE_CANVAS_BOX_CHILD for first argument I have this warning:
DiaCanvas2-WARNING **: dia-placement-tool.c:319: object class
`DiaCanvasBoxChild' has no property named `fill_color', and of course he
is right he doesn't have this property his parent have.

The second place where I need some help is answer for that :

- I want to place line, which will have size (width or  height) no less
or more then canvas.
- I want (i like this word :) have lines or even some object which cant
be resized or rotated. I think it is only about blocking some signals
for that object when i created them.
- I want to (again) add every created object to list or some tree when I
put then on the canvas.

I wrote this letter because I had bed expierence creating application in
gtk without asking anybody how some things can be done in the correct
way. I had read "Glib object system" by Mathieu Lacage but I don't
understand it, i think because I only read this and never try to create
my own widgets :) So this is my first try to understood gobject system.
I hope anybody help me.

Best regards.
ps. when i have littke i will try to understand uml for diacanvas :)

-- 
Przemysław Staniszewski

/*
 * DiaCanvasBoxChild
 *
 * This is LGPL'ed code.
 */

#include "dia-canvas-box-child.h"
#include <libart_lgpl/art_affine.h>
#include "dia-canvas-i18n.h"

enum {
	PROP_NAME = 1
	/* Depricated: */
};

static void dia_canvas_box_child_class_init (DiaCanvasBoxChildClass *klass);
static void dia_canvas_box_child_init (DiaCanvasBoxChild *item);
static void dia_canvas_box_child_set_property (GObject *object,
					 guint property_id,
					 const GValue *value,
					 GParamSpec *pspec);
static void dia_canvas_box_child_get_property (GObject *object,
					 guint property_id,
					 GValue *value,
					 GParamSpec *pspec);
static void dia_canvas_box_child_dispose	(GObject *object);
static void dia_canvas_box_child_update	(DiaCanvasItem *item,
					 gdouble affine[6]);
static gboolean dia_canvas_box_child_get_shape_iter	(DiaCanvasItem *item,
						 DiaCanvasIter *iter);
static gboolean dia_canvas_box_child_shape_next	(DiaCanvasItem *item,
						 DiaCanvasIter *iter);
static DiaShape* dia_canvas_box_child_shape_value	(DiaCanvasItem *item,
						 DiaCanvasIter *iter);
static DiaCanvasElementClass *parent_class = NULL;

GType
dia_canvas_box_child_get_type (void)
{
	static GType object_type = 0;

	if (!object_type) {
		static const GTypeInfo object_info = {
			sizeof (DiaCanvasBoxChildClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) dia_canvas_box_child_class_init,
			(GClassFinalizeFunc) NULL,
			(gconstpointer) NULL, /* class_data */
			sizeof (DiaCanvasBoxChild),
			(guint16) 0, /* n_preallocs */
			(GInstanceInitFunc) dia_canvas_box_child_init,
		};

		object_type = g_type_register_static (DIA_TYPE_CANVAS_ELEMENT,
						      "DiaCanvasBoxChild",
						      &object_info, 0);
	}

	return object_type;
}


static void
dia_canvas_box_child_class_init (DiaCanvasBoxChildClass *klass)
{
	GObjectClass *object_class;
	DiaCanvasItemClass *item_class;
	
	object_class = (GObjectClass*) klass;
	item_class = DIA_CANVAS_ITEM_CLASS (klass);
	
	parent_class = g_type_class_peek_parent (klass);

	object_class->get_property = dia_canvas_box_child_get_property;
	object_class->set_property = dia_canvas_box_child_set_property;

	//dia_canvas_box_class = (DiaCanvasBoxClass *) class;

	g_object_class_install_property (object_class,
					 PROP_NAME,
					 g_param_spec_string ("name",
						_("Name"),
						_("Name for tex box"),
						NULL,
					       	G_PARAM_READWRITE));
}


static void
dia_canvas_box_child_init (DiaCanvasBoxChild *item)
{
	item->name = NULL;
}


static void
dia_canvas_box_child_set_property (GObject *object, guint property_id,
			     const GValue *value, GParamSpec *pspec)
{
	DiaCanvasBoxChild *box_child = (DiaCanvasBoxChild*) object;

	switch (property_id) {
	case PROP_NAME:
		dia_canvas_item_preserve_property (DIA_CANVAS_ITEM(box_child), "name");
		if (box_child->name) {
			g_printf("Juz jest jakas nazwa - trzeba ja zwolnic przez free");
		}
		box_child->name=g_strdup(g_value_get_string (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
dia_canvas_box_child_get_property (GObject *object, guint property_id,
			     GValue *value, GParamSpec *pspec)
{
	switch (property_id) {
	case PROP_NAME:
		g_value_set_string (value, DIA_CANVAS_BOX_CHILD (object)->name);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

/* dia-canvas-box_child.h
 * Copyright (C) 2001  Arjan Molenaar
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
/*
 * DiaCanvasBoxChild
 * ----------
 * Base class for box_child like objects, which includes basically everything
 * that's not a line.
 * BoxChilds have eight handles around them and can move, but handles can not
 * connect to other box_childs. Moving an individual handle will cause the
 * box_child to call DiaCanvasBoxChildClass::resize().
 */

#ifndef __DIA_CANVAS_BOX_CHILD_H__
#define __DIA_CANVAS_BOX_CHILD_H__

#include <diacanvas/dia-canvas-box.h>

G_BEGIN_DECLS

#define DIA_TYPE_CANVAS_BOX_CHILD        (dia_canvas_box_child_get_type ())
#define DIA_CANVAS_BOX_CHILD(obj)		(G_TYPE_CHECK_INSTANCE_CAST ((obj), DIA_TYPE_CANVAS_BOX_CHILD, DiaCanvasBoxChild))
#define DIA_CANVAS_BOX_CHILD_CLASS(klass)	(G_TYPE_CHECK_CLASS_CAST ((klass), DIA_TYPE_CANVAS_BOX_CHILD, DiaCanvasBoxChildClass))
#define DIA_IS_CANVAS_BOX_CHILD(obj)		(G_TYPE_CHECK_INSTANCE_TYPE ((obj), DIA_TYPE_CANVAS_BOX_CHILD))
#define DIA_IS_CANVAS_BOX_CHILD_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE ((klass), DIA_TYPE_CANVAS_BOX_CHILD))
#define DIA_CANVAS_BOX_CHILD_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS ((obj), DIA_TYPE_CANVAS_BOX_CHILD, DiaCanvasBoxChildClass))

typedef struct _DiaCanvasBoxChild DiaCanvasBoxChild;
typedef struct _DiaCanvasBoxChildClass DiaCanvasBoxChildClass;

struct _DiaCanvasBoxChild
{
	DiaCanvasBox item;

	gchar *name;
};


struct _DiaCanvasBoxChildClass
{
	DiaCanvasBoxClass parent_class;
};

GType dia_canvas_box_child_get_type (void);

	
G_END_DECLS


#endif /* __DIA_CANVAS_BOX_CHILD_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]