1 | dojo.provide("dojox.drawing.manager.Canvas"); |
---|
2 | |
---|
3 | (function(){ |
---|
4 | |
---|
5 | dojox.drawing.manager.Canvas = dojox.drawing.util.oo.declare( |
---|
6 | // summary: |
---|
7 | // Creates a dojox.gfx.surface to be used for Drawing. Note that |
---|
8 | // The 'surface' that Drawing uses is actually a dojox.gfx.group. |
---|
9 | // This allows for more versatility. |
---|
10 | // |
---|
11 | // Called internally from a dojox.Drawing. |
---|
12 | // |
---|
13 | // Note: Surface creation is asynchrous. Connect to |
---|
14 | // onSurfaceReady in Drawing. |
---|
15 | // |
---|
16 | function(/*Object*/options){ |
---|
17 | dojo.mixin(this, options); |
---|
18 | |
---|
19 | var dim = dojo.contentBox(this.srcRefNode); |
---|
20 | this.height = this.parentHeight = dim.h; |
---|
21 | this.width = this.parentWidth = dim.w; |
---|
22 | this.domNode = dojo.create("div", {id:"canvasNode"}, this.srcRefNode); |
---|
23 | dojo.style(this.domNode, { |
---|
24 | width:this.width, |
---|
25 | height:"auto" |
---|
26 | }); |
---|
27 | |
---|
28 | dojo.setSelectable(this.domNode, false); |
---|
29 | |
---|
30 | this.id = this.id || this.util.uid("surface"); |
---|
31 | |
---|
32 | console.info("create canvas"); |
---|
33 | this.gfxSurface = dojox.gfx.createSurface(this.domNode, this.width, this.height); |
---|
34 | this.gfxSurface.whenLoaded(this, function(){ |
---|
35 | setTimeout(dojo.hitch(this, function(){ |
---|
36 | this.surfaceReady = true; |
---|
37 | if(dojo.isIE){ |
---|
38 | //this.gfxSurface.rawNode.parentNode.id = this.id; |
---|
39 | }else if(dojox.gfx.renderer == "silverlight"){ |
---|
40 | this.id = this.domNode.firstChild.id |
---|
41 | }else{ |
---|
42 | //this.gfxSurface.rawNode.id = this.id; |
---|
43 | } |
---|
44 | |
---|
45 | this.underlay = this.gfxSurface.createGroup(); |
---|
46 | this.surface = this.gfxSurface.createGroup(); |
---|
47 | this.overlay = this.gfxSurface.createGroup(); |
---|
48 | this.surface.setTransform({dx:0, dy:0,xx:1,yy:1}); |
---|
49 | |
---|
50 | this.gfxSurface.getDimensions = dojo.hitch(this.gfxSurface, "getDimensions"); |
---|
51 | if(options.callback){ |
---|
52 | options.callback(this.domNode); |
---|
53 | } |
---|
54 | }),500); |
---|
55 | }); |
---|
56 | this._mouseHandle = this.mouse.register(this); |
---|
57 | }, |
---|
58 | { |
---|
59 | // zoom: [readonly] Number |
---|
60 | // The amount the canvas is zoomed |
---|
61 | zoom:1, |
---|
62 | |
---|
63 | useScrollbars: true, |
---|
64 | baseClass:"drawingCanvas", |
---|
65 | |
---|
66 | resize: function(width, height){ |
---|
67 | // summary: |
---|
68 | // Method used to change size of canvas. Potentially |
---|
69 | // called from a container like ContentPane. May be |
---|
70 | // called directly. |
---|
71 | // |
---|
72 | this.parentWidth = width; |
---|
73 | this.parentHeight = height; |
---|
74 | this.setDimensions(width, height); |
---|
75 | }, |
---|
76 | |
---|
77 | setDimensions: function(width, height, scrollx, scrolly){ |
---|
78 | // summary: |
---|
79 | // Internal. Changes canvas size and sets scroll position. |
---|
80 | // Do not call this, use resize(). |
---|
81 | // |
---|
82 | // changing the size of the surface and setting scroll |
---|
83 | // if items are off screen |
---|
84 | var sw = this.getScrollWidth(); //+ 10; |
---|
85 | this.width = Math.max(width, this.parentWidth); |
---|
86 | this.height = Math.max(height, this.parentHeight); |
---|
87 | |
---|
88 | if(this.height>this.parentHeight){ |
---|
89 | this.width -= sw; |
---|
90 | } |
---|
91 | if(this.width>this.parentWidth){ |
---|
92 | this.height -= sw; |
---|
93 | } |
---|
94 | |
---|
95 | this.mouse.resize(this.width,this.height); |
---|
96 | this.gfxSurface.setDimensions(this.width, this.height); |
---|
97 | |
---|
98 | |
---|
99 | this.domNode.parentNode.scrollTop = scrolly || 0; |
---|
100 | this.domNode.parentNode.scrollLeft = scrollx || 0; |
---|
101 | |
---|
102 | |
---|
103 | if(this.useScrollbars){ |
---|
104 | //console.info("Set Canvas Scroll", (this.height > this.parentHeight), this.height, this.parentHeight) |
---|
105 | dojo.style(this.domNode.parentNode, { |
---|
106 | overflowY: this.height > this.parentHeight ? "scroll" : "hidden", |
---|
107 | overflowX: this.width > this.parentWidth ? "scroll" : "hidden" |
---|
108 | }); |
---|
109 | }else{ |
---|
110 | dojo.style(this.domNode.parentNode, { |
---|
111 | overflowY: "hidden", |
---|
112 | overflowX: "hidden" |
---|
113 | }); |
---|
114 | } |
---|
115 | }, |
---|
116 | |
---|
117 | |
---|
118 | setZoom: function(zoom){ |
---|
119 | // summary: |
---|
120 | // Internal. Zooms canvas in and out. |
---|
121 | this.zoom = zoom; |
---|
122 | this.surface.setTransform({xx:zoom, yy:zoom}); |
---|
123 | this.setDimensions(this.width*zoom, this.height*zoom) |
---|
124 | }, |
---|
125 | |
---|
126 | onScroll: function(){ |
---|
127 | // summary: |
---|
128 | // Event fires on scroll.NOT IMPLEMENTED |
---|
129 | }, |
---|
130 | |
---|
131 | getScrollOffset: function(){ |
---|
132 | // summary: |
---|
133 | // Get the scroll position of the canvas |
---|
134 | return { |
---|
135 | top:this.domNode.parentNode.scrollTop, |
---|
136 | left:this.domNode.parentNode.scrollLeft |
---|
137 | }; // Object |
---|
138 | }, |
---|
139 | |
---|
140 | getScrollWidth: function(){ |
---|
141 | // summary: |
---|
142 | // Special method used to detect the width (and height) |
---|
143 | // of the browser scrollbars. Becomes memoized. |
---|
144 | // |
---|
145 | var p = dojo.create('div'); |
---|
146 | p.innerHTML = '<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:0;left:-1000px;"><div style="height:100px;"></div>'; |
---|
147 | var div = p.firstChild; |
---|
148 | dojo.body().appendChild(div); |
---|
149 | var noscroll = dojo.contentBox(div).h; |
---|
150 | dojo.style(div, "overflow", "scroll"); |
---|
151 | var scrollWidth = noscroll - dojo.contentBox(div).h; |
---|
152 | dojo.destroy(div); |
---|
153 | this.getScrollWidth = function(){ |
---|
154 | return scrollWidth; |
---|
155 | }; |
---|
156 | return scrollWidth; // Object |
---|
157 | } |
---|
158 | } |
---|
159 | ); |
---|
160 | |
---|
161 | })(); |
---|