Code

turns out, all these synthesize_events were not necessary at all - they just fired...
[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) 2000-2005 authors
11  * Copyright (C) 2000-2001 Ximian, Inc.
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #include "config.h"
18 #include <gdk/gdkkeysyms.h>
20 #include "macros.h"
21 #include "display/sp-canvas.h"
22 #include "sp-rect.h"
23 #include "document.h"
24 #include "sp-namedview.h"
25 #include "selection.h"
26 #include "desktop-handles.h"
27 #include "snap.h"
28 #include "desktop.h"
29 #include "desktop-style.h"
30 #include "message-context.h"
31 #include "pixmaps/cursor-rect.xpm"
32 #include "rect-context.h"
33 #include "sp-metrics.h"
34 #include <glibmm/i18n.h>
35 #include "object-edit.h"
36 #include "xml/repr.h"
37 #include "xml/node-event-vector.h"
38 #include "prefs-utils.h"
39 #include "context-fns.h"
41 static void sp_rect_context_class_init(SPRectContextClass *klass);
42 static void sp_rect_context_init(SPRectContext *rect_context);
43 static void sp_rect_context_dispose(GObject *object);
45 static void sp_rect_context_setup(SPEventContext *ec);
46 static void sp_rect_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
48 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event);
49 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
51 static void sp_rect_drag(SPRectContext &rc, NR::Point const pt, guint state);
52 static void sp_rect_finish(SPRectContext *rc);
54 static SPEventContextClass *parent_class;
57 GtkType sp_rect_context_get_type()
58 {
59     static GType type = 0;
60     if (!type) {
61         GTypeInfo info = {
62             sizeof(SPRectContextClass),
63             NULL, NULL,
64             (GClassInitFunc) sp_rect_context_class_init,
65             NULL, NULL,
66             sizeof(SPRectContext),
67             4,
68             (GInstanceInitFunc) sp_rect_context_init,
69             NULL,    /* value_table */
70         };
71         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPRectContext", &info, (GTypeFlags) 0);
72     }
73     return type;
74 }
76 static void sp_rect_context_class_init(SPRectContextClass *klass)
77 {
78     GObjectClass *object_class = (GObjectClass *) klass;
79     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
81     parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
83     object_class->dispose = sp_rect_context_dispose;
85     event_context_class->setup = sp_rect_context_setup;
86     event_context_class->set = sp_rect_context_set;
87     event_context_class->root_handler  = sp_rect_context_root_handler;
88     event_context_class->item_handler  = sp_rect_context_item_handler;
89 }
91 static void sp_rect_context_init(SPRectContext *rect_context)
92 {
93     SPEventContext *event_context = SP_EVENT_CONTEXT(rect_context);
95     event_context->cursor_shape = cursor_rect_xpm;
96     event_context->hot_x = 4;
97     event_context->hot_y = 4;
98     event_context->xp = 0;
99     event_context->yp = 0;
100     event_context->tolerance = 0;
101     event_context->within_tolerance = false;
102     event_context->item_to_select = NULL;
104     event_context->shape_repr = NULL;
105     event_context->shape_knot_holder = NULL;
107     rect_context->item = NULL;
109     rect_context->rx = 0.0;
110     rect_context->ry = 0.0;
112     new (&rect_context->sel_changed_connection) sigc::connection();
115 static void sp_rect_context_dispose(GObject *object)
117     SPRectContext *rc = SP_RECT_CONTEXT(object);
118     SPEventContext *ec = SP_EVENT_CONTEXT(object);
120     ec->enableGrDrag(false);
122     rc->sel_changed_connection.disconnect();
123     rc->sel_changed_connection.~connection();
125     /* fixme: This is necessary because we do not grab */
126     if (rc->item) {
127         sp_rect_finish(rc);
128     }
130     if (ec->shape_knot_holder) {
131         sp_knot_holder_destroy(ec->shape_knot_holder);
132         ec->shape_knot_holder = NULL;
133     }
135     if (ec->shape_repr) { // remove old listener
136         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
137         Inkscape::GC::release(ec->shape_repr);
138         ec->shape_repr = 0;
139     }
141     if (rc->_message_context) {
142         delete rc->_message_context;
143     }
145     G_OBJECT_CLASS(parent_class)->dispose(object);
148 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
149     NULL, /* child_added */
150     NULL, /* child_removed */
151     ec_shape_event_attr_changed,
152     NULL, /* content_changed */
153     NULL  /* order_changed */
154 };
156 /**
157 \brief  Callback that processes the "changed" signal on the selection;
158 destroys old and creates new knotholder
159 */
160 void sp_rect_context_selection_changed(Inkscape::Selection *selection, gpointer data)
162     SPRectContext *rc = SP_RECT_CONTEXT(data);
163     SPEventContext *ec = SP_EVENT_CONTEXT(rc);
165     if (ec->shape_knot_holder) { // destroy knotholder
166         sp_knot_holder_destroy(ec->shape_knot_holder);
167         ec->shape_knot_holder = NULL;
168     }
170     if (ec->shape_repr) { // remove old listener
171         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
172         Inkscape::GC::release(ec->shape_repr);
173         ec->shape_repr = 0;
174     }
176     SPItem *item = selection->singleItem();
177     if (item) {
178         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
179         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
180         if (shape_repr) {
181             ec->shape_repr = shape_repr;
182             Inkscape::GC::anchor(shape_repr);
183             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
184         }
185     }
188 static void sp_rect_context_setup(SPEventContext *ec)
190     SPRectContext *rc = SP_RECT_CONTEXT(ec);
192     if (((SPEventContextClass *) parent_class)->setup) {
193         ((SPEventContextClass *) parent_class)->setup(ec);
194     }
196     SPItem *item = SP_DT_SELECTION(ec->desktop)->singleItem();
197     if (item) {
198         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
199         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
200         if (shape_repr) {
201             ec->shape_repr = shape_repr;
202             Inkscape::GC::anchor(shape_repr);
203             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
204         }
205     }
207     rc->sel_changed_connection.disconnect();
208     rc->sel_changed_connection = SP_DT_SELECTION(ec->desktop)->connectChanged(
209         sigc::bind(sigc::ptr_fun(&sp_rect_context_selection_changed), (gpointer)rc)
210     );
212     sp_event_context_read(ec, "rx");
213     sp_event_context_read(ec, "ry");
215     if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
216         ec->enableSelectionCue();
217     }
219     if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
220         ec->enableGrDrag();
221     }
223     rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
226 static void sp_rect_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
228     SPRectContext *rc = SP_RECT_CONTEXT(ec);
230     /* fixme: Proper error handling for non-numeric data.  Use a locale-independent function like
231      * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
232     if ( strcmp(key, "rx") == 0 ) {
233         rc->rx = ( val
234                          ? g_ascii_strtod (val, NULL)
235                          : 0.0 );
236     } else if ( strcmp(key, "ry") == 0 ) {
237         rc->ry = ( val
238                          ? g_ascii_strtod (val, NULL)
239                          : 0.0 );
240     }
243 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
245     SPDesktop *desktop = event_context->desktop;
247     gint ret = FALSE;
249     switch (event->type) {
250     case GDK_BUTTON_PRESS:
251         if ( event->button.button == 1 ) {
252             Inkscape::setup_for_drag_start(desktop, event_context, event);
253             ret = TRUE;
254         }
255         break;
256         // motion and release are always on root (why?)
257     default:
258         break;
259     }
261     if (((SPEventContextClass *) parent_class)->item_handler) {
262         ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
263     }
265     return ret;
268 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event)
270     static bool dragging;
272     SPDesktop *desktop = event_context->desktop;
273     Inkscape::Selection *selection = SP_DT_SELECTION (desktop);
275     SPRectContext *rc = SP_RECT_CONTEXT(event_context);
277     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
279     gint ret = FALSE;
280     switch (event->type) {
281     case GDK_BUTTON_PRESS:
282         if ( event->button.button == 1 ) {
283             NR::Point const button_w(event->button.x,
284                                      event->button.y);
286             // save drag origin
287             event_context->xp = (gint) button_w[NR::X];
288             event_context->yp = (gint) button_w[NR::Y];
289             event_context->within_tolerance = true;
291             // remember clicked item, disregarding groups, honoring Alt
292             event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
294             dragging = true;
296             /* Position center */
297             NR::Point const button_dt(desktop->w2d(button_w));
299             /* Snap center */
300             SnapManager const m(desktop->namedview);
301             rc->center = m.freeSnap(Inkscape::Snapper::SNAP_POINT | Inkscape::Snapper::BBOX_POINT,
302                                     button_dt, rc->item).getPoint();
304             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
305                                 ( GDK_KEY_PRESS_MASK |
306                                   GDK_BUTTON_RELEASE_MASK       |
307                                   GDK_POINTER_MOTION_MASK       |
308                                   GDK_POINTER_MOTION_HINT_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 const motion_dt(desktop->w2d(motion_w));
331             sp_rect_drag(*rc, motion_dt, event->motion.state);
332             ret = TRUE;
333         }
334         break;
335     case GDK_BUTTON_RELEASE:
336         event_context->xp = event_context->yp = 0;
337         if ( event->button.button == 1 ) {
338             dragging = false;
340             if (!event_context->within_tolerance) {
341                 // we've been dragging, finish the rect
342                 sp_rect_finish(rc);
343             } else if (event_context->item_to_select) {
344                 // no dragging, select clicked item if any
345                 if (event->button.state & GDK_SHIFT_MASK) {
346                     selection->toggle(event_context->item_to_select);
347                 } else {
348                     selection->set(event_context->item_to_select);
349                 }
350             } else {
351                 // click in an empty space
352                 selection->clear();
353             }
355             event_context->item_to_select = NULL;
356             ret = TRUE;
357             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
358                                   event->button.time);
359         }
360         break;
361     case GDK_KEY_PRESS:
362         switch (get_group0_keyval (&event->key)) {
363         case GDK_Alt_L:
364         case GDK_Alt_R:
365         case GDK_Control_L:
366         case GDK_Control_R:
367         case GDK_Shift_L:
368         case GDK_Shift_R:
369         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
370         case GDK_Meta_R:
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             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_Escape:
394             SP_DT_SELECTION(desktop)->clear();
395             //TODO: make dragging escapable by Esc
396         default:
397             break;
398         }
399         break;
400     case GDK_KEY_RELEASE:
401         switch (get_group0_keyval (&event->key)) {
402         case GDK_Alt_L:
403         case GDK_Alt_R:
404         case GDK_Control_L:
405         case GDK_Control_R:
406         case GDK_Shift_L:
407         case GDK_Shift_R:
408         case GDK_Meta_L:  // Meta is when you press Shift+Alt
409         case GDK_Meta_R:
410             event_context->defaultMessageContext()->clear();
411             break;
412         default:
413             break;
414         }
415         break;
416     default:
417         break;
418     }
420     if (!ret) {
421         if (((SPEventContextClass *) parent_class)->root_handler) {
422             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
423         }
424     }
426     return ret;
429 static void sp_rect_drag(SPRectContext &rc, NR::Point const pt, guint state)
431     SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
433     if (!rc.item) {
435         if (Inkscape::have_viable_layer(desktop, rc._message_context) == false) {
436             return;
437         }
439         /* Create object */
440         Inkscape::XML::Node *repr = sp_repr_new("svg:rect");
442         /* Set style */
443         sp_desktop_apply_style_tool (desktop, repr, "tools.shapes.rect", false);
445         rc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
446         Inkscape::GC::release(repr);
447         rc.item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
448         rc.item->updateRepr();
449     }
451     NR::Rect const r = Inkscape::snap_rectangular_box(desktop, rc.item, pt, rc.center, state);
453     sp_rect_position_set(SP_RECT(rc.item), r.min()[NR::X], r.min()[NR::Y], r.dimensions()[NR::X], r.dimensions()[NR::Y]);
454     if ( rc.rx != 0.0 ) {
455         sp_rect_set_rx (SP_RECT(rc.item), TRUE, rc.rx);
456     }
457     if ( rc.ry != 0.0 ) {
458         if (rc.rx == 0.0)
459             sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, MIN(r.dimensions()[NR::X], r.dimensions()[NR::Y])/2));
460         else
461             sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, r.dimensions()[NR::Y]));
462     }
464     // status text
465     GString *xs = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::X], desktop->namedview->getDefaultMetric());
466     GString *ys = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::Y], desktop->namedview->getDefaultMetric());
467     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);
468     g_string_free(xs, FALSE);
469     g_string_free(ys, FALSE);
472 static void sp_rect_finish(SPRectContext *rc)
474     rc->_message_context->clear();
476     if ( rc->item != NULL ) {
477         SPDesktop * dt;
479         dt = SP_EVENT_CONTEXT_DESKTOP(rc);
481         SP_OBJECT(rc->item)->updateRepr();
483         SP_DT_SELECTION(dt)->set(rc->item);
484         sp_document_done(SP_DT_DOCUMENT(dt));
486         rc->item = NULL;
487     }
490 /*
491   Local Variables:
492   mode:c++
493   c-file-style:"stroustrup"
494   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
495   indent-tabs-mode:nil
496   fill-column:99
497   End:
498 */
499 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :