// $Revision: 1.4 $ // Copyright (c) 1994-1995 Taligent, Inc. All rights reserved. #ifndef TaligentSamples_KEYINPUTVIEW #include "KeyInputView.h" #endif #ifndef Taligent_GRAFPORT #include #endif #ifndef Taligent_NUMBERFORMAT #include #endif #ifndef Taligent_RGBCOLOR #include #endif #ifndef Taligent_TEXTDISPLAY #include #endif MCollectibleDefinitionsMacro(TKeyInputView, kOriginalVersion); TKeyInputView::TKeyInputView() : TContentView(TGPoint(200, 50)), MKeyEventHandler(), fLastKey(TVirtualKeyCode::kUndefinedVirtualKey) { } TKeyInputView::TKeyInputView(const TGPoint& size, const TGPoint& location) : TContentView(size, location), MKeyEventHandler(), fLastKey(TVirtualKeyCode::kUndefinedVirtualKey) { } TKeyInputView::~TKeyInputView() { } // We use the event receiver in the view root, so need to wait until after we're // connected before we can register with it. Similarly, we need to deregister // before we're disconnected. void TKeyInputView::HandleAfterConnectionToViewRoot() { RegisterSimpleTarget(*InternalGetEventReceiver()); } void TKeyInputView::HandleBeforeDisconnectionFromViewRoot() { DeregisterSimpleTarget(*InternalGetEventReceiver()); } // Draw the key code of the last key we received. void TKeyInputView::DrawContents(TGrafPort& port) const { static const TFillAndFrameBundle bundle(TRGBColor(1,1,1), TRGBColor(0,0,0), 2.0, TPen::kInsetFrame); TGArea area; GetBounds(area); port.Draw(area, bundle); static const 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); } // Remember the key code, and invalidate so the new code is displayed. // Return true to stop the event from being distributed any further. bool TKeyInputView::KeyDown(TKeyDownEvent& keyDownEvent) { fLastKey = keyDownEvent.GetVirtualKey(); Invalidate(); return true; } void TKeyInputView::HandleAllocatedAreaChanged(const TGArea&) { InvalidateAll(); }