]> git.saurik.com Git - wxWidgets.git/blame - src/common/mediactrlcmn.cpp
fixed reverted #if condition around wxDoLogTraceWchar
[wxWidgets.git] / src / common / mediactrlcmn.cpp
CommitLineData
ff4aedc5 1/////////////////////////////////////////////////////////////////////////////
32d4c30a 2// Name: src/common/mediactrl.cpp
ff4aedc5
RN
3// Purpose: wxMediaCtrl common code
4// Author: Ryan Norton <wxprojects@comcast.net>
5// Modified by:
6// Created: 11/07/04
7// RCS-ID: $Id$
8// Copyright: (c) Ryan Norton
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
557002cf
VZ
12// TODO: Platform specific backend defaults?
13
ff4aedc5 14//===========================================================================
70eca0fa 15// Declarations
ff4aedc5
RN
16//===========================================================================
17
18//---------------------------------------------------------------------------
70eca0fa 19// Includes
ff4aedc5
RN
20//---------------------------------------------------------------------------
21
ff4aedc5
RN
22#include "wx/wxprec.h"
23
24#ifdef __BORLANDC__
32d4c30a
WS
25 #pragma hdrstop
26#endif
27
28#if wxUSE_MEDIACTRL
29
30#ifndef WX_PRECOMP
31 #include "wx/hash.h"
70eca0fa 32 #include "wx/log.h"
ff4aedc5
RN
33#endif
34
ff4aedc5 35#include "wx/mediactrl.h"
ff4aedc5
RN
36
37//===========================================================================
38//
39// Implementation
226ec5a7 40//
ff4aedc5
RN
41//===========================================================================
42
43//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
44// RTTI and Event implementations
45//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
46
f74fd79b 47IMPLEMENT_CLASS(wxMediaCtrl, wxControl)
557002cf
VZ
48DEFINE_EVENT_TYPE(wxEVT_MEDIA_STATECHANGED)
49DEFINE_EVENT_TYPE(wxEVT_MEDIA_PLAY)
50DEFINE_EVENT_TYPE(wxEVT_MEDIA_PAUSE)
f74fd79b
VZ
51IMPLEMENT_CLASS(wxMediaBackend, wxObject)
52IMPLEMENT_DYNAMIC_CLASS(wxMediaEvent, wxEvent)
53DEFINE_EVENT_TYPE(wxEVT_MEDIA_FINISHED)
54DEFINE_EVENT_TYPE(wxEVT_MEDIA_LOADED)
55DEFINE_EVENT_TYPE(wxEVT_MEDIA_STOP)
ff4aedc5
RN
56
57//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
58//
59// wxMediaCtrl
60//
61//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
62
38647faa
WS
63//---------------------------------------------------------------------------
64// wxMediaBackend Destructor
65//
66// This is here because the DARWIN gcc compiler badly screwed up and
67// needs the destructor implementation in the source
68//---------------------------------------------------------------------------
69wxMediaBackend::~wxMediaBackend()
70{
71}
72
ff4aedc5
RN
73//---------------------------------------------------------------------------
74// wxMediaCtrl::Create (file version)
75// wxMediaCtrl::Create (URL version)
76//
77// Searches for a backend that is installed on the system (backends
78// starting with lower characters in the alphabet are given priority),
79// and creates the control from it
226ec5a7 80//
ff4aedc5
RN
81// This searches by searching the global RTTI hashtable, class by class,
82// attempting to call CreateControl on each one found that is a derivative
9180b535 83// of wxMediaBackend - if it succeeded Create returns true, otherwise
ff4aedc5
RN
84// it keeps iterating through the hashmap.
85//---------------------------------------------------------------------------
86bool wxMediaCtrl::Create(wxWindow* parent, wxWindowID id,
87 const wxString& fileName,
226ec5a7 88 const wxPoint& pos,
ff4aedc5 89 const wxSize& size,
226ec5a7 90 long style,
ff4aedc5
RN
91 const wxString& szBackend,
92 const wxValidator& validator,
93 const wxString& name)
94{
95 if(!szBackend.empty())
96 {
38647faa
WS
97 wxClassInfo* pClassInfo = wxClassInfo::FindClass(szBackend);
98
99 if(!pClassInfo || !DoCreate(pClassInfo, parent, id,
100 pos, size, style, validator, name))
ff4aedc5
RN
101 {
102 m_imp = NULL;
103 return false;
104 }
105
106 if (!fileName.empty())
107 {
4ac61319 108 if (!Load(fileName))
ff4aedc5
RN
109 {
110 delete m_imp;
111 m_imp = NULL;
112 return false;
113 }
114 }
115
170acdc9 116 SetInitialSize(size);
ff4aedc5
RN
117 return true;
118 }
119 else
120 {
644cb537 121 wxClassInfo::const_iterator it = wxClassInfo::begin_classinfo();
ff4aedc5 122
644cb537 123 const wxClassInfo* classInfo;
ff4aedc5 124
644cb537 125 while((classInfo = NextBackend(&it)) != NULL)
ff4aedc5 126 {
644cb537 127 wxLogMessage( classInfo->GetClassName() );
ff4aedc5
RN
128 if(!DoCreate(classInfo, parent, id,
129 pos, size, style, validator, name))
226ec5a7 130 continue;
ff4aedc5
RN
131
132 if (!fileName.empty())
133 {
4ac61319
RN
134 if (Load(fileName))
135 {
170acdc9 136 SetInitialSize(size);
ff4aedc5 137 return true;
4ac61319 138 }
ff4aedc5
RN
139 else
140 delete m_imp;
141 }
142 else
4ac61319 143 {
170acdc9 144 SetInitialSize(size);
ff4aedc5 145 return true;
4ac61319 146 }
ff4aedc5
RN
147 }
148
149 m_imp = NULL;
150 return false;
151 }
152}
153
154bool wxMediaCtrl::Create(wxWindow* parent, wxWindowID id,
38647faa
WS
155 const wxURI& location,
156 const wxPoint& pos,
157 const wxSize& size,
158 long style,
159 const wxString& szBackend,
160 const wxValidator& validator,
161 const wxString& name)
ff4aedc5
RN
162{
163 if(!szBackend.empty())
164 {
38647faa
WS
165 wxClassInfo* pClassInfo = wxClassInfo::FindClass(szBackend);
166 if(!pClassInfo || !DoCreate(pClassInfo, parent, id,
167 pos, size, style, validator, name))
ff4aedc5
RN
168 {
169 m_imp = NULL;
170 return false;
171 }
172
4ac61319 173 if (!Load(location))
ff4aedc5
RN
174 {
175 delete m_imp;
176 m_imp = NULL;
177 return false;
178 }
179
170acdc9 180 SetInitialSize(size);
ff4aedc5
RN
181 return true;
182 }
183 else
184 {
644cb537 185 wxClassInfo::const_iterator it = wxClassInfo::begin_classinfo();
ff4aedc5 186
644cb537 187 const wxClassInfo* classInfo;
ff4aedc5 188
644cb537 189 while((classInfo = NextBackend(&it)) != NULL)
ff4aedc5
RN
190 {
191 if(!DoCreate(classInfo, parent, id,
192 pos, size, style, validator, name))
226ec5a7 193 continue;
ff4aedc5 194
4ac61319
RN
195 if (Load(location))
196 {
170acdc9 197 SetInitialSize(size);
ff4aedc5 198 return true;
4ac61319 199 }
ff4aedc5
RN
200 else
201 delete m_imp;
ff4aedc5
RN
202 }
203
204 m_imp = NULL;
205 return false;
206 }
207}
208
209//---------------------------------------------------------------------------
210// wxMediaCtrl::DoCreate
211//
212// Attempts to create the control from a backend
213//---------------------------------------------------------------------------
644cb537 214bool wxMediaCtrl::DoCreate(const wxClassInfo* classInfo,
ff4aedc5 215 wxWindow* parent, wxWindowID id,
226ec5a7 216 const wxPoint& pos,
ff4aedc5 217 const wxSize& size,
226ec5a7 218 long style,
ff4aedc5
RN
219 const wxValidator& validator,
220 const wxString& name)
221{
222 m_imp = (wxMediaBackend*)classInfo->CreateObject();
226ec5a7 223
ff4aedc5
RN
224 if( m_imp->CreateControl(this, parent, id, pos, size,
225 style, validator, name) )
226 {
ff4aedc5
RN
227 return true;
228 }
226ec5a7 229
ff4aedc5
RN
230 delete m_imp;
231 return false;
232}
233
234//---------------------------------------------------------------------------
557002cf 235// wxMediaCtrl::NextBackend (static)
ff4aedc5
RN
236//
237//
238// Search through the RTTI hashmap one at a
239// time, attempting to create each derivative
240// of wxMediaBackend
226ec5a7 241//
ff4aedc5 242//
43e8916f 243// STL isn't compatible with and will have a compilation error
ff4aedc5
RN
244// on a wxNode, however, wxHashTable::compatibility_iterator is
245// incompatible with the old 2.4 stable version - but since
557002cf 246// we're in 2.5+ only we don't need to worry about the new version
ff4aedc5 247//---------------------------------------------------------------------------
644cb537 248const wxClassInfo* wxMediaCtrl::NextBackend(wxClassInfo::const_iterator* it)
ff4aedc5 249{
644cb537
MB
250 for ( wxClassInfo::const_iterator end = wxClassInfo::end_classinfo();
251 *it != end; ++(*it) )
ff4aedc5 252 {
644cb537 253 const wxClassInfo* classInfo = **it;
ff4aedc5
RN
254 if ( classInfo->IsKindOf(CLASSINFO(wxMediaBackend)) &&
255 classInfo != CLASSINFO(wxMediaBackend) )
256 {
257 return classInfo;
258 }
ff4aedc5
RN
259 }
260
261 //
262 // Nope - couldn't successfully find one... fail
263 //
264 return NULL;
265}
266
267
268//---------------------------------------------------------------------------
269// wxMediaCtrl Destructor
270//
271// Free up the backend if it exists
272//---------------------------------------------------------------------------
273wxMediaCtrl::~wxMediaCtrl()
274{
275 if (m_imp)
276 delete m_imp;
277}
278
279//---------------------------------------------------------------------------
280// wxMediaCtrl::Load (file version)
281// wxMediaCtrl::Load (URL version)
c5191fbd
VZ
282// wxMediaCtrl::Load (URL & Proxy version)
283// wxMediaCtrl::Load (wxInputStream version)
ff4aedc5
RN
284//
285// Here we call load of the backend - keeping
286// track of whether it was successful or not - which
287// will determine which later method calls work
288//---------------------------------------------------------------------------
289bool wxMediaCtrl::Load(const wxString& fileName)
290{
291 if(m_imp)
292 return (m_bLoaded = m_imp->Load(fileName));
293 return false;
294}
295
296bool wxMediaCtrl::Load(const wxURI& location)
297{
298 if(m_imp)
299 return (m_bLoaded = m_imp->Load(location));
300 return false;
301}
302
c5191fbd
VZ
303bool wxMediaCtrl::Load(const wxURI& location, const wxURI& proxy)
304{
305 if(m_imp)
306 return (m_bLoaded = m_imp->Load(location, proxy));
307 return false;
308}
309
ff4aedc5
RN
310//---------------------------------------------------------------------------
311// wxMediaCtrl::Play
312// wxMediaCtrl::Pause
313// wxMediaCtrl::Stop
314// wxMediaCtrl::GetPlaybackRate
315// wxMediaCtrl::SetPlaybackRate
9180b535
RN
316// wxMediaCtrl::Seek --> SetPosition
317// wxMediaCtrl::Tell --> GetPosition
318// wxMediaCtrl::Length --> GetDuration
ff4aedc5
RN
319// wxMediaCtrl::GetState
320// wxMediaCtrl::DoGetBestSize
6f8c67e7
JS
321// wxMediaCtrl::SetVolume
322// wxMediaCtrl::GetVolume
c5191fbd
VZ
323// wxMediaCtrl::ShowInterface
324// wxMediaCtrl::GetDownloadProgress
325// wxMediaCtrl::GetDownloadTotal
226ec5a7 326//
ff4aedc5
RN
327// 1) Check to see whether the backend exists and is loading
328// 2) Call the backend's version of the method, returning success
329// if the backend's version succeeds
330//---------------------------------------------------------------------------
331bool wxMediaCtrl::Play()
332{
333 if(m_imp && m_bLoaded)
334 return m_imp->Play();
335 return 0;
336}
337
338bool wxMediaCtrl::Pause()
339{
340 if(m_imp && m_bLoaded)
341 return m_imp->Pause();
342 return 0;
343}
344
345bool wxMediaCtrl::Stop()
346{
347 if(m_imp && m_bLoaded)
348 return m_imp->Stop();
349 return 0;
350}
351
352double wxMediaCtrl::GetPlaybackRate()
353{
354 if(m_imp && m_bLoaded)
355 return m_imp->GetPlaybackRate();
356 return 0;
357}
358
359bool wxMediaCtrl::SetPlaybackRate(double dRate)
360{
361 if(m_imp && m_bLoaded)
362 return m_imp->SetPlaybackRate(dRate);
363 return false;
364}
365
9180b535 366wxFileOffset wxMediaCtrl::Seek(wxFileOffset where, wxSeekMode mode)
ff4aedc5 367{
9180b535
RN
368 wxFileOffset offset;
369
370 switch (mode)
371 {
372 case wxFromStart:
373 offset = where;
374 break;
375 case wxFromEnd:
376 offset = Length() - where;
377 break;
378// case wxFromCurrent:
379 default:
380 offset = Tell() + where;
381 break;
382 }
383
384 if(m_imp && m_bLoaded && m_imp->SetPosition(offset))
385 return offset;
386 return wxInvalidOffset;
ff4aedc5
RN
387}
388
9180b535 389wxFileOffset wxMediaCtrl::Tell()
ff4aedc5
RN
390{
391 if(m_imp && m_bLoaded)
9180b535
RN
392 return (wxFileOffset) m_imp->GetPosition().ToLong();
393 return wxInvalidOffset;
ff4aedc5
RN
394}
395
9180b535 396wxFileOffset wxMediaCtrl::Length()
ff4aedc5
RN
397{
398 if(m_imp && m_bLoaded)
9180b535
RN
399 return (wxFileOffset) m_imp->GetDuration().ToLong();
400 return wxInvalidOffset;
ff4aedc5
RN
401}
402
403wxMediaState wxMediaCtrl::GetState()
404{
405 if(m_imp && m_bLoaded)
406 return m_imp->GetState();
407 return wxMEDIASTATE_STOPPED;
408}
409
410wxSize wxMediaCtrl::DoGetBestSize() const
411{
412 if(m_imp)
413 return m_imp->GetVideoSize();
c47addef 414 return wxSize(0,0);
ff4aedc5
RN
415}
416
32d4c30a 417double wxMediaCtrl::GetVolume()
6f8c67e7
JS
418{
419 if(m_imp && m_bLoaded)
420 return m_imp->GetVolume();
421 return 0.0;
422}
423
32d4c30a 424bool wxMediaCtrl::SetVolume(double dVolume)
6f8c67e7
JS
425{
426 if(m_imp && m_bLoaded)
427 return m_imp->SetVolume(dVolume);
428 return false;
429}
430
32d4c30a 431bool wxMediaCtrl::ShowPlayerControls(wxMediaCtrlPlayerControls flags)
c5191fbd
VZ
432{
433 if(m_imp)
434 return m_imp->ShowPlayerControls(flags);
435 return false;
436}
437
438wxFileOffset wxMediaCtrl::GetDownloadProgress()
439{
440 if(m_imp && m_bLoaded)
441 return (wxFileOffset) m_imp->GetDownloadProgress().ToLong();
442 return wxInvalidOffset;
443}
444
445wxFileOffset wxMediaCtrl::GetDownloadTotal()
446{
447 if(m_imp && m_bLoaded)
448 return (wxFileOffset) m_imp->GetDownloadTotal().ToLong();
449 return wxInvalidOffset;
450}
451
ff4aedc5
RN
452//---------------------------------------------------------------------------
453// wxMediaCtrl::DoMoveWindow
226ec5a7 454//
ff4aedc5
RN
455// 1) Call parent's version so that our control's window moves where
456// it's supposed to
457// 2) If the backend exists and is loaded, move the video
458// of the media to where our control's window is now located
459//---------------------------------------------------------------------------
460void wxMediaCtrl::DoMoveWindow(int x, int y, int w, int h)
461{
462 wxControl::DoMoveWindow(x,y,w,h);
463
464 if(m_imp)
465 m_imp->Move(x, y, w, h);
466}
467
0c81ef7f
SC
468//---------------------------------------------------------------------------
469// wxMediaCtrl::MacVisibilityChanged
470//---------------------------------------------------------------------------
471#ifdef __WXMAC__
472void wxMediaCtrl::MacVisibilityChanged()
473{
474 wxControl::MacVisibilityChanged();
32d4c30a 475
0c81ef7f 476 if(m_imp)
32d4c30a 477 m_imp->MacVisibilityChanged();
0c81ef7f
SC
478}
479#endif
480
bf354396
VZ
481//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
482//
483// wxMediaBackendCommonBase
484//
485//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
486
487void wxMediaBackendCommonBase::NotifyMovieSizeChanged()
488{
489 // our best size changed after opening a new file
490 m_ctrl->InvalidateBestSize();
491 m_ctrl->SetSize(m_ctrl->GetSize());
492
493 // if the parent of the control has a sizer ask it to refresh our size
494 wxWindow * const parent = m_ctrl->GetParent();
495 if ( parent->GetSizer() )
496 {
497 m_ctrl->GetParent()->Layout();
498 m_ctrl->GetParent()->Refresh();
499 m_ctrl->GetParent()->Update();
500 }
501}
502
503void wxMediaBackendCommonBase::NotifyMovieLoaded()
504{
505 NotifyMovieSizeChanged();
506
507 // notify about movie being fully loaded
508 QueueEvent(wxEVT_MEDIA_LOADED);
509}
510
511bool wxMediaBackendCommonBase::SendStopEvent()
512{
513 wxMediaEvent theEvent(wxEVT_MEDIA_STOP, m_ctrl->GetId());
514
515 return !m_ctrl->ProcessEvent(theEvent) || theEvent.IsAllowed();
516}
517
518void wxMediaBackendCommonBase::QueueEvent(wxEventType evtType)
519{
520 wxMediaEvent theEvent(evtType, m_ctrl->GetId());
521 m_ctrl->AddPendingEvent(theEvent);
522}
523
557002cf
VZ
524void wxMediaBackendCommonBase::QueuePlayEvent()
525{
526 QueueEvent(wxEVT_MEDIA_STATECHANGED);
527 QueueEvent(wxEVT_MEDIA_PLAY);
528}
529
530void wxMediaBackendCommonBase::QueuePauseEvent()
531{
532 QueueEvent(wxEVT_MEDIA_STATECHANGED);
533 QueueEvent(wxEVT_MEDIA_PAUSE);
534}
535
536void wxMediaBackendCommonBase::QueueStopEvent()
537{
538 QueueEvent(wxEVT_MEDIA_STATECHANGED);
539 QueueEvent(wxEVT_MEDIA_STOP);
540}
541
542
543//
544// Force link default backends in -
545// see http://wiki.wxwidgets.org/wiki.pl?RTTI
546//
02250fa1 547#include "wx/html/forcelnk.h"
ff4aedc5 548
557002cf 549#ifdef __WXMSW__ // MSW has huge backends so we do it seperately
f74fd79b
VZ
550FORCE_LINK(wxmediabackend_am)
551FORCE_LINK(wxmediabackend_wmp10)
557002cf 552#else
f74fd79b 553FORCE_LINK(basewxmediabackends)
557002cf 554#endif
ff4aedc5 555
32d4c30a 556#endif //wxUSE_MEDIACTRL