Code

add test for config
[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_desktop_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_desktop_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_desktop_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->snap_manager;
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_BUTTON_PRESS_MASK ),
309                                 NULL, event->button.time);
310             ret = TRUE;
311         }
312         break;
313     case GDK_MOTION_NOTIFY:
314         if ( dragging
315              && ( event->motion.state & GDK_BUTTON1_MASK ) )
316         {
317             if ( event_context->within_tolerance
318                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
319                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
320                 break; // do not drag if we're within tolerance from origin
321             }
322             // Once the user has moved farther than tolerance from the original location
323             // (indicating they intend to draw, not click), then always process the
324             // motion notify coordinates as given (no snapping back to origin)
325             event_context->within_tolerance = false;
327             NR::Point const motion_w(event->motion.x,
328                                      event->motion.y);
329             NR::Point const motion_dt(desktop->w2d(motion_w));
330             sp_rect_drag(*rc, motion_dt, event->motion.state);
331             ret = TRUE;
332         }
333         break;
334     case GDK_BUTTON_RELEASE:
335         event_context->xp = event_context->yp = 0;
336         if ( event->button.button == 1 ) {
337             dragging = false;
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             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             break;
375         case GDK_Up:
376         case GDK_Down:
377         case GDK_KP_Up:
378         case GDK_KP_Down:
379             // prevent the zoom field from activation
380             if (!MOD__CTRL_ONLY)
381                 ret = TRUE;
382             break;
384         case GDK_x:
385         case GDK_X:
386             if (MOD__ALT_ONLY) {
387                 desktop->setToolboxFocusTo ("altx-rect");
388                 ret = TRUE;
389             }
390             break;
392         case GDK_Escape:
393             sp_desktop_selection(desktop)->clear();
394             //TODO: make dragging escapable by Esc
395         default:
396             break;
397         }
398         break;
399     case GDK_KEY_RELEASE:
400         switch (get_group0_keyval (&event->key)) {
401         case GDK_Alt_L:
402         case GDK_Alt_R:
403         case GDK_Control_L:
404         case GDK_Control_R:
405         case GDK_Shift_L:
406         case GDK_Shift_R:
407         case GDK_Meta_L:  // Meta is when you press Shift+Alt
408         case GDK_Meta_R:
409             event_context->defaultMessageContext()->clear();
410             break;
411         default:
412             break;
413         }
414         break;
415     default:
416         break;
417     }
419     if (!ret) {
420         if (((SPEventContextClass *) parent_class)->root_handler) {
421             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
422         }
423     }
425     return ret;
428 static void sp_rect_drag(SPRectContext &rc, NR::Point const pt, guint state)
430     SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
432     if (!rc.item) {
434         if (Inkscape::have_viable_layer(desktop, rc._message_context) == false) {
435             return;
436         }
438         /* Create object */
439         Inkscape::XML::Node *repr = sp_repr_new("svg:rect");
441         /* Set style */
442         sp_desktop_apply_style_tool (desktop, repr, "tools.shapes.rect", false);
444         rc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
445         Inkscape::GC::release(repr);
446         rc.item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
447         rc.item->updateRepr();
448     }
450     NR::Rect const r = Inkscape::snap_rectangular_box(desktop, rc.item, pt, rc.center, state);
452     sp_rect_position_set(SP_RECT(rc.item), r.min()[NR::X], r.min()[NR::Y], r.dimensions()[NR::X], r.dimensions()[NR::Y]);
453     if ( rc.rx != 0.0 ) {
454         sp_rect_set_rx (SP_RECT(rc.item), TRUE, rc.rx);
455     }
456     if ( rc.ry != 0.0 ) {
457         if (rc.rx == 0.0)
458             sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, MIN(r.dimensions()[NR::X], r.dimensions()[NR::Y])/2));
459         else
460             sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, r.dimensions()[NR::Y]));
461     }
463     // status text
464     GString *xs = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::X], desktop->namedview->getDefaultMetric());
465     GString *ys = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::Y], desktop->namedview->getDefaultMetric());
466     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);
467     g_string_free(xs, FALSE);
468     g_string_free(ys, FALSE);
471 static void sp_rect_finish(SPRectContext *rc)
473     rc->_message_context->clear();
475     if ( rc->item != NULL ) {
476         SPDesktop * dt;
478         dt = SP_EVENT_CONTEXT_DESKTOP(rc);
480         SP_OBJECT(rc->item)->updateRepr();
482         sp_desktop_selection(dt)->set(rc->item);
483         sp_document_done(sp_desktop_document(dt));
485         rc->item = NULL;
486     }
489 /*
490   Local Variables:
491   mode:c++
492   c-file-style:"stroustrup"
493   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
494   indent-tabs-mode:nil
495   fill-column:99
496   End:
497 */
498 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :