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 / RepeatButton.cs / 1 / RepeatButton.cs
//----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//---------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Windows.Threading;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using MS.Win32;
using MS.Utility;
namespace System.Windows.Controls.Primitives
{
///
/// RepeatButton control adds repeating semantics of when the Click event occurs
///
public class RepeatButton : ButtonBase
{
#region Constructors
static RepeatButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(RepeatButton), new FrameworkPropertyMetadata(typeof(RepeatButton)));
_dType = DependencyObjectType.FromSystemTypeInternal(typeof(RepeatButton));
ClickModeProperty.OverrideMetadata(typeof(RepeatButton), new FrameworkPropertyMetadata(ClickMode.Press));
}
///
/// Default RepeatButton constructor
///
///
/// Automatic determination of current Dispatcher. Use alternative constructor
/// that accepts a Dispatcher for best performance.
///
public RepeatButton() : base()
{
}
#endregion
#region Dependencies and Events
///
/// The Property for the Delay property.
/// Flags: Can be used in style rules
/// Default Value: Depend on SPI_GETKEYBOARDDELAY from SystemMetrics
///
public static readonly DependencyProperty DelayProperty
= DependencyProperty.Register("Delay", typeof(int), typeof(RepeatButton),
new FrameworkPropertyMetadata(GetKeyboardDelay()),
new ValidateValueCallback(IsDelayValid));
///
/// Specifies the amount of time, in milliseconds, to wait before repeating begins.
/// Must be non-negative
///
[Bindable(true), Category("Behavior")]
public int Delay
{
get
{
return (int)GetValue(DelayProperty);
}
set
{
SetValue(DelayProperty, value);
}
}
///
/// The Property for the Interval property.
/// Flags: Can be used in style rules
/// Default Value: Depend on SPI_GETKEYBOARDSPEED from SystemMetrics
///
public static readonly DependencyProperty IntervalProperty
= DependencyProperty.Register("Interval", typeof(int), typeof(RepeatButton),
new FrameworkPropertyMetadata(GetKeyboardSpeed()),
new ValidateValueCallback(IsIntervalValid));
///
/// Specifies the amount of time, in milliseconds, between repeats once repeating starts.
/// Must be non-negative
///
[Bindable(true), Category("Behavior")]
public int Interval
{
get
{
return (int)GetValue(IntervalProperty);
}
set
{
SetValue(IntervalProperty, value);
}
}
#endregion Dependencies and Events
#region Private helpers
private static bool IsDelayValid(object value) { return ((int)value) >= 0; }
private static bool IsIntervalValid(object value) { return ((int)value) > 0; }
///
/// Starts a _timer ticking
///
private void StartTimer()
{
if (_timer == null)
{
_timer = new DispatcherTimer();
_timer.Tick += new EventHandler(OnTimeout);
}
else if (_timer.IsEnabled)
return;
_timer.Interval = TimeSpan.FromMilliseconds(Delay);
_timer.Start();
}
///
/// Stops a _timer that has already started
///
private void StopTimer()
{
if (_timer != null)
{
_timer.Stop();
}
}
///
/// This is the handler for when the repeat _timer expires. All we do
/// is invoke a click.
///
/// Sender of the event
/// Event arguments
private void OnTimeout(object sender, EventArgs e)
{
TimeSpan interval = TimeSpan.FromMilliseconds(Interval);
if (_timer.Interval != interval)
_timer.Interval = interval;
if (IsPressed)
{
OnClick();
}
}
///
/// Retrieves the keyboard repeat-delay setting, which is a value in the range from 0
/// (approximately 250 ms delay) through 3 (approximately 1 second delay).
/// The actual delay associated with each value may vary depending on the hardware.
///
///
internal static int GetKeyboardDelay()
{
int delay = SystemParameters.KeyboardDelay;
// SPI_GETKEYBOARDDELAY 0,1,2,3 correspond to 250,500,750,1000ms
if (delay < 0 || delay > 3)
delay = 0;
return (delay + 1) * 250;
}
///
/// Retrieves the keyboard repeat-speed setting, which is a value in the range from 0
/// (approximately 2.5 repetitions per second) through 31 (approximately 30 repetitions per second).
/// The actual repeat rates are hardware-dependent and may vary from a linear scale by as much as 20%
///
///
internal static int GetKeyboardSpeed()
{
int speed = SystemParameters.KeyboardSpeed;
// SPI_GETKEYBOARDSPEED 0,...,31 correspond to 1000/2.5=400,...,1000/30 ms
if (speed < 0 || speed > 31)
speed = 31;
return (31 - speed) * (400 - 1000/30) / 31 + 1000/30;
}
#endregion Private helpers
#region Override methods
///
/// Creates AutomationPeer ( )
///
protected override AutomationPeer OnCreateAutomationPeer()
{
return new RepeatButtonAutomationPeer(this);
}
///
/// Raises InvokedAutomationEvent and call the base method to raise the Click event
///
///
protected override void OnClick()
{
if (AutomationPeer.ListenerExists(AutomationEvents.InvokePatternOnInvoked))
{
AutomationPeer peer = UIElementAutomationPeer.CreatePeerForElement(this);
if (peer != null)
peer.RaiseAutomationEvent(AutomationEvents.InvokePatternOnInvoked);
}
base.OnClick();
}
///
/// This is the method that responds to the MouseButtonEvent event.
///
///
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
if (IsPressed && (ClickMode != ClickMode.Hover))
{
StartTimer();
}
}
///
/// This is the method that responds to the MouseButtonEvent event.
///
///
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonUp(e);
if (ClickMode != ClickMode.Hover)
{
StopTimer();
}
}
///
/// Called when this element loses mouse capture.
///
///
protected override void OnLostMouseCapture(MouseEventArgs e)
{
base.OnLostMouseCapture(e);
StopTimer();
}
///
/// An event reporting the mouse entered this element.
///
/// Event arguments
protected override void OnMouseEnter(MouseEventArgs e)
{
base.OnMouseEnter(e);
if (HandleIsMouseOverChanged())
{
e.Handled = true;
}
}
///
/// An event reporting the mouse left this element.
///
/// Event arguments
protected override void OnMouseLeave(MouseEventArgs e)
{
base.OnMouseLeave(e);
if (HandleIsMouseOverChanged())
{
e.Handled = true;
}
}
///
/// An event reporting that the IsMouseOver property changed.
///
private bool HandleIsMouseOverChanged()
{
if (ClickMode == ClickMode.Hover)
{
if (IsMouseOver)
{
StartTimer();
}
else
{
StopTimer();
}
return true;
}
return false;
}
///
/// This is the method that responds to the KeyDown event.
///
///
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if ((e.Key == Key.Space) && (ClickMode != ClickMode.Hover))
{
StartTimer();
}
}
///
/// This is the method that responds to the KeyUp event.
///
///
protected override void OnKeyUp(KeyEventArgs e)
{
if ((e.Key == Key.Space) && (ClickMode != ClickMode.Hover))
{
StopTimer();
}
base.OnKeyUp(e);
}
//
// This property
// 1. Finds the correct initial size for the _effectiveValues store on the current DependencyObject
// 2. This is a performance optimization
//
internal override int EffectiveValuesInitialSize
{
get { return 28; }
}
#endregion
#region Data
private DispatcherTimer _timer;
#endregion
#region DTypeThemeStyleKey
// Returns the DependencyObjectType for the registered ThemeStyleKey's default
// value. Controls will override this method to return approriate types.
internal override DependencyObjectType DTypeThemeStyleKey
{
get { return _dType; }
}
private static DependencyObjectType _dType;
#endregion DTypeThemeStyleKey
}
}
// 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.Windows.Threading;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using MS.Win32;
using MS.Utility;
namespace System.Windows.Controls.Primitives
{
///
/// RepeatButton control adds repeating semantics of when the Click event occurs
///
public class RepeatButton : ButtonBase
{
#region Constructors
static RepeatButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(RepeatButton), new FrameworkPropertyMetadata(typeof(RepeatButton)));
_dType = DependencyObjectType.FromSystemTypeInternal(typeof(RepeatButton));
ClickModeProperty.OverrideMetadata(typeof(RepeatButton), new FrameworkPropertyMetadata(ClickMode.Press));
}
///
/// Default RepeatButton constructor
///
///
/// Automatic determination of current Dispatcher. Use alternative constructor
/// that accepts a Dispatcher for best performance.
///
public RepeatButton() : base()
{
}
#endregion
#region Dependencies and Events
///
/// The Property for the Delay property.
/// Flags: Can be used in style rules
/// Default Value: Depend on SPI_GETKEYBOARDDELAY from SystemMetrics
///
public static readonly DependencyProperty DelayProperty
= DependencyProperty.Register("Delay", typeof(int), typeof(RepeatButton),
new FrameworkPropertyMetadata(GetKeyboardDelay()),
new ValidateValueCallback(IsDelayValid));
///
/// Specifies the amount of time, in milliseconds, to wait before repeating begins.
/// Must be non-negative
///
[Bindable(true), Category("Behavior")]
public int Delay
{
get
{
return (int)GetValue(DelayProperty);
}
set
{
SetValue(DelayProperty, value);
}
}
///
/// The Property for the Interval property.
/// Flags: Can be used in style rules
/// Default Value: Depend on SPI_GETKEYBOARDSPEED from SystemMetrics
///
public static readonly DependencyProperty IntervalProperty
= DependencyProperty.Register("Interval", typeof(int), typeof(RepeatButton),
new FrameworkPropertyMetadata(GetKeyboardSpeed()),
new ValidateValueCallback(IsIntervalValid));
///
/// Specifies the amount of time, in milliseconds, between repeats once repeating starts.
/// Must be non-negative
///
[Bindable(true), Category("Behavior")]
public int Interval
{
get
{
return (int)GetValue(IntervalProperty);
}
set
{
SetValue(IntervalProperty, value);
}
}
#endregion Dependencies and Events
#region Private helpers
private static bool IsDelayValid(object value) { return ((int)value) >= 0; }
private static bool IsIntervalValid(object value) { return ((int)value) > 0; }
///
/// Starts a _timer ticking
///
private void StartTimer()
{
if (_timer == null)
{
_timer = new DispatcherTimer();
_timer.Tick += new EventHandler(OnTimeout);
}
else if (_timer.IsEnabled)
return;
_timer.Interval = TimeSpan.FromMilliseconds(Delay);
_timer.Start();
}
///
/// Stops a _timer that has already started
///
private void StopTimer()
{
if (_timer != null)
{
_timer.Stop();
}
}
///
/// This is the handler for when the repeat _timer expires. All we do
/// is invoke a click.
///
/// Sender of the event
/// Event arguments
private void OnTimeout(object sender, EventArgs e)
{
TimeSpan interval = TimeSpan.FromMilliseconds(Interval);
if (_timer.Interval != interval)
_timer.Interval = interval;
if (IsPressed)
{
OnClick();
}
}
///
/// Retrieves the keyboard repeat-delay setting, which is a value in the range from 0
/// (approximately 250 ms delay) through 3 (approximately 1 second delay).
/// The actual delay associated with each value may vary depending on the hardware.
///
///
internal static int GetKeyboardDelay()
{
int delay = SystemParameters.KeyboardDelay;
// SPI_GETKEYBOARDDELAY 0,1,2,3 correspond to 250,500,750,1000ms
if (delay < 0 || delay > 3)
delay = 0;
return (delay + 1) * 250;
}
///
/// Retrieves the keyboard repeat-speed setting, which is a value in the range from 0
/// (approximately 2.5 repetitions per second) through 31 (approximately 30 repetitions per second).
/// The actual repeat rates are hardware-dependent and may vary from a linear scale by as much as 20%
///
///
internal static int GetKeyboardSpeed()
{
int speed = SystemParameters.KeyboardSpeed;
// SPI_GETKEYBOARDSPEED 0,...,31 correspond to 1000/2.5=400,...,1000/30 ms
if (speed < 0 || speed > 31)
speed = 31;
return (31 - speed) * (400 - 1000/30) / 31 + 1000/30;
}
#endregion Private helpers
#region Override methods
///
/// Creates AutomationPeer ( )
///
protected override AutomationPeer OnCreateAutomationPeer()
{
return new RepeatButtonAutomationPeer(this);
}
///
/// Raises InvokedAutomationEvent and call the base method to raise the Click event
///
///
protected override void OnClick()
{
if (AutomationPeer.ListenerExists(AutomationEvents.InvokePatternOnInvoked))
{
AutomationPeer peer = UIElementAutomationPeer.CreatePeerForElement(this);
if (peer != null)
peer.RaiseAutomationEvent(AutomationEvents.InvokePatternOnInvoked);
}
base.OnClick();
}
///
/// This is the method that responds to the MouseButtonEvent event.
///
///
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
if (IsPressed && (ClickMode != ClickMode.Hover))
{
StartTimer();
}
}
///
/// This is the method that responds to the MouseButtonEvent event.
///
///
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonUp(e);
if (ClickMode != ClickMode.Hover)
{
StopTimer();
}
}
///
/// Called when this element loses mouse capture.
///
///
protected override void OnLostMouseCapture(MouseEventArgs e)
{
base.OnLostMouseCapture(e);
StopTimer();
}
///
/// An event reporting the mouse entered this element.
///
/// Event arguments
protected override void OnMouseEnter(MouseEventArgs e)
{
base.OnMouseEnter(e);
if (HandleIsMouseOverChanged())
{
e.Handled = true;
}
}
///
/// An event reporting the mouse left this element.
///
/// Event arguments
protected override void OnMouseLeave(MouseEventArgs e)
{
base.OnMouseLeave(e);
if (HandleIsMouseOverChanged())
{
e.Handled = true;
}
}
///
/// An event reporting that the IsMouseOver property changed.
///
private bool HandleIsMouseOverChanged()
{
if (ClickMode == ClickMode.Hover)
{
if (IsMouseOver)
{
StartTimer();
}
else
{
StopTimer();
}
return true;
}
return false;
}
///
/// This is the method that responds to the KeyDown event.
///
///
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if ((e.Key == Key.Space) && (ClickMode != ClickMode.Hover))
{
StartTimer();
}
}
///
/// This is the method that responds to the KeyUp event.
///
///
protected override void OnKeyUp(KeyEventArgs e)
{
if ((e.Key == Key.Space) && (ClickMode != ClickMode.Hover))
{
StopTimer();
}
base.OnKeyUp(e);
}
//
// This property
// 1. Finds the correct initial size for the _effectiveValues store on the current DependencyObject
// 2. This is a performance optimization
//
internal override int EffectiveValuesInitialSize
{
get { return 28; }
}
#endregion
#region Data
private DispatcherTimer _timer;
#endregion
#region DTypeThemeStyleKey
// Returns the DependencyObjectType for the registered ThemeStyleKey's default
// value. Controls will override this method to return approriate types.
internal override DependencyObjectType DTypeThemeStyleKey
{
get { return _dType; }
}
private static DependencyObjectType _dType;
#endregion DTypeThemeStyleKey
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- CapabilitiesUse.cs
- BamlTreeNode.cs
- SizeKeyFrameCollection.cs
- DataGridViewComboBoxEditingControl.cs
- EventToken.cs
- PieceNameHelper.cs
- SQLInt64.cs
- DataListItemEventArgs.cs
- StylusTouchDevice.cs
- BitmapEffectState.cs
- SqlParameter.cs
- SafePEFileHandle.cs
- HttpProfileBase.cs
- SqlConnectionHelper.cs
- Point4D.cs
- XmlSchemaComplexContentRestriction.cs
- QuaternionRotation3D.cs
- DataColumnMapping.cs
- DeclaredTypeElementCollection.cs
- WindowsIdentity.cs
- Label.cs
- EdgeModeValidation.cs
- StylusPoint.cs
- DesignerSerializationOptionsAttribute.cs
- TargetException.cs
- SqlTypeConverter.cs
- SaveFileDialog.cs
- PenContext.cs
- ColumnHeader.cs
- PolicyException.cs
- DesignerDataTableBase.cs
- BitmapSource.cs
- AffineTransform3D.cs
- StaticSiteMapProvider.cs
- SrgsDocument.cs
- OutgoingWebRequestContext.cs
- PassportAuthentication.cs
- CommandHelpers.cs
- nulltextcontainer.cs
- ToolStripLocationCancelEventArgs.cs
- EmbeddedMailObjectsCollection.cs
- TrackingMemoryStream.cs
- HtmlTextBoxAdapter.cs
- XmlProcessingInstruction.cs
- TracedNativeMethods.cs
- TypeUtils.cs
- smtppermission.cs
- SymbolDocumentInfo.cs
- NativeMethods.cs
- StrokeNodeData.cs
- autovalidator.cs
- Profiler.cs
- FileSystemEventArgs.cs
- filewebrequest.cs
- MD5CryptoServiceProvider.cs
- XmlException.cs
- XPathNodeList.cs
- OdbcConnectionString.cs
- RowToFieldTransformer.cs
- ConditionBrowserDialog.cs
- PerformanceCounterPermission.cs
- ColorMap.cs
- VisualStates.cs
- ConditionChanges.cs
- AudioLevelUpdatedEventArgs.cs
- UpdateExpressionVisitor.cs
- StringReader.cs
- WebPartMovingEventArgs.cs
- GraphicsContainer.cs
- TextTrailingWordEllipsis.cs
- DataListItemEventArgs.cs
- MissingMethodException.cs
- AnnouncementEventArgs.cs
- PrintController.cs
- IconConverter.cs
- HttpContext.cs
- Propagator.JoinPropagator.JoinPredicateVisitor.cs
- DataBindEngine.cs
- BidOverLoads.cs
- DataGridViewAutoSizeColumnsModeEventArgs.cs
- HtmlShimManager.cs
- SchemaTableOptionalColumn.cs
- KernelTypeValidation.cs
- QuaternionAnimationBase.cs
- ConstructorExpr.cs
- CompareValidator.cs
- PersonalizationAdministration.cs
- SourceSwitch.cs
- GZipStream.cs
- KeyValueSerializer.cs
- MissingFieldException.cs
- Funcletizer.cs
- ScriptResourceMapping.cs
- CapabilitiesPattern.cs
- EntityDataSourceQueryBuilder.cs
- ThemeableAttribute.cs
- HtmlInputImage.cs
- PropagatorResult.cs
- TrackingDataItemValue.cs
- ContentPlaceHolder.cs