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