MainWin.cpp revision 9282400ff444c8d85c264f0f5fd16d1c639b7fae
1/*
2 * MainWin.cpp - Media Player for the Haiku Operating System
3 *
4 * Copyright (C) 2006 Marcus Overhagen <marcus@overhagen.de>
5 * Copyright (C) 2007-2008 Stephan A��mus <superstippi@gmx.de> (GPL->MIT ok)
6 * Copyright (C) 2007-2008 Fredrik Mod��en <fredrik@modeen.se>
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,
20 * USA.
21 *
22 */
23#include "MainWin.h"
24
25#include <math.h>
26#include <stdio.h>
27#include <string.h>
28
29#include <Alert.h>
30#include <Application.h>
31#include <Autolock.h>
32#include <Debug.h>
33#include <Menu.h>
34#include <MenuBar.h>
35#include <MenuItem.h>
36#include <Messenger.h>
37#include <PopUpMenu.h>
38#include <RecentItems.h>
39#include <Roster.h>
40#include <Screen.h>
41#include <String.h>
42#include <View.h>
43
44#include "AudioProducer.h"
45#include "ControllerObserver.h"
46#include "MainApp.h"
47#include "PeakView.h"
48#include "PlaylistObserver.h"
49#include "PlaylistWindow.h"
50#include "SettingsWindow.h"
51
52#define NAME "MediaPlayer"
53#define MIN_WIDTH 250
54
55
56// XXX TODO: why is lround not defined?
57#define lround(a) ((int)(0.99999 + (a)))
58
59enum {
60	M_DUMMY = 0x100,
61	M_FILE_OPEN = 0x1000,
62	M_FILE_NEWPLAYER,
63	M_FILE_INFO,
64	M_FILE_PLAYLIST,
65	M_FILE_CLOSE,
66	M_FILE_QUIT,
67	M_VIEW_50,
68	M_VIEW_100,
69	M_VIEW_200,
70	M_VIEW_300,
71	M_VIEW_400,
72	M_TOGGLE_FULLSCREEN,
73	M_TOGGLE_NO_BORDER,
74	M_TOGGLE_NO_MENU,
75	M_TOGGLE_NO_CONTROLS,
76	M_TOGGLE_NO_BORDER_NO_MENU_NO_CONTROLS,
77	M_TOGGLE_ALWAYS_ON_TOP,
78	M_TOGGLE_KEEP_ASPECT_RATIO,
79	M_SETTINGS,
80	M_VOLUME_UP,
81	M_VOLUME_DOWN,
82	M_SKIP_NEXT,
83	M_SKIP_PREV,
84	M_ASPECT_100000_1,
85	M_ASPECT_106666_1,
86	M_ASPECT_109091_1,
87	M_ASPECT_141176_1,
88	M_ASPECT_720_576,
89	M_ASPECT_704_576,
90	M_ASPECT_544_576,
91	M_SELECT_AUDIO_TRACK		= 0x00000800,
92	M_SELECT_AUDIO_TRACK_END	= 0x00000fff,
93	M_SELECT_VIDEO_TRACK		= 0x00010000,
94	M_SELECT_VIDEO_TRACK_END	= 0x000fffff,
95
96	M_SET_PLAYLIST_POSITION
97};
98
99//#define printf(a...)
100
101
102MainWin::MainWin()
103 :	BWindow(BRect(100,100,400,300), NAME, B_TITLED_WINDOW,
104 		B_ASYNCHRONOUS_CONTROLS /* | B_WILL_ACCEPT_FIRST_CLICK */)
105 ,  fFilePanel(NULL)
106 ,	fInfoWin(NULL)
107 ,	fPlaylistWindow(NULL)
108 ,	fSettingsWindow(NULL)
109 ,	fHasFile(false)
110 ,	fHasVideo(false)
111 ,	fHasAudio(false)
112 ,	fPlaylist(new Playlist)
113 ,	fPlaylistObserver(new PlaylistObserver(this))
114 ,	fController(new Controller)
115 ,	fControllerObserver(new ControllerObserver(this,
116 		OBSERVE_FILE_CHANGES | OBSERVE_TRACK_CHANGES
117 			| OBSERVE_PLAYBACK_STATE_CHANGES | OBSERVE_POSITION_CHANGES
118 			| OBSERVE_VOLUME_CHANGES))
119 ,	fIsFullscreen(false)
120 ,	fKeepAspectRatio(true)
121 ,	fAlwaysOnTop(false)
122 ,	fNoMenu(false)
123 ,	fNoBorder(false)
124 ,	fNoControls(false)
125 ,	fSourceWidth(0)
126 ,	fSourceHeight(0)
127 ,	fWidthScale(1.0)
128 ,	fHeightScale(1.0)
129 ,	fMouseDownTracking(false)
130{
131	static int pos = 0;
132	MoveBy(pos * 25, pos * 25);
133	pos = (pos + 1) % 15;
134
135	BRect rect = Bounds();
136
137	// background
138	fBackground = new BView(rect, "background", B_FOLLOW_ALL,
139		B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE);
140	fBackground->SetViewColor(0,0,0);
141	AddChild(fBackground);
142
143	// menu
144	fMenuBar = new BMenuBar(fBackground->Bounds(), "menu");
145	_CreateMenu();
146	fBackground->AddChild(fMenuBar);
147	fMenuBar->ResizeToPreferred();
148	fMenuBarWidth = (int)fMenuBar->Frame().Width() + 1;
149	fMenuBarHeight = (int)fMenuBar->Frame().Height() + 1;
150	fMenuBar->SetResizingMode(B_FOLLOW_NONE);
151
152	// video view
153	rect = BRect(0, fMenuBarHeight, fBackground->Bounds().right,
154		fMenuBarHeight + 10);
155	fVideoView = new VideoView(rect, "video display", B_FOLLOW_NONE);
156	fBackground->AddChild(fVideoView);
157
158	// controls
159	rect = BRect(0, fMenuBarHeight + 11, fBackground->Bounds().right,
160		fBackground->Bounds().bottom);
161	fControls = new ControllerView(rect, fController, fPlaylist);
162	fBackground->AddChild(fControls);
163	fControls->ResizeToPreferred();
164	fControlsHeight = (int)fControls->Frame().Height() + 1;
165	fControlsWidth = (int)fControls->Frame().Width() + 1;
166	fControls->SetResizingMode(B_FOLLOW_BOTTOM | B_FOLLOW_LEFT_RIGHT);
167//	fControls->MoveTo(0, fBackground->Bounds().bottom - fControlsHeight + 1);
168
169//	fVideoView->ResizeTo(fBackground->Bounds().Width(),
170//		fBackground->Bounds().Height() - fMenuBarHeight - fControlsHeight);
171
172	fPlaylist->AddListener(fPlaylistObserver);
173	fController->SetVideoView(fVideoView);
174	fController->AddListener(fControllerObserver);
175	PeakView* peakView = fControls->GetPeakView();
176	peakView->SetPeakNotificationWhat(MSG_PEAK_NOTIFICATION);
177	fController->SetPeakListener(peakView);
178
179//	printf("fMenuBarHeight %d\n", fMenuBarHeight);
180//	printf("fControlsHeight %d\n", fControlsHeight);
181//	printf("fControlsWidth %d\n", fControlsWidth);
182
183	_SetupWindow();
184
185	// setup the settings window now, we need to have it
186	fSettingsWindow = new SettingsWindow(BRect(150, 150, 450, 520));
187	fSettingsWindow->Hide();
188	fSettingsWindow->Show();
189
190	// setup the playlist window now, we need to have it
191	// running for the undo/redo playlist editing
192	fPlaylistWindow = new PlaylistWindow(BRect(150, 150, 500, 600), fPlaylist,
193		fController);
194	fPlaylistWindow->Hide();
195	fPlaylistWindow->Show();
196		// this makes sure the window thread is running without
197		// showing the window just yet
198
199	Show();
200}
201
202
203MainWin::~MainWin()
204{
205	printf("MainWin::~MainWin\n");
206
207	fPlaylist->RemoveListener(fPlaylistObserver);
208	fController->RemoveListener(fControllerObserver);
209	fController->SetPeakListener(NULL);
210
211	// give the views a chance to detach from any notifiers
212	// before we delete them
213	fBackground->RemoveSelf();
214	delete fBackground;
215
216	if (fInfoWin) {
217		fInfoWin->Lock();
218		fInfoWin->Quit();
219	}
220	if (fPlaylistWindow) {
221		fPlaylistWindow->Lock();
222		fPlaylistWindow->Quit();
223	}
224
225	if (fSettingsWindow) {
226		fSettingsWindow->Lock();
227		fSettingsWindow->Quit();
228	}
229
230	delete fPlaylist;
231	delete fFilePanel;
232
233	// quit the Controller looper thread
234	thread_id controllerThread = fController->Thread();
235	fController->PostMessage(B_QUIT_REQUESTED);
236	status_t exitValue;
237	wait_for_thread(controllerThread, &exitValue);
238}
239
240
241// #pragma mark -
242
243
244void
245MainWin::FrameResized(float new_width, float new_height)
246{
247	if (new_width != Bounds().Width() || new_height != Bounds().Height()) {
248		debugger("size wrong\n");
249	}
250
251	bool no_menu = fNoMenu || fIsFullscreen;
252	bool no_controls = fNoControls || fIsFullscreen;
253
254	printf("FrameResized enter: new_width %.0f, new_height %.0f\n",
255		new_width, new_height);
256
257	int max_video_width  = int(new_width) + 1;
258	int max_video_height = int(new_height) + 1
259		- (no_menu  ? 0 : fMenuBarHeight)
260		- (no_controls ? 0 : fControlsHeight);
261
262	ASSERT(max_video_height >= 0);
263
264	int y = 0;
265
266	if (no_menu) {
267		if (!fMenuBar->IsHidden())
268			fMenuBar->Hide();
269	} else {
270//		fMenuBar->MoveTo(0, y);
271		fMenuBar->ResizeTo(new_width, fMenuBarHeight - 1);
272		if (fMenuBar->IsHidden())
273			fMenuBar->Show();
274		y += fMenuBarHeight;
275	}
276
277	if (max_video_height == 0) {
278		if (!fVideoView->IsHidden())
279			fVideoView->Hide();
280	} else {
281//		fVideoView->MoveTo(0, y);
282//		fVideoView->ResizeTo(max_video_width - 1, max_video_height - 1);
283		_ResizeVideoView(0, y, max_video_width, max_video_height);
284		if (fVideoView->IsHidden())
285			fVideoView->Show();
286		y += max_video_height;
287	}
288
289	if (no_controls) {
290		if (!fControls->IsHidden())
291			fControls->Hide();
292	} else {
293		fControls->MoveTo(0, y);
294		fControls->ResizeTo(new_width, fControlsHeight - 1);
295		if (fControls->IsHidden())
296			fControls->Show();
297//		y += fControlsHeight;
298	}
299
300	printf("FrameResized leave\n");
301}
302
303
304void
305MainWin::Zoom(BPoint rec_position, float rec_width, float rec_height)
306{
307	PostMessage(M_TOGGLE_FULLSCREEN);
308}
309
310
311void
312MainWin::DispatchMessage(BMessage *msg, BHandler *handler)
313{
314	if ((msg->what == B_MOUSE_DOWN)
315		&& (handler == fBackground || handler == fVideoView
316				|| handler == fControls))
317		_MouseDown(msg, dynamic_cast<BView*>(handler));
318
319	if ((msg->what == B_MOUSE_MOVED)
320		&& (handler == fBackground || handler == fVideoView
321				|| handler == fControls))
322		_MouseMoved(msg, dynamic_cast<BView*>(handler));
323
324	if ((msg->what == B_MOUSE_UP)
325		&& (handler == fBackground || handler == fVideoView))
326		_MouseUp(msg);
327
328	if ((msg->what == B_KEY_DOWN)
329		&& (handler == fBackground || handler == fVideoView)) {
330
331		// special case for PrintScreen key
332		if (msg->FindInt32("key") == B_PRINT_KEY) {
333			fVideoView->OverlayScreenshotPrepare();
334			BWindow::DispatchMessage(msg, handler);
335			fVideoView->OverlayScreenshotCleanup();
336			return;
337		}
338
339		// every other key gets dispatched to our _KeyDown first
340		if (_KeyDown(msg) == B_OK) {
341			// it got handled, don't pass it on
342			return;
343		}
344	}
345
346	BWindow::DispatchMessage(msg, handler);
347}
348
349
350void
351MainWin::MessageReceived(BMessage *msg)
352{
353//	msg->PrintToStream();
354	switch (msg->what) {
355		case B_REFS_RECEIVED:
356			printf("MainWin::MessageReceived: B_REFS_RECEIVED\n");
357			_RefsReceived(msg);
358			break;
359		case B_SIMPLE_DATA:
360			printf("MainWin::MessageReceived: B_SIMPLE_DATA\n");
361			if (msg->HasRef("refs")) {
362				// add to recent documents as it's not done with drag-n-drop
363				entry_ref ref;
364				for (int32 i = 0; msg->FindRef("refs", i, &ref) == B_OK; i++) {
365					be_roster->AddToRecentDocuments(&ref, kAppSig);
366				}
367				_RefsReceived(msg);
368			}
369			break;
370
371		case M_MEDIA_SERVER_STARTED:
372			// fController->...
373			break;
374
375		case M_MEDIA_SERVER_QUIT:
376			// fController->...
377			break;
378
379		// PlaylistObserver messages
380		case MSG_PLAYLIST_REF_ADDED: {
381			entry_ref ref;
382			int32 index;
383			if (msg->FindRef("refs", &ref) == B_OK
384				&& msg->FindInt32("index", &index) == B_OK) {
385				_AddPlaylistItem(ref, index);
386			}
387			break;
388		}
389		case MSG_PLAYLIST_REF_REMOVED: {
390			int32 index;
391			if (msg->FindInt32("index", &index) == B_OK) {
392				_RemovePlaylistItem(index);
393			}
394			break;
395		}
396		case MSG_PLAYLIST_CURRENT_REF_CHANGED: {
397			BAutolock _(fPlaylist);
398
399			int32 index;
400			if (msg->FindInt32("index", &index) < B_OK
401				|| index != fPlaylist->CurrentRefIndex())
402				break;
403			entry_ref ref;
404			if (fPlaylist->GetRefAt(index, &ref) == B_OK) {
405				printf("open ref: %s\n", ref.name);
406				OpenFile(ref);
407				_MarkPlaylistItem(index);
408			}
409			break;
410		}
411
412		// ControllerObserver messages
413		case MSG_CONTROLLER_FILE_FINISHED:
414			fPlaylist->SetCurrentRefIndex(fPlaylist->CurrentRefIndex() + 1);
415			break;
416		case MSG_CONTROLLER_FILE_CHANGED:
417			// TODO: move all other GUI changes as a reaction to this
418			// notification
419//			_UpdatePlaylistMenu();
420			break;
421		case MSG_CONTROLLER_VIDEO_TRACK_CHANGED: {
422			int32 index;
423			if (msg->FindInt32("index", &index) == B_OK) {
424				BMenuItem* item = fVideoTrackMenu->ItemAt(index);
425				if (item)
426					item->SetMarked(true);
427			}
428			break;
429		}
430		case MSG_CONTROLLER_AUDIO_TRACK_CHANGED: {
431			int32 index;
432			if (msg->FindInt32("index", &index) == B_OK) {
433				BMenuItem* item = fAudioTrackMenu->ItemAt(index);
434				if (item)
435					item->SetMarked(true);
436			}
437			break;
438		}
439		case MSG_CONTROLLER_PLAYBACK_STATE_CHANGED: {
440			uint32 state;
441			if (msg->FindInt32("state", (int32*)&state) == B_OK)
442				fControls->SetPlaybackState(state);
443			break;
444		}
445		case MSG_CONTROLLER_POSITION_CHANGED: {
446			float position;
447			if (msg->FindFloat("position", &position) == B_OK)
448				fControls->SetPosition(position);
449			break;
450		}
451		case MSG_CONTROLLER_VOLUME_CHANGED: {
452			float volume;
453			if (msg->FindFloat("volume", &volume) == B_OK)
454				fControls->SetVolume(volume);
455			break;
456		}
457		case MSG_CONTROLLER_MUTED_CHANGED: {
458			bool muted;
459			if (msg->FindBool("muted", &muted) == B_OK)
460				fControls->SetMuted(muted);
461			break;
462		}
463
464		// menu item messages
465		case M_FILE_NEWPLAYER:
466			gMainApp->NewWindow();
467			break;
468		case M_FILE_OPEN:
469			if (!fFilePanel) {
470				fFilePanel = new BFilePanel();
471				fFilePanel->SetTarget(BMessenger(0, this));
472				fFilePanel->SetPanelDirectory("/boot/home/");
473			}
474			fFilePanel->Show();
475			break;
476		case M_FILE_INFO:
477			ShowFileInfo();
478			break;
479		case M_FILE_PLAYLIST:
480			ShowPlaylistWindow();
481			break;
482		case B_ABOUT_REQUESTED:
483			BAlert *alert;
484			alert = new BAlert("about", NAME"\n\n Written by Marcus Overhagen "
485				", Stephan A��mus and Frederik Mod��en", "Thanks");
486			if (fAlwaysOnTop) {
487				_ToggleAlwaysOnTop();
488				alert->Go(NULL);  // Asynchronous mode
489				_ToggleAlwaysOnTop();
490			} else {
491				alert->Go(NULL); // Asynchronous mode
492			}
493			break;
494		case M_FILE_CLOSE:
495			PostMessage(B_QUIT_REQUESTED);
496			break;
497		case M_FILE_QUIT:
498			be_app->PostMessage(B_QUIT_REQUESTED);
499			break;
500
501		case M_TOGGLE_FULLSCREEN:
502			_ToggleFullscreen();
503			break;
504
505		case M_TOGGLE_NO_MENU:
506			_ToggleNoMenu();
507			break;
508
509		case M_TOGGLE_NO_CONTROLS:
510			_ToggleNoControls();
511			break;
512
513		case M_TOGGLE_NO_BORDER:
514			_ToggleNoBorder();
515			break;
516
517		case M_TOGGLE_ALWAYS_ON_TOP:
518			_ToggleAlwaysOnTop();
519			break;
520
521		case M_TOGGLE_KEEP_ASPECT_RATIO:
522			_ToggleKeepAspectRatio();
523			break;
524
525		case M_TOGGLE_NO_BORDER_NO_MENU_NO_CONTROLS:
526			_ToggleNoBorderNoMenu();
527			break;
528
529		case M_VIEW_50:
530			if (!fHasVideo)
531				break;
532			if (fIsFullscreen)
533				_ToggleFullscreen();
534			_ResizeWindow(50);
535			break;
536
537		case M_VIEW_100:
538			if (!fHasVideo)
539				break;
540			if (fIsFullscreen)
541				_ToggleFullscreen();
542			_ResizeWindow(100);
543			break;
544
545		case M_VIEW_200:
546			if (!fHasVideo)
547				break;
548			if (fIsFullscreen)
549				_ToggleFullscreen();
550			_ResizeWindow(200);
551			break;
552
553		case M_VIEW_300:
554			if (!fHasVideo)
555				break;
556			if (fIsFullscreen)
557				_ToggleFullscreen();
558			_ResizeWindow(300);
559			break;
560
561		case M_VIEW_400:
562			if (!fHasVideo)
563				break;
564			if (fIsFullscreen)
565				_ToggleFullscreen();
566			_ResizeWindow(400);
567			break;
568
569/*
570
571		case B_ACQUIRE_OVERLAY_LOCK:
572			printf("B_ACQUIRE_OVERLAY_LOCK\n");
573			fVideoView->OverlayLockAcquire();
574			break;
575
576		case B_RELEASE_OVERLAY_LOCK:
577			printf("B_RELEASE_OVERLAY_LOCK\n");
578			fVideoView->OverlayLockRelease();
579			break;
580
581		case B_MOUSE_WHEEL_CHANGED:
582		{
583			printf("B_MOUSE_WHEEL_CHANGED\n");
584			float dx = msg->FindFloat("be:wheel_delta_x");
585			float dy = msg->FindFloat("be:wheel_delta_y");
586			bool inv = modifiers() & B_COMMAND_KEY;
587			if (dx > 0.1)	PostMessage(inv ? M_VOLUME_DOWN : M_SKIP_PREV);
588			if (dx < -0.1)	PostMessage(inv ? M_VOLUME_UP : M_SKIP_NEXT);
589			if (dy > 0.1)	PostMessage(inv ? M_SKIP_PREV : M_VOLUME_DOWN);
590			if (dy < -0.1)	PostMessage(inv ? M_SKIP_NEXT : M_VOLUME_UP);
591			break;
592		}
593*/
594		case M_SKIP_NEXT:
595			fControls->SkipForward();
596			break;
597
598		case M_SKIP_PREV:
599			fControls->SkipBackward();
600			break;
601
602		case M_VOLUME_UP:
603			fController->VolumeUp();
604			break;
605
606		case M_VOLUME_DOWN:
607			fController->VolumeDown();
608			break;
609
610		case M_ASPECT_100000_1:
611			VideoFormatChange(fSourceWidth, fSourceHeight, 1.0, 1.0);
612			break;
613
614		case M_ASPECT_106666_1:
615			VideoFormatChange(fSourceWidth, fSourceHeight, 1.06666, 1.0);
616			break;
617
618		case M_ASPECT_109091_1:
619			VideoFormatChange(fSourceWidth, fSourceHeight, 1.09091, 1.0);
620			break;
621
622		case M_ASPECT_141176_1:
623			VideoFormatChange(fSourceWidth, fSourceHeight, 1.41176, 1.0);
624			break;
625
626		case M_ASPECT_720_576:
627			VideoFormatChange(720, 576, 1.06666, 1.0);
628			break;
629
630		case M_ASPECT_704_576:
631			VideoFormatChange(704, 576, 1.09091, 1.0);
632			break;
633
634		case M_ASPECT_544_576:
635			VideoFormatChange(544, 576, 1.41176, 1.0);
636			break;
637
638		case M_SETTINGS:
639			ShowSettingsWindow();
640			break;
641/*
642		default:
643			if (msg->what >= M_SELECT_CHANNEL
644				&& msg->what <= M_SELECT_CHANNEL_END) {
645				SelectChannel(msg->what - M_SELECT_CHANNEL);
646				break;
647			}
648			if (msg->what >= M_SELECT_INTERFACE
649				&& msg->what <= M_SELECT_INTERFACE_END) {
650				SelectInterface(msg->what - M_SELECT_INTERFACE - 1);
651				break;
652			}
653*/
654		case M_SET_PLAYLIST_POSITION: {
655			int32 index;
656			if (msg->FindInt32("index", &index) == B_OK)
657				fPlaylist->SetCurrentRefIndex(index);
658			break;
659		}
660
661		default:
662			// let BWindow handle the rest
663			BWindow::MessageReceived(msg);
664	}
665}
666
667
668void
669MainWin::WindowActivated(bool active)
670{
671	if (active) {
672		BScreen screen(this);
673		BRect screenFrame = screen.Frame();
674		BRect frame = Frame();
675		float diffX = 0.0;
676		float diffY = 0.0;
677
678		// If the frame if off the edge of the screen at all
679		// we will move it so all the window is on the screen.
680		if (frame.left < screenFrame.left)
681			// Move right
682			diffX = screenFrame.left - frame.left;
683		if (frame.top < screenFrame.top)
684			// Move down
685			diffY = screenFrame.top - frame.top;
686		if (frame.right > screenFrame.right)
687			// Move left
688			diffX = screenFrame.right - frame.right;
689		if (frame.bottom > screenFrame.bottom)
690			// Move up
691			diffY = screenFrame.bottom - frame.bottom;
692
693		MoveBy(diffX, diffY);
694	}
695}
696
697
698bool
699MainWin::QuitRequested()
700{
701	be_app->PostMessage(M_PLAYER_QUIT);
702	return true;
703}
704
705
706// #pragma mark -
707
708
709void
710MainWin::OpenFile(const entry_ref &ref)
711{
712	printf("MainWin::OpenFile\n");
713
714	status_t err = fController->SetTo(ref);
715	if (err != B_OK) {
716		if (fPlaylist->CountItems() == 1) {
717			// display error if this is the only file we're supposed to play
718			BString message;
719			message << "The file '";
720			message << ref.name;
721			message << "' could not be opened.\n\n";
722
723			if (err == B_MEDIA_NO_HANDLER) {
724				// give a more detailed message for the most likely of all
725				// errors
726				message << "There is no decoder installed to handle the "
727					"file format, or the decoder has trouble with the specific "
728					"version of the format.";
729			} else {
730				message << "Error: " << strerror(err);
731			}
732			(new BAlert("error", message.String(), "OK"))->Go();
733		} else {
734			// just go to the next file and don't bother user
735			fPlaylist->SetCurrentRefIndex(fPlaylist->CurrentRefIndex() + 1);
736		}
737		fHasFile = false;
738		fHasVideo = false;
739		fHasAudio = false;
740		SetTitle(NAME);
741	} else {
742		fHasFile = true;
743		fHasVideo = fController->VideoTrackCount() != 0;
744		fHasAudio = fController->AudioTrackCount() != 0;
745		SetTitle(ref.name);
746	}
747	_SetupWindow();
748}
749
750
751void
752MainWin::ShowFileInfo()
753{
754	if (!fInfoWin)
755		fInfoWin = new InfoWin(Frame().LeftTop(), fController);
756
757	if (fInfoWin->Lock()) {
758		if (fInfoWin->IsHidden())
759			fInfoWin->Show();
760		else
761			fInfoWin->Activate();
762		fInfoWin->Unlock();
763	}
764}
765
766
767void
768MainWin::ShowPlaylistWindow()
769{
770	if (fPlaylistWindow->Lock()) {
771		if (fPlaylistWindow->IsHidden())
772			fPlaylistWindow->Show();
773		else
774			fPlaylistWindow->Activate();
775		fPlaylistWindow->Unlock();
776	}
777}
778
779
780void
781MainWin::ShowSettingsWindow()
782{
783	if (fSettingsWindow->Lock()) {
784		if (fSettingsWindow->IsHidden())
785			fSettingsWindow->Show();
786		else
787			fSettingsWindow->Activate();
788		fSettingsWindow->Unlock();
789	}
790}
791
792
793void
794MainWin::VideoFormatChange(int width, int height, float width_scale,
795	float height_scale)
796{
797	// called when video format or aspect ratio changes
798
799	printf("VideoFormatChange enter: width %d, height %d, width_scale %.6f, "
800		"height_scale %.6f\n", width, height, width_scale, height_scale);
801
802	if (width_scale < 1.0 && height_scale >= 1.0) {
803		width_scale  = 1.0 / width_scale;
804		height_scale = 1.0 / height_scale;
805		printf("inverting! new values: width_scale %.6f, height_scale %.6f\n",
806			width_scale, height_scale);
807	}
808
809 	fSourceWidth  = width;
810 	fSourceHeight = height;
811 	fWidthScale   = width_scale;
812 	fHeightScale  = height_scale;
813
814 	FrameResized(Bounds().Width(), Bounds().Height());
815
816	printf("VideoFormatChange leave\n");
817}
818
819
820// #pragma mark -
821
822
823void
824MainWin::_RefsReceived(BMessage* msg)
825{
826	// the playlist ist replaced by dropped files
827	// or the dropped files are appended to the end
828	// of the existing playlist if <shift> is pressed
829	int32 appendIndex = modifiers() & B_SHIFT_KEY ?
830		fPlaylist->CountItems() : -1;
831	msg->AddInt32("append_index", appendIndex);
832
833	// forward the message to the playlist window,
834	// so that undo/redo is used for modifying the playlist
835	fPlaylistWindow->PostMessage(msg);
836}
837
838
839void
840MainWin::_SetupWindow()
841{
842	printf("MainWin::_SetupWindow\n");
843	// Populate the track menus
844	_SetupTrackMenus();
845	// Enable both if a file was loaded
846	fAudioTrackMenu->SetEnabled(fHasFile);
847	fVideoTrackMenu->SetEnabled(fHasFile);
848
849	fVideoMenu->SetEnabled(fHasVideo);
850	fAudioMenu->SetEnabled(fHasAudio);
851//	fDebugMenu->SetEnabled(fHasVideo);
852	if (fHasVideo) {
853		fController->GetSize(&fSourceWidth, &fSourceHeight);
854		fWidthScale = 1.0;
855		fHeightScale = 1.0;
856	} else {
857		fSourceWidth = 0;
858		fSourceHeight = 0;
859		fWidthScale = 1.0;
860		fHeightScale = 1.0;
861	}
862	_UpdateControlsEnabledStatus();
863
864	// TODO: Don't if the video size did not change! Also don't
865	// exit full screen mode.
866	_ResizeWindow(100);
867
868	fVideoView->MakeFocus();
869}
870
871
872void
873MainWin::_CreateMenu()
874{
875	fFileMenu = new BMenu(NAME);
876	fPlaylistMenu = new BMenu("Playlist"B_UTF8_ELLIPSIS);
877	fAudioMenu = new BMenu("Audio");
878	fVideoMenu = new BMenu("Video");
879	fSettingsMenu = new BMenu("Settings");
880	fAudioTrackMenu = new BMenu("Track");
881	fVideoTrackMenu = new BMenu("Track");
882//	fDebugMenu = new BMenu("Debug");
883
884	fMenuBar->AddItem(fFileMenu);
885	fMenuBar->AddItem(fAudioMenu);
886	fMenuBar->AddItem(fVideoMenu);
887	fMenuBar->AddItem(fSettingsMenu);
888//	fMenuBar->AddItem(fDebugMenu);
889
890	fFileMenu->AddItem(new BMenuItem("New Player"B_UTF8_ELLIPSIS,
891		new BMessage(M_FILE_NEWPLAYER), 'N'));
892	fFileMenu->AddSeparatorItem();
893
894//	fFileMenu->AddItem(new BMenuItem("Open File"B_UTF8_ELLIPSIS,
895//		new BMessage(M_FILE_OPEN), 'O'));
896	// Add recent files
897	BRecentFilesList recentFiles(10, false, NULL, kAppSig);
898	BMenuItem *item = new BMenuItem(recentFiles.NewFileListMenu(
899		"Open File"B_UTF8_ELLIPSIS, new BMessage(B_REFS_RECEIVED),
900		NULL, this, 10, false, NULL, 0, kAppSig), new BMessage(M_FILE_OPEN));
901	item->SetShortcut('O', 0);
902	fFileMenu->AddItem(item);
903
904	fFileMenu->AddItem(new BMenuItem("File Info"B_UTF8_ELLIPSIS,
905		new BMessage(M_FILE_INFO), 'I'));
906	fFileMenu->AddItem(fPlaylistMenu);
907	fPlaylistMenu->Superitem()->SetShortcut('P', B_COMMAND_KEY);
908	fPlaylistMenu->Superitem()->SetMessage(new BMessage(M_FILE_PLAYLIST));
909
910	fFileMenu->AddSeparatorItem();
911	fFileMenu->AddItem(new BMenuItem("About " NAME B_UTF8_ELLIPSIS,
912		new BMessage(B_ABOUT_REQUESTED)));
913	fFileMenu->AddSeparatorItem();
914	fFileMenu->AddItem(new BMenuItem("Close", new BMessage(M_FILE_CLOSE), 'W'));
915	fFileMenu->AddItem(new BMenuItem("Quit", new BMessage(M_FILE_QUIT), 'Q'));
916
917	fPlaylistMenu->SetRadioMode(true);
918
919	fAudioMenu->AddItem(fAudioTrackMenu);
920
921	fVideoMenu->AddItem(fVideoTrackMenu);
922	fVideoMenu->AddSeparatorItem();
923	fVideoMenu->AddItem(new BMenuItem("50% scale",
924		new BMessage(M_VIEW_50), '0'));
925	fVideoMenu->AddItem(new BMenuItem("100% scale",
926		new BMessage(M_VIEW_100), '1'));
927	fVideoMenu->AddItem(new BMenuItem("200% scale",
928		new BMessage(M_VIEW_200), '2'));
929	fVideoMenu->AddItem(new BMenuItem("300% scale",
930		new BMessage(M_VIEW_300), '3'));
931	fVideoMenu->AddItem(new BMenuItem("400% scale",
932		new BMessage(M_VIEW_400), '4'));
933	fVideoMenu->AddSeparatorItem();
934	fVideoMenu->AddItem(new BMenuItem("Full Screen",
935		new BMessage(M_TOGGLE_FULLSCREEN), 'F'));
936	fVideoMenu->AddItem(new BMenuItem("Keep Aspect Ratio",
937		new BMessage(M_TOGGLE_KEEP_ASPECT_RATIO), 'K'));
938
939	fSettingsMenu->AddItem(new BMenuItem("No Menu",
940		new BMessage(M_TOGGLE_NO_MENU), 'M'));
941	fSettingsMenu->AddItem(new BMenuItem("No Border",
942		new BMessage(M_TOGGLE_NO_BORDER), 'B'));
943	fSettingsMenu->AddItem(new BMenuItem("No Controls",
944		new BMessage(M_TOGGLE_NO_CONTROLS), 'C'));
945	fSettingsMenu->AddItem(new BMenuItem("Always on Top",
946		new BMessage(M_TOGGLE_ALWAYS_ON_TOP), 'T'));
947//	fSettingsMenu->AddSeparatorItem();
948//	fSettingsMenu->AddItem(new BMenuItem("Settings"B_UTF8_ELLIPSIS,
949//		new BMessage(M_SETTINGS), 'S'));
950
951//	fDebugMenu->AddItem(new BMenuItem("pixel aspect ratio 1.00000:1",
952//		new BMessage(M_ASPECT_100000_1)));
953//	fDebugMenu->AddItem(new BMenuItem("pixel aspect ratio 1.06666:1",
954//		new BMessage(M_ASPECT_106666_1)));
955//	fDebugMenu->AddItem(new BMenuItem("pixel aspect ratio 1.09091:1",
956//		new BMessage(M_ASPECT_109091_1)));
957//	fDebugMenu->AddItem(new BMenuItem("pixel aspect ratio 1.41176:1",
958//		new BMessage(M_ASPECT_141176_1)));
959//	fDebugMenu->AddItem(new BMenuItem("force 720 x 576, display aspect 4:3",
960//		new BMessage(M_ASPECT_720_576)));
961//	fDebugMenu->AddItem(new BMenuItem("force 704 x 576, display aspect 4:3",
962//		new BMessage(M_ASPECT_704_576)));
963//	fDebugMenu->AddItem(new BMenuItem("force 544 x 576, display aspect 4:3",
964//		new BMessage(M_ASPECT_544_576)));
965
966	fAudioTrackMenu->SetRadioMode(true);
967	fVideoTrackMenu->SetRadioMode(true);
968}
969
970
971void
972MainWin::_SetupTrackMenus()
973{
974	fAudioTrackMenu->RemoveItems(0, fAudioTrackMenu->CountItems(), true);
975	fVideoTrackMenu->RemoveItems(0, fVideoTrackMenu->CountItems(), true);
976
977	char s[100];
978
979	int count = fController->AudioTrackCount();
980	int current = fController->CurrentAudioTrack();
981	for (int i = 0; i < count; i++) {
982		sprintf(s, "Track %d", i + 1);
983		BMenuItem* item = new BMenuItem(s,
984			new BMessage(M_SELECT_AUDIO_TRACK + i));
985		item->SetMarked(i == current);
986		fAudioTrackMenu->AddItem(item);
987	}
988	if (!count) {
989		fAudioTrackMenu->AddItem(new BMenuItem("none", new BMessage(M_DUMMY)));
990		fAudioTrackMenu->ItemAt(0)->SetMarked(true);
991	}
992
993
994	count = fController->VideoTrackCount();
995	current = fController->CurrentVideoTrack();
996	for (int i = 0; i < count; i++) {
997		sprintf(s, "Track %d", i + 1);
998		BMenuItem* item = new BMenuItem(s,
999			new BMessage(M_SELECT_VIDEO_TRACK + i));
1000		item->SetMarked(i == current);
1001		fVideoTrackMenu->AddItem(item);
1002	}
1003	if (!count) {
1004		fVideoTrackMenu->AddItem(new BMenuItem("none", new BMessage(M_DUMMY)));
1005		fVideoTrackMenu->ItemAt(0)->SetMarked(true);
1006	}
1007}
1008
1009
1010void
1011MainWin::_SetWindowSizeLimits()
1012{
1013	int minWidth = fNoControls  ? MIN_WIDTH : fControlsWidth;
1014	if (!fNoMenu)
1015		minWidth = max_c(minWidth, fMenuBarWidth);
1016	int minHeight = (fNoMenu ? 0 : fMenuBarHeight)
1017		+ (fNoControls ? 0 : fControlsHeight);
1018
1019	SetSizeLimits(minWidth - 1, 32000, minHeight - 1, fHasVideo ?
1020		32000 : minHeight - 1);
1021}
1022
1023
1024void
1025MainWin::_ResizeWindow(int percent)
1026{
1027	int video_width;
1028	int video_height;
1029
1030	// Get required window size
1031	video_width = lround(fSourceWidth * fWidthScale);
1032	video_height = lround(fSourceHeight * fHeightScale);
1033
1034	video_width = (video_width * percent) / 100;
1035	video_height = (video_height * percent) / 100;
1036
1037	// Calculate and set the initial window size
1038	int width = max_c(fControlsWidth, video_width);
1039	int height = (fNoControls ? 0 : fControlsHeight) + video_height;
1040	if (!fNoMenu) {
1041		width = max_c(width, fMenuBarWidth);
1042		height += fMenuBarHeight;
1043	}
1044	_SetWindowSizeLimits();
1045	ResizeTo(width - 1, height - 1);
1046}
1047
1048
1049void
1050MainWin::_ResizeVideoView(int x, int y, int width, int height)
1051{
1052	printf("_ResizeVideoView: %d,%d, width %d, height %d\n", x, y,
1053		width, height);
1054
1055	if (fKeepAspectRatio) {
1056		// Keep aspect ratio, place video view inside
1057		// the background area (may create black bars).
1058		float scaled_width  = fSourceWidth * fWidthScale;
1059		float scaled_height = fSourceHeight * fHeightScale;
1060		float factor = min_c(width / scaled_width, height / scaled_height);
1061		int render_width = lround(scaled_width * factor);
1062		int render_height = lround(scaled_height * factor);
1063		if (render_width > width)
1064			render_width = width;
1065		if (render_height > height)
1066			render_height = height;
1067
1068		int x_ofs = x + (width - render_width) / 2;
1069		int y_ofs = y + (height - render_height) / 2;
1070
1071		fVideoView->MoveTo(x_ofs, y_ofs);
1072		fVideoView->ResizeTo(render_width - 1, render_height - 1);
1073
1074	} else {
1075		fVideoView->MoveTo(x, y);
1076		fVideoView->ResizeTo(width - 1, height - 1);
1077	}
1078}
1079
1080
1081// #pragma mark -
1082
1083
1084void
1085MainWin::_MouseDown(BMessage *msg, BView* originalHandler)
1086{
1087	BPoint screen_where;
1088	uint32 buttons = msg->FindInt32("buttons");
1089
1090	// On Zeta, only "screen_where" is relyable, "where" and "be:view_where"
1091	// seem to be broken
1092	if (B_OK != msg->FindPoint("screen_where", &screen_where)) {
1093		// Workaround for BeOS R5, it has no "screen_where"
1094		if (!originalHandler || msg->FindPoint("where", &screen_where) < B_OK)
1095			return;
1096		originalHandler->ConvertToScreen(&screen_where);
1097	}
1098
1099//	msg->PrintToStream();
1100
1101//	if (1 == msg->FindInt32("buttons") && msg->FindInt32("clicks") == 1) {
1102
1103	if (1 == buttons && msg->FindInt32("clicks") % 2 == 0) {
1104		BRect r(screen_where.x - 1, screen_where.y - 1, screen_where.x + 1,
1105			screen_where.y + 1);
1106		if (r.Contains(fMouseDownMousePos)) {
1107			PostMessage(M_TOGGLE_FULLSCREEN);
1108			return;
1109		}
1110	}
1111
1112	if (2 == buttons && msg->FindInt32("clicks") % 2 == 0) {
1113		BRect r(screen_where.x - 1, screen_where.y - 1, screen_where.x + 1,
1114			screen_where.y + 1);
1115		if (r.Contains(fMouseDownMousePos)) {
1116			PostMessage(M_TOGGLE_NO_BORDER_NO_MENU_NO_CONTROLS);
1117			return;
1118		}
1119	}
1120
1121/*
1122		// very broken in Zeta:
1123		fMouseDownMousePos = fVideoView->ConvertToScreen(
1124			msg->FindPoint("where"));
1125*/
1126	fMouseDownMousePos = screen_where;
1127	fMouseDownWindowPos = Frame().LeftTop();
1128
1129	if (buttons == 1 && !fIsFullscreen) {
1130		// start mouse tracking
1131		fVideoView->SetMouseEventMask(B_POINTER_EVENTS | B_NO_POINTER_HISTORY
1132			/* | B_LOCK_WINDOW_FOCUS */);
1133		fMouseDownTracking = true;
1134	}
1135
1136	// pop up a context menu if right mouse button is down for 200 ms
1137
1138	if ((buttons & 2) == 0)
1139		return;
1140	bigtime_t start = system_time();
1141	bigtime_t delay = 200000;
1142	BPoint location;
1143	do {
1144		fVideoView->GetMouse(&location, &buttons);
1145		if ((buttons & 2) == 0)
1146			break;
1147		snooze(1000);
1148	} while (system_time() - start < delay);
1149
1150	if (buttons & 2)
1151		_ShowContextMenu(screen_where);
1152}
1153
1154
1155void
1156MainWin::_MouseMoved(BMessage *msg, BView* originalHandler)
1157{
1158//	msg->PrintToStream();
1159
1160	BPoint mousePos;
1161	uint32 buttons = msg->FindInt32("buttons");
1162
1163	if (1 == buttons && fMouseDownTracking && !fIsFullscreen) {
1164/*
1165		// very broken in Zeta:
1166		BPoint mousePos = msg->FindPoint("where");
1167		printf("view where: %.0f, %.0f => ", mousePos.x, mousePos.y);
1168		fVideoView->ConvertToScreen(&mousePos);
1169*/
1170		// On Zeta, only "screen_where" is relyable, "where"
1171		// and "be:view_where" seem to be broken
1172		if (B_OK != msg->FindPoint("screen_where", &mousePos)) {
1173			// Workaround for BeOS R5, it has no "screen_where"
1174			if (!originalHandler || msg->FindPoint("where", &mousePos) < B_OK)
1175				return;
1176			originalHandler->ConvertToScreen(&mousePos);
1177		}
1178//		printf("screen where: %.0f, %.0f => ", mousePos.x, mousePos.y);
1179		float delta_x = mousePos.x - fMouseDownMousePos.x;
1180		float delta_y = mousePos.y - fMouseDownMousePos.y;
1181		float x = fMouseDownWindowPos.x + delta_x;
1182		float y = fMouseDownWindowPos.y + delta_y;
1183//		printf("move window to %.0f, %.0f\n", x, y);
1184		MoveTo(x, y);
1185	}
1186}
1187
1188
1189void
1190MainWin::_MouseUp(BMessage *msg)
1191{
1192//	msg->PrintToStream();
1193	fMouseDownTracking = false;
1194}
1195
1196
1197void
1198MainWin::_ShowContextMenu(const BPoint &screen_point)
1199{
1200	printf("Show context menu\n");
1201	BPopUpMenu *menu = new BPopUpMenu("context menu", false, false);
1202	BMenuItem *item;
1203	menu->AddItem(item = new BMenuItem("Full Screen",
1204		new BMessage(M_TOGGLE_FULLSCREEN), 'F'));
1205	item->SetMarked(fIsFullscreen);
1206	item->SetEnabled(fHasVideo);
1207	menu->AddItem(item = new BMenuItem("Keep Aspect Ratio",
1208		new BMessage(M_TOGGLE_KEEP_ASPECT_RATIO), 'K'));
1209	item->SetMarked(fKeepAspectRatio);
1210	item->SetEnabled(fHasVideo);
1211
1212	menu->AddSeparatorItem();
1213	menu->AddItem(item = new BMenuItem("No Menu",
1214		new BMessage(M_TOGGLE_NO_MENU), 'M'));
1215	item->SetMarked(fNoMenu);
1216	menu->AddItem(item = new BMenuItem("No Border",
1217		new BMessage(M_TOGGLE_NO_BORDER), 'B'));
1218	item->SetMarked(fNoBorder);
1219	menu->AddItem(item = new BMenuItem("Always on Top",
1220		new BMessage(M_TOGGLE_ALWAYS_ON_TOP), 'T'));
1221	item->SetMarked(fAlwaysOnTop);
1222
1223	menu->AddSeparatorItem();
1224	menu->AddItem(new BMenuItem("About " NAME B_UTF8_ELLIPSIS,
1225		new BMessage(B_ABOUT_REQUESTED)));
1226	menu->AddSeparatorItem();
1227	menu->AddItem(new BMenuItem("Quit", new BMessage(M_FILE_QUIT), 'Q'));
1228
1229	menu->AddSeparatorItem();
1230	menu->AddItem(new BMenuItem("pixel aspect ratio 1.00000:1",
1231		new BMessage(M_ASPECT_100000_1)));
1232	menu->AddItem(new BMenuItem("pixel aspect ratio 1.06666:1",
1233		new BMessage(M_ASPECT_106666_1)));
1234	menu->AddItem(new BMenuItem("pixel aspect ratio 1.09091:1",
1235		new BMessage(M_ASPECT_109091_1)));
1236	menu->AddItem(new BMenuItem("pixel aspect ratio 1.41176:1",
1237		new BMessage(M_ASPECT_141176_1)));
1238	menu->AddItem(new BMenuItem("force 720 x 576, display aspect 4:3",
1239		new BMessage(M_ASPECT_720_576)));
1240	menu->AddItem(new BMenuItem("force 704 x 576, display aspect 4:3",
1241		new BMessage(M_ASPECT_704_576)));
1242	menu->AddItem(new BMenuItem("force 544 x 576, display aspect 4:3",
1243		new BMessage(M_ASPECT_544_576)));
1244
1245	menu->SetTargetForItems(this);
1246	BRect r(screen_point.x - 5, screen_point.y - 5, screen_point.x + 5,
1247		screen_point.y + 5);
1248	menu->Go(screen_point, true, true, r, true);
1249}
1250
1251
1252/* Trap keys that are about to be send to background or renderer view.
1253 * Return B_OK if it shouldn't be passed to the view
1254 */
1255status_t
1256MainWin::_KeyDown(BMessage *msg)
1257{
1258//	msg->PrintToStream();
1259
1260	uint32 key		 = msg->FindInt32("key");
1261	uint32 raw_char  = msg->FindInt32("raw_char");
1262	uint32 modifiers = msg->FindInt32("modifiers");
1263
1264	printf("key 0x%lx, raw_char 0x%lx, modifiers 0x%lx\n", key, raw_char,
1265		modifiers);
1266
1267	switch (raw_char) {
1268		case B_SPACE:
1269			fController->TogglePlaying();
1270			return B_OK;
1271
1272		case B_ESCAPE:
1273			if (fIsFullscreen) {
1274				PostMessage(M_TOGGLE_FULLSCREEN);
1275				return B_OK;
1276			} else
1277				break;
1278
1279		case B_ENTER:		// Enter / Return
1280			if (modifiers & B_COMMAND_KEY) {
1281				PostMessage(M_TOGGLE_FULLSCREEN);
1282				return B_OK;
1283			} else
1284				break;
1285
1286		case B_TAB:
1287			if ((modifiers & (B_COMMAND_KEY | B_CONTROL_KEY | B_OPTION_KEY
1288					| B_MENU_KEY)) == 0) {
1289				PostMessage(M_TOGGLE_FULLSCREEN);
1290				return B_OK;
1291			} else
1292				break;
1293
1294		case B_UP_ARROW:
1295			if (modifiers & B_COMMAND_KEY) {
1296				PostMessage(M_SKIP_NEXT);
1297			} else {
1298				PostMessage(M_VOLUME_UP);
1299			}
1300			return B_OK;
1301
1302		case B_DOWN_ARROW:
1303			if (modifiers & B_COMMAND_KEY) {
1304				PostMessage(M_SKIP_PREV);
1305			} else {
1306				PostMessage(M_VOLUME_DOWN);
1307			}
1308			return B_OK;
1309
1310		case B_RIGHT_ARROW:
1311			if (modifiers & B_COMMAND_KEY) {
1312				PostMessage(M_VOLUME_UP);
1313			} else {
1314				PostMessage(M_SKIP_NEXT);
1315			}
1316			return B_OK;
1317
1318		case B_LEFT_ARROW:
1319			if (modifiers & B_COMMAND_KEY) {
1320				PostMessage(M_VOLUME_DOWN);
1321			} else {
1322				PostMessage(M_SKIP_PREV);
1323			}
1324			return B_OK;
1325
1326		case B_PAGE_UP:
1327			PostMessage(M_SKIP_NEXT);
1328			return B_OK;
1329
1330		case B_PAGE_DOWN:
1331			PostMessage(M_SKIP_PREV);
1332			return B_OK;
1333	}
1334
1335	switch (key) {
1336		case 0x3a:  		// numeric keypad +
1337			if ((modifiers & B_COMMAND_KEY) == 0) {
1338				printf("if\n");
1339				PostMessage(M_VOLUME_UP);
1340				return B_OK;
1341			} else {
1342				printf("else\n");
1343				break;
1344			}
1345
1346		case 0x25:  		// numeric keypad -
1347			if ((modifiers & B_COMMAND_KEY) == 0) {
1348				PostMessage(M_VOLUME_DOWN);
1349				return B_OK;
1350			} else {
1351				break;
1352			}
1353
1354		case 0x38:			// numeric keypad up arrow
1355			PostMessage(M_VOLUME_UP);
1356			return B_OK;
1357
1358		case 0x59:			// numeric keypad down arrow
1359			PostMessage(M_VOLUME_DOWN);
1360			return B_OK;
1361
1362		case 0x39:			// numeric keypad page up
1363		case 0x4a:			// numeric keypad right arrow
1364			PostMessage(M_SKIP_NEXT);
1365			return B_OK;
1366
1367		case 0x5a:			// numeric keypad page down
1368		case 0x48:			// numeric keypad left arrow
1369			PostMessage(M_SKIP_PREV);
1370			return B_OK;
1371	}
1372
1373	return B_ERROR;
1374}
1375
1376
1377// #pragma mark -
1378
1379
1380void
1381MainWin::_ToggleNoBorderNoMenu()
1382{
1383	if (!fNoMenu && !fNoBorder && !fNoControls) {
1384		PostMessage(M_TOGGLE_NO_MENU);
1385		PostMessage(M_TOGGLE_NO_BORDER);
1386		PostMessage(M_TOGGLE_NO_CONTROLS);
1387	} else {
1388		if (!fNoMenu)
1389			PostMessage(M_TOGGLE_NO_MENU);
1390		if (!fNoBorder)
1391			PostMessage(M_TOGGLE_NO_BORDER);
1392		if (!fNoControls)
1393			PostMessage(M_TOGGLE_NO_CONTROLS);
1394	}
1395}
1396
1397
1398void
1399MainWin::_ToggleFullscreen()
1400{
1401	printf("_ToggleFullscreen enter\n");
1402
1403	if (!fHasVideo) {
1404		printf("_ToggleFullscreen - ignoring, as we don't have a video\n");
1405		return;
1406	}
1407
1408	fIsFullscreen = !fIsFullscreen;
1409
1410	if (fIsFullscreen) {
1411		// switch to fullscreen
1412
1413		fSavedFrame = Frame();
1414		printf("saving current frame: %d %d %d %d\n", int(fSavedFrame.left),
1415			int(fSavedFrame.top), int(fSavedFrame.right),
1416			int(fSavedFrame.bottom));
1417		BScreen screen(this);
1418		BRect rect(screen.Frame());
1419
1420		Hide();
1421		MoveTo(rect.left, rect.top);
1422		ResizeTo(rect.Width(), rect.Height());
1423		Show();
1424
1425	} else {
1426		// switch back from full screen mode
1427
1428		Hide();
1429		MoveTo(fSavedFrame.left, fSavedFrame.top);
1430		ResizeTo(fSavedFrame.Width(), fSavedFrame.Height());
1431		Show();
1432	}
1433
1434	_MarkSettingsItem(M_TOGGLE_FULLSCREEN, fIsFullscreen);
1435
1436	printf("_ToggleFullscreen leave\n");
1437}
1438
1439void
1440MainWin::_ToggleNoControls()
1441{
1442	printf("_ToggleNoControls enter\n");
1443
1444	if (fIsFullscreen) {
1445		// fullscreen is always without menu
1446		printf("_ToggleNoControls leave, doing nothing, we are fullscreen\n");
1447		return;
1448	}
1449
1450	fNoControls = !fNoControls;
1451	_SetWindowSizeLimits();
1452
1453	if (fNoControls) {
1454		ResizeBy(0, - fControlsHeight);
1455	} else {
1456		ResizeBy(0, fControlsHeight);
1457	}
1458
1459	_MarkSettingsItem(M_TOGGLE_NO_CONTROLS, fNoControls);
1460
1461	printf("_ToggleNoControls leave\n");
1462}
1463
1464void
1465MainWin::_ToggleNoMenu()
1466{
1467	printf("_ToggleNoMenu enter\n");
1468
1469	if (fIsFullscreen) {
1470		// fullscreen is always without menu
1471		printf("_ToggleNoMenu leave, doing nothing, we are fullscreen\n");
1472		return;
1473	}
1474
1475	fNoMenu = !fNoMenu;
1476	_SetWindowSizeLimits();
1477
1478	if (fNoMenu) {
1479		MoveBy(0, fMenuBarHeight);
1480		ResizeBy(0, - fMenuBarHeight);
1481	} else {
1482		MoveBy(0, - fMenuBarHeight);
1483		ResizeBy(0, fMenuBarHeight);
1484	}
1485
1486	_MarkSettingsItem(M_TOGGLE_NO_MENU, fNoMenu);
1487
1488	printf("_ToggleNoMenu leave\n");
1489}
1490
1491
1492void
1493MainWin::_ToggleNoBorder()
1494{
1495	fNoBorder = !fNoBorder;
1496	SetLook(fNoBorder ? B_BORDERED_WINDOW_LOOK : B_TITLED_WINDOW_LOOK);
1497
1498	_MarkSettingsItem(M_TOGGLE_NO_BORDER, fNoBorder);
1499}
1500
1501
1502void
1503MainWin::_ToggleAlwaysOnTop()
1504{
1505	fAlwaysOnTop = !fAlwaysOnTop;
1506	SetFeel(fAlwaysOnTop ? B_FLOATING_ALL_WINDOW_FEEL : B_NORMAL_WINDOW_FEEL);
1507
1508	_MarkSettingsItem(M_TOGGLE_ALWAYS_ON_TOP, fAlwaysOnTop);
1509}
1510
1511
1512void
1513MainWin::_ToggleKeepAspectRatio()
1514{
1515	fKeepAspectRatio = !fKeepAspectRatio;
1516	FrameResized(Bounds().Width(), Bounds().Height());
1517
1518	_MarkSettingsItem(M_TOGGLE_KEEP_ASPECT_RATIO, fKeepAspectRatio);
1519}
1520
1521
1522// #pragma mark -
1523
1524
1525void
1526MainWin::_UpdateControlsEnabledStatus()
1527{
1528	uint32 enabledButtons = 0;
1529	if (fHasVideo || fHasAudio) {
1530		enabledButtons |= PLAYBACK_ENABLED | SEEK_ENABLED
1531			| SEEK_BACK_ENABLED | SEEK_FORWARD_ENABLED;
1532	}
1533	if (fHasAudio)
1534		enabledButtons |= VOLUME_ENABLED;
1535
1536	bool canSkipPrevious, canSkipNext;
1537	fPlaylist->GetSkipInfo(&canSkipPrevious, &canSkipNext);
1538	if (canSkipPrevious)
1539		enabledButtons |= SKIP_BACK_ENABLED;
1540	if (canSkipNext)
1541		enabledButtons |= SKIP_FORWARD_ENABLED;
1542
1543	fControls->SetEnabled(enabledButtons);
1544}
1545
1546
1547void
1548MainWin::_UpdatePlaylistMenu()
1549{
1550	if (!fPlaylist->Lock())
1551		return;
1552
1553	fPlaylistMenu->RemoveItems(0, fPlaylistMenu->CountItems(), true);
1554
1555	int32 count = fPlaylist->CountItems();
1556	for (int32 i = 0; i < count; i++) {
1557		entry_ref ref;
1558		if (fPlaylist->GetRefAt(i, &ref) < B_OK)
1559			continue;
1560		_AddPlaylistItem(ref, i);
1561	}
1562	fPlaylistMenu->SetTargetForItems(this);
1563
1564	_MarkPlaylistItem(fPlaylist->CurrentRefIndex());
1565
1566	fPlaylist->Unlock();
1567}
1568
1569
1570void
1571MainWin::_AddPlaylistItem(const entry_ref& ref, int32 index)
1572{
1573	BMessage* message = new BMessage(M_SET_PLAYLIST_POSITION);
1574	message->AddInt32("index", index);
1575	BMenuItem* item = new BMenuItem(ref.name, message);
1576	fPlaylistMenu->AddItem(item, index);
1577}
1578
1579
1580void
1581MainWin::_RemovePlaylistItem(int32 index)
1582{
1583	delete fPlaylistMenu->RemoveItem(index);
1584}
1585
1586
1587void
1588MainWin::_MarkPlaylistItem(int32 index)
1589{
1590	if (BMenuItem* item = fPlaylistMenu->ItemAt(index)) {
1591		item->SetMarked(true);
1592		// ... and in case the menu is currently on screen:
1593		if (fPlaylistMenu->LockLooper()) {
1594			fPlaylistMenu->Invalidate();
1595			fPlaylistMenu->UnlockLooper();
1596		}
1597	}
1598}
1599
1600
1601void
1602MainWin::_MarkSettingsItem(uint32 command, bool mark)
1603{
1604	if (BMenuItem* item = fSettingsMenu->FindItem(command))
1605		item->SetMarked(mark);
1606}
1607
1608
1609