// $Revision: 1.3 $ // Copyright (C) 1994-1995 Taligent, Inc. All rights reserved. #ifndef TaligentSamples_SAMPLESELECTTEXTINTERACTOR #include "SampleSelectTextInteractor.h" #endif #ifndef TaligentSamples_STANDARDSELECTSTATES #include "StandardSelectStates.h" #endif #ifndef TaligentSamples_DISJOINTSELECTSTATES #include "DisjointSelectStates.h" #endif #ifndef Taligent_TEXTVIEW #include #endif #ifndef Taligent_TEXTPRESENTERSTATE #include #endif #ifndef Taligent_TEXTSELECTION #include #endif #ifndef Taligent_TEXTMODEL #include #endif #ifndef Taligent_COMPOUNDDOCUMENT #include #endif #ifndef Taligent_BREAKWORD #include #endif #ifndef Taligent_KEYBOARDINPUT #include #endif MCollectibleDefinitionsMacro(TSampleSelectTextInteractor, 0); TSampleSelectTextInteractor::TSampleSelectTextInteractor() #if 0 : TInteractor(), MMouseEventHandler(), #else : TSelectTextInteractor(), #endif fView(NIL), fLastOffset(0), fSelectState(NIL) { } TSampleSelectTextInteractor::TSampleSelectTextInteractor(TTextView* view) #if 0 : TInteractor(), MMouseEventHandler(), #else : TSelectTextInteractor(view), #endif fView(view), fLastOffset(0), fSelectState(NIL) { Assertion(fView != NIL); } TSampleSelectTextInteractor::TSampleSelectTextInteractor(const TSampleSelectTextInteractor& source) #if 0 : TInteractor(source), MMouseEventHandler(source), #else : TSelectTextInteractor(source), #endif fView(source.fView), fLastOffset(source.fLastOffset), fSelectState(::CopyPointer(source.fSelectState)) { } TSampleSelectTextInteractor::~TSampleSelectTextInteractor() { delete fSelectState; } bool TSampleSelectTextInteractor::MouseDown(TMouseDownEvent& mouseDown) { StartMouseMovedEvents(*mouseDown.GetMouseInputDevice()); Setup(mouseDown); Assertion(fSelectState != NIL); TGPoint position = mouseDown.GetEventPosition(); TInsertionOffset insertionOffset; fView->CoordinateToInsertionOffset(position, insertionOffset); fLastOffset = insertionOffset; fSelectState->Setup(insertionOffset, fView->GetTextSelection()->GetTextArea()); TTextArea selection; fSelectState->Track(insertionOffset, selection); fView->SetCurrentSelection(selection); return true; } bool TSampleSelectTextInteractor::MouseMoved(TMouseMovedEvent& mouseMove) { TGPoint position = mouseMove.GetEventPosition(); TInsertionOffset insertionOffset; fView->CoordinateToInsertionOffset(position, insertionOffset); if (insertionOffset != fLastOffset) { fLastOffset = insertionOffset; TTextArea selection; fSelectState->Track(insertionOffset, selection); fView->SetCurrentSelection(selection); } return true; } bool TSampleSelectTextInteractor::MouseUp(TMouseUpEvent& mouseUp) { SetDone(true); return true; } void TSampleSelectTextInteractor::Setup(TMouseDownEvent& mouseDown) { TSelectState* state = NIL; TTextChunkIteratorReference iteratorReference; switch (mouseDown.GetClickCount()) { case 1: break; case 2: TTextChunkIterator::GetPreferredWordSelectionIteratorReference(iteratorReference); break; case 3: default: TTextChunkIterator::GetPreferredSentenceSelectionIteratorReference(iteratorReference); break; } MTextRepresentation* textRepresentation = fView->GetTextRepresentation(); const TKeyboardInputDevice* keyDevice = TKeyboardInputDevice::GetDefaultKeyboardInputDevice(mouseDown); TModifierKeys modifierKeys = keyDevice->GetModifierKeys(); if (modifierKeys.IsPowerKeyDown()) { if (modifierKeys.IsShiftKeyDown()) { state = new TExtendDisjointSelectState(textRepresentation, iteratorReference); } else { state = new TDisjointSelectState(textRepresentation,iteratorReference); } } else { if (modifierKeys.IsShiftKeyDown()) { state = new TExtendSelectState(textRepresentation, iteratorReference); } else { state = new TSetSelectState(textRepresentation, iteratorReference); } } fSelectState = state; } // =============== TaligentTypeExtensionMacro_Abstract(TSelectState); TSelectState::TSelectState(MTextRepresentation* textRepresentation, TTextChunkIteratorReference& iteratorReference) : fReadEntry(textRepresentation), fChunkIterator(NIL) { Assertion(textRepresentation != NIL); fChunkIterator = iteratorReference.CreateTextChunkIterator(fReadEntry.GetTextForReading()); } TSelectState::~TSelectState() { delete fChunkIterator; } void TSelectState::ComputeSelection(const TInsertionOffset& insertionOffset, TTextArea& selection) { if (fChunkIterator) { TTextRange range; fChunkIterator->Set(insertionOffset); fChunkIterator->Current(range); selection.SetRegion(range); } else { selection.SetInsertionOffset(insertionOffset); } } TSelectState::TSelectState(const TSelectState&) : fReadEntry((MTextRepresentation*)NIL), fChunkIterator(NIL) { Assertion(false, "do not copy TSelectState."); } TSelectState& TSelectState::operator=(const TSelectState&) { Assertion(false, "do not copy TSelectState."); return *this; }