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);
73 const int num_subtools = 4;
75 Inkscape::LivePathEffect::EffectType lpesubtools[] = {
76 Inkscape::LivePathEffect::LINE_SEGMENT,
77 Inkscape::LivePathEffect::ANGLE_BISECTOR,
78 Inkscape::LivePathEffect::CIRCLE_3PTS,
79 Inkscape::LivePathEffect::PERP_BISECTOR,
80 };
82 static SPPenContextClass *lpetool_parent_class = 0;
84 GType sp_lpetool_context_get_type(void)
85 {
86 static GType type = 0;
87 if (!type) {
88 GTypeInfo info = {
89 sizeof(SPLPEToolContextClass),
90 0, // base_init
91 0, // base_finalize
92 (GClassInitFunc)sp_lpetool_context_class_init,
93 0, // class_finalize
94 0, // class_data
95 sizeof(SPLPEToolContext),
96 0, // n_preallocs
97 (GInstanceInitFunc)sp_lpetool_context_init,
98 0 // value_table
99 };
100 type = g_type_register_static(SP_TYPE_PEN_CONTEXT, "SPLPEToolContext", &info, static_cast<GTypeFlags>(0));
101 }
102 return type;
103 }
105 static void
106 sp_lpetool_context_class_init(SPLPEToolContextClass *klass)
107 {
108 GObjectClass *object_class = (GObjectClass *) klass;
109 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
111 lpetool_parent_class = (SPPenContextClass*)g_type_class_peek_parent(klass);
113 object_class->dispose = sp_lpetool_context_dispose;
115 event_context_class->setup = sp_lpetool_context_setup;
116 event_context_class->set = sp_lpetool_context_set;
117 event_context_class->root_handler = sp_lpetool_context_root_handler;
118 }
120 static void
121 sp_lpetool_context_init(SPLPEToolContext *lc)
122 {
123 /**
124 lc->NodeContextCpp::cursor_shape = cursor_pencil_xpm;
125 lc->NodeContextCpp::hot_x = 4;
126 lc->NodeContextCpp::hot_y = 4;
127 **/
128 lc->cursor_shape = cursor_pencil_xpm;
129 lc->hot_x = 4;
130 lc->hot_y = 4;
132 //lc->tool_state = LPETOOL_STATE_NODE;
133 }
135 static void
136 sp_lpetool_context_dispose(GObject *object)
137 {
138 //SPLPEToolContext *erc = SP_LPETOOL_CONTEXT(object);
140 G_OBJECT_CLASS(lpetool_parent_class)->dispose(object);
141 }
143 static void
144 sp_lpetool_context_setup(SPEventContext *ec)
145 {
146 SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(ec);
148 if (((SPEventContextClass *) lpetool_parent_class)->setup)
149 ((SPEventContextClass *) lpetool_parent_class)->setup(ec);
151 lc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
153 //lc->my_nc = new NodeContextCpp(lc->desktop, lc->prefs_repr, lc->key);
155 if (prefs_get_int_attribute("tools.lpetool", "selcue", 0) != 0) {
156 ec->enableSelectionCue();
157 }
158 // TODO temp force:
159 ec->enableSelectionCue();
160 }
162 static void
163 sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
164 {
165 // FIXME: how to set this correcly? the value from preferences-skeleton.h doesn't seem to get
166 // read (it wants to set drag = 1)
167 lpetool_parent_class->set(ec, key, "drag");
169 /**
170 //pass on up to parent class to handle common attributes.
171 if ( lpetool_parent_class->set ) {
172 lpetool_parent_class->set(ec, key, val);
173 }
174 **/
175 }
177 /**
178 void
179 sp_erc_update_toolbox (SPDesktop *desktop, const gchar *id, double value)
180 {
181 desktop->setToolboxAdjustmentValue (id, value);
182 }
183 **/
185 gint
186 sp_lpetool_context_root_handler(SPEventContext *event_context, GdkEvent *event)
187 {
188 SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(event_context);
189 //SPDesktop *desktop = event_context->desktop;
191 //gint ret = FALSE;
192 bool ret = false;
194 if (sp_pen_context_has_waiting_LPE(lc)) {
195 // quit when we are waiting for a LPE to be applied
196 g_print ("LPETool has waiting LPE. We call the pen tool parent context and return\n");
198 if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
199 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
200 }
202 return ret;
203 }
205 switch (event->type) {
206 case GDK_BUTTON_PRESS:
207 {
208 using namespace Inkscape::LivePathEffect;
210 int mode = prefs_get_int_attribute("tools.lpetool", "mode", 0);
211 EffectType type = lpesubtools[mode];
212 g_print ("Activating mode %d\n", mode);
214 sp_pen_context_wait_for_LPE_mouse_clicks(lc, type, Effect::acceptsNumClicks(type));
216 // we pass the mouse click on to pen tool as the first click which it should collect
217 if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
218 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
219 }
221 ret = true;
222 break;
223 }
224 /**
225 if (event->button.button == 1 && !event_context->space_panning) {
226 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
228 NR::Point const button_w(event->button.x,
229 event->button.y);
230 NR::Point const button_dt(desktop->w2d(button_w));
233 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
234 return TRUE;
235 }
237 sp_lpetool_reset(dc, button_dt);
238 sp_lpetool_extinput(dc, event);
239 sp_lpetool_apply(dc, button_dt);
240 dc->accumulated->reset();
241 if (dc->repr) {
242 dc->repr = NULL;
243 }
245 Inkscape::Rubberband::get()->start(desktop, button_dt);
246 Inkscape::Rubberband::get()->setMode(RUBBERBAND_MODE_TOUCHPATH);
248 // initialize first point
249 dc->npoints = 0;
251 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
252 ( GDK_KEY_PRESS_MASK |
253 GDK_BUTTON_RELEASE_MASK |
254 GDK_POINTER_MOTION_MASK |
255 GDK_BUTTON_PRESS_MASK ),
256 NULL,
257 event->button.time);
259 ret = TRUE;
261 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
262 dc->is_drawing = true;
263 }
264 **/
265 break;
266 case GDK_MOTION_NOTIFY:
267 /**
268 {
269 NR::Point const motion_w(event->motion.x,
270 event->motion.y);
271 NR::Point motion_dt(desktop->w2d(motion_w));
272 sp_lpetool_extinput(dc, event);
274 dc->_message_context->clear();
276 if ( dc->is_drawing && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
277 dc->dragging = TRUE;
279 dc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drawing</b> an lpetool stroke"));
281 if (!sp_lpetool_apply(dc, motion_dt)) {
282 ret = TRUE;
283 break;
284 }
286 if ( dc->cur != dc->last ) {
287 sp_lpetool_brush(dc);
288 g_assert( dc->npoints > 0 );
289 fit_and_split(dc, FALSE);
290 }
291 ret = TRUE;
292 }
293 Inkscape::Rubberband::get()->move(motion_dt);
294 }
295 **/
296 break;
299 case GDK_BUTTON_RELEASE:
300 /**
301 {
302 NR::Point const motion_w(event->button.x, event->button.y);
303 NR::Point const motion_dt(desktop->w2d(motion_w));
305 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
306 sp_canvas_end_forced_full_redraws(desktop->canvas);
307 dc->is_drawing = false;
309 if (dc->dragging && event->button.button == 1 && !event_context->space_panning) {
310 dc->dragging = FALSE;
312 NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
314 sp_lpetool_apply(dc, motion_dt);
316 // Remove all temporary line segments
317 while (dc->segments) {
318 gtk_object_destroy(GTK_OBJECT(dc->segments->data));
319 dc->segments = g_slist_remove(dc->segments, dc->segments->data);
320 }
322 // Create object
323 fit_and_split(dc, TRUE);
324 accumulate_lpetool(dc);
325 set_to_accumulated(dc); // performs document_done
327 // reset accumulated curve
328 dc->accumulated->reset();
330 clear_current(dc);
331 if (dc->repr) {
332 dc->repr = NULL;
333 }
335 Inkscape::Rubberband::get()->stop();
336 dc->_message_context->clear();
337 ret = TRUE;
338 }
339 break;
340 }
341 **/
343 case GDK_KEY_PRESS:
344 /**
345 switch (get_group0_keyval (&event->key)) {
346 case GDK_Up:
347 case GDK_KP_Up:
348 if (!MOD__CTRL_ONLY) {
349 dc->angle += 5.0;
350 if (dc->angle > 90.0)
351 dc->angle = 90.0;
352 sp_erc_update_toolbox (desktop, "lpetool-angle", dc->angle);
353 ret = TRUE;
354 }
355 break;
356 case GDK_Down:
357 case GDK_KP_Down:
358 if (!MOD__CTRL_ONLY) {
359 dc->angle -= 5.0;
360 if (dc->angle < -90.0)
361 dc->angle = -90.0;
362 sp_erc_update_toolbox (desktop, "lpetool-angle", dc->angle);
363 ret = TRUE;
364 }
365 break;
366 case GDK_Right:
367 case GDK_KP_Right:
368 if (!MOD__CTRL_ONLY) {
369 dc->width += 0.01;
370 if (dc->width > 1.0)
371 dc->width = 1.0;
372 sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100); // the same spinbutton is for alt+x
373 ret = TRUE;
374 }
375 break;
376 case GDK_Left:
377 case GDK_KP_Left:
378 if (!MOD__CTRL_ONLY) {
379 dc->width -= 0.01;
380 if (dc->width < 0.01)
381 dc->width = 0.01;
382 sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
383 ret = TRUE;
384 }
385 break;
386 case GDK_Home:
387 case GDK_KP_Home:
388 dc->width = 0.01;
389 sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
390 ret = TRUE;
391 break;
392 case GDK_End:
393 case GDK_KP_End:
394 dc->width = 1.0;
395 sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
396 ret = TRUE;
397 break;
398 case GDK_x:
399 case GDK_X:
400 if (MOD__ALT_ONLY) {
401 desktop->setToolboxFocusTo ("altx-lpetool");
402 ret = TRUE;
403 }
404 break;
405 case GDK_Escape:
406 Inkscape::Rubberband::get()->stop();
407 if (dc->is_drawing) {
408 // if drawing, cancel, otherwise pass it up for deselecting
409 lpetool_cancel (dc);
410 ret = TRUE;
411 }
412 break;
413 case GDK_z:
414 case GDK_Z:
415 if (MOD__CTRL_ONLY && dc->is_drawing) {
416 // if drawing, cancel, otherwise pass it up for undo
417 lpetool_cancel (dc);
418 ret = TRUE;
419 }
420 break;
421 default:
422 break;
423 }
424 **/
425 break;
427 case GDK_KEY_RELEASE:
428 /**
429 switch (get_group0_keyval(&event->key)) {
430 case GDK_Control_L:
431 case GDK_Control_R:
432 dc->_message_context->clear();
433 break;
434 default:
435 break;
436 }
437 **/
439 default:
440 break;
441 }
443 if (!ret) {
444 if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
445 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
446 }
447 }
449 return ret;
450 }
452 /*
453 Local Variables:
454 mode:c++
455 c-file-style:"stroustrup"
456 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
457 indent-tabs-mode:nil
458 fill-column:99
459 End:
460 */
461 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :