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
|
// SPDX-License-Identifier: GPL-2.0-or-later
/**
* @file
* A light-weight widget containing an SPCanvas for rendering an SVG.
*/
/*
* Authors:
* Tavmjong Bah <tavmjong@free.fr>
*
* Includes code moved from svg-view.cpp authored by:
* MenTaLGuy
* Ralf Stephan <ralf@ark.in-berlin.de>
* Jon A. Cruz <jon@joncruz.org>
*
* Copyright (C) 2018 Authors
*
* The contents of this file may be used under the GNU General Public License Version 2 or later.
* Read the file 'COPYING' for more information.
*
*/
#include <iostream>
#include "svg-view-widget.h"
#include "document.h"
#include "2geom/transforms.h"
#include "display/sp-canvas.h"
#include "display/sp-canvas-group.h"
#include "display/sp-canvas-item.h"
#include "display/canvas-arena.h"
#include "object/sp-item.h"
#include "object/sp-root.h"
#include "util/units.h"
namespace Inkscape {
namespace UI {
namespace View {
/**
* Callback connected with arena_event.
*/
// This hasn't worked since at least 0.48. It should result in a cursor change over <a></a> links.
// There should be a better way of doing this. See note in canvas-arena.cpp.
static gint arena_handler(SPCanvasArena */*arena*/, Inkscape::DrawingItem *ai,
GdkEvent *event, SVGViewWidget *svgview)
{
static gdouble x, y;
static gboolean active = FALSE;
SPEvent spev;
SPItem *spitem = (ai) ? ai->getItem() : nullptr;
switch (event->type) {
case GDK_BUTTON_PRESS:
if (event->button.button == 1) {
active = TRUE;
x = event->button.x;
y = event->button.y;
}
break;
case GDK_BUTTON_RELEASE:
if (event->button.button == 1) {
if (active && (event->button.x == x) &&
(event->button.y == y)) {
spev.type = SPEvent::ACTIVATE;
if ( spitem != nullptr )
{
spitem->emitEvent (spev);
}
}
}
active = FALSE;
break;
case GDK_MOTION_NOTIFY:
active = FALSE;
break;
case GDK_ENTER_NOTIFY:
spev.type = SPEvent::MOUSEOVER;
spev.view = svgview;
if ( spitem != nullptr )
{
spitem->emitEvent (spev);
}
break;
case GDK_LEAVE_NOTIFY:
spev.type = SPEvent::MOUSEOUT;
spev.view = svgview;
if ( spitem != nullptr )
{
spitem->emitEvent (spev);
}
break;
default:
break;
}
return TRUE;
}
/**
* A light-weight widget containing an SPCanvas for rendering an SVG.
* It's derived from a Gtk::ScrolledWindow like the previous C version, but that doesn't seem to be
* too useful.
*/
SVGViewWidget::SVGViewWidget(SPDocument* document)
: _document(nullptr)
, _dkey(0)
, _parent(nullptr)
, _drawing(nullptr)
, _hscale(1.0)
, _vscale(1.0)
, _rescale(false)
, _keepaspect(false)
, _width(0.0)
, _height(0.0)
{
_canvas = SPCanvas::createAA();
add(*Glib::wrap(_canvas));
SPCanvasItem* item =
sp_canvas_item_new(SP_CANVAS(_canvas)->getRoot(), SP_TYPE_CANVAS_GROUP, nullptr);
_parent = SP_CANVAS_GROUP(item);
_drawing = sp_canvas_item_new (_parent, SP_TYPE_CANVAS_ARENA, nullptr);
g_signal_connect (G_OBJECT (_drawing), "arena_event", G_CALLBACK (arena_handler), this);
setDocument(document);
signal_size_allocate().connect(sigc::mem_fun(*this, &SVGViewWidget::size_allocate));
}
SVGViewWidget::~SVGViewWidget()
{
if (_document) {
_document = nullptr;
}
}
void
SVGViewWidget::setDocument(SPDocument* document)
{
// Clear old document
if (_document) {
_document->getRoot()->invoke_hide(_dkey); // Removed from display tree
}
// Add new document
if (document) {
_document = document;
Inkscape::DrawingItem *ai = document->getRoot()->invoke_show(
SP_CANVAS_ARENA (_drawing)->drawing,
_dkey,
SP_ITEM_SHOW_DISPLAY);
if (ai) {
SP_CANVAS_ARENA (_drawing)->drawing.root()->prependChild(ai);
}
doRescale ();
}
}
void
SVGViewWidget::setResize(int width, int height)
{
// Triggers size_allocation which calls SVGViewWidget::size_allocate.
set_size_request(width, height);
queue_resize();
}
void
SVGViewWidget::size_allocate(Gtk::Allocation& allocation)
{
double width = allocation.get_width();
double height = allocation.get_height();
if (width < 0.0 || height < 0.0) {
std::cerr << "SVGViewWidget::size_allocate: negative dimensions!" << std::endl;
return;
}
_rescale = true;
_keepaspect = true;
_width = width;
_height = height;
doRescale ();
}
void
SVGViewWidget::doRescale()
{
if (!_document) {
std::cerr << "SVGViewWidget::doRescale: No document!" << std::endl;
return;
}
if (_document->getWidth().value("px") < 1e-9) {
std::cerr << "SVGViewWidget::doRescale: Width too small!" << std::endl;
return;
}
if (_document->getHeight().value("px") < 1e-9) {
std::cerr << "SVGViewWidget::doRescale: Height too small!" << std::endl;
return;
}
double x_offset = 0.0;
double y_offset = 0.0;
if (_rescale) {
_hscale = _width / _document->getWidth().value("px");
_vscale = _height / _document->getHeight().value("px");
if (_keepaspect) {
if (_hscale > _vscale) {
_hscale = _vscale;
x_offset = (_width - _document->getWidth().value("px") * _vscale)/2.0;
} else {
_vscale = _hscale;
y_offset = (_height - _document->getHeight().value("px") * _hscale)/2.0;
}
}
}
if (_drawing) {
sp_canvas_item_affine_absolute (_drawing, Geom::Scale(_hscale, _vscale) * Geom::Translate(x_offset, y_offset));
}
}
void
SVGViewWidget::mouseover()
{
GdkDisplay *display = gdk_display_get_default();
GdkCursor *cursor = gdk_cursor_new_for_display(display, GDK_HAND2);
GdkWindow *window = gtk_widget_get_window (GTK_WIDGET(SP_CANVAS_ITEM(_drawing)->canvas));
gdk_window_set_cursor(window, cursor);
g_object_unref(cursor);
}
void
SVGViewWidget::mouseout()
{
GdkWindow *window = gtk_widget_get_window (GTK_WIDGET(SP_CANVAS_ITEM(_drawing)->canvas));
gdk_window_set_cursor(window, nullptr);
}
} // Namespace View
} // Namespace UI
} // 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:fileencoding=utf-8 :
|