2 #include "common-context.h"
4 #include <gtk/gtk.h>
6 #include "config.h"
8 #include "forward.h"
9 #include "message-context.h"
11 #define MIN_PRESSURE 0.0
12 #define MAX_PRESSURE 1.0
13 #define DEFAULT_PRESSURE 1.0
15 #define DRAG_MIN 0.0
16 #define DRAG_DEFAULT 1.0
17 #define DRAG_MAX 1.0
20 static void sp_common_context_class_init(SPCommonContextClass *klass);
21 static void sp_common_context_init(SPCommonContext *erc);
22 static void sp_common_context_dispose(GObject *object);
24 static void sp_common_context_setup(SPEventContext *ec);
25 static void sp_common_context_set(SPEventContext *ec, gchar const *key, gchar const *value);
27 static gint sp_common_context_root_handler(SPEventContext *event_context, GdkEvent *event);
30 static SPEventContextClass *common_parent_class = 0;
32 GType sp_common_context_get_type(void)
33 {
34 static GType type = 0;
35 if (!type) {
36 GTypeInfo info = {
37 sizeof(SPCommonContextClass),
38 0, // base_init
39 0, // base_finalize
40 (GClassInitFunc)sp_common_context_class_init,
41 0, // class_finalize
42 0, // class_data
43 sizeof(SPCommonContext),
44 0, // n_preallocs
45 (GInstanceInitFunc)sp_common_context_init,
46 0 // value_table
47 };
48 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPCommonContext", &info, static_cast<GTypeFlags>(0));
49 }
50 return type;
51 }
54 static void sp_common_context_class_init(SPCommonContextClass *klass)
55 {
56 GObjectClass *object_class = (GObjectClass *) klass;
57 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
59 common_parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
61 object_class->dispose = sp_common_context_dispose;
63 event_context_class->setup = sp_common_context_setup;
64 event_context_class->set = sp_common_context_set;
65 event_context_class->root_handler = sp_common_context_root_handler;
66 }
68 static void sp_common_context_init(SPCommonContext *ctx)
69 {
70 // ctx->cursor_shape = cursor_eraser_xpm;
71 // ctx->hot_x = 4;
72 // ctx->hot_y = 4;
74 ctx->accumulated = 0;
75 ctx->segments = 0;
76 ctx->currentcurve = 0;
77 ctx->currentshape = 0;
78 ctx->npoints = 0;
79 ctx->cal1 = 0;
80 ctx->cal2 = 0;
81 ctx->repr = 0;
83 /* Common values */
84 ctx->cur = NR::Point(0,0);
85 ctx->last = NR::Point(0,0);
86 ctx->vel = NR::Point(0,0);
87 ctx->vel_max = 0;
88 ctx->acc = NR::Point(0,0);
89 ctx->ang = NR::Point(0,0);
90 ctx->del = NR::Point(0,0);
92 /* attributes */
93 ctx->dragging = FALSE;
95 ctx->mass = 0.3;
96 ctx->drag = DRAG_DEFAULT;
97 ctx->angle = 30.0;
98 ctx->width = 0.2;
99 ctx->pressure = DEFAULT_PRESSURE;
101 ctx->vel_thin = 0.1;
102 ctx->flatness = 0.9;
103 ctx->cap_rounding = 0.0;
105 ctx->abs_width = false;
106 }
108 static void sp_common_context_dispose(GObject *object)
109 {
110 SPCommonContext *ctx = SP_COMMON_CONTEXT(object);
112 if (ctx->accumulated) {
113 ctx->accumulated = ctx->accumulated->unref();
114 ctx->accumulated = 0;
115 }
117 while (ctx->segments) {
118 gtk_object_destroy(GTK_OBJECT(ctx->segments->data));
119 ctx->segments = g_slist_remove(ctx->segments, ctx->segments->data);
120 }
122 if (ctx->currentcurve) {
123 ctx->currentcurve = ctx->currentcurve->unref();
124 ctx->currentcurve = 0;
125 }
126 if (ctx->cal1) {
127 ctx->cal1 = ctx->cal1->unref();
128 ctx->cal1 = 0;
129 }
130 if (ctx->cal2) {
131 ctx->cal2 = ctx->cal2->unref();
132 ctx->cal2 = 0;
133 }
135 if (ctx->currentshape) {
136 gtk_object_destroy(GTK_OBJECT(ctx->currentshape));
137 ctx->currentshape = 0;
138 }
140 if (ctx->_message_context) {
141 delete ctx->_message_context;
142 ctx->_message_context = 0;
143 }
145 G_OBJECT_CLASS(common_parent_class)->dispose(object);
146 }
149 static void sp_common_context_setup(SPEventContext *ec)
150 {
151 if ( common_parent_class->setup ) {
152 common_parent_class->setup(ec);
153 }
154 }
157 static void sp_common_context_set(SPEventContext *ec, gchar const *key, gchar const *value)
158 {
159 SPCommonContext *ctx = SP_COMMON_CONTEXT(ec);
161 if (!strcmp(key, "mass")) {
162 double const dval = ( value ? g_ascii_strtod (value, NULL) : 0.2 );
163 ctx->mass = CLAMP(dval, -1000.0, 1000.0);
164 } else if (!strcmp(key, "wiggle")) {
165 double const dval = ( value ? g_ascii_strtod (value, NULL) : (1 - DRAG_DEFAULT));
166 ctx->drag = CLAMP((1 - dval), DRAG_MIN, DRAG_MAX); // drag is inverse to wiggle
167 } else if (!strcmp(key, "angle")) {
168 double const dval = ( value ? g_ascii_strtod (value, NULL) : 0.0);
169 ctx->angle = CLAMP (dval, -90, 90);
170 } else if (!strcmp(key, "width")) {
171 double const dval = ( value ? g_ascii_strtod (value, NULL) : 0.1 );
172 ctx->width = CLAMP(dval, -1000.0, 1000.0);
173 } else if (!strcmp(key, "thinning")) {
174 double const dval = ( value ? g_ascii_strtod (value, NULL) : 0.1 );
175 ctx->vel_thin = CLAMP(dval, -1.0, 1.0);
176 } else if (!strcmp(key, "tremor")) {
177 double const dval = ( value ? g_ascii_strtod (value, NULL) : 0.0 );
178 ctx->tremor = CLAMP(dval, 0.0, 1.0);
179 } else if (!strcmp(key, "flatness")) {
180 double const dval = ( value ? g_ascii_strtod (value, NULL) : 1.0 );
181 ctx->flatness = CLAMP(dval, 0, 1.0);
182 } else if (!strcmp(key, "usepressure")) {
183 ctx->usepressure = ( value && strcmp(value, "0"));
184 } else if (!strcmp(key, "usetilt")) {
185 ctx->usetilt = ( value && strcmp(value, "0"));
186 } else if (!strcmp(key, "abs_width")) {
187 ctx->abs_width = ( value && strcmp(value, "0"));
188 } else if (!strcmp(key, "cap_rounding")) {
189 ctx->cap_rounding = ( value ? g_ascii_strtod (value, NULL) : 0.0 );
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 :