1/*
2 * Copyright 2004-2011, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Andrew McCall <mccall@@digitalparadise.co.uk>
7 *		Mike Berg <mike@berg-net.us>
8 *		Julun <host.haiku@gmx.de>
9 *		Hamish Morrison <hamish@lavabit.com>
10 */
11
12#include "TZDisplay.h"
13
14#include <stdio.h>
15
16#include <LayoutUtils.h>
17
18
19
20TTZDisplay::TTZDisplay(const char* name, const char* label)
21	:
22	BView(name, B_WILL_DRAW),
23	fLabel(label),
24	fText(""),
25	fTime("")
26{
27}
28
29
30TTZDisplay::~TTZDisplay()
31{
32}
33
34
35void
36TTZDisplay::AttachedToWindow()
37{
38	AdoptParentColors();
39}
40
41
42void
43TTZDisplay::ResizeToPreferred()
44{
45	BSize size = _CalcPrefSize();
46	ResizeTo(size.width, size.height);
47}
48
49
50void
51TTZDisplay::Draw(BRect)
52{
53	SetLowColor(ViewColor());
54
55	BRect bounds = Bounds();
56	FillRect(Bounds(), B_SOLID_LOW);
57
58	font_height height;
59	GetFontHeight(&height);
60	float fontHeight = ceilf(height.descent + height.ascent +
61		height.leading);
62
63	BPoint pt(bounds.left, ceilf(bounds.top + height.ascent));
64	DrawString(fLabel.String(), pt);
65
66	pt.y += fontHeight;
67	DrawString(fText.String(), pt);
68
69	pt.y -= fontHeight;
70	pt.x = bounds.right - StringWidth(fTime.String());
71	DrawString(fTime.String(), pt);
72}
73
74
75const char*
76TTZDisplay::Label() const
77{
78	return fLabel.String();
79}
80
81
82void
83TTZDisplay::SetLabel(const char* label)
84{
85	fLabel.SetTo(label);
86	Invalidate();
87	InvalidateLayout();
88}
89
90
91const char*
92TTZDisplay::Text() const
93{
94	return fText.String();
95}
96
97
98void
99TTZDisplay::SetText(const char* text)
100{
101	fText.SetTo(text);
102	Invalidate();
103	InvalidateLayout();
104}
105
106
107const char*
108TTZDisplay::Time() const
109{
110	return fTime.String();
111}
112
113
114void
115TTZDisplay::SetTime(const char* time)
116{
117	fTime.SetTo(time);
118	Invalidate();
119	InvalidateLayout();
120}
121
122
123BSize
124TTZDisplay::MaxSize()
125{
126	BSize size = _CalcPrefSize();
127	size.width = B_SIZE_UNLIMITED;
128
129	return BLayoutUtils::ComposeSize(ExplicitMaxSize(),
130		size);
131}
132
133
134BSize
135TTZDisplay::MinSize()
136{
137	return BLayoutUtils::ComposeSize(ExplicitMinSize(),
138		_CalcPrefSize());
139}
140
141
142BSize
143TTZDisplay::PreferredSize()
144{
145	return BLayoutUtils::ComposeSize(ExplicitPreferredSize(),
146		_CalcPrefSize());
147}
148
149
150BSize
151TTZDisplay::_CalcPrefSize()
152{
153	font_height fontHeight;
154	GetFontHeight(&fontHeight);
155
156	BSize size;
157	size.height = 2 * ceilf(fontHeight.ascent + fontHeight.descent +
158		fontHeight.leading);
159
160	// Add a little padding
161	float padding = 10.0;
162	float firstLine = ceilf(StringWidth(fLabel.String()) +
163		StringWidth(" ") + StringWidth(fTime.String()) + padding);
164	float secondLine = ceilf(StringWidth(fText.String()) + padding);
165	size.width = firstLine > secondLine ? firstLine : secondLine;
166
167	return size;
168}
169