Code

Make click-select work in 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 <2geom/sbasis-geometric.h>
21 #include <gdk/gdkkeysyms.h>
23 #include "macros.h"
24 #include "forward.h"
25 #include "pixmaps/cursor-node.xpm"
26 #include "pixmaps/cursor-crosshairs.xpm"
27 #include <gtk/gtk.h>
28 #include "desktop.h"
29 #include "message-context.h"
30 #include "prefs-utils.h"
31 #include "shape-editor.h"
32 #include "selection.h"
33 #include "desktop-handles.h"
34 #include "document.h"
35 #include "display/curve.h"
36 #include "display/canvas-bpath.h"
37 #include "message-stack.h"
38 #include "sp-path.h"
39 #include "helper/units.h"
41 #include "lpe-tool-context.h"
43 static void sp_lpetool_context_class_init(SPLPEToolContextClass *klass);
44 static void sp_lpetool_context_init(SPLPEToolContext *erc);
45 static void sp_lpetool_context_dispose(GObject *object);
47 static void sp_lpetool_context_setup(SPEventContext *ec);
48 static void sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
49 static gint sp_lpetool_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event);
50 static gint sp_lpetool_context_root_handler(SPEventContext *ec, GdkEvent *event);
52 void sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data);
54 const int num_subtools = 8;
56 Inkscape::LivePathEffect::EffectType lpesubtools[] = {
57     Inkscape::LivePathEffect::INVALID_LPE, // this must be here to account for the "all inactive" action
58     Inkscape::LivePathEffect::LINE_SEGMENT,
59     Inkscape::LivePathEffect::CIRCLE_3PTS,
60     Inkscape::LivePathEffect::CIRCLE_WITH_RADIUS,
61     Inkscape::LivePathEffect::PARALLEL,
62     Inkscape::LivePathEffect::PERP_BISECTOR,
63     Inkscape::LivePathEffect::ANGLE_BISECTOR,
64     Inkscape::LivePathEffect::MIRROR_SYMMETRY,
65 };
67 static SPPenContextClass *lpetool_parent_class = 0;
69 GType sp_lpetool_context_get_type(void)
70 {
71     static GType type = 0;
72     if (!type) {
73         GTypeInfo info = {
74             sizeof(SPLPEToolContextClass),
75             0, // base_init
76             0, // base_finalize
77             (GClassInitFunc)sp_lpetool_context_class_init,
78             0, // class_finalize
79             0, // class_data
80             sizeof(SPLPEToolContext),
81             0, // n_preallocs
82             (GInstanceInitFunc)sp_lpetool_context_init,
83             0 // value_table
84         };
85         type = g_type_register_static(SP_TYPE_PEN_CONTEXT, "SPLPEToolContext", &info, static_cast<GTypeFlags>(0));
86     }
87     return type;
88 }
90 static void
91 sp_lpetool_context_class_init(SPLPEToolContextClass *klass)
92 {
93     GObjectClass *object_class = (GObjectClass *) klass;
94     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
96     lpetool_parent_class = (SPPenContextClass*)g_type_class_peek_parent(klass);
98     object_class->dispose = sp_lpetool_context_dispose;
100     event_context_class->setup = sp_lpetool_context_setup;
101     event_context_class->set = sp_lpetool_context_set;
102     event_context_class->root_handler = sp_lpetool_context_root_handler;
103     event_context_class->item_handler = sp_lpetool_context_item_handler;
106 static void
107 sp_lpetool_context_init(SPLPEToolContext *lc)
109     lc->cursor_shape = cursor_crosshairs_xpm;
110     lc->hot_x = 7;
111     lc->hot_y = 7;
113     lc->canvas_bbox = NULL;
114     lc->measuring_items = new std::map<SPPath *, SPCanvasItem*>;
116     new (&lc->sel_changed_connection) sigc::connection();
119 static void
120 sp_lpetool_context_dispose(GObject *object)
122     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(object);
123     delete lc->shape_editor;
125     if (lc->canvas_bbox) {
126         gtk_object_destroy(GTK_OBJECT(lc->canvas_bbox));
127         lc->canvas_bbox = NULL;
128     }
130     lpetool_delete_measuring_items(lc);
131     delete lc->measuring_items;
132     lc->measuring_items = NULL;
134     lc->sel_changed_connection.disconnect();
135     lc->sel_changed_connection.~connection();
137     if (lc->_lpetool_message_context) {
138         delete lc->_lpetool_message_context;
139     }
141     G_OBJECT_CLASS(lpetool_parent_class)->dispose(object);
144 static void
145 sp_lpetool_context_setup(SPEventContext *ec)
147     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(ec);
149     if (((SPEventContextClass *) lpetool_parent_class)->setup)
150         ((SPEventContextClass *) lpetool_parent_class)->setup(ec);
152     Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
153     SPItem *item = selection->singleItem();
155     lc->sel_changed_connection.disconnect();
156     lc->sel_changed_connection =
157         selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_lpetool_context_selection_changed), (gpointer)lc));
159     lc->shape_editor = new ShapeEditor(ec->desktop);
161     lpetool_context_switch_mode(lc, Inkscape::LivePathEffect::INVALID_LPE);
162     lpetool_context_reset_limiting_bbox(lc);
163     lpetool_create_measuring_items(lc);
165 // TODO temp force:
166     ec->enableSelectionCue();
168     if (item) {
169         lc->shape_editor->set_item(item, SH_NODEPATH);
170         lc->shape_editor->set_item(item, SH_KNOTHOLDER);
171     }
173     if (prefs_get_int_attribute("tools.lpetool", "selcue", 0) != 0) {
174         ec->enableSelectionCue();
175     }
177     lc->_lpetool_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
180     lc->shape_editor->update_statusbar();
183 /**
184 \brief  Callback that processes the "changed" signal on the selection;
185 destroys old and creates new nodepath and reassigns listeners to the new selected item's repr
186 */
187 void
188 sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data)
190     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(data);
192     // TODO: update ShapeEditorsCollective instead
193     lc->shape_editor->unset_item(SH_NODEPATH);
194     lc->shape_editor->unset_item(SH_KNOTHOLDER);
195     SPItem *item = selection->singleItem();
196     lc->shape_editor->set_item(item, SH_NODEPATH);
197     lc->shape_editor->set_item(item, SH_KNOTHOLDER);
198     lc->shape_editor->update_statusbar();
201 static void
202 sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
204     // FIXME: how to set this correcly? the value from preferences-skeleton.h doesn't seem to get
205     // read (it wants to set drag = 1)
206     lpetool_parent_class->set(ec, key, "drag");
208     /**
209     //pass on up to parent class to handle common attributes.
210     if ( lpetool_parent_class->set ) {
211         lpetool_parent_class->set(ec, key, val);
212     }
213     **/
216 static gint 
217 sp_lpetool_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
219     gint ret = FALSE;
221     switch (event->type) {
222         case GDK_BUTTON_PRESS:
223         {
224             // select the clicked item but do nothing else
225             Inkscape::Selection * const selection = sp_desktop_selection(ec->desktop);
226             selection->clear();
227             selection->add(item);
228             ret = TRUE;
229             break;
230         }
231         case GDK_BUTTON_RELEASE:
232             // TODO: do we need to catch this or can we pass it on to the parent handler?
233             ret = TRUE;
234             break;
235         default:
236             break;
237     }
239     if (!ret) {
240         if (((SPEventContextClass *) lpetool_parent_class)->item_handler)
241             ret = ((SPEventContextClass *) lpetool_parent_class)->item_handler(ec, item, event);
242     }
244     return ret;
247 gint
248 sp_lpetool_context_root_handler(SPEventContext *event_context, GdkEvent *event)
250     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(event_context);
251     SPDesktop *desktop = event_context->desktop;
252     Inkscape::Selection *selection = sp_desktop_selection (desktop);
254     bool ret = false;
256     if (sp_pen_context_has_waiting_LPE(lc)) {
257         // quit when we are waiting for a LPE to be applied
258         ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
259         return ret;
260     }
262     switch (event->type) {
263         case GDK_BUTTON_PRESS:
264             if (event->button.button == 1 && !event_context->space_panning) {
265                 if (lc->mode == Inkscape::LivePathEffect::INVALID_LPE) {
266                     // don't do anything for now if we are inactive
267                     desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Choose a construction tool from the toolbar."));
268                     ret = true;
269                     break;
270                 }
272                 // save drag origin
273                 event_context->xp = (gint) event->button.x;
274                 event_context->yp = (gint) event->button.y;
275                 event_context->within_tolerance = true;
276                 lc->shape_editor->cancel_hit();
278                 using namespace Inkscape::LivePathEffect;
280                 int mode = prefs_get_int_attribute("tools.lpetool", "mode", 0);
281                 EffectType type = lpesubtools[mode];
283                 //bool over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->button.x, event->button.y), true);
285                 sp_pen_context_wait_for_LPE_mouse_clicks(lc, type, Inkscape::LivePathEffect::Effect::acceptsNumClicks(type));
287                 // we pass the mouse click on to pen tool as the first click which it should collect
288                 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
289             }
290             break;
291         case GDK_MOTION_NOTIFY:
292         {
293             if (!lc->shape_editor->has_nodepath() || selection->singleItem() == NULL) {
294                 break;
295             }
297             bool over_stroke = false;
298             over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->motion.x, event->motion.y), false);
300             if (over_stroke) {
301                 event_context->cursor_shape = cursor_node_xpm;
302                 event_context->hot_x = 1;
303                 event_context->hot_y = 1;
304                 sp_event_context_update_cursor(event_context);
305             } else {
306                 lc->cursor_shape = cursor_crosshairs_xpm;
307                 lc->hot_x = 7;
308                 lc->hot_y = 7;
309                 sp_event_context_update_cursor(event_context);
310             }
311         }
312         break;
315     case GDK_BUTTON_RELEASE:
316     {
317         /**
318         break;
319         **/
320     }
322     case GDK_KEY_PRESS:
323         /**
324         switch (get_group0_keyval (&event->key)) {
325         }
326         break;
327         **/
329     case GDK_KEY_RELEASE:
330         /**
331         switch (get_group0_keyval(&event->key)) {
332             case GDK_Control_L:
333             case GDK_Control_R:
334                 dc->_message_context->clear();
335                 break;
336             default:
337                 break;
338         }
339         **/
341     default:
342         break;
343     }
345     if (!ret) {
346         if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
347             ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
348         }
349     }
351     return ret;
354 /*
355  * Finds the index in the list of geometric subtools corresponding to the given LPE type.
356  * Returns -1 if no subtool is found.
357  */
358 int
359 lpetool_mode_to_index(Inkscape::LivePathEffect::EffectType const type) {
360     for (int i = 0; i < num_subtools; ++i) {
361         if (lpesubtools[i] == type) {
362             return i;
363         }
364     }
365     return -1;
368 /*
369  * Checks whether an item has a construction applied as LPE and if so returns the index in
370  * lpesubtools of this construction
371  */
372 int lpetool_item_has_construction(SPLPEToolContext *lc, SPItem *item)
374     if (!SP_IS_LPE_ITEM(item)) {
375         return -1;
376     }
378     Inkscape::LivePathEffect::Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
379     if (!lpe) {
380         return -1;
381     }
382     return lpetool_mode_to_index(lpe->effectType());
385 /*
386  * Attempts to perform the construction of the given type (i.e., to apply the corresponding LPE) to
387  * a single selected item. Returns whether we succeeded.
388  */
389 bool
390 lpetool_try_construction(SPLPEToolContext *lc, Inkscape::LivePathEffect::EffectType const type)
392     Inkscape::Selection *selection = sp_desktop_selection(lc->desktop);
393     SPItem *item = selection->singleItem();
395     // TODO: should we check whether type represents a valid geometric construction?
396     if (item && SP_IS_LPE_ITEM(item) && Inkscape::LivePathEffect::Effect::acceptsNumClicks(type) == 0) {
397         Inkscape::LivePathEffect::Effect::createAndApply(type, sp_desktop_document(lc->desktop), item);
398         return true;
399     }
400     return false;
403 void
404 lpetool_context_switch_mode(SPLPEToolContext *lc, Inkscape::LivePathEffect::EffectType const type)
406     int index = lpetool_mode_to_index(type);
407     if (index != -1) {
408         lc->mode = type;
409         lc->desktop->setToolboxSelectOneValue ("lpetool_mode_action", index);
410     } else {
411         g_warning ("Invalid mode selected: %d", type);
412         return;
413     }
416 void
417 lpetool_get_limiting_bbox_corners(SPDocument *document, Geom::Point &A, Geom::Point &B) {
418     Geom::Coord w = sp_document_width(document);
419     Geom::Coord h = sp_document_height(document);
421     double ulx = prefs_get_double_attribute ("tools.lpetool", "bbox_upperleftx", 0);
422     double uly = prefs_get_double_attribute ("tools.lpetool", "bbox_upperlefty", 0);
423     double lrx = prefs_get_double_attribute ("tools.lpetool", "bbox_lowerrightx", w);
424     double lry = prefs_get_double_attribute ("tools.lpetool", "bbox_lowerrighty", h);
426     A = Geom::Point(ulx, uly);
427     B = Geom::Point(lrx, lry);
430 /*
431  * Reads the limiting bounding box from preferences and draws it on the screen
432  */
433 // TODO: Note that currently the bbox is not user-settable; we simply use the page borders
434 void
435 lpetool_context_reset_limiting_bbox(SPLPEToolContext *lc)
437     if (lc->canvas_bbox) {
438         gtk_object_destroy(GTK_OBJECT(lc->canvas_bbox));
439         lc->canvas_bbox = NULL;
440     }
442     if (prefs_get_int_attribute("tools.lpetool", "show_bbox", 1) == 0)
443         return;
445     SPDocument *document = sp_desktop_document(lc->desktop);
447     Geom::Point A, B;
448     lpetool_get_limiting_bbox_corners(document, A, B);
449     NR::Matrix doc2dt(lc->desktop->doc2dt());
450     A *= doc2dt;
451     B *= doc2dt;
453     Geom::Rect rect(A, B);
454     SPCurve *curve = SPCurve::new_from_rect(rect);
456     lc->canvas_bbox = sp_canvas_bpath_new (sp_desktop_controls(lc->desktop), curve);
457     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(lc->canvas_bbox), 0x0000ffff, 0.8, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT, 5, 5);
460 static void
461 set_pos_and_anchor(SPCanvasText *canvas_text, const Geom::Piecewise<Geom::D2<Geom::SBasis> > &pwd2,
462                    const double t, const double length, bool use_curvature = false)
464     using namespace Geom;
466     Piecewise<D2<SBasis> > pwd2_reparam = arc_length_parametrization(pwd2, 2 , 0.1);
467     double t_reparam = pwd2_reparam.cuts.back() * t;
468     Point pos = pwd2_reparam.valueAt(t_reparam);
469     Point dir = unit_vector(derivative(pwd2_reparam).valueAt(t_reparam));
470     Point n = -rot90(dir);
471     double angle = Geom::angle_between(dir, Point(1,0));
473     sp_canvastext_set_coords(canvas_text, pos + n * length);
474     sp_canvastext_set_anchor(canvas_text, std::sin(angle), -std::cos(angle));
477 void
478 lpetool_create_measuring_items(SPLPEToolContext *lc, Inkscape::Selection *selection)
480     bool show = prefs_get_int_attribute ("tools.lpetool", "show_measuring_info",  1) == 1 ? true : false;
481     if (!selection) {
482         selection = sp_desktop_selection(lc->desktop);
483     }
485     SPPath *path;
486     SPCurve *curve;
487     SPCanvasText *canvas_text;
488     SPCanvasGroup *tmpgrp = sp_desktop_tempgroup(lc->desktop);
489     gchar *arc_length;
490     double lengthval;
492     for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
493         if (SP_IS_PATH(i->data)) {
494             path = SP_PATH(i->data);
495             curve = sp_shape_get_curve(SP_SHAPE(path));
496             Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = paths_to_pw(curve->get_pathvector());
497             canvas_text = (SPCanvasText *) sp_canvastext_new(tmpgrp, lc->desktop, Geom::Point(0,0), "");
498             if (!show)
499                 sp_canvas_item_hide(SP_CANVAS_ITEM(canvas_text));
501             SPUnitId unitid = static_cast<SPUnitId>(prefs_get_int_attribute("tools.lpetool", "unitid", SP_UNIT_PX));
502             SPUnit unit = sp_unit_get_by_id(unitid);
504             lengthval = Geom::length(pwd2);
505             gboolean success;
506             success = sp_convert_distance(&lengthval, &sp_unit_get_by_id(SP_UNIT_PX), &unit);
507             arc_length = g_strdup_printf("%.2f %s", lengthval, success ? sp_unit_get_abbreviation(&unit) : "px");
508             sp_canvastext_set_text (canvas_text, arc_length);
509             set_pos_and_anchor(canvas_text, pwd2, 0.5, 10);
510             // TODO: must we free arc_length?
511             (*lc->measuring_items)[path] = SP_CANVAS_ITEM(canvas_text);
512         }
513     }
516 void
517 lpetool_delete_measuring_items(SPLPEToolContext *lc)
519     std::map<SPPath *, SPCanvasItem*>::iterator i;
520     for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) {
521         gtk_object_destroy(GTK_OBJECT(i->second));
522     }
523     lc->measuring_items->clear();
526 void
527 lpetool_update_measuring_items(SPLPEToolContext *lc)
529     SPPath *path;
530     SPCurve *curve;
531     double lengthval;
532     gchar *arc_length;
533     std::map<SPPath *, SPCanvasItem*>::iterator i;
534     for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) {
535         path = i->first;
536         curve = sp_shape_get_curve(SP_SHAPE(path));
537         Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = Geom::paths_to_pw(curve->get_pathvector());
538         SPUnitId unitid = static_cast<SPUnitId>(prefs_get_int_attribute("tools.lpetool", "unitid", SP_UNIT_PX));
539         SPUnit unit = sp_unit_get_by_id(unitid);
540         lengthval = Geom::length(pwd2);
541         gboolean success;
542         success = sp_convert_distance(&lengthval, &sp_unit_get_by_id(SP_UNIT_PX), &unit);
543         arc_length = g_strdup_printf("%.2f %s", lengthval, success ? sp_unit_get_abbreviation(&unit) : "px");
544         sp_canvastext_set_text (SP_CANVASTEXT(i->second), arc_length);
545         set_pos_and_anchor(SP_CANVASTEXT(i->second), pwd2, 0.5, 10);
546         // TODO: must we free arc_length?
547     }
550 void
551 lpetool_show_measuring_info(SPLPEToolContext *lc, bool show)
553     std::map<SPPath *, SPCanvasItem*>::iterator i;
554     for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) {
555         if (show) {
556             sp_canvas_item_show(i->second);
557         } else {
558             sp_canvas_item_hide(i->second);
559         }
560     }
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 :