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>
21 #include "macros.h"
22 #include "display/sp-canvas.h"
23 #include "sp-rect.h"
24 #include "document.h"
25 #include "sp-namedview.h"
26 #include "selection.h"
27 #include "desktop-handles.h"
28 #include "snap.h"
29 #include "desktop.h"
30 #include "desktop-style.h"
31 #include "message-context.h"
32 #include "pixmaps/cursor-rect.xpm"
33 #include "rect-context.h"
34 #include "sp-metrics.h"
35 #include <glibmm/i18n.h>
36 #include "object-edit.h"
37 #include "xml/repr.h"
38 #include "xml/node-event-vector.h"
39 #include "prefs-utils.h"
40 #include "context-fns.h"
42 static void sp_rect_context_class_init(SPRectContextClass *klass);
43 static void sp_rect_context_init(SPRectContext *rect_context);
44 static void sp_rect_context_dispose(GObject *object);
46 static void sp_rect_context_setup(SPEventContext *ec);
47 static void sp_rect_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
49 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event);
50 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
52 static void sp_rect_drag(SPRectContext &rc, NR::Point const pt, guint state);
53 static void sp_rect_finish(SPRectContext *rc);
55 static SPEventContextClass *parent_class;
58 GtkType sp_rect_context_get_type()
59 {
60 static GType type = 0;
61 if (!type) {
62 GTypeInfo info = {
63 sizeof(SPRectContextClass),
64 NULL, NULL,
65 (GClassInitFunc) sp_rect_context_class_init,
66 NULL, NULL,
67 sizeof(SPRectContext),
68 4,
69 (GInstanceInitFunc) sp_rect_context_init,
70 NULL, /* value_table */
71 };
72 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPRectContext", &info, (GTypeFlags) 0);
73 }
74 return type;
75 }
77 static void sp_rect_context_class_init(SPRectContextClass *klass)
78 {
79 GObjectClass *object_class = (GObjectClass *) klass;
80 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
82 parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
84 object_class->dispose = sp_rect_context_dispose;
86 event_context_class->setup = sp_rect_context_setup;
87 event_context_class->set = sp_rect_context_set;
88 event_context_class->root_handler = sp_rect_context_root_handler;
89 event_context_class->item_handler = sp_rect_context_item_handler;
90 }
92 static void sp_rect_context_init(SPRectContext *rect_context)
93 {
94 SPEventContext *event_context = SP_EVENT_CONTEXT(rect_context);
96 event_context->cursor_shape = cursor_rect_xpm;
97 event_context->hot_x = 4;
98 event_context->hot_y = 4;
99 event_context->xp = 0;
100 event_context->yp = 0;
101 event_context->tolerance = 0;
102 event_context->within_tolerance = false;
103 event_context->item_to_select = NULL;
105 event_context->shape_repr = NULL;
106 event_context->shape_knot_holder = NULL;
108 rect_context->item = NULL;
110 rect_context->rx = 0.0;
111 rect_context->ry = 0.0;
113 new (&rect_context->sel_changed_connection) sigc::connection();
114 }
116 static void sp_rect_context_dispose(GObject *object)
117 {
118 SPRectContext *rc = SP_RECT_CONTEXT(object);
119 SPEventContext *ec = SP_EVENT_CONTEXT(object);
121 ec->enableGrDrag(false);
123 rc->sel_changed_connection.disconnect();
124 rc->sel_changed_connection.~connection();
126 /* fixme: This is necessary because we do not grab */
127 if (rc->item) {
128 sp_rect_finish(rc);
129 }
131 if (ec->shape_knot_holder) {
132 sp_knot_holder_destroy(ec->shape_knot_holder);
133 ec->shape_knot_holder = NULL;
134 }
136 if (ec->shape_repr) { // remove old listener
137 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
138 Inkscape::GC::release(ec->shape_repr);
139 ec->shape_repr = 0;
140 }
142 if (rc->_message_context) {
143 delete rc->_message_context;
144 }
146 G_OBJECT_CLASS(parent_class)->dispose(object);
147 }
149 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
150 NULL, /* child_added */
151 NULL, /* child_removed */
152 ec_shape_event_attr_changed,
153 NULL, /* content_changed */
154 NULL /* order_changed */
155 };
157 /**
158 \brief Callback that processes the "changed" signal on the selection;
159 destroys old and creates new knotholder
160 */
161 void sp_rect_context_selection_changed(Inkscape::Selection *selection, gpointer data)
162 {
163 SPRectContext *rc = SP_RECT_CONTEXT(data);
164 SPEventContext *ec = SP_EVENT_CONTEXT(rc);
166 if (ec->shape_knot_holder) { // destroy knotholder
167 sp_knot_holder_destroy(ec->shape_knot_holder);
168 ec->shape_knot_holder = NULL;
169 }
171 if (ec->shape_repr) { // remove old listener
172 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
173 Inkscape::GC::release(ec->shape_repr);
174 ec->shape_repr = 0;
175 }
177 SPItem *item = selection->singleItem();
178 if (item) {
179 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
180 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
181 if (shape_repr) {
182 ec->shape_repr = shape_repr;
183 Inkscape::GC::anchor(shape_repr);
184 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
185 }
186 }
187 }
189 static void sp_rect_context_setup(SPEventContext *ec)
190 {
191 SPRectContext *rc = SP_RECT_CONTEXT(ec);
193 if (((SPEventContextClass *) parent_class)->setup) {
194 ((SPEventContextClass *) parent_class)->setup(ec);
195 }
197 SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
198 if (item) {
199 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
200 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
201 if (shape_repr) {
202 ec->shape_repr = shape_repr;
203 Inkscape::GC::anchor(shape_repr);
204 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
205 }
206 }
208 rc->sel_changed_connection.disconnect();
209 rc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
210 sigc::bind(sigc::ptr_fun(&sp_rect_context_selection_changed), (gpointer)rc)
211 );
213 sp_event_context_read(ec, "rx");
214 sp_event_context_read(ec, "ry");
216 if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
217 ec->enableSelectionCue();
218 }
220 if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
221 ec->enableGrDrag();
222 }
224 rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
225 }
227 static void sp_rect_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
228 {
229 SPRectContext *rc = SP_RECT_CONTEXT(ec);
231 /* fixme: Proper error handling for non-numeric data. Use a locale-independent function like
232 * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
233 if ( strcmp(key, "rx") == 0 ) {
234 rc->rx = ( val
235 ? g_ascii_strtod (val, NULL)
236 : 0.0 );
237 } else if ( strcmp(key, "ry") == 0 ) {
238 rc->ry = ( val
239 ? g_ascii_strtod (val, NULL)
240 : 0.0 );
241 }
242 }
244 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
245 {
246 SPDesktop *desktop = event_context->desktop;
248 gint ret = FALSE;
250 switch (event->type) {
251 case GDK_BUTTON_PRESS:
252 if ( event->button.button == 1 ) {
253 Inkscape::setup_for_drag_start(desktop, event_context, event);
254 ret = TRUE;
255 }
256 break;
257 // motion and release are always on root (why?)
258 default:
259 break;
260 }
262 if (((SPEventContextClass *) parent_class)->item_handler) {
263 ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
264 }
266 return ret;
267 }
269 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event)
270 {
271 static bool dragging;
273 SPDesktop *desktop = event_context->desktop;
274 Inkscape::Selection *selection = sp_desktop_selection (desktop);
276 SPRectContext *rc = SP_RECT_CONTEXT(event_context);
278 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
280 gint ret = FALSE;
281 switch (event->type) {
282 case GDK_BUTTON_PRESS:
283 if ( event->button.button == 1 ) {
284 NR::Point const button_w(event->button.x,
285 event->button.y);
287 // save drag origin
288 event_context->xp = (gint) button_w[NR::X];
289 event_context->yp = (gint) button_w[NR::Y];
290 event_context->within_tolerance = true;
292 // remember clicked item, disregarding groups, honoring Alt
293 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
295 dragging = true;
297 /* Position center */
298 NR::Point const button_dt(desktop->w2d(button_w));
300 /* Snap center */
301 SnapManager const &m = desktop->namedview->snap_manager;
302 rc->center = m.freeSnap(Inkscape::Snapper::SNAP_POINT | Inkscape::Snapper::BBOX_POINT,
303 button_dt, rc->item).getPoint();
305 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
306 ( GDK_KEY_PRESS_MASK |
307 GDK_BUTTON_RELEASE_MASK |
308 GDK_POINTER_MOTION_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 if (!dragging){
372 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
373 _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
374 _("<b>Shift</b>: draw around the starting point"),
375 NULL);
376 }
377 break;
378 case GDK_Up:
379 case GDK_Down:
380 case GDK_KP_Up:
381 case GDK_KP_Down:
382 // prevent the zoom field from activation
383 if (!MOD__CTRL_ONLY)
384 ret = TRUE;
385 break;
387 case GDK_x:
388 case GDK_X:
389 if (MOD__ALT_ONLY) {
390 desktop->setToolboxFocusTo ("altx-rect");
391 ret = TRUE;
392 }
393 break;
395 case GDK_Escape:
396 sp_desktop_selection(desktop)->clear();
397 //TODO: make dragging escapable by Esc
398 default:
399 break;
400 }
401 break;
402 case GDK_KEY_RELEASE:
403 switch (get_group0_keyval (&event->key)) {
404 case GDK_Alt_L:
405 case GDK_Alt_R:
406 case GDK_Control_L:
407 case GDK_Control_R:
408 case GDK_Shift_L:
409 case GDK_Shift_R:
410 case GDK_Meta_L: // Meta is when you press Shift+Alt
411 case GDK_Meta_R:
412 event_context->defaultMessageContext()->clear();
413 break;
414 default:
415 break;
416 }
417 break;
418 default:
419 break;
420 }
422 if (!ret) {
423 if (((SPEventContextClass *) parent_class)->root_handler) {
424 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
425 }
426 }
428 return ret;
429 }
431 static void sp_rect_drag(SPRectContext &rc, NR::Point const pt, guint state)
432 {
433 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
435 if (!rc.item) {
437 if (Inkscape::have_viable_layer(desktop, rc._message_context) == false) {
438 return;
439 }
441 /* Create object */
442 Inkscape::XML::Node *repr = sp_repr_new("svg:rect");
444 /* Set style */
445 sp_desktop_apply_style_tool (desktop, repr, "tools.shapes.rect", false);
447 rc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
448 Inkscape::GC::release(repr);
449 rc.item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
450 rc.item->updateRepr();
452 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
453 }
455 NR::Rect const r = Inkscape::snap_rectangular_box(desktop, rc.item, pt, rc.center, state);
457 sp_rect_position_set(SP_RECT(rc.item), r.min()[NR::X], r.min()[NR::Y], r.dimensions()[NR::X], r.dimensions()[NR::Y]);
458 if ( rc.rx != 0.0 ) {
459 sp_rect_set_rx (SP_RECT(rc.item), TRUE, rc.rx);
460 }
461 if ( rc.ry != 0.0 ) {
462 if (rc.rx == 0.0)
463 sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, MIN(r.dimensions()[NR::X], r.dimensions()[NR::Y])/2));
464 else
465 sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, r.dimensions()[NR::Y]));
466 }
468 // status text
469 GString *xs = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::X], desktop->namedview->getDefaultMetric());
470 GString *ys = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::Y], desktop->namedview->getDefaultMetric());
471 rc._message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>Rectangle</b>: %s × %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);
472 g_string_free(xs, FALSE);
473 g_string_free(ys, FALSE);
474 }
476 static void sp_rect_finish(SPRectContext *rc)
477 {
478 rc->_message_context->clear();
480 if ( rc->item != NULL ) {
481 SPDesktop * desktop;
483 desktop = SP_EVENT_CONTEXT_DESKTOP(rc);
485 SP_OBJECT(rc->item)->updateRepr();
487 sp_canvas_end_forced_full_redraws(desktop->canvas);
489 sp_desktop_selection(desktop)->set(rc->item);
490 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_RECT,
491 _("Create rectangle"));
493 rc->item = NULL;
494 }
495 }
497 /*
498 Local Variables:
499 mode:c++
500 c-file-style:"stroustrup"
501 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
502 indent-tabs-mode:nil
503 fill-column:99
504 End:
505 */
506 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :