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(Inkscape::SnapPreferences::SNAPPOINT_NODE, button_dt, Inkscape::SNAPSOURCE_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 }
321 break;
322 case GDK_BUTTON_RELEASE:
323 event_context->xp = event_context->yp = 0;
324 if (event->button.button == 1 && !event_context->space_panning) {
325 dragging = false;
326 sp_event_context_discard_delayed_snap_event(event_context);
328 if (!event_context->within_tolerance) {
329 // we've been dragging, finish the rect
330 sp_rect_finish(rc);
331 } else if (event_context->item_to_select) {
332 // no dragging, select clicked item if any
333 if (event->button.state & GDK_SHIFT_MASK) {
334 selection->toggle(event_context->item_to_select);
335 } else {
336 selection->set(event_context->item_to_select);
337 }
338 } else {
339 // click in an empty space
340 selection->clear();
341 }
343 event_context->item_to_select = NULL;
344 ret = TRUE;
345 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
346 event->button.time);
347 }
348 break;
349 case GDK_KEY_PRESS:
350 switch (get_group0_keyval (&event->key)) {
351 case GDK_Alt_L:
352 case GDK_Alt_R:
353 case GDK_Control_L:
354 case GDK_Control_R:
355 case GDK_Shift_L:
356 case GDK_Shift_R:
357 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
358 case GDK_Meta_R:
359 if (!dragging){
360 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
361 _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
362 _("<b>Shift</b>: draw around the starting point"),
363 NULL);
364 }
365 break;
366 case GDK_Up:
367 case GDK_Down:
368 case GDK_KP_Up:
369 case GDK_KP_Down:
370 // prevent the zoom field from activation
371 if (!MOD__CTRL_ONLY)
372 ret = TRUE;
373 break;
375 case GDK_x:
376 case GDK_X:
377 if (MOD__ALT_ONLY) {
378 desktop->setToolboxFocusTo ("altx-rect");
379 ret = TRUE;
380 }
381 break;
383 case GDK_g:
384 case GDK_G:
385 if (MOD__SHIFT_ONLY) {
386 sp_selection_to_guides(desktop);
387 ret = true;
388 }
389 break;
391 case GDK_Escape:
392 if (dragging) {
393 dragging = false;
394 sp_event_context_discard_delayed_snap_event(event_context);
395 // if drawing, cancel, otherwise pass it up for deselecting
396 sp_rect_cancel(rc);
397 ret = TRUE;
398 }
399 break;
401 case GDK_space:
402 if (dragging) {
403 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
404 event->button.time);
405 dragging = false;
406 sp_event_context_discard_delayed_snap_event(event_context);
407 if (!event_context->within_tolerance) {
408 // we've been dragging, finish the rect
409 sp_rect_finish(rc);
410 }
411 // do not return true, so that space would work switching to selector
412 }
413 break;
415 default:
416 break;
417 }
418 break;
419 case GDK_KEY_RELEASE:
420 switch (get_group0_keyval (&event->key)) {
421 case GDK_Alt_L:
422 case GDK_Alt_R:
423 case GDK_Control_L:
424 case GDK_Control_R:
425 case GDK_Shift_L:
426 case GDK_Shift_R:
427 case GDK_Meta_L: // Meta is when you press Shift+Alt
428 case GDK_Meta_R:
429 event_context->defaultMessageContext()->clear();
430 break;
431 default:
432 break;
433 }
434 break;
435 default:
436 break;
437 }
439 if (!ret) {
440 if (((SPEventContextClass *) parent_class)->root_handler) {
441 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
442 }
443 }
445 return ret;
446 }
448 static void sp_rect_drag(SPRectContext &rc, Geom::Point const pt, guint state)
449 {
450 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
452 if (!rc.item) {
454 if (Inkscape::have_viable_layer(desktop, rc._message_context) == false) {
455 return;
456 }
458 /* Create object */
459 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(&rc));
460 Inkscape::XML::Node *repr = xml_doc->createElement("svg:rect");
462 /* Set style */
463 sp_desktop_apply_style_tool (desktop, repr, "/tools/shapes/rect", false);
465 rc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
466 Inkscape::GC::release(repr);
467 rc.item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
468 rc.item->updateRepr();
470 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
471 }
473 Geom::Rect const r = Inkscape::snap_rectangular_box(desktop, rc.item, pt, rc.center, state);
475 sp_rect_position_set(SP_RECT(rc.item), r.min()[Geom::X], r.min()[Geom::Y], r.dimensions()[Geom::X], r.dimensions()[Geom::Y]);
476 if ( rc.rx != 0.0 ) {
477 sp_rect_set_rx (SP_RECT(rc.item), TRUE, rc.rx);
478 }
479 if ( rc.ry != 0.0 ) {
480 if (rc.rx == 0.0)
481 sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, MIN(r.dimensions()[Geom::X], r.dimensions()[Geom::Y])/2));
482 else
483 sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, r.dimensions()[Geom::Y]));
484 }
486 // status text
487 double rdimx = r.dimensions()[Geom::X];
488 double rdimy = r.dimensions()[Geom::Y];
489 GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
490 GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
491 if (state & GDK_CONTROL_MASK) {
492 int ratio_x, ratio_y;
493 bool is_golden_ratio = false;
494 if (fabs (rdimx) > fabs (rdimy)) {
495 if (fabs(rdimx / rdimy - goldenratio) < 1e-6) {
496 is_golden_ratio = true;
497 }
498 ratio_x = (int) rint (rdimx / rdimy);
499 ratio_y = 1;
500 } else {
501 if (fabs(rdimy / rdimx - goldenratio) < 1e-6) {
502 is_golden_ratio = true;
503 }
504 ratio_x = 1;
505 ratio_y = (int) rint (rdimy / rdimx);
506 }
507 if (!is_golden_ratio) {
508 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);
509 } else {
510 if (ratio_y == 1) {
511 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);
512 } else {
513 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);
514 }
515 }
516 } else {
517 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);
518 }
519 g_string_free(xs, FALSE);
520 g_string_free(ys, FALSE);
521 }
523 static void sp_rect_finish(SPRectContext *rc)
524 {
525 rc->_message_context->clear();
527 if ( rc->item != NULL ) {
528 SPRect *rect = SP_RECT(rc->item);
529 if (rect->width.computed == 0 || rect->height.computed == 0) {
530 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
531 return;
532 }
534 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(rc);
536 SP_OBJECT(rc->item)->updateRepr();
538 sp_canvas_end_forced_full_redraws(desktop->canvas);
540 sp_desktop_selection(desktop)->set(rc->item);
541 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_RECT,
542 _("Create rectangle"));
544 rc->item = NULL;
545 }
546 }
548 static void sp_rect_cancel(SPRectContext *rc)
549 {
550 SPDesktop *desktop = SP_EVENT_CONTEXT(rc)->desktop;
552 sp_desktop_selection(desktop)->clear();
553 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
555 if (rc->item != NULL) {
556 SP_OBJECT(rc->item)->deleteObject();
557 rc->item = NULL;
558 }
560 rc->within_tolerance = false;
561 rc->xp = 0;
562 rc->yp = 0;
563 rc->item_to_select = NULL;
565 sp_canvas_end_forced_full_redraws(desktop->canvas);
567 sp_document_cancel(sp_desktop_document(desktop));
568 }
571 /*
572 Local Variables:
573 mode:c++
574 c-file-style:"stroustrup"
575 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
576 indent-tabs-mode:nil
577 fill-column:99
578 End:
579 */
580 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :