1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/moviectrl.cpp
3 // Purpose: wxMovieCtrl MSW
4 // Author: Ryan Norton <wxprojects@comcast.net>
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 //#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 //#pragma implementation "moviectrl.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #define wxUSE_MOVIECTRL 1
27 #include "wx/moviectrl.h"
28 #include "wx/msw/ole/oleutils.h" //for wxBasicString
32 IMPLEMENT_CLASS(wxMovieCtrl
, wxControl
);
33 IMPLEMENT_DYNAMIC_CLASS(wxMovieEvent
, wxEvent
);
34 DEFINE_EVENT_TYPE(wxEVT_MOVIE_FINISHED
);
36 #define SAFE_RELEASE(x) { if (x) x->Release(); x = NULL; }
38 #define WM_GRAPHNOTIFY WM_USER+13
41 #define wxDSVERIFY(x) wxASSERT( SUCCEEDED ((x)) )
43 #define wxDSVERIFY(x) (x)
46 //it's there someplace :)
47 extern "C" WXDLLIMPEXP_BASE HWND
48 wxCreateHiddenWindow(LPCTSTR
*pclassname
, LPCTSTR classname
, WNDPROC wndproc
);
50 bool wxMovieCtrl::Create(wxWindow
* parent
, wxWindowID id
, const wxString
& fileName
,
51 const wxString
& label
, const wxPoint
& pos
, const wxSize
& size
,
52 long style
, const wxString
& name
)
55 IGraphBuilder
*& pGB
= (IGraphBuilder
*&) m_pGB
;
56 IMediaControl
*& pMC
= (IMediaControl
*&) m_pMC
;
57 IMediaEventEx
*& pME
= (IMediaEventEx
*&) m_pME
;
58 IVideoWindow
*& pVW
= (IVideoWindow
*&) m_pVW
;
59 IBasicAudio
*& pBA
= (IBasicAudio
*&) m_pBA
;
60 IBasicVideo
*& pBV
= (IBasicVideo
*&) m_pBV
;
61 IMediaSeeking
*& pMS
= (IMediaSeeking
*&) m_pMS
;
63 //create our filter graph
64 CoCreateInstance(CLSID_FilterGraph
, NULL
, CLSCTX_INPROC_SERVER
,
65 IID_IGraphBuilder
, (void**)&pGB
);
67 //load the graph & render
68 if (FAILED(pGB
->RenderFile(fileName
.wc_str(wxConvLocal
), NULL
)))
70 wxFAIL_MSG("Could not load movie!");
74 //get the interfaces, all of them
75 wxDSVERIFY( pGB
->QueryInterface(IID_IMediaControl
, (void**)&pMC
) );
76 wxDSVERIFY( pGB
->QueryInterface(IID_IMediaEventEx
, (void**)&pME
) );
77 wxDSVERIFY( pGB
->QueryInterface(IID_IMediaSeeking
, (void**)&pMS
) );
78 wxDSVERIFY( pGB
->QueryInterface(IID_IVideoWindow
, (void**)&pVW
) );
79 wxDSVERIFY( pGB
->QueryInterface(IID_IBasicAudio
, (void**)&pBA
) );
80 wxDSVERIFY( pGB
->QueryInterface(IID_IBasicVideo
, (void**)&pBV
) );
82 //get the _actual_ size of the movie & remember it
83 long nX
, nY
, nSX
, nSY
;
84 if (FAILED(pVW
->GetWindowPosition(&nX
,&nY
,&nSX
,&nSY
)))
90 this->Connect( wxID_ANY
,
92 (wxObjectEventFunction
) (wxEventFunction
) (wxSizeEventFunction
) &wxMovieCtrl::OnSize
);
99 //do some window stuff - ORDER IS IMPORTANT
101 if ( !wxControl::Create(parent
, id
, pos
, size
, wxNO_BORDER
| wxCLIP_CHILDREN
, wxDefaultValidator
, name
) )
104 //TODO: Connect() here instead of message maps
106 //Set our background color to black by default
107 SetBackgroundColour(*wxBLACK
);
111 wxDSVERIFY( pVW
->put_Owner((OAHWND
)this->GetHandle()) );
112 wxDSVERIFY( pVW
->put_WindowStyle(WS_CHILD
| WS_CLIPSIBLINGS
) );
113 // wxDSVERIFY( pME->SetNotifyWindow((OAHWND)this->GetHandle(), WM_GRAPHNOTIFY, 0) );
114 wxDSVERIFY( pVW
->put_Visible(OATRUE
) ); //OATRUE actually == -1 :)
117 //set the time format
118 wxDSVERIFY( pMS
->SetTimeFormat(&TIME_FORMAT_MEDIA_TIME
) );
126 void wxMovieCtrl::SetLabel(const wxString
& label
)
128 wxControl::SetLabel(label
);
130 IVideoWindow
*& pVW
= (IVideoWindow
*&) m_pVW
;
132 //wxBasicString will have a null string on an
133 //empty wxString - gotta love those workarounds!!
134 if(!label
.empty() && m_bVideo
)
136 wxBasicString
theBasicString(label
.mb_str());
137 wxDSVERIFY( pVW
->put_Caption(theBasicString
.Get()) );
141 bool wxMovieCtrl::Play()
143 return SUCCEEDED( ((IMediaControl
*&)m_pMC
)->Run() );
146 bool wxMovieCtrl::Pause()
148 return SUCCEEDED( ((IMediaControl
*&)m_pMC
)->Pause() );
151 bool wxMovieCtrl::Stop()
153 return SUCCEEDED( ((IMediaControl
*&)m_pMC
)->Stop() );
158 bool wxMovieCtrl::Seek(const wxTimeSpan
& where
)
160 //DS uses 100 nanos - so we need a 10 mult
161 LONGLONG pos
= ((size_t)where
.GetMilliseconds().ToLong()) * 10;
163 return SUCCEEDED( ((IMediaSeeking
*&)m_pMS
)->SetPositions(
165 AM_SEEKING_AbsolutePositioning
,
167 AM_SEEKING_NoPositioning
171 wxTimeSpan
wxMovieCtrl::Tell()
173 LONGLONG outCur
, outStop
;
174 wxDSVERIFY( ((IMediaSeeking
*&)m_pMS
)->GetPositions(&outCur
, &outStop
) );
179 wxTimeSpan
wxMovieCtrl::Length()
181 LONGLONG outDuration
;
182 wxDSVERIFY( ((IMediaSeeking
*&)m_pMS
)->GetDuration(&outDuration
) );
187 #endif // wxUSE_DATETIME
189 wxMovieCtrlState
wxMovieCtrl::GetState()
192 OAFilterState theState
;
193 hr
= ((IMediaControl
*&)m_pMC
)->GetState(INFINITE
, &theState
);
195 wxASSERT( SUCCEEDED(hr
) );
197 //MSW state is the same as ours
199 //State_Paused = State_Stopped + 1,
200 //State_Running = State_Paused + 1
202 return (wxMovieCtrlState
) theState
;
205 double wxMovieCtrl::GetPlaybackRate()
208 wxDSVERIFY( ((IMediaSeeking
*&)m_pMS
)->GetRate(&dRate
) );
212 bool wxMovieCtrl::SetPlaybackRate(double dRate
)
214 return SUCCEEDED( ((IMediaSeeking
*&)m_pMS
)->SetRate(dRate
) );
217 WXLRESULT
wxMovieCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
220 // IMediaControl*& pMC = (IMediaControl*&) m_pMC;
221 IMediaEventEx
*& pME
= (IMediaEventEx
*&) m_pME
;
222 // IMediaSeeking*& pMS = (IMediaSeeking*&) m_pMS;
224 if (nMsg
== WM_GRAPHNOTIFY
)
226 LONG evCode
, evParam1
, evParam2
;
229 // Process all queued events
230 while(SUCCEEDED(pME
->GetEvent(&evCode
, (LONG_PTR
*) &evParam1
,
231 (LONG_PTR
*) &evParam2
, 0)
235 // Free memory associated with callback, since we're not using it
236 hr
= pME
->FreeEventParams(evCode
, evParam1
, evParam2
);
238 // If this is the end of the clip, notify handler
239 if(EC_COMPLETE
== evCode
)
241 wxMovieEvent
theEvent(wxEVT_MOVIE_FINISHED
, this->GetId());
242 GetParent()->ProcessEvent(theEvent
);
246 // Reset to first frame of movie
247 hr = pMS->SetPositions(&pos, AM_SEEKING_AbsolutePositioning ,
248 NULL, AM_SEEKING_NoPositioning);
252 // for filters that don't support seeking do a rewind
256 wxString::Format(TEXT("Failed(0x%08lx) to stop media clip!\r\n"), hr)
261 hr = hr = pMC->Run();
266 wxString::Format(TEXT("Failed(0x%08lx) to reset media clip!\r\n"), hr)
274 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
276 //pass the event to our parent
277 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
280 wxMovieCtrl::~wxMovieCtrl()
283 IGraphBuilder
*& pGB
= (IGraphBuilder
*&) m_pGB
;
284 IMediaControl
*& pMC
= (IMediaControl
*&) m_pMC
;
285 IMediaEventEx
*& pME
= (IMediaEventEx
*&) m_pME
;
286 IVideoWindow
*& pVW
= (IVideoWindow
*&) m_pVW
;
287 IBasicAudio
*& pBA
= (IBasicAudio
*&) m_pBA
;
288 IBasicVideo
*& pBV
= (IBasicVideo
*&) m_pBV
;
289 IMediaSeeking
*& pMS
= (IMediaSeeking
*&) m_pMS
;
291 // Hide then disown the window
294 pVW
->put_Visible(OAFALSE
); //OSFALSE == 0
295 pVW
->put_Owner(NULL
);
298 // Release and zero DirectShow interfaces
308 wxSize
wxMovieCtrl::DoGetBestSize() const
313 void wxMovieCtrl::OnSize(wxSizeEvent
& evt
)
315 IVideoWindow
*& pVW
= (IVideoWindow
*&) m_pVW
;
316 wxDSVERIFY( pVW
->SetWindowPosition(0, 0, evt
.GetSize().GetWidth(), evt
.GetSize().GetHeight()) );
321 #endif //wxUSE_MOVIECTRL