Code

a93176067695e98c7208bb254e59463250685e0d
[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 #include "config.h"
18 #include "forward.h"
19 #include "pixmaps/cursor-pencil.xpm"
20 #include <gtk/gtk.h>
21 #include "desktop.h"
22 #include "message-context.h"
23 #include "prefs-utils.h"
25 /**
27 #include <gdk/gdkkeysyms.h>
28 #include <glibmm/i18n.h>
29 #include <string>
30 #include <cstring>
31 #include <numeric>
33 #include "svg/svg.h"
34 #include "display/canvas-bpath.h"
35 #include "display/bezier-utils.h"
37 #include <glib/gmem.h>
38 #include "macros.h"
39 #include "document.h"
40 #include "selection.h"
41 #include "desktop-events.h"
42 #include "desktop-handles.h"
43 #include "desktop-affine.h"
44 #include "desktop-style.h"
45 #include "xml/repr.h"
46 #include "context-fns.h"
47 #include "sp-item.h"
48 #include "inkscape.h"
49 #include "color.h"
50 #include "rubberband.h"
51 #include "splivarot.h"
52 #include "sp-item-group.h"
53 #include "sp-shape.h"
54 #include "sp-path.h"
55 #include "sp-text.h"
56 #include "display/canvas-bpath.h"
57 #include "display/canvas-arena.h"
58 #include "livarot/Shape.h"
59 #include <2geom/isnan.h>
60 #include <2geom/pathvector.h>
61 **/
63 #include "lpe-tool-context.h"
65 static void sp_lpetool_context_class_init(SPLPEToolContextClass *klass);
66 static void sp_lpetool_context_init(SPLPEToolContext *erc);
67 static void sp_lpetool_context_dispose(GObject *object);
69 static void sp_lpetool_context_setup(SPEventContext *ec);
70 static void sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
71 static gint sp_lpetool_context_root_handler(SPEventContext *ec, GdkEvent *event);
74 static SPEventContextClass *lpetool_parent_class = 0;
76 GType sp_lpetool_context_get_type(void)
77 {
78     static GType type = 0;
79     if (!type) {
80         GTypeInfo info = {
81             sizeof(SPLPEToolContextClass),
82             0, // base_init
83             0, // base_finalize
84             (GClassInitFunc)sp_lpetool_context_class_init,
85             0, // class_finalize
86             0, // class_data
87             sizeof(SPLPEToolContext),
88             0, // n_preallocs
89             (GInstanceInitFunc)sp_lpetool_context_init,
90             0 // value_table
91         };
92         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPLPEToolContext", &info, static_cast<GTypeFlags>(0));
93     }
94     return type;
95 }
97 static void
98 sp_lpetool_context_class_init(SPLPEToolContextClass *klass)
99 {
100     GObjectClass *object_class = (GObjectClass *) klass;
101     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
103     lpetool_parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
105     object_class->dispose = sp_lpetool_context_dispose;
107     event_context_class->setup = sp_lpetool_context_setup;
108     event_context_class->set = sp_lpetool_context_set;
109     event_context_class->root_handler = sp_lpetool_context_root_handler;
112 static void
113 sp_lpetool_context_init(SPLPEToolContext *erc)
115     erc->cursor_shape = cursor_pencil_xpm;
116     erc->hot_x = 4;
117     erc->hot_y = 4;
120 static void
121 sp_lpetool_context_dispose(GObject *object)
123     //SPLPEToolContext *erc = SP_LPETOOL_CONTEXT(object);
125     G_OBJECT_CLASS(lpetool_parent_class)->dispose(object);
128 static void
129 sp_lpetool_context_setup(SPEventContext *ec)
131     SPLPEToolContext *erc = SP_LPETOOL_CONTEXT(ec);
133     if (((SPEventContextClass *) lpetool_parent_class)->setup)
134         ((SPEventContextClass *) lpetool_parent_class)->setup(ec);
136     erc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
138     if (prefs_get_int_attribute("tools.lpetool", "selcue", 0) != 0) {
139         ec->enableSelectionCue();
140     }
141 // TODO temp force:
142     ec->enableSelectionCue();
145 static void
146 sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
148     //pass on up to parent class to handle common attributes.
149     if ( lpetool_parent_class->set ) {
150         lpetool_parent_class->set(ec, key, val);
151     }
154 /**
155 void
156 sp_erc_update_toolbox (SPDesktop *desktop, const gchar *id, double value)
158     desktop->setToolboxAdjustmentValue (id, value);
160 **/
162 gint
163 sp_lpetool_context_root_handler(SPEventContext *event_context,
164                                   GdkEvent *event)
166     //SPLPEToolContext *dc = SP_LPETOOL_CONTEXT(event_context);
167     //SPDesktop *desktop = event_context->desktop;
169     gint ret = FALSE;
171     /**
172     switch (event->type) {
173         case GDK_BUTTON_PRESS:
174             if (event->button.button == 1 && !event_context->space_panning) {
176                 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
178                 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
179                     return TRUE;
180                 }
182                 NR::Point const button_w(event->button.x,
183                                          event->button.y);
184                 NR::Point const button_dt(desktop->w2d(button_w));
185                 sp_lpetool_reset(dc, button_dt);
186                 sp_lpetool_extinput(dc, event);
187                 sp_lpetool_apply(dc, button_dt);
188                 dc->accumulated->reset();
189                 if (dc->repr) {
190                     dc->repr = NULL;
191                 }
193                 Inkscape::Rubberband::get()->start(desktop, button_dt);
194                 Inkscape::Rubberband::get()->setMode(RUBBERBAND_MODE_TOUCHPATH);
196                 // initialize first point
197                 dc->npoints = 0;
199                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
200                                     ( GDK_KEY_PRESS_MASK |
201                                       GDK_BUTTON_RELEASE_MASK |
202                                       GDK_POINTER_MOTION_MASK |
203                                       GDK_BUTTON_PRESS_MASK ),
204                                     NULL,
205                                     event->button.time);
207                 ret = TRUE;
209                 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
210                 dc->is_drawing = true;
211             }
212             break;
213         case GDK_MOTION_NOTIFY:
214         {
215             NR::Point const motion_w(event->motion.x,
216                                      event->motion.y);
217             NR::Point motion_dt(desktop->w2d(motion_w));
218             sp_lpetool_extinput(dc, event);
220             dc->_message_context->clear();
222             if ( dc->is_drawing && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
223                 dc->dragging = TRUE;
225                 dc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drawing</b> an lpetool stroke"));
227                 if (!sp_lpetool_apply(dc, motion_dt)) {
228                     ret = TRUE;
229                     break;
230                 }
232                 if ( dc->cur != dc->last ) {
233                     sp_lpetool_brush(dc);
234                     g_assert( dc->npoints > 0 );
235                     fit_and_split(dc, FALSE);
236                 }
237                 ret = TRUE;
238             }
239             Inkscape::Rubberband::get()->move(motion_dt);
240         }
241         break;
244     case GDK_BUTTON_RELEASE:
245     {
246         NR::Point const motion_w(event->button.x, event->button.y);
247         NR::Point const motion_dt(desktop->w2d(motion_w));
249         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
250         sp_canvas_end_forced_full_redraws(desktop->canvas);
251         dc->is_drawing = false;
253         if (dc->dragging && event->button.button == 1 && !event_context->space_panning) {
254             dc->dragging = FALSE;
256             NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
258             sp_lpetool_apply(dc, motion_dt);
259             
260             // Remove all temporary line segments
261             while (dc->segments) {
262                 gtk_object_destroy(GTK_OBJECT(dc->segments->data));
263                 dc->segments = g_slist_remove(dc->segments, dc->segments->data);
264             }
266             // Create object
267             fit_and_split(dc, TRUE);
268             accumulate_lpetool(dc);
269             set_to_accumulated(dc); // performs document_done
271             // reset accumulated curve
272             dc->accumulated->reset();
274             clear_current(dc);
275             if (dc->repr) {
276                 dc->repr = NULL;
277             }
279             Inkscape::Rubberband::get()->stop();
280             dc->_message_context->clear();
281             ret = TRUE;
282         }
283         break;
284     }
286     case GDK_KEY_PRESS:
287         switch (get_group0_keyval (&event->key)) {
288         case GDK_Up:
289         case GDK_KP_Up:
290             if (!MOD__CTRL_ONLY) {
291                 dc->angle += 5.0;
292                 if (dc->angle > 90.0)
293                     dc->angle = 90.0;
294                 sp_erc_update_toolbox (desktop, "lpetool-angle", dc->angle);
295                 ret = TRUE;
296             }
297             break;
298         case GDK_Down:
299         case GDK_KP_Down:
300             if (!MOD__CTRL_ONLY) {
301                 dc->angle -= 5.0;
302                 if (dc->angle < -90.0)
303                     dc->angle = -90.0;
304                 sp_erc_update_toolbox (desktop, "lpetool-angle", dc->angle);
305                 ret = TRUE;
306             }
307             break;
308         case GDK_Right:
309         case GDK_KP_Right:
310             if (!MOD__CTRL_ONLY) {
311                 dc->width += 0.01;
312                 if (dc->width > 1.0)
313                     dc->width = 1.0;
314                 sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100); // the same spinbutton is for alt+x
315                 ret = TRUE;
316             }
317             break;
318         case GDK_Left:
319         case GDK_KP_Left:
320             if (!MOD__CTRL_ONLY) {
321                 dc->width -= 0.01;
322                 if (dc->width < 0.01)
323                     dc->width = 0.01;
324                 sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
325                 ret = TRUE;
326             }
327             break;
328         case GDK_Home:
329         case GDK_KP_Home:
330             dc->width = 0.01;
331             sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
332             ret = TRUE;
333             break;
334         case GDK_End:
335         case GDK_KP_End:
336             dc->width = 1.0;
337             sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
338             ret = TRUE;
339             break;
340         case GDK_x:
341         case GDK_X:
342             if (MOD__ALT_ONLY) {
343                 desktop->setToolboxFocusTo ("altx-lpetool");
344                 ret = TRUE;
345             }
346             break;
347         case GDK_Escape:
348             Inkscape::Rubberband::get()->stop();
349             if (dc->is_drawing) {
350                 // if drawing, cancel, otherwise pass it up for deselecting
351                 lpetool_cancel (dc);
352                 ret = TRUE;
353             }
354             break;
355         case GDK_z:
356         case GDK_Z:
357             if (MOD__CTRL_ONLY && dc->is_drawing) {
358                 // if drawing, cancel, otherwise pass it up for undo
359                 lpetool_cancel (dc);
360                 ret = TRUE;
361             }
362             break;
363         default:
364             break;
365         }
366         break;
368     case GDK_KEY_RELEASE:
369         switch (get_group0_keyval(&event->key)) {
370             case GDK_Control_L:
371             case GDK_Control_R:
372                 dc->_message_context->clear();
373                 break;
374             default:
375                 break;
376         }
378     default:
379         break;
380     }
381     **/
383     if (!ret) {
384         if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
385             ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
386         }
387     }
389     return ret;
392 /*
393   Local Variables:
394   mode:c++
395   c-file-style:"stroustrup"
396   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
397   indent-tabs-mode:nil
398   fill-column:99
399   End:
400 */
401 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :