// $Revision: 1.4 $ // Copyright (c) 1994-1995 Taligent, Inc. All rights reserved. #ifndef TaligentSamples_STANDARDSELECTSTATES #include "StandardSelectStates.h" #endif TaligentTypeExtensionMacro_Abstract(TSetSelectState); TSetSelectState::TSetSelectState(MTextRepresentation* textRepresentation, TTextChunkIteratorReference& iteratorReference) : TSelectState(textRepresentation, iteratorReference) { } TSetSelectState::~TSetSelectState() { } void TSetSelectState::Setup(const TInsertionOffset& initialOffset, const TTextArea& initialArea) { TTextArea area; ComputeSelection(initialOffset, area); SetAnchor(area); } void TSetSelectState::Track(const TInsertionOffset& trackingOffset, TTextArea& resultArea) { ComputeSelection(trackingOffset, resultArea); ExtendArea(resultArea); } void TSetSelectState::SetAnchor(const TTextArea& area) { fAnchor = area; } // TextRegions cannot contain insertion points or zero-length TextRanges. // So we can't use the computation operations defined on regions, and have // to hack it ourselves. And unfortunately, there is no union operation // defined on ranges either, so we have to hack that ourselves too. void TSetSelectState::ExtendArea(TTextArea& resultArea) { if (resultArea != fAnchor) { TTextRange anchorRange; if (fAnchor.IsInsertionPoint()) { TInsertionOffset insertionOffset; fAnchor.GetInsertionOffset(insertionOffset); anchorRange.SetBeginAndEnd(insertionOffset, insertionOffset); } else { TTextRegion region; fAnchor.GetRegion(region); region.GetBounds(anchorRange); } TTextRange resultRange; if (resultArea.IsInsertionPoint()) { TInsertionOffset insertionOffset; resultArea.GetInsertionOffset(insertionOffset); resultRange.SetBeginAndEnd(insertionOffset, insertionOffset); } else { TTextRegion region; resultArea.GetRegion(region); region.GetBounds(resultRange); } if (anchorRange.GetBegin() < resultRange.GetBegin()) { resultRange.SetBegin(anchorRange.GetBegin()); } if (anchorRange.GetEnd() > resultRange.GetEnd()) { resultRange.SetEnd(anchorRange.GetEnd()); } resultArea = resultRange; } } // ========================================================================= TaligentTypeExtensionMacro_Abstract(TExtendSelectState); TExtendSelectState::TExtendSelectState(MTextRepresentation* textRepresentation, TTextChunkIteratorReference& iteratorReference) : TSetSelectState(textRepresentation, iteratorReference) { } TExtendSelectState::~TExtendSelectState() { } void TExtendSelectState::Setup(const TInsertionOffset& initialOffset, const TTextArea& initialArea) { if (initialArea.IsInsertionPoint()) { SetAnchor(initialArea); } else { TTextRegion region; initialArea.GetRegion(region); TTextRange range; region.GetBounds(range); TTextOffset lowAnchor = range.GetBegin(); TTextOffset highAnchor = range.GetEnd(); TTextOffset midAnchor = (TTextOffset)(((long)lowAnchor + (long)highAnchor) / 2); TTextOffset offset = initialOffset.GetOffset(); TTextOffset anchor = lowAnchor; if (offset < midAnchor) { anchor = highAnchor; } SetAnchor(TTextArea(TInsertionOffset(anchor, TInsertionOffset::kBeforeOffset))); } }