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-node.xpm"
20 #include "pixmaps/cursor-crosshairs.xpm"
21 #include <gtk/gtk.h>
22 #include "desktop.h"
23 #include "message-context.h"
24 #include "prefs-utils.h"
25 #include "shape-editor.h"
26 #include "selection.h"
27 #include "desktop-handles.h"
29 /**
31 #include <gdk/gdkkeysyms.h>
32 #include <glibmm/i18n.h>
33 #include <string>
34 #include <cstring>
35 #include <numeric>
37 #include "svg/svg.h"
38 #include "display/canvas-bpath.h"
39 #include "display/bezier-utils.h"
41 #include <glib/gmem.h>
42 #include "macros.h"
43 #include "document.h"
44 #include "selection.h"
45 #include "desktop-events.h"
46 #include "desktop-handles.h"
47 #include "desktop-affine.h"
48 #include "desktop-style.h"
49 #include "xml/repr.h"
50 #include "context-fns.h"
51 #include "sp-item.h"
52 #include "inkscape.h"
53 #include "color.h"
54 #include "rubberband.h"
55 #include "splivarot.h"
56 #include "sp-item-group.h"
57 #include "sp-shape.h"
58 #include "sp-path.h"
59 #include "sp-text.h"
60 #include "display/canvas-bpath.h"
61 #include "display/canvas-arena.h"
62 #include "livarot/Shape.h"
63 #include <2geom/isnan.h>
64 #include <2geom/pathvector.h>
65 **/
67 #include "lpe-tool-context.h"
69 static void sp_lpetool_context_class_init(SPLPEToolContextClass *klass);
70 static void sp_lpetool_context_init(SPLPEToolContext *erc);
71 static void sp_lpetool_context_dispose(GObject *object);
73 static void sp_lpetool_context_setup(SPEventContext *ec);
74 static void sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
75 static gint sp_lpetool_context_root_handler(SPEventContext *ec, GdkEvent *event);
77 void sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data);
79 const int num_subtools = 4;
81 Inkscape::LivePathEffect::EffectType lpesubtools[] = {
82 Inkscape::LivePathEffect::LINE_SEGMENT,
83 Inkscape::LivePathEffect::ANGLE_BISECTOR,
84 Inkscape::LivePathEffect::CIRCLE_3PTS,
85 Inkscape::LivePathEffect::PERP_BISECTOR,
86 };
88 static SPPenContextClass *lpetool_parent_class = 0;
90 GType sp_lpetool_context_get_type(void)
91 {
92 static GType type = 0;
93 if (!type) {
94 GTypeInfo info = {
95 sizeof(SPLPEToolContextClass),
96 0, // base_init
97 0, // base_finalize
98 (GClassInitFunc)sp_lpetool_context_class_init,
99 0, // class_finalize
100 0, // class_data
101 sizeof(SPLPEToolContext),
102 0, // n_preallocs
103 (GInstanceInitFunc)sp_lpetool_context_init,
104 0 // value_table
105 };
106 type = g_type_register_static(SP_TYPE_PEN_CONTEXT, "SPLPEToolContext", &info, static_cast<GTypeFlags>(0));
107 }
108 return type;
109 }
111 static void
112 sp_lpetool_context_class_init(SPLPEToolContextClass *klass)
113 {
114 GObjectClass *object_class = (GObjectClass *) klass;
115 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
117 lpetool_parent_class = (SPPenContextClass*)g_type_class_peek_parent(klass);
119 object_class->dispose = sp_lpetool_context_dispose;
121 event_context_class->setup = sp_lpetool_context_setup;
122 event_context_class->set = sp_lpetool_context_set;
123 event_context_class->root_handler = sp_lpetool_context_root_handler;
124 }
126 static void
127 sp_lpetool_context_init(SPLPEToolContext *lc)
128 {
129 lc->cursor_shape = cursor_crosshairs_xpm;
130 lc->hot_x = 7;
131 lc->hot_y = 7;
133 new (&lc->sel_changed_connection) sigc::connection();
135 //lc->tool_state = LPETOOL_STATE_NODE;
136 }
138 static void
139 sp_lpetool_context_dispose(GObject *object)
140 {
141 SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(object);
142 delete lc->shape_editor;
144 lc->sel_changed_connection.disconnect();
145 lc->sel_changed_connection.~connection();
147 G_OBJECT_CLASS(lpetool_parent_class)->dispose(object);
148 }
150 static void
151 sp_lpetool_context_setup(SPEventContext *ec)
152 {
153 SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(ec);
155 if (((SPEventContextClass *) lpetool_parent_class)->setup)
156 ((SPEventContextClass *) lpetool_parent_class)->setup(ec);
158 Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
159 SPItem *item = selection->singleItem();
161 lc->sel_changed_connection.disconnect();
162 lc->sel_changed_connection =
163 selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_lpetool_context_selection_changed), (gpointer)lc));
165 //lc->my_nc = new NodeContextCpp(lc->desktop, lc->prefs_repr, lc->key);
166 lc->shape_editor = new ShapeEditor(ec->desktop);
168 // TODO temp force:
169 ec->enableSelectionCue();
171 if (item) {
172 lc->shape_editor->set_item(item, SH_NODEPATH);
173 lc->shape_editor->set_item(item, SH_KNOTHOLDER);
174 }
176 if (prefs_get_int_attribute("tools.lpetool", "selcue", 0) != 0) {
177 ec->enableSelectionCue();
178 }
180 lc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
182 lc->shape_editor->update_statusbar();
183 }
185 /**
186 \brief Callback that processes the "changed" signal on the selection;
187 destroys old and creates new nodepath and reassigns listeners to the new selected item's repr
188 */
189 void
190 sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data)
191 {
192 SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(data);
194 // TODO: update ShapeEditorsCollective instead
195 lc->shape_editor->unset_item(SH_NODEPATH);
196 lc->shape_editor->unset_item(SH_KNOTHOLDER);
197 SPItem *item = selection->singleItem();
198 lc->shape_editor->set_item(item, SH_NODEPATH);
199 lc->shape_editor->set_item(item, SH_KNOTHOLDER);
200 lc->shape_editor->update_statusbar();
201 }
203 static void
204 sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
205 {
206 // FIXME: how to set this correcly? the value from preferences-skeleton.h doesn't seem to get
207 // read (it wants to set drag = 1)
208 lpetool_parent_class->set(ec, key, "drag");
210 /**
211 //pass on up to parent class to handle common attributes.
212 if ( lpetool_parent_class->set ) {
213 lpetool_parent_class->set(ec, key, val);
214 }
215 **/
216 }
218 /**
219 void
220 sp_erc_update_toolbox (SPDesktop *desktop, const gchar *id, double value)
221 {
222 desktop->setToolboxAdjustmentValue (id, value);
223 }
224 **/
226 gint
227 sp_lpetool_context_root_handler(SPEventContext *event_context, GdkEvent *event)
228 {
229 //g_print ("sp_lpetool_context_root_handler()\n");
230 SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(event_context);
231 SPDesktop *desktop = event_context->desktop;
232 Inkscape::Selection *selection = sp_desktop_selection (desktop);
234 //gint ret = FALSE;
235 bool ret = false;
237 if (sp_pen_context_has_waiting_LPE(lc)) {
238 // quit when we are waiting for a LPE to be applied
239 g_print ("LPETool has waiting LPE. We call the pen tool parent context and return\n");
240 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
241 return ret;
242 }
244 switch (event->type) {
245 case GDK_BUTTON_PRESS:
246 g_print ("GDK_BUTTON_PRESS\n");
247 if (event->button.button == 1 && !event_context->space_panning) {
248 g_print (" ... (passed if construct)\n");
249 // save drag origin
250 event_context->xp = (gint) event->button.x;
251 event_context->yp = (gint) event->button.y;
252 event_context->within_tolerance = true;
253 lc->shape_editor->cancel_hit();
255 using namespace Inkscape::LivePathEffect;
257 int mode = prefs_get_int_attribute("tools.lpetool", "mode", 0);
258 EffectType type = lpesubtools[mode];
259 g_print ("Activating mode %d\n", mode);
261 // save drag origin
262 bool over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->button.x, event->button.y), true);
263 g_print ("over_stroke: %s\n", over_stroke ? "true" : "false");
265 sp_pen_context_wait_for_LPE_mouse_clicks(lc, type, Effect::acceptsNumClicks(type));
267 // we pass the mouse click on to pen tool as the first click which it should collect
268 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
270 /**
271 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
273 NR::Point const button_w(event->button.x,
274 event->button.y);
275 NR::Point const button_dt(desktop->w2d(button_w));
277 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
278 return TRUE;
279 }
281 sp_lpetool_reset(dc, button_dt);
282 sp_lpetool_extinput(dc, event);
283 sp_lpetool_apply(dc, button_dt);
284 dc->accumulated->reset();
285 if (dc->repr) {
286 dc->repr = NULL;
287 }
289 Inkscape::Rubberband::get()->start(desktop, button_dt);
290 Inkscape::Rubberband::get()->setMode(RUBBERBAND_MODE_TOUCHPATH);
292 // initialize first point
293 dc->npoints = 0;
295 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
296 ( GDK_KEY_PRESS_MASK |
297 GDK_BUTTON_RELEASE_MASK |
298 GDK_POINTER_MOTION_MASK |
299 GDK_BUTTON_PRESS_MASK ),
300 NULL,
301 event->button.time);
303 ret = TRUE;
305 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
306 dc->is_drawing = true;
307 **/
308 }
309 break;
310 case GDK_MOTION_NOTIFY:
311 {
312 if (!lc->shape_editor->has_nodepath() || selection->singleItem() == NULL) {
313 break;
314 }
316 bool over_stroke = false;
317 over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->motion.x, event->motion.y), false);
319 if (over_stroke) {
320 event_context->cursor_shape = cursor_node_xpm;
321 event_context->hot_x = 1;
322 event_context->hot_y = 1;
323 sp_event_context_update_cursor(event_context);
324 } else {
325 lc->cursor_shape = cursor_crosshairs_xpm;
326 lc->hot_x = 7;
327 lc->hot_y = 7;
328 sp_event_context_update_cursor(event_context);
329 }
330 /**
331 {
332 NR::Point const motion_w(event->motion.x,
333 event->motion.y);
334 NR::Point motion_dt(desktop->w2d(motion_w));
335 sp_lpetool_extinput(dc, event);
337 dc->_message_context->clear();
339 if ( dc->is_drawing && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
340 dc->dragging = TRUE;
342 dc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drawing</b> an lpetool stroke"));
344 if (!sp_lpetool_apply(dc, motion_dt)) {
345 ret = TRUE;
346 break;
347 }
349 if ( dc->cur != dc->last ) {
350 sp_lpetool_brush(dc);
351 g_assert( dc->npoints > 0 );
352 fit_and_split(dc, FALSE);
353 }
354 ret = TRUE;
355 }
356 Inkscape::Rubberband::get()->move(motion_dt);
357 }
358 **/
359 }
360 break;
363 case GDK_BUTTON_RELEASE:
364 /**
365 {
366 NR::Point const motion_w(event->button.x, event->button.y);
367 NR::Point const motion_dt(desktop->w2d(motion_w));
369 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
370 sp_canvas_end_forced_full_redraws(desktop->canvas);
371 dc->is_drawing = false;
373 if (dc->dragging && event->button.button == 1 && !event_context->space_panning) {
374 dc->dragging = FALSE;
376 NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
378 sp_lpetool_apply(dc, motion_dt);
380 // Remove all temporary line segments
381 while (dc->segments) {
382 gtk_object_destroy(GTK_OBJECT(dc->segments->data));
383 dc->segments = g_slist_remove(dc->segments, dc->segments->data);
384 }
386 // Create object
387 fit_and_split(dc, TRUE);
388 accumulate_lpetool(dc);
389 set_to_accumulated(dc); // performs document_done
391 // reset accumulated curve
392 dc->accumulated->reset();
394 clear_current(dc);
395 if (dc->repr) {
396 dc->repr = NULL;
397 }
399 Inkscape::Rubberband::get()->stop();
400 dc->_message_context->clear();
401 ret = TRUE;
402 }
403 break;
404 }
405 **/
407 case GDK_KEY_PRESS:
408 /**
409 switch (get_group0_keyval (&event->key)) {
410 case GDK_Up:
411 case GDK_KP_Up:
412 if (!MOD__CTRL_ONLY) {
413 dc->angle += 5.0;
414 if (dc->angle > 90.0)
415 dc->angle = 90.0;
416 sp_erc_update_toolbox (desktop, "lpetool-angle", dc->angle);
417 ret = TRUE;
418 }
419 break;
420 case GDK_Down:
421 case GDK_KP_Down:
422 if (!MOD__CTRL_ONLY) {
423 dc->angle -= 5.0;
424 if (dc->angle < -90.0)
425 dc->angle = -90.0;
426 sp_erc_update_toolbox (desktop, "lpetool-angle", dc->angle);
427 ret = TRUE;
428 }
429 break;
430 case GDK_Right:
431 case GDK_KP_Right:
432 if (!MOD__CTRL_ONLY) {
433 dc->width += 0.01;
434 if (dc->width > 1.0)
435 dc->width = 1.0;
436 sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100); // the same spinbutton is for alt+x
437 ret = TRUE;
438 }
439 break;
440 case GDK_Left:
441 case GDK_KP_Left:
442 if (!MOD__CTRL_ONLY) {
443 dc->width -= 0.01;
444 if (dc->width < 0.01)
445 dc->width = 0.01;
446 sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
447 ret = TRUE;
448 }
449 break;
450 case GDK_Home:
451 case GDK_KP_Home:
452 dc->width = 0.01;
453 sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
454 ret = TRUE;
455 break;
456 case GDK_End:
457 case GDK_KP_End:
458 dc->width = 1.0;
459 sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
460 ret = TRUE;
461 break;
462 case GDK_x:
463 case GDK_X:
464 if (MOD__ALT_ONLY) {
465 desktop->setToolboxFocusTo ("altx-lpetool");
466 ret = TRUE;
467 }
468 break;
469 case GDK_Escape:
470 Inkscape::Rubberband::get()->stop();
471 if (dc->is_drawing) {
472 // if drawing, cancel, otherwise pass it up for deselecting
473 lpetool_cancel (dc);
474 ret = TRUE;
475 }
476 break;
477 case GDK_z:
478 case GDK_Z:
479 if (MOD__CTRL_ONLY && dc->is_drawing) {
480 // if drawing, cancel, otherwise pass it up for undo
481 lpetool_cancel (dc);
482 ret = TRUE;
483 }
484 break;
485 default:
486 break;
487 }
488 **/
489 break;
491 case GDK_KEY_RELEASE:
492 /**
493 switch (get_group0_keyval(&event->key)) {
494 case GDK_Control_L:
495 case GDK_Control_R:
496 dc->_message_context->clear();
497 break;
498 default:
499 break;
500 }
501 **/
503 default:
504 break;
505 }
507 if (!ret) {
508 if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
509 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
510 }
511 }
513 return ret;
514 }
516 /*
517 Local Variables:
518 mode:c++
519 c-file-style:"stroustrup"
520 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
521 indent-tabs-mode:nil
522 fill-column:99
523 End:
524 */
525 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :