1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
//------------------------------------------------------------------------------
// <copyright file="SkinBuilder.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
/*
* Classes related to templated control support
*
* Copyright (c) 1999 Microsoft Corporation
*/
namespace System.Web.UI {
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Web.Compilation;
using System.Web.UI.WebControls;
using System.Web.Util;
#if !FEATURE_PAL
using System.Web.UI.Design;
#endif // !FEATURE_PAL
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public sealed class SkinBuilder : ControlBuilder {
private ThemeProvider _provider;
private Control _control;
private ControlBuilder _skinBuilder;
private string _themePath;
internal static readonly Object[] EmptyParams = new Object[0];
public SkinBuilder(ThemeProvider provider, Control control, ControlBuilder skinBuilder, string themePath) {
_provider = provider;
_control = control;
_skinBuilder = skinBuilder;
_themePath = themePath;
}
private void ApplyTemplateProperties(Control control) {
object[] parameters = new object[1];
ICollection entries = GetFilteredPropertyEntrySet(_skinBuilder.TemplatePropertyEntries);
foreach (TemplatePropertyEntry entry in entries) {
try {
object originalValue = FastPropertyAccessor.GetProperty(control, entry.Name, InDesigner);
if (originalValue == null) {
ControlBuilder controlBuilder = ((TemplatePropertyEntry)entry).Builder;
controlBuilder.SetServiceProvider(ServiceProvider);
try {
object objectValue = controlBuilder.BuildObject(true);
parameters[0] = objectValue;
}
finally {
controlBuilder.SetServiceProvider(null);
}
MethodInfo methodInfo = entry.PropertyInfo.GetSetMethod();
Util.InvokeMethod(methodInfo, control, parameters);
}
}
catch (Exception e) {
Debug.Fail(e.Message);
}
#pragma warning disable 1058
catch {
}
#pragma warning restore 1058
}
}
private void ApplyComplexProperties(Control control) {
ICollection entries = GetFilteredPropertyEntrySet(_skinBuilder.ComplexPropertyEntries);
foreach (ComplexPropertyEntry entry in entries) {
ControlBuilder builder = entry.Builder;
if (builder != null) {
string propertyName = entry.Name;
if (entry.ReadOnly) {
object objectValue = FastPropertyAccessor.GetProperty(control, propertyName, InDesigner);
if (objectValue == null) continue;
entry.Builder.SetServiceProvider(ServiceProvider);
try {
entry.Builder.InitObject(objectValue);
}
finally {
entry.Builder.SetServiceProvider(null);
}
}
else {
object childObj;
string actualPropName;
object value = entry.Builder.BuildObject(true);
// Make the UrlProperty based on theme path for control themes(Must be a string)
PropertyDescriptor desc = PropertyMapper.GetMappedPropertyDescriptor(control, PropertyMapper.MapNameToPropertyName(propertyName), out childObj, out actualPropName, InDesigner);
if (desc != null) {
string str = value as string;
if (value != null && desc.Attributes[typeof(UrlPropertyAttribute)] != null &&
UrlPath.IsRelativeUrl(str)) {
value = _themePath + str;
}
}
FastPropertyAccessor.SetProperty(childObj, propertyName, value, InDesigner);
}
}
}
}
private void ApplySimpleProperties(Control control) {
ICollection entries = GetFilteredPropertyEntrySet(_skinBuilder.SimplePropertyEntries);
foreach (SimplePropertyEntry entry in entries) {
try {
if (entry.UseSetAttribute) {
SetSimpleProperty(entry, control);
continue;
}
string propertyName = PropertyMapper.MapNameToPropertyName(entry.Name);
object childObj;
string actualPropName;
PropertyDescriptor desc = PropertyMapper.GetMappedPropertyDescriptor(control, propertyName, out childObj, out actualPropName, InDesigner);
if (desc != null) {
DefaultValueAttribute defValAttr = (DefaultValueAttribute)desc.Attributes[typeof(DefaultValueAttribute)];
object currentValue = desc.GetValue(childObj);
// Only apply the themed value if different from default value.
if (defValAttr != null && !object.Equals(defValAttr.Value, currentValue)) {
continue;
}
object value = entry.Value;
// Make the UrlProperty based on theme path for control themes.
string str = value as string;
if (value != null && desc.Attributes[typeof(UrlPropertyAttribute)] != null &&
UrlPath.IsRelativeUrl(str)) {
value = _themePath + str;
}
SetSimpleProperty(entry, control);
}
}
catch (Exception e) {
Debug.Fail(e.Message);
}
#pragma warning disable 1058
catch {
}
#pragma warning restore 1058
}
}
private void ApplyBoundProperties(Control control) {
DataBindingCollection dataBindings = null;
IAttributeAccessor attributeAccessor = null;
// If there are no filters in the picture, use the entries as is
ICollection entries = GetFilteredPropertyEntrySet(_skinBuilder.BoundPropertyEntries);
foreach (BoundPropertyEntry entry in entries) {
InitBoundProperty(control, entry, ref dataBindings, ref attributeAccessor);
}
}
private void InitBoundProperty(Control control, BoundPropertyEntry entry,
ref DataBindingCollection dataBindings, ref IAttributeAccessor attributeAccessor) {
string expressionPrefix = entry.ExpressionPrefix;
// If we're in the designer, add the bound properties to the collections
if (expressionPrefix.Length == 0) {
if (dataBindings == null && control is IDataBindingsAccessor) {
dataBindings = ((IDataBindingsAccessor)control).DataBindings;
}
dataBindings.Add(new DataBinding(entry.Name, entry.Type, entry.Expression.Trim()));
}
else {
throw new InvalidOperationException(SR.GetString(SR.ControlBuilder_ExpressionsNotAllowedInThemes));
}
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public Control ApplyTheme() {
if (_skinBuilder != null) {
ApplySimpleProperties(_control);
ApplyComplexProperties(_control);
ApplyBoundProperties(_control);
ApplyTemplateProperties(_control);
}
return _control;
}
}
public sealed class ThemeProvider {
private IDictionary _skinBuilders;
private string[] _cssFiles;
private string _themeName;
private string _themePath;
private int _contentHashCode;
private IDesignerHost _host;
public ThemeProvider(IDesignerHost host, string name, string themeDefinition, string[] cssFiles, string themePath) {
_themeName = name;
_themePath = themePath;
_cssFiles = cssFiles;
_host = host;
ControlBuilder themeBuilder = DesignTimeTemplateParser.ParseTheme(host, themeDefinition, themePath);
_contentHashCode = themeDefinition.GetHashCode();
ArrayList subBuilders = themeBuilder.SubBuilders;
_skinBuilders = new Hashtable();
for (int i=0; i<subBuilders.Count; ++i) {
ControlBuilder builder = subBuilders[i] as ControlBuilder;
if (builder != null) {
IDictionary skins = _skinBuilders[builder.ControlType] as IDictionary;
if (skins == null) {
skins = new SortedList(StringComparer.OrdinalIgnoreCase);
_skinBuilders[builder.ControlType] = skins;
}
Control builtControl = builder.BuildObject() as Control;
if (builtControl != null) {
skins[builtControl.SkinID] = builder;
}
}
}
}
public int ContentHashCode {
get {
return _contentHashCode;
}
}
public ICollection CssFiles {
get {
return _cssFiles;
}
}
public IDesignerHost DesignerHost {
get {
return _host;
}
}
public string ThemeName {
get {
return _themeName;
}
}
public ICollection GetSkinsForControl(Type type) {
IDictionary skins = _skinBuilders[type] as IDictionary;
if (skins == null) {
return new ArrayList();
}
return skins.Keys;
}
public SkinBuilder GetSkinBuilder(Control control) {
IDictionary skins = _skinBuilders[control.GetType()] as IDictionary;
if (skins == null) {
return null;
}
ControlBuilder builder = skins[control.SkinID] as ControlBuilder;
if (builder == null) {
return null;
}
return new SkinBuilder(this, control, builder, _themePath);
}
public IDictionary GetSkinControlBuildersForControlType(Type type) {
IDictionary skins = _skinBuilders[type] as IDictionary;
return skins;
}
}
}
|