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 "prefs-utils.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_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK)
37 static bool nograb = false;
39 static bool grabbed = FALSE;
40 static bool moved = FALSE;
42 static gint xp = 0, yp = 0; // where drag started
43 static gint tolerance = 0;
44 static bool within_tolerance = false;
46 static bool transform_escaped = false; // true iff resize or rotate was cancelled by esc.
48 enum {
49 EVENT,
50 CLICKED,
51 DOUBLECLICKED,
52 GRABBED,
53 UNGRABBED,
54 MOVED,
55 REQUEST,
56 DISTANCE,
57 LAST_SIGNAL
58 };
60 static void sp_knot_class_init(SPKnotClass *klass);
61 static void sp_knot_init(SPKnot *knot);
62 static void sp_knot_dispose(GObject *object);
64 static int sp_knot_handler(SPCanvasItem *item, GdkEvent *event, SPKnot *knot);
65 static void sp_knot_set_ctrl_state(SPKnot *knot);
67 static GObjectClass *parent_class;
68 static guint knot_signals[LAST_SIGNAL] = { 0 };
70 /**
71 * Registers SPKnot class and returns its type number.
72 */
73 GType sp_knot_get_type()
74 {
75 static GType type = 0;
76 if (!type) {
77 GTypeInfo info = {
78 sizeof(SPKnotClass),
79 NULL, /* base_init */
80 NULL, /* base_finalize */
81 (GClassInitFunc) sp_knot_class_init,
82 NULL, /* class_finalize */
83 NULL, /* class_data */
84 sizeof (SPKnot),
85 16, /* n_preallocs */
86 (GInstanceInitFunc) sp_knot_init,
87 NULL
88 };
89 type = g_type_register_static (G_TYPE_OBJECT, "SPKnot", &info, (GTypeFlags) 0);
90 }
91 return type;
92 }
94 /**
95 * SPKnot vtable initialization.
96 */
97 static void sp_knot_class_init(SPKnotClass *klass)
98 {
99 GObjectClass *object_class = (GObjectClass*)klass;
101 parent_class = (GObjectClass*) g_type_class_peek_parent(klass);
103 object_class->dispose = sp_knot_dispose;
105 knot_signals[EVENT] = g_signal_new("event",
106 G_TYPE_FROM_CLASS(klass),
107 G_SIGNAL_RUN_LAST,
108 G_STRUCT_OFFSET(SPKnotClass, event),
109 NULL, NULL,
110 sp_marshal_BOOLEAN__POINTER,
111 G_TYPE_BOOLEAN, 1,
112 GDK_TYPE_EVENT);
114 knot_signals[CLICKED] = g_signal_new("clicked",
115 G_TYPE_FROM_CLASS(klass),
116 G_SIGNAL_RUN_FIRST,
117 G_STRUCT_OFFSET(SPKnotClass, clicked),
118 NULL, NULL,
119 sp_marshal_NONE__UINT,
120 G_TYPE_NONE, 1,
121 G_TYPE_UINT);
123 knot_signals[DOUBLECLICKED] = g_signal_new("doubleclicked",
124 G_TYPE_FROM_CLASS(klass),
125 G_SIGNAL_RUN_FIRST,
126 G_STRUCT_OFFSET(SPKnotClass, doubleclicked),
127 NULL, NULL,
128 sp_marshal_NONE__UINT,
129 G_TYPE_NONE, 1,
130 G_TYPE_UINT);
132 knot_signals[GRABBED] = g_signal_new("grabbed",
133 G_TYPE_FROM_CLASS(klass),
134 G_SIGNAL_RUN_FIRST,
135 G_STRUCT_OFFSET(SPKnotClass, grabbed),
136 NULL, NULL,
137 sp_marshal_NONE__UINT,
138 G_TYPE_NONE, 1,
139 G_TYPE_UINT);
141 knot_signals[UNGRABBED] = g_signal_new("ungrabbed",
142 G_TYPE_FROM_CLASS(klass),
143 G_SIGNAL_RUN_FIRST,
144 G_STRUCT_OFFSET(SPKnotClass, ungrabbed),
145 NULL, NULL,
146 sp_marshal_NONE__UINT,
147 G_TYPE_NONE, 1,
148 G_TYPE_UINT);
150 knot_signals[MOVED] = g_signal_new("moved",
151 G_TYPE_FROM_CLASS(klass),
152 G_SIGNAL_RUN_FIRST,
153 G_STRUCT_OFFSET(SPKnotClass, moved),
154 NULL, NULL,
155 sp_marshal_NONE__POINTER_UINT,
156 G_TYPE_NONE, 2,
157 G_TYPE_POINTER, G_TYPE_UINT);
159 knot_signals[REQUEST] = g_signal_new("request",
160 G_TYPE_FROM_CLASS(klass),
161 G_SIGNAL_RUN_LAST,
162 G_STRUCT_OFFSET(SPKnotClass, request),
163 NULL, NULL,
164 sp_marshal_BOOLEAN__POINTER_UINT,
165 G_TYPE_BOOLEAN, 2,
166 G_TYPE_POINTER, G_TYPE_UINT);
168 knot_signals[DISTANCE] = g_signal_new("distance",
169 G_TYPE_FROM_CLASS(klass),
170 G_SIGNAL_RUN_LAST,
171 G_STRUCT_OFFSET(SPKnotClass, distance),
172 NULL, NULL,
173 sp_marshal_DOUBLE__POINTER_UINT,
174 G_TYPE_DOUBLE, 2,
175 G_TYPE_POINTER, G_TYPE_UINT);
177 const gchar *nograbenv = getenv("INKSCAPE_NO_GRAB");
178 nograb = (nograbenv && *nograbenv && (*nograbenv != '0'));
179 }
181 /**
182 * Callback for SPKnot initialization.
183 */
184 static void sp_knot_init(SPKnot *knot)
185 {
186 knot->desktop = NULL;
187 knot->item = NULL;
188 knot->flags = 0;
190 knot->size = 8;
191 knot->pos = NR::Point(0, 0);
192 knot->grabbed_rel_pos = NR::Point(0, 0);
193 knot->anchor = GTK_ANCHOR_CENTER;
194 knot->shape = SP_KNOT_SHAPE_SQUARE;
195 knot->mode = SP_KNOT_MODE_XOR;
196 knot->tip = NULL;
197 knot->_event_handler_id = 0;
198 knot->pressure = 0;
200 knot->fill[SP_KNOT_STATE_NORMAL] = 0xffffff00;
201 knot->fill[SP_KNOT_STATE_MOUSEOVER] = 0xff0000ff;
202 knot->fill[SP_KNOT_STATE_DRAGGING] = 0x0000ffff;
204 knot->stroke[SP_KNOT_STATE_NORMAL] = 0x01000000;
205 knot->stroke[SP_KNOT_STATE_MOUSEOVER] = 0x01000000;
206 knot->stroke[SP_KNOT_STATE_DRAGGING] = 0x01000000;
208 knot->image[SP_KNOT_STATE_NORMAL] = NULL;
209 knot->image[SP_KNOT_STATE_MOUSEOVER] = NULL;
210 knot->image[SP_KNOT_STATE_DRAGGING] = NULL;
212 knot->cursor[SP_KNOT_STATE_NORMAL] = NULL;
213 knot->cursor[SP_KNOT_STATE_MOUSEOVER] = NULL;
214 knot->cursor[SP_KNOT_STATE_DRAGGING] = NULL;
216 knot->saved_cursor = NULL;
217 knot->pixbuf = NULL;
218 }
220 /**
221 * Called before SPKnot destruction.
222 */
223 static void sp_knot_dispose(GObject *object)
224 {
225 SPKnot *knot = (SPKnot *) object;
227 if ((knot->flags & SP_KNOT_GRABBED) && gdk_pointer_is_grabbed ()) {
228 // This happens e.g. when deleting a node in node tool while dragging it
229 gdk_pointer_ungrab (GDK_CURRENT_TIME);
230 }
232 if (knot->_event_handler_id > 0)
233 {
234 g_signal_handler_disconnect(G_OBJECT (knot->item), knot->_event_handler_id);
235 knot->_event_handler_id = 0;
236 }
238 if (knot->item) {
239 gtk_object_destroy (GTK_OBJECT (knot->item));
240 knot->item = NULL;
241 }
243 for (gint i = 0; i < SP_KNOT_VISIBLE_STATES; i++) {
244 if (knot->cursor[i]) {
245 gdk_cursor_unref(knot->cursor[i]);
246 knot->cursor[i] = NULL;
247 }
248 }
250 if (knot->tip) {
251 g_free(knot->tip);
252 knot->tip = NULL;
253 }
255 if (parent_class->dispose) {
256 (*parent_class->dispose) (object);
257 }
258 }
260 /**
261 * Update knot for dragging and tell canvas an item was grabbed.
262 */
263 void sp_knot_start_dragging(SPKnot *knot, NR::Point p, gint x, gint y, guint32 etime)
264 {
265 // save drag origin
266 xp = x;
267 yp = y;
268 within_tolerance = true;
270 knot->grabbed_rel_pos = p - knot->pos;
271 knot->drag_origin = knot->pos;
272 if (!nograb) {
273 sp_canvas_item_grab(knot->item,
274 KNOT_EVENT_MASK,
275 knot->cursor[SP_KNOT_STATE_DRAGGING],
276 etime);
277 }
278 sp_knot_set_flag(knot, SP_KNOT_GRABBED, TRUE);
279 grabbed = TRUE;
280 }
282 /**
283 * Called to handle events on knots.
284 */
285 static int sp_knot_handler(SPCanvasItem *item, GdkEvent *event, SPKnot *knot)
286 {
287 g_assert(knot != NULL);
288 g_assert(SP_IS_KNOT(knot));
290 g_object_ref(knot);
291 tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
293 gboolean consumed = FALSE;
295 /* Run client universal event handler, if present */
297 g_signal_emit(knot, knot_signals[EVENT], 0, event, &consumed);
299 if (consumed) {
300 return TRUE;
301 }
303 switch (event->type) {
304 case GDK_2BUTTON_PRESS:
305 if (event->button.button == 1) {
306 g_signal_emit(knot, knot_signals[DOUBLECLICKED], 0, event->button.state);
308 grabbed = FALSE;
309 moved = FALSE;
310 consumed = TRUE;
311 }
312 break;
313 case GDK_BUTTON_PRESS:
314 if (event->button.button == 1) {
315 NR::Point const p = knot->desktop->w2d(NR::Point(event->button.x, event->button.y));
316 sp_knot_start_dragging(knot, p, (gint) event->button.x, (gint) event->button.y, event->button.time);
317 consumed = TRUE;
318 }
319 break;
320 case GDK_BUTTON_RELEASE:
321 if (event->button.button == 1) {
322 knot->pressure = 0;
323 if (transform_escaped) {
324 transform_escaped = false;
325 consumed = TRUE;
326 } else {
327 sp_knot_set_flag(knot, SP_KNOT_GRABBED, FALSE);
328 if (!nograb) {
329 sp_canvas_item_ungrab(knot->item, event->button.time);
330 }
331 if (moved) {
332 sp_knot_set_flag(knot,
333 SP_KNOT_DRAGGING,
334 FALSE);
335 g_signal_emit(knot,
336 knot_signals[UNGRABBED], 0,
337 event->button.state);
338 } else {
339 g_signal_emit(knot,
340 knot_signals[CLICKED], 0,
341 event->button.state);
342 }
343 grabbed = FALSE;
344 moved = FALSE;
345 consumed = TRUE;
346 }
347 }
348 break;
349 case GDK_MOTION_NOTIFY:
350 if (grabbed) {
351 consumed = TRUE;
353 if ( within_tolerance
354 && ( abs( (gint) event->motion.x - xp ) < tolerance )
355 && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
356 break; // do not drag if we're within tolerance from origin
357 }
359 // Once the user has moved farther than tolerance from the original location
360 // (indicating they intend to move the object, not click), then always process the
361 // motion notify coordinates as given (no snapping back to origin)
362 within_tolerance = false;
364 if (gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &knot->pressure))
365 knot->pressure = CLAMP (knot->pressure, 0, 1);
366 else
367 knot->pressure = 0.5;
369 if (!moved) {
370 g_signal_emit(knot,
371 knot_signals[GRABBED], 0,
372 event->motion.state);
373 sp_knot_set_flag(knot,
374 SP_KNOT_DRAGGING,
375 TRUE);
376 }
377 NR::Point const motion_w(event->motion.x, event->motion.y);
378 NR::Point const motion_dt = knot->desktop->w2d(motion_w);
379 NR::Point p = motion_dt - knot->grabbed_rel_pos;
380 sp_knot_request_position (knot, &p, event->motion.state);
381 knot->desktop->scroll_to_point (&motion_dt);
382 moved = TRUE;
383 }
384 break;
385 case GDK_ENTER_NOTIFY:
386 sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, TRUE);
387 sp_knot_set_flag(knot, SP_KNOT_GRABBED, FALSE);
389 if (knot->tip) {
390 knot->desktop->event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, knot->tip);
391 }
393 grabbed = FALSE;
394 moved = FALSE;
395 consumed = TRUE;
396 break;
397 case GDK_LEAVE_NOTIFY:
398 sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, FALSE);
399 sp_knot_set_flag(knot, SP_KNOT_GRABBED, FALSE);
401 if (knot->tip) {
402 knot->desktop->event_context->defaultMessageContext()->clear();
403 }
405 grabbed = FALSE;
406 moved = FALSE;
408 consumed = TRUE;
409 break;
410 case GDK_KEY_PRESS: // keybindings for knot
411 switch (get_group0_keyval(&event->key)) {
412 case GDK_Escape:
413 sp_knot_set_flag(knot, SP_KNOT_GRABBED, FALSE);
414 if (!nograb) {
415 sp_canvas_item_ungrab(knot->item, event->button.time);
416 }
417 if (moved) {
418 sp_knot_set_flag(knot,
419 SP_KNOT_DRAGGING,
420 FALSE);
421 g_signal_emit(knot,
422 knot_signals[UNGRABBED], 0,
423 event->button.state);
424 sp_document_undo(sp_desktop_document(knot->desktop));
425 knot->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Node or handle drag canceled."));
426 transform_escaped = true;
427 consumed = TRUE;
428 }
429 grabbed = FALSE;
430 moved = FALSE;
431 break;
432 default:
433 consumed = FALSE;
434 break;
435 }
436 break;
437 default:
438 break;
439 }
441 g_object_unref(knot);
443 return consumed;
444 }
446 /**
447 * Return new knot object.
448 */
449 SPKnot *sp_knot_new(SPDesktop *desktop, const gchar *tip)
450 {
451 g_return_val_if_fail(desktop != NULL, NULL);
453 SPKnot * knot = (SPKnot*) g_object_new(SP_TYPE_KNOT, 0);
455 knot->desktop = desktop;
456 knot->flags = SP_KNOT_VISIBLE;
457 if (tip) {
458 knot->tip = g_strdup (tip);
459 }
461 knot->item = sp_canvas_item_new(sp_desktop_controls (desktop),
462 SP_TYPE_CTRL,
463 "anchor", GTK_ANCHOR_CENTER,
464 "size", 8.0,
465 "filled", TRUE,
466 "fill_color", 0xffffff00,
467 "stroked", TRUE,
468 "stroke_color", 0x01000000,
469 "mode", SP_KNOT_MODE_XOR,
470 NULL);
472 knot->_event_handler_id = gtk_signal_connect(GTK_OBJECT(knot->item), "event",
473 GTK_SIGNAL_FUNC(sp_knot_handler), knot);
475 return knot;
476 }
478 /**
479 * Show knot on its canvas.
480 */
481 void sp_knot_show(SPKnot *knot)
482 {
483 g_return_if_fail(knot != NULL);
484 g_return_if_fail(SP_IS_KNOT (knot));
486 sp_knot_set_flag(knot, SP_KNOT_VISIBLE, TRUE);
487 }
489 /**
490 * Hide knot on its canvas.
491 */
492 void sp_knot_hide(SPKnot *knot)
493 {
494 g_return_if_fail(knot != NULL);
495 g_return_if_fail(SP_IS_KNOT(knot));
497 sp_knot_set_flag(knot, SP_KNOT_VISIBLE, FALSE);
498 }
500 /**
501 * Request or set new position for knot.
502 */
503 void sp_knot_request_position(SPKnot *knot, NR::Point *p, guint state)
504 {
505 g_return_if_fail(knot != NULL);
506 g_return_if_fail(SP_IS_KNOT(knot));
508 gboolean done = FALSE;
510 g_signal_emit(knot,
511 knot_signals[REQUEST], 0,
512 p,
513 state,
514 &done);
516 /* If user did not complete, we simply move knot to new position */
518 if (!done) {
519 sp_knot_set_position (knot, p, state);
520 }
521 }
523 /**
524 * Return distance of point to knot's position; unused.
525 */
526 gdouble sp_knot_distance(SPKnot * knot, NR::Point *p, guint state)
527 {
528 g_return_val_if_fail(knot != NULL, 1e18);
529 g_return_val_if_fail(SP_IS_KNOT(knot), 1e18);
531 gdouble distance = NR::L2(*p - knot->pos);
533 g_signal_emit(knot,
534 knot_signals[DISTANCE], 0,
535 p,
536 state,
537 &distance);
539 return distance;
540 }
542 /**
543 * Move knot to new position.
544 */
545 void sp_knot_set_position(SPKnot *knot, NR::Point *p, guint state)
546 {
547 g_return_if_fail(knot != NULL);
548 g_return_if_fail(SP_IS_KNOT (knot));
550 knot->pos = *p;
552 if (knot->item) {
553 SP_CTRL(knot->item)->moveto (*p);
554 }
556 g_signal_emit(knot,
557 knot_signals[MOVED], 0,
558 p,
559 state);
560 }
562 /**
563 * Move knot to new position, without emitting a MOVED signal.
564 */
565 void sp_knot_moveto(SPKnot *knot, NR::Point *p)
566 {
567 g_return_if_fail(knot != NULL);
568 g_return_if_fail(SP_IS_KNOT(knot));
570 knot->pos = *p;
572 if (knot->item) {
573 SP_CTRL(knot->item)->moveto (*p);
574 }
575 }
577 /**
578 * Returns position of knot.
579 */
580 NR::Point sp_knot_position(SPKnot const *knot)
581 {
582 g_assert(knot != NULL);
583 g_assert(SP_IS_KNOT (knot));
585 return knot->pos;
586 }
588 /**
589 * Set flag in knot, with side effects.
590 */
591 void sp_knot_set_flag(SPKnot *knot, guint flag, bool set)
592 {
593 g_assert(knot != NULL);
594 g_assert(SP_IS_KNOT(knot));
596 if (set) {
597 knot->flags |= flag;
598 } else {
599 knot->flags &= ~flag;
600 }
602 switch (flag) {
603 case SP_KNOT_VISIBLE:
604 if (set) {
605 sp_canvas_item_show(knot->item);
606 } else {
607 sp_canvas_item_hide(knot->item);
608 }
609 break;
610 case SP_KNOT_MOUSEOVER:
611 case SP_KNOT_DRAGGING:
612 sp_knot_set_ctrl_state(knot);
613 break;
614 case SP_KNOT_GRABBED:
615 break;
616 default:
617 g_assert_not_reached();
618 break;
619 }
620 }
622 /**
623 * Update knot's pixbuf and set its control state.
624 */
625 void sp_knot_update_ctrl(SPKnot *knot)
626 {
627 if (!knot->item) {
628 return;
629 }
631 gtk_object_set(GTK_OBJECT(knot->item), "shape", knot->shape, NULL);
632 gtk_object_set(GTK_OBJECT(knot->item), "mode", knot->mode, NULL);
633 gtk_object_set(GTK_OBJECT(knot->item), "size", (gdouble) knot->size, NULL);
634 gtk_object_set(GTK_OBJECT(knot->item), "anchor", knot->anchor, NULL);
635 if (knot->pixbuf) {
636 gtk_object_set(GTK_OBJECT (knot->item), "pixbuf", knot->pixbuf, NULL);
637 }
639 sp_knot_set_ctrl_state(knot);
640 }
642 /**
643 * Set knot control state (dragging/mouseover/normal).
644 */
645 static void sp_knot_set_ctrl_state(SPKnot *knot)
646 {
647 if (knot->flags & SP_KNOT_DRAGGING) {
648 gtk_object_set(GTK_OBJECT (knot->item),
649 "fill_color",
650 knot->fill[SP_KNOT_STATE_DRAGGING],
651 NULL);
652 gtk_object_set(GTK_OBJECT (knot->item),
653 "stroke_color",
654 knot->stroke[SP_KNOT_STATE_DRAGGING],
655 NULL);
656 } else if (knot->flags & SP_KNOT_MOUSEOVER) {
657 gtk_object_set(GTK_OBJECT(knot->item),
658 "fill_color",
659 knot->fill[SP_KNOT_STATE_MOUSEOVER],
660 NULL);
661 gtk_object_set(GTK_OBJECT(knot->item),
662 "stroke_color",
663 knot->stroke[SP_KNOT_STATE_MOUSEOVER],
664 NULL);
665 } else {
666 gtk_object_set(GTK_OBJECT(knot->item),
667 "fill_color",
668 knot->fill[SP_KNOT_STATE_NORMAL],
669 NULL);
670 gtk_object_set(GTK_OBJECT(knot->item),
671 "stroke_color",
672 knot->stroke[SP_KNOT_STATE_NORMAL],
673 NULL);
674 }
675 }
679 /*
680 Local Variables:
681 mode:c++
682 c-file-style:"stroustrup"
683 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
684 indent-tabs-mode:nil
685 fill-column:99
686 End:
687 */
688 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :