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 /* Run client universal event handler, if present */
292 gboolean consumed = FALSE;
294 g_signal_emit(knot, knot_signals[EVENT], 0, event, &consumed);
296 if (consumed) {
297 return TRUE;
298 }
300 g_object_ref(knot);
301 tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
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 knot->desktop->set_coordinate_status(knot->pos); // display the coordinate of knot, not cursor - they may be different!
383 moved = TRUE;
384 }
385 break;
386 case GDK_ENTER_NOTIFY:
387 sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, TRUE);
388 sp_knot_set_flag(knot, SP_KNOT_GRABBED, FALSE);
390 if (knot->tip) {
391 knot->desktop->event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, knot->tip);
392 }
394 grabbed = FALSE;
395 moved = FALSE;
396 consumed = TRUE;
397 break;
398 case GDK_LEAVE_NOTIFY:
399 sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, FALSE);
400 sp_knot_set_flag(knot, SP_KNOT_GRABBED, FALSE);
402 if (knot->tip) {
403 knot->desktop->event_context->defaultMessageContext()->clear();
404 }
406 grabbed = FALSE;
407 moved = FALSE;
409 consumed = TRUE;
410 break;
411 case GDK_KEY_PRESS: // keybindings for knot
412 switch (get_group0_keyval(&event->key)) {
413 case GDK_Escape:
414 sp_knot_set_flag(knot, SP_KNOT_GRABBED, FALSE);
415 if (!nograb) {
416 sp_canvas_item_ungrab(knot->item, event->button.time);
417 }
418 if (moved) {
419 sp_knot_set_flag(knot,
420 SP_KNOT_DRAGGING,
421 FALSE);
422 g_signal_emit(knot,
423 knot_signals[UNGRABBED], 0,
424 event->button.state);
425 sp_document_undo(sp_desktop_document(knot->desktop));
426 knot->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Node or handle drag canceled."));
427 transform_escaped = true;
428 consumed = TRUE;
429 }
430 grabbed = FALSE;
431 moved = FALSE;
432 break;
433 default:
434 consumed = FALSE;
435 break;
436 }
437 break;
438 default:
439 break;
440 }
442 g_object_unref(knot);
444 return consumed;
445 }
447 /**
448 * Return new knot object.
449 */
450 SPKnot *sp_knot_new(SPDesktop *desktop, const gchar *tip)
451 {
452 g_return_val_if_fail(desktop != NULL, NULL);
454 SPKnot * knot = (SPKnot*) g_object_new(SP_TYPE_KNOT, 0);
456 knot->desktop = desktop;
457 knot->flags = SP_KNOT_VISIBLE;
458 if (tip) {
459 knot->tip = g_strdup (tip);
460 }
462 knot->item = sp_canvas_item_new(sp_desktop_controls (desktop),
463 SP_TYPE_CTRL,
464 "anchor", GTK_ANCHOR_CENTER,
465 "size", 8.0,
466 "filled", TRUE,
467 "fill_color", 0xffffff00,
468 "stroked", TRUE,
469 "stroke_color", 0x01000000,
470 "mode", SP_KNOT_MODE_XOR,
471 NULL);
473 knot->_event_handler_id = gtk_signal_connect(GTK_OBJECT(knot->item), "event",
474 GTK_SIGNAL_FUNC(sp_knot_handler), knot);
476 return knot;
477 }
479 /**
480 * Show knot on its canvas.
481 */
482 void sp_knot_show(SPKnot *knot)
483 {
484 g_return_if_fail(knot != NULL);
485 g_return_if_fail(SP_IS_KNOT (knot));
487 sp_knot_set_flag(knot, SP_KNOT_VISIBLE, TRUE);
488 }
490 /**
491 * Hide knot on its canvas.
492 */
493 void sp_knot_hide(SPKnot *knot)
494 {
495 g_return_if_fail(knot != NULL);
496 g_return_if_fail(SP_IS_KNOT(knot));
498 sp_knot_set_flag(knot, SP_KNOT_VISIBLE, FALSE);
499 }
501 /**
502 * Request or set new position for knot.
503 */
504 void sp_knot_request_position(SPKnot *knot, NR::Point *p, guint state)
505 {
506 g_return_if_fail(knot != NULL);
507 g_return_if_fail(SP_IS_KNOT(knot));
509 gboolean done = FALSE;
511 g_signal_emit(knot,
512 knot_signals[REQUEST], 0,
513 p,
514 state,
515 &done);
517 /* If user did not complete, we simply move knot to new position */
519 if (!done) {
520 sp_knot_set_position (knot, p, state);
521 }
522 }
524 /**
525 * Return distance of point to knot's position; unused.
526 */
527 gdouble sp_knot_distance(SPKnot * knot, NR::Point *p, guint state)
528 {
529 g_return_val_if_fail(knot != NULL, 1e18);
530 g_return_val_if_fail(SP_IS_KNOT(knot), 1e18);
532 gdouble distance = NR::L2(*p - knot->pos);
534 g_signal_emit(knot,
535 knot_signals[DISTANCE], 0,
536 p,
537 state,
538 &distance);
540 return distance;
541 }
543 /**
544 * Move knot to new position.
545 */
546 void sp_knot_set_position(SPKnot *knot, NR::Point *p, guint state)
547 {
548 g_return_if_fail(knot != NULL);
549 g_return_if_fail(SP_IS_KNOT (knot));
551 knot->pos = *p;
553 if (knot->item) {
554 SP_CTRL(knot->item)->moveto (*p);
555 }
557 g_signal_emit(knot,
558 knot_signals[MOVED], 0,
559 p,
560 state);
561 }
563 /**
564 * Move knot to new position, without emitting a MOVED signal.
565 */
566 void sp_knot_moveto(SPKnot *knot, NR::Point *p)
567 {
568 g_return_if_fail(knot != NULL);
569 g_return_if_fail(SP_IS_KNOT(knot));
571 knot->pos = *p;
573 if (knot->item) {
574 SP_CTRL(knot->item)->moveto (*p);
575 }
576 }
578 /**
579 * Returns position of knot.
580 */
581 NR::Point sp_knot_position(SPKnot const *knot)
582 {
583 g_assert(knot != NULL);
584 g_assert(SP_IS_KNOT (knot));
586 return knot->pos;
587 }
589 /**
590 * Set flag in knot, with side effects.
591 */
592 void sp_knot_set_flag(SPKnot *knot, guint flag, bool set)
593 {
594 g_assert(knot != NULL);
595 g_assert(SP_IS_KNOT(knot));
597 if (set) {
598 knot->flags |= flag;
599 } else {
600 knot->flags &= ~flag;
601 }
603 switch (flag) {
604 case SP_KNOT_VISIBLE:
605 if (set) {
606 sp_canvas_item_show(knot->item);
607 } else {
608 sp_canvas_item_hide(knot->item);
609 }
610 break;
611 case SP_KNOT_MOUSEOVER:
612 case SP_KNOT_DRAGGING:
613 sp_knot_set_ctrl_state(knot);
614 break;
615 case SP_KNOT_GRABBED:
616 break;
617 default:
618 g_assert_not_reached();
619 break;
620 }
621 }
623 /**
624 * Update knot's pixbuf and set its control state.
625 */
626 void sp_knot_update_ctrl(SPKnot *knot)
627 {
628 if (!knot->item) {
629 return;
630 }
632 gtk_object_set(GTK_OBJECT(knot->item), "shape", knot->shape, NULL);
633 gtk_object_set(GTK_OBJECT(knot->item), "mode", knot->mode, NULL);
634 gtk_object_set(GTK_OBJECT(knot->item), "size", (gdouble) knot->size, NULL);
635 gtk_object_set(GTK_OBJECT(knot->item), "anchor", knot->anchor, NULL);
636 if (knot->pixbuf) {
637 gtk_object_set(GTK_OBJECT (knot->item), "pixbuf", knot->pixbuf, NULL);
638 }
640 sp_knot_set_ctrl_state(knot);
641 }
643 /**
644 * Set knot control state (dragging/mouseover/normal).
645 */
646 static void sp_knot_set_ctrl_state(SPKnot *knot)
647 {
648 if (knot->flags & SP_KNOT_DRAGGING) {
649 gtk_object_set(GTK_OBJECT (knot->item),
650 "fill_color",
651 knot->fill[SP_KNOT_STATE_DRAGGING],
652 NULL);
653 gtk_object_set(GTK_OBJECT (knot->item),
654 "stroke_color",
655 knot->stroke[SP_KNOT_STATE_DRAGGING],
656 NULL);
657 } else if (knot->flags & SP_KNOT_MOUSEOVER) {
658 gtk_object_set(GTK_OBJECT(knot->item),
659 "fill_color",
660 knot->fill[SP_KNOT_STATE_MOUSEOVER],
661 NULL);
662 gtk_object_set(GTK_OBJECT(knot->item),
663 "stroke_color",
664 knot->stroke[SP_KNOT_STATE_MOUSEOVER],
665 NULL);
666 } else {
667 gtk_object_set(GTK_OBJECT(knot->item),
668 "fill_color",
669 knot->fill[SP_KNOT_STATE_NORMAL],
670 NULL);
671 gtk_object_set(GTK_OBJECT(knot->item),
672 "stroke_color",
673 knot->stroke[SP_KNOT_STATE_NORMAL],
674 NULL);
675 }
676 }
680 /*
681 Local Variables:
682 mode:c++
683 c-file-style:"stroustrup"
684 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
685 indent-tabs-mode:nil
686 fill-column:99
687 End:
688 */
689 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :