summaryrefslogtreecommitdiffstats
path: root/src/ui/view/svg-view-widget.cpp
blob: 45755e237e4fc2b611fc901f111b91ab50c61d88 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/**
 * @file
 * A light-weight widget containing an Inkscape canvas 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/drawing.h"
#include "display/control/canvas-item.h"
#include "display/control/canvas-item-drawing.h"
#include "display/control/canvas-item-group.h"

#include "object/sp-item.h"
#include "object/sp-root.h"

#include "ui/widget/canvas.h"

#include "util/units.h"

namespace Inkscape {
namespace UI {
namespace View {

/**
 * Callback connected with drawing_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 bool _drawing_handler(GdkEvent *event, Inkscape::DrawingItem *drawing_item, SVGViewWidget *svgview)
{
    static gdouble x, y;
    static gboolean active = FALSE;
    SPEvent spev;

    SPItem *spitem = (drawing_item) ? drawing_item->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.
 */
SVGViewWidget::SVGViewWidget(SPDocument* document)
{
    _canvas = Gtk::make_managed<Inkscape::UI::Widget::Canvas>();
    add(*_canvas);

    _parent = new Inkscape::CanvasItemGroup(_canvas->get_canvas_item_root());
    _drawing = new Inkscape::CanvasItemDrawing(_parent);
    _canvas->set_drawing(_drawing->get_drawing());
    _drawing->connect_drawing_event(sigc::bind(sigc::ptr_fun(_drawing_handler), this));

    setDocument(document);

    show_all();
}

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 *drawing_item = document->getRoot()->invoke_show(
            *_drawing->get_drawing(),
            _dkey,
            SP_ITEM_SHOW_DISPLAY);

        if (drawing_item) {
            _drawing->get_drawing()->root()->prependChild(drawing_item);
        }

        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::on_size_allocate(Gtk::Allocation& allocation)
{
    if (!(_allocation == allocation)) {
        _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;
            Gtk::Bin::on_size_allocate(allocation);
            return;
        }

        _rescale = true;
        _keepaspect = true;
        _width = width;
        _height = height;

        doRescale ();
    }

    Gtk::Bin::on_size_allocate(allocation);
}

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 = (_document->getWidth().value("px") * _hscale - _width) / 2.0;
            } else {
                _vscale = _hscale;
                y_offset = (_document->getHeight().value("px") * _vscale - _height) / 2.0;
            }
        }
    }

    if (_drawing) {
        _canvas->set_affine(Geom::Scale(_hscale, _vscale));
        _canvas->set_pos(Geom::Point(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(_canvas->gobj()));
    gdk_window_set_cursor(window, cursor);
    g_object_unref(cursor);
}

void
SVGViewWidget::mouseout()
{
    GdkWindow *window = gtk_widget_get_window (GTK_WIDGET(_canvas->gobj()));
    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 :