xref: /haiku/src/apps/mediaplayer/ControllerView.cpp (revision 21258e2674226d6aa732321b6f8494841895af5f)
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 			break;
74 	}
75 }
76 
77 
78 // #pragma mark -
79 
80 
81 void
82 ControllerView::TogglePlaying()
83 {
84 	BAutolock _(fPlaylist);
85 	if (fPlaylist->CurrentItemIndex() == fPlaylist->CountItems() - 1
86 		&& Position() == 1.0) {
87 		// Reached end of playlist and end of last item
88 		// -> start again from the first item.
89 		fPlaylist->SetCurrentItemIndex(0, true);
90 	} else
91 		fController->TogglePlaying();
92 }
93 
94 
95 void
96 ControllerView::Stop()
97 {
98 	fController->Stop();
99 }
100 
101 
102 void
103 ControllerView::Rewind()
104 {
105 	// TODO: make it so this function is called repeatedly
106 	//printf("ControllerView::Rewind()\n");
107 }
108 
109 
110 void
111 ControllerView::Forward()
112 {
113 	// TODO: make it so this function is called repeatedly
114 	//printf("ControllerView::Forward()\n");
115 }
116 
117 
118 void
119 ControllerView::SkipBackward()
120 {
121 	BAutolock _(fPlaylist);
122 	int32 index = fPlaylist->CurrentItemIndex() - 1;
123 	if (index < 0)
124 		index = 0;
125 	fPlaylist->SetCurrentItemIndex(index, true);
126 }
127 
128 
129 void
130 ControllerView::SkipForward()
131 {
132 	BAutolock _(fPlaylist);
133 	int32 index = fPlaylist->CurrentItemIndex() + 1;
134 	if (index >= fPlaylist->CountItems())
135 		index = fPlaylist->CountItems() - 1;
136 	fPlaylist->SetCurrentItemIndex(index, true);
137 }
138 
139 
140 void
141 ControllerView::VolumeChanged(float value)
142 {
143 	fController->SetVolume(value);
144 }
145 
146 
147 void
148 ControllerView::ToggleMute()
149 {
150 	fController->ToggleMute();
151 }
152 
153 
154 void
155 ControllerView::PositionChanged(float value)
156 {
157 	// 0.0 ... 1.0
158 	fController->SetPosition(value);
159 }
160 
161 
162 // #pragma mark -
163 
164 
165 void
166 ControllerView::_CheckSkippable()
167 {
168 	BAutolock _(fPlaylist);
169 
170 	bool canSkipNext, canSkipPrevious;
171 	fPlaylist->GetSkipInfo(&canSkipPrevious, &canSkipNext);
172 	SetSkippable(canSkipPrevious, canSkipNext);
173 }
174