From a14e5c464d36bc01bcf3575700b28d22fb856e89 Mon Sep 17 00:00:00 2001 From: Nicola Fontana Date: Fri, 9 Jan 2009 21:13:30 +0100 Subject: [PATCH] [AdgPositionable] Using origin instead of org in public API Also reworked AdgPositionable to use the classical approach in the virtual table (one getter and one setter) instead of directly access the instance object. --- adg/adg-positionable.c | 112 ++++++++++++++++++++++++++------------------- adg/adg-positionable.h | 14 ++++-- adg/adg-toy-text-private.h | 4 +- adg/adg-toy-text.c | 81 ++++++++++++++++++-------------- demo/adg-demo.c | 6 +-- docs/adg-sections.txt | 6 +-- 6 files changed, 128 insertions(+), 95 deletions(-) diff --git a/adg/adg-positionable.c b/adg/adg-positionable.c index cd13e53d..8719fac1 100644 --- a/adg/adg-positionable.c +++ b/adg/adg-positionable.c @@ -29,11 +29,12 @@ /** * AdgPositionableIface: - * @base_iface: the base interface - * @org: the origin point accessor + * @base_iface: the base interface + * @get_origin: returns the current origin + * @set_origin: sets a new origin * - * The virtual methods @org must be defined by all the types which - * implement this interface. + * The virtual methods @get_origin and @set_origin must be defined + * by all the types which implement this interface. **/ #include "adg-positionable.h" @@ -41,14 +42,16 @@ enum { - ORG_MOVED, + ORIGIN_MOVED, LAST_SIGNAL }; -static void iface_base (AdgPositionableIface *iface); -static void iface_init (AdgPositionableIface *iface); -static AdgPoint *org (AdgPositionable *positionable); - +static void iface_base (AdgPositionableIface *iface); +static void iface_init (AdgPositionableIface *iface); +static void get_origin (AdgPositionable *positionable, + AdgPoint *dest); +static void set_origin (AdgPositionable *positionable, + const AdgPoint *origin); static guint signals[LAST_SIGNAL] = { 0 }; @@ -87,82 +90,95 @@ iface_base(AdgPositionableIface *iface) } initialized = TRUE; - param = g_param_spec_object("org", - P_("Org"), + param = g_param_spec_object("origin", + P_("Origin"), P_("Origin point"), ADG_TYPE_POINT, G_PARAM_READWRITE); g_object_interface_install_property(iface, param); /** - * AdgPositionable::org-moved: + * AdgPositionable::origin-moved: * @positionable: an entity implementing #AdgPositionable * * Emitted whenever the origin has changed. **/ param_types[0] = ADG_TYPE_POINT; - signals[ORG_MOVED] = g_signal_newv("org-moved", ADG_TYPE_POSITIONABLE, - G_SIGNAL_RUN_FIRST, - NULL, NULL, NULL, - g_cclosure_marshal_VOID__OBJECT, - G_TYPE_NONE, 1, param_types); + signals[ORIGIN_MOVED] = g_signal_newv("origin-moved", + ADG_TYPE_POSITIONABLE, + G_SIGNAL_RUN_FIRST, + NULL, NULL, NULL, + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, param_types); } static void iface_init (AdgPositionableIface *iface) { - iface->org = org; + iface->get_origin = get_origin; + iface->set_origin = set_origin; } -static AdgPoint * -org(AdgPositionable *positionable) +static void +get_origin(AdgPositionable *positionable, AdgPoint *dest) +{ + g_warning("AdgPositionable::get_origin not implemented for `%s'", + g_type_name(G_TYPE_FROM_INSTANCE(positionable))); +} + +static void +set_origin(AdgPositionable *positionable, const AdgPoint *point) { - g_warning("AdgPositionable::org not implemented for `%s'", + g_warning("AdgPositionable::set_origin not implemented for `%s'", g_type_name(G_TYPE_FROM_INSTANCE(positionable))); - return NULL; } /** - * adg_positionable_get_org: + * adg_positionable_get_origin: * @positionable: an entity implementing AdgPositionable * * Gets the origin point of @positionable. * * Return value: A pointer to the internal origin point **/ -const AdgPoint * -adg_positionable_get_org(AdgPositionable *positionable) +void +adg_positionable_get_origin(AdgPositionable *positionable, AdgPoint *dest) { - g_return_val_if_fail(ADG_IS_POSITIONABLE(positionable), NULL); + g_return_if_fail(ADG_IS_POSITIONABLE(positionable)); + g_return_if_fail(dest != NULL); - return ADG_POSITIONABLE_GET_IFACE(positionable)->org(positionable); + ADG_POSITIONABLE_GET_IFACE(positionable)->get_origin(positionable, dest); } /** - * adg_positionable_set_org: + * adg_positionable_set_origin: * @positionable: an entity implementing AdgPositionable - * @org: the new origin + * @origin: the new origin * - * Sets the origin of @positionable to @org. An "org-moved" signal is emitted. + * Sets the origin of @positionable to @origin. + * An "origin-moved" signal is emitted. **/ void -adg_positionable_set_org(AdgPositionable *positionable, const AdgPoint *org) +adg_positionable_set_origin(AdgPositionable *positionable, + const AdgPoint *origin) { - AdgPoint *current_org; - AdgPoint old_org; + AdgPositionableIface *iface; + AdgPoint old_origin; g_return_if_fail(ADG_IS_POSITIONABLE(positionable)); + g_return_if_fail(origin != NULL); + + iface = ADG_POSITIONABLE_GET_IFACE(positionable); - current_org = ADG_POSITIONABLE_GET_IFACE(positionable)->org(positionable); + iface->get_origin(positionable, &old_origin); + iface->set_origin(positionable, origin); - adg_point_copy(&old_org, current_org); - adg_point_copy(current_org, org); - g_signal_emit(positionable, signals[ORG_MOVED], 0, &old_org); + g_signal_emit(positionable, signals[ORIGIN_MOVED], 0, &old_origin); } /** - * adg_positionable_set_org_explicit: + * adg_positionable_set_origin_explicit: * @positionable: an entity implementing AdgPositionable * @model_x: the new x position in model space * @model_y: the new y position in model space @@ -170,19 +186,19 @@ adg_positionable_set_org(AdgPositionable *positionable, const AdgPoint *org) * @paper_y: the new y position in paper space * * Sets the origin of @positionable to the new coordinates. It calls - * adg_positionable_set_org() internally. + * adg_positionable_set_origin() internally. **/ void -adg_positionable_set_org_explicit(AdgPositionable *positionable, - gdouble model_x, gdouble model_y, - gdouble paper_x, gdouble paper_y) +adg_positionable_set_origin_explicit(AdgPositionable *positionable, + gdouble model_x, gdouble model_y, + gdouble paper_x, gdouble paper_y) { - AdgPoint org; + AdgPoint origin; - org.model.x = model_x; - org.model.y = model_y; - org.paper.x = paper_x; - org.paper.y = paper_y; + origin.model.x = model_x; + origin.model.y = model_y; + origin.paper.x = paper_x; + origin.paper.y = paper_y; - adg_positionable_set_org(positionable, &org); + adg_positionable_set_origin(positionable, &origin); } diff --git a/adg/adg-positionable.h b/adg/adg-positionable.h index b738d35a..01d49521 100644 --- a/adg/adg-positionable.h +++ b/adg/adg-positionable.h @@ -38,16 +38,20 @@ struct _AdgPositionableIface { GTypeInterface base_iface; /* Virtual Table */ - AdgPoint * (*org) (AdgPositionable *positionable); + void (*get_origin) (AdgPositionable *positionable, + AdgPoint *dest); + void (*set_origin) (AdgPositionable *positionable, + const AdgPoint *origin); }; GType adg_positionable_get_type (void) G_GNUC_CONST; -const AdgPoint *adg_positionable_get_org (AdgPositionable *positionable); -void adg_positionable_set_org (AdgPositionable *positionable, - const AdgPoint *org); -void adg_positionable_set_org_explicit +void adg_positionable_get_origin (AdgPositionable *positionable, + AdgPoint *dest); +void adg_positionable_set_origin (AdgPositionable *positionable, + const AdgPoint *origin); +void adg_positionable_set_origin_explicit (AdgPositionable *positionable, gdouble model_x, gdouble model_y, diff --git a/adg/adg-toy-text-private.h b/adg/adg-toy-text-private.h index ad45b799..c3c01c2d 100644 --- a/adg/adg-toy-text-private.h +++ b/adg/adg-toy-text-private.h @@ -28,11 +28,11 @@ G_BEGIN_DECLS struct _AdgToyTextPrivate { /* Properties */ - AdgPoint org; + AdgPoint origin; gchar *label; /* Cache */ - AdgPair org_pair; + AdgPair origin_pair; int num_glyphs; cairo_glyph_t *glyphs; cairo_text_extents_t extents; diff --git a/adg/adg-toy-text.c b/adg/adg-toy-text.c index 49c78ffc..6284805a 100644 --- a/adg/adg-toy-text.c +++ b/adg/adg-toy-text.c @@ -37,12 +37,15 @@ enum { PROP_0, - PROP_ORG, + PROP_ORIGIN, PROP_LABEL }; static void positionable_init (AdgPositionableIface *iface); -static AdgPoint*org (AdgPositionable*positionable); +static void get_origin (AdgPositionable*positionable, + AdgPoint *dest); +static void set_origin (AdgPositionable*positionable, + const AdgPoint *origin); static void finalize (GObject *object); static void get_property (GObject *object, @@ -58,7 +61,7 @@ static void model_matrix_changed (AdgEntity *entity, static void invalidate (AdgEntity *entity); static void render (AdgEntity *entity, cairo_t *cr); -static gboolean update_org_cache (AdgToyText *toy_text); +static gboolean update_origin_cache (AdgToyText *toy_text); static gboolean update_label_cache (AdgToyText *toy_text, cairo_t *cr); static void clear_label_cache (AdgToyText *toy_text); @@ -72,6 +75,28 @@ G_DEFINE_TYPE_WITH_CODE(AdgToyText, adg_toy_text, ADG_TYPE_ENTITY, static void +positionable_init(AdgPositionableIface *iface) +{ + iface->get_origin = get_origin; + iface->set_origin = set_origin; +} + +static void +get_origin(AdgPositionable *positionable, AdgPoint *dest) +{ + AdgToyText *toy_text = (AdgToyText *) positionable; + adg_point_copy(dest, &toy_text->priv->origin); +} + +static void +set_origin(AdgPositionable *positionable, const AdgPoint *origin) +{ + AdgToyText *toy_text = (AdgToyText *) positionable; + adg_point_copy(&toy_text->priv->origin, origin); +} + + +static void adg_toy_text_class_init(AdgToyTextClass *klass) { GObjectClass *gobject_class; @@ -91,7 +116,7 @@ adg_toy_text_class_init(AdgToyTextClass *klass) entity_class->invalidate = invalidate; entity_class->render = render; - g_object_class_override_property(gobject_class, PROP_ORG, "org"); + g_object_class_override_property(gobject_class, PROP_ORIGIN, "origin"); param = g_param_spec_string("label", P_("Label"), @@ -108,8 +133,8 @@ adg_toy_text_init(AdgToyText *toy_text) AdgToyTextPrivate); priv->label = NULL; - adg_point_unset(&priv->org); - priv->org_pair.x = priv->org_pair.y = 0.; + adg_point_unset(&priv->origin); + priv->origin_pair.x = priv->origin_pair.y = 0.; priv->num_glyphs = 0; priv->glyphs = NULL; @@ -117,18 +142,6 @@ adg_toy_text_init(AdgToyText *toy_text) } static void -positionable_init(AdgPositionableIface *iface) -{ - iface->org = org; -} - -static AdgPoint * -org(AdgPositionable *positionable) -{ - return & ((AdgToyText *) positionable)->priv->org; -} - -static void finalize(GObject *object) { AdgToyText *toy_text = (AdgToyText *) object; @@ -145,8 +158,8 @@ get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) AdgToyText *toy_text = (AdgToyText *) toy_text; switch (prop_id) { - case PROP_ORG: - g_value_set_boxed(value, &toy_text->priv->org); + case PROP_ORIGIN: + g_value_set_boxed(value, &toy_text->priv->origin); break; case PROP_LABEL: g_value_set_string(value, toy_text->priv->label); @@ -165,9 +178,9 @@ set_property(GObject *object, guint prop_id, AdgToyTextPrivate *priv = toy_text->priv; switch (prop_id) { - case PROP_ORG: - adg_point_copy(&priv->org, (AdgPoint *) g_value_get_boxed(value)); - update_org_cache(toy_text); + case PROP_ORIGIN: + adg_point_copy(&priv->origin, (AdgPoint *) g_value_get_boxed(value)); + update_origin_cache(toy_text); break; case PROP_LABEL: g_free(priv->label); @@ -245,7 +258,7 @@ render(AdgEntity *entity, cairo_t *cr) if (!priv->glyphs) { update_label_cache(toy_text, cr); - update_org_cache(toy_text); + update_origin_cache(toy_text); } cairo_show_glyphs(cr, priv->glyphs, priv->num_glyphs); @@ -257,7 +270,7 @@ render(AdgEntity *entity, cairo_t *cr) static void model_matrix_changed(AdgEntity *entity, AdgMatrix *parent_matrix) { - update_org_cache((AdgToyText *) entity); + update_origin_cache((AdgToyText *) entity); PARENT_CLASS->model_matrix_changed(entity, parent_matrix); } @@ -269,17 +282,17 @@ invalidate(AdgEntity *entity) } static gboolean -update_org_cache(AdgToyText *toy_text) +update_origin_cache(AdgToyText *toy_text) { AdgToyTextPrivate *priv = toy_text->priv; - AdgPoint *org = &toy_text->priv->org; - AdgPair *org_pair = &toy_text->priv->org_pair; + AdgPoint *origin = &toy_text->priv->origin; + AdgPair *origin_pair = &toy_text->priv->origin_pair; cairo_glyph_t *glyph; double x, y; int cnt; - org = &priv->org; - org_pair = &priv->org_pair; + origin = &priv->origin; + origin_pair = &priv->origin_pair; glyph = priv->glyphs; cnt = priv->num_glyphs; @@ -288,14 +301,14 @@ update_org_cache(AdgToyText *toy_text) return TRUE; } - adg_entity_point_to_paper_pair((AdgEntity *) toy_text, org, org_pair); - if (org_pair->x == glyph->x && org_pair->y == glyph->y) { + adg_entity_point_to_paper_pair((AdgEntity *) toy_text, origin, origin_pair); + if (origin_pair->x == glyph->x && origin_pair->y == glyph->y) { /* The label is yet properly positioned */ return TRUE; } - x = org_pair->x - glyph->x; - y = org_pair->y - glyph->y; + x = origin_pair->x - glyph->x; + y = origin_pair->y - glyph->y; while (cnt --) { glyph->x += x; diff --git a/demo/adg-demo.c b/demo/adg-demo.c index 772635ce..f1104cbe 100644 --- a/demo/adg-demo.c +++ b/demo/adg-demo.c @@ -260,9 +260,9 @@ add_sample_stuff(AdgCanvas *canvas) AdgEntity *toy_text; toy_text = adg_toy_text_new("Test script near the piston"); - adg_positionable_set_org_explicit(ADG_POSITIONABLE(toy_text), - 0., -4.65, - -2., -5.); + adg_positionable_set_origin_explicit(ADG_POSITIONABLE(toy_text), + 0., -4.65, + -2., -5.); adg_container_add(ADG_CONTAINER(canvas), toy_text); } diff --git a/docs/adg-sections.txt b/docs/adg-sections.txt index 94ad1615..4c209e00 100644 --- a/docs/adg-sections.txt +++ b/docs/adg-sections.txt @@ -329,9 +329,9 @@ ADG_ENTITY_GET_CLASS adg/adg.h AdgPositionableIface -adg_positionable_get_org -adg_positionable_set_org -adg_positionable_set_org_explicit +adg_positionable_get_origin +adg_positionable_set_origin +adg_positionable_set_origin_explicit AdgPositionable adg_positionable_get_type -- 2.11.4.GIT