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 "prefs-utils.h"
43 #include "context-fns.h"
45 //static const double goldenratio = 1.61803398874989484820; // golden ratio
47 static void sp_rect_context_class_init(SPRectContextClass *klass);
48 static void sp_rect_context_init(SPRectContext *rect_context);
49 static void sp_rect_context_dispose(GObject *object);
51 static void sp_rect_context_setup(SPEventContext *ec);
52 static void sp_rect_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
54 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event);
55 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
57 static void sp_rect_drag(SPRectContext &rc, NR::Point const pt, guint state);
58 static void sp_rect_finish(SPRectContext *rc);
60 static SPEventContextClass *parent_class;
63 GtkType sp_rect_context_get_type()
64 {
65 static GType type = 0;
66 if (!type) {
67 GTypeInfo info = {
68 sizeof(SPRectContextClass),
69 NULL, NULL,
70 (GClassInitFunc) sp_rect_context_class_init,
71 NULL, NULL,
72 sizeof(SPRectContext),
73 4,
74 (GInstanceInitFunc) sp_rect_context_init,
75 NULL, /* value_table */
76 };
77 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPRectContext", &info, (GTypeFlags) 0);
78 }
79 return type;
80 }
82 static void sp_rect_context_class_init(SPRectContextClass *klass)
83 {
84 GObjectClass *object_class = (GObjectClass *) klass;
85 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
87 parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
89 object_class->dispose = sp_rect_context_dispose;
91 event_context_class->setup = sp_rect_context_setup;
92 event_context_class->set = sp_rect_context_set;
93 event_context_class->root_handler = sp_rect_context_root_handler;
94 event_context_class->item_handler = sp_rect_context_item_handler;
95 }
97 static void sp_rect_context_init(SPRectContext *rect_context)
98 {
99 SPEventContext *event_context = SP_EVENT_CONTEXT(rect_context);
101 event_context->cursor_shape = cursor_rect_xpm;
102 event_context->hot_x = 4;
103 event_context->hot_y = 4;
104 event_context->xp = 0;
105 event_context->yp = 0;
106 event_context->tolerance = 0;
107 event_context->within_tolerance = false;
108 event_context->item_to_select = NULL;
110 event_context->shape_repr = NULL;
111 event_context->shape_knot_holder = NULL;
113 rect_context->item = NULL;
115 rect_context->rx = 0.0;
116 rect_context->ry = 0.0;
118 new (&rect_context->sel_changed_connection) sigc::connection();
119 }
121 static void sp_rect_context_dispose(GObject *object)
122 {
123 SPRectContext *rc = SP_RECT_CONTEXT(object);
124 SPEventContext *ec = SP_EVENT_CONTEXT(object);
126 ec->enableGrDrag(false);
128 rc->sel_changed_connection.disconnect();
129 rc->sel_changed_connection.~connection();
131 /* fixme: This is necessary because we do not grab */
132 if (rc->item) {
133 sp_rect_finish(rc);
134 }
136 if (ec->shape_knot_holder) {
137 delete ec->shape_knot_holder;
138 ec->shape_knot_holder = NULL;
139 }
141 if (ec->shape_repr) { // remove old listener
142 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
143 Inkscape::GC::release(ec->shape_repr);
144 ec->shape_repr = 0;
145 }
147 if (rc->_message_context) {
148 delete rc->_message_context;
149 }
151 G_OBJECT_CLASS(parent_class)->dispose(object);
152 }
154 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
155 NULL, /* child_added */
156 NULL, /* child_removed */
157 ec_shape_event_attr_changed,
158 NULL, /* content_changed */
159 NULL /* order_changed */
160 };
162 /**
163 \brief Callback that processes the "changed" signal on the selection;
164 destroys old and creates new knotholder
165 */
166 void sp_rect_context_selection_changed(Inkscape::Selection *selection, gpointer data)
167 {
168 SPRectContext *rc = SP_RECT_CONTEXT(data);
169 SPEventContext *ec = SP_EVENT_CONTEXT(rc);
171 if (ec->shape_knot_holder) { // destroy knotholder
172 delete ec->shape_knot_holder;
173 ec->shape_knot_holder = NULL;
174 }
176 if (ec->shape_repr) { // remove old listener
177 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
178 Inkscape::GC::release(ec->shape_repr);
179 ec->shape_repr = 0;
180 }
182 SPItem *item = selection->singleItem();
183 if (item) {
184 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
185 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
186 if (shape_repr) {
187 ec->shape_repr = shape_repr;
188 Inkscape::GC::anchor(shape_repr);
189 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
190 }
191 }
192 }
194 static void sp_rect_context_setup(SPEventContext *ec)
195 {
196 SPRectContext *rc = SP_RECT_CONTEXT(ec);
198 if (((SPEventContextClass *) parent_class)->setup) {
199 ((SPEventContextClass *) parent_class)->setup(ec);
200 }
202 SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
203 if (item) {
204 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
205 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
206 if (shape_repr) {
207 ec->shape_repr = shape_repr;
208 Inkscape::GC::anchor(shape_repr);
209 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
210 }
211 }
213 rc->sel_changed_connection.disconnect();
214 rc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
215 sigc::bind(sigc::ptr_fun(&sp_rect_context_selection_changed), (gpointer)rc)
216 );
218 sp_event_context_read(ec, "rx");
219 sp_event_context_read(ec, "ry");
221 if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
222 ec->enableSelectionCue();
223 }
225 if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
226 ec->enableGrDrag();
227 }
229 rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
230 }
232 static void sp_rect_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
233 {
234 SPRectContext *rc = SP_RECT_CONTEXT(ec);
236 /* fixme: Proper error handling for non-numeric data. Use a locale-independent function like
237 * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
238 if ( strcmp(key, "rx") == 0 ) {
239 rc->rx = ( val
240 ? g_ascii_strtod (val, NULL)
241 : 0.0 );
242 } else if ( strcmp(key, "ry") == 0 ) {
243 rc->ry = ( val
244 ? g_ascii_strtod (val, NULL)
245 : 0.0 );
246 }
247 }
249 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
250 {
251 SPDesktop *desktop = event_context->desktop;
253 gint ret = FALSE;
255 switch (event->type) {
256 case GDK_BUTTON_PRESS:
257 if ( event->button.button == 1 && !event_context->space_panning) {
258 Inkscape::setup_for_drag_start(desktop, event_context, event);
259 ret = TRUE;
260 }
261 break;
262 // motion and release are always on root (why?)
263 default:
264 break;
265 }
267 if (((SPEventContextClass *) parent_class)->item_handler) {
268 ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
269 }
271 return ret;
272 }
274 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event)
275 {
276 static bool dragging;
278 SPDesktop *desktop = event_context->desktop;
279 Inkscape::Selection *selection = sp_desktop_selection (desktop);
281 SPRectContext *rc = SP_RECT_CONTEXT(event_context);
283 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
285 gint ret = FALSE;
286 switch (event->type) {
287 case GDK_BUTTON_PRESS:
288 if (event->button.button == 1 && !event_context->space_panning) {
289 NR::Point const button_w(event->button.x,
290 event->button.y);
292 // save drag origin
293 event_context->xp = (gint) button_w[NR::X];
294 event_context->yp = (gint) button_w[NR::Y];
295 event_context->within_tolerance = true;
297 // remember clicked item, disregarding groups, honoring Alt
298 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
300 dragging = true;
302 /* Position center */
303 Geom::Point button_dt(desktop->w2d(button_w));
304 rc->center = from_2geom(button_dt);
306 /* Snap center */
307 SnapManager &m = desktop->namedview->snap_manager;
308 m.setup(desktop);
309 m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, button_dt);
310 rc->center = from_2geom(button_dt);
312 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
313 ( GDK_KEY_PRESS_MASK |
314 GDK_BUTTON_RELEASE_MASK |
315 GDK_POINTER_MOTION_MASK |
316 GDK_POINTER_MOTION_HINT_MASK |
317 GDK_BUTTON_PRESS_MASK ),
318 NULL, event->button.time);
320 ret = TRUE;
321 }
322 break;
323 case GDK_MOTION_NOTIFY:
324 if ( dragging
325 && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning)
326 {
327 if ( event_context->within_tolerance
328 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
329 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
330 break; // do not drag if we're within tolerance from origin
331 }
332 // Once the user has moved farther than tolerance from the original location
333 // (indicating they intend to draw, not click), then always process the
334 // motion notify coordinates as given (no snapping back to origin)
335 event_context->within_tolerance = false;
337 NR::Point const motion_w(event->motion.x, event->motion.y);
338 NR::Point motion_dt(desktop->w2d(motion_w));
340 sp_rect_drag(*rc, motion_dt, event->motion.state); // this will also handle the snapping
341 gobble_motion_events(GDK_BUTTON1_MASK);
342 ret = TRUE;
343 }
344 break;
345 case GDK_BUTTON_RELEASE:
346 event_context->xp = event_context->yp = 0;
347 if (event->button.button == 1 && !event_context->space_panning) {
348 dragging = false;
350 if (!event_context->within_tolerance) {
351 // we've been dragging, finish the rect
352 sp_rect_finish(rc);
353 } else if (event_context->item_to_select) {
354 // no dragging, select clicked item if any
355 if (event->button.state & GDK_SHIFT_MASK) {
356 selection->toggle(event_context->item_to_select);
357 } else {
358 selection->set(event_context->item_to_select);
359 }
360 } else {
361 // click in an empty space
362 selection->clear();
363 }
365 event_context->item_to_select = NULL;
366 ret = TRUE;
367 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
368 event->button.time);
369 }
370 break;
371 case GDK_KEY_PRESS:
372 switch (get_group0_keyval (&event->key)) {
373 case GDK_Alt_L:
374 case GDK_Alt_R:
375 case GDK_Control_L:
376 case GDK_Control_R:
377 case GDK_Shift_L:
378 case GDK_Shift_R:
379 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
380 case GDK_Meta_R:
381 if (!dragging){
382 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
383 _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
384 _("<b>Shift</b>: draw around the starting point"),
385 NULL);
386 }
387 break;
388 case GDK_Up:
389 case GDK_Down:
390 case GDK_KP_Up:
391 case GDK_KP_Down:
392 // prevent the zoom field from activation
393 if (!MOD__CTRL_ONLY)
394 ret = TRUE;
395 break;
397 case GDK_x:
398 case GDK_X:
399 if (MOD__ALT_ONLY) {
400 desktop->setToolboxFocusTo ("altx-rect");
401 ret = TRUE;
402 }
403 break;
405 case GDK_g:
406 case GDK_G:
407 if (MOD__SHIFT_ONLY) {
408 sp_selection_to_guides(desktop);
409 ret = true;
410 }
411 break;
413 case GDK_Escape:
414 sp_desktop_selection(desktop)->clear();
415 //TODO: make dragging escapable by Esc
416 break;
418 case GDK_space:
419 if (dragging) {
420 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
421 event->button.time);
422 dragging = false;
423 if (!event_context->within_tolerance) {
424 // we've been dragging, finish the rect
425 sp_rect_finish(rc);
426 }
427 // do not return true, so that space would work switching to selector
428 }
429 break;
431 default:
432 break;
433 }
434 break;
435 case GDK_KEY_RELEASE:
436 switch (get_group0_keyval (&event->key)) {
437 case GDK_Alt_L:
438 case GDK_Alt_R:
439 case GDK_Control_L:
440 case GDK_Control_R:
441 case GDK_Shift_L:
442 case GDK_Shift_R:
443 case GDK_Meta_L: // Meta is when you press Shift+Alt
444 case GDK_Meta_R:
445 event_context->defaultMessageContext()->clear();
446 break;
447 default:
448 break;
449 }
450 break;
451 default:
452 break;
453 }
455 if (!ret) {
456 if (((SPEventContextClass *) parent_class)->root_handler) {
457 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
458 }
459 }
461 return ret;
462 }
464 static void sp_rect_drag(SPRectContext &rc, NR::Point const pt, guint state)
465 {
466 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
468 if (!rc.item) {
470 if (Inkscape::have_viable_layer(desktop, rc._message_context) == false) {
471 return;
472 }
474 /* Create object */
475 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(&rc));
476 Inkscape::XML::Node *repr = xml_doc->createElement("svg:rect");
478 /* Set style */
479 sp_desktop_apply_style_tool (desktop, repr, "tools.shapes.rect", false);
481 rc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
482 Inkscape::GC::release(repr);
483 rc.item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
484 rc.item->updateRepr();
486 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
487 }
489 Geom::Rect const r = Inkscape::snap_rectangular_box(desktop, rc.item, pt, rc.center, state);
491 sp_rect_position_set(SP_RECT(rc.item), r.min()[Geom::X], r.min()[Geom::Y], r.dimensions()[Geom::X], r.dimensions()[Geom::Y]);
492 if ( rc.rx != 0.0 ) {
493 sp_rect_set_rx (SP_RECT(rc.item), TRUE, rc.rx);
494 }
495 if ( rc.ry != 0.0 ) {
496 if (rc.rx == 0.0)
497 sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, MIN(r.dimensions()[Geom::X], r.dimensions()[Geom::Y])/2));
498 else
499 sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, r.dimensions()[Geom::Y]));
500 }
502 // status text
503 double rdimx = r.dimensions()[Geom::X];
504 double rdimy = r.dimensions()[Geom::Y];
505 GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
506 GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
507 if (state & GDK_CONTROL_MASK) {
508 int ratio_x, ratio_y;
509 bool is_golden_ratio = false;
510 if (fabs (rdimx) > fabs (rdimy)) {
511 if (fabs(rdimx / rdimy - goldenratio) < 1e-6) {
512 is_golden_ratio = true;
513 }
514 ratio_x = (int) rint (rdimx / rdimy);
515 ratio_y = 1;
516 } else {
517 if (fabs(rdimy / rdimx - goldenratio) < 1e-6) {
518 is_golden_ratio = true;
519 }
520 ratio_x = 1;
521 ratio_y = (int) rint (rdimy / rdimx);
522 }
523 if (!is_golden_ratio) {
524 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);
525 } else {
526 if (ratio_y == 1) {
527 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);
528 } else {
529 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);
530 }
531 }
532 } else {
533 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);
534 }
535 g_string_free(xs, FALSE);
536 g_string_free(ys, FALSE);
537 }
539 static void sp_rect_finish(SPRectContext *rc)
540 {
541 rc->_message_context->clear();
543 if ( rc->item != NULL ) {
544 SPDesktop * desktop;
546 desktop = SP_EVENT_CONTEXT_DESKTOP(rc);
548 SP_OBJECT(rc->item)->updateRepr();
550 sp_canvas_end_forced_full_redraws(desktop->canvas);
552 sp_desktop_selection(desktop)->set(rc->item);
553 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_RECT,
554 _("Create rectangle"));
556 rc->item = NULL;
557 }
558 }
560 /*
561 Local Variables:
562 mode:c++
563 c-file-style:"stroustrup"
564 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
565 indent-tabs-mode:nil
566 fill-column:99
567 End:
568 */
569 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :