// $Revision: 1.4 $ // Copyright (c) 1994-1995 Taligent, Inc. All rights reserved. #ifndef TaligentSamples_MOUSEINPUTVIEW #include "MouseInputView.h" #endif #ifndef Taligent_GRAFPORT #include #endif #ifndef Taligent_NUMBERFORMAT #include #endif #ifndef Taligent_RGBCOLOR #include #endif #ifndef Taligent_TEXTDISPLAY #include #endif #ifndef Taligent_KEYBOARDINPUT #include #endif MCollectibleDefinitionsMacro(TMouseInputView, kOriginalVersion); TMouseInputView::TMouseInputView() : TContentView(TGPoint(300, 50)), MMouseEventHandler(), fLastButtonChanged(0), fLastButtonChangeWasDown(false), fLastPowerKeyDown(false), fLastButtonChangeViewCoordinates(TGPoint::kOrigin) { } TMouseInputView::TMouseInputView(const TGPoint& size, const TGPoint& location) : TContentView(size, location), MMouseEventHandler(), fLastButtonChanged(0), fLastButtonChangeWasDown(false), fLastPowerKeyDown(false), fLastButtonChangeViewCoordinates(TGPoint::kOrigin) { } TMouseInputView::~TMouseInputView() { } // Calling SetCoordinateView tells the input system to provide the mouse location // in this view's coordinate system, rather than global coordinates. void TMouseInputView::HandleAfterConnectionToViewRoot() { RegisterSimpleTarget(*InternalGetEventReceiver()); SetCoordinateView(TViewHandle(*this)); } void TMouseInputView::HandleBeforeDisconnectionFromViewRoot() { DeregisterSimpleTarget(*InternalGetEventReceiver()); } // Draw the information about the last mouse event that we received. void TMouseInputView::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); TStandardText text; if (fLastButtonChanged == 0) { text = TStandardText("No mouse changes received yet."); } else { static TUniversalNumberFormatter formatter; TStandardText buttonNumber; formatter.Format(TFormattableNumber(fLastButtonChanged), buttonNumber); TStandardText change = TStandardText(fLastButtonChangeWasDown ? " Down" : " Up"); TStandardText xLoc; formatter.Format(TFormattableNumber(fLastButtonChangeViewCoordinates.fX), xLoc); TStandardText yLoc; formatter.Format(TFormattableNumber(fLastButtonChangeViewCoordinates.fY), yLoc); TStandardText powerKeyState = TStandardText(fLastPowerKeyDown ? " Down" : " Up"); text = TStandardText("Button ") + buttonNumber + change + TStandardText (" at ") + xLoc + TStandardText("@") + yLoc + TStandardText(" Power key") + powerKeyState; } TTextDisplay display(text); TGRect textBounds = area.GetBounds(); textBounds.Inset(TGPoint(5, 5)); display.SetBoundaryRectangle(textBounds); display.SetPointSize(TFontPointSizeStyle(18)); display.Draw(port); } // To find out the state of the keyboard when the mouse went down, ask the keyboard // input device, passing it the event. bool TMouseInputView::MouseButtonDown(TMouseDownEvent& mouseDownEvent, short buttonNumber) { fLastButtonChanged = mouseDownEvent.GetButtonNumber(); fLastButtonChangeWasDown = true; fLastButtonChangeViewCoordinates = mouseDownEvent.GetEventPosition(); const TKeyboardInputDevice* device = TKeyboardInputDevice::GetDefaultKeyboardInputDevice(mouseDownEvent); if (device) { fLastPowerKeyDown = device->GetModifierKeys().IsPowerKeyDown(); } else { fLastPowerKeyDown = false; } Invalidate(); return true; } bool TMouseInputView::MouseButtonUp(TMouseUpEvent& mouseUpEvent, short buttonNumber) { fLastButtonChanged = mouseUpEvent.GetButtonNumber(); fLastButtonChangeWasDown = false; fLastButtonChangeViewCoordinates = mouseUpEvent.GetEventPosition(); const TKeyboardInputDevice* device = TKeyboardInputDevice::GetDefaultKeyboardInputDevice(mouseUpEvent); if (device) { fLastPowerKeyDown = device->GetModifierKeys().IsPowerKeyDown(); } else { fLastPowerKeyDown = false; } Invalidate(); return true; }