Commit 28b654f0 authored by Tebjan Halm's avatar Tebjan Halm
Browse files

added more events and fixed IdManager bug

parent 88f9340e
...@@ -61,7 +61,6 @@ namespace Svg ...@@ -61,7 +61,6 @@ namespace Svg
set { this.Attributes["clip-rule"] = value; } set { this.Attributes["clip-rule"] = value; }
} }
/// <summary> /// <summary>
/// Gets the associated <see cref="SvgClipPath"/> if one has been specified. /// Gets the associated <see cref="SvgClipPath"/> if one has been specified.
/// </summary> /// </summary>
...@@ -72,7 +71,6 @@ namespace Svg ...@@ -72,7 +71,6 @@ namespace Svg
set { this.Attributes["filter"] = value; } set { this.Attributes["filter"] = value; }
} }
/// <summary> /// <summary>
/// Gets or sets a value to determine if anti-aliasing should occur when the element is being rendered. /// Gets or sets a value to determine if anti-aliasing should occur when the element is being rendered.
/// </summary> /// </summary>
......
...@@ -26,7 +26,6 @@ namespace Svg ...@@ -26,7 +26,6 @@ namespace Svg
// Do nothing. Children should NOT be rendered. // Do nothing. Children should NOT be rendered.
} }
public override SvgElement DeepCopy() public override SvgElement DeepCopy()
{ {
return DeepCopy<SvgDefinitionList>(); return DeepCopy<SvgDefinitionList>();
......
...@@ -88,7 +88,7 @@ namespace Svg ...@@ -88,7 +88,7 @@ namespace Svg
/// <summary> /// <summary>
/// Gets or sets another gradient fill from which to inherit the stops from. /// Gets or sets another gradient fill from which to inherit the stops from.
/// </summary> /// </summary>
[SvgAttributeAttribute("href")] [SvgAttribute("href")]
public SvgGradientServer InheritGradient public SvgGradientServer InheritGradient
{ {
get { return this._inheritGradient; } get { return this._inheritGradient; }
......
...@@ -87,7 +87,90 @@ namespace Svg ...@@ -87,7 +87,90 @@ namespace Svg
public new object this[string attributeName] public new object this[string attributeName]
{ {
get { return this.GetInheritedAttribute<object>(attributeName); } get { return this.GetInheritedAttribute<object>(attributeName); }
set { base[attributeName] = value; } set
{
if(base.ContainsKey(attributeName))
{
var oldVal = base[attributeName];
base[attributeName] = value;
if(oldVal != value) OnAttributeChanged(attributeName, value);
}
else
{
base[attributeName] = value;
OnAttributeChanged(attributeName, value);
}
}
}
/// <summary>
/// Fired when an Atrribute has changed
/// </summary>
public event EventHandler<AttributeEventArgs> AttributeChanged;
protected void OnAttributeChanged(string attribute, object value)
{
var handler = AttributeChanged;
if(handler != null)
{
handler(this._owner, new AttributeEventArgs { Attribute = attribute, Value = value });
}
}
}
/// <summary>
/// A collection of Custom Attributes
/// </summary>
public sealed class SvgCustomAttributeCollection : Dictionary<string, string>
{
private SvgElement _owner;
/// <summary>
/// Initialises a new instance of a <see cref="SvgAttributeCollection"/> with the given <see cref="SvgElement"/> as the owner.
/// </summary>
/// <param name="owner">The <see cref="SvgElement"/> owner of the collection.</param>
public SvgCustomAttributeCollection(SvgElement owner)
{
this._owner = owner;
}
/// <summary>
/// Gets the attribute with the specified name.
/// </summary>
/// <param name="attributeName">A <see cref="string"/> containing the attribute name.</param>
/// <returns>The attribute value associated with the specified name; If there is no attribute the parent's value will be inherited.</returns>
public new string this[string attributeName]
{
get { return base[attributeName]; }
set
{
if(base.ContainsKey(attributeName))
{
var oldVal = base[attributeName];
base[attributeName] = value;
if(oldVal != value) OnAttributeChanged(attributeName, value);
}
else
{
base[attributeName] = value;
OnAttributeChanged(attributeName, value);
}
}
}
/// <summary>
/// Fired when an Atrribute has changed
/// </summary>
public event EventHandler<AttributeEventArgs> AttributeChanged;
protected void OnAttributeChanged(string attribute, object value)
{
var handler = AttributeChanged;
if(handler != null)
{
handler(this._owner, new AttributeEventArgs { Attribute = attribute, Value = value });
}
} }
} }
} }
\ No newline at end of file
...@@ -45,6 +45,17 @@ namespace Svg ...@@ -45,6 +45,17 @@ namespace Svg
} }
} }
/// <summary>
/// Overwrites the current IdManager with a custom implementation.
/// Be careful with this: If elements have been inserted into the document before,
/// you have to take care that the new IdManager also knows of them.
/// </summary>
/// <param name="manager"></param>
public void OverwriteIdManager(SvgElementIdManager manager)
{
_idManager = manager;
}
/// <summary> /// <summary>
/// Gets or sets the Pixels Per Inch of the rendered image. /// Gets or sets the Pixels Per Inch of the rendered image.
/// </summary> /// </summary>
......
...@@ -41,7 +41,7 @@ namespace Svg ...@@ -41,7 +41,7 @@ namespace Svg
private SvgElementCollection _children; private SvgElementCollection _children;
private static readonly object _loadEventKey = new object(); private static readonly object _loadEventKey = new object();
private Matrix _graphicsMatrix; private Matrix _graphicsMatrix;
private Dictionary<string, string> _customAttributes; private SvgCustomAttributeCollection _customAttributes;
/// <summary> /// <summary>
/// Gets the name of the element. /// Gets the name of the element.
...@@ -122,21 +122,17 @@ namespace Svg ...@@ -122,21 +122,17 @@ namespace Svg
public virtual SvgDocument OwnerDocument public virtual SvgDocument OwnerDocument
{ {
get get
{
if (Parent == null)
{ {
if (this is SvgDocument) if (this is SvgDocument)
{ {
return (SvgDocument)this; return this as SvgDocument;
}
else
{
return null;
}
} }
else else
{ {
if(this.Parent != null)
return Parent.OwnerDocument; return Parent.OwnerDocument;
else
return null;
} }
} }
} }
...@@ -157,7 +153,10 @@ namespace Svg ...@@ -157,7 +153,10 @@ namespace Svg
} }
} }
public Dictionary<string, string> CustomAttributes /// <summary>
/// Gets a collection of custom attributes
/// </summary>
public SvgCustomAttributeCollection CustomAttributes
{ {
get { return this._customAttributes; } get { return this._customAttributes; }
} }
...@@ -301,7 +300,11 @@ namespace Svg ...@@ -301,7 +300,11 @@ namespace Svg
this._children = new SvgElementCollection(this); this._children = new SvgElementCollection(this);
this._eventHandlers = new EventHandlerList(); this._eventHandlers = new EventHandlerList();
this._elementName = string.Empty; this._elementName = string.Empty;
this._customAttributes = new Dictionary<string, string>(); this._customAttributes = new SvgCustomAttributeCollection(this);
//subscribe to attribute events
Attributes.AttributeChanged += Attributes_AttributeChanged;
CustomAttributes.AttributeChanged += Attributes_AttributeChanged;
//find svg attribute descriptions //find svg attribute descriptions
_svgPropertyAttributes = from PropertyDescriptor a in TypeDescriptor.GetProperties(this) _svgPropertyAttributes = from PropertyDescriptor a in TypeDescriptor.GetProperties(this)
...@@ -316,6 +319,12 @@ namespace Svg ...@@ -316,6 +319,12 @@ namespace Svg
} }
//dispatch attribute event
void Attributes_AttributeChanged(object sender, AttributeEventArgs e)
{
OnAttributeChanged(e);
}
public virtual void InitialiseFromXML(XmlTextReader reader, SvgDocument document) public virtual void InitialiseFromXML(XmlTextReader reader, SvgDocument document)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
...@@ -629,6 +638,20 @@ namespace Svg ...@@ -629,6 +638,20 @@ namespace Svg
return newObj; return newObj;
} }
/// <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);
}
}
#region graphical EVENTS #region graphical EVENTS
/* /*
...@@ -663,6 +686,25 @@ namespace Svg ...@@ -663,6 +686,25 @@ namespace Svg
} }
} }
/// <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");
}
}
[SvgAttribute("onclick")] [SvgAttribute("onclick")]
public event EventHandler<MouseArg> Click; public event EventHandler<MouseArg> Click;
...@@ -775,6 +817,15 @@ namespace Svg ...@@ -775,6 +817,15 @@ namespace Svg
} }
/// <summary>
/// Describes the Attribute which was set
/// </summary>
public class AttributeEventArgs : EventArgs
{
public string Attribute;
public object Value;
}
//deriving class registers event actions and calls the actions if the event occurs //deriving class registers event actions and calls the actions if the event occurs
public interface ISvgEventCaller public interface ISvgEventCaller
{ {
...@@ -783,6 +834,7 @@ namespace Svg ...@@ -783,6 +834,7 @@ namespace Svg
void RegisterAction<T1, T2>(string rpcID, Action<T1, T2> 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>(string rpcID, Action<T1, T2, T3> action);
void RegisterAction<T1, T2, T3, T4>(string rpcID, Action<T1, T2, T3, T4> action); void RegisterAction<T1, T2, T3, T4>(string rpcID, Action<T1, T2, T3, T4> action);
void UnregisterAction(string rpcID);
} }
/// <summary> /// <summary>
......
...@@ -78,7 +78,7 @@ namespace Svg ...@@ -78,7 +78,7 @@ namespace Svg
{ {
if (this._owner.OwnerDocument != null) if (this._owner.OwnerDocument != null)
{ {
this._owner.OwnerDocument.IdManager.Add(item); item.ApplyRecursive(this._owner.OwnerDocument.IdManager.Add);
} }
item._parent = this._owner; item._parent = this._owner;
...@@ -132,7 +132,7 @@ namespace Svg ...@@ -132,7 +132,7 @@ namespace Svg
if (this._owner.OwnerDocument != null) if (this._owner.OwnerDocument != null)
{ {
this._owner.OwnerDocument.IdManager.Remove(item); item.ApplyRecursive(this._owner.OwnerDocument.IdManager.Remove);
} }
} }
} }
......
...@@ -52,6 +52,8 @@ namespace Svg ...@@ -52,6 +52,8 @@ namespace Svg
this.EnsureValidId(element.ID); this.EnsureValidId(element.ID);
this._idValueMap.Add(element.ID, element); this._idValueMap.Add(element.ID, element);
} }
OnAdded(element);
} }
/// <summary> /// <summary>
...@@ -64,6 +66,8 @@ namespace Svg ...@@ -64,6 +66,8 @@ namespace Svg
{ {
this._idValueMap.Remove(element.ID); this._idValueMap.Remove(element.ID);
} }
OnRemoved(element);
} }
/// <summary> /// <summary>
...@@ -101,5 +105,32 @@ namespace Svg ...@@ -101,5 +105,32 @@ namespace Svg
this._document = document; this._document = document;
this._idValueMap = new Dictionary<string, SvgElement>(); this._idValueMap = new Dictionary<string, SvgElement>();
} }
public event EventHandler<SvgElementEventArgs> ElementAdded;
public event EventHandler<SvgElementEventArgs> ElementRemoved;
protected void OnAdded(SvgElement element)
{
var handler = ElementAdded;
if(handler != null)
{
handler(this._document, new SvgElementEventArgs{ Element = element });
}
}
protected void OnRemoved(SvgElement element)
{
var handler = ElementRemoved;
if(handler != null)
{
handler(this._document, new SvgElementEventArgs{ Element = element });
}
}
}
public class SvgElementEventArgs : EventArgs
{
public SvgElement Element;
} }
} }
\ No newline at end of file
...@@ -52,5 +52,16 @@ namespace Svg ...@@ -52,5 +52,16 @@ namespace Svg
{ {
return element.CustomAttributes.ContainsKey(name) && !string.IsNullOrEmpty(element.CustomAttributes[name]); return element.CustomAttributes.ContainsKey(name) && !string.IsNullOrEmpty(element.CustomAttributes[name]);
} }
public static void ApplyRecursive(this SvgElement elem, Action<SvgElement> action)
{
action(elem);
foreach (var element in elem.Children)
{
if(!(elem is SvgDocument))
element.ApplyRecursive(action);
}
}
} }
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment