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"
25 #include "wx/moviectrl.h"
26 #include "wx/msw/ole/oleutils.h" //for wxBasicString
30 IMPLEMENT_CLASS(wxMovieCtrl
, wxControl
);
32 #define SAFE_RELEASE(x) { if (x) x->Release(); x = NULL; }
34 #define WM_GRAPHNOTIFY WM_USER+13
37 #define wxDSVERIFY(x) wxASSERT( SUCCEEDED ((x)) )
39 #define wxDSVERIFY(x) (x)
42 BEGIN_EVENT_TABLE(wxMovieCtrl
, wxControl
)
43 EVT_SIZE(wxMovieCtrl::OnSize
)
44 // EVT_ACTIVATE(wxMovieCtrl::OnActivate)
47 //it's there someplace :)
48 extern "C" WXDLLIMPEXP_BASE HWND
49 wxCreateHiddenWindow(LPCTSTR
*pclassname
, LPCTSTR classname
, WNDPROC wndproc
);
51 bool wxMovieCtrl::Create(wxWindow
* parent
, wxWindowID id
, const wxString
& fileName
,
52 const wxString
& label
, const wxPoint
& pos
, const wxSize
& size
,
53 long style
, const wxString
& name
)
56 IGraphBuilder
*& pGB
= (IGraphBuilder
*&) m_pGB
;
57 IMediaControl
*& pMC
= (IMediaControl
*&) m_pMC
;
58 IMediaEventEx
*& pME
= (IMediaEventEx
*&) m_pME
;
59 IVideoWindow
*& pVW
= (IVideoWindow
*&) m_pVW
;
60 IBasicAudio
*& pBA
= (IBasicAudio
*&) m_pBA
;
61 IBasicVideo
*& pBV
= (IBasicVideo
*&) m_pBV
;
62 IMediaSeeking
*& pMS
= (IMediaSeeking
*&) m_pMS
;
64 //create our filter graph
65 CoCreateInstance(CLSID_FilterGraph
, NULL
, CLSCTX_INPROC_SERVER
,
66 IID_IGraphBuilder
, (void**)&pGB
);
68 //load the graph & render
69 if (FAILED(pGB
->RenderFile(fileName
.wc_str(wxConvLocal
), NULL
)))
71 wxFAIL_MSG("Could not load movie!");
75 //get the interfaces, all of them
76 wxDSVERIFY( pGB
->QueryInterface(IID_IMediaControl
, (void**)&pMC
) );
77 wxDSVERIFY( pGB
->QueryInterface(IID_IMediaEventEx
, (void**)&pME
) );
78 wxDSVERIFY( pGB
->QueryInterface(IID_IMediaSeeking
, (void**)&pMS
) );
79 wxDSVERIFY( pGB
->QueryInterface(IID_IVideoWindow
, (void**)&pVW
) );
80 wxDSVERIFY( pGB
->QueryInterface(IID_IBasicAudio
, (void**)&pBA
) );
81 wxDSVERIFY( pGB
->QueryInterface(IID_IBasicVideo
, (void**)&pBV
) );
83 //get the _actual_ size of the movie & remember it
84 long nX
, nY
, nSX
, nSY
;
85 pVW
->GetWindowPosition(&nX
,&nY
,&nSX
,&nSY
);
90 //do some window stuff - ORDER IS IMPORTANT
92 if ( !wxControl::Create(parent
, id
, pos
, size
, wxNO_BORDER
| wxCLIP_CHILDREN
, wxDefaultValidator
, name
) )
95 //Set our background color to black by default
96 SetBackgroundColour(*wxBLACK
);
98 wxDSVERIFY( pVW
->put_Owner((OAHWND
)this->GetHandle()) );
99 wxDSVERIFY( pVW
->put_WindowStyle(WS_CHILD
| WS_CLIPSIBLINGS
) );
100 // wxDSVERIFY( pME->SetNotifyWindow((OAHWND)this->GetHandle(), WM_GRAPHNOTIFY, 0) );
101 wxDSVERIFY( pVW
->put_Visible(OATRUE
) ); //OATRUE actually == -1 :)
103 //set the time format
104 wxDSVERIFY( pMS
->SetTimeFormat(&TIME_FORMAT_MEDIA_TIME
) );
112 void wxMovieCtrl::SetLabel(const wxString
& label
)
114 wxControl::SetLabel(label
);
116 IVideoWindow
*& pVW
= (IVideoWindow
*&) m_pVW
;
118 //wxBasicString will have a null string on an
119 //empty wxString - gotta love those workarounds!!
122 wxBasicString
theBasicString(label
.mb_str());
123 wxDSVERIFY( pVW
->put_Caption(theBasicString
.Get()) );
127 bool wxMovieCtrl::Play()
129 return SUCCEEDED( ((IMediaControl
*&)m_pMC
)->Run() );
132 bool wxMovieCtrl::Pause()
134 return SUCCEEDED( ((IMediaControl
*&)m_pMC
)->Pause() );
137 bool wxMovieCtrl::Stop()
139 return SUCCEEDED( ((IMediaControl
*&)m_pMC
)->Stop() );
144 bool wxMovieCtrl::Seek(const wxTimeSpan
& where
)
146 //DS uses 100 nanos - so we need a 10 mult
147 LONGLONG pos
= ((size_t)where
.GetMilliseconds().ToLong()) * 10;
149 return SUCCEEDED( ((IMediaSeeking
*&)m_pMS
)->SetPositions(
151 AM_SEEKING_AbsolutePositioning
,
153 AM_SEEKING_NoPositioning
157 #endif // wxUSE_DATETIME
159 wxMovieCtrlState
wxMovieCtrl::GetState()
162 OAFilterState theState
;
163 hr
= ((IMediaControl
*&)m_pMC
)->GetState(INFINITE
, &theState
);
165 wxASSERT( SUCCEEDED(hr
) );
167 //MSW state is the same as ours
169 //State_Paused = State_Stopped + 1,
170 //State_Running = State_Paused + 1
172 return (wxMovieCtrlState
) theState
;
175 //WXLRESULT wxMovieCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
179 IMediaControl*& pMC = (IMediaControl*&) m_pMC;
180 IMediaEventEx*& pME = (IMediaEventEx*&) m_pME;
181 IMediaSeeking*& pMS = (IMediaSeeking*&) m_pMS;
183 if (nMsg == WM_GRAPHNOTIFY)
185 LONG evCode, evParam1, evParam2;
192 // Process all queued events
193 while(SUCCEEDED(pME->GetEvent(&evCode, (LONG_PTR *) &evParam1,
194 (LONG_PTR *) &evParam2, 0)
198 // Free memory associated with callback, since we're not using it
199 hr = pME->FreeEventParams(evCode, evParam1, evParam2);
201 // If this is the end of the clip, reset to beginning
202 if(EC_COMPLETE == evCode)
206 // Reset to first frame of movie
207 hr = pMS->SetPositions(&pos, AM_SEEKING_AbsolutePositioning ,
208 NULL, AM_SEEKING_NoPositioning);
212 // for filters that don't support seeking do a rewind
216 wxString::Format(TEXT("Failed(0x%08lx) to stop media clip!\r\n"), hr)
221 hr = hr = pMC->Run();
226 wxString::Format(TEXT("Failed(0x%08lx) to reset media clip!\r\n"), hr)
233 return wxControl::MSWDefWindowProc(nMsg, wParam, lParam);
236 //pass the event to our parent
237 // return wxControl::MSWWindowProc(nMsg, wParam, lParam);
240 wxMovieCtrl::~wxMovieCtrl()
243 IGraphBuilder
*& pGB
= (IGraphBuilder
*&) m_pGB
;
244 IMediaControl
*& pMC
= (IMediaControl
*&) m_pMC
;
245 IMediaEventEx
*& pME
= (IMediaEventEx
*&) m_pME
;
246 IVideoWindow
*& pVW
= (IVideoWindow
*&) m_pVW
;
247 IBasicAudio
*& pBA
= (IBasicAudio
*&) m_pBA
;
248 IBasicVideo
*& pBV
= (IBasicVideo
*&) m_pBV
;
249 IMediaSeeking
*& pMS
= (IMediaSeeking
*&) m_pMS
;
251 // Hide then disown the window
254 pVW
->put_Visible(OAFALSE
); //OSFALSE == 0
255 pVW
->put_Owner(NULL
);
258 // Release and zero DirectShow interfaces
268 wxSize
wxMovieCtrl::DoGetBestSize() const
277 void wxMovieCtrl::OnSize(wxSizeEvent
& evt
)
279 IVideoWindow
*& pVW
= (IVideoWindow
*&) m_pVW
;
280 wxDSVERIFY( pVW
->SetWindowPosition(0, 0, evt
.GetSize().GetWidth(), evt
.GetSize().GetHeight()) );
285 void wxMovieCtrl::OnActivate(wxActivateEvent& evt)
289 //HACK: Make the window show :)
295 #endif //wxUSE_MOVIECTRL