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>
21 #include "macros.h"
22 #include "display/sp-canvas.h"
23 #include "sp-rect.h"
24 #include "document.h"
25 #include "sp-namedview.h"
26 #include "selection.h"
27 #include "desktop-handles.h"
28 #include "snap.h"
29 #include "desktop.h"
30 #include "desktop-style.h"
31 #include "message-context.h"
32 #include "pixmaps/cursor-rect.xpm"
33 #include "pixmaps/cursor-rect.pixbuf"
34 #include "rect-context.h"
35 #include "sp-metrics.h"
36 #include <glibmm/i18n.h>
37 #include "object-edit.h"
38 #include "xml/repr.h"
39 #include "xml/node-event-vector.h"
40 #include "prefs-utils.h"
41 #include "context-fns.h"
43 static void sp_rect_context_class_init(SPRectContextClass *klass);
44 static void sp_rect_context_init(SPRectContext *rect_context);
45 static void sp_rect_context_dispose(GObject *object);
47 static void sp_rect_context_setup(SPEventContext *ec);
48 static void sp_rect_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
50 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event);
51 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
53 static void sp_rect_drag(SPRectContext &rc, NR::Point const pt, guint state);
54 static void sp_rect_finish(SPRectContext *rc);
56 static SPEventContextClass *parent_class;
59 GtkType sp_rect_context_get_type()
60 {
61 static GType type = 0;
62 if (!type) {
63 GTypeInfo info = {
64 sizeof(SPRectContextClass),
65 NULL, NULL,
66 (GClassInitFunc) sp_rect_context_class_init,
67 NULL, NULL,
68 sizeof(SPRectContext),
69 4,
70 (GInstanceInitFunc) sp_rect_context_init,
71 NULL, /* value_table */
72 };
73 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPRectContext", &info, (GTypeFlags) 0);
74 }
75 return type;
76 }
78 static void sp_rect_context_class_init(SPRectContextClass *klass)
79 {
80 GObjectClass *object_class = (GObjectClass *) klass;
81 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
83 parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
85 object_class->dispose = sp_rect_context_dispose;
87 event_context_class->setup = sp_rect_context_setup;
88 event_context_class->set = sp_rect_context_set;
89 event_context_class->root_handler = sp_rect_context_root_handler;
90 event_context_class->item_handler = sp_rect_context_item_handler;
91 }
93 static void sp_rect_context_init(SPRectContext *rect_context)
94 {
95 SPEventContext *event_context = SP_EVENT_CONTEXT(rect_context);
97 event_context->cursor_shape = cursor_rect_xpm;
98 event_context->cursor_pixbuf = gdk_pixbuf_new_from_inline(
99 -1,
100 cursor_rect_pixbuf,
101 FALSE,
102 NULL);
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 event_context->shape_repr = NULL;
112 event_context->shape_knot_holder = 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_dispose(GObject *object)
123 {
124 SPRectContext *rc = SP_RECT_CONTEXT(object);
125 SPEventContext *ec = SP_EVENT_CONTEXT(object);
127 ec->enableGrDrag(false);
129 rc->sel_changed_connection.disconnect();
130 rc->sel_changed_connection.~connection();
132 /* fixme: This is necessary because we do not grab */
133 if (rc->item) {
134 sp_rect_finish(rc);
135 }
137 if (ec->shape_knot_holder) {
138 sp_knot_holder_destroy(ec->shape_knot_holder);
139 ec->shape_knot_holder = NULL;
140 }
142 if (ec->shape_repr) { // remove old listener
143 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
144 Inkscape::GC::release(ec->shape_repr);
145 ec->shape_repr = 0;
146 }
148 if (rc->_message_context) {
149 delete rc->_message_context;
150 }
152 G_OBJECT_CLASS(parent_class)->dispose(object);
153 }
155 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
156 NULL, /* child_added */
157 NULL, /* child_removed */
158 ec_shape_event_attr_changed,
159 NULL, /* content_changed */
160 NULL /* order_changed */
161 };
163 /**
164 \brief Callback that processes the "changed" signal on the selection;
165 destroys old and creates new knotholder
166 */
167 void sp_rect_context_selection_changed(Inkscape::Selection *selection, gpointer data)
168 {
169 SPRectContext *rc = SP_RECT_CONTEXT(data);
170 SPEventContext *ec = SP_EVENT_CONTEXT(rc);
172 if (ec->shape_knot_holder) { // destroy knotholder
173 sp_knot_holder_destroy(ec->shape_knot_holder);
174 ec->shape_knot_holder = NULL;
175 }
177 if (ec->shape_repr) { // remove old listener
178 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
179 Inkscape::GC::release(ec->shape_repr);
180 ec->shape_repr = 0;
181 }
183 SPItem *item = selection->singleItem();
184 if (item) {
185 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
186 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
187 if (shape_repr) {
188 ec->shape_repr = shape_repr;
189 Inkscape::GC::anchor(shape_repr);
190 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
191 }
192 }
193 }
195 static void sp_rect_context_setup(SPEventContext *ec)
196 {
197 SPRectContext *rc = SP_RECT_CONTEXT(ec);
199 if (((SPEventContextClass *) parent_class)->setup) {
200 ((SPEventContextClass *) parent_class)->setup(ec);
201 }
203 SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
204 if (item) {
205 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
206 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
207 if (shape_repr) {
208 ec->shape_repr = shape_repr;
209 Inkscape::GC::anchor(shape_repr);
210 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
211 }
212 }
214 rc->sel_changed_connection.disconnect();
215 rc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
216 sigc::bind(sigc::ptr_fun(&sp_rect_context_selection_changed), (gpointer)rc)
217 );
219 sp_event_context_read(ec, "rx");
220 sp_event_context_read(ec, "ry");
222 if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
223 ec->enableSelectionCue();
224 }
226 if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
227 ec->enableGrDrag();
228 }
230 rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
231 }
233 static void sp_rect_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
234 {
235 SPRectContext *rc = SP_RECT_CONTEXT(ec);
237 /* fixme: Proper error handling for non-numeric data. Use a locale-independent function like
238 * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
239 if ( strcmp(key, "rx") == 0 ) {
240 rc->rx = ( val
241 ? g_ascii_strtod (val, NULL)
242 : 0.0 );
243 } else if ( strcmp(key, "ry") == 0 ) {
244 rc->ry = ( val
245 ? g_ascii_strtod (val, NULL)
246 : 0.0 );
247 }
248 }
250 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
251 {
252 SPDesktop *desktop = event_context->desktop;
254 gint ret = FALSE;
256 switch (event->type) {
257 case GDK_BUTTON_PRESS:
258 if ( event->button.button == 1 ) {
259 Inkscape::setup_for_drag_start(desktop, event_context, event);
260 ret = TRUE;
261 }
262 break;
263 // motion and release are always on root (why?)
264 default:
265 break;
266 }
268 if (((SPEventContextClass *) parent_class)->item_handler) {
269 ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
270 }
272 return ret;
273 }
275 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event)
276 {
277 static bool dragging;
279 SPDesktop *desktop = event_context->desktop;
280 Inkscape::Selection *selection = sp_desktop_selection (desktop);
282 SPRectContext *rc = SP_RECT_CONTEXT(event_context);
284 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
286 gint ret = FALSE;
287 switch (event->type) {
288 case GDK_BUTTON_PRESS:
289 if ( event->button.button == 1 ) {
290 NR::Point const button_w(event->button.x,
291 event->button.y);
293 // save drag origin
294 event_context->xp = (gint) button_w[NR::X];
295 event_context->yp = (gint) button_w[NR::Y];
296 event_context->within_tolerance = true;
298 // remember clicked item, disregarding groups, honoring Alt
299 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
301 dragging = true;
303 /* Position center */
304 NR::Point const button_dt(desktop->w2d(button_w));
306 /* Snap center */
307 SnapManager const &m = desktop->namedview->snap_manager;
308 rc->center = m.freeSnap(Inkscape::Snapper::SNAP_POINT | Inkscape::Snapper::BBOX_POINT,
309 button_dt, rc->item).getPoint();
311 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
312 ( GDK_KEY_PRESS_MASK |
313 GDK_BUTTON_RELEASE_MASK |
314 GDK_POINTER_MOTION_MASK |
315 GDK_BUTTON_PRESS_MASK ),
316 NULL, event->button.time);
317 ret = TRUE;
318 }
319 break;
320 case GDK_MOTION_NOTIFY:
321 if ( dragging
322 && ( event->motion.state & GDK_BUTTON1_MASK ) )
323 {
324 if ( event_context->within_tolerance
325 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
326 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
327 break; // do not drag if we're within tolerance from origin
328 }
329 // Once the user has moved farther than tolerance from the original location
330 // (indicating they intend to draw, not click), then always process the
331 // motion notify coordinates as given (no snapping back to origin)
332 event_context->within_tolerance = false;
334 NR::Point const motion_w(event->motion.x,
335 event->motion.y);
336 NR::Point const motion_dt(desktop->w2d(motion_w));
337 sp_rect_drag(*rc, motion_dt, event->motion.state);
338 ret = TRUE;
339 }
340 break;
341 case GDK_BUTTON_RELEASE:
342 event_context->xp = event_context->yp = 0;
343 if ( event->button.button == 1 ) {
344 dragging = false;
346 if (!event_context->within_tolerance) {
347 // we've been dragging, finish the rect
348 sp_rect_finish(rc);
349 } else if (event_context->item_to_select) {
350 // no dragging, select clicked item if any
351 if (event->button.state & GDK_SHIFT_MASK) {
352 selection->toggle(event_context->item_to_select);
353 } else {
354 selection->set(event_context->item_to_select);
355 }
356 } else {
357 // click in an empty space
358 selection->clear();
359 }
361 event_context->item_to_select = NULL;
362 ret = TRUE;
363 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
364 event->button.time);
365 }
366 break;
367 case GDK_KEY_PRESS:
368 switch (get_group0_keyval (&event->key)) {
369 case GDK_Alt_L:
370 case GDK_Alt_R:
371 case GDK_Control_L:
372 case GDK_Control_R:
373 case GDK_Shift_L:
374 case GDK_Shift_R:
375 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
376 case GDK_Meta_R:
377 if (!dragging){
378 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
379 _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
380 _("<b>Shift</b>: draw around the starting point"),
381 NULL);
382 }
383 break;
384 case GDK_Up:
385 case GDK_Down:
386 case GDK_KP_Up:
387 case GDK_KP_Down:
388 // prevent the zoom field from activation
389 if (!MOD__CTRL_ONLY)
390 ret = TRUE;
391 break;
393 case GDK_x:
394 case GDK_X:
395 if (MOD__ALT_ONLY) {
396 desktop->setToolboxFocusTo ("altx-rect");
397 ret = TRUE;
398 }
399 break;
401 case GDK_Escape:
402 sp_desktop_selection(desktop)->clear();
403 //TODO: make dragging escapable by Esc
404 default:
405 break;
406 }
407 break;
408 case GDK_KEY_RELEASE:
409 switch (get_group0_keyval (&event->key)) {
410 case GDK_Alt_L:
411 case GDK_Alt_R:
412 case GDK_Control_L:
413 case GDK_Control_R:
414 case GDK_Shift_L:
415 case GDK_Shift_R:
416 case GDK_Meta_L: // Meta is when you press Shift+Alt
417 case GDK_Meta_R:
418 event_context->defaultMessageContext()->clear();
419 break;
420 default:
421 break;
422 }
423 break;
424 default:
425 break;
426 }
428 if (!ret) {
429 if (((SPEventContextClass *) parent_class)->root_handler) {
430 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
431 }
432 }
434 return ret;
435 }
437 static void sp_rect_drag(SPRectContext &rc, NR::Point const pt, guint state)
438 {
439 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
441 if (!rc.item) {
443 if (Inkscape::have_viable_layer(desktop, rc._message_context) == false) {
444 return;
445 }
447 /* Create object */
448 Inkscape::XML::Node *repr = sp_repr_new("svg:rect");
450 /* Set style */
451 sp_desktop_apply_style_tool (desktop, repr, "tools.shapes.rect", false);
453 rc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
454 Inkscape::GC::release(repr);
455 rc.item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
456 rc.item->updateRepr();
458 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
459 }
461 NR::Rect const r = Inkscape::snap_rectangular_box(desktop, rc.item, pt, rc.center, state);
463 sp_rect_position_set(SP_RECT(rc.item), r.min()[NR::X], r.min()[NR::Y], r.dimensions()[NR::X], r.dimensions()[NR::Y]);
464 if ( rc.rx != 0.0 ) {
465 sp_rect_set_rx (SP_RECT(rc.item), TRUE, rc.rx);
466 }
467 if ( rc.ry != 0.0 ) {
468 if (rc.rx == 0.0)
469 sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, MIN(r.dimensions()[NR::X], r.dimensions()[NR::Y])/2));
470 else
471 sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, r.dimensions()[NR::Y]));
472 }
474 // status text
475 GString *xs = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::X], desktop->namedview->getDefaultMetric());
476 GString *ys = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::Y], desktop->namedview->getDefaultMetric());
477 rc._message_context->setF(Inkscape::NORMAL_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);
478 g_string_free(xs, FALSE);
479 g_string_free(ys, FALSE);
480 }
482 static void sp_rect_finish(SPRectContext *rc)
483 {
484 rc->_message_context->clear();
486 if ( rc->item != NULL ) {
487 SPDesktop * desktop;
489 desktop = SP_EVENT_CONTEXT_DESKTOP(rc);
491 SP_OBJECT(rc->item)->updateRepr();
493 sp_canvas_end_forced_full_redraws(desktop->canvas);
495 sp_desktop_selection(desktop)->set(rc->item);
496 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_RECT,
497 _("Create rectangle"));
499 rc->item = NULL;
500 }
501 }
503 /*
504 Local Variables:
505 mode:c++
506 c-file-style:"stroustrup"
507 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
508 indent-tabs-mode:nil
509 fill-column:99
510 End:
511 */
512 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :