Code

fix crash when pressing space while drawing a shape
[inkscape.git] / src / rect-context.cpp
1 #define __SP_RECT_CONTEXT_C__
3 /*
4  * Rectangle drawing context
5  *
6  * Author:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *
10  * Copyright (C) 2006      Johan Engelen <johan@shouraizou.nl>
11  * Copyright (C) 2000-2005 authors
12  * Copyright (C) 2000-2001 Ximian, Inc.
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  */
17 #include "config.h"
19 #include <gdk/gdkkeysyms.h>
21 #include "macros.h"
22 #include "display/sp-canvas.h"
23 #include "sp-rect.h"
24 #include "document.h"
25 #include "sp-namedview.h"
26 #include "selection.h"
27 #include "desktop-handles.h"
28 #include "snap.h"
29 #include "desktop.h"
30 #include "desktop-style.h"
31 #include "message-context.h"
32 #include "pixmaps/cursor-rect.xpm"
33 #include "rect-context.h"
34 #include "sp-metrics.h"
35 #include <glibmm/i18n.h>
36 #include "object-edit.h"
37 #include "xml/repr.h"
38 #include "xml/node-event-vector.h"
39 #include "prefs-utils.h"
40 #include "context-fns.h"
42 static void sp_rect_context_class_init(SPRectContextClass *klass);
43 static void sp_rect_context_init(SPRectContext *rect_context);
44 static void sp_rect_context_dispose(GObject *object);
46 static void sp_rect_context_setup(SPEventContext *ec);
47 static void sp_rect_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
49 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event);
50 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
52 static void sp_rect_drag(SPRectContext &rc, NR::Point const pt, guint state);
53 static void sp_rect_finish(SPRectContext *rc);
55 static SPEventContextClass *parent_class;
58 GtkType sp_rect_context_get_type()
59 {
60     static GType type = 0;
61     if (!type) {
62         GTypeInfo info = {
63             sizeof(SPRectContextClass),
64             NULL, NULL,
65             (GClassInitFunc) sp_rect_context_class_init,
66             NULL, NULL,
67             sizeof(SPRectContext),
68             4,
69             (GInstanceInitFunc) sp_rect_context_init,
70             NULL,    /* value_table */
71         };
72         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPRectContext", &info, (GTypeFlags) 0);
73     }
74     return type;
75 }
77 static void sp_rect_context_class_init(SPRectContextClass *klass)
78 {
79     GObjectClass *object_class = (GObjectClass *) klass;
80     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
82     parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
84     object_class->dispose = sp_rect_context_dispose;
86     event_context_class->setup = sp_rect_context_setup;
87     event_context_class->set = sp_rect_context_set;
88     event_context_class->root_handler  = sp_rect_context_root_handler;
89     event_context_class->item_handler  = sp_rect_context_item_handler;
90 }
92 static void sp_rect_context_init(SPRectContext *rect_context)
93 {
94     SPEventContext *event_context = SP_EVENT_CONTEXT(rect_context);
96     event_context->cursor_shape = cursor_rect_xpm;
97     event_context->hot_x = 4;
98     event_context->hot_y = 4;
99     event_context->xp = 0;
100     event_context->yp = 0;
101     event_context->tolerance = 0;
102     event_context->within_tolerance = false;
103     event_context->item_to_select = NULL;
105     event_context->shape_repr = NULL;
106     event_context->shape_knot_holder = NULL;
108     rect_context->item = NULL;
110     rect_context->rx = 0.0;
111     rect_context->ry = 0.0;
113     new (&rect_context->sel_changed_connection) sigc::connection();
116 static void sp_rect_context_dispose(GObject *object)
118     SPRectContext *rc = SP_RECT_CONTEXT(object);
119     SPEventContext *ec = SP_EVENT_CONTEXT(object);
121     ec->enableGrDrag(false);
123     rc->sel_changed_connection.disconnect();
124     rc->sel_changed_connection.~connection();
126     /* fixme: This is necessary because we do not grab */
127     if (rc->item) {
128         sp_rect_finish(rc);
129     }
131     if (ec->shape_knot_holder) {
132         sp_knot_holder_destroy(ec->shape_knot_holder);
133         ec->shape_knot_holder = NULL;
134     }
136     if (ec->shape_repr) { // remove old listener
137         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
138         Inkscape::GC::release(ec->shape_repr);
139         ec->shape_repr = 0;
140     }
142     if (rc->_message_context) {
143         delete rc->_message_context;
144     }
146     G_OBJECT_CLASS(parent_class)->dispose(object);
149 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
150     NULL, /* child_added */
151     NULL, /* child_removed */
152     ec_shape_event_attr_changed,
153     NULL, /* content_changed */
154     NULL  /* order_changed */
155 };
157 /**
158 \brief  Callback that processes the "changed" signal on the selection;
159 destroys old and creates new knotholder
160 */
161 void sp_rect_context_selection_changed(Inkscape::Selection *selection, gpointer data)
163     SPRectContext *rc = SP_RECT_CONTEXT(data);
164     SPEventContext *ec = SP_EVENT_CONTEXT(rc);
166     if (ec->shape_knot_holder) { // destroy knotholder
167         sp_knot_holder_destroy(ec->shape_knot_holder);
168         ec->shape_knot_holder = NULL;
169     }
171     if (ec->shape_repr) { // remove old listener
172         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
173         Inkscape::GC::release(ec->shape_repr);
174         ec->shape_repr = 0;
175     }
177     SPItem *item = selection->singleItem();
178     if (item) {
179         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
180         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
181         if (shape_repr) {
182             ec->shape_repr = shape_repr;
183             Inkscape::GC::anchor(shape_repr);
184             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
185         }
186     }
189 static void sp_rect_context_setup(SPEventContext *ec)
191     SPRectContext *rc = SP_RECT_CONTEXT(ec);
193     if (((SPEventContextClass *) parent_class)->setup) {
194         ((SPEventContextClass *) parent_class)->setup(ec);
195     }
197     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
198     if (item) {
199         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
200         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
201         if (shape_repr) {
202             ec->shape_repr = shape_repr;
203             Inkscape::GC::anchor(shape_repr);
204             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
205         }
206     }
208     rc->sel_changed_connection.disconnect();
209     rc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
210         sigc::bind(sigc::ptr_fun(&sp_rect_context_selection_changed), (gpointer)rc)
211     );
213     sp_event_context_read(ec, "rx");
214     sp_event_context_read(ec, "ry");
216     if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
217         ec->enableSelectionCue();
218     }
220     if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
221         ec->enableGrDrag();
222     }
224     rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
227 static void sp_rect_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
229     SPRectContext *rc = SP_RECT_CONTEXT(ec);
231     /* fixme: Proper error handling for non-numeric data.  Use a locale-independent function like
232      * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
233     if ( strcmp(key, "rx") == 0 ) {
234         rc->rx = ( val
235                          ? g_ascii_strtod (val, NULL)
236                          : 0.0 );
237     } else if ( strcmp(key, "ry") == 0 ) {
238         rc->ry = ( val
239                          ? g_ascii_strtod (val, NULL)
240                          : 0.0 );
241     }
244 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
246     SPDesktop *desktop = event_context->desktop;
248     gint ret = FALSE;
250     switch (event->type) {
251     case GDK_BUTTON_PRESS:
252         if ( event->button.button == 1 ) {
253             Inkscape::setup_for_drag_start(desktop, event_context, event);
254             ret = TRUE;
255         }
256         break;
257         // motion and release are always on root (why?)
258     default:
259         break;
260     }
262     if (((SPEventContextClass *) parent_class)->item_handler) {
263         ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
264     }
266     return ret;
269 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event)
271     static bool dragging;
273     SPDesktop *desktop = event_context->desktop;
274     Inkscape::Selection *selection = sp_desktop_selection (desktop);
276     SPRectContext *rc = SP_RECT_CONTEXT(event_context);
278     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
280     gint ret = FALSE;
281     switch (event->type) {
282     case GDK_BUTTON_PRESS:
283         if ( event->button.button == 1 ) {
284             NR::Point const button_w(event->button.x,
285                                      event->button.y);
287             // save drag origin
288             event_context->xp = (gint) button_w[NR::X];
289             event_context->yp = (gint) button_w[NR::Y];
290             event_context->within_tolerance = true;
292             // remember clicked item, disregarding groups, honoring Alt
293             event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
295             dragging = true;
297             /* Position center */
298             NR::Point const button_dt(desktop->w2d(button_w));
300             /* Snap center */
301             SnapManager const &m = desktop->namedview->snap_manager;
302             rc->center = m.freeSnap(Inkscape::Snapper::SNAP_POINT | Inkscape::Snapper::BBOX_POINT,
303                                     button_dt, rc->item).getPoint();
305             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
306                                 ( GDK_KEY_PRESS_MASK |
307                                   GDK_BUTTON_RELEASE_MASK       |
308                                   GDK_POINTER_MOTION_MASK       |
309                                   GDK_BUTTON_PRESS_MASK ),
310                                 NULL, event->button.time);
311             ret = TRUE;
312         }
313         break;
314     case GDK_MOTION_NOTIFY:
315         if ( dragging
316              && ( event->motion.state & GDK_BUTTON1_MASK ) )
317         {
318             if ( event_context->within_tolerance
319                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
320                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
321                 break; // do not drag if we're within tolerance from origin
322             }
323             // Once the user has moved farther than tolerance from the original location
324             // (indicating they intend to draw, not click), then always process the
325             // motion notify coordinates as given (no snapping back to origin)
326             event_context->within_tolerance = false;
328             NR::Point const motion_w(event->motion.x,
329                                      event->motion.y);
330             NR::Point motion_dt(desktop->w2d(motion_w));
331             
332             SnapManager const &m = desktop->namedview->snap_manager;
333             motion_dt = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, motion_dt, rc->item).getPoint();
334             
335             sp_rect_drag(*rc, motion_dt, event->motion.state);
336             
337             ret = TRUE;
338         }
339         break;
340     case GDK_BUTTON_RELEASE:
341         event_context->xp = event_context->yp = 0;
342         if ( event->button.button == 1 ) {
343             dragging = false;
345             if (!event_context->within_tolerance) {
346                 // we've been dragging, finish the rect
347                 sp_rect_finish(rc);
348             } else if (event_context->item_to_select) {
349                 // no dragging, select clicked item if any
350                 if (event->button.state & GDK_SHIFT_MASK) {
351                     selection->toggle(event_context->item_to_select);
352                 } else {
353                     selection->set(event_context->item_to_select);
354                 }
355             } else {
356                 // click in an empty space
357                 selection->clear();
358             }
360             event_context->item_to_select = NULL;
361             ret = TRUE;
362             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
363                                   event->button.time);
364         }
365         break;
366     case GDK_KEY_PRESS:
367         switch (get_group0_keyval (&event->key)) {
368         case GDK_Alt_L:
369         case GDK_Alt_R:
370         case GDK_Control_L:
371         case GDK_Control_R:
372         case GDK_Shift_L:
373         case GDK_Shift_R:
374         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
375         case GDK_Meta_R:
376             if (!dragging){
377                 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
378                                             _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
379                                             _("<b>Shift</b>: draw around the starting point"),
380                                             NULL);
381             }
382             break;
383         case GDK_Up:
384         case GDK_Down:
385         case GDK_KP_Up:
386         case GDK_KP_Down:
387             // prevent the zoom field from activation
388             if (!MOD__CTRL_ONLY)
389                 ret = TRUE;
390             break;
392         case GDK_x:
393         case GDK_X:
394             if (MOD__ALT_ONLY) {
395                 desktop->setToolboxFocusTo ("altx-rect");
396                 ret = TRUE;
397             }
398             break;
400         case GDK_Escape:
401             sp_desktop_selection(desktop)->clear();
402             //TODO: make dragging escapable by Esc
403             break;
405         case GDK_space:
406             if (dragging) {
407                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
408                                       event->button.time);
409                 dragging = false;
410                 if (!event_context->within_tolerance) {
411                     // we've been dragging, finish the rect
412                     sp_rect_finish(rc);
413                 }
414                 // do not return true, so that space would work switching to selector
415             }
416             break;
418         default:
419             break;
420         }
421         break;
422     case GDK_KEY_RELEASE:
423         switch (get_group0_keyval (&event->key)) {
424         case GDK_Alt_L:
425         case GDK_Alt_R:
426         case GDK_Control_L:
427         case GDK_Control_R:
428         case GDK_Shift_L:
429         case GDK_Shift_R:
430         case GDK_Meta_L:  // Meta is when you press Shift+Alt
431         case GDK_Meta_R:
432             event_context->defaultMessageContext()->clear();
433             break;
434         default:
435             break;
436         }
437         break;
438     default:
439         break;
440     }
442     if (!ret) {
443         if (((SPEventContextClass *) parent_class)->root_handler) {
444             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
445         }
446     }
448     return ret;
451 static void sp_rect_drag(SPRectContext &rc, NR::Point const pt, guint state)
453     SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
455     if (!rc.item) {
457         if (Inkscape::have_viable_layer(desktop, rc._message_context) == false) {
458             return;
459         }
461         /* Create object */
462         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(&rc));
463         Inkscape::XML::Node *repr = xml_doc->createElement("svg:rect");
465         /* Set style */
466         sp_desktop_apply_style_tool (desktop, repr, "tools.shapes.rect", false);
468         rc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
469         Inkscape::GC::release(repr);
470         rc.item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
471         rc.item->updateRepr();
473         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
474     }
476     NR::Rect const r = Inkscape::snap_rectangular_box(desktop, rc.item, pt, rc.center, state);
478     sp_rect_position_set(SP_RECT(rc.item), r.min()[NR::X], r.min()[NR::Y], r.dimensions()[NR::X], r.dimensions()[NR::Y]);
479     if ( rc.rx != 0.0 ) {
480         sp_rect_set_rx (SP_RECT(rc.item), TRUE, rc.rx);
481     }
482     if ( rc.ry != 0.0 ) {
483         if (rc.rx == 0.0)
484             sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, MIN(r.dimensions()[NR::X], r.dimensions()[NR::Y])/2));
485         else
486             sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, r.dimensions()[NR::Y]));
487     }
489     // status text
490     GString *xs = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::X], desktop->namedview->getDefaultMetric());
491     GString *ys = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::Y], desktop->namedview->getDefaultMetric());
492     rc._message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>Rectangle</b>: %s &#215; %s; with <b>Ctrl</b> to make square or integer-ratio rectangle; with <b>Shift</b> to draw around the starting point"), xs->str, ys->str);
493     g_string_free(xs, FALSE);
494     g_string_free(ys, FALSE);
497 static void sp_rect_finish(SPRectContext *rc)
499     rc->_message_context->clear();
501     if ( rc->item != NULL ) {
502         SPDesktop * desktop;
504         desktop = SP_EVENT_CONTEXT_DESKTOP(rc);
506         SP_OBJECT(rc->item)->updateRepr();
508         sp_canvas_end_forced_full_redraws(desktop->canvas);
510         sp_desktop_selection(desktop)->set(rc->item);
511         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_RECT,
512                          _("Create rectangle"));
514         rc->item = NULL;
515     }
518 /*
519   Local Variables:
520   mode:c++
521   c-file-style:"stroustrup"
522   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
523   indent-tabs-mode:nil
524   fill-column:99
525   End:
526 */
527 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :