xref: /haiku/src/apps/debuganalyzer/gui/chart/StringChartLegend.cpp (revision 28b268491295533f279e4730bdbd28c0ff8102ac)
1 /*
2  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include "chart/StringChartLegend.h"
7 
8 #include <Font.h>
9 #include <View.h>
10 
11 
12 // #pragma mark - StringChartLegend
13 
14 
StringChartLegend(const char * string,int32 level)15 StringChartLegend::StringChartLegend(const char* string, int32 level)
16 	:
17 	ChartLegend(level),
18 	fString(string)
19 {
20 }
21 
22 
23 // #pragma mark - StringChartLegendRenderer
24 
25 
StringChartLegendRenderer()26 StringChartLegendRenderer::StringChartLegendRenderer()
27 	:
28 	fFont(*be_plain_font)
29 {
30 	_Init();
31 }
32 
33 
StringChartLegendRenderer(const BFont & font)34 StringChartLegendRenderer::StringChartLegendRenderer(const BFont& font)
35 	:
36 	fFont(font)
37 {
38 	_Init();
39 }
40 
41 
42 void
GetMinimumLegendSpacing(BView * view,float * horizontal,float * vertical)43 StringChartLegendRenderer::GetMinimumLegendSpacing(BView* view,
44 	float* horizontal, float* vertical)
45 {
46 	*horizontal = 2 * fEmWidth;
47 	*vertical = fFontHeight / 2;
48 }
49 
50 
51 BSize
LegendSize(ChartLegend * _legend,BView * view)52 StringChartLegendRenderer::LegendSize(ChartLegend* _legend, BView* view)
53 {
54 	StringChartLegend* legend = dynamic_cast<StringChartLegend*>(_legend);
55 	if (legend == NULL)
56 		return BSize();
57 
58 	float width = ceilf(fFont.StringWidth(legend->String()));
59 	return BSize(width, fFontHeight);
60 }
61 
62 
63 void
RenderLegend(ChartLegend * _legend,BView * view,BPoint point)64 StringChartLegendRenderer::RenderLegend(ChartLegend* _legend, BView* view,
65 	BPoint point)
66 {
67 	StringChartLegend* legend = dynamic_cast<StringChartLegend*>(_legend);
68 	if (legend == NULL)
69 		return;
70 
71 	point.y += ceil(fFontAscent);
72 
73 	view->SetFont(&fFont);
74 	view->SetHighColor(rgb_color().set_to(0, 0, 0, 255));
75 	view->DrawString(legend->String(), point);
76 }
77 
78 
79 void
_Init()80 StringChartLegendRenderer::_Init()
81 {
82 	font_height fh;
83 	fFont.GetHeight(&fh);
84 	fFontAscent = ceilf(fh.ascent);
85 	fFontHeight = fFontAscent + ceilf(fh.descent);
86 	fEmWidth = ceilf(fFont.StringWidth("m", 1));
87 }
88