Code

Fix mask editing behavior on undo and outline display for masks/clips;
[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 "message-context.h"
20 #include "selection.h"
21 #include "shape-editor.h" // temporary!
22 #include "sp-clippath.h"
23 #include "sp-item-group.h"
24 #include "sp-mask.h"
25 #include "sp-object-group.h"
26 #include "sp-path.h"
27 #include "ui/tool/node-tool.h"
28 #include "ui/tool/control-point-selection.h"
29 #include "ui/tool/curve-drag-point.h"
30 #include "ui/tool/event-utils.h"
31 #include "ui/tool/manipulator.h"
32 #include "ui/tool/multi-path-manipulator.h"
33 #include "ui/tool/path-manipulator.h"
34 #include "ui/tool/selector.h"
35 #include "ui/tool/shape-record.h"
37 #include "pixmaps/cursor-node.xpm"
38 #include "pixmaps/cursor-node-d.xpm"
40 namespace {
41 SPCanvasGroup *create_control_group(SPDesktop *d);
42 void ink_node_tool_class_init(InkNodeToolClass *klass);
43 void ink_node_tool_init(InkNodeTool *node_context);
44 void ink_node_tool_dispose(GObject *object);
46 void ink_node_tool_setup(SPEventContext *ec);
47 gint ink_node_tool_root_handler(SPEventContext *event_context, GdkEvent *event);
48 gint ink_node_tool_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
49 void ink_node_tool_set(SPEventContext *ec, Inkscape::Preferences::Entry *value);
51 void ink_node_tool_update_tip(InkNodeTool *nt, GdkEvent *event);
52 void ink_node_tool_selection_changed(InkNodeTool *nt, Inkscape::Selection *sel);
53 void ink_node_tool_select_area(InkNodeTool *nt, Geom::Rect const &, GdkEventButton *);
54 void ink_node_tool_select_point(InkNodeTool *nt, Geom::Point const &, GdkEventButton *);
55 void ink_node_tool_mouseover_changed(InkNodeTool *nt, Inkscape::UI::ControlPoint *p);
56 } // anonymous namespace
58 GType ink_node_tool_get_type()
59 {
60     static GType type = 0;
61     if (!type) {
62         GTypeInfo info = {
63             sizeof(InkNodeToolClass),
64             NULL, NULL,
65             (GClassInitFunc) ink_node_tool_class_init,
66             NULL, NULL,
67             sizeof(InkNodeTool),
68             4,
69             (GInstanceInitFunc) ink_node_tool_init,
70             NULL,    /* value_table */
71         };
72         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "InkNodeTool", &info, (GTypeFlags)0);
73     }
74     return type;
75 }
77 namespace {
79 SPCanvasGroup *create_control_group(SPDesktop *d)
80 {
81     return reinterpret_cast<SPCanvasGroup*>(sp_canvas_item_new(
82         sp_desktop_controls(d), SP_TYPE_CANVAS_GROUP, NULL));
83 }
85 void destroy_group(SPCanvasGroup *g)
86 {
87     gtk_object_destroy(GTK_OBJECT(g));
88 }
90 void ink_node_tool_class_init(InkNodeToolClass *klass)
91 {
92     GObjectClass *object_class = (GObjectClass *) klass;
93     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
95     object_class->dispose = ink_node_tool_dispose;
97     event_context_class->setup = ink_node_tool_setup;
98     event_context_class->set = ink_node_tool_set;
99     event_context_class->root_handler = ink_node_tool_root_handler;
100     event_context_class->item_handler = ink_node_tool_item_handler;
103 void ink_node_tool_init(InkNodeTool *nt)
105     SPEventContext *event_context = SP_EVENT_CONTEXT(nt);
107     event_context->cursor_shape = cursor_node_xpm;
108     event_context->hot_x = 1;
109     event_context->hot_y = 1;
111     new (&nt->_selection_changed_connection) sigc::connection();
112     new (&nt->_selection_modified_connection) sigc::connection();
113     new (&nt->_mouseover_changed_connection) sigc::connection();
114     //new (&nt->_mgroup) Inkscape::UI::ManipulatorGroup(nt->desktop);
115     new (&nt->_selected_nodes) CSelPtr();
116     new (&nt->_multipath) MultiPathPtr();
117     new (&nt->_selector) SelectorPtr();
118     new (&nt->_path_data) PathSharedDataPtr();
121 void ink_node_tool_dispose(GObject *object)
123     InkNodeTool *nt = INK_NODE_TOOL(object);
125     nt->enableGrDrag(false);
127     nt->_selection_changed_connection.disconnect();
128     nt->_selection_modified_connection.disconnect();
129     nt->_mouseover_changed_connection.disconnect();
130     nt->_multipath.~MultiPathPtr();
131     nt->_selected_nodes.~CSelPtr();
132     nt->_selector.~SelectorPtr();
133     
134     Inkscape::UI::PathSharedData &data = *nt->_path_data;
135     destroy_group(data.node_data.node_group);
136     destroy_group(data.node_data.handle_group);
137     destroy_group(data.node_data.handle_line_group);
138     destroy_group(data.outline_group);
139     destroy_group(data.dragpoint_group);
140     destroy_group(nt->_transform_handle_group);
141     
142     nt->_path_data.~PathSharedDataPtr();
143     nt->_selection_changed_connection.~connection();
144     nt->_selection_modified_connection.~connection();
145     nt->_mouseover_changed_connection.~connection();
147     if (nt->_node_message_context) {
148         delete nt->_node_message_context;
149     }
150     if (nt->shape_editor) {
151         nt->shape_editor->unset_item(SH_KNOTHOLDER);
152         delete nt->shape_editor;
153     }
155     G_OBJECT_CLASS(g_type_class_peek(g_type_parent(INK_TYPE_NODE_TOOL)))->dispose(object);
158 void ink_node_tool_setup(SPEventContext *ec)
160     InkNodeTool *nt = INK_NODE_TOOL(ec);
162     SPEventContextClass *parent = (SPEventContextClass *) g_type_class_peek(g_type_parent(INK_TYPE_NODE_TOOL));
163     if (parent->setup) parent->setup(ec);
165     nt->_node_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
167     nt->_path_data.reset(new Inkscape::UI::PathSharedData());
168     Inkscape::UI::PathSharedData &data = *nt->_path_data;
169     data.node_data.desktop = nt->desktop;
171     // selector has to be created here, so that its hidden control point is on the bottom
172     nt->_selector.reset(new Inkscape::UI::Selector(nt->desktop));
174     // Prepare canvas groups for controls. This guarantees correct z-order, so that
175     // for example a dragpoint won't obscure a node
176     data.outline_group = create_control_group(nt->desktop);
177     data.node_data.handle_line_group = create_control_group(nt->desktop);
178     data.dragpoint_group = create_control_group(nt->desktop);
179     nt->_transform_handle_group = create_control_group(nt->desktop);
180     data.node_data.node_group = create_control_group(nt->desktop);
181     data.node_data.handle_group = create_control_group(nt->desktop);
183     Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
184     nt->_selection_changed_connection.disconnect();
185     nt->_selection_changed_connection =
186         selection->connectChanged(
187             sigc::bind<0>(
188                 sigc::ptr_fun(&ink_node_tool_selection_changed),
189                 nt));
190     nt->_selection_modified_connection.disconnect();
191     nt->_selection_modified_connection =
192         selection->connectModified(
193             sigc::hide(sigc::bind<0>(
194                 sigc::ptr_fun(&ink_node_tool_selection_changed),
195                 nt)));
196     nt->_mouseover_changed_connection.disconnect();
197     nt->_mouseover_changed_connection = 
198         Inkscape::UI::ControlPoint::signal_mouseover_change.connect(
199             sigc::bind<0>(
200                 sigc::ptr_fun(&ink_node_tool_mouseover_changed),
201                 nt));
202     
203     nt->_selected_nodes.reset(
204         new Inkscape::UI::ControlPointSelection(nt->desktop, nt->_transform_handle_group));
205     data.node_data.selection = nt->_selected_nodes.get();
206     nt->_multipath.reset(new Inkscape::UI::MultiPathManipulator(data,
207         nt->_selection_changed_connection));
209     nt->_selector->signal_point.connect(
210         sigc::bind<0>(
211             sigc::ptr_fun(&ink_node_tool_select_point),
212             nt));
213     nt->_selector->signal_area.connect(
214         sigc::bind<0>(
215             sigc::ptr_fun(&ink_node_tool_select_area),
216             nt));
218     nt->_multipath->signal_coords_changed.connect(
219         sigc::bind(
220             sigc::mem_fun(*nt->desktop, &SPDesktop::emitToolSubselectionChanged),
221             (void*) 0));
222     nt->_selected_nodes->signal_point_changed.connect(
223         sigc::hide( sigc::hide(
224             sigc::bind(
225                 sigc::bind(
226                     sigc::ptr_fun(ink_node_tool_update_tip),
227                     (GdkEvent*)0),
228                 nt))));
230     nt->cursor_drag = false;
231     nt->show_transform_handles = true;
232     nt->single_node_transform_handles = false;
233     nt->flash_tempitem = NULL;
234     nt->flashed_item = NULL;
235     // TODO remove this!
236     nt->shape_editor = new ShapeEditor(nt->desktop);
238     // read prefs before adding items to selection to prevent momentarily showing the outline
239     sp_event_context_read(nt, "show_handles");
240     sp_event_context_read(nt, "show_outline");
241     sp_event_context_read(nt, "show_path_direction");
242     sp_event_context_read(nt, "show_transform_handles");
243     sp_event_context_read(nt, "single_node_transform_handles");
244     sp_event_context_read(nt, "edit_clipping_paths");
245     sp_event_context_read(nt, "edit_masks");
247     ink_node_tool_selection_changed(nt, selection);
248     ink_node_tool_update_tip(nt, NULL);
250     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
251     if (prefs->getBool("/tools/nodes/selcue")) {
252         ec->enableSelectionCue();
253     }
254     if (prefs->getBool("/tools/nodes/gradientdrag")) {
255         ec->enableGrDrag();
256     }
258     nt->desktop->emitToolSubselectionChanged(NULL); // sets the coord entry fields to inactive
261 void ink_node_tool_set(SPEventContext *ec, Inkscape::Preferences::Entry *value)
263     InkNodeTool *nt = INK_NODE_TOOL(ec);
264     Glib::ustring entry_name = value->getEntryName();
266     if (entry_name == "show_handles") {
267         nt->_multipath->showHandles(value->getBool(true));
268     } else if (entry_name == "show_outline") {
269         nt->show_outline = value->getBool();
270         nt->_multipath->showOutline(nt->show_outline);
271     } else if (entry_name == "show_path_direction") {
272         nt->show_path_direction = value->getBool();
273         nt->_multipath->showPathDirection(nt->show_path_direction);
274     } else if (entry_name == "show_transform_handles") {
275         nt->show_transform_handles = value->getBool(true);
276         nt->_selected_nodes->showTransformHandles(
277             nt->show_transform_handles, nt->single_node_transform_handles);
278     } else if (entry_name == "single_node_transform_handles") {
279         nt->single_node_transform_handles = value->getBool();
280         nt->_selected_nodes->showTransformHandles(
281             nt->show_transform_handles, nt->single_node_transform_handles);
282     } else if (entry_name == "edit_clipping_paths") {
283         nt->edit_clipping_paths = value->getBool();
284         ink_node_tool_selection_changed(nt, nt->desktop->selection);
285     } else if (entry_name == "edit_masks") {
286         nt->edit_masks = value->getBool();
287         ink_node_tool_selection_changed(nt, nt->desktop->selection);
288     } else {
289         SPEventContextClass *parent_class =
290             (SPEventContextClass *) g_type_class_peek(g_type_parent(INK_TYPE_NODE_TOOL));
291         if (parent_class->set)
292             parent_class->set(ec, value);
293     }
296 void store_clip_mask_items(SPItem *clipped, SPObject *obj, std::map<SPItem*,
297     std::pair<Geom::Matrix, guint32> > &s, Geom::Matrix const &postm, guint32 color)
299     if (!obj) return;
300     if (SP_IS_GROUP(obj) || SP_IS_OBJECTGROUP(obj)) {
301         //TODO is checking for obj->children != NULL above better?
302         for (SPObject *c = obj->children; c; c = c->next) {
303             store_clip_mask_items(clipped, c, s, postm, color);
304         }
305     } else if (SP_IS_ITEM(obj)) {
306         s.insert(std::make_pair(SP_ITEM(obj),
307             std::make_pair(sp_item_i2d_affine(clipped) * postm, color)));
308     }
311 /** Recursively collect ShapeRecords */
312 void gather_items(InkNodeTool *nt, SPItem *base, SPObject *obj, Inkscape::UI::ShapeRole role,
313     std::set<Inkscape::UI::ShapeRecord> &s)
315     using namespace Inkscape::UI;
316     if (!obj) return;
317     if (role != SHAPE_ROLE_NORMAL && (SP_IS_GROUP(obj) || SP_IS_OBJECTGROUP(obj))) {
318         for (SPObject *c = obj->children; c; c = c->next) {
319             gather_items(nt, base, c, role, s);
320         }
321     } else if (SP_IS_ITEM(obj)) {
322         SPItem *item = static_cast<SPItem*>(obj);
323         ShapeRecord r;
324         r.item = item;
325         // TODO add support for objectBoundingBox
326         r.edit_transform = base ? sp_item_i2doc_affine(base) : Geom::identity();
327         r.role = role;
328         r.edit_original = false;
329         if (s.insert(r).second) {
330             // this item was encountered the first time
331             if (nt->edit_clipping_paths && item->clip_ref) {
332                 gather_items(nt, item, item->clip_ref->getObject(), SHAPE_ROLE_CLIPPING_PATH, s);
333             }
334             if (nt->edit_masks && item->mask_ref) {
335                 gather_items(nt, item, item->mask_ref->getObject(), SHAPE_ROLE_MASK, s);
336             }
337         }
338     }
341 struct IsPath {
342     bool operator()(SPItem *i) const { return SP_IS_PATH(i); }
343 };
345 void ink_node_tool_selection_changed(InkNodeTool *nt, Inkscape::Selection *sel)
347     using namespace Inkscape::UI;
349     std::set<ShapeRecord> shapes;
351     // TODO this is ugly!!!
352     //typedef std::map<SPItem*, std::pair<Geom::Matrix, guint32> > TransMap;
353     //typedef std::map<SPPath*, std::pair<Geom::Matrix, guint32> > PathMap;
354     GSList const *ilist = sel->itemList();
356     for (GSList *i = const_cast<GSList*>(ilist); i; i = i->next) {
357         SPObject *obj = static_cast<SPObject*>(i->data);
358         if (SP_IS_ITEM(obj)) {
359             gather_items(nt, NULL, static_cast<SPItem*>(obj), SHAPE_ROLE_NORMAL, shapes);
360         }
361     }
363     // ugly hack: set the first editable non-path item for knotholder
364     // maybe use multiple ShapeEditors for now, to allow editing many shapes at once?
365     bool something_set = false;
366     for (std::set<ShapeRecord>::iterator i = shapes.begin(); i != shapes.end(); ++i) {
367         ShapeRecord const &r = *i;
368         if (SP_IS_SHAPE(r.item) && !SP_IS_PATH(r.item)) {
369             nt->shape_editor->set_item(r.item, SH_KNOTHOLDER);
370             something_set = true;
371             break;
372         }
373     }
374     if (!something_set) {
375         nt->shape_editor->unset_item(SH_KNOTHOLDER);
376     }
378     nt->_multipath->setItems(shapes);
379     ink_node_tool_update_tip(nt, NULL);
380     nt->desktop->updateNow();
383 gint ink_node_tool_root_handler(SPEventContext *event_context, GdkEvent *event)
385     /* things to handle here:
386      * 1. selection of items
387      * 2. passing events to manipulators
388      * 3. some keybindings
389      */
390     using namespace Inkscape::UI; // pull in event helpers
391     
392     SPDesktop *desktop = event_context->desktop;
393     Inkscape::Selection *selection = desktop->selection;
394     InkNodeTool *nt = static_cast<InkNodeTool*>(event_context);
395     static Inkscape::Preferences *prefs = Inkscape::Preferences::get();
396     
397     if (nt->_multipath->event(event)) return true;
398     if (nt->_selector->event(event)) return true;
399     if (nt->_selected_nodes->event(event)) return true;
401     switch (event->type)
402     {
403     case GDK_MOTION_NOTIFY:
404         // create outline
405         if (prefs->getBool("/tools/nodes/pathflash_enabled")) {
406             if (prefs->getBool("/tools/nodes/pathflash_unselected") && !nt->_multipath->empty())
407                 break;
409             SPItem *over_item = sp_event_context_find_item (desktop, event_point(event->button),
410                 FALSE, TRUE);
411             if (over_item == nt->flashed_item) break;
412             if (nt->flash_tempitem) {
413                 desktop->remove_temporary_canvasitem(nt->flash_tempitem);
414                 nt->flash_tempitem = NULL;
415                 nt->flashed_item = NULL;
416             }
417             if (!SP_IS_PATH(over_item)) break; // for now, handle only paths
419             nt->flashed_item = over_item;
420             SPCurve *c = sp_path_get_curve_for_edit(SP_PATH(over_item));
421             c->transform(sp_item_i2d_affine(over_item));
422             SPCanvasItem *flash = sp_canvas_bpath_new(sp_desktop_tempgroup(desktop), c);
423             sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(flash),
424                 prefs->getInt("/tools/nodes/highlight_color", 0xff0000ff), 1.0,
425                 SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
426             sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(flash), 0, SP_WIND_RULE_NONZERO);
427             nt->flash_tempitem = desktop->add_temporary_canvasitem(flash,
428                 prefs->getInt("/tools/nodes/pathflash_timeout", 500));
429             c->unref();
430         }
431         return true;
432     case GDK_KEY_PRESS:
433         switch (get_group0_keyval(&event->key))
434         {
435         case GDK_Escape: // deselect everything
436             if (nt->_selected_nodes->empty()) {
437                 selection->clear();
438             } else {
439                 nt->_selected_nodes->clear();
440             }
441             ink_node_tool_update_tip(nt, event);
442             return TRUE;
443         case GDK_a:
444             if (held_control(event->key)) {
445                 if (held_alt(event->key)) {
446                     nt->_multipath->selectAll();
447                 } else {
448                     // select all nodes in subpaths that have something selected
449                     // if nothing is selected, select everything
450                     nt->_multipath->selectSubpaths();
451                 }
452                 ink_node_tool_update_tip(nt, event);
453                 return TRUE;
454             }
455             break;
456         default:
457             break;
458         }
459         ink_node_tool_update_tip(nt, event);
460         break;
461     case GDK_KEY_RELEASE:
462         ink_node_tool_update_tip(nt, event);
463         break;
464     default: break;
465     }
466     
467     SPEventContextClass *parent_class = (SPEventContextClass *) g_type_class_peek(g_type_parent(INK_TYPE_NODE_TOOL));
468     if (parent_class->root_handler)
469         return parent_class->root_handler(event_context, event);
470     return FALSE;
473 void ink_node_tool_update_tip(InkNodeTool *nt, GdkEvent *event)
475     using namespace Inkscape::UI;
476     if (event && (event->type == GDK_KEY_PRESS || event->type == GDK_KEY_RELEASE)) {
477         unsigned new_state = state_after_event(event);
478         if (new_state == event->key.state) return;
479         if (state_held_shift(new_state)) {
480             nt->_node_message_context->set(Inkscape::NORMAL_MESSAGE,
481                 C_("Node tool tip", "<b>Shift:</b> drag to add nodes to the selection, "
482                 "click to toggle object selection"));
483             return;
484         }
485     }
486     unsigned sz = nt->_selected_nodes->size();
487     if (sz != 0) {
488         char *dyntip = g_strdup_printf(C_("Node tool tip",
489             "Selected <b>%d nodes</b>. Drag to select nodes, click to select a single object "
490             "or unselect all objects"), sz);
491         nt->_node_message_context->set(Inkscape::NORMAL_MESSAGE, dyntip);
492         g_free(dyntip);
493     } else if (nt->_multipath->empty()) {
494         nt->_node_message_context->set(Inkscape::NORMAL_MESSAGE,
495             C_("Node tool tip", "Drag or click to select objects to edit"));
496     } else {
497         nt->_node_message_context->set(Inkscape::NORMAL_MESSAGE,
498             C_("Node tool tip", "Drag to select nodes, click to select an object "
499             "or clear the selection"));
500     }
503 gint ink_node_tool_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
505     SPEventContextClass *parent_class =
506         (SPEventContextClass *) g_type_class_peek(g_type_parent(INK_TYPE_NODE_TOOL));
507     if (parent_class->item_handler)
508         return parent_class->item_handler(event_context, item, event);
509     return FALSE;
512 void ink_node_tool_select_area(InkNodeTool *nt, Geom::Rect const &sel, GdkEventButton *event)
514     using namespace Inkscape::UI;
515     if (nt->_multipath->empty()) {
516         // if multipath is empty, select rubberbanded items rather than nodes
517         Inkscape::Selection *selection = nt->desktop->selection;
518         GSList *items = sp_document_items_in_box(
519             sp_desktop_document(nt->desktop), nt->desktop->dkey, sel);
520         selection->setList(items);
521         g_slist_free(items);
522     } else {
523         nt->_multipath->selectArea(sel, !held_shift(*event));
524     }
526 void ink_node_tool_select_point(InkNodeTool *nt, Geom::Point const &sel, GdkEventButton *event)
528     using namespace Inkscape::UI; // pull in event helpers
529     if (!event) return;
530     if (event->button != 1) return;
532     Inkscape::Selection *selection = nt->desktop->selection;
534     SPItem *item_clicked = sp_event_context_find_item (nt->desktop, event_point(*event),
535                     (event->state & GDK_MOD1_MASK) && !(event->state & GDK_CONTROL_MASK), TRUE);
537     if (item_clicked == NULL) { // nothing under cursor
538         // if no Shift, deselect
539         if (!(event->state & GDK_SHIFT_MASK)) {
540             selection->clear();
541         }
542         return;
543     }
544     if (held_shift(*event)) {
545         selection->toggle(item_clicked);
546     } else {
547         selection->set(item_clicked);
548     }
549     nt->desktop->updateNow();
552 void ink_node_tool_mouseover_changed(InkNodeTool *nt, Inkscape::UI::ControlPoint *p)
554     using Inkscape::UI::CurveDragPoint;
555     CurveDragPoint *cdp = dynamic_cast<CurveDragPoint*>(p);
556     if (cdp && !nt->cursor_drag) {
557         nt->cursor_shape = cursor_node_d_xpm;
558         nt->hot_x = 1;
559         nt->hot_y = 1;
560         sp_event_context_update_cursor(nt);
561         nt->cursor_drag = true;
562     } else if (!cdp && nt->cursor_drag) {
563         nt->cursor_shape = cursor_node_xpm;
564         nt->hot_x = 1;
565         nt->hot_y = 1;
566         sp_event_context_update_cursor(nt);
567         nt->cursor_drag = false;
568     }
571 } // anonymous namespace
573 /*
574   Local Variables:
575   mode:c++
576   c-file-style:"stroustrup"
577   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
578   indent-tabs-mode:nil
579   fill-column:99
580   End:
581 */
582 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :