repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / apps / mediaplayer / ControllerView.cpp
blob07d639c9e7c44bebbfe4c289eb78fd701f9ceddd
1 /*
2 * Controller.cpp - Media Player for the Haiku Operating System
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)
8 * Released under the terms of the MIT license.
9 */
12 #include "ControllerView.h"
14 #include <Autolock.h>
15 #include <Message.h>
16 #include <stdio.h>
17 #include <string.h>
19 #include "Controller.h"
20 #include "Playlist.h"
21 #include "PlaylistObserver.h"
24 ControllerView::ControllerView(BRect frame, Controller* controller,
25 Playlist* playlist)
27 TransportControlGroup(frame, true, true, false),
28 fController(controller),
29 fPlaylist(playlist),
30 fPlaylistObserver(new PlaylistObserver(this))
32 fPlaylist->AddListener(fPlaylistObserver);
36 ControllerView::~ControllerView()
38 fPlaylist->RemoveListener(fPlaylistObserver);
39 delete fPlaylistObserver;
43 void
44 ControllerView::AttachedToWindow()
46 TransportControlGroup::AttachedToWindow();
50 void
51 ControllerView::Draw(BRect updateRect)
53 TransportControlGroup::Draw(updateRect);
57 void
58 ControllerView::MessageReceived(BMessage* message)
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;
68 case MSG_PLAYLIST_IMPORT_FAILED:
69 break;
71 default:
72 TransportControlGroup::MessageReceived(message);
77 // #pragma mark -
80 void
81 ControllerView::TogglePlaying()
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();
94 void
95 ControllerView::Stop()
97 fController->Stop();
101 void
102 ControllerView::Rewind()
104 // TODO: make it so this function is called repeatedly
105 //printf("ControllerView::Rewind()\n");
109 void
110 ControllerView::Forward()
112 // TODO: make it so this function is called repeatedly
113 //printf("ControllerView::Forward()\n");
117 void
118 ControllerView::SkipBackward()
120 BAutolock _(fPlaylist);
121 int32 index = fPlaylist->CurrentItemIndex() - 1;
122 if (index < 0)
123 index = 0;
124 fPlaylist->SetCurrentItemIndex(index, true);
128 void
129 ControllerView::SkipForward()
131 BAutolock _(fPlaylist);
132 int32 index = fPlaylist->CurrentItemIndex() + 1;
133 if (index >= fPlaylist->CountItems())
134 index = fPlaylist->CountItems() - 1;
135 fPlaylist->SetCurrentItemIndex(index, true);
139 void
140 ControllerView::VolumeChanged(float value)
142 fController->SetVolume(value);
146 void
147 ControllerView::ToggleMute()
149 fController->ToggleMute();
153 void
154 ControllerView::PositionChanged(float value)
156 // 0.0 ... 1.0
157 fController->SetPosition(value);
161 // #pragma mark -
164 void
165 ControllerView::_CheckSkippable()
167 BAutolock _(fPlaylist);
169 bool canSkipNext, canSkipPrevious;
170 fPlaylist->GetSkipInfo(&canSkipPrevious, &canSkipNext);
171 SetSkippable(canSkipPrevious, canSkipNext);