// $Revision: 1.31 $ // PickerView.C // Copyright (C) 1994, 1995 Taligent, Inc. All rights reserved. #ifndef TaligentSamples_PICKERVIEW #include "PickerView.h" #endif #ifndef TaligentSamples_STOCKDATA #include #endif #ifndef TaligentSamples_INSERTIONTARGET #include #endif #ifndef Taligent_KEYBOARDINPUT #include #endif #ifndef Taligent_PRINTINGFLAGSNAPSHOT #include #endif //============================================================================== // TPickerView TaligentTypeExtensionMacro(TPickerView); TPickerView::TPickerView() : TDOMDocumentComponentView(), fBackgroundPaint(NIL), fNameDisplays() { } TPickerView::TPickerView(TGUIBundle* guiBundleToAlias, const TPaint& backgroundPaint) : TDOMDocumentComponentView(guiBundleToAlias), fBackgroundPaint(NIL), fNameDisplays() { fBackgroundPaint = ::Copy(backgroundPaint); } TPickerView::~TPickerView() { delete fBackgroundPaint; } TStream& TPickerView::operator>>=(TStream& toStream) const { ::WriteVersion(toStream, kOriginalVersion); TDOMDocumentComponentView::operator>>=(toStream); ::Flatten(fBackgroundPaint, toStream); fNameDisplays >>= toStream; return toStream; } TStream& TPickerView::operator<<=(TStream& fromStream) { ::ReadVersion(fromStream, kOriginalVersion, kOriginalVersion); TDOMDocumentComponentView::operator<<=(fromStream); delete fBackgroundPaint; ::Resurrect(fBackgroundPaint, fromStream); fNameDisplays.DeleteAll(); fNameDisplays <<= fromStream; return fromStream; } void TPickerView::DrawContents(TGrafPort& port) const { bool printing = TPrintingFlagSnapshot(port).IsPrintingPort(); if (!printing) { // Paint the background. if (fBackgroundPaint != NIL) { TGArea viewGArea; GetAllocatedArea(viewGArea); const TColor& backgroundColor = *fBackgroundPaint->GetColor(); TArea viewArea(viewGArea, new TGrafBundle(backgroundColor, backgroundColor)); viewArea.Draw(port); } //Draw feedback & current selection TDOMDocumentComponentView::DrawContents(port); } // Draw the contents TDequeOfIterator iter(&fNameDisplays); TTextDisplay* display = iter.First(); while (display != NIL) { display->Draw(port); display = iter.Next(); } } void TPickerView::HandleAfterConnectionToViewRoot() { TDOMDocumentComponentView::HandleAfterConnectionToViewRoot(); CacheDisplaysAndUpdate(); } void TPickerView::HandleModelChanged(const TNotification& notification) { CacheDisplaysAndUpdate(); } TGArea TPickerView::GetSelectionDisplayArea(const TModelSelection& selection) const { TGArea selectionArea; if (selection.IsEmpty()) { TTextDisplay* lastName = fNameDisplays.Last(); if (lastName != NIL) { TGRect emptySelectionBounds(lastName->GetTextBounds()); emptySelectionBounds.fTop = emptySelectionBounds.fBottom + 1.0; emptySelectionBounds.OrderPoints(); selectionArea = emptySelectionBounds; } else { TGRect viewBounds(GetAllocatedArea()); viewBounds.fBottom = (++viewBounds.fTop) + 1.0; selectionArea = viewBounds; } } else { TPickerSelection* pickerSelection = NIL; ::DynamicCastTo(pickerSelection, &selection); if (pickerSelection != NIL) { TDequeOf selectionNames(new TOrderedOperatorComparator, NIL); { TDocumentComponentReadEntry entry(pickerSelection->GetModelReference()); pickerSelection->GetSelectedNames(selectionNames); } TDequeOfIterator iter(&fNameDisplays); for (TTextDisplay* display = iter.First(); display != NIL; display = iter.Next()) { TStandardText stockName(*display->GetText()); if (selectionNames.Member(stockName) != NIL) { TGRect displayBounds; display->GetBounds(displayBounds); selectionArea.Add(displayBounds); } } } } return selectionArea; } TModelSelection* TPickerView::CreateInsertionPoint(const TGPoint& where) const { const TModelPointerTo model(GetModelReference()); TModelSelection* result = model->CreateSelection(); result->DeselectAll(); return result; } TModelSelection* TPickerView::CreateSelectionFromPoint(const TGPoint& where) const { const TModelPointerTo model(GetModelReference()); TModelSelection* selection = model->CreateSelection(); selection->DeselectAll(); TDequeOfIterator iter(&fNameDisplays); TTextDisplay* display = iter.First(); while (display != NIL) { TGRect displayBounds; display->GetBounds(displayBounds); if (displayBounds.Contains(where)) { TStandardText stockName(*display->GetText()); ((TPickerSelection*)selection)->SelectStock(stockName); } display = iter.Next(); } return selection; } TModelSelection* TPickerView::CreateSelectionFromArea(const TGArea& area) const { const TModelPointerTo model(GetModelReference()); TModelSelection* selection = model->CreateSelection(); selection->DeselectAll(); TDequeOfIterator iter(&fNameDisplays); TTextDisplay* display = iter.First(); while (display != NIL) { TGRect displayBounds; display->GetBounds(displayBounds); if (area.Intersects(displayBounds)) { TStandardText stockName(*display->GetText()); ((TPickerSelection*)selection)->SelectStock(stockName); } display = iter.Next(); } return selection; } void TPickerView::CacheDisplaysAndUpdate() { static const GCoordinate kOffsetFromLeft = 3; static const TGPoint kMinimumSize(100,200); try { const TModelPointerTo model(GetModelReference()); TSortedSequenceOf stockNames( new TOrderedOperatorComparator, NIL); model->GetNames(stockNames); fNameDisplays.DeleteAll(); TSortedSequenceOfIterator stockNameIterator(&stockNames); GCoordinate yPosition = 0; TGPoint bottomRightCorner; for (TStandardText* stockName = stockNameIterator.First(); stockName != NIL; stockName = stockNameIterator.Next()) { TTextDisplay* display = new TTextDisplay(*stockName); TGRect displayBounds; display->GetBounds(displayBounds); display->SetOrigin(TGPoint(kOffsetFromLeft, yPosition + displayBounds.GetHeight())); display->GetBounds(displayBounds); bottomRightCorner = displayBounds.GetBottomRight(); yPosition = bottomRightCorner.fY; fNameDisplays.AddLast(display); } TGPoint newSize( ::Max(kMinimumSize.fX, bottomRightCorner.fX), ::Max(kMinimumSize.fY, yPosition)); SetAllocatedArea(TGRect(TGPoint::kOrigin, newSize)); InvalidateAll(); } catch (const TCompoundDocumentException& exception) { } } bool TPickerView::GetDisplaysHit(const TGArea& hitArea, TCollectionOf& fill) const { // See TPickerView::CreateSelectionFromPoint and TPickerView::CreateSelectionFromArea // for an interesting mechanism. ::Assertion(false, "This function is obsolete"); return false; } TPickerView& TPickerView::operator=(const TPickerView& source) { ::Assertion(false, "Can't call TPickerView assignment operator."); return *this; } TPickerView::TPickerView(const TPickerView& source) : TDOMDocumentComponentView(), fBackgroundPaint(NIL), fNameDisplays() { ::Assertion(false, "Can't copy TPickerView."); }