// $Revision: 1.4 $ // Copyright (c) 1994-1995 Taligent, Inc. All rights reserved. #ifndef TaligentSamples_KEYINPUTVIEW #include "KeyInputView.h" #endif #ifndef TaligentSamples_KEYINPUTINTERACTOR #include "KeyInputInteractor.h" #endif #ifndef Taligent_ASSERTIONS #include #endif #ifndef Taligent_GRAFPORT #include #endif #ifndef Taligent_NUMBERFORMAT #include #endif #ifndef Taligent_RGBCOLOR #include #endif #ifndef Taligent_TEXTDISPLAY #include #endif #ifndef _IOSTREAMH #include #endif MCollectibleDefinitionsMacro(TKeyInputView, kOriginalVersion); TKeyInputView::TKeyInputView() : TContentView(TGPoint(200, 50)), MKeyEventHandler(), fLastKey(TVirtualKeyCode::kUndefinedVirtualKey), fColorIsLight(true), fInteractorAlias(NIL) { } TKeyInputView::TKeyInputView(const TGPoint& size, const TGPoint& location) : TContentView(size, location), MKeyEventHandler(), fLastKey(TVirtualKeyCode::kUndefinedVirtualKey), fColorIsLight(true), fInteractorAlias(NIL) { } // Tell the TKeyInputInteractor to NIL its alias to this view. It in turn // will NIL this view's alias to it. TKeyInputView::~TKeyInputView() { if (fInteractorAlias) { fInteractorAlias->SetDone(); // fInteractorAlias is now NIL } } void TKeyInputView::HandleAfterConnectionToViewRoot() { RegisterSimpleTarget(*InternalGetEventReceiver()); } void TKeyInputView::HandleBeforeDisconnectionFromViewRoot() { DeregisterSimpleTarget(*InternalGetEventReceiver()); } void TKeyInputView::HandleActivate() { if (fInteractorAlias) { fInteractorAlias->Activate(); } } void TKeyInputView::HandleDeactivate() { if (fInteractorAlias) { fInteractorAlias->Deactivate(); } } void TKeyInputView::AddInteractor(TInteractor* interactor) { Assertion(interactor != NIL); if (fInteractorAlias == NIL) { fInteractorAlias = interactor; } } void TKeyInputView::RemoveInteractor(const TInteractor& interactor) { if (fInteractorAlias == &interactor) { fInteractorAlias = NIL; } } // Display the color state and the code of the last key we received. void TKeyInputView::DrawContents(TGrafPort& port) const { static const TFillBundle lightBundle(TRGBColor(1, 1, 1)); static const TFillBundle darkBundle(TRGBColor(0.5, 0.8, 1.0)); TGArea area; GetBounds(area); port.Draw(area, fColorIsLight ? lightBundle : darkBundle); static TUniversalNumberFormatter formatter; TStandardText text; formatter.Format(TFormattableNumber(fLastKey), text); TTextDisplay display(TStandardText("Key code: ") + text); TGRect textBounds = area.GetBounds(); textBounds.Inset(TGPoint(5, 5)); display.SetBoundaryRectangle(textBounds); display.SetPointSize(TFontPointSizeStyle(18)); display.Draw(port); } // If the power key is down, start an interactor that waits for keys that come through // that match "color". Otherwise just remember the key and invalidate. bool TKeyInputView::KeyDown(TKeyDownEvent& keyDownEvent) { if (keyDownEvent.GetModifierKeys().IsPowerKeyDown()) { keyDownEvent.StartInteractor(new TKeyInputInteractor(this, TStandardText("color"))); } else { fLastKey = keyDownEvent.GetVirtualKey(); Invalidate(); } return true; } // The interactor will call this member function when it successfully parses the input stream. void TKeyInputView::ToggleColor() { fColorIsLight = !fColorIsLight; Invalidate(); } void TKeyInputView::HandleAllocatedAreaChanged(const TGArea&) { InvalidateAll(); }