Code

Remove INKSCAPE_VERSION from menus-skeleton.h
[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_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
55 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event);
56 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
58 static void sp_rect_drag(SPRectContext &rc, Geom::Point const pt, guint state);
59 static void sp_rect_finish(SPRectContext *rc);
61 static SPEventContextClass *parent_class;
64 GtkType sp_rect_context_get_type()
65 {
66     static GType type = 0;
67     if (!type) {
68         GTypeInfo info = {
69             sizeof(SPRectContextClass),
70             NULL, NULL,
71             (GClassInitFunc) sp_rect_context_class_init,
72             NULL, NULL,
73             sizeof(SPRectContext),
74             4,
75             (GInstanceInitFunc) sp_rect_context_init,
76             NULL,    /* value_table */
77         };
78         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPRectContext", &info, (GTypeFlags) 0);
79     }
80     return type;
81 }
83 static void sp_rect_context_class_init(SPRectContextClass *klass)
84 {
85     GObjectClass *object_class = (GObjectClass *) klass;
86     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
88     parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
90     object_class->dispose = sp_rect_context_dispose;
92     event_context_class->setup = sp_rect_context_setup;
93     event_context_class->set = sp_rect_context_set;
94     event_context_class->root_handler  = sp_rect_context_root_handler;
95     event_context_class->item_handler  = sp_rect_context_item_handler;
96 }
98 static void sp_rect_context_init(SPRectContext *rect_context)
99 {
100     SPEventContext *event_context = SP_EVENT_CONTEXT(rect_context);
102     event_context->cursor_shape = cursor_rect_xpm;
103     event_context->hot_x = 4;
104     event_context->hot_y = 4;
105     event_context->xp = 0;
106     event_context->yp = 0;
107     event_context->tolerance = 0;
108     event_context->within_tolerance = false;
109     event_context->item_to_select = NULL;
111     rect_context->item = NULL;
113     rect_context->rx = 0.0;
114     rect_context->ry = 0.0;
116     new (&rect_context->sel_changed_connection) sigc::connection();
119 static void sp_rect_context_dispose(GObject *object)
121     SPRectContext *rc = SP_RECT_CONTEXT(object);
122     SPEventContext *ec = SP_EVENT_CONTEXT(object);
124     ec->enableGrDrag(false);
126     rc->sel_changed_connection.disconnect();
127     rc->sel_changed_connection.~connection();
129     delete ec->shape_editor;
130     ec->shape_editor = NULL;
132     /* fixme: This is necessary because we do not grab */
133     if (rc->item) {
134         sp_rect_finish(rc);
135     }
137     if (rc->_message_context) {
138         delete rc->_message_context;
139     }
141     G_OBJECT_CLASS(parent_class)->dispose(object);
144 /**
145 \brief  Callback that processes the "changed" signal on the selection;
146 destroys old and creates new knotholder
147 */
148 void sp_rect_context_selection_changed(Inkscape::Selection *selection, gpointer data)
150     SPRectContext *rc = SP_RECT_CONTEXT(data);
151     SPEventContext *ec = SP_EVENT_CONTEXT(rc);
153     ec->shape_editor->unset_item(SH_KNOTHOLDER);
154     SPItem *item = selection->singleItem(); 
155     ec->shape_editor->set_item(item, SH_KNOTHOLDER);
158 static void sp_rect_context_setup(SPEventContext *ec)
160     SPRectContext *rc = SP_RECT_CONTEXT(ec);
162     if (((SPEventContextClass *) parent_class)->setup) {
163         ((SPEventContextClass *) parent_class)->setup(ec);
164     }
166     ec->shape_editor = new ShapeEditor(ec->desktop);
168     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
169     if (item) {
170         ec->shape_editor->set_item(item, SH_KNOTHOLDER);
171     }
173     rc->sel_changed_connection.disconnect();
174     rc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
175         sigc::bind(sigc::ptr_fun(&sp_rect_context_selection_changed), (gpointer)rc)
176     );
178     sp_event_context_read(ec, "rx");
179     sp_event_context_read(ec, "ry");
181     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
182     if (prefs->getBool("/tools/shapes/selcue")) {
183         ec->enableSelectionCue();
184     }
186     if (prefs->getBool("/tools/shapes/gradientdrag")) {
187         ec->enableGrDrag();
188     }
190     rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
193 static void sp_rect_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
195     SPRectContext *rc = SP_RECT_CONTEXT(ec);
197     /* fixme: Proper error handling for non-numeric data.  Use a locale-independent function like
198      * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
199     Glib::ustring name = val->getEntryName();
200     if ( name == "rx" ) {
201         rc->rx = val->getDoubleLimited(); // prevents NaN and +/-Inf from messing up
202     } else if ( name == "ry" ) {
203         rc->ry = val->getDoubleLimited();
204     }
207 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
209     SPDesktop *desktop = event_context->desktop;
211     gint ret = FALSE;
213     switch (event->type) {
214     case GDK_BUTTON_PRESS:
215         if ( event->button.button == 1 && !event_context->space_panning) {
216             Inkscape::setup_for_drag_start(desktop, event_context, event);
217             ret = TRUE;
218         }
219         break;
220         // motion and release are always on root (why?)
221     default:
222         break;
223     }
225     if (((SPEventContextClass *) parent_class)->item_handler) {
226         ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
227     }
229     return ret;
232 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event)
234     static bool dragging;
236     SPDesktop *desktop = event_context->desktop;
237     Inkscape::Selection *selection = sp_desktop_selection (desktop);
239     SPRectContext *rc = SP_RECT_CONTEXT(event_context);
240     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
242     event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
244     gint ret = FALSE;
245     switch (event->type) {
246     case GDK_BUTTON_PRESS:
247         if (event->button.button == 1 && !event_context->space_panning) {
248             Geom::Point const button_w(event->button.x,
249                                      event->button.y);
251             // save drag origin
252             event_context->xp = (gint) button_w[Geom::X];
253             event_context->yp = (gint) button_w[Geom::Y];
254             event_context->within_tolerance = true;
256             // remember clicked item, disregarding groups, honoring Alt
257             event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
259             dragging = true;
261             /* Position center */
262             Geom::Point button_dt(desktop->w2d(button_w));
263             rc->center = from_2geom(button_dt);
264             
265             /* Snap center */
266             SnapManager &m = desktop->namedview->snap_manager;
267             m.setup(desktop);
268             m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, button_dt);
269             rc->center = from_2geom(button_dt);
270             
271             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
272                                 ( GDK_KEY_PRESS_MASK |
273                                   GDK_BUTTON_RELEASE_MASK       |
274                                   GDK_POINTER_MOTION_MASK       |
275                                   GDK_POINTER_MOTION_HINT_MASK       |
276                                   GDK_BUTTON_PRESS_MASK ),
277                                 NULL, event->button.time);
278                                 
279             ret = TRUE;
280         }
281         break;
282     case GDK_MOTION_NOTIFY:
283         if ( dragging
284              && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning)
285         {
286             if ( event_context->within_tolerance
287                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
288                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
289                 break; // do not drag if we're within tolerance from origin
290             }
291             // Once the user has moved farther than tolerance from the original location
292             // (indicating they intend to draw, not click), then always process the
293             // motion notify coordinates as given (no snapping back to origin)
294             event_context->within_tolerance = false;
296             Geom::Point const motion_w(event->motion.x, event->motion.y);
297             Geom::Point motion_dt(desktop->w2d(motion_w));
298             
299             sp_rect_drag(*rc, motion_dt, event->motion.state); // this will also handle the snapping
300             gobble_motion_events(GDK_BUTTON1_MASK);
301             ret = TRUE;
302         }
303         break;
304     case GDK_BUTTON_RELEASE:
305         event_context->xp = event_context->yp = 0;
306         if (event->button.button == 1 && !event_context->space_panning) {
307             dragging = false;
309             if (!event_context->within_tolerance) {
310                 // we've been dragging, finish the rect
311                 sp_rect_finish(rc);
312             } else if (event_context->item_to_select) {
313                 // no dragging, select clicked item if any
314                 if (event->button.state & GDK_SHIFT_MASK) {
315                     selection->toggle(event_context->item_to_select);
316                 } else {
317                     selection->set(event_context->item_to_select);
318                 }
319             } else {
320                 // click in an empty space
321                 selection->clear();
322             }
324             event_context->item_to_select = NULL;
325             ret = TRUE;
326             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
327                                   event->button.time);
328         }
329         break;
330     case GDK_KEY_PRESS:
331         switch (get_group0_keyval (&event->key)) {
332         case GDK_Alt_L:
333         case GDK_Alt_R:
334         case GDK_Control_L:
335         case GDK_Control_R:
336         case GDK_Shift_L:
337         case GDK_Shift_R:
338         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
339         case GDK_Meta_R:
340             if (!dragging){
341                 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
342                                             _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
343                                             _("<b>Shift</b>: draw around the starting point"),
344                                             NULL);
345             }
346             break;
347         case GDK_Up:
348         case GDK_Down:
349         case GDK_KP_Up:
350         case GDK_KP_Down:
351             // prevent the zoom field from activation
352             if (!MOD__CTRL_ONLY)
353                 ret = TRUE;
354             break;
356         case GDK_x:
357         case GDK_X:
358             if (MOD__ALT_ONLY) {
359                 desktop->setToolboxFocusTo ("altx-rect");
360                 ret = TRUE;
361             }
362             break;
364         case GDK_g:
365         case GDK_G:
366             if (MOD__SHIFT_ONLY) {
367                 sp_selection_to_guides(desktop);
368                 ret = true;
369             }
370             break;
372         case GDK_Escape:
373             sp_desktop_selection(desktop)->clear();
374             //TODO: make dragging escapable by Esc
375             break;
377         case GDK_space:
378             if (dragging) {
379                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
380                                       event->button.time);
381                 dragging = false;
382                 if (!event_context->within_tolerance) {
383                     // we've been dragging, finish the rect
384                     sp_rect_finish(rc);
385                 }
386                 // do not return true, so that space would work switching to selector
387             }
388             break;
390         default:
391             break;
392         }
393         break;
394     case GDK_KEY_RELEASE:
395         switch (get_group0_keyval (&event->key)) {
396         case GDK_Alt_L:
397         case GDK_Alt_R:
398         case GDK_Control_L:
399         case GDK_Control_R:
400         case GDK_Shift_L:
401         case GDK_Shift_R:
402         case GDK_Meta_L:  // Meta is when you press Shift+Alt
403         case GDK_Meta_R:
404             event_context->defaultMessageContext()->clear();
405             break;
406         default:
407             break;
408         }
409         break;
410     default:
411         break;
412     }
414     if (!ret) {
415         if (((SPEventContextClass *) parent_class)->root_handler) {
416             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
417         }
418     }
420     return ret;
423 static void sp_rect_drag(SPRectContext &rc, Geom::Point const pt, guint state)
425     SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
427     if (!rc.item) {
429         if (Inkscape::have_viable_layer(desktop, rc._message_context) == false) {
430             return;
431         }
433         /* Create object */
434         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(&rc));
435         Inkscape::XML::Node *repr = xml_doc->createElement("svg:rect");
437         /* Set style */
438         sp_desktop_apply_style_tool (desktop, repr, "/tools/shapes/rect", false);
440         rc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
441         Inkscape::GC::release(repr);
442         rc.item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
443         rc.item->updateRepr();
445         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
446     }
448     Geom::Rect const r = Inkscape::snap_rectangular_box(desktop, rc.item, pt, rc.center, state);
450     sp_rect_position_set(SP_RECT(rc.item), r.min()[Geom::X], r.min()[Geom::Y], r.dimensions()[Geom::X], r.dimensions()[Geom::Y]);
451     if ( rc.rx != 0.0 ) {
452         sp_rect_set_rx (SP_RECT(rc.item), TRUE, rc.rx);
453     }
454     if ( rc.ry != 0.0 ) {
455         if (rc.rx == 0.0)
456             sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, MIN(r.dimensions()[Geom::X], r.dimensions()[Geom::Y])/2));
457         else
458             sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, r.dimensions()[Geom::Y]));
459     }
461     // status text
462     double rdimx = r.dimensions()[Geom::X];
463     double rdimy = r.dimensions()[Geom::Y];
464     GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
465     GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
466     if (state & GDK_CONTROL_MASK) {
467         int ratio_x, ratio_y;
468         bool is_golden_ratio = false;
469         if (fabs (rdimx) > fabs (rdimy)) {
470             if (fabs(rdimx / rdimy - goldenratio) < 1e-6) {
471                 is_golden_ratio = true;
472             }
473             ratio_x = (int) rint (rdimx / rdimy);
474             ratio_y = 1;
475         } else {
476             if (fabs(rdimy / rdimx - goldenratio) < 1e-6) {
477                 is_golden_ratio = true;
478             }
479             ratio_x = 1;
480             ratio_y = (int) rint (rdimy / rdimx);
481         }
482         if (!is_golden_ratio) {
483             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);
484         } else {
485             if (ratio_y == 1) {
486                 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);
487             } else {
488                 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);
489             }
490         }
491     } else {
492         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);
493     }
494     g_string_free(xs, FALSE);
495     g_string_free(ys, FALSE);
498 static void sp_rect_finish(SPRectContext *rc)
500     rc->_message_context->clear();
502     if ( rc->item != NULL ) {
503         SPDesktop * desktop;
505         desktop = SP_EVENT_CONTEXT_DESKTOP(rc);
507         SP_OBJECT(rc->item)->updateRepr();
509         sp_canvas_end_forced_full_redraws(desktop->canvas);
511         sp_desktop_selection(desktop)->set(rc->item);
512         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_RECT,
513                          _("Create rectangle"));
515         rc->item = NULL;
516     }
519 /*
520   Local Variables:
521   mode:c++
522   c-file-style:"stroustrup"
523   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
524   indent-tabs-mode:nil
525   fill-column:99
526   End:
527 */
528 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :