Code

Clear pointers in the snapmanager if they're no longer needed.
[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>
20 #include <cstring>
21 #include <string>
23 #include "macros.h"
24 #include "display/sp-canvas.h"
25 #include "sp-rect.h"
26 #include "document.h"
27 #include "sp-namedview.h"
28 #include "selection.h"
29 #include "selection-chemistry.h"
30 #include "desktop-handles.h"
31 #include "snap.h"
32 #include "desktop.h"
33 #include "desktop-style.h"
34 #include "message-context.h"
35 #include "pixmaps/cursor-rect.xpm"
36 #include "rect-context.h"
37 #include "sp-metrics.h"
38 #include <glibmm/i18n.h>
39 #include "object-edit.h"
40 #include "xml/repr.h"
41 #include "xml/node-event-vector.h"
42 #include "preferences.h"
43 #include "context-fns.h"
44 #include "shape-editor.h"
46 //static const double goldenratio = 1.61803398874989484820; // golden ratio
48 static void sp_rect_context_class_init(SPRectContextClass *klass);
49 static void sp_rect_context_init(SPRectContext *rect_context);
50 static void sp_rect_context_dispose(GObject *object);
52 static void sp_rect_context_setup(SPEventContext *ec);
53 static void sp_rect_context_finish(SPEventContext *ec);
54 static void sp_rect_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
56 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event);
57 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
59 static void sp_rect_drag(SPRectContext &rc, Geom::Point const pt, guint state);
60 static void sp_rect_finish(SPRectContext *rc);
61 static void sp_rect_cancel(SPRectContext *rc);
63 static SPEventContextClass *parent_class;
66 GtkType sp_rect_context_get_type()
67 {
68     static GType type = 0;
69     if (!type) {
70         GTypeInfo info = {
71             sizeof(SPRectContextClass),
72             NULL, NULL,
73             (GClassInitFunc) sp_rect_context_class_init,
74             NULL, NULL,
75             sizeof(SPRectContext),
76             4,
77             (GInstanceInitFunc) sp_rect_context_init,
78             NULL,    /* value_table */
79         };
80         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPRectContext", &info, (GTypeFlags) 0);
81     }
82     return type;
83 }
85 static void sp_rect_context_class_init(SPRectContextClass *klass)
86 {
87     GObjectClass *object_class = (GObjectClass *) klass;
88     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
90     parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
92     object_class->dispose = sp_rect_context_dispose;
94     event_context_class->setup = sp_rect_context_setup;
95     event_context_class->finish = sp_rect_context_finish;
96     event_context_class->set = sp_rect_context_set;
97     event_context_class->root_handler  = sp_rect_context_root_handler;
98     event_context_class->item_handler  = sp_rect_context_item_handler;
99 }
101 static void sp_rect_context_init(SPRectContext *rect_context)
103     SPEventContext *event_context = SP_EVENT_CONTEXT(rect_context);
105     event_context->cursor_shape = cursor_rect_xpm;
106     event_context->hot_x = 4;
107     event_context->hot_y = 4;
108     event_context->xp = 0;
109     event_context->yp = 0;
110     event_context->tolerance = 0;
111     event_context->within_tolerance = false;
112     event_context->item_to_select = NULL;
114     rect_context->item = NULL;
116     rect_context->rx = 0.0;
117     rect_context->ry = 0.0;
119     new (&rect_context->sel_changed_connection) sigc::connection();
122 static void sp_rect_context_finish(SPEventContext *ec)
124     SPRectContext *rc = SP_RECT_CONTEXT(ec);
125     SPDesktop *desktop = ec->desktop;
127     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), GDK_CURRENT_TIME);
128     sp_rect_finish(rc);
129     rc->sel_changed_connection.disconnect();
131     if (((SPEventContextClass *) parent_class)->finish) {
132         ((SPEventContextClass *) parent_class)->finish(ec);
133     }
137 static void sp_rect_context_dispose(GObject *object)
139     SPRectContext *rc = SP_RECT_CONTEXT(object);
140     SPEventContext *ec = SP_EVENT_CONTEXT(object);
142     ec->enableGrDrag(false);
144     rc->sel_changed_connection.disconnect();
145     rc->sel_changed_connection.~connection();
147     delete ec->shape_editor;
148     ec->shape_editor = NULL;
150     /* fixme: This is necessary because we do not grab */
151     if (rc->item) {
152         sp_rect_finish(rc);
153     }
155     if (rc->_message_context) {
156         delete rc->_message_context;
157     }
159     G_OBJECT_CLASS(parent_class)->dispose(object);
162 /**
163 \brief  Callback that processes the "changed" signal on the selection;
164 destroys old and creates new knotholder
165 */
166 void sp_rect_context_selection_changed(Inkscape::Selection *selection, gpointer data)
168     SPRectContext *rc = SP_RECT_CONTEXT(data);
169     SPEventContext *ec = SP_EVENT_CONTEXT(rc);
171     ec->shape_editor->unset_item(SH_KNOTHOLDER);
172     SPItem *item = selection->singleItem();
173     ec->shape_editor->set_item(item, SH_KNOTHOLDER);
176 static void sp_rect_context_setup(SPEventContext *ec)
178     SPRectContext *rc = SP_RECT_CONTEXT(ec);
180     if (((SPEventContextClass *) parent_class)->setup) {
181         ((SPEventContextClass *) parent_class)->setup(ec);
182     }
184     ec->shape_editor = new ShapeEditor(ec->desktop);
186     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
187     if (item) {
188         ec->shape_editor->set_item(item, SH_KNOTHOLDER);
189     }
191     rc->sel_changed_connection.disconnect();
192     rc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
193         sigc::bind(sigc::ptr_fun(&sp_rect_context_selection_changed), (gpointer)rc)
194     );
196     sp_event_context_read(ec, "rx");
197     sp_event_context_read(ec, "ry");
199     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
200     if (prefs->getBool("/tools/shapes/selcue")) {
201         ec->enableSelectionCue();
202     }
204     if (prefs->getBool("/tools/shapes/gradientdrag")) {
205         ec->enableGrDrag();
206     }
208     rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
211 static void sp_rect_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
213     SPRectContext *rc = SP_RECT_CONTEXT(ec);
215     /* fixme: Proper error handling for non-numeric data.  Use a locale-independent function like
216      * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
217     Glib::ustring name = val->getEntryName();
218     if ( name == "rx" ) {
219         rc->rx = val->getDoubleLimited(); // prevents NaN and +/-Inf from messing up
220     } else if ( name == "ry" ) {
221         rc->ry = val->getDoubleLimited();
222     }
225 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
227     SPDesktop *desktop = event_context->desktop;
229     gint ret = FALSE;
231     switch (event->type) {
232     case GDK_BUTTON_PRESS:
233         if ( event->button.button == 1 && !event_context->space_panning) {
234             Inkscape::setup_for_drag_start(desktop, event_context, event);
235             ret = TRUE;
236         }
237         break;
238         // motion and release are always on root (why?)
239     default:
240         break;
241     }
243     if (((SPEventContextClass *) parent_class)->item_handler) {
244         ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
245     }
247     return ret;
250 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event)
252     static bool dragging;
254     SPDesktop *desktop = event_context->desktop;
255     Inkscape::Selection *selection = sp_desktop_selection (desktop);
257     SPRectContext *rc = SP_RECT_CONTEXT(event_context);
258     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
260     event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
262     gint ret = FALSE;
263     switch (event->type) {
264     case GDK_BUTTON_PRESS:
265         if (event->button.button == 1 && !event_context->space_panning) {
266             Geom::Point const button_w(event->button.x,
267                                      event->button.y);
269             // save drag origin
270             event_context->xp = (gint) button_w[Geom::X];
271             event_context->yp = (gint) button_w[Geom::Y];
272             event_context->within_tolerance = true;
274             // remember clicked item, disregarding groups, honoring Alt
275             event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
277             dragging = true;
279             /* Position center */
280             Geom::Point button_dt(desktop->w2d(button_w));
281             rc->center = from_2geom(button_dt);
283             /* Snap center */
284             SnapManager &m = desktop->namedview->snap_manager;
285             m.setup(desktop);
286             m.freeSnapReturnByRef(button_dt, Inkscape::SNAPSOURCE_NODE_HANDLE);
287             m.unSetup();
288             rc->center = from_2geom(button_dt);
290             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
291                                 ( GDK_KEY_PRESS_MASK |
292                                   GDK_BUTTON_RELEASE_MASK       |
293                                   GDK_POINTER_MOTION_MASK       |
294                                   GDK_POINTER_MOTION_HINT_MASK       |
295                                   GDK_BUTTON_PRESS_MASK ),
296                                 NULL, event->button.time);
298             ret = TRUE;
299         }
300         break;
301     case GDK_MOTION_NOTIFY:
302         if ( dragging
303              && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning)
304         {
305             if ( event_context->within_tolerance
306                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
307                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
308                 break; // do not drag if we're within tolerance from origin
309             }
310             // Once the user has moved farther than tolerance from the original location
311             // (indicating they intend to draw, not click), then always process the
312             // motion notify coordinates as given (no snapping back to origin)
313             event_context->within_tolerance = false;
315             Geom::Point const motion_w(event->motion.x, event->motion.y);
316             Geom::Point motion_dt(desktop->w2d(motion_w));
318             sp_rect_drag(*rc, motion_dt, event->motion.state); // this will also handle the snapping
319             gobble_motion_events(GDK_BUTTON1_MASK);
320             ret = TRUE;
321         } else if (!sp_event_context_knot_mouseover(rc)) {
322             SnapManager &m = desktop->namedview->snap_manager;
323             m.setup(desktop);
325             Geom::Point const motion_w(event->motion.x, event->motion.y);
326             Geom::Point motion_dt(desktop->w2d(motion_w));
328             m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE));
329             m.unSetup();
330         }
331         break;
332     case GDK_BUTTON_RELEASE:
333         event_context->xp = event_context->yp = 0;
334         if (event->button.button == 1 && !event_context->space_panning) {
335             dragging = false;
336             sp_event_context_discard_delayed_snap_event(event_context);
338             if (!event_context->within_tolerance) {
339                 // we've been dragging, finish the rect
340                 sp_rect_finish(rc);
341             } else if (event_context->item_to_select) {
342                 // no dragging, select clicked item if any
343                 if (event->button.state & GDK_SHIFT_MASK) {
344                     selection->toggle(event_context->item_to_select);
345                 } else {
346                     selection->set(event_context->item_to_select);
347                 }
348             } else {
349                 // click in an empty space
350                 selection->clear();
351             }
353             event_context->item_to_select = NULL;
354             ret = TRUE;
355             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
356                                   event->button.time);
357         }
358         break;
359     case GDK_KEY_PRESS:
360         switch (get_group0_keyval (&event->key)) {
361         case GDK_Alt_L:
362         case GDK_Alt_R:
363         case GDK_Control_L:
364         case GDK_Control_R:
365         case GDK_Shift_L:
366         case GDK_Shift_R:
367         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
368         case GDK_Meta_R:
369             if (!dragging){
370                 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
371                                             _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
372                                             _("<b>Shift</b>: draw around the starting point"),
373                                             NULL);
374             }
375             break;
376         case GDK_Up:
377         case GDK_Down:
378         case GDK_KP_Up:
379         case GDK_KP_Down:
380             // prevent the zoom field from activation
381             if (!MOD__CTRL_ONLY)
382                 ret = TRUE;
383             break;
385         case GDK_x:
386         case GDK_X:
387             if (MOD__ALT_ONLY) {
388                 desktop->setToolboxFocusTo ("altx-rect");
389                 ret = TRUE;
390             }
391             break;
393         case GDK_g:
394         case GDK_G:
395             if (MOD__SHIFT_ONLY) {
396                 sp_selection_to_guides(desktop);
397                 ret = true;
398             }
399             break;
401         case GDK_Escape:
402             if (dragging) {
403                 dragging = false;
404                 sp_event_context_discard_delayed_snap_event(event_context);
405                 // if drawing, cancel, otherwise pass it up for deselecting
406                 sp_rect_cancel(rc);
407                 ret = TRUE;
408             }
409             break;
411         case GDK_space:
412             if (dragging) {
413                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
414                                       event->button.time);
415                 dragging = false;
416                 sp_event_context_discard_delayed_snap_event(event_context);
417                 if (!event_context->within_tolerance) {
418                     // we've been dragging, finish the rect
419                     sp_rect_finish(rc);
420                 }
421                 // do not return true, so that space would work switching to selector
422             }
423             break;
425         default:
426             break;
427         }
428         break;
429     case GDK_KEY_RELEASE:
430         switch (get_group0_keyval (&event->key)) {
431         case GDK_Alt_L:
432         case GDK_Alt_R:
433         case GDK_Control_L:
434         case GDK_Control_R:
435         case GDK_Shift_L:
436         case GDK_Shift_R:
437         case GDK_Meta_L:  // Meta is when you press Shift+Alt
438         case GDK_Meta_R:
439             event_context->defaultMessageContext()->clear();
440             break;
441         default:
442             break;
443         }
444         break;
445     default:
446         break;
447     }
449     if (!ret) {
450         if (((SPEventContextClass *) parent_class)->root_handler) {
451             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
452         }
453     }
455     return ret;
458 static void sp_rect_drag(SPRectContext &rc, Geom::Point const pt, guint state)
460     SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
462     if (!rc.item) {
464         if (Inkscape::have_viable_layer(desktop, rc._message_context) == false) {
465             return;
466         }
468         /* Create object */
469         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(&rc));
470         Inkscape::XML::Node *repr = xml_doc->createElement("svg:rect");
472         /* Set style */
473         sp_desktop_apply_style_tool (desktop, repr, "/tools/shapes/rect", false);
475         rc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
476         Inkscape::GC::release(repr);
477         rc.item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
478         rc.item->updateRepr();
480         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
481     }
483     Geom::Rect const r = Inkscape::snap_rectangular_box(desktop, rc.item, pt, rc.center, state);
485     sp_rect_position_set(SP_RECT(rc.item), r.min()[Geom::X], r.min()[Geom::Y], r.dimensions()[Geom::X], r.dimensions()[Geom::Y]);
486     if ( rc.rx != 0.0 ) {
487         sp_rect_set_rx (SP_RECT(rc.item), TRUE, rc.rx);
488     }
489     if ( rc.ry != 0.0 ) {
490         if (rc.rx == 0.0)
491             sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, MIN(r.dimensions()[Geom::X], r.dimensions()[Geom::Y])/2));
492         else
493             sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, r.dimensions()[Geom::Y]));
494     }
496     // status text
497     double rdimx = r.dimensions()[Geom::X];
498     double rdimy = r.dimensions()[Geom::Y];
499     GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
500     GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
501     if (state & GDK_CONTROL_MASK) {
502         int ratio_x, ratio_y;
503         bool is_golden_ratio = false;
504         if (fabs (rdimx) > fabs (rdimy)) {
505             if (fabs(rdimx / rdimy - goldenratio) < 1e-6) {
506                 is_golden_ratio = true;
507             }
508             ratio_x = (int) rint (rdimx / rdimy);
509             ratio_y = 1;
510         } else {
511             if (fabs(rdimy / rdimx - goldenratio) < 1e-6) {
512                 is_golden_ratio = true;
513             }
514             ratio_x = 1;
515             ratio_y = (int) rint (rdimy / rdimx);
516         }
517         if (!is_golden_ratio) {
518             rc._message_context->setF(Inkscape::IMMEDIATE_MESSAGE, _("<b>Rectangle</b>: %s &#215; %s (constrained to ratio %d:%d); with <b>Shift</b> to draw around the starting point"), xs->str, ys->str, ratio_x, ratio_y);
519         } else {
520             if (ratio_y == 1) {
521                 rc._message_context->setF(Inkscape::IMMEDIATE_MESSAGE, _("<b>Rectangle</b>: %s &#215; %s (constrained to golden ratio 1.618 : 1); with <b>Shift</b> to draw around the starting point"), xs->str, ys->str);
522             } else {
523                 rc._message_context->setF(Inkscape::IMMEDIATE_MESSAGE, _("<b>Rectangle</b>: %s &#215; %s (constrained to golden ratio 1 : 1.618); with <b>Shift</b> to draw around the starting point"), xs->str, ys->str);
524             }
525         }
526     } else {
527         rc._message_context->setF(Inkscape::IMMEDIATE_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);
528     }
529     g_string_free(xs, FALSE);
530     g_string_free(ys, FALSE);
533 static void sp_rect_finish(SPRectContext *rc)
535     rc->_message_context->clear();
537     if ( rc->item != NULL ) {
538         SPRect *rect = SP_RECT(rc->item);
539         if (rect->width.computed == 0 || rect->height.computed == 0) {
540             sp_rect_cancel(rc); // Don't allow the creating of zero sized rectangle, for example when the start and and point snap to the snap grid point
541             return;
542         }
544         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(rc);
546         SP_OBJECT(rc->item)->updateRepr();
548         sp_canvas_end_forced_full_redraws(desktop->canvas);
550         sp_desktop_selection(desktop)->set(rc->item);
551         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_RECT,
552                          _("Create rectangle"));
554         rc->item = NULL;
555     }
558 static void sp_rect_cancel(SPRectContext *rc)
560     SPDesktop *desktop = SP_EVENT_CONTEXT(rc)->desktop;
562     sp_desktop_selection(desktop)->clear();
563     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
565     if (rc->item != NULL) {
566         SP_OBJECT(rc->item)->deleteObject();
567         rc->item = NULL;
568     }
570     rc->within_tolerance = false;
571     rc->xp = 0;
572     rc->yp = 0;
573     rc->item_to_select = NULL;
575     sp_canvas_end_forced_full_redraws(desktop->canvas);
577     sp_document_cancel(sp_desktop_document(desktop));
581 /*
582   Local Variables:
583   mode:c++
584   c-file-style:"stroustrup"
585   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
586   indent-tabs-mode:nil
587   fill-column:99
588   End:
589 */
590 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :