summaryrefslogtreecommitdiffstats
path: root/src/display/control/canvas-item-drawing.cpp
blob: 17750b726d6e69efda7de0be02275071691490d7 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// 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 _SPCavasArena.
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "canvas-item-drawing.h"

#include "desktop.h"

#include "preferences.h"

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

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

namespace Inkscape {

struct CachePref2Observer : public Inkscape::Preferences::Observer {
    CachePref2Observer(Inkscape::CanvasItemDrawing *item)
        : Inkscape::Preferences::Observer("/options/renderingcache")
        , _canvas_item_drawing(item)
    {
        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
        std::vector<Inkscape::Preferences::Entry> v = prefs->getAllEntries(observed_path);
        for (const auto & i : v) {
            notify(i);
        }
        prefs->addObserver(*this);
    }
    void notify(Inkscape::Preferences::Entry const &v) override {
        Glib::ustring name = v.getEntryName();
        if (name == "size") {
            _canvas_item_drawing->get_drawing()->setCacheBudget((1 << 20) * v.getIntLimited(64, 0, 4096));
        }
    }
    Inkscape::CanvasItemDrawing *_canvas_item_drawing;
};

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

    _drawing = new Inkscape::Drawing(this);
    _drawing->delta = 1.0; // Default
    auto root = new Inkscape::DrawingGroup(*_drawing);
    root->setPickChildren(true);
    _drawing->setRoot(root);

    // _drawing->signal_request_update.connect(...);  Not needed now.
    // _drawing->signal_request_render.connect(...);  Not needed now.
    // _drawing->signal_item_deleted.connect(...);    Not needed now.

    _observer = new CachePref2Observer(this);
}

CanvasItemDrawing::~CanvasItemDrawing()
{
    delete _observer;
    delete _drawing;
}

/**
 * Returns distance between point in canvas units and nearest point on drawing.
 */
double CanvasItemDrawing::closest_distance_to(Geom::Point const &p)
{
    double d = Geom::infinity();
    std::cerr << "CanvasItemDrawing::closest_distance_to: Not implemented!" << std::endl;
    return d;
}

/**
 * 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;
    }

    _drawing->update(Geom::IntRect::infinite(), DrawingItem::STATE_PICK | DrawingItem::STATE_BBOX);
    _picked_item = _drawing->pick(p, _drawing->delta, _sticky);

    if (_picked_item) {
        // This will trigger a signal that is handled by our event handler. Seems a bit of a
        // round-a-bout 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(Geom::Affine const &affine)
{
    auto new_affine = affine;

    // Correct for y-axis. This should not be here!!!!
    if (auto *desktop = _canvas->get_desktop()) {
        new_affine = desktop->doc2dt() * new_affine;
    }

    // if (_affine == new_affine && !_need_update) {
    //     // Nothing to do.
    //     return;
    // }

    _ctx.ctm = new_affine;  // TODO Remove _ctx.ctm... it's exactly the same as _affine!

    unsigned reset = (_affine != new_affine ? DrawingItem::STATE_ALL : 0);

    _affine = new_affine;

    _drawing->update(Geom::IntRect::infinite(), DrawingItem::STATE_ALL, reset);

    Geom::OptIntRect bbox = _drawing->root()->visualBounds();
    if (bbox) {
        _bounds = *bbox;
        _bounds.expandBy(1); // Avoid aliasing artifacts.
    }

    if (_cursor) {
        /* Mess with enter/leave notifiers */
        DrawingItem *new_drawing_item = _drawing->pick(_c, _delta, _sticky);
        if (_active_item != new_drawing_item) {

            GdkEventCrossing ec;
            ec.window = _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);
            }
        }
    }

    _need_update = false;
}

/**
 * Render drawing to screen via Cairo.
 */
void CanvasItemDrawing::render(Inkscape::CanvasItemBuffer *buf)
{
    if (!buf) {
        std::cerr << "CanvasItemDrawing::Render: No buffer!" << std::endl;
        return;
    }

    if (buf->rect.hasZeroArea()) {
        return;
    }

    Inkscape::DrawingContext dc(buf->cr->cobj(), buf->rect.min());
    _drawing->update();
    _drawing->render(dc, buf->rect);
}

/**
 * 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);

                /* fixme: Not sure abut this, but seems the right thing (Lauris) */
                //_drawing->update(Geom::IntRect::infinite(), _ctx, DrawingItem::STATE_PICK | DrawingItem::STATE_BBOX, 0);
                _active_item = _drawing->pick(_c, _drawing->delta, _sticky);
                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);

            /* fixme: Not sure abut this, but seems the right thing (Lauris) */
            //_drawing->update(Geom::IntRect::infinite(), _ctx, DrawingItem::STATE_PICK | DrawingItem::STATE_BBOX);

            auto new_drawing_item = _drawing->pick(_c, _drawing->delta, _sticky);
            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 :