@ -10,24 +10,41 @@ using System.Diagnostics;
namespace ICSharpCode.WpfDesign
namespace ICSharpCode.WpfDesign
{
{
/// <summary>
/// Contains helper methods for retrieving meta data.
/// </summary>
public static class Metadata
public static class Metadata
{
{
/// <summary>
/// Gets the full name of a dependency property (OwnerType.FullName + "." + Name).
/// </summary>
public static string GetFullName ( this DependencyProperty p )
public static string GetFullName ( this DependencyProperty p )
{
{
return p . OwnerType . FullName + "." + p . Name ;
return p . OwnerType . FullName + "." + p . Name ;
}
}
// TODO: do we really want to store these values in a static dictionary?
// Why not per-design context (as a service?)
static Dictionary < Type , List < object > > standardValues = new Dictionary < Type , List < object > > ( ) ;
static Dictionary < Type , List < object > > standardValues = new Dictionary < Type , List < object > > ( ) ;
/// <summary>
/// Registers a set of standard values for a <paramref name="type"/> by using the
/// public static properties of the type <paramref name="valuesContainer"/>.
/// </summary>
/// <example>Metadata.AddStandardValues(typeof(Brush), typeof(Brushes));</example>
public static void AddStandardValues ( Type type , Type valuesContainer )
public static void AddStandardValues ( Type type , Type valuesContainer )
{
{
AddStandardValues ( type , valuesContainer
AddStandardValues ( type ,
. GetProperties ( BindingFlags . Public | BindingFlags . Static )
valuesContainer . GetProperties ( BindingFlags . Public | BindingFlags . Static )
. Select ( p = > p . GetValue ( null , null ) ) ) ;
. Select ( p = > p . GetValue ( null , null ) ) ) ;
}
}
/// <summary>
/// Registers a set of standard <paramref name="values"/> for a <paramref name="type"/>.
/// </summary>
/// <remarks>You can call this method multiple times to add additional standard values.</remarks>
public static void AddStandardValues < T > ( Type type , IEnumerable < T > values )
public static void AddStandardValues < T > ( Type type , IEnumerable < T > values )
{
{
List < object > list ;
List < object > list ;
lock ( standardValues ) {
lock ( standardValues ) {
if ( ! standardValues . TryGetValue ( type , out list ) ) {
if ( ! standardValues . TryGetValue ( type , out list ) ) {
@ -40,6 +57,9 @@ namespace ICSharpCode.WpfDesign
}
}
}
}
/// <summary>
/// Retrieves the standard values for the specified <paramref name="type"/>.
/// </summary>
public static IEnumerable GetStandardValues ( Type type )
public static IEnumerable GetStandardValues ( Type type )
{
{
if ( type . IsEnum ) {
if ( type . IsEnum ) {
@ -109,27 +129,36 @@ namespace ICSharpCode.WpfDesign
static HashSet < string > hiddenProperties = new HashSet < string > ( ) ;
static HashSet < string > hiddenProperties = new HashSet < string > ( ) ;
/// <summary>
/// Hides the specified property (marks it as not browsable).
/// </summary>
public static void HideProperty ( DependencyProperty p )
public static void HideProperty ( DependencyProperty p )
{
{
lock ( hiddenProperties ) {
lock ( hiddenProperties ) {
hiddenProperties . Add ( p . GetFullName ( ) ) ;
hiddenProperties . Add ( p . GetFullName ( ) ) ;
}
}
}
}
/// <summary>
/// Hides the specified property (marks it as not browsable).
/// </summary>
public static void HideProperty ( Type type , string member )
public static void HideProperty ( Type type , string member )
{
{
lock ( hiddenProperties ) {
lock ( hiddenProperties ) {
hiddenProperties . Add ( type . FullName + "." + member ) ;
hiddenProperties . Add ( type . FullName + "." + member ) ;
}
}
}
}
/// <summary>
/// Gets whether the specified property is browsable (should be visible in property grids).
/// </summary>
public static bool IsBrowsable ( DesignItemProperty p )
public static bool IsBrowsable ( DesignItemProperty p )
{
{
lock ( hiddenProperties ) {
lock ( hiddenProperties ) {
if ( hiddenProperties . Contains ( p . DependencyFullName ) ) {
if ( hiddenProperties . Contains ( p . DependencyFullName ) ) {
return false ;
return false ;
}
}
}
}
return true ;
return true ;
}
}
@ -137,57 +166,76 @@ namespace ICSharpCode.WpfDesign
static HashSet < string > popularProperties = new HashSet < string > ( ) ;
static HashSet < string > popularProperties = new HashSet < string > ( ) ;
/// <summary>
/// Registers a popular property (shown first in the property grid).
/// </summary>
public static void AddPopularProperty ( DependencyProperty p )
public static void AddPopularProperty ( DependencyProperty p )
{
{
lock ( popularProperties ) {
lock ( popularProperties ) {
popularProperties . Add ( p . GetFullName ( ) ) ;
popularProperties . Add ( p . GetFullName ( ) ) ;
}
}
}
}
/// <summary>
/// Registers a popular property (shown first in the property grid).
/// </summary>
public static void AddPopularProperty ( Type type , string member )
public static void AddPopularProperty ( Type type , string member )
{
{
lock ( popularProperties ) {
lock ( popularProperties ) {
popularProperties . Add ( type . FullName + "." + member ) ;
popularProperties . Add ( type . FullName + "." + member ) ;
}
}
}
}
/// <summary>
/// Gets whether the specified property was registered as popular.
/// </summary>
public static bool IsPopularProperty ( DesignItemProperty p )
public static bool IsPopularProperty ( DesignItemProperty p )
{
{
lock ( popularProperties ) {
lock ( popularProperties ) {
if ( popularProperties . Contains ( p . DependencyFullName ) ) {
if ( popularProperties . Contains ( p . DependencyFullName ) ) {
return true ;
return true ;
}
}
}
}
return false ;
return false ;
}
}
static HashSet < Type > popularControls = new HashSet < Type > ( ) ;
static HashSet < Type > popularControls = new HashSet < Type > ( ) ;
/// <summary>
/// Registers a popular control (visible in the default toolbox).
/// </summary>
public static void AddPopularControl ( Type t )
public static void AddPopularControl ( Type t )
{
{
lock ( popularControls ) {
lock ( popularControls ) {
popularControls . Add ( t ) ;
popularControls . Add ( t ) ;
}
}
}
}
/// <summary>
/// Gets the list of popular controls.
/// </summary>
public static IEnumerable < Type > GetPopularControls ( )
public static IEnumerable < Type > GetPopularControls ( )
{
{
lock ( popularControls ) {
lock ( popularControls ) {
foreach ( var t in popularControls ) {
return popularControls . ToArray ( ) ;
yield return t ;
}
}
}
}
}
/// <summary>
/// Gets whether the specified control was registered as popular.
/// </summary>
public static bool IsPopularControl ( Type t )
public static bool IsPopularControl ( Type t )
{
{
lock ( popularControls ) {
lock ( popularControls ) {
return popularControls . Contains ( t ) ;
return popularControls . Contains ( t ) ;
}
}
}
}
static Dictionary < string , NumberRange > ranges = new Dictionary < string , NumberRange > ( ) ;
static Dictionary < string , NumberRange > ranges = new Dictionary < string , NumberRange > ( ) ;
/// <summary>
/// Registers the value range for the property.
/// </summary>
public static void AddValueRange ( DependencyProperty p , double min , double max )
public static void AddValueRange ( DependencyProperty p , double min , double max )
{
{
lock ( ranges ) {
lock ( ranges ) {
@ -195,6 +243,9 @@ namespace ICSharpCode.WpfDesign
}
}
}
}
/// <summary>
/// Gets the registered value range for the property, or null if no range was registered.
/// </summary>
public static NumberRange GetValueRange ( DesignItemProperty p )
public static NumberRange GetValueRange ( DesignItemProperty p )
{
{
NumberRange r ;
NumberRange r ;
@ -208,6 +259,9 @@ namespace ICSharpCode.WpfDesign
static HashSet < Type > placementDisabled = new HashSet < Type > ( ) ;
static HashSet < Type > placementDisabled = new HashSet < Type > ( ) ;
/// <summary>
/// Disables the default placement behaviour (setting the ContentProperty) for the type.
/// </summary>
public static void DisablePlacement ( Type type )
public static void DisablePlacement ( Type type )
{
{
lock ( placementDisabled ) {
lock ( placementDisabled ) {
@ -215,6 +269,9 @@ namespace ICSharpCode.WpfDesign
}
}
}
}
/// <summary>
/// Gets whether thr default placement behaviour (setting the ContentProperty) was disabled for the type.
/// </summary>
public static bool IsPlacementDisabled ( Type type )
public static bool IsPlacementDisabled ( Type type )
{
{
lock ( placementDisabled ) {
lock ( placementDisabled ) {
@ -222,33 +279,50 @@ namespace ICSharpCode.WpfDesign
}
}
}
}
static Dictionary < Type , Size > defaultSizes = new Dictionary < Type , Size > ( ) ;
static Dictionary < Type , Size > defaultSizes = new Dictionary < Type , Size > ( ) ;
public static void AddDefaultSize ( Type t , Size s )
/// <summary>
{
/// Registers a default size for new controls of the specified type.
lock ( defaultSizes ) {
/// </summary>
defaultSizes [ t ] = s ;
public static void AddDefaultSize ( Type t , Size s )
}
{
}
lock ( defaultSizes ) {
defaultSizes [ t ] = s ;
public static Size GetDefaultSize ( Type t )
}
{
}
Size s ;
lock ( defaultSizes ) {
/// <summary>
while ( t ! = null ) {
/// Gets the default size for new controls of the specified type,
if ( defaultSizes . TryGetValue ( t , out s ) ) {
/// or new Size(double.NaN, double.NaN) if no default size was registered.
return s ;
/// </summary>
}
public static Size GetDefaultSize ( Type t )
t = t . BaseType ;
{
}
Size s ;
}
lock ( defaultSizes ) {
return new Size ( double . NaN , double . NaN ) ;
while ( t ! = null ) {
}
if ( defaultSizes . TryGetValue ( t , out s ) ) {
return s ;
}
t = t . BaseType ;
}
}
return new Size ( double . NaN , double . NaN ) ;
}
}
}
/// <summary>
/// Represets the minimum and maximum valid value for a double property.
/// </summary>
public class NumberRange
public class NumberRange
{
{
public double Min ;
/// <summary>
public double Max ;
/// Gets/Sets the minimum value.
/// </summary>
public double Min { get ; set ; }
/// <summary>
/// Gets/Sets the maximum value.
/// </summary>
public double Max { get ; set ; }
}
}
}
}