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) 2000-2005 authors
11 * Copyright (C) 2000-2001 Ximian, Inc.
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #include "config.h"
18 #include <gdk/gdkkeysyms.h>
20 #include "macros.h"
21 #include "display/sp-canvas.h"
22 #include "sp-rect.h"
23 #include "document.h"
24 #include "sp-namedview.h"
25 #include "selection.h"
26 #include "desktop-handles.h"
27 #include "snap.h"
28 #include "desktop.h"
29 #include "desktop-style.h"
30 #include "message-context.h"
31 #include "pixmaps/cursor-rect.xpm"
32 #include "rect-context.h"
33 #include "sp-metrics.h"
34 #include <glibmm/i18n.h>
35 #include "object-edit.h"
36 #include "xml/repr.h"
37 #include "xml/node-event-vector.h"
38 #include "prefs-utils.h"
39 #include "context-fns.h"
41 static void sp_rect_context_class_init(SPRectContextClass *klass);
42 static void sp_rect_context_init(SPRectContext *rect_context);
43 static void sp_rect_context_dispose(GObject *object);
45 static void sp_rect_context_setup(SPEventContext *ec);
46 static void sp_rect_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
48 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event);
49 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
51 static void sp_rect_drag(SPRectContext &rc, NR::Point const pt, guint state);
52 static void sp_rect_finish(SPRectContext *rc);
54 static SPEventContextClass *parent_class;
57 GtkType sp_rect_context_get_type()
58 {
59 static GType type = 0;
60 if (!type) {
61 GTypeInfo info = {
62 sizeof(SPRectContextClass),
63 NULL, NULL,
64 (GClassInitFunc) sp_rect_context_class_init,
65 NULL, NULL,
66 sizeof(SPRectContext),
67 4,
68 (GInstanceInitFunc) sp_rect_context_init,
69 NULL, /* value_table */
70 };
71 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPRectContext", &info, (GTypeFlags) 0);
72 }
73 return type;
74 }
76 static void sp_rect_context_class_init(SPRectContextClass *klass)
77 {
78 GObjectClass *object_class = (GObjectClass *) klass;
79 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
81 parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
83 object_class->dispose = sp_rect_context_dispose;
85 event_context_class->setup = sp_rect_context_setup;
86 event_context_class->set = sp_rect_context_set;
87 event_context_class->root_handler = sp_rect_context_root_handler;
88 event_context_class->item_handler = sp_rect_context_item_handler;
89 }
91 static void sp_rect_context_init(SPRectContext *rect_context)
92 {
93 SPEventContext *event_context = SP_EVENT_CONTEXT(rect_context);
95 event_context->cursor_shape = cursor_rect_xpm;
96 event_context->hot_x = 4;
97 event_context->hot_y = 4;
98 event_context->xp = 0;
99 event_context->yp = 0;
100 event_context->tolerance = 0;
101 event_context->within_tolerance = false;
102 event_context->item_to_select = NULL;
104 event_context->shape_repr = NULL;
105 event_context->shape_knot_holder = NULL;
107 rect_context->item = NULL;
109 rect_context->rx = 0.0;
110 rect_context->ry = 0.0;
112 new (&rect_context->sel_changed_connection) sigc::connection();
113 }
115 static void sp_rect_context_dispose(GObject *object)
116 {
117 SPRectContext *rc = SP_RECT_CONTEXT(object);
118 SPEventContext *ec = SP_EVENT_CONTEXT(object);
120 ec->enableGrDrag(false);
122 rc->sel_changed_connection.disconnect();
123 rc->sel_changed_connection.~connection();
125 /* fixme: This is necessary because we do not grab */
126 if (rc->item) {
127 sp_rect_finish(rc);
128 }
130 if (ec->shape_knot_holder) {
131 sp_knot_holder_destroy(ec->shape_knot_holder);
132 ec->shape_knot_holder = NULL;
133 }
135 if (ec->shape_repr) { // remove old listener
136 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
137 Inkscape::GC::release(ec->shape_repr);
138 ec->shape_repr = 0;
139 }
141 if (rc->_message_context) {
142 delete rc->_message_context;
143 }
145 G_OBJECT_CLASS(parent_class)->dispose(object);
146 }
148 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
149 NULL, /* child_added */
150 NULL, /* child_removed */
151 ec_shape_event_attr_changed,
152 NULL, /* content_changed */
153 NULL /* order_changed */
154 };
156 /**
157 \brief Callback that processes the "changed" signal on the selection;
158 destroys old and creates new knotholder
159 */
160 void sp_rect_context_selection_changed(Inkscape::Selection *selection, gpointer data)
161 {
162 SPRectContext *rc = SP_RECT_CONTEXT(data);
163 SPEventContext *ec = SP_EVENT_CONTEXT(rc);
165 if (ec->shape_knot_holder) { // destroy knotholder
166 sp_knot_holder_destroy(ec->shape_knot_holder);
167 ec->shape_knot_holder = NULL;
168 }
170 if (ec->shape_repr) { // remove old listener
171 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
172 Inkscape::GC::release(ec->shape_repr);
173 ec->shape_repr = 0;
174 }
176 SPItem *item = selection->singleItem();
177 if (item) {
178 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
179 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
180 if (shape_repr) {
181 ec->shape_repr = shape_repr;
182 Inkscape::GC::anchor(shape_repr);
183 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
184 sp_repr_synthesize_events(shape_repr, &ec_shape_repr_events, ec);
185 }
186 }
187 }
189 static void sp_rect_context_setup(SPEventContext *ec)
190 {
191 SPRectContext *rc = SP_RECT_CONTEXT(ec);
193 if (((SPEventContextClass *) parent_class)->setup) {
194 ((SPEventContextClass *) parent_class)->setup(ec);
195 }
197 SPItem *item = SP_DT_SELECTION(ec->desktop)->singleItem();
198 if (item) {
199 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
200 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
201 if (shape_repr) {
202 ec->shape_repr = shape_repr;
203 Inkscape::GC::anchor(shape_repr);
204 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
205 sp_repr_synthesize_events(shape_repr, &ec_shape_repr_events, ec);
206 }
207 }
209 rc->sel_changed_connection.disconnect();
210 rc->sel_changed_connection = SP_DT_SELECTION(ec->desktop)->connectChanged(
211 sigc::bind(sigc::ptr_fun(&sp_rect_context_selection_changed), (gpointer)rc)
212 );
214 sp_event_context_read(ec, "rx");
215 sp_event_context_read(ec, "ry");
217 if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
218 ec->enableSelectionCue();
219 }
221 if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
222 ec->enableGrDrag();
223 }
225 rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
226 }
228 static void sp_rect_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
229 {
230 SPRectContext *rc = SP_RECT_CONTEXT(ec);
232 /* fixme: Proper error handling for non-numeric data. Use a locale-independent function like
233 * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
234 if ( strcmp(key, "rx") == 0 ) {
235 rc->rx = ( val
236 ? g_ascii_strtod (val, NULL)
237 : 0.0 );
238 } else if ( strcmp(key, "ry") == 0 ) {
239 rc->ry = ( val
240 ? g_ascii_strtod (val, NULL)
241 : 0.0 );
242 }
243 }
245 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
246 {
247 SPDesktop *desktop = event_context->desktop;
249 gint ret = FALSE;
251 switch (event->type) {
252 case GDK_BUTTON_PRESS:
253 if ( event->button.button == 1 ) {
254 Inkscape::setup_for_drag_start(desktop, event_context, event);
255 ret = TRUE;
256 }
257 break;
258 // motion and release are always on root (why?)
259 default:
260 break;
261 }
263 if (((SPEventContextClass *) parent_class)->item_handler) {
264 ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
265 }
267 return ret;
268 }
270 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event)
271 {
272 static bool dragging;
274 SPDesktop *desktop = event_context->desktop;
275 Inkscape::Selection *selection = SP_DT_SELECTION (desktop);
277 SPRectContext *rc = SP_RECT_CONTEXT(event_context);
279 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
281 gint ret = FALSE;
282 switch (event->type) {
283 case GDK_BUTTON_PRESS:
284 if ( event->button.button == 1 ) {
285 NR::Point const button_w(event->button.x,
286 event->button.y);
288 // save drag origin
289 event_context->xp = (gint) button_w[NR::X];
290 event_context->yp = (gint) button_w[NR::Y];
291 event_context->within_tolerance = true;
293 // remember clicked item, disregarding groups, honoring Alt
294 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
296 dragging = true;
298 /* Position center */
299 NR::Point const button_dt(desktop->w2d(button_w));
301 /* Snap center */
302 SnapManager const m(desktop->namedview);
303 rc->center = m.freeSnap(Inkscape::Snapper::SNAP_POINT | Inkscape::Snapper::BBOX_POINT,
304 button_dt, rc->item).getPoint();
306 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
307 ( GDK_KEY_PRESS_MASK |
308 GDK_BUTTON_RELEASE_MASK |
309 GDK_POINTER_MOTION_MASK |
310 GDK_POINTER_MOTION_HINT_MASK |
311 GDK_BUTTON_PRESS_MASK ),
312 NULL, event->button.time);
313 ret = TRUE;
314 }
315 break;
316 case GDK_MOTION_NOTIFY:
317 if ( dragging
318 && ( event->motion.state & GDK_BUTTON1_MASK ) )
319 {
320 if ( event_context->within_tolerance
321 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
322 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
323 break; // do not drag if we're within tolerance from origin
324 }
325 // Once the user has moved farther than tolerance from the original location
326 // (indicating they intend to draw, not click), then always process the
327 // motion notify coordinates as given (no snapping back to origin)
328 event_context->within_tolerance = false;
330 NR::Point const motion_w(event->motion.x,
331 event->motion.y);
332 NR::Point const motion_dt(desktop->w2d(motion_w));
333 sp_rect_drag(*rc, motion_dt, event->motion.state);
334 ret = TRUE;
335 }
336 break;
337 case GDK_BUTTON_RELEASE:
338 event_context->xp = event_context->yp = 0;
339 if ( event->button.button == 1 ) {
340 dragging = false;
342 if (!event_context->within_tolerance) {
343 // we've been dragging, finish the rect
344 sp_rect_finish(rc);
345 } else if (event_context->item_to_select) {
346 // no dragging, select clicked item if any
347 if (event->button.state & GDK_SHIFT_MASK) {
348 selection->toggle(event_context->item_to_select);
349 } else {
350 selection->set(event_context->item_to_select);
351 }
352 } else {
353 // click in an empty space
354 selection->clear();
355 }
357 event_context->item_to_select = NULL;
358 ret = TRUE;
359 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
360 event->button.time);
361 }
362 break;
363 case GDK_KEY_PRESS:
364 switch (get_group0_keyval (&event->key)) {
365 case GDK_Alt_L:
366 case GDK_Alt_R:
367 case GDK_Control_L:
368 case GDK_Control_R:
369 case GDK_Shift_L:
370 case GDK_Shift_R:
371 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
372 case GDK_Meta_R:
373 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
374 _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
375 _("<b>Shift</b>: draw around the starting point"),
376 NULL);
377 break;
378 case GDK_Up:
379 case GDK_Down:
380 case GDK_KP_Up:
381 case GDK_KP_Down:
382 // prevent the zoom field from activation
383 if (!MOD__CTRL_ONLY)
384 ret = TRUE;
385 break;
387 case GDK_x:
388 case GDK_X:
389 if (MOD__ALT_ONLY) {
390 desktop->setToolboxFocusTo ("altx-rect");
391 ret = TRUE;
392 }
393 break;
395 case GDK_Escape:
396 SP_DT_SELECTION(desktop)->clear();
397 //TODO: make dragging escapable by Esc
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, NR::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::Node *repr = sp_repr_new("svg:rect");
444 /* Set style */
445 sp_desktop_apply_style_tool (desktop, repr, "tools.shapes.rect", false);
447 rc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
448 Inkscape::GC::release(repr);
449 rc.item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
450 rc.item->updateRepr();
451 }
453 NR::Rect const r = Inkscape::snap_rectangular_box(desktop, rc.item, pt, rc.center, state);
455 sp_rect_position_set(SP_RECT(rc.item), r.min()[NR::X], r.min()[NR::Y], r.dimensions()[NR::X], r.dimensions()[NR::Y]);
456 if ( rc.rx != 0.0 ) {
457 sp_rect_set_rx (SP_RECT(rc.item), TRUE, rc.rx);
458 }
459 if ( rc.ry != 0.0 ) {
460 if (rc.rx == 0.0)
461 sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, MIN(r.dimensions()[NR::X], r.dimensions()[NR::Y])/2));
462 else
463 sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, r.dimensions()[NR::Y]));
464 }
466 // status text
467 GString *xs = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::X], desktop->namedview->getDefaultMetric());
468 GString *ys = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::Y], desktop->namedview->getDefaultMetric());
469 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);
470 g_string_free(xs, FALSE);
471 g_string_free(ys, FALSE);
472 }
474 static void sp_rect_finish(SPRectContext *rc)
475 {
476 rc->_message_context->clear();
478 if ( rc->item != NULL ) {
479 SPDesktop * dt;
481 dt = SP_EVENT_CONTEXT_DESKTOP(rc);
483 SP_OBJECT(rc->item)->updateRepr();
485 SP_DT_SELECTION(dt)->set(rc->item);
486 sp_document_done(SP_DT_DOCUMENT(dt));
488 rc->item = NULL;
489 }
490 }
492 /*
493 Local Variables:
494 mode:c++
495 c-file-style:"stroustrup"
496 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
497 indent-tabs-mode:nil
498 fill-column:99
499 End:
500 */
501 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :