Code

No more NRMatrix or NRPoint.
[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"
18 #include "inkscape.h"
20 #include <gdk/gdkkeysyms.h>
21 #include <cstring>
22 #include <string>
24 #include "macros.h"
25 #include "display/sp-canvas.h"
26 #include "display/snap-indicator.h"
27 #include "sp-rect.h"
28 #include "document.h"
29 #include "sp-namedview.h"
30 #include "selection.h"
31 #include "selection-chemistry.h"
32 #include "desktop-handles.h"
33 #include "snap.h"
34 #include "desktop.h"
35 #include "desktop-style.h"
36 #include "message-context.h"
37 #include "pixmaps/cursor-rect.xpm"
38 #include "rect-context.h"
39 #include "sp-metrics.h"
40 #include <glibmm/i18n.h>
41 #include "object-edit.h"
42 #include "xml/repr.h"
43 #include "xml/node-event-vector.h"
44 #include "prefs-utils.h"
45 #include "context-fns.h"
47 //static const double goldenratio = 1.61803398874989484820; // golden ratio
49 static void sp_rect_context_class_init(SPRectContextClass *klass);
50 static void sp_rect_context_init(SPRectContext *rect_context);
51 static void sp_rect_context_dispose(GObject *object);
53 static void sp_rect_context_setup(SPEventContext *ec);
54 static void sp_rect_context_set(SPEventContext *ec, gchar const *key, gchar const *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, NR::Point const pt, guint state);
60 static void sp_rect_finish(SPRectContext *rc);
62 static SPEventContextClass *parent_class;
65 GtkType sp_rect_context_get_type()
66 {
67     static GType type = 0;
68     if (!type) {
69         GTypeInfo info = {
70             sizeof(SPRectContextClass),
71             NULL, NULL,
72             (GClassInitFunc) sp_rect_context_class_init,
73             NULL, NULL,
74             sizeof(SPRectContext),
75             4,
76             (GInstanceInitFunc) sp_rect_context_init,
77             NULL,    /* value_table */
78         };
79         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPRectContext", &info, (GTypeFlags) 0);
80     }
81     return type;
82 }
84 static void sp_rect_context_class_init(SPRectContextClass *klass)
85 {
86     GObjectClass *object_class = (GObjectClass *) klass;
87     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
89     parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
91     object_class->dispose = sp_rect_context_dispose;
93     event_context_class->setup = sp_rect_context_setup;
94     event_context_class->set = sp_rect_context_set;
95     event_context_class->root_handler  = sp_rect_context_root_handler;
96     event_context_class->item_handler  = sp_rect_context_item_handler;
97 }
99 static void sp_rect_context_init(SPRectContext *rect_context)
101     SPEventContext *event_context = SP_EVENT_CONTEXT(rect_context);
103     event_context->cursor_shape = cursor_rect_xpm;
104     event_context->hot_x = 4;
105     event_context->hot_y = 4;
106     event_context->xp = 0;
107     event_context->yp = 0;
108     event_context->tolerance = 0;
109     event_context->within_tolerance = false;
110     event_context->item_to_select = NULL;
112     event_context->shape_repr = NULL;
113     event_context->shape_knot_holder = NULL;
115     rect_context->item = NULL;
117     rect_context->rx = 0.0;
118     rect_context->ry = 0.0;
120     new (&rect_context->sel_changed_connection) sigc::connection();
123 static void sp_rect_context_dispose(GObject *object)
125     SPRectContext *rc = SP_RECT_CONTEXT(object);
126     SPEventContext *ec = SP_EVENT_CONTEXT(object);
128     ec->enableGrDrag(false);
130     rc->sel_changed_connection.disconnect();
131     rc->sel_changed_connection.~connection();
133     /* fixme: This is necessary because we do not grab */
134     if (rc->item) {
135         sp_rect_finish(rc);
136     }
138     if (ec->shape_knot_holder) {
139         sp_knot_holder_destroy(ec->shape_knot_holder);
140         ec->shape_knot_holder = NULL;
141     }
143     if (ec->shape_repr) { // remove old listener
144         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
145         Inkscape::GC::release(ec->shape_repr);
146         ec->shape_repr = 0;
147     }
149     if (rc->_message_context) {
150         delete rc->_message_context;
151     }
153     G_OBJECT_CLASS(parent_class)->dispose(object);
156 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
157     NULL, /* child_added */
158     NULL, /* child_removed */
159     ec_shape_event_attr_changed,
160     NULL, /* content_changed */
161     NULL  /* order_changed */
162 };
164 /**
165 \brief  Callback that processes the "changed" signal on the selection;
166 destroys old and creates new knotholder
167 */
168 void sp_rect_context_selection_changed(Inkscape::Selection *selection, gpointer data)
170     SPRectContext *rc = SP_RECT_CONTEXT(data);
171     SPEventContext *ec = SP_EVENT_CONTEXT(rc);
173     if (ec->shape_knot_holder) { // destroy knotholder
174         sp_knot_holder_destroy(ec->shape_knot_holder);
175         ec->shape_knot_holder = NULL;
176     }
178     if (ec->shape_repr) { // remove old listener
179         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
180         Inkscape::GC::release(ec->shape_repr);
181         ec->shape_repr = 0;
182     }
184     SPItem *item = selection->singleItem();
185     if (item) {
186         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
187         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
188         if (shape_repr) {
189             ec->shape_repr = shape_repr;
190             Inkscape::GC::anchor(shape_repr);
191             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
192         }
193     }
196 static void sp_rect_context_setup(SPEventContext *ec)
198     SPRectContext *rc = SP_RECT_CONTEXT(ec);
200     if (((SPEventContextClass *) parent_class)->setup) {
201         ((SPEventContextClass *) parent_class)->setup(ec);
202     }
204     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
205     if (item) {
206         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
207         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
208         if (shape_repr) {
209             ec->shape_repr = shape_repr;
210             Inkscape::GC::anchor(shape_repr);
211             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
212         }
213     }
215     rc->sel_changed_connection.disconnect();
216     rc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
217         sigc::bind(sigc::ptr_fun(&sp_rect_context_selection_changed), (gpointer)rc)
218     );
220     sp_event_context_read(ec, "rx");
221     sp_event_context_read(ec, "ry");
223     if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
224         ec->enableSelectionCue();
225     }
227     if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
228         ec->enableGrDrag();
229     }
231     rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
234 static void sp_rect_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
236     SPRectContext *rc = SP_RECT_CONTEXT(ec);
238     /* fixme: Proper error handling for non-numeric data.  Use a locale-independent function like
239      * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
240     if ( strcmp(key, "rx") == 0 ) {
241         rc->rx = ( val
242                          ? g_ascii_strtod (val, NULL)
243                          : 0.0 );
244     } else if ( strcmp(key, "ry") == 0 ) {
245         rc->ry = ( val
246                          ? g_ascii_strtod (val, NULL)
247                          : 0.0 );
248     }
251 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
253     SPDesktop *desktop = event_context->desktop;
255     gint ret = FALSE;
257     switch (event->type) {
258     case GDK_BUTTON_PRESS:
259         if ( event->button.button == 1 && !event_context->space_panning) {
260             Inkscape::setup_for_drag_start(desktop, event_context, event);
261             ret = TRUE;
262         }
263         break;
264         // motion and release are always on root (why?)
265     default:
266         break;
267     }
269     if (((SPEventContextClass *) parent_class)->item_handler) {
270         ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
271     }
273     return ret;
276 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event)
278     static bool dragging;
280     SPDesktop *desktop = event_context->desktop;
281     Inkscape::Selection *selection = sp_desktop_selection (desktop);
283     SPRectContext *rc = SP_RECT_CONTEXT(event_context);
285     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
287     desktop->snapindicator->remove_snappoint();
289     gint ret = FALSE;
290     switch (event->type) {
291     case GDK_BUTTON_PRESS:
292         if (event->button.button == 1 && !event_context->space_panning) {
293             NR::Point const button_w(event->button.x,
294                                      event->button.y);
296             // save drag origin
297             event_context->xp = (gint) button_w[NR::X];
298             event_context->yp = (gint) button_w[NR::Y];
299             event_context->within_tolerance = true;
301             // remember clicked item, disregarding groups, honoring Alt
302             event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
304             dragging = true;
306             /* Position center */
307             NR::Point const button_dt(desktop->w2d(button_w));
309             /* Snap center */
310             SnapManager const &m = desktop->namedview->snap_manager;
311             Inkscape::SnappedPoint s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, button_dt, rc->item);
312             rc->center = s.getPoint();
313             if (s.getDistance() < NR_HUGE) {
314                 desktop->snapindicator->set_new_snappoint(s.getPoint().to_2geom());
315             }
317             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
318                                 ( GDK_KEY_PRESS_MASK |
319                                   GDK_BUTTON_RELEASE_MASK       |
320                                   GDK_POINTER_MOTION_MASK       |
321                                   GDK_BUTTON_PRESS_MASK ),
322                                 NULL, event->button.time);
323             ret = TRUE;
324         }
325         break;
326     case GDK_MOTION_NOTIFY:
327         if ( dragging
328              && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning)
329         {
330             if ( event_context->within_tolerance
331                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
332                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
333                 break; // do not drag if we're within tolerance from origin
334             }
335             // Once the user has moved farther than tolerance from the original location
336             // (indicating they intend to draw, not click), then always process the
337             // motion notify coordinates as given (no snapping back to origin)
338             event_context->within_tolerance = false;
340             NR::Point const motion_w(event->motion.x,
341                                      event->motion.y);
342             NR::Point motion_dt(desktop->w2d(motion_w));
343             
344             SnapManager const &m = desktop->namedview->snap_manager;
345             Inkscape::SnappedPoint s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, motion_dt, rc->item);
346             motion_dt = s.getPoint();
347             if (s.getDistance() < NR_HUGE) {
348                 desktop->snapindicator->set_new_snappoint(s.getPoint().to_2geom());
349             }
351             sp_rect_drag(*rc, motion_dt, event->motion.state);
352             gobble_motion_events(GDK_BUTTON1_MASK);
353             ret = TRUE;
354         }
355         break;
356     case GDK_BUTTON_RELEASE:
357         event_context->xp = event_context->yp = 0;
358         if (event->button.button == 1 && !event_context->space_panning) {
359             dragging = false;
361             if (!event_context->within_tolerance) {
362                 // we've been dragging, finish the rect
363                 sp_rect_finish(rc);
364             } else if (event_context->item_to_select) {
365                 // no dragging, select clicked item if any
366                 if (event->button.state & GDK_SHIFT_MASK) {
367                     selection->toggle(event_context->item_to_select);
368                 } else {
369                     selection->set(event_context->item_to_select);
370                 }
371             } else {
372                 // click in an empty space
373                 selection->clear();
374             }
376             event_context->item_to_select = NULL;
377             ret = TRUE;
378             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
379                                   event->button.time);
380         }
381         break;
382     case GDK_KEY_PRESS:
383         switch (get_group0_keyval (&event->key)) {
384         case GDK_Alt_L:
385         case GDK_Alt_R:
386         case GDK_Control_L:
387         case GDK_Control_R:
388         case GDK_Shift_L:
389         case GDK_Shift_R:
390         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
391         case GDK_Meta_R:
392             if (!dragging){
393                 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
394                                             _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
395                                             _("<b>Shift</b>: draw around the starting point"),
396                                             NULL);
397             }
398             break;
399         case GDK_Up:
400         case GDK_Down:
401         case GDK_KP_Up:
402         case GDK_KP_Down:
403             // prevent the zoom field from activation
404             if (!MOD__CTRL_ONLY)
405                 ret = TRUE;
406             break;
408         case GDK_x:
409         case GDK_X:
410             if (MOD__ALT_ONLY) {
411                 desktop->setToolboxFocusTo ("altx-rect");
412                 ret = TRUE;
413             }
414             break;
416         case GDK_T:
417             {
418                 Inkscape::Selection *selection = sp_desktop_selection (inkscape_active_desktop());
419                 SPItem *item = selection->singleItem();
420                 if (item && SP_IS_RECT (item)) {
421                     g_print ("Scaling transformation matrix\n");
422                     SP_RECT (item)->transform = NR::Matrix(NR::scale(1.25, 1.5));
423                     SP_OBJECT (item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
424                 }
425                 ret = TRUE;
426             }
427             break;
429         case GDK_g:
430         case GDK_G:
431             if (MOD__SHIFT_ONLY) {
432                 sp_selection_to_guides();
433                 ret = true;
434             }
435             break;
437         case GDK_Escape:
438             sp_desktop_selection(desktop)->clear();
439             //TODO: make dragging escapable by Esc
440             break;
442         case GDK_space:
443             if (dragging) {
444                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
445                                       event->button.time);
446                 dragging = false;
447                 if (!event_context->within_tolerance) {
448                     // we've been dragging, finish the rect
449                     sp_rect_finish(rc);
450                 }
451                 // do not return true, so that space would work switching to selector
452             }
453             break;
455         default:
456             break;
457         }
458         break;
459     case GDK_KEY_RELEASE:
460         switch (get_group0_keyval (&event->key)) {
461         case GDK_Alt_L:
462         case GDK_Alt_R:
463         case GDK_Control_L:
464         case GDK_Control_R:
465         case GDK_Shift_L:
466         case GDK_Shift_R:
467         case GDK_Meta_L:  // Meta is when you press Shift+Alt
468         case GDK_Meta_R:
469             event_context->defaultMessageContext()->clear();
470             break;
471         default:
472             break;
473         }
474         break;
475     default:
476         break;
477     }
479     if (!ret) {
480         if (((SPEventContextClass *) parent_class)->root_handler) {
481             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
482         }
483     }
485     return ret;
488 static void sp_rect_drag(SPRectContext &rc, NR::Point const pt, guint state)
490     SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
492     if (!rc.item) {
494         if (Inkscape::have_viable_layer(desktop, rc._message_context) == false) {
495             return;
496         }
498         /* Create object */
499         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(&rc));
500         Inkscape::XML::Node *repr = xml_doc->createElement("svg:rect");
502         /* Set style */
503         sp_desktop_apply_style_tool (desktop, repr, "tools.shapes.rect", false);
505         rc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
506         Inkscape::GC::release(repr);
507         rc.item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
508         rc.item->updateRepr();
510         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
511     }
513     NR::Rect const r = Inkscape::snap_rectangular_box(desktop, rc.item, pt, rc.center, state);
515     sp_rect_position_set(SP_RECT(rc.item), r.min()[NR::X], r.min()[NR::Y], r.dimensions()[NR::X], r.dimensions()[NR::Y]);
516     if ( rc.rx != 0.0 ) {
517         sp_rect_set_rx (SP_RECT(rc.item), TRUE, rc.rx);
518     }
519     if ( rc.ry != 0.0 ) {
520         if (rc.rx == 0.0)
521             sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, MIN(r.dimensions()[NR::X], r.dimensions()[NR::Y])/2));
522         else
523             sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, r.dimensions()[NR::Y]));
524     }
526     // status text
527     double rdimx = r.dimensions()[NR::X];
528     double rdimy = r.dimensions()[NR::Y];
529     GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
530     GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
531     if (state & GDK_CONTROL_MASK) {
532         int ratio_x, ratio_y;
533         bool is_golden_ratio = false;
534         if (fabs (rdimx) > fabs (rdimy)) {
535             if (fabs(rdimx / rdimy - goldenratio) < 1e-6) {
536                 is_golden_ratio = true;
537             }
538             ratio_x = (int) rint (rdimx / rdimy);
539             ratio_y = 1;
540         } else {
541             if (fabs(rdimy / rdimx - goldenratio) < 1e-6) {
542                 is_golden_ratio = true;
543             }
544             ratio_x = 1;
545             ratio_y = (int) rint (rdimy / rdimx);
546         }
547         if (!is_golden_ratio) {
548             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);
549         } else {
550             if (ratio_y == 1) {
551                 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);
552             } else {
553                 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);
554             }
555         }
556     } else {
557         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);
558     }
559     g_string_free(xs, FALSE);
560     g_string_free(ys, FALSE);
563 static void sp_rect_finish(SPRectContext *rc)
565     rc->_message_context->clear();
567     if ( rc->item != NULL ) {
568         SPDesktop * desktop;
570         desktop = SP_EVENT_CONTEXT_DESKTOP(rc);
572         SP_OBJECT(rc->item)->updateRepr();
574         sp_canvas_end_forced_full_redraws(desktop->canvas);
576         sp_desktop_selection(desktop)->set(rc->item);
577         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_RECT,
578                          _("Create rectangle"));
580         rc->item = NULL;
581     }
584 /*
585   Local Variables:
586   mode:c++
587   c-file-style:"stroustrup"
588   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
589   indent-tabs-mode:nil
590   fill-column:99
591   End:
592 */
593 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :