Code

Change cursor in LPETool when over stroke; call root handler of parent context, too...
[inkscape.git] / src / lpe-tool-context.cpp
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 "pixmaps/cursor-node.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;
111 static void
112 sp_lpetool_context_class_init(SPLPEToolContextClass *klass)
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;
126 static void
127 sp_lpetool_context_init(SPLPEToolContext *lc)
129     /**
130     lc->NodeContextCpp::cursor_shape = cursor_pencil_xpm;
131     lc->NodeContextCpp::hot_x = 4;
132     lc->NodeContextCpp::hot_y = 4;
133     **/
134     lc->cursor_shape = cursor_pencil_xpm;
135     lc->hot_x = 4;
136     lc->hot_y = 4;
138     new (&lc->sel_changed_connection) sigc::connection();
140     //lc->tool_state = LPETOOL_STATE_NODE;
143 static void
144 sp_lpetool_context_dispose(GObject *object)
146     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(object);
147     delete lc->shape_editor;
149     lc->sel_changed_connection.disconnect();
150     lc->sel_changed_connection.~connection();
152     G_OBJECT_CLASS(lpetool_parent_class)->dispose(object);
155 static void
156 sp_lpetool_context_setup(SPEventContext *ec)
158     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(ec);
160     if (((SPEventContextClass *) lpetool_parent_class)->setup)
161         ((SPEventContextClass *) lpetool_parent_class)->setup(ec);
163     Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
164     SPItem *item = selection->singleItem();
166     lc->sel_changed_connection.disconnect();
167     lc->sel_changed_connection =
168         selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_lpetool_context_selection_changed), (gpointer)lc));
170     //lc->my_nc = new NodeContextCpp(lc->desktop, lc->prefs_repr, lc->key);
171     lc->shape_editor = new ShapeEditor(ec->desktop);
173 // TODO temp force:
174     ec->enableSelectionCue();
176     if (item) {
177         lc->shape_editor->set_item(item, SH_NODEPATH);
178         lc->shape_editor->set_item(item, SH_KNOTHOLDER);
179     }
181     if (prefs_get_int_attribute("tools.lpetool", "selcue", 0) != 0) {
182         ec->enableSelectionCue();
183     }
185     lc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
187     lc->shape_editor->update_statusbar();
190 /**
191 \brief  Callback that processes the "changed" signal on the selection;
192 destroys old and creates new nodepath and reassigns listeners to the new selected item's repr
193 */
194 void
195 sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data)
197     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(data);
199     // TODO: update ShapeEditorsCollective instead
200     lc->shape_editor->unset_item(SH_NODEPATH);
201     lc->shape_editor->unset_item(SH_KNOTHOLDER);
202     SPItem *item = selection->singleItem(); 
203     lc->shape_editor->set_item(item, SH_NODEPATH);
204     lc->shape_editor->set_item(item, SH_KNOTHOLDER);
205     lc->shape_editor->update_statusbar();
208 static void
209 sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
211     // FIXME: how to set this correcly? the value from preferences-skeleton.h doesn't seem to get
212     // read (it wants to set drag = 1)
213     lpetool_parent_class->set(ec, key, "drag");
215     /**
216     //pass on up to parent class to handle common attributes.
217     if ( lpetool_parent_class->set ) {
218         lpetool_parent_class->set(ec, key, val);
219     }
220     **/
223 /**
224 void
225 sp_erc_update_toolbox (SPDesktop *desktop, const gchar *id, double value)
227     desktop->setToolboxAdjustmentValue (id, value);
229 **/
231 gint
232 sp_lpetool_context_root_handler(SPEventContext *event_context, GdkEvent *event)
234     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(event_context);
235     SPDesktop *desktop = event_context->desktop;
236     Inkscape::Selection *selection = sp_desktop_selection (desktop);
238     //gint ret = FALSE;
239     bool ret = false;
241     if (sp_pen_context_has_waiting_LPE(lc)) {
242         // quit when we are waiting for a LPE to be applied
243         g_print ("LPETool has waiting LPE. We call the pen tool parent context and return\n");
245         if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
246             ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
247         }
249         return ret;
250     }
252     switch (event->type) {
253         case GDK_BUTTON_PRESS:
254             if (event->button.button == 1 && !event_context->space_panning) {
255                 // save drag origin
256                 event_context->xp = (gint) event->button.x;
257                 event_context->yp = (gint) event->button.y;
258                 event_context->within_tolerance = true;
259                 lc->shape_editor->cancel_hit();
261                 using namespace Inkscape::LivePathEffect;
263                 int mode = prefs_get_int_attribute("tools.lpetool", "mode", 0);
264                 EffectType type = lpesubtools[mode];
265                 g_print ("Activating mode %d\n", mode);
267                 // save drag origin
268                 bool over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->button.x, event->button.y), true);
269                 g_print ("over_stroke: %s\n", over_stroke ? "true" : "false");
271                 sp_pen_context_wait_for_LPE_mouse_clicks(lc, type, Effect::acceptsNumClicks(type));
273                 // we pass the mouse click on to pen tool as the first click which it should collect
274                 if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
275                     ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
276                 }
278                 /**
279                 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
281                 NR::Point const button_w(event->button.x,
282                                          event->button.y);
283                 NR::Point const button_dt(desktop->w2d(button_w));
285                 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
286                     return TRUE;
287                 }
289                 sp_lpetool_reset(dc, button_dt);
290                 sp_lpetool_extinput(dc, event);
291                 sp_lpetool_apply(dc, button_dt);
292                 dc->accumulated->reset();
293                 if (dc->repr) {
294                     dc->repr = NULL;
295                 }
297                 Inkscape::Rubberband::get()->start(desktop, button_dt);
298                 Inkscape::Rubberband::get()->setMode(RUBBERBAND_MODE_TOUCHPATH);
300                 // initialize first point
301                 dc->npoints = 0;
303                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
304                                     ( GDK_KEY_PRESS_MASK |
305                                       GDK_BUTTON_RELEASE_MASK |
306                                       GDK_POINTER_MOTION_MASK |
307                                       GDK_BUTTON_PRESS_MASK ),
308                                     NULL,
309                                     event->button.time);
311                 ret = TRUE;
313                 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
314                 dc->is_drawing = true;
315                 **/
316             }
317             break;
318         case GDK_MOTION_NOTIFY:
319         {
320             if (!lc->shape_editor->has_nodepath() || selection->singleItem() == NULL) {
321                 break;
322             }
324             bool over_stroke = false;
325             over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->motion.x, event->motion.y), false);
327             if (over_stroke) {
328                 event_context->cursor_shape = cursor_node_xpm;
329                 event_context->hot_x = 1;
330                 event_context->hot_y = 1;
331                 sp_event_context_update_cursor(event_context);
332                 //lc->cursor_drag = false;
333             } else {
334                 lc->cursor_shape = cursor_pencil_xpm;
335                 lc->hot_x = 4;
336                 lc->hot_y = 4;
337                 sp_event_context_update_cursor(event_context);
338                 //lc->cursor_drag = false;
339             }
340         /**
341         {
342             NR::Point const motion_w(event->motion.x,
343                                      event->motion.y);
344             NR::Point motion_dt(desktop->w2d(motion_w));
345             sp_lpetool_extinput(dc, event);
347             dc->_message_context->clear();
349             if ( dc->is_drawing && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
350                 dc->dragging = TRUE;
352                 dc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drawing</b> an lpetool stroke"));
354                 if (!sp_lpetool_apply(dc, motion_dt)) {
355                     ret = TRUE;
356                     break;
357                 }
359                 if ( dc->cur != dc->last ) {
360                     sp_lpetool_brush(dc);
361                     g_assert( dc->npoints > 0 );
362                     fit_and_split(dc, FALSE);
363                 }
364                 ret = TRUE;
365             }
366             Inkscape::Rubberband::get()->move(motion_dt);
367         }
368         **/
369         }
370         break;
373     case GDK_BUTTON_RELEASE:
374     /**
375     {
376         NR::Point const motion_w(event->button.x, event->button.y);
377         NR::Point const motion_dt(desktop->w2d(motion_w));
379         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
380         sp_canvas_end_forced_full_redraws(desktop->canvas);
381         dc->is_drawing = false;
383         if (dc->dragging && event->button.button == 1 && !event_context->space_panning) {
384             dc->dragging = FALSE;
386             NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
388             sp_lpetool_apply(dc, motion_dt);
389             
390             // Remove all temporary line segments
391             while (dc->segments) {
392                 gtk_object_destroy(GTK_OBJECT(dc->segments->data));
393                 dc->segments = g_slist_remove(dc->segments, dc->segments->data);
394             }
396             // Create object
397             fit_and_split(dc, TRUE);
398             accumulate_lpetool(dc);
399             set_to_accumulated(dc); // performs document_done
401             // reset accumulated curve
402             dc->accumulated->reset();
404             clear_current(dc);
405             if (dc->repr) {
406                 dc->repr = NULL;
407             }
409             Inkscape::Rubberband::get()->stop();
410             dc->_message_context->clear();
411             ret = TRUE;
412         }
413         break;
414     }
415     **/
417     case GDK_KEY_PRESS:
418         /**
419         switch (get_group0_keyval (&event->key)) {
420         case GDK_Up:
421         case GDK_KP_Up:
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_Down:
431         case GDK_KP_Down:
432             if (!MOD__CTRL_ONLY) {
433                 dc->angle -= 5.0;
434                 if (dc->angle < -90.0)
435                     dc->angle = -90.0;
436                 sp_erc_update_toolbox (desktop, "lpetool-angle", dc->angle);
437                 ret = TRUE;
438             }
439             break;
440         case GDK_Right:
441         case GDK_KP_Right:
442             if (!MOD__CTRL_ONLY) {
443                 dc->width += 0.01;
444                 if (dc->width > 1.0)
445                     dc->width = 1.0;
446                 sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100); // the same spinbutton is for alt+x
447                 ret = TRUE;
448             }
449             break;
450         case GDK_Left:
451         case GDK_KP_Left:
452             if (!MOD__CTRL_ONLY) {
453                 dc->width -= 0.01;
454                 if (dc->width < 0.01)
455                     dc->width = 0.01;
456                 sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
457                 ret = TRUE;
458             }
459             break;
460         case GDK_Home:
461         case GDK_KP_Home:
462             dc->width = 0.01;
463             sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
464             ret = TRUE;
465             break;
466         case GDK_End:
467         case GDK_KP_End:
468             dc->width = 1.0;
469             sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
470             ret = TRUE;
471             break;
472         case GDK_x:
473         case GDK_X:
474             if (MOD__ALT_ONLY) {
475                 desktop->setToolboxFocusTo ("altx-lpetool");
476                 ret = TRUE;
477             }
478             break;
479         case GDK_Escape:
480             Inkscape::Rubberband::get()->stop();
481             if (dc->is_drawing) {
482                 // if drawing, cancel, otherwise pass it up for deselecting
483                 lpetool_cancel (dc);
484                 ret = TRUE;
485             }
486             break;
487         case GDK_z:
488         case GDK_Z:
489             if (MOD__CTRL_ONLY && dc->is_drawing) {
490                 // if drawing, cancel, otherwise pass it up for undo
491                 lpetool_cancel (dc);
492                 ret = TRUE;
493             }
494             break;
495         default:
496             break;
497         }
498         **/
499         break;
501     case GDK_KEY_RELEASE:
502         /**
503         switch (get_group0_keyval(&event->key)) {
504             case GDK_Control_L:
505             case GDK_Control_R:
506                 dc->_message_context->clear();
507                 break;
508             default:
509                 break;
510         }
511         **/
513     default:
514         break;
515     }
517     if (!ret) {
518         if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
519             ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
520         }
521     }
523     return ret;
526 /*
527   Local Variables:
528   mode:c++
529   c-file-style:"stroustrup"
530   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
531   indent-tabs-mode:nil
532   fill-column:99
533   End:
534 */
535 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :