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
TTZDisplay(const char * name,const char * label)20 TTZDisplay::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
~TTZDisplay()30 TTZDisplay::~TTZDisplay()
31 {
32 }
33
34
35 void
AttachedToWindow()36 TTZDisplay::AttachedToWindow()
37 {
38 AdoptParentColors();
39 }
40
41
42 void
ResizeToPreferred()43 TTZDisplay::ResizeToPreferred()
44 {
45 BSize size = _CalcPrefSize();
46 ResizeTo(size.width, size.height);
47 }
48
49
50 void
Draw(BRect)51 TTZDisplay::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
75 const char*
Label() const76 TTZDisplay::Label() const
77 {
78 return fLabel.String();
79 }
80
81
82 void
SetLabel(const char * label)83 TTZDisplay::SetLabel(const char* label)
84 {
85 fLabel.SetTo(label);
86 Invalidate();
87 InvalidateLayout();
88 }
89
90
91 const char*
Text() const92 TTZDisplay::Text() const
93 {
94 return fText.String();
95 }
96
97
98 void
SetText(const char * text)99 TTZDisplay::SetText(const char* text)
100 {
101 fText.SetTo(text);
102 Invalidate();
103 InvalidateLayout();
104 }
105
106
107 const char*
Time() const108 TTZDisplay::Time() const
109 {
110 return fTime.String();
111 }
112
113
114 void
SetTime(const char * time)115 TTZDisplay::SetTime(const char* time)
116 {
117 fTime.SetTo(time);
118 Invalidate();
119 InvalidateLayout();
120 }
121
122
123 BSize
MaxSize()124 TTZDisplay::MaxSize()
125 {
126 BSize size = _CalcPrefSize();
127 size.width = B_SIZE_UNLIMITED;
128
129 return BLayoutUtils::ComposeSize(ExplicitMaxSize(),
130 size);
131 }
132
133
134 BSize
MinSize()135 TTZDisplay::MinSize()
136 {
137 return BLayoutUtils::ComposeSize(ExplicitMinSize(),
138 _CalcPrefSize());
139 }
140
141
142 BSize
PreferredSize()143 TTZDisplay::PreferredSize()
144 {
145 return BLayoutUtils::ComposeSize(ExplicitPreferredSize(),
146 _CalcPrefSize());
147 }
148
149
150 BSize
_CalcPrefSize()151 TTZDisplay::_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