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 "preferences.h"
43 #include "context-fns.h"
44 #include "shape-editor.h"
46 //static const double goldenratio = 1.61803398874989484820; // golden ratio
48 static void sp_rect_context_class_init(SPRectContextClass *klass);
49 static void sp_rect_context_init(SPRectContext *rect_context);
50 static void sp_rect_context_dispose(GObject *object);
52 static void sp_rect_context_setup(SPEventContext *ec);
53 static void sp_rect_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
55 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event);
56 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
58 static void sp_rect_drag(SPRectContext &rc, Geom::Point const pt, guint state);
59 static void sp_rect_finish(SPRectContext *rc);
60 static void sp_rect_cancel(SPRectContext *rc);
62 static SPEventContextClass *parent_class;
65 GtkType sp_rect_context_get_type()
66 {
67 static GType type = 0;
68 if (!type) {
69 GTypeInfo info = {
70 sizeof(SPRectContextClass),
71 NULL, NULL,
72 (GClassInitFunc) sp_rect_context_class_init,
73 NULL, NULL,
74 sizeof(SPRectContext),
75 4,
76 (GInstanceInitFunc) sp_rect_context_init,
77 NULL, /* value_table */
78 };
79 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPRectContext", &info, (GTypeFlags) 0);
80 }
81 return type;
82 }
84 static void sp_rect_context_class_init(SPRectContextClass *klass)
85 {
86 GObjectClass *object_class = (GObjectClass *) klass;
87 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
89 parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
91 object_class->dispose = sp_rect_context_dispose;
93 event_context_class->setup = sp_rect_context_setup;
94 event_context_class->set = sp_rect_context_set;
95 event_context_class->root_handler = sp_rect_context_root_handler;
96 event_context_class->item_handler = sp_rect_context_item_handler;
97 }
99 static void sp_rect_context_init(SPRectContext *rect_context)
100 {
101 SPEventContext *event_context = SP_EVENT_CONTEXT(rect_context);
103 event_context->cursor_shape = cursor_rect_xpm;
104 event_context->hot_x = 4;
105 event_context->hot_y = 4;
106 event_context->xp = 0;
107 event_context->yp = 0;
108 event_context->tolerance = 0;
109 event_context->within_tolerance = false;
110 event_context->item_to_select = NULL;
112 rect_context->item = NULL;
114 rect_context->rx = 0.0;
115 rect_context->ry = 0.0;
117 new (&rect_context->sel_changed_connection) sigc::connection();
118 }
120 static void sp_rect_context_dispose(GObject *object)
121 {
122 SPRectContext *rc = SP_RECT_CONTEXT(object);
123 SPEventContext *ec = SP_EVENT_CONTEXT(object);
125 ec->enableGrDrag(false);
127 rc->sel_changed_connection.disconnect();
128 rc->sel_changed_connection.~connection();
130 delete ec->shape_editor;
131 ec->shape_editor = NULL;
133 /* fixme: This is necessary because we do not grab */
134 if (rc->item) {
135 sp_rect_finish(rc);
136 }
138 if (rc->_message_context) {
139 delete rc->_message_context;
140 }
142 G_OBJECT_CLASS(parent_class)->dispose(object);
143 }
145 /**
146 \brief Callback that processes the "changed" signal on the selection;
147 destroys old and creates new knotholder
148 */
149 void sp_rect_context_selection_changed(Inkscape::Selection *selection, gpointer data)
150 {
151 SPRectContext *rc = SP_RECT_CONTEXT(data);
152 SPEventContext *ec = SP_EVENT_CONTEXT(rc);
154 ec->shape_editor->unset_item(SH_KNOTHOLDER);
155 SPItem *item = selection->singleItem();
156 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
157 }
159 static void sp_rect_context_setup(SPEventContext *ec)
160 {
161 SPRectContext *rc = SP_RECT_CONTEXT(ec);
163 if (((SPEventContextClass *) parent_class)->setup) {
164 ((SPEventContextClass *) parent_class)->setup(ec);
165 }
167 ec->shape_editor = new ShapeEditor(ec->desktop);
169 SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
170 if (item) {
171 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
172 }
174 rc->sel_changed_connection.disconnect();
175 rc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
176 sigc::bind(sigc::ptr_fun(&sp_rect_context_selection_changed), (gpointer)rc)
177 );
179 sp_event_context_read(ec, "rx");
180 sp_event_context_read(ec, "ry");
182 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
183 if (prefs->getBool("/tools/shapes/selcue")) {
184 ec->enableSelectionCue();
185 }
187 if (prefs->getBool("/tools/shapes/gradientdrag")) {
188 ec->enableGrDrag();
189 }
191 rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
192 }
194 static void sp_rect_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
195 {
196 SPRectContext *rc = SP_RECT_CONTEXT(ec);
198 /* fixme: Proper error handling for non-numeric data. Use a locale-independent function like
199 * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
200 Glib::ustring name = val->getEntryName();
201 if ( name == "rx" ) {
202 rc->rx = val->getDoubleLimited(); // prevents NaN and +/-Inf from messing up
203 } else if ( name == "ry" ) {
204 rc->ry = val->getDoubleLimited();
205 }
206 }
208 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
209 {
210 SPDesktop *desktop = event_context->desktop;
212 gint ret = FALSE;
214 switch (event->type) {
215 case GDK_BUTTON_PRESS:
216 if ( event->button.button == 1 && !event_context->space_panning) {
217 Inkscape::setup_for_drag_start(desktop, event_context, event);
218 ret = TRUE;
219 }
220 break;
221 // motion and release are always on root (why?)
222 default:
223 break;
224 }
226 if (((SPEventContextClass *) parent_class)->item_handler) {
227 ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
228 }
230 return ret;
231 }
233 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event)
234 {
235 static bool dragging;
237 SPDesktop *desktop = event_context->desktop;
238 Inkscape::Selection *selection = sp_desktop_selection (desktop);
240 SPRectContext *rc = SP_RECT_CONTEXT(event_context);
241 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
243 event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
245 gint ret = FALSE;
246 switch (event->type) {
247 case GDK_BUTTON_PRESS:
248 if (event->button.button == 1 && !event_context->space_panning) {
249 Geom::Point const button_w(event->button.x,
250 event->button.y);
252 // save drag origin
253 event_context->xp = (gint) button_w[Geom::X];
254 event_context->yp = (gint) button_w[Geom::Y];
255 event_context->within_tolerance = true;
257 // remember clicked item, disregarding groups, honoring Alt
258 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
260 dragging = true;
262 /* Position center */
263 Geom::Point button_dt(desktop->w2d(button_w));
264 rc->center = from_2geom(button_dt);
266 /* Snap center */
267 SnapManager &m = desktop->namedview->snap_manager;
268 m.setup(desktop);
269 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, button_dt, Inkscape::SNAPSOURCE_HANDLE);
270 rc->center = from_2geom(button_dt);
272 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
273 ( GDK_KEY_PRESS_MASK |
274 GDK_BUTTON_RELEASE_MASK |
275 GDK_POINTER_MOTION_MASK |
276 GDK_POINTER_MOTION_HINT_MASK |
277 GDK_BUTTON_PRESS_MASK ),
278 NULL, event->button.time);
280 ret = TRUE;
281 }
282 break;
283 case GDK_MOTION_NOTIFY:
284 if ( dragging
285 && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning)
286 {
287 if ( event_context->within_tolerance
288 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
289 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
290 break; // do not drag if we're within tolerance from origin
291 }
292 // Once the user has moved farther than tolerance from the original location
293 // (indicating they intend to draw, not click), then always process the
294 // motion notify coordinates as given (no snapping back to origin)
295 event_context->within_tolerance = false;
297 Geom::Point const motion_w(event->motion.x, event->motion.y);
298 Geom::Point motion_dt(desktop->w2d(motion_w));
300 sp_rect_drag(*rc, motion_dt, event->motion.state); // this will also handle the snapping
301 gobble_motion_events(GDK_BUTTON1_MASK);
302 ret = TRUE;
303 }
304 break;
305 case GDK_BUTTON_RELEASE:
306 event_context->xp = event_context->yp = 0;
307 if (event->button.button == 1 && !event_context->space_panning) {
308 dragging = false;
309 sp_event_context_discard_delayed_snap_event(event_context);
311 if (!event_context->within_tolerance) {
312 // we've been dragging, finish the rect
313 sp_rect_finish(rc);
314 } else if (event_context->item_to_select) {
315 // no dragging, select clicked item if any
316 if (event->button.state & GDK_SHIFT_MASK) {
317 selection->toggle(event_context->item_to_select);
318 } else {
319 selection->set(event_context->item_to_select);
320 }
321 } else {
322 // click in an empty space
323 selection->clear();
324 }
326 event_context->item_to_select = NULL;
327 ret = TRUE;
328 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
329 event->button.time);
330 }
331 break;
332 case GDK_KEY_PRESS:
333 switch (get_group0_keyval (&event->key)) {
334 case GDK_Alt_L:
335 case GDK_Alt_R:
336 case GDK_Control_L:
337 case GDK_Control_R:
338 case GDK_Shift_L:
339 case GDK_Shift_R:
340 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
341 case GDK_Meta_R:
342 if (!dragging){
343 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
344 _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
345 _("<b>Shift</b>: draw around the starting point"),
346 NULL);
347 }
348 break;
349 case GDK_Up:
350 case GDK_Down:
351 case GDK_KP_Up:
352 case GDK_KP_Down:
353 // prevent the zoom field from activation
354 if (!MOD__CTRL_ONLY)
355 ret = TRUE;
356 break;
358 case GDK_x:
359 case GDK_X:
360 if (MOD__ALT_ONLY) {
361 desktop->setToolboxFocusTo ("altx-rect");
362 ret = TRUE;
363 }
364 break;
366 case GDK_g:
367 case GDK_G:
368 if (MOD__SHIFT_ONLY) {
369 sp_selection_to_guides(desktop);
370 ret = true;
371 }
372 break;
374 case GDK_Escape:
375 if (dragging) {
376 dragging = false;
377 sp_event_context_discard_delayed_snap_event(event_context);
378 // if drawing, cancel, otherwise pass it up for deselecting
379 sp_rect_cancel(rc);
380 ret = TRUE;
381 }
382 break;
384 case GDK_space:
385 if (dragging) {
386 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
387 event->button.time);
388 dragging = false;
389 sp_event_context_discard_delayed_snap_event(event_context);
390 if (!event_context->within_tolerance) {
391 // we've been dragging, finish the rect
392 sp_rect_finish(rc);
393 }
394 // do not return true, so that space would work switching to selector
395 }
396 break;
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, Geom::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::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(&rc));
443 Inkscape::XML::Node *repr = xml_doc->createElement("svg:rect");
445 /* Set style */
446 sp_desktop_apply_style_tool (desktop, repr, "/tools/shapes/rect", false);
448 rc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
449 Inkscape::GC::release(repr);
450 rc.item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
451 rc.item->updateRepr();
453 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
454 }
456 Geom::Rect const r = Inkscape::snap_rectangular_box(desktop, rc.item, pt, rc.center, state);
458 sp_rect_position_set(SP_RECT(rc.item), r.min()[Geom::X], r.min()[Geom::Y], r.dimensions()[Geom::X], r.dimensions()[Geom::Y]);
459 if ( rc.rx != 0.0 ) {
460 sp_rect_set_rx (SP_RECT(rc.item), TRUE, rc.rx);
461 }
462 if ( rc.ry != 0.0 ) {
463 if (rc.rx == 0.0)
464 sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, MIN(r.dimensions()[Geom::X], r.dimensions()[Geom::Y])/2));
465 else
466 sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, r.dimensions()[Geom::Y]));
467 }
469 // status text
470 double rdimx = r.dimensions()[Geom::X];
471 double rdimy = r.dimensions()[Geom::Y];
472 GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
473 GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
474 if (state & GDK_CONTROL_MASK) {
475 int ratio_x, ratio_y;
476 bool is_golden_ratio = false;
477 if (fabs (rdimx) > fabs (rdimy)) {
478 if (fabs(rdimx / rdimy - goldenratio) < 1e-6) {
479 is_golden_ratio = true;
480 }
481 ratio_x = (int) rint (rdimx / rdimy);
482 ratio_y = 1;
483 } else {
484 if (fabs(rdimy / rdimx - goldenratio) < 1e-6) {
485 is_golden_ratio = true;
486 }
487 ratio_x = 1;
488 ratio_y = (int) rint (rdimy / rdimx);
489 }
490 if (!is_golden_ratio) {
491 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);
492 } else {
493 if (ratio_y == 1) {
494 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);
495 } else {
496 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);
497 }
498 }
499 } else {
500 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);
501 }
502 g_string_free(xs, FALSE);
503 g_string_free(ys, FALSE);
504 }
506 static void sp_rect_finish(SPRectContext *rc)
507 {
508 rc->_message_context->clear();
510 if ( rc->item != NULL ) {
511 SPRect *rect = SP_RECT(rc->item);
512 if (rect->width.computed == 0 || rect->height.computed == 0) {
513 sp_rect_cancel(rc); // Don't allow the creating of zero sized rectangle, for example when the start and and point snap to the snap grid point
514 return;
515 }
517 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(rc);
519 SP_OBJECT(rc->item)->updateRepr();
521 sp_canvas_end_forced_full_redraws(desktop->canvas);
523 sp_desktop_selection(desktop)->set(rc->item);
524 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_RECT,
525 _("Create rectangle"));
527 rc->item = NULL;
528 }
529 }
531 static void sp_rect_cancel(SPRectContext *rc)
532 {
533 SPDesktop *desktop = SP_EVENT_CONTEXT(rc)->desktop;
535 sp_desktop_selection(desktop)->clear();
536 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
538 if (rc->item != NULL) {
539 SP_OBJECT(rc->item)->deleteObject();
540 rc->item = NULL;
541 }
543 rc->within_tolerance = false;
544 rc->xp = 0;
545 rc->yp = 0;
546 rc->item_to_select = NULL;
548 sp_canvas_end_forced_full_redraws(desktop->canvas);
550 sp_document_cancel(sp_desktop_document(desktop));
551 }
554 /*
555 Local Variables:
556 mode:c++
557 c-file-style:"stroustrup"
558 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
559 indent-tabs-mode:nil
560 fill-column:99
561 End:
562 */
563 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :