// $Revision: 1.6 $ #ifndef TaligentSamples_WEBLINKVIEW #include "WebLinkView.h" #endif #ifndef TaligentSamples_FOLLOWURLCOMMAND #include "FollowURLCommand.h" #endif #ifndef TaligentSamples_SIMPLEDRAGANDDROPITEM #include #endif #ifndef TaligentSamples_ATOMICMODEL #include #endif #ifndef Taligent_MODELSCRAPITEM #include #endif #ifndef Taligent_GRAYCOLOR #include #endif #ifndef Taligent_TEXTGRAPHIC #include #endif #ifndef Taligent_KEYBOARDINPUT #include #endif #ifndef Taligent_MOUSEDRAGANDDROP #include #endif #ifndef Taligent_GRAPHICIMAGE #include #endif VersionDefinitionsMacro(TWebLinkView, kOriginalVersion); TWebLinkView::TWebLinkView() : TLabelView(NIL), MMouseEventHandler(TViewHandle(*this)), MGUIBundle() { } TWebLinkView::TWebLinkView(TGUIBundle* bundleToAlias) : TLabelView(NIL), MMouseEventHandler(TViewHandle(*this)), MGUIBundle(bundleToAlias) { } TWebLinkView::TWebLinkView(const TWebLinkView& source) : TLabelView(source), MMouseEventHandler(source), MGUIBundle() { } TWebLinkView::~TWebLinkView() { } TStream& TWebLinkView::operator>>=(TStream& toStream) const { ::WriteVersion(toStream); TLabelView::operator>>=(toStream); return toStream; } TStream& TWebLinkView::operator<<=(TStream& fromStream) { ::ReadVersion(fromStream, kOriginalVersion, kOriginalVersion); TLabelView::operator<<=(fromStream); return fromStream; } TLabel* TWebLinkView::CreateCurrentLabel(const TTextColorStyle& textColor) const { const TModelPointerTo > model(GetModelReference()); const TWebLink* link = model->GetValueForReading(); TStandardText linkText = link->GetDisplayedText(); linkText.AddStyles(textColor); linkText.AddStyles(TUnderlineStyle()); TTextLabel* label = new TTextLabel(linkText); return label; } void TWebLinkView::ActivateFeedback() { TLabelView::AdoptLabel(CreateCurrentLabel(TTextColorStyle::GetBlue())); DrawContents(*GetGrafPort()); } void TWebLinkView::DeactivateFeedback() { TLabelView::AdoptLabel(CreateCurrentLabel()); DrawContents(*GetGrafPort()); } bool TWebLinkView::MouseDown(TMouseDownEvent& mouseDown) { const TKeyboardInputDevice* inputDevice = TKeyboardInputDevice::GetDefaultKeyboardInputDevice(mouseDown); if (inputDevice != 0 && inputDevice->GetModifierKeys().IsPowerKeyDown()) { InitiateDrag(mouseDown); } else { ActivateFeedback(); TModelReference parentViewerReference; TURL destinationURL; { TModelPointerTo > model(GetModelReference()); const TWebLink* webLink = model->GetValueForReading(); parentViewerReference = webLink->GetViewerModelReference(); destinationURL = webLink->GetDestination(); } if (parentViewerReference.IsDefined()) { TFollowURLCommand* command = new TFollowURLCommand(destinationURL); TDocumentComponentCommandBindingTo* commandBinding = new TDocumentComponentCommandBindingTo( command, new TModelSelection(parentViewerReference)); TDocumentSurrogate document(parentViewerReference); document.AdoptAndDo(commandBinding); } else { TModelPointerTo > model(GetModelReference()); model->GetValueForWriting()->OpenWebDocument(); DeactivateFeedback(); } } return true; } void TWebLinkView::InitiateDrag(TMouseDownEvent& mouseDown) { TModelPointerTo > model(GetModelReference()); TModelScrapItem* scrapItem = new TModelScrapItem(new TAtomicModelOn(*model), new TGUIPresenterStateFor >); TSimpleDragAndDropItem* dragItem = new TSimpleDragAndDropItem(TViewHandle(*this)); dragItem->AdoptScrapItem(scrapItem); TGArea viewArea; GetAllocatedAreaInParent(viewArea); TIndexedImage* image = new TIndexedImage(TGPoint(0,0), viewArea.GetBounds().GetSize()); DrawContents(*image->GetGrafPort()); TGraphicGroup* group = new TGraphicGroup(); group->AdoptLast(image); TGrafBundle* bundle = new TGrafBundle(*TColorPaint::GetRed().GetColor()); bundle->AdoptFramePen(new TSolidPen(1, TPen::kInsetFrame)); group->AdoptLast(new TArea(viewArea.GetBounds(), bundle)); MGraphic *graphic = group; graphic->TranslateBy(-mouseDown.GetEventPosition()); TMouseDragAndDropInteractor *interactor = new TMouseDragAndDropInteractor(dragItem, graphic, TViewHandle(*this)); mouseDown.StartInteractor(interactor); } void TWebLinkView::HandleAfterConnectionToViewRoot() { TLabelView::HandleAfterConnectionToViewRoot(); TLabel* label = CreateCurrentLabel(); TGRect bounds = label->GetBounds(); AdoptLabel(label); SetBackgroundColor(TRGBColor(1.0, 1.0, 1.0, 0.0)); GCoordinate width = bounds.GetSize().fX; GCoordinate height = bounds.GetSize().fY + 5; SetAllocatedArea(TGArea(TGRect(0, 0, width, height))); }