xref: /haiku/src/apps/mediaplayer/ControllerView.cpp (revision 95c9effd68127df2dce202d5e254a7c86560010a)
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  * Released under the terms of the MIT license.
9  */
10 
11 
12 #include "ControllerView.h"
13 
14 #include <Autolock.h>
15 #include <Message.h>
16 #include <stdio.h>
17 #include <string.h>
18 
19 #include "Controller.h"
20 #include "Playlist.h"
21 #include "PlaylistObserver.h"
22 
23 
24 ControllerView::ControllerView(BRect frame, Controller* controller,
25 		Playlist* playlist)
26 	:
27 	TransportControlGroup(frame, true, true, false),
28 	fController(controller),
29 	fPlaylist(playlist),
30 	fPlaylistObserver(new PlaylistObserver(this))
31 {
32 	fPlaylist->AddListener(fPlaylistObserver);
33 }
34 
35 
36 ControllerView::~ControllerView()
37 {
38 	fPlaylist->RemoveListener(fPlaylistObserver);
39 	delete fPlaylistObserver;
40 }
41 
42 
43 void
44 ControllerView::AttachedToWindow()
45 {
46 	TransportControlGroup::AttachedToWindow();
47 }
48 
49 
50 void
51 ControllerView::Draw(BRect updateRect)
52 {
53 	TransportControlGroup::Draw(updateRect);
54 }
55 
56 
57 void
58 ControllerView::MessageReceived(BMessage* message)
59 {
60 	switch (message->what) {
61 		case MSG_PLAYLIST_ITEM_ADDED:
62 		case MSG_PLAYLIST_ITEM_REMOVED:
63 		case MSG_PLAYLIST_ITEMS_SORTED:
64 		case MSG_PLAYLIST_CURRENT_ITEM_CHANGED:
65 			_CheckSkippable();
66 			break;
67 
68 		case MSG_PLAYLIST_IMPORT_FAILED:
69 			break;
70 
71 		default:
72 			TransportControlGroup::MessageReceived(message);
73 	}
74 }
75 
76 
77 // #pragma mark -
78 
79 
80 void
81 ControllerView::TogglePlaying()
82 {
83 	BAutolock _(fPlaylist);
84 	if (fPlaylist->CurrentItemIndex() == fPlaylist->CountItems() - 1
85 		&& Position() == 1.0) {
86 		// Reached end of playlist and end of last item
87 		// -> start again from the first item.
88 		fPlaylist->SetCurrentItemIndex(0, true);
89 	} else
90 		fController->TogglePlaying();
91 }
92 
93 
94 void
95 ControllerView::Stop()
96 {
97 	fController->Stop();
98 }
99 
100 
101 void
102 ControllerView::Rewind()
103 {
104 	// TODO: make it so this function is called repeatedly
105 	//printf("ControllerView::Rewind()\n");
106 }
107 
108 
109 void
110 ControllerView::Forward()
111 {
112 	// TODO: make it so this function is called repeatedly
113 	//printf("ControllerView::Forward()\n");
114 }
115 
116 
117 void
118 ControllerView::SkipBackward()
119 {
120 	BAutolock _(fPlaylist);
121 	int32 index = fPlaylist->CurrentItemIndex() - 1;
122 	if (index < 0)
123 		index = 0;
124 	fPlaylist->SetCurrentItemIndex(index, true);
125 }
126 
127 
128 void
129 ControllerView::SkipForward()
130 {
131 	BAutolock _(fPlaylist);
132 	int32 index = fPlaylist->CurrentItemIndex() + 1;
133 	if (index >= fPlaylist->CountItems())
134 		index = fPlaylist->CountItems() - 1;
135 	fPlaylist->SetCurrentItemIndex(index, true);
136 }
137 
138 
139 void
140 ControllerView::VolumeChanged(float value)
141 {
142 	fController->SetVolume(value);
143 }
144 
145 
146 void
147 ControllerView::ToggleMute()
148 {
149 	fController->ToggleMute();
150 }
151 
152 
153 void
154 ControllerView::PositionChanged(float value)
155 {
156 	// 0.0 ... 1.0
157 	fController->SetPosition(value);
158 }
159 
160 
161 // #pragma mark -
162 
163 
164 void
165 ControllerView::_CheckSkippable()
166 {
167 	BAutolock _(fPlaylist);
168 
169 	bool canSkipNext, canSkipPrevious;
170 	fPlaylist->GetSkipInfo(&canSkipPrevious, &canSkipNext);
171 	SetSkippable(canSkipPrevious, canSkipNext);
172 }
173