Code

SPDrawAnchor should add a refcount to SPCurve!
[inkscape.git] / src / draw-anchor.cpp
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 "display/sodipodi-ctrl.h"
21 #include "display/curve.h"
23 /**
24  * Creates an anchor object and initializes it.
25  */
26 SPDrawAnchor *
27 sp_draw_anchor_new(SPDrawContext *dc, SPCurve *curve, gboolean start, NR::Point delta)
28 {
29     SPDesktop *dt = SP_EVENT_CONTEXT_DESKTOP(dc);
31     SPDrawAnchor *a = g_new(SPDrawAnchor, 1);
33     a->dc = dc;
34     a->curve = curve;
35     curve->ref();
36     a->start = start;
37     a->active = FALSE;
38     a->dp = delta;
39     a->ctrl = sp_canvas_item_new(sp_desktop_controls(dt), SP_TYPE_CTRL,
40                                  "size", 6.0,
41                                  "filled", 0,
42                                  "fill_color", 0xff00007f,
43                                  "stroked", 1,
44                                  "stroke_color", 0x000000ff,
45                                  NULL);
47     SP_CTRL(a->ctrl)->moveto(delta);
49     return a;
50 }
52 /**
53  * Destroys the anchor's canvas item and frees the anchor object.
54  */
55 SPDrawAnchor *
56 sp_draw_anchor_destroy(SPDrawAnchor *anchor)
57 {
58     if (anchor->curve) {
59         anchor->curve->unref();
60     }
61     if (anchor->ctrl) {
62         gtk_object_destroy(GTK_OBJECT(anchor->ctrl));
63     }
64     g_free(anchor);
65     return NULL;
66 }
68 #define A_SNAP 4.0
70 /**
71  * Test if point is near anchor, if so fill anchor on canvas and return
72  * pointer to it or NULL.
73  */
74 SPDrawAnchor *
75 sp_draw_anchor_test(SPDrawAnchor *anchor, NR::Point w, gboolean activate)
76 {
77     SPDesktop *dt = SP_EVENT_CONTEXT_DESKTOP(anchor->dc);
79     if ( activate && ( NR::LInfty( w - dt->d2w(anchor->dp) ) <= A_SNAP ) ) {
80         if (!anchor->active) {
81             sp_canvas_item_set((GtkObject *) anchor->ctrl, "filled", TRUE, NULL);
82             anchor->active = TRUE;
83         }
84         return anchor;
85     }
87     if (anchor->active) {
88         sp_canvas_item_set((GtkObject *) anchor->ctrl, "filled", FALSE, NULL);
89         anchor->active = FALSE;
90     }
91     return NULL;
92 }
95 /*
96   Local Variables:
97   mode:c++
98   c-file-style:"stroustrup"
99   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
100   indent-tabs-mode:nil
101   fill-column:99
102   End:
103 */
104 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :