xref: /haiku/src/apps/mediaplayer/MainWin.cpp (revision e28afdf1ea3cb78c0fa888e50ecf1c494ff636cb)
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-2010 Stephan Aßmus <superstippi@gmx.de> (GPL->MIT ok)
6  * Copyright (C) 2007-2009 Fredrik Modéen <[FirstName]@[LastName].se> (MIT ok)
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 
24 #include "MainWin.h"
25 
26 #include <math.h>
27 #include <stdio.h>
28 #include <string.h>
29 
30 #include <Alert.h>
31 #include <Application.h>
32 #include <Autolock.h>
33 #include <Catalog.h>
34 #include <Debug.h>
35 #include <fs_attr.h>
36 #include <Language.h>
37 #include <Locale.h>
38 #include <Menu.h>
39 #include <MenuBar.h>
40 #include <MenuItem.h>
41 #include <MessageRunner.h>
42 #include <Messenger.h>
43 #include <PopUpMenu.h>
44 #include <PropertyInfo.h>
45 #include <RecentItems.h>
46 #include <Roster.h>
47 #include <Screen.h>
48 #include <String.h>
49 #include <TypeConstants.h>
50 #include <View.h>
51 
52 #include "AudioProducer.h"
53 #include "ControllerObserver.h"
54 #include "DurationToString.h"
55 #include "FilePlaylistItem.h"
56 #include "MainApp.h"
57 #include "PeakView.h"
58 #include "PlaylistItem.h"
59 #include "PlaylistObserver.h"
60 #include "PlaylistWindow.h"
61 #include "Settings.h"
62 
63 
64 #undef B_TRANSLATE_CONTEXT
65 #define B_TRANSLATE_CONTEXT "MediaPlayer-Main"
66 #define MIN_WIDTH 250
67 
68 
69 int MainWin::sNoVideoWidth = MIN_WIDTH;
70 
71 
72 // XXX TODO: why is lround not defined?
73 #define lround(a) ((int)(0.99999 + (a)))
74 
75 enum {
76 	M_DUMMY = 0x100,
77 	M_FILE_OPEN = 0x1000,
78 	M_FILE_INFO,
79 	M_FILE_PLAYLIST,
80 	M_FILE_CLOSE,
81 	M_FILE_QUIT,
82 	M_VIEW_SIZE,
83 	M_TOGGLE_FULLSCREEN,
84 	M_TOGGLE_ALWAYS_ON_TOP,
85 	M_TOGGLE_NO_INTERFACE,
86 	M_VOLUME_UP,
87 	M_VOLUME_DOWN,
88 	M_SKIP_NEXT,
89 	M_SKIP_PREV,
90 	M_WIND,
91 
92 	// The common display aspect ratios
93 	M_ASPECT_SAME_AS_SOURCE,
94 	M_ASPECT_NO_DISTORTION,
95 	M_ASPECT_4_3,
96 	M_ASPECT_16_9,
97 	M_ASPECT_83_50,
98 	M_ASPECT_7_4,
99 	M_ASPECT_37_20,
100 	M_ASPECT_47_20,
101 
102 	M_SELECT_AUDIO_TRACK			= 0x00000800,
103 	M_SELECT_AUDIO_TRACK_END		= 0x00000fff,
104 	M_SELECT_VIDEO_TRACK			= 0x00010000,
105 	M_SELECT_VIDEO_TRACK_END		= 0x00010fff,
106 	M_SELECT_SUB_TITLE_TRACK		= 0x00020000,
107 	M_SELECT_SUB_TITLE_TRACK_END	= 0x00020fff,
108 
109 	M_SET_RATING,
110 
111 	M_SET_PLAYLIST_POSITION,
112 
113 	M_FILE_DELETE,
114 
115 	M_SHOW_IF_NEEDED,
116 
117 	M_SLIDE_CONTROLS,
118 	M_FINISH_SLIDING_CONTROLS
119 };
120 
121 
122 static property_info sPropertyInfo[] = {
123 	{ B_TRANSLATE("Next"), { B_EXECUTE_PROPERTY },
124 		{ B_DIRECT_SPECIFIER, 0 },
125 		B_TRANSLATE("Skip to the next track."), 0
126 	},
127 	{ B_TRANSLATE("Prev"), { B_EXECUTE_PROPERTY },
128 		{ B_DIRECT_SPECIFIER, 0 },
129 		B_TRANSLATE("Skip to the previous track."), 0
130 	},
131 	{ B_TRANSLATE("Play"), { B_EXECUTE_PROPERTY },
132 		{ B_DIRECT_SPECIFIER, 0 },
133 		B_TRANSLATE("Start playing."), 0
134 	},
135 	{ B_TRANSLATE("Stop"), { B_EXECUTE_PROPERTY },
136 		{ B_DIRECT_SPECIFIER, 0 },
137 		B_TRANSLATE("Stop playing."), 0
138 	},
139 	{ B_TRANSLATE("Pause"), { B_EXECUTE_PROPERTY },
140 		{ B_DIRECT_SPECIFIER, 0 },
141 		B_TRANSLATE("Pause playback."), 0
142 	},
143 	{ B_TRANSLATE("TogglePlaying"), { B_EXECUTE_PROPERTY },
144 		{ B_DIRECT_SPECIFIER, 0 },
145 		B_TRANSLATE("Toggle pause/play."), 0
146 	},
147 	{ B_TRANSLATE("Mute"), { B_EXECUTE_PROPERTY },
148 		{ B_DIRECT_SPECIFIER, 0 },
149 		B_TRANSLATE("Toggle mute."), 0
150 	},
151 	{ B_TRANSLATE("Volume"), { B_GET_PROPERTY, B_SET_PROPERTY, 0 },
152 		{ B_DIRECT_SPECIFIER, 0 },
153 		B_TRANSLATE("Gets/sets the volume (0.0-2.0)."), 0,
154 		{ B_FLOAT_TYPE }
155 	},
156 	{ B_TRANSLATE("URI"), { B_GET_PROPERTY, 0 },
157 		{ B_DIRECT_SPECIFIER, 0 },
158 		B_TRANSLATE("Gets the URI of the currently playing item."), 0,
159 		{ B_STRING_TYPE }
160 	},
161 	{ 0, { 0 }, { 0 }, 0, 0 }
162 };
163 
164 
165 static const char* kRatingAttrName = "Media:Rating";
166 
167 static const char* kDisabledSeekMessage = B_TRANSLATE("Drop files to play");
168 
169 static const char* kApplicationName = B_TRANSLATE_SYSTEM_NAME(NAME);
170 
171 
172 //#define printf(a...)
173 
174 
175 MainWin::MainWin(bool isFirstWindow, BMessage* message)
176 	:
177 	BWindow(BRect(100, 100, 400, 300), kApplicationName, B_TITLED_WINDOW,
178  		B_ASYNCHRONOUS_CONTROLS /* | B_WILL_ACCEPT_FIRST_CLICK */),
179  	fCreationTime(system_time()),
180 	fInfoWin(NULL),
181 	fPlaylistWindow(NULL),
182 	fHasFile(false),
183 	fHasVideo(false),
184 	fHasAudio(false),
185 	fPlaylist(new Playlist),
186 	fPlaylistObserver(new PlaylistObserver(this)),
187 	fController(new Controller),
188 	fControllerObserver(new ControllerObserver(this,
189 		OBSERVE_FILE_CHANGES | OBSERVE_TRACK_CHANGES
190 			| OBSERVE_PLAYBACK_STATE_CHANGES | OBSERVE_POSITION_CHANGES
191 			| OBSERVE_VOLUME_CHANGES)),
192 	fIsFullscreen(false),
193 	fAlwaysOnTop(false),
194 	fNoInterface(false),
195 	fShowsFullscreenControls(false),
196 	fSourceWidth(-1),
197 	fSourceHeight(-1),
198 	fWidthAspect(0),
199 	fHeightAspect(0),
200 	fSavedFrame(),
201 	fNoVideoFrame(),
202 
203 	fMouseDownTracking(false),
204 	fLastMousePos(0, 0),
205 	fLastMouseMovedTime(system_time()),
206 	fMouseMoveDist(0),
207 
208 	fGlobalSettingsListener(this),
209 	fInitialSeekPosition(0),
210 	fAllowWinding(true)
211 {
212 	// Handle window position and size depending on whether this is the
213 	// first window or not. Use the window size from the window that was
214 	// last resized by the user.
215 	static int pos = 0;
216 	MoveBy(pos * 25, pos * 25);
217 	pos = (pos + 1) % 15;
218 
219 	BRect frame = Settings::Default()->CurrentSettings()
220 		.audioPlayerWindowFrame;
221 	if (frame.IsValid()) {
222 		if (isFirstWindow) {
223 			if (message == NULL) {
224 				MoveTo(frame.LeftTop());
225 				ResizeTo(frame.Width(), frame.Height());
226 			} else {
227 				// Delay moving to the initial position, since we don't
228 				// know if we will be playing audio at all.
229 				message->AddRect("window frame", frame);
230 			}
231 		}
232 		if (sNoVideoWidth == MIN_WIDTH)
233 			sNoVideoWidth = frame.IntegerWidth();
234 	} else if (sNoVideoWidth > MIN_WIDTH) {
235 		ResizeTo(sNoVideoWidth, Bounds().Height());
236 	}
237 	fNoVideoWidth = sNoVideoWidth;
238 
239 	BRect rect = Bounds();
240 
241 	// background
242 	fBackground = new BView(rect, "background", B_FOLLOW_ALL,
243 		B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE);
244 	fBackground->SetViewColor(0, 0, 0);
245 	AddChild(fBackground);
246 
247 	// menu
248 	fMenuBar = new BMenuBar(fBackground->Bounds(), "menu");
249 	_CreateMenu();
250 	fBackground->AddChild(fMenuBar);
251 	fMenuBar->SetResizingMode(B_FOLLOW_NONE);
252 	fMenuBar->ResizeToPreferred();
253 	fMenuBarWidth = (int)fMenuBar->Frame().Width() + 1;
254 	fMenuBarHeight = (int)fMenuBar->Frame().Height() + 1;
255 
256 	// video view
257 	rect = BRect(0, fMenuBarHeight, fBackground->Bounds().right,
258 		fMenuBarHeight + 10);
259 	fVideoView = new VideoView(rect, "video display", B_FOLLOW_NONE);
260 	fBackground->AddChild(fVideoView);
261 
262 	// controls
263 	rect = BRect(0, fMenuBarHeight + 11, fBackground->Bounds().right,
264 		fBackground->Bounds().bottom);
265 	fControls = new ControllerView(rect, fController, fPlaylist);
266 	fBackground->AddChild(fControls);
267 	fControls->ResizeToPreferred();
268 	fControlsHeight = (int)fControls->Frame().Height() + 1;
269 	fControlsWidth = (int)fControls->Frame().Width() + 1;
270 	fControls->SetResizingMode(B_FOLLOW_BOTTOM | B_FOLLOW_LEFT_RIGHT);
271 	fControls->SetDisabledString(kDisabledSeekMessage);
272 
273 	fPlaylist->AddListener(fPlaylistObserver);
274 	fController->SetVideoView(fVideoView);
275 	fController->AddListener(fControllerObserver);
276 	PeakView* peakView = fControls->GetPeakView();
277 	peakView->SetPeakNotificationWhat(MSG_PEAK_NOTIFICATION);
278 	fController->SetPeakListener(peakView);
279 
280 	_SetupWindow();
281 
282 	// setup the playlist window now, we need to have it
283 	// running for the undo/redo playlist editing
284 	fPlaylistWindow = new PlaylistWindow(BRect(150, 150, 500, 600), fPlaylist,
285 		fController);
286 	fPlaylistWindow->Hide();
287 	fPlaylistWindow->Show();
288 		// this makes sure the window thread is running without
289 		// showing the window just yet
290 
291 	Settings::Default()->AddListener(&fGlobalSettingsListener);
292 	_AdoptGlobalSettings();
293 
294 	AddShortcut('z', B_COMMAND_KEY, new BMessage(B_UNDO));
295 	AddShortcut('y', B_COMMAND_KEY, new BMessage(B_UNDO));
296 	AddShortcut('z', B_COMMAND_KEY | B_SHIFT_KEY, new BMessage(B_REDO));
297 	AddShortcut('y', B_COMMAND_KEY | B_SHIFT_KEY, new BMessage(B_REDO));
298 
299 	Hide();
300 	Show();
301 
302 	if (message != NULL)
303 		PostMessage(message);
304 }
305 
306 
307 MainWin::~MainWin()
308 {
309 //	printf("MainWin::~MainWin\n");
310 
311 	Settings::Default()->RemoveListener(&fGlobalSettingsListener);
312 	fPlaylist->RemoveListener(fPlaylistObserver);
313 	fController->Lock();
314 	fController->RemoveListener(fControllerObserver);
315 	fController->SetPeakListener(NULL);
316 	fController->SetVideoTarget(NULL);
317 	fController->Unlock();
318 
319 	// give the views a chance to detach from any notifiers
320 	// before we delete them
321 	fBackground->RemoveSelf();
322 	delete fBackground;
323 
324 	if (fInfoWin && fInfoWin->Lock())
325 		fInfoWin->Quit();
326 
327 	if (fPlaylistWindow && fPlaylistWindow->Lock())
328 		fPlaylistWindow->Quit();
329 
330 	delete fPlaylist;
331 	fPlaylist = NULL;
332 
333 	// quit the Controller looper thread
334 	thread_id controllerThread = fController->Thread();
335 	fController->PostMessage(B_QUIT_REQUESTED);
336 	status_t exitValue;
337 	wait_for_thread(controllerThread, &exitValue);
338 }
339 
340 
341 // #pragma mark -
342 
343 
344 void
345 MainWin::FrameResized(float newWidth, float newHeight)
346 {
347 	if (newWidth != Bounds().Width() || newHeight != Bounds().Height()) {
348 		debugger("size wrong\n");
349 	}
350 
351 	bool noMenu = fNoInterface || fIsFullscreen;
352 	bool noControls = fNoInterface || fIsFullscreen;
353 
354 //	printf("FrameResized enter: newWidth %.0f, newHeight %.0f\n",
355 //		newWidth, newHeight);
356 
357 	if (!fHasVideo)
358 		sNoVideoWidth = fNoVideoWidth = (int)newWidth;
359 
360 	int maxVideoWidth  = int(newWidth) + 1;
361 	int maxVideoHeight = int(newHeight) + 1
362 		- (noMenu  ? 0 : fMenuBarHeight)
363 		- (noControls ? 0 : fControlsHeight);
364 
365 	ASSERT(maxVideoHeight >= 0);
366 
367 	int y = 0;
368 
369 	if (noMenu) {
370 		if (!fMenuBar->IsHidden(fMenuBar))
371 			fMenuBar->Hide();
372 	} else {
373 		fMenuBar->MoveTo(0, y);
374 		fMenuBar->ResizeTo(newWidth, fMenuBarHeight - 1);
375 		if (fMenuBar->IsHidden(fMenuBar))
376 			fMenuBar->Show();
377 		y += fMenuBarHeight;
378 	}
379 
380 	if (maxVideoHeight == 0) {
381 		if (!fVideoView->IsHidden(fVideoView))
382 			fVideoView->Hide();
383 	} else {
384 		_ResizeVideoView(0, y, maxVideoWidth, maxVideoHeight);
385 		if (fVideoView->IsHidden(fVideoView))
386 			fVideoView->Show();
387 		y += maxVideoHeight;
388 	}
389 
390 	if (noControls) {
391 		if (!fControls->IsHidden(fControls))
392 			fControls->Hide();
393 	} else {
394 		fControls->MoveTo(0, y);
395 		fControls->ResizeTo(newWidth, fControlsHeight - 1);
396 		if (fControls->IsHidden(fControls))
397 			fControls->Show();
398 //		y += fControlsHeight;
399 	}
400 
401 //	printf("FrameResized leave\n");
402 }
403 
404 
405 void
406 MainWin::Zoom(BPoint /*position*/, float /*width*/, float /*height*/)
407 {
408 	PostMessage(M_TOGGLE_FULLSCREEN);
409 }
410 
411 
412 void
413 MainWin::DispatchMessage(BMessage* msg, BHandler* handler)
414 {
415 	if ((msg->what == B_MOUSE_DOWN)
416 		&& (handler == fBackground || handler == fVideoView
417 			|| handler == fControls)) {
418 		_MouseDown(msg, dynamic_cast<BView*>(handler));
419 	}
420 
421 	if ((msg->what == B_MOUSE_MOVED)
422 		&& (handler == fBackground || handler == fVideoView
423 			|| handler == fControls)) {
424 		_MouseMoved(msg, dynamic_cast<BView*>(handler));
425 	}
426 
427 	if ((msg->what == B_MOUSE_UP)
428 		&& (handler == fBackground || handler == fVideoView)) {
429 		_MouseUp(msg);
430 	}
431 
432 	if ((msg->what == B_KEY_DOWN)
433 		&& (handler == fBackground || handler == fVideoView)) {
434 		// special case for PrintScreen key
435 		if (msg->FindInt32("key") == B_PRINT_KEY) {
436 			fVideoView->OverlayScreenshotPrepare();
437 			BWindow::DispatchMessage(msg, handler);
438 			fVideoView->OverlayScreenshotCleanup();
439 			return;
440 		}
441 
442 		// every other key gets dispatched to our _KeyDown first
443 		if (_KeyDown(msg)) {
444 			// it got handled, don't pass it on
445 			return;
446 		}
447 	}
448 
449 	BWindow::DispatchMessage(msg, handler);
450 }
451 
452 
453 void
454 MainWin::MessageReceived(BMessage* msg)
455 {
456 //	msg->PrintToStream();
457 	switch (msg->what) {
458 		case B_EXECUTE_PROPERTY:
459 		case B_GET_PROPERTY:
460 		case B_SET_PROPERTY:
461 		{
462 			BMessage reply(B_REPLY);
463 			status_t result = B_BAD_SCRIPT_SYNTAX;
464 			int32 index;
465 			BMessage specifier;
466 			int32 what;
467 			const char* property;
468 
469 			if (msg->GetCurrentSpecifier(&index, &specifier, &what,
470 					&property) != B_OK) {
471 				return BWindow::MessageReceived(msg);
472 			}
473 
474 			BPropertyInfo propertyInfo(sPropertyInfo);
475 			switch (propertyInfo.FindMatch(msg, index, &specifier, what,
476 					property)) {
477 				case 0:
478 					fControls->SkipForward();
479 					result = B_OK;
480 					break;
481 
482 				case 1:
483 					fControls->SkipBackward();
484 					result = B_OK;
485 					break;
486 
487 				case 2:
488 					fController->Play();
489 					result = B_OK;
490 					break;
491 
492 				case 3:
493 					fController->Stop();
494 					result = B_OK;
495 					break;
496 
497 				case 4:
498 					fController->Pause();
499 					result = B_OK;
500 					break;
501 
502 				case 5:
503 					fController->TogglePlaying();
504 					result = B_OK;
505 					break;
506 
507 				case 6:
508 					fController->ToggleMute();
509 					result = B_OK;
510 					break;
511 
512 				case 7:
513 				{
514 					if (msg->what == B_GET_PROPERTY) {
515 						result = reply.AddFloat("result",
516 							fController->Volume());
517 					} else if (msg->what == B_SET_PROPERTY) {
518 						float newVolume;
519 						result = msg->FindFloat("data", &newVolume);
520 						if (result == B_OK)
521 							fController->SetVolume(newVolume);
522 					}
523 					break;
524 				}
525 
526 				case 8:
527 				{
528 					if (msg->what == B_GET_PROPERTY) {
529 						BAutolock _(fPlaylist);
530 						const PlaylistItem* item = fController->Item();
531 						if (item == NULL) {
532 							result = B_NO_INIT;
533 							break;
534 						}
535 
536 						result = reply.AddString("result", item->LocationURI());
537 					}
538 					break;
539 				}
540 
541 				default:
542 					return BWindow::MessageReceived(msg);
543 			}
544 
545 			if (result != B_OK) {
546 				reply.what = B_MESSAGE_NOT_UNDERSTOOD;
547 				reply.AddString("message", strerror(result));
548 				reply.AddInt32("error", result);
549 			}
550 
551 			msg->SendReply(&reply);
552 			break;
553 		}
554 
555 		case B_REFS_RECEIVED:
556 			_RefsReceived(msg);
557 			break;
558 		case B_SIMPLE_DATA:
559 			if (msg->HasRef("refs"))
560 				_RefsReceived(msg);
561 			break;
562 		case M_OPEN_PREVIOUS_PLAYLIST:
563 			OpenPlaylist(msg);
564 			break;
565 
566 		case B_UNDO:
567 		case B_REDO:
568 			fPlaylistWindow->PostMessage(msg);
569 			break;
570 
571 		case M_MEDIA_SERVER_STARTED:
572 		{
573 			printf("TODO: implement M_MEDIA_SERVER_STARTED\n");
574 //
575 //			BAutolock _(fPlaylist);
576 //			BMessage fakePlaylistMessage(MSG_PLAYLIST_CURRENT_ITEM_CHANGED);
577 //			fakePlaylistMessage.AddInt32("index",
578 //				fPlaylist->CurrentItemIndex());
579 //			PostMessage(&fakePlaylistMessage);
580 			break;
581 		}
582 
583 		case M_MEDIA_SERVER_QUIT:
584 			printf("TODO: implement M_MEDIA_SERVER_QUIT\n");
585 //			if (fController->Lock()) {
586 //				fController->CleanupNodes();
587 //				fController->Unlock();
588 //			}
589 			break;
590 
591 		// PlaylistObserver messages
592 		case MSG_PLAYLIST_ITEM_ADDED:
593 		{
594 			PlaylistItem* item;
595 			int32 index;
596 			if (msg->FindPointer("item", (void**)&item) == B_OK
597 				&& msg->FindInt32("index", &index) == B_OK) {
598 				_AddPlaylistItem(item, index);
599 			}
600 			break;
601 		}
602 		case MSG_PLAYLIST_ITEM_REMOVED:
603 		{
604 			int32 index;
605 			if (msg->FindInt32("index", &index) == B_OK)
606 				_RemovePlaylistItem(index);
607 			break;
608 		}
609 		case MSG_PLAYLIST_CURRENT_ITEM_CHANGED:
610 		{
611 			BAutolock _(fPlaylist);
612 
613 			int32 index;
614 			if (msg->FindInt32("index", &index) < B_OK
615 				|| index != fPlaylist->CurrentItemIndex())
616 				break;
617 			PlaylistItemRef item(fPlaylist->ItemAt(index));
618 			if (item.Get() != NULL) {
619 				printf("open playlist item: %s\n", item->Name().String());
620 				OpenPlaylistItem(item);
621 				_MarkPlaylistItem(index);
622 			}
623 			break;
624 		}
625 		case MSG_PLAYLIST_IMPORT_FAILED:
626 		{
627 			BAlert* alert = new BAlert(B_TRANSLATE("Nothing to Play"),
628 				B_TRANSLATE("None of the files you wanted to play appear "
629 				"to be media files."), B_TRANSLATE("OK"));
630 			alert->Go();
631 			fControls->SetDisabledString(kDisabledSeekMessage);
632 			break;
633 		}
634 
635 		// ControllerObserver messages
636 		case MSG_CONTROLLER_FILE_FINISHED:
637 		{
638 			BAutolock _(fPlaylist);
639 
640 			bool hadNext = fPlaylist->SetCurrentItemIndex(
641 				fPlaylist->CurrentItemIndex() + 1);
642 			if (!hadNext) {
643 				// Reached end of playlist
644 				// Handle "quit when done" settings
645 				if ((fHasVideo && fCloseWhenDonePlayingMovie)
646 					|| (!fHasVideo && fCloseWhenDonePlayingSound))
647 					PostMessage(B_QUIT_REQUESTED);
648 				// Handle "loop by default" settings
649 				if ((fHasVideo && fLoopMovies)
650 					|| (!fHasVideo && fLoopSounds)) {
651 					if (fPlaylist->CountItems() > 1)
652 						fPlaylist->SetCurrentItemIndex(0);
653 					else
654 						fController->Play();
655 				}
656 			}
657 			break;
658 		}
659 		case MSG_CONTROLLER_FILE_CHANGED:
660 		{
661 			status_t result = B_ERROR;
662 			msg->FindInt32("result", &result);
663 			PlaylistItemRef itemRef;
664 			PlaylistItem* item;
665 			if (msg->FindPointer("item", (void**)&item) == B_OK) {
666 				itemRef.SetTo(item, true);
667 					// The reference was passed along with the message.
668 			} else {
669 				BAutolock _(fPlaylist);
670 				itemRef.SetTo(fPlaylist->ItemAt(
671 					fPlaylist->CurrentItemIndex()));
672 			}
673 			_PlaylistItemOpened(itemRef, result);
674 			break;
675 		}
676 		case MSG_CONTROLLER_VIDEO_TRACK_CHANGED:
677 		{
678 			int32 index;
679 			if (msg->FindInt32("index", &index) == B_OK) {
680 				int32 i = 0;
681 				while (BMenuItem* item = fVideoTrackMenu->ItemAt(i)) {
682 					item->SetMarked(i == index);
683 					i++;
684 				}
685 			}
686 			break;
687 		}
688 		case MSG_CONTROLLER_AUDIO_TRACK_CHANGED:
689 		{
690 			int32 index;
691 			if (msg->FindInt32("index", &index) == B_OK) {
692 				int32 i = 0;
693 				while (BMenuItem* item = fAudioTrackMenu->ItemAt(i)) {
694 					item->SetMarked(i == index);
695 					i++;
696 				}
697 				_UpdateAudioChannelCount(index);
698 			}
699 			break;
700 		}
701 		case MSG_CONTROLLER_SUB_TITLE_TRACK_CHANGED:
702 		{
703 			int32 index;
704 			if (msg->FindInt32("index", &index) == B_OK) {
705 				int32 i = 0;
706 				while (BMenuItem* item = fSubTitleTrackMenu->ItemAt(i)) {
707 					BMessage* message = item->Message();
708 					if (message != NULL) {
709 						item->SetMarked((int32)message->what
710 							- M_SELECT_SUB_TITLE_TRACK == index);
711 					}
712 					i++;
713 				}
714 			}
715 			break;
716 		}
717 		case MSG_CONTROLLER_PLAYBACK_STATE_CHANGED:
718 		{
719 			uint32 state;
720 			if (msg->FindInt32("state", (int32*)&state) == B_OK)
721 				fControls->SetPlaybackState(state);
722 			break;
723 		}
724 		case MSG_CONTROLLER_POSITION_CHANGED:
725 		{
726 			float position;
727 			if (msg->FindFloat("position", &position) == B_OK) {
728 				fControls->SetPosition(position, fController->TimePosition(),
729 					fController->TimeDuration());
730 				fAllowWinding = true;
731 			}
732 			break;
733 		}
734 		case MSG_CONTROLLER_SEEK_HANDLED:
735 			break;
736 
737 		case MSG_CONTROLLER_VOLUME_CHANGED:
738 		{
739 			float volume;
740 			if (msg->FindFloat("volume", &volume) == B_OK)
741 				fControls->SetVolume(volume);
742 			break;
743 		}
744 		case MSG_CONTROLLER_MUTED_CHANGED:
745 		{
746 			bool muted;
747 			if (msg->FindBool("muted", &muted) == B_OK)
748 				fControls->SetMuted(muted);
749 			break;
750 		}
751 
752 		// menu item messages
753 		case M_FILE_OPEN:
754 		{
755 			BMessenger target(this);
756 			BMessage result(B_REFS_RECEIVED);
757 			BMessage appMessage(M_SHOW_OPEN_PANEL);
758 			appMessage.AddMessenger("target", target);
759 			appMessage.AddMessage("message", &result);
760 			appMessage.AddString("title", B_TRANSLATE("Open Clips"));
761 			appMessage.AddString("label", B_TRANSLATE("Open"));
762 			be_app->PostMessage(&appMessage);
763 			break;
764 		}
765 		case M_FILE_INFO:
766 			ShowFileInfo();
767 			break;
768 		case M_FILE_PLAYLIST:
769 			ShowPlaylistWindow();
770 			break;
771 		case M_FILE_CLOSE:
772 			PostMessage(B_QUIT_REQUESTED);
773 			break;
774 		case M_FILE_QUIT:
775 			be_app->PostMessage(B_QUIT_REQUESTED);
776 			break;
777 
778 		case M_TOGGLE_FULLSCREEN:
779 			_ToggleFullscreen();
780 			break;
781 
782 		case M_TOGGLE_ALWAYS_ON_TOP:
783 			_ToggleAlwaysOnTop();
784 			break;
785 
786 		case M_TOGGLE_NO_INTERFACE:
787 			_ToggleNoInterface();
788 			break;
789 
790 		case M_VIEW_SIZE:
791 		{
792 			int32 size;
793 			if (msg->FindInt32("size", &size) == B_OK) {
794 				if (!fHasVideo)
795 					break;
796 				if (fIsFullscreen)
797 					_ToggleFullscreen();
798 				_ResizeWindow(size);
799 			}
800 			break;
801 		}
802 
803 /*
804 		case B_ACQUIRE_OVERLAY_LOCK:
805 			printf("B_ACQUIRE_OVERLAY_LOCK\n");
806 			fVideoView->OverlayLockAcquire();
807 			break;
808 
809 		case B_RELEASE_OVERLAY_LOCK:
810 			printf("B_RELEASE_OVERLAY_LOCK\n");
811 			fVideoView->OverlayLockRelease();
812 			break;
813 */
814 		case B_MOUSE_WHEEL_CHANGED:
815 		{
816 			float dx = msg->FindFloat("be:wheel_delta_x");
817 			float dy = msg->FindFloat("be:wheel_delta_y");
818 			bool inv = modifiers() & B_COMMAND_KEY;
819 			if (dx > 0.1)
820 				PostMessage(inv ? M_VOLUME_DOWN : M_SKIP_PREV);
821 			if (dx < -0.1)
822 				PostMessage(inv ? M_VOLUME_UP : M_SKIP_NEXT);
823 			if (dy > 0.1)
824 				PostMessage(inv ? M_SKIP_PREV : M_VOLUME_DOWN);
825 			if (dy < -0.1)
826 				PostMessage(inv ? M_SKIP_NEXT : M_VOLUME_UP);
827 			break;
828 		}
829 
830 		case M_SKIP_NEXT:
831 			fControls->SkipForward();
832 			break;
833 
834 		case M_SKIP_PREV:
835 			fControls->SkipBackward();
836 			break;
837 
838 		case M_WIND:
839 		{
840 			if (!fAllowWinding)
841 				break;
842 
843 			bigtime_t howMuch;
844 			int64 frames;
845 			if (msg->FindInt64("how much", &howMuch) != B_OK
846 				|| msg->FindInt64("frames", &frames) != B_OK) {
847 				break;
848 			}
849 
850 			if (fController->Lock()) {
851 				if (fHasVideo && !fController->IsPlaying()) {
852 					int64 newFrame = fController->CurrentFrame() + frames;
853 					fController->SetFramePosition(newFrame);
854 				} else {
855 					bigtime_t seekTime = fController->TimePosition() + howMuch;
856 					if (seekTime < 0) {
857 						fInitialSeekPosition = seekTime;
858 						PostMessage(M_SKIP_PREV);
859 					} else if (seekTime > fController->TimeDuration()) {
860 						fInitialSeekPosition = 0;
861 						PostMessage(M_SKIP_NEXT);
862 					} else
863 						fController->SetTimePosition(seekTime);
864 				}
865 				fController->Unlock();
866 
867 				fAllowWinding = false;
868 			}
869 			break;
870 		}
871 
872 		case M_VOLUME_UP:
873 			fController->VolumeUp();
874 			break;
875 
876 		case M_VOLUME_DOWN:
877 			fController->VolumeDown();
878 			break;
879 
880 		case M_ASPECT_SAME_AS_SOURCE:
881 			if (fHasVideo) {
882 				int width;
883 				int height;
884 				int widthAspect;
885 				int heightAspect;
886 				fController->GetSize(&width, &height,
887 					&widthAspect, &heightAspect);
888 				VideoFormatChange(width, height, widthAspect, heightAspect);
889 			}
890 			break;
891 
892 		case M_ASPECT_NO_DISTORTION:
893 			if (fHasVideo) {
894 				int width;
895 				int height;
896 				fController->GetSize(&width, &height);
897 				VideoFormatChange(width, height, width, height);
898 			}
899 			break;
900 
901 		case M_ASPECT_4_3:
902 			VideoAspectChange(4, 3);
903 			break;
904 
905 		case M_ASPECT_16_9: // 1.77 : 1
906 			VideoAspectChange(16, 9);
907 			break;
908 
909 		case M_ASPECT_83_50: // 1.66 : 1
910 			VideoAspectChange(83, 50);
911 			break;
912 
913 		case M_ASPECT_7_4: // 1.75 : 1
914 			VideoAspectChange(7, 4);
915 			break;
916 
917 		case M_ASPECT_37_20: // 1.85 : 1
918 			VideoAspectChange(37, 20);
919 			break;
920 
921 		case M_ASPECT_47_20: // 2.35 : 1
922 			VideoAspectChange(47, 20);
923 			break;
924 
925 		case M_SET_PLAYLIST_POSITION:
926 		{
927 			BAutolock _(fPlaylist);
928 
929 			int32 index;
930 			if (msg->FindInt32("index", &index) == B_OK)
931 				fPlaylist->SetCurrentItemIndex(index);
932 			break;
933 		}
934 
935 		case MSG_OBJECT_CHANGED:
936 			// received from fGlobalSettingsListener
937 			// TODO: find out which object, if we ever watch more than
938 			// the global settings instance...
939 			_AdoptGlobalSettings();
940 			break;
941 
942 		case M_SHOW_IF_NEEDED:
943 			_ShowIfNeeded();
944 			break;
945 
946 		case M_SLIDE_CONTROLS:
947 		{
948 			float offset;
949 			if (msg->FindFloat("offset", &offset) == B_OK) {
950 				fControls->MoveBy(0, offset);
951 				fVideoView->SetSubTitleMaxBottom(fControls->Frame().top - 1);
952 				UpdateIfNeeded();
953 				snooze(15000);
954 			}
955 			break;
956 		}
957 		case M_FINISH_SLIDING_CONTROLS:
958 		{
959 			float offset;
960 			bool show;
961 			if (msg->FindFloat("offset", &offset) == B_OK
962 				&& msg->FindBool("show", &show) == B_OK) {
963 				if (show) {
964 					fControls->MoveTo(fControls->Frame().left, offset);
965 					fVideoView->SetSubTitleMaxBottom(offset - 1);
966 				} else {
967 					fVideoView->SetSubTitleMaxBottom(
968 						fVideoView->Bounds().bottom);
969 					fControls->RemoveSelf();
970 					fControls->MoveTo(fVideoView->Frame().left,
971 						fVideoView->Frame().bottom + 1);
972 					fBackground->AddChild(fControls);
973 					fControls->SetSymbolScale(1.0f);
974 					while (!fControls->IsHidden())
975 						fControls->Hide();
976 				}
977 			}
978 			break;
979 		}
980 		case M_HIDE_FULL_SCREEN_CONTROLS:
981 			if (fIsFullscreen) {
982 				BPoint videoViewWhere;
983 				if (msg->FindPoint("where", &videoViewWhere) == B_OK) {
984 					if (!fControls->Frame().Contains(videoViewWhere)) {
985 						_ShowFullscreenControls(false);
986 						// hide the mouse cursor until the user moves it
987 						be_app->ObscureCursor();
988 					}
989 				}
990 			}
991 			break;
992 
993 		case M_SET_RATING:
994 		{
995 			int32 rating;
996 			if (msg->FindInt32("rating", &rating) == B_OK)
997 				_SetRating(rating);
998 			break;
999 		}
1000 
1001 		default:
1002 			if (msg->what >= M_SELECT_AUDIO_TRACK
1003 				&& msg->what <= M_SELECT_AUDIO_TRACK_END) {
1004 				fController->SelectAudioTrack(msg->what - M_SELECT_AUDIO_TRACK);
1005 				break;
1006 			}
1007 			if (msg->what >= M_SELECT_VIDEO_TRACK
1008 				&& msg->what <= M_SELECT_VIDEO_TRACK_END) {
1009 				fController->SelectVideoTrack(msg->what - M_SELECT_VIDEO_TRACK);
1010 				break;
1011 			}
1012 			if ((int32)msg->what >= M_SELECT_SUB_TITLE_TRACK - 1
1013 				&& msg->what <= M_SELECT_SUB_TITLE_TRACK_END) {
1014 				fController->SelectSubTitleTrack((int32)msg->what
1015 					- M_SELECT_SUB_TITLE_TRACK);
1016 				break;
1017 			}
1018 			// let BWindow handle the rest
1019 			BWindow::MessageReceived(msg);
1020 	}
1021 }
1022 
1023 
1024 void
1025 MainWin::WindowActivated(bool active)
1026 {
1027 	fController->PlayerActivated(active);
1028 }
1029 
1030 
1031 bool
1032 MainWin::QuitRequested()
1033 {
1034 	BMessage message(M_PLAYER_QUIT);
1035 	GetQuitMessage(&message);
1036 	be_app->PostMessage(&message);
1037 	return true;
1038 }
1039 
1040 
1041 void
1042 MainWin::MenusBeginning()
1043 {
1044 	_SetupVideoAspectItems(fVideoAspectMenu);
1045 }
1046 
1047 
1048 // #pragma mark -
1049 
1050 
1051 void
1052 MainWin::OpenPlaylist(const BMessage* playlistArchive)
1053 {
1054 	if (playlistArchive == NULL)
1055 		return;
1056 
1057 	BAutolock _(this);
1058 	BAutolock playlistLocker(fPlaylist);
1059 
1060 	if (fPlaylist->Unarchive(playlistArchive) != B_OK)
1061 		return;
1062 
1063 	int32 currentIndex;
1064 	if (playlistArchive->FindInt32("index", &currentIndex) != B_OK)
1065 		currentIndex = 0;
1066 	fPlaylist->SetCurrentItemIndex(currentIndex);
1067 
1068 	playlistLocker.Unlock();
1069 
1070 	if (currentIndex != -1) {
1071 		// Restore the current play position only if we have something to play
1072 		playlistArchive->FindInt64("position", (int64*)&fInitialSeekPosition);
1073 	}
1074 
1075 	if (IsHidden())
1076 		Show();
1077 }
1078 
1079 
1080 void
1081 MainWin::OpenPlaylistItem(const PlaylistItemRef& item)
1082 {
1083 	status_t ret = fController->SetToAsync(item);
1084 	if (ret != B_OK) {
1085 		fprintf(stderr, "MainWin::OpenPlaylistItem() - Failed to send message "
1086 			"to Controller.\n");
1087 		BString message = B_TRANSLATE("%app% encountered an internal error. "
1088 			"The file could not be opened.");
1089 		message.ReplaceFirst("%app%", kApplicationName);
1090 		(new BAlert(kApplicationName, message.String(),
1091 			B_TRANSLATE("OK")))->Go();
1092 		_PlaylistItemOpened(item, ret);
1093 	} else {
1094 		BString string;
1095 		string << "Opening '" << item->Name() << "'.";
1096 		fControls->SetDisabledString(string.String());
1097 
1098 		if (IsHidden()) {
1099 			BMessage showMessage(M_SHOW_IF_NEEDED);
1100 			BMessageRunner::StartSending(BMessenger(this), &showMessage,
1101 				150000, 1);
1102 		}
1103 	}
1104 }
1105 
1106 
1107 void
1108 MainWin::ShowFileInfo()
1109 {
1110 	if (!fInfoWin)
1111 		fInfoWin = new InfoWin(Frame().LeftTop(), fController);
1112 
1113 	if (fInfoWin->Lock()) {
1114 		if (fInfoWin->IsHidden())
1115 			fInfoWin->Show();
1116 		else
1117 			fInfoWin->Activate();
1118 		fInfoWin->Unlock();
1119 	}
1120 }
1121 
1122 
1123 void
1124 MainWin::ShowPlaylistWindow()
1125 {
1126 	if (fPlaylistWindow->Lock()) {
1127 		// make sure the window shows on the same workspace as ourself
1128 		uint32 workspaces = Workspaces();
1129 		if (fPlaylistWindow->Workspaces() != workspaces)
1130 			fPlaylistWindow->SetWorkspaces(workspaces);
1131 
1132 		// show or activate
1133 		if (fPlaylistWindow->IsHidden())
1134 			fPlaylistWindow->Show();
1135 		else
1136 			fPlaylistWindow->Activate();
1137 
1138 		fPlaylistWindow->Unlock();
1139 	}
1140 }
1141 
1142 
1143 void
1144 MainWin::VideoAspectChange(int forcedWidth, int forcedHeight, float widthScale)
1145 {
1146 	// Force specific source size and pixel width scale.
1147 	if (fHasVideo) {
1148 		int width;
1149 		int height;
1150 		fController->GetSize(&width, &height);
1151 		VideoFormatChange(forcedWidth, forcedHeight,
1152 			lround(width * widthScale), height);
1153 	}
1154 }
1155 
1156 
1157 void
1158 MainWin::VideoAspectChange(float widthScale)
1159 {
1160 	// Called when video aspect ratio changes and the original
1161 	// width/height should be restored too, display aspect is not known,
1162 	// only pixel width scale.
1163 	if (fHasVideo) {
1164 		int width;
1165 		int height;
1166 		fController->GetSize(&width, &height);
1167 		VideoFormatChange(width, height, lround(width * widthScale), height);
1168 	}
1169 }
1170 
1171 
1172 void
1173 MainWin::VideoAspectChange(int widthAspect, int heightAspect)
1174 {
1175 	// Called when video aspect ratio changes and the original
1176 	// width/height should be restored too.
1177 	if (fHasVideo) {
1178 		int width;
1179 		int height;
1180 		fController->GetSize(&width, &height);
1181 		VideoFormatChange(width, height, widthAspect, heightAspect);
1182 	}
1183 }
1184 
1185 
1186 void
1187 MainWin::VideoFormatChange(int width, int height, int widthAspect,
1188 	int heightAspect)
1189 {
1190 	// Called when video format or aspect ratio changes.
1191 
1192 	printf("VideoFormatChange enter: width %d, height %d, "
1193 		"aspect ratio: %d:%d\n", width, height, widthAspect, heightAspect);
1194 
1195 	// remember current view scale
1196 	int percent = _CurrentVideoSizeInPercent();
1197 
1198  	fSourceWidth = width;
1199  	fSourceHeight = height;
1200  	fWidthAspect = widthAspect;
1201  	fHeightAspect = heightAspect;
1202 
1203 	if (percent == 100)
1204 		_ResizeWindow(100);
1205 	else
1206 	 	FrameResized(Bounds().Width(), Bounds().Height());
1207 
1208 	printf("VideoFormatChange leave\n");
1209 }
1210 
1211 
1212 void
1213 MainWin::GetQuitMessage(BMessage* message)
1214 {
1215 	message->AddPointer("instance", this);
1216 	message->AddRect("window frame", Frame());
1217 	message->AddBool("audio only", !fHasVideo);
1218 	message->AddInt64("creation time", fCreationTime);
1219 
1220 	if (!fHasVideo && fHasAudio) {
1221 		// store playlist, current index and position if this is audio
1222 		BMessage playlistArchive;
1223 
1224 		BAutolock controllerLocker(fController);
1225 		playlistArchive.AddInt64("position", fController->TimePosition());
1226 		controllerLocker.Unlock();
1227 
1228 		if (!fPlaylist)
1229 			return;
1230 
1231 		BAutolock playlistLocker(fPlaylist);
1232 		if (fPlaylist->Archive(&playlistArchive) != B_OK
1233 			|| playlistArchive.AddInt32("index",
1234 				fPlaylist->CurrentItemIndex()) != B_OK
1235 			|| message->AddMessage("playlist", &playlistArchive) != B_OK) {
1236 			fprintf(stderr, "Failed to store current playlist.\n");
1237 		}
1238 	}
1239 }
1240 
1241 
1242 BHandler*
1243 MainWin::ResolveSpecifier(BMessage* message, int32 index, BMessage* specifier,
1244 	int32 what, const char* property)
1245 {
1246 	BPropertyInfo propertyInfo(sPropertyInfo);
1247 	switch (propertyInfo.FindMatch(message, index, specifier, what, property)) {
1248 		case 0:
1249 		case 1:
1250 		case 2:
1251 		case 3:
1252 		case 4:
1253 		case 5:
1254 		case 6:
1255 		case 7:
1256 		case 8:
1257 			return this;
1258 	}
1259 
1260 	return BWindow::ResolveSpecifier(message, index, specifier, what, property);
1261 }
1262 
1263 
1264 status_t
1265 MainWin::GetSupportedSuites(BMessage* data)
1266 {
1267 	if (data == NULL)
1268 		return B_BAD_VALUE;
1269 
1270 	status_t status = data->AddString("suites", "suite/vnd.Haiku-MediaPlayer");
1271 	if (status != B_OK)
1272 		return status;
1273 
1274 	BPropertyInfo propertyInfo(sPropertyInfo);
1275 	status = data->AddFlat("messages", &propertyInfo);
1276 	if (status != B_OK)
1277 		return status;
1278 
1279 	return BWindow::GetSupportedSuites(data);
1280 }
1281 
1282 
1283 // #pragma mark -
1284 
1285 
1286 void
1287 MainWin::_RefsReceived(BMessage* message)
1288 {
1289 	// the playlist is replaced by dropped files
1290 	// or the dropped files are appended to the end
1291 	// of the existing playlist if <shift> is pressed
1292 	bool append = false;
1293 	if (message->FindBool("append to playlist", &append) != B_OK)
1294 		append = modifiers() & B_SHIFT_KEY;
1295 
1296 	BAutolock _(fPlaylist);
1297 	int32 appendIndex = append ? APPEND_INDEX_APPEND_LAST
1298 		: APPEND_INDEX_REPLACE_PLAYLIST;
1299 	message->AddInt32("append_index", appendIndex);
1300 
1301 	// forward the message to the playlist window,
1302 	// so that undo/redo is used for modifying the playlist
1303 	fPlaylistWindow->PostMessage(message);
1304 
1305 	if (message->FindRect("window frame", &fNoVideoFrame) != B_OK)
1306 		fNoVideoFrame = BRect();
1307 	_ShowIfNeeded();
1308 }
1309 
1310 
1311 void
1312 MainWin::_PlaylistItemOpened(const PlaylistItemRef& item, status_t result)
1313 {
1314 	if (result != B_OK) {
1315 		BAutolock _(fPlaylist);
1316 
1317 		item->SetPlaybackFailed();
1318 		bool allItemsFailed = true;
1319 		int32 count = fPlaylist->CountItems();
1320 		for (int32 i = 0; i < count; i++) {
1321 			if (!fPlaylist->ItemAtFast(i)->PlaybackFailed()) {
1322 				allItemsFailed = false;
1323 				break;
1324 			}
1325 		}
1326 
1327 		if (allItemsFailed) {
1328 			// Display error if all files failed to play.
1329 			BString message(B_TRANSLATE(
1330 				"The file'%filename' could not be opened.\n\n"));;
1331 			message.ReplaceAll("%filename", item->Name());
1332 
1333 			if (result == B_MEDIA_NO_HANDLER) {
1334 				// give a more detailed message for the most likely of all
1335 				// errors
1336 				message << B_TRANSLATE(
1337 					"There is no decoder installed to handle the "
1338 					"file format, or the decoder has trouble with the "
1339 					"specific version of the format.");
1340 			} else {
1341 				message << B_TRANSLATE("Error: ") << strerror(result);
1342 			}
1343 			(new BAlert("error", message.String(), B_TRANSLATE("OK")))->Go();
1344 			fControls->SetDisabledString(kDisabledSeekMessage);
1345 		} else {
1346 			// Just go to the next file and don't bother user (yet)
1347 			fPlaylist->SetCurrentItemIndex(fPlaylist->CurrentItemIndex() + 1);
1348 		}
1349 
1350 		fHasFile = false;
1351 		fHasVideo = false;
1352 		fHasAudio = false;
1353 		SetTitle(kApplicationName);
1354 	} else {
1355 		fHasFile = true;
1356 		fHasVideo = fController->VideoTrackCount() != 0;
1357 		fHasAudio = fController->AudioTrackCount() != 0;
1358 		SetTitle(item->Name().String());
1359 
1360 		if (fInitialSeekPosition < 0) {
1361 			fInitialSeekPosition
1362 				= fController->TimeDuration() + fInitialSeekPosition;
1363 		}
1364 		fController->SetTimePosition(fInitialSeekPosition);
1365 		fInitialSeekPosition = 0;
1366 	}
1367 	_SetupWindow();
1368 
1369 	if (result == B_OK)
1370 		_UpdatePlaylistItemFile();
1371 }
1372 
1373 
1374 void
1375 MainWin::_SetupWindow()
1376 {
1377 //	printf("MainWin::_SetupWindow\n");
1378 	// Populate the track menus
1379 	_SetupTrackMenus(fAudioTrackMenu, fVideoTrackMenu, fSubTitleTrackMenu);
1380 	_UpdateAudioChannelCount(fController->CurrentAudioTrack());
1381 
1382 	fVideoMenu->SetEnabled(fHasVideo);
1383 	fAudioMenu->SetEnabled(fHasAudio);
1384 	int previousSourceWidth = fSourceWidth;
1385 	int previousSourceHeight = fSourceHeight;
1386 	int previousWidthAspect = fWidthAspect;
1387 	int previousHeightAspect = fHeightAspect;
1388 	if (fHasVideo) {
1389 		fController->GetSize(&fSourceWidth, &fSourceHeight,
1390 			&fWidthAspect, &fHeightAspect);
1391 	} else {
1392 		fSourceWidth = 0;
1393 		fSourceHeight = 0;
1394 		fWidthAspect = 1;
1395 		fHeightAspect = 1;
1396 	}
1397 	_UpdateControlsEnabledStatus();
1398 
1399 	_ShowIfNeeded();
1400 
1401 	// Adopt the size and window layout if necessary
1402 	if (previousSourceWidth != fSourceWidth
1403 		|| previousSourceHeight != fSourceHeight
1404 		|| previousWidthAspect != fWidthAspect
1405 		|| previousHeightAspect != fHeightAspect) {
1406 
1407 		_SetWindowSizeLimits();
1408 
1409 		if (!fIsFullscreen) {
1410 			// Resize to 100% but stay on screen
1411 			_ResizeWindow(100, !fHasVideo, true);
1412 		} else {
1413 			// Make sure we relayout the video view when in full screen mode
1414 			FrameResized(Frame().Width(), Frame().Height());
1415 		}
1416 	}
1417 
1418 	fVideoView->MakeFocus();
1419 }
1420 
1421 
1422 void
1423 MainWin::_CreateMenu()
1424 {
1425 	fFileMenu = new BMenu(kApplicationName);
1426 	fPlaylistMenu = new BMenu(B_TRANSLATE("Playlist"B_UTF8_ELLIPSIS));
1427 	fAudioMenu = new BMenu(B_TRANSLATE("Audio"));
1428 	fVideoMenu = new BMenu(B_TRANSLATE("Video"));
1429 	fVideoAspectMenu = new BMenu(B_TRANSLATE("Aspect ratio"));
1430 	fAudioTrackMenu = new BMenu(B_TRANSLATE_WITH_CONTEXT("Track",
1431 		"Audio Track Menu"));
1432 	fVideoTrackMenu = new BMenu(B_TRANSLATE_WITH_CONTEXT("Track",
1433 		"Video Track Menu"));
1434 	fSubTitleTrackMenu = new BMenu(B_TRANSLATE("Subtitles"));
1435 	fAttributesMenu = new BMenu(B_TRANSLATE("Attributes"));
1436 
1437 	fMenuBar->AddItem(fFileMenu);
1438 	fMenuBar->AddItem(fAudioMenu);
1439 	fMenuBar->AddItem(fVideoMenu);
1440 	fMenuBar->AddItem(fAttributesMenu);
1441 
1442 	BMenuItem* item = new BMenuItem(B_TRANSLATE("New player"B_UTF8_ELLIPSIS),
1443 		new BMessage(M_NEW_PLAYER), 'N');
1444 	fFileMenu->AddItem(item);
1445 	item->SetTarget(be_app);
1446 
1447 	// Add recent files to "Open File" entry as sub-menu.
1448 	BRecentFilesList recentFiles(10, false, NULL, kAppSig);
1449 	item = new BMenuItem(recentFiles.NewFileListMenu(
1450 		B_TRANSLATE("Open file"B_UTF8_ELLIPSIS), NULL, NULL, this, 10, true,
1451 		NULL, kAppSig), new BMessage(M_FILE_OPEN));
1452 	item->SetShortcut('O', 0);
1453 	fFileMenu->AddItem(item);
1454 
1455 	fFileMenu->AddSeparatorItem();
1456 
1457 	fFileMenu->AddItem(new BMenuItem(B_TRANSLATE("File info"B_UTF8_ELLIPSIS),
1458 		new BMessage(M_FILE_INFO), 'I'));
1459 	fFileMenu->AddItem(fPlaylistMenu);
1460 	fPlaylistMenu->Superitem()->SetShortcut('P', B_COMMAND_KEY);
1461 	fPlaylistMenu->Superitem()->SetMessage(new BMessage(M_FILE_PLAYLIST));
1462 
1463 	fFileMenu->AddSeparatorItem();
1464 
1465 	fNoInterfaceMenuItem = new BMenuItem(B_TRANSLATE("Hide interface"),
1466 		new BMessage(M_TOGGLE_NO_INTERFACE), 'H');
1467 	fFileMenu->AddItem(fNoInterfaceMenuItem);
1468 	fFileMenu->AddItem(new BMenuItem(B_TRANSLATE("Always on top"),
1469 		new BMessage(M_TOGGLE_ALWAYS_ON_TOP), 'A'));
1470 
1471 	item = new BMenuItem(B_TRANSLATE("Settings"B_UTF8_ELLIPSIS),
1472 		new BMessage(M_SETTINGS), 'S');
1473 	fFileMenu->AddItem(item);
1474 	item->SetTarget(be_app);
1475 
1476 	fFileMenu->AddSeparatorItem();
1477 
1478 	fFileMenu->AddItem(new BMenuItem(B_TRANSLATE("Close"),
1479 		new BMessage(M_FILE_CLOSE), 'W'));
1480 	fFileMenu->AddItem(new BMenuItem(B_TRANSLATE("Quit"),
1481 		new BMessage(M_FILE_QUIT), 'Q'));
1482 
1483 	fPlaylistMenu->SetRadioMode(true);
1484 
1485 	fAudioMenu->AddItem(fAudioTrackMenu);
1486 
1487 	fVideoMenu->AddItem(fVideoTrackMenu);
1488 	fVideoMenu->AddItem(fSubTitleTrackMenu);
1489 	fVideoMenu->AddSeparatorItem();
1490 	BMessage* resizeMessage = new BMessage(M_VIEW_SIZE);
1491 	resizeMessage->AddInt32("size", 50);
1492 	fVideoMenu->AddItem(new BMenuItem(
1493 		B_TRANSLATE("50% scale"), resizeMessage, '0'));
1494 
1495 	resizeMessage = new BMessage(M_VIEW_SIZE);
1496 	resizeMessage->AddInt32("size", 100);
1497 	fVideoMenu->AddItem(new BMenuItem(
1498 		B_TRANSLATE("100% scale"), resizeMessage, '1'));
1499 
1500 	resizeMessage = new BMessage(M_VIEW_SIZE);
1501 	resizeMessage->AddInt32("size", 200);
1502 	fVideoMenu->AddItem(new BMenuItem(
1503 		B_TRANSLATE("200% scale"), resizeMessage, '2'));
1504 
1505 	resizeMessage = new BMessage(M_VIEW_SIZE);
1506 	resizeMessage->AddInt32("size", 300);
1507 	fVideoMenu->AddItem(new BMenuItem(
1508 		B_TRANSLATE("300% scale"), resizeMessage, '3'));
1509 
1510 	resizeMessage = new BMessage(M_VIEW_SIZE);
1511 	resizeMessage->AddInt32("size", 400);
1512 	fVideoMenu->AddItem(new BMenuItem(
1513 		B_TRANSLATE("400% scale"), resizeMessage, '4'));
1514 
1515 	fVideoMenu->AddSeparatorItem();
1516 
1517 	fVideoMenu->AddItem(new BMenuItem(B_TRANSLATE("Full screen"),
1518 		new BMessage(M_TOGGLE_FULLSCREEN), B_ENTER));
1519 
1520 	fVideoMenu->AddSeparatorItem();
1521 
1522 	_SetupVideoAspectItems(fVideoAspectMenu);
1523 	fVideoMenu->AddItem(fVideoAspectMenu);
1524 
1525 	fRatingMenu = new BMenu(B_TRANSLATE("Rating"));
1526 	fAttributesMenu->AddItem(fRatingMenu);
1527 	for (int32 i = 1; i <= 10; i++) {
1528 		char label[16];
1529 		snprintf(label, sizeof(label), "%ld", i);
1530 		BMessage* setRatingMsg = new BMessage(M_SET_RATING);
1531 		setRatingMsg->AddInt32("rating", i);
1532 		fRatingMenu->AddItem(new BMenuItem(label, setRatingMsg));
1533 	}
1534 }
1535 
1536 
1537 void
1538 MainWin::_SetupVideoAspectItems(BMenu* menu)
1539 {
1540 	BMenuItem* item;
1541 	while ((item = menu->RemoveItem(0L)) != NULL)
1542 		delete item;
1543 
1544 	int width;
1545 	int height;
1546 	int widthAspect;
1547 	int heightAspect;
1548 	fController->GetSize(&width, &height, &widthAspect, &heightAspect);
1549 		// We don't care if there is a video track at all. In that
1550 		// case we should end up not marking any item.
1551 
1552 	// NOTE: The item marking may end up marking for example both
1553 	// "Stream Settings" and "16 : 9" if the stream settings happen to
1554 	// be "16 : 9".
1555 
1556 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("Stream settings"),
1557 		new BMessage(M_ASPECT_SAME_AS_SOURCE)));
1558 	item->SetMarked(widthAspect == fWidthAspect
1559 		&& heightAspect == fHeightAspect);
1560 
1561 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("No aspect correction"),
1562 		new BMessage(M_ASPECT_NO_DISTORTION)));
1563 	item->SetMarked(width == fWidthAspect && height == fHeightAspect);
1564 
1565 	menu->AddSeparatorItem();
1566 
1567 	menu->AddItem(item = new BMenuItem("4 : 3",
1568 		new BMessage(M_ASPECT_4_3)));
1569 	item->SetMarked(fWidthAspect == 4 && fHeightAspect == 3);
1570 	menu->AddItem(item = new BMenuItem("16 : 9",
1571 		new BMessage(M_ASPECT_16_9)));
1572 	item->SetMarked(fWidthAspect == 16 && fHeightAspect == 9);
1573 
1574 	menu->AddSeparatorItem();
1575 
1576 	menu->AddItem(item = new BMenuItem("1.66 : 1",
1577 		new BMessage(M_ASPECT_83_50)));
1578 	item->SetMarked(fWidthAspect == 83 && fHeightAspect == 50);
1579 	menu->AddItem(item = new BMenuItem("1.75 : 1",
1580 		new BMessage(M_ASPECT_7_4)));
1581 	item->SetMarked(fWidthAspect == 7 && fHeightAspect == 4);
1582 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("1.85 : 1 (American)"),
1583 		new BMessage(M_ASPECT_37_20)));
1584 	item->SetMarked(fWidthAspect == 37 && fHeightAspect == 20);
1585 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("2.35 : 1 (Cinemascope)"),
1586 		new BMessage(M_ASPECT_47_20)));
1587 	item->SetMarked(fWidthAspect == 47 && fHeightAspect == 20);
1588 }
1589 
1590 
1591 void
1592 MainWin::_SetupTrackMenus(BMenu* audioTrackMenu, BMenu* videoTrackMenu,
1593 	BMenu* subTitleTrackMenu)
1594 {
1595 	audioTrackMenu->RemoveItems(0, audioTrackMenu->CountItems(), true);
1596 	videoTrackMenu->RemoveItems(0, videoTrackMenu->CountItems(), true);
1597 	subTitleTrackMenu->RemoveItems(0, subTitleTrackMenu->CountItems(), true);
1598 
1599 	char s[100];
1600 
1601 	int count = fController->AudioTrackCount();
1602 	int current = fController->CurrentAudioTrack();
1603 	for (int i = 0; i < count; i++) {
1604 		BMessage metaData;
1605 		const char* languageString = NULL;
1606 		if (fController->GetAudioMetaData(i, &metaData) == B_OK)
1607 			metaData.FindString("language", &languageString);
1608 		if (languageString != NULL) {
1609 			BLanguage language(languageString);
1610 			BString languageName;
1611 			if (language.GetName(languageName) == B_OK)
1612 				languageString = languageName.String();
1613 			snprintf(s, sizeof(s), "%s", languageString);
1614 		} else
1615 			snprintf(s, sizeof(s), B_TRANSLATE("Track %d"), i + 1);
1616 		BMenuItem* item = new BMenuItem(s,
1617 			new BMessage(M_SELECT_AUDIO_TRACK + i));
1618 		item->SetMarked(i == current);
1619 		audioTrackMenu->AddItem(item);
1620 	}
1621 	if (count == 0) {
1622 		audioTrackMenu->AddItem(new BMenuItem(B_TRANSLATE_WITH_CONTEXT("none",
1623 			"Audio track menu"), new BMessage(M_DUMMY)));
1624 		audioTrackMenu->ItemAt(0)->SetMarked(true);
1625 	}
1626 
1627 
1628 	count = fController->VideoTrackCount();
1629 	current = fController->CurrentVideoTrack();
1630 	for (int i = 0; i < count; i++) {
1631 		snprintf(s, sizeof(s), B_TRANSLATE("Track %d"), i + 1);
1632 		BMenuItem* item = new BMenuItem(s,
1633 			new BMessage(M_SELECT_VIDEO_TRACK + i));
1634 		item->SetMarked(i == current);
1635 		videoTrackMenu->AddItem(item);
1636 	}
1637 	if (count == 0) {
1638 		videoTrackMenu->AddItem(new BMenuItem("none", new BMessage(M_DUMMY)));
1639 		videoTrackMenu->ItemAt(0)->SetMarked(true);
1640 	}
1641 
1642 	count = fController->SubTitleTrackCount();
1643 	if (count > 0) {
1644 		current = fController->CurrentSubTitleTrack();
1645 		BMenuItem* item = new BMenuItem(
1646 			B_TRANSLATE_WITH_CONTEXT("Off", "Subtitles menu"),
1647 			new BMessage(M_SELECT_SUB_TITLE_TRACK - 1));
1648 		subTitleTrackMenu->AddItem(item);
1649 		item->SetMarked(current == -1);
1650 
1651 		subTitleTrackMenu->AddSeparatorItem();
1652 
1653 		for (int i = 0; i < count; i++) {
1654 			const char* name = fController->SubTitleTrackName(i);
1655 			if (name != NULL)
1656 				snprintf(s, sizeof(s), "%s", name);
1657 			else
1658 				snprintf(s, sizeof(s), B_TRANSLATE("Track %d"), i + 1);
1659 			item = new BMenuItem(s,
1660 				new BMessage(M_SELECT_SUB_TITLE_TRACK + i));
1661 			item->SetMarked(i == current);
1662 			subTitleTrackMenu->AddItem(item);
1663 		}
1664 	} else {
1665 		subTitleTrackMenu->AddItem(new BMenuItem(
1666 			B_TRANSLATE_WITH_CONTEXT("none", "Subtitles menu"),
1667 			new BMessage(M_DUMMY)));
1668 		subTitleTrackMenu->ItemAt(0)->SetMarked(true);
1669 	}
1670 }
1671 
1672 
1673 void
1674 MainWin::_UpdateAudioChannelCount(int32 audioTrackIndex)
1675 {
1676 	fControls->SetAudioChannelCount(fController->AudioTrackChannelCount());
1677 }
1678 
1679 
1680 void
1681 MainWin::_GetMinimumWindowSize(int& width, int& height) const
1682 {
1683 	width = MIN_WIDTH;
1684 	height = 0;
1685 	if (!fNoInterface) {
1686 		width = max_c(width, fMenuBarWidth);
1687 		width = max_c(width, fControlsWidth);
1688 		height = fMenuBarHeight + fControlsHeight;
1689 	}
1690 }
1691 
1692 
1693 void
1694 MainWin::_GetUnscaledVideoSize(int& videoWidth, int& videoHeight) const
1695 {
1696 	if (fWidthAspect != 0 && fHeightAspect != 0) {
1697 		videoWidth = fSourceHeight * fWidthAspect / fHeightAspect;
1698 		videoHeight = fSourceWidth * fHeightAspect / fWidthAspect;
1699 		// Use the scaling which produces an enlarged view.
1700 		if (videoWidth > fSourceWidth) {
1701 			// Enlarge width
1702 			videoHeight = fSourceHeight;
1703 		} else {
1704 			// Enlarge height
1705 			videoWidth = fSourceWidth;
1706 		}
1707 	} else {
1708 		videoWidth = fSourceWidth;
1709 		videoHeight = fSourceHeight;
1710 	}
1711 }
1712 
1713 
1714 void
1715 MainWin::_SetWindowSizeLimits()
1716 {
1717 	int minWidth;
1718 	int minHeight;
1719 	_GetMinimumWindowSize(minWidth, minHeight);
1720 	SetSizeLimits(minWidth - 1, 32000, minHeight - 1,
1721 		fHasVideo ? 32000 : minHeight - 1);
1722 }
1723 
1724 
1725 int
1726 MainWin::_CurrentVideoSizeInPercent() const
1727 {
1728 	if (!fHasVideo)
1729 		return 0;
1730 
1731 	int videoWidth;
1732 	int videoHeight;
1733 	_GetUnscaledVideoSize(videoWidth, videoHeight);
1734 
1735 	int viewWidth = fVideoView->Bounds().IntegerWidth() + 1;
1736 	int viewHeight = fVideoView->Bounds().IntegerHeight() + 1;
1737 
1738 	int widthPercent = viewWidth * 100 / videoWidth;
1739 	int heightPercent = viewHeight * 100 / videoHeight;
1740 
1741 	if (widthPercent > heightPercent)
1742 		return widthPercent;
1743 	return heightPercent;
1744 }
1745 
1746 
1747 void
1748 MainWin::_ZoomVideoView(int percentDiff)
1749 {
1750 	if (!fHasVideo)
1751 		return;
1752 
1753 	int percent = _CurrentVideoSizeInPercent();
1754 	int newSize = percent * (100 + percentDiff) / 100;
1755 
1756 	if (newSize < 25)
1757 		newSize = 25;
1758 	if (newSize > 400)
1759 		newSize = 400;
1760 	if (newSize != percent) {
1761 		BMessage message(M_VIEW_SIZE);
1762 		message.AddInt32("size", newSize);
1763 		PostMessage(&message);
1764 	}
1765 }
1766 
1767 
1768 void
1769 MainWin::_ResizeWindow(int percent, bool useNoVideoWidth, bool stayOnScreen)
1770 {
1771 	// Get required window size
1772 	int videoWidth;
1773 	int videoHeight;
1774 	_GetUnscaledVideoSize(videoWidth, videoHeight);
1775 
1776 	videoWidth = (videoWidth * percent) / 100;
1777 	videoHeight = (videoHeight * percent) / 100;
1778 
1779 	// Calculate and set the minimum window size
1780 	int width;
1781 	int height;
1782 	_GetMinimumWindowSize(width, height);
1783 
1784 	width = max_c(width, videoWidth) - 1;
1785 	if (useNoVideoWidth)
1786 		width = max_c(width, fNoVideoWidth);
1787 	height = height + videoHeight - 1;
1788 
1789 	if (stayOnScreen) {
1790 		BRect screenFrame(BScreen(this).Frame());
1791 		BRect frame(Frame());
1792 		BRect decoratorFrame(DecoratorFrame());
1793 
1794 		// Shrink the screen frame by the window border size
1795 		screenFrame.top += frame.top - decoratorFrame.top;
1796 		screenFrame.left += frame.left - decoratorFrame.left;
1797 		screenFrame.right += frame.right - decoratorFrame.right;
1798 		screenFrame.bottom += frame.bottom - decoratorFrame.bottom;
1799 
1800 		// Update frame to what the new size would be
1801 		frame.right = frame.left + width;
1802 		frame.bottom = frame.top + height;
1803 
1804 		if (!screenFrame.Contains(frame)) {
1805 			// Resize the window so it doesn't extend outside the current
1806 			// screen frame.
1807 			if (frame.Width() > screenFrame.Width()
1808 				|| frame.Height() > screenFrame.Height()) {
1809 				// too large
1810 				int widthDiff
1811 					= frame.IntegerWidth() - screenFrame.IntegerWidth();
1812 				int heightDiff
1813 					= frame.IntegerHeight() - screenFrame.IntegerHeight();
1814 
1815 				float shrinkScale;
1816 				if (widthDiff > heightDiff)
1817 					shrinkScale = (float)(width - widthDiff) / width;
1818 				else
1819 					shrinkScale = (float)(height - heightDiff) / height;
1820 
1821 				// Resize width/height and center window
1822 				width = lround(width * shrinkScale);
1823 				height = lround(height * shrinkScale);
1824 				MoveTo((screenFrame.left + screenFrame.right - width) / 2,
1825 					(screenFrame.top + screenFrame.bottom - height) / 2);
1826 			} else {
1827 				// just off-screen on one or more sides
1828 				int offsetX = 0;
1829 				int offsetY = 0;
1830 				if (frame.left < screenFrame.left)
1831 					offsetX = (int)(screenFrame.left - frame.left);
1832 				else if (frame.right > screenFrame.right)
1833 					offsetX = (int)(screenFrame.right - frame.right);
1834 				if (frame.top < screenFrame.top)
1835 					offsetY = (int)(screenFrame.top - frame.top);
1836 				else if (frame.bottom > screenFrame.bottom)
1837 					offsetY = (int)(screenFrame.bottom - frame.bottom);
1838 				MoveBy(offsetX, offsetY);
1839 			}
1840 		}
1841 	}
1842 
1843 	ResizeTo(width, height);
1844 }
1845 
1846 
1847 void
1848 MainWin::_ResizeVideoView(int x, int y, int width, int height)
1849 {
1850 	// Keep aspect ratio, place video view inside
1851 	// the background area (may create black bars).
1852 	int videoWidth;
1853 	int videoHeight;
1854 	_GetUnscaledVideoSize(videoWidth, videoHeight);
1855 	float scaledWidth  = videoWidth;
1856 	float scaledHeight = videoHeight;
1857 	float factor = min_c(width / scaledWidth, height / scaledHeight);
1858 	int renderWidth = lround(scaledWidth * factor);
1859 	int renderHeight = lround(scaledHeight * factor);
1860 	if (renderWidth > width)
1861 		renderWidth = width;
1862 	if (renderHeight > height)
1863 		renderHeight = height;
1864 
1865 	int xOffset = (width - renderWidth) / 2;
1866 	int yOffset = (height - renderHeight) / 2;
1867 
1868 	fVideoView->MoveTo(x, y);
1869 	fVideoView->ResizeTo(width - 1, height - 1);
1870 
1871 	BRect videoFrame(xOffset, yOffset,
1872 		xOffset + renderWidth - 1, yOffset + renderHeight - 1);
1873 
1874 	fVideoView->SetVideoFrame(videoFrame);
1875 	fVideoView->SetSubTitleMaxBottom(height - 1);
1876 }
1877 
1878 
1879 // #pragma mark -
1880 
1881 
1882 void
1883 MainWin::_MouseDown(BMessage* msg, BView* originalHandler)
1884 {
1885 	uint32 buttons = msg->FindInt32("buttons");
1886 
1887 	// On Zeta, only "screen_where" is reliable, "where" and "be:view_where"
1888 	// seem to be broken
1889 	BPoint screenWhere;
1890 	if (msg->FindPoint("screen_where", &screenWhere) != B_OK) {
1891 		// TODO: remove
1892 		// Workaround for BeOS R5, it has no "screen_where"
1893 		if (!originalHandler || msg->FindPoint("where", &screenWhere) < B_OK)
1894 			return;
1895 		originalHandler->ConvertToScreen(&screenWhere);
1896 	}
1897 
1898 	// double click handling
1899 
1900 	if (msg->FindInt32("clicks") % 2 == 0) {
1901 		BRect rect(screenWhere.x - 1, screenWhere.y - 1, screenWhere.x + 1,
1902 			screenWhere.y + 1);
1903 		if (rect.Contains(fMouseDownMousePos)) {
1904 			if (buttons == B_PRIMARY_MOUSE_BUTTON)
1905 				PostMessage(M_TOGGLE_FULLSCREEN);
1906 			else if (buttons == B_SECONDARY_MOUSE_BUTTON)
1907 				PostMessage(M_TOGGLE_NO_INTERFACE);
1908 
1909 			return;
1910 		}
1911 	}
1912 
1913 	fMouseDownMousePos = screenWhere;
1914 	fMouseDownWindowPos = Frame().LeftTop();
1915 
1916 	if (buttons == B_PRIMARY_MOUSE_BUTTON && !fIsFullscreen) {
1917 		// start mouse tracking
1918 		fVideoView->SetMouseEventMask(B_POINTER_EVENTS | B_NO_POINTER_HISTORY
1919 			/* | B_LOCK_WINDOW_FOCUS */);
1920 		fMouseDownTracking = true;
1921 	}
1922 
1923 	// pop up a context menu if right mouse button is down for 200 ms
1924 
1925 	if ((buttons & B_SECONDARY_MOUSE_BUTTON) == 0)
1926 		return;
1927 
1928 	bigtime_t start = system_time();
1929 	bigtime_t delay = 200000;
1930 	BPoint location;
1931 	do {
1932 		fVideoView->GetMouse(&location, &buttons);
1933 		if ((buttons & B_SECONDARY_MOUSE_BUTTON) == 0)
1934 			break;
1935 		snooze(1000);
1936 	} while (system_time() - start < delay);
1937 
1938 	if ((buttons & B_SECONDARY_MOUSE_BUTTON) != 0)
1939 		_ShowContextMenu(screenWhere);
1940 }
1941 
1942 
1943 void
1944 MainWin::_MouseMoved(BMessage* msg, BView* originalHandler)
1945 {
1946 //	msg->PrintToStream();
1947 
1948 	BPoint mousePos;
1949 	uint32 buttons = msg->FindInt32("buttons");
1950 	// On Zeta, only "screen_where" is reliable, "where"
1951 	// and "be:view_where" seem to be broken
1952 	if (msg->FindPoint("screen_where", &mousePos) != B_OK) {
1953 		// TODO: remove
1954 		// Workaround for BeOS R5, it has no "screen_where"
1955 		if (!originalHandler || msg->FindPoint("where", &mousePos) < B_OK)
1956 			return;
1957 		originalHandler->ConvertToScreen(&mousePos);
1958 	}
1959 
1960 	if (buttons == B_PRIMARY_MOUSE_BUTTON && fMouseDownTracking
1961 		&& !fIsFullscreen) {
1962 //		printf("screen where: %.0f, %.0f => ", mousePos.x, mousePos.y);
1963 		float delta_x = mousePos.x - fMouseDownMousePos.x;
1964 		float delta_y = mousePos.y - fMouseDownMousePos.y;
1965 		float x = fMouseDownWindowPos.x + delta_x;
1966 		float y = fMouseDownWindowPos.y + delta_y;
1967 //		printf("move window to %.0f, %.0f\n", x, y);
1968 		MoveTo(x, y);
1969 	}
1970 
1971 	bigtime_t eventTime;
1972 	if (msg->FindInt64("when", &eventTime) != B_OK)
1973 		eventTime = system_time();
1974 
1975 	if (buttons == 0 && fIsFullscreen) {
1976 		BPoint moveDelta = mousePos - fLastMousePos;
1977 		float moveDeltaDist
1978 			= sqrtf(moveDelta.x * moveDelta.x + moveDelta.y * moveDelta.y);
1979 		if (eventTime - fLastMouseMovedTime < 200000)
1980 			fMouseMoveDist += moveDeltaDist;
1981 		else
1982 			fMouseMoveDist = moveDeltaDist;
1983 		if (fMouseMoveDist > 5)
1984 			_ShowFullscreenControls(true);
1985 	}
1986 
1987 	fLastMousePos = mousePos;
1988 	fLastMouseMovedTime =eventTime;
1989 }
1990 
1991 
1992 void
1993 MainWin::_MouseUp(BMessage* msg)
1994 {
1995 	fMouseDownTracking = false;
1996 }
1997 
1998 
1999 void
2000 MainWin::_ShowContextMenu(const BPoint& screenPoint)
2001 {
2002 	printf("Show context menu\n");
2003 	BPopUpMenu* menu = new BPopUpMenu("context menu", false, false);
2004 	BMenuItem* item;
2005 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("Full screen"),
2006 		new BMessage(M_TOGGLE_FULLSCREEN), B_ENTER));
2007 	item->SetMarked(fIsFullscreen);
2008 	item->SetEnabled(fHasVideo);
2009 
2010 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("Hide interface"),
2011 		new BMessage(M_TOGGLE_NO_INTERFACE), 'H'));
2012 	item->SetMarked(fNoInterface);
2013 	item->SetEnabled(fHasVideo && !fIsFullscreen);
2014 
2015 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("Always on top"),
2016 		new BMessage(M_TOGGLE_ALWAYS_ON_TOP), 'A'));
2017 	item->SetMarked(fAlwaysOnTop);
2018 	item->SetEnabled(fHasVideo);
2019 
2020 	BMenu* aspectSubMenu = new BMenu(B_TRANSLATE("Aspect ratio"));
2021 	_SetupVideoAspectItems(aspectSubMenu);
2022 	aspectSubMenu->SetTargetForItems(this);
2023 	menu->AddItem(item = new BMenuItem(aspectSubMenu));
2024 	item->SetEnabled(fHasVideo);
2025 
2026 	menu->AddSeparatorItem();
2027 
2028 	// Add track selector menus
2029 	BMenu* audioTrackMenu = new BMenu(B_TRANSLATE("Audio track"));
2030 	BMenu* videoTrackMenu = new BMenu(B_TRANSLATE("Video track"));
2031 	BMenu* subTitleTrackMenu = new BMenu(B_TRANSLATE("Subtitles"));
2032 	_SetupTrackMenus(audioTrackMenu, videoTrackMenu, subTitleTrackMenu);
2033 
2034 	audioTrackMenu->SetTargetForItems(this);
2035 	videoTrackMenu->SetTargetForItems(this);
2036 	subTitleTrackMenu->SetTargetForItems(this);
2037 
2038 	menu->AddItem(item = new BMenuItem(audioTrackMenu));
2039 	item->SetEnabled(fHasAudio);
2040 
2041 	menu->AddItem(item = new BMenuItem(videoTrackMenu));
2042 	item->SetEnabled(fHasVideo);
2043 
2044 	menu->AddItem(item = new BMenuItem(subTitleTrackMenu));
2045 	item->SetEnabled(fHasVideo);
2046 
2047 	menu->AddSeparatorItem();
2048 	menu->AddItem(new BMenuItem(B_TRANSLATE("Quit"), new BMessage(M_FILE_QUIT), 'Q'));
2049 
2050 	menu->SetTargetForItems(this);
2051 	BRect rect(screenPoint.x - 5, screenPoint.y - 5, screenPoint.x + 5,
2052 		screenPoint.y + 5);
2053 	menu->Go(screenPoint, true, true, rect, true);
2054 }
2055 
2056 
2057 /*!	Trap keys that are about to be send to background or renderer view.
2058 	Return true if it shouldn't be passed to the view.
2059 */
2060 bool
2061 MainWin::_KeyDown(BMessage* msg)
2062 {
2063 	uint32 key = msg->FindInt32("key");
2064 	uint32 rawChar = msg->FindInt32("raw_char");
2065 	uint32 modifier = msg->FindInt32("modifiers");
2066 
2067 //	printf("key 0x%lx, rawChar 0x%lx, modifiers 0x%lx\n", key, rawChar,
2068 //		modifier);
2069 
2070 	// ignore the system modifier namespace
2071 	if ((modifier & (B_CONTROL_KEY | B_COMMAND_KEY))
2072 			== (B_CONTROL_KEY | B_COMMAND_KEY))
2073 		return false;
2074 
2075 	switch (rawChar) {
2076 		case B_SPACE:
2077 			fController->TogglePlaying();
2078 			return true;
2079 
2080 		case 'm':
2081 			fController->ToggleMute();
2082 			return true;
2083 
2084 		case B_ESCAPE:
2085 			if (!fIsFullscreen)
2086 				break;
2087 
2088 			PostMessage(M_TOGGLE_FULLSCREEN);
2089 			return true;
2090 
2091 		case B_ENTER:		// Enter / Return
2092 			if ((modifier & B_COMMAND_KEY) != 0) {
2093 				PostMessage(M_TOGGLE_FULLSCREEN);
2094 				return true;
2095 			}
2096 			break;
2097 
2098 		case B_TAB:
2099 			if ((modifier & (B_COMMAND_KEY | B_CONTROL_KEY | B_OPTION_KEY
2100 					| B_MENU_KEY)) == 0) {
2101 				PostMessage(M_TOGGLE_FULLSCREEN);
2102 				return true;
2103 			}
2104 			break;
2105 
2106 		case B_UP_ARROW:
2107 			if ((modifier & B_COMMAND_KEY) != 0)
2108 				PostMessage(M_SKIP_NEXT);
2109 			else
2110 				PostMessage(M_VOLUME_UP);
2111 			return true;
2112 
2113 		case B_DOWN_ARROW:
2114 			if ((modifier & B_COMMAND_KEY) != 0)
2115 				PostMessage(M_SKIP_PREV);
2116 			else
2117 				PostMessage(M_VOLUME_DOWN);
2118 			return true;
2119 
2120 		case B_RIGHT_ARROW:
2121 			if ((modifier & B_COMMAND_KEY) != 0)
2122 				PostMessage(M_SKIP_NEXT);
2123 			else if (fAllowWinding) {
2124 				BMessage windMessage(M_WIND);
2125 				if ((modifier & B_SHIFT_KEY) != 0) {
2126 					windMessage.AddInt64("how much", 30000000LL);
2127 					windMessage.AddInt64("frames", 5);
2128 				} else {
2129 					windMessage.AddInt64("how much", 5000000LL);
2130 					windMessage.AddInt64("frames", 1);
2131 				}
2132 				PostMessage(&windMessage);
2133 			}
2134 			return true;
2135 
2136 		case B_LEFT_ARROW:
2137 			if ((modifier & B_COMMAND_KEY) != 0)
2138 				PostMessage(M_SKIP_PREV);
2139 			else if (fAllowWinding) {
2140 				BMessage windMessage(M_WIND);
2141 				if ((modifier & B_SHIFT_KEY) != 0) {
2142 					windMessage.AddInt64("how much", -30000000LL);
2143 					windMessage.AddInt64("frames", -5);
2144 				} else {
2145 					windMessage.AddInt64("how much", -5000000LL);
2146 					windMessage.AddInt64("frames", -1);
2147 				}
2148 				PostMessage(&windMessage);
2149 			}
2150 			return true;
2151 
2152 		case B_PAGE_UP:
2153 			PostMessage(M_SKIP_NEXT);
2154 			return true;
2155 
2156 		case B_PAGE_DOWN:
2157 			PostMessage(M_SKIP_PREV);
2158 			return true;
2159 
2160 		case '+':
2161 			if ((modifier & B_COMMAND_KEY) == 0) {
2162 				_ZoomVideoView(10);
2163 				return true;
2164 			}
2165 			break;
2166 
2167 		case '-':
2168 			if ((modifier & B_COMMAND_KEY) == 0) {
2169 				_ZoomVideoView(-10);
2170 				return true;
2171 			}
2172 			break;
2173 
2174 		case B_DELETE:
2175 		case 'd': 			// d for delete
2176 		case 't':			// t for Trash
2177 			if ((modifiers() & B_COMMAND_KEY) != 0) {
2178 				BAutolock _(fPlaylist);
2179 				BMessage removeMessage(M_PLAYLIST_MOVE_TO_TRASH);
2180 				removeMessage.AddInt32("playlist index",
2181 					fPlaylist->CurrentItemIndex());
2182 				fPlaylistWindow->PostMessage(&removeMessage);
2183 				return true;
2184 			}
2185 			break;
2186 	}
2187 
2188 	switch (key) {
2189 		case 0x3a:  		// numeric keypad +
2190 			if ((modifier & B_COMMAND_KEY) == 0) {
2191 				_ZoomVideoView(10);
2192 				return true;
2193 			}
2194 			break;
2195 
2196 		case 0x25:  		// numeric keypad -
2197 			if ((modifier & B_COMMAND_KEY) == 0) {
2198 				_ZoomVideoView(-10);
2199 				return true;
2200 			}
2201 			break;
2202 
2203 		case 0x38:			// numeric keypad up arrow
2204 			PostMessage(M_VOLUME_UP);
2205 			return true;
2206 
2207 		case 0x59:			// numeric keypad down arrow
2208 			PostMessage(M_VOLUME_DOWN);
2209 			return true;
2210 
2211 		case 0x39:			// numeric keypad page up
2212 		case 0x4a:			// numeric keypad right arrow
2213 			PostMessage(M_SKIP_NEXT);
2214 			return true;
2215 
2216 		case 0x5a:			// numeric keypad page down
2217 		case 0x48:			// numeric keypad left arrow
2218 			PostMessage(M_SKIP_PREV);
2219 			return true;
2220 
2221 		// Playback controls along the bottom of the keyboard:
2222 		// Z X C V B  for US International
2223 		case 0x4c:
2224 			PostMessage(M_SKIP_PREV);
2225 			return true;
2226 		case 0x4d:
2227 			fController->Play();
2228 			return true;
2229 		case 0x4e:
2230 			fController->Pause();
2231 			return true;
2232 		case 0x4f:
2233 			fController->Stop();
2234 			return true;
2235 		case 0x50:
2236 			PostMessage(M_SKIP_NEXT);
2237 			return true;
2238 	}
2239 
2240 	return false;
2241 }
2242 
2243 
2244 // #pragma mark -
2245 
2246 
2247 void
2248 MainWin::_ToggleFullscreen()
2249 {
2250 	printf("_ToggleFullscreen enter\n");
2251 
2252 	if (!fHasVideo) {
2253 		printf("_ToggleFullscreen - ignoring, as we don't have a video\n");
2254 		return;
2255 	}
2256 
2257 	fIsFullscreen = !fIsFullscreen;
2258 
2259 	if (fIsFullscreen) {
2260 		// switch to fullscreen
2261 
2262 		fSavedFrame = Frame();
2263 		printf("saving current frame: %d %d %d %d\n", int(fSavedFrame.left),
2264 			int(fSavedFrame.top), int(fSavedFrame.right),
2265 			int(fSavedFrame.bottom));
2266 		BScreen screen(this);
2267 		BRect rect(screen.Frame());
2268 
2269 		Hide();
2270 		MoveTo(rect.left, rect.top);
2271 		ResizeTo(rect.Width(), rect.Height());
2272 		Show();
2273 
2274 	} else {
2275 		// switch back from full screen mode
2276 		_ShowFullscreenControls(false, false);
2277 
2278 		Hide();
2279 		MoveTo(fSavedFrame.left, fSavedFrame.top);
2280 		ResizeTo(fSavedFrame.Width(), fSavedFrame.Height());
2281 		Show();
2282 	}
2283 
2284 	fVideoView->SetFullscreen(fIsFullscreen);
2285 
2286 	_MarkItem(fFileMenu, M_TOGGLE_FULLSCREEN, fIsFullscreen);
2287 
2288 	printf("_ToggleFullscreen leave\n");
2289 }
2290 
2291 void
2292 MainWin::_ToggleAlwaysOnTop()
2293 {
2294 	fAlwaysOnTop = !fAlwaysOnTop;
2295 	SetFeel(fAlwaysOnTop ? B_FLOATING_ALL_WINDOW_FEEL : B_NORMAL_WINDOW_FEEL);
2296 
2297 	_MarkItem(fFileMenu, M_TOGGLE_ALWAYS_ON_TOP, fAlwaysOnTop);
2298 }
2299 
2300 
2301 void
2302 MainWin::_ToggleNoInterface()
2303 {
2304 	printf("_ToggleNoInterface enter\n");
2305 
2306 	if (fIsFullscreen || !fHasVideo) {
2307 		// Fullscreen playback is always without interface and
2308 		// audio playback is always with interface. So we ignore these
2309 		// two states here.
2310 		printf("_ToggleNoControls leave, doing nothing, we are fullscreen\n");
2311 		return;
2312 	}
2313 
2314 	fNoInterface = !fNoInterface;
2315 	_SetWindowSizeLimits();
2316 
2317 	if (fNoInterface) {
2318 		MoveBy(0, fMenuBarHeight);
2319 		ResizeBy(0, -(fControlsHeight + fMenuBarHeight));
2320 		SetLook(B_BORDERED_WINDOW_LOOK);
2321 	} else {
2322 		MoveBy(0, -fMenuBarHeight);
2323 		ResizeBy(0, fControlsHeight + fMenuBarHeight);
2324 		SetLook(B_TITLED_WINDOW_LOOK);
2325 	}
2326 
2327 	_MarkItem(fFileMenu, M_TOGGLE_NO_INTERFACE, fNoInterface);
2328 
2329 	printf("_ToggleNoInterface leave\n");
2330 }
2331 
2332 
2333 void
2334 MainWin::_ShowIfNeeded()
2335 {
2336 	if (find_thread(NULL) != Thread())
2337 		return;
2338 
2339 	if (!fHasVideo && fNoVideoFrame.IsValid()) {
2340 		MoveTo(fNoVideoFrame.LeftTop());
2341 		ResizeTo(fNoVideoFrame.Width(), fNoVideoFrame.Height());
2342 	}
2343 	fNoVideoFrame = BRect();
2344 
2345 	if (IsHidden()) {
2346 		Show();
2347 		UpdateIfNeeded();
2348 	}
2349 }
2350 
2351 
2352 void
2353 MainWin::_ShowFullscreenControls(bool show, bool animate)
2354 {
2355 	if (fShowsFullscreenControls == show)
2356 		return;
2357 
2358 	fShowsFullscreenControls = show;
2359 
2360 	if (show) {
2361 		fControls->RemoveSelf();
2362 		fControls->MoveTo(fVideoView->Bounds().left,
2363 			fVideoView->Bounds().bottom + 1);
2364 		fVideoView->AddChild(fControls);
2365 		if (fScaleFullscreenControls)
2366 			fControls->SetSymbolScale(1.5f);
2367 		while (fControls->IsHidden())
2368 			fControls->Show();
2369 	}
2370 
2371 	if (animate) {
2372 		// Slide the controls into view. We need to do this with
2373 		// messages, otherwise we block the video playback for the
2374 		// time of the animation.
2375 		const float kAnimationOffsets[] = { 0.05, 0.2, 0.5, 0.2, 0.05 };
2376 		const int32 steps = sizeof(kAnimationOffsets) / sizeof(float);
2377 		float height = fControls->Bounds().Height();
2378 		float moveDist = show ? -height : height;
2379 		float originalY = fControls->Frame().top;
2380 		for (int32 i = 0; i < steps; i++) {
2381 			BMessage message(M_SLIDE_CONTROLS);
2382 			message.AddFloat("offset",
2383 				floorf(moveDist * kAnimationOffsets[i]));
2384 			PostMessage(&message, this);
2385 		}
2386 		BMessage finalMessage(M_FINISH_SLIDING_CONTROLS);
2387 		finalMessage.AddFloat("offset", originalY + moveDist);
2388 		finalMessage.AddBool("show", show);
2389 		PostMessage(&finalMessage, this);
2390 	} else {
2391 		if (!show) {
2392 			fControls->RemoveSelf();
2393 			fControls->MoveTo(fVideoView->Frame().left,
2394 				fVideoView->Frame().bottom + 1);
2395 			fBackground->AddChild(fControls);
2396 			fControls->SetSymbolScale(1.0f);
2397 			while (!fControls->IsHidden())
2398 				fControls->Hide();
2399 		}
2400 	}
2401 }
2402 
2403 
2404 // #pragma mark -
2405 
2406 
2407 void
2408 MainWin::_UpdatePlaylistItemFile()
2409 {
2410 	BAutolock locker(fPlaylist);
2411 	const FilePlaylistItem* item
2412 		= dynamic_cast<const FilePlaylistItem*>(fController->Item());
2413 	if (item == NULL)
2414 		return;
2415 
2416 	if (!fHasVideo && !fHasAudio)
2417 		return;
2418 
2419 	BNode node(&item->Ref());
2420 	if (node.InitCheck())
2421 		return;
2422 
2423 	locker.Unlock();
2424 
2425 	// Set some standard attributes of the currently played file.
2426 	// This should only be a temporary solution.
2427 
2428 	// Write duration
2429 	const char* kDurationAttrName = "Media:Length";
2430 	attr_info info;
2431 	status_t status = node.GetAttrInfo(kDurationAttrName, &info);
2432 	if (status != B_OK || info.size == 0) {
2433 		bigtime_t duration = fController->TimeDuration();
2434 		// TODO: Tracker does not seem to care about endian for scalar types
2435 		node.WriteAttr(kDurationAttrName, B_INT64_TYPE, 0, &duration,
2436 			sizeof(int64));
2437 	}
2438 
2439 	// Write audio bitrate
2440 	if (fHasAudio) {
2441 		status = node.GetAttrInfo("Audio:Bitrate", &info);
2442 		if (status != B_OK || info.size == 0) {
2443 			media_format format;
2444 			if (fController->GetEncodedAudioFormat(&format) == B_OK
2445 				&& format.type == B_MEDIA_ENCODED_AUDIO) {
2446 				int32 bitrate = (int32)(format.u.encoded_audio.bit_rate
2447 					/ 1000);
2448 				char text[256];
2449 				snprintf(text, sizeof(text), "%ld kbit", bitrate);
2450 				node.WriteAttr("Audio:Bitrate", B_STRING_TYPE, 0, text,
2451 					strlen(text) + 1);
2452 			}
2453 		}
2454 	}
2455 
2456 	// Write video bitrate
2457 	if (fHasVideo) {
2458 		status = node.GetAttrInfo("Video:Bitrate", &info);
2459 		if (status != B_OK || info.size == 0) {
2460 			media_format format;
2461 			if (fController->GetEncodedVideoFormat(&format) == B_OK
2462 				&& format.type == B_MEDIA_ENCODED_VIDEO) {
2463 				int32 bitrate = (int32)(format.u.encoded_video.avg_bit_rate
2464 					/ 1000);
2465 				char text[256];
2466 				snprintf(text, sizeof(text), "%ld kbit", bitrate);
2467 				node.WriteAttr("Video:Bitrate", B_STRING_TYPE, 0, text,
2468 					strlen(text) + 1);
2469 			}
2470 		}
2471 	}
2472 
2473 	_UpdateAttributesMenu(node);
2474 }
2475 
2476 
2477 void
2478 MainWin::_UpdateAttributesMenu(const BNode& node)
2479 {
2480 	int32 rating = -1;
2481 
2482 	attr_info info;
2483 	status_t status = node.GetAttrInfo(kRatingAttrName, &info);
2484 	if (status == B_OK && info.type == B_INT32_TYPE) {
2485 		// Node has the Rating attribute.
2486 		node.ReadAttr(kRatingAttrName, B_INT32_TYPE, 0, &rating,
2487 			sizeof(rating));
2488 	}
2489 
2490 	for (int32 i = 0; BMenuItem* item = fRatingMenu->ItemAt(i); i++)
2491 		item->SetMarked(i + 1 == rating);
2492 }
2493 
2494 
2495 void
2496 MainWin::_SetRating(int32 rating)
2497 {
2498 	BAutolock locker(fPlaylist);
2499 	const FilePlaylistItem* item
2500 		= dynamic_cast<const FilePlaylistItem*>(fController->Item());
2501 	if (item == NULL)
2502 		return;
2503 
2504 	BNode node(&item->Ref());
2505 	if (node.InitCheck())
2506 		return;
2507 
2508 	locker.Unlock();
2509 
2510 	node.WriteAttr(kRatingAttrName, B_INT32_TYPE, 0, &rating, sizeof(rating));
2511 
2512 	// TODO: The whole mechnism should work like this:
2513 	// * There is already an attribute API for PlaylistItem, flesh it out!
2514 	// * FilePlaylistItem node-monitors it's file somehow.
2515 	// * FilePlaylistItem keeps attributes in sync and sends notications.
2516 	// * MainWin updates the menu according to FilePlaylistItem notifications.
2517 	// * PlaylistWin shows columns with attribute and other info.
2518 	// * PlaylistWin updates also upon FilePlaylistItem notifications.
2519 	// * This keeps attributes in sync when another app changes them.
2520 
2521 	_UpdateAttributesMenu(node);
2522 }
2523 
2524 
2525 void
2526 MainWin::_UpdateControlsEnabledStatus()
2527 {
2528 	uint32 enabledButtons = 0;
2529 	if (fHasVideo || fHasAudio) {
2530 		enabledButtons |= PLAYBACK_ENABLED | SEEK_ENABLED
2531 			| SEEK_BACK_ENABLED | SEEK_FORWARD_ENABLED;
2532 	}
2533 	if (fHasAudio)
2534 		enabledButtons |= VOLUME_ENABLED;
2535 
2536 	BAutolock _(fPlaylist);
2537 	bool canSkipPrevious, canSkipNext;
2538 	fPlaylist->GetSkipInfo(&canSkipPrevious, &canSkipNext);
2539 	if (canSkipPrevious)
2540 		enabledButtons |= SKIP_BACK_ENABLED;
2541 	if (canSkipNext)
2542 		enabledButtons |= SKIP_FORWARD_ENABLED;
2543 
2544 	fControls->SetEnabled(enabledButtons);
2545 
2546 	fNoInterfaceMenuItem->SetEnabled(fHasVideo);
2547 	fAttributesMenu->SetEnabled(fHasAudio || fHasVideo);
2548 }
2549 
2550 
2551 void
2552 MainWin::_UpdatePlaylistMenu()
2553 {
2554 	if (!fPlaylist->Lock())
2555 		return;
2556 
2557 	fPlaylistMenu->RemoveItems(0, fPlaylistMenu->CountItems(), true);
2558 
2559 	int32 count = fPlaylist->CountItems();
2560 	for (int32 i = 0; i < count; i++) {
2561 		PlaylistItem* item = fPlaylist->ItemAtFast(i);
2562 		_AddPlaylistItem(item, i);
2563 	}
2564 	fPlaylistMenu->SetTargetForItems(this);
2565 
2566 	_MarkPlaylistItem(fPlaylist->CurrentItemIndex());
2567 
2568 	fPlaylist->Unlock();
2569 }
2570 
2571 
2572 void
2573 MainWin::_AddPlaylistItem(PlaylistItem* item, int32 index)
2574 {
2575 	BMessage* message = new BMessage(M_SET_PLAYLIST_POSITION);
2576 	message->AddInt32("index", index);
2577 	BMenuItem* menuItem = new BMenuItem(item->Name().String(), message);
2578 	fPlaylistMenu->AddItem(menuItem, index);
2579 }
2580 
2581 
2582 void
2583 MainWin::_RemovePlaylistItem(int32 index)
2584 {
2585 	delete fPlaylistMenu->RemoveItem(index);
2586 }
2587 
2588 
2589 void
2590 MainWin::_MarkPlaylistItem(int32 index)
2591 {
2592 	if (BMenuItem* item = fPlaylistMenu->ItemAt(index)) {
2593 		item->SetMarked(true);
2594 		// ... and in case the menu is currently on screen:
2595 		if (fPlaylistMenu->LockLooper()) {
2596 			fPlaylistMenu->Invalidate();
2597 			fPlaylistMenu->UnlockLooper();
2598 		}
2599 	}
2600 }
2601 
2602 
2603 void
2604 MainWin::_MarkItem(BMenu* menu, uint32 command, bool mark)
2605 {
2606 	if (BMenuItem* item = menu->FindItem(command))
2607 		item->SetMarked(mark);
2608 }
2609 
2610 
2611 void
2612 MainWin::_AdoptGlobalSettings()
2613 {
2614 	mpSettings settings = Settings::CurrentSettings();
2615 		// thread safe
2616 
2617 	fCloseWhenDonePlayingMovie = settings.closeWhenDonePlayingMovie;
2618 	fCloseWhenDonePlayingSound = settings.closeWhenDonePlayingSound;
2619 	fLoopMovies = settings.loopMovie;
2620 	fLoopSounds = settings.loopSound;
2621 	fScaleFullscreenControls = settings.scaleFullscreenControls;
2622 }
2623 
2624 
2625