1 /** \file
2 * Anchors implementation.
3 */
5 /*
6 * Authors:
7 * Copyright (C) 2000 Lauris Kaplinski
8 * Copyright (C) 2000-2001 Ximian, Inc.
9 * Copyright (C) 2002 Lauris Kaplinski
10 * Copyright (C) 2004 Monash University
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
16 #include "draw-anchor.h"
17 #include "desktop.h"
18 #include "desktop-handles.h"
19 #include "event-context.h"
20 #include "lpe-tool-context.h"
21 #include "display/sodipodi-ctrl.h"
22 #include "display/curve.h"
24 /**
25 * Creates an anchor object and initializes it.
26 */
27 SPDrawAnchor *
28 sp_draw_anchor_new(SPDrawContext *dc, SPCurve *curve, gboolean start, Geom::Point delta)
29 {
30 if (SP_IS_LPETOOL_CONTEXT(dc)) {
31 // suppress all kinds of anchors in LPEToolContext
32 return NULL;
33 }
35 SPDesktop *dt = SP_EVENT_CONTEXT_DESKTOP(dc);
37 SPDrawAnchor *a = g_new(SPDrawAnchor, 1);
39 a->dc = dc;
40 a->curve = curve;
41 curve->ref();
42 a->start = start;
43 a->active = FALSE;
44 a->dp = delta;
45 a->ctrl = sp_canvas_item_new(sp_desktop_controls(dt), SP_TYPE_CTRL,
46 "size", 6.0,
47 "filled", 0,
48 "fill_color", 0xff00007f,
49 "stroked", 1,
50 "stroke_color", 0x000000ff,
51 NULL);
53 SP_CTRL(a->ctrl)->moveto(delta);
55 return a;
56 }
58 /**
59 * Destroys the anchor's canvas item and frees the anchor object.
60 */
61 SPDrawAnchor *
62 sp_draw_anchor_destroy(SPDrawAnchor *anchor)
63 {
64 if (anchor->curve) {
65 anchor->curve->unref();
66 }
67 if (anchor->ctrl) {
68 gtk_object_destroy(GTK_OBJECT(anchor->ctrl));
69 }
70 g_free(anchor);
71 return NULL;
72 }
74 #define A_SNAP 4.0
76 /**
77 * Test if point is near anchor, if so fill anchor on canvas and return
78 * pointer to it or NULL.
79 */
80 SPDrawAnchor *
81 sp_draw_anchor_test(SPDrawAnchor *anchor, Geom::Point w, gboolean activate)
82 {
83 SPDesktop *dt = SP_EVENT_CONTEXT_DESKTOP(anchor->dc);
85 if ( activate && ( Geom::LInfty( w - dt->d2w(anchor->dp) ) <= A_SNAP ) ) {
86 if (!anchor->active) {
87 sp_canvas_item_set((GtkObject *) anchor->ctrl, "filled", TRUE, NULL);
88 anchor->active = TRUE;
89 }
90 return anchor;
91 }
93 if (anchor->active) {
94 sp_canvas_item_set((GtkObject *) anchor->ctrl, "filled", FALSE, NULL);
95 anchor->active = FALSE;
96 }
97 return NULL;
98 }
101 /*
102 Local Variables:
103 mode:c++
104 c-file-style:"stroustrup"
105 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
106 indent-tabs-mode:nil
107 fill-column:99
108 End:
109 */
110 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :