// $Revision: 1.5 $ // Copyright (C) 1995 Taligent, Inc. All rights reserved. #ifndef TaligentSamples_GRAPHICDOCUMENTVIEW #include "GraphicDocumentView.h" #endif #ifndef Taligent_SCRAPITEMON #include #endif #ifndef TaligentSamples_SIMPLEDRAGANDDROPITEM #include #endif #ifndef Taligent_MOUSEDRAGANDDROP #include #endif VersionDefinitionsMacro(TGraphicDocumentView, TGraphicDocumentView::kOriginalVersion); TGraphicDocumentView::TGraphicDocumentView(TGUIBundle* bundle) : TDocumentComponentView(bundle), MDropAcceptor(TViewHandle(*this)) { const TModelPointerTo > model(bundle->GetModelReference()); const MGraphic* modelGraphic = model->GetValueForReading(); TGRect graphicSize = modelGraphic->GetLooseFitBounds(); SetAllocatedArea(TGArea(graphicSize)); } TGraphicDocumentView::TGraphicDocumentView() : TDocumentComponentView(), MDropAcceptor(TViewHandle(*this)) { } TGraphicDocumentView::TGraphicDocumentView(const TGraphicDocumentView& source) : TDocumentComponentView(source), MDropAcceptor(TViewHandle(*this)) { } TStream& TGraphicDocumentView::operator>>=(TStream& toStream) const { ::WriteVersion(toStream); TDocumentComponentView::operator>>=(toStream); return toStream; } TStream& TGraphicDocumentView::operator<<=(TStream& fromStream) { ::ReadVersion(fromStream, kOriginalVersion, kOriginalVersion); TDocumentComponentView::operator<<=(fromStream); return fromStream; } const MGraphic* TGraphicDocumentView::LookupGraphic() const { const TModelPointerTo > model(GetModelReference()); return model->GetValueForReading(); } void TGraphicDocumentView::AdoptGraphic(MGraphic* toGraphic) const { TModelPointerTo > model(GetModelReference()); model->AdoptValue(toGraphic); } void TGraphicDocumentView::DrawContents(TGrafPort& port) const { const MGraphic* originalGraphic = LookupGraphic(); if (originalGraphic) { TLinkedModelMatrixPort matrixPort(&port, fGrafMatrix); originalGraphic->Draw(matrixPort); } } void TGraphicDocumentView::HandleAllocatedAreaChanged(const TGArea& newArea) { TDocumentComponentView::HandleAllocatedAreaChanged(newArea); TGRect areaBounds = newArea.GetBounds(); TGRect graphicBounds = LookupGraphic()->GetLooseFitBounds(); GCoordinate xScale = areaBounds.GetWidth() / graphicBounds.GetWidth(); GCoordinate yScale = areaBounds.GetHeight() / graphicBounds.GetHeight(); fGrafMatrix.SetToScale(TGPoint(xScale, yScale)); Invalidate(); } bool TGraphicDocumentView::MouseDown(TMouseDownEvent& event) { const MGraphic* graphic = LookupGraphic(); TGPoint offset(0,0); TSimpleDragAndDropItem* item = new TSimpleDragAndDropItem(*this); item->AdoptScrapItem(new TScrapItemOn(::Copy(*graphic), offset)); MGraphic* dragGraphic = ::Copy(*graphic); dragGraphic->TranslateBy(-graphic->GetLooseFitBounds().GetCenter()); TInteractor* interactor = new TMouseDragAndDropInteractor(item, dragGraphic, TViewHandle(*this)); event.StartInteractor(interactor); return true; } bool TGraphicDocumentView::ChoosePreferredType(const TGPoint& whereDropped, const TSequenceOf& availableTypes, TTypeDescription& chosenType) const { bool result = false; TTypeDescription graphicScrapType = TScrapItemOn::GetScrapItemType(); CollectionCount count = availableTypes.OccurrencesOf(graphicScrapType); if (count > 0) { chosenType = graphicScrapType; result = true; } return result; } bool TGraphicDocumentView::AcceptDrop(const TGPoint& droppedAt, const TTypeDescription& type, const TScrapItem& item) { bool accepted = false; if ( type == TScrapItemOn::GetScrapItemType() ) { TScrapItemOn* graphicScrapItem = (TScrapItemOn*)&item; MGraphic* graphic = graphicScrapItem->OrphanObject(); if (graphic != NIL) { AdoptGraphic(graphic); HandleAllocatedAreaChanged(GetLooseFitBounds()); accepted = true; } } return accepted; }