2 #include "common-context.h"
4 #include <gtk/gtk.h>
6 #include "config.h"
8 #include "forward.h"
9 #include "message-context.h"
10 #include "streq.h"
11 #include "preferences.h"
13 #define MIN_PRESSURE 0.0
14 #define MAX_PRESSURE 1.0
15 #define DEFAULT_PRESSURE 1.0
17 #define DRAG_MIN 0.0
18 #define DRAG_DEFAULT 1.0
19 #define DRAG_MAX 1.0
22 static void sp_common_context_class_init(SPCommonContextClass *klass);
23 static void sp_common_context_init(SPCommonContext *erc);
24 static void sp_common_context_dispose(GObject *object);
26 static void sp_common_context_setup(SPEventContext *ec);
27 static void sp_common_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
29 static gint sp_common_context_root_handler(SPEventContext *event_context, GdkEvent *event);
32 static SPEventContextClass *common_parent_class = 0;
34 GType sp_common_context_get_type(void)
35 {
36 static GType type = 0;
37 if (!type) {
38 GTypeInfo info = {
39 sizeof(SPCommonContextClass),
40 0, // base_init
41 0, // base_finalize
42 (GClassInitFunc)sp_common_context_class_init,
43 0, // class_finalize
44 0, // class_data
45 sizeof(SPCommonContext),
46 0, // n_preallocs
47 (GInstanceInitFunc)sp_common_context_init,
48 0 // value_table
49 };
50 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPCommonContext", &info, static_cast<GTypeFlags>(0));
51 }
52 return type;
53 }
56 static void sp_common_context_class_init(SPCommonContextClass *klass)
57 {
58 GObjectClass *object_class = (GObjectClass *) klass;
59 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
61 common_parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
63 object_class->dispose = sp_common_context_dispose;
65 event_context_class->setup = sp_common_context_setup;
66 event_context_class->set = sp_common_context_set;
67 event_context_class->root_handler = sp_common_context_root_handler;
68 }
70 static void sp_common_context_init(SPCommonContext *ctx)
71 {
72 // ctx->cursor_shape = cursor_eraser_xpm;
73 // ctx->hot_x = 4;
74 // ctx->hot_y = 4;
76 ctx->accumulated = 0;
77 ctx->segments = 0;
78 ctx->currentcurve = 0;
79 ctx->currentshape = 0;
80 ctx->npoints = 0;
81 ctx->cal1 = 0;
82 ctx->cal2 = 0;
83 ctx->repr = 0;
85 /* Common values */
86 ctx->cur = Geom::Point(0,0);
87 ctx->last = Geom::Point(0,0);
88 ctx->vel = Geom::Point(0,0);
89 ctx->vel_max = 0;
90 ctx->acc = Geom::Point(0,0);
91 ctx->ang = Geom::Point(0,0);
92 ctx->del = Geom::Point(0,0);
94 /* attributes */
95 ctx->dragging = FALSE;
97 ctx->mass = 0.3;
98 ctx->drag = DRAG_DEFAULT;
99 ctx->angle = 30.0;
100 ctx->width = 0.2;
101 ctx->pressure = DEFAULT_PRESSURE;
103 ctx->vel_thin = 0.1;
104 ctx->flatness = 0.9;
105 ctx->cap_rounding = 0.0;
107 ctx->abs_width = false;
108 }
110 static void sp_common_context_dispose(GObject *object)
111 {
112 SPCommonContext *ctx = SP_COMMON_CONTEXT(object);
114 if (ctx->accumulated) {
115 ctx->accumulated = ctx->accumulated->unref();
116 ctx->accumulated = 0;
117 }
119 while (ctx->segments) {
120 gtk_object_destroy(GTK_OBJECT(ctx->segments->data));
121 ctx->segments = g_slist_remove(ctx->segments, ctx->segments->data);
122 }
124 if (ctx->currentcurve) {
125 ctx->currentcurve = ctx->currentcurve->unref();
126 ctx->currentcurve = 0;
127 }
128 if (ctx->cal1) {
129 ctx->cal1 = ctx->cal1->unref();
130 ctx->cal1 = 0;
131 }
132 if (ctx->cal2) {
133 ctx->cal2 = ctx->cal2->unref();
134 ctx->cal2 = 0;
135 }
137 if (ctx->currentshape) {
138 gtk_object_destroy(GTK_OBJECT(ctx->currentshape));
139 ctx->currentshape = 0;
140 }
142 if (ctx->_message_context) {
143 delete ctx->_message_context;
144 ctx->_message_context = 0;
145 }
147 G_OBJECT_CLASS(common_parent_class)->dispose(object);
148 }
151 static void sp_common_context_setup(SPEventContext *ec)
152 {
153 if ( common_parent_class->setup ) {
154 common_parent_class->setup(ec);
155 }
156 }
158 static void sp_common_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *value)
159 {
160 SPCommonContext *ctx = SP_COMMON_CONTEXT(ec);
161 Glib::ustring path = value->getEntryName();
163 // ignore preset modifications
164 static Glib::ustring const presets_path = ec->pref_observer->observed_path + "/preset";
165 Glib::ustring const &full_path = value->getPath();
166 if (full_path.compare(0, presets_path.size(), presets_path) == 0) return;
168 if (path == "mass") {
169 ctx->mass = CLAMP(value->getDouble(0.2), -1000.0, 1000.0);
170 } else if (path == "wiggle") {
171 ctx->drag = CLAMP((1 - value->getDouble(1 - DRAG_DEFAULT)), DRAG_MIN, DRAG_MAX); // drag is inverse to wiggle
172 } else if (path == "angle") {
173 ctx->angle = CLAMP(value->getDouble(), -90, 90);
174 } else if (path == "width") {
175 ctx->width = CLAMP(value->getDouble(0.1), -1000.0, 1000.0);
176 } else if (path == "thinning") {
177 ctx->vel_thin = CLAMP(value->getDouble(0.1), -1.0, 1.0);
178 } else if (path == "tremor") {
179 ctx->tremor = CLAMP(value->getDouble(), 0.0, 1.0);
180 } else if (path == "flatness") {
181 ctx->flatness = CLAMP(value->getDouble(1.0), 0, 1.0);
182 } else if (path == "usepressure") {
183 ctx->usepressure = value->getBool();
184 } else if (path == "usetilt") {
185 ctx->usetilt = value->getBool();
186 } else if (path == "abs_width") {
187 ctx->abs_width = value->getBool();
188 } else if (path == "cap_rounding") {
189 ctx->cap_rounding = value->getDouble();
190 }
191 }
193 static gint sp_common_context_root_handler(SPEventContext *event_context, GdkEvent *event)
194 {
195 gint ret = FALSE;
197 // TODO add common hanlding
200 if ( !ret ) {
201 if ( common_parent_class->root_handler ) {
202 ret = common_parent_class->root_handler(event_context, event);
203 }
204 }
206 return ret;
207 }
209 /*
210 Local Variables:
211 mode:c++
212 c-file-style:"stroustrup"
213 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
214 indent-tabs-mode:nil
215 fill-column:99
216 End:
217 */
218 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :