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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
|
//>>built
define("dojox/gfx/silverlight", ["dojo/_base/kernel", "dojo/_base/lang", "dojo/_base/declare", "dojo/_base/Color",
"dojo/_base/array", "dojo/dom-geometry", "dojo/dom", "dojo/_base/sniff",
"./_base", "./shape", "./path"],
function(kernel,lang,declare,color,arr,domGeom,dom,has,g,gs,pathLib){
/*=====
dojox.gfx.silverlight = {
// module:
// dojox/gfx/silverlight
// summary:
// This the graphics rendering bridge for the Microsoft Silverlight plugin.
// Silverlight is a faster implementation on IE6-8 than the default 2d graphics, VML
};
g = dojox.gfx;
pathLib.Path = dojox.gfx.path.Path;
pathLib.TextPath = dojox.gfx.path.TextPath;
sl.Shape = dojox.gfx.canvas.Shape;
gs.Shape = dojox.gfx.shape.Shape;
gs.Rect = dojox.gfx.shape.Rect;
gs.Ellipse = dojox.gfx.shape.Ellipse;
gs.Circle = dojox.gfx.shape.Circle;
gs.Line = dojox.gfx.shape.Line;
gs.PolyLine = dojox.gfx.shape.PolyLine;
gs.Image = dojox.gfx.shape.Image;
gs.Text = dojox.gfx.shape.Text;
gs.Surface = dojox.gfx.shape.Surface;
=====*/
var sl = g.silverlight = {};
kernel.experimental("dojox.gfx.silverlight");
var 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]
},
fontweight = {
normal: 400,
bold: 700
},
caps = {butt: "Flat", round: "Round", square: "Square"},
joins = {bevel: "Bevel", round: "Round"},
fonts = {
serif: "Times New Roman",
times: "Times New Roman",
"sans-serif": "Arial",
helvetica: "Arial",
monotone: "Courier New",
courier: "Courier New"
};
function hexColor(/*String|Array|dojo.Color*/ color){
// summary: converts a color object to a Silverlight hex color string (#aarrggbb)
var c = g.normalizeColor(color),
t = c.toHex(), a = Math.round(c.a * 255);
a = (a < 0 ? 0 : a > 255 ? 255 : a).toString(16);
return "#" + (a.length < 2 ? "0" + a : a) + t.slice(1); // String
}
declare("dojox.gfx.silverlight.Shape", gs.Shape, {
// summary: Silverlight-specific implementation of dojox.gfx.Shape methods
setFill: function(fill){
// summary: sets a fill object (Silverlight)
// fill: Object: a fill object
// (see dojox.gfx.defaultLinearGradient,
// dojox.gfx.defaultRadialGradient,
// dojox.gfx.defaultPattern,
// or dojo.Color)
var p = this.rawNode.getHost().content, r = this.rawNode, f;
if(!fill){
// don't fill
this.fillStyle = null;
this._setFillAttr(null);
return this; // self
}
if(typeof(fill) == "object" && "type" in fill){
// gradient
switch(fill.type){
case "linear":
this.fillStyle = f = g.makeParameters(g.defaultLinearGradient, fill);
var lgb = p.createFromXaml("<LinearGradientBrush/>");
lgb.mappingMode = "Absolute";
lgb.startPoint = f.x1 + "," + f.y1;
lgb.endPoint = f.x2 + "," + f.y2;
arr.forEach(f.colors, function(c){
var t = p.createFromXaml("<GradientStop/>");
t.offset = c.offset;
t.color = hexColor(c.color);
lgb.gradientStops.add(t);
});
this._setFillAttr(lgb);
break;
case "radial":
this.fillStyle = f = g.makeParameters(g.defaultRadialGradient, fill);
var rgb = p.createFromXaml("<RadialGradientBrush/>"),
c = g.matrix.multiplyPoint(g.matrix.invert(this._getAdjustedMatrix()), f.cx, f.cy),
pt = c.x + "," + c.y;
rgb.mappingMode = "Absolute";
rgb.gradientOrigin = pt;
rgb.center = pt;
rgb.radiusX = rgb.radiusY = f.r;
arr.forEach(f.colors, function(c){
var t = p.createFromXaml("<GradientStop/>");
t.offset = c.offset;
t.color = hexColor(c.color);
rgb.gradientStops.add(t);
});
this._setFillAttr(rgb);
break;
case "pattern":
// don't fill: Silverlight doesn't define TileBrush for some reason
this.fillStyle = null;
this._setFillAttr(null);
break;
}
return this; // self
}
// color object
this.fillStyle = f = g.normalizeColor(fill);
var scb = p.createFromXaml("<SolidColorBrush/>");
scb.color = f.toHex();
scb.opacity = f.a;
this._setFillAttr(scb);
return this; // self
},
_setFillAttr: function(f){
this.rawNode.fill = f;
},
setStroke: function(stroke){
// summary: sets a stroke object (Silverlight)
// stroke: Object: a stroke object
// (see dojox.gfx.defaultStroke)
var p = this.rawNode.getHost().content, r = this.rawNode;
if(!stroke){
// don't stroke
this.strokeStyle = null;
r.stroke = null;
return this;
}
// normalize the stroke
if(typeof stroke == "string" || lang.isArray(stroke) || stroke instanceof color){
stroke = {color: stroke};
}
var s = this.strokeStyle = g.makeParameters(g.defaultStroke, stroke);
s.color = g.normalizeColor(s.color);
// generate attributes
if(s){
var scb = p.createFromXaml("<SolidColorBrush/>");
scb.color = s.color.toHex();
scb.opacity = s.color.a;
r.stroke = scb;
r.strokeThickness = s.width;
r.strokeStartLineCap = r.strokeEndLineCap = r.strokeDashCap =
caps[s.cap];
if(typeof s.join == "number"){
r.strokeLineJoin = "Miter";
r.strokeMiterLimit = s.join;
}else{
r.strokeLineJoin = joins[s.join];
}
var da = s.style.toLowerCase();
if(da in dasharray){ da = dasharray[da]; }
if(da instanceof Array){
da = lang.clone(da);
var i;
/*
for(var i = 0; i < da.length; ++i){
da[i] *= s.width;
}
*/
if(s.cap != "butt"){
for(i = 0; i < da.length; i += 2){
//da[i] -= s.width;
--da[i]
if(da[i] < 1){ da[i] = 1; }
}
for(i = 1; i < da.length; i += 2){
//da[i] += s.width;
++da[i];
}
}
r.strokeDashArray = da.join(",");
}else{
r.strokeDashArray = null;
}
}
return this; // self
},
_getParentSurface: function(){
var surface = this.parent;
for(; surface && !(surface instanceof g.Surface); surface = surface.parent);
return surface;
},
_applyTransform: function() {
var tm = this._getAdjustedMatrix(), r = this.rawNode;
if(tm){
var p = this.rawNode.getHost().content,
mt = p.createFromXaml("<MatrixTransform/>"),
mm = p.createFromXaml("<Matrix/>");
mm.m11 = tm.xx;
mm.m21 = tm.xy;
mm.m12 = tm.yx;
mm.m22 = tm.yy;
mm.offsetX = tm.dx;
mm.offsetY = tm.dy;
mt.matrix = mm;
r.renderTransform = mt;
}else{
r.renderTransform = null;
}
return this;
},
setRawNode: function(rawNode){
// summary:
// assigns and clears the underlying node that will represent this
// shape. Once set, transforms, gradients, etc, can be applied.
// (no fill & stroke by default)
rawNode.fill = null;
rawNode.stroke = null;
this.rawNode = rawNode;
this.rawNode.tag = this.getUID();
},
// move family
_moveToFront: function(){
// summary: moves a shape to front of its parent's list of shapes (Silverlight)
var c = this.parent.rawNode.children, r = this.rawNode;
c.remove(r);
c.add(r);
return this; // self
},
_moveToBack: function(){
// summary: moves a shape to back of its parent's list of shapes (Silverlight)
var c = this.parent.rawNode.children, r = this.rawNode;
c.remove(r);
c.insert(0, r);
return this; // self
},
_getAdjustedMatrix: function(){
// summary: returns the adjusted ("real") transformation matrix
return this.matrix; // dojox.gfx.Matrix2D
}
});
declare("dojox.gfx.silverlight.Group", sl.Shape, {
// summary: a group shape (Silverlight), which can be used
// to logically group shapes (e.g, to propagate matricies)
constructor: function(){
gs.Container._init.call(this);
},
setRawNode: function(rawNode){
// summary: sets a raw Silverlight node to be used by this shape
// rawNode: Node: an Silverlight node
this.rawNode = rawNode;
this.rawNode.tag = this.getUID();
}
});
sl.Group.nodeType = "Canvas";
declare("dojox.gfx.silverlight.Rect", [sl.Shape, gs.Rect], {
// summary: a rectangle shape (Silverlight)
setShape: function(newShape){
// summary: sets a rectangle shape object (Silverlight)
// newShape: Object: a rectangle shape object
this.shape = g.makeParameters(this.shape, newShape);
this.bbox = null;
var r = this.rawNode, n = this.shape;
r.width = n.width;
r.height = n.height;
r.radiusX = r.radiusY = n.r;
return this._applyTransform(); // self
},
_getAdjustedMatrix: function(){
// summary: returns the adjusted ("real") transformation matrix
var matrix = this.matrix, s = this.shape, delta = {dx: s.x, dy: s.y};
return new g.Matrix2D(matrix ? [matrix, delta] : delta); // dojox.gfx.Matrix2D
}
});
sl.Rect.nodeType = "Rectangle";
declare("dojox.gfx.silverlight.Ellipse", [sl.Shape, gs.Ellipse], {
// summary: an ellipse shape (Silverlight)
setShape: function(newShape){
// summary: sets an ellipse shape object (Silverlight)
// newShape: Object: an ellipse shape object
this.shape = g.makeParameters(this.shape, newShape);
this.bbox = null;
var r = this.rawNode, n = this.shape;
r.width = 2 * n.rx;
r.height = 2 * n.ry;
return this._applyTransform(); // self
},
_getAdjustedMatrix: function(){
// summary: returns the adjusted ("real") transformation matrix
var matrix = this.matrix, s = this.shape, delta = {dx: s.cx - s.rx, dy: s.cy - s.ry};
return new g.Matrix2D(matrix ? [matrix, delta] : delta); // dojox.gfx.Matrix2D
}
});
sl.Ellipse.nodeType = "Ellipse";
declare("dojox.gfx.silverlight.Circle", [sl.Shape, gs.Circle], {
// summary: a circle shape (Silverlight)
setShape: function(newShape){
// summary: sets a circle shape object (Silverlight)
// newShape: Object: a circle shape object
this.shape = g.makeParameters(this.shape, newShape);
this.bbox = null;
var r = this.rawNode, n = this.shape;
r.width = r.height = 2 * n.r;
return this._applyTransform(); // self
},
_getAdjustedMatrix: function(){
// summary: returns the adjusted ("real") transformation matrix
var matrix = this.matrix, s = this.shape, delta = {dx: s.cx - s.r, dy: s.cy - s.r};
return new g.Matrix2D(matrix ? [matrix, delta] : delta); // dojox.gfx.Matrix2D
}
});
sl.Circle.nodeType = "Ellipse";
declare("dojox.gfx.silverlight.Line", [sl.Shape, gs.Line], {
// summary: a line shape (Silverlight)
setShape: function(newShape){
// summary: sets a line shape object (Silverlight)
// newShape: Object: a line shape object
this.shape = g.makeParameters(this.shape, newShape);
this.bbox = null;
var r = this.rawNode, n = this.shape;
r.x1 = n.x1; r.y1 = n.y1; r.x2 = n.x2; r.y2 = n.y2;
return this; // self
}
});
sl.Line.nodeType = "Line";
declare("dojox.gfx.silverlight.Polyline", [sl.Shape, gs.Polyline], {
// summary: a polyline/polygon shape (Silverlight)
setShape: function(points, closed){
// summary: sets a polyline/polygon shape object (Silverlight)
// points: Object: a polyline/polygon shape object
if(points && points instanceof Array){
// branch
// points: Array: an array of points
this.shape = g.makeParameters(this.shape, {points: points});
if(closed && this.shape.points.length){
this.shape.points.push(this.shape.points[0]);
}
}else{
this.shape = g.makeParameters(this.shape, points);
}
this.bbox = null;
this._normalizePoints();
var p = this.shape.points, rp = [];
for(var i = 0; i < p.length; ++i){
rp.push(p[i].x, p[i].y);
}
this.rawNode.points = rp.join(",");
return this; // self
}
});
sl.Polyline.nodeType = "Polyline";
declare("dojox.gfx.silverlight.Image", [sl.Shape, gs.Image], {
// summary: an image (Silverlight)
setShape: function(newShape){
// summary: sets an image shape object (Silverlight)
// newShape: Object: an image shape object
this.shape = g.makeParameters(this.shape, newShape);
this.bbox = null;
var r = this.rawNode, n = this.shape;
r.width = n.width;
r.height = n.height;
r.source = n.src;
return this._applyTransform(); // self
},
_getAdjustedMatrix: function(){
// summary: returns the adjusted ("real") transformation matrix
var matrix = this.matrix, s = this.shape, delta = {dx: s.x, dy: s.y};
return new g.Matrix2D(matrix ? [matrix, delta] : delta); // dojox.gfx.Matrix2D
},
setRawNode: function(rawNode){
// summary:
// assigns and clears the underlying node that will represent this
// shape. Once set, transforms, gradients, etc, can be applied.
// (no fill & stroke by default)
this.rawNode = rawNode;
this.rawNode.tag = this.getUID();
}
});
sl.Image.nodeType = "Image";
declare("dojox.gfx.silverlight.Text", [sl.Shape, gs.Text], {
// summary: an anchored text (Silverlight)
setShape: function(newShape){
// summary: sets a text shape object (Silverlight)
// newShape: Object: a text shape object
this.shape = g.makeParameters(this.shape, newShape);
this.bbox = null;
var r = this.rawNode, s = this.shape;
r.text = s.text;
r.textDecorations = s.decoration === "underline" ? "Underline" : "None";
r["Canvas.Left"] = -10000;
r["Canvas.Top"] = -10000;
if(!this._delay){
this._delay = window.setTimeout(lang.hitch(this, "_delayAlignment"), 10);
}
return this; // self
},
_delayAlignment: function(){
// handle alignment
var r = this.rawNode, s = this.shape, w, h;
try{
w = r.actualWidth;
h = r.actualHeight;
}catch(e){
// bail out if the node is hidden
return;
}
var x = s.x, y = s.y - h * 0.75;
switch(s.align){
case "middle":
x -= w / 2;
break;
case "end":
x -= w;
break;
}
this._delta = {dx: x, dy: y};
r["Canvas.Left"] = 0;
r["Canvas.Top"] = 0;
this._applyTransform();
delete this._delay;
},
_getAdjustedMatrix: function(){
// summary: returns the adjusted ("real") transformation matrix
var matrix = this.matrix, delta = this._delta, x;
if(matrix){
x = delta ? [matrix, delta] : matrix;
}else{
x = delta ? delta : {};
}
return new g.Matrix2D(x);
},
setStroke: function(){
// summary: ignore setting a stroke style
return this; // self
},
_setFillAttr: function(f){
this.rawNode.foreground = f;
},
setRawNode: function(rawNode){
// summary:
// assigns and clears the underlying node that will represent this
// shape. Once set, transforms, gradients, etc, can be applied.
// (no fill & stroke by default)
this.rawNode = rawNode;
this.rawNode.tag = this.getUID();
},
getTextWidth: function(){
// summary: get the text width in pixels
return this.rawNode.actualWidth;
}
});
sl.Text.nodeType = "TextBlock";
declare("dojox.gfx.silverlight.Path", [sl.Shape, pathLib.Path], {
// summary: a path shape (Silverlight)
_updateWithSegment: function(segment){
// summary: updates the bounding box of path with new segment
// segment: Object: a segment
this.inherited(arguments);
var p = this.shape.path;
if(typeof(p) == "string"){
this.rawNode.data = p ? p : null;
}
},
setShape: function(newShape){
// summary: forms a path using a shape (Silverlight)
// newShape: Object: an SVG path string or a path object (see dojox.gfx.defaultPath)
this.inherited(arguments);
var p = this.shape.path;
this.rawNode.data = p ? p : null;
return this; // self
}
});
sl.Path.nodeType = "Path";
declare("dojox.gfx.silverlight.TextPath", [sl.Shape, pathLib.TextPath], {
// summary: a textpath shape (Silverlight)
_updateWithSegment: function(segment){
// summary: updates the bounding box of path with new segment
// segment: Object: a segment
},
setShape: function(newShape){
// summary: forms a path using a shape (Silverlight)
// newShape: Object: an SVG path string or a path object (see dojox.gfx.defaultPath)
},
_setText: function(){
}
});
sl.TextPath.nodeType = "text";
var surfaces = {}, nullFunc = new Function;
declare("dojox.gfx.silverlight.Surface", gs.Surface, {
// summary: a surface object to be used for drawings (Silverlight)
constructor: function(){
gs.Container._init.call(this);
},
destroy: function(){
window[this._onLoadName] = nullFunc;
delete surfaces[this._nodeName];
this.inherited(arguments);
},
setDimensions: function(width, height){
// summary: sets the width and height of the rawNode
// width: String: width of surface, e.g., "100px"
// height: String: height of surface, e.g., "100px"
this.width = g.normalizedLength(width); // in pixels
this.height = g.normalizedLength(height); // in pixels
var p = this.rawNode && this.rawNode.getHost();
if(p){
p.width = width;
p.height = height;
}
return this; // self
},
getDimensions: function(){
// summary: returns an object with properties "width" and "height"
var p = this.rawNode && this.rawNode.getHost();
var t = p ? {width: p.content.actualWidth, height: p.content.actualHeight} : null;
if(t.width <= 0){ t.width = this.width; }
if(t.height <= 0){ t.height = this.height; }
return t; // Object
}
});
sl.createSurface = function(parentNode, width, height){
// summary: creates a surface (Silverlight)
// parentNode: Node: a parent node
// width: String: width of surface, e.g., "100px"
// height: String: height of surface, e.g., "100px"
if(!width && !height){
var pos = domGeom.position(parentNode);
width = width || pos.w;
height = height || pos.h;
}
if(typeof width == "number"){
width = width + "px";
}
if(typeof height == "number"){
height = height + "px";
}
var s = new sl.Surface();
parentNode = dom.byId(parentNode);
s._parent = parentNode;
s._nodeName = g._base._getUniqueId();
// create an empty canvas
var t = parentNode.ownerDocument.createElement("script");
t.type = "text/xaml";
t.id = g._base._getUniqueId();
t.text = "<?xml version='1.0'?><Canvas xmlns='http://schemas.microsoft.com/client/2007' Name='" +
s._nodeName + "'/>";
parentNode.parentNode.insertBefore(t, parentNode);
s._nodes.push(t);
// build the object
var obj, pluginName = g._base._getUniqueId(),
onLoadName = "__" + g._base._getUniqueId() + "_onLoad";
s._onLoadName = onLoadName;
window[onLoadName] = function(sender){
if(!s.rawNode){
s.rawNode = dom.byId(pluginName).content.root;
// register the plugin with its parent node
surfaces[s._nodeName] = parentNode;
s.onLoad(s);
}
};
if(has("safari")){
obj = "<embed type='application/x-silverlight' id='" +
pluginName + "' width='" + width + "' height='" + height +
" background='transparent'" +
" source='#" + t.id + "'" +
" windowless='true'" +
" maxFramerate='60'" +
" onLoad='" + onLoadName + "'" +
" onError='__dojoSilverlightError'" +
" /><iframe style='visibility:hidden;height:0;width:0'/>";
}else{
obj = "<object type='application/x-silverlight' data='data:application/x-silverlight,' id='" +
pluginName + "' width='" + width + "' height='" + height + "'>" +
"<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='" + onLoadName + "' />" +
"<param name='onError' value='__dojoSilverlightError' />" +
"</object>";
}
parentNode.innerHTML = obj;
var pluginNode = dom.byId(pluginName);
if(pluginNode.content && pluginNode.content.root){
// the plugin was created synchronously
s.rawNode = pluginNode.content.root;
// register the plugin with its parent node
surfaces[s._nodeName] = parentNode;
}else{
// the plugin is being created asynchronously
s.rawNode = null;
s.isLoaded = false;
}
s._nodes.push(pluginNode);
s.width = g.normalizedLength(width); // in pixels
s.height = g.normalizedLength(height); // in pixels
return s; // dojox.gfx.Surface
};
// the function below is meant to be global, it is called from
// the Silverlight's error handler
__dojoSilverlightError = function(sender, err){
var t = "Silverlight Error:\n" +
"Code: " + err.ErrorCode + "\n" +
"Type: " + err.ErrorType + "\n" +
"Message: " + err.ErrorMessage + "\n";
switch(err.ErrorType){
case "ParserError":
t += "XamlFile: " + err.xamlFile + "\n" +
"Line: " + err.lineNumber + "\n" +
"Position: " + err.charPosition + "\n";
break;
case "RuntimeError":
t += "MethodName: " + err.methodName + "\n";
if(err.lineNumber != 0){
t +=
"Line: " + err.lineNumber + "\n" +
"Position: " + err.charPosition + "\n";
}
break;
}
};
// Extenders
var Font = {
_setFont: function(){
// summary: sets a font object (Silverlight)
var f = this.fontStyle, r = this.rawNode, t = f.family.toLowerCase();
r.fontStyle = f.style == "italic" ? "Italic" : "Normal";
r.fontWeight = f.weight in fontweight ? fontweight[f.weight] : f.weight;
r.fontSize = g.normalizedLength(f.size);
r.fontFamily = t in fonts ? fonts[t] : f.family;
// update the transform
if(!this._delay){
this._delay = window.setTimeout(lang.hitch(this, "_delayAlignment"), 10);
}
}
};
var C = gs.Container, Container = {
add: function(shape){
// summary: adds a shape to a group/surface
// shape: dojox.gfx.Shape: a Silverlight shape object
if(this != shape.getParent()){
C.add.apply(this, arguments);
this.rawNode.children.add(shape.rawNode);
}
return this; // self
},
remove: function(shape, silently){
// summary: remove a shape from a group/surface
// shape: dojox.gfx.Shape: a Silverlight shape object
// silently: Boolean?: if true, regenerate a picture
if(this == shape.getParent()){
var parent = shape.rawNode.getParent();
if(parent){
parent.children.remove(shape.rawNode);
}
C.remove.apply(this, arguments);
}
return this; // self
},
clear: function(){
// summary: removes all shapes from a group/surface
this.rawNode.children.clear();
return C.clear.apply(this, arguments);
},
_moveChildToFront: C._moveChildToFront,
_moveChildToBack: C._moveChildToBack
};
var Creator = {
createObject: function(shapeType, rawShape){
// summary: creates an instance of the passed shapeType class
// shapeType: Function: a class constructor to create an instance of
// rawShape: Object: properties to be passed in to the classes "setShape" method
if(!this.rawNode){ return null; }
var shape = new shapeType();
var node = this.rawNode.getHost().content.createFromXaml("<" + shapeType.nodeType + "/>");
shape.setRawNode(node);
shape.setShape(rawShape);
this.add(shape);
return shape; // dojox.gfx.Shape
}
};
lang.extend(sl.Text, Font);
//dojo.extend(sl.TextPath, Font);
lang.extend(sl.Group, Container);
lang.extend(sl.Group, gs.Creator);
lang.extend(sl.Group, Creator);
lang.extend(sl.Surface, Container);
lang.extend(sl.Surface, gs.Creator);
lang.extend(sl.Surface, Creator);
function mouseFix(s, a){
var ev = {target: s, currentTarget: s, preventDefault: function(){}, stopPropagation: function(){}};
try{
if(a.source){
// support silverlight 2.0
ev.target = a.source;
var gfxId = ev.target.tag;
ev.gfxTarget = gs.byId(gfxId);
}
}catch(e){
// a.source does not exist in 1.0
}
if(a){
try{
ev.ctrlKey = a.ctrl;
ev.shiftKey = a.shift;
var p = a.getPosition(null);
ev.x = ev.offsetX = ev.layerX = p.x;
ev.y = ev.offsetY = ev.layerY = p.y;
// calculate clientX and clientY
var parent = surfaces[s.getHost().content.root.name];
var t = domGeom.position(parent);
ev.clientX = t.x + p.x;
ev.clientY = t.y + p.y;
}catch(e){
// squelch bugs in MouseLeave's implementation
}
}
return ev;
}
function keyFix(s, a){
var ev = {
keyCode: a.platformKeyCode,
ctrlKey: a.ctrl,
shiftKey: a.shift
};
try{
if(a.source){
// source is defined from Silverlight 2+
ev.target = a.source;
ev.gfxTarget = gs.byId(ev.target.tag);
}
}catch(e){
// a.source does not exist in 1.0
}
return ev;
}
var eventNames = {
onclick: {name: "MouseLeftButtonUp", fix: mouseFix},
onmouseenter: {name: "MouseEnter", fix: mouseFix},
onmouseleave: {name: "MouseLeave", fix: mouseFix},
onmouseover: {name: "MouseEnter", fix: mouseFix},
onmouseout: {name: "MouseLeave", fix: mouseFix},
onmousedown: {name: "MouseLeftButtonDown", fix: mouseFix},
onmouseup: {name: "MouseLeftButtonUp", fix: mouseFix},
onmousemove: {name: "MouseMove", fix: mouseFix},
onkeydown: {name: "KeyDown", fix: keyFix},
onkeyup: {name: "KeyUp", fix: keyFix}
};
var eventsProcessing = {
connect: function(name, object, method){
var token, n = name in eventNames ? eventNames[name] :
{name: name, fix: function(){ return {}; }};
if(arguments.length > 2){
token = this.getEventSource().addEventListener(n.name,
function(s, a){ lang.hitch(object, method)(n.fix(s, a)); });
}else{
token = this.getEventSource().addEventListener(n.name,
function(s, a){ object(n.fix(s, a)); });
}
return {name: n.name, token: token};
},
disconnect: function(token){
try{
this.getEventSource().removeEventListener(token.name, token.token);
}catch(e){
// bail out if the node is hidden
}
}
};
lang.extend(sl.Shape, eventsProcessing);
lang.extend(sl.Surface, eventsProcessing);
// patch dojox.gfx
g.equalSources = function(a, b){
// summary: compares event sources, returns true if they are equal
return a && b && a.equals(b);
};
return sl;
});
|