Code

Fix the "show temporary outlines for selected paths" preference.
[inkscape.git] / src / ui / tool / node-tool.cpp
1 /** @file
2  * @brief New node tool - implementation
3  */
4 /* Authors:
5  *   Krzysztof KosiƄski <tweenk@gmail.com>
6  *
7  * Copyright (C) 2009 Authors
8  * Released under GNU GPL, read the file 'COPYING' for more information
9  */
11 #include <glib.h>
12 #include <glib/gi18n.h>
13 #include "desktop.h"
14 #include "desktop-handles.h"
15 #include "display/canvas-bpath.h"
16 #include "display/curve.h"
17 #include "display/sp-canvas.h"
18 #include "document.h"
19 #include "live_effects/lpeobject.h"
20 #include "message-context.h"
21 #include "selection.h"
22 #include "shape-editor.h" // temporary!
23 #include "sp-clippath.h"
24 #include "sp-item-group.h"
25 #include "sp-mask.h"
26 #include "sp-object-group.h"
27 #include "sp-path.h"
28 #include "ui/tool/node-tool.h"
29 #include "ui/tool/control-point-selection.h"
30 #include "ui/tool/curve-drag-point.h"
31 #include "ui/tool/event-utils.h"
32 #include "ui/tool/manipulator.h"
33 #include "ui/tool/multi-path-manipulator.h"
34 #include "ui/tool/path-manipulator.h"
35 #include "ui/tool/selector.h"
36 #include "ui/tool/shape-record.h"
38 #include "pixmaps/cursor-node.xpm"
39 #include "pixmaps/cursor-node-d.xpm"
41 namespace {
42 SPCanvasGroup *create_control_group(SPDesktop *d);
43 void ink_node_tool_class_init(InkNodeToolClass *klass);
44 void ink_node_tool_init(InkNodeTool *node_context);
45 void ink_node_tool_dispose(GObject *object);
47 void ink_node_tool_setup(SPEventContext *ec);
48 gint ink_node_tool_root_handler(SPEventContext *event_context, GdkEvent *event);
49 gint ink_node_tool_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
50 void ink_node_tool_set(SPEventContext *ec, Inkscape::Preferences::Entry *value);
52 void ink_node_tool_update_tip(InkNodeTool *nt, GdkEvent *event);
53 void ink_node_tool_selection_changed(InkNodeTool *nt, Inkscape::Selection *sel);
54 void ink_node_tool_select_area(InkNodeTool *nt, Geom::Rect const &, GdkEventButton *);
55 void ink_node_tool_select_point(InkNodeTool *nt, Geom::Point const &, GdkEventButton *);
56 void ink_node_tool_mouseover_changed(InkNodeTool *nt, Inkscape::UI::ControlPoint *p);
57 } // anonymous namespace
59 GType ink_node_tool_get_type()
60 {
61     static GType type = 0;
62     if (!type) {
63         GTypeInfo info = {
64             sizeof(InkNodeToolClass),
65             NULL, NULL,
66             (GClassInitFunc) ink_node_tool_class_init,
67             NULL, NULL,
68             sizeof(InkNodeTool),
69             4,
70             (GInstanceInitFunc) ink_node_tool_init,
71             NULL,    /* value_table */
72         };
73         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "InkNodeTool", &info, (GTypeFlags)0);
74     }
75     return type;
76 }
78 namespace {
80 SPCanvasGroup *create_control_group(SPDesktop *d)
81 {
82     return reinterpret_cast<SPCanvasGroup*>(sp_canvas_item_new(
83         sp_desktop_controls(d), SP_TYPE_CANVAS_GROUP, NULL));
84 }
86 void destroy_group(SPCanvasGroup *g)
87 {
88     gtk_object_destroy(GTK_OBJECT(g));
89 }
91 void ink_node_tool_class_init(InkNodeToolClass *klass)
92 {
93     GObjectClass *object_class = (GObjectClass *) klass;
94     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
96     object_class->dispose = ink_node_tool_dispose;
98     event_context_class->setup = ink_node_tool_setup;
99     event_context_class->set = ink_node_tool_set;
100     event_context_class->root_handler = ink_node_tool_root_handler;
101     event_context_class->item_handler = ink_node_tool_item_handler;
104 void ink_node_tool_init(InkNodeTool *nt)
106     SPEventContext *event_context = SP_EVENT_CONTEXT(nt);
108     event_context->cursor_shape = cursor_node_xpm;
109     event_context->hot_x = 1;
110     event_context->hot_y = 1;
112     new (&nt->_selection_changed_connection) sigc::connection();
113     new (&nt->_selection_modified_connection) sigc::connection();
114     new (&nt->_mouseover_changed_connection) sigc::connection();
115     //new (&nt->_mgroup) Inkscape::UI::ManipulatorGroup(nt->desktop);
116     new (&nt->_selected_nodes) CSelPtr();
117     new (&nt->_multipath) MultiPathPtr();
118     new (&nt->_selector) SelectorPtr();
119     new (&nt->_path_data) PathSharedDataPtr();
122 void ink_node_tool_dispose(GObject *object)
124     InkNodeTool *nt = INK_NODE_TOOL(object);
126     nt->enableGrDrag(false);
128     nt->_selection_changed_connection.disconnect();
129     nt->_selection_modified_connection.disconnect();
130     nt->_mouseover_changed_connection.disconnect();
131     nt->_multipath.~MultiPathPtr();
132     nt->_selected_nodes.~CSelPtr();
133     nt->_selector.~SelectorPtr();
134     
135     Inkscape::UI::PathSharedData &data = *nt->_path_data;
136     destroy_group(data.node_data.node_group);
137     destroy_group(data.node_data.handle_group);
138     destroy_group(data.node_data.handle_line_group);
139     destroy_group(data.outline_group);
140     destroy_group(data.dragpoint_group);
141     destroy_group(nt->_transform_handle_group);
142     
143     nt->_path_data.~PathSharedDataPtr();
144     nt->_selection_changed_connection.~connection();
145     nt->_selection_modified_connection.~connection();
146     nt->_mouseover_changed_connection.~connection();
148     if (nt->_node_message_context) {
149         delete nt->_node_message_context;
150     }
151     if (nt->shape_editor) {
152         nt->shape_editor->unset_item(SH_KNOTHOLDER);
153         delete nt->shape_editor;
154     }
156     G_OBJECT_CLASS(g_type_class_peek(g_type_parent(INK_TYPE_NODE_TOOL)))->dispose(object);
159 void ink_node_tool_setup(SPEventContext *ec)
161     InkNodeTool *nt = INK_NODE_TOOL(ec);
163     SPEventContextClass *parent = (SPEventContextClass *) g_type_class_peek(g_type_parent(INK_TYPE_NODE_TOOL));
164     if (parent->setup) parent->setup(ec);
166     nt->_node_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
168     nt->_path_data.reset(new Inkscape::UI::PathSharedData());
169     Inkscape::UI::PathSharedData &data = *nt->_path_data;
170     data.node_data.desktop = nt->desktop;
172     // selector has to be created here, so that its hidden control point is on the bottom
173     nt->_selector.reset(new Inkscape::UI::Selector(nt->desktop));
175     // Prepare canvas groups for controls. This guarantees correct z-order, so that
176     // for example a dragpoint won't obscure a node
177     data.outline_group = create_control_group(nt->desktop);
178     data.node_data.handle_line_group = create_control_group(nt->desktop);
179     data.dragpoint_group = create_control_group(nt->desktop);
180     nt->_transform_handle_group = create_control_group(nt->desktop);
181     data.node_data.node_group = create_control_group(nt->desktop);
182     data.node_data.handle_group = create_control_group(nt->desktop);
184     Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
185     nt->_selection_changed_connection.disconnect();
186     nt->_selection_changed_connection =
187         selection->connectChanged(
188             sigc::bind<0>(
189                 sigc::ptr_fun(&ink_node_tool_selection_changed),
190                 nt));
191     /*nt->_selection_modified_connection.disconnect();
192     nt->_selection_modified_connection =
193         selection->connectModified(
194             sigc::hide(sigc::bind<0>(
195                 sigc::ptr_fun(&ink_node_tool_selection_modified),
196                 nt)));*/
197     nt->_mouseover_changed_connection.disconnect();
198     nt->_mouseover_changed_connection = 
199         Inkscape::UI::ControlPoint::signal_mouseover_change.connect(
200             sigc::bind<0>(
201                 sigc::ptr_fun(&ink_node_tool_mouseover_changed),
202                 nt));
203     
204     nt->_selected_nodes.reset(
205         new Inkscape::UI::ControlPointSelection(nt->desktop, nt->_transform_handle_group));
206     data.node_data.selection = nt->_selected_nodes.get();
207     nt->_multipath.reset(new Inkscape::UI::MultiPathManipulator(data,
208         nt->_selection_changed_connection));
210     nt->_selector->signal_point.connect(
211         sigc::bind<0>(
212             sigc::ptr_fun(&ink_node_tool_select_point),
213             nt));
214     nt->_selector->signal_area.connect(
215         sigc::bind<0>(
216             sigc::ptr_fun(&ink_node_tool_select_area),
217             nt));
219     nt->_multipath->signal_coords_changed.connect(
220         sigc::bind(
221             sigc::mem_fun(*nt->desktop, &SPDesktop::emitToolSubselectionChanged),
222             (void*) 0));
223     nt->_selected_nodes->signal_point_changed.connect(
224         sigc::hide( sigc::hide(
225             sigc::bind(
226                 sigc::bind(
227                     sigc::ptr_fun(ink_node_tool_update_tip),
228                     (GdkEvent*)0),
229                 nt))));
231     nt->cursor_drag = false;
232     nt->show_transform_handles = true;
233     nt->single_node_transform_handles = false;
234     nt->flash_tempitem = NULL;
235     nt->flashed_item = NULL;
236     // TODO remove this!
237     nt->shape_editor = new ShapeEditor(nt->desktop);
239     // read prefs before adding items to selection to prevent momentarily showing the outline
240     sp_event_context_read(nt, "show_handles");
241     sp_event_context_read(nt, "show_outline");
242     sp_event_context_read(nt, "show_path_direction");
243     sp_event_context_read(nt, "show_transform_handles");
244     sp_event_context_read(nt, "single_node_transform_handles");
245     sp_event_context_read(nt, "edit_clipping_paths");
246     sp_event_context_read(nt, "edit_masks");
248     ink_node_tool_selection_changed(nt, selection);
249     ink_node_tool_update_tip(nt, NULL);
251     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
252     if (prefs->getBool("/tools/nodes/selcue")) {
253         ec->enableSelectionCue();
254     }
255     if (prefs->getBool("/tools/nodes/gradientdrag")) {
256         ec->enableGrDrag();
257     }
259     nt->desktop->emitToolSubselectionChanged(NULL); // sets the coord entry fields to inactive
262 void ink_node_tool_set(SPEventContext *ec, Inkscape::Preferences::Entry *value)
264     InkNodeTool *nt = INK_NODE_TOOL(ec);
265     Glib::ustring entry_name = value->getEntryName();
267     if (entry_name == "show_handles") {
268         nt->_multipath->showHandles(value->getBool(true));
269     } else if (entry_name == "show_outline") {
270         nt->show_outline = value->getBool();
271         nt->_multipath->showOutline(nt->show_outline);
272     } else if (entry_name == "show_path_direction") {
273         nt->show_path_direction = value->getBool();
274         nt->_multipath->showPathDirection(nt->show_path_direction);
275     } else if (entry_name == "show_transform_handles") {
276         nt->show_transform_handles = value->getBool(true);
277         nt->_selected_nodes->showTransformHandles(
278             nt->show_transform_handles, nt->single_node_transform_handles);
279     } else if (entry_name == "single_node_transform_handles") {
280         nt->single_node_transform_handles = value->getBool();
281         nt->_selected_nodes->showTransformHandles(
282             nt->show_transform_handles, nt->single_node_transform_handles);
283     } else if (entry_name == "edit_clipping_paths") {
284         nt->edit_clipping_paths = value->getBool();
285         ink_node_tool_selection_changed(nt, nt->desktop->selection);
286     } else if (entry_name == "edit_masks") {
287         nt->edit_masks = value->getBool();
288         ink_node_tool_selection_changed(nt, nt->desktop->selection);
289     } else {
290         SPEventContextClass *parent_class =
291             (SPEventContextClass *) g_type_class_peek(g_type_parent(INK_TYPE_NODE_TOOL));
292         if (parent_class->set)
293             parent_class->set(ec, value);
294     }
297 /** Recursively collect ShapeRecords */
298 void gather_items(InkNodeTool *nt, SPItem *base, SPObject *obj, Inkscape::UI::ShapeRole role,
299     std::set<Inkscape::UI::ShapeRecord> &s)
301     using namespace Inkscape::UI;
302     if (!obj) return;
304     if (SP_IS_PATH(obj) && obj->repr->attribute("inkscape:original-d") != NULL) {
305         ShapeRecord r;
306         r.item = static_cast<SPItem*>(obj);
307         r.edit_transform = Geom::identity(); // TODO wrong?
308         r.role = SHAPE_ROLE_LPE_PARAM;
309         s.insert(r);
310     } else if (role != SHAPE_ROLE_NORMAL && (SP_IS_GROUP(obj) || SP_IS_OBJECTGROUP(obj))) {
311         for (SPObject *c = obj->children; c; c = c->next) {
312             gather_items(nt, base, c, role, s);
313         }
314     } else if (SP_IS_ITEM(obj)) {
315         SPItem *item = static_cast<SPItem*>(obj);
316         ShapeRecord r;
317         r.item = item;
318         // TODO add support for objectBoundingBox
319         r.edit_transform = base ? sp_item_i2doc_affine(base) : Geom::identity();
320         r.role = role;
321         if (s.insert(r).second) {
322             // this item was encountered the first time
323             if (nt->edit_clipping_paths && item->clip_ref) {
324                 gather_items(nt, item, item->clip_ref->getObject(), SHAPE_ROLE_CLIPPING_PATH, s);
325             }
326             if (nt->edit_masks && item->mask_ref) {
327                 gather_items(nt, item, item->mask_ref->getObject(), SHAPE_ROLE_MASK, s);
328             }
329         }
330     }
333 struct IsPath {
334     bool operator()(SPItem *i) const { return SP_IS_PATH(i); }
335 };
337 void ink_node_tool_selection_changed(InkNodeTool *nt, Inkscape::Selection *sel)
339     using namespace Inkscape::UI;
341     std::set<ShapeRecord> shapes;
343     // TODO this is ugly!!!
344     //typedef std::map<SPItem*, std::pair<Geom::Matrix, guint32> > TransMap;
345     //typedef std::map<SPPath*, std::pair<Geom::Matrix, guint32> > PathMap;
346     GSList const *ilist = sel->itemList();
348     for (GSList *i = const_cast<GSList*>(ilist); i; i = i->next) {
349         SPObject *obj = static_cast<SPObject*>(i->data);
350         if (SP_IS_ITEM(obj)) {
351             gather_items(nt, NULL, static_cast<SPItem*>(obj), SHAPE_ROLE_NORMAL, shapes);
352         }
353     }
355     // ugly hack: set the first editable non-path item for knotholder
356     // maybe use multiple ShapeEditors for now, to allow editing many shapes at once?
357     bool something_set = false;
358     for (std::set<ShapeRecord>::iterator i = shapes.begin(); i != shapes.end(); ++i) {
359         ShapeRecord const &r = *i;
360         if (SP_IS_SHAPE(r.item) && !SP_IS_PATH(r.item)) {
361             nt->shape_editor->set_item(r.item, SH_KNOTHOLDER);
362             something_set = true;
363             break;
364         }
365     }
366     if (!something_set) {
367         nt->shape_editor->unset_item(SH_KNOTHOLDER);
368     }
370     nt->_multipath->setItems(shapes);
371     ink_node_tool_update_tip(nt, NULL);
372     nt->desktop->updateNow();
375 gint ink_node_tool_root_handler(SPEventContext *event_context, GdkEvent *event)
377     /* things to handle here:
378      * 1. selection of items
379      * 2. passing events to manipulators
380      * 3. some keybindings
381      */
382     using namespace Inkscape::UI; // pull in event helpers
383     
384     SPDesktop *desktop = event_context->desktop;
385     Inkscape::Selection *selection = desktop->selection;
386     InkNodeTool *nt = static_cast<InkNodeTool*>(event_context);
387     static Inkscape::Preferences *prefs = Inkscape::Preferences::get();
388     
389     if (nt->_multipath->event(event)) return true;
390     if (nt->_selector->event(event)) return true;
391     if (nt->_selected_nodes->event(event)) return true;
393     switch (event->type)
394     {
395     case GDK_MOTION_NOTIFY:
396         // create outline
397         if (prefs->getBool("/tools/nodes/pathflash_enabled")) {
398             SPItem *over_item = sp_event_context_find_item (desktop, event_point(event->button),
399                 FALSE, TRUE);
400             if (over_item == nt->flashed_item) break;
401             if (!prefs->getBool("/tools/nodes/pathflash_selected") && selection->includes(over_item)) break;
402             if (nt->flash_tempitem) {
403                 desktop->remove_temporary_canvasitem(nt->flash_tempitem);
404                 nt->flash_tempitem = NULL;
405                 nt->flashed_item = NULL;
406             }
407             if (!SP_IS_PATH(over_item)) break; // for now, handle only paths
409             nt->flashed_item = over_item;
410             SPCurve *c = sp_path_get_curve_for_edit(SP_PATH(over_item));
411             c->transform(sp_item_i2d_affine(over_item));
412             SPCanvasItem *flash = sp_canvas_bpath_new(sp_desktop_tempgroup(desktop), c);
413             sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(flash),
414                 prefs->getInt("/tools/nodes/highlight_color", 0xff0000ff), 1.0,
415                 SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
416             sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(flash), 0, SP_WIND_RULE_NONZERO);
417             nt->flash_tempitem = desktop->add_temporary_canvasitem(flash,
418                 prefs->getInt("/tools/nodes/pathflash_timeout", 500));
419             c->unref();
420         }
421         return true;
422     case GDK_KEY_PRESS:
423         switch (get_group0_keyval(&event->key))
424         {
425         case GDK_Escape: // deselect everything
426             if (nt->_selected_nodes->empty()) {
427                 selection->clear();
428             } else {
429                 nt->_selected_nodes->clear();
430             }
431             ink_node_tool_update_tip(nt, event);
432             return TRUE;
433         case GDK_a:
434             if (held_control(event->key)) {
435                 if (held_alt(event->key)) {
436                     nt->_multipath->selectAll();
437                 } else {
438                     // select all nodes in subpaths that have something selected
439                     // if nothing is selected, select everything
440                     nt->_multipath->selectSubpaths();
441                 }
442                 ink_node_tool_update_tip(nt, event);
443                 return TRUE;
444             }
445             break;
446         default:
447             break;
448         }
449         ink_node_tool_update_tip(nt, event);
450         break;
451     case GDK_KEY_RELEASE:
452         ink_node_tool_update_tip(nt, event);
453         break;
454     default: break;
455     }
456     
457     SPEventContextClass *parent_class = (SPEventContextClass *) g_type_class_peek(g_type_parent(INK_TYPE_NODE_TOOL));
458     if (parent_class->root_handler)
459         return parent_class->root_handler(event_context, event);
460     return FALSE;
463 void ink_node_tool_update_tip(InkNodeTool *nt, GdkEvent *event)
465     using namespace Inkscape::UI;
466     if (event && (event->type == GDK_KEY_PRESS || event->type == GDK_KEY_RELEASE)) {
467         unsigned new_state = state_after_event(event);
468         if (new_state == event->key.state) return;
469         if (state_held_shift(new_state)) {
470             nt->_node_message_context->set(Inkscape::NORMAL_MESSAGE,
471                 C_("Node tool tip", "<b>Shift:</b> drag to add nodes to the selection, "
472                 "click to toggle object selection"));
473             return;
474         }
475     }
476     unsigned sz = nt->_selected_nodes->size();
477     if (sz != 0) {
478         char *dyntip = g_strdup_printf(C_("Node tool tip",
479             "Selected <b>%d nodes</b>. Drag to select nodes, click to select a single object "
480             "or unselect all objects"), sz);
481         nt->_node_message_context->set(Inkscape::NORMAL_MESSAGE, dyntip);
482         g_free(dyntip);
483     } else if (nt->_multipath->empty()) {
484         nt->_node_message_context->set(Inkscape::NORMAL_MESSAGE,
485             C_("Node tool tip", "Drag or click to select objects to edit"));
486     } else {
487         nt->_node_message_context->set(Inkscape::NORMAL_MESSAGE,
488             C_("Node tool tip", "Drag to select nodes, click to select an object "
489             "or clear the selection"));
490     }
493 gint ink_node_tool_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
495     SPEventContextClass *parent_class =
496         (SPEventContextClass *) g_type_class_peek(g_type_parent(INK_TYPE_NODE_TOOL));
497     if (parent_class->item_handler)
498         return parent_class->item_handler(event_context, item, event);
499     return FALSE;
502 void ink_node_tool_select_area(InkNodeTool *nt, Geom::Rect const &sel, GdkEventButton *event)
504     using namespace Inkscape::UI;
505     if (nt->_multipath->empty()) {
506         // if multipath is empty, select rubberbanded items rather than nodes
507         Inkscape::Selection *selection = nt->desktop->selection;
508         GSList *items = sp_document_items_in_box(
509             sp_desktop_document(nt->desktop), nt->desktop->dkey, sel);
510         selection->setList(items);
511         g_slist_free(items);
512     } else {
513         nt->_multipath->selectArea(sel, !held_shift(*event));
514     }
516 void ink_node_tool_select_point(InkNodeTool *nt, Geom::Point const &sel, GdkEventButton *event)
518     using namespace Inkscape::UI; // pull in event helpers
519     if (!event) return;
520     if (event->button != 1) return;
522     Inkscape::Selection *selection = nt->desktop->selection;
524     SPItem *item_clicked = sp_event_context_find_item (nt->desktop, event_point(*event),
525                     (event->state & GDK_MOD1_MASK) && !(event->state & GDK_CONTROL_MASK), TRUE);
527     if (item_clicked == NULL) { // nothing under cursor
528         // if no Shift, deselect
529         if (!(event->state & GDK_SHIFT_MASK)) {
530             selection->clear();
531         }
532         return;
533     }
534     if (held_shift(*event)) {
535         selection->toggle(item_clicked);
536     } else {
537         selection->set(item_clicked);
538     }
539     nt->desktop->updateNow();
542 void ink_node_tool_mouseover_changed(InkNodeTool *nt, Inkscape::UI::ControlPoint *p)
544     using Inkscape::UI::CurveDragPoint;
545     CurveDragPoint *cdp = dynamic_cast<CurveDragPoint*>(p);
546     if (cdp && !nt->cursor_drag) {
547         nt->cursor_shape = cursor_node_d_xpm;
548         nt->hot_x = 1;
549         nt->hot_y = 1;
550         sp_event_context_update_cursor(nt);
551         nt->cursor_drag = true;
552     } else if (!cdp && nt->cursor_drag) {
553         nt->cursor_shape = cursor_node_xpm;
554         nt->hot_x = 1;
555         nt->hot_y = 1;
556         sp_event_context_update_cursor(nt);
557         nt->cursor_drag = false;
558     }
561 } // anonymous namespace
563 /*
564   Local Variables:
565   mode:c++
566   c-file-style:"stroustrup"
567   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
568   indent-tabs-mode:nil
569   fill-column:99
570   End:
571 */
572 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :