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