1 /*
2 * LPEToolContext: a context for a generic tool composed of subtools that are given by LPEs
3 *
4 * Authors:
5 * Maximilian Albert <maximilian.albert@gmail.com>
6 * Lauris Kaplinski <lauris@kaplinski.com>
7 *
8 * Copyright (C) 1998 The Free Software Foundation
9 * Copyright (C) 1999-2005 authors
10 * Copyright (C) 2001-2002 Ximian, Inc.
11 * Copyright (C) 2008 Maximilian Albert
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #include "config.h"
18 #include "forward.h"
19 #include "pixmaps/cursor-pencil.xpm"
20 #include <gtk/gtk.h>
21 #include "desktop.h"
22 #include "message-context.h"
23 #include "prefs-utils.h"
25 /**
27 #include <gdk/gdkkeysyms.h>
28 #include <glibmm/i18n.h>
29 #include <string>
30 #include <cstring>
31 #include <numeric>
33 #include "svg/svg.h"
34 #include "display/canvas-bpath.h"
35 #include "display/bezier-utils.h"
37 #include <glib/gmem.h>
38 #include "macros.h"
39 #include "document.h"
40 #include "selection.h"
41 #include "desktop-events.h"
42 #include "desktop-handles.h"
43 #include "desktop-affine.h"
44 #include "desktop-style.h"
45 #include "xml/repr.h"
46 #include "context-fns.h"
47 #include "sp-item.h"
48 #include "inkscape.h"
49 #include "color.h"
50 #include "rubberband.h"
51 #include "splivarot.h"
52 #include "sp-item-group.h"
53 #include "sp-shape.h"
54 #include "sp-path.h"
55 #include "sp-text.h"
56 #include "display/canvas-bpath.h"
57 #include "display/canvas-arena.h"
58 #include "livarot/Shape.h"
59 #include <2geom/isnan.h>
60 #include <2geom/pathvector.h>
61 **/
63 #include "lpe-tool-context.h"
65 static void sp_lpetool_context_class_init(SPLPEToolContextClass *klass);
66 static void sp_lpetool_context_init(SPLPEToolContext *erc);
67 static void sp_lpetool_context_dispose(GObject *object);
69 static void sp_lpetool_context_setup(SPEventContext *ec);
70 static void sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
71 static gint sp_lpetool_context_root_handler(SPEventContext *ec, GdkEvent *event);
74 static SPPenContextClass *lpetool_parent_class = 0;
76 GType sp_lpetool_context_get_type(void)
77 {
78 static GType type = 0;
79 if (!type) {
80 GTypeInfo info = {
81 sizeof(SPLPEToolContextClass),
82 0, // base_init
83 0, // base_finalize
84 (GClassInitFunc)sp_lpetool_context_class_init,
85 0, // class_finalize
86 0, // class_data
87 sizeof(SPLPEToolContext),
88 0, // n_preallocs
89 (GInstanceInitFunc)sp_lpetool_context_init,
90 0 // value_table
91 };
92 type = g_type_register_static(SP_TYPE_PEN_CONTEXT, "SPLPEToolContext", &info, static_cast<GTypeFlags>(0));
93 }
94 return type;
95 }
97 static void
98 sp_lpetool_context_class_init(SPLPEToolContextClass *klass)
99 {
100 GObjectClass *object_class = (GObjectClass *) klass;
101 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
103 lpetool_parent_class = (SPPenContextClass*)g_type_class_peek_parent(klass);
105 object_class->dispose = sp_lpetool_context_dispose;
107 event_context_class->setup = sp_lpetool_context_setup;
108 event_context_class->set = sp_lpetool_context_set;
109 //event_context_class->root_handler = sp_lpetool_context_root_handler;
110 }
112 static void
113 sp_lpetool_context_init(SPLPEToolContext *erc)
114 {
115 erc->cursor_shape = cursor_pencil_xpm;
116 erc->hot_x = 4;
117 erc->hot_y = 4;
118 }
120 static void
121 sp_lpetool_context_dispose(GObject *object)
122 {
123 //SPLPEToolContext *erc = SP_LPETOOL_CONTEXT(object);
125 G_OBJECT_CLASS(lpetool_parent_class)->dispose(object);
126 }
128 static void
129 sp_lpetool_context_setup(SPEventContext *ec)
130 {
131 SPLPEToolContext *erc = SP_LPETOOL_CONTEXT(ec);
133 if (((SPEventContextClass *) lpetool_parent_class)->setup)
134 ((SPEventContextClass *) lpetool_parent_class)->setup(ec);
136 erc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
138 if (prefs_get_int_attribute("tools.lpetool", "selcue", 0) != 0) {
139 ec->enableSelectionCue();
140 }
141 // TODO temp force:
142 ec->enableSelectionCue();
143 }
145 static void
146 sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
147 {
148 // FIXME: how to set this correcly? the value from preferences-skeleton.h doesn't seem to get
149 // read (it wants to set drag = 1)
150 lpetool_parent_class->set(ec, key, "drag");
152 /**
153 //pass on up to parent class to handle common attributes.
154 if ( lpetool_parent_class->set ) {
155 lpetool_parent_class->set(ec, key, val);
156 }
157 **/
158 }
160 /**
161 void
162 sp_erc_update_toolbox (SPDesktop *desktop, const gchar *id, double value)
163 {
164 desktop->setToolboxAdjustmentValue (id, value);
165 }
166 **/
168 gint
169 sp_lpetool_context_root_handler(SPEventContext *event_context,
170 GdkEvent *event)
171 {
172 //SPLPEToolContext *dc = SP_LPETOOL_CONTEXT(event_context);
173 //SPDesktop *desktop = event_context->desktop;
175 gint ret = FALSE;
177 /**
178 switch (event->type) {
179 case GDK_BUTTON_PRESS:
180 if (event->button.button == 1 && !event_context->space_panning) {
182 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
184 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
185 return TRUE;
186 }
188 NR::Point const button_w(event->button.x,
189 event->button.y);
190 NR::Point const button_dt(desktop->w2d(button_w));
191 sp_lpetool_reset(dc, button_dt);
192 sp_lpetool_extinput(dc, event);
193 sp_lpetool_apply(dc, button_dt);
194 dc->accumulated->reset();
195 if (dc->repr) {
196 dc->repr = NULL;
197 }
199 Inkscape::Rubberband::get()->start(desktop, button_dt);
200 Inkscape::Rubberband::get()->setMode(RUBBERBAND_MODE_TOUCHPATH);
202 // initialize first point
203 dc->npoints = 0;
205 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
206 ( GDK_KEY_PRESS_MASK |
207 GDK_BUTTON_RELEASE_MASK |
208 GDK_POINTER_MOTION_MASK |
209 GDK_BUTTON_PRESS_MASK ),
210 NULL,
211 event->button.time);
213 ret = TRUE;
215 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
216 dc->is_drawing = true;
217 }
218 break;
219 case GDK_MOTION_NOTIFY:
220 {
221 NR::Point const motion_w(event->motion.x,
222 event->motion.y);
223 NR::Point motion_dt(desktop->w2d(motion_w));
224 sp_lpetool_extinput(dc, event);
226 dc->_message_context->clear();
228 if ( dc->is_drawing && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
229 dc->dragging = TRUE;
231 dc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drawing</b> an lpetool stroke"));
233 if (!sp_lpetool_apply(dc, motion_dt)) {
234 ret = TRUE;
235 break;
236 }
238 if ( dc->cur != dc->last ) {
239 sp_lpetool_brush(dc);
240 g_assert( dc->npoints > 0 );
241 fit_and_split(dc, FALSE);
242 }
243 ret = TRUE;
244 }
245 Inkscape::Rubberband::get()->move(motion_dt);
246 }
247 break;
250 case GDK_BUTTON_RELEASE:
251 {
252 NR::Point const motion_w(event->button.x, event->button.y);
253 NR::Point const motion_dt(desktop->w2d(motion_w));
255 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
256 sp_canvas_end_forced_full_redraws(desktop->canvas);
257 dc->is_drawing = false;
259 if (dc->dragging && event->button.button == 1 && !event_context->space_panning) {
260 dc->dragging = FALSE;
262 NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
264 sp_lpetool_apply(dc, motion_dt);
266 // Remove all temporary line segments
267 while (dc->segments) {
268 gtk_object_destroy(GTK_OBJECT(dc->segments->data));
269 dc->segments = g_slist_remove(dc->segments, dc->segments->data);
270 }
272 // Create object
273 fit_and_split(dc, TRUE);
274 accumulate_lpetool(dc);
275 set_to_accumulated(dc); // performs document_done
277 // reset accumulated curve
278 dc->accumulated->reset();
280 clear_current(dc);
281 if (dc->repr) {
282 dc->repr = NULL;
283 }
285 Inkscape::Rubberband::get()->stop();
286 dc->_message_context->clear();
287 ret = TRUE;
288 }
289 break;
290 }
292 case GDK_KEY_PRESS:
293 switch (get_group0_keyval (&event->key)) {
294 case GDK_Up:
295 case GDK_KP_Up:
296 if (!MOD__CTRL_ONLY) {
297 dc->angle += 5.0;
298 if (dc->angle > 90.0)
299 dc->angle = 90.0;
300 sp_erc_update_toolbox (desktop, "lpetool-angle", dc->angle);
301 ret = TRUE;
302 }
303 break;
304 case GDK_Down:
305 case GDK_KP_Down:
306 if (!MOD__CTRL_ONLY) {
307 dc->angle -= 5.0;
308 if (dc->angle < -90.0)
309 dc->angle = -90.0;
310 sp_erc_update_toolbox (desktop, "lpetool-angle", dc->angle);
311 ret = TRUE;
312 }
313 break;
314 case GDK_Right:
315 case GDK_KP_Right:
316 if (!MOD__CTRL_ONLY) {
317 dc->width += 0.01;
318 if (dc->width > 1.0)
319 dc->width = 1.0;
320 sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100); // the same spinbutton is for alt+x
321 ret = TRUE;
322 }
323 break;
324 case GDK_Left:
325 case GDK_KP_Left:
326 if (!MOD__CTRL_ONLY) {
327 dc->width -= 0.01;
328 if (dc->width < 0.01)
329 dc->width = 0.01;
330 sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
331 ret = TRUE;
332 }
333 break;
334 case GDK_Home:
335 case GDK_KP_Home:
336 dc->width = 0.01;
337 sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
338 ret = TRUE;
339 break;
340 case GDK_End:
341 case GDK_KP_End:
342 dc->width = 1.0;
343 sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
344 ret = TRUE;
345 break;
346 case GDK_x:
347 case GDK_X:
348 if (MOD__ALT_ONLY) {
349 desktop->setToolboxFocusTo ("altx-lpetool");
350 ret = TRUE;
351 }
352 break;
353 case GDK_Escape:
354 Inkscape::Rubberband::get()->stop();
355 if (dc->is_drawing) {
356 // if drawing, cancel, otherwise pass it up for deselecting
357 lpetool_cancel (dc);
358 ret = TRUE;
359 }
360 break;
361 case GDK_z:
362 case GDK_Z:
363 if (MOD__CTRL_ONLY && dc->is_drawing) {
364 // if drawing, cancel, otherwise pass it up for undo
365 lpetool_cancel (dc);
366 ret = TRUE;
367 }
368 break;
369 default:
370 break;
371 }
372 break;
374 case GDK_KEY_RELEASE:
375 switch (get_group0_keyval(&event->key)) {
376 case GDK_Control_L:
377 case GDK_Control_R:
378 dc->_message_context->clear();
379 break;
380 default:
381 break;
382 }
384 default:
385 break;
386 }
387 **/
389 if (!ret) {
390 if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
391 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
392 }
393 }
395 return ret;
396 }
398 /*
399 Local Variables:
400 mode:c++
401 c-file-style:"stroustrup"
402 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
403 indent-tabs-mode:nil
404 fill-column:99
405 End:
406 */
407 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :