DrawGroupLabel
(
DrawingContext oDrawingContext,
GraphDrawingContext oGraphDrawingContext,
GroupInfo oGroupInfo
)
{
Debug.Assert(oDrawingContext != null);
Debug.Assert(oGraphDrawingContext != null);
Debug.Assert(oGroupInfo != null);
Debug.Assert(m_eLabelPosition != VertexLabelPosition.Nowhere);
AssertValid();
String sLabel = oGroupInfo.Label;
Rect oGroupRectangle;
if (
String.IsNullOrEmpty(sLabel)
||
!TryGetGroupRectangle(oGroupInfo, out oGroupRectangle)
)
{
return;
}
FormattedText oFormattedText =
m_oFormattedTextManager.CreateFormattedText(sLabel,
m_oLabelTextColor, m_dLabelScale);
oFormattedText.MaxTextWidth = oGroupRectangle.Width;
oFormattedText.MaxTextHeight = oGroupRectangle.Height;
// The alignment needs to be set before the width and height of the
// FormattedText are obtained.
SetTextAlignment(oFormattedText, m_eLabelPosition);
// You can't use FormattedText.Width to get the width of the actual
// text when text wrapping is enabled (FormattedText.MaxTextWidth > 0).
// Instead, use a method that takes wrapping into account.
Double dLabelWidth =
WpfGraphicsUtil.GetFormattedTextSize(oFormattedText).Width;
Double dLabelHeight = oFormattedText.Height;
Double dGroupRectangleWidth = oGroupRectangle.Width;
Double dGroupRectangleHeight = oGroupRectangle.Height;
Double dMaxTextWidth = oFormattedText.MaxTextWidth;
Double dTextOffsetXForCenter =
(dGroupRectangleWidth - dMaxTextWidth) / 2.0;
Double dTextOffsetYForMiddle =
(dGroupRectangleHeight - dLabelHeight) / 2.0;
Double dTextOffsetYForBottom =
dGroupRectangleHeight - dLabelHeight - LabelVerticalMargin;
Point oTextOrigin = oGroupRectangle.Location;
Double dTextOffsetX = 0;
Double dTextOffsetY = 0;
switch (m_eLabelPosition)
{
case VertexLabelPosition.TopLeft:
dTextOffsetX = LabelHorizontalMargin;
dTextOffsetY = LabelVerticalMargin;
break;
case VertexLabelPosition.TopCenter:
dTextOffsetX = dTextOffsetXForCenter;
dTextOffsetY = LabelVerticalMargin;
break;
case VertexLabelPosition.TopRight:
dTextOffsetX = -LabelHorizontalMargin;
dTextOffsetY = LabelVerticalMargin;
break;
case VertexLabelPosition.MiddleLeft:
dTextOffsetX = LabelHorizontalMargin;
dTextOffsetY = dTextOffsetYForMiddle;
break;
case VertexLabelPosition.MiddleCenter:
dTextOffsetX = dTextOffsetXForCenter;
dTextOffsetY = dTextOffsetYForMiddle;
break;
case VertexLabelPosition.MiddleRight:
dTextOffsetX = -LabelHorizontalMargin;
dTextOffsetY = dTextOffsetYForMiddle;
break;
case VertexLabelPosition.BottomLeft:
dTextOffsetX = LabelHorizontalMargin;
dTextOffsetY = dTextOffsetYForBottom;
break;
case VertexLabelPosition.BottomCenter:
dTextOffsetX = dTextOffsetXForCenter;
dTextOffsetY = dTextOffsetYForBottom;
break;
case VertexLabelPosition.BottomRight:
dTextOffsetX = -LabelHorizontalMargin;
dTextOffsetY = dTextOffsetYForBottom;
break;
default:
Debug.Assert(false);
break;
}
oTextOrigin.Offset(dTextOffsetX, dTextOffsetY);
DrawLabelBackground(oDrawingContext, oGraphDrawingContext,
oFormattedText, m_oLabelTextColor, LabelBackgroundAlpha,
oTextOrigin);
oDrawingContext.DrawText(oFormattedText, oTextOrigin);
}