Code

Add mirror symmetry to geometry tool
[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 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
20 #include "forward.h"
21 #include "pixmaps/cursor-node.xpm"
22 #include "pixmaps/cursor-crosshairs.xpm"
23 #include <gtk/gtk.h>
24 #include "desktop.h"
25 #include "message-context.h"
26 #include "prefs-utils.h"
27 #include "shape-editor.h"
28 #include "selection.h"
29 #include "desktop-handles.h"
30 #include "document.h"
31 #include "display/curve.h"
32 #include "display/canvas-bpath.h"
33 #include "message-stack.h"
35 #include "lpe-tool-context.h"
37 static void sp_lpetool_context_class_init(SPLPEToolContextClass *klass);
38 static void sp_lpetool_context_init(SPLPEToolContext *erc);
39 static void sp_lpetool_context_dispose(GObject *object);
41 static void sp_lpetool_context_setup(SPEventContext *ec);
42 static void sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
43 static gint sp_lpetool_context_root_handler(SPEventContext *ec, GdkEvent *event);
45 void sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data);
47 const int num_subtools = 8;
49 Inkscape::LivePathEffect::EffectType lpesubtools[] = {
50     Inkscape::LivePathEffect::INVALID_LPE, // this must be here to account for the "all inactive" action
51     Inkscape::LivePathEffect::LINE_SEGMENT,
52     Inkscape::LivePathEffect::CIRCLE_3PTS,
53     Inkscape::LivePathEffect::CIRCLE_WITH_RADIUS,
54     Inkscape::LivePathEffect::PARALLEL,
55     Inkscape::LivePathEffect::PERP_BISECTOR,
56     Inkscape::LivePathEffect::ANGLE_BISECTOR,
57     Inkscape::LivePathEffect::MIRROR_SYMMETRY,
58 };
60 static SPPenContextClass *lpetool_parent_class = 0;
62 GType sp_lpetool_context_get_type(void)
63 {
64     static GType type = 0;
65     if (!type) {
66         GTypeInfo info = {
67             sizeof(SPLPEToolContextClass),
68             0, // base_init
69             0, // base_finalize
70             (GClassInitFunc)sp_lpetool_context_class_init,
71             0, // class_finalize
72             0, // class_data
73             sizeof(SPLPEToolContext),
74             0, // n_preallocs
75             (GInstanceInitFunc)sp_lpetool_context_init,
76             0 // value_table
77         };
78         type = g_type_register_static(SP_TYPE_PEN_CONTEXT, "SPLPEToolContext", &info, static_cast<GTypeFlags>(0));
79     }
80     return type;
81 }
83 static void
84 sp_lpetool_context_class_init(SPLPEToolContextClass *klass)
85 {
86     GObjectClass *object_class = (GObjectClass *) klass;
87     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
89     lpetool_parent_class = (SPPenContextClass*)g_type_class_peek_parent(klass);
91     object_class->dispose = sp_lpetool_context_dispose;
93     event_context_class->setup = sp_lpetool_context_setup;
94     event_context_class->set = sp_lpetool_context_set;
95     event_context_class->root_handler = sp_lpetool_context_root_handler;
96 }
98 static void
99 sp_lpetool_context_init(SPLPEToolContext *lc)
101     lc->cursor_shape = cursor_crosshairs_xpm;
102     lc->hot_x = 7;
103     lc->hot_y = 7;
105     lc->canvas_bbox = NULL;
107     new (&lc->sel_changed_connection) sigc::connection();
110 static void
111 sp_lpetool_context_dispose(GObject *object)
113     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(object);
114     delete lc->shape_editor;
116     if (lc->canvas_bbox) {
117         gtk_object_destroy(GTK_OBJECT(lc->canvas_bbox));
118         lc->canvas_bbox = NULL;
119     }
121     lc->sel_changed_connection.disconnect();
122     lc->sel_changed_connection.~connection();
124     G_OBJECT_CLASS(lpetool_parent_class)->dispose(object);
127 static void
128 sp_lpetool_context_setup(SPEventContext *ec)
130     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(ec);
132     if (((SPEventContextClass *) lpetool_parent_class)->setup)
133         ((SPEventContextClass *) lpetool_parent_class)->setup(ec);
135     Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
136     SPItem *item = selection->singleItem();
138     lc->sel_changed_connection.disconnect();
139     lc->sel_changed_connection =
140         selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_lpetool_context_selection_changed), (gpointer)lc));
142     lc->shape_editor = new ShapeEditor(ec->desktop);
144     lpetool_context_switch_mode(lc, Inkscape::LivePathEffect::INVALID_LPE);
145     lpetool_context_reset_limiting_bbox(lc);
147 // TODO temp force:
148     ec->enableSelectionCue();
150     if (item) {
151         lc->shape_editor->set_item(item, SH_NODEPATH);
152         lc->shape_editor->set_item(item, SH_KNOTHOLDER);
153     }
155     if (prefs_get_int_attribute("tools.lpetool", "selcue", 0) != 0) {
156         ec->enableSelectionCue();
157     }
159     lc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
161     lc->shape_editor->update_statusbar();
164 /**
165 \brief  Callback that processes the "changed" signal on the selection;
166 destroys old and creates new nodepath and reassigns listeners to the new selected item's repr
167 */
168 void
169 sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data)
171     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(data);
173     // TODO: update ShapeEditorsCollective instead
174     lc->shape_editor->unset_item(SH_NODEPATH);
175     lc->shape_editor->unset_item(SH_KNOTHOLDER);
176     SPItem *item = selection->singleItem();
177     lc->shape_editor->set_item(item, SH_NODEPATH);
178     lc->shape_editor->set_item(item, SH_KNOTHOLDER);
179     lc->shape_editor->update_statusbar();
182 static void
183 sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
185     // FIXME: how to set this correcly? the value from preferences-skeleton.h doesn't seem to get
186     // read (it wants to set drag = 1)
187     lpetool_parent_class->set(ec, key, "drag");
189     /**
190     //pass on up to parent class to handle common attributes.
191     if ( lpetool_parent_class->set ) {
192         lpetool_parent_class->set(ec, key, val);
193     }
194     **/
197 gint
198 sp_lpetool_context_root_handler(SPEventContext *event_context, GdkEvent *event)
200     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(event_context);
201     SPDesktop *desktop = event_context->desktop;
202     Inkscape::Selection *selection = sp_desktop_selection (desktop);
204     bool ret = false;
206     if (sp_pen_context_has_waiting_LPE(lc)) {
207         // quit when we are waiting for a LPE to be applied
208         ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
209         return ret;
210     }
212     switch (event->type) {
213         case GDK_BUTTON_PRESS:
214             if (lc->mode == Inkscape::LivePathEffect::INVALID_LPE) {
215                 // don't do anything for now if we are inactive
216                 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Choose a construction tool from the toolbar."));
217                 ret = true;
218                 break;
219             }
221             if (event->button.button == 1 && !event_context->space_panning) {
222                 // save drag origin
223                 event_context->xp = (gint) event->button.x;
224                 event_context->yp = (gint) event->button.y;
225                 event_context->within_tolerance = true;
226                 lc->shape_editor->cancel_hit();
228                 using namespace Inkscape::LivePathEffect;
230                 int mode = prefs_get_int_attribute("tools.lpetool", "mode", 0);
231                 EffectType type = lpesubtools[mode];
233                 // save drag origin
234                 bool over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->button.x, event->button.y), true);
236                 sp_pen_context_wait_for_LPE_mouse_clicks(lc, type, Inkscape::LivePathEffect::Effect::acceptsNumClicks(type));
238                 // we pass the mouse click on to pen tool as the first click which it should collect
239                 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
240             }
241             break;
242         case GDK_MOTION_NOTIFY:
243         {
244             if (!lc->shape_editor->has_nodepath() || selection->singleItem() == NULL) {
245                 break;
246             }
248             bool over_stroke = false;
249             over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->motion.x, event->motion.y), false);
251             if (over_stroke) {
252                 event_context->cursor_shape = cursor_node_xpm;
253                 event_context->hot_x = 1;
254                 event_context->hot_y = 1;
255                 sp_event_context_update_cursor(event_context);
256             } else {
257                 lc->cursor_shape = cursor_crosshairs_xpm;
258                 lc->hot_x = 7;
259                 lc->hot_y = 7;
260                 sp_event_context_update_cursor(event_context);
261             }
262         }
263         break;
266     case GDK_BUTTON_RELEASE:
267     {
268         /**
269         break;
270         **/
271     }
273     case GDK_KEY_PRESS:
274         /**
275         switch (get_group0_keyval (&event->key)) {
276         }
277         break;
278         **/
280     case GDK_KEY_RELEASE:
281         /**
282         switch (get_group0_keyval(&event->key)) {
283             case GDK_Control_L:
284             case GDK_Control_R:
285                 dc->_message_context->clear();
286                 break;
287             default:
288                 break;
289         }
290         **/
292     default:
293         break;
294     }
296     if (!ret) {
297         if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
298             ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
299         }
300     }
302     return ret;
305 /*
306  * Finds the index in the list of geometric subtools corresponding to the given LPE type.
307  * Returns -1 if no subtool is found.
308  */
309 int
310 lpetool_mode_to_index(Inkscape::LivePathEffect::EffectType const type) {
311     for (int i = 0; i < num_subtools; ++i) {
312         if (lpesubtools[i] == type) {
313             return i;
314         }
315     }
316     return -1;
319 /*
320  * Checks whether an item has a construction applied as LPE and if so returns the index in
321  * lpesubtools of this construction
322  */
323 int lpetool_item_has_construction(SPLPEToolContext *lc, SPItem *item)
325     if (!SP_IS_LPE_ITEM(item)) {
326         return -1;
327     }
329     Inkscape::LivePathEffect::Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
330     if (!lpe) {
331         return -1;
332     }
333     return lpetool_mode_to_index(lpe->effectType());
336 /*
337  * Attempts to perform the construction of the given type (i.e., to apply the corresponding LPE) to
338  * a single selected item. Returns whether we succeeded.
339  */
340 bool
341 lpetool_try_construction(SPLPEToolContext *lc, Inkscape::LivePathEffect::EffectType const type)
343     Inkscape::Selection *selection = sp_desktop_selection(lc->desktop);
344     SPItem *item = selection->singleItem();
346     // TODO: should we check whether type represents a valid geometric construction?
347     if (item && SP_IS_LPE_ITEM(item) && Inkscape::LivePathEffect::Effect::acceptsNumClicks(type) == 0) {
348         Inkscape::LivePathEffect::Effect::createAndApply(type, sp_desktop_document(lc->desktop), item);
349         return true;
350     }
351     return false;
354 void
355 lpetool_context_switch_mode(SPLPEToolContext *lc, Inkscape::LivePathEffect::EffectType const type)
357     int index = lpetool_mode_to_index(type);
358     if (index != -1) {
359         lc->mode = type;
360         lc->desktop->setToolboxSelectOneValue ("lpetool_mode_action", index);
361     } else {
362         g_warning ("Invalid mode selected: %d", type);
363         return;
364     }
367 void
368 lpetool_get_limiting_bbox_corners(SPDocument *document, Geom::Point &A, Geom::Point &B) {
369     Geom::Coord w = sp_document_width(document);
370     Geom::Coord h = sp_document_height(document);
372     double ulx = prefs_get_double_attribute ("tools.lpetool", "bbox_upperleftx", 0);
373     double uly = prefs_get_double_attribute ("tools.lpetool", "bbox_upperlefty", 0);
374     double lrx = prefs_get_double_attribute ("tools.lpetool", "bbox_lowerrightx", w);
375     double lry = prefs_get_double_attribute ("tools.lpetool", "bbox_lowerrighty", h);
377     A = Geom::Point(ulx, uly);
378     B = Geom::Point(lrx, lry);
381 /*
382  * Reads the limiting bounding box from preferences and draws it on the screen
383  */
384 // TODO: Note that currently the bbox is not user-settable; we simply use the page borders
385 void
386 lpetool_context_reset_limiting_bbox(SPLPEToolContext *lc)
388     if (lc->canvas_bbox) {
389         gtk_object_destroy(GTK_OBJECT(lc->canvas_bbox));
390         lc->canvas_bbox = NULL;
391     }
393     if (prefs_get_int_attribute("tools.lpetool", "show_bbox", 1) == 0)
394         return;
396     SPDocument *document = sp_desktop_document(lc->desktop);
398     Geom::Point A, B;
399     lpetool_get_limiting_bbox_corners(document, A, B);
400     NR::Matrix doc2dt(lc->desktop->doc2dt());
401     A *= doc2dt;
402     B *= doc2dt;
404     Geom::Rect rect(A, B);
405     SPCurve *curve = SPCurve::new_from_rect(rect);
407     lc->canvas_bbox = sp_canvas_bpath_new (sp_desktop_controls(lc->desktop), curve);
408     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(lc->canvas_bbox), 0x0000ffff, 0.8, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT, 5, 5);
411 /*
412   Local Variables:
413   mode:c++
414   c-file-style:"stroustrup"
415   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
416   indent-tabs-mode:nil
417   fill-column:99
418   End:
419 */
420 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :