Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Core / CSharp / System / Windows / Ink / GestureRecognizer.cs / 1305600 / GestureRecognizer.cs
//----------------------------------------------------------------------------
//
// File: GestureRecognizer.cs
//
// Description:
// The implementation of GestureRecognizer class
//
// Features:
//
// History:
// 01/14/2005 waynezen: Created
//
// Copyright (C) 2001 by Microsoft Corporation. All rights reserved.
//
//---------------------------------------------------------------------------
using MS.Utility;
using MS.Internal.Ink.GestureRecognition;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using System;
using System.Security;
using System.Security.Permissions;
using SecurityHelper=MS.Internal.SecurityHelper;
using SR=MS.Internal.PresentationCore.SR;
using SRID=MS.Internal.PresentationCore.SRID;
namespace System.Windows.Ink
{
///
/// Performs gesture recognition on a StrokeCollection
///
///
/// No finalizer is defined because all unmanaged resources are wrapped in SafeHandle's
/// NOTE: this class provides the public APIs that call into unmanaged code to perform
/// recognition. There are two inputs that are accepted:
/// ApplicationGesture[] and StrokeCollection.
/// This class verifies the ApplicationGesture[] and StrokeCollection / Stroke are validated.
///
public sealed class GestureRecognizer : DependencyObject, IDisposable
{
//-------------------------------------------------------------------------------
//
// Constructors
//
//-------------------------------------------------------------------------------
#region Constructors
///
/// The default constructor which enables all the application gestures.
///
public GestureRecognizer() : this ( new ApplicationGesture[] { ApplicationGesture.AllGestures } )
{
}
///
/// The constructor which take an array of the enabled application gestures.
///
///
public GestureRecognizer(IEnumerable enabledApplicationGestures)
{
_nativeRecognizer = NativeRecognizer.CreateInstance();
if (_nativeRecognizer == null)
{
//just verify the gestures
NativeRecognizer.GetApplicationGestureArrayAndVerify(enabledApplicationGestures);
}
else
{
//
// we only set this if _nativeRecognizer is non null
// (available) because there is no way to use this state
// otherwise.
//
SetEnabledGestures(enabledApplicationGestures);
}
}
#endregion Constructors
//--------------------------------------------------------------------------------
//
// Public Methods
//
//-------------------------------------------------------------------------------
#region Public Methods
///
/// Set the enabled gestures
///
///
///
/// We wrap the System.Runtime.InteropServices.COMException with a more specific one
///
/// Do wrap specific exceptions in a more descriptive exception when appropriate.
/// public Int32 GetInt(Int32[] array, Int32 index){
/// try{
/// return array[index];
/// }catch(IndexOutOfRangeException e){
/// throw new ArgumentOutOfRangeException(
/// Parameter index is out of range.);
/// }
/// }
///
///
public void SetEnabledGestures(IEnumerable applicationGestures)
{
VerifyAccess();
VerifyDisposed();
VerifyRecognizerAvailable();
//don't verfify the gestures, NativeRecognizer.SetEnabledGestures does it
// since it is the TAS boundary
//
// we don't wrap the COM exceptions generated from the Recognizer
// with our own exception
//
ApplicationGesture[] enabledGestures =
_nativeRecognizer.SetEnabledGestures(applicationGestures);
//only update the state when SetEnabledGestures succeeds (since it verifies the array)
_enabledGestures = enabledGestures;
}
///
/// Get the enabled gestures
///
///
public ReadOnlyCollection GetEnabledGestures()
{
VerifyAccess();
VerifyDisposed();
VerifyRecognizerAvailable();
//can be null if the call to SetEnabledGestures failed
if (_enabledGestures == null)
{
_enabledGestures = new ApplicationGesture[] { };
}
return new ReadOnlyCollection(_enabledGestures);
}
///
/// Performs gesture recognition on the StrokeCollection if a gesture recognizer
/// is present and installed on the system. If not, this method throws an InvalidOperationException.
/// To determine if this method will throw an exception, only call this method if
/// the RecognizerAvailable property returns true.
///
///
/// The StrokeCollection to perform gesture recognition on
///
/// Callers must have UnmanagedCode permission to call this API.
///
/// Critical: Calls SecurityCritical method RecognizeImpl
///
/// PublicOK: We demand UnmanagedCode before proceeding and
/// UnmanagedCode is the only permission we assert.
///
[SecurityCritical]
public ReadOnlyCollection Recognize(StrokeCollection strokes)
{
//
// due to possible exploits in the Tablet PC Gesture recognizer's Recognize method,
// we demand unmanaged code.
//
SecurityHelper.DemandUnmanagedCode();
return RecognizeImpl(strokes);
}
///
/// Performs gesture recognition on the StrokeCollection if a gesture recognizer
/// is present and installed on the system. If not, this method throws an InvalidOperationException.
/// To determine if this method will throw an exception, only call this method if
/// the RecognizerAvailable property returns true.
///
/// The StrokeCollection to perform gesture recognition on
///
///
/// Critical: Calls a SecurityCritical method RecognizeImpl.
///
/// FriendAccess is allowed so the critical method InkCanvas.RaiseGestureOrStrokeCollected
/// can use this method
///
// Built into Core, also used by Framework.
[SecurityCritical]
internal ReadOnlyCollection CriticalRecognize(StrokeCollection strokes)
{
return RecognizeImpl(strokes);
}
///
/// Performs gesture recognition on the StrokeCollection if a gesture recognizer
/// is present and installed on the system.
///
/// The StrokeCollection to perform gesture recognition on
///
///
/// Critical: Calls SecurityCritical method NativeRecognizer.Recognize
///
[SecurityCritical]
private ReadOnlyCollection RecognizeImpl(StrokeCollection strokes)
{
if (strokes == null)
{
throw new ArgumentNullException("strokes"); // Null is not allowed as the argument value
}
if (strokes.Count > 2)
{
throw new ArgumentException(SR.Get(SRID.StrokeCollectionCountTooBig), "strokes");
}
VerifyAccess();
VerifyDisposed();
VerifyRecognizerAvailable();
return new ReadOnlyCollection(_nativeRecognizer.Recognize(strokes));
}
#endregion Public Methods
//--------------------------------------------------------------------------------
//
// Public Properties
//
//--------------------------------------------------------------------------------
#region Public Properties
///
/// Indicates if a GestureRecognizer is present and installed on the system.
///
public bool IsRecognizerAvailable
{
get
{
VerifyAccess();
VerifyDisposed();
if (_nativeRecognizer == null)
{
return false;
}
return true;
}
}
#endregion Public Properties
//-------------------------------------------------------------------------------
//
// IDisposable
//
//--------------------------------------------------------------------------------
#region IDisposable
///
/// Dispose this GestureRecognizer
///
public void Dispose()
{
VerifyAccess();
// A simple pattern of the dispose implementation.
// There is no finalizer since the SafeHandle in the NativeRecognizer will release the context properly.
if ( _disposed )
{
return;
}
// NTRAID-WINDOWSBUG#1102945-WAYNEZEN,
// Since the constructor might create a null _nativeRecognizer,
// here we have to make sure we do have some thing to dispose.
// Otherwise just no-op.
if ( _nativeRecognizer != null )
{
_nativeRecognizer.Dispose();
_nativeRecognizer = null;
}
_disposed = true;
}
#endregion IDisposable
//-------------------------------------------------------------------------------
//
// Private Methods
//
//-------------------------------------------------------------------------------
#region Private Methods
//verify that there is a recognizer available, throw if not
private void VerifyRecognizerAvailable()
{
if (_nativeRecognizer == null)
{
throw new InvalidOperationException(SR.Get(SRID.GestureRecognizerNotAvailable));
}
}
// Verify whether this object has been disposed.
private void VerifyDisposed()
{
if ( _disposed )
{
throw new ObjectDisposedException("GestureRecognizer");
}
}
#endregion Private Methods
//-------------------------------------------------------------------------------
//
// Private Fields
//
//--------------------------------------------------------------------------------
#region Private Fields
private ApplicationGesture[] _enabledGestures;
private NativeRecognizer _nativeRecognizer;
private bool _disposed;
#endregion Private Fields
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
//
// File: GestureRecognizer.cs
//
// Description:
// The implementation of GestureRecognizer class
//
// Features:
//
// History:
// 01/14/2005 waynezen: Created
//
// Copyright (C) 2001 by Microsoft Corporation. All rights reserved.
//
//---------------------------------------------------------------------------
using MS.Utility;
using MS.Internal.Ink.GestureRecognition;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using System;
using System.Security;
using System.Security.Permissions;
using SecurityHelper=MS.Internal.SecurityHelper;
using SR=MS.Internal.PresentationCore.SR;
using SRID=MS.Internal.PresentationCore.SRID;
namespace System.Windows.Ink
{
///
/// Performs gesture recognition on a StrokeCollection
///
///
/// No finalizer is defined because all unmanaged resources are wrapped in SafeHandle's
/// NOTE: this class provides the public APIs that call into unmanaged code to perform
/// recognition. There are two inputs that are accepted:
/// ApplicationGesture[] and StrokeCollection.
/// This class verifies the ApplicationGesture[] and StrokeCollection / Stroke are validated.
///
public sealed class GestureRecognizer : DependencyObject, IDisposable
{
//-------------------------------------------------------------------------------
//
// Constructors
//
//-------------------------------------------------------------------------------
#region Constructors
///
/// The default constructor which enables all the application gestures.
///
public GestureRecognizer() : this ( new ApplicationGesture[] { ApplicationGesture.AllGestures } )
{
}
///
/// The constructor which take an array of the enabled application gestures.
///
///
public GestureRecognizer(IEnumerable enabledApplicationGestures)
{
_nativeRecognizer = NativeRecognizer.CreateInstance();
if (_nativeRecognizer == null)
{
//just verify the gestures
NativeRecognizer.GetApplicationGestureArrayAndVerify(enabledApplicationGestures);
}
else
{
//
// we only set this if _nativeRecognizer is non null
// (available) because there is no way to use this state
// otherwise.
//
SetEnabledGestures(enabledApplicationGestures);
}
}
#endregion Constructors
//--------------------------------------------------------------------------------
//
// Public Methods
//
//-------------------------------------------------------------------------------
#region Public Methods
///
/// Set the enabled gestures
///
///
///
/// We wrap the System.Runtime.InteropServices.COMException with a more specific one
///
/// Do wrap specific exceptions in a more descriptive exception when appropriate.
/// public Int32 GetInt(Int32[] array, Int32 index){
/// try{
/// return array[index];
/// }catch(IndexOutOfRangeException e){
/// throw new ArgumentOutOfRangeException(
/// Parameter index is out of range.);
/// }
/// }
///
///
public void SetEnabledGestures(IEnumerable applicationGestures)
{
VerifyAccess();
VerifyDisposed();
VerifyRecognizerAvailable();
//don't verfify the gestures, NativeRecognizer.SetEnabledGestures does it
// since it is the TAS boundary
//
// we don't wrap the COM exceptions generated from the Recognizer
// with our own exception
//
ApplicationGesture[] enabledGestures =
_nativeRecognizer.SetEnabledGestures(applicationGestures);
//only update the state when SetEnabledGestures succeeds (since it verifies the array)
_enabledGestures = enabledGestures;
}
///
/// Get the enabled gestures
///
///
public ReadOnlyCollection GetEnabledGestures()
{
VerifyAccess();
VerifyDisposed();
VerifyRecognizerAvailable();
//can be null if the call to SetEnabledGestures failed
if (_enabledGestures == null)
{
_enabledGestures = new ApplicationGesture[] { };
}
return new ReadOnlyCollection(_enabledGestures);
}
///
/// Performs gesture recognition on the StrokeCollection if a gesture recognizer
/// is present and installed on the system. If not, this method throws an InvalidOperationException.
/// To determine if this method will throw an exception, only call this method if
/// the RecognizerAvailable property returns true.
///
///
/// The StrokeCollection to perform gesture recognition on
///
/// Callers must have UnmanagedCode permission to call this API.
///
/// Critical: Calls SecurityCritical method RecognizeImpl
///
/// PublicOK: We demand UnmanagedCode before proceeding and
/// UnmanagedCode is the only permission we assert.
///
[SecurityCritical]
public ReadOnlyCollection Recognize(StrokeCollection strokes)
{
//
// due to possible exploits in the Tablet PC Gesture recognizer's Recognize method,
// we demand unmanaged code.
//
SecurityHelper.DemandUnmanagedCode();
return RecognizeImpl(strokes);
}
///
/// Performs gesture recognition on the StrokeCollection if a gesture recognizer
/// is present and installed on the system. If not, this method throws an InvalidOperationException.
/// To determine if this method will throw an exception, only call this method if
/// the RecognizerAvailable property returns true.
///
/// The StrokeCollection to perform gesture recognition on
///
///
/// Critical: Calls a SecurityCritical method RecognizeImpl.
///
/// FriendAccess is allowed so the critical method InkCanvas.RaiseGestureOrStrokeCollected
/// can use this method
///
// Built into Core, also used by Framework.
[SecurityCritical]
internal ReadOnlyCollection CriticalRecognize(StrokeCollection strokes)
{
return RecognizeImpl(strokes);
}
///
/// Performs gesture recognition on the StrokeCollection if a gesture recognizer
/// is present and installed on the system.
///
/// The StrokeCollection to perform gesture recognition on
///
///
/// Critical: Calls SecurityCritical method NativeRecognizer.Recognize
///
[SecurityCritical]
private ReadOnlyCollection RecognizeImpl(StrokeCollection strokes)
{
if (strokes == null)
{
throw new ArgumentNullException("strokes"); // Null is not allowed as the argument value
}
if (strokes.Count > 2)
{
throw new ArgumentException(SR.Get(SRID.StrokeCollectionCountTooBig), "strokes");
}
VerifyAccess();
VerifyDisposed();
VerifyRecognizerAvailable();
return new ReadOnlyCollection(_nativeRecognizer.Recognize(strokes));
}
#endregion Public Methods
//--------------------------------------------------------------------------------
//
// Public Properties
//
//--------------------------------------------------------------------------------
#region Public Properties
///
/// Indicates if a GestureRecognizer is present and installed on the system.
///
public bool IsRecognizerAvailable
{
get
{
VerifyAccess();
VerifyDisposed();
if (_nativeRecognizer == null)
{
return false;
}
return true;
}
}
#endregion Public Properties
//-------------------------------------------------------------------------------
//
// IDisposable
//
//--------------------------------------------------------------------------------
#region IDisposable
///
/// Dispose this GestureRecognizer
///
public void Dispose()
{
VerifyAccess();
// A simple pattern of the dispose implementation.
// There is no finalizer since the SafeHandle in the NativeRecognizer will release the context properly.
if ( _disposed )
{
return;
}
// NTRAID-WINDOWSBUG#1102945-WAYNEZEN,
// Since the constructor might create a null _nativeRecognizer,
// here we have to make sure we do have some thing to dispose.
// Otherwise just no-op.
if ( _nativeRecognizer != null )
{
_nativeRecognizer.Dispose();
_nativeRecognizer = null;
}
_disposed = true;
}
#endregion IDisposable
//-------------------------------------------------------------------------------
//
// Private Methods
//
//-------------------------------------------------------------------------------
#region Private Methods
//verify that there is a recognizer available, throw if not
private void VerifyRecognizerAvailable()
{
if (_nativeRecognizer == null)
{
throw new InvalidOperationException(SR.Get(SRID.GestureRecognizerNotAvailable));
}
}
// Verify whether this object has been disposed.
private void VerifyDisposed()
{
if ( _disposed )
{
throw new ObjectDisposedException("GestureRecognizer");
}
}
#endregion Private Methods
//-------------------------------------------------------------------------------
//
// Private Fields
//
//--------------------------------------------------------------------------------
#region Private Fields
private ApplicationGesture[] _enabledGestures;
private NativeRecognizer _nativeRecognizer;
private bool _disposed;
#endregion Private Fields
}
}
// 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
- CaseInsensitiveComparer.cs
- QueryOpeningEnumerator.cs
- HttpListenerPrefixCollection.cs
- TypeDependencyAttribute.cs
- HtmlInputFile.cs
- ClaimTypes.cs
- SchemaObjectWriter.cs
- OleDbError.cs
- SafeSystemMetrics.cs
- ProcessThreadCollection.cs
- PenCursorManager.cs
- Listbox.cs
- TraceUtils.cs
- DataContractSerializer.cs
- Page.cs
- ArithmeticException.cs
- InvokeMethodDesigner.xaml.cs
- EventLog.cs
- DelegatedStream.cs
- Comparer.cs
- RegistryKey.cs
- ToolStripSplitStackLayout.cs
- EncoderExceptionFallback.cs
- PreApplicationStartMethodAttribute.cs
- EditingMode.cs
- WindowsScrollBarBits.cs
- XPathNodeInfoAtom.cs
- mda.cs
- columnmapfactory.cs
- ObjectPersistData.cs
- WorkflowIdleElement.cs
- Processor.cs
- CallContext.cs
- XmlReflectionMember.cs
- DataGridViewColumnEventArgs.cs
- EllipticalNodeOperations.cs
- WebPartMinimizeVerb.cs
- XmlWrappingReader.cs
- BitmapMetadataEnumerator.cs
- userdatakeys.cs
- DataGridViewColumnConverter.cs
- ObjectDataSourceSelectingEventArgs.cs
- ValueProviderWrapper.cs
- DataSourceView.cs
- PointKeyFrameCollection.cs
- PersonalizableTypeEntry.cs
- StrokeRenderer.cs
- _AutoWebProxyScriptEngine.cs
- PresentationTraceSources.cs
- GetPageCompletedEventArgs.cs
- WebPartZoneAutoFormat.cs
- UserMapPath.cs
- HwndSourceParameters.cs
- HttpRuntime.cs
- QilScopedVisitor.cs
- RegexStringValidatorAttribute.cs
- TypeCacheManager.cs
- milexports.cs
- IOException.cs
- XmlSchemaObjectCollection.cs
- AnnotationComponentChooser.cs
- MenuAutoFormat.cs
- ContainerFilterService.cs
- CapabilitiesUse.cs
- DictionaryEntry.cs
- BackStopAuthenticationModule.cs
- Property.cs
- ListViewHitTestInfo.cs
- SafeNativeMethods.cs
- NativeCppClassAttribute.cs
- HealthMonitoringSection.cs
- PrefixQName.cs
- Nullable.cs
- DrawingGroup.cs
- SubMenuStyle.cs
- EncoderFallback.cs
- DiffuseMaterial.cs
- XmlAnyAttributeAttribute.cs
- EventLogPermission.cs
- SetterBaseCollection.cs
- SupportingTokenBindingElement.cs
- RemotingConfigParser.cs
- SelectionEditor.cs
- BitmapEffectCollection.cs
- SqlClientMetaDataCollectionNames.cs
- ScrollChrome.cs
- RuntimeArgumentHandle.cs
- CustomWebEventKey.cs
- DataGridTableCollection.cs
- PropertyMetadata.cs
- EdmItemCollection.cs
- FlowDocumentView.cs
- HTMLTextWriter.cs
- XmlObjectSerializerContext.cs
- HtmlInputCheckBox.cs
- ContactManager.cs
- XsltContext.cs
- FontStyles.cs
- LicenseManager.cs
- RijndaelCryptoServiceProvider.cs