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;
92 if (top.window && top.window.registerShow) // register show function in html doc for dynamic sections
94 top.window.registerShow(sectionId,show);
99 "onmousedown" : "handleMouseDown(evt)",
100 "onmousemove" : "handleMouseMove(evt)",
101 "onmouseup" : "handleMouseUp(evt)"
104 if (window.addEventListener)
106 if (navigator.userAgent.toLowerCase().indexOf('webkit') >= 0 ||
107 navigator.userAgent.toLowerCase().indexOf("opera") >= 0 ||
108 navigator.appVersion.indexOf("MSIE") != -1)
110 window.addEventListener('mousewheel', handleMouseWheel, false); // Chrome/Safari/IE9
114 window.addEventListener('DOMMouseScroll', handleMouseWheel, false); // Others
119 window.onresize=function()
121 if (svgDoc) { show(); }
125 * Instance an SVGPoint object with given event coordinates.
127 function getEventPoint(evt)
129 var p = root.createSVGPoint();
136 * Sets the current transform matrix of an element.
138 function setCTM(element, matrix)
140 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
141 element.setAttribute("transform", s);
145 * Sets attributes of an element.
147 function setAttributes(element, attributes)
149 for (i in attributes)
150 element.setAttributeNS(null, i, attributes[i]);
153 function doZoom(g,point,zoomFactor)
155 var p = point.matrixTransform(g.getCTM().inverse());
156 var k = root.createSVGMatrix().translate(p.x, p.y).scale(zoomFactor).translate(-p.x, -p.y);
157 var n = g.getCTM().multiply(k);
158 var s = Math.max(n.a,n.d);
159 if (s>maxZoom) n=n.translate(p.x,p.y).scale(maxZoom/s).translate(-p.x,-p.y);
160 else if (s<minZoom) n=n.translate(p.x,p.y).scale(minZoom/s).translate(-p.x,-p.y);
162 stateTf = stateTf.multiply(n.inverse());
166 * Handle mouse move event.
168 function handleMouseWheel(evt)
170 if (!evt) evt = window.evt;
171 if (!evt.shiftKey) return; // only zoom when shift is pressed
172 if (evt.preventDefault) evt.preventDefault();
173 evt.returnValue = false;
180 delta = evt.wheelDelta / 7200; // Opera/Chrome/IE9/Safari
184 delta = evt.detail / -180; // Mozilla
186 var svgDoc = evt.target.ownerDocument;
187 var g = svgDoc.getElementById("viewport");
188 var p = getEventPoint(evt);
194 * Handle mouse move event.
196 function handleMouseMove(evt)
198 if(evt.preventDefault)
199 evt.preventDefault();
201 evt.returnValue = false;
203 var g = svgDoc.getElementById("viewport");
208 var p = getEventPoint(evt).matrixTransform(stateTf);
209 setCTM(g,stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y));
214 * Handle click event.
216 function handleMouseDown(evt)
218 if(evt.preventDefault)
219 evt.preventDefault();
220 evt.returnValue = false;
221 var g = svgDoc.getElementById("viewport");
223 stateTf = g.getCTM().inverse();
224 stateOrigin = getEventPoint(evt).matrixTransform(stateTf);
225 g.style.cursor = cursorGrab;
229 * Handle mouse button release event.
231 function handleMouseUp(evt)
233 if (evt.preventDefault) evt.preventDefault();
234 evt.returnValue = false;
235 var g = svgDoc.getElementById("viewport");
236 g.style.cursor = "default";
242 * Dumps a matrix to a string (useful for debug).
244 function dumpMatrix(matrix)
246 var s = "[ " + matrix.a + ", " + matrix.c + ", " + matrix.e + "\n " + matrix.b + ", " + matrix.d + ", " + matrix.f + "\n 0, 0, 1 ]";
251 * Handler for pan buttons
253 function handlePan(x,y)
255 var g = svgDoc.getElementById("viewport");
256 setCTM(g,g.getCTM().translate(x*20/minZoom,y*20/minZoom));
260 * Handle reset button
262 function handleReset()
268 * Handler for zoom buttons
270 function handleZoom(evt,direction)
272 var g = svgDoc.getElementById("viewport");
273 var factor = direction=='in' ? zoomInFactor : zoomOutFactor;
275 var p = root.createSVGPoint();
277 p.y = windowHeight/2;
281 function serializeXmlNode(xmlNode)
283 if (typeof window.XMLSerializer != "undefined") {
284 return (new window.XMLSerializer()).serializeToString(xmlNode);
285 } else if (typeof xmlNode.xml != "undefined") {
292 * Handler for print function
294 function handlePrint(evt)
296 evt.returnValue = false;
297 var g = svgDoc.getElementById("graph");
298 var xs = serializeXmlNode(g);
300 var w = window.open('about:blank','_blank','width='+windowWidth+',height='+windowHeight+
301 ',toolbar=0,status=0,menubar=0,scrollbars=0,resizable=0,location=0,directories=0');
303 d.write('<html xmlns="http://www.w3.org/1999/xhtml" '+
304 'xmlns:svg="http://www.w3.org/2000/svg" '+
305 'xmlns:xlink="http://www.w3.org/1999/xlink">');
306 d.write('<head><title>Print SVG</title></head>');
307 d.write('<body style="margin: 0px; padding: 0px;" onload="window.print();">');
308 d.write('<div id="svg" style="width:'+windowWidth+'px; height:'+windowHeight+'px;">'+xs+'</div>');
313 alert('Failed to open popup window needed for printing!\n'+e.message);