vfs: check userland buffers before reading them.
[haiku.git] / src / apps / mediaplayer / interface / DurationView.cpp
blob08c15f0287167802a479b7c73ad5cad16918e7d0
1 /*
2 * Copyright 2010, Stephan Aßmus <superstippi@gmx.de>.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "DurationView.h"
9 #include <LayoutUtils.h>
11 #include "DurationToString.h"
14 DurationView::DurationView(const char* name)
16 BStringView(name, ""),
17 fMode(kTimeToFinish),
18 fPosition(0),
19 fDuration(0),
20 fDisplayDuration(0)
22 SetSymbolScale(1.0f);
24 SetAlignment(B_ALIGN_RIGHT);
26 _Update();
30 void
31 DurationView::AttachedToWindow()
33 BStringView::AttachedToWindow();
34 _UpdateTextColor();
38 void
39 DurationView::MouseDown(BPoint where)
41 // Switch through the modes
42 uint32 mode = fMode + 1;
43 if (mode == kLastMode)
44 mode = 0;
45 SetMode(mode);
49 void
50 DurationView::MessageReceived(BMessage* message)
52 if (message->what == B_COLORS_UPDATED
53 && message->HasColor(ui_color_name(B_PANEL_TEXT_COLOR)))
54 _UpdateTextColor();
56 BStringView::MessageReceived(message);
60 BSize
61 DurationView::MinSize()
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);
74 BSize
75 DurationView::MaxSize()
77 return BLayoutUtils::ComposeSize(ExplicitMaxSize(), MinSize());
81 // #pragma mark -
84 void
85 DurationView::Update(bigtime_t position, bigtime_t duration)
87 if (position == fPosition && duration == fDuration)
88 return;
90 fPosition = position;
91 if (fDuration != duration) {
92 fDuration = duration;
93 InvalidateLayout();
95 _Update();
99 void
100 DurationView::SetMode(uint32 mode)
102 if (mode == fMode)
103 return;
105 fMode = mode;
106 _Update();
110 void
111 DurationView::SetSymbolScale(float scale)
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);
120 InvalidateLayout();
124 void
125 DurationView::_Update()
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;
142 void
143 DurationView::_UpdateTextColor()
145 SetHighColor(mix_color(ViewColor(), ui_color(B_PANEL_TEXT_COLOR), 128));
149 void
150 DurationView::_GenerateString(bigtime_t duration)
152 duration /= 1000000;
153 if (fDisplayDuration == duration)
154 return;
156 fDisplayDuration = duration;
158 char string[64];
159 duration_to_string(duration, string, sizeof(string));
161 SetText(string);