diff options
author | Iain McCoy <iainmc@mono-cvs.ximian.com> | 2005-08-31 16:27:49 +0400 |
---|---|---|
committer | Iain McCoy <iainmc@mono-cvs.ximian.com> | 2005-08-31 16:27:49 +0400 |
commit | 2263d0bfa06277fda50d8b95620f1331fdd9a660 (patch) | |
tree | 6d1f5ce676b8132e3daae35219ff52b3849caef9 /mcs/class/PresentationFramework | |
parent | 873947b3d7a69ed42a80458fb972b52237bda1d4 (diff) |
2005-08-31 Iain McCoy <iain@mccoy.id.au>
* System.Windows.Serialization/Parser.cs,
System.Windows.Serialization/data-classes.txt,
Mono.Windows.Serialization/ParserToCode.cs,
Mono.Windows.Serialization/ParserConsumerBase.cs
Mono.Windows.Serialization/XamlParser.cs: initial support for
x:Key and StaticResource - these exist mainly to support the styling
system that will eventually live in System.Windows
svn path=/trunk/mcs/; revision=49171
Diffstat (limited to 'mcs/class/PresentationFramework')
6 files changed, 142 insertions, 58 deletions
diff --git a/mcs/class/PresentationFramework/ChangeLog b/mcs/class/PresentationFramework/ChangeLog index 4cbed6a6d61..23e4762dfca 100644 --- a/mcs/class/PresentationFramework/ChangeLog +++ b/mcs/class/PresentationFramework/ChangeLog @@ -1,3 +1,13 @@ +2005-08-31 Iain McCoy <iain@mccoy.id.au> + + * System.Windows.Serialization/Parser.cs, + System.Windows.Serialization/data-classes.txt, + Mono.Windows.Serialization/ParserToCode.cs, + Mono.Windows.Serialization/ParserConsumerBase.cs + Mono.Windows.Serialization/XamlParser.cs: initial support for + x:Key and StaticResource - these exist mainly to support the styling + system that will eventually live in System.Windows + 2005-08-28 Iain McCoy <iain@mccoy.id.au> * Test/Parser.cs, Test/ParserToCode.cs: added tests for objects as diff --git a/mcs/class/PresentationFramework/Mono.Windows.Serialization/ParserConsumerBase.cs b/mcs/class/PresentationFramework/Mono.Windows.Serialization/ParserConsumerBase.cs index cc334af1982..c14afb2c82e 100644 --- a/mcs/class/PresentationFramework/Mono.Windows.Serialization/ParserConsumerBase.cs +++ b/mcs/class/PresentationFramework/Mono.Windows.Serialization/ParserConsumerBase.cs @@ -24,14 +24,17 @@ namespace Mono.Windows.Serialization { CreateTopLevel(((XamlElementStartNode)n).ElementType, ((XamlElementStartNode)n).name); } else if (n is XamlElementStartNode && ((XamlElementStartNode)n).propertyObject) { Debug.WriteLine(this.GetType() + ": element begins as property value"); - CreatePropertyObject(((XamlElementStartNode)n).ElementType, ((XamlElementStartNode)n).name); + string key = getKeyFromNode(n); + CreatePropertyObject(((XamlElementStartNode)n).ElementType, ((XamlElementStartNode)n).name, key); } else if (n is XamlElementStartNode && ((XamlElementStartNode)n).depPropertyObject) { Debug.WriteLine(this.GetType() + ": element begins as dependency property value"); - CreateDependencyPropertyObject(((XamlElementStartNode)n).ElementType, ((XamlElementStartNode)n).name); + string key = getKeyFromNode(n); + CreateDependencyPropertyObject(((XamlElementStartNode)n).ElementType, ((XamlElementStartNode)n).name, key); } else if (n is XamlElementStartNode) { Debug.WriteLine(this.GetType() + ": element begins"); - CreateObject(((XamlElementStartNode)n).ElementType, ((XamlElementStartNode)n).name); + string key = getKeyFromNode(n); + CreateObject(((XamlElementStartNode)n).ElementType, ((XamlElementStartNode)n).name, key); } else if (n is XamlPropertyNode && ((XamlPropertyNode)n).PropInfo != null) { Debug.WriteLine(this.GetType() + ": normal property begins"); CreateProperty(((XamlPropertyNode)n).PropInfo); @@ -59,11 +62,17 @@ namespace Mono.Windows.Serialization { CreateObjectText(((XamlTextNode)n).TextContent); } else if (n is XamlTextNode && ((XamlTextNode)n).mode == XamlParseMode.Property){ Debug.WriteLine(this.GetType() + ": text for property"); - CreatePropertyText(((XamlTextNode)n).TextContent, ((XamlTextNode)n).finalType); + if (((XamlTextNode)n).keyText != null) + CreatePropertyReference(((XamlTextNode)n).keyText); + else + CreatePropertyText(((XamlTextNode)n).TextContent, ((XamlTextNode)n).finalType); EndProperty(); } else if (n is XamlTextNode && ((XamlTextNode)n).mode == XamlParseMode.DependencyProperty){ Debug.WriteLine(this.GetType() + ": text for dependency property"); - CreateDependencyPropertyText(((XamlTextNode)n).TextContent, ((XamlTextNode)n).finalType); + if (((XamlTextNode)n).keyText != null) + CreateDependencyPropertyReference(((XamlTextNode)n).keyText); + else + CreateDependencyPropertyText(((XamlTextNode)n).TextContent, ((XamlTextNode)n).finalType); EndDependencyProperty(); } else if (n is XamlPropertyComplexEndNode) { Debug.WriteLine(this.GetType() + ": end complex property"); @@ -100,9 +109,18 @@ namespace Mono.Windows.Serialization { } + private string getKeyFromNode(XamlNode n) + { + // we know that n is a XamlElementStartNode, but don't need that knowledge + if (n is XamlKeyElementStartNode) + return ((XamlKeyElementStartNode)n).key; + else + return null; + } + public abstract void CreateTopLevel(Type parent, string className); - public abstract void CreateObject(Type type, string varName); + public abstract void CreateObject(Type type, string varName, string key); public abstract void CreateProperty(PropertyInfo property); public abstract void CreateEvent(EventInfo evt); public abstract void CreateDependencyProperty(Type attachedTo, string propertyName, Type propertyType); @@ -111,9 +129,11 @@ namespace Mono.Windows.Serialization { public abstract void CreateEventDelegate(string functionName, Type eventDelegateType); public abstract void CreatePropertyDelegate(string functionName, Type propertyType); public abstract void CreatePropertyText(string text, Type propertyType); + public abstract void CreatePropertyReference(string key); public abstract void CreateDependencyPropertyText(string text, Type propertyType); - public abstract void CreateDependencyPropertyObject(Type type, string varName); - public abstract void CreatePropertyObject(Type type, string varName); + public abstract void CreateDependencyPropertyObject(Type type, string varName, string key); + public abstract void CreateDependencyPropertyReference(string key); + public abstract void CreatePropertyObject(Type type, string varName, string key); public abstract void EndDependencyPropertyObject(Type destType); public abstract void EndPropertyObject(Type destType); public abstract void EndObject(); diff --git a/mcs/class/PresentationFramework/Mono.Windows.Serialization/ParserToCode.cs b/mcs/class/PresentationFramework/Mono.Windows.Serialization/ParserToCode.cs index 95818a309ad..cf663e9002b 100644 --- a/mcs/class/PresentationFramework/Mono.Windows.Serialization/ParserToCode.cs +++ b/mcs/class/PresentationFramework/Mono.Windows.Serialization/ParserToCode.cs @@ -64,6 +64,8 @@ namespace Mono.Windows.Serialization { ArrayList objects = new ArrayList(); Hashtable nameClashes = new Hashtable(); int tempIndex = 0; + + Hashtable keys = new Hashtable(); CodeCompileUnit code; CodeTypeDeclaration type; @@ -115,7 +117,7 @@ namespace Mono.Windows.Serialization { // bottom of stack holds CodeVariableReferenceExpression // pushes a reference to the new current type - public override void CreateObject(Type type, string varName) + public override void CreateObject(Type type, string varName, string key) { debug(); bool isDefaultName; @@ -138,6 +140,8 @@ namespace Mono.Windows.Serialization { varName += (int)nameClashes[varName]; } + if (key != null) + keys[key] = varName; if (isDefaultName) { CodeVariableDeclarationStatement declaration = @@ -293,12 +297,26 @@ namespace Mono.Windows.Serialization { constructor.Statements.Add(assignment); } - public override void CreateDependencyPropertyObject(Type type, string varName) + + public override void CreatePropertyReference(string key) + { + CodeAssignStatement assignment = new CodeAssignStatement( + (CodeExpression)peek(), + new CodeVariableReferenceExpression(key)); + + constructor.Statements.Add(assignment); + } + public override void CreateDependencyPropertyReference(string key) + { + CreatePropertyReference(key); + } + + public override void CreateDependencyPropertyObject(Type type, string varName, string key) { - CreatePropertyObject(type, varName); + CreatePropertyObject(type, varName, key); } - public override void CreatePropertyObject(Type type, string varName) + public override void CreatePropertyObject(Type type, string varName, string key) { debug(); bool isDefaultName; @@ -320,6 +338,8 @@ namespace Mono.Windows.Serialization { varName += (int)nameClashes[varName]; } + if (key != null) + keys[key] = varName; if (isDefaultName) { CodeVariableDeclarationStatement declaration = diff --git a/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlParser.cs b/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlParser.cs index eda245a8bdd..9829f54b30a 100644 --- a/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlParser.cs +++ b/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlParser.cs @@ -339,7 +339,7 @@ namespace Mono.Windows.Serialization { } else { parseChildObjectElement(parent); } - + if (isEmpty) tempStateCount ++; processObjectAttributes(); @@ -353,6 +353,8 @@ namespace Mono.Windows.Serialization { if (reader.GetAttribute("Name", XAML_NAMESPACE) != null) throw new Exception("The XAML Name attribute can not be applied to top level elements\n"+ "Do you mean the Class attribute?"); + if (reader.GetAttribute("Key", XAML_NAMESPACE) != null) + throw new Exception("The XAML Key attribute can not be applied to top level elements."); begun = true; createTopLevel(parent.AssemblyQualifiedName, reader.GetAttribute("Class", XAML_NAMESPACE)); } @@ -366,16 +368,18 @@ namespace Mono.Windows.Serialization { if (name == null) name = reader.GetAttribute("Name", reader.NamespaceURI); + string key = reader.GetAttribute("Key", XAML_NAMESPACE); + Debug.WriteLine("XamlParser: parent is " + parent); if (currentState().type == CurrentType.Object || currentState().type == CurrentType.PropertyObject || currentState().type == CurrentType.DependencyPropertyObject) { abortIfNotAddChild("object"); - addChild(parent, name); + addChild(parent, name, key); } else if (currentState().type == CurrentType.Property) { - addPropertyChild(parent, name); + addPropertyChild(parent, name, key); } else if (currentState().type == CurrentType.DependencyProperty) { - addDependencyPropertyChild(parent, name); + addDependencyPropertyChild(parent, name, key); } else { throw new NotImplementedException(currentState().type.ToString()); } @@ -388,7 +392,7 @@ namespace Mono.Windows.Serialization { continue; if (reader.NamespaceURI == XAML_NAMESPACE) continue; - if (reader.LocalName.IndexOf(".") < 0) + else if (reader.LocalName.IndexOf(".") < 0) parseLocalPropertyAttribute(); else parseDependencyPropertyAttribute(); @@ -455,33 +459,44 @@ namespace Mono.Windows.Serialization { push(CurrentType.Object, t); } - void addChild(Type type, string objectName) + XamlElementStartNode getChildStart(Type type, string key) { - nodeQueue.Add(new XamlElementStartNode( - reader.LineNumber, - reader.LinePosition, - getDepth(), - type.Assembly.FullName, - type.AssemblyQualifiedName, - type, - null)); + if (key == null) { + return new XamlElementStartNode( + reader.LineNumber, + reader.LinePosition, + getDepth(), + type.Assembly.FullName, + type.AssemblyQualifiedName, + type, + null); + } else { + XamlKeyElementStartNode n = new XamlKeyElementStartNode( + reader.LineNumber, + reader.LinePosition, + getDepth(), + type.Assembly.FullName, + type.AssemblyQualifiedName, + type, + null); + n.setkey(key); + return n; + } + } + + void addChild(Type type, string objectName, string key) + { + nodeQueue.Add(getChildStart(type, key)); ((XamlElementStartNode)topNode()).setname(objectName); // writer.CreateObject(type, objectName); push(CurrentType.Object, type); } - void addPropertyChild(Type type, string objectName) + void addPropertyChild(Type type, string objectName, string key) { // writer.CreatePropertyObject(type, objectName); - nodeQueue.Add(new XamlElementStartNode( - reader.LineNumber, - reader.LinePosition, - getDepth(), - type.Assembly.FullName, - type.AssemblyQualifiedName, - type, - null)); + nodeQueue.Add(getChildStart(type, key)); ((XamlElementStartNode)topNode()).setname(objectName); ((XamlElementStartNode)topNode()).setpropertyObject(true); @@ -489,17 +504,10 @@ namespace Mono.Windows.Serialization { push(CurrentType.PropertyObject, type); } - void addDependencyPropertyChild(Type type, string objectName) + void addDependencyPropertyChild(Type type, string objectName, string key) { // writer.CreatePropertyObject(type, objectName); - nodeQueue.Add(new XamlElementStartNode( - reader.LineNumber, - reader.LinePosition, - getDepth(), - type.Assembly.FullName, - type.AssemblyQualifiedName, - type, - null)); + nodeQueue.Add(getChildStart(type, key)); ((XamlElementStartNode)topNode()).setname(objectName); ((XamlElementStartNode)topNode()).setdepPropertyObject(true); @@ -534,12 +542,7 @@ namespace Mono.Windows.Serialization { ((XamlPropertyNode)nodeQueue[nodeQueue.Count - 1]).setPropInfo(prop); if (!prop.PropertyType.IsSubclassOf(typeof(Delegate))) { - - nodeQueue.Add(new XamlTextNode( - reader.LineNumber, - reader.LinePosition, - getDepth(), - reader.Value)); + nodeQueue.Add(getPropertyValueNode()); ((XamlTextNode)topNode()).setmode(XamlParseMode.Property); // writer.CreatePropertyText(reader.Value, prop.PropertyType); @@ -576,6 +579,21 @@ namespace Mono.Windows.Serialization { return true; } + XamlTextNode getPropertyValueNode() + { + XamlTextNode n = new XamlTextNode( + reader.LineNumber, + reader.LinePosition, + getDepth(), + reader.Value); + if (n.TextContent.StartsWith("{StaticResource ")) { + n.setkeyText(n.TextContent.Remove(0, "{StaticResource ".Length).TrimEnd('}')); + } + return n; + } + + + void ensureDependencyObject(Type currentType) { if (!currentType.IsSubclassOf(typeof(System.Windows.DependencyObject))) @@ -632,7 +650,7 @@ namespace Mono.Windows.Serialization { false)); ((XamlPropertyNode)topNode()).setDP(dp); - nodeQueue.Add(new XamlTextNode(reader.LineNumber, reader.LinePosition, getDepth(), reader.Value)); + nodeQueue.Add(getPropertyValueNode()); ((XamlTextNode)topNode()).setmode(XamlParseMode.DependencyProperty); ((XamlTextNode)topNode()).setfinalType(dp.PropertyType); diff --git a/mcs/class/PresentationFramework/System.Windows.Serialization/Parser.cs b/mcs/class/PresentationFramework/System.Windows.Serialization/Parser.cs index b715ffffce7..67abdba2f6d 100644 --- a/mcs/class/PresentationFramework/System.Windows.Serialization/Parser.cs +++ b/mcs/class/PresentationFramework/System.Windows.Serialization/Parser.cs @@ -57,16 +57,21 @@ namespace System.Windows.Serialization { public object instance; ArrayList objects = new ArrayList(); + Hashtable keys = new Hashtable(); + public override void CreateTopLevel(Type parent, string className) { instance = Activator.CreateInstance(parent); push(instance); } - public override void CreateObject(Type type, string varName) + public override void CreateObject(Type type, string varName, string key) { Object o = Activator.CreateInstance(type); ((IAddChild)peek()).AddChild(o); + + if (key != null) + keys[key] = o; push(o); } @@ -125,12 +130,23 @@ namespace System.Windows.Serialization { storeToProperty(value); } - public override void CreatePropertyObject(Type type, string name) + public override void CreatePropertyObject(Type type, string name, string key) { object value = Activator.CreateInstance(type); Debug.WriteLine("ObjectWriter CREATING PROPERTY OBJECT of type" + type); + if (key != null) + keys[key] = value; push(value); } + + public override void CreatePropertyReference(string key) + { + push(keys[key]); + } + public override void CreateDependencyPropertyReference(string key) + { + push(keys[key]); + } public override void EndPropertyObject(Type destType) { object value = convertPropertyObjectValue(destType, pop()); @@ -161,9 +177,9 @@ namespace System.Windows.Serialization { } } - public override void CreateDependencyPropertyObject(Type type, string name) + public override void CreateDependencyPropertyObject(Type type, string name, string key) { - CreatePropertyObject(type, name); + CreatePropertyObject(type, name, key); } public override void EndDependencyPropertyObject(Type finalType) { diff --git a/mcs/class/PresentationFramework/System.Windows.Serialization/data-classes.txt b/mcs/class/PresentationFramework/System.Windows.Serialization/data-classes.txt index 35ddd610576..c7e8ddf5f58 100644 --- a/mcs/class/PresentationFramework/System.Windows.Serialization/data-classes.txt +++ b/mcs/class/PresentationFramework/System.Windows.Serialization/data-classes.txt @@ -3,7 +3,7 @@ XamlClrEventNode(XamlAttributeNode): string eventName, MemberInfo eventMember, + XamlConstructorParametersEndNode XamlConstructorParametersStartNode XamlConstructorParameterTypeNode: string valueTypeFullName, string valueAssemblyName, Type valueElementType -XamlDefAttributeKeyTypeNode(XamlAttributeNode): string assemblyName, string valueType +XamlDefAttributeKeyTypeNode(XamlAttributeNode): string assemblyName, Type valueType XamlDefAttributeNode(XamlAttributeNode): string name, +string value XamlDefTagNode(XamlAttributeNode): bool isEmptyElement, System.Xml.XmlReader xmlReader, string defTagName XamlDocumentEndNode @@ -12,7 +12,7 @@ XamlElementStartNode: string assemblyName, string typeFullName, Type elementType XamlElementEndNode; bool propertyObject, bool depPropertyObject, Type finalType XamlEndAttributesNode: bool compact XamlKeyElementEndNode(XamlElementEndNode) -XamlKeyElementStartNode(XamlElementStartNode) +XamlKeyElementStartNode(XamlElementStartNode); string key XamlLanguageNode: string language XamlLiteralContentNode: string content XamlNode(object): XamlNodeType tokenType, int lineNumber, int linePosition, int depth @@ -28,7 +28,7 @@ XamlPropertyIListStartNode(XamlPropertyComplexStartNode) XamlPropertyNode(XamlPropertyComplexStartNode): string value, string xmlNamespace, BamlAttributeUsage attributeUsage, bool complexAsSimple XamlPropertyWithTypeNode(XamlPropertyComplexStartNode): String valueTypeFullName, string valueAssemblyFullName, Type valueElementType, string valueSerializerTypeFullName, string valueSerializerTypeAssemblyName, string xmlNamespace XamlRoutedEventNode(XamlAttributeNode): System.Windows.RoutedEvent routedEvent, string assemblyName, string typeFullName, string routedEventName, +string value -XamlTextNode: string textContent; XamlParseMode mode, Type finalType +XamlTextNode: string textContent; XamlParseMode mode, Type finalType, string keyText XamlUnknownAttributeNode(XamlAttributeNode): string xmlNamespace, string name, +string value XamlUnknownTagEndNode XamlUnknownTagStartNode(XamlAttributeNode): string xmlNamespace, +string value |