Code

fb3814ab736620fbab33a9e17ea8424903cd6c82
[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     //g_print ("sp_lpetool_context_root_handler()\n");
235     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(event_context);
236     SPDesktop *desktop = event_context->desktop;
237     Inkscape::Selection *selection = sp_desktop_selection (desktop);
239     //gint ret = FALSE;
240     bool ret = false;
242     if (sp_pen_context_has_waiting_LPE(lc)) {
243         // quit when we are waiting for a LPE to be applied
244         g_print ("LPETool has waiting LPE. We call the pen tool parent context and return\n");
245         ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
246         return ret;
247     }
249     switch (event->type) {
250         case GDK_BUTTON_PRESS:
251             g_print ("GDK_BUTTON_PRESS\n");
252             if (event->button.button == 1 && !event_context->space_panning) {
253                 g_print ("   ... (passed if construct)\n");
254                 // save drag origin
255                 event_context->xp = (gint) event->button.x;
256                 event_context->yp = (gint) event->button.y;
257                 event_context->within_tolerance = true;
258                 lc->shape_editor->cancel_hit();
260                 using namespace Inkscape::LivePathEffect;
262                 int mode = prefs_get_int_attribute("tools.lpetool", "mode", 0);
263                 EffectType type = lpesubtools[mode];
264                 g_print ("Activating mode %d\n", mode);
266                 // save drag origin
267                 bool over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->button.x, event->button.y), true);
268                 g_print ("over_stroke: %s\n", over_stroke ? "true" : "false");
270                 sp_pen_context_wait_for_LPE_mouse_clicks(lc, type, Effect::acceptsNumClicks(type));
272                 // we pass the mouse click on to pen tool as the first click which it should collect
273                 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
275                 /**
276                 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
278                 NR::Point const button_w(event->button.x,
279                                          event->button.y);
280                 NR::Point const button_dt(desktop->w2d(button_w));
282                 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
283                     return TRUE;
284                 }
286                 sp_lpetool_reset(dc, button_dt);
287                 sp_lpetool_extinput(dc, event);
288                 sp_lpetool_apply(dc, button_dt);
289                 dc->accumulated->reset();
290                 if (dc->repr) {
291                     dc->repr = NULL;
292                 }
294                 Inkscape::Rubberband::get()->start(desktop, button_dt);
295                 Inkscape::Rubberband::get()->setMode(RUBBERBAND_MODE_TOUCHPATH);
297                 // initialize first point
298                 dc->npoints = 0;
300                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
301                                     ( GDK_KEY_PRESS_MASK |
302                                       GDK_BUTTON_RELEASE_MASK |
303                                       GDK_POINTER_MOTION_MASK |
304                                       GDK_BUTTON_PRESS_MASK ),
305                                     NULL,
306                                     event->button.time);
308                 ret = TRUE;
310                 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
311                 dc->is_drawing = true;
312                 **/
313             }
314             break;
315         case GDK_MOTION_NOTIFY:
316         {
317             if (!lc->shape_editor->has_nodepath() || selection->singleItem() == NULL) {
318                 break;
319             }
321             bool over_stroke = false;
322             over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->motion.x, event->motion.y), false);
324             if (over_stroke) {
325                 event_context->cursor_shape = cursor_node_xpm;
326                 event_context->hot_x = 1;
327                 event_context->hot_y = 1;
328                 sp_event_context_update_cursor(event_context);
329                 //lc->cursor_drag = false;
330             } else {
331                 lc->cursor_shape = cursor_pencil_xpm;
332                 lc->hot_x = 4;
333                 lc->hot_y = 4;
334                 sp_event_context_update_cursor(event_context);
335                 //lc->cursor_drag = false;
336             }
337         /**
338         {
339             NR::Point const motion_w(event->motion.x,
340                                      event->motion.y);
341             NR::Point motion_dt(desktop->w2d(motion_w));
342             sp_lpetool_extinput(dc, event);
344             dc->_message_context->clear();
346             if ( dc->is_drawing && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
347                 dc->dragging = TRUE;
349                 dc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drawing</b> an lpetool stroke"));
351                 if (!sp_lpetool_apply(dc, motion_dt)) {
352                     ret = TRUE;
353                     break;
354                 }
356                 if ( dc->cur != dc->last ) {
357                     sp_lpetool_brush(dc);
358                     g_assert( dc->npoints > 0 );
359                     fit_and_split(dc, FALSE);
360                 }
361                 ret = TRUE;
362             }
363             Inkscape::Rubberband::get()->move(motion_dt);
364         }
365         **/
366         }
367         break;
370     case GDK_BUTTON_RELEASE:
371     /**
372     {
373         NR::Point const motion_w(event->button.x, event->button.y);
374         NR::Point const motion_dt(desktop->w2d(motion_w));
376         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
377         sp_canvas_end_forced_full_redraws(desktop->canvas);
378         dc->is_drawing = false;
380         if (dc->dragging && event->button.button == 1 && !event_context->space_panning) {
381             dc->dragging = FALSE;
383             NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
385             sp_lpetool_apply(dc, motion_dt);
386             
387             // Remove all temporary line segments
388             while (dc->segments) {
389                 gtk_object_destroy(GTK_OBJECT(dc->segments->data));
390                 dc->segments = g_slist_remove(dc->segments, dc->segments->data);
391             }
393             // Create object
394             fit_and_split(dc, TRUE);
395             accumulate_lpetool(dc);
396             set_to_accumulated(dc); // performs document_done
398             // reset accumulated curve
399             dc->accumulated->reset();
401             clear_current(dc);
402             if (dc->repr) {
403                 dc->repr = NULL;
404             }
406             Inkscape::Rubberband::get()->stop();
407             dc->_message_context->clear();
408             ret = TRUE;
409         }
410         break;
411     }
412     **/
414     case GDK_KEY_PRESS:
415         /**
416         switch (get_group0_keyval (&event->key)) {
417         case GDK_Up:
418         case GDK_KP_Up:
419             if (!MOD__CTRL_ONLY) {
420                 dc->angle += 5.0;
421                 if (dc->angle > 90.0)
422                     dc->angle = 90.0;
423                 sp_erc_update_toolbox (desktop, "lpetool-angle", dc->angle);
424                 ret = TRUE;
425             }
426             break;
427         case GDK_Down:
428         case GDK_KP_Down:
429             if (!MOD__CTRL_ONLY) {
430                 dc->angle -= 5.0;
431                 if (dc->angle < -90.0)
432                     dc->angle = -90.0;
433                 sp_erc_update_toolbox (desktop, "lpetool-angle", dc->angle);
434                 ret = TRUE;
435             }
436             break;
437         case GDK_Right:
438         case GDK_KP_Right:
439             if (!MOD__CTRL_ONLY) {
440                 dc->width += 0.01;
441                 if (dc->width > 1.0)
442                     dc->width = 1.0;
443                 sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100); // the same spinbutton is for alt+x
444                 ret = TRUE;
445             }
446             break;
447         case GDK_Left:
448         case GDK_KP_Left:
449             if (!MOD__CTRL_ONLY) {
450                 dc->width -= 0.01;
451                 if (dc->width < 0.01)
452                     dc->width = 0.01;
453                 sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
454                 ret = TRUE;
455             }
456             break;
457         case GDK_Home:
458         case GDK_KP_Home:
459             dc->width = 0.01;
460             sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
461             ret = TRUE;
462             break;
463         case GDK_End:
464         case GDK_KP_End:
465             dc->width = 1.0;
466             sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
467             ret = TRUE;
468             break;
469         case GDK_x:
470         case GDK_X:
471             if (MOD__ALT_ONLY) {
472                 desktop->setToolboxFocusTo ("altx-lpetool");
473                 ret = TRUE;
474             }
475             break;
476         case GDK_Escape:
477             Inkscape::Rubberband::get()->stop();
478             if (dc->is_drawing) {
479                 // if drawing, cancel, otherwise pass it up for deselecting
480                 lpetool_cancel (dc);
481                 ret = TRUE;
482             }
483             break;
484         case GDK_z:
485         case GDK_Z:
486             if (MOD__CTRL_ONLY && dc->is_drawing) {
487                 // if drawing, cancel, otherwise pass it up for undo
488                 lpetool_cancel (dc);
489                 ret = TRUE;
490             }
491             break;
492         default:
493             break;
494         }
495         **/
496         break;
498     case GDK_KEY_RELEASE:
499         /**
500         switch (get_group0_keyval(&event->key)) {
501             case GDK_Control_L:
502             case GDK_Control_R:
503                 dc->_message_context->clear();
504                 break;
505             default:
506                 break;
507         }
508         **/
510     default:
511         break;
512     }
514     if (!ret) {
515         if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
516             ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
517         }
518     }
520     return ret;
523 /*
524   Local Variables:
525   mode:c++
526   c-file-style:"stroustrup"
527   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
528   indent-tabs-mode:nil
529   fill-column:99
530   End:
531 */
532 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :