@ -10,24 +10,41 @@ using System.Diagnostics;
@@ -10,24 +10,41 @@ using System.Diagnostics;
namespace ICSharpCode.WpfDesign
{
/// <summary>
/// Contains helper methods for retrieving meta data.
/// </summary>
public static class Metadata
{
/// <summary>
/// Gets the full name of a dependency property (OwnerType.FullName + "." + Name).
/// </summary>
public static string GetFullName ( this DependencyProperty p )
{
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 > > ( ) ;
/// <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 )
{
AddStandardValues ( type , valuesContainer
. GetProperties ( BindingFlags . Public | BindingFlags . Static )
. Select ( p = > p . GetValue ( null , null ) ) ) ;
AddStandardValues ( type ,
valuesContainer . GetProperties ( BindingFlags . Public | BindingFlags . Static )
. 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 )
{
{
List < object > list ;
lock ( standardValues ) {
if ( ! standardValues . TryGetValue ( type , out list ) ) {
@ -40,6 +57,9 @@ namespace ICSharpCode.WpfDesign
@@ -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 )
{
if ( type . IsEnum ) {
@ -109,27 +129,36 @@ namespace ICSharpCode.WpfDesign
@@ -109,27 +129,36 @@ namespace ICSharpCode.WpfDesign
static HashSet < string > hiddenProperties = new HashSet < string > ( ) ;
/// <summary>
/// Hides the specified property (marks it as not browsable).
/// </summary>
public static void HideProperty ( DependencyProperty p )
{
lock ( hiddenProperties ) {
hiddenProperties . Add ( p . GetFullName ( ) ) ;
}
lock ( hiddenProperties ) {
hiddenProperties . Add ( p . GetFullName ( ) ) ;
}
}
/// <summary>
/// Hides the specified property (marks it as not browsable).
/// </summary>
public static void HideProperty ( Type type , string member )
{
lock ( hiddenProperties ) {
hiddenProperties . Add ( type . FullName + "." + member ) ;
}
lock ( hiddenProperties ) {
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 )
{
lock ( hiddenProperties ) {
if ( hiddenProperties . Contains ( p . DependencyFullName ) ) {
return false ;
}
}
lock ( hiddenProperties ) {
if ( hiddenProperties . Contains ( p . DependencyFullName ) ) {
return false ;
}
}
return true ;
}
@ -137,57 +166,76 @@ namespace ICSharpCode.WpfDesign
@@ -137,57 +166,76 @@ namespace ICSharpCode.WpfDesign
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 )
{
lock ( popularProperties ) {
popularProperties . Add ( p . GetFullName ( ) ) ;
}
lock ( popularProperties ) {
popularProperties . Add ( p . GetFullName ( ) ) ;
}
}
/// <summary>
/// Registers a popular property (shown first in the property grid).
/// </summary>
public static void AddPopularProperty ( Type type , string member )
{
lock ( popularProperties ) {
popularProperties . Add ( type . FullName + "." + member ) ;
}
lock ( popularProperties ) {
popularProperties . Add ( type . FullName + "." + member ) ;
}
}
/// <summary>
/// Gets whether the specified property was registered as popular.
/// </summary>
public static bool IsPopularProperty ( DesignItemProperty p )
{
lock ( popularProperties ) {
if ( popularProperties . Contains ( p . DependencyFullName ) ) {
return true ;
}
}
return false ;
lock ( popularProperties ) {
if ( popularProperties . Contains ( p . DependencyFullName ) ) {
return true ;
}
}
return false ;
}
static HashSet < Type > popularControls = new HashSet < Type > ( ) ;
/// <summary>
/// Registers a popular control (visible in the default toolbox).
/// </summary>
public static void AddPopularControl ( Type t )
{
lock ( popularControls ) {
popularControls . Add ( t ) ;
}
lock ( popularControls ) {
popularControls . Add ( t ) ;
}
}
/// <summary>
/// Gets the list of popular controls.
/// </summary>
public static IEnumerable < Type > GetPopularControls ( )
{
lock ( popularControls ) {
foreach ( var t in popularControls ) {
yield return t ;
}
}
lock ( popularControls ) {
return popularControls . ToArray ( ) ;
}
}
/// <summary>
/// Gets whether the specified control was registered as popular.
/// </summary>
public static bool IsPopularControl ( Type t )
{
lock ( popularControls ) {
return popularControls . Contains ( t ) ;
}
lock ( popularControls ) {
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 )
{
lock ( ranges ) {
@ -195,6 +243,9 @@ namespace ICSharpCode.WpfDesign
@@ -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 )
{
NumberRange r ;
@ -208,6 +259,9 @@ namespace ICSharpCode.WpfDesign
@@ -208,6 +259,9 @@ namespace ICSharpCode.WpfDesign
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 )
{
lock ( placementDisabled ) {
@ -215,6 +269,9 @@ namespace ICSharpCode.WpfDesign
@@ -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 )
{
lock ( placementDisabled ) {
@ -222,33 +279,50 @@ namespace ICSharpCode.WpfDesign
@@ -222,33 +279,50 @@ namespace ICSharpCode.WpfDesign
}
}
static Dictionary < Type , Size > defaultSizes = new Dictionary < Type , Size > ( ) ;
public static void AddDefaultSize ( Type t , Size s )
{
lock ( defaultSizes ) {
defaultSizes [ t ] = s ;
}
}
public static Size GetDefaultSize ( Type t )
{
Size s ;
lock ( defaultSizes ) {
while ( t ! = null ) {
if ( defaultSizes . TryGetValue ( t , out s ) ) {
return s ;
}
t = t . BaseType ;
}
}
return new Size ( double . NaN , double . NaN ) ;
}
static Dictionary < Type , Size > defaultSizes = new Dictionary < Type , Size > ( ) ;
/// <summary>
/// Registers a default size for new controls of the specified type.
/// </summary>
public static void AddDefaultSize ( Type t , Size s )
{
lock ( defaultSizes ) {
defaultSizes [ t ] = s ;
}
}
/// <summary>
/// Gets the default size for new controls of the specified type,
/// or new Size(double.NaN, double.NaN) if no default size was registered.
/// </summary>
public static Size GetDefaultSize ( Type t )
{
Size s ;
lock ( defaultSizes ) {
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 double Min ;
public double Max ;
/// <summary>
/// Gets/Sets the minimum value.
/// </summary>
public double Min { get ; set ; }
/// <summary>
/// Gets/Sets the maximum value.
/// </summary>
public double Max { get ; set ; }
}
}