//#########################################################################################
//★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
//★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★
//#########################################################################################
/*
* Copyright ?2005, Mathew Hall
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
namespace XPTable.Models
{
#region CellPadding
///
/// Specifies the amount of space between the border and any contained
/// items along each edge of an object
///
[Serializable(),
StructLayout(LayoutKind.Sequential),
TypeConverter(typeof(CellPaddingConverter))]
public struct CellPadding
{
#region Class Data
///
/// Represents a Padding structure with its properties
/// left uninitialized
///
public static readonly CellPadding Empty = new CellPadding(0, 0, 0, 0);
///
/// The width of the left padding
///
private int left;
///
/// The width of the right padding
///
private int right;
///
/// The width of the top padding
///
private int top;
///
/// The width of the bottom padding
///
private int bottom;
#endregion
#region Constructor
///
/// Initializes a new instance of the Padding class
///
/// The width of the left padding value
/// The height of top padding value
/// The width of the right padding value
/// The height of bottom padding value
public CellPadding(int left, int top, int right, int bottom)
{
this.left = left;
this.right = right;
this.top = top;
this.bottom = bottom;
}
#endregion
#region Methods
///
/// Tests whether obj is a CellPadding structure with the same values as
/// this Padding structure
///
/// The Object to test
/// This method returns true if obj is a CellPadding structure
/// and its Left, Top, Right, and Bottom properties are equal to
/// the corresponding properties of this CellPadding structure;
/// otherwise, false
public override bool Equals(object obj)
{
if (!(obj is CellPadding))
{
return false;
}
CellPadding padding = (CellPadding) obj;
if (((padding.Left == this.Left) && (padding.Top == this.Top)) && (padding.Right == this.Right))
{
return (padding.Bottom == this.Bottom);
}
return false;
}
///
/// Returns the hash code for this CellPadding structure
///
/// An integer that represents the hashcode for this
/// padding
public override int GetHashCode()
{
return (((this.Left ^ ((this.Top << 13) | (this.Top >> 0x13))) ^ ((this.Right << 0x1a) | (this.Right >> 6))) ^ ((this.Bottom << 7) | (this.Bottom >> 0x19)));
}
#endregion
#region Properties
///
/// Gets or sets the width of the left padding value
///
public int Left
{
get
{
return this.left;
}
set
{
if (value < 0)
{
throw new ArgumentException("Padding value cannot be negative");
}
this.left = value;
}
}
///
/// Gets or sets the width of the right padding value
///
public int Right
{
get
{
return this.right;
}
set
{
if (value < 0)
{
throw new ArgumentException("Padding value cannot be negative");
}
this.right = value;
}
}
///
/// Gets or sets the height of the top padding value
///
public int Top
{
get
{
return this.top;
}
set
{
if (value < 0)
{
throw new ArgumentException("Padding value cannot be negative");
}
this.top = value;
}
}
///
/// Gets or sets the height of the bottom padding value
///
public int Bottom
{
get
{
return this.bottom;
}
set
{
if (value < 0)
{
throw new ArgumentException("Padding value cannot be negative");
}
this.bottom = value;
}
}
///
/// Tests whether all numeric properties of this CellPadding have
/// values of zero
///
[Browsable(false)]
public bool IsEmpty
{
get
{
if (((this.Left == 0) && (this.Top == 0)) && (this.Right == 0))
{
return (this.Bottom == 0);
}
return false;
}
}
#endregion
#region Operators
///
/// Tests whether two CellPadding structures have equal Left, Top,
/// Right, and Bottom properties
///
/// The CellPadding structure that is to the left
/// of the equality operator
/// The CellPadding structure that is to the right
/// of the equality operator
/// This operator returns true if the two CellPadding structures
/// have equal Left, Top, Right, and Bottom properties
public static bool operator ==(CellPadding left, CellPadding right)
{
if (((left.Left == right.Left) && (left.Top == right.Top)) && (left.Right == right.Right))
{
return (left.Bottom == right.Bottom);
}
return false;
}
///
/// Tests whether two CellPadding structures differ in their Left, Top,
/// Right, and Bottom properties
///
/// The CellPadding structure that is to the left
/// of the equality operator
/// The CellPadding structure that is to the right
/// of the equality operator
/// This operator returns true if any of the Left, Top, Right,
/// and Bottom properties of the two CellPadding structures are unequal;
/// otherwise false
public static bool operator !=(CellPadding left, CellPadding right)
{
return !(left == right);
}
#endregion
}
#endregion
#region CellPaddingConverter
///
/// A custom TypeConverter used to help convert CellPadding objects from
/// one Type to another
///
internal class CellPaddingConverter : TypeConverter
{
///
/// Returns whether this converter can convert an object of the
/// given type to the type of this converter, using the specified context
///
/// An ITypeDescriptorContext that provides
/// a format context
/// A Type that represents the type you
/// want to convert from
/// true if this converter can perform the conversion;
/// otherwise, false
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
///
/// Returns whether this converter can convert the object to the
/// specified type, using the specified context
///
/// An ITypeDescriptorContext that provides a
/// format context
/// A Type that represents the type you
/// want to convert to
/// true if this converter can perform the conversion;
/// otherwise, false
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
///
/// Converts the given object to the type of this converter, using
/// the specified context and culture information
///
/// An ITypeDescriptorContext that provides a
/// format context
/// The CultureInfo to use as the current culture
/// The Object to convert
/// An Object that represents the converted value
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
string text = ((string) value).Trim();
if (text.Length == 0)
{
return null;
}
if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}
char[] listSeparators = culture.TextInfo.ListSeparator.ToCharArray();
string[] s = text.Split(listSeparators);
if (s.Length < 4)
{
return null;
}
return new CellPadding(int.Parse(s[0]), int.Parse(s[1]), int.Parse(s[2]), int.Parse(s[3]));
}
return base.ConvertFrom(context, culture, value);
}
///
/// Converts the given value object to the specified type, using
/// the specified context and culture information
///
/// An ITypeDescriptorContext that provides
/// a format context
/// A CultureInfo object. If a null reference
/// is passed, the current culture is assumed
/// The Object to convert
/// The Type to convert the value
/// parameter to
/// An Object that represents the converted value
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if ((destinationType == typeof(string)) && (value is CellPadding))
{
CellPadding p = (CellPadding) value;
if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}
string separator = culture.TextInfo.ListSeparator + " ";
TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
string[] s = new string[4];
s[0] = converter.ConvertToString(context, culture, p.Left);
s[1] = converter.ConvertToString(context, culture, p.Top);
s[2] = converter.ConvertToString(context, culture, p.Right);
s[3] = converter.ConvertToString(context, culture, p.Bottom);
return string.Join(separator, s);
}
if ((destinationType == typeof(InstanceDescriptor)) && (value is CellPadding))
{
CellPadding p = (CellPadding) value;
Type[] t = new Type[4];
t[0] = t[1] = t[2] = t[3] = typeof(int);
ConstructorInfo info = typeof(CellPadding).GetConstructor(t);
if (info != null)
{
object[] o = new object[4];
o[0] = p.Left;
o[1] = p.Top;
o[2] = p.Right;
o[3] = p.Bottom;
return new InstanceDescriptor(info, o);
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
///
/// Creates an instance of the Type that this TypeConverter is associated
/// with, using the specified context, given a set of property values for
/// the object
///
/// An ITypeDescriptorContext that provides a format
/// context
/// An IDictionary of new property values
/// An Object representing the given IDictionary, or a null
/// reference if the object cannot be created
public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
{
return new CellPadding((int) propertyValues["Left"],
(int) propertyValues["Top"],
(int) propertyValues["Right"],
(int) propertyValues["Bottom"]);
}
///
/// Returns whether changing a value on this object requires a call to
/// CreateInstance to create a new value, using the specified context
///
/// An ITypeDescriptorContext that provides a
/// format context
/// true if changing a property on this object requires a call
/// to CreateInstance to create a new value; otherwise, false
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
///
/// Returns a collection of properties for the type of array specified
/// by the value parameter, using the specified context and attributes
///
/// An ITypeDescriptorContext that provides a format
/// context
/// An Object that specifies the type of array for
/// which to get properties
/// An array of type Attribute that is used as
/// a filter
/// A PropertyDescriptorCollection with the properties that are
/// exposed for this data type, or a null reference if there are no
/// properties
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
PropertyDescriptorCollection collection = TypeDescriptor.GetProperties(typeof(CellPadding), attributes);
string[] s = new string[4];
s[0] = "Left";
s[1] = "Top";
s[2] = "Right";
s[3] = "Bottom";
return collection.Sort(s);
}
///
/// Returns whether this object supports properties, using the specified context
///
/// An ITypeDescriptorContext that provides a format context
/// true if GetProperties should be called to find the properties of this
/// object; otherwise, false
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
}
#endregion
}