1 #define __SP_KNOT_C__
3 /** \file
4 * SPKnot implementation
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * bulia byak <buliabyak@users.sf.net>
9 *
10 * Copyright (C) 1999-2005 authors
11 * Copyright (C) 2001-2002 Ximian, Inc.
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
19 #include <gdk/gdkkeysyms.h>
20 #include <glibmm/i18n.h>
21 #include "helper/sp-marshal.h"
22 #include "display/sodipodi-ctrl.h"
23 #include "desktop.h"
24 #include "desktop-handles.h"
25 #include "knot.h"
26 #include "document.h"
27 #include "preferences.h"
28 #include "message-stack.h"
29 #include "message-context.h"
30 #include "event-context.h"
33 #define KNOT_EVENT_MASK (GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | \
34 GDK_POINTER_MOTION_MASK | \
35 GDK_POINTER_MOTION_HINT_MASK | \
36 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK)
38 static bool nograb = false;
40 static bool grabbed = FALSE;
41 static bool moved = FALSE;
43 static gint xp = 0, yp = 0; // where drag started
44 static gint tolerance = 0;
45 static bool within_tolerance = false;
47 static bool transform_escaped = false; // true iff resize or rotate was cancelled by esc.
49 enum {
50 EVENT,
51 CLICKED,
52 DOUBLECLICKED,
53 GRABBED,
54 UNGRABBED,
55 MOVED,
56 REQUEST,
57 DISTANCE,
58 LAST_SIGNAL
59 };
61 static void sp_knot_class_init(SPKnotClass *klass);
62 static void sp_knot_init(SPKnot *knot);
63 static void sp_knot_dispose(GObject *object);
65 static int sp_knot_handler(SPCanvasItem *item, GdkEvent *event, SPKnot *knot);
66 static void sp_knot_set_ctrl_state(SPKnot *knot);
68 static GObjectClass *parent_class;
69 static guint knot_signals[LAST_SIGNAL] = { 0 };
71 /**
72 * Registers SPKnot class and returns its type number.
73 */
74 GType sp_knot_get_type()
75 {
76 static GType type = 0;
77 if (!type) {
78 GTypeInfo info = {
79 sizeof(SPKnotClass),
80 NULL, /* base_init */
81 NULL, /* base_finalize */
82 (GClassInitFunc) sp_knot_class_init,
83 NULL, /* class_finalize */
84 NULL, /* class_data */
85 sizeof (SPKnot),
86 16, /* n_preallocs */
87 (GInstanceInitFunc) sp_knot_init,
88 NULL
89 };
90 type = g_type_register_static (G_TYPE_OBJECT, "SPKnot", &info, (GTypeFlags) 0);
91 }
92 return type;
93 }
95 /**
96 * SPKnot vtable initialization.
97 */
98 static void sp_knot_class_init(SPKnotClass *klass)
99 {
100 GObjectClass *object_class = (GObjectClass*)klass;
102 parent_class = (GObjectClass*) g_type_class_peek_parent(klass);
104 object_class->dispose = sp_knot_dispose;
106 knot_signals[EVENT] = g_signal_new("event",
107 G_TYPE_FROM_CLASS(klass),
108 G_SIGNAL_RUN_LAST,
109 G_STRUCT_OFFSET(SPKnotClass, event),
110 NULL, NULL,
111 sp_marshal_BOOLEAN__POINTER,
112 G_TYPE_BOOLEAN, 1,
113 GDK_TYPE_EVENT);
115 knot_signals[CLICKED] = g_signal_new("clicked",
116 G_TYPE_FROM_CLASS(klass),
117 G_SIGNAL_RUN_FIRST,
118 G_STRUCT_OFFSET(SPKnotClass, clicked),
119 NULL, NULL,
120 sp_marshal_NONE__UINT,
121 G_TYPE_NONE, 1,
122 G_TYPE_UINT);
124 knot_signals[DOUBLECLICKED] = g_signal_new("doubleclicked",
125 G_TYPE_FROM_CLASS(klass),
126 G_SIGNAL_RUN_FIRST,
127 G_STRUCT_OFFSET(SPKnotClass, doubleclicked),
128 NULL, NULL,
129 sp_marshal_NONE__UINT,
130 G_TYPE_NONE, 1,
131 G_TYPE_UINT);
133 knot_signals[GRABBED] = g_signal_new("grabbed",
134 G_TYPE_FROM_CLASS(klass),
135 G_SIGNAL_RUN_FIRST,
136 G_STRUCT_OFFSET(SPKnotClass, grabbed),
137 NULL, NULL,
138 sp_marshal_NONE__UINT,
139 G_TYPE_NONE, 1,
140 G_TYPE_UINT);
142 knot_signals[UNGRABBED] = g_signal_new("ungrabbed",
143 G_TYPE_FROM_CLASS(klass),
144 G_SIGNAL_RUN_FIRST,
145 G_STRUCT_OFFSET(SPKnotClass, ungrabbed),
146 NULL, NULL,
147 sp_marshal_NONE__UINT,
148 G_TYPE_NONE, 1,
149 G_TYPE_UINT);
151 knot_signals[MOVED] = g_signal_new("moved",
152 G_TYPE_FROM_CLASS(klass),
153 G_SIGNAL_RUN_FIRST,
154 G_STRUCT_OFFSET(SPKnotClass, moved),
155 NULL, NULL,
156 sp_marshal_NONE__POINTER_UINT,
157 G_TYPE_NONE, 2,
158 G_TYPE_POINTER, G_TYPE_UINT);
160 knot_signals[REQUEST] = g_signal_new("request",
161 G_TYPE_FROM_CLASS(klass),
162 G_SIGNAL_RUN_LAST,
163 G_STRUCT_OFFSET(SPKnotClass, request),
164 NULL, NULL,
165 sp_marshal_BOOLEAN__POINTER_UINT,
166 G_TYPE_BOOLEAN, 2,
167 G_TYPE_POINTER, G_TYPE_UINT);
169 knot_signals[DISTANCE] = g_signal_new("distance",
170 G_TYPE_FROM_CLASS(klass),
171 G_SIGNAL_RUN_LAST,
172 G_STRUCT_OFFSET(SPKnotClass, distance),
173 NULL, NULL,
174 sp_marshal_DOUBLE__POINTER_UINT,
175 G_TYPE_DOUBLE, 2,
176 G_TYPE_POINTER, G_TYPE_UINT);
178 const gchar *nograbenv = getenv("INKSCAPE_NO_GRAB");
179 nograb = (nograbenv && *nograbenv && (*nograbenv != '0'));
180 }
182 /**
183 * Callback for SPKnot initialization.
184 */
185 static void sp_knot_init(SPKnot *knot)
186 {
187 knot->desktop = NULL;
188 knot->item = NULL;
189 knot->flags = 0;
191 knot->size = 8;
192 knot->pos = Geom::Point(0, 0);
193 knot->grabbed_rel_pos = Geom::Point(0, 0);
194 knot->anchor = GTK_ANCHOR_CENTER;
195 knot->shape = SP_KNOT_SHAPE_SQUARE;
196 knot->mode = SP_KNOT_MODE_XOR;
197 knot->tip = NULL;
198 knot->_event_handler_id = 0;
199 knot->pressure = 0;
201 knot->fill[SP_KNOT_STATE_NORMAL] = 0xffffff00;
202 knot->fill[SP_KNOT_STATE_MOUSEOVER] = 0xff0000ff;
203 knot->fill[SP_KNOT_STATE_DRAGGING] = 0x0000ffff;
205 knot->stroke[SP_KNOT_STATE_NORMAL] = 0x01000000;
206 knot->stroke[SP_KNOT_STATE_MOUSEOVER] = 0x01000000;
207 knot->stroke[SP_KNOT_STATE_DRAGGING] = 0x01000000;
209 knot->image[SP_KNOT_STATE_NORMAL] = NULL;
210 knot->image[SP_KNOT_STATE_MOUSEOVER] = NULL;
211 knot->image[SP_KNOT_STATE_DRAGGING] = NULL;
213 knot->cursor[SP_KNOT_STATE_NORMAL] = NULL;
214 knot->cursor[SP_KNOT_STATE_MOUSEOVER] = NULL;
215 knot->cursor[SP_KNOT_STATE_DRAGGING] = NULL;
217 knot->saved_cursor = NULL;
218 knot->pixbuf = NULL;
219 }
221 /**
222 * Called before SPKnot destruction.
223 */
224 static void sp_knot_dispose(GObject *object)
225 {
226 SPKnot *knot = (SPKnot *) object;
228 if ((knot->flags & SP_KNOT_GRABBED) && gdk_pointer_is_grabbed ()) {
229 // This happens e.g. when deleting a node in node tool while dragging it
230 gdk_pointer_ungrab (GDK_CURRENT_TIME);
231 }
233 if (knot->_event_handler_id > 0)
234 {
235 g_signal_handler_disconnect(G_OBJECT (knot->item), knot->_event_handler_id);
236 knot->_event_handler_id = 0;
237 }
239 if (knot->item) {
240 gtk_object_destroy (GTK_OBJECT (knot->item));
241 knot->item = NULL;
242 }
244 for (gint i = 0; i < SP_KNOT_VISIBLE_STATES; i++) {
245 if (knot->cursor[i]) {
246 gdk_cursor_unref(knot->cursor[i]);
247 knot->cursor[i] = NULL;
248 }
249 }
251 if (knot->tip) {
252 g_free(knot->tip);
253 knot->tip = NULL;
254 }
256 if (parent_class->dispose) {
257 (*parent_class->dispose) (object);
258 }
259 }
261 /**
262 * Update knot for dragging and tell canvas an item was grabbed.
263 */
264 void sp_knot_start_dragging(SPKnot *knot, Geom::Point const &p, gint x, gint y, guint32 etime)
265 {
266 // save drag origin
267 xp = x;
268 yp = y;
269 within_tolerance = true;
271 knot->grabbed_rel_pos = p - knot->pos;
272 knot->drag_origin = knot->pos;
273 if (!nograb) {
274 sp_canvas_item_grab(knot->item,
275 KNOT_EVENT_MASK,
276 knot->cursor[SP_KNOT_STATE_DRAGGING],
277 etime);
278 }
279 sp_knot_set_flag(knot, SP_KNOT_GRABBED, TRUE);
280 grabbed = TRUE;
281 }
283 /**
284 * Called to handle events on knots.
285 */
286 static int sp_knot_handler(SPCanvasItem */*item*/, GdkEvent *event, SPKnot *knot)
287 {
288 g_assert(knot != NULL);
289 g_assert(SP_IS_KNOT(knot));
291 /* Run client universal event handler, if present */
293 gboolean consumed = FALSE;
295 g_signal_emit(knot, knot_signals[EVENT], 0, event, &consumed);
297 if (consumed) {
298 return TRUE;
299 }
301 g_object_ref(knot);
302 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
303 tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
305 switch (event->type) {
306 case GDK_2BUTTON_PRESS:
307 if (event->button.button == 1) {
308 g_signal_emit(knot, knot_signals[DOUBLECLICKED], 0, event->button.state);
310 grabbed = FALSE;
311 moved = FALSE;
312 consumed = TRUE;
313 }
314 break;
315 case GDK_BUTTON_PRESS:
316 if (event->button.button == 1 && !knot->desktop->event_context->space_panning) {
317 Geom::Point const p = knot->desktop->w2d(Geom::Point(event->button.x, event->button.y));
318 sp_knot_start_dragging(knot, p, (gint) event->button.x, (gint) event->button.y, event->button.time);
319 consumed = TRUE;
320 }
321 break;
322 case GDK_BUTTON_RELEASE:
323 if (event->button.button == 1 && !knot->desktop->event_context->space_panning) {
324 knot->pressure = 0;
325 if (transform_escaped) {
326 transform_escaped = false;
327 consumed = TRUE;
328 } else {
329 sp_knot_set_flag(knot, SP_KNOT_GRABBED, FALSE);
330 if (!nograb) {
331 sp_canvas_item_ungrab(knot->item, event->button.time);
332 }
333 if (moved) {
334 sp_knot_set_flag(knot,
335 SP_KNOT_DRAGGING,
336 FALSE);
337 g_signal_emit(knot,
338 knot_signals[UNGRABBED], 0,
339 event->button.state);
340 knot->_ungrabbed_signal.emit(knot);
341 } else {
342 g_signal_emit(knot,
343 knot_signals[CLICKED], 0,
344 event->button.state);
345 knot->_click_signal.emit(knot, event->button.state);
346 }
347 grabbed = FALSE;
348 moved = FALSE;
349 consumed = TRUE;
350 }
351 }
352 break;
353 case GDK_MOTION_NOTIFY:
354 if (grabbed && !knot->desktop->event_context->space_panning) {
355 consumed = TRUE;
357 if ( within_tolerance
358 && ( abs( (gint) event->motion.x - xp ) < tolerance )
359 && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
360 break; // do not drag if we're within tolerance from origin
361 }
363 // Once the user has moved farther than tolerance from the original location
364 // (indicating they intend to move the object, not click), then always process the
365 // motion notify coordinates as given (no snapping back to origin)
366 within_tolerance = false;
368 if (gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &knot->pressure))
369 knot->pressure = CLAMP (knot->pressure, 0, 1);
370 else
371 knot->pressure = 0.5;
373 if (!moved) {
374 g_signal_emit(knot,
375 knot_signals[GRABBED], 0,
376 event->motion.state);
377 sp_knot_set_flag(knot,
378 SP_KNOT_DRAGGING,
379 TRUE);
380 }
381 Geom::Point const motion_w(event->motion.x, event->motion.y);
382 Geom::Point const motion_dt = knot->desktop->w2d(motion_w);
383 Geom::Point p = motion_dt - knot->grabbed_rel_pos;
384 sp_knot_request_position (knot, p, event->motion.state);
385 knot->desktop->scroll_to_point (motion_dt);
386 knot->desktop->set_coordinate_status(knot->pos); // display the coordinate of knot, not cursor - they may be different!
387 if (event->motion.state & GDK_BUTTON1_MASK)
388 gobble_motion_events(GDK_BUTTON1_MASK);
389 moved = TRUE;
390 }
391 break;
392 case GDK_ENTER_NOTIFY:
393 sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, TRUE);
394 sp_knot_set_flag(knot, SP_KNOT_GRABBED, FALSE);
396 if (knot->tip) {
397 knot->desktop->event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, knot->tip);
398 }
400 grabbed = FALSE;
401 moved = FALSE;
402 consumed = TRUE;
403 break;
404 case GDK_LEAVE_NOTIFY:
405 sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, FALSE);
406 sp_knot_set_flag(knot, SP_KNOT_GRABBED, FALSE);
408 if (knot->tip) {
409 knot->desktop->event_context->defaultMessageContext()->clear();
410 }
412 grabbed = FALSE;
413 moved = FALSE;
415 consumed = TRUE;
416 break;
417 case GDK_KEY_PRESS: // keybindings for knot
418 switch (get_group0_keyval(&event->key)) {
419 case GDK_Escape:
420 sp_knot_set_flag(knot, SP_KNOT_GRABBED, FALSE);
421 if (!nograb) {
422 sp_canvas_item_ungrab(knot->item, event->button.time);
423 }
424 if (moved) {
425 sp_knot_set_flag(knot,
426 SP_KNOT_DRAGGING,
427 FALSE);
428 g_signal_emit(knot,
429 knot_signals[UNGRABBED], 0,
430 event->button.state);
431 sp_document_undo(sp_desktop_document(knot->desktop));
432 knot->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Node or handle drag canceled."));
433 transform_escaped = true;
434 consumed = TRUE;
435 }
436 grabbed = FALSE;
437 moved = FALSE;
438 break;
439 default:
440 consumed = FALSE;
441 break;
442 }
443 break;
444 default:
445 break;
446 }
448 g_object_unref(knot);
450 return consumed;
451 }
453 /**
454 * Return new knot object.
455 */
456 SPKnot *sp_knot_new(SPDesktop *desktop, const gchar *tip)
457 {
458 g_return_val_if_fail(desktop != NULL, NULL);
460 SPKnot * knot = (SPKnot*) g_object_new(SP_TYPE_KNOT, 0);
462 knot->desktop = desktop;
463 knot->flags = SP_KNOT_VISIBLE;
464 if (tip) {
465 knot->tip = g_strdup (tip);
466 }
468 knot->item = sp_canvas_item_new(sp_desktop_controls (desktop),
469 SP_TYPE_CTRL,
470 "anchor", GTK_ANCHOR_CENTER,
471 "size", 8.0,
472 "filled", TRUE,
473 "fill_color", 0xffffff00,
474 "stroked", TRUE,
475 "stroke_color", 0x01000000,
476 "mode", SP_KNOT_MODE_XOR,
477 NULL);
479 knot->_event_handler_id = gtk_signal_connect(GTK_OBJECT(knot->item), "event",
480 GTK_SIGNAL_FUNC(sp_knot_handler), knot);
482 return knot;
483 }
485 /**
486 * Show knot on its canvas.
487 */
488 void sp_knot_show(SPKnot *knot)
489 {
490 g_return_if_fail(knot != NULL);
491 g_return_if_fail(SP_IS_KNOT (knot));
493 sp_knot_set_flag(knot, SP_KNOT_VISIBLE, TRUE);
494 }
496 /**
497 * Hide knot on its canvas.
498 */
499 void sp_knot_hide(SPKnot *knot)
500 {
501 g_return_if_fail(knot != NULL);
502 g_return_if_fail(SP_IS_KNOT(knot));
504 sp_knot_set_flag(knot, SP_KNOT_VISIBLE, FALSE);
505 }
507 /**
508 * Request or set new position for knot.
509 */
510 void sp_knot_request_position(SPKnot *knot, Geom::Point const &p, guint state)
511 {
512 g_return_if_fail(knot != NULL);
513 g_return_if_fail(SP_IS_KNOT(knot));
515 gboolean done = FALSE;
517 g_signal_emit(knot,
518 knot_signals[REQUEST], 0,
519 &p,
520 state,
521 &done);
523 /* If user did not complete, we simply move knot to new position */
525 if (!done) {
526 sp_knot_set_position (knot, p, state);
527 }
528 }
530 /**
531 * Return distance of point to knot's position; unused.
532 */
533 gdouble sp_knot_distance(SPKnot * knot, Geom::Point const &p, guint state)
534 {
535 g_return_val_if_fail(knot != NULL, 1e18);
536 g_return_val_if_fail(SP_IS_KNOT(knot), 1e18);
538 gdouble distance = Geom::L2(p - knot->pos);
540 g_signal_emit(knot,
541 knot_signals[DISTANCE], 0,
542 &p,
543 state,
544 &distance);
546 return distance;
547 }
549 /**
550 * Move knot to new position.
551 */
552 void sp_knot_set_position(SPKnot *knot, Geom::Point const &p, guint state)
553 {
554 g_return_if_fail(knot != NULL);
555 g_return_if_fail(SP_IS_KNOT (knot));
557 knot->pos = p;
559 if (knot->item) {
560 SP_CTRL(knot->item)->moveto (p);
561 }
563 g_signal_emit(knot,
564 knot_signals[MOVED], 0,
565 &p,
566 state);
567 knot->_moved_signal.emit(knot, p, state);
568 }
570 /**
571 * Move knot to new position, without emitting a MOVED signal.
572 */
573 void sp_knot_moveto(SPKnot *knot, Geom::Point const &p)
574 {
575 g_return_if_fail(knot != NULL);
576 g_return_if_fail(SP_IS_KNOT(knot));
578 knot->pos = p;
580 if (knot->item) {
581 SP_CTRL(knot->item)->moveto (p);
582 }
583 }
585 /**
586 * Returns position of knot.
587 */
588 Geom::Point sp_knot_position(SPKnot const *knot)
589 {
590 g_assert(knot != NULL);
591 g_assert(SP_IS_KNOT (knot));
593 return knot->pos;
594 }
596 /**
597 * Set flag in knot, with side effects.
598 */
599 void sp_knot_set_flag(SPKnot *knot, guint flag, bool set)
600 {
601 g_assert(knot != NULL);
602 g_assert(SP_IS_KNOT(knot));
604 if (set) {
605 knot->flags |= flag;
606 } else {
607 knot->flags &= ~flag;
608 }
610 switch (flag) {
611 case SP_KNOT_VISIBLE:
612 if (set) {
613 sp_canvas_item_show(knot->item);
614 } else {
615 sp_canvas_item_hide(knot->item);
616 }
617 break;
618 case SP_KNOT_MOUSEOVER:
619 case SP_KNOT_DRAGGING:
620 sp_knot_set_ctrl_state(knot);
621 break;
622 case SP_KNOT_GRABBED:
623 break;
624 default:
625 g_assert_not_reached();
626 break;
627 }
628 }
630 /**
631 * Update knot's pixbuf and set its control state.
632 */
633 void sp_knot_update_ctrl(SPKnot *knot)
634 {
635 if (!knot->item) {
636 return;
637 }
639 gtk_object_set(GTK_OBJECT(knot->item), "shape", knot->shape, NULL);
640 gtk_object_set(GTK_OBJECT(knot->item), "mode", knot->mode, NULL);
641 gtk_object_set(GTK_OBJECT(knot->item), "size", (gdouble) knot->size, NULL);
642 gtk_object_set(GTK_OBJECT(knot->item), "anchor", knot->anchor, NULL);
643 if (knot->pixbuf) {
644 gtk_object_set(GTK_OBJECT (knot->item), "pixbuf", knot->pixbuf, NULL);
645 }
647 sp_knot_set_ctrl_state(knot);
648 }
650 /**
651 * Set knot control state (dragging/mouseover/normal).
652 */
653 static void sp_knot_set_ctrl_state(SPKnot *knot)
654 {
655 if (knot->flags & SP_KNOT_DRAGGING) {
656 gtk_object_set(GTK_OBJECT (knot->item),
657 "fill_color",
658 knot->fill[SP_KNOT_STATE_DRAGGING],
659 NULL);
660 gtk_object_set(GTK_OBJECT (knot->item),
661 "stroke_color",
662 knot->stroke[SP_KNOT_STATE_DRAGGING],
663 NULL);
664 } else if (knot->flags & SP_KNOT_MOUSEOVER) {
665 gtk_object_set(GTK_OBJECT(knot->item),
666 "fill_color",
667 knot->fill[SP_KNOT_STATE_MOUSEOVER],
668 NULL);
669 gtk_object_set(GTK_OBJECT(knot->item),
670 "stroke_color",
671 knot->stroke[SP_KNOT_STATE_MOUSEOVER],
672 NULL);
673 } else {
674 gtk_object_set(GTK_OBJECT(knot->item),
675 "fill_color",
676 knot->fill[SP_KNOT_STATE_NORMAL],
677 NULL);
678 gtk_object_set(GTK_OBJECT(knot->item),
679 "stroke_color",
680 knot->stroke[SP_KNOT_STATE_NORMAL],
681 NULL);
682 }
683 }
687 /*
688 Local Variables:
689 mode:c++
690 c-file-style:"stroustrup"
691 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
692 indent-tabs-mode:nil
693 fill-column:99
694 End:
695 */
696 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :