// $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); TPseudoTimeStamp gTimeStamp; TMouseInputView::TMouseInputView() : TContentView(TGPoint(300, 50)), MMouseEventHandler(), fTrackedMouse(NIL), fLastLocation(TGPoint::kOrigin) { } TMouseInputView::TMouseInputView(const TGPoint& size, const TGPoint& location) : TContentView(size, location), MMouseEventHandler(), fTrackedMouse(NIL), fLastLocation(TGPoint::kOrigin) { } TMouseInputView::~TMouseInputView() { } // You must set the coordinate view in order to receive entry and exit events for // that view. If you don't, you will never receive these events (and may in the // future cause an exception when you call StartMouseEntryEvents). void TMouseInputView::HandleAfterConnectionToViewRoot() { RegisterSimpleTarget(*InternalGetEventReceiver()); SetCoordinateView(TViewHandle(*this)); StartMouseEntryEvents(); } 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 (fTrackedMouse) { static const TUniversalNumberFormatter formatter; TStandardText xLoc; formatter.Format(TFormattableNumber(fLastLocation.fX), xLoc); TStandardText yLoc; formatter.Format(TFormattableNumber(fLastLocation.fY), yLoc); text = xLoc + TStandardText("@") + yLoc; } else { text = "No mouse in view."; } TTextDisplay display(text); TGRect textBounds = area.GetBounds(); textBounds.Inset(TGPoint(5, 5)); display.SetBoundaryRectangle(textBounds); display.SetPointSize(TFontPointSizeStyle(18)); display.Draw(port); } // !!! Despite appearances to the contrary, the input system does not really // support mustiple mice as of D36.6. // The input system can only track one mouse at a time. StartMouseMovedEvents // will forget about any previous mouse, and only report mouse moved events for // the new mouse. bool TMouseInputView::MouseEntered(TMouseMovedEvent& mouseEntry) { fTrackedMouse = mouseEntry.GetMouseInputDevice(); StartMouseMovedEvents(*fTrackedMouse); Invalidate(); return true; } // StopMouseMoved events stops movement reporting for any mouse. bool TMouseInputView::MouseExited(TMouseMovedEvent& mouseExit) { if (mouseExit.GetMouseInputDevice() == fTrackedMouse) { StopMouseMovedEvents(); Invalidate(); } return true; } bool TMouseInputView::MouseMoved(TMouseMovedEvent& mouseMoved) { fLastLocation = mouseMoved.GetEventPosition(); Invalidate(); return true; } void TMouseInputView::HandleAllocatedAreaChanged(const TGArea&) { InvalidateAll(); }