// $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(), fTrackingEntryExitEvents(false), fNumberOfMiceWithinMe(0) { } TMouseInputView::TMouseInputView(const TGPoint& size, const TGPoint& location) : TContentView(size, location), MMouseEventHandler(), fTrackingEntryExitEvents(false), fNumberOfMiceWithinMe(0) { } 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)); } 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 (!fTrackingEntryExitEvents) { text = TStandardText("Double-click to start and stop entry/exit events."); } else { static const TUniversalNumberFormatter formatter; TStandardText mouseCount; formatter.Format(TFormattableNumber(fNumberOfMiceWithinMe), mouseCount); text = TStandardText("Number of mice: ") + mouseCount; } 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 multiple mice yet. bool TMouseInputView::MouseEntered(TMouseMovedEvent& mouseMoved) { fNumberOfMiceWithinMe++; Invalidate(); return true; } bool TMouseInputView::MouseExited(TMouseMovedEvent& mouseMoved) { fNumberOfMiceWithinMe--; Invalidate(); return true; } bool TMouseInputView::MouseButtonDown(TMouseDownEvent& mouseDownEvent, short) { if (mouseDownEvent.GetClickCount() > 1) { fTrackingEntryExitEvents = !fTrackingEntryExitEvents; if (fTrackingEntryExitEvents) { fNumberOfMiceWithinMe = 0; // reset StartMouseEntryEvents(); } else { StopMouseEntryEvents(); } Invalidate(); } return true; } void TMouseInputView::HandleAllocatedAreaChanged(const TGArea&) { InvalidateAll(); }