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