summaryrefslogtreecommitdiff
path: root/src/svgpan_js.h
blob: ac996da5f1c166d9f210488ebf406dfa993bbe36 (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"/**\n"
" * The code below is based on SVGPan Library 1.2 and was modified for doxygen\n"
" * to support both zooming and panning via the mouse and via embedded bottons.\n"
" *\n"
" * This code is licensed under the following BSD license:\n"
" *\n"
" * Copyright 2009-2010 Andrea Leofreddi <a.leofreddi@itcharm.com>. All rights reserved.\n"
" * \n"
" * Redistribution and use in source and binary forms, with or without modification, are\n"
" * permitted provided that the following conditions are met:\n"
" * \n"
" *    1. Redistributions of source code must retain the above copyright notice, this list of\n"
" *       conditions and the following disclaimer.\n"
" * \n"
" *    2. Redistributions in binary form must reproduce the above copyright notice, this list\n"
" *       of conditions and the following disclaimer in the documentation and/or other materials\n"
" *       provided with the distribution.\n"
" * \n"
" * THIS SOFTWARE IS PROVIDED BY Andrea Leofreddi ``AS IS'' AND ANY EXPRESS OR IMPLIED\n"
" * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND\n"
" * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Andrea Leofreddi OR\n"
" * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n"
" * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n"
" * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\n"
" * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\n"
" * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n"
" * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
" * \n"
" * The views and conclusions contained in the software and documentation are those of the\n"
" * authors and should not be interpreted as representing official policies, either expressed\n"
" * or implied, of Andrea Leofreddi.\n"
" */\n"
"\n"
"var root = document.documentElement;\n"
"var state = 'none';\n"
"var stateOrigin;\n"
"var stateTf = root.createSVGMatrix();\n"
"var cursorGrab = ' url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAMAAAAolt3jAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAAlQTFRFAAAA////////c3ilYwAAAAN0Uk5T//8A18oNQQAAAD1JREFUeNp0zlEKACAIA9Bt9z90bZBZkQj29qFBEuBOzQHSnWTTyckEfqUuZgFvslH4ch3qLCO/Kr8cAgwATw4Ax6XRCcoAAAAASUVORK5CYII=\"), move';\n"
"var zoomSteps = 10;\n"
"var zoomInFactor;\n"
"var zoomOutFactor;\n"
"var windowWidth;\n"
"var windowHeight;\n"
"var svgDoc;\n"
"var minZoom;\n"
"var maxZoom;\n"
"if (!window) window=this;\n"
"\n"
"/**\n"
" * Show the graph in the middle of the view, scaled to fit \n"
" */\n"
"function show()\n"
"{\n"
"  if (window.innerHeight) // Firefox\n"
"  {\n"
"    windowWidth = window.innerWidth;\n"
"    windowHeight = window.innerHeight;\n"
"  }\n"
"  else if (document.documentElement.clientWidth) // Chrome/Safari\n"
"  {\n"
"    windowWidth = document.documentElement.clientWidth\n"
"    windowHeight = document.documentElement.clientHeight\n"
"  }\n"
"  if (!windowWidth || !windowHeight) // failsafe\n"
"  {\n"
"    windowWidth = 800;\n"
"    windowHeight = 600;\n"
"  }\n"
"  minZoom = Math.min(Math.min(viewHeight,windowHeight)/viewHeight,Math.min(viewWidth,windowWidth)/viewWidth);\n"
"  maxZoom = minZoom+1.5;\n"
"  zoomInFactor = Math.pow(maxZoom/minZoom,1.0/zoomSteps);\n"
"  zoomOutFactor = 1.0/zoomInFactor;\n"
"\n"
"  var g = svgDoc.getElementById('viewport');\n"
"  try\n"
"  {\n"
"    var bb = g.getBBox(); // this can throw an exception if css { display: none }\n"
"    var tx = (windowWidth-viewWidth*minZoom+8)/(2*minZoom);\n"
"    var ty = viewHeight+(windowHeight-viewHeight*minZoom)/(2*minZoom);\n"
"    var a = 'scale('+minZoom+') rotate(0) translate('+tx+' '+ty+')';\n"
"    g.setAttribute('transform',a);\n"
"  }\n"
"  catch(e) {}\n"
"}\n"
"\n"
"/**\n"
" * Register handlers\n"
" */\n"
"function init(evt) \n"
"{\n"
"  svgDoc = evt.target.ownerDocument;\n"
"  if (top.window && top.window.registerShow) // register show function in html doc for dynamic sections\n"
"  {\n"
"    top.window.registerShow(sectionId,show);\n"
"  }\n"
"  show();\n"
"\n"
"  setAttributes(root, {\n"
"     \"onmousedown\" : \"handleMouseDown(evt)\",\n"
"     \"onmousemove\" : \"handleMouseMove(evt)\",\n"
"     \"onmouseup\"   : \"handleMouseUp(evt)\"\n"
"  });\n"
"\n"
"  if (window.addEventListener)\n"
"  {\n"
"    if (navigator.userAgent.toLowerCase().indexOf('webkit') >= 0 || \n"
"        navigator.userAgent.toLowerCase().indexOf(\"opera\") >= 0 || \n"
"        navigator.appVersion.indexOf(\"MSIE\") != -1)\n"
"    {\n"
"      window.addEventListener('mousewheel', handleMouseWheel, false); // Chrome/Safari/IE9\n"
"    }\n"
"    else\n"
"    {\n"
"      window.addEventListener('DOMMouseScroll', handleMouseWheel, false); // Others\n"
"    }\n"
"  }\n"
"}\n"
"\n"
"window.onresize=function()\n"
"{\n"
"  if (svgDoc) { show(); }\n"
"}\n"
"\n"
"/**\n"
" * Instance an SVGPoint object with given event coordinates.\n"
" */\n"
"function getEventPoint(evt) \n"
"{\n"
"  var p = root.createSVGPoint();\n"
"  p.x = evt.clientX;\n"
"  p.y = evt.clientY;\n"
"  return p;\n"
"}\n"
"\n"
"/**\n"
" * Sets the current transform matrix of an element.\n"
" */\n"
"function setCTM(element, matrix) \n"
"{\n"
"  var s = \"matrix(\" + matrix.a + \",\" + matrix.b + \",\" + matrix.c + \",\" + matrix.d + \",\" + matrix.e + \",\" + matrix.f + \")\";\n"
"  element.setAttribute(\"transform\", s);\n"
"}\n"
"\n"
"/**\n"
" * Sets attributes of an element.\n"
" */\n"
"function setAttributes(element, attributes)\n"
"{\n"
"  for (i in attributes)\n"
"    element.setAttributeNS(null, i, attributes[i]);\n"
"}\n"
"\n"
"function doZoom(g,point,zoomFactor)\n"
"{\n"
"  var p = point.matrixTransform(g.getCTM().inverse());\n"
"  var k = root.createSVGMatrix().translate(p.x, p.y).scale(zoomFactor).translate(-p.x, -p.y);\n"
"  var n = g.getCTM().multiply(k);\n"
"  var s = Math.max(n.a,n.d);\n"
"  if      (s>maxZoom) n=n.translate(p.x,p.y).scale(maxZoom/s).translate(-p.x,-p.y);\n"
"  else if (s<minZoom) n=n.translate(p.x,p.y).scale(minZoom/s).translate(-p.x,-p.y);\n"
"  setCTM(g, n);\n"
"  stateTf = stateTf.multiply(n.inverse());\n"
"}\n"
"\n"
"/**\n"
" * Handle mouse move event.\n"
" */\n"
"function handleMouseWheel(evt) \n"
"{\n"
"  if (!evt) evt = window.evt;\n"
"  if (!evt.shiftKey) return; // only zoom when shift is pressed\n"
"  if (evt.preventDefault) evt.preventDefault();\n"
"  evt.returnValue = false;\n"
"\n"
"  if (state!='pan')\n"
"  {\n"
"    var delta;\n"
"    if (evt.wheelDelta)\n"
"    {\n"
"      delta = evt.wheelDelta / 7200; // Opera/Chrome/IE9/Safari\n"
"    }\n"
"    else\n"
"    {\n"
"      delta = evt.detail / -180; // Mozilla\n"
"    }\n"
"    var svgDoc = evt.target.ownerDocument;\n"
"    var g = svgDoc.getElementById(\"viewport\");\n"
"    var p = getEventPoint(evt);\n"
"    doZoom(g,p,1+delta);\n"
"  }\n"
"}\n"
"\n"
"/**\n"
" * Handle mouse move event.\n"
" */\n"
"function handleMouseMove(evt) \n"
"{\n"
"  if(evt.preventDefault)\n"
"    evt.preventDefault();\n"
"\n"
"  evt.returnValue = false;\n"
"\n"
"  var g = svgDoc.getElementById(\"viewport\");\n"
"\n"
"  if (state == 'pan') \n"
"  {\n"
"    // Pan mode\n"
"    var p = getEventPoint(evt).matrixTransform(stateTf);\n"
"    setCTM(g,stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y));\n"
"  } \n"
"}\n"
"\n"
"/**\n"
" * Handle click event.\n"
" */\n"
"function handleMouseDown(evt) \n"
"{\n"
"  if(evt.preventDefault)\n"
"    evt.preventDefault();\n"
"  evt.returnValue = false;\n"
"  var g = svgDoc.getElementById(\"viewport\");\n"
"  state = 'pan';\n"
"  stateTf = g.getCTM().inverse();\n"
"  stateOrigin = getEventPoint(evt).matrixTransform(stateTf);\n"
"  g.style.cursor = cursorGrab;\n"
"}\n"
"\n"
"/**\n"
" * Handle mouse button release event.\n"
" */\n"
"function handleMouseUp(evt) \n"
"{\n"
"  if (evt.preventDefault) evt.preventDefault();\n"
"  evt.returnValue = false;\n"
"  var g = svgDoc.getElementById(\"viewport\");\n"
"  g.style.cursor = \"default\";\n"
"  // Quit pan mode\n"
"  state = '';\n"
"}\n"
"\n"
"/**\n"
" * Dumps a matrix to a string (useful for debug).\n"
" */\n"
"function dumpMatrix(matrix) \n"
"{\n"
"  var s = \"[ \" + matrix.a + \", \" + matrix.c + \", \" + matrix.e + \"\\n  \" + matrix.b + \", \" + matrix.d + \", \" + matrix.f + \"\\n  0, 0, 1 ]\";\n"
"  return s;\n"
"}\n"
"\n"
"/**\n"
" * Handler for pan buttons\n"
" */\n"
"function handlePan(x,y)\n"
"{\n"
"  var g = svgDoc.getElementById(\"viewport\");\n"
"  setCTM(g,g.getCTM().translate(x*20/minZoom,y*20/minZoom));\n"
"}\n"
"\n"
"/**\n"
" * Handle reset button\n"
" */\n"
"function handleReset()\n"
"{\n"
"  show();\n"
"}\n"
"\n"
"/**\n"
" * Handler for zoom buttons\n"
" */\n"
"function handleZoom(evt,direction)\n"
"{\n"
"  var g = svgDoc.getElementById(\"viewport\");\n"
"  var factor = direction=='in' ? zoomInFactor : zoomOutFactor;\n"
"  var m = g.getCTM();\n"
"  var p = root.createSVGPoint();\n"
"  p.x = windowWidth/2;\n"
"  p.y = windowHeight/2;\n"
"  doZoom(g,p,factor);\n"
"}\n"
"\n"
"function serializeXmlNode(xmlNode) \n"
"{\n"
"  if (typeof window.XMLSerializer != \"undefined\") {\n"
"    return (new window.XMLSerializer()).serializeToString(xmlNode);\n"
"  } else if (typeof xmlNode.xml != \"undefined\") {\n"
"    return xmlNode.xml;\n"
"  }\n"
"  return \"\";\n"
"}\n"
"\n"
"/** \n"
" * Handler for print function\n"
" */\n"
"function handlePrint(evt)\n"
"{\n"
"  evt.returnValue = false;\n"
"  var g = svgDoc.getElementById(\"graph\");\n"
"  var xs = serializeXmlNode(g);\n"
"  try {\n"
"    var w = window.open('about:blank','_blank','width='+windowWidth+',height='+windowHeight+\n"
"                        ',toolbar=0,status=0,menubar=0,scrollbars=0,resizable=0,location=0,directories=0');\n"
"    var d = w.document;\n"
"    d.write('<html xmlns=\"http://www.w3.org/1999/xhtml\" '+\n"
"            'xmlns:svg=\"http://www.w3.org/2000/svg\" '+\n"
"            'xmlns:xlink=\"http://www.w3.org/1999/xlink\">');\n"
"    d.write('<head><title>Print SVG</title></head>');\n"
"    d.write('<body style=\"margin: 0px; padding: 0px;\" onload=\"window.print();\">');\n"
"    d.write('<div id=\"svg\" style=\"width:'+windowWidth+'px; height:'+windowHeight+'px;\">'+xs+'</div>');\n"
"    d.write('</body>');\n"
"    d.write('</html>');\n"
"    d.close();\n"
"  } catch(e) {\n"
"    alert('Failed to open popup window needed for printing!\\n'+e.message);\n"
"  }\n"
"}\n"
"\n"
"\n"
"\n"
"\n"