xref: /haiku/src/apps/mediaplayer/MainWin.cpp (revision c9060eb991e10e477ece52478d6743fc7691c143)
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 
59 enum {
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 
102 MainWin::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(-1)
126  ,	fSourceHeight(-1)
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 
203 MainWin::~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 
244 void
245 MainWin::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 
304 void
305 MainWin::Zoom(BPoint rec_position, float rec_width, float rec_height)
306 {
307 	PostMessage(M_TOGGLE_FULLSCREEN);
308 }
309 
310 
311 void
312 MainWin::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 
350 void
351 MainWin::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 
668 void
669 MainWin::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 
698 bool
699 MainWin::QuitRequested()
700 {
701 	be_app->PostMessage(M_PLAYER_QUIT);
702 	return true;
703 }
704 
705 
706 // #pragma mark -
707 
708 
709 void
710 MainWin::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 
751 void
752 MainWin::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 
767 void
768 MainWin::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 
780 void
781 MainWin::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 
793 void
794 MainWin::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 
823 void
824 MainWin::_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 
839 void
840 MainWin::_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 	int previousSourceWidth = fSourceWidth;
853 	int previousSourceHeight = fSourceHeight;
854 	if (fHasVideo) {
855 		fController->GetSize(&fSourceWidth, &fSourceHeight);
856 		fWidthScale = 1.0;
857 		fHeightScale = 1.0;
858 			// TODO: implement aspect ratio
859 	} else {
860 		fSourceWidth = 0;
861 		fSourceHeight = 0;
862 		fWidthScale = 1.0;
863 		fHeightScale = 1.0;
864 	}
865 	_UpdateControlsEnabledStatus();
866 
867 	// adopt the size and window layout if necessary
868 	if (!fIsFullscreen && (previousSourceWidth != fSourceWidth
869 			|| previousSourceHeight != fSourceHeight)) {
870 		_ResizeWindow(100);
871 	}
872 
873 	fVideoView->MakeFocus();
874 }
875 
876 
877 void
878 MainWin::_CreateMenu()
879 {
880 	fFileMenu = new BMenu(NAME);
881 	fPlaylistMenu = new BMenu("Playlist"B_UTF8_ELLIPSIS);
882 	fAudioMenu = new BMenu("Audio");
883 	fVideoMenu = new BMenu("Video");
884 	fSettingsMenu = new BMenu("Settings");
885 	fAudioTrackMenu = new BMenu("Track");
886 	fVideoTrackMenu = new BMenu("Track");
887 //	fDebugMenu = new BMenu("Debug");
888 
889 	fMenuBar->AddItem(fFileMenu);
890 	fMenuBar->AddItem(fAudioMenu);
891 	fMenuBar->AddItem(fVideoMenu);
892 	fMenuBar->AddItem(fSettingsMenu);
893 //	fMenuBar->AddItem(fDebugMenu);
894 
895 	fFileMenu->AddItem(new BMenuItem("New Player"B_UTF8_ELLIPSIS,
896 		new BMessage(M_FILE_NEWPLAYER), 'N'));
897 	fFileMenu->AddSeparatorItem();
898 
899 //	fFileMenu->AddItem(new BMenuItem("Open File"B_UTF8_ELLIPSIS,
900 //		new BMessage(M_FILE_OPEN), 'O'));
901 	// Add recent files
902 	BRecentFilesList recentFiles(10, false, NULL, kAppSig);
903 	BMenuItem *item = new BMenuItem(recentFiles.NewFileListMenu(
904 		"Open File"B_UTF8_ELLIPSIS, new BMessage(B_REFS_RECEIVED),
905 		NULL, this, 10, false, NULL, 0, kAppSig), new BMessage(M_FILE_OPEN));
906 	item->SetShortcut('O', 0);
907 	fFileMenu->AddItem(item);
908 
909 	fFileMenu->AddItem(new BMenuItem("File Info"B_UTF8_ELLIPSIS,
910 		new BMessage(M_FILE_INFO), 'I'));
911 	fFileMenu->AddItem(fPlaylistMenu);
912 	fPlaylistMenu->Superitem()->SetShortcut('P', B_COMMAND_KEY);
913 	fPlaylistMenu->Superitem()->SetMessage(new BMessage(M_FILE_PLAYLIST));
914 
915 	fFileMenu->AddSeparatorItem();
916 	fFileMenu->AddItem(new BMenuItem("About " NAME B_UTF8_ELLIPSIS,
917 		new BMessage(B_ABOUT_REQUESTED)));
918 	fFileMenu->AddSeparatorItem();
919 	fFileMenu->AddItem(new BMenuItem("Close", new BMessage(M_FILE_CLOSE), 'W'));
920 	fFileMenu->AddItem(new BMenuItem("Quit", new BMessage(M_FILE_QUIT), 'Q'));
921 
922 	fPlaylistMenu->SetRadioMode(true);
923 
924 	fAudioMenu->AddItem(fAudioTrackMenu);
925 
926 	fVideoMenu->AddItem(fVideoTrackMenu);
927 	fVideoMenu->AddSeparatorItem();
928 	fVideoMenu->AddItem(new BMenuItem("50% scale",
929 		new BMessage(M_VIEW_50), '0'));
930 	fVideoMenu->AddItem(new BMenuItem("100% scale",
931 		new BMessage(M_VIEW_100), '1'));
932 	fVideoMenu->AddItem(new BMenuItem("200% scale",
933 		new BMessage(M_VIEW_200), '2'));
934 	fVideoMenu->AddItem(new BMenuItem("300% scale",
935 		new BMessage(M_VIEW_300), '3'));
936 	fVideoMenu->AddItem(new BMenuItem("400% scale",
937 		new BMessage(M_VIEW_400), '4'));
938 	fVideoMenu->AddSeparatorItem();
939 	fVideoMenu->AddItem(new BMenuItem("Full Screen",
940 		new BMessage(M_TOGGLE_FULLSCREEN), 'F'));
941 	fVideoMenu->AddItem(new BMenuItem("Keep Aspect Ratio",
942 		new BMessage(M_TOGGLE_KEEP_ASPECT_RATIO), 'K'));
943 
944 	fSettingsMenu->AddItem(new BMenuItem("No Menu",
945 		new BMessage(M_TOGGLE_NO_MENU), 'M'));
946 	fSettingsMenu->AddItem(new BMenuItem("No Border",
947 		new BMessage(M_TOGGLE_NO_BORDER), 'B'));
948 	fSettingsMenu->AddItem(new BMenuItem("No Controls",
949 		new BMessage(M_TOGGLE_NO_CONTROLS), 'C'));
950 	fSettingsMenu->AddItem(new BMenuItem("Always on Top",
951 		new BMessage(M_TOGGLE_ALWAYS_ON_TOP), 'T'));
952 //	fSettingsMenu->AddSeparatorItem();
953 //	fSettingsMenu->AddItem(new BMenuItem("Settings"B_UTF8_ELLIPSIS,
954 //		new BMessage(M_SETTINGS), 'S'));
955 
956 //	fDebugMenu->AddItem(new BMenuItem("pixel aspect ratio 1.00000:1",
957 //		new BMessage(M_ASPECT_100000_1)));
958 //	fDebugMenu->AddItem(new BMenuItem("pixel aspect ratio 1.06666:1",
959 //		new BMessage(M_ASPECT_106666_1)));
960 //	fDebugMenu->AddItem(new BMenuItem("pixel aspect ratio 1.09091:1",
961 //		new BMessage(M_ASPECT_109091_1)));
962 //	fDebugMenu->AddItem(new BMenuItem("pixel aspect ratio 1.41176:1",
963 //		new BMessage(M_ASPECT_141176_1)));
964 //	fDebugMenu->AddItem(new BMenuItem("force 720 x 576, display aspect 4:3",
965 //		new BMessage(M_ASPECT_720_576)));
966 //	fDebugMenu->AddItem(new BMenuItem("force 704 x 576, display aspect 4:3",
967 //		new BMessage(M_ASPECT_704_576)));
968 //	fDebugMenu->AddItem(new BMenuItem("force 544 x 576, display aspect 4:3",
969 //		new BMessage(M_ASPECT_544_576)));
970 
971 	fAudioTrackMenu->SetRadioMode(true);
972 	fVideoTrackMenu->SetRadioMode(true);
973 }
974 
975 
976 void
977 MainWin::_SetupTrackMenus()
978 {
979 	fAudioTrackMenu->RemoveItems(0, fAudioTrackMenu->CountItems(), true);
980 	fVideoTrackMenu->RemoveItems(0, fVideoTrackMenu->CountItems(), true);
981 
982 	char s[100];
983 
984 	int count = fController->AudioTrackCount();
985 	int current = fController->CurrentAudioTrack();
986 	for (int i = 0; i < count; i++) {
987 		sprintf(s, "Track %d", i + 1);
988 		BMenuItem* item = new BMenuItem(s,
989 			new BMessage(M_SELECT_AUDIO_TRACK + i));
990 		item->SetMarked(i == current);
991 		fAudioTrackMenu->AddItem(item);
992 	}
993 	if (!count) {
994 		fAudioTrackMenu->AddItem(new BMenuItem("none", new BMessage(M_DUMMY)));
995 		fAudioTrackMenu->ItemAt(0)->SetMarked(true);
996 	}
997 
998 
999 	count = fController->VideoTrackCount();
1000 	current = fController->CurrentVideoTrack();
1001 	for (int i = 0; i < count; i++) {
1002 		sprintf(s, "Track %d", i + 1);
1003 		BMenuItem* item = new BMenuItem(s,
1004 			new BMessage(M_SELECT_VIDEO_TRACK + i));
1005 		item->SetMarked(i == current);
1006 		fVideoTrackMenu->AddItem(item);
1007 	}
1008 	if (!count) {
1009 		fVideoTrackMenu->AddItem(new BMenuItem("none", new BMessage(M_DUMMY)));
1010 		fVideoTrackMenu->ItemAt(0)->SetMarked(true);
1011 	}
1012 }
1013 
1014 
1015 void
1016 MainWin::_SetWindowSizeLimits()
1017 {
1018 	int minWidth = fNoControls  ? MIN_WIDTH : fControlsWidth;
1019 	if (!fNoMenu)
1020 		minWidth = max_c(minWidth, fMenuBarWidth);
1021 	int minHeight = (fNoMenu ? 0 : fMenuBarHeight)
1022 		+ (fNoControls ? 0 : fControlsHeight);
1023 
1024 	SetSizeLimits(minWidth - 1, 32000, minHeight - 1, fHasVideo ?
1025 		32000 : minHeight - 1);
1026 }
1027 
1028 
1029 void
1030 MainWin::_ResizeWindow(int percent)
1031 {
1032 	// Get required window size
1033 	int video_width = lround(fSourceWidth * fWidthScale);
1034 	int video_height = lround(fSourceHeight * fHeightScale);
1035 
1036 	video_width = (video_width * percent) / 100;
1037 	video_height = (video_height * percent) / 100;
1038 
1039 	// Calculate and set the initial window size
1040 	int width = max_c(fControlsWidth, video_width);
1041 	int height = (fNoControls ? 0 : fControlsHeight) + video_height;
1042 	if (!fNoMenu) {
1043 		width = max_c(width, fMenuBarWidth);
1044 		height += fMenuBarHeight;
1045 	}
1046 	_SetWindowSizeLimits();
1047 	ResizeTo(width - 1, height - 1);
1048 }
1049 
1050 
1051 void
1052 MainWin::_ResizeVideoView(int x, int y, int width, int height)
1053 {
1054 	printf("_ResizeVideoView: %d,%d, width %d, height %d\n", x, y,
1055 		width, height);
1056 
1057 	if (fKeepAspectRatio) {
1058 		// Keep aspect ratio, place video view inside
1059 		// the background area (may create black bars).
1060 		float scaled_width  = fSourceWidth * fWidthScale;
1061 		float scaled_height = fSourceHeight * fHeightScale;
1062 		float factor = min_c(width / scaled_width, height / scaled_height);
1063 		int render_width = lround(scaled_width * factor);
1064 		int render_height = lround(scaled_height * factor);
1065 		if (render_width > width)
1066 			render_width = width;
1067 		if (render_height > height)
1068 			render_height = height;
1069 
1070 		int x_ofs = x + (width - render_width) / 2;
1071 		int y_ofs = y + (height - render_height) / 2;
1072 
1073 		fVideoView->MoveTo(x_ofs, y_ofs);
1074 		fVideoView->ResizeTo(render_width - 1, render_height - 1);
1075 
1076 	} else {
1077 		fVideoView->MoveTo(x, y);
1078 		fVideoView->ResizeTo(width - 1, height - 1);
1079 	}
1080 }
1081 
1082 
1083 // #pragma mark -
1084 
1085 
1086 void
1087 MainWin::_MouseDown(BMessage *msg, BView* originalHandler)
1088 {
1089 	BPoint screen_where;
1090 	uint32 buttons = msg->FindInt32("buttons");
1091 
1092 	// On Zeta, only "screen_where" is relyable, "where" and "be:view_where"
1093 	// seem to be broken
1094 	if (B_OK != msg->FindPoint("screen_where", &screen_where)) {
1095 		// Workaround for BeOS R5, it has no "screen_where"
1096 		if (!originalHandler || msg->FindPoint("where", &screen_where) < B_OK)
1097 			return;
1098 		originalHandler->ConvertToScreen(&screen_where);
1099 	}
1100 
1101 //	msg->PrintToStream();
1102 
1103 //	if (1 == msg->FindInt32("buttons") && msg->FindInt32("clicks") == 1) {
1104 
1105 	if (1 == buttons && msg->FindInt32("clicks") % 2 == 0) {
1106 		BRect r(screen_where.x - 1, screen_where.y - 1, screen_where.x + 1,
1107 			screen_where.y + 1);
1108 		if (r.Contains(fMouseDownMousePos)) {
1109 			PostMessage(M_TOGGLE_FULLSCREEN);
1110 			return;
1111 		}
1112 	}
1113 
1114 	if (2 == buttons && msg->FindInt32("clicks") % 2 == 0) {
1115 		BRect r(screen_where.x - 1, screen_where.y - 1, screen_where.x + 1,
1116 			screen_where.y + 1);
1117 		if (r.Contains(fMouseDownMousePos)) {
1118 			PostMessage(M_TOGGLE_NO_BORDER_NO_MENU_NO_CONTROLS);
1119 			return;
1120 		}
1121 	}
1122 
1123 /*
1124 		// very broken in Zeta:
1125 		fMouseDownMousePos = fVideoView->ConvertToScreen(
1126 			msg->FindPoint("where"));
1127 */
1128 	fMouseDownMousePos = screen_where;
1129 	fMouseDownWindowPos = Frame().LeftTop();
1130 
1131 	if (buttons == 1 && !fIsFullscreen) {
1132 		// start mouse tracking
1133 		fVideoView->SetMouseEventMask(B_POINTER_EVENTS | B_NO_POINTER_HISTORY
1134 			/* | B_LOCK_WINDOW_FOCUS */);
1135 		fMouseDownTracking = true;
1136 	}
1137 
1138 	// pop up a context menu if right mouse button is down for 200 ms
1139 
1140 	if ((buttons & 2) == 0)
1141 		return;
1142 	bigtime_t start = system_time();
1143 	bigtime_t delay = 200000;
1144 	BPoint location;
1145 	do {
1146 		fVideoView->GetMouse(&location, &buttons);
1147 		if ((buttons & 2) == 0)
1148 			break;
1149 		snooze(1000);
1150 	} while (system_time() - start < delay);
1151 
1152 	if (buttons & 2)
1153 		_ShowContextMenu(screen_where);
1154 }
1155 
1156 
1157 void
1158 MainWin::_MouseMoved(BMessage *msg, BView* originalHandler)
1159 {
1160 //	msg->PrintToStream();
1161 
1162 	BPoint mousePos;
1163 	uint32 buttons = msg->FindInt32("buttons");
1164 
1165 	if (1 == buttons && fMouseDownTracking && !fIsFullscreen) {
1166 /*
1167 		// very broken in Zeta:
1168 		BPoint mousePos = msg->FindPoint("where");
1169 		printf("view where: %.0f, %.0f => ", mousePos.x, mousePos.y);
1170 		fVideoView->ConvertToScreen(&mousePos);
1171 */
1172 		// On Zeta, only "screen_where" is relyable, "where"
1173 		// and "be:view_where" seem to be broken
1174 		if (B_OK != msg->FindPoint("screen_where", &mousePos)) {
1175 			// Workaround for BeOS R5, it has no "screen_where"
1176 			if (!originalHandler || msg->FindPoint("where", &mousePos) < B_OK)
1177 				return;
1178 			originalHandler->ConvertToScreen(&mousePos);
1179 		}
1180 //		printf("screen where: %.0f, %.0f => ", mousePos.x, mousePos.y);
1181 		float delta_x = mousePos.x - fMouseDownMousePos.x;
1182 		float delta_y = mousePos.y - fMouseDownMousePos.y;
1183 		float x = fMouseDownWindowPos.x + delta_x;
1184 		float y = fMouseDownWindowPos.y + delta_y;
1185 //		printf("move window to %.0f, %.0f\n", x, y);
1186 		MoveTo(x, y);
1187 	}
1188 }
1189 
1190 
1191 void
1192 MainWin::_MouseUp(BMessage *msg)
1193 {
1194 //	msg->PrintToStream();
1195 	fMouseDownTracking = false;
1196 }
1197 
1198 
1199 void
1200 MainWin::_ShowContextMenu(const BPoint &screen_point)
1201 {
1202 	printf("Show context menu\n");
1203 	BPopUpMenu *menu = new BPopUpMenu("context menu", false, false);
1204 	BMenuItem *item;
1205 	menu->AddItem(item = new BMenuItem("Full Screen",
1206 		new BMessage(M_TOGGLE_FULLSCREEN), 'F'));
1207 	item->SetMarked(fIsFullscreen);
1208 	item->SetEnabled(fHasVideo);
1209 	menu->AddItem(item = new BMenuItem("Keep Aspect Ratio",
1210 		new BMessage(M_TOGGLE_KEEP_ASPECT_RATIO), 'K'));
1211 	item->SetMarked(fKeepAspectRatio);
1212 	item->SetEnabled(fHasVideo);
1213 
1214 	menu->AddSeparatorItem();
1215 	menu->AddItem(item = new BMenuItem("No Menu",
1216 		new BMessage(M_TOGGLE_NO_MENU), 'M'));
1217 	item->SetMarked(fNoMenu);
1218 	menu->AddItem(item = new BMenuItem("No Border",
1219 		new BMessage(M_TOGGLE_NO_BORDER), 'B'));
1220 	item->SetMarked(fNoBorder);
1221 	menu->AddItem(item = new BMenuItem("Always on Top",
1222 		new BMessage(M_TOGGLE_ALWAYS_ON_TOP), 'T'));
1223 	item->SetMarked(fAlwaysOnTop);
1224 
1225 	menu->AddSeparatorItem();
1226 	menu->AddItem(new BMenuItem("About " NAME B_UTF8_ELLIPSIS,
1227 		new BMessage(B_ABOUT_REQUESTED)));
1228 	menu->AddSeparatorItem();
1229 	menu->AddItem(new BMenuItem("Quit", new BMessage(M_FILE_QUIT), 'Q'));
1230 
1231 	menu->AddSeparatorItem();
1232 	menu->AddItem(new BMenuItem("pixel aspect ratio 1.00000:1",
1233 		new BMessage(M_ASPECT_100000_1)));
1234 	menu->AddItem(new BMenuItem("pixel aspect ratio 1.06666:1",
1235 		new BMessage(M_ASPECT_106666_1)));
1236 	menu->AddItem(new BMenuItem("pixel aspect ratio 1.09091:1",
1237 		new BMessage(M_ASPECT_109091_1)));
1238 	menu->AddItem(new BMenuItem("pixel aspect ratio 1.41176:1",
1239 		new BMessage(M_ASPECT_141176_1)));
1240 	menu->AddItem(new BMenuItem("force 720 x 576, display aspect 4:3",
1241 		new BMessage(M_ASPECT_720_576)));
1242 	menu->AddItem(new BMenuItem("force 704 x 576, display aspect 4:3",
1243 		new BMessage(M_ASPECT_704_576)));
1244 	menu->AddItem(new BMenuItem("force 544 x 576, display aspect 4:3",
1245 		new BMessage(M_ASPECT_544_576)));
1246 
1247 	menu->SetTargetForItems(this);
1248 	BRect r(screen_point.x - 5, screen_point.y - 5, screen_point.x + 5,
1249 		screen_point.y + 5);
1250 	menu->Go(screen_point, true, true, r, true);
1251 }
1252 
1253 
1254 /* Trap keys that are about to be send to background or renderer view.
1255  * Return B_OK if it shouldn't be passed to the view
1256  */
1257 status_t
1258 MainWin::_KeyDown(BMessage *msg)
1259 {
1260 //	msg->PrintToStream();
1261 
1262 	uint32 key		 = msg->FindInt32("key");
1263 	uint32 raw_char  = msg->FindInt32("raw_char");
1264 	uint32 modifiers = msg->FindInt32("modifiers");
1265 
1266 	printf("key 0x%lx, raw_char 0x%lx, modifiers 0x%lx\n", key, raw_char,
1267 		modifiers);
1268 
1269 	switch (raw_char) {
1270 		case B_SPACE:
1271 			fController->TogglePlaying();
1272 			return B_OK;
1273 
1274 		case B_ESCAPE:
1275 			if (fIsFullscreen) {
1276 				PostMessage(M_TOGGLE_FULLSCREEN);
1277 				return B_OK;
1278 			} else
1279 				break;
1280 
1281 		case B_ENTER:		// Enter / Return
1282 			if (modifiers & B_COMMAND_KEY) {
1283 				PostMessage(M_TOGGLE_FULLSCREEN);
1284 				return B_OK;
1285 			} else
1286 				break;
1287 
1288 		case B_TAB:
1289 			if ((modifiers & (B_COMMAND_KEY | B_CONTROL_KEY | B_OPTION_KEY
1290 					| B_MENU_KEY)) == 0) {
1291 				PostMessage(M_TOGGLE_FULLSCREEN);
1292 				return B_OK;
1293 			} else
1294 				break;
1295 
1296 		case B_UP_ARROW:
1297 			if (modifiers & B_COMMAND_KEY) {
1298 				PostMessage(M_SKIP_NEXT);
1299 			} else {
1300 				PostMessage(M_VOLUME_UP);
1301 			}
1302 			return B_OK;
1303 
1304 		case B_DOWN_ARROW:
1305 			if (modifiers & B_COMMAND_KEY) {
1306 				PostMessage(M_SKIP_PREV);
1307 			} else {
1308 				PostMessage(M_VOLUME_DOWN);
1309 			}
1310 			return B_OK;
1311 
1312 		case B_RIGHT_ARROW:
1313 			if (modifiers & B_COMMAND_KEY) {
1314 				PostMessage(M_VOLUME_UP);
1315 			} else {
1316 				PostMessage(M_SKIP_NEXT);
1317 			}
1318 			return B_OK;
1319 
1320 		case B_LEFT_ARROW:
1321 			if (modifiers & B_COMMAND_KEY) {
1322 				PostMessage(M_VOLUME_DOWN);
1323 			} else {
1324 				PostMessage(M_SKIP_PREV);
1325 			}
1326 			return B_OK;
1327 
1328 		case B_PAGE_UP:
1329 			PostMessage(M_SKIP_NEXT);
1330 			return B_OK;
1331 
1332 		case B_PAGE_DOWN:
1333 			PostMessage(M_SKIP_PREV);
1334 			return B_OK;
1335 	}
1336 
1337 	switch (key) {
1338 		case 0x3a:  		// numeric keypad +
1339 			if ((modifiers & B_COMMAND_KEY) == 0) {
1340 				printf("if\n");
1341 				PostMessage(M_VOLUME_UP);
1342 				return B_OK;
1343 			} else {
1344 				printf("else\n");
1345 				break;
1346 			}
1347 
1348 		case 0x25:  		// numeric keypad -
1349 			if ((modifiers & B_COMMAND_KEY) == 0) {
1350 				PostMessage(M_VOLUME_DOWN);
1351 				return B_OK;
1352 			} else {
1353 				break;
1354 			}
1355 
1356 		case 0x38:			// numeric keypad up arrow
1357 			PostMessage(M_VOLUME_UP);
1358 			return B_OK;
1359 
1360 		case 0x59:			// numeric keypad down arrow
1361 			PostMessage(M_VOLUME_DOWN);
1362 			return B_OK;
1363 
1364 		case 0x39:			// numeric keypad page up
1365 		case 0x4a:			// numeric keypad right arrow
1366 			PostMessage(M_SKIP_NEXT);
1367 			return B_OK;
1368 
1369 		case 0x5a:			// numeric keypad page down
1370 		case 0x48:			// numeric keypad left arrow
1371 			PostMessage(M_SKIP_PREV);
1372 			return B_OK;
1373 	}
1374 
1375 	return B_ERROR;
1376 }
1377 
1378 
1379 // #pragma mark -
1380 
1381 
1382 void
1383 MainWin::_ToggleNoBorderNoMenu()
1384 {
1385 	if (!fNoMenu && !fNoBorder && !fNoControls) {
1386 		PostMessage(M_TOGGLE_NO_MENU);
1387 		PostMessage(M_TOGGLE_NO_BORDER);
1388 		PostMessage(M_TOGGLE_NO_CONTROLS);
1389 	} else {
1390 		if (!fNoMenu)
1391 			PostMessage(M_TOGGLE_NO_MENU);
1392 		if (!fNoBorder)
1393 			PostMessage(M_TOGGLE_NO_BORDER);
1394 		if (!fNoControls)
1395 			PostMessage(M_TOGGLE_NO_CONTROLS);
1396 	}
1397 }
1398 
1399 
1400 void
1401 MainWin::_ToggleFullscreen()
1402 {
1403 	printf("_ToggleFullscreen enter\n");
1404 
1405 	if (!fHasVideo) {
1406 		printf("_ToggleFullscreen - ignoring, as we don't have a video\n");
1407 		return;
1408 	}
1409 
1410 	fIsFullscreen = !fIsFullscreen;
1411 
1412 	if (fIsFullscreen) {
1413 		// switch to fullscreen
1414 
1415 		fSavedFrame = Frame();
1416 		printf("saving current frame: %d %d %d %d\n", int(fSavedFrame.left),
1417 			int(fSavedFrame.top), int(fSavedFrame.right),
1418 			int(fSavedFrame.bottom));
1419 		BScreen screen(this);
1420 		BRect rect(screen.Frame());
1421 
1422 		Hide();
1423 		MoveTo(rect.left, rect.top);
1424 		ResizeTo(rect.Width(), rect.Height());
1425 		Show();
1426 
1427 	} else {
1428 		// switch back from full screen mode
1429 
1430 		Hide();
1431 		MoveTo(fSavedFrame.left, fSavedFrame.top);
1432 		ResizeTo(fSavedFrame.Width(), fSavedFrame.Height());
1433 		Show();
1434 	}
1435 
1436 	_MarkSettingsItem(M_TOGGLE_FULLSCREEN, fIsFullscreen);
1437 
1438 	printf("_ToggleFullscreen leave\n");
1439 }
1440 
1441 void
1442 MainWin::_ToggleNoControls()
1443 {
1444 	printf("_ToggleNoControls enter\n");
1445 
1446 	if (fIsFullscreen) {
1447 		// fullscreen is always without menu
1448 		printf("_ToggleNoControls leave, doing nothing, we are fullscreen\n");
1449 		return;
1450 	}
1451 
1452 	fNoControls = !fNoControls;
1453 	_SetWindowSizeLimits();
1454 
1455 	if (fNoControls) {
1456 		ResizeBy(0, - fControlsHeight);
1457 	} else {
1458 		ResizeBy(0, fControlsHeight);
1459 	}
1460 
1461 	_MarkSettingsItem(M_TOGGLE_NO_CONTROLS, fNoControls);
1462 
1463 	printf("_ToggleNoControls leave\n");
1464 }
1465 
1466 void
1467 MainWin::_ToggleNoMenu()
1468 {
1469 	printf("_ToggleNoMenu enter\n");
1470 
1471 	if (fIsFullscreen) {
1472 		// fullscreen is always without menu
1473 		printf("_ToggleNoMenu leave, doing nothing, we are fullscreen\n");
1474 		return;
1475 	}
1476 
1477 	fNoMenu = !fNoMenu;
1478 	_SetWindowSizeLimits();
1479 
1480 	if (fNoMenu) {
1481 		MoveBy(0, fMenuBarHeight);
1482 		ResizeBy(0, - fMenuBarHeight);
1483 	} else {
1484 		MoveBy(0, - fMenuBarHeight);
1485 		ResizeBy(0, fMenuBarHeight);
1486 	}
1487 
1488 	_MarkSettingsItem(M_TOGGLE_NO_MENU, fNoMenu);
1489 
1490 	printf("_ToggleNoMenu leave\n");
1491 }
1492 
1493 
1494 void
1495 MainWin::_ToggleNoBorder()
1496 {
1497 	fNoBorder = !fNoBorder;
1498 	SetLook(fNoBorder ? B_BORDERED_WINDOW_LOOK : B_TITLED_WINDOW_LOOK);
1499 
1500 	_MarkSettingsItem(M_TOGGLE_NO_BORDER, fNoBorder);
1501 }
1502 
1503 
1504 void
1505 MainWin::_ToggleAlwaysOnTop()
1506 {
1507 	fAlwaysOnTop = !fAlwaysOnTop;
1508 	SetFeel(fAlwaysOnTop ? B_FLOATING_ALL_WINDOW_FEEL : B_NORMAL_WINDOW_FEEL);
1509 
1510 	_MarkSettingsItem(M_TOGGLE_ALWAYS_ON_TOP, fAlwaysOnTop);
1511 }
1512 
1513 
1514 void
1515 MainWin::_ToggleKeepAspectRatio()
1516 {
1517 	fKeepAspectRatio = !fKeepAspectRatio;
1518 	FrameResized(Bounds().Width(), Bounds().Height());
1519 
1520 	_MarkSettingsItem(M_TOGGLE_KEEP_ASPECT_RATIO, fKeepAspectRatio);
1521 }
1522 
1523 
1524 // #pragma mark -
1525 
1526 
1527 void
1528 MainWin::_UpdateControlsEnabledStatus()
1529 {
1530 	uint32 enabledButtons = 0;
1531 	if (fHasVideo || fHasAudio) {
1532 		enabledButtons |= PLAYBACK_ENABLED | SEEK_ENABLED
1533 			| SEEK_BACK_ENABLED | SEEK_FORWARD_ENABLED;
1534 	}
1535 	if (fHasAudio)
1536 		enabledButtons |= VOLUME_ENABLED;
1537 
1538 	bool canSkipPrevious, canSkipNext;
1539 	fPlaylist->GetSkipInfo(&canSkipPrevious, &canSkipNext);
1540 	if (canSkipPrevious)
1541 		enabledButtons |= SKIP_BACK_ENABLED;
1542 	if (canSkipNext)
1543 		enabledButtons |= SKIP_FORWARD_ENABLED;
1544 
1545 	fControls->SetEnabled(enabledButtons);
1546 }
1547 
1548 
1549 void
1550 MainWin::_UpdatePlaylistMenu()
1551 {
1552 	if (!fPlaylist->Lock())
1553 		return;
1554 
1555 	fPlaylistMenu->RemoveItems(0, fPlaylistMenu->CountItems(), true);
1556 
1557 	int32 count = fPlaylist->CountItems();
1558 	for (int32 i = 0; i < count; i++) {
1559 		entry_ref ref;
1560 		if (fPlaylist->GetRefAt(i, &ref) < B_OK)
1561 			continue;
1562 		_AddPlaylistItem(ref, i);
1563 	}
1564 	fPlaylistMenu->SetTargetForItems(this);
1565 
1566 	_MarkPlaylistItem(fPlaylist->CurrentRefIndex());
1567 
1568 	fPlaylist->Unlock();
1569 }
1570 
1571 
1572 void
1573 MainWin::_AddPlaylistItem(const entry_ref& ref, int32 index)
1574 {
1575 	BMessage* message = new BMessage(M_SET_PLAYLIST_POSITION);
1576 	message->AddInt32("index", index);
1577 	BMenuItem* item = new BMenuItem(ref.name, message);
1578 	fPlaylistMenu->AddItem(item, index);
1579 }
1580 
1581 
1582 void
1583 MainWin::_RemovePlaylistItem(int32 index)
1584 {
1585 	delete fPlaylistMenu->RemoveItem(index);
1586 }
1587 
1588 
1589 void
1590 MainWin::_MarkPlaylistItem(int32 index)
1591 {
1592 	if (BMenuItem* item = fPlaylistMenu->ItemAt(index)) {
1593 		item->SetMarked(true);
1594 		// ... and in case the menu is currently on screen:
1595 		if (fPlaylistMenu->LockLooper()) {
1596 			fPlaylistMenu->Invalidate();
1597 			fPlaylistMenu->UnlockLooper();
1598 		}
1599 	}
1600 }
1601 
1602 
1603 void
1604 MainWin::_MarkSettingsItem(uint32 command, bool mark)
1605 {
1606 	if (BMenuItem* item = fSettingsMenu->FindItem(command))
1607 		item->SetMarked(mark);
1608 }
1609 
1610 
1611