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