// // // // // $Revision$ // using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Reflection; using System.Windows.Forms; namespace ICSharpCode.PythonBinding { /// /// Contains the default values for all properties on a Control, Form or UserControl. /// public class PythonControlDefaultPropertyValues { Dictionary defaultPropertyValues = new Dictionary(); public PythonControlDefaultPropertyValues() { defaultPropertyValues.Add("Text", String.Empty); defaultPropertyValues.Add("AutoValidate", AutoValidate.EnablePreventFocusChange); defaultPropertyValues.Add("Enabled", true); defaultPropertyValues.Add("Visible", true); defaultPropertyValues.Add("AutoScaleMode", AutoScaleMode.Inherit); defaultPropertyValues.Add("DoubleBuffered", false); defaultPropertyValues.Add("ImeMode", ImeMode.NoControl); defaultPropertyValues.Add("RightToLeft", RightToLeft.No); defaultPropertyValues.Add("Cursor", Cursors.Default); defaultPropertyValues.Add("MinimumSize", new Size(0, 0)); defaultPropertyValues.Add("AutoScrollMinSize", new Size(0, 0)); defaultPropertyValues.Add("AutoScrollMargin", new Size(0, 0)); defaultPropertyValues.Add("Location", new Point(0, 0)); defaultPropertyValues.Add("Padding", Padding.Empty); defaultPropertyValues.Add("BackColor", Control.DefaultBackColor); defaultPropertyValues.Add("ForeColor", Control.DefaultForeColor); } /// /// Determines if the property value has its default value. /// /// The name of the property. /// The object that has the property. /// False if the property does not exist. public bool IsDefaultValue(string propertyName, object obj) { PropertyInfo propertyInfo = obj.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance); if (propertyInfo != null) { object propertyValue = propertyInfo.GetValue(obj, null); return IsDefaultValue(propertyInfo, propertyValue); } return false; } /// /// Determines if the property value is the default value by checking the DefaultValueAttribute. /// /// The property descriptor for the property. /// The object that has the property. /// /// For some properties such as Form.AutoValidate there is no default value specified by the /// DefaultValueAttribute. /// public bool IsDefaultValue(PropertyDescriptor propertyDescriptor, object obj) { PropertyInfo propertyInfo = obj.GetType().GetProperty(propertyDescriptor.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance); if (propertyInfo != null) { return IsDefaultValue(propertyInfo, propertyDescriptor.GetValue(obj)); } return false; } /// /// Determines if the property value is the default value by checking the DefaultValueAttribute. /// /// /// For some properties such as Form.AutoValidate there is no default value specified by the /// DefaultValueAttribute. /// public bool IsDefaultValue(PropertyInfo propertyInfo, object propertyValue) { // Check default attribute. DefaultValueAttribute defaultValueAttribute = (DefaultValueAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(DefaultValueAttribute), true); if (defaultValueAttribute != null) { if (defaultValueAttribute.Value != null) { return defaultValueAttribute.Value.Equals(propertyValue); } return defaultValueAttribute.Value == propertyValue; } object defaultPropertyValue = null; if (defaultPropertyValues.TryGetValue(propertyInfo.Name, out defaultPropertyValue)) { return defaultPropertyValue.Equals(propertyValue); } if (propertyInfo.Name == "Icon") { return true; } else if (propertyInfo.Name == "TransparencyKey") { return true; } else if (propertyInfo.Name == "Font") { // Default is Control.DefaultFont return true; } return false; } } }