Code

Draw limiting bounding box in geometriy 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"
34 #include "lpe-tool-context.h"
36 static void sp_lpetool_context_class_init(SPLPEToolContextClass *klass);
37 static void sp_lpetool_context_init(SPLPEToolContext *erc);
38 static void sp_lpetool_context_dispose(GObject *object);
40 static void sp_lpetool_context_setup(SPEventContext *ec);
41 static void sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
42 static gint sp_lpetool_context_root_handler(SPEventContext *ec, GdkEvent *event);
44 void sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data);
46 const int num_subtools = 6;
48 Inkscape::LivePathEffect::EffectType lpesubtools[] = {
49     Inkscape::LivePathEffect::LINE_SEGMENT,
50     Inkscape::LivePathEffect::CIRCLE_3PTS,
51     Inkscape::LivePathEffect::CIRCLE_WITH_RADIUS,
52     Inkscape::LivePathEffect::PARALLEL,
53     Inkscape::LivePathEffect::PERP_BISECTOR,
54     Inkscape::LivePathEffect::ANGLE_BISECTOR,
55 };
57 static SPPenContextClass *lpetool_parent_class = 0;
59 GType sp_lpetool_context_get_type(void)
60 {
61     static GType type = 0;
62     if (!type) {
63         GTypeInfo info = {
64             sizeof(SPLPEToolContextClass),
65             0, // base_init
66             0, // base_finalize
67             (GClassInitFunc)sp_lpetool_context_class_init,
68             0, // class_finalize
69             0, // class_data
70             sizeof(SPLPEToolContext),
71             0, // n_preallocs
72             (GInstanceInitFunc)sp_lpetool_context_init,
73             0 // value_table
74         };
75         type = g_type_register_static(SP_TYPE_PEN_CONTEXT, "SPLPEToolContext", &info, static_cast<GTypeFlags>(0));
76     }
77     return type;
78 }
80 static void
81 sp_lpetool_context_class_init(SPLPEToolContextClass *klass)
82 {
83     GObjectClass *object_class = (GObjectClass *) klass;
84     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
86     lpetool_parent_class = (SPPenContextClass*)g_type_class_peek_parent(klass);
88     object_class->dispose = sp_lpetool_context_dispose;
90     event_context_class->setup = sp_lpetool_context_setup;
91     event_context_class->set = sp_lpetool_context_set;
92     event_context_class->root_handler = sp_lpetool_context_root_handler;
93 }
95 static void
96 sp_lpetool_context_init(SPLPEToolContext *lc)
97 {
98     lc->cursor_shape = cursor_crosshairs_xpm;
99     lc->hot_x = 7;
100     lc->hot_y = 7;
102     lc->canvas_bbox = NULL;
104     new (&lc->sel_changed_connection) sigc::connection();
107 static void
108 sp_lpetool_context_dispose(GObject *object)
110     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(object);
111     delete lc->shape_editor;
113     if (lc->canvas_bbox) {
114         gtk_object_destroy(GTK_OBJECT(lc->canvas_bbox));
115         lc->canvas_bbox = NULL;
116     }
118     lc->sel_changed_connection.disconnect();
119     lc->sel_changed_connection.~connection();
121     G_OBJECT_CLASS(lpetool_parent_class)->dispose(object);
124 static void
125 sp_lpetool_context_setup(SPEventContext *ec)
127     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(ec);
129     if (((SPEventContextClass *) lpetool_parent_class)->setup)
130         ((SPEventContextClass *) lpetool_parent_class)->setup(ec);
132     Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
133     SPItem *item = selection->singleItem();
135     lc->sel_changed_connection.disconnect();
136     lc->sel_changed_connection =
137         selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_lpetool_context_selection_changed), (gpointer)lc));
139     //lc->my_nc = new NodeContextCpp(lc->desktop, lc->prefs_repr, lc->key);
140     lc->shape_editor = new ShapeEditor(ec->desktop);
142     lpetool_context_reset_limiting_bbox(lc);
144 // TODO temp force:
145     ec->enableSelectionCue();
147     if (item) {
148         lc->shape_editor->set_item(item, SH_NODEPATH);
149         lc->shape_editor->set_item(item, SH_KNOTHOLDER);
150     }
152     if (prefs_get_int_attribute("tools.lpetool", "selcue", 0) != 0) {
153         ec->enableSelectionCue();
154     }
156     lc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
158     lc->shape_editor->update_statusbar();
161 /**
162 \brief  Callback that processes the "changed" signal on the selection;
163 destroys old and creates new nodepath and reassigns listeners to the new selected item's repr
164 */
165 void
166 sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data)
168     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(data);
170     // TODO: update ShapeEditorsCollective instead
171     lc->shape_editor->unset_item(SH_NODEPATH);
172     lc->shape_editor->unset_item(SH_KNOTHOLDER);
173     SPItem *item = selection->singleItem();
174     lc->shape_editor->set_item(item, SH_NODEPATH);
175     lc->shape_editor->set_item(item, SH_KNOTHOLDER);
176     lc->shape_editor->update_statusbar();
179 static void
180 sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
182     // FIXME: how to set this correcly? the value from preferences-skeleton.h doesn't seem to get
183     // read (it wants to set drag = 1)
184     lpetool_parent_class->set(ec, key, "drag");
186     /**
187     //pass on up to parent class to handle common attributes.
188     if ( lpetool_parent_class->set ) {
189         lpetool_parent_class->set(ec, key, val);
190     }
191     **/
194 gint
195 sp_lpetool_context_root_handler(SPEventContext *event_context, GdkEvent *event)
197     //g_print ("sp_lpetool_context_root_handler()\n");
198     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(event_context);
199     SPDesktop *desktop = event_context->desktop;
200     Inkscape::Selection *selection = sp_desktop_selection (desktop);
202     bool ret = false;
204     if (sp_pen_context_has_waiting_LPE(lc)) {
205         // quit when we are waiting for a LPE to be applied
206         g_print ("LPETool has waiting LPE. We call the pen tool parent context and return\n");
207         ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
208         return ret;
209     }
211     switch (event->type) {
212         case GDK_BUTTON_PRESS:
213             g_print ("GDK_BUTTON_PRESS\n");
214             if (event->button.button == 1 && !event_context->space_panning) {
215                 g_print ("   ... (passed if construct)\n");
216                 // save drag origin
217                 event_context->xp = (gint) event->button.x;
218                 event_context->yp = (gint) event->button.y;
219                 event_context->within_tolerance = true;
220                 lc->shape_editor->cancel_hit();
222                 using namespace Inkscape::LivePathEffect;
224                 int mode = prefs_get_int_attribute("tools.lpetool", "mode", 0);
225                 EffectType type = lpesubtools[mode];
226                 g_print ("Activating mode %d\n", mode);
228                 // save drag origin
229                 bool over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->button.x, event->button.y), true);
230                 g_print ("over_stroke: %s\n", over_stroke ? "true" : "false");
232                 sp_pen_context_wait_for_LPE_mouse_clicks(lc, type, Effect::acceptsNumClicks(type));
234                 // we pass the mouse click on to pen tool as the first click which it should collect
235                 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
236             }
237             break;
238         case GDK_MOTION_NOTIFY:
239         {
240             if (!lc->shape_editor->has_nodepath() || selection->singleItem() == NULL) {
241                 break;
242             }
244             bool over_stroke = false;
245             over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->motion.x, event->motion.y), false);
247             if (over_stroke) {
248                 event_context->cursor_shape = cursor_node_xpm;
249                 event_context->hot_x = 1;
250                 event_context->hot_y = 1;
251                 sp_event_context_update_cursor(event_context);
252             } else {
253                 lc->cursor_shape = cursor_crosshairs_xpm;
254                 lc->hot_x = 7;
255                 lc->hot_y = 7;
256                 sp_event_context_update_cursor(event_context);
257             }
258         }
259         break;
262     case GDK_BUTTON_RELEASE:
263     {
264         /**
265         break;
266         **/
267     }
269     case GDK_KEY_PRESS:
270         /**
271         switch (get_group0_keyval (&event->key)) {
272         }
273         break;
274         **/
276     case GDK_KEY_RELEASE:
277         /**
278         switch (get_group0_keyval(&event->key)) {
279             case GDK_Control_L:
280             case GDK_Control_R:
281                 dc->_message_context->clear();
282                 break;
283             default:
284                 break;
285         }
286         **/
288     default:
289         break;
290     }
292     if (!ret) {
293         if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
294             ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
295         }
296     }
298     return ret;
301 /*
302  * Reads the limiting bounding box from preferences and draws it on the screen
303  */
304 // TODO: Note that currently the bbox is not user-settable; we simply use the page borders
305 void
306 lpetool_context_reset_limiting_bbox(SPLPEToolContext *lc)
308     SPDocument *document = sp_desktop_document(lc->desktop);
309     Geom::Coord w = sp_document_width(document);
310     Geom::Coord h = sp_document_height(document);
312     Geom::Point A(0,0);
313     Geom::Point B(w,h);
315     Geom::Rect rect(A, B);
316     SPCurve *curve = SPCurve::new_from_rect(rect);
318     lc->canvas_bbox = sp_canvas_bpath_new (sp_desktop_controls(lc->desktop), curve);
319     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(lc->canvas_bbox), 0x0000ffff, 0.8, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT, 5, 5);
322 /*
323   Local Variables:
324   mode:c++
325   c-file-style:"stroustrup"
326   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
327   indent-tabs-mode:nil
328   fill-column:99
329   End:
330 */
331 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :