1/*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include "TableCellValueRendererUtils.h"
8
9#include <Font.h>
10#include <String.h>
11#include <View.h>
12
13
14static const float kTextMargin = 8;
15
16
17/*static*/ void
18TableCellValueRendererUtils::DrawString(BView* view, BRect rect,
19	const char* string, enum alignment alignment, bool truncate)
20{
21	// get font height info
22	font_height	fontHeight;
23	view->GetFontHeight(&fontHeight);
24
25	// truncate, if requested
26	BString truncatedString;
27	if (truncate) {
28		truncatedString = string;
29		view->TruncateString(&truncatedString, B_TRUNCATE_END,
30			rect.Width() - 2 * kTextMargin + 2);
31		string = truncatedString.String();
32	}
33
34	// compute horizontal position according to alignment
35	float x;
36	switch (alignment) {
37		default:
38		case B_ALIGN_LEFT:
39			x = rect.left + kTextMargin;
40			break;
41
42		case B_ALIGN_CENTER:
43			x = rect.left + (rect.Width() - view->StringWidth(string)) / 2;
44			break;
45
46		case B_ALIGN_RIGHT:
47			x = rect.right - kTextMargin - view->StringWidth(string);
48			break;
49	}
50
51	// compute vertical position (base line)
52	float y = rect.top
53		+ (rect.Height() - (fontHeight.ascent + fontHeight.descent
54			+ fontHeight.leading)) / 2
55		+ (fontHeight.ascent + fontHeight.descent) - 2;
56		// TODO: This is the computation BColumnListView (respectively
57		// BTitledColumn) is using, which I find somewhat weird.
58
59	view->DrawString(string, BPoint(x, y));
60}
61
62
63/*static*/ float
64TableCellValueRendererUtils::PreferredStringWidth(BView* view,
65	const char* string)
66{
67	return view->StringWidth(string) + 2 * kTextMargin;
68}
69