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