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_finish(SPEventContext *ec);
54 static void sp_rect_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
56 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event);
57 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
59 static void sp_rect_drag(SPRectContext &rc, Geom::Point const pt, guint state);
60 static void sp_rect_finish(SPRectContext *rc);
61 static void sp_rect_cancel(SPRectContext *rc);
63 static SPEventContextClass *parent_class;
66 GtkType sp_rect_context_get_type()
67 {
68 static GType type = 0;
69 if (!type) {
70 GTypeInfo info = {
71 sizeof(SPRectContextClass),
72 NULL, NULL,
73 (GClassInitFunc) sp_rect_context_class_init,
74 NULL, NULL,
75 sizeof(SPRectContext),
76 4,
77 (GInstanceInitFunc) sp_rect_context_init,
78 NULL, /* value_table */
79 };
80 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPRectContext", &info, (GTypeFlags) 0);
81 }
82 return type;
83 }
85 static void sp_rect_context_class_init(SPRectContextClass *klass)
86 {
87 GObjectClass *object_class = (GObjectClass *) klass;
88 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
90 parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
92 object_class->dispose = sp_rect_context_dispose;
94 event_context_class->setup = sp_rect_context_setup;
95 event_context_class->finish = sp_rect_context_finish;
96 event_context_class->set = sp_rect_context_set;
97 event_context_class->root_handler = sp_rect_context_root_handler;
98 event_context_class->item_handler = sp_rect_context_item_handler;
99 }
101 static void sp_rect_context_init(SPRectContext *rect_context)
102 {
103 SPEventContext *event_context = SP_EVENT_CONTEXT(rect_context);
105 event_context->cursor_shape = cursor_rect_xpm;
106 event_context->hot_x = 4;
107 event_context->hot_y = 4;
108 event_context->xp = 0;
109 event_context->yp = 0;
110 event_context->tolerance = 0;
111 event_context->within_tolerance = false;
112 event_context->item_to_select = NULL;
114 rect_context->item = NULL;
116 rect_context->rx = 0.0;
117 rect_context->ry = 0.0;
119 new (&rect_context->sel_changed_connection) sigc::connection();
120 }
122 static void sp_rect_context_finish(SPEventContext *ec)
123 {
124 SPRectContext *rc = SP_RECT_CONTEXT(ec);
125 SPDesktop *desktop = ec->desktop;
127 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), GDK_CURRENT_TIME);
128 sp_rect_finish(rc);
129 rc->sel_changed_connection.disconnect();
131 if (((SPEventContextClass *) parent_class)->finish) {
132 ((SPEventContextClass *) parent_class)->finish(ec);
133 }
134 }
137 static void sp_rect_context_dispose(GObject *object)
138 {
139 SPRectContext *rc = SP_RECT_CONTEXT(object);
140 SPEventContext *ec = SP_EVENT_CONTEXT(object);
142 ec->enableGrDrag(false);
144 rc->sel_changed_connection.disconnect();
145 rc->sel_changed_connection.~connection();
147 delete ec->shape_editor;
148 ec->shape_editor = NULL;
150 /* fixme: This is necessary because we do not grab */
151 if (rc->item) {
152 sp_rect_finish(rc);
153 }
155 if (rc->_message_context) {
156 delete rc->_message_context;
157 }
159 G_OBJECT_CLASS(parent_class)->dispose(object);
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 ec->shape_editor->unset_item(SH_KNOTHOLDER);
172 SPItem *item = selection->singleItem();
173 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
174 }
176 static void sp_rect_context_setup(SPEventContext *ec)
177 {
178 SPRectContext *rc = SP_RECT_CONTEXT(ec);
180 if (((SPEventContextClass *) parent_class)->setup) {
181 ((SPEventContextClass *) parent_class)->setup(ec);
182 }
184 ec->shape_editor = new ShapeEditor(ec->desktop);
186 SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
187 if (item) {
188 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
189 }
191 rc->sel_changed_connection.disconnect();
192 rc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
193 sigc::bind(sigc::ptr_fun(&sp_rect_context_selection_changed), (gpointer)rc)
194 );
196 sp_event_context_read(ec, "rx");
197 sp_event_context_read(ec, "ry");
199 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
200 if (prefs->getBool("/tools/shapes/selcue")) {
201 ec->enableSelectionCue();
202 }
204 if (prefs->getBool("/tools/shapes/gradientdrag")) {
205 ec->enableGrDrag();
206 }
208 rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
209 }
211 static void sp_rect_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
212 {
213 SPRectContext *rc = SP_RECT_CONTEXT(ec);
215 /* fixme: Proper error handling for non-numeric data. Use a locale-independent function like
216 * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
217 Glib::ustring name = val->getEntryName();
218 if ( name == "rx" ) {
219 rc->rx = val->getDoubleLimited(); // prevents NaN and +/-Inf from messing up
220 } else if ( name == "ry" ) {
221 rc->ry = val->getDoubleLimited();
222 }
223 }
225 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
226 {
227 SPDesktop *desktop = event_context->desktop;
229 gint ret = FALSE;
231 switch (event->type) {
232 case GDK_BUTTON_PRESS:
233 if ( event->button.button == 1 && !event_context->space_panning) {
234 Inkscape::setup_for_drag_start(desktop, event_context, event);
235 ret = TRUE;
236 }
237 break;
238 // motion and release are always on root (why?)
239 default:
240 break;
241 }
243 if (((SPEventContextClass *) parent_class)->item_handler) {
244 ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
245 }
247 return ret;
248 }
250 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event)
251 {
252 static bool dragging;
254 SPDesktop *desktop = event_context->desktop;
255 Inkscape::Selection *selection = sp_desktop_selection (desktop);
257 SPRectContext *rc = SP_RECT_CONTEXT(event_context);
258 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
260 event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
262 gint ret = FALSE;
263 switch (event->type) {
264 case GDK_BUTTON_PRESS:
265 if (event->button.button == 1 && !event_context->space_panning) {
266 Geom::Point const button_w(event->button.x,
267 event->button.y);
269 // save drag origin
270 event_context->xp = (gint) button_w[Geom::X];
271 event_context->yp = (gint) button_w[Geom::Y];
272 event_context->within_tolerance = true;
274 // remember clicked item, disregarding groups, honoring Alt
275 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
277 dragging = true;
279 /* Position center */
280 Geom::Point button_dt(desktop->w2d(button_w));
281 rc->center = from_2geom(button_dt);
283 /* Snap center */
284 SnapManager &m = desktop->namedview->snap_manager;
285 m.setup(desktop);
286 m.freeSnapReturnByRef(button_dt, Inkscape::SNAPSOURCE_NODE_HANDLE);
287 rc->center = from_2geom(button_dt);
289 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
290 ( GDK_KEY_PRESS_MASK |
291 GDK_BUTTON_RELEASE_MASK |
292 GDK_POINTER_MOTION_MASK |
293 GDK_POINTER_MOTION_HINT_MASK |
294 GDK_BUTTON_PRESS_MASK ),
295 NULL, event->button.time);
297 ret = TRUE;
298 }
299 break;
300 case GDK_MOTION_NOTIFY:
301 if ( dragging
302 && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning)
303 {
304 if ( event_context->within_tolerance
305 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
306 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
307 break; // do not drag if we're within tolerance from origin
308 }
309 // Once the user has moved farther than tolerance from the original location
310 // (indicating they intend to draw, not click), then always process the
311 // motion notify coordinates as given (no snapping back to origin)
312 event_context->within_tolerance = false;
314 Geom::Point const motion_w(event->motion.x, event->motion.y);
315 Geom::Point motion_dt(desktop->w2d(motion_w));
317 sp_rect_drag(*rc, motion_dt, event->motion.state); // this will also handle the snapping
318 gobble_motion_events(GDK_BUTTON1_MASK);
319 ret = TRUE;
320 } else if (!sp_event_context_knot_mouseover(rc)) {
321 SnapManager &m = desktop->namedview->snap_manager;
322 m.setup(desktop);
324 Geom::Point const motion_w(event->motion.x, event->motion.y);
325 Geom::Point motion_dt(desktop->w2d(motion_w));
326 m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE));
327 }
328 break;
329 case GDK_BUTTON_RELEASE:
330 event_context->xp = event_context->yp = 0;
331 if (event->button.button == 1 && !event_context->space_panning) {
332 dragging = false;
333 sp_event_context_discard_delayed_snap_event(event_context);
335 if (!event_context->within_tolerance) {
336 // we've been dragging, finish the rect
337 sp_rect_finish(rc);
338 } else if (event_context->item_to_select) {
339 // no dragging, select clicked item if any
340 if (event->button.state & GDK_SHIFT_MASK) {
341 selection->toggle(event_context->item_to_select);
342 } else {
343 selection->set(event_context->item_to_select);
344 }
345 } else {
346 // click in an empty space
347 selection->clear();
348 }
350 event_context->item_to_select = NULL;
351 ret = TRUE;
352 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
353 event->button.time);
354 }
355 break;
356 case GDK_KEY_PRESS:
357 switch (get_group0_keyval (&event->key)) {
358 case GDK_Alt_L:
359 case GDK_Alt_R:
360 case GDK_Control_L:
361 case GDK_Control_R:
362 case GDK_Shift_L:
363 case GDK_Shift_R:
364 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
365 case GDK_Meta_R:
366 if (!dragging){
367 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
368 _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
369 _("<b>Shift</b>: draw around the starting point"),
370 NULL);
371 }
372 break;
373 case GDK_Up:
374 case GDK_Down:
375 case GDK_KP_Up:
376 case GDK_KP_Down:
377 // prevent the zoom field from activation
378 if (!MOD__CTRL_ONLY)
379 ret = TRUE;
380 break;
382 case GDK_x:
383 case GDK_X:
384 if (MOD__ALT_ONLY) {
385 desktop->setToolboxFocusTo ("altx-rect");
386 ret = TRUE;
387 }
388 break;
390 case GDK_g:
391 case GDK_G:
392 if (MOD__SHIFT_ONLY) {
393 sp_selection_to_guides(desktop);
394 ret = true;
395 }
396 break;
398 case GDK_Escape:
399 if (dragging) {
400 dragging = false;
401 sp_event_context_discard_delayed_snap_event(event_context);
402 // if drawing, cancel, otherwise pass it up for deselecting
403 sp_rect_cancel(rc);
404 ret = TRUE;
405 }
406 break;
408 case GDK_space:
409 if (dragging) {
410 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
411 event->button.time);
412 dragging = false;
413 sp_event_context_discard_delayed_snap_event(event_context);
414 if (!event_context->within_tolerance) {
415 // we've been dragging, finish the rect
416 sp_rect_finish(rc);
417 }
418 // do not return true, so that space would work switching to selector
419 }
420 break;
422 default:
423 break;
424 }
425 break;
426 case GDK_KEY_RELEASE:
427 switch (get_group0_keyval (&event->key)) {
428 case GDK_Alt_L:
429 case GDK_Alt_R:
430 case GDK_Control_L:
431 case GDK_Control_R:
432 case GDK_Shift_L:
433 case GDK_Shift_R:
434 case GDK_Meta_L: // Meta is when you press Shift+Alt
435 case GDK_Meta_R:
436 event_context->defaultMessageContext()->clear();
437 break;
438 default:
439 break;
440 }
441 break;
442 default:
443 break;
444 }
446 if (!ret) {
447 if (((SPEventContextClass *) parent_class)->root_handler) {
448 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
449 }
450 }
452 return ret;
453 }
455 static void sp_rect_drag(SPRectContext &rc, Geom::Point const pt, guint state)
456 {
457 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
459 if (!rc.item) {
461 if (Inkscape::have_viable_layer(desktop, rc._message_context) == false) {
462 return;
463 }
465 /* Create object */
466 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(&rc));
467 Inkscape::XML::Node *repr = xml_doc->createElement("svg:rect");
469 /* Set style */
470 sp_desktop_apply_style_tool (desktop, repr, "/tools/shapes/rect", false);
472 rc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
473 Inkscape::GC::release(repr);
474 rc.item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
475 rc.item->updateRepr();
477 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
478 }
480 Geom::Rect const r = Inkscape::snap_rectangular_box(desktop, rc.item, pt, rc.center, state);
482 sp_rect_position_set(SP_RECT(rc.item), r.min()[Geom::X], r.min()[Geom::Y], r.dimensions()[Geom::X], r.dimensions()[Geom::Y]);
483 if ( rc.rx != 0.0 ) {
484 sp_rect_set_rx (SP_RECT(rc.item), TRUE, rc.rx);
485 }
486 if ( rc.ry != 0.0 ) {
487 if (rc.rx == 0.0)
488 sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, MIN(r.dimensions()[Geom::X], r.dimensions()[Geom::Y])/2));
489 else
490 sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, r.dimensions()[Geom::Y]));
491 }
493 // status text
494 double rdimx = r.dimensions()[Geom::X];
495 double rdimy = r.dimensions()[Geom::Y];
496 GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
497 GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
498 if (state & GDK_CONTROL_MASK) {
499 int ratio_x, ratio_y;
500 bool is_golden_ratio = false;
501 if (fabs (rdimx) > fabs (rdimy)) {
502 if (fabs(rdimx / rdimy - goldenratio) < 1e-6) {
503 is_golden_ratio = true;
504 }
505 ratio_x = (int) rint (rdimx / rdimy);
506 ratio_y = 1;
507 } else {
508 if (fabs(rdimy / rdimx - goldenratio) < 1e-6) {
509 is_golden_ratio = true;
510 }
511 ratio_x = 1;
512 ratio_y = (int) rint (rdimy / rdimx);
513 }
514 if (!is_golden_ratio) {
515 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);
516 } else {
517 if (ratio_y == 1) {
518 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);
519 } else {
520 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);
521 }
522 }
523 } else {
524 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);
525 }
526 g_string_free(xs, FALSE);
527 g_string_free(ys, FALSE);
528 }
530 static void sp_rect_finish(SPRectContext *rc)
531 {
532 rc->_message_context->clear();
534 if ( rc->item != NULL ) {
535 SPRect *rect = SP_RECT(rc->item);
536 if (rect->width.computed == 0 || rect->height.computed == 0) {
537 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
538 return;
539 }
541 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(rc);
543 SP_OBJECT(rc->item)->updateRepr();
545 sp_canvas_end_forced_full_redraws(desktop->canvas);
547 sp_desktop_selection(desktop)->set(rc->item);
548 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_RECT,
549 _("Create rectangle"));
551 rc->item = NULL;
552 }
553 }
555 static void sp_rect_cancel(SPRectContext *rc)
556 {
557 SPDesktop *desktop = SP_EVENT_CONTEXT(rc)->desktop;
559 sp_desktop_selection(desktop)->clear();
560 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
562 if (rc->item != NULL) {
563 SP_OBJECT(rc->item)->deleteObject();
564 rc->item = NULL;
565 }
567 rc->within_tolerance = false;
568 rc->xp = 0;
569 rc->yp = 0;
570 rc->item_to_select = NULL;
572 sp_canvas_end_forced_full_redraws(desktop->canvas);
574 sp_document_cancel(sp_desktop_document(desktop));
575 }
578 /*
579 Local Variables:
580 mode:c++
581 c-file-style:"stroustrup"
582 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
583 indent-tabs-mode:nil
584 fill-column:99
585 End:
586 */
587 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :