Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / MS / Internal / Ink / SelectionEditingBehavior.cs / 1305600 / SelectionEditingBehavior.cs
//----------------------------------------------------------------------------
//
// File: ResizeEditingBehavior .cs
//
// Description:
// ResizeEditingBehavior
//
// Authors: samgeo
//
// Copyright (C) 2003 by Microsoft Corporation. All rights reserved.
//
//---------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Interop;
using System.Windows.Navigation;
using System.Windows.Media;
using System.Windows.Ink;
namespace MS.Internal.Ink
{
///
/// SelectionEditingBehavior
///
internal sealed class SelectionEditingBehavior : EditingBehavior
{
//-------------------------------------------------------------------------------
//
// Constructors
//
//-------------------------------------------------------------------------------
#region Constructors
///
/// SelectionEditingBehavior constructor
///
internal SelectionEditingBehavior(EditingCoordinator editingCoordinator, InkCanvas inkCanvas)
: base(editingCoordinator, inkCanvas)
{
}
#endregion Constructors
//--------------------------------------------------------------------------------
//
// Protected Methods
//
//-------------------------------------------------------------------------------
#region Protected Methods
///
/// Attaching to the element, we get attached in StylusDown
///
protected override void OnActivate()
{
_actionStarted = false;
// Capture the mouse.
InitializeCapture();
// Hittest for the grab handle
MouseDevice mouse = Mouse.PrimaryDevice;
_hitResult = InkCanvas.SelectionAdorner.SelectionHandleHitTest(
mouse.GetPosition((IInputElement)(InkCanvas.SelectionAdorner)));
Debug.Assert(_hitResult != InkCanvasSelectionHitResult.None);
EditingCoordinator.InvalidateBehaviorCursor(this);
// Get the current selection bounds.
_selectionRect = InkCanvas.GetSelectionBounds( );
// Set the initial tracking position and rectangle
_previousLocation = mouse.GetPosition(InkCanvas.SelectionAdorner);
_previousRect = _selectionRect;
// Start the feedback rubber band.
InkCanvas.InkCanvasSelection.StartFeedbackAdorner(_selectionRect, _hitResult);
// Add handlers to the mouse events.
InkCanvas.SelectionAdorner.AddHandler(Mouse.MouseUpEvent, new MouseButtonEventHandler(OnMouseUp));
InkCanvas.SelectionAdorner.AddHandler(Mouse.MouseMoveEvent, new MouseEventHandler(OnMouseMove));
InkCanvas.SelectionAdorner.AddHandler(UIElement.LostMouseCaptureEvent,
new MouseEventHandler(OnLostMouseCapture));
}
///
/// Called when the ResizeEditingBehavior is detached
///
protected override void OnDeactivate()
{
// Remove the mouse event handlers.
InkCanvas.SelectionAdorner.RemoveHandler(Mouse.MouseUpEvent, new MouseButtonEventHandler(OnMouseUp));
InkCanvas.SelectionAdorner.RemoveHandler(Mouse.MouseMoveEvent, new MouseEventHandler(OnMouseMove));
InkCanvas.SelectionAdorner.RemoveHandler(UIElement.LostMouseCaptureEvent,
new MouseEventHandler(OnLostMouseCapture));
}
protected override void OnCommit(bool commit)
{
// Release the inputs.
ReleaseCapture(true, commit);
}
protected override Cursor GetCurrentCursor()
{
return PenCursorManager.GetSelectionCursor(_hitResult,
(this.InkCanvas.FlowDirection == FlowDirection.RightToLeft));
}
#endregion Protected Methods
//--------------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------------
#region Private Methods
///
/// The handler of the MouseMove event
///
///
///
private void OnMouseMove(object sender, MouseEventArgs args)
{
// Get the current mouse location.
Point curPoint = args.GetPosition(InkCanvas.SelectionAdorner);
// Check if we have a mouse movement at all.
if ( !DoubleUtil.AreClose(curPoint.X, _previousLocation.X)
|| !DoubleUtil.AreClose(curPoint.Y, _previousLocation.Y) )
{
// We won't start the move until we really see a movement.
if ( !_actionStarted )
{
_actionStarted = true;
}
// Get the new rectangle
Rect newRect = ChangeFeedbackRectangle(curPoint);
// Update the feedback rubber band and record the current tracking rectangle.
InkCanvas.InkCanvasSelection.UpdateFeedbackAdorner(newRect);
_previousRect = newRect;
}
}
///
/// The handler of the MouseUp event
///
///
///
private void OnMouseUp(object sender, MouseButtonEventArgs args)
{
// We won't start the move until we really see a movement.
if ( _actionStarted )
{
_previousRect = ChangeFeedbackRectangle(args.GetPosition(InkCanvas.SelectionAdorner));
}
Commit(true);
}
///
/// InkCanvas.LostStylusCapture handler
///
///
///
private void OnLostMouseCapture(object sender, MouseEventArgs args)
{
// If user is editing, we have to commit the current operation and reset our state.
if ( EditingCoordinator.UserIsEditing )
{
ReleaseCapture(false, true);
}
}
///
/// Get the new feedback rectangle based on the location
///
///
private Rect ChangeFeedbackRectangle(Point newPoint)
{
// First check to make sure that we are not going past the zero point on
// the original element in which we hit the resize handle.
// If moving the left side -- don't go past the right side.
if ( _hitResult == InkCanvasSelectionHitResult.TopLeft ||
_hitResult == InkCanvasSelectionHitResult.BottomLeft ||
_hitResult == InkCanvasSelectionHitResult.Left )
{
if ( newPoint.X > _selectionRect.Right - MinimumHeightWidthSize )
{
newPoint.X = _selectionRect.Right - MinimumHeightWidthSize;
}
}
// If moving the right side -- don't go past the left side.
if ( _hitResult == InkCanvasSelectionHitResult.TopRight ||
_hitResult == InkCanvasSelectionHitResult.BottomRight ||
_hitResult == InkCanvasSelectionHitResult.Right )
{
if ( newPoint.X < _selectionRect.Left + MinimumHeightWidthSize )
{
newPoint.X = _selectionRect.Left + MinimumHeightWidthSize;
}
}
// If moving the top side -- don't go past the bottom side.
if ( _hitResult == InkCanvasSelectionHitResult.TopLeft ||
_hitResult == InkCanvasSelectionHitResult.TopRight ||
_hitResult == InkCanvasSelectionHitResult.Top )
{
if ( newPoint.Y > _selectionRect.Bottom - MinimumHeightWidthSize )
{
newPoint.Y = _selectionRect.Bottom - MinimumHeightWidthSize;
}
}
// If moving the bottom side -- don't go past the top side.
if ( _hitResult == InkCanvasSelectionHitResult.BottomLeft ||
_hitResult == InkCanvasSelectionHitResult.BottomRight ||
_hitResult == InkCanvasSelectionHitResult.Bottom )
{
if ( newPoint.Y < _selectionRect.Top + MinimumHeightWidthSize )
{
newPoint.Y = _selectionRect.Top + MinimumHeightWidthSize;
}
}
// Get the new boundary of the selection
Rect newRect = CalculateRect(newPoint.X - _previousLocation.X, newPoint.Y - _previousLocation.Y);
// Depends on the current grab handle, we record the tracking position accordingly.
if ( _hitResult == InkCanvasSelectionHitResult.BottomRight ||
_hitResult == InkCanvasSelectionHitResult.BottomLeft ||
_hitResult == InkCanvasSelectionHitResult.TopRight ||
_hitResult == InkCanvasSelectionHitResult.TopLeft ||
_hitResult == InkCanvasSelectionHitResult.Selection )
{
_previousLocation.X = newPoint.X;
_previousLocation.Y = newPoint.Y;
}
else if ( _hitResult == InkCanvasSelectionHitResult.Left ||
_hitResult == InkCanvasSelectionHitResult.Right )
{
_previousLocation.X = newPoint.X;
}
else if ( _hitResult == InkCanvasSelectionHitResult.Top ||
_hitResult == InkCanvasSelectionHitResult.Bottom )
{
_previousLocation.Y = newPoint.Y;
}
return newRect;
}
///
/// Resize a given element based on the grab handle
///
///
///
private Rect CalculateRect(double x, double y)
{
Rect newRect = _previousRect;
switch ( _hitResult )
{
case InkCanvasSelectionHitResult.BottomRight:
{
newRect = ExtendSelectionRight(newRect, x);
newRect = ExtendSelectionBottom(newRect, y);
break;
}
case InkCanvasSelectionHitResult.Bottom:
{
newRect = ExtendSelectionBottom(newRect, y);
break;
}
case InkCanvasSelectionHitResult.BottomLeft:
{
newRect = ExtendSelectionLeft(newRect, x);
newRect = ExtendSelectionBottom(newRect, y);
break;
}
case InkCanvasSelectionHitResult.TopRight:
{
newRect = ExtendSelectionTop(newRect, y);
newRect = ExtendSelectionRight(newRect, x);
break;
}
case InkCanvasSelectionHitResult.Top:
{
newRect = ExtendSelectionTop(newRect, y);
break;
}
case InkCanvasSelectionHitResult.TopLeft:
{
newRect = ExtendSelectionTop(newRect, y);
newRect = ExtendSelectionLeft(newRect, x);
break;
}
case InkCanvasSelectionHitResult.Left:
{
newRect = ExtendSelectionLeft(newRect, x);
break;
}
case InkCanvasSelectionHitResult.Right:
{
newRect = ExtendSelectionRight(newRect, x);
break;
}
case InkCanvasSelectionHitResult.Selection:
{
// Translate the rectangle
newRect.Offset(x, y);
break;
}
default:
{
Debug.Assert(false);
break;
}
}
return newRect;
}
// ExtendSelectionLeft
private static Rect ExtendSelectionLeft(Rect rect, double extendBy)
{
Rect newRect = rect;
newRect.X += extendBy;
newRect.Width -= extendBy;
return newRect;
}
// ExtendSelectionTop
private static Rect ExtendSelectionTop(Rect rect, double extendBy)
{
Rect newRect = rect;
newRect.Y += extendBy;
newRect.Height -= extendBy;
return newRect;
}
// ExtendSelectionRight
private static Rect ExtendSelectionRight(Rect rect, double extendBy)
{
Rect newRect = rect;
newRect.Width += extendBy;
return newRect;
}
// ExtendSelectionBottom
private static Rect ExtendSelectionBottom(Rect rect, double extendBy)
{
Rect newRect = rect;
newRect.Height += extendBy;
return newRect;
}
///
/// Capture Mouse
///
private void InitializeCapture()
{
Debug.Assert(EditingCoordinator.UserIsEditing == false, "Unexpect UserIsEditng state." );
EditingCoordinator.UserIsEditing = true;
InkCanvas.SelectionAdorner.CaptureMouse();
}
///
/// Release Mouse capture
///
///
///
private void ReleaseCapture(bool releaseDevice, bool commit)
{
if ( EditingCoordinator.UserIsEditing )
{
EditingCoordinator.UserIsEditing = false;
if ( releaseDevice )
{
InkCanvas.SelectionAdorner.ReleaseMouseCapture();
}
SelfDeactivate();
// End the rubber band. If the operation is committed, we set the selection to the tracking rectangle.
// Otherwise, reset the selection to the original bounds.
InkCanvas.InkCanvasSelection.EndFeedbackAdorner(commit ? _previousRect : _selectionRect);
}
}
#endregion Private Methods
//-------------------------------------------------------------------------------
//
// Private Fields
//
//--------------------------------------------------------------------------------
#region Private Fields
private const double MinimumHeightWidthSize = 16.0;
private Point _previousLocation;
private Rect _previousRect;
private Rect _selectionRect;
private InkCanvasSelectionHitResult _hitResult;
private bool _actionStarted = false;
#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
- BindingCollection.cs
- FontWeightConverter.cs
- WindowPatternIdentifiers.cs
- ColumnWidthChangedEvent.cs
- DiscoveryDocumentReference.cs
- CallSiteHelpers.cs
- NamespaceDecl.cs
- BuildProvider.cs
- SafeProcessHandle.cs
- DbConnectionStringBuilder.cs
- CultureInfoConverter.cs
- Stacktrace.cs
- AssemblyInfo.cs
- XPathExpr.cs
- OLEDB_Util.cs
- PreApplicationStartMethodAttribute.cs
- DrawingAttributesDefaultValueFactory.cs
- StringComparer.cs
- ServiceMemoryGates.cs
- RadioButtonAutomationPeer.cs
- ProcessHost.cs
- XamlInt32CollectionSerializer.cs
- ComplexTypeEmitter.cs
- CollectionConverter.cs
- BitmapData.cs
- TextEditorContextMenu.cs
- Logging.cs
- StandardCommands.cs
- SqlTypesSchemaImporter.cs
- SynchronizedDispatch.cs
- RawUIStateInputReport.cs
- HttpProfileBase.cs
- StreamGeometryContext.cs
- PropertyValue.cs
- TextAdaptor.cs
- ManagementObject.cs
- unsafeIndexingFilterStream.cs
- XmlAnyElementAttribute.cs
- SQLGuid.cs
- EncodingNLS.cs
- InheritedPropertyChangedEventArgs.cs
- DesignerActionItemCollection.cs
- TextRangeAdaptor.cs
- ExclusiveCanonicalizationTransform.cs
- EndPoint.cs
- SizeChangedInfo.cs
- StandardToolWindows.cs
- HtmlInputRadioButton.cs
- ThemeableAttribute.cs
- Tracer.cs
- DataGridViewCell.cs
- SiteMapNodeItemEventArgs.cs
- HyperlinkAutomationPeer.cs
- Point4D.cs
- PointConverter.cs
- WebConfigurationHostFileChange.cs
- OdbcUtils.cs
- Boolean.cs
- CanonicalFontFamilyReference.cs
- SqlDataSource.cs
- SiteMapNodeCollection.cs
- ToolStripPanelCell.cs
- Parameter.cs
- DropShadowBitmapEffect.cs
- Registry.cs
- ModelChangedEventArgsImpl.cs
- DecoderReplacementFallback.cs
- CodeSnippetTypeMember.cs
- Converter.cs
- ZipIOZip64EndOfCentralDirectoryLocatorBlock.cs
- TrackingConditionCollection.cs
- StorageScalarPropertyMapping.cs
- WebRequestModulesSection.cs
- Int32Animation.cs
- TimeSpanConverter.cs
- CompilationUtil.cs
- ResourceReferenceExpressionConverter.cs
- EdmEntityTypeAttribute.cs
- ImageSource.cs
- MarkupProperty.cs
- BatchStream.cs
- FontConverter.cs
- MetadataItemEmitter.cs
- PolyQuadraticBezierSegmentFigureLogic.cs
- AxImporter.cs
- NativeMethods.cs
- PrintPreviewGraphics.cs
- FormViewModeEventArgs.cs
- ProtocolsConfigurationEntry.cs
- AppDomainManager.cs
- TimeEnumHelper.cs
- FocusManager.cs
- AnyAllSearchOperator.cs
- PasswordRecovery.cs
- WsdlBuildProvider.cs
- RuleSetDialog.cs
- ChangeDirector.cs
- TableHeaderCell.cs
- ParenthesizePropertyNameAttribute.cs
- XamlInt32CollectionSerializer.cs