-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStationListView.cpp
231 lines (199 loc) · 7.19 KB
/
StationListView.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* File: StationListView.cpp
* Author: user
*
* Created on 26.02.2013, 09:22
*/
#include <Application.h>
#include <Resources.h>
#include <TranslationUtils.h>
#include "StreamPlayer.h"
#include "StationListView.h"
#include "Utils.h"
#define SLV_INSET 2
#define SLV_PADDING 6
#define SLV_HEIGHT 50
#define SLV_MAIN_FONT_SIZE 13
#define SLV_SMALL_FONT_SIZE 10
#define SLV_BUTTON_SIZE 20
StationListViewItem::StationListViewItem(Station* station)
: BListItem(0, true),
fPlayer(NULL)
{
fStation = station;
SetHeight(SLV_HEIGHT);
}
StationListViewItem::~StationListViewItem() {
if (fPlayer) {
fPlayer->Stop();
delete fPlayer;
}
}
StreamPlayer::PlayState
StationListViewItem::State() {
return (fPlayer) ?
fPlayer->State()
:
fStation->Flags(STATION_URI_VALID) ?
StreamPlayer::Stopped
:
StreamPlayer::InActive;
}
void StationListViewItem::DrawItem(BView* owner, BRect frame, bool complete) {
int index = ((BListView*)owner)->IndexOf(this);
StationListView* ownerList = (StationListView*)owner;
ownerList->SetHighColor(ui_color(IsSelected() ? B_MENU_SELECTION_BACKGROUND_COLOR :
((index % 2) ? B_MENU_BACKGROUND_COLOR : B_DOCUMENT_BACKGROUND_COLOR)));
ownerList->FillRect(frame);
owner->SetHighColor(ui_color(IsSelected() ? B_MENU_SELECTED_ITEM_TEXT_COLOR : B_MENU_ITEM_TEXT_COLOR));
owner->SetLowColor(ui_color(IsSelected() ? B_MENU_SELECTION_BACKGROUND_COLOR : B_MENU_BACKGROUND_COLOR));
if (BBitmap* logo = fStation->Logo()) {
BRect target(SLV_INSET, SLV_INSET, SLV_HEIGHT - 2 * SLV_INSET, SLV_HEIGHT - 2 * SLV_INSET);
target.OffsetBy(frame.LeftTop());
owner->DrawBitmap(logo, logo->Bounds(), target, B_FILTER_BITMAP_BILINEAR);
}
owner->SetFontSize(SLV_MAIN_FONT_SIZE);
font_height fontHeight;
owner->GetFontHeight(&fontHeight);
float baseline = SLV_INSET + fontHeight.ascent + fontHeight.leading;
owner->DrawString(fStation->Name()->String(), frame.LeftTop() + BPoint(SLV_HEIGHT - SLV_INSET + SLV_PADDING, SLV_INSET + baseline));
owner->SetFontSize(SLV_SMALL_FONT_SIZE);
owner->GetFontHeight(&fontHeight);
baseline += fontHeight.ascent + fontHeight.descent + fontHeight.leading;
owner->DrawString(fStation->StreamUrl().UrlString(), frame.LeftTop() + BPoint(SLV_HEIGHT - SLV_INSET + SLV_PADDING, baseline));
baseline += fontHeight.ascent + fontHeight.descent + fontHeight.leading;
owner->DrawString(BString("") << fStation->BitRate()/1000.0 << " kbps " << fStation->Mime()->Type(), frame.LeftTop() + BPoint(SLV_HEIGHT - SLV_INSET + SLV_PADDING, baseline));
frame = ownerList->ItemFrame(ownerList->StationIndex(fStation));
if (ownerList->CanPlay() && fStation->Flags(STATION_URI_VALID)) {
BBitmap* bnBitmap = getButtonBitmap(State());
if (bnBitmap != NULL) {
owner->SetDrawingMode(B_OP_ALPHA);
owner->DrawBitmap(bnBitmap, frame.LeftTop()+BPoint(SLV_INSET, SLV_HEIGHT - SLV_INSET - SLV_BUTTON_SIZE));
owner->SetDrawingMode(B_OP_COPY);
}
}
}
void StationListViewItem::DrawBufferFillBar(BView* owner, BRect frame) {
if (State() == StreamPlayer::Playing || State() == StreamPlayer::Buffering) {
frame.bottom -= SLV_INSET;
frame.left = SLV_HEIGHT - SLV_INSET + SLV_PADDING;
frame.top = frame.bottom - 5;
frame.right -= SLV_PADDING;
owner->SetHighColor(tint_color(ui_color(B_MENU_BACKGROUND_COLOR), B_LIGHTEN_2_TINT));
owner->FillRect(frame);
owner->SetHighColor(200, 30, 0);
frame.right = frame.left + frame.Width() * fFillRatio;
owner->FillRect(frame);
}
}
void StationListViewItem::Update(BView* owner, const BFont* font) {
SetHeight(SLV_HEIGHT);
}
void
StationListViewItem::StateChanged(StreamPlayer::PlayState newState) {
if (fList && fList->LockLooperWithTimeout(0) == B_OK) {
fList->InvalidateItem(fList->IndexOf(this));
fList->UnlockLooper();
}
}
BBitmap*
StationListViewItem::getButtonBitmap(StreamPlayer::PlayState state) {
if (state < 0) return NULL;
if (buttonBitmap[state] == NULL)
buttonBitmap[state] = Utils::ResourceBitmap(RES_BN_STOPPED + state);
return buttonBitmap[state];
}
BBitmap* StationListViewItem::buttonBitmap[3] = { NULL, NULL, NULL };
void
StationListViewItem::SetFillRatio(float fillRatio) {
fFillRatio = fillRatio;
if (fList->LockLooperWithTimeout(0) == B_OK) {
BRect frame = fList->ItemFrame(fList->IndexOf(this));
DrawBufferFillBar(fList, frame);
fList->UnlockLooper();
}
}
StationListView::StationListView(bool canPlay)
: BListView("Stations", B_SINGLE_SELECTION_LIST),
fCanPlay(canPlay)
{
SetResizingMode(B_FOLLOW_ALL_SIDES);
SetExplicitMinSize(BSize(300, 2 * SLV_HEIGHT));
playMsg = NULL;
}
StationListView::~StationListView() {
delete playMsg;
}
bool
StationListView::AddItem(StationListViewItem* item) {
item->fList = this;
return BListView::AddItem(item);
}
bool
StationListView::AddItem(Station* station) {
return AddItem(new StationListViewItem(station));
}
int32 StationListView::StationIndex(Station* station) {
for (int32 i = 0; i < CountItems(); i++) {
if (((StationListViewItem*)ItemAt(i))->GetStation() == station)
return i;
}
return -1;
}
StationListViewItem* StationListView::ItemAt(int32 index) {
return (StationListViewItem*)BListView::ItemAt(index);
}
Station* StationListView::StationAt(int32 index) {
StationListViewItem* item = ItemAt(index);
return (item) ? item->GetStation() : NULL;
}
StationListViewItem* StationListView::Item(Station* station) {
for (int32 i = 0; i < CountItems(); i++) {
StationListViewItem* stationItem = (StationListViewItem*)ItemAt(i);
if (stationItem->GetStation() == station)
return stationItem;
}
return NULL;
}
void StationListView::Sync(StationsList* stations) {
LockLooper();
for (int32 i = CountItems() -1; i >= 0; i--) {
StationListViewItem* stationItem = (StationListViewItem*)ItemAt(i);
if (!stations->HasItem(stationItem->GetStation())) {
RemoveItem(i);
delete stationItem;
}
}
for (int32 i = 0; i < stations->CountItems(); i++) {
Station* station = stations->ItemAt(i);
if (Item(station) == NULL) {
AddItem(station);
}
}
Invalidate();
UnlockLooper();
}
void StationListView::SetPlayMessage(BMessage* playMsg) {
delete this->playMsg;
this->playMsg = playMsg;
}
void StationListView::MouseDown(BPoint where) {
whereDown = where;
BListView::MouseDown(where);
}
void StationListView::MouseUp(BPoint where) {
whereDown -= where;
if (whereDown.x * whereDown.x + whereDown.y * whereDown.y < 5 && playMsg) {
int stationIndex = IndexOf(where);
BRect playFrame = ItemFrame(stationIndex);
playFrame.InsetBy(SLV_INSET, SLV_INSET);
playFrame.right = playFrame.left + SLV_BUTTON_SIZE;
playFrame.top = playFrame.bottom - SLV_BUTTON_SIZE;
if (playFrame.Contains(where)) {
BMessage* clone = new BMessage(playMsg->what);
clone->AddInt32("index", stationIndex);
InvokeNotify(clone);
}
}
whereDown = BPoint(-1, -1);
}