1 /*
2 * Copyright 2010, Stephan Aßmus <superstippi@gmx.de>.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7 #include "DurationView.h"
8
9 #include <LayoutUtils.h>
10
11 #include "DurationToString.h"
12
13
DurationView(const char * name)14 DurationView::DurationView(const char* name)
15 :
16 BStringView(name, ""),
17 fMode(kTimeToFinish),
18 fPosition(0),
19 fDuration(0),
20 fDisplayDuration(0)
21 {
22 SetSymbolScale(1.0f);
23
24 SetAlignment(B_ALIGN_RIGHT);
25
26 _Update();
27 }
28
29
30 void
AttachedToWindow()31 DurationView::AttachedToWindow()
32 {
33 BStringView::AttachedToWindow();
34 _UpdateTextColor();
35 }
36
37
38 void
MouseDown(BPoint where)39 DurationView::MouseDown(BPoint where)
40 {
41 // Switch through the modes
42 uint32 mode = fMode + 1;
43 if (mode == kLastMode)
44 mode = 0;
45 SetMode(mode);
46 }
47
48
49 void
MessageReceived(BMessage * message)50 DurationView::MessageReceived(BMessage* message)
51 {
52 if (message->what == B_COLORS_UPDATED
53 && message->HasColor(ui_color_name(B_PANEL_TEXT_COLOR)))
54 _UpdateTextColor();
55
56 BStringView::MessageReceived(message);
57 }
58
59
60 BSize
MinSize()61 DurationView::MinSize()
62 {
63 BSize size;
64 char string[64];
65 duration_to_string(int32(fDuration / -1000000LL), string, sizeof(string));
66 size.width = StringWidth(string);
67 font_height fontHeight;
68 GetFontHeight(&fontHeight);
69 size.height = ceilf(fontHeight.ascent) + ceilf(fontHeight.descent);
70 return BLayoutUtils::ComposeSize(ExplicitMinSize(), size);
71 }
72
73
74 BSize
MaxSize()75 DurationView::MaxSize()
76 {
77 return BLayoutUtils::ComposeSize(ExplicitMaxSize(), MinSize());
78 }
79
80
81 // #pragma mark -
82
83
84 void
Update(bigtime_t position,bigtime_t duration)85 DurationView::Update(bigtime_t position, bigtime_t duration)
86 {
87 if (position == fPosition && duration == fDuration)
88 return;
89
90 fPosition = position;
91 if (fDuration != duration) {
92 fDuration = duration;
93 InvalidateLayout();
94 }
95 _Update();
96 }
97
98
99 void
SetMode(uint32 mode)100 DurationView::SetMode(uint32 mode)
101 {
102 if (mode == fMode)
103 return;
104
105 fMode = mode;
106 _Update();
107 }
108
109
110 void
SetSymbolScale(float scale)111 DurationView::SetSymbolScale(float scale)
112 {
113 if (scale != 1.0f) {
114 BFont font(be_bold_font);
115 font.SetSize(font.Size() * scale * 1.2);
116 SetFont(&font);
117 } else
118 SetFont(be_plain_font);
119
120 InvalidateLayout();
121 }
122
123
124 void
_Update()125 DurationView::_Update()
126 {
127 switch (fMode) {
128 case kTimeElapsed:
129 _GenerateString(fPosition);
130 break;
131 default:
132 case kTimeToFinish:
133 _GenerateString(fPosition - fDuration);
134 break;
135 case kDuration:
136 _GenerateString(fDuration);
137 break;
138 }
139 }
140
141
142 void
_UpdateTextColor()143 DurationView::_UpdateTextColor()
144 {
145 SetHighColor(mix_color(ViewColor(), ui_color(B_PANEL_TEXT_COLOR), 128));
146 }
147
148
149 void
_GenerateString(bigtime_t duration)150 DurationView::_GenerateString(bigtime_t duration)
151 {
152 duration /= 1000000;
153 if (fDisplayDuration == duration)
154 return;
155
156 fDisplayDuration = duration;
157
158 char string[64];
159 duration_to_string(duration, string, sizeof(string));
160
161 SetText(string);
162 }
163
164