Project

General

Profile

Statistics
| Revision:

root / trunk / web / dojo / dojox / gfx / silverlight.js @ 12

History | View | Annotate | Download (15.3 KB)

1
/*
2
        Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3
        Available via Academic Free License >= 2.1 OR the modified BSD license.
4
        see: http://dojotoolkit.org/license for details
5
*/
6

    
7

    
8
if(!dojo._hasResource["dojox.gfx.silverlight"]){
9
dojo._hasResource["dojox.gfx.silverlight"]=true;
10
dojo.provide("dojox.gfx.silverlight");
11
dojo.require("dojox.gfx._base");
12
dojo.require("dojox.gfx.shape");
13
dojo.require("dojox.gfx.path");
14
dojo.experimental("dojox.gfx.silverlight");
15
dojox.gfx.silverlight.dasharray={solid:"none",shortdash:[4,1],shortdot:[1,1],shortdashdot:[4,1,1,1],shortdashdotdot:[4,1,1,1,1,1],dot:[1,3],dash:[4,3],longdash:[8,3],dashdot:[4,3,1,3],longdashdot:[8,3,1,3],longdashdotdot:[8,3,1,3,1,3]};
16
dojox.gfx.silverlight.fontweight={normal:400,bold:700};
17
dojox.gfx.silverlight.caps={butt:"Flat",round:"Round",square:"Square"};
18
dojox.gfx.silverlight.joins={bevel:"Bevel",round:"Round"};
19
dojox.gfx.silverlight.fonts={serif:"Times New Roman",times:"Times New Roman","sans-serif":"Arial",helvetica:"Arial",monotone:"Courier New",courier:"Courier New"};
20
dojox.gfx.silverlight.hexColor=function(_1){
21
var c=dojox.gfx.normalizeColor(_1),t=c.toHex(),a=Math.round(c.a*255);
22
a=(a<0?0:a>255?255:a).toString(16);
23
return "#"+(a.length<2?"0"+a:a)+t.slice(1);
24
};
25
dojo.extend(dojox.gfx.Shape,{setFill:function(_2){
26
var p=this.rawNode.getHost().content,r=this.rawNode,f;
27
if(!_2){
28
this.fillStyle=null;
29
this._setFillAttr(null);
30
return this;
31
}
32
if(typeof (_2)=="object"&&"type" in _2){
33
switch(_2.type){
34
case "linear":
35
this.fillStyle=f=dojox.gfx.makeParameters(dojox.gfx.defaultLinearGradient,_2);
36
var _3=p.createFromXaml("<LinearGradientBrush/>");
37
_3.mappingMode="Absolute";
38
_3.startPoint=f.x1+","+f.y1;
39
_3.endPoint=f.x2+","+f.y2;
40
dojo.forEach(f.colors,function(c){
41
var t=p.createFromXaml("<GradientStop/>");
42
t.offset=c.offset;
43
t.color=dojox.gfx.silverlight.hexColor(c.color);
44
_3.gradientStops.add(t);
45
});
46
this._setFillAttr(_3);
47
break;
48
case "radial":
49
this.fillStyle=f=dojox.gfx.makeParameters(dojox.gfx.defaultRadialGradient,_2);
50
var _4=p.createFromXaml("<RadialGradientBrush/>"),c=dojox.gfx.matrix.multiplyPoint(dojox.gfx.matrix.invert(this._getAdjustedMatrix()),f.cx,f.cy),pt=c.x+","+c.y;
51
_4.mappingMode="Absolute";
52
_4.gradientOrigin=pt;
53
_4.center=pt;
54
_4.radiusX=_4.radiusY=f.r;
55
dojo.forEach(f.colors,function(c){
56
var t=p.createFromXaml("<GradientStop/>");
57
t.offset=c.offset;
58
t.color=dojox.gfx.silverlight.hexColor(c.color);
59
_4.gradientStops.add(t);
60
});
61
this._setFillAttr(_4);
62
break;
63
case "pattern":
64
this.fillStyle=null;
65
this._setFillAttr(null);
66
break;
67
}
68
return this;
69
}
70
this.fillStyle=f=dojox.gfx.normalizeColor(_2);
71
var _5=p.createFromXaml("<SolidColorBrush/>");
72
_5.color=f.toHex();
73
_5.opacity=f.a;
74
this._setFillAttr(_5);
75
return this;
76
},_setFillAttr:function(f){
77
this.rawNode.fill=f;
78
},setStroke:function(_6){
79
var p=this.rawNode.getHost().content,r=this.rawNode;
80
if(!_6){
81
this.strokeStyle=null;
82
r.stroke=null;
83
return this;
84
}
85
if(typeof _6=="string"||dojo.isArray(_6)||_6 instanceof dojo.Color){
86
_6={color:_6};
87
}
88
var s=this.strokeStyle=dojox.gfx.makeParameters(dojox.gfx.defaultStroke,_6);
89
s.color=dojox.gfx.normalizeColor(s.color);
90
if(s){
91
var _7=p.createFromXaml("<SolidColorBrush/>");
92
_7.color=s.color.toHex();
93
_7.opacity=s.color.a;
94
r.stroke=_7;
95
r.strokeThickness=s.width;
96
r.strokeStartLineCap=r.strokeEndLineCap=r.strokeDashCap=dojox.gfx.silverlight.caps[s.cap];
97
if(typeof s.join=="number"){
98
r.strokeLineJoin="Miter";
99
r.strokeMiterLimit=s.join;
100
}else{
101
r.strokeLineJoin=dojox.gfx.silverlight.joins[s.join];
102
}
103
var da=s.style.toLowerCase();
104
if(da in dojox.gfx.silverlight.dasharray){
105
da=dojox.gfx.silverlight.dasharray[da];
106
}
107
if(da instanceof Array){
108
da=dojo.clone(da);
109
var i;
110
if(s.cap!="butt"){
111
for(i=0;i<da.length;i+=2){
112
--da[i];
113
if(da[i]<1){
114
da[i]=1;
115
}
116
}
117
for(i=1;i<da.length;i+=2){
118
++da[i];
119
}
120
}
121
r.strokeDashArray=da.join(",");
122
}else{
123
r.strokeDashArray=null;
124
}
125
}
126
return this;
127
},_getParentSurface:function(){
128
var _8=this.parent;
129
for(;_8&&!(_8 instanceof dojox.gfx.Surface);_8=_8.parent){
130
}
131
return _8;
132
},_applyTransform:function(){
133
var tm=this._getAdjustedMatrix(),r=this.rawNode;
134
if(tm){
135
var p=this.rawNode.getHost().content,m=p.createFromXaml("<MatrixTransform/>"),mm=p.createFromXaml("<Matrix/>");
136
mm.m11=tm.xx;
137
mm.m21=tm.xy;
138
mm.m12=tm.yx;
139
mm.m22=tm.yy;
140
mm.offsetX=tm.dx;
141
mm.offsetY=tm.dy;
142
m.matrix=mm;
143
r.renderTransform=m;
144
}else{
145
r.renderTransform=null;
146
}
147
return this;
148
},setRawNode:function(_9){
149
_9.fill=null;
150
_9.stroke=null;
151
this.rawNode=_9;
152
},_moveToFront:function(){
153
var c=this.parent.rawNode.children,r=this.rawNode;
154
c.remove(r);
155
c.add(r);
156
return this;
157
},_moveToBack:function(){
158
var c=this.parent.rawNode.children,r=this.rawNode;
159
c.remove(r);
160
c.insert(0,r);
161
return this;
162
},_getAdjustedMatrix:function(){
163
return this.matrix;
164
}});
165
dojo.declare("dojox.gfx.Group",dojox.gfx.Shape,{constructor:function(){
166
dojox.gfx.silverlight.Container._init.call(this);
167
},setRawNode:function(_a){
168
this.rawNode=_a;
169
}});
170
dojox.gfx.Group.nodeType="Canvas";
171
dojo.declare("dojox.gfx.Rect",dojox.gfx.shape.Rect,{setShape:function(_b){
172
this.shape=dojox.gfx.makeParameters(this.shape,_b);
173
this.bbox=null;
174
var r=this.rawNode,n=this.shape;
175
r.width=n.width;
176
r.height=n.height;
177
r.radiusX=r.radiusY=n.r;
178
return this._applyTransform();
179
},_getAdjustedMatrix:function(){
180
var m=this.matrix,s=this.shape,d={dx:s.x,dy:s.y};
181
return new dojox.gfx.Matrix2D(m?[m,d]:d);
182
}});
183
dojox.gfx.Rect.nodeType="Rectangle";
184
dojo.declare("dojox.gfx.Ellipse",dojox.gfx.shape.Ellipse,{setShape:function(_c){
185
this.shape=dojox.gfx.makeParameters(this.shape,_c);
186
this.bbox=null;
187
var r=this.rawNode,n=this.shape;
188
r.width=2*n.rx;
189
r.height=2*n.ry;
190
return this._applyTransform();
191
},_getAdjustedMatrix:function(){
192
var m=this.matrix,s=this.shape,d={dx:s.cx-s.rx,dy:s.cy-s.ry};
193
return new dojox.gfx.Matrix2D(m?[m,d]:d);
194
}});
195
dojox.gfx.Ellipse.nodeType="Ellipse";
196
dojo.declare("dojox.gfx.Circle",dojox.gfx.shape.Circle,{setShape:function(_d){
197
this.shape=dojox.gfx.makeParameters(this.shape,_d);
198
this.bbox=null;
199
var r=this.rawNode,n=this.shape;
200
r.width=r.height=2*n.r;
201
return this._applyTransform();
202
},_getAdjustedMatrix:function(){
203
var m=this.matrix,s=this.shape,d={dx:s.cx-s.r,dy:s.cy-s.r};
204
return new dojox.gfx.Matrix2D(m?[m,d]:d);
205
}});
206
dojox.gfx.Circle.nodeType="Ellipse";
207
dojo.declare("dojox.gfx.Line",dojox.gfx.shape.Line,{setShape:function(_e){
208
this.shape=dojox.gfx.makeParameters(this.shape,_e);
209
this.bbox=null;
210
var r=this.rawNode,n=this.shape;
211
r.x1=n.x1;
212
r.y1=n.y1;
213
r.x2=n.x2;
214
r.y2=n.y2;
215
return this;
216
}});
217
dojox.gfx.Line.nodeType="Line";
218
dojo.declare("dojox.gfx.Polyline",dojox.gfx.shape.Polyline,{setShape:function(_f,_10){
219
if(_f&&_f instanceof Array){
220
this.shape=dojox.gfx.makeParameters(this.shape,{points:_f});
221
if(_10&&this.shape.points.length){
222
this.shape.points.push(this.shape.points[0]);
223
}
224
}else{
225
this.shape=dojox.gfx.makeParameters(this.shape,_f);
226
}
227
this.bbox=null;
228
this._normalizePoints();
229
var p=this.shape.points,rp=[];
230
for(var i=0;i<p.length;++i){
231
rp.push(p[i].x,p[i].y);
232
}
233
this.rawNode.points=rp.join(",");
234
return this;
235
}});
236
dojox.gfx.Polyline.nodeType="Polyline";
237
dojo.declare("dojox.gfx.Image",dojox.gfx.shape.Image,{setShape:function(_11){
238
this.shape=dojox.gfx.makeParameters(this.shape,_11);
239
this.bbox=null;
240
var r=this.rawNode,n=this.shape;
241
r.width=n.width;
242
r.height=n.height;
243
r.source=n.src;
244
return this._applyTransform();
245
},_getAdjustedMatrix:function(){
246
var m=this.matrix,s=this.shape,d={dx:s.x,dy:s.y};
247
return new dojox.gfx.Matrix2D(m?[m,d]:d);
248
},setRawNode:function(_12){
249
this.rawNode=_12;
250
}});
251
dojox.gfx.Image.nodeType="Image";
252
dojo.declare("dojox.gfx.Text",dojox.gfx.shape.Text,{setShape:function(_13){
253
this.shape=dojox.gfx.makeParameters(this.shape,_13);
254
this.bbox=null;
255
var r=this.rawNode,s=this.shape;
256
r.text=s.text;
257
r.textDecorations=s.decoration==="underline"?"Underline":"None";
258
r["Canvas.Left"]=-10000;
259
r["Canvas.Top"]=-10000;
260
if(!this._delay){
261
this._delay=window.setTimeout(dojo.hitch(this,"_delayAlignment"),10);
262
}
263
return this;
264
},_delayAlignment:function(){
265
var r=this.rawNode,s=this.shape,w=r.actualWidth,h=r.actualHeight,x=s.x,y=s.y-h*0.75;
266
switch(s.align){
267
case "middle":
268
x-=w/2;
269
break;
270
case "end":
271
x-=w;
272
break;
273
}
274
this._delta={dx:x,dy:y};
275
r["Canvas.Left"]=0;
276
r["Canvas.Top"]=0;
277
this._applyTransform();
278
delete this._delay;
279
},_getAdjustedMatrix:function(){
280
var m=this.matrix,d=this._delta,x;
281
if(m){
282
x=d?[m,d]:m;
283
}else{
284
x=d?d:{};
285
}
286
return new dojox.gfx.Matrix2D(x);
287
},setStroke:function(){
288
return this;
289
},_setFillAttr:function(f){
290
this.rawNode.foreground=f;
291
},setRawNode:function(_14){
292
this.rawNode=_14;
293
},getTextWidth:function(){
294
return this.rawNode.actualWidth;
295
}});
296
dojox.gfx.Text.nodeType="TextBlock";
297
dojo.declare("dojox.gfx.Path",dojox.gfx.path.Path,{_updateWithSegment:function(_15){
298
dojox.gfx.Path.superclass._updateWithSegment.apply(this,arguments);
299
var p=this.shape.path;
300
if(typeof (p)=="string"){
301
this.rawNode.data=p?p:null;
302
}
303
},setShape:function(_16){
304
dojox.gfx.Path.superclass.setShape.apply(this,arguments);
305
var p=this.shape.path;
306
this.rawNode.data=p?p:null;
307
return this;
308
}});
309
dojox.gfx.Path.nodeType="Path";
310
dojo.declare("dojox.gfx.TextPath",dojox.gfx.path.TextPath,{_updateWithSegment:function(_17){
311
},setShape:function(_18){
312
},_setText:function(){
313
}});
314
dojox.gfx.TextPath.nodeType="text";
315
dojox.gfx.silverlight.surfaces={};
316
dojox.gfx.silverlight.nullFunc=function(){
317
};
318
dojo.declare("dojox.gfx.Surface",dojox.gfx.shape.Surface,{constructor:function(){
319
dojox.gfx.silverlight.Container._init.call(this);
320
},destroy:function(){
321
window[this._onLoadName]=dojox.gfx.silverlight.nullFunc;
322
delete dojox.gfx.silverlight.surfaces[this.rawNode.name];
323
this.inherited(arguments);
324
},setDimensions:function(_19,_1a){
325
this.width=dojox.gfx.normalizedLength(_19);
326
this.height=dojox.gfx.normalizedLength(_1a);
327
var p=this.rawNode&&this.rawNode.getHost();
328
if(p){
329
p.width=_19;
330
p.height=_1a;
331
}
332
return this;
333
},getDimensions:function(){
334
var p=this.rawNode&&this.rawNode.getHost();
335
var t=p?{width:p.content.actualWidth,height:p.content.actualHeight}:null;
336
if(t.width<=0){
337
t.width=this.width;
338
}
339
if(t.height<=0){
340
t.height=this.height;
341
}
342
return t;
343
}});
344
dojox.gfx.createSurface=function(_1b,_1c,_1d){
345
if(!_1c&&!_1d){
346
var pos=d.position(_1b);
347
_1c=_1c||pos.w;
348
_1d=_1d||pos.h;
349
}
350
if(typeof _1c=="number"){
351
_1c=_1c+"px";
352
}
353
if(typeof _1d=="number"){
354
_1d=_1d+"px";
355
}
356
var s=new dojox.gfx.Surface();
357
_1b=dojo.byId(_1b);
358
s._parent=_1b;
359
var t=_1b.ownerDocument.createElement("script");
360
t.type="text/xaml";
361
t.id=dojox.gfx._base._getUniqueId();
362
t.text="<?xml version='1.0'?><Canvas xmlns='http://schemas.microsoft.com/client/2007' Name='"+dojox.gfx._base._getUniqueId()+"'/>";
363
_1b.parentNode.insertBefore(t,_1b);
364
s._nodes.push(t);
365
var obj,_1e=dojox.gfx._base._getUniqueId(),_1f="__"+dojox.gfx._base._getUniqueId()+"_onLoad";
366
s._onLoadName=_1f;
367
window[_1f]=function(_20){
368
if(!s.rawNode){
369
s.rawNode=dojo.byId(_1e).content.root;
370
dojox.gfx.silverlight.surfaces[s.rawNode.name]=_1b;
371
s.onLoad(s);
372
}
373
};
374
if(dojo.isSafari){
375
obj="<embed type='application/x-silverlight' id='"+_1e+"' width='"+_1c+"' height='"+_1d+" background='transparent'"+" source='#"+t.id+"'"+" windowless='true'"+" maxFramerate='60'"+" onLoad='"+_1f+"'"+" onError='__dojoSilverlightError'"+" /><iframe style='visibility:hidden;height:0;width:0'/>";
376
}else{
377
obj="<object type='application/x-silverlight' data='data:application/x-silverlight,' id='"+_1e+"' width='"+_1c+"' height='"+_1d+"'>"+"<param name='background' value='transparent' />"+"<param name='source' value='#"+t.id+"' />"+"<param name='windowless' value='true' />"+"<param name='maxFramerate' value='60' />"+"<param name='onLoad' value='"+_1f+"' />"+"<param name='onError' value='__dojoSilverlightError' />"+"</object>";
378
}
379
_1b.innerHTML=obj;
380
var _21=dojo.byId(_1e);
381
if(_21.content&&_21.content.root){
382
s.rawNode=_21.content.root;
383
dojox.gfx.silverlight.surfaces[s.rawNode.name]=_1b;
384
}else{
385
s.rawNode=null;
386
s.isLoaded=false;
387
}
388
s._nodes.push(_21);
389
s.width=dojox.gfx.normalizedLength(_1c);
390
s.height=dojox.gfx.normalizedLength(_1d);
391
return s;
392
};
393
__dojoSilverlightError=function(_22,err){
394
var t="Silverlight Error:\n"+"Code: "+err.ErrorCode+"\n"+"Type: "+err.ErrorType+"\n"+"Message: "+err.ErrorMessage+"\n";
395
switch(err.ErrorType){
396
case "ParserError":
397
t+="XamlFile: "+err.xamlFile+"\n"+"Line: "+err.lineNumber+"\n"+"Position: "+err.charPosition+"\n";
398
break;
399
case "RuntimeError":
400
t+="MethodName: "+err.methodName+"\n";
401
if(err.lineNumber!=0){
402
t+="Line: "+err.lineNumber+"\n"+"Position: "+err.charPosition+"\n";
403
}
404
break;
405
}
406
console.error(t);
407
};
408
dojox.gfx.silverlight.Font={_setFont:function(){
409
var f=this.fontStyle,r=this.rawNode,fw=dojox.gfx.silverlight.fontweight,fo=dojox.gfx.silverlight.fonts,t=f.family.toLowerCase();
410
r.fontStyle=f.style=="italic"?"Italic":"Normal";
411
r.fontWeight=f.weight in fw?fw[f.weight]:f.weight;
412
r.fontSize=dojox.gfx.normalizedLength(f.size);
413
r.fontFamily=t in fo?fo[t]:f.family;
414
if(!this._delay){
415
this._delay=window.setTimeout(dojo.hitch(this,"_delayAlignment"),10);
416
}
417
}};
418
dojox.gfx.silverlight.Container={_init:function(){
419
dojox.gfx.shape.Container._init.call(this);
420
},add:function(_23){
421
if(this!=_23.getParent()){
422
dojox.gfx.shape.Container.add.apply(this,arguments);
423
this.rawNode.children.add(_23.rawNode);
424
}
425
return this;
426
},remove:function(_24,_25){
427
if(this==_24.getParent()){
428
var _26=_24.rawNode.getParent();
429
if(_26){
430
_26.children.remove(_24.rawNode);
431
}
432
dojox.gfx.shape.Container.remove.apply(this,arguments);
433
}
434
return this;
435
},clear:function(){
436
this.rawNode.children.clear();
437
return dojox.gfx.shape.Container.clear.apply(this,arguments);
438
},_moveChildToFront:dojox.gfx.shape.Container._moveChildToFront,_moveChildToBack:dojox.gfx.shape.Container._moveChildToBack};
439
dojo.mixin(dojox.gfx.shape.Creator,{createObject:function(_27,_28){
440
if(!this.rawNode){
441
return null;
442
}
443
var _29=new _27();
444
var _2a=this.rawNode.getHost().content.createFromXaml("<"+_27.nodeType+"/>");
445
_29.setRawNode(_2a);
446
_29.setShape(_28);
447
this.add(_29);
448
return _29;
449
}});
450
dojo.extend(dojox.gfx.Text,dojox.gfx.silverlight.Font);
451
dojo.extend(dojox.gfx.Group,dojox.gfx.silverlight.Container);
452
dojo.extend(dojox.gfx.Group,dojox.gfx.shape.Creator);
453
dojo.extend(dojox.gfx.Surface,dojox.gfx.silverlight.Container);
454
dojo.extend(dojox.gfx.Surface,dojox.gfx.shape.Creator);
455
(function(){
456
var _2b=dojox.gfx.silverlight.surfaces;
457
var _2c=function(s,a){
458
var ev={target:s,currentTarget:s,preventDefault:function(){
459
},stopPropagation:function(){
460
}};
461
if(a){
462
try{
463
ev.ctrlKey=a.ctrl;
464
ev.shiftKey=a.shift;
465
var p=a.getPosition(null);
466
ev.x=ev.offsetX=ev.layerX=p.x;
467
ev.y=ev.offsetY=ev.layerY=p.y;
468
var _2d=_2b[s.getHost().content.root.name];
469
var t=dojo.position(_2d);
470
ev.clientX=t.x+p.x;
471
ev.clientY=t.y+p.y;
472
}
473
catch(e){
474
}
475
}
476
return ev;
477
};
478
var _2e=function(s,a){
479
var ev={keyCode:a.platformKeyCode,ctrlKey:a.ctrl,shiftKey:a.shift};
480
return ev;
481
};
482
var _2f={onclick:{name:"MouseLeftButtonUp",fix:_2c},onmouseenter:{name:"MouseEnter",fix:_2c},onmouseleave:{name:"MouseLeave",fix:_2c},onmouseover:{name:"MouseEnter",fix:_2c},onmouseout:{name:"MouseLeave",fix:_2c},onmousedown:{name:"MouseLeftButtonDown",fix:_2c},onmouseup:{name:"MouseLeftButtonUp",fix:_2c},onmousemove:{name:"MouseMove",fix:_2c},onkeydown:{name:"KeyDown",fix:_2e},onkeyup:{name:"KeyUp",fix:_2e}};
483
var _30={connect:function(_31,_32,_33){
484
var _34,n=_31 in _2f?_2f[_31]:{name:_31,fix:function(){
485
return {};
486
}};
487
if(arguments.length>2){
488
_34=this.getEventSource().addEventListener(n.name,function(s,a){
489
dojo.hitch(_32,_33)(n.fix(s,a));
490
});
491
}else{
492
_34=this.getEventSource().addEventListener(n.name,function(s,a){
493
_32(n.fix(s,a));
494
});
495
}
496
return {name:n.name,token:_34};
497
},disconnect:function(_35){
498
this.getEventSource().removeEventListener(_35.name,_35.token);
499
}};
500
dojo.extend(dojox.gfx.Shape,_30);
501
dojo.extend(dojox.gfx.Surface,_30);
502
dojox.gfx.equalSources=function(a,b){
503
return a&&b&&a.equals(b);
504
};
505
})();
506
}