Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Framework / System / Windows / Documents / TextEditorCharacters.cs / 1 / TextEditorCharacters.cs
//---------------------------------------------------------------------------- // // File: TextEditorCharacters.cs // // Copyright (C) Microsoft Corporation. All rights reserved. // // Description: A component of TextEditor supporting character formatting commands // //--------------------------------------------------------------------------- namespace System.Windows.Documents { using MS.Internal; using System.Globalization; using System.Threading; using System.ComponentModel; using System.Text; using System.Collections; // ArrayList using System.Runtime.InteropServices; using System.Windows.Threading; using System.Windows.Input; using System.Windows.Controls; // ScrollChangedEventArgs using System.Windows.Controls.Primitives; // CharacterCasing, TextBoxBase using System.Windows.Media; using System.Windows.Markup; using MS.Utility; using MS.Win32; using MS.Internal.Documents; using MS.Internal.Commands; // CommandHelpers ////// Text editing service for controls. /// internal static class TextEditorCharacters { //----------------------------------------------------- // // Class Internal Methods // //----------------------------------------------------- #region Class Internal Methods // Registers all text editing command handlers for a given control type internal static void _RegisterClassHandlers(Type controlType, bool registerEventListeners) { // Editing Commands: Character Editing // ----------------------------------- CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.ResetFormat , new ExecutedRoutedEventHandler(OnResetFormat) , new CanExecuteRoutedEventHandler(OnQueryStatusNYI), KeyGesture.CreateFromResourceStrings(SR.Get(SRID.KeyResetFormat), SR.Get(SRID.KeyResetFormatDisplayString)) ); CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.ToggleBold , new ExecutedRoutedEventHandler(OnToggleBold) , new CanExecuteRoutedEventHandler(OnQueryStatusNYI), KeyGesture.CreateFromResourceStrings(SR.Get(SRID.KeyToggleBold), SR.Get(SRID.KeyToggleBoldDisplayString)) ); CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.ToggleItalic , new ExecutedRoutedEventHandler(OnToggleItalic) , new CanExecuteRoutedEventHandler(OnQueryStatusNYI), KeyGesture.CreateFromResourceStrings(SR.Get(SRID.KeyToggleItalic), SR.Get(SRID.KeyToggleItalicDisplayString)) ); CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.ToggleUnderline , new ExecutedRoutedEventHandler(OnToggleUnderline) , new CanExecuteRoutedEventHandler(OnQueryStatusNYI), KeyGesture.CreateFromResourceStrings(SR.Get(SRID.KeyToggleUnderline), SR.Get(SRID.KeyToggleUnderlineDisplayString)) ); CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.ToggleSubscript , new ExecutedRoutedEventHandler(OnToggleSubscript) , new CanExecuteRoutedEventHandler(OnQueryStatusNYI), KeyGesture.CreateFromResourceStrings(SR.Get(SRID.KeyToggleSubscript), SR.Get(SRID.KeyToggleSubscriptDisplayString)) ); CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.ToggleSuperscript , new ExecutedRoutedEventHandler(OnToggleSuperscript) , new CanExecuteRoutedEventHandler(OnQueryStatusNYI), KeyGesture.CreateFromResourceStrings(SR.Get(SRID.KeyToggleSuperscript), SR.Get(SRID.KeyToggleSuperscriptDisplayString)) ); CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.IncreaseFontSize , new ExecutedRoutedEventHandler(OnIncreaseFontSize) , new CanExecuteRoutedEventHandler(OnQueryStatusNYI), KeyGesture.CreateFromResourceStrings(SR.Get(SRID.KeyIncreaseFontSize), SR.Get(SRID.KeyIncreaseFontSizeDisplayString)) ); CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.DecreaseFontSize , new ExecutedRoutedEventHandler(OnDecreaseFontSize) , new CanExecuteRoutedEventHandler(OnQueryStatusNYI), KeyGesture.CreateFromResourceStrings(SR.Get(SRID.KeyDecreaseFontSize), SR.Get(SRID.KeyDecreaseFontSizeDisplayString)) ); CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.ApplyFontSize , new ExecutedRoutedEventHandler(OnApplyFontSize) , new CanExecuteRoutedEventHandler(OnQueryStatusNYI), KeyGesture.CreateFromResourceStrings(SR.Get(SRID.KeyApplyFontSize), SR.Get(SRID.KeyApplyFontSizeDisplayString)) ); CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.ApplyFontFamily , new ExecutedRoutedEventHandler(OnApplyFontFamily) , new CanExecuteRoutedEventHandler(OnQueryStatusNYI), KeyGesture.CreateFromResourceStrings(SR.Get(SRID.KeyApplyFontFamily), SR.Get(SRID.KeyApplyFontFamilyDisplayString)) ); CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.ApplyForeground , new ExecutedRoutedEventHandler(OnApplyForeground) , new CanExecuteRoutedEventHandler(OnQueryStatusNYI), KeyGesture.CreateFromResourceStrings(SR.Get(SRID.KeyApplyForeground), SR.Get(SRID.KeyApplyForegroundDisplayString)) ); CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.ApplyBackground , new ExecutedRoutedEventHandler(OnApplyBackground) , new CanExecuteRoutedEventHandler(OnQueryStatusNYI), KeyGesture.CreateFromResourceStrings(SR.Get(SRID.KeyApplyBackground), SR.Get(SRID.KeyApplyBackgroundDisplayString)) ); CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.ToggleSpellCheck , new ExecutedRoutedEventHandler(OnToggleSpellCheck) , new CanExecuteRoutedEventHandler(OnQueryStatusNYI), KeyGesture.CreateFromResourceStrings(SR.Get(SRID.KeyToggleSpellCheck), SR.Get(SRID.KeyToggleSpellCheckDisplayString)) ); CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.ApplyInlineFlowDirectionRTL , new ExecutedRoutedEventHandler(OnApplyInlineFlowDirectionRTL), new CanExecuteRoutedEventHandler(OnQueryStatusNYI)); CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.ApplyInlineFlowDirectionLTR , new ExecutedRoutedEventHandler(OnApplyInlineFlowDirectionLTR), new CanExecuteRoutedEventHandler(OnQueryStatusNYI)); } // A common method for all formatting commands. // Applies a property to current selection. // Takes care of toggling operations (like bold/italic). // Creates undo unit for this action. internal static void _OnApplyProperty(TextEditor This, DependencyProperty formattingProperty, object propertyValue) { _OnApplyProperty(This, formattingProperty, propertyValue, /*applyToParagraphs*/false, PropertyValueAction.SetValue); } internal static void _OnApplyProperty(TextEditor This, DependencyProperty formattingProperty, object propertyValue, bool applyToParagraphs) { _OnApplyProperty(This, formattingProperty, propertyValue, applyToParagraphs, PropertyValueAction.SetValue); } internal static void _OnApplyProperty(TextEditor This, DependencyProperty formattingProperty, object propertyValue, bool applyToParagraphs, PropertyValueAction propertyValueAction) { if (This == null || !This._IsEnabled || This.IsReadOnly || !This.AcceptsRichContent || !(This.Selection is TextSelection)) { return; } // Check whether the property is known if (!TextSchema.IsParagraphProperty(formattingProperty) && !TextSchema.IsCharacterProperty(formattingProperty)) { Invariant.Assert(false, "The property '" + formattingProperty.Name + "' is unknown to TextEditor"); return; } TextSelection selection = (TextSelection)This.Selection; if (TextSchema.IsStructuralCharacterProperty(formattingProperty) && !TextRangeEdit.CanApplyStructuralInlineProperty(selection.Start, selection.End)) { // Ignore structural commands fires in inappropriate context. return; } TextEditorTyping._FlushPendingInputItems(This); // Forget previously suggested horizontal position TextEditorSelection._ClearSuggestedX(This); // Break merged typing sequence TextEditorTyping._BreakTypingSequence(This); // Apply property selection.ApplyPropertyValue(formattingProperty, propertyValue, applyToParagraphs, propertyValueAction); } #endregion Class Internal Methods //------------------------------------------------------ // // Private Methods // //----------------------------------------------------- #region Private Methods // ................................................................ // // Editing Commands: Character Editing // // ................................................................ private static void OnResetFormat(object target, ExecutedRoutedEventArgs args) { TextEditor This = TextEditor._GetTextEditor(target); if (This == null || !This._IsEnabled || This.IsReadOnly || !This.AcceptsRichContent || !(This.Selection.Start is TextPointer)) { return; } TextEditorTyping._FlushPendingInputItems(This); using (This.Selection.DeclareChangeBlock()) { // Positions to clear all inline formatting properties TextPointer startResetFormatPosition = (TextPointer)This.Selection.Start; TextPointer endResetFormatPosition = (TextPointer)This.Selection.End; if (This.Selection.IsEmpty) { TextSegment autoWordRange = TextRangeBase.GetAutoWord(This.Selection); if (autoWordRange.IsNull) { // Clear springloaded formatting ((TextSelection)This.Selection).ClearSpringloadFormatting(); return; } else { // If we have a word, apply reset format to it startResetFormatPosition = (TextPointer)autoWordRange.Start; endResetFormatPosition = (TextPointer)autoWordRange.End; } } // Forget previously suggested horizontal position TextEditorSelection._ClearSuggestedX(This); // Clear all inline formattings TextRangeEdit.CharacterResetFormatting(startResetFormatPosition, endResetFormatPosition); } } ////// ToggleBold command event handler. /// private static void OnToggleBold(object target, ExecutedRoutedEventArgs args) { TextEditor This = TextEditor._GetTextEditor(target); if (This == null || !This._IsEnabled || This.IsReadOnly || !This.AcceptsRichContent || !(This.Selection is TextSelection)) { return; } TextEditorTyping._FlushPendingInputItems(This); object propertyValue = ((TextSelection)This.Selection).GetCurrentValue(TextElement.FontWeightProperty); FontWeight fontWeight = (propertyValue != DependencyProperty.UnsetValue && (FontWeight)propertyValue == FontWeights.Bold) ? FontWeights.Normal : FontWeights.Bold; TextEditorCharacters._OnApplyProperty(This, TextElement.FontWeightProperty, fontWeight); } ////// ToggleItalic command event handler. /// private static void OnToggleItalic(object target, ExecutedRoutedEventArgs args) { TextEditor This = TextEditor._GetTextEditor(target); if (This == null || !This._IsEnabled || This.IsReadOnly || !This.AcceptsRichContent || !(This.Selection is TextSelection)) { return; } TextEditorTyping._FlushPendingInputItems(This); object propertyValue = ((TextSelection)This.Selection).GetCurrentValue(TextElement.FontStyleProperty); FontStyle fontStyle = (propertyValue != DependencyProperty.UnsetValue && (FontStyle)propertyValue == FontStyles.Italic) ? FontStyles.Normal : FontStyles.Italic; TextEditorCharacters._OnApplyProperty(This, TextElement.FontStyleProperty, fontStyle); // Update the caret to show it as italic or normal caret. This.Selection.RefreshCaret(); } ////// ToggleUnderline command event handler. /// private static void OnToggleUnderline(object target, ExecutedRoutedEventArgs args) { TextEditor This = TextEditor._GetTextEditor(target); if (This == null || !This._IsEnabled || This.IsReadOnly || !This.AcceptsRichContent || !(This.Selection is TextSelection)) { return; } TextEditorTyping._FlushPendingInputItems(This); object propertyValue = ((TextSelection)This.Selection).GetCurrentValue(Inline.TextDecorationsProperty); TextDecorationCollection textDecorations = propertyValue != DependencyProperty.UnsetValue ? (TextDecorationCollection)propertyValue : null; if (!TextSchema.HasTextDecorations(textDecorations)) { textDecorations = TextDecorations.Underline; } else { textDecorations = new TextDecorationCollection(); // empty collection - no underline } TextEditorCharacters._OnApplyProperty(This, Inline.TextDecorationsProperty, textDecorations); } // Command handler for Ctrl+"+" key (non-numpad) private static void OnToggleSubscript(object sender, ExecutedRoutedEventArgs args) { TextEditor This = TextEditor._GetTextEditor(sender); if (This == null || !This._IsEnabled || This.IsReadOnly || !This.AcceptsRichContent || !(This.Selection is TextSelection)) { return; } TextEditorTyping._FlushPendingInputItems(This); FontVariants fontVariants = (FontVariants)((TextSelection)This.Selection).GetCurrentValue(Typography.VariantsProperty); fontVariants = fontVariants == FontVariants.Subscript ? FontVariants.Normal : FontVariants.Subscript; TextEditorCharacters._OnApplyProperty(This, Typography.VariantsProperty, fontVariants); } // Command handler fro Ctrl+Shift+"+" (non-numpad) private static void OnToggleSuperscript(object sender, ExecutedRoutedEventArgs args) { TextEditor This = TextEditor._GetTextEditor(sender); if (This == null || !This._IsEnabled || This.IsReadOnly || !This.AcceptsRichContent || !(This.Selection is TextSelection)) { return; } TextEditorTyping._FlushPendingInputItems(This); FontVariants fontVariants = (FontVariants)((TextSelection)This.Selection).GetCurrentValue(Typography.VariantsProperty); fontVariants = fontVariants == FontVariants.Superscript ? FontVariants.Normal : FontVariants.Superscript; TextEditorCharacters._OnApplyProperty(This, Typography.VariantsProperty, fontVariants); } // Used in IncreaseFontSize and DecreaseFontSize commands internal const double OneFontPoint = 72.0 / 96.0; // The limiting constant is taken from Word UI - it suggests to choose font size from a range between 1 and 1638. // internal const double MaxFontPoint = 1638.0; ////// IncreaseFontSize command event handler /// private static void OnIncreaseFontSize(object target, ExecutedRoutedEventArgs args) { TextEditor This = TextEditor._GetTextEditor(target); if (This == null || !This._IsEnabled || This.IsReadOnly || !This.AcceptsRichContent || !(This.Selection is TextSelection)) { return; } TextEditorTyping._FlushPendingInputItems(This); if (This.Selection.IsEmpty) { // Springload an increased font size double fontSize = (double)((TextSelection)This.Selection).GetCurrentValue(TextElement.FontSizeProperty); if (fontSize == 0.0) { return; // no characters available for font operation } if (fontSize < TextEditorCharacters.MaxFontPoint) { fontSize += TextEditorCharacters.OneFontPoint; if (fontSize > TextEditorCharacters.MaxFontPoint) { fontSize = TextEditorCharacters.MaxFontPoint; } // The limiting constant is taken from Word UI - it suggests to choose font size from a range between 1 and 1638. TextEditorCharacters._OnApplyProperty(This, TextElement.FontSizeProperty, fontSize); } } else { // Apply font size in incremental mode to a nonempty selection TextEditorCharacters._OnApplyProperty(This, TextElement.FontSizeProperty, OneFontPoint, /*applyToParagraphs:*/false, PropertyValueAction.IncreaseByAbsoluteValue); } } ////// DecreaseFontSize command event handler /// private static void OnDecreaseFontSize(object target, ExecutedRoutedEventArgs args) { TextEditor This = TextEditor._GetTextEditor(target); if (This == null || !This._IsEnabled || This.IsReadOnly || !This.AcceptsRichContent || !(This.Selection is TextSelection)) { return; } TextEditorTyping._FlushPendingInputItems(This); if (This.Selection.IsEmpty) { // Springload a decreased font size double fontSize = (double)((TextSelection)This.Selection).GetCurrentValue(TextElement.FontSizeProperty); if (fontSize == 0.0) { return; // no characters available for font operation } if (fontSize > TextEditorCharacters.OneFontPoint) { fontSize -= TextEditorCharacters.OneFontPoint; if (fontSize < TextEditorCharacters.OneFontPoint) { fontSize = TextEditorCharacters.OneFontPoint; } TextEditorCharacters._OnApplyProperty(This, TextElement.FontSizeProperty, fontSize); } } else { // Apply font size in decremental mode to a nonempty selection TextEditorCharacters._OnApplyProperty(This, TextElement.FontSizeProperty, OneFontPoint, /*applyToParagraphs:*/false, PropertyValueAction.DecreaseByAbsoluteValue); } } ////// ApplyFontSize command event handler. /// private static void OnApplyFontSize(object target, ExecutedRoutedEventArgs args) { if (args.Parameter == null) { return; // Ignore the command if no argument provided } TextEditor This = TextEditor._GetTextEditor(target); TextEditorCharacters._OnApplyProperty(This, TextElement.FontSizeProperty, args.Parameter); } ////// ApplyFontFamily command event handler. /// private static void OnApplyFontFamily(object target, ExecutedRoutedEventArgs args) { if (args.Parameter == null) { return; // Ignore the command if no argument provided } TextEditor This = TextEditor._GetTextEditor(target); TextEditorCharacters._OnApplyProperty(This, TextElement.FontFamilyProperty, args.Parameter); } ////// ApplyForeground command event handler. /// private static void OnApplyForeground(object target, ExecutedRoutedEventArgs args) { if (args.Parameter == null) { return; // Ignore the command if no argument provided } TextEditor This = TextEditor._GetTextEditor(target); TextEditorCharacters._OnApplyProperty(This, TextElement.ForegroundProperty, args.Parameter); } ////// ApplyBackground command event handler. /// private static void OnApplyBackground(object target, ExecutedRoutedEventArgs args) { if (args.Parameter == null) { return; // Ignore the command if no argument provided } TextEditor This = TextEditor._GetTextEditor(target); TextEditorCharacters._OnApplyProperty(This, TextElement.BackgroundProperty, args.Parameter); } ////// ToggleSpellCheck command event handler. /// private static void OnToggleSpellCheck(object target, ExecutedRoutedEventArgs args) { TextEditor This = TextEditor._GetTextEditor(target); if (This == null || !This._IsEnabled || This.IsReadOnly) { return; } This.IsSpellCheckEnabled = !This.IsSpellCheckEnabled; } ////// ApplyInlineFlowDirectionRTL command event handler. /// private static void OnApplyInlineFlowDirectionRTL(object target, ExecutedRoutedEventArgs args) { TextEditor This = TextEditor._GetTextEditor(target); TextEditorCharacters._OnApplyProperty(This, Inline.FlowDirectionProperty, FlowDirection.RightToLeft); } ////// ApplyInlineFlowDirectionLTR command event handler. /// private static void OnApplyInlineFlowDirectionLTR(object target, ExecutedRoutedEventArgs args) { TextEditor This = TextEditor._GetTextEditor(target); TextEditorCharacters._OnApplyProperty(This, Inline.FlowDirectionProperty, FlowDirection.LeftToRight); } // ---------------------------------------------------------- // // Misceleneous Commands // // ---------------------------------------------------------- #region Misceleneous Commands ////// StartInputCorrection command QueryStatus handler /// private static void OnQueryStatusNYI(object target, CanExecuteRoutedEventArgs args) { TextEditor This = TextEditor._GetTextEditor(target); if (This == null) { return; } args.CanExecute = true; } #endregion Misceleneous Commands #endregion Private Methods } } // 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
- NonValidatingSecurityTokenAuthenticator.cs
- ToolStripComboBox.cs
- NameTable.cs
- ScaleTransform3D.cs
- DiffuseMaterial.cs
- BlockUIContainer.cs
- CompilationLock.cs
- TrackingMemoryStreamFactory.cs
- XmlSchemaInclude.cs
- AsyncResult.cs
- ScrollProperties.cs
- UniformGrid.cs
- NameObjectCollectionBase.cs
- MetadataProperty.cs
- ExtendedProtectionPolicy.cs
- SessionMode.cs
- CardSpacePolicyElement.cs
- DesignerLoader.cs
- HyperLinkField.cs
- EntityClassGenerator.cs
- InputScopeAttribute.cs
- XmlSerializerSection.cs
- DocumentGridPage.cs
- PathSegmentCollection.cs
- KeyValuePair.cs
- AssemblyName.cs
- ScrollData.cs
- MultipartIdentifier.cs
- SettingsSavedEventArgs.cs
- DesignerTransaction.cs
- TypeElement.cs
- NetworkInterface.cs
- GeometryConverter.cs
- WorkflowRuntimeSection.cs
- Query.cs
- HttpCacheVary.cs
- IDataContractSurrogate.cs
- StaticSiteMapProvider.cs
- WebAdminConfigurationHelper.cs
- DataKeyArray.cs
- WindowsPen.cs
- CngProperty.cs
- EmptyReadOnlyDictionaryInternal.cs
- DefaultObjectMappingItemCollection.cs
- DocumentViewer.cs
- FreezableOperations.cs
- TreeNodeCollection.cs
- log.cs
- SafeEventLogWriteHandle.cs
- CompilerErrorCollection.cs
- DateTimePicker.cs
- CodeGenerationManager.cs
- FileDetails.cs
- DynamicActivity.cs
- TypeDescriptionProvider.cs
- Claim.cs
- PathNode.cs
- DiscreteKeyFrames.cs
- XmlNullResolver.cs
- ObjectTag.cs
- HtmlDocument.cs
- Calendar.cs
- SecurityCriticalDataForSet.cs
- SetIterators.cs
- ConfigurationStrings.cs
- StreamUpgradeBindingElement.cs
- CollectionViewGroup.cs
- StreamAsIStream.cs
- RTLAwareMessageBox.cs
- TextReturnReader.cs
- OutputCacheProfile.cs
- Registry.cs
- TimerElapsedEvenArgs.cs
- FileDataSourceCache.cs
- BamlRecords.cs
- SelectionService.cs
- TabControlToolboxItem.cs
- ProtectedUri.cs
- Point3D.cs
- CodeRegionDirective.cs
- sqlpipe.cs
- ToolStripContentPanel.cs
- ImageSourceTypeConverter.cs
- PointF.cs
- ToolboxItemCollection.cs
- CodeStatement.cs
- SoapFaultCodes.cs
- Cursor.cs
- StylusPlugInCollection.cs
- SevenBitStream.cs
- DetailsViewRow.cs
- InputBinder.cs
- Attribute.cs
- TimelineGroup.cs
- MessageBox.cs
- PropertyItemInternal.cs
- SurrogateEncoder.cs
- PackWebRequestFactory.cs
- TextViewElement.cs
- TimeIntervalCollection.cs