* Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*/
#include "TableCellValueRendererUtils.h"
#include <Font.h>
#include <String.h>
#include <View.h>
static const float kTextMargin = 8;
void
TableCellValueRendererUtils::DrawString(BView* view, BRect rect,
const char* string, bool valueChanged, enum alignment alignment,
bool truncate)
{
font_height fontHeight;
view->GetFontHeight(&fontHeight);
BString truncatedString;
if (truncate) {
truncatedString = string;
view->TruncateString(&truncatedString, B_TRUNCATE_END,
rect.Width() - 2 * kTextMargin + 2);
string = truncatedString.String();
}
float x;
switch (alignment) {
default:
case B_ALIGN_LEFT:
x = rect.left + kTextMargin;
break;
case B_ALIGN_CENTER:
x = rect.left + (rect.Width() - view->StringWidth(string)) / 2;
break;
case B_ALIGN_RIGHT:
x = rect.right - kTextMargin - view->StringWidth(string);
break;
}
float y = rect.top
+ (rect.Height() - (fontHeight.ascent + fontHeight.descent
+ fontHeight.leading)) / 2
+ (fontHeight.ascent + fontHeight.descent) - 2;
if (valueChanged) {
view->PushState();
view->SetHighColor((rgb_color){255, 0, 0, 255});
}
view->DrawString(string, BPoint(x, y));
if (valueChanged)
view->PopState();
}
float
TableCellValueRendererUtils::PreferredStringWidth(BView* view,
const char* string)
{
return view->StringWidth(string) + 2 * kTextMargin;
}