]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/mediactrl.cpp
new native toolbar implementation (turned off by default, not supporting embedded...
[wxWidgets.git] / src / mac / carbon / mediactrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mac/carbon/mediactrl.cpp
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-2005 Ryan Norton, portions (c) 2004 Kevin Olliver
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 //===========================================================================
13 // DECLARATIONS
14 //===========================================================================
15
16 //---------------------------------------------------------------------------
17 // Pre-compiled header stuff
18 //---------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "mediactrl.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 //---------------------------------------------------------------------------
32 // Includes
33 //---------------------------------------------------------------------------
34 #include "wx/mediactrl.h"
35
36 //---------------------------------------------------------------------------
37 // Compilation guard
38 //---------------------------------------------------------------------------
39 #if wxUSE_MEDIACTRL
40
41 //---------------------------------------------------------------------------
42 // Whether or not to use OSX 10.2's CreateMovieControl for native QuickTime
43 // control - i.e. native positioning and event handling etc..
44 //---------------------------------------------------------------------------
45 #ifndef wxUSE_CREATEMOVIECONTROL
46 # if defined( __WXMAC_OSX__ ) && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 )
47 # define wxUSE_CREATEMOVIECONTROL 1
48 # else
49 # define wxUSE_CREATEMOVIECONTROL 0
50 # endif
51 #endif
52
53 //===========================================================================
54 // BACKEND DECLARATIONS
55 //===========================================================================
56
57 //---------------------------------------------------------------------------
58 //
59 // wxQTMediaBackend
60 //
61 //---------------------------------------------------------------------------
62
63 //---------------------------------------------------------------------------
64 // QT Includes
65 //---------------------------------------------------------------------------
66 //uma is for wxMacFSSpec
67 #include "wx/mac/uma.h"
68 #include "wx/timer.h"
69 #ifndef __DARWIN__
70 #include <Movies.h>
71 #include <Gestalt.h>
72 #include <QuickTimeComponents.h> //Standard QT stuff
73 #else
74 #include <QuickTime/QuickTimeComponents.h>
75 #endif
76
77 //Determines whether version 4 of QT is installed (Pretty much for classic only)
78 Boolean _wxIsQuickTime4Installed (void)
79 {
80 short error;
81 long result;
82
83 error = Gestalt (gestaltQuickTime, &result);
84 return (error == noErr) && (((result >> 16) & 0xffff) >= 0x0400);
85 }
86
87 class WXDLLIMPEXP_MEDIA wxQTMediaBackend : public wxMediaBackend
88 {
89 public:
90
91 wxQTMediaBackend();
92 ~wxQTMediaBackend();
93
94 virtual bool CreateControl(wxControl* ctrl, wxWindow* parent,
95 wxWindowID id,
96 const wxPoint& pos,
97 const wxSize& size,
98 long style,
99 const wxValidator& validator,
100 const wxString& name);
101
102 virtual bool Play();
103 virtual bool Pause();
104 virtual bool Stop();
105
106 virtual bool Load(const wxString& fileName);
107 virtual bool Load(const wxURI& location);
108
109 virtual wxMediaState GetState();
110
111 virtual bool SetPosition(wxLongLong where);
112 virtual wxLongLong GetPosition();
113 virtual wxLongLong GetDuration();
114
115 virtual void Move(int x, int y, int w, int h);
116 wxSize GetVideoSize() const;
117
118 virtual double GetPlaybackRate();
119 virtual bool SetPlaybackRate(double dRate);
120
121 void Cleanup();
122 void FinishLoad();
123
124 wxSize m_bestSize; //Original movie size
125 #ifdef __WXMAC_OSX__
126 struct MovieType** m_movie; //QT Movie handle/instance
127 #else
128 Movie m_movie ;
129 #endif
130 wxControl* m_ctrl; //Parent control
131 bool m_bVideo; //Whether or not we have video
132 class _wxQTTimer* m_timer; //Timer for streaming the movie
133
134 DECLARE_DYNAMIC_CLASS(wxQTMediaBackend)
135 };
136
137
138 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
139 //
140 // wxQTMediaBackend
141 //
142 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
143
144 IMPLEMENT_DYNAMIC_CLASS(wxQTMediaBackend, wxMediaBackend);
145
146 //Time between timer calls
147 #define MOVIE_DELAY 100
148
149 // --------------------------------------------------------------------------
150 // wxQTTimer - Handle Asyncronous Playing
151 // --------------------------------------------------------------------------
152 class _wxQTTimer : public wxTimer
153 {
154 public:
155 _wxQTTimer(Movie movie, wxQTMediaBackend* parent) :
156 m_movie(movie), m_bPaused(false), m_parent(parent)
157 {
158 }
159
160 ~_wxQTTimer()
161 {
162 }
163
164 bool GetPaused() {return m_bPaused;}
165 void SetPaused(bool bPaused) {m_bPaused = bPaused;}
166
167 //-----------------------------------------------------------------------
168 // _wxQTTimer::Notify
169 //
170 // 1) Checks to see if the movie is done, and if not continues
171 // streaming the movie
172 // 2) Sends the wxEVT_MEDIA_STOP event if we have reached the end of
173 // the movie.
174 //-----------------------------------------------------------------------
175 void Notify()
176 {
177 if (!m_bPaused)
178 {
179 if(!IsMovieDone(m_movie))
180 MoviesTask(m_movie, MOVIE_DELAY);
181 else
182 {
183 wxMediaEvent theEvent(wxEVT_MEDIA_STOP,
184 m_parent->m_ctrl->GetId());
185 m_parent->m_ctrl->ProcessEvent(theEvent);
186
187 if(theEvent.IsAllowed())
188 {
189 Stop();
190 m_parent->Stop();
191 wxASSERT(::GetMoviesError() == noErr);
192
193 //send the event to our child
194 wxMediaEvent theEvent(wxEVT_MEDIA_FINISHED,
195 m_parent->m_ctrl->GetId());
196 m_parent->m_ctrl->ProcessEvent(theEvent);
197 }
198 }
199 }
200 }
201
202 protected:
203 Movie m_movie; //Our movie instance
204 bool m_bPaused; //Whether we are paused or not
205 wxQTMediaBackend* m_parent; //Backend pointer
206 };
207
208 //---------------------------------------------------------------------------
209 // wxQTMediaBackend Constructor
210 //
211 // Sets m_timer to NULL signifying we havn't loaded anything yet
212 //---------------------------------------------------------------------------
213 wxQTMediaBackend::wxQTMediaBackend() : m_timer(NULL)
214 {
215 }
216
217 //---------------------------------------------------------------------------
218 // wxQTMediaBackend Destructor
219 //
220 // 1) Cleans up the QuickTime movie instance
221 // 2) Decrements the QuickTime reference counter - if this reaches
222 // 0, QuickTime shuts down
223 // 3) Decrements the QuickTime Windows Media Layer reference counter -
224 // if this reaches 0, QuickTime shuts down the Windows Media Layer
225 //---------------------------------------------------------------------------
226 wxQTMediaBackend::~wxQTMediaBackend()
227 {
228 if(m_timer)
229 Cleanup();
230
231 //Note that ExitMovies() is not necessary...
232 ExitMovies();
233 }
234
235 //---------------------------------------------------------------------------
236 // wxQTMediaBackend::CreateControl
237 //
238 // 1) Intializes QuickTime
239 // 2) Creates the control window
240 //---------------------------------------------------------------------------
241 bool wxQTMediaBackend::CreateControl(wxControl* ctrl, wxWindow* parent,
242 wxWindowID id,
243 const wxPoint& pos,
244 const wxSize& size,
245 long style,
246 const wxValidator& validator,
247 const wxString& name)
248 {
249 //Don't bother in Native control mode
250 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_2 )
251 if (!_wxIsQuickTime4Installed())
252 return false;
253 #endif
254
255 EnterMovies();
256
257 //
258 // Create window
259 // By default wxWindow(s) is created with a border -
260 // so we need to get rid of those
261 //
262 // Since we don't have a child window like most other
263 // backends, we don't need wxCLIP_CHILDREN
264 //
265 if ( !
266
267 #if wxUSE_CREATEMOVIECONTROL
268 ctrl->wxWindow::Create(parent, id, pos, size,
269 wxWindow::MacRemoveBordersFromStyle(style),
270 name)
271 #else
272 ctrl->wxControl::Create(parent, id, pos, size,
273 wxWindow::MacRemoveBordersFromStyle(style),
274 validator, name)
275 #endif
276 )
277 return false;
278
279 #if wxUSE_VALIDATORS
280 ctrl->SetValidator(validator);
281 #endif
282
283 m_ctrl = ctrl;
284 return true;
285 }
286
287 //---------------------------------------------------------------------------
288 // wxQTMediaBackend::Load (file version)
289 //
290 // 1) Get an FSSpec from the Windows path name
291 // 2) Open the movie
292 // 3) Obtain the movie instance from the movie resource
293 // 4) Close the movie resource
294 // 5) Finish loading
295 //---------------------------------------------------------------------------
296 bool wxQTMediaBackend::Load(const wxString& fileName)
297 {
298 if(m_timer)
299 Cleanup();
300
301 OSErr err = noErr;
302 short movieResFile;
303 FSSpec sfFile;
304
305 //FIXME:wxMacFilename2FSSpec crashes on empty string -
306 //does it crash on other strings too and should this
307 //"fix" be put in the carbon wxSound?
308 if (fileName.empty())
309 return false;
310
311 wxMacFilename2FSSpec( fileName , &sfFile );
312
313 if (OpenMovieFile (&sfFile, &movieResFile, fsRdPerm) != noErr)
314 return false;
315
316 short movieResID = 0;
317 Str255 movieName;
318
319 err = NewMovieFromFile (
320 &m_movie,
321 movieResFile,
322 &movieResID,
323 movieName,
324 newMovieActive,
325 NULL); //wasChanged
326
327 CloseMovieFile (movieResFile);
328
329 if (err != noErr)
330 return false;
331
332 FinishLoad();
333
334 return ::GetMoviesError() == noErr;
335 }
336
337 //---------------------------------------------------------------------------
338 // wxQTMediaBackend::Load (URL Version)
339 //
340 // 1) Build an escaped URI from location
341 // 2) Create a handle to store the URI string
342 // 3) Put the URI string inside the handle
343 // 4) Make a QuickTime URL data ref from the handle with the URI in it
344 // 5) Clean up the URI string handle
345 // 6) Do some prerolling
346 // 7) Finish Loading
347 //---------------------------------------------------------------------------
348 bool wxQTMediaBackend::Load(const wxURI& location)
349 {
350 if(m_timer)
351 Cleanup();
352
353 wxString theURI = location.BuildURI();
354
355 OSErr err = noErr;
356
357 Handle theHandle = NewHandleClear(theURI.length() + 1);
358 wxASSERT(theHandle);
359
360 BlockMove(theURI.mb_str(), *theHandle, theURI.length() + 1);
361
362 //create the movie from the handle that refers to the URI
363 err = NewMovieFromDataRef(&m_movie, newMovieActive,
364 NULL, theHandle,
365 URLDataHandlerSubType);
366
367 DisposeHandle(theHandle);
368
369 if (err != noErr)
370 return false;
371
372 //preroll movie for streaming
373 //TODO:Async this using threads?
374 TimeValue timeNow;
375 Fixed playRate;
376 timeNow = GetMovieTime(m_movie, NULL);
377 playRate = GetMoviePreferredRate(m_movie);
378 PrePrerollMovie(m_movie, timeNow, playRate, NULL, NULL);
379 PrerollMovie(m_movie, timeNow, playRate);
380 SetMovieRate(m_movie, playRate);
381
382 FinishLoad();
383
384 return ::GetMoviesError() == noErr;
385 }
386
387 //---------------------------------------------------------------------------
388 // wxQTMediaBackend::FinishLoad
389 //
390 // 1) Create the movie timer
391 // 2) Get real size of movie for GetBestSize/sizers
392 // 3) See if there is video in the movie, and if so then either
393 // SetMovieGWorld if < 10.2 or use Native CreateMovieControl
394 // 4) Set the movie time scale to something usable so that seeking
395 // etc. will work correctly
396 // 5) Refresh parent window
397 //---------------------------------------------------------------------------
398 void wxQTMediaBackend::FinishLoad()
399 {
400 m_timer = new _wxQTTimer(m_movie, (wxQTMediaBackend*) this);
401 wxASSERT(m_timer);
402
403 //get the real size of the movie
404 Rect outRect;
405 ::GetMovieNaturalBoundsRect (m_movie, &outRect);
406 wxASSERT(::GetMoviesError() == noErr);
407
408 m_bestSize.x = outRect.right - outRect.left;
409 m_bestSize.y = outRect.bottom - outRect.top;
410
411 //reparent movie/*AudioMediaCharacteristic*/
412 if(GetMovieIndTrackType(m_movie, 1,
413 VisualMediaCharacteristic,
414 movieTrackCharacteristic |
415 movieTrackEnabledOnly) != NULL)
416 {
417 #if wxUSE_CREATEMOVIECONTROL
418 //
419 //Native CreateMovieControl QT control (Thanks to Kevin Olliver's
420 //wxQTMovie for some of this).
421 //
422 #define GetControlPeer(whatever) ctrl->m_peer
423 wxMediaCtrl* ctrl = (wxMediaCtrl*) m_ctrl;
424 Rect bounds = wxMacGetBoundsForControl(m_ctrl,
425 m_ctrl->GetPosition(),
426 m_ctrl->GetSize());
427
428 //Dispose of old control for new one
429 if (GetControlPeer(m_ctrl) && GetControlPeer(m_ctrl)->Ok() )
430 GetControlPeer(m_ctrl)->Dispose();
431
432 //Options-
433 //kMovieControlOptionXXX
434 //HideController - hide the movie controller
435 //LocateTopLeft - movie is pinned to top left rather than centered in the control
436 //EnableEditing - Allows programmatic editing and dragn'drop
437 //HandleEditingHI- Installs event stuff for edit menu - forces EnableEditing also
438 //SetKeysEnabled - Allows keyboard input
439 //ManuallyIdled - app handles movie idling rather than internal timer event loop
440 ::CreateMovieControl(
441 (WindowRef)
442 ctrl->MacGetTopLevelWindowRef(), //parent
443 &bounds, //control bounds
444 m_movie, //movie handle
445 kMovieControlOptionHideController
446 | kMovieControlOptionLocateTopLeft
447 | kMovieControlOptionSetKeysEnabled
448 // | kMovieControlOptionManuallyIdled
449 , //flags
450 ctrl->m_peer->GetControlRefAddr() );
451
452 ::EmbedControl(ctrl->m_peer->GetControlRef(), (ControlRef)ctrl->GetParent()->GetHandle());
453 #else
454 //
455 //"Emulation"
456 //
457 SetMovieGWorld(m_movie,
458 (CGrafPtr)
459 GetWindowPort(
460 (WindowRef)
461 m_ctrl->MacGetTopLevelWindowRef()
462 ),
463 nil);
464 #endif
465 }
466
467 //we want millisecond precision
468 ::SetMovieTimeScale(m_movie, 1000);
469 wxASSERT(::GetMoviesError() == noErr);
470
471 //
472 //Here, if the parent of the control has a sizer - we
473 //tell it to recalculate the size of this control since
474 //the user opened a separate media file
475 //
476 m_ctrl->InvalidateBestSize();
477 m_ctrl->GetParent()->Layout();
478 m_ctrl->GetParent()->Refresh();
479 m_ctrl->GetParent()->Update();
480 }
481
482 //---------------------------------------------------------------------------
483 // wxQTMediaBackend::Play
484 //
485 // 1) Start the QT movie
486 // 2) Start the movie loading timer
487 //---------------------------------------------------------------------------
488 bool wxQTMediaBackend::Play()
489 {
490 ::StartMovie(m_movie);
491 m_timer->SetPaused(false);
492 m_timer->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
493 return ::GetMoviesError() == noErr;
494 }
495
496 //---------------------------------------------------------------------------
497 // wxQTMediaBackend::Pause
498 //
499 // 1) Stop the movie
500 // 2) Stop the movie timer
501 //---------------------------------------------------------------------------
502 bool wxQTMediaBackend::Pause()
503 {
504 ::StopMovie(m_movie);
505 m_timer->SetPaused(true);
506 m_timer->Stop();
507 return ::GetMoviesError() == noErr;
508 }
509
510 //---------------------------------------------------------------------------
511 // wxQTMediaBackend::Stop
512 //
513 // 1) Stop the movie
514 // 2) Stop the movie timer
515 // 3) Seek to the beginning of the movie
516 //---------------------------------------------------------------------------
517 bool wxQTMediaBackend::Stop()
518 {
519 m_timer->SetPaused(false);
520 m_timer->Stop();
521
522 ::StopMovie(m_movie);
523 if(::GetMoviesError() != noErr)
524 return false;
525
526 ::GoToBeginningOfMovie(m_movie);
527 return ::GetMoviesError() == noErr;
528 }
529
530 //---------------------------------------------------------------------------
531 // wxQTMediaBackend::GetPlaybackRate
532 //
533 // 1) Get the movie playback rate from ::GetMovieRate
534 //---------------------------------------------------------------------------
535 double wxQTMediaBackend::GetPlaybackRate()
536 {
537 return ( ((double)::GetMovieRate(m_movie)) / 0x10000);
538 }
539
540 //---------------------------------------------------------------------------
541 // wxQTMediaBackend::SetPlaybackRate
542 //
543 // 1) Convert dRate to Fixed and Set the movie rate through SetMovieRate
544 //---------------------------------------------------------------------------
545 bool wxQTMediaBackend::SetPlaybackRate(double dRate)
546 {
547 ::SetMovieRate(m_movie, (Fixed) (dRate * 0x10000));
548 return ::GetMoviesError() == noErr;
549 }
550
551 //---------------------------------------------------------------------------
552 // wxQTMediaBackend::SetPosition
553 //
554 // 1) Create a time record struct (TimeRecord) with appropriate values
555 // 2) Pass struct to SetMovieTime
556 //---------------------------------------------------------------------------
557 bool wxQTMediaBackend::SetPosition(wxLongLong where)
558 {
559 TimeRecord theTimeRecord;
560 memset(&theTimeRecord, 0, sizeof(TimeRecord));
561 theTimeRecord.value.lo = where.GetValue();
562 theTimeRecord.scale = ::GetMovieTimeScale(m_movie);
563 theTimeRecord.base = ::GetMovieTimeBase(m_movie);
564 ::SetMovieTime(m_movie, &theTimeRecord);
565
566 if (::GetMoviesError() != noErr)
567 return false;
568
569 return true;
570 }
571
572 //---------------------------------------------------------------------------
573 // wxQTMediaBackend::GetPosition
574 //
575 // Calls GetMovieTime
576 //---------------------------------------------------------------------------
577 wxLongLong wxQTMediaBackend::GetPosition()
578 {
579 return ::GetMovieTime(m_movie, NULL);
580 }
581
582 //---------------------------------------------------------------------------
583 // wxQTMediaBackend::GetDuration
584 //
585 // Calls GetMovieDuration
586 //---------------------------------------------------------------------------
587 wxLongLong wxQTMediaBackend::GetDuration()
588 {
589 return ::GetMovieDuration(m_movie);
590 }
591
592 //---------------------------------------------------------------------------
593 // wxQTMediaBackend::GetState
594 //
595 // Determines the current state - the timer keeps track of whether or not
596 // we are paused or stopped (if the timer is running we are playing)
597 //---------------------------------------------------------------------------
598 wxMediaState wxQTMediaBackend::GetState()
599 {
600 if ( !m_timer || (m_timer->IsRunning() == false &&
601 m_timer->GetPaused() == false) )
602 return wxMEDIASTATE_STOPPED;
603
604 if( m_timer->IsRunning() )
605 return wxMEDIASTATE_PLAYING;
606 else
607 return wxMEDIASTATE_PAUSED;
608 }
609
610 //---------------------------------------------------------------------------
611 // wxQTMediaBackend::Cleanup
612 //
613 // Diposes of the movie timer, Control if native, and stops and disposes
614 // of the QT movie
615 //---------------------------------------------------------------------------
616 void wxQTMediaBackend::Cleanup()
617 {
618 delete m_timer;
619 m_timer = NULL;
620
621 #if wxUSE_CREATEMOVIECONTROL
622 DisposeControl(((wxMediaCtrl*)m_ctrl)->m_peer->GetControlRef());
623 #endif
624
625 StopMovie(m_movie);
626 DisposeMovie(m_movie);
627 }
628
629 //---------------------------------------------------------------------------
630 // wxQTMediaBackend::GetVideoSize
631 //
632 // Returns the actual size of the QT movie
633 //---------------------------------------------------------------------------
634 wxSize wxQTMediaBackend::GetVideoSize() const
635 {
636 return m_bestSize;
637 }
638
639 //---------------------------------------------------------------------------
640 // wxQTMediaBackend::Move
641 //
642 // We need to do this even when using native qt control because
643 // CreateMovieControl is broken in this regard...
644 //---------------------------------------------------------------------------
645 void wxQTMediaBackend::Move(int x, int y, int w, int h)
646 {
647 #if !wxUSE_CREATEMOVIECONTROL
648 if(m_timer)
649 {
650 if ( m_ctrl )
651 {
652 m_ctrl->GetParent()->MacWindowToRootWindow(&x, &y);
653 }
654
655 Rect theRect = {y, x, y+h, x+w};
656
657 ::SetMovieBox(m_movie, &theRect);
658 wxASSERT(::GetMoviesError() == noErr);
659 }
660 #else
661 if(m_timer && m_ctrl)
662 {
663 m_ctrl->GetParent()->MacWindowToRootWindow(&x, &y);
664
665 ::MoveControl( (ControlRef) m_ctrl->GetHandle(), x, y );
666 m_ctrl->GetParent()->Refresh();
667 m_ctrl->GetParent()->Update();
668 }
669 #endif
670 }
671
672
673 //in source file that contains stuff you don't directly use
674 #include "wx/html/forcelnk.h"
675 FORCE_LINK_ME(basewxmediabackends);
676
677 #endif //wxUSE_MEDIACTRL