From 4fe50a80bfee178be2bb98e881a327464cd77982 Mon Sep 17 00:00:00 2001 From: joncruz Date: Thu, 29 May 2008 01:19:24 +0000 Subject: [PATCH] Refactoring out common code --- src/Makefile_insert | 1 + src/common-context.cpp | 169 ++++++++++++++++++++++++++++++++++++++ src/common-context.h | 119 +++++++++++++++++++++++++++ src/dyna-draw-context.cpp | 72 +++------------- src/dyna-draw-context.h | 74 +---------------- src/eraser-context.cpp | 68 ++------------- src/eraser-context.h | 71 +--------------- 7 files changed, 310 insertions(+), 264 deletions(-) create mode 100644 src/common-context.cpp create mode 100644 src/common-context.h diff --git a/src/Makefile_insert b/src/Makefile_insert index f39ad098f..a81b7a2f5 100644 --- a/src/Makefile_insert +++ b/src/Makefile_insert @@ -49,6 +49,7 @@ libinkpre_a_SOURCES = \ color-rgba.h \ color-profile.cpp color-profile.h \ color-profile-fns.h \ + common-context.cpp common-context.h\ conditions.cpp conditions.h\ conn-avoid-ref.cpp conn-avoid-ref.h \ connector-context.cpp connector-context.h \ diff --git a/src/common-context.cpp b/src/common-context.cpp new file mode 100644 index 000000000..a52e83c22 --- /dev/null +++ b/src/common-context.cpp @@ -0,0 +1,169 @@ + +#include "common-context.h" + +#include + +#include "config.h" + +#include "forward.h" +#include "message-context.h" + +#define DRAG_DEFAULT 1.0 +#define MIN_PRESSURE 0.0 +#define MAX_PRESSURE 1.0 +#define DEFAULT_PRESSURE 1.0 + + +static void sp_common_context_class_init(SPCommonContextClass *klass); +static void sp_common_context_init(SPCommonContext *erc); +static void sp_common_context_dispose(GObject *object); + +static void sp_common_context_setup(SPEventContext *ec); +static void sp_common_context_set(SPEventContext *ec, gchar const *key, gchar const *value); + +static gint sp_common_context_root_handler(SPEventContext *event_context, GdkEvent *event); + + +static SPEventContextClass *common_parent_class = 0; + +GType sp_common_context_get_type(void) +{ + static GType type = 0; + if (!type) { + GTypeInfo info = { + sizeof(SPCommonContextClass), + 0, // base_init + 0, // base_finalize + (GClassInitFunc)sp_common_context_class_init, + 0, // class_finalize + 0, // class_data + sizeof(SPCommonContext), + 0, // n_preallocs + (GInstanceInitFunc)sp_common_context_init, + 0 // value_table + }; + type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPCommonContext", &info, static_cast(0)); + } + return type; +} + + +static void sp_common_context_class_init(SPCommonContextClass *klass) +{ + GObjectClass *object_class = (GObjectClass *) klass; + SPEventContextClass *event_context_class = (SPEventContextClass *) klass; + + common_parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass); + + object_class->dispose = sp_common_context_dispose; + + event_context_class->setup = sp_common_context_setup; + event_context_class->set = sp_common_context_set; + event_context_class->root_handler = sp_common_context_root_handler; +} + +static void sp_common_context_init(SPCommonContext *ctx) +{ +// ctx->cursor_shape = cursor_eraser_xpm; +// ctx->hot_x = 4; +// ctx->hot_y = 4; + + ctx->accumulated = 0; + ctx->segments = 0; + ctx->currentcurve = 0; + ctx->currentshape = 0; + ctx->npoints = 0; + ctx->cal1 = 0; + ctx->cal2 = 0; + ctx->repr = 0; + + /* Common values */ + ctx->cur = NR::Point(0,0); + ctx->last = NR::Point(0,0); + ctx->vel = NR::Point(0,0); + ctx->vel_max = 0; + ctx->acc = NR::Point(0,0); + ctx->ang = NR::Point(0,0); + ctx->del = NR::Point(0,0); + + /* attributes */ + ctx->dragging = FALSE; + + ctx->mass = 0.3; + ctx->drag = DRAG_DEFAULT; + ctx->angle = 30.0; + ctx->width = 0.2; + ctx->pressure = DEFAULT_PRESSURE; + + ctx->vel_thin = 0.1; + ctx->flatness = 0.9; + ctx->cap_rounding = 0.0; + + ctx->abs_width = false; +} + +static void sp_common_context_dispose(GObject *object) +{ + SPCommonContext *ctx = SP_COMMON_CONTEXT(object); + + if (ctx->accumulated) { + ctx->accumulated = ctx->accumulated->unref(); + ctx->accumulated = 0; + } + + while (ctx->segments) { + gtk_object_destroy(GTK_OBJECT(ctx->segments->data)); + ctx->segments = g_slist_remove(ctx->segments, ctx->segments->data); + } + + if (ctx->currentcurve) { + ctx->currentcurve = ctx->currentcurve->unref(); + ctx->currentcurve = 0; + } + if (ctx->cal1) { + ctx->cal1 = ctx->cal1->unref(); + ctx->cal1 = 0; + } + if (ctx->cal2) { + ctx->cal2 = ctx->cal2->unref(); + ctx->cal2 = 0; + } + + if (ctx->currentshape) { + gtk_object_destroy(GTK_OBJECT(ctx->currentshape)); + ctx->currentshape = 0; + } + + if (ctx->_message_context) { + delete ctx->_message_context; + ctx->_message_context = 0; + } + + G_OBJECT_CLASS(common_parent_class)->dispose(object); +} + + +static void sp_common_context_setup(SPEventContext */*ec*/) +{ +} + + +static void sp_common_context_set(SPEventContext */*ec*/, gchar const */*key*/, gchar const */*value*/) +{ +} + +static gint sp_common_context_root_handler(SPEventContext */*event_context*/, GdkEvent */*event*/) +{ + return 0; +} + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : diff --git a/src/common-context.h b/src/common-context.h new file mode 100644 index 000000000..1f3963608 --- /dev/null +++ b/src/common-context.h @@ -0,0 +1,119 @@ +#ifndef COMMON_CONTEXT_H_SEEN +#define COMMON_CONTEXT_H_SEEN + +/* + * Common drawing mode + * + * Authors: + * Mitsuru Oka + * Lauris Kaplinski + * + * The original dynadraw code: + * Paul Haeberli + * + * Copyright (C) 1998 The Free Software Foundation + * Copyright (C) 1999-2002 authors + * Copyright (C) 2001-2002 Ximian, Inc. + * Copyright (C) 2008 Jon A. Cruz + * + * Released under GNU GPL, read the file 'COPYING' for more information + */ + +#include "event-context.h" +#include "display/curve.h" +#include "display/display-forward.h" + +#define SP_TYPE_COMMON_CONTEXT (sp_common_context_get_type()) +#define SP_COMMON_CONTEXT(o) (GTK_CHECK_CAST((o), SP_TYPE_COMMON_CONTEXT, SPCommonContext)) +#define SP_COMMON_CONTEXT_CLASS(k) (GTK_CHECK_CLASS_CAST((k), SP_TYPE_COMMON_CONTEXT, SPCommonContextClass)) +#define SP_IS_COMMON_CONTEXT(o) (GTK_CHECK_TYPE((o), SP_TYPE_COMMON_CONTEXT)) +#define SP_IS_COMMON_CONTEXT_CLASS(k) (GTK_CHECK_CLASS_TYPE((k), SP_TYPE_COMMON_CONTEXT)) + +class SPCommonContext; +class SPCommonContextClass; + +#define SAMPLING_SIZE 8 /* fixme: ?? */ + + +struct SPCommonContext : public SPEventContext { + /** accumulated shape which ultimately goes in svg:path */ + SPCurve *accumulated; + + /** canvas items for "comitted" segments */ + GSList *segments; + + /** canvas item for red "leading" segment */ + SPCanvasItem *currentshape; + /** shape of red "leading" segment */ + SPCurve *currentcurve; + + /** left edge of the stroke; combined to get accumulated */ + SPCurve *cal1; + + /** right edge of the stroke; combined to get accumulated */ + SPCurve *cal2; + + /** left edge points for this segment */ + NR::Point point1[SAMPLING_SIZE]; + + /** right edge points for this segment */ + NR::Point point2[SAMPLING_SIZE]; + + /** number of edge points for this segment */ + gint npoints; + + /* repr */ + Inkscape::XML::Node *repr; + + /* common */ + NR::Point cur; + NR::Point vel; + double vel_max; + NR::Point acc; + NR::Point ang; + NR::Point last; + NR::Point del; + + /* extended input data */ + gdouble pressure; + gdouble xtilt; + gdouble ytilt; + + /* attributes */ + guint dragging : 1; /* mouse state: mouse is dragging */ + guint usepressure : 1; + guint usetilt : 1; + double mass, drag; + double angle; + double width; + + double vel_thin; + double flatness; + double tremor; + double cap_rounding; + + Inkscape::MessageContext *_message_context; + + bool is_drawing; + + /** uses absolute width independent of zoom */ + bool abs_width; +}; + +struct SPCommonContextClass : public SPEventContextClass{}; + +GType sp_common_context_get_type(void); + +#endif // COMMON_CONTEXT_H_SEEN + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : + diff --git a/src/dyna-draw-context.cpp b/src/dyna-draw-context.cpp index 9574d2cad..9f759558c 100644 --- a/src/dyna-draw-context.cpp +++ b/src/dyna-draw-context.cpp @@ -117,7 +117,7 @@ static NR::Point sp_dyna_draw_get_vpoint(SPDynaDrawContext const *ddc, NR::Point static void draw_temporary_box(SPDynaDrawContext *dc); -static SPEventContextClass *parent_class; +static SPEventContextClass *dd_parent_class = 0; GType sp_dyna_draw_context_get_type(void) { @@ -135,7 +135,7 @@ GType sp_dyna_draw_context_get_type(void) (GInstanceInitFunc)sp_dyna_draw_context_init, 0 // value_table }; - type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPDynaDrawContext", &info, static_cast(0)); + type = g_type_register_static(SP_TYPE_COMMON_CONTEXT, "SPDynaDrawContext", &info, static_cast(0)); } return type; } @@ -146,7 +146,7 @@ sp_dyna_draw_context_class_init(SPDynaDrawContextClass *klass) GObjectClass *object_class = (GObjectClass *) klass; SPEventContextClass *event_context_class = (SPEventContextClass *) klass; - parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass); + dd_parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass); object_class->dispose = sp_dyna_draw_context_dispose; @@ -158,38 +158,9 @@ sp_dyna_draw_context_class_init(SPDynaDrawContextClass *klass) static void sp_dyna_draw_context_init(SPDynaDrawContext *ddc) { - SPEventContext *event_context = SP_EVENT_CONTEXT(ddc); - - event_context->cursor_shape = cursor_calligraphy_xpm; - event_context->hot_x = 4; - event_context->hot_y = 4; - - ddc->accumulated = NULL; - ddc->segments = NULL; - ddc->currentcurve = NULL; - ddc->currentshape = NULL; - ddc->npoints = 0; - ddc->cal1 = NULL; - ddc->cal2 = NULL; - ddc->repr = NULL; - - /* DynaDraw values */ - ddc->cur = NR::Point(0,0); - ddc->last = NR::Point(0,0); - ddc->vel = NR::Point(0,0); - ddc->vel_max = 0; - ddc->acc = NR::Point(0,0); - ddc->ang = NR::Point(0,0); - ddc->del = NR::Point(0,0); - - /* attributes */ - ddc->dragging = FALSE; - - ddc->mass = 0.3; - ddc->drag = DRAG_DEFAULT; - ddc->angle = 30.0; - ddc->width = 0.2; - ddc->pressure = DDC_DEFAULT_PRESSURE; + ddc->cursor_shape = cursor_calligraphy_xpm; + ddc->hot_x = 4; + ddc->hot_y = 4; ddc->vel_thin = 0.1; ddc->flatness = 0.9; @@ -223,29 +194,8 @@ sp_dyna_draw_context_dispose(GObject *object) ddc->hatch_area = NULL; } - if (ddc->accumulated) { - ddc->accumulated = ddc->accumulated->unref(); - } - - while (ddc->segments) { - gtk_object_destroy(GTK_OBJECT(ddc->segments->data)); - ddc->segments = g_slist_remove(ddc->segments, ddc->segments->data); - } - - if (ddc->currentcurve) ddc->currentcurve = ddc->currentcurve->unref(); - if (ddc->cal1) ddc->cal1 = ddc->cal1->unref(); - if (ddc->cal2) ddc->cal2 = ddc->cal2->unref(); - - if (ddc->currentshape) { - gtk_object_destroy(GTK_OBJECT(ddc->currentshape)); - ddc->currentshape = NULL; - } - - if (ddc->_message_context) { - delete ddc->_message_context; - } - G_OBJECT_CLASS(parent_class)->dispose(object); + G_OBJECT_CLASS(dd_parent_class)->dispose(object); ddc->hatch_pointer_past.~list(); ddc->hatch_nearest_past.~list(); @@ -256,8 +206,8 @@ sp_dyna_draw_context_setup(SPEventContext *ec) { SPDynaDrawContext *ddc = SP_DYNA_DRAW_CONTEXT(ec); - if (((SPEventContextClass *) parent_class)->setup) - ((SPEventContextClass *) parent_class)->setup(ec); + if (((SPEventContextClass *) dd_parent_class)->setup) + ((SPEventContextClass *) dd_parent_class)->setup(ec); ddc->accumulated = new SPCurve(32); ddc->currentcurve = new SPCurve(4); @@ -1020,8 +970,8 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context, } if (!ret) { - if (((SPEventContextClass *) parent_class)->root_handler) { - ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event); + if (((SPEventContextClass *) dd_parent_class)->root_handler) { + ret = ((SPEventContextClass *) dd_parent_class)->root_handler(event_context, event); } } diff --git a/src/dyna-draw-context.h b/src/dyna-draw-context.h index 0e83f6b50..9232dd04b 100644 --- a/src/dyna-draw-context.h +++ b/src/dyna-draw-context.h @@ -18,9 +18,7 @@ * Released under GNU GPL, read the file 'COPYING' for more information */ -#include "event-context.h" -#include -#include +#include "common-context.h" #define SP_TYPE_DYNA_DRAW_CONTEXT (sp_dyna_draw_context_get_type()) #define SP_DYNA_DRAW_CONTEXT(o) (GTK_CHECK_CAST((o), SP_TYPE_DYNA_DRAW_CONTEXT, SPDynaDrawContext)) @@ -31,8 +29,6 @@ class SPDynaDrawContext; class SPDynaDrawContextClass; -#define SAMPLING_SIZE 8 /* fixme: ?? */ - #define DDC_MIN_PRESSURE 0.0 #define DDC_MAX_PRESSURE 1.0 #define DDC_DEFAULT_PRESSURE 1.0 @@ -41,68 +37,7 @@ class SPDynaDrawContextClass; #define DDC_MAX_TILT 1.0 #define DDC_DEFAULT_TILT 0.0 -struct SPDynaDrawContext -{ - SPEventContext event_context; - - /** accumulated shape which ultimately goes in svg:path */ - SPCurve *accumulated; - - /** canvas items for "comitted" segments */ - GSList *segments; - - /** canvas item for red "leading" segment */ - SPCanvasItem *currentshape; - /** shape of red "leading" segment */ - SPCurve *currentcurve; - - /** left edge of the stroke; combined to get accumulated */ - SPCurve *cal1; - /** right edge of the stroke; combined to get accumulated */ - SPCurve *cal2; - - /** left edge points for this segment */ - NR::Point point1[SAMPLING_SIZE]; - /** right edge points for this segment */ - NR::Point point2[SAMPLING_SIZE]; - /** number of edge points for this segment */ - gint npoints; - - /* repr */ - Inkscape::XML::Node *repr; - - /* DynaDraw */ - NR::Point cur; - NR::Point vel; - double vel_max; - NR::Point acc; - NR::Point ang; - NR::Point last; - NR::Point del; - /* extended input data */ - gdouble pressure; - gdouble xtilt; - gdouble ytilt; - /* attributes */ - guint dragging : 1; /* mouse state: mouse is dragging */ - guint usepressure : 1; - guint usetilt : 1; - double mass, drag; - double angle; - double width; - - double vel_thin; - double flatness; - double tremor; - double cap_rounding; - - Inkscape::MessageContext *_message_context; - - bool is_drawing; - - /** uses absolute width independent of zoom */ - bool abs_width; - +struct SPDynaDrawContext : public SPCommonContext { /** newly created object remain selected */ bool keep_selected; @@ -120,10 +55,7 @@ struct SPDynaDrawContext bool trace_bg; }; -struct SPDynaDrawContextClass -{ - SPEventContextClass parent_class; -}; +struct SPDynaDrawContextClass : public SPEventContextClass{}; GType sp_dyna_draw_context_get_type(void); diff --git a/src/eraser-context.cpp b/src/eraser-context.cpp index f30b004ea..1920baed5 100644 --- a/src/eraser-context.cpp +++ b/src/eraser-context.cpp @@ -122,7 +122,7 @@ GType sp_eraser_context_get_type(void) (GInstanceInitFunc)sp_eraser_context_init, 0 // value_table }; - type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPEraserContext", &info, static_cast(0)); + type = g_type_register_static(SP_TYPE_COMMON_CONTEXT, "SPEraserContext", &info, static_cast(0)); } return type; } @@ -145,73 +145,15 @@ sp_eraser_context_class_init(SPEraserContextClass *klass) static void sp_eraser_context_init(SPEraserContext *erc) { - SPEventContext *event_context = SP_EVENT_CONTEXT(erc); - - event_context->cursor_shape = cursor_eraser_xpm; - event_context->hot_x = 4; - event_context->hot_y = 4; - - erc->accumulated = NULL; - erc->segments = NULL; - erc->currentcurve = NULL; - erc->currentshape = NULL; - erc->npoints = 0; - erc->cal1 = NULL; - erc->cal2 = NULL; - erc->repr = NULL; - - /* Eraser values */ - erc->cur = NR::Point(0,0); - erc->last = NR::Point(0,0); - erc->vel = NR::Point(0,0); - erc->vel_max = 0; - erc->acc = NR::Point(0,0); - erc->ang = NR::Point(0,0); - erc->del = NR::Point(0,0); - - /* attributes */ - erc->dragging = FALSE; - - erc->mass = 0.3; - erc->drag = DRAG_DEFAULT; - erc->angle = 30.0; - erc->width = 0.2; - erc->pressure = ERC_DEFAULT_PRESSURE; - - erc->vel_thin = 0.1; - erc->flatness = 0.9; - erc->cap_rounding = 0.0; - - erc->abs_width = false; + erc->cursor_shape = cursor_eraser_xpm; + erc->hot_x = 4; + erc->hot_y = 4; } static void sp_eraser_context_dispose(GObject *object) { - SPEraserContext *erc = SP_ERASER_CONTEXT(object); - - if (erc->accumulated) { - erc->accumulated = erc->accumulated->unref(); - } - - while (erc->segments) { - gtk_object_destroy(GTK_OBJECT(erc->segments->data)); - erc->segments = g_slist_remove(erc->segments, erc->segments->data); - } - - if (erc->currentcurve) erc->currentcurve = erc->currentcurve->unref(); - if (erc->cal1) erc->cal1 = erc->cal1->unref(); - if (erc->cal2) erc->cal2 = erc->cal2->unref(); - - if (erc->currentshape) { - gtk_object_destroy(GTK_OBJECT(erc->currentshape)); - erc->currentshape = 0; - } - - if (erc->_message_context) { - delete erc->_message_context; - erc->_message_context = 0; - } + //SPEraserContext *erc = SP_ERASER_CONTEXT(object); G_OBJECT_CLASS(eraser_parent_class)->dispose(object); } diff --git a/src/eraser-context.h b/src/eraser-context.h index b47c21b57..0e3f5c625 100644 --- a/src/eraser-context.h +++ b/src/eraser-context.h @@ -19,10 +19,7 @@ * Released under GNU GPL, read the file 'COPYING' for more information */ -#include "display/curve.h" -#include "event-context.h" -#include -#include +#include "common-context.h" #define SP_TYPE_ERASER_CONTEXT (sp_eraser_context_get_type()) #define SP_ERASER_CONTEXT(o) (GTK_CHECK_CAST((o), SP_TYPE_ERASER_CONTEXT, SPEraserContext)) @@ -33,8 +30,6 @@ class SPEraserContext; class SPEraserContextClass; -#define SAMPLING_SIZE 8 /* fixme: ?? */ - #define ERC_MIN_PRESSURE 0.0 #define ERC_MAX_PRESSURE 1.0 #define ERC_DEFAULT_PRESSURE 1.0 @@ -43,69 +38,7 @@ class SPEraserContextClass; #define ERC_MAX_TILT 1.0 #define ERC_DEFAULT_TILT 0.0 -struct SPEraserContext : public SPEventContext { - /** accumulated shape which ultimately goes in svg:path */ - SPCurve *accumulated; - - /** canvas items for "comitted" segments */ - GSList *segments; - - /** canvas item for red "leading" segment */ - SPCanvasItem *currentshape; - /** shape of red "leading" segment */ - SPCurve *currentcurve; - - /** left edge of the stroke; combined to get accumulated */ - SPCurve *cal1; - - /** right edge of the stroke; combined to get accumulated */ - SPCurve *cal2; - - /** left edge points for this segment */ - NR::Point point1[SAMPLING_SIZE]; - - /** right edge points for this segment */ - NR::Point point2[SAMPLING_SIZE]; - - /** number of edge points for this segment */ - gint npoints; - - /* repr */ - Inkscape::XML::Node *repr; - - /* Eraser */ - NR::Point cur; - NR::Point vel; - double vel_max; - NR::Point acc; - NR::Point ang; - NR::Point last; - NR::Point del; - - /* extended input data */ - gdouble pressure; - gdouble xtilt; - gdouble ytilt; - - /* attributes */ - guint dragging : 1; /* mouse state: mouse is dragging */ - guint usepressure : 1; - guint usetilt : 1; - double mass, drag; - double angle; - double width; - - double vel_thin; - double flatness; - double tremor; - double cap_rounding; - - Inkscape::MessageContext *_message_context; - - bool is_drawing; - - /** uses absolute width independent of zoom */ - bool abs_width; +struct SPEraserContext : public SPCommonContext { }; struct SPEraserContextClass : public SPEventContextClass{}; -- 2.30.2