]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/mediactrl.cpp
Don't set wxTextAttr font family to invalid value.
[wxWidgets.git] / src / osx / carbon / mediactrl.cpp
CommitLineData
489468fe 1/////////////////////////////////////////////////////////////////////////////
524c47aa 2// Name: src/osx/carbon/mediactrl.cpp
489468fe
SC
3// Purpose: Built-in Media Backends for Mac
4// Author: Ryan Norton <wxprojects@comcast.net>
5// Modified by:
6// Created: 11/07/04
7// RCS-ID: $Id$
8// Copyright: (c) 2004-2006 Ryan Norton
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13// OK, a casual overseer of this file may wonder why we don't use
14// either CreateMovieControl or HIMovieView...
15//
16// CreateMovieControl
17// 1) Need to dispose and create each time a new movie is loaded
18// 2) Not that many real advantages
19// 3) Progressively buggier in higher OSX versions
20// (see main.c of QTCarbonShell sample for details)
21// HIMovieView
22// 1) Crashes on destruction in ALL cases on quite a few systems!
23// (With the only real "alternative" is to simply not
24// dispose of it and let it leak...)
25// 2) Massive refreshing bugs with its movie controller between
26// movies
27//
28// At one point we had a complete implementation for CreateMovieControl
29// and on my (RN) local copy I had one for HIMovieView - but they
30// were simply deemed to be too buggy/unuseful. HIMovieView could
31// have been useful as well because it uses OpenGL contexts instead
32// of GWorlds. Perhaps someday when someone comes out with some
33// ingenious workarounds :).
34//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35
36// For compilers that support precompilation, includes "wx.h".
37#include "wx/wxprec.h"
38
39#if wxUSE_MEDIACTRL
40
41#include "wx/mediactrl.h"
42
43#ifndef WX_PRECOMP
44 #include "wx/log.h"
45 #include "wx/timer.h"
46#endif
47
b2680ced 48#if wxOSX_USE_CARBON
489468fe
SC
49#define USE_QUICKTIME 1
50#else
51#define USE_QUICKTIME 0
52#endif
53
54#if USE_QUICKTIME
55
524c47aa
SC
56#include "wx/osx/private.h"
57#include <QuickTime/QuickTimeComponents.h>
58
489468fe
SC
59//---------------------------------------------------------------------------
60// Height and Width of movie controller in the movie control (apple samples)
61//---------------------------------------------------------------------------
62#define wxMCWIDTH 320
63#define wxMCHEIGHT 16
64
65//===========================================================================
66// BACKEND DECLARATIONS
67//===========================================================================
68
69//---------------------------------------------------------------------------
70// wxQTMediaBackend
71//---------------------------------------------------------------------------
72
73class WXDLLIMPEXP_MEDIA wxQTMediaBackend : public wxMediaBackendCommonBase
74{
75public:
76 wxQTMediaBackend();
77 virtual ~wxQTMediaBackend();
78
79 virtual bool CreateControl(wxControl* ctrl, wxWindow* parent,
80 wxWindowID id,
81 const wxPoint& pos,
82 const wxSize& size,
83 long style,
84 const wxValidator& validator,
85 const wxString& name);
86
87 virtual bool Load(const wxString& fileName);
88 virtual bool Load(const wxURI& location);
8f2a8de6
VZ
89 virtual bool Load(const wxURI& location,
90 const wxURI& WXUNUSED(proxy))
91 {
92 return Load(location);
93 }
489468fe
SC
94
95 virtual bool Play();
96 virtual bool Pause();
97 virtual bool Stop();
98
99 virtual wxMediaState GetState();
100
101 virtual bool SetPosition(wxLongLong where);
102 virtual wxLongLong GetPosition();
103 virtual wxLongLong GetDuration();
104
105 virtual void Move(int x, int y, int w, int h);
106 wxSize GetVideoSize() const;
107
108 virtual double GetPlaybackRate();
109 virtual bool SetPlaybackRate(double dRate);
110
111 virtual double GetVolume();
112 virtual bool SetVolume(double);
113
114 void Cleanup();
115 void FinishLoad();
116
117 virtual bool ShowPlayerControls(wxMediaCtrlPlayerControls flags);
118
119 virtual wxLongLong GetDownloadProgress();
120 virtual wxLongLong GetDownloadTotal();
121
122 virtual void MacVisibilityChanged();
123
124 //
125 // ------ Implementation from now on --------
126 //
127 bool DoPause();
128 bool DoStop();
129
130 void DoLoadBestSize();
131 void DoSetControllerVisible(wxMediaCtrlPlayerControls flags);
132
133 wxLongLong GetDataSizeFromStart(TimeValue end);
134
135 Boolean IsQuickTime4Installed();
136 void DoNewMovieController();
137
138 static pascal void PPRMProc(
139 Movie theMovie, OSErr theErr, void* theRefCon);
140
141 //TODO: Last param actually long - does this work on 64bit machines?
142 static pascal Boolean MCFilterProc(MovieController theController,
143 short action, void *params, long refCon);
144
145 static pascal OSStatus WindowEventHandler(
146 EventHandlerCallRef inHandlerCallRef,
147 EventRef inEvent, void *inUserData );
148
149 wxSize m_bestSize; // Original movie size
150 Movie m_movie; // Movie instance
151 bool m_bPlaying; // Whether media is playing or not
152 class wxTimer* m_timer; // Timer for streaming the movie
153 MovieController m_mc; // MovieController instance
154 wxMediaCtrlPlayerControls m_interfaceflags; // Saved interface flags
155
156 // Event handlers and UPPs/Callbacks
157 EventHandlerRef m_windowEventHandler;
158 EventHandlerUPP m_windowUPP;
159
160 MoviePrePrerollCompleteUPP m_preprerollupp;
161 MCActionFilterWithRefConUPP m_mcactionupp;
162
163 GWorldPtr m_movieWorld; //Offscreen movie GWorld
164
165 friend class wxQTMediaEvtHandler;
166
167 DECLARE_DYNAMIC_CLASS(wxQTMediaBackend)
168};
169
170// helper to hijack background erasing for the QT window
171class WXDLLIMPEXP_MEDIA wxQTMediaEvtHandler : public wxEvtHandler
172{
173public:
174 wxQTMediaEvtHandler(wxQTMediaBackend *qtb)
175 {
176 m_qtb = qtb;
177
178 qtb->m_ctrl->Connect(
179 qtb->m_ctrl->GetId(), wxEVT_ERASE_BACKGROUND,
180 wxEraseEventHandler(wxQTMediaEvtHandler::OnEraseBackground),
181 NULL, this);
182 }
183
184 void OnEraseBackground(wxEraseEvent& event);
185
186private:
187 wxQTMediaBackend *m_qtb;
188
c0c133e1 189 wxDECLARE_NO_COPY_CLASS(wxQTMediaEvtHandler);
489468fe
SC
190};
191
192//===========================================================================
193// IMPLEMENTATION
194//===========================================================================
195
196
197//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
198//
199// wxQTMediaBackend
200//
201//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
202
203IMPLEMENT_DYNAMIC_CLASS(wxQTMediaBackend, wxMediaBackend)
204
205//Time between timer calls - this is the Apple recommondation to the TCL
206//team I believe
207#define MOVIE_DELAY 20
208
209//---------------------------------------------------------------------------
210// wxQTMediaLoadTimer
211//
212// QT, esp. QT for Windows is very picky about how you go about
213// async loading. If you were to go through a Windows message loop
214// or a MoviesTask or both and then check the movie load state
215// it would still return 1000 (loading)... even (pre)prerolling doesn't
216// help. However, making a load timer like this works
217//---------------------------------------------------------------------------
218class wxQTMediaLoadTimer : public wxTimer
219{
220public:
221 wxQTMediaLoadTimer(wxQTMediaBackend* parent) :
222 m_parent(parent) {}
223
224 void Notify()
225 {
226 ::MCIdle(m_parent->m_mc);
227
228 // kMovieLoadStatePlayable is not enough on MAC:
229 // it plays, but IsMovieDone might return true (!)
230 // sure we need to wait until kMovieLoadStatePlaythroughOK
231 if (::GetMovieLoadState(m_parent->m_movie) >= 20000)
232 {
233 m_parent->FinishLoad();
234 delete this;
235 }
236 }
237
238protected:
239 wxQTMediaBackend *m_parent; // Backend pointer
240};
241
242// --------------------------------------------------------------------------
243// wxQTMediaPlayTimer - Handle Asyncronous Playing
244//
245// 1) Checks to see if the movie is done, and if not continues
246// streaming the movie
247// 2) Sends the wxEVT_MEDIA_STOP event if we have reached the end of
248// the movie.
249// --------------------------------------------------------------------------
250class wxQTMediaPlayTimer : public wxTimer
251{
252public:
253 wxQTMediaPlayTimer(wxQTMediaBackend* parent) :
254 m_parent(parent) {}
255
256 void Notify()
257 {
258 //
259 // OK, a little explaining - basically originally
260 // we only called MoviesTask if the movie was actually
261 // playing (not paused or stopped)... this was before
262 // we realized MoviesTask actually handles repainting
263 // of the current frame - so if you were to resize
264 // or something it would previously not redraw that
265 // portion of the movie.
266 //
267 // So now we call MoviesTask always so that it repaints
268 // correctly.
269 //
270 ::MCIdle(m_parent->m_mc);
271
272 //
273 // Handle the stop event - if the movie has reached
274 // the end, notify our handler
275 //
276 if (::IsMovieDone(m_parent->m_movie))
277 {
278 if ( m_parent->SendStopEvent() )
279 {
280 m_parent->Stop();
281 wxASSERT(::GetMoviesError() == noErr);
282
283 m_parent->QueueFinishEvent();
284 }
285 }
286 }
287
288protected:
289 wxQTMediaBackend* m_parent; // Backend pointer
290};
291
292
293//---------------------------------------------------------------------------
294// wxQTMediaBackend Constructor
295//
296// Sets m_timer to NULL signifying we havn't loaded anything yet
297//---------------------------------------------------------------------------
298wxQTMediaBackend::wxQTMediaBackend()
299 : m_movie(NULL), m_bPlaying(false), m_timer(NULL)
300 , m_mc(NULL), m_interfaceflags(wxMEDIACTRLPLAYERCONTROLS_NONE)
301 , m_preprerollupp(NULL), m_movieWorld(NULL)
302{
303}
304
305//---------------------------------------------------------------------------
306// wxQTMediaBackend Destructor
307//
308// 1) Cleans up the QuickTime movie instance
309// 2) Decrements the QuickTime reference counter - if this reaches
310// 0, QuickTime shuts down
311// 3) Decrements the QuickTime Windows Media Layer reference counter -
312// if this reaches 0, QuickTime shuts down the Windows Media Layer
313//---------------------------------------------------------------------------
314wxQTMediaBackend::~wxQTMediaBackend()
315{
316 if (m_movie)
317 Cleanup();
318
319 // Cleanup for moviecontroller
320 if (m_mc)
321 {
322 // destroy wxQTMediaEvtHandler we pushed on it
323 m_ctrl->PopEventHandler(true);
324 RemoveEventHandler(m_windowEventHandler);
325 DisposeEventHandlerUPP(m_windowUPP);
326
327 // Dispose of the movie controller
328 ::DisposeMovieController(m_mc);
329 m_mc = NULL;
03647350 330
489468fe
SC
331 // Dispose of offscreen GWorld
332 ::DisposeGWorld(m_movieWorld);
333 }
334
335 // Note that ExitMovies() is not necessary...
336 ExitMovies();
337}
338
339//---------------------------------------------------------------------------
340// wxQTMediaBackend::CreateControl
341//
342// 1) Intializes QuickTime
343// 2) Creates the control window
344//---------------------------------------------------------------------------
345bool wxQTMediaBackend::CreateControl(
346 wxControl* ctrl,
347 wxWindow* parent,
348 wxWindowID id,
349 const wxPoint& pos,
350 const wxSize& size,
351 long style,
352 const wxValidator& validator,
353 const wxString& name)
354{
355 if (!IsQuickTime4Installed())
356 return false;
357
358 EnterMovies();
359
360 wxMediaCtrl* mediactrl = (wxMediaCtrl*)ctrl;
361
362 //
363 // Create window
364 // By default wxWindow(s) is created with a border -
365 // so we need to get rid of those
366 //
367 // Since we don't have a child window like most other
368 // backends, we don't need wxCLIP_CHILDREN
369 //
370 if ( !mediactrl->wxControl::Create(
371 parent, id, pos, size,
372 wxWindow::MacRemoveBordersFromStyle(style),
373 validator, name))
374 {
375 return false;
376 }
377
378#if wxUSE_VALIDATORS
379 mediactrl->SetValidator(validator);
380#endif
381
382 m_ctrl = mediactrl;
383 return true;
384}
385
386//---------------------------------------------------------------------------
387// wxQTMediaBackend::IsQuickTime4Installed
388//
389// Determines whether version 4 of QT is installed
390// (Pretty much for Classic only)
391//---------------------------------------------------------------------------
392Boolean wxQTMediaBackend::IsQuickTime4Installed()
393{
394 OSErr error;
395 long result;
396
397 error = Gestalt(gestaltQuickTime, &result);
398 return (error == noErr) && (((result >> 16) & 0xffff) >= 0x0400);
399}
400
401//---------------------------------------------------------------------------
402// wxQTMediaBackend::Load (file version)
403//
404// 1) Get an FSSpec from the Windows path name
405// 2) Open the movie
406// 3) Obtain the movie instance from the movie resource
407// 4) Close the movie resource
408// 5) Finish loading
409//---------------------------------------------------------------------------
410bool wxQTMediaBackend::Load(const wxString& fileName)
411{
412 if (m_movie)
413 Cleanup();
414
415 ::ClearMoviesStickyError(); // clear previous errors so
416 // GetMoviesStickyError is useful
417
418 OSErr err = noErr;
419 short movieResFile;
420 FSSpec sfFile;
421
422 wxMacFilename2FSSpec( fileName, &sfFile );
423 if (OpenMovieFile( &sfFile, &movieResFile, fsRdPerm ) != noErr)
424 return false;
425
426 short movieResID = 0;
427 Str255 movieName;
428
429 err = NewMovieFromFile(
430 &m_movie,
431 movieResFile,
432 &movieResID,
433 movieName,
434 newMovieActive,
435 NULL); // wasChanged
436
437 // Do not use ::GetMoviesStickyError() here because it returns -2009
03647350 438 // a.k.a. invalid track on valid mpegs
489468fe
SC
439 if (err == noErr && ::GetMoviesError() == noErr)
440 {
441 ::CloseMovieFile(movieResFile);
442
443 // Create movie controller/control
444 DoNewMovieController();
445
446 FinishLoad();
447 return true;
448 }
449
450 return false;
451}
452
453//---------------------------------------------------------------------------
454// wxQTMediaBackend::Load (URL Version)
455//
456// 1) Build an escaped URI from location
457// 2) Create a handle to store the URI string
458// 3) Put the URI string inside the handle
459// 4) Make a QuickTime URL data ref from the handle with the URI in it
460// 5) Clean up the URI string handle
461// 6) Do some prerolling
462// 7) Finish Loading
463//---------------------------------------------------------------------------
464bool wxQTMediaBackend::Load(const wxURI& location)
465{
466 if (m_movie)
467 Cleanup();
468
469 ::ClearMoviesStickyError(); // clear previous errors so
470 // GetMoviesStickyError is useful
471
472 wxString theURI = location.BuildURI();
473 OSErr err;
474
475 size_t len;
476 const char* theURIString;
477
478#if wxUSE_UNICODE
479 wxCharBuffer buf = wxConvLocal.cWC2MB(theURI.wc_str(), theURI.length(), &len);
480 theURIString = buf;
481#else
482 theURIString = theURI;
483 len = theURI.length();
484#endif
485
486 Handle theHandle = ::NewHandleClear(len + 1);
487 wxASSERT(theHandle);
488
489 ::BlockMoveData(theURIString, *theHandle, len + 1);
490
491 // create the movie from the handle that refers to the URI
492 err = ::NewMovieFromDataRef(
493 &m_movie,
494 newMovieActive | newMovieAsyncOK /* | newMovieIdleImportOK*/,
495 NULL, theHandle,
496 URLDataHandlerSubType);
497
498 ::DisposeHandle(theHandle);
499
500 if (err == noErr && ::GetMoviesStickyError() == noErr)
501 {
502 // Movie controller resets prerolling, so we must create first
503 DoNewMovieController();
504
505 long timeNow;
506 Fixed playRate;
507
508 timeNow = ::GetMovieTime(m_movie, NULL);
509 wxASSERT(::GetMoviesError() == noErr);
510
511 playRate = ::GetMoviePreferredRate(m_movie);
512 wxASSERT(::GetMoviesError() == noErr);
513
514 //
515 // Note that the callback here is optional,
516 // but without it PrePrerollMovie can be buggy
517 // (see Apple ml). Also, some may wonder
518 // why we need this at all - this is because
519 // Apple docs say QuickTime streamed movies
520 // require it if you don't use a Movie Controller,
521 // which we don't by default.
522 //
523 m_preprerollupp = wxQTMediaBackend::PPRMProc;
524 ::PrePrerollMovie( m_movie, timeNow, playRate,
525 m_preprerollupp, (void*)this);
526
527 return true;
528 }
529
530 return false;
531}
532
533//---------------------------------------------------------------------------
534// wxQTMediaBackend::DoNewMovieController
535//
536// Attaches movie to moviecontroller or creates moviecontroller
537// if not created yet
538//---------------------------------------------------------------------------
539void wxQTMediaBackend::DoNewMovieController()
540{
541 if (!m_mc)
542 {
543 // Get top level window ref for some mac functions
544 WindowRef wrTLW = (WindowRef) m_ctrl->MacGetTopLevelWindowRef();
545
546 // MovieController not set up yet, so we need to create a new one.
547 // You have to pass a valid movie to NewMovieController, evidently
548 ::SetMovieGWorld(m_movie,
549 (CGrafPtr) GetWindowPort(wrTLW),
550 NULL);
551 wxASSERT(::GetMoviesError() == noErr);
552
553 Rect bounds = wxMacGetBoundsForControl(
554 m_ctrl,
555 m_ctrl->GetPosition(),
556 m_ctrl->GetSize());
557
558 m_mc = ::NewMovieController(
559 m_movie, &bounds,
560 mcTopLeftMovie | mcNotVisible /* | mcWithFrame */ );
561 wxASSERT(::GetMoviesError() == noErr);
562
563 ::MCDoAction(m_mc, 32, (void*)true); // mcActionSetKeysEnabled
564 wxASSERT(::GetMoviesError() == noErr);
565
566 // Setup a callback so we can tell when the user presses
567 // play on the player controls
568 m_mcactionupp = wxQTMediaBackend::MCFilterProc;
569 ::MCSetActionFilterWithRefCon( m_mc, m_mcactionupp, (long)this );
570 wxASSERT(::GetMoviesError() == noErr);
571
572 // Part of a suggestion from Greg Hazel to repaint movie when idle
573 m_ctrl->PushEventHandler(new wxQTMediaEvtHandler(this));
574
575 // Create offscreen GWorld for where to "show" when window is hidden
576 Rect worldRect;
577 worldRect.left = worldRect.top = 0;
578 worldRect.right = worldRect.bottom = 1;
579 ::NewGWorld(&m_movieWorld, 0, &worldRect, NULL, NULL, 0);
580
581 // Catch window messages:
582 // if we do not do this and if the user clicks the play
583 // button on the controller, for instance, nothing will happen...
584 EventTypeSpec theWindowEventTypes[] =
585 {
586 { kEventClassMouse, kEventMouseDown },
587 { kEventClassMouse, kEventMouseUp },
588 { kEventClassMouse, kEventMouseDragged },
589 { kEventClassKeyboard, kEventRawKeyDown },
590 { kEventClassKeyboard, kEventRawKeyRepeat },
591 { kEventClassKeyboard, kEventRawKeyUp },
592 { kEventClassWindow, kEventWindowUpdate },
593 { kEventClassWindow, kEventWindowActivated },
594 { kEventClassWindow, kEventWindowDeactivated }
595 };
596 m_windowUPP =
597 NewEventHandlerUPP( wxQTMediaBackend::WindowEventHandler );
598 InstallWindowEventHandler(
599 wrTLW,
600 m_windowUPP,
601 GetEventTypeCount( theWindowEventTypes ), theWindowEventTypes,
602 this,
603 &m_windowEventHandler );
604 }
605 else
606 {
607 // MovieController already created:
608 // Just change the movie in it and we're good to go
609 Point thePoint;
610 thePoint.h = thePoint.v = 0;
611 ::MCSetMovie(m_mc, m_movie,
612 (WindowRef)m_ctrl->MacGetTopLevelWindowRef(),
613 thePoint);
614 wxASSERT(::GetMoviesError() == noErr);
615 }
616}
617
618//---------------------------------------------------------------------------
619// wxQTMediaBackend::FinishLoad
620//
621// Performs operations after a movie ready to play/loaded.
622//---------------------------------------------------------------------------
623void wxQTMediaBackend::FinishLoad()
624{
625 // get the real size of the movie
626 DoLoadBestSize();
627
628 // show the player controls if the user wants to
629 if (m_interfaceflags)
630 DoSetControllerVisible(m_interfaceflags);
631
632 // we want millisecond precision
633 ::SetMovieTimeScale(m_movie, 1000);
634 wxASSERT(::GetMoviesError() == noErr);
635
636 // start movie progress timer
637 m_timer = new wxQTMediaPlayTimer(this);
638 wxASSERT(m_timer);
639 m_timer->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
640
641 // send loaded event and refresh size
642 NotifyMovieLoaded();
643}
644
645//---------------------------------------------------------------------------
646// wxQTMediaBackend::DoLoadBestSize
647//
648// Sets the best size of the control from the real size of the movie
649//---------------------------------------------------------------------------
650void wxQTMediaBackend::DoLoadBestSize()
651{
652 // get the real size of the movie
653 Rect outRect;
654 ::GetMovieNaturalBoundsRect(m_movie, &outRect);
655 wxASSERT(::GetMoviesError() == noErr);
656
657 // determine best size
658 m_bestSize.x = outRect.right - outRect.left;
659 m_bestSize.y = outRect.bottom - outRect.top;
660}
661
662//---------------------------------------------------------------------------
663// wxQTMediaBackend::Play
664//
665// Start the QT movie
666// (Apple recommends mcActionPrerollAndPlay but that's QT 4.1+)
667//---------------------------------------------------------------------------
668bool wxQTMediaBackend::Play()
669{
670 Fixed fixRate = (Fixed) (wxQTMediaBackend::GetPlaybackRate() * 0x10000);
671 if (!fixRate)
672 fixRate = ::GetMoviePreferredRate(m_movie);
673
674 wxASSERT(fixRate != 0);
675
676 if (!m_bPlaying)
677 ::MCDoAction( m_mc, 8 /* mcActionPlay */, (void*) fixRate);
678
679 bool result = (::GetMoviesError() == noErr);
680 if (result)
681 {
682 m_bPlaying = true;
683 QueuePlayEvent();
684 }
685
686 return result;
687}
688
689//---------------------------------------------------------------------------
690// wxQTMediaBackend::Pause
691//
692// Stop the movie
693//---------------------------------------------------------------------------
694bool wxQTMediaBackend::DoPause()
695{
696 // Stop the movie A.K.A. ::StopMovie(m_movie);
697 if (m_bPlaying)
698 {
699 ::MCDoAction( m_mc, 8 /*mcActionPlay*/, (void *) 0);
700 m_bPlaying = false;
701 return ::GetMoviesError() == noErr;
702 }
703
704 // already paused
705 return true;
706}
707
708bool wxQTMediaBackend::Pause()
709{
710 bool bSuccess = DoPause();
711 if (bSuccess)
712 this->QueuePauseEvent();
713
714 return bSuccess;
715}
716
717//---------------------------------------------------------------------------
718// wxQTMediaBackend::Stop
719//
720// 1) Stop the movie
721// 2) Seek to the beginning of the movie
722//---------------------------------------------------------------------------
723bool wxQTMediaBackend::DoStop()
724{
725 if (!wxQTMediaBackend::DoPause())
726 return false;
727
728 ::GoToBeginningOfMovie(m_movie);
729 return ::GetMoviesError() == noErr;
730}
731
732bool wxQTMediaBackend::Stop()
733{
734 bool bSuccess = DoStop();
735 if (bSuccess)
736 QueueStopEvent();
737
738 return bSuccess;
739}
740
741//---------------------------------------------------------------------------
742// wxQTMediaBackend::GetPlaybackRate
743//
744// 1) Get the movie playback rate from ::GetMovieRate
745//---------------------------------------------------------------------------
746double wxQTMediaBackend::GetPlaybackRate()
747{
748 return ( ((double)::GetMovieRate(m_movie)) / 0x10000);
749}
750
751//---------------------------------------------------------------------------
752// wxQTMediaBackend::SetPlaybackRate
753//
754// 1) Convert dRate to Fixed and Set the movie rate through SetMovieRate
755//---------------------------------------------------------------------------
756bool wxQTMediaBackend::SetPlaybackRate(double dRate)
757{
758 ::SetMovieRate(m_movie, (Fixed) (dRate * 0x10000));
759 return ::GetMoviesError() == noErr;
760}
761
762//---------------------------------------------------------------------------
763// wxQTMediaBackend::SetPosition
764//
765// 1) Create a time record struct (TimeRecord) with appropriate values
766// 2) Pass struct to SetMovieTime
767//---------------------------------------------------------------------------
768bool wxQTMediaBackend::SetPosition(wxLongLong where)
769{
770 TimeRecord theTimeRecord;
771 memset(&theTimeRecord, 0, sizeof(TimeRecord));
1f450872
SC
772 theTimeRecord.value.lo = where.GetLo();
773 theTimeRecord.value.hi = where.GetHi();
489468fe
SC
774 theTimeRecord.scale = ::GetMovieTimeScale(m_movie);
775 theTimeRecord.base = ::GetMovieTimeBase(m_movie);
776 ::SetMovieTime(m_movie, &theTimeRecord);
777
778 if (::GetMoviesError() != noErr)
779 return false;
780
781 return true;
782}
783
784//---------------------------------------------------------------------------
785// wxQTMediaBackend::GetPosition
786//
787// Calls GetMovieTime
788//---------------------------------------------------------------------------
789wxLongLong wxQTMediaBackend::GetPosition()
790{
791 return ::GetMovieTime(m_movie, NULL);
792}
793
794//---------------------------------------------------------------------------
795// wxQTMediaBackend::GetVolume
796//
797// Gets the volume through GetMovieVolume - which returns a 16 bit short -
798//
799// +--------+--------+
800// + (1) + (2) +
801// +--------+--------+
802//
803// (1) first 8 bits are value before decimal
804// (2) second 8 bits are value after decimal
805//
806// Volume ranges from -1.0 (gain but no sound), 0 (no sound and no gain) to
807// 1 (full gain and sound)
808//---------------------------------------------------------------------------
809double wxQTMediaBackend::GetVolume()
810{
811 short sVolume = ::GetMovieVolume(m_movie);
812
813 if (sVolume & (128 << 8)) //negative - no sound
814 return 0.0;
815
816 return sVolume / 256.0;
817}
818
819//---------------------------------------------------------------------------
820// wxQTMediaBackend::SetVolume
821//
822// Sets the volume through SetMovieVolume - which takes a 16 bit short -
823//
824// +--------+--------+
825// + (1) + (2) +
826// +--------+--------+
827//
828// (1) first 8 bits are value before decimal
829// (2) second 8 bits are value after decimal
830//
831// Volume ranges from -1.0 (gain but no sound), 0 (no sound and no gain) to
832// 1 (full gain and sound)
833//---------------------------------------------------------------------------
834bool wxQTMediaBackend::SetVolume(double dVolume)
835{
836 ::SetMovieVolume(m_movie, (short) (dVolume * 256));
837 return true;
838}
839
840//---------------------------------------------------------------------------
841// wxQTMediaBackend::GetDuration
842//
843// Calls GetMovieDuration
844//---------------------------------------------------------------------------
845wxLongLong wxQTMediaBackend::GetDuration()
846{
847 return ::GetMovieDuration(m_movie);
848}
849
850//---------------------------------------------------------------------------
851// wxQTMediaBackend::GetState
852//
853// Determines the current state - the timer keeps track of whether or not
854// we are paused or stopped (if the timer is running we are playing)
855//---------------------------------------------------------------------------
856wxMediaState wxQTMediaBackend::GetState()
857{
858 // Could use
859 // GetMovieActive/IsMovieDone/SetMovieActive
860 // combo if implemented that way
861 if (m_bPlaying)
862 return wxMEDIASTATE_PLAYING;
863 else if (!m_movie || wxQTMediaBackend::GetPosition() == 0)
864 return wxMEDIASTATE_STOPPED;
865 else
866 return wxMEDIASTATE_PAUSED;
867}
868
869//---------------------------------------------------------------------------
870// wxQTMediaBackend::Cleanup
871//
872// Diposes of the movie timer, Control if native, and stops and disposes
873// of the QT movie
874//---------------------------------------------------------------------------
875void wxQTMediaBackend::Cleanup()
876{
877 m_bPlaying = false;
878 if (m_timer)
879 {
880 delete m_timer;
881 m_timer = NULL;
882 }
883
884 // Stop the movie:
885 // Apple samples with CreateMovieControl typically
886 // install a event handler and do this on the dispose
887 // event, but we do it here for simplicity
888 // (It might keep playing for several seconds after
889 // control destruction if not)
890 wxQTMediaBackend::Pause();
891
892 // Dispose of control or remove movie from MovieController
893 Point thePoint;
894 thePoint.h = thePoint.v = 0;
895 ::MCSetVisible(m_mc, false);
896 ::MCSetMovie(m_mc, NULL, NULL, thePoint);
897
898 ::DisposeMovie(m_movie);
899 m_movie = NULL;
900}
901
902//---------------------------------------------------------------------------
903// wxQTMediaBackend::GetVideoSize
904//
905// Returns the actual size of the QT movie
906//---------------------------------------------------------------------------
907wxSize wxQTMediaBackend::GetVideoSize() const
908{
909 return m_bestSize;
910}
911
912//---------------------------------------------------------------------------
913// wxQTMediaBackend::Move
914//
915// Move the movie controller or movie control
916// (we need to actually move the movie control manually...)
917// Top 10 things to do with quicktime in March 93's issue
918// of DEVELOP - very useful
919// http:// www.mactech.com/articles/develop/issue_13/031-033_QuickTime_column.html
920// OLD NOTE: Calling MCSetControllerBoundsRect without detaching
921// supposively resulted in a crash back then. Current code even
922// with CFM classic runs fine. If there is ever a problem,
923// take out the if 0 lines below
924//---------------------------------------------------------------------------
925void wxQTMediaBackend::Move(int x, int y, int w, int h)
926{
927 if (m_timer)
928 {
929 m_ctrl->GetParent()->MacWindowToRootWindow(&x, &y);
930 Rect theRect = {y, x, y + h, x + w};
931
932#if 0 // see note above
933 ::MCSetControllerAttached(m_mc, false);
934 wxASSERT(::GetMoviesError() == noErr);
935#endif
936
937 ::MCSetControllerBoundsRect(m_mc, &theRect);
938 wxASSERT(::GetMoviesError() == noErr);
939
940#if 0 // see note above
941 if (m_interfaceflags)
942 {
943 ::MCSetVisible(m_mc, true);
944 wxASSERT(::GetMoviesError() == noErr);
945 }
946#endif
947 }
948}
949
950//---------------------------------------------------------------------------
951// wxQTMediaBackend::DoSetControllerVisible
952//
953// Utility function that takes care of showing the moviecontroller
954// and showing/hiding the particular controls on it
955//---------------------------------------------------------------------------
956void wxQTMediaBackend::DoSetControllerVisible(
957 wxMediaCtrlPlayerControls flags)
958{
959 ::MCSetVisible(m_mc, true);
960
961 // Take care of subcontrols
962 if (::GetMoviesError() == noErr)
963 {
964 long mcFlags = 0;
965 ::MCDoAction(m_mc, 39/*mcActionGetFlags*/, (void*)&mcFlags);
966
967 if (::GetMoviesError() == noErr)
968 {
969 mcFlags |= ( //(1<<0)/*mcFlagSuppressMovieFrame*/ |
970 (1 << 3)/*mcFlagsUseWindowPalette*/
971 | ((flags & wxMEDIACTRLPLAYERCONTROLS_STEP)
972 ? 0 : (1 << 1)/*mcFlagSuppressStepButtons*/)
973 | ((flags & wxMEDIACTRLPLAYERCONTROLS_VOLUME)
974 ? 0 : (1 << 2)/*mcFlagSuppressSpeakerButton*/)
975 //if we take care of repainting ourselves
976 // | (1 << 4) /*mcFlagDontInvalidate*/
977 );
978
979 ::MCDoAction(m_mc, 38/*mcActionSetFlags*/, (void*)mcFlags);
980 }
981 }
982
983 // Adjust height and width of best size for movie controller
984 // if the user wants it shown
985 m_bestSize.x = m_bestSize.x > wxMCWIDTH ? m_bestSize.x : wxMCWIDTH;
986 m_bestSize.y += wxMCHEIGHT;
987}
988
989//---------------------------------------------------------------------------
990// wxQTMediaBackend::ShowPlayerControls
991//
992// Shows/Hides subcontrols on the media control
993//---------------------------------------------------------------------------
994bool wxQTMediaBackend::ShowPlayerControls(wxMediaCtrlPlayerControls flags)
995{
996 if (!m_mc)
997 return false; // no movie controller...
998
999 bool bSizeChanged = false;
1000
1001 // if the controller is visible and we want to hide it do so
1002 if (m_interfaceflags && !flags)
1003 {
1004 bSizeChanged = true;
1005 DoLoadBestSize();
1006 ::MCSetVisible(m_mc, false);
1007 }
1008 else if (!m_interfaceflags && flags) // show controller if hidden
1009 {
1010 bSizeChanged = true;
1011 DoSetControllerVisible(flags);
1012 }
1013
1014 // readjust parent sizers
1015 if (bSizeChanged)
1016 {
1017 NotifyMovieSizeChanged();
1018
1019 // remember state in case of loading new media
1020 m_interfaceflags = flags;
1021 }
1022
1023 return ::GetMoviesError() == noErr;
1024}
1025
1026//---------------------------------------------------------------------------
1027// wxQTMediaBackend::GetDataSizeFromStart
1028//
1029// Calls either GetMovieDataSize or GetMovieDataSize64 with a value
1030// of 0 for the starting value
1031//---------------------------------------------------------------------------
1032wxLongLong wxQTMediaBackend::GetDataSizeFromStart(TimeValue end)
1033{
1034#if 0 // old pre-qt4 way
1035 return ::GetMovieDataSize(m_movie, 0, end)
1036#else // qt4 way
1037 wide llDataSize;
1038 ::GetMovieDataSize64(m_movie, 0, end, &llDataSize);
1039 return wxLongLong(llDataSize.hi, llDataSize.lo);
1040#endif
1041}
1042
1043//---------------------------------------------------------------------------
1044// wxQTMediaBackend::GetDownloadProgress
1045//---------------------------------------------------------------------------
1046wxLongLong wxQTMediaBackend::GetDownloadProgress()
1047{
1048#if 0 // hackish and slow
1049 Handle hMovie = NewHandle(0);
1050 PutMovieIntoHandle(m_movie, hMovie);
1051 long lSize = GetHandleSize(hMovie);
1052 DisposeHandle(hMovie);
1053
1054 return lSize;
1055#else
1056 TimeValue tv;
1057 if (::GetMaxLoadedTimeInMovie(m_movie, &tv) != noErr)
1058 {
1059 wxLogDebug(wxT("GetMaxLoadedTimeInMovie failed"));
1060 return 0;
1061 }
1062
1063 return wxQTMediaBackend::GetDataSizeFromStart(tv);
1064#endif
1065}
1066
1067//---------------------------------------------------------------------------
1068// wxQTMediaBackend::GetDownloadTotal
1069//---------------------------------------------------------------------------
1070wxLongLong wxQTMediaBackend::GetDownloadTotal()
1071{
1072 return wxQTMediaBackend::GetDataSizeFromStart(
1073 ::GetMovieDuration(m_movie)
1074 );
1075}
1076
1077//---------------------------------------------------------------------------
1078// wxQTMediaBackend::MacVisibilityChanged
1079//
1080// The main problem here is that Windows quicktime, for example,
1081// renders more directly to a HWND. Mac quicktime does not do this
1082// and instead renders to the port of the WindowRef/WindowPtr on top
1083// of everything else/all other windows.
1084//
1085// So, for example, if you were to have a CreateTabsControl/wxNotebook
1086// and change pages, even if you called HIViewSetVisible/SetControlVisibility
1087// directly the movie will still continue playing on top of everything else
1088// if you went to a different tab.
1089//
1090// Note that another issue, and why we call MCSetControllerPort instead
1091// of SetMovieGWorld directly, is that in addition to rendering on
1092// top of everything else the last created controller steals mouse and
1093// other input from everything else in the window, including other
1094// controllers. Setting the port of it releases this behaviour.
1095//---------------------------------------------------------------------------
1096void wxQTMediaBackend::MacVisibilityChanged()
1097{
1098 if(!m_mc || !m_ctrl->m_bLoaded)
1099 return; //not initialized yet
1100
1101 if(m_ctrl->IsShownOnScreen())
1102 {
1103 //The window is being shown again, so set the GWorld of the
1104 //controller back to the port of the parent WindowRef
1105 WindowRef wrTLW =
1106 (WindowRef) m_ctrl->MacGetTopLevelWindowRef();
1107
1108 ::MCSetControllerPort(m_mc, (CGrafPtr) GetWindowPort(wrTLW));
1109 wxASSERT(::GetMoviesError() == noErr);
1110 }
1111 else
1112 {
1113 //We are being hidden - set the GWorld of the controller
1114 //to the offscreen GWorld
1115 ::MCSetControllerPort(m_mc, m_movieWorld);
1116 wxASSERT(::GetMoviesError() == noErr);
1117 }
1118}
1119
1120//---------------------------------------------------------------------------
1121// wxQTMediaBackend::OnEraseBackground
1122//
1123// Suggestion from Greg Hazel to repaint the movie when idle
1124// (on pause also)
1125//---------------------------------------------------------------------------
1126void wxQTMediaEvtHandler::OnEraseBackground(wxEraseEvent& WXUNUSED(evt))
1127{
1128 // Work around Nasty OSX drawing bug:
1129 // http://lists.apple.com/archives/QuickTime-API/2002/Feb/msg00311.html
1130 WindowRef wrTLW = (WindowRef) m_qtb->m_ctrl->MacGetTopLevelWindowRef();
1131
1132 RgnHandle region = ::MCGetControllerBoundsRgn(m_qtb->m_mc);
1133 ::MCInvalidate(m_qtb->m_mc, wrTLW, region);
1134 ::MCIdle(m_qtb->m_mc);
1135}
1136
1137//---------------------------------------------------------------------------
1138// wxQTMediaBackend::PPRMProc (static)
1139//
1140// Called when done PrePrerolling the movie.
1141// Note that in 99% of the cases this does nothing...
1142// Anyway we set up the loading timer here to tell us when the movie is done
1143//---------------------------------------------------------------------------
1144pascal void wxQTMediaBackend::PPRMProc(
1145 Movie theMovie,
1146 OSErr WXUNUSED_UNLESS_DEBUG(theErr),
1147 void* theRefCon)
1148{
1149 wxASSERT( theMovie );
1150 wxASSERT( theRefCon );
1151 wxASSERT( theErr == noErr );
1152
1153 wxQTMediaBackend* pBE = (wxQTMediaBackend*) theRefCon;
1154
1155 long lTime = ::GetMovieTime(theMovie,NULL);
1156 Fixed rate = ::GetMoviePreferredRate(theMovie);
1157 ::PrerollMovie(theMovie,lTime,rate);
1158 pBE->m_timer = new wxQTMediaLoadTimer(pBE);
1159 pBE->m_timer->Start(MOVIE_DELAY);
1160}
1161
1162//---------------------------------------------------------------------------
1163// wxQTMediaBackend::MCFilterProc (static)
1164//
1165// Callback for when the movie controller recieves a message
1166//---------------------------------------------------------------------------
1167pascal Boolean wxQTMediaBackend::MCFilterProc(
1168 MovieController WXUNUSED(theController),
1169 short action,
1170 void * WXUNUSED(params),
1171 long refCon)
1172{
1173 wxQTMediaBackend* pThis = (wxQTMediaBackend*)refCon;
1174
1175 switch (action)
1176 {
1177 case 1:
1178 // don't process idle events
1179 break;
1180
1181 case 8:
1182 // play button triggered - MC will set movie to opposite state
1183 // of current - playing ? paused : playing
1184 pThis->m_bPlaying = !(pThis->m_bPlaying);
1185 break;
1186
1187 default:
1188 break;
1189 }
1190
1191 return 0;
1192}
1193
1194//---------------------------------------------------------------------------
1195// wxQTMediaBackend::WindowEventHandler [static]
1196//
1197// Event callback for the top level window of our control that passes
1198// messages to our moviecontroller so it can receive mouse clicks etc.
1199//---------------------------------------------------------------------------
1200pascal OSStatus wxQTMediaBackend::WindowEventHandler(
1201 EventHandlerCallRef WXUNUSED(inHandlerCallRef),
1202 EventRef inEvent,
1203 void *inUserData)
1204{
1205 wxQTMediaBackend* be = (wxQTMediaBackend*) inUserData;
1206
1207 // Only process keyboard messages on this window if it actually
1208 // has focus, otherwise it will steal keystrokes from other windows!
1209 // As well as when it is not loaded properly as it
1210 // will crash in MCIsPlayerEvent
1211 if((GetEventClass(inEvent) == kEventClassKeyboard &&
1212 wxWindow::FindFocus() != be->m_ctrl)
1213 || !be->m_ctrl->m_bLoaded)
1214 return eventNotHandledErr;
1215
1216 // Pass the event onto the movie controller
1217 EventRecord theEvent;
1218 ConvertEventRefToEventRecord( inEvent, &theEvent );
1219 OSStatus err;
1220
1221 // TODO: Apple says MCIsPlayerEvent is depreciated and
1222 // MCClick, MCKey, MCIdle etc. should be used
1223 // (RN: Of course that's what they say about
1224 // CreateMovieControl and HIMovieView as well, LOL!)
1225 err = ::MCIsPlayerEvent( be->m_mc, &theEvent );
1226
1227 // Pass on to other event handlers if not handled- i.e. wx
1228 if (err != noErr)
1229 return noErr;
1230 else
1231 return eventNotHandledErr;
1232}
1233
1234#endif
1235
1236// in source file that contains stuff you don't directly use
1237#include "wx/html/forcelnk.h"
1238FORCE_LINK_ME(basewxmediabackends)
1239
1240#endif // wxUSE_MEDIACTRL