2 * The code below is based on SVGPan Library 1.2 and was modified for doxygen
3 * to support both zooming and panning via the mouse and via embedded bottons.
5 * This code is licensed under the following BSD license:
7 * Copyright 2009-2010 Andrea Leofreddi <a.leofreddi@itcharm.com>. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without modification, are
10 * permitted provided that the following conditions are met:
12 * 1. Redistributions of source code must retain the above copyright notice, this list of
13 * conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
16 * of conditions and the following disclaimer in the documentation and/or other materials
17 * provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY Andrea Leofreddi ``AS IS'' AND ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Andrea Leofreddi OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation are those of the
30 * authors and should not be interpreted as representing official policies, either expressed
31 * or implied, of Andrea Leofreddi.
34 var root = document.documentElement;
37 var stateTf = root.createSVGMatrix();
38 var cursorGrab = ' url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAMAAAAolt3jAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAAlQTFRFAAAA////////c3ilYwAAAAN0Uk5T//8A18oNQQAAAD1JREFUeNp0zlEKACAIA9Bt9z90bZBZkQj29qFBEuBOzQHSnWTTyckEfqUuZgFvslH4ch3qLCO/Kr8cAgwATw4Ax6XRCcoAAAAASUVORK5CYII="), move';
47 if (!window) window=this;
50 * Show the graph in the middle of the view, scaled to fit
54 if (window.innerHeight) // Firefox
56 windowWidth = window.innerWidth;
57 windowHeight = window.innerHeight;
59 else if (document.documentElement.clientWidth) // Chrome/Safari
61 windowWidth = document.documentElement.clientWidth
62 windowHeight = document.documentElement.clientHeight
64 if (!windowWidth || !windowHeight) // failsafe
69 minZoom = Math.min(Math.min(viewHeight,windowHeight)/viewHeight,Math.min(viewWidth,windowWidth)/viewWidth);
70 maxZoom = minZoom+1.5;
71 zoomInFactor = Math.pow(maxZoom/minZoom,1.0/zoomSteps);
72 zoomOutFactor = 1.0/zoomInFactor;
74 var g = svgDoc.getElementById('viewport');
77 var bb = g.getBBox(); // this can throw an exception if css { display: none }
78 var tx = (windowWidth-viewWidth*minZoom+8)/(2*minZoom);
79 var ty = viewHeight+(windowHeight-viewHeight*minZoom)/(2*minZoom);
80 var a = 'scale('+minZoom+') rotate(0) translate('+tx+' '+ty+')';
81 g.setAttribute('transform',a);
91 svgDoc = evt.target.ownerDocument;
93 if (top.window && top.window.registerShow) { // register show function in html doc for dynamic sections
94 top.window.registerShow(sectionId,show);
97 // ugh, we are not allowed to talk to the parent; can happen with Chrome when viewing pages
98 // locally, since they treat every local page as having a different origin
102 setAttributes(root, {
103 "onmousedown" : "handleMouseDown(evt)",
104 "onmousemove" : "handleMouseMove(evt)",
105 "onmouseup" : "handleMouseUp(evt)"
108 if (window.addEventListener)
110 if (navigator.userAgent.toLowerCase().indexOf('webkit') >= 0 ||
111 navigator.userAgent.toLowerCase().indexOf("opera") >= 0 ||
112 navigator.appVersion.indexOf("MSIE") != -1)
114 window.addEventListener('mousewheel', handleMouseWheel, false); // Chrome/Safari/IE9
118 window.addEventListener('DOMMouseScroll', handleMouseWheel, false); // Others
123 window.onresize=function()
125 if (svgDoc) { show(); }
129 * Instance an SVGPoint object with given event coordinates.
131 function getEventPoint(evt)
133 var p = root.createSVGPoint();
140 * Sets the current transform matrix of an element.
142 function setCTM(element, matrix)
144 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
145 element.setAttribute("transform", s);
149 * Sets attributes of an element.
151 function setAttributes(element, attributes)
153 for (i in attributes)
154 element.setAttributeNS(null, i, attributes[i]);
157 function doZoom(g,point,zoomFactor)
159 var p = point.matrixTransform(g.getCTM().inverse());
160 var k = root.createSVGMatrix().translate(p.x, p.y).scale(zoomFactor).translate(-p.x, -p.y);
161 var n = g.getCTM().multiply(k);
162 var s = Math.max(n.a,n.d);
163 if (s>maxZoom) n=n.translate(p.x,p.y).scale(maxZoom/s).translate(-p.x,-p.y);
164 else if (s<minZoom) n=n.translate(p.x,p.y).scale(minZoom/s).translate(-p.x,-p.y);
166 stateTf = stateTf.multiply(n.inverse());
170 * Handle mouse move event.
172 function handleMouseWheel(evt)
174 if (!evt) evt = window.evt;
175 if (!evt.shiftKey) return; // only zoom when shift is pressed
176 if (evt.preventDefault) evt.preventDefault();
177 evt.returnValue = false;
184 delta = evt.wheelDelta / 7200; // Opera/Chrome/IE9/Safari
188 delta = evt.detail / -180; // Mozilla
190 var svgDoc = evt.target.ownerDocument;
191 var g = svgDoc.getElementById("viewport");
192 var p = getEventPoint(evt);
198 * Handle mouse move event.
200 function handleMouseMove(evt)
202 if(evt.preventDefault)
203 evt.preventDefault();
205 evt.returnValue = false;
207 var g = svgDoc.getElementById("viewport");
212 var p = getEventPoint(evt).matrixTransform(stateTf);
213 setCTM(g,stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y));
218 * Handle click event.
220 function handleMouseDown(evt)
222 if(evt.preventDefault)
223 evt.preventDefault();
224 evt.returnValue = false;
225 var g = svgDoc.getElementById("viewport");
227 stateTf = g.getCTM().inverse();
228 stateOrigin = getEventPoint(evt).matrixTransform(stateTf);
229 g.style.cursor = cursorGrab;
233 * Handle mouse button release event.
235 function handleMouseUp(evt)
237 if (evt.preventDefault) evt.preventDefault();
238 evt.returnValue = false;
239 var g = svgDoc.getElementById("viewport");
240 g.style.cursor = "default";
246 * Dumps a matrix to a string (useful for debug).
248 function dumpMatrix(matrix)
250 var s = "[ " + matrix.a + ", " + matrix.c + ", " + matrix.e + "\n " + matrix.b + ", " + matrix.d + ", " + matrix.f + "\n 0, 0, 1 ]";
255 * Handler for pan buttons
257 function handlePan(x,y)
259 var g = svgDoc.getElementById("viewport");
260 setCTM(g,g.getCTM().translate(x*20/minZoom,y*20/minZoom));
264 * Handle reset button
266 function handleReset()
272 * Handler for zoom buttons
274 function handleZoom(evt,direction)
276 var g = svgDoc.getElementById("viewport");
277 var factor = direction=='in' ? zoomInFactor : zoomOutFactor;
279 var p = root.createSVGPoint();
281 p.y = windowHeight/2;
285 function serializeXmlNode(xmlNode)
287 if (typeof window.XMLSerializer != "undefined") {
288 return (new window.XMLSerializer()).serializeToString(xmlNode);
289 } else if (typeof xmlNode.xml != "undefined") {
296 * Handler for print function
298 function handlePrint(evt)
300 evt.returnValue = false;
301 var g = svgDoc.getElementById("graph");
302 var xs = serializeXmlNode(g);
304 var w = window.open('about:blank','_blank','width='+windowWidth+',height='+windowHeight+
305 ',toolbar=0,status=0,menubar=0,scrollbars=0,resizable=0,location=0,directories=0');
307 d.write('<html xmlns="http://www.w3.org/1999/xhtml" '+
308 'xmlns:svg="http://www.w3.org/2000/svg" '+
309 'xmlns:xlink="http://www.w3.org/1999/xlink">');
310 d.write('<head><title>Print SVG</title></head>');
311 d.write('<body style="margin: 0px; padding: 0px;" onload="window.print();">');
312 d.write('<div id="svg" style="width:'+windowWidth+'px; height:'+windowHeight+'px;">'+xs+'</div>');
317 alert('Failed to open popup window needed for printing!\n'+e.message);