summaryrefslogtreecommitdiffstats
path: root/src/display/control/canvas-item-drawing.cpp
blob: cfa527aafdecf8b6951f9e27f24ca940095b45c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// SPDX-License-Identifier: GPL-2.0-or-later
/**
 * A class to render the SVG drawing.
 */

/*
 * Author:
 *   Tavmjong Bah
 *
 * Copyright (C) 2020 Tavmjong Bah
 *
 * Rewrite of _SPCanvasArena.
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "canvas-item-drawing.h"

#include "desktop.h"

#include "display/drawing.h"
#include "display/drawing-context.h"
#include "display/drawing-item.h"
#include "display/drawing-group.h"

#include "helper/geom.h"
#include "ui/widget/canvas.h"
#include "ui/modifiers.h"

namespace Inkscape {

/**
 * Create the drawing. One per window!
 */
CanvasItemDrawing::CanvasItemDrawing(CanvasItemGroup *group)
    : CanvasItem(group)
{
    _name = "CanvasItemDrawing";
    _pickable = true;

    _drawing = std::make_unique<Drawing>(this);
    auto root = new DrawingGroup(*_drawing);
    root->setPickChildren(true);
    _drawing->setRoot(root);
}

/**
 * Returns true if point p (in canvas units) is inside some object in drawing.
 */
bool CanvasItemDrawing::contains(Geom::Point const &p, double tolerance)
{
    if (tolerance != 0) {
        std::cerr << "CanvasItemDrawing::contains: Non-zero tolerance not implemented!" << std::endl;
    }

    _picked_item = _drawing->pick(p, _drawing->cursorTolerance(), _sticky * DrawingItem::PICK_STICKY | _pick_outline * DrawingItem::PICK_OUTLINE);

    if (_picked_item) {
        // This will trigger a signal that is handled by our event handler. Seems a bit of a
        // round-about way of doing things but it matches what other pickable canvas-item classes do.
        return true;
    }

    return false;
}

/**
 * Update and redraw drawing.
 */
void CanvasItemDrawing::_update(bool)
{
    // Undo y-axis flip. This should not be here!!!!
    auto new_drawing_affine = affine();
    if (auto desktop = get_canvas()->get_desktop()) {
        new_drawing_affine = desktop->doc2dt() * new_drawing_affine;
    }

    bool affine_changed = _drawing_affine != new_drawing_affine;
    if (affine_changed) {
        _drawing_affine = new_drawing_affine;
    }

    _drawing->update(Geom::IntRect::infinite(), _drawing_affine, DrawingItem::STATE_ALL, affine_changed * DrawingItem::STATE_ALL);

    _bounds = expandedBy(_drawing->root()->drawbox(), 1); // Avoid aliasing artifacts

    // Todo: This should be managed elsewhere.
    if (_cursor) {
        /* Mess with enter/leave notifiers */
        DrawingItem *new_drawing_item = _drawing->pick(_c, _delta, _sticky * DrawingItem::PICK_STICKY | _pick_outline * DrawingItem::PICK_OUTLINE);
        if (_active_item != new_drawing_item) {

            GdkEventCrossing ec;
            ec.window = get_canvas()->get_window()->gobj();
            ec.send_event = true;
            ec.subwindow = ec.window;
            ec.time = GDK_CURRENT_TIME;
            ec.x = _c.x();
            ec.y = _c.y();

            /* fixme: Why? */
            if (_active_item) {
                ec.type = GDK_LEAVE_NOTIFY;
                _drawing_event_signal.emit((GdkEvent *) &ec, _active_item);
            }

            _active_item = new_drawing_item;

            if (_active_item) {
                ec.type = GDK_ENTER_NOTIFY;
                _drawing_event_signal.emit((GdkEvent *) &ec, _active_item);
            }
        }
    }
}

/**
 * Render drawing to screen via Cairo.
 */
void CanvasItemDrawing::_render(Inkscape::CanvasItemBuffer &buf) const
{
    auto dc = Inkscape::DrawingContext(buf.cr->cobj(), buf.rect.min());
    _drawing->render(dc, buf.rect, buf.outline_pass * DrawingItem::RENDER_OUTLINE);
}

/**
 * Handle events directed at the drawing. We first attempt to handle them here.
 */
bool CanvasItemDrawing::handle_event(GdkEvent *event)
{
    bool retval = false;
    
    switch (event->type) {
        case GDK_ENTER_NOTIFY:
            if (!_cursor) {
                if (_active_item) {
                    std::cerr << "CanvasItemDrawing::event_handler: cursor entered drawing with an active item!" << std::endl;
                }
                _cursor = true;

                /* TODO ... event -> arena transform? */
                _c = Geom::Point(event->crossing.x, event->crossing.y);

                _active_item = _drawing->pick(_c, _drawing->cursorTolerance(), _sticky * DrawingItem::PICK_STICKY | _pick_outline * DrawingItem::PICK_OUTLINE);
                retval = _drawing_event_signal.emit(event, _active_item);
            }
            break;

        case GDK_LEAVE_NOTIFY:
            if (_cursor) {
                retval = _drawing_event_signal.emit(event, _active_item);
                _active_item = nullptr;
                _cursor = false;
            }
            break;

        case GDK_MOTION_NOTIFY:
        {
            /* TODO ... event -> arena transform? */
            _c = Geom::Point(event->motion.x, event->motion.y);

            auto new_drawing_item = _drawing->pick(_c, _drawing->cursorTolerance(), _sticky * DrawingItem::PICK_STICKY | _pick_outline * DrawingItem::PICK_OUTLINE);
            if (_active_item != new_drawing_item) {

                GdkEventCrossing ec;
                ec.window = event->motion.window;
                ec.send_event = event->motion.send_event;
                ec.subwindow = event->motion.window;
                ec.time = event->motion.time;
                ec.x = event->motion.x;
                ec.y = event->motion.y;

                /* fixme: What is wrong? */
                if (_active_item) {
                    ec.type = GDK_LEAVE_NOTIFY;
                    retval = _drawing_event_signal.emit((GdkEvent *) &ec, _active_item);
                }

                _active_item = new_drawing_item;

                if (_active_item) {
                    ec.type = GDK_ENTER_NOTIFY;
                    retval = _drawing_event_signal.emit((GdkEvent *) &ec, _active_item);
                }
            }
            retval = retval || _drawing_event_signal.emit(event, _active_item);
            break;
        }

        case GDK_SCROLL:
        {
            if (Modifiers::Modifier::get(Modifiers::Type::CANVAS_ZOOM)->active(event->scroll.state)) {
                /* Zoom is emitted by the canvas as well, ignore here */
                return false;
            }
            retval = _drawing_event_signal.emit(event, _active_item);
            break;
        }

        default:
            /* Just send event */
            retval = _drawing_event_signal.emit(event, _active_item);
            break;
    }

    return retval;
}

} // namespace Inkscape

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :