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);
61 static SPEventContextClass *parent_class;
64 GtkType sp_rect_context_get_type()
65 {
66 static GType type = 0;
67 if (!type) {
68 GTypeInfo info = {
69 sizeof(SPRectContextClass),
70 NULL, NULL,
71 (GClassInitFunc) sp_rect_context_class_init,
72 NULL, NULL,
73 sizeof(SPRectContext),
74 4,
75 (GInstanceInitFunc) sp_rect_context_init,
76 NULL, /* value_table */
77 };
78 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPRectContext", &info, (GTypeFlags) 0);
79 }
80 return type;
81 }
83 static void sp_rect_context_class_init(SPRectContextClass *klass)
84 {
85 GObjectClass *object_class = (GObjectClass *) klass;
86 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
88 parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
90 object_class->dispose = sp_rect_context_dispose;
92 event_context_class->setup = sp_rect_context_setup;
93 event_context_class->set = sp_rect_context_set;
94 event_context_class->root_handler = sp_rect_context_root_handler;
95 event_context_class->item_handler = sp_rect_context_item_handler;
96 }
98 static void sp_rect_context_init(SPRectContext *rect_context)
99 {
100 SPEventContext *event_context = SP_EVENT_CONTEXT(rect_context);
102 event_context->cursor_shape = cursor_rect_xpm;
103 event_context->hot_x = 4;
104 event_context->hot_y = 4;
105 event_context->xp = 0;
106 event_context->yp = 0;
107 event_context->tolerance = 0;
108 event_context->within_tolerance = false;
109 event_context->item_to_select = 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 delete ec->shape_editor;
130 ec->shape_editor = NULL;
132 /* fixme: This is necessary because we do not grab */
133 if (rc->item) {
134 sp_rect_finish(rc);
135 }
137 if (rc->_message_context) {
138 delete rc->_message_context;
139 }
141 G_OBJECT_CLASS(parent_class)->dispose(object);
142 }
144 /**
145 \brief Callback that processes the "changed" signal on the selection;
146 destroys old and creates new knotholder
147 */
148 void sp_rect_context_selection_changed(Inkscape::Selection *selection, gpointer data)
149 {
150 SPRectContext *rc = SP_RECT_CONTEXT(data);
151 SPEventContext *ec = SP_EVENT_CONTEXT(rc);
153 ec->shape_editor->unset_item(SH_KNOTHOLDER);
154 SPItem *item = selection->singleItem();
155 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
156 }
158 static void sp_rect_context_setup(SPEventContext *ec)
159 {
160 SPRectContext *rc = SP_RECT_CONTEXT(ec);
162 if (((SPEventContextClass *) parent_class)->setup) {
163 ((SPEventContextClass *) parent_class)->setup(ec);
164 }
166 ec->shape_editor = new ShapeEditor(ec->desktop);
168 SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
169 if (item) {
170 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
171 }
173 rc->sel_changed_connection.disconnect();
174 rc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
175 sigc::bind(sigc::ptr_fun(&sp_rect_context_selection_changed), (gpointer)rc)
176 );
178 sp_event_context_read(ec, "rx");
179 sp_event_context_read(ec, "ry");
181 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
182 if (prefs->getBool("/tools/shapes/selcue")) {
183 ec->enableSelectionCue();
184 }
186 if (prefs->getBool("/tools/shapes/gradientdrag")) {
187 ec->enableGrDrag();
188 }
190 rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
191 }
193 static void sp_rect_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
194 {
195 SPRectContext *rc = SP_RECT_CONTEXT(ec);
197 /* fixme: Proper error handling for non-numeric data. Use a locale-independent function like
198 * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
199 Glib::ustring name = val->getEntryName();
200 if ( name == "rx" ) {
201 rc->rx = val->getDoubleLimited(); // prevents NaN and +/-Inf from messing up
202 } else if ( name == "ry" ) {
203 rc->ry = val->getDoubleLimited();
204 }
205 }
207 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
208 {
209 SPDesktop *desktop = event_context->desktop;
211 gint ret = FALSE;
213 switch (event->type) {
214 case GDK_BUTTON_PRESS:
215 if ( event->button.button == 1 && !event_context->space_panning) {
216 Inkscape::setup_for_drag_start(desktop, event_context, event);
217 ret = TRUE;
218 }
219 break;
220 // motion and release are always on root (why?)
221 default:
222 break;
223 }
225 if (((SPEventContextClass *) parent_class)->item_handler) {
226 ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
227 }
229 return ret;
230 }
232 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event)
233 {
234 static bool dragging;
236 SPDesktop *desktop = event_context->desktop;
237 Inkscape::Selection *selection = sp_desktop_selection (desktop);
239 SPRectContext *rc = SP_RECT_CONTEXT(event_context);
240 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
242 event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
244 gint ret = FALSE;
245 switch (event->type) {
246 case GDK_BUTTON_PRESS:
247 if (event->button.button == 1 && !event_context->space_panning) {
248 Geom::Point const button_w(event->button.x,
249 event->button.y);
251 // save drag origin
252 event_context->xp = (gint) button_w[Geom::X];
253 event_context->yp = (gint) button_w[Geom::Y];
254 event_context->within_tolerance = true;
256 // remember clicked item, disregarding groups, honoring Alt
257 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
259 dragging = true;
260 sp_canvas_set_snap_delay_active(desktop->canvas, 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);
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_canvas_set_snap_delay_active(desktop->canvas, false);
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 sp_desktop_selection(desktop)->clear();
376 //TODO: make dragging escapable by Esc
377 break;
379 case GDK_space:
380 if (dragging) {
381 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
382 event->button.time);
383 dragging = false;
384 sp_canvas_set_snap_delay_active(desktop->canvas, false);
385 if (!event_context->within_tolerance) {
386 // we've been dragging, finish the rect
387 sp_rect_finish(rc);
388 }
389 // do not return true, so that space would work switching to selector
390 }
391 break;
393 default:
394 break;
395 }
396 break;
397 case GDK_KEY_RELEASE:
398 switch (get_group0_keyval (&event->key)) {
399 case GDK_Alt_L:
400 case GDK_Alt_R:
401 case GDK_Control_L:
402 case GDK_Control_R:
403 case GDK_Shift_L:
404 case GDK_Shift_R:
405 case GDK_Meta_L: // Meta is when you press Shift+Alt
406 case GDK_Meta_R:
407 event_context->defaultMessageContext()->clear();
408 break;
409 default:
410 break;
411 }
412 break;
413 default:
414 break;
415 }
417 if (!ret) {
418 if (((SPEventContextClass *) parent_class)->root_handler) {
419 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
420 }
421 }
423 return ret;
424 }
426 static void sp_rect_drag(SPRectContext &rc, Geom::Point const pt, guint state)
427 {
428 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
430 if (!rc.item) {
432 if (Inkscape::have_viable_layer(desktop, rc._message_context) == false) {
433 return;
434 }
436 /* Create object */
437 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(&rc));
438 Inkscape::XML::Node *repr = xml_doc->createElement("svg:rect");
440 /* Set style */
441 sp_desktop_apply_style_tool (desktop, repr, "/tools/shapes/rect", false);
443 rc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
444 Inkscape::GC::release(repr);
445 rc.item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
446 rc.item->updateRepr();
448 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
449 }
451 Geom::Rect const r = Inkscape::snap_rectangular_box(desktop, rc.item, pt, rc.center, state);
453 sp_rect_position_set(SP_RECT(rc.item), r.min()[Geom::X], r.min()[Geom::Y], r.dimensions()[Geom::X], r.dimensions()[Geom::Y]);
454 if ( rc.rx != 0.0 ) {
455 sp_rect_set_rx (SP_RECT(rc.item), TRUE, rc.rx);
456 }
457 if ( rc.ry != 0.0 ) {
458 if (rc.rx == 0.0)
459 sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, MIN(r.dimensions()[Geom::X], r.dimensions()[Geom::Y])/2));
460 else
461 sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, r.dimensions()[Geom::Y]));
462 }
464 // status text
465 double rdimx = r.dimensions()[Geom::X];
466 double rdimy = r.dimensions()[Geom::Y];
467 GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
468 GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
469 if (state & GDK_CONTROL_MASK) {
470 int ratio_x, ratio_y;
471 bool is_golden_ratio = false;
472 if (fabs (rdimx) > fabs (rdimy)) {
473 if (fabs(rdimx / rdimy - goldenratio) < 1e-6) {
474 is_golden_ratio = true;
475 }
476 ratio_x = (int) rint (rdimx / rdimy);
477 ratio_y = 1;
478 } else {
479 if (fabs(rdimy / rdimx - goldenratio) < 1e-6) {
480 is_golden_ratio = true;
481 }
482 ratio_x = 1;
483 ratio_y = (int) rint (rdimy / rdimx);
484 }
485 if (!is_golden_ratio) {
486 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);
487 } else {
488 if (ratio_y == 1) {
489 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);
490 } else {
491 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);
492 }
493 }
494 } else {
495 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);
496 }
497 g_string_free(xs, FALSE);
498 g_string_free(ys, FALSE);
499 }
501 static void sp_rect_finish(SPRectContext *rc)
502 {
503 rc->_message_context->clear();
505 if ( rc->item != NULL ) {
506 SPDesktop * desktop;
508 desktop = SP_EVENT_CONTEXT_DESKTOP(rc);
510 SP_OBJECT(rc->item)->updateRepr();
512 sp_canvas_end_forced_full_redraws(desktop->canvas);
514 sp_desktop_selection(desktop)->set(rc->item);
515 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_RECT,
516 _("Create rectangle"));
518 rc->item = NULL;
519 }
520 }
522 /*
523 Local Variables:
524 mode:c++
525 c-file-style:"stroustrup"
526 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
527 indent-tabs-mode:nil
528 fill-column:99
529 End:
530 */
531 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :