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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
// SPDX-License-Identifier: GPL-2.0-or-later
/**
* Abstract base class for on-canvas control items.
*/
/*
* Author:
* Tavmjong Bah
*
* Copyright (C) 2020 Tavmjong Bah
*
* Rewrite of SPCanvasItem
*
* Released under GNU GPL v2+, read the file 'COPYING' for more information.
*/
#include "canvas-item.h"
#include "canvas-item-group.h"
#include "canvas-item-ctrl.h"
#include "ui/widget/canvas.h"
constexpr bool DEBUG_LOGGING = false;
constexpr bool DEBUG_BOUNDS = false;
namespace Inkscape {
CanvasItem::CanvasItem(CanvasItemContext *context)
: _context(context)
, _parent(nullptr)
{
if constexpr (DEBUG_LOGGING) std::cout << "CanvasItem: create root " << get_name() << std::endl;
request_update();
}
CanvasItem::CanvasItem(CanvasItemGroup *parent)
: _context(parent->_context)
, _parent(parent)
{
if constexpr (DEBUG_LOGGING) std::cout << "CanvasItem: add " << get_name() << " to " << parent->get_name() << " " << parent->items.size() << std::endl;
defer([=] {
parent->items.push_back(*this);
request_update();
});
}
void CanvasItem::unlink()
{
defer([=] {
// Clear canvas of item.
request_redraw();
// Remove from parent.
if (_parent) {
if constexpr (DEBUG_LOGGING) std::cout << "CanvasItem: remove " << get_name() << " from " << _parent->get_name() << " " << _parent->items.size() << std::endl;
auto it = _parent->items.iterator_to(*this);
assert(it != _parent->items.end());
_parent->items.erase(it);
_parent->request_update();
} else {
if constexpr (DEBUG_LOGGING) std::cout << "CanvasItem: destroy root " << get_name() << std::endl;
}
delete this;
});
}
CanvasItem::~CanvasItem()
{
// Clear any pointers to this object in canvas.
get_canvas()->canvas_item_destructed(this);
}
bool CanvasItem::is_descendant_of(CanvasItem const *ancestor) const
{
auto item = this;
while (item) {
if (item == ancestor) {
return true;
}
item = item->_parent;
}
return false;
}
void CanvasItem::set_z_position(int zpos)
{
if (!_parent) {
std::cerr << "CanvasItem::set_z_position: No parent!" << std::endl;
return;
}
defer([=] {
_parent->items.erase(_parent->items.iterator_to(*this));
if (zpos <= 0) {
_parent->items.push_front(*this);
} else if (zpos >= _parent->items.size() - 1) {
_parent->items.push_back(*this);
} else {
auto it = _parent->items.begin();
std::advance(it, zpos);
_parent->items.insert(it, *this);
}
});
}
void CanvasItem::raise_to_top()
{
if (!_parent) {
std::cerr << "CanvasItem::raise_to_top: No parent!" << std::endl;
return;
}
defer([=] {
_parent->items.erase(_parent->items.iterator_to(*this));
_parent->items.push_back(*this);
});
}
void CanvasItem::lower_to_bottom()
{
if (!_parent) {
std::cerr << "CanvasItem::lower_to_bottom: No parent!" << std::endl;
return;
}
defer([=] {
_parent->items.erase(_parent->items.iterator_to(*this));
_parent->items.push_front(*this);
});
}
// Indicate geometry changed and bounds needs recalculating.
void CanvasItem::request_update()
{
if (_need_update || !_visible) {
return;
}
_need_update = true;
if (_parent) {
_parent->request_update();
} else {
get_canvas()->request_update();
}
}
void CanvasItem::update(bool propagate)
{
if (!_visible) {
_mark_net_invisible();
return;
}
bool reappearing = !_net_visible;
_net_visible = true;
if (!_need_update && !reappearing && !propagate) {
return;
}
_need_update = false;
// Get new bounds
_update(propagate);
if (reappearing) {
request_redraw();
}
}
void CanvasItem::_mark_net_invisible()
{
if (!_net_visible) {
return;
}
_net_visible = false;
_need_update = false;
request_redraw();
_bounds = {};
}
// Grab all events!
void CanvasItem::grab(Gdk::EventMask event_mask, Glib::RefPtr<Gdk::Cursor> const &cursor)
{
if constexpr (DEBUG_LOGGING) std::cout << "CanvasItem::grab: " << _name << std::endl;
auto canvas = get_canvas();
// Don't grab if we already have a grabbed item!
if (canvas->get_grabbed_canvas_item()) {
return;
}
gtk_grab_add(GTK_WIDGET(canvas->gobj()));
canvas->set_grabbed_canvas_item(this, event_mask);
canvas->set_current_canvas_item(this); // So that all events go to grabbed item.
}
void CanvasItem::ungrab()
{
if constexpr (DEBUG_LOGGING) std::cout << "CanvasItem::ungrab: " << _name << std::endl;
auto canvas = get_canvas();
if (canvas->get_grabbed_canvas_item() != this) {
return; // Sanity check
}
canvas->set_grabbed_canvas_item(nullptr, (Gdk::EventMask)0); // Zero mask
gtk_grab_remove(GTK_WIDGET(canvas->gobj()));
}
void CanvasItem::render(CanvasItemBuffer &buf) const
{
if (_visible && _bounds && _bounds->interiorIntersects(buf.rect)) {
_render(buf);
if constexpr (DEBUG_BOUNDS) {
auto bounds = *_bounds;
bounds.expandBy(-1);
bounds -= buf.rect.min();
buf.cr->set_source_rgba(1.0, 0.0, 0.0, 1.0);
buf.cr->rectangle(bounds.min().x(), bounds.min().y(), bounds.width(), bounds.height());
buf.cr->stroke();
}
}
}
/*
* The main invariant of the invisibility system is
*
* x needs update and is visible ==> parent(x) needs update or is invisible
*
* When x belongs to the visible subtree, meaning it and all its parents are visible,
* this condition reduces to
*
* x needs update ==> parent(x) needs update
*
* Thus within the visible subtree, the subset of nodes that need updating forms a subtree.
*
* In the update() function, we only walk this latter subtree.
*/
void CanvasItem::set_visible(bool visible)
{
defer([=] {
if (_visible == visible) return;
if (_visible) {
request_update();
_visible = false;
} else {
_visible = true;
_need_update = false;
request_update();
}
});
}
void CanvasItem::request_redraw()
{
// Queue redraw request
if (_bounds) {
get_canvas()->redraw_area(*_bounds);
}
}
void CanvasItem::set_fill(uint32_t fill)
{
defer([=] {
if (_fill == fill) return;
_fill = fill;
request_redraw();
});
}
void CanvasItem::set_stroke(uint32_t stroke)
{
defer([=] {
if (_stroke == stroke) return;
_stroke = stroke;
request_redraw();
});
}
void CanvasItem::update_canvas_item_ctrl_sizes(int size_index)
{
if (auto ctrl = dynamic_cast<CanvasItemCtrl*>(this)) {
// We can't use set_size_default as the preference file is updated ->after<- the signal is emitted!
ctrl->set_size_via_index(size_index);
} else if (auto group = dynamic_cast<CanvasItemGroup*>(this)) {
for (auto &item : group->items) {
item.update_canvas_item_ctrl_sizes(size_index);
}
}
}
void CanvasItem::canvas_item_print_tree(int level, int zorder) const
{
if (level == 0) {
std::cout << "Canvas Item Tree" << std::endl;
}
std::cout << "CC: ";
for (int i = 0; i < level; ++i) {
std::cout << " ";
}
std::cout << zorder << ": " << _name << std::endl;
if (auto group = dynamic_cast<Inkscape::CanvasItemGroup const*>(this)) {
int i = 0;
for (auto &item : group->items) {
item.canvas_item_print_tree(level + 1, i);
i++;
}
}
}
} // 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 :
|