// $Revision: 1.6 $ // LabelDrawer.C // Copyright (C) 1994, 1995 Taligent, Inc. All rights reserved. #ifndef TaligentSamples_STANDARDGRAPH #include "StandardGraph.h" #endif #ifndef TaligentSamples_LABELDRAWER #include "LabelDrawer.h" #endif #ifndef Taligent_ASSERTIONS #include #endif #ifndef Taligent_NUMBERFORMAT #include #endif //============================================================================== // TLabelDrawer TaligentTypeExtensionMacro_Abstract(TLabelDrawer); TLabelDrawer::TLabelDrawer() : TGraphDrawer(), fLabelColor(NIL), fLabelFont(), fLabelSize(9), fLabelDistance(0.0), fLabelInterval(1.0) { fLabelColor = new TTextColorStyle(TTextColorStyle::GetBlack()); } TLabelDrawer::TLabelDrawer(GraphValue labelDistanceFromAxis, GraphValue labelInterval, GraphValue pointSize, const TFontFamilyStyle& font, const TTextColorStyle& labelColor) : TGraphDrawer(), fLabelColor(NIL), fLabelFont(font), fLabelSize(pointSize), fLabelDistance(labelDistanceFromAxis), fLabelInterval(labelInterval) { fLabelColor = ::Copy(labelColor); } TLabelDrawer::TLabelDrawer(const TLabelDrawer& source) : TGraphDrawer(source), fLabelColor(NIL), fLabelFont(source.fLabelFont), fLabelSize(source.fLabelSize), fLabelDistance(source.fLabelDistance), fLabelInterval(source.fLabelInterval) { fLabelColor = ::CopyPointer(source.fLabelColor); } TLabelDrawer::~TLabelDrawer() { delete fLabelColor; } TLabelDrawer& TLabelDrawer::operator=(const TLabelDrawer& source) { if (&source != this) { TGraphDrawer::operator=(source); delete fLabelColor; fLabelColor = ::CopyPointer(source.fLabelColor); fLabelFont = source.fLabelFont; fLabelSize = source.fLabelSize; fLabelDistance = source.fLabelDistance; fLabelInterval = source.fLabelInterval; } return *this; } TStream& TLabelDrawer::operator>>=(TStream& toStream) const { ::WriteVersion(toStream, kOriginalVersion); TGraphDrawer::operator>>=(toStream); ::Flatten(fLabelColor, toStream); fLabelFont >>= toStream; fLabelSize >>= toStream; fLabelDistance >>= toStream; fLabelInterval >>= toStream; return toStream; } TStream& TLabelDrawer::operator<<=(TStream& fromStream) { ::ReadVersion(fromStream, kOriginalVersion, kOriginalVersion); TGraphDrawer::operator<<=(fromStream); delete fLabelColor; ::Resurrect(fLabelColor, fromStream); fLabelFont <<= fromStream; fLabelSize <<= fromStream; fLabelDistance <<= fromStream; fLabelInterval <<= fromStream; return fromStream; } const TTextColorStyle& TLabelDrawer::GetLabelColor() const { ::Assertion(fLabelColor != NIL, "Label color can't be NIL here!"); return *fLabelColor; } const TFontFamilyStyle& TLabelDrawer::GetLabelFont() const { return fLabelFont; } GraphValue TLabelDrawer::GetLabelSize() const { return fLabelSize; } GraphValue TLabelDrawer::GetLabelDistance() const { return fLabelDistance; } GraphValue TLabelDrawer::GetLabelInterval() const { return fLabelInterval; } void TLabelDrawer::SetLabelColor(const TTextColorStyle& color) { delete fLabelColor; fLabelColor = ::Copy(color); } void TLabelDrawer::SetLabelFont(const TFontFamilyStyle& font) { fLabelFont = font; } void TLabelDrawer::SetLabelSize(GraphValue pointSize) { fLabelSize = pointSize; } void TLabelDrawer::SetLabelDistance(GraphValue distance) { fLabelDistance = distance; } void TLabelDrawer::SetLabelInterval(GraphValue interval) { fLabelInterval = interval; } void TLabelDrawer::DrawIntoGraph(TGrafPort& port) const { TPositionalNumberFormatter formatter; GraphValue value = GetRange().GetMin(); unsigned long numberOfLabels = (GetRange().GetLength() / GetLabelInterval()) + 1; for (unsigned long index = 0; index < numberOfLabels; ++index) { TStandardText text(80); TFormatableNumber number(value); formatter.Format(number, text); TTextDisplay textDisplay(text, GetLabelFont(), GetLabelSize(), GetLabelColor()); PositionLabel(textDisplay, value); textDisplay.Draw(port); value += GetLabelInterval(); } } TGRect TLabelDrawer::GetBounds() const { TPositionalNumberFormatter formatter; GraphValue value = GetRange().GetMin(); TGRect textBounds; TGRect totalBounds(TGRect::kZeroRect); unsigned long numberOfLabels = (GetRange().GetLength() / GetLabelInterval()) + 1; for (unsigned long index = 0; index < numberOfLabels; ++index) { TStandardText text(80); TFormatableNumber number(value); formatter.Format(number, text); TTextDisplay textDisplay(text, GetLabelFont(), GetLabelSize(), GetLabelColor()); PositionLabel(textDisplay, value); textDisplay.GetBounds(textBounds); totalBounds.ExtendTo(textBounds); value += GetLabelInterval(); } return totalBounds; } //============================================================================== // TXLabelDrawer TaligentTypeExtensionMacro(TXLabelDrawer); TXLabelDrawer::TXLabelDrawer() : TLabelDrawer() { } TXLabelDrawer::TXLabelDrawer(GraphValue labelDistanceFromAxis, GraphValue labelInterval, GraphValue pointSize, const TFontFamilyStyle& font) : TLabelDrawer(labelDistanceFromAxis, labelInterval, pointSize, font) { } TXLabelDrawer::TXLabelDrawer(const TXLabelDrawer& source) : TLabelDrawer(source) { } TXLabelDrawer::~TXLabelDrawer() { } TXLabelDrawer& TXLabelDrawer::operator=(const TXLabelDrawer& source) { if (&source != this) { TLabelDrawer::operator=(source); } return *this; } TStream& TXLabelDrawer::operator>>=(TStream& toStream) const { ::WriteVersion(toStream, kOriginalVersion); TLabelDrawer::operator>>=(toStream); return toStream; } TStream& TXLabelDrawer::operator<<=(TStream& fromStream) { ::ReadVersion(fromStream, kOriginalVersion, kOriginalVersion); TLabelDrawer::operator<<=(fromStream); return fromStream; } void TXLabelDrawer::PositionLabel(TTextDisplay& label, GraphValue value) const { const TStandardGraph& graph = GetTargetGraph(); TGRect labelBounds; label.GetBounds(labelBounds); TGPoint startPoint; startPoint = graph.MapValuesToPoint(value, graph.GetYRange().GetMin()); startPoint.fX -= labelBounds.GetWidth() / 2; startPoint.fY += GetLabelDistance() + labelBounds.GetHeight(); label.SetOrigin(startPoint); } const TGraphRange& TXLabelDrawer::GetRange() const { return GetTargetGraph().GetXRange(); } //============================================================================== // TYLabelDrawer TaligentTypeExtensionMacro(TYLabelDrawer); TYLabelDrawer::TYLabelDrawer() : TLabelDrawer() { } TYLabelDrawer::TYLabelDrawer(GraphValue labelDistanceFromAxis, GraphValue labelInterval, GraphValue pointSize, const TFontFamilyStyle& font) : TLabelDrawer(labelDistanceFromAxis, labelInterval, pointSize, font) { } TYLabelDrawer::TYLabelDrawer(const TYLabelDrawer& source) : TLabelDrawer(source) { } TYLabelDrawer::~TYLabelDrawer() { } TYLabelDrawer& TYLabelDrawer::operator=(const TYLabelDrawer& source) { if (&source != this) { TLabelDrawer::operator=(source); } return *this; } TStream& TYLabelDrawer::operator>>=(TStream& toStream) const { ::WriteVersion(toStream, kOriginalVersion); TLabelDrawer::operator>>=(toStream); return toStream; } TStream& TYLabelDrawer::operator<<=(TStream& fromStream) { ::ReadVersion(fromStream, kOriginalVersion, kOriginalVersion); TLabelDrawer::operator<<=(fromStream); return fromStream; } void TYLabelDrawer::PositionLabel(TTextDisplay& label, GraphValue value) const { const TStandardGraph& graph = GetTargetGraph(); TGRect labelBounds; label.GetBounds(labelBounds); TGPoint startPoint; startPoint = graph.MapValuesToPoint(graph.GetXRange().GetMin(), value); startPoint.fX -= labelBounds.GetWidth() + GetLabelDistance(); startPoint.fY += labelBounds.GetHeight() / 2; label.SetOrigin(startPoint); } const TGraphRange& TYLabelDrawer::GetRange() const { return GetTargetGraph().GetYRange(); }