1 /*
2 * Copyright (C) 2004, 2005 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #include "config.h"
22 #include "core/svg/SVGDocument.h"
23
24 #include "SVGNames.h"
25 #include "bindings/v8/ExceptionStatePlaceholder.h"
26 #include "core/events/ThreadLocalEventNames.h"
27 #include "core/frame/FrameView.h"
28 #include "core/rendering/RenderView.h"
29 #include "core/svg/SVGElement.h"
30 #include "core/svg/SVGSVGElement.h"
31 #include "core/svg/SVGViewSpec.h"
32 #include "core/svg/SVGZoomAndPan.h"
33 #include "core/svg/SVGZoomEvent.h"
34
35 namespace WebCore {
36
SVGDocument(const DocumentInit & initializer)37 SVGDocument::SVGDocument(const DocumentInit& initializer)
38 : Document(initializer, SVGDocumentClass)
39 {
40 ScriptWrappable::init(this);
41 }
42
rootElement() const43 SVGSVGElement* SVGDocument::rootElement() const
44 {
45 Element* elem = documentElement();
46 if (elem && elem->hasTagName(SVGNames::svgTag))
47 return toSVGSVGElement(elem);
48
49 return 0;
50 }
51
dispatchZoomEvent(float prevScale,float newScale)52 void SVGDocument::dispatchZoomEvent(float prevScale, float newScale)
53 {
54 RefPtr<SVGZoomEvent> event = SVGZoomEvent::create();
55 event->initEvent(EventTypeNames::zoom, true, false);
56 event->setPreviousScale(prevScale);
57 event->setNewScale(newScale);
58 rootElement()->dispatchEvent(event.release(), IGNORE_EXCEPTION);
59 }
60
dispatchScrollEvent()61 void SVGDocument::dispatchScrollEvent()
62 {
63 RefPtr<Event> event = Event::create();
64 event->initEvent(EventTypeNames::scroll, true, false);
65 rootElement()->dispatchEvent(event.release(), IGNORE_EXCEPTION);
66 }
67
zoomAndPanEnabled() const68 bool SVGDocument::zoomAndPanEnabled() const
69 {
70 if (SVGSVGElement* svg = rootElement()) {
71 if (svg->useCurrentView()) {
72 if (svg->currentView())
73 return svg->currentView()->zoomAndPan() == SVGZoomAndPanMagnify;
74 } else {
75 return svg->zoomAndPan() == SVGZoomAndPanMagnify;
76 }
77 }
78
79 return false;
80 }
81
startPan(const FloatPoint & start)82 void SVGDocument::startPan(const FloatPoint& start)
83 {
84 if (SVGSVGElement* svg = rootElement())
85 m_translate = FloatPoint(start.x() - svg->currentTranslate().x(), start.y() - svg->currentTranslate().y());
86 }
87
updatePan(const FloatPoint & pos) const88 void SVGDocument::updatePan(const FloatPoint& pos) const
89 {
90 if (SVGSVGElement* svg = rootElement()) {
91 svg->setCurrentTranslate(FloatPoint(pos.x() - m_translate.x(), pos.y() - m_translate.y()));
92 if (renderer())
93 renderer()->repaint();
94 }
95 }
96
childShouldCreateRenderer(const Node & child) const97 bool SVGDocument::childShouldCreateRenderer(const Node& child) const
98 {
99 if (child.hasTagName(SVGNames::svgTag))
100 return toSVGSVGElement(&child)->isValid();
101 return true;
102 }
103
cloneDocumentWithoutChildren()104 PassRefPtr<Document> SVGDocument::cloneDocumentWithoutChildren()
105 {
106 return create(DocumentInit(url()));
107 }
108
109 }
110