Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Core / CSharp / System / Windows / Input / Stylus / StylusTouchDevice.cs / 1305600 / StylusTouchDevice.cs
//---------------------------------------------------------------------------- // // Copyright (C) Microsoft Corporation. All rights reserved. // //--------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Security; using System.Security.Permissions; using MS.Internal; namespace System.Windows.Input { ////// The implementation of a TouchDevice specific to StylusDevice. /// internal sealed class StylusTouchDevice : TouchDevice { #region TouchDevice Implementation ////// Critical: Accesses critical _stylusLogic and InputManager /// TreatAsSafe: This constructor handles critical data but does not expose it /// [SecurityCritical, SecurityTreatAsSafe] internal StylusTouchDevice(StylusDevice stylusDevice) : base(stylusDevice.Id) { _stylusDevice = stylusDevice; _stylusLogic = InputManager.UnsecureCurrent.StylusLogic; PromotingToOther = true; } ////// Provides the current position. /// /// Defines the coordinate space. ///The current position in the coordinate space of relativeTo. public override TouchPoint GetTouchPoint(IInputElement relativeTo) { Point position = _stylusDevice.GetPosition(relativeTo); Rect rectBounds = GetBounds(_stylusDevice.RawStylusPoint, position, relativeTo); return new TouchPoint(this, position, rectBounds, _lastAction); } private Rect GetBounds(StylusPoint stylusPoint, Point position, IInputElement relativeTo) { GeneralTransform elementToRoot; GeneralTransform rootToElement; GetRootTransforms(relativeTo, out elementToRoot, out rootToElement); return GetBounds(stylusPoint, position, relativeTo, elementToRoot, rootToElement); } private Rect GetBounds(StylusPoint stylusPoint, Point position, IInputElement relativeTo, GeneralTransform elementToRoot, GeneralTransform rootToElement) { // Get width and heith in pixel value double width = GetStylusPointWidthOrHeight(stylusPoint, /*isWidth*/ true); double height = GetStylusPointWidthOrHeight(stylusPoint, /*isWidth*/ false); // Get the position with respect to root Point rootPoint; if (elementToRoot == null || !elementToRoot.TryTransform(position, out rootPoint)) { rootPoint = position; } // Create a Rect with respect to root and transform it to element coordinate space Rect rectBounds = new Rect(rootPoint.X - width * 0.5, rootPoint.Y - height * 0.5, width, height); if (rootToElement != null) { rectBounds = rootToElement.TransformBounds(rectBounds); } return rectBounds; } private double GetStylusPointWidthOrHeight(StylusPoint stylusPoint, bool isWidth) { const double CentimeterPerInch = 2.54d; const double PixelPerInch = 96d; StylusPointProperty property = (isWidth ? StylusPointProperties.Width : StylusPointProperties.Height); double value = 0d; if (stylusPoint.HasProperty(property)) { // Get the property value in the corresponding units value = (double)stylusPoint.GetPropertyValue(property); StylusPointPropertyInfo propertyInfo = stylusPoint.Description.GetPropertyInfo(property); if (!DoubleUtil.AreClose(propertyInfo.Resolution, 0d)) { value /= propertyInfo.Resolution; } else { value = 0; } // Convert the value to Inches if (propertyInfo.Unit == StylusPointPropertyUnit.Centimeters) { value /= CentimeterPerInch; } // Convert the value to pixels value *= PixelPerInch; } return value; } ////// Provides all of the known points the device hit since the last reported position update. /// /// Defines the coordinate space. ///A list of points in the coordinate space of relativeTo. public override TouchPointCollection GetIntermediateTouchPoints(IInputElement relativeTo) { // Retrieve the stylus points StylusPointCollection stylusPoints = _stylusDevice.GetStylusPoints(relativeTo, _stylusPointDescription); int count = stylusPoints.Count; TouchPointCollection touchPoints = new TouchPointCollection(); GeneralTransform elementToRoot; GeneralTransform rootToElement; GetRootTransforms(relativeTo, out elementToRoot, out rootToElement); // Convert the stylus points into touch points for (int i = 0; i < count; i++) { StylusPoint stylusPoint = stylusPoints[i]; Point position = new Point(stylusPoint.X, stylusPoint.Y); Rect rectBounds = GetBounds(stylusPoint, position, relativeTo, elementToRoot, rootToElement); TouchPoint touchPoint = new TouchPoint(this, position, rectBounds, _lastAction); touchPoints.Add(touchPoint); } return touchPoints; } ////// Critical - Calls PresentationSource.CriticalFromVisual. /// TreatAsSafe - Does not expose PresentationSource. /// [SecurityCritical, SecurityTreatAsSafe] private void GetRootTransforms(IInputElement relativeTo, out GeneralTransform elementToRoot, out GeneralTransform rootToElement) { elementToRoot = rootToElement = null; DependencyObject containingVisual = InputElement.GetContainingVisual(relativeTo as DependencyObject); if (containingVisual != null) { PresentationSource relativePresentationSource = PresentationSource.CriticalFromVisual(containingVisual); Visual rootVisual = (relativePresentationSource != null) ? relativePresentationSource.RootVisual : null; Visual containingVisual2D = VisualTreeHelper.GetContainingVisual2D(containingVisual); if ((rootVisual != null) && (containingVisual2D != null)) { elementToRoot = containingVisual2D.TransformToAncestor(rootVisual); rootToElement = rootVisual.TransformToDescendant(containingVisual2D); } } } #endregion #region StylusLogic Related protected override void OnManipulationStarted() { base.OnManipulationStarted(); PromotingToOther = false; } ////// Critical: Accesses _stylusLogic and _storedStagingAreaItems /// and calls StylusLogic.PromoteStoredItemsToMouse /// PublicOK: This method has a demand on it. /// Demand: Technically the demand is not needed because the /// user can already do this indirectly by canceling the /// manipulation. But the decision is to limit the scope /// of this raw method to full trust. /// [SecurityCritical, UIPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)] protected override void OnManipulationEnded(bool cancel) { base.OnManipulationEnded(cancel); if (cancel) { // Set PromotingToOther to true because the // the manipulation has been canceled and hence // we want the promotions to happen. PromotingToOther = true; _stylusLogic.PromoteStoredItemsToMouse(this); } else { // Set PromotingToOther to false because we // dont want any mouse promotions to happen for // rest of this touch cycle. PromotingToOther = false; } if (_storedStagingAreaItems != null) { _storedStagingAreaItems.Clear(); } } ////// Whether input associated with this device is being promoted to some other device. /// internal bool PromotingToOther { get; private set; } ////// Stored items that occurred during the time before we knew where to promote to. /// ////// Critical: The items stored in this collection will be promoted to mouse. /// internal IListStoredStagingAreaItems { [SecurityCritical] get { if (_storedStagingAreaItems == null) { _storedStagingAreaItems = new List (); } return _storedStagingAreaItems; } } /// /// The associated StylusDevice /// internal StylusDevice StylusDevice { get { return _stylusDevice; } } ////// Critical - Setting the ActiveSource on the device. /// [SecurityCritical] internal void ChangeActiveSource(PresentationSource activeSource) { SetActiveSource(activeSource); } ////// Critical: Accesses _stylusLogic. Could be TreatAsSafe /// because doesn't expose _stylusLogic itself. /// [SecurityCritical] internal void OnActivate() { Activate(); _activeDeviceCount++; if (_activeDeviceCount == 1) { _isPrimary = true; _stylusLogic.CurrentMousePromotionStylusDevice = _stylusDevice; } PromotingToOther = true; } ////// Critical: Accesses _stylusLogic and _storedStagingAreaItems. /// Clears _storedStagingAreaItems collection which /// stores the staging items to be promoted to mouse. /// Could be TreatAsSafe because doesn't expose the /// objects themselves. /// [SecurityCritical] internal void OnDeactivate() { Deactivate(); if (_storedStagingAreaItems != null) { _storedStagingAreaItems.Clear(); } PromotingToOther = true; DownHandled = false; _activeDeviceCount--; if (_activeDeviceCount == 0) { _stylusLogic.CurrentMousePromotionStylusDevice = null; } else if (_isPrimary) { _stylusLogic.CurrentMousePromotionStylusDevice = NoMousePromotionStylusDevice; } _isPrimary = false; } internal bool OnDown() { _lastAction = TouchAction.Down; DownHandled = ReportDown(); return DownHandled; } internal bool OnMove() { _lastAction = TouchAction.Move; return ReportMove(); } internal bool OnUp() { _lastAction = TouchAction.Up; return ReportUp(); } ////// Bool indicates if down event was handled /// internal bool DownHandled { get; private set; } #endregion #region Data private StylusDevice _stylusDevice; ////// Critical: To prevent accidental spread to transparent code /// [SecurityCritical] private StylusLogic _stylusLogic; ////// Critical: Holds the staging items to be promoted to mouse. /// [SecurityCritical] private List_storedStagingAreaItems; private TouchAction _lastAction = TouchAction.Move; private bool _isPrimary = false; [ThreadStatic] private static int _activeDeviceCount = 0; private static readonly StylusPointPropertyInfo[] _stylusPropertyInfos = new StylusPointPropertyInfo[] { StylusPointPropertyInfoDefaults.X, StylusPointPropertyInfoDefaults.Y, StylusPointPropertyInfoDefaults.NormalPressure, StylusPointPropertyInfoDefaults.Width, StylusPointPropertyInfoDefaults.Height }; private static readonly StylusPointDescription _stylusPointDescription = new StylusPointDescription(_stylusPropertyInfos); private static object NoMousePromotionStylusDevice = new object(); #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.Generic; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Security; using System.Security.Permissions; using MS.Internal; namespace System.Windows.Input { /// /// The implementation of a TouchDevice specific to StylusDevice. /// internal sealed class StylusTouchDevice : TouchDevice { #region TouchDevice Implementation ////// Critical: Accesses critical _stylusLogic and InputManager /// TreatAsSafe: This constructor handles critical data but does not expose it /// [SecurityCritical, SecurityTreatAsSafe] internal StylusTouchDevice(StylusDevice stylusDevice) : base(stylusDevice.Id) { _stylusDevice = stylusDevice; _stylusLogic = InputManager.UnsecureCurrent.StylusLogic; PromotingToOther = true; } ////// Provides the current position. /// /// Defines the coordinate space. ///The current position in the coordinate space of relativeTo. public override TouchPoint GetTouchPoint(IInputElement relativeTo) { Point position = _stylusDevice.GetPosition(relativeTo); Rect rectBounds = GetBounds(_stylusDevice.RawStylusPoint, position, relativeTo); return new TouchPoint(this, position, rectBounds, _lastAction); } private Rect GetBounds(StylusPoint stylusPoint, Point position, IInputElement relativeTo) { GeneralTransform elementToRoot; GeneralTransform rootToElement; GetRootTransforms(relativeTo, out elementToRoot, out rootToElement); return GetBounds(stylusPoint, position, relativeTo, elementToRoot, rootToElement); } private Rect GetBounds(StylusPoint stylusPoint, Point position, IInputElement relativeTo, GeneralTransform elementToRoot, GeneralTransform rootToElement) { // Get width and heith in pixel value double width = GetStylusPointWidthOrHeight(stylusPoint, /*isWidth*/ true); double height = GetStylusPointWidthOrHeight(stylusPoint, /*isWidth*/ false); // Get the position with respect to root Point rootPoint; if (elementToRoot == null || !elementToRoot.TryTransform(position, out rootPoint)) { rootPoint = position; } // Create a Rect with respect to root and transform it to element coordinate space Rect rectBounds = new Rect(rootPoint.X - width * 0.5, rootPoint.Y - height * 0.5, width, height); if (rootToElement != null) { rectBounds = rootToElement.TransformBounds(rectBounds); } return rectBounds; } private double GetStylusPointWidthOrHeight(StylusPoint stylusPoint, bool isWidth) { const double CentimeterPerInch = 2.54d; const double PixelPerInch = 96d; StylusPointProperty property = (isWidth ? StylusPointProperties.Width : StylusPointProperties.Height); double value = 0d; if (stylusPoint.HasProperty(property)) { // Get the property value in the corresponding units value = (double)stylusPoint.GetPropertyValue(property); StylusPointPropertyInfo propertyInfo = stylusPoint.Description.GetPropertyInfo(property); if (!DoubleUtil.AreClose(propertyInfo.Resolution, 0d)) { value /= propertyInfo.Resolution; } else { value = 0; } // Convert the value to Inches if (propertyInfo.Unit == StylusPointPropertyUnit.Centimeters) { value /= CentimeterPerInch; } // Convert the value to pixels value *= PixelPerInch; } return value; } ////// Provides all of the known points the device hit since the last reported position update. /// /// Defines the coordinate space. ///A list of points in the coordinate space of relativeTo. public override TouchPointCollection GetIntermediateTouchPoints(IInputElement relativeTo) { // Retrieve the stylus points StylusPointCollection stylusPoints = _stylusDevice.GetStylusPoints(relativeTo, _stylusPointDescription); int count = stylusPoints.Count; TouchPointCollection touchPoints = new TouchPointCollection(); GeneralTransform elementToRoot; GeneralTransform rootToElement; GetRootTransforms(relativeTo, out elementToRoot, out rootToElement); // Convert the stylus points into touch points for (int i = 0; i < count; i++) { StylusPoint stylusPoint = stylusPoints[i]; Point position = new Point(stylusPoint.X, stylusPoint.Y); Rect rectBounds = GetBounds(stylusPoint, position, relativeTo, elementToRoot, rootToElement); TouchPoint touchPoint = new TouchPoint(this, position, rectBounds, _lastAction); touchPoints.Add(touchPoint); } return touchPoints; } ////// Critical - Calls PresentationSource.CriticalFromVisual. /// TreatAsSafe - Does not expose PresentationSource. /// [SecurityCritical, SecurityTreatAsSafe] private void GetRootTransforms(IInputElement relativeTo, out GeneralTransform elementToRoot, out GeneralTransform rootToElement) { elementToRoot = rootToElement = null; DependencyObject containingVisual = InputElement.GetContainingVisual(relativeTo as DependencyObject); if (containingVisual != null) { PresentationSource relativePresentationSource = PresentationSource.CriticalFromVisual(containingVisual); Visual rootVisual = (relativePresentationSource != null) ? relativePresentationSource.RootVisual : null; Visual containingVisual2D = VisualTreeHelper.GetContainingVisual2D(containingVisual); if ((rootVisual != null) && (containingVisual2D != null)) { elementToRoot = containingVisual2D.TransformToAncestor(rootVisual); rootToElement = rootVisual.TransformToDescendant(containingVisual2D); } } } #endregion #region StylusLogic Related protected override void OnManipulationStarted() { base.OnManipulationStarted(); PromotingToOther = false; } ////// Critical: Accesses _stylusLogic and _storedStagingAreaItems /// and calls StylusLogic.PromoteStoredItemsToMouse /// PublicOK: This method has a demand on it. /// Demand: Technically the demand is not needed because the /// user can already do this indirectly by canceling the /// manipulation. But the decision is to limit the scope /// of this raw method to full trust. /// [SecurityCritical, UIPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)] protected override void OnManipulationEnded(bool cancel) { base.OnManipulationEnded(cancel); if (cancel) { // Set PromotingToOther to true because the // the manipulation has been canceled and hence // we want the promotions to happen. PromotingToOther = true; _stylusLogic.PromoteStoredItemsToMouse(this); } else { // Set PromotingToOther to false because we // dont want any mouse promotions to happen for // rest of this touch cycle. PromotingToOther = false; } if (_storedStagingAreaItems != null) { _storedStagingAreaItems.Clear(); } } ////// Whether input associated with this device is being promoted to some other device. /// internal bool PromotingToOther { get; private set; } ////// Stored items that occurred during the time before we knew where to promote to. /// ////// Critical: The items stored in this collection will be promoted to mouse. /// internal IListStoredStagingAreaItems { [SecurityCritical] get { if (_storedStagingAreaItems == null) { _storedStagingAreaItems = new List (); } return _storedStagingAreaItems; } } /// /// The associated StylusDevice /// internal StylusDevice StylusDevice { get { return _stylusDevice; } } ////// Critical - Setting the ActiveSource on the device. /// [SecurityCritical] internal void ChangeActiveSource(PresentationSource activeSource) { SetActiveSource(activeSource); } ////// Critical: Accesses _stylusLogic. Could be TreatAsSafe /// because doesn't expose _stylusLogic itself. /// [SecurityCritical] internal void OnActivate() { Activate(); _activeDeviceCount++; if (_activeDeviceCount == 1) { _isPrimary = true; _stylusLogic.CurrentMousePromotionStylusDevice = _stylusDevice; } PromotingToOther = true; } ////// Critical: Accesses _stylusLogic and _storedStagingAreaItems. /// Clears _storedStagingAreaItems collection which /// stores the staging items to be promoted to mouse. /// Could be TreatAsSafe because doesn't expose the /// objects themselves. /// [SecurityCritical] internal void OnDeactivate() { Deactivate(); if (_storedStagingAreaItems != null) { _storedStagingAreaItems.Clear(); } PromotingToOther = true; DownHandled = false; _activeDeviceCount--; if (_activeDeviceCount == 0) { _stylusLogic.CurrentMousePromotionStylusDevice = null; } else if (_isPrimary) { _stylusLogic.CurrentMousePromotionStylusDevice = NoMousePromotionStylusDevice; } _isPrimary = false; } internal bool OnDown() { _lastAction = TouchAction.Down; DownHandled = ReportDown(); return DownHandled; } internal bool OnMove() { _lastAction = TouchAction.Move; return ReportMove(); } internal bool OnUp() { _lastAction = TouchAction.Up; return ReportUp(); } ////// Bool indicates if down event was handled /// internal bool DownHandled { get; private set; } #endregion #region Data private StylusDevice _stylusDevice; ////// Critical: To prevent accidental spread to transparent code /// [SecurityCritical] private StylusLogic _stylusLogic; ////// Critical: Holds the staging items to be promoted to mouse. /// [SecurityCritical] private List_storedStagingAreaItems; private TouchAction _lastAction = TouchAction.Move; private bool _isPrimary = false; [ThreadStatic] private static int _activeDeviceCount = 0; private static readonly StylusPointPropertyInfo[] _stylusPropertyInfos = new StylusPointPropertyInfo[] { StylusPointPropertyInfoDefaults.X, StylusPointPropertyInfoDefaults.Y, StylusPointPropertyInfoDefaults.NormalPressure, StylusPointPropertyInfoDefaults.Width, StylusPointPropertyInfoDefaults.Height }; private static readonly StylusPointDescription _stylusPointDescription = new StylusPointDescription(_stylusPropertyInfos); private static object NoMousePromotionStylusDevice = new object(); #endregion } } // 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
- TrackingStringDictionary.cs
- TopClause.cs
- ArithmeticException.cs
- WebBrowserContainer.cs
- SliderAutomationPeer.cs
- EntityException.cs
- XmlUtf8RawTextWriter.cs
- StylusCollection.cs
- TrackingDataItem.cs
- AnonymousIdentificationModule.cs
- DrawingCollection.cs
- EncoderParameter.cs
- AppDomain.cs
- DataGridViewAutoSizeColumnsModeEventArgs.cs
- EntityDataSourceContainerNameConverter.cs
- TimeSpanStorage.cs
- OracleMonthSpan.cs
- ToolBarButton.cs
- XmlAttributeAttribute.cs
- NativeMethods.cs
- ContainerFilterService.cs
- TimersDescriptionAttribute.cs
- BidPrivateBase.cs
- PreviewPrintController.cs
- Track.cs
- ThicknessAnimationUsingKeyFrames.cs
- IndexedGlyphRun.cs
- ControlPaint.cs
- Attributes.cs
- MaskedTextProvider.cs
- _DynamicWinsockMethods.cs
- xml.cs
- WrappedIUnknown.cs
- UnsafeNativeMethodsCLR.cs
- CorrelationScope.cs
- _ProxyChain.cs
- ContentWrapperAttribute.cs
- PackageStore.cs
- Tuple.cs
- ClientScriptItem.cs
- QuaternionConverter.cs
- LayoutManager.cs
- NavigatingCancelEventArgs.cs
- InfoCardMasterKey.cs
- UnsafeNetInfoNativeMethods.cs
- SqlFormatter.cs
- XmlSchemaSet.cs
- AssemblyNameProxy.cs
- BooleanConverter.cs
- EqualityComparer.cs
- ApplySecurityAndSendAsyncResult.cs
- BaseCAMarshaler.cs
- SoapExtensionStream.cs
- XmlMessageFormatter.cs
- ExpressionBuilderCollection.cs
- FixedSOMSemanticBox.cs
- XmlReaderDelegator.cs
- TagPrefixInfo.cs
- ValueTypeFixupInfo.cs
- ColorConvertedBitmap.cs
- Brushes.cs
- UIAgentRequest.cs
- PreservationFileReader.cs
- XmlSchemaSimpleTypeUnion.cs
- storepermission.cs
- TextTabProperties.cs
- NullRuntimeConfig.cs
- TextSelectionProcessor.cs
- OutOfMemoryException.cs
- ToolStripContainer.cs
- InputLangChangeRequestEvent.cs
- CultureTableRecord.cs
- SqlServices.cs
- HandleCollector.cs
- HyperLinkField.cs
- LocalizableResourceBuilder.cs
- __ConsoleStream.cs
- SHA512Cng.cs
- DrawingContextWalker.cs
- Odbc32.cs
- ObjectReaderCompiler.cs
- ACE.cs
- Attributes.cs
- PenThread.cs
- RuleSetDialog.Designer.cs
- PageThemeCodeDomTreeGenerator.cs
- IDReferencePropertyAttribute.cs
- Geometry.cs
- XPathNavigatorReader.cs
- BitmapSizeOptions.cs
- GridViewAutoFormat.cs
- BitmapDownload.cs
- OracleParameterCollection.cs
- ConfigXmlDocument.cs
- MimeMultiPart.cs
- Int64Animation.cs
- DocumentAutomationPeer.cs
- NameObjectCollectionBase.cs
- MultiPartWriter.cs
- ClientRoleProvider.cs