Code

Patch to fix build on Natty by Alex Valavanis
[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;
113     event_context->tool_url = "/tools/shapes/rect";
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_finish(SPEventContext *ec)
125     SPRectContext *rc = SP_RECT_CONTEXT(ec);
126     SPDesktop *desktop = ec->desktop;
128     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), GDK_CURRENT_TIME);
129     sp_rect_finish(rc);
130     rc->sel_changed_connection.disconnect();
132     if (((SPEventContextClass *) parent_class)->finish) {
133         ((SPEventContextClass *) parent_class)->finish(ec);
134     }
138 static void sp_rect_context_dispose(GObject *object)
140     SPRectContext *rc = SP_RECT_CONTEXT(object);
141     SPEventContext *ec = SP_EVENT_CONTEXT(object);
143     ec->enableGrDrag(false);
145     rc->sel_changed_connection.disconnect();
146     rc->sel_changed_connection.~connection();
148     delete ec->shape_editor;
149     ec->shape_editor = NULL;
151     /* fixme: This is necessary because we do not grab */
152     if (rc->item) {
153         sp_rect_finish(rc);
154     }
156     if (rc->_message_context) {
157         delete rc->_message_context;
158     }
160     G_OBJECT_CLASS(parent_class)->dispose(object);
163 /**
164 \brief  Callback that processes the "changed" signal on the selection;
165 destroys old and creates new knotholder
166 */
167 void sp_rect_context_selection_changed(Inkscape::Selection *selection, gpointer data)
169     SPRectContext *rc = SP_RECT_CONTEXT(data);
170     SPEventContext *ec = SP_EVENT_CONTEXT(rc);
172     ec->shape_editor->unset_item(SH_KNOTHOLDER);
173     SPItem *item = selection->singleItem();
174     ec->shape_editor->set_item(item, SH_KNOTHOLDER);
177 static void sp_rect_context_setup(SPEventContext *ec)
179     SPRectContext *rc = SP_RECT_CONTEXT(ec);
181     if (((SPEventContextClass *) parent_class)->setup) {
182         ((SPEventContextClass *) parent_class)->setup(ec);
183     }
185     ec->shape_editor = new ShapeEditor(ec->desktop);
187     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
188     if (item) {
189         ec->shape_editor->set_item(item, SH_KNOTHOLDER);
190     }
192     rc->sel_changed_connection.disconnect();
193     rc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
194         sigc::bind(sigc::ptr_fun(&sp_rect_context_selection_changed), (gpointer)rc)
195     );
197     sp_event_context_read(ec, "rx");
198     sp_event_context_read(ec, "ry");
200     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
201     if (prefs->getBool("/tools/shapes/selcue")) {
202         ec->enableSelectionCue();
203     }
205     if (prefs->getBool("/tools/shapes/gradientdrag")) {
206         ec->enableGrDrag();
207     }
209     rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
212 static void sp_rect_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
214     SPRectContext *rc = SP_RECT_CONTEXT(ec);
216     /* fixme: Proper error handling for non-numeric data.  Use a locale-independent function like
217      * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
218     Glib::ustring name = val->getEntryName();
219     if ( name == "rx" ) {
220         rc->rx = val->getDoubleLimited(); // prevents NaN and +/-Inf from messing up
221     } else if ( name == "ry" ) {
222         rc->ry = val->getDoubleLimited();
223     }
226 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
228     SPDesktop *desktop = event_context->desktop;
230     gint ret = FALSE;
232     switch (event->type) {
233     case GDK_BUTTON_PRESS:
234         if ( event->button.button == 1 && !event_context->space_panning) {
235             Inkscape::setup_for_drag_start(desktop, event_context, event);
236             ret = TRUE;
237         }
238         break;
239         // motion and release are always on root (why?)
240     default:
241         break;
242     }
244     if (((SPEventContextClass *) parent_class)->item_handler) {
245         ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
246     }
248     return ret;
251 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event)
253     static bool dragging;
255     SPDesktop *desktop = event_context->desktop;
256     Inkscape::Selection *selection = sp_desktop_selection (desktop);
258     SPRectContext *rc = SP_RECT_CONTEXT(event_context);
259     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
261     event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
263     gint ret = FALSE;
264     switch (event->type) {
265     case GDK_BUTTON_PRESS:
266         if (event->button.button == 1 && !event_context->space_panning) {
267             Geom::Point const button_w(event->button.x,
268                                      event->button.y);
270             // save drag origin
271             event_context->xp = (gint) button_w[Geom::X];
272             event_context->yp = (gint) button_w[Geom::Y];
273             event_context->within_tolerance = true;
275             // remember clicked item, disregarding groups, honoring Alt
276             event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
278             dragging = true;
280             /* Position center */
281             Geom::Point button_dt(desktop->w2d(button_w));
282             rc->center = from_2geom(button_dt);
284             /* Snap center */
285             SnapManager &m = desktop->namedview->snap_manager;
286             m.setup(desktop);
287             m.freeSnapReturnByRef(button_dt, Inkscape::SNAPSOURCE_NODE_HANDLE);
288             m.unSetup();
289             rc->center = from_2geom(button_dt);
291             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
292                                 ( GDK_KEY_PRESS_MASK |
293                                   GDK_BUTTON_RELEASE_MASK       |
294                                   GDK_POINTER_MOTION_MASK       |
295                                   GDK_POINTER_MOTION_HINT_MASK       |
296                                   GDK_BUTTON_PRESS_MASK ),
297                                 NULL, event->button.time);
299             ret = TRUE;
300         }
301         break;
302     case GDK_MOTION_NOTIFY:
303         if ( dragging
304              && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning)
305         {
306             if ( event_context->within_tolerance
307                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
308                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
309                 break; // do not drag if we're within tolerance from origin
310             }
311             // Once the user has moved farther than tolerance from the original location
312             // (indicating they intend to draw, not click), then always process the
313             // motion notify coordinates as given (no snapping back to origin)
314             event_context->within_tolerance = false;
316             Geom::Point const motion_w(event->motion.x, event->motion.y);
317             Geom::Point motion_dt(desktop->w2d(motion_w));
319             sp_rect_drag(*rc, motion_dt, event->motion.state); // this will also handle the snapping
320             gobble_motion_events(GDK_BUTTON1_MASK);
321             ret = TRUE;
322         } else if (!sp_event_context_knot_mouseover(rc)) {
323             SnapManager &m = desktop->namedview->snap_manager;
324             m.setup(desktop);
326             Geom::Point const motion_w(event->motion.x, event->motion.y);
327             Geom::Point motion_dt(desktop->w2d(motion_w));
329             m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE));
330             m.unSetup();
331         }
332         break;
333     case GDK_BUTTON_RELEASE:
334         event_context->xp = event_context->yp = 0;
335         if (event->button.button == 1 && !event_context->space_panning) {
336             dragging = false;
337             sp_event_context_discard_delayed_snap_event(event_context);
339             if (!event_context->within_tolerance) {
340                 // we've been dragging, finish the rect
341                 sp_rect_finish(rc);
342             } else if (event_context->item_to_select) {
343                 // no dragging, select clicked item if any
344                 if (event->button.state & GDK_SHIFT_MASK) {
345                     selection->toggle(event_context->item_to_select);
346                 } else {
347                     selection->set(event_context->item_to_select);
348                 }
349             } else {
350                 // click in an empty space
351                 selection->clear();
352             }
354             event_context->item_to_select = NULL;
355             ret = TRUE;
356             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
357                                   event->button.time);
358         }
359         break;
360     case GDK_KEY_PRESS:
361         switch (get_group0_keyval (&event->key)) {
362         case GDK_Alt_L:
363         case GDK_Alt_R:
364         case GDK_Control_L:
365         case GDK_Control_R:
366         case GDK_Shift_L:
367         case GDK_Shift_R:
368         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
369         case GDK_Meta_R:
370             if (!dragging){
371                 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
372                                             _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
373                                             _("<b>Shift</b>: draw around the starting point"),
374                                             NULL);
375             }
376             break;
377         case GDK_Up:
378         case GDK_Down:
379         case GDK_KP_Up:
380         case GDK_KP_Down:
381             // prevent the zoom field from activation
382             if (!MOD__CTRL_ONLY)
383                 ret = TRUE;
384             break;
386         case GDK_x:
387         case GDK_X:
388             if (MOD__ALT_ONLY) {
389                 desktop->setToolboxFocusTo ("altx-rect");
390                 ret = TRUE;
391             }
392             break;
394         case GDK_g:
395         case GDK_G:
396             if (MOD__SHIFT_ONLY) {
397                 sp_selection_to_guides(desktop);
398                 ret = true;
399             }
400             break;
402         case GDK_Escape:
403             if (dragging) {
404                 dragging = false;
405                 sp_event_context_discard_delayed_snap_event(event_context);
406                 // if drawing, cancel, otherwise pass it up for deselecting
407                 sp_rect_cancel(rc);
408                 ret = TRUE;
409             }
410             break;
412         case GDK_space:
413             if (dragging) {
414                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
415                                       event->button.time);
416                 dragging = false;
417                 sp_event_context_discard_delayed_snap_event(event_context);
418                 if (!event_context->within_tolerance) {
419                     // we've been dragging, finish the rect
420                     sp_rect_finish(rc);
421                 }
422                 // do not return true, so that space would work switching to selector
423             }
424             break;
426         default:
427             break;
428         }
429         break;
430     case GDK_KEY_RELEASE:
431         switch (get_group0_keyval (&event->key)) {
432         case GDK_Alt_L:
433         case GDK_Alt_R:
434         case GDK_Control_L:
435         case GDK_Control_R:
436         case GDK_Shift_L:
437         case GDK_Shift_R:
438         case GDK_Meta_L:  // Meta is when you press Shift+Alt
439         case GDK_Meta_R:
440             event_context->defaultMessageContext()->clear();
441             break;
442         default:
443             break;
444         }
445         break;
446     default:
447         break;
448     }
450     if (!ret) {
451         if (((SPEventContextClass *) parent_class)->root_handler) {
452             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
453         }
454     }
456     return ret;
459 static void sp_rect_drag(SPRectContext &rc, Geom::Point const pt, guint state)
461     SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
463     if (!rc.item) {
465         if (Inkscape::have_viable_layer(desktop, rc._message_context) == false) {
466             return;
467         }
469         /* Create object */
470         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(&rc));
471         Inkscape::XML::Node *repr = xml_doc->createElement("svg:rect");
473         /* Set style */
474         sp_desktop_apply_style_tool (desktop, repr, "/tools/shapes/rect", false);
476         rc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
477         Inkscape::GC::release(repr);
478         rc.item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
479         rc.item->updateRepr();
481         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
482     }
484     Geom::Rect const r = Inkscape::snap_rectangular_box(desktop, rc.item, pt, rc.center, state);
486     sp_rect_position_set(SP_RECT(rc.item), r.min()[Geom::X], r.min()[Geom::Y], r.dimensions()[Geom::X], r.dimensions()[Geom::Y]);
487     if ( rc.rx != 0.0 ) {
488         sp_rect_set_rx (SP_RECT(rc.item), TRUE, rc.rx);
489     }
490     if ( rc.ry != 0.0 ) {
491         if (rc.rx == 0.0)
492             sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, MIN(r.dimensions()[Geom::X], r.dimensions()[Geom::Y])/2));
493         else
494             sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, r.dimensions()[Geom::Y]));
495     }
497     // status text
498     double rdimx = r.dimensions()[Geom::X];
499     double rdimy = r.dimensions()[Geom::Y];
500     GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
501     GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
502     if (state & GDK_CONTROL_MASK) {
503         int ratio_x, ratio_y;
504         bool is_golden_ratio = false;
505         if (fabs (rdimx) > fabs (rdimy)) {
506             if (fabs(rdimx / rdimy - goldenratio) < 1e-6) {
507                 is_golden_ratio = true;
508             }
509             ratio_x = (int) rint (rdimx / rdimy);
510             ratio_y = 1;
511         } else {
512             if (fabs(rdimy / rdimx - goldenratio) < 1e-6) {
513                 is_golden_ratio = true;
514             }
515             ratio_x = 1;
516             ratio_y = (int) rint (rdimy / rdimx);
517         }
518         if (!is_golden_ratio) {
519             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);
520         } else {
521             if (ratio_y == 1) {
522                 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);
523             } else {
524                 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);
525             }
526         }
527     } else {
528         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);
529     }
530     g_string_free(xs, FALSE);
531     g_string_free(ys, FALSE);
534 static void sp_rect_finish(SPRectContext *rc)
536     rc->_message_context->clear();
538     if ( rc->item != NULL ) {
539         SPRect *rect = SP_RECT(rc->item);
540         if (rect->width.computed == 0 || rect->height.computed == 0) {
541             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
542             return;
543         }
545         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(rc);
547         SP_OBJECT(rc->item)->updateRepr();
549         sp_canvas_end_forced_full_redraws(desktop->canvas);
551         sp_desktop_selection(desktop)->set(rc->item);
552         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_RECT,
553                          _("Create rectangle"));
555         rc->item = NULL;
556     }
559 static void sp_rect_cancel(SPRectContext *rc)
561     SPDesktop *desktop = SP_EVENT_CONTEXT(rc)->desktop;
563     sp_desktop_selection(desktop)->clear();
564     sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
566     if (rc->item != NULL) {
567         SP_OBJECT(rc->item)->deleteObject();
568         rc->item = NULL;
569     }
571     rc->within_tolerance = false;
572     rc->xp = 0;
573     rc->yp = 0;
574     rc->item_to_select = NULL;
576     sp_canvas_end_forced_full_redraws(desktop->canvas);
578     sp_document_cancel(sp_desktop_document(desktop));
582 /*
583   Local Variables:
584   mode:c++
585   c-file-style:"stroustrup"
586   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
587   indent-tabs-mode:nil
588   fill-column:99
589   End:
590 */
591 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :