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 "pixmaps/cursor-rect.pixbuf"
33 #include "rect-context.h"
34 #include "sp-metrics.h"
35 #include <glibmm/i18n.h>
36 #include "object-edit.h"
37 #include "xml/repr.h"
38 #include "xml/node-event-vector.h"
39 #include "prefs-utils.h"
40 #include "context-fns.h"
42 static void sp_rect_context_class_init(SPRectContextClass *klass);
43 static void sp_rect_context_init(SPRectContext *rect_context);
44 static void sp_rect_context_dispose(GObject *object);
46 static void sp_rect_context_setup(SPEventContext *ec);
47 static void sp_rect_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
49 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event);
50 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
52 static void sp_rect_drag(SPRectContext &rc, NR::Point const pt, guint state);
53 static void sp_rect_finish(SPRectContext *rc);
55 static SPEventContextClass *parent_class;
58 GtkType sp_rect_context_get_type()
59 {
60 static GType type = 0;
61 if (!type) {
62 GTypeInfo info = {
63 sizeof(SPRectContextClass),
64 NULL, NULL,
65 (GClassInitFunc) sp_rect_context_class_init,
66 NULL, NULL,
67 sizeof(SPRectContext),
68 4,
69 (GInstanceInitFunc) sp_rect_context_init,
70 NULL, /* value_table */
71 };
72 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPRectContext", &info, (GTypeFlags) 0);
73 }
74 return type;
75 }
77 static void sp_rect_context_class_init(SPRectContextClass *klass)
78 {
79 GObjectClass *object_class = (GObjectClass *) klass;
80 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
82 parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
84 object_class->dispose = sp_rect_context_dispose;
86 event_context_class->setup = sp_rect_context_setup;
87 event_context_class->set = sp_rect_context_set;
88 event_context_class->root_handler = sp_rect_context_root_handler;
89 event_context_class->item_handler = sp_rect_context_item_handler;
90 }
92 static void sp_rect_context_init(SPRectContext *rect_context)
93 {
94 SPEventContext *event_context = SP_EVENT_CONTEXT(rect_context);
96 event_context->cursor_shape = cursor_rect_xpm;
97 event_context->cursor_pixbuf = gdk_pixbuf_new_from_inline(
98 -1,
99 cursor_rect_pixbuf,
100 FALSE,
101 NULL);
102 event_context->hot_x = 4;
103 event_context->hot_y = 4;
104 event_context->xp = 0;
105 event_context->yp = 0;
106 event_context->tolerance = 0;
107 event_context->within_tolerance = false;
108 event_context->item_to_select = NULL;
110 event_context->shape_repr = NULL;
111 event_context->shape_knot_holder = NULL;
113 rect_context->item = NULL;
115 rect_context->rx = 0.0;
116 rect_context->ry = 0.0;
118 new (&rect_context->sel_changed_connection) sigc::connection();
119 }
121 static void sp_rect_context_dispose(GObject *object)
122 {
123 SPRectContext *rc = SP_RECT_CONTEXT(object);
124 SPEventContext *ec = SP_EVENT_CONTEXT(object);
126 ec->enableGrDrag(false);
128 rc->sel_changed_connection.disconnect();
129 rc->sel_changed_connection.~connection();
131 /* fixme: This is necessary because we do not grab */
132 if (rc->item) {
133 sp_rect_finish(rc);
134 }
136 if (ec->shape_knot_holder) {
137 sp_knot_holder_destroy(ec->shape_knot_holder);
138 ec->shape_knot_holder = NULL;
139 }
141 if (ec->shape_repr) { // remove old listener
142 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
143 Inkscape::GC::release(ec->shape_repr);
144 ec->shape_repr = 0;
145 }
147 if (rc->_message_context) {
148 delete rc->_message_context;
149 }
151 G_OBJECT_CLASS(parent_class)->dispose(object);
152 }
154 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
155 NULL, /* child_added */
156 NULL, /* child_removed */
157 ec_shape_event_attr_changed,
158 NULL, /* content_changed */
159 NULL /* order_changed */
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 if (ec->shape_knot_holder) { // destroy knotholder
172 sp_knot_holder_destroy(ec->shape_knot_holder);
173 ec->shape_knot_holder = NULL;
174 }
176 if (ec->shape_repr) { // remove old listener
177 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
178 Inkscape::GC::release(ec->shape_repr);
179 ec->shape_repr = 0;
180 }
182 SPItem *item = selection->singleItem();
183 if (item) {
184 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
185 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
186 if (shape_repr) {
187 ec->shape_repr = shape_repr;
188 Inkscape::GC::anchor(shape_repr);
189 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
190 }
191 }
192 }
194 static void sp_rect_context_setup(SPEventContext *ec)
195 {
196 SPRectContext *rc = SP_RECT_CONTEXT(ec);
198 if (((SPEventContextClass *) parent_class)->setup) {
199 ((SPEventContextClass *) parent_class)->setup(ec);
200 }
202 SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
203 if (item) {
204 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
205 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
206 if (shape_repr) {
207 ec->shape_repr = shape_repr;
208 Inkscape::GC::anchor(shape_repr);
209 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
210 }
211 }
213 rc->sel_changed_connection.disconnect();
214 rc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
215 sigc::bind(sigc::ptr_fun(&sp_rect_context_selection_changed), (gpointer)rc)
216 );
218 sp_event_context_read(ec, "rx");
219 sp_event_context_read(ec, "ry");
221 if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
222 ec->enableSelectionCue();
223 }
225 if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
226 ec->enableGrDrag();
227 }
229 rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
230 }
232 static void sp_rect_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
233 {
234 SPRectContext *rc = SP_RECT_CONTEXT(ec);
236 /* fixme: Proper error handling for non-numeric data. Use a locale-independent function like
237 * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
238 if ( strcmp(key, "rx") == 0 ) {
239 rc->rx = ( val
240 ? g_ascii_strtod (val, NULL)
241 : 0.0 );
242 } else if ( strcmp(key, "ry") == 0 ) {
243 rc->ry = ( val
244 ? g_ascii_strtod (val, NULL)
245 : 0.0 );
246 }
247 }
249 static gint sp_rect_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
250 {
251 SPDesktop *desktop = event_context->desktop;
253 gint ret = FALSE;
255 switch (event->type) {
256 case GDK_BUTTON_PRESS:
257 if ( event->button.button == 1 ) {
258 Inkscape::setup_for_drag_start(desktop, event_context, event);
259 ret = TRUE;
260 }
261 break;
262 // motion and release are always on root (why?)
263 default:
264 break;
265 }
267 if (((SPEventContextClass *) parent_class)->item_handler) {
268 ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
269 }
271 return ret;
272 }
274 static gint sp_rect_context_root_handler(SPEventContext *event_context, GdkEvent *event)
275 {
276 static bool dragging;
278 SPDesktop *desktop = event_context->desktop;
279 Inkscape::Selection *selection = sp_desktop_selection (desktop);
281 SPRectContext *rc = SP_RECT_CONTEXT(event_context);
283 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
285 gint ret = FALSE;
286 switch (event->type) {
287 case GDK_BUTTON_PRESS:
288 if ( event->button.button == 1 ) {
289 NR::Point const button_w(event->button.x,
290 event->button.y);
292 // save drag origin
293 event_context->xp = (gint) button_w[NR::X];
294 event_context->yp = (gint) button_w[NR::Y];
295 event_context->within_tolerance = true;
297 // remember clicked item, disregarding groups, honoring Alt
298 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
300 dragging = true;
302 /* Position center */
303 NR::Point const button_dt(desktop->w2d(button_w));
305 /* Snap center */
306 SnapManager const &m = desktop->namedview->snap_manager;
307 rc->center = m.freeSnap(Inkscape::Snapper::SNAP_POINT | Inkscape::Snapper::BBOX_POINT,
308 button_dt, rc->item).getPoint();
310 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
311 ( GDK_KEY_PRESS_MASK |
312 GDK_BUTTON_RELEASE_MASK |
313 GDK_POINTER_MOTION_MASK |
314 GDK_BUTTON_PRESS_MASK ),
315 NULL, event->button.time);
316 ret = TRUE;
317 }
318 break;
319 case GDK_MOTION_NOTIFY:
320 if ( dragging
321 && ( event->motion.state & GDK_BUTTON1_MASK ) )
322 {
323 if ( event_context->within_tolerance
324 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
325 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
326 break; // do not drag if we're within tolerance from origin
327 }
328 // Once the user has moved farther than tolerance from the original location
329 // (indicating they intend to draw, not click), then always process the
330 // motion notify coordinates as given (no snapping back to origin)
331 event_context->within_tolerance = false;
333 NR::Point const motion_w(event->motion.x,
334 event->motion.y);
335 NR::Point const motion_dt(desktop->w2d(motion_w));
336 sp_rect_drag(*rc, motion_dt, event->motion.state);
337 ret = TRUE;
338 }
339 break;
340 case GDK_BUTTON_RELEASE:
341 event_context->xp = event_context->yp = 0;
342 if ( event->button.button == 1 ) {
343 dragging = false;
345 if (!event_context->within_tolerance) {
346 // we've been dragging, finish the rect
347 sp_rect_finish(rc);
348 } else if (event_context->item_to_select) {
349 // no dragging, select clicked item if any
350 if (event->button.state & GDK_SHIFT_MASK) {
351 selection->toggle(event_context->item_to_select);
352 } else {
353 selection->set(event_context->item_to_select);
354 }
355 } else {
356 // click in an empty space
357 selection->clear();
358 }
360 event_context->item_to_select = NULL;
361 ret = TRUE;
362 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
363 event->button.time);
364 }
365 break;
366 case GDK_KEY_PRESS:
367 switch (get_group0_keyval (&event->key)) {
368 case GDK_Alt_L:
369 case GDK_Alt_R:
370 case GDK_Control_L:
371 case GDK_Control_R:
372 case GDK_Shift_L:
373 case GDK_Shift_R:
374 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
375 case GDK_Meta_R:
376 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
377 _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
378 _("<b>Shift</b>: draw around the starting point"),
379 NULL);
380 break;
381 case GDK_Up:
382 case GDK_Down:
383 case GDK_KP_Up:
384 case GDK_KP_Down:
385 // prevent the zoom field from activation
386 if (!MOD__CTRL_ONLY)
387 ret = TRUE;
388 break;
390 case GDK_x:
391 case GDK_X:
392 if (MOD__ALT_ONLY) {
393 desktop->setToolboxFocusTo ("altx-rect");
394 ret = TRUE;
395 }
396 break;
398 case GDK_Escape:
399 sp_desktop_selection(desktop)->clear();
400 //TODO: make dragging escapable by Esc
401 default:
402 break;
403 }
404 break;
405 case GDK_KEY_RELEASE:
406 switch (get_group0_keyval (&event->key)) {
407 case GDK_Alt_L:
408 case GDK_Alt_R:
409 case GDK_Control_L:
410 case GDK_Control_R:
411 case GDK_Shift_L:
412 case GDK_Shift_R:
413 case GDK_Meta_L: // Meta is when you press Shift+Alt
414 case GDK_Meta_R:
415 event_context->defaultMessageContext()->clear();
416 break;
417 default:
418 break;
419 }
420 break;
421 default:
422 break;
423 }
425 if (!ret) {
426 if (((SPEventContextClass *) parent_class)->root_handler) {
427 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
428 }
429 }
431 return ret;
432 }
434 static void sp_rect_drag(SPRectContext &rc, NR::Point const pt, guint state)
435 {
436 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
438 if (!rc.item) {
440 if (Inkscape::have_viable_layer(desktop, rc._message_context) == false) {
441 return;
442 }
444 /* Create object */
445 Inkscape::XML::Node *repr = sp_repr_new("svg:rect");
447 /* Set style */
448 sp_desktop_apply_style_tool (desktop, repr, "tools.shapes.rect", false);
450 rc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
451 Inkscape::GC::release(repr);
452 rc.item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
453 rc.item->updateRepr();
454 }
456 NR::Rect const r = Inkscape::snap_rectangular_box(desktop, rc.item, pt, rc.center, state);
458 sp_rect_position_set(SP_RECT(rc.item), r.min()[NR::X], r.min()[NR::Y], r.dimensions()[NR::X], r.dimensions()[NR::Y]);
459 if ( rc.rx != 0.0 ) {
460 sp_rect_set_rx (SP_RECT(rc.item), TRUE, rc.rx);
461 }
462 if ( rc.ry != 0.0 ) {
463 if (rc.rx == 0.0)
464 sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, MIN(r.dimensions()[NR::X], r.dimensions()[NR::Y])/2));
465 else
466 sp_rect_set_ry (SP_RECT(rc.item), TRUE, CLAMP(rc.ry, 0, r.dimensions()[NR::Y]));
467 }
469 // status text
470 GString *xs = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::X], desktop->namedview->getDefaultMetric());
471 GString *ys = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::Y], desktop->namedview->getDefaultMetric());
472 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);
473 g_string_free(xs, FALSE);
474 g_string_free(ys, FALSE);
475 }
477 static void sp_rect_finish(SPRectContext *rc)
478 {
479 rc->_message_context->clear();
481 if ( rc->item != NULL ) {
482 SPDesktop * dt;
484 dt = SP_EVENT_CONTEXT_DESKTOP(rc);
486 SP_OBJECT(rc->item)->updateRepr();
488 sp_desktop_selection(dt)->set(rc->item);
489 sp_document_done(sp_desktop_document(dt), SP_VERB_CONTEXT_RECT,
490 _("Create rectangle"));
492 rc->item = NULL;
493 }
494 }
496 /*
497 Local Variables:
498 mode:c++
499 c-file-style:"stroustrup"
500 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
501 indent-tabs-mode:nil
502 fill-column:99
503 End:
504 */
505 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :