Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Framework / System / Windows / Documents / Highlights.cs / 1 / Highlights.cs
//---------------------------------------------------------------------------- // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // // Description: Text highlights associated with a TextContainer. // // History: // 7/1/2004 : [....] - Created. // //--------------------------------------------------------------------------- using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using MS.Internal; namespace System.Windows.Documents { ////// Text highlights associated with a TextContainer. /// internal class Highlights { //----------------------------------------------------- // // Constructors // //----------------------------------------------------- #region Constructors // Constructor. internal Highlights(ITextContainer textContainer) { _textContainer = textContainer; } #endregion Constructors //------------------------------------------------------ // // Internal Methods // //----------------------------------------------------- #region Internal Methods ////// Returns the value of a property stored on scoping highlight, if any. /// /// /// Position to query. /// /// /// Direction of content to query. /// /// /// Type of the matching highlight layer owner. /// ////// The highlight value if set on any scoping highlight. If no property /// value is set, returns DependencyProperty.UnsetValue. /// internal virtual object GetHighlightValue(StaticTextPointer textPosition, LogicalDirection direction, Type highlightLayerOwnerType) { int layerIndex; object value; HighlightLayer layer; value = DependencyProperty.UnsetValue; // Take the value on the closest layer. "Closest" == added first. for (layerIndex = 0; layerIndex < this.LayerCount; layerIndex++) { layer = GetLayer(layerIndex); if (layer.OwnerType == highlightLayerOwnerType) { value = layer.GetHighlightValue(textPosition, direction); if (value != DependencyProperty.UnsetValue) break; } } return value; } ////// Returns true iff the indicated content has scoping highlights. /// /// /// Position to query. /// /// /// Direction of content to query. /// internal virtual bool IsContentHighlighted(StaticTextPointer textPosition, LogicalDirection direction) { int i; for (i = 0; i < this.LayerCount; i++) { if (GetLayer(i).IsContentHighlighted(textPosition, direction)) { break; } } return (i < this.LayerCount); } ////// Returns the position of the next highlight start or end in an /// indicated direction, or null if there is no such position. /// /// /// Position to query. /// /// /// Direction of content to query. /// internal virtual StaticTextPointer GetNextHighlightChangePosition(StaticTextPointer textPosition, LogicalDirection direction) { StaticTextPointer changePosition; StaticTextPointer closestChangePosition; int i; closestChangePosition = StaticTextPointer.Null; // Calculate the min of the layers' transitions. for (i = 0; i < this.LayerCount; i++) { changePosition = GetLayer(i).GetNextChangePosition(textPosition, direction); if (!changePosition.IsNull) { if (closestChangePosition.IsNull) { closestChangePosition = changePosition; } else if (direction == LogicalDirection.Forward) { closestChangePosition = StaticTextPointer.Min(closestChangePosition, changePosition); } else { closestChangePosition = StaticTextPointer.Max(closestChangePosition, changePosition); } } } return closestChangePosition; } ////// Returns the closest neighboring TextPointer in an indicated /// direction where a property value calculated from an embedded /// object, scoping text element, or scoping highlight could /// change. /// /// /// Position to query. /// /// /// Direction of content to query. /// ////// If the following symbol is TextPointerContext.EmbeddedElement, /// TextPointerContext.ElementBegin, or TextPointerContext.ElementEnd, returns /// a TextPointer exactly one symbol distant. /// /// If the following symbol is TextPointerContext.Text, the distance /// of the returned TextPointer is the minimum of the value returned /// by textPosition.GetTextLength and the distance to any highlight /// start or end edge. /// /// If the following symbol is TextPointerContext.None, returns null. /// internal virtual StaticTextPointer GetNextPropertyChangePosition(StaticTextPointer textPosition, LogicalDirection direction) { StaticTextPointer changePosition; StaticTextPointer characterRunEndPosition; switch (textPosition.GetPointerContext(direction)) { case TextPointerContext.None: changePosition = StaticTextPointer.Null; break; case TextPointerContext.Text: changePosition = GetNextHighlightChangePosition(textPosition, direction); characterRunEndPosition = textPosition.GetNextContextPosition(LogicalDirection.Forward); if (changePosition.IsNull || characterRunEndPosition.CompareTo(changePosition) < 0) { changePosition = characterRunEndPosition; } break; case TextPointerContext.EmbeddedElement: case TextPointerContext.ElementStart: case TextPointerContext.ElementEnd: default: changePosition = textPosition.CreatePointer(+1); break; } return changePosition; } ////// Adds a HighlightLayer to this collection. /// /// /// HighlightLayer to add. /// internal void AddLayer(HighlightLayer highlightLayer) { // Delay alloc the layers store. if (_layers == null) { _layers = new ArrayList(1); } Invariant.Assert(!_layers.Contains(highlightLayer)); _layers.Add(highlightLayer); highlightLayer.Changed += new HighlightChangedEventHandler(OnLayerChanged); // Raise initial change event to cover existing content. RaiseChangedEventForLayerContent(highlightLayer); } ////// Removes a HighlightLayer to this collection. /// /// /// HighlightLayer to add. /// internal void RemoveLayer(HighlightLayer highlightLayer) { Invariant.Assert(_layers != null && _layers.Contains(highlightLayer)); // Raise final change event to cover existing content. RaiseChangedEventForLayerContent(highlightLayer); highlightLayer.Changed -= new HighlightChangedEventHandler(OnLayerChanged); _layers.Remove(highlightLayer); } ////// Retrieve a HighlightLayer from this collection /// /// /// ////// The first added HighlightLayer owned by the type. /// null if no such HighlightLayer can be found. /// internal HighlightLayer GetLayer(Type highlightLayerType) { int i; for (i = 0; i < this.LayerCount; i++) { if (highlightLayerType == GetLayer(i).OwnerType) { return GetLayer(i); } } return null; } #endregion Internal Methods //------------------------------------------------------ // // Protected Properties // //------------------------------------------------------ #region Protected Properties protected ITextContainer TextContainer { get { return _textContainer; } } #endregion Protected Properties //----------------------------------------------------- // // Internal Events // //------------------------------------------------------ #region Internal Events ////// Event raised when a highlight is inserted, removed, moved, or /// has a local property value change. /// internal event HighlightChangedEventHandler Changed; #endregion Internal Events //----------------------------------------------------- // // Private Methods // //----------------------------------------------------- #region Private Methods // Returns a contained HighlightLayer. private HighlightLayer GetLayer(int index) { return (HighlightLayer)_layers[index]; } // Callback for changes on a single layer. private void OnLayerChanged(object sender, HighlightChangedEventArgs args) { // Forward on the event to any listeners. if (this.Changed != null) { Changed(this, args); } } // Raises initial or final change event for added/removed layers. private void RaiseChangedEventForLayerContent(HighlightLayer highlightLayer) { Listranges; StaticTextPointer highlightTransitionPosition; StaticTextPointer highlightRangeStart; if (this.Changed != null) { // Build a list of all highlights in this layer -- they're all // going to "change" as the layer is added/removed. ranges = new List (); highlightTransitionPosition = _textContainer.CreateStaticPointerAtOffset(0); while (true) { // Move to the next highlight start. if (!highlightLayer.IsContentHighlighted(highlightTransitionPosition, LogicalDirection.Forward)) { highlightTransitionPosition = highlightLayer.GetNextChangePosition(highlightTransitionPosition, LogicalDirection.Forward); // No more highlights? if (highlightTransitionPosition.IsNull) break; } highlightRangeStart = highlightTransitionPosition; highlightTransitionPosition = highlightLayer.GetNextChangePosition(highlightTransitionPosition, LogicalDirection.Forward); Invariant.Assert(!highlightTransitionPosition.IsNull, "Highlight start not followed by highlight end!"); ranges.Add(new TextSegment(highlightRangeStart.CreateDynamicTextPointer(LogicalDirection.Forward), highlightTransitionPosition.CreateDynamicTextPointer(LogicalDirection.Forward))); } if (ranges.Count > 0) { Changed(this, new LayerHighlightChangedEventArgs(new ReadOnlyCollection (ranges), highlightLayer.OwnerType)); } } } #endregion Private Methods //----------------------------------------------------- // // Private Properties // //------------------------------------------------------ #region Private Properties // Count of contained HighlightLayers. private int LayerCount { get { return (_layers == null) ? 0 : _layers.Count; } } #endregion Private Properties //----------------------------------------------------- // // Private Types // //------------------------------------------------------ #region Private Types // EventArgs for the Changed event. private class LayerHighlightChangedEventArgs : HighlightChangedEventArgs { // Constructor. internal LayerHighlightChangedEventArgs(ReadOnlyCollection ranges, Type ownerType) { _ranges = ranges; _ownerType = ownerType; } // List of changed ranges. internal override IList Ranges { get { return _ranges; } } // Type identifying the owner of the changed layer. internal override Type OwnerType { get { return _ownerType; } } // List of changed ranges. private readonly ReadOnlyCollection _ranges; // Type identifying the owner of the changed layer. private readonly Type _ownerType; } #endregion Private Types //------------------------------------------------------ // // Private Fields // //----------------------------------------------------- #region Private Fields // Associated TextContainer. private readonly ITextContainer _textContainer; // List of contained HighlightLayers. private ArrayList _layers; #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
- DataGridView.cs
- WorkerRequest.cs
- XmlReader.cs
- OpenFileDialog.cs
- VisualTarget.cs
- ThreadExceptionDialog.cs
- Queue.cs
- AttributeData.cs
- TransactionInformation.cs
- TextPattern.cs
- XPathDocumentNavigator.cs
- DataColumnChangeEvent.cs
- BaseCAMarshaler.cs
- StackBuilderSink.cs
- ParserStreamGeometryContext.cs
- StackBuilderSink.cs
- ResourceExpressionBuilder.cs
- RemotingAttributes.cs
- CodeArrayIndexerExpression.cs
- DesignerOptionService.cs
- ObjectDataSourceDisposingEventArgs.cs
- KeyValueSerializer.cs
- Msec.cs
- WebPartTransformerAttribute.cs
- RuleSetDialog.cs
- X509SecurityTokenProvider.cs
- FieldDescriptor.cs
- SerializationInfo.cs
- DataTemplateSelector.cs
- WebRequestModuleElementCollection.cs
- OutputCacheProfile.cs
- ExpressionEditorAttribute.cs
- SqlEnums.cs
- DateTimeSerializationSection.cs
- ScrollChrome.cs
- TransformerInfoCollection.cs
- CapabilitiesAssignment.cs
- PageClientProxyGenerator.cs
- AggregateNode.cs
- EdmError.cs
- QueryResults.cs
- _CacheStreams.cs
- HttpAsyncResult.cs
- CollectionDataContract.cs
- WorkflowRuntimeServicesBehavior.cs
- Trace.cs
- CultureSpecificStringDictionary.cs
- StrongNameKeyPair.cs
- SoapTypeAttribute.cs
- DataGridHyperlinkColumn.cs
- BitStack.cs
- PasswordTextNavigator.cs
- LocalizationComments.cs
- LongAverageAggregationOperator.cs
- HttpResponseBase.cs
- LookupNode.cs
- RecipientInfo.cs
- RectAnimationBase.cs
- SoapElementAttribute.cs
- RotateTransform.cs
- ValidationSummary.cs
- BitmapCacheBrush.cs
- SoapSchemaImporter.cs
- FtpRequestCacheValidator.cs
- HelpInfo.cs
- DiscoveryDocumentLinksPattern.cs
- DataGridCellsPresenter.cs
- RectangleF.cs
- DbReferenceCollection.cs
- PathData.cs
- CultureData.cs
- PtsContext.cs
- AvTrace.cs
- AnnotationAuthorChangedEventArgs.cs
- PageFunction.cs
- Durable.cs
- XmlSchemaAnnotation.cs
- CommandBinding.cs
- MenuItemBinding.cs
- JournalEntryStack.cs
- Rect.cs
- StringFormat.cs
- ResourceReferenceExpressionConverter.cs
- DiscoveryClientProtocol.cs
- RenderDataDrawingContext.cs
- ComboBoxItem.cs
- EncoderExceptionFallback.cs
- StringUtil.cs
- SQLInt64Storage.cs
- FlatButtonAppearance.cs
- DBDataPermissionAttribute.cs
- RowUpdatedEventArgs.cs
- SetUserPreferenceRequest.cs
- PrintDialog.cs
- ErrorWrapper.cs
- DoubleCollectionConverter.cs
- XmlAggregates.cs
- Triplet.cs
- RegistryDataKey.cs
- OleDbConnection.cs