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
set { this.Attributes["clip-rule"] = value; }
}
/// <summary>
/// Gets the associated <see cref="SvgClipPath"/> if one has been specified.
/// </summary>
......@@ -72,7 +71,6 @@ namespace Svg
set { this.Attributes["filter"] = value; }
}
/// <summary>
/// Gets or sets a value to determine if anti-aliasing should occur when the element is being rendered.
/// </summary>
......
......@@ -26,7 +26,6 @@ namespace Svg
// Do nothing. Children should NOT be rendered.
}
public override SvgElement DeepCopy()
{
return DeepCopy<SvgDefinitionList>();
......
......@@ -88,7 +88,7 @@ namespace Svg
/// <summary>
/// Gets or sets another gradient fill from which to inherit the stops from.
/// </summary>
[SvgAttributeAttribute("href")]
[SvgAttribute("href")]
public SvgGradientServer InheritGradient
{
get { return this._inheritGradient; }
......
......@@ -87,7 +87,90 @@ namespace Svg
public new object this[string 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
}
}
/// <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>
/// Gets or sets the Pixels Per Inch of the rendered image.
/// </summary>
......
......@@ -41,7 +41,7 @@ namespace Svg
private SvgElementCollection _children;
private static readonly object _loadEventKey = new object();
private Matrix _graphicsMatrix;
private Dictionary<string, string> _customAttributes;
private SvgCustomAttributeCollection _customAttributes;
/// <summary>
/// Gets the name of the element.
......@@ -122,21 +122,17 @@ namespace Svg
public virtual SvgDocument OwnerDocument
{
get
{
if (Parent == null)
{
if (this is SvgDocument)
{
return (SvgDocument)this;
}
else
{
return null;
}
return this as SvgDocument;
}
else
{
if(this.Parent != null)
return Parent.OwnerDocument;
else
return null;
}
}
}
......@@ -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; }
}
......@@ -301,7 +300,11 @@ namespace Svg
this._children = new SvgElementCollection(this);
this._eventHandlers = new EventHandlerList();
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
_svgPropertyAttributes = from PropertyDescriptor a in TypeDescriptor.GetProperties(this)
......@@ -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)
{
throw new NotImplementedException();
......@@ -629,6 +638,20 @@ namespace Svg
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
/*
......@@ -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")]
public event EventHandler<MouseArg> Click;
......@@ -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
public interface ISvgEventCaller
{
......@@ -783,6 +834,7 @@ namespace Svg
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);
void UnregisterAction(string rpcID);
}
/// <summary>
......
......@@ -78,7 +78,7 @@ namespace Svg
{
if (this._owner.OwnerDocument != null)
{
this._owner.OwnerDocument.IdManager.Add(item);
item.ApplyRecursive(this._owner.OwnerDocument.IdManager.Add);
}
item._parent = this._owner;
......@@ -132,7 +132,7 @@ namespace Svg
if (this._owner.OwnerDocument != null)
{
this._owner.OwnerDocument.IdManager.Remove(item);
item.ApplyRecursive(this._owner.OwnerDocument.IdManager.Remove);
}
}
}
......
......@@ -52,6 +52,8 @@ namespace Svg
this.EnsureValidId(element.ID);
this._idValueMap.Add(element.ID, element);
}
OnAdded(element);
}
/// <summary>
......@@ -64,6 +66,8 @@ namespace Svg
{
this._idValueMap.Remove(element.ID);
}
OnRemoved(element);
}
/// <summary>
......@@ -101,5 +105,32 @@ namespace Svg
this._document = document;
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
{
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