SvgElement.cs 29.6 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml;
7
using System.Linq;
davescriven's avatar
davescriven committed
8
using Svg.Transforms;
9
using System.Reflection;
Tebjan Halm's avatar
Tebjan Halm committed
10
11
using System.Threading;
using System.Globalization;
davescriven's avatar
davescriven committed
12
13
14
15
16
17
18
19

namespace Svg
{
    /// <summary>
    /// The base class of which all SVG elements are derived from.
    /// </summary>
    public abstract class SvgElement : ISvgElement, ISvgTransformable, ICloneable
    {
20
        //optimization
21
        protected class PropertyAttributeTuple
22
        {
23
            public PropertyDescriptor Property;
24
25
            public SvgAttributeAttribute Attribute;
        }
26
27
28
29
30
31
32

        protected class EventAttributeTuple
        {
            public FieldInfo Event;
            public SvgAttributeAttribute Attribute;
        }

Tebjan Halm's avatar
Tebjan Halm committed
33
        //reflection cache
34
35
        protected IEnumerable<PropertyAttributeTuple> _svgPropertyAttributes;
        protected IEnumerable<EventAttributeTuple> _svgEventAttributes;
36
37


davescriven's avatar
davescriven committed
38
39
40
41
42
43
44
        internal SvgElement _parent;
        private string _elementName;
        private SvgAttributeCollection _attributes;
        private EventHandlerList _eventHandlers;
        private SvgElementCollection _children;
        private static readonly object _loadEventKey = new object();
        private Matrix _graphicsMatrix;
45
        private SvgCustomAttributeCollection _customAttributes;
davescriven's avatar
davescriven committed
46
47
48
49

        /// <summary>
        /// Gets the name of the element.
        /// </summary>
50
        protected internal string ElementName
davescriven's avatar
davescriven committed
51
        {
52
53
54
55
56
57
58
59
60
61
62
63
64
65
            get
            {
                if (string.IsNullOrEmpty(this._elementName))
                {
                    var attr = TypeDescriptor.GetAttributes(this).OfType<SvgElementAttribute>().SingleOrDefault();

                    if (attr != null)
                    {
                        this._elementName = attr.ElementName;
                    }
                }

                return this._elementName;
            }
66
            internal set { this._elementName = value; }
davescriven's avatar
davescriven committed
67
68
69
70
71
        }

        /// <summary>
        /// Gets or sets the content of the element.
        /// </summary>
Tebjan Halm's avatar
Tebjan Halm committed
72
        private string _content;
davescriven's avatar
davescriven committed
73
74
        public virtual string Content
        {
Tebjan Halm's avatar
Tebjan Halm committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
            get
            {
            	return _content;
            }
            set
            {
            	if(_content != null)
            	{
            		var oldVal = _content;
            		_content = value;
            		if(_content != oldVal)
            			OnAttributeChanged(new AttributeEventArgs{ Attribute = "", Value = value });
            	}
            	else
            	{
            		_content = value;
            		OnAttributeChanged(new AttributeEventArgs{ Attribute = "", Value = value });
            	}
            }
davescriven's avatar
davescriven committed
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
        }

        /// <summary>
        /// Gets an <see cref="EventHandlerList"/> of all events belonging to the element.
        /// </summary>
        protected virtual EventHandlerList Events
        {
            get { return this._eventHandlers; }
        }

        /// <summary>
        /// Occurs when the element is loaded.
        /// </summary>
        public event EventHandler Load
        {
            add { this.Events.AddHandler(_loadEventKey, value); }
            remove { this.Events.RemoveHandler(_loadEventKey, value); }
        }

        /// <summary>
        /// Gets a collection of all child <see cref="SvgElements"/>.
        /// </summary>
        public virtual SvgElementCollection Children
        {
            get { return this._children; }
        }

        /// <summary>
        /// Gets a value to determine whether the element has children.
        /// </summary>
        public virtual bool HasChildren()
        {
            return (this.Children.Count > 0);
        }

        /// <summary>
        /// Gets the parent <see cref="SvgElement"/>.
        /// </summary>
        /// <value>An <see cref="SvgElement"/> if one exists; otherwise null.</value>
        public virtual SvgElement Parent
        {
            get { return this._parent; }
        }

        /// <summary>
        /// Gets the owner <see cref="SvgDocument"/>.
        /// </summary>
        public virtual SvgDocument OwnerDocument
        {
143
144
145
146
147
148
149
150
151
152
153
154
155
156
        	get
        	{
        		if (this is SvgDocument)
        		{
        			return this as SvgDocument;
        		}
        		else
        		{
        			if(this.Parent != null)
        				return Parent.OwnerDocument;
        			else
        				return null;
        		}
        	}
davescriven's avatar
davescriven committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
        }

        /// <summary>
        /// Gets a collection of element attributes.
        /// </summary>
        protected internal virtual SvgAttributeCollection Attributes
        {
            get
            {
                if (this._attributes == null)
                {
                    this._attributes = new SvgAttributeCollection(this);
                }

                return this._attributes;
            }
        }

175
176
177
178
        /// <summary>
        /// Gets a collection of custom attributes
        /// </summary>
        public SvgCustomAttributeCollection CustomAttributes
179
180
181
182
        {
            get { return this._customAttributes; }
        }

183
184
185
186
187
        /// <summary>
        /// Applies the required transforms to <see cref="SvgRenderer"/>.
        /// </summary>
        /// <param name="renderer">The <see cref="SvgRenderer"/> to be transformed.</param>
        protected internal virtual void PushTransforms(SvgRenderer renderer)
davescriven's avatar
davescriven committed
188
        {
189
            _graphicsMatrix = renderer.Transform;
190

davescriven's avatar
davescriven committed
191
192
193
194
195
196
            // Return if there are no transforms
            if (this.Transforms == null || this.Transforms.Count == 0)
            {
                return;
            }

197
            Matrix transformMatrix = renderer.Transform;
davescriven's avatar
davescriven committed
198
199
200

            foreach (SvgTransform transformation in this.Transforms)
            {
201
                transformMatrix.Multiply(transformation.Matrix);
davescriven's avatar
davescriven committed
202
203
            }

204
            renderer.Transform = transformMatrix;
davescriven's avatar
davescriven committed
205
206
        }

207
208
209
210
211
        /// <summary>
        /// Removes any previously applied transforms from the specified <see cref="SvgRenderer"/>.
        /// </summary>
        /// <param name="renderer">The <see cref="SvgRenderer"/> that should have transforms removed.</param>
        protected internal virtual void PopTransforms(SvgRenderer renderer)
davescriven's avatar
davescriven committed
212
        {
213
            renderer.Transform = _graphicsMatrix;
davescriven's avatar
davescriven committed
214
215
216
            _graphicsMatrix = null;
        }

217
218
219
220
221
        /// <summary>
        /// Applies the required transforms to <see cref="SvgRenderer"/>.
        /// </summary>
        /// <param name="renderer">The <see cref="SvgRenderer"/> to be transformed.</param>
        void ISvgTransformable.PushTransforms(SvgRenderer renderer)
davescriven's avatar
davescriven committed
222
        {
223
            this.PushTransforms(renderer);
davescriven's avatar
davescriven committed
224
225
        }

226
227
228
229
230
        /// <summary>
        /// Removes any previously applied transforms from the specified <see cref="SvgRenderer"/>.
        /// </summary>
        /// <param name="renderer">The <see cref="SvgRenderer"/> that should have transforms removed.</param>
        void ISvgTransformable.PopTransforms(SvgRenderer renderer)
davescriven's avatar
davescriven committed
231
        {
232
            this.PopTransforms(renderer);
davescriven's avatar
davescriven committed
233
234
235
236
237
238
239
240
241
        }

        /// <summary>
        /// Gets or sets the element transforms.
        /// </summary>
        /// <value>The transforms.</value>
        [SvgAttribute("transform")]
        public SvgTransformCollection Transforms
        {
Tebjan Halm's avatar
Tebjan Halm committed
242
243
244
245
246
247
248
249
250
            get { return (this.Attributes.GetAttribute<SvgTransformCollection>("Transforms")); }
            set 
            { 
            	var old = this.Transforms;
            	if(old != null)
            		old.TransformChanged -= Attributes_AttributeChanged;
            	value.TransformChanged += Attributes_AttributeChanged;
            	this.Attributes["Transforms"] = value; 
            }
davescriven's avatar
davescriven committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
        }

        /// <summary>
        /// Gets or sets the ID of the element.
        /// </summary>
        /// <exception cref="SvgException">The ID is already used within the <see cref="SvgDocument"/>.</exception>
        [SvgAttribute("id")]
        public string ID
        {
            get { return this.Attributes.GetAttribute<string>("ID"); }
            set
            {
                // Don't do anything if it hasn't changed
                if (string.Compare(this.ID, value) == 0)
265
                {
davescriven's avatar
davescriven committed
266
                    return;
267
                }
davescriven's avatar
davescriven committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282

                if (this.OwnerDocument != null)
                {
                    this.OwnerDocument.IdManager.Remove(this);
                }

                this.Attributes["ID"] = value;

                if (this.OwnerDocument != null)
                {
                    this.OwnerDocument.IdManager.Add(this);
                }
            }
        }

283
284
285
286
287
288
        /// <summary>
        /// Called by the underlying <see cref="SvgElement"/> when an element has been added to the
        /// <see cref="Children"/> collection.
        /// </summary>
        /// <param name="child">The <see cref="SvgElement"/> that has been added.</param>
        /// <param name="index">An <see cref="int"/> representing the index where the element was added to the collection.</param>
289
        protected virtual void AddElement(SvgElement child, int index)
davescriven's avatar
davescriven committed
290
291
292
        {
        }

293
294
295
296
297
        /// <summary>
        /// Calls the <see cref="AddElement"/> method with the specified parameters.
        /// </summary>
        /// <param name="child">The <see cref="SvgElement"/> that has been added.</param>
        /// <param name="index">An <see cref="int"/> representing the index where the element was added to the collection.</param>
davescriven's avatar
davescriven committed
298
299
        internal void OnElementAdded(SvgElement child, int index)
        {
300
            this.AddElement(child, index);
davescriven's avatar
davescriven committed
301
302
        }

303
304
305
306
307
        /// <summary>
        /// Called by the underlying <see cref="SvgElement"/> when an element has been removed from the
        /// <see cref="Children"/> collection.
        /// </summary>
        /// <param name="child">The <see cref="SvgElement"/> that has been removed.</param>
308
        protected virtual void RemoveElement(SvgElement child)
davescriven's avatar
davescriven committed
309
310
311
        {
        }

312
313
314
315
        /// <summary>
        /// Calls the <see cref="RemoveElement"/> method with the specified <see cref="SvgElement"/> as the parameter.
        /// </summary>
        /// <param name="child">The <see cref="SvgElement"/> that has been removed.</param>
davescriven's avatar
davescriven committed
316
317
        internal void OnElementRemoved(SvgElement child)
        {
318
            this.RemoveElement(child);
davescriven's avatar
davescriven committed
319
320
321
322
323
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgElement"/> class.
        /// </summary>
324
        public SvgElement()
davescriven's avatar
davescriven committed
325
326
327
328
        {
            this._children = new SvgElementCollection(this);
            this._eventHandlers = new EventHandlerList();
            this._elementName = string.Empty;
329
330
            this._customAttributes = new SvgCustomAttributeCollection(this);
            
Tebjan Halm's avatar
Tebjan Halm committed
331
332
            Transforms = new SvgTransformCollection();
            
333
334
335
            //subscribe to attribute events
            Attributes.AttributeChanged += Attributes_AttributeChanged;
            CustomAttributes.AttributeChanged += Attributes_AttributeChanged;
336

337
338
339
340
341
            //find svg attribute descriptions
            _svgPropertyAttributes = from PropertyDescriptor a in TypeDescriptor.GetProperties(this)
                            let attribute = a.Attributes[typeof(SvgAttributeAttribute)] as SvgAttributeAttribute
                            where attribute != null
                            select new PropertyAttributeTuple { Property = a, Attribute = attribute };
Tebjan Halm's avatar
Tebjan Halm committed
342

343
            _svgEventAttributes = from EventDescriptor a in TypeDescriptor.GetEvents(this)
344
345
                            let attribute = a.Attributes[typeof(SvgAttributeAttribute)] as SvgAttributeAttribute
                            where attribute != null
346
                            select new EventAttributeTuple { Event = a.ComponentType.GetField(a.Name, BindingFlags.Instance | BindingFlags.NonPublic), Attribute = attribute };
davescriven's avatar
davescriven committed
347

348
        }
349

350
351
352
353
354
355
        //dispatch attribute event
        void Attributes_AttributeChanged(object sender, AttributeEventArgs e)
        {
        	OnAttributeChanged(e);
        }

356
357
		public virtual void InitialiseFromXML(XmlTextReader reader, SvgDocument document)
		{
358
            throw new NotImplementedException();
359
360
361
		}


362
363
364
365
366
        /// <summary>
        /// Renders this element to the <see cref="SvgRenderer"/>.
        /// </summary>
        /// <param name="renderer">The <see cref="SvgRenderer"/> that the element should use to render itself.</param>
        public void RenderElement(SvgRenderer renderer)
davescriven's avatar
davescriven committed
367
        {
368
            this.Render(renderer);
davescriven's avatar
davescriven committed
369
370
371
372
        }

        public void WriteElement(XmlTextWriter writer)
        {
Tebjan Halm's avatar
Tebjan Halm committed
373
374
375
376
            //Save previous culture and switch to invariant for writing
            var previousCulture = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

davescriven's avatar
davescriven committed
377
            this.Write(writer);
Tebjan Halm's avatar
Tebjan Halm committed
378
379
380

            //Switch culture back
            Thread.CurrentThread.CurrentCulture = previousCulture;
davescriven's avatar
davescriven committed
381
382
383
384
385
386
387
        }

        protected virtual void WriteStartElement(XmlTextWriter writer)
        {
            if (this.ElementName != String.Empty)
            {
                writer.WriteStartElement(this.ElementName);
Tebjan Halm's avatar
Tebjan Halm committed
388
389
                if (this.ElementName == "svg")
                {
390
391
392
393
394
395
396
397
398
					foreach (var ns in SvgAttributeAttribute.Namespaces)
					{
						if (string.IsNullOrEmpty(ns.Key))
							writer.WriteAttributeString("xmlns", ns.Value);
						else
							writer.WriteAttributeString("xmlns:" + ns.Key, ns.Value);
					}
					writer.WriteAttributeString("version", "1.1");
				}
davescriven's avatar
davescriven committed
399
400
401
402
403
404
405
406
407
408
409
410
411
412
            }
            this.WriteAttributes(writer);
        }

        protected virtual void WriteEndElement(XmlTextWriter writer)
        {
            if (this.ElementName != String.Empty)
            {
                writer.WriteEndElement();
            }
        }

        protected virtual void WriteAttributes(XmlTextWriter writer)
        {
413
414
            //properties
            foreach (var attr in _svgPropertyAttributes)
415
416
417
418
419
            {
                if (attr.Property.Converter.CanConvertTo(typeof(string)))
                {
                    object propertyValue = attr.Property.GetValue(this);

420
421
422
                    var forceWrite = false;
                    if ((attr.Attribute.Name == "fill") && (Parent != null))
                    {
423
424
                    	if(propertyValue == SvgColourServer.NotSet) continue;
                    	
425
426
                        object parentValue;
                        if (TryResolveParentAttributeValue(attr.Attribute.Name, out parentValue))
427
                        {
428
429
                            if ((parentValue == propertyValue) 
                                || ((parentValue != null) &&  parentValue.Equals(propertyValue)))
430
                                continue;
431
                            
432
433
434
435
                            forceWrite = true;
                        }
                    }

436
437
                    if (propertyValue != null)
                    {
Tebjan Halm's avatar
Tebjan Halm committed
438
                        var type = propertyValue.GetType();
439
                        string value = (string)attr.Property.Converter.ConvertTo(propertyValue, typeof(string));
Tebjan Halm's avatar
Tebjan Halm committed
440

441
                        if (!SvgDefaults.IsDefault(attr.Attribute.Name, value) || forceWrite)
Tebjan Halm's avatar
Tebjan Halm committed
442
                        {
443
                            writer.WriteAttributeString(attr.Attribute.NamespaceAndName, value);
Tebjan Halm's avatar
Tebjan Halm committed
444
445
446
447
                        }
                    }
                    else if(attr.Attribute.Name == "fill") //if fill equals null, write 'none'
                    {
448
449
                        string value = (string)attr.Property.Converter.ConvertTo(propertyValue, typeof(string));
                        writer.WriteAttributeString(attr.Attribute.NamespaceAndName, value);
450
451
452
                    }
                }
            }
453

Tebjan Halm's avatar
Tebjan Halm committed
454
            
455
            //events
Tebjan Halm's avatar
Tebjan Halm committed
456
            if(AutoPublishEvents)
457
            {
458
459
460
461
462
463
464
465
466
	            foreach (var attr in _svgEventAttributes)
	            {
	                var evt = attr.Event.GetValue(this);
	                
	                if (evt != null && !string.IsNullOrWhiteSpace(this.ID))
	                {
	                    writer.WriteAttributeString(attr.Attribute.Name, this.ID + "/" + attr.Attribute.Name);
	                }
	            }
467
468
            }

469
470
471
472
473
            //add the custom attributes
            foreach (var item in this._customAttributes)
            {
                writer.WriteAttributeString(item.Key, item.Value);
            }
474
        }
Tebjan Halm's avatar
Tebjan Halm committed
475
476
        
        public bool AutoPublishEvents = true;
477

478
        private bool TryResolveParentAttributeValue(string attributeKey, out object parentAttributeValue)
479
        {
480
            parentAttributeValue = null;
481

482
            attributeKey = char.ToUpper(attributeKey[0]) + attributeKey.Substring(1);
483
484

            var currentParent = Parent;
485
            var resolved = false;
486
487
488
489
            while (currentParent != null)
            {
                if (currentParent.Attributes.ContainsKey(attributeKey))
                {
490
491
492
                    resolved = true;
                    parentAttributeValue = currentParent.Attributes[attributeKey];
                    if (parentAttributeValue != null)
493
494
495
496
                        break;
                }
                currentParent = currentParent.Parent;
            }
497

498
            return resolved;
davescriven's avatar
davescriven committed
499
500
501
502
503
504
505
506
507
508
509
510
511
512
        }

        protected virtual void Write(XmlTextWriter writer)
        {
            if (this.ElementName != String.Empty)
            {
                this.WriteStartElement(writer);
                this.WriteChildren(writer);
                this.WriteEndElement(writer);
            }
        }

        protected virtual void WriteChildren(XmlTextWriter writer)
        {
Tebjan Halm's avatar
Tebjan Halm committed
513
514
515
516
517
            //write the content
            if(!String.IsNullOrEmpty(this.Content))
                writer.WriteString(this.Content);

            //write all children
davescriven's avatar
davescriven committed
518
519
520
521
522
523
524
            foreach (SvgElement child in this.Children)
            {
                child.Write(writer);
            }
        }

        /// <summary>
525
        /// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="SvgRenderer"/> object.
davescriven's avatar
davescriven committed
526
        /// </summary>
527
528
        /// <param name="renderer">The <see cref="SvgRenderer"/> object to render to.</param>
        protected virtual void Render(SvgRenderer renderer)
davescriven's avatar
davescriven committed
529
        {
530
531
532
            this.PushTransforms(renderer);
            this.RenderChildren(renderer);
            this.PopTransforms(renderer);
davescriven's avatar
davescriven committed
533
534
        }

535
536
537
538
539
        /// <summary>
        /// Renders the children of this <see cref="SvgElement"/>.
        /// </summary>
        /// <param name="renderer">The <see cref="SvgRenderer"/> to render the child <see cref="SvgElement"/>s to.</param>
        protected virtual void RenderChildren(SvgRenderer renderer)
davescriven's avatar
davescriven committed
540
541
542
        {
            foreach (SvgElement element in this.Children)
            {
543
                element.Render(renderer);
davescriven's avatar
davescriven committed
544
545
546
            }
        }

547
548
549
550
551
        /// <summary>
        /// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="SvgRenderer"/> object.
        /// </summary>
        /// <param name="renderer">The <see cref="SvgRenderer"/> object to render to.</param>
        void ISvgElement.Render(SvgRenderer renderer)
davescriven's avatar
davescriven committed
552
        {
553
            this.Render(renderer);
davescriven's avatar
davescriven committed
554
        }
Tebjan Halm's avatar
Tebjan Halm committed
555
556
557
558
559
560
561
562
        
        /// <summary>
        /// Recursive method to add up the paths of all children
        /// </summary>
        /// <param name="elem"></param>
        /// <param name="path"></param>
        protected void AddPaths(SvgElement elem, GraphicsPath path)
        {
Tebjan Halm's avatar
Tebjan Halm committed
563
        	foreach(var child in elem.Children)
Tebjan Halm's avatar
Tebjan Halm committed
564
        	{
Tebjan Halm's avatar
Tebjan Halm committed
565
        		if (child is SvgVisualElement)
Tebjan Halm's avatar
Tebjan Halm committed
566
        		{
Tebjan Halm's avatar
Tebjan Halm committed
567
        			if(!(child is SvgGroup))
568
        			{
Tebjan Halm's avatar
Tebjan Halm committed
569
        				var childPath = ((SvgVisualElement)child).Path;
570
        				
Tebjan Halm's avatar
Tebjan Halm committed
571
572
573
574
575
576
577
578
        				if (childPath != null)
        				{
        					childPath = (GraphicsPath)childPath.Clone();
        					if(child.Transforms != null)
        						childPath.Transform(child.Transforms.GetMatrix());
        					
        					path.AddPath(childPath, false);
        				}
579
        			}
Tebjan Halm's avatar
Tebjan Halm committed
580
        		}
Tebjan Halm's avatar
Tebjan Halm committed
581
        			
Tebjan Halm's avatar
Tebjan Halm committed
582
        		AddPaths(child, path);
Tebjan Halm's avatar
Tebjan Halm committed
583
        	}
Tebjan Halm's avatar
Tebjan Halm committed
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
        }
        
        /// <summary>
        /// Recursive method to add up the paths of all children
        /// </summary>
        /// <param name="elem"></param>
        /// <param name="path"></param>
        protected GraphicsPath GetPaths(SvgElement elem)
        {
        	var ret = new GraphicsPath();
        	
        	foreach(var child in elem.Children)
        	{
        		if (child is SvgVisualElement)
        		{
        			if(!(child is SvgGroup))
        			{
        				var childPath = ((SvgVisualElement)child).Path;
        				
        				if (childPath != null)
        				{
        					childPath = (GraphicsPath)childPath.Clone();
        					if(child.Transforms != null)
        						childPath.Transform(child.Transforms.GetMatrix());
        					
        					ret.AddPath(childPath, false);
        				}
        			}
        			else
        			{
        				var childPath = GetPaths(child);
        				if(child.Transforms != null)
        					childPath.Transform(child.Transforms.GetMatrix());
        			}
        		}
        			
        	}
Tebjan Halm's avatar
Tebjan Halm committed
621
        	
Tebjan Halm's avatar
Tebjan Halm committed
622
        	return ret;
Tebjan Halm's avatar
Tebjan Halm committed
623
        }
davescriven's avatar
davescriven committed
624

625
626
627
628
629
630
        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
davescriven's avatar
davescriven committed
631
632
633
634
        public virtual object Clone()
        {
            return this.MemberwiseClone();
        }
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656

    	public abstract SvgElement DeepCopy();

		public virtual SvgElement DeepCopy<T>() where T : SvgElement, new()
		{
			var newObj = new T();
			newObj.Content = this.Content;
			newObj.ElementName = this.ElementName;
//			if (this.Parent != null)
	//			this.Parent.Children.Add(newObj);

			if (this.Transforms != null)
			{
				newObj.Transforms = new SvgTransformCollection();
				foreach (var transform in this.Transforms)
					newObj.Transforms.Add(transform.Clone() as SvgTransform);
			}

			foreach (var child in this.Children)
			{
				newObj.Children.Add(child.DeepCopy());
			}
657
658
659
660
661
662
663
664
			
			if(this._customAttributes.Count > 0)
			{
				foreach (var element in _customAttributes) 
				{
					newObj.CustomAttributes.Add(element.Key, element.Value);
				}
			}
665
666
				
			return newObj;
Tebjan Halm's avatar
Tebjan Halm committed
667
        }
668
669
670
671
672
673
674
675
676
677
678
679
680
681
		
		/// <summary>
        /// Fired when an Atrribute of this Element has changed
        /// </summary>
		public event EventHandler<AttributeEventArgs> AttributeChanged;
		
		protected void OnAttributeChanged(AttributeEventArgs args)
		{
			var handler = AttributeChanged;
			if(handler != null)
			{
				handler(this, args);
			}
		}
Tebjan Halm's avatar
Tebjan Halm committed
682
683
684
685
686
687
688

        #region graphical EVENTS

        /*  
            onfocusin = "<anything>"
            onfocusout = "<anything>"
            onactivate = "<anything>"
689
690
691
692
693
                onclick = "<anything>"
                onmousedown = "<anything>"
                onmouseup = "<anything>"
                onmouseover = "<anything>"
                onmousemove = "<anything>"
694
            	onmouseout = "<anything>" 
Tebjan Halm's avatar
Tebjan Halm committed
695
696
         */

697
698
699
700
701
702
703
704
705
706
707
        /// <summary>
        /// Use this method to provide your implementation ISvgEventCaller which can register Actions 
        /// and call them if one of the events occurs. Make sure, that your SvgElement has a unique ID.
        /// </summary>
        /// <param name="caller"></param>
        public void RegisterEvents(ISvgEventCaller caller)
        {
            if (caller != null && !string.IsNullOrWhiteSpace(this.ID))
            {
                var rpcID = this.ID + "/";

Tebjan Halm's avatar
Tebjan Halm committed
708
709
                caller.RegisterAction<float, float, int, int>(rpcID + "onclick", OnClick);
                caller.RegisterAction<float, float, int, int>(rpcID + "onmousedown", OnMouseDown);
710
                caller.RegisterAction<float, float, int>(rpcID + "onmouseup", OnMouseUp);
711
                caller.RegisterAction<float, float>(rpcID + "onmousemove", OnMouseMove);
712
713
                caller.RegisterAction(rpcID + "onmouseover", OnMouseOver);
                caller.RegisterAction(rpcID + "onmouseout", OnMouseOut);
714
715
            }
        }
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
        
        /// <summary>
        /// Use this method to provide your implementation ISvgEventCaller to unregister Actions
        /// </summary>
        /// <param name="caller"></param>
        public void UnregisterEvents(ISvgEventCaller caller)
        {
        	if (caller != null && !string.IsNullOrWhiteSpace(this.ID))
        	{
        		var rpcID = this.ID + "/";

        		caller.UnregisterAction(rpcID + "onclick");
        		caller.UnregisterAction(rpcID + "onmousedown");
        		caller.UnregisterAction(rpcID + "onmouseup");
        		caller.UnregisterAction(rpcID + "onmousemove");
        		caller.UnregisterAction(rpcID + "onmouseover");
        		caller.UnregisterAction(rpcID + "onmouseout");
        	}
        }
735

Tebjan Halm's avatar
Tebjan Halm committed
736
737
738
        [SvgAttribute("onclick")]
        public event EventHandler<MouseArg> Click;

739
740
741
742
743
        [SvgAttribute("onmousedown")]
        public event EventHandler<MouseArg> MouseDown;

        [SvgAttribute("onmouseup")]
        public event EventHandler<MouseArg> MouseUp;
744
745
746
        
        [SvgAttribute("onmousemove")]
        public event EventHandler<PointArg> MouseMove;
747
748
749
750
751
752
753

        [SvgAttribute("onmouseover")]
        public event EventHandler MouseOver;

        [SvgAttribute("onmouseout")]
        public event EventHandler MouseOut;

754
        //click
755
        protected void OnClick(float x, float y, int button, int clickCount)
Tebjan Halm's avatar
Tebjan Halm committed
756
        {
757
758
759
760
761
762
763
764
765
        	RaiseMouseClick(this, new MouseArg { x = x, y = y, Button = button, ClickCount = clickCount});
        }
        
        protected void RaiseMouseClick(object sender, MouseArg e)
        {
        	var handler = Click;
        	if (handler != null)
        	{
        		handler(sender, e);
766
767
768
            }
        }

769
        //down
770
        protected void OnMouseDown(float x, float y, int button, int clickCount)
771
        {
772
        	RaiseMouseDown(this, new MouseArg { x = x, y = y, Button = button, ClickCount = clickCount});
Tebjan Halm's avatar
Tebjan Halm committed
773
774
775
776
777
        }
        
        protected void RaiseMouseDown(object sender, MouseArg e)
        {
        	var handler = MouseDown;
778
779
            if (handler != null)
            {
Tebjan Halm's avatar
Tebjan Halm committed
780
                handler(sender, e);
781
782
783
            }
        }

784
        //up
785
786
        protected void OnMouseUp(float x, float y, int button)
        {
787
788
789
790
791
792
        	RaiseMouseUp(this, new MouseArg { x = x, y = y, Button = button});
        }
        
        protected void RaiseMouseUp(object sender, MouseArg e)
        {
        	var handler = MouseUp;
793
794
            if (handler != null)
            {
795
                handler(sender, e);
796
797
            }
        }
798
799
800
        
        //move
        protected void OnMouseMove(float x, float y)
801
        {
802
803
804
805
806
807
        	RaiseMouseMove(this, new PointArg { x = x, y = y});
        }
        
        protected void RaiseMouseMove(object sender, PointArg e)
        {
        	var handler = MouseMove;
808
809
            if (handler != null)
            {
810
                handler(sender, e);
811
812
813
            }
        }

814
815
		//over        
        protected void OnMouseOver()
816
        {
817
818
819
820
821
822
        	RaiseMouseOver(this);
        }
        
        protected void RaiseMouseOver(object sender)
        {
        	var handler = MouseOver;
823
824
            if (handler != null)
            {
825
                handler(sender, new EventArgs());
Tebjan Halm's avatar
Tebjan Halm committed
826
827
828
            }
        }

829
830
831
832
833
834
835
        //out
        protected void OnMouseOut()
        {
        	RaiseMouseOut(this);
        }
        
        protected void RaiseMouseOut(object sender)
836
        {
837
        	var handler = MouseOut;
838
839
            if (handler != null)
            {
840
                handler(sender, new EventArgs());
841
842
            }
        }
Tebjan Halm's avatar
Tebjan Halm committed
843
844
845
846

        #endregion graphical EVENTS

    }
847
848
849
850
851
852
853
854
855
    
