How I can add some properties to object without touching it
- From: Przemysław Staniszewski <ihi marzyciel wp pl>
- To: gtk-app-devel-list gnome org
- Subject: How I can add some properties to object without touching it
- Date: Tue, 20 Dec 2005 00:12:21 +0100
Hello.
I wonder about something like in topic. I have dia-canvas-box.c, but I
want to add one more properties like gchar *name. I want to make gobject
child of dia-canvas-box but i don't know how :( . My konwledge is poor
so i was copy first file, replace such string (like box) into another
(like box_tex) and add gchar *name into header, of course some functions
to set and get this property. But what is the correct way to make that work?
Something happy here: :)
--
Przemysław Staniszewski
--- /home/users/p/tmp/diacanvas2-0.14.2/diacanvas/dia-canvas-box.c 2003-11-20 21:48:36.000000000 +0100
+++ /home/users/p/Projekty/new_dyplom/src/dia-canvas-box-tex.c 2005-12-18 23:01:09.000000000 +0100
@@ -1,10 +1,10 @@
/*
- * DiaCanvasBox
+ * DiaCanvasBoxTex
*
* This is LGPL'ed code.
*/
-#include "dia-canvas-box.h"
+#include "dia-canvas-box-tex.h"
#include "dia-shape.h"
#include <libart_lgpl/art_affine.h>
#include "dia-canvas-i18n.h"
@@ -15,50 +15,51 @@
PROP_BORDER_WIDTH,
/* Depricated: */
PROP_BG_COLOR,
+ PROP_NAME,
PROP_LINE_WIDTH
};
-static void dia_canvas_box_class_init (DiaCanvasBoxClass *klass);
-static void dia_canvas_box_init (DiaCanvasBox *item);
-static void dia_canvas_box_set_property (GObject *object,
+static void dia_canvas_box_tex_class_init (DiaCanvasBoxTexClass *klass);
+static void dia_canvas_box_tex_init (DiaCanvasBoxTex *item);
+static void dia_canvas_box_tex_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
-static void dia_canvas_box_get_property (GObject *object,
+static void dia_canvas_box_tex_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
-static void dia_canvas_box_dispose (GObject *object);
-static void dia_canvas_box_update (DiaCanvasItem *item,
+static void dia_canvas_box_tex_dispose (GObject *object);
+static void dia_canvas_box_tex_update (DiaCanvasItem *item,
gdouble affine[6]);
-static gboolean dia_canvas_box_get_shape_iter (DiaCanvasItem *item,
+static gboolean dia_canvas_box_tex_get_shape_iter (DiaCanvasItem *item,
DiaCanvasIter *iter);
-static gboolean dia_canvas_box_shape_next (DiaCanvasItem *item,
+static gboolean dia_canvas_box_tex_shape_next (DiaCanvasItem *item,
DiaCanvasIter *iter);
-static DiaShape* dia_canvas_box_shape_value (DiaCanvasItem *item,
+static DiaShape* dia_canvas_box_tex_shape_value (DiaCanvasItem *item,
DiaCanvasIter *iter);
static DiaCanvasElementClass *parent_class = NULL;
GType
-dia_canvas_box_get_type (void)
+dia_canvas_box_tex_get_type (void)
{
static GType object_type = 0;
if (!object_type) {
static const GTypeInfo object_info = {
- sizeof (DiaCanvasBoxClass),
+ sizeof (DiaCanvasBoxTexClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
- (GClassInitFunc) dia_canvas_box_class_init,
+ (GClassInitFunc) dia_canvas_box_tex_class_init,
(GClassFinalizeFunc) NULL,
(gconstpointer) NULL, /* class_data */
- sizeof (DiaCanvasBox),
+ sizeof (DiaCanvasBoxTex),
(guint16) 0, /* n_preallocs */
- (GInstanceInitFunc) dia_canvas_box_init,
+ (GInstanceInitFunc) dia_canvas_box_tex_init,
};
object_type = g_type_register_static (DIA_TYPE_CANVAS_ELEMENT,
- "DiaCanvasBox",
+ "DiaCanvasBoxTex",
&object_info, 0);
}
@@ -67,7 +68,7 @@
static void
-dia_canvas_box_class_init (DiaCanvasBoxClass *klass)
+dia_canvas_box_tex_class_init (DiaCanvasBoxTexClass *klass)
{
GObjectClass *object_class;
DiaCanvasItemClass *item_class;
@@ -77,15 +78,23 @@
parent_class = g_type_class_peek_parent (klass);
- object_class->get_property = dia_canvas_box_get_property;
- object_class->set_property = dia_canvas_box_set_property;
- object_class->dispose = dia_canvas_box_dispose;
-
- item_class->update = dia_canvas_box_update;
- item_class->get_shape_iter = dia_canvas_box_get_shape_iter;
- item_class->shape_next = dia_canvas_box_shape_next;
- item_class->shape_value = dia_canvas_box_shape_value;
+ object_class->get_property = dia_canvas_box_tex_get_property;
+ object_class->set_property = dia_canvas_box_tex_set_property;
+ object_class->dispose = dia_canvas_box_tex_dispose;
+
+ item_class->update = dia_canvas_box_tex_update;
+ item_class->get_shape_iter = dia_canvas_box_tex_get_shape_iter;
+ item_class->shape_next = dia_canvas_box_tex_shape_next;
+ item_class->shape_value = dia_canvas_box_tex_shape_value;
+ g_printf("Instaluje\n");
+ g_object_class_install_property (object_class,
+ PROP_NAME,
+ g_param_spec_string ("name",
+ _("Name"),
+ _("Name for tex box"),
+ NULL,
+ G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_COLOR,
g_param_spec_ulong ("color",
@@ -126,41 +135,49 @@
static void
-dia_canvas_box_init (DiaCanvasBox *item)
+dia_canvas_box_tex_init (DiaCanvasBoxTex *item)
{
item->color = DIA_COLOR (0, 0, 0);
item->fill_color = 0; /* fully transparent */
item->border_width = 1.0;
item->border = dia_shape_new (DIA_SHAPE_PATH);
+ item->name = NULL;
}
static void
-dia_canvas_box_set_property (GObject *object, guint property_id,
+dia_canvas_box_tex_set_property (GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec)
{
- DiaCanvasBox *box = (DiaCanvasBox*) object;
+ DiaCanvasBoxTex *box_tex = (DiaCanvasBoxTex*) object;
switch (property_id) {
case PROP_COLOR:
- dia_canvas_item_preserve_property (DIA_CANVAS_ITEM (box), "color");
- box->color = g_value_get_ulong (value);
+ dia_canvas_item_preserve_property (DIA_CANVAS_ITEM (box_tex), "color");
+ box_tex->color = g_value_get_ulong (value);
dia_canvas_item_request_update (DIA_CANVAS_ITEM (object));
break;
case PROP_BG_COLOR:
g_warning("Depricated property: bg_color, use fill_color instead");
case PROP_FILL_COLOR:
- dia_canvas_item_preserve_property (DIA_CANVAS_ITEM(box), "fill_color");
- box->fill_color = g_value_get_ulong (value);
+ dia_canvas_item_preserve_property (DIA_CANVAS_ITEM(box_tex), "fill_color");
+ box_tex->fill_color = g_value_get_ulong (value);
dia_canvas_item_request_update (DIA_CANVAS_ITEM (object));
break;
case PROP_LINE_WIDTH:
g_warning("Depricated property: line_width, use border_width instead");
case PROP_BORDER_WIDTH:
- dia_canvas_item_preserve_property (DIA_CANVAS_ITEM(box), "border_width");
- box->border_width = g_value_get_double (value);
+ dia_canvas_item_preserve_property (DIA_CANVAS_ITEM(box_tex), "border_width");
+ box_tex->border_width = g_value_get_double (value);
dia_canvas_item_request_update (DIA_CANVAS_ITEM (object));
break;
+ case PROP_NAME:
+ dia_canvas_item_preserve_property (DIA_CANVAS_ITEM(box_tex), "name");
+ if (box_tex->name) {
+ g_printf("Juz jest jakas nazwa - trzeba ja zwolnic przez free");
+ }
+ box_tex->name=g_strdup(g_value_get_string (value));
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
@@ -168,20 +185,23 @@
}
static void
-dia_canvas_box_get_property (GObject *object, guint property_id,
+dia_canvas_box_tex_get_property (GObject *object, guint property_id,
GValue *value, GParamSpec *pspec)
{
switch (property_id) {
case PROP_COLOR:
- g_value_set_ulong (value, DIA_CANVAS_BOX (object)->color);
+ g_value_set_ulong (value, DIA_CANVAS_BOX_TEX (object)->color);
break;
case PROP_BG_COLOR:
case PROP_FILL_COLOR:
- g_value_set_ulong (value, DIA_CANVAS_BOX (object)->fill_color);
+ g_value_set_ulong (value, DIA_CANVAS_BOX_TEX (object)->fill_color);
break;
case PROP_LINE_WIDTH:
case PROP_BORDER_WIDTH:
- g_value_set_double (value, DIA_CANVAS_BOX (object)->border_width);
+ g_value_set_double (value, DIA_CANVAS_BOX_TEX (object)->border_width);
+ break;
+ case PROP_NAME:
+ g_value_set_string (value, DIA_CANVAS_BOX_TEX (object)->name);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -190,21 +210,29 @@
}
static void
-dia_canvas_box_dispose (GObject *object)
+dia_canvas_box_tex_dispose (GObject *object)
{
- DiaCanvasBox *box = (DiaCanvasBox*) object;
+ DiaCanvasBoxTex *box_tex = (DiaCanvasBoxTex*) object;
- if (box->border) {
- dia_shape_free (box->border);
- box->border = NULL;
+ if (box_tex->border) {
+ dia_shape_free (box_tex->border);
+ box_tex->border = NULL;
}
+ // jesli dispose oznacza destroy
+ if (box_tex->name)
+ {
+ g_free (box_tex->name);
+ box_tex->name = NULL;
+ }
+
+
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static void
-dia_canvas_box_update (DiaCanvasItem *item, gdouble affine[6])
+dia_canvas_box_tex_update (DiaCanvasItem *item, gdouble affine[6])
{
DiaShape *border;
DiaPoint p1 = { 0.0, 0.0 }, p2;
@@ -216,9 +244,9 @@
if (DIA_CANVAS_ITEM_CLASS (parent_class)->update)
DIA_CANVAS_ITEM_CLASS (parent_class)->update (item, affine);
- border = DIA_CANVAS_BOX (item)->border;
+ border = DIA_CANVAS_BOX_TEX (item)->border;
- half_w = DIA_CANVAS_BOX (item)->border_width / 2.0;
+ half_w = DIA_CANVAS_BOX_TEX (item)->border_width / 2.0;
item->bounds.top -= half_w;
item->bounds.left -= half_w;
item->bounds.bottom += half_w;
@@ -226,28 +254,28 @@
/* Create the border */
dia_shape_rectangle (border, &p1, &p2);
- dia_shape_set_color (border, DIA_CANVAS_BOX (item)->color);
+ dia_shape_set_color (border, DIA_CANVAS_BOX_TEX (item)->color);
dia_shape_path_set_fill (border, DIA_FILL_SOLID);
- dia_shape_path_set_fill_color (border, DIA_CANVAS_BOX (item)->fill_color);
- dia_shape_path_set_line_width (border, DIA_CANVAS_BOX (item)->border_width);
+ dia_shape_path_set_fill_color (border, DIA_CANVAS_BOX_TEX (item)->fill_color);
+ dia_shape_path_set_line_width (border, DIA_CANVAS_BOX_TEX (item)->border_width);
}
static gboolean
-dia_canvas_box_get_shape_iter (DiaCanvasItem *item, DiaCanvasIter *iter)
+dia_canvas_box_tex_get_shape_iter (DiaCanvasItem *item, DiaCanvasIter *iter)
{
- iter->data[0] = DIA_CANVAS_BOX (item)->border;
+ iter->data[0] = DIA_CANVAS_BOX_TEX (item)->border;
return TRUE;
}
static gboolean
-dia_canvas_box_shape_next (DiaCanvasItem *item, DiaCanvasIter *iter)
+dia_canvas_box_tex_shape_next (DiaCanvasItem *item, DiaCanvasIter *iter)
{
iter->data[0] = NULL;
return FALSE;
}
static DiaShape*
-dia_canvas_box_shape_value (DiaCanvasItem *item, DiaCanvasIter *iter)
+dia_canvas_box_tex_shape_value (DiaCanvasItem *item, DiaCanvasIter *iter)
{
return iter->data[0];
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]