xref: /haiku/src/apps/mediaplayer/ControllerView.cpp (revision 37c7d5d83a2372a6971e383411d5bacbeef0ebdc)
1 /*
2  * Controller.cpp - Media Player for the Haiku Operating System
3  *
4  * Copyright (C) 2006 Marcus Overhagen <marcus@overhagen.de>
5  * Copyright (C) 2007 Stephan Aßmus <superstippi@gmx.de>
6  * Copyright (C) 2008-2009 Fredrik Modéen 	<[FirstName]@[LastName].se> (MIT ok)
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  *
21  */
22 
23 
24 #include "ControllerView.h"
25 
26 #include <Autolock.h>
27 #include <Message.h>
28 #include <stdio.h>
29 #include <string.h>
30 
31 #include "Controller.h"
32 #include "Playlist.h"
33 #include "PlaylistObserver.h"
34 
35 
36 ControllerView::ControllerView(BRect frame, Controller* controller,
37 		Playlist* playlist)
38 	:
39 	TransportControlGroup(frame, true, true, false),
40 	fController(controller),
41 	fPlaylist(playlist),
42 	fPlaylistObserver(new PlaylistObserver(this))
43 {
44 	fPlaylist->AddListener(fPlaylistObserver);
45 }
46 
47 
48 ControllerView::~ControllerView()
49 {
50 	fPlaylist->RemoveListener(fPlaylistObserver);
51 	delete fPlaylistObserver;
52 }
53 
54 
55 void
56 ControllerView::AttachedToWindow()
57 {
58 	TransportControlGroup::AttachedToWindow();
59 }
60 
61 
62 void
63 ControllerView::Draw(BRect updateRect)
64 {
65 	TransportControlGroup::Draw(updateRect);
66 }
67 
68 
69 void
70 ControllerView::MessageReceived(BMessage* message)
71 {
72 	switch (message->what) {
73 		case MSG_PLAYLIST_ITEM_ADDED:
74 		case MSG_PLAYLIST_ITEM_REMOVED:
75 		case MSG_PLAYLIST_ITEMS_SORTED:
76 		case MSG_PLAYLIST_CURRENT_ITEM_CHANGED:
77 			_CheckSkippable();
78 			break;
79 
80 		default:
81 			TransportControlGroup::MessageReceived(message);
82 	}
83 }
84 
85 
86 // #pragma mark -
87 
88 
89 uint32
90 ControllerView::EnabledButtons()
91 {
92 	// TODO: superflous
93 	return 0xffffffff;
94 }
95 
96 
97 void
98 ControllerView::TogglePlaying()
99 {
100 	BAutolock _(fPlaylist);
101 	if (fPlaylist->CurrentItemIndex() == fPlaylist->CountItems() - 1
102 		&& Position() == 1.0) {
103 		// Reached end of playlist and end of last item
104 		// -> start again from the first item.
105 		fPlaylist->SetCurrentItemIndex(0);
106 	} else
107 		fController->TogglePlaying();
108 }
109 
110 
111 void
112 ControllerView::Stop()
113 {
114 	fController->Stop();
115 }
116 
117 
118 void
119 ControllerView::Rewind()
120 {
121 	// TODO: make it so this function is called repeatedly
122 	//printf("ControllerView::Rewind()\n");
123 }
124 
125 
126 void
127 ControllerView::Forward()
128 {
129 	// TODO: make it so this function is called repeatedly
130 	//printf("ControllerView::Forward()\n");
131 }
132 
133 
134 void
135 ControllerView::SkipBackward()
136 {
137 	BAutolock _(fPlaylist);
138 	fPlaylist->SetCurrentItemIndex(fPlaylist->CurrentItemIndex() - 1);
139 }
140 
141 
142 void
143 ControllerView::SkipForward()
144 {
145 	BAutolock _(fPlaylist);
146 	fPlaylist->SetCurrentItemIndex(fPlaylist->CurrentItemIndex() + 1);
147 }
148 
149 
150 void
151 ControllerView::VolumeChanged(float value)
152 {
153 	fController->SetVolume(value);
154 }
155 
156 
157 void
158 ControllerView::ToggleMute()
159 {
160 	fController->ToggleMute();
161 }
162 
163 
164 void
165 ControllerView::PositionChanged(float value)
166 {
167 	// 0.0 ... 1.0
168 	fController->SetPosition(value);
169 }
170 
171 
172 // #pragma mark -
173 
174 
175 void
176 ControllerView::_CheckSkippable()
177 {
178 	BAutolock _(fPlaylist);
179 
180 	bool canSkipNext, canSkipPrevious;
181 	fPlaylist->GetSkipInfo(&canSkipPrevious, &canSkipNext);
182 	SetSkippable(canSkipPrevious, canSkipNext);
183 }
184