RangeBase.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Framework / System / Windows / Controls / Primitives / RangeBase.cs / 1 / RangeBase.cs

                            //---------------------------------------------------------------------------- 
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
//--------------------------------------------------------------------------- 

using System; 
using System.Collections; 
using System.ComponentModel;
using System.Globalization; 
using System.Windows.Threading;

using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider; 
using System.Windows.Controls;
using System.Windows.Controls.Primitives; 
using System.Windows; 
using System.Windows.Input;
using System.Windows.Media; 

using MS.Internal;
using MS.Utility;
 
namespace System.Windows.Controls.Primitives
{ 
    ///  
    ///     The RangeBase class is the base class from which all "range-like"
    /// controls derive.  It defines the relevant events and properties, as 
    /// well as providing handlers for the relevant input events.
    /// 
    /// 
    [DefaultEvent("ValueChanged"), DefaultProperty("Value")] 
    public abstract class RangeBase : Control
    { 
        #region Constructors 

        ///  
        ///     Default RangeBase constructor
        /// 
        /// 
        ///     Automatic determination of current Dispatcher. Use alternative constructor 
        ///     that accepts a Dispatcher for best performance.
        ///  
        protected RangeBase() 
        {
        } 

        #endregion Constructors

        #region Events 
        /// 
        /// Event correspond to Value changed event 
        ///  
        public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent("ValueChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler), typeof(RangeBase));
 
        /// 
        /// Add / Remove ValueChangedEvent handler
        /// 
        [Category("Behavior")] 
        public event RoutedPropertyChangedEventHandler ValueChanged { add { AddHandler(ValueChangedEvent, value); } remove { RemoveHandler(ValueChangedEvent, value); } }
        #endregion Events 
 
        #region Properties
        ///  
        ///     The DependencyProperty for the Minimum property.
        ///     Flags:              none
        ///     Default Value:      0
        ///  
        public static readonly DependencyProperty MinimumProperty =
                DependencyProperty.Register( 
                        "Minimum", 
                        typeof(double),
                        typeof(RangeBase), 
                        new FrameworkPropertyMetadata(
                                0.0d,
                                new PropertyChangedCallback(OnMinimumChanged)),
                        new ValidateValueCallback(IsValidDoubleValue)); 

        ///  
        ///     Minimum restricts the minimum value of the Value property 
        /// 
        [Bindable(true), Category("Behavior")] 
        public double Minimum
        {
            get { return (double) GetValue(MinimumProperty); }
            set { SetValue(MinimumProperty, value); } 
        }
 
        ///  
        ///     Called when MinimumProperty is changed on "d."
        ///  
        private static void OnMinimumChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RangeBase ctrl = (RangeBase) d;
 
            RangeBaseAutomationPeer peer = UIElementAutomationPeer.FromElement(ctrl) as RangeBaseAutomationPeer;
            if (peer != null) 
            { 
                peer.RaiseMinimumPropertyChangedEvent((double)e.OldValue, (double)e.NewValue);
            } 

            ctrl.CoerceValue(MaximumProperty);
            ctrl.CoerceValue(ValueProperty);
            ctrl.OnMinimumChanged((double)e.OldValue, (double)e.NewValue); 
        }
 
        ///  
        ///     This method is invoked when the Minimum property changes.
        ///  
        /// The old value of the Minimum property.
        /// The new value of the Minimum property.
        protected virtual void OnMinimumChanged(double oldMinimum, double newMinimum)
        { 
        }
 
        ///  
        ///     The DependencyProperty for the Maximum property.
        ///     Flags:              none 
        ///     Default Value:      1
        /// 
        public static readonly DependencyProperty MaximumProperty =
                DependencyProperty.Register( 
                        "Maximum",
                        typeof(double), 
                        typeof(RangeBase), 
                        new FrameworkPropertyMetadata(
                                1.0d, 
                                new PropertyChangedCallback(OnMaximumChanged),
                                new CoerceValueCallback(CoerceMaximum)),
                        new ValidateValueCallback(IsValidDoubleValue));
 
        private static object CoerceMaximum(DependencyObject d, object value)
        { 
            RangeBase ctrl = (RangeBase) d; 
            double min = ctrl.Minimum;
            if ((double) value < min) 
            {
                return min;
            }
            return value; 
        }
 
        ///  
        ///     Maximum restricts the maximum value of the Value property
        ///  
        [Bindable(true), Category("Behavior")]
        public double Maximum
        {
            get { return (double) GetValue(MaximumProperty); } 
            set { SetValue(MaximumProperty, value); }
        } 
 
        /// 
        ///     Called when MaximumProperty is changed on "d." 
        /// 
        private static void OnMaximumChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RangeBase ctrl = (RangeBase) d; 

            RangeBaseAutomationPeer peer = UIElementAutomationPeer.FromElement(ctrl) as RangeBaseAutomationPeer; 
            if (peer != null) 
            {
                peer.RaiseMaximumPropertyChangedEvent((double)e.OldValue, (double)e.NewValue); 
            }

            ctrl.CoerceValue(ValueProperty);
            ctrl.OnMaximumChanged((double) e.OldValue, (double) e.NewValue); 
        }
 
        ///  
        ///     This method is invoked when the Maximum property changes.
        ///  
        /// The old value of the Maximum property.
        /// The new value of the Maximum property.
        protected virtual void OnMaximumChanged(double oldMaximum, double newMaximum)
        { 
        }
 
        ///  
        ///     The DependencyProperty for the Value property.
        ///     Flags:              None 
        ///     Default Value:      0
        /// 
        public static readonly DependencyProperty ValueProperty =
                DependencyProperty.Register( 
                        "Value",
                        typeof(double), 
                        typeof(RangeBase), 
                        new FrameworkPropertyMetadata(
                                0.0d, 
                                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal,
                                new PropertyChangedCallback(OnValueChanged),
                                new CoerceValueCallback(ConstrainToRange)),
                        new ValidateValueCallback(IsValidDoubleValue)); 

        // made this internal because Slider wants to leverage it 
        internal static object ConstrainToRange(DependencyObject d, object value) 
        {
            RangeBase ctrl = (RangeBase) d; 
            double min = ctrl.Minimum;
            double v = (double) value;
            if (v < min)
            { 
                return min;
            } 
 
            double max = ctrl.Maximum;
            if (v > max) 
            {
                return max;
            }
 
            return value;
        } 
 
        /// 
        ///     Value property 
        /// 
        [Bindable(true), Category("Behavior")]
        public double Value
        { 
            get { return (double) GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); } 
        } 

        ///  
        ///     Called when ValueID is changed on "d."
        /// 
        private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        { 
            RangeBase ctrl = (RangeBase)d;
 
            RangeBaseAutomationPeer peer = UIElementAutomationPeer.FromElement(ctrl) as RangeBaseAutomationPeer; 
            if (peer != null)
            { 
                peer.RaiseValuePropertyChangedEvent((double)e.OldValue, (double)e.NewValue);
            }

            ctrl.OnValueChanged((double) e.OldValue, (double) e.NewValue); 
        }
 
        ///  
        ///     This method is invoked when the Value property changes.
        ///  
        /// The old value of the Value property.
        /// The new value of the Value property.
        protected virtual void OnValueChanged(double oldValue, double newValue)
        { 
            RoutedPropertyChangedEventArgs args = new RoutedPropertyChangedEventArgs(oldValue, newValue);
            args.RoutedEvent=RangeBase.ValueChangedEvent; 
            RaiseEvent(args); 
        }
 
        /// 
        /// Validate input value in RangeBase (Minimum, Maximum, and Value).
        /// 
        ///  
        /// Returns False if value is NaN or NegativeInfinity or PositiveInfinity. Otherwise, returns True.
        private static bool IsValidDoubleValue(object value) 
        { 
            double d = (double)value;
 
            return !(DoubleUtil.IsNaN(d) || double.IsInfinity(d));
        }

        ///  
        /// Validate input value in RangeBase (SmallChange and LargeChange).
        ///  
        ///  
        /// Returns False if value is NaN or NegativeInfinity or PositiveInfinity or negative. Otherwise, returns True.
        private static bool IsValidChange(object value) 
        {
            double d = (double)value;

            return IsValidDoubleValue(value) && d >= 0.0; 
        }
 
        #region LargeChange Property 
        /// 
        ///     The DependencyProperty for the LargeChange property. 
        /// 
        public static readonly DependencyProperty LargeChangeProperty
            = DependencyProperty.Register("LargeChange", typeof(double), typeof(RangeBase),
                                          new FrameworkPropertyMetadata(1.0), 
                                          new ValidateValueCallback(IsValidChange));
 
        ///  
        ///     LargeChange property
        ///  
        [Bindable(true), Category("Behavior")]
        public double LargeChange
        {
            get 
            {
                return (double)GetValue(LargeChangeProperty); 
            } 
            set
            { 
                SetValue(LargeChangeProperty, value);
            }
        }
 
        #endregion
 
        #region SmallChange Property 
        /// 
        ///     The DependencyProperty for the SmallChange property. 
        /// 
        public static readonly DependencyProperty SmallChangeProperty
            = DependencyProperty.Register("SmallChange", typeof(double), typeof(RangeBase),
                                          new FrameworkPropertyMetadata(0.1), 
                                          new ValidateValueCallback(IsValidChange));
 
        ///  
        ///     SmallChange property
        ///  
        [Bindable(true), Category("Behavior")]
        public double SmallChange
        {
            get 
            {
                return (double)GetValue(SmallChangeProperty); 
            } 
            set
            { 
                SetValue(SmallChangeProperty, value);
            }
        }
 
        #endregion
 
        #endregion 

        #region Method Overrides 

        /// 
        ///     Gives a string representation of this object.
        ///  
        public override string ToString()
        { 
            string typeText = this.GetType().ToString(); 
            double min = double.NaN;
            double max = double.NaN; 
            double val = double.NaN;
            bool valuesDefined = false;

            // Accessing RangeBase properties may be thread sensitive 
            if (CheckAccess())
            { 
                min = Minimum; 
                max = Maximum;
                val = Value; 
                valuesDefined = true;
            }
            else
            { 
                //Not on dispatcher, try posting to the dispatcher with 20ms timeout
                Dispatcher.Invoke(DispatcherPriority.Send, new TimeSpan(0, 0, 0, 0, 20), new DispatcherOperationCallback(delegate(object o) 
                { 
                    min = Minimum;
                    max = Maximum; 
                    val = Value;
                    valuesDefined = true;
                    return null;
                }), null); 
            }
 
            // If min, max, value are defined 
            if (valuesDefined)
            { 
                return SR.Get(SRID.ToStringFormatString_RangeBase, typeText, min, max, val);
            }

            // Not able to access the dispatcher 
            return typeText;
        } 
 
        #endregion
    } 
}



// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//---------------------------------------------------------------------------- 
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
//--------------------------------------------------------------------------- 

using System; 
using System.Collections; 
using System.ComponentModel;
using System.Globalization; 
using System.Windows.Threading;

using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider; 
using System.Windows.Controls;
using System.Windows.Controls.Primitives; 
using System.Windows; 
using System.Windows.Input;
using System.Windows.Media; 

using MS.Internal;
using MS.Utility;
 
namespace System.Windows.Controls.Primitives
{ 
    ///  
    ///     The RangeBase class is the base class from which all "range-like"
    /// controls derive.  It defines the relevant events and properties, as 
    /// well as providing handlers for the relevant input events.
    /// 
    /// 
    [DefaultEvent("ValueChanged"), DefaultProperty("Value")] 
    public abstract class RangeBase : Control
    { 
        #region Constructors 

        ///  
        ///     Default RangeBase constructor
        /// 
        /// 
        ///     Automatic determination of current Dispatcher. Use alternative constructor 
        ///     that accepts a Dispatcher for best performance.
        ///  
        protected RangeBase() 
        {
        } 

        #endregion Constructors

        #region Events 
        /// 
        /// Event correspond to Value changed event 
        ///  
        public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent("ValueChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler), typeof(RangeBase));
 
        /// 
        /// Add / Remove ValueChangedEvent handler
        /// 
        [Category("Behavior")] 
        public event RoutedPropertyChangedEventHandler ValueChanged { add { AddHandler(ValueChangedEvent, value); } remove { RemoveHandler(ValueChangedEvent, value); } }
        #endregion Events 
 
        #region Properties
        ///  
        ///     The DependencyProperty for the Minimum property.
        ///     Flags:              none
        ///     Default Value:      0
        ///  
        public static readonly DependencyProperty MinimumProperty =
                DependencyProperty.Register( 
                        "Minimum", 
                        typeof(double),
                        typeof(RangeBase), 
                        new FrameworkPropertyMetadata(
                                0.0d,
                                new PropertyChangedCallback(OnMinimumChanged)),
                        new ValidateValueCallback(IsValidDoubleValue)); 

        ///  
        ///     Minimum restricts the minimum value of the Value property 
        /// 
        [Bindable(true), Category("Behavior")] 
        public double Minimum
        {
            get { return (double) GetValue(MinimumProperty); }
            set { SetValue(MinimumProperty, value); } 
        }
 
        ///  
        ///     Called when MinimumProperty is changed on "d."
        ///  
        private static void OnMinimumChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RangeBase ctrl = (RangeBase) d;
 
            RangeBaseAutomationPeer peer = UIElementAutomationPeer.FromElement(ctrl) as RangeBaseAutomationPeer;
            if (peer != null) 
            { 
                peer.RaiseMinimumPropertyChangedEvent((double)e.OldValue, (double)e.NewValue);
            } 

            ctrl.CoerceValue(MaximumProperty);
            ctrl.CoerceValue(ValueProperty);
            ctrl.OnMinimumChanged((double)e.OldValue, (double)e.NewValue); 
        }
 
        ///  
        ///     This method is invoked when the Minimum property changes.
        ///  
        /// The old value of the Minimum property.
        /// The new value of the Minimum property.
        protected virtual void OnMinimumChanged(double oldMinimum, double newMinimum)
        { 
        }
 
        ///  
        ///     The DependencyProperty for the Maximum property.
        ///     Flags:              none 
        ///     Default Value:      1
        /// 
        public static readonly DependencyProperty MaximumProperty =
                DependencyProperty.Register( 
                        "Maximum",
                        typeof(double), 
                        typeof(RangeBase), 
                        new FrameworkPropertyMetadata(
                                1.0d, 
                                new PropertyChangedCallback(OnMaximumChanged),
                                new CoerceValueCallback(CoerceMaximum)),
                        new ValidateValueCallback(IsValidDoubleValue));
 
        private static object CoerceMaximum(DependencyObject d, object value)
        { 
            RangeBase ctrl = (RangeBase) d; 
            double min = ctrl.Minimum;
            if ((double) value < min) 
            {
                return min;
            }
            return value; 
        }
 
        ///  
        ///     Maximum restricts the maximum value of the Value property
        ///  
        [Bindable(true), Category("Behavior")]
        public double Maximum
        {
            get { return (double) GetValue(MaximumProperty); } 
            set { SetValue(MaximumProperty, value); }
        } 
 
        /// 
        ///     Called when MaximumProperty is changed on "d." 
        /// 
        private static void OnMaximumChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RangeBase ctrl = (RangeBase) d; 

            RangeBaseAutomationPeer peer = UIElementAutomationPeer.FromElement(ctrl) as RangeBaseAutomationPeer; 
            if (peer != null) 
            {
                peer.RaiseMaximumPropertyChangedEvent((double)e.OldValue, (double)e.NewValue); 
            }

            ctrl.CoerceValue(ValueProperty);
            ctrl.OnMaximumChanged((double) e.OldValue, (double) e.NewValue); 
        }
 
        ///  
        ///     This method is invoked when the Maximum property changes.
        ///  
        /// The old value of the Maximum property.
        /// The new value of the Maximum property.
        protected virtual void OnMaximumChanged(double oldMaximum, double newMaximum)
        { 
        }
 
        ///  
        ///     The DependencyProperty for the Value property.
        ///     Flags:              None 
        ///     Default Value:      0
        /// 
        public static readonly DependencyProperty ValueProperty =
                DependencyProperty.Register( 
                        "Value",
                        typeof(double), 
                        typeof(RangeBase), 
                        new FrameworkPropertyMetadata(
                                0.0d, 
                                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal,
                                new PropertyChangedCallback(OnValueChanged),
                                new CoerceValueCallback(ConstrainToRange)),
                        new ValidateValueCallback(IsValidDoubleValue)); 

        // made this internal because Slider wants to leverage it 
        internal static object ConstrainToRange(DependencyObject d, object value) 
        {
            RangeBase ctrl = (RangeBase) d; 
            double min = ctrl.Minimum;
            double v = (double) value;
            if (v < min)
            { 
                return min;
            } 
 
            double max = ctrl.Maximum;
            if (v > max) 
            {
                return max;
            }
 
            return value;
        } 
 
        /// 
        ///     Value property 
        /// 
        [Bindable(true), Category("Behavior")]
        public double Value
        { 
            get { return (double) GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); } 
        } 

        ///  
        ///     Called when ValueID is changed on "d."
        /// 
        private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        { 
            RangeBase ctrl = (RangeBase)d;
 
            RangeBaseAutomationPeer peer = UIElementAutomationPeer.FromElement(ctrl) as RangeBaseAutomationPeer; 
            if (peer != null)
            { 
                peer.RaiseValuePropertyChangedEvent((double)e.OldValue, (double)e.NewValue);
            }

            ctrl.OnValueChanged((double) e.OldValue, (double) e.NewValue); 
        }
 
        ///  
        ///     This method is invoked when the Value property changes.
        ///  
        /// The old value of the Value property.
        /// The new value of the Value property.
        protected virtual void OnValueChanged(double oldValue, double newValue)
        { 
            RoutedPropertyChangedEventArgs args = new RoutedPropertyChangedEventArgs(oldValue, newValue);
            args.RoutedEvent=RangeBase.ValueChangedEvent; 
            RaiseEvent(args); 
        }
 
        /// 
        /// Validate input value in RangeBase (Minimum, Maximum, and Value).
        /// 
        ///  
        /// Returns False if value is NaN or NegativeInfinity or PositiveInfinity. Otherwise, returns True.
        private static bool IsValidDoubleValue(object value) 
        { 
            double d = (double)value;
 
            return !(DoubleUtil.IsNaN(d) || double.IsInfinity(d));
        }

        ///  
        /// Validate input value in RangeBase (SmallChange and LargeChange).
        ///  
        ///  
        /// Returns False if value is NaN or NegativeInfinity or PositiveInfinity or negative. Otherwise, returns True.
        private static bool IsValidChange(object value) 
        {
            double d = (double)value;

            return IsValidDoubleValue(value) && d >= 0.0; 
        }
 
        #region LargeChange Property 
        /// 
        ///     The DependencyProperty for the LargeChange property. 
        /// 
        public static readonly DependencyProperty LargeChangeProperty
            = DependencyProperty.Register("LargeChange", typeof(double), typeof(RangeBase),
                                          new FrameworkPropertyMetadata(1.0), 
                                          new ValidateValueCallback(IsValidChange));
 
        ///  
        ///     LargeChange property
        ///  
        [Bindable(true), Category("Behavior")]
        public double LargeChange
        {
            get 
            {
                return (double)GetValue(LargeChangeProperty); 
            } 
            set
            { 
                SetValue(LargeChangeProperty, value);
            }
        }
 
        #endregion
 
        #region SmallChange Property 
        /// 
        ///     The DependencyProperty for the SmallChange property. 
        /// 
        public static readonly DependencyProperty SmallChangeProperty
            = DependencyProperty.Register("SmallChange", typeof(double), typeof(RangeBase),
                                          new FrameworkPropertyMetadata(0.1), 
                                          new ValidateValueCallback(IsValidChange));
 
        ///  
        ///     SmallChange property
        ///  
        [Bindable(true), Category("Behavior")]
        public double SmallChange
        {
            get 
            {
                return (double)GetValue(SmallChangeProperty); 
            } 
            set
            { 
                SetValue(SmallChangeProperty, value);
            }
        }
 
        #endregion
 
        #endregion 

        #region Method Overrides 

        /// 
        ///     Gives a string representation of this object.
        ///  
        public override string ToString()
        { 
            string typeText = this.GetType().ToString(); 
            double min = double.NaN;
            double max = double.NaN; 
            double val = double.NaN;
            bool valuesDefined = false;

            // Accessing RangeBase properties may be thread sensitive 
            if (CheckAccess())
            { 
                min = Minimum; 
                max = Maximum;
                val = Value; 
                valuesDefined = true;
            }
            else
            { 
                //Not on dispatcher, try posting to the dispatcher with 20ms timeout
                Dispatcher.Invoke(DispatcherPriority.Send, new TimeSpan(0, 0, 0, 0, 20), new DispatcherOperationCallback(delegate(object o) 
                { 
                    min = Minimum;
                    max = Maximum; 
                    val = Value;
                    valuesDefined = true;
                    return null;
                }), null); 
            }
 
            // If min, max, value are defined 
            if (valuesDefined)
            { 
                return SR.Get(SRID.ToStringFormatString_RangeBase, typeText, min, max, val);
            }

            // Not able to access the dispatcher 
            return typeText;
        } 
 
        #endregion
    } 
}



// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK