示例#1
0
void TBWidgetString::ValidatCachedSize(TBWidget *widget)
{
	const TBFontDescription fd = widget->GetCalculatedFontDescription();
	if (!m_height || fd != m_fd)
	{
		m_fd = fd;
		TBFontFace *font = g_font_manager->GetFontFace(fd);
		m_width = font->GetStringWidth(m_text);
		m_height = font->GetHeight();
	}
}
void TBWidgetString::Paint(TBWidget *widget, const TBRect &rect, const TBColor &color)
{
	TBFontFace *font = widget->GetFont();
	int string_w = GetWidth(widget);

	int x = rect.x;
	if (m_text_align == TB_TEXT_ALIGN_RIGHT)
		x += rect.w - string_w;
	else if (m_text_align == TB_TEXT_ALIGN_CENTER)
		x += MAX(0, (rect.w - string_w) / 2);
	int y = rect.y + (rect.h - GetHeight(widget)) / 2;

	if (string_w <= rect.w)
		font->DrawString(x, y, color, m_text);
	else
	{
		// There's not enough room for the entire string
		// so cut it off and end with ellipsis (...)

		// const char *end = "…"; // 2026 HORIZONTAL ELLIPSIS
		// Some fonts seem to render ellipsis a lot uglier than three dots.
		const char *end = "...";

		int endw = font->GetStringWidth(end);
		int startw = 0;
		int startlen = 0;
		while (m_text.CStr()[startlen])
		{
			int new_startw = font->GetStringWidth(m_text, startlen);
			if (new_startw + endw > rect.w)
				break;
			startw = new_startw;
			startlen++;
		}
		startlen = MAX(0, startlen - 1);
		font->DrawString(x, y, color, m_text, startlen);
		font->DrawString(x + startw, y, color, end);
	}
}