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