// $Revision: 1.4 $ // Copyright (c) 1994-1995 Taligent, Inc. All rights reserved. #ifndef TaligentSamples_GRAPHICVIEW #include "GraphicView.h" #endif #ifndef TaligentSamples_CLICKORDRAGINTERACTOR #include "ClickOrDragInteractor.h" #endif #ifndef TaligentSamples_SETCOLORINTERACTOR #include "SetColorInteractor.h" #endif #ifndef TaligentSamples_SETPOINTINTERACTOR #include "SetPointInteractor.h" #endif #ifndef Taligent_GRAFPORT #include #endif #ifndef Taligent_KEYBOARDINPUT #include #endif MCollectibleDefinitionsMacro(TGraphicView, kOriginalVersion); TGraphicView::TGraphicView() : TContentView(TGPoint(300, 50)), MMouseEventHandler(), fColor(TRGBColor(1.0, 1.0, 1.0)), fPoint(TGPoint::kOrigin), fInteractor(NIL), fDisplay(), fFormatter() { fDisplay.SetPointSize(TFontPointSizeStyle(18.0)); TGArea area; GetAllocatedArea(area); ResizeDisplay(area); } TGraphicView::TGraphicView(const TGPoint& size, const TGPoint& location) : TContentView(size, location), MMouseEventHandler(), fColor(TRGBColor(1.0, 1.0, 1.0)), fPoint(TGPoint::kOrigin), fInteractor(NIL), fDisplay(), fFormatter() { fDisplay.SetPointSize(TFontPointSizeStyle(18.0)); TGArea area; GetAllocatedArea(area); ResizeDisplay(area); } void TGraphicView::ResizeDisplay(const TGArea& area) { TGRect textBounds = area.GetBounds(); textBounds.Inset(TGPoint(5, 5)); fDisplay.SetBoundaryRectangle(textBounds); } TGraphicView::~TGraphicView() { } void TGraphicView::HandleAfterConnectionToViewRoot() { RegisterSimpleTarget(*InternalGetEventReceiver()); } void TGraphicView::HandleBeforeDisconnectionFromViewRoot() { DeregisterSimpleTarget(*InternalGetEventReceiver()); } void TGraphicView::SetColor(const TColor& color) { fColor = color; Invalidate(); } void TGraphicView::SetPoint(const TGPoint& point) { fPoint = point; Invalidate(); } // Draw background with current color and display current point. void TGraphicView::DrawContents(TGrafPort& port) const { TGArea area; GetBounds(area); port.Draw(area, TFillAndFrameBundle(fColor, TRGBColor(0,0,0), 2.0, TPen::kInsetFrame)); TStandardText xPos; fFormatter.Format(TFormattableNumber(fPoint.fX), xPos); TStandardText yPos; fFormatter.Format(TFormattableNumber(fPoint.fY), yPos); TStandardText text = xPos + TStandardText("@") + yPos; // Cast away const. ((TGraphicView*)this)->fDisplay.SetText(text); fDisplay.Draw(port); } void TGraphicView::AddInteractor(TInteractor* interactor) { if (fInteractor == NIL) { fInteractor = interactor; } } void TGraphicView::RemoveInteractor(const TInteractor& interactor) { if (fInteractor == &interactor) { fInteractor = NIL; } } // If no modifier key is down. start the ClickOrDragInteractor, passing it // the interactors to use for double-click or drag transitions. // If the power key is down, start just the SetColorInteractor. // If the power key is not down and the second power key is, start just the // SetPointInteractor. bool TGraphicView::MouseButtonDown(TMouseDownEvent& mouseDown, short) { const TKeyboardInputDevice* device = TKeyboardInputDevice::GetDefaultKeyboardInputDevice(mouseDown); if (device) { TModifierKeys modifierKeys = device->GetModifierKeys(); TInteractor* interactor = NIL; if (modifierKeys.IsPowerKeyDown()) { qprintf("starting set color interactor.\n"); interactor = new TSetColorInteractor(this); } else if (modifierKeys.IsSecondPowerKeyDown()) { qprintf("starting set point interactor.\n"); interactor = new TSetPointInteractor(this); } else { qprintf("starting click or drag interactor, double-click to set color, drag to set point.\n"); interactor = new TClickOrDragInteractor(this, new TSetColorInteractor(this), new TSetPointInteractor(this)); } mouseDown.StartInteractor(interactor); } return true; } void TGraphicView::HandleDeactivate() { if (fInteractor) { fInteractor->SetDone(true); } } void TGraphicView::HandleAllocatedAreaChanged(const TGArea& newArea) { ResizeDisplay(newArea); InvalidateAll(); }