    /// <summary>
    /// Describes the Attribute which was set
    /// </summary>
    public class AttributeEventArgs : EventArgs
    {
    	public string Attribute;
    	public object Value;
    }
Tebjan Halm's avatar
Tebjan Halm committed
856

857
858
859
    //deriving class registers event actions and calls the actions if the event occurs
    public interface ISvgEventCaller
    {
860
        void RegisterAction(string rpcID, Action action);
861
862
863
864
        void RegisterAction<T1>(string rpcID, Action<T1> action);
        void RegisterAction<T1, T2>(string rpcID, Action<T1, T2> action);
        void RegisterAction<T1, T2, T3>(string rpcID, Action<T1, T2, T3> action);
        void RegisterAction<T1, T2, T3, T4>(string rpcID, Action<T1, T2, T3, T4> action);
865
        void UnregisterAction(string rpcID);
866
867
    }

Tebjan Halm's avatar
Tebjan Halm committed
868
869
870
871
872
873
874
875
876
    /// <summary>
    /// Represents the state of the mouse at the moment the event occured.
    /// </summary>
    public class MouseArg : EventArgs
    {
        public float x;
        public float y;

        /// <summary>
877
        /// 1 = left, 2 = middle, 3 = right
Tebjan Halm's avatar
Tebjan Halm committed
878
        /// </summary>
879
        public int Button;
880
881
        
        public int ClickCount = -1;
davescriven's avatar
davescriven committed
882
883
    }

884
885
886
887
888
889
890
891
892
    /// <summary>
    /// Represents the mouse position at the moment the event occured.
    /// </summary>
    public class PointArg : EventArgs
    {
        public float x;
        public float y;
    }

davescriven's avatar
davescriven committed
893
894
    internal interface ISvgElement
    {
895
896
897
		SvgElement Parent {get;}
		SvgElementCollection Children { get; }

898
        void Render(SvgRenderer renderer);
davescriven's avatar
davescriven committed
899
900
    }
}