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