// $Revision: 1.3 $ //Copyright (c) 1994-1995 Taligent, Inc. All rights reserved. #ifndef TaligentSamples_KEYINPUTINTERACTOR #include "KeyInputInteractor.h" #endif #ifndef Taligent_ASSERTIONS #include #endif #ifndef _IOSTREAMH #include #endif VersionDefinitionsMacro(TKeyInputInteractor, kOriginalVersion); TKeyInputInteractor::TKeyInputInteractor() : TInteractor(), MKeyEventHandler(), fViewHandle(), fState(0), fString("!") { } TKeyInputInteractor::TKeyInputInteractor(TKeyInputView* view, const TText& string) : TInteractor(), MKeyEventHandler(), fViewHandle(), fState(0), fString(string) { Assertion(view != NIL); Assertion(string.GetLength() > 0); fViewHandle = TViewHandle(*view); view->AddInteractor(this); } TKeyInputInteractor::TKeyInputInteractor(const TKeyInputInteractor& source) : TInteractor(source), MKeyEventHandler(source), fViewHandle(), fState(source.fState), fString(source.fString) { Assertion(false, "do not copy TKeyInputInteractor"); } TKeyInputInteractor::~TKeyInputInteractor() { TKeyInputView* view = (TKeyInputView*)fViewHandle.GetView(); if (view) { view->RemoveInteractor(*this); } } TKeyInputInteractor& TKeyInputInteractor::operator=(const TKeyInputInteractor& source) { if (&source != this) { TInteractor::operator=(source); MKeyEventHandler::operator=(source); fState = source.fState; fString = source.fString; } return *this; } TStream& TKeyInputInteractor::operator>>=(TStream& toStream) const { ::WriteVersion(toStream, kOriginalVersion); TInteractor::operator>>=(toStream); fState >>= toStream; fString >>= toStream; return toStream; } TStream& TKeyInputInteractor::operator<<=(TStream& fromStream) { ::ReadVersion(fromStream, kOriginalVersion, kOriginalVersion); TInteractor::operator<<=(fromStream); fState <<= fromStream; fString <<= fromStream; return fromStream; } // The view has been deactivated, so we'll assume we have to reset our state. // When the new is next activated, typing will have to begin anew. void TKeyInputInteractor::HandleDeactivate() { cout << "deactivate" << endl; SetState(0); } // Match the next event against the string. If it matches, increment the state, and if // we have a complete match, call MatchedString. If it doesn't match, reset. bool TKeyInputInteractor::KeyDown(TKeyDownEvent& keyDownEvent) { TStandardText text; fTypingConfiguration.MapKeyEventToText(keyDownEvent, text); if (text.GetLength()) { UniChar keyChar = text[0]; if (keyChar == fString[fState]) { SetState(fState + 1); if (fState == fString.GetLength()) { MatchedString(); } } else { SetState(0); } } return true; } short TKeyInputInteractor::GetState() const { return fState; } // For illustration, echo to cout the extent of the key currently matched. void TKeyInputInteractor::SetState(short newState) { if (newState != fState) { fState = newState; if (fState > 0) { TStandardText text; fString.Extract(TTextRange(0, TTextOffset(fState)), text); QPrintText(text, false); } else { cout << "reset"; } cout << endl; } } // The interactor has parsed the input stream into a special higher-level semantic. Inform // the view, and SetDone to true. The interactor would swallow all events from this point // on, but if it's been attached to the input device (as it has since TEvent.:StartInteractor // was used to start it) the device will detect that it's done and delete it, resuming normal // event dispatching. void TKeyInputInteractor::MatchedString() { cout << "match" << endl; TKeyInputView* view = (TKeyInputView*)fViewHandle.GetView(); if (view) { view->ToggleColor(); } SetDone(true); }