You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
243 lines
6.3 KiB
243 lines
6.3 KiB
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team |
|
// |
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this |
|
// software and associated documentation files (the "Software"), to deal in the Software |
|
// without restriction, including without limitation the rights to use, copy, modify, merge, |
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
|
// to whom the Software is furnished to do so, subject to the following conditions: |
|
// |
|
// The above copyright notice and this permission notice shall be included in all copies or |
|
// substantial portions of the Software. |
|
// |
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|
// DEALINGS IN THE SOFTWARE. |
|
|
|
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Text; |
|
using System.Windows.Markup; |
|
using System.Xaml; |
|
|
|
namespace ICSharpCode.WpfDesign.XamlDom |
|
{ |
|
/// <summary> |
|
/// A service provider that provides the IProvideValueTarget and IXamlTypeResolver services. |
|
/// No other services (e.g. from the document's service provider) are offered. |
|
/// </summary> |
|
public class XamlObjectServiceProvider : IServiceProvider, IXamlNameResolver, IProvideValueTarget, IXamlSchemaContextProvider, IAmbientProvider |
|
{ |
|
/// <summary> |
|
/// Creates a new XamlObjectServiceProvider instance. |
|
/// </summary> |
|
public XamlObjectServiceProvider(XamlObject obj) |
|
{ |
|
if (obj == null) |
|
throw new ArgumentNullException("obj"); |
|
XamlObject = obj; |
|
Resolver = new XamlTypeResolverProvider(obj); |
|
} |
|
|
|
/// <summary> |
|
/// Gets the XamlObject that owns this service provider (e.g. the XamlObject that represents a markup extension). |
|
/// </summary> |
|
public XamlObject XamlObject { get; private set; } |
|
internal XamlTypeResolverProvider Resolver { get; private set; } |
|
|
|
#region IServiceProvider Members |
|
|
|
/// <summary> |
|
/// Retrieves the service of the specified type. |
|
/// </summary> |
|
public object GetService(Type serviceType) |
|
{ |
|
if (serviceType == typeof(IProvideValueTarget)) { |
|
return this; |
|
} |
|
if (serviceType == typeof(IXamlTypeResolver)) { |
|
return Resolver; |
|
} |
|
if (serviceType == typeof(XamlTypeResolverProvider)) |
|
{ |
|
return Resolver; |
|
} |
|
if (serviceType == typeof(IXamlSchemaContextProvider)) { |
|
return this; |
|
} |
|
if (serviceType == typeof(IAmbientProvider)) { |
|
return this; |
|
} |
|
if (serviceType == typeof(IXamlNameResolver)) |
|
{ |
|
return this; |
|
} |
|
|
|
return null; |
|
} |
|
|
|
#endregion |
|
|
|
#region IProvideValueTarget Members |
|
|
|
/// <summary> |
|
/// Gets the target object (the DependencyObject instance on which a property should be set) |
|
/// </summary> |
|
public object TargetObject { |
|
get { |
|
var parentProperty = XamlObject.ParentProperty; |
|
|
|
if (parentProperty == null) { |
|
return null; |
|
} |
|
|
|
if (parentProperty.IsCollection) { |
|
return parentProperty.ValueOnInstance; |
|
} |
|
|
|
return parentProperty.ParentObject.Instance; |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// Gets the target dependency property. |
|
/// </summary> |
|
public object TargetProperty { |
|
get { |
|
var parentProperty = XamlObject.ParentProperty; |
|
|
|
if (parentProperty == null) { |
|
return null; |
|
} |
|
|
|
return parentProperty.DependencyProperty; |
|
} |
|
} |
|
|
|
#endregion |
|
|
|
#region IXamlSchemaContextProvider Members |
|
|
|
private XamlSchemaContext iCsharpXamlSchemaContext; |
|
|
|
//Maybe we new our own XamlSchemaContext? |
|
//private class ICsharpXamlSchemaContext : XamlSchemaContext |
|
//{ |
|
// public override XamlType GetXamlType(Type type) |
|
// { |
|
// return base.GetXamlType(type); |
|
// } |
|
//} |
|
|
|
public XamlSchemaContext SchemaContext |
|
{ |
|
get |
|
{ |
|
return iCsharpXamlSchemaContext = iCsharpXamlSchemaContext ?? new XamlSchemaContext(); |
|
} |
|
} |
|
|
|
#endregion |
|
|
|
#region IAmbientProvider Members |
|
|
|
public AmbientPropertyValue GetFirstAmbientValue(IEnumerable<XamlType> ceilingTypes, params XamlMember[] properties) |
|
{ |
|
return GetAllAmbientValues(ceilingTypes, properties).FirstOrDefault(); |
|
} |
|
|
|
public object GetFirstAmbientValue(params XamlType[] types) |
|
{ |
|
return null; |
|
} |
|
|
|
public IEnumerable<AmbientPropertyValue> GetAllAmbientValues(IEnumerable<XamlType> ceilingTypes, params XamlMember[] properties) |
|
{ |
|
var obj = this.XamlObject.ParentObject; |
|
|
|
while (obj != null) |
|
{ |
|
if (ceilingTypes.Any(x => obj.SystemXamlTypeForProperty.CanAssignTo(x))) |
|
{ |
|
foreach (var pr in obj.Properties) |
|
{ |
|
if (properties.Any(x => x.Name == pr.PropertyName)) |
|
{ |
|
yield return new AmbientPropertyValue(pr.SystemXamlMemberForProperty, pr.ValueOnInstance); |
|
} |
|
} |
|
} |
|
|
|
obj = obj.ParentObject; |
|
} |
|
} |
|
|
|
public IEnumerable<object> GetAllAmbientValues(params XamlType[] types) |
|
{ |
|
return new List<object>(); |
|
} |
|
|
|
public IEnumerable<AmbientPropertyValue> GetAllAmbientValues(IEnumerable<XamlType> ceilingTypes, bool searchLiveStackOnly, IEnumerable<XamlType> types, params XamlMember[] properties) |
|
{ |
|
return new List<AmbientPropertyValue>(); |
|
} |
|
|
|
#endregion |
|
|
|
#region IXamlNameResolver |
|
|
|
public object Resolve(string name) |
|
{ |
|
INameScope ns = null; |
|
var xamlObj = this.XamlObject; |
|
while (xamlObj != null) |
|
{ |
|
ns = NameScopeHelper.GetNameScopeFromObject(xamlObj.Instance); |
|
|
|
if (ns != null) { |
|
var obj = ns.FindName(name); |
|
if (obj != null) |
|
return obj; |
|
} |
|
|
|
xamlObj = xamlObj.ParentObject; |
|
} |
|
|
|
return null; |
|
} |
|
|
|
public object Resolve(string name, out bool isFullyInitialized) |
|
{ |
|
var ret = Resolve(name); |
|
isFullyInitialized = ret != null; |
|
return ret; |
|
} |
|
|
|
public object GetFixupToken(IEnumerable<string> names) |
|
{ |
|
return null; |
|
} |
|
|
|
public object GetFixupToken(IEnumerable<string> names, bool canAssignDirectly) |
|
{ |
|
return null; |
|
} |
|
|
|
public IEnumerable<KeyValuePair<string, object>> GetAllNamesAndValuesInScope() |
|
{ |
|
return null; |
|
} |
|
|
|
public bool IsFixupTokenAvailable |
|
{ |
|
get { return false; } |
|
} |
|
|
|
public event EventHandler OnNameScopeInitializationComplete; |
|
|
|
#endregion |
|
} |
|
}
|
|
|