vfs: check userland buffers before reading them.
[haiku.git] / src / apps / debugger / user_interface / gui / value / TableCellValueRendererUtils.cpp
blob2df04720d7d03fe66f145c0e78ca870dce14dce4
1 /*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "TableCellValueRendererUtils.h"
9 #include <Font.h>
10 #include <String.h>
11 #include <View.h>
14 static const float kTextMargin = 8;
17 /*static*/ void
18 TableCellValueRendererUtils::DrawString(BView* view, BRect rect,
19 const char* string, bool valueChanged, enum alignment alignment,
20 bool truncate)
22 // get font height info
23 font_height fontHeight;
24 view->GetFontHeight(&fontHeight);
26 // truncate, if requested
27 BString truncatedString;
28 if (truncate) {
29 truncatedString = string;
30 view->TruncateString(&truncatedString, B_TRUNCATE_END,
31 rect.Width() - 2 * kTextMargin + 2);
32 string = truncatedString.String();
35 // compute horizontal position according to alignment
36 float x;
37 switch (alignment) {
38 default:
39 case B_ALIGN_LEFT:
40 x = rect.left + kTextMargin;
41 break;
43 case B_ALIGN_CENTER:
44 x = rect.left + (rect.Width() - view->StringWidth(string)) / 2;
45 break;
47 case B_ALIGN_RIGHT:
48 x = rect.right - kTextMargin - view->StringWidth(string);
49 break;
52 // compute vertical position (base line)
53 float y = rect.top
54 + (rect.Height() - (fontHeight.ascent + fontHeight.descent
55 + fontHeight.leading)) / 2
56 + (fontHeight.ascent + fontHeight.descent) - 2;
57 // TODO: This is the computation BColumnListView (respectively
58 // BTitledColumn) is using, which I find somewhat weird.
60 if (valueChanged) {
61 view->PushState();
62 view->SetHighColor((rgb_color){255, 0, 0, 255});
65 view->DrawString(string, BPoint(x, y));
67 if (valueChanged)
68 view->PopState();
72 /*static*/ float
73 TableCellValueRendererUtils::PreferredStringWidth(BView* view,
74 const char* string)
76 return view->StringWidth(string) + 2 * kTextMargin;