]> git.sesse.net Git - vlc/blob - activex/plugin.cpp
- added support for non OLE containers, such as Windows Script Host WScript.CreateObject.
[vlc] / activex / plugin.cpp
1 /*****************************************************************************
2  * plugin.cpp: ActiveX control for VLC
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  *
6  * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #include "plugin.h"
24
25 #include "oleobject.h"
26 #include "olecontrol.h"
27 #include "oleinplaceobject.h"
28 #include "oleinplaceactiveobject.h"
29 #include "persistpropbag.h"
30 #include "persiststreaminit.h"
31 #include "persiststorage.h"
32 #include "provideclassinfo.h"
33 #include "connectioncontainer.h"
34 #include "objectsafety.h"
35 #include "vlccontrol.h"
36 #include "viewobject.h"
37 #include "dataobject.h"
38
39 #include "utils.h"
40
41 #include <string.h>
42 #include <winreg.h>
43 #include <winuser.h>
44 #include <servprov.h>
45 #include <shlwapi.h>
46 #include <wininet.h>
47
48 using namespace std;
49
50 ////////////////////////////////////////////////////////////////////////
51 //class factory
52
53 // {E23FE9C6-778E-49d4-B537-38FCDE4887D8}
54 //const GUID CLSID_VLCPlugin = 
55 //    { 0xe23fe9c6, 0x778e, 0x49d4, { 0xb5, 0x37, 0x38, 0xfc, 0xde, 0x48, 0x87, 0xd8 } };
56
57 static LRESULT CALLBACK VLCInPlaceClassWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
58     switch( uMsg )
59     {
60         case WM_ERASEBKGND:
61             return 1L;
62
63         case WM_PAINT:
64             PAINTSTRUCT ps;
65             if( GetUpdateRect(hWnd, NULL, FALSE) )
66             {
67                 BeginPaint(hWnd, &ps);
68                 EndPaint(hWnd, &ps);
69             }
70             return 0L;
71
72         default:
73             return DefWindowProc(hWnd, uMsg, wParam, lParam);
74     }
75 };
76
77 static LRESULT CALLBACK VLCVideoClassWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
78     VLCPlugin *p_instance = reinterpret_cast<VLCPlugin *>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
79
80     switch( uMsg )
81     {
82         case WM_ERASEBKGND:
83             return 1L;
84
85         case WM_PAINT:
86             PAINTSTRUCT ps;
87             RECT pr;
88             if( GetUpdateRect(hWnd, &pr, FALSE) )
89             {
90                 RECT bounds;
91                 GetClientRect(hWnd, &bounds);
92                 BeginPaint(hWnd, &ps);
93                 p_instance->onPaint(ps.hdc, bounds, pr);
94                 EndPaint(hWnd, &ps);
95             }
96             return 0L;
97
98         default:
99             return DefWindowProc(hWnd, uMsg, wParam, lParam);
100     }
101 };
102
103 VLCPluginClass::VLCPluginClass(LONG *p_class_ref, HINSTANCE hInstance) :
104     _p_class_ref(p_class_ref),
105     _hinstance(hInstance),
106     _inplace_picture(NULL)
107 {
108     WNDCLASS wClass;
109
110     if( ! GetClassInfo(hInstance, getInPlaceWndClassName(), &wClass) )
111     {
112         wClass.style          = CS_NOCLOSE|CS_DBLCLKS;
113         wClass.lpfnWndProc    = VLCInPlaceClassWndProc;
114         wClass.cbClsExtra     = 0;
115         wClass.cbWndExtra     = 0;
116         wClass.hInstance      = hInstance;
117         wClass.hIcon          = NULL;
118         wClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
119         wClass.hbrBackground  = NULL;
120         wClass.lpszMenuName   = NULL;
121         wClass.lpszClassName  = getInPlaceWndClassName();
122        
123         _inplace_wndclass_atom = RegisterClass(&wClass);
124     }
125     else
126     {
127         _inplace_wndclass_atom = 0;
128     }
129
130     if( ! GetClassInfo(hInstance, getVideoWndClassName(), &wClass) )
131     {
132         wClass.style          = CS_NOCLOSE;
133         wClass.lpfnWndProc    = VLCVideoClassWndProc;
134         wClass.cbClsExtra     = 0;
135         wClass.cbWndExtra     = 0;
136         wClass.hInstance      = hInstance;
137         wClass.hIcon          = NULL;
138         wClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
139         wClass.hbrBackground  = NULL;
140         wClass.lpszMenuName   = NULL;
141         wClass.lpszClassName  = getVideoWndClassName();
142        
143         _video_wndclass_atom = RegisterClass(&wClass);
144     }
145     else
146     {
147         _video_wndclass_atom = 0;
148     }
149
150     HBITMAP hbitmap = (HBITMAP)LoadImage(getHInstance(), TEXT("INPLACE-PICT"), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
151     if( NULL != hbitmap )
152     {
153         PICTDESC pictDesc;
154
155         pictDesc.cbSizeofstruct = sizeof(PICTDESC);
156         pictDesc.picType        = PICTYPE_BITMAP;
157         pictDesc.bmp.hbitmap    = hbitmap;
158         pictDesc.bmp.hpal       = NULL;
159
160         if( FAILED(OleCreatePictureIndirect(&pictDesc, IID_IPicture, TRUE, reinterpret_cast<LPVOID*>(&_inplace_picture))) )
161             _inplace_picture = NULL;
162     }
163     AddRef();
164 };
165
166 VLCPluginClass::~VLCPluginClass()
167 {
168     if( 0 != _inplace_wndclass_atom )
169         UnregisterClass(MAKEINTATOM(_inplace_wndclass_atom), _hinstance);
170
171     if( 0 != _video_wndclass_atom )
172         UnregisterClass(MAKEINTATOM(_video_wndclass_atom), _hinstance);
173
174     if( NULL != _inplace_picture )
175         _inplace_picture->Release();
176 };
177
178 STDMETHODIMP VLCPluginClass::QueryInterface(REFIID riid, void **ppv)
179 {
180     if( NULL == ppv )
181         return E_INVALIDARG;
182
183     if( (IID_IUnknown == riid) || (IID_IClassFactory == riid) )
184     {
185         AddRef();
186         *ppv = reinterpret_cast<LPVOID>(this);
187
188         return NOERROR;
189     }
190
191     *ppv = NULL;
192
193     return E_NOINTERFACE;
194 };
195
196 STDMETHODIMP_(ULONG) VLCPluginClass::AddRef(void)
197 {
198     return InterlockedIncrement(_p_class_ref);
199 };
200
201 STDMETHODIMP_(ULONG) VLCPluginClass::Release(void)
202 {
203     ULONG refcount = InterlockedDecrement(_p_class_ref);
204     if( 0 == refcount )
205     {
206         delete this;
207         return 0;
208     }
209     return refcount;
210 };
211
212 STDMETHODIMP VLCPluginClass::CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid, void **ppv)
213 {
214     if( NULL == ppv )
215         return E_POINTER;
216
217     *ppv = NULL;
218
219     if( (NULL != pUnkOuter) && (IID_IUnknown != riid) ) {
220         return CLASS_E_NOAGGREGATION;
221     }
222
223     VLCPlugin *plugin = new VLCPlugin(this, pUnkOuter);
224     if( NULL != plugin )
225     {
226         HRESULT hr = plugin->QueryInterface(riid, ppv);
227         plugin->Release();
228         return hr;
229     }
230     return E_OUTOFMEMORY;
231 };
232
233 STDMETHODIMP VLCPluginClass::LockServer(BOOL fLock)
234 {
235     if( fLock )
236         AddRef();
237     else 
238         Release();
239
240     return S_OK;
241 };
242
243 ////////////////////////////////////////////////////////////////////////
244
245 VLCPlugin::VLCPlugin(VLCPluginClass *p_class, LPUNKNOWN pUnkOuter) :
246     _inplacewnd(NULL),
247     _videownd(NULL),
248     _p_class(p_class),
249     _i_ref(1UL),
250     _i_codepage(CP_ACP),
251     _b_usermode(TRUE),
252     _i_vlc(0)
253 {
254     p_class->AddRef();
255
256     vlcOleControl = new VLCOleControl(this);
257     vlcOleInPlaceObject = new VLCOleInPlaceObject(this);
258     vlcOleInPlaceActiveObject = new VLCOleInPlaceActiveObject(this);
259     vlcPersistStorage = new VLCPersistStorage(this);
260     vlcPersistStreamInit = new VLCPersistStreamInit(this);
261     vlcPersistPropertyBag = new VLCPersistPropertyBag(this);
262     vlcProvideClassInfo = new VLCProvideClassInfo(this);
263     vlcConnectionPointContainer = new VLCConnectionPointContainer(this);
264     vlcObjectSafety = new VLCObjectSafety(this);
265     vlcControl = new VLCControl(this);
266     vlcViewObject = new VLCViewObject(this);
267     vlcDataObject = new VLCDataObject(this);
268     vlcOleObject = new VLCOleObject(this);
269
270     // configure controlling IUnknown interface for implemented interfaces
271     this->pUnkOuter = (NULL != pUnkOuter) ? pUnkOuter : dynamic_cast<LPUNKNOWN>(this);
272
273     // default picure
274     _p_pict = p_class->getInPlacePict();
275
276     // make sure that persistable properties are initialized
277     onInit();
278 };
279
280 VLCPlugin::~VLCPlugin()
281 {
282     delete vlcOleObject;
283     delete vlcDataObject;
284     delete vlcViewObject;
285     delete vlcControl;
286     delete vlcConnectionPointContainer;
287     delete vlcProvideClassInfo;
288     delete vlcPersistPropertyBag;
289     delete vlcPersistStreamInit;
290     delete vlcPersistStorage;
291     delete vlcOleInPlaceActiveObject;
292     delete vlcOleInPlaceObject;
293     delete vlcObjectSafety;
294
295     delete vlcOleControl;
296     if( _p_pict )
297         _p_pict->Release();
298
299     SysFreeString(_bstr_mrl);
300
301     _p_class->Release();
302 };
303
304 STDMETHODIMP VLCPlugin::QueryInterface(REFIID riid, void **ppv)
305 {
306     if( NULL == ppv )
307         return E_INVALIDARG;
308
309     if( IID_IUnknown == riid )
310         *ppv = reinterpret_cast<LPVOID>(this);
311     else if( IID_IOleObject == riid )
312         *ppv = reinterpret_cast<LPVOID>(vlcOleObject);
313     else if( IID_IOleControl == riid )
314         *ppv = reinterpret_cast<LPVOID>(vlcOleControl);
315     else if( IID_IOleWindow == riid )
316         *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
317     else if( IID_IOleInPlaceObject == riid )
318         *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceObject);
319     else if( IID_IOleInPlaceActiveObject == riid )
320         *ppv = reinterpret_cast<LPVOID>(vlcOleInPlaceActiveObject);
321     else if( IID_IPersist == riid )
322         *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
323     else if( IID_IPersistStreamInit == riid )
324         *ppv = reinterpret_cast<LPVOID>(vlcPersistStreamInit);
325     else if( IID_IPersistStorage == riid )
326         *ppv = reinterpret_cast<LPVOID>(vlcPersistStorage);
327     else if( IID_IPersistPropertyBag == riid )
328         *ppv = reinterpret_cast<LPVOID>(vlcPersistPropertyBag);
329     else if( IID_IProvideClassInfo == riid )
330         *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
331     else if( IID_IProvideClassInfo2 == riid )
332         *ppv = reinterpret_cast<LPVOID>(vlcProvideClassInfo);
333     else if( IID_IConnectionPointContainer == riid )
334         *ppv = reinterpret_cast<LPVOID>(vlcConnectionPointContainer);
335     else if( IID_IObjectSafety == riid )
336         *ppv = reinterpret_cast<LPVOID>(vlcObjectSafety);
337     else if( IID_IDispatch == riid )
338         *ppv = reinterpret_cast<LPVOID>(vlcControl);
339     else if( IID_IVLCControl == riid )
340         *ppv = reinterpret_cast<LPVOID>(vlcControl);
341     else if( IID_IViewObject == riid )
342         *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
343     else if( IID_IViewObject2 == riid )
344         *ppv = reinterpret_cast<LPVOID>(vlcViewObject);
345     else if( IID_IDataObject == riid )
346         *ppv = reinterpret_cast<LPVOID>(vlcDataObject);
347     else
348     {
349         *ppv = NULL;
350         return E_NOINTERFACE;
351     }
352     ((LPUNKNOWN)*ppv)->AddRef();
353     return NOERROR;
354 };
355
356 STDMETHODIMP_(ULONG) VLCPlugin::AddRef(void)
357 {
358     return InterlockedIncrement((LONG *)&_i_ref);
359 };
360
361 STDMETHODIMP_(ULONG) VLCPlugin::Release(void)
362 {
363     if( ! InterlockedDecrement((LONG *)&_i_ref) )
364     {
365         delete this;
366         return 0;
367     }
368     return _i_ref;
369 };
370
371 //////////////////////////////////////
372
373 /*
374 ** we use a window to represent plugin viewport,
375 ** whose geometry is limited by the clipping rectangle
376 ** all drawing within this window must follow must
377 ** follow coordinates system described in lprPosRect
378 */
379
380 static void getViewportCoords(LPRECT lprPosRect, LPRECT lprClipRect)
381 {
382     RECT bounds;
383     bounds.right  = lprPosRect->right-lprPosRect->left;
384
385     if( lprClipRect->left <= lprPosRect->left )
386     {
387         // left side is not clipped out
388         bounds.left = 0;
389
390         if( lprClipRect->right >= lprPosRect->right )
391         {
392             // right side is not clipped out, no change
393         }
394         else if( lprClipRect->right >= lprPosRect->left )
395         {
396             // right side is clipped out
397             lprPosRect->right = lprClipRect->right;
398         }
399         else
400         {
401             // outside of clipping rectange, not visible
402             lprPosRect->right = lprPosRect->left;
403         }
404     }
405     else
406     {
407         // left side is clipped out
408         bounds.left = lprPosRect->left-lprClipRect->left;
409         bounds.right += bounds.left;
410
411         lprPosRect->left = lprClipRect->left;
412         if( lprClipRect->right >= lprPosRect->right )
413         {
414             // right side is not clipped out
415         }
416         else
417         {
418             // right side is clipped out
419             lprPosRect->right = lprClipRect->right;
420         }
421     }
422
423     bounds.bottom = lprPosRect->bottom-lprPosRect->top;
424
425     if( lprClipRect->top <= lprPosRect->top )
426     {
427         // top side is not clipped out
428         bounds.top = 0;
429
430         if( lprClipRect->bottom >= lprPosRect->bottom )
431         {
432             // bottom side is not clipped out, no change
433         }
434         else if( lprClipRect->bottom >= lprPosRect->top )
435         {
436             // bottom side is clipped out
437             lprPosRect->bottom = lprClipRect->bottom;
438         }
439         else
440         {
441             // outside of clipping rectange, not visible
442             lprPosRect->right = lprPosRect->left;
443         }
444     }
445     else
446     {
447         bounds.top = lprPosRect->top-lprClipRect->top;
448         bounds.bottom += bounds.top;
449
450         lprPosRect->top = lprClipRect->top;
451         if( lprClipRect->bottom >= lprPosRect->bottom )
452         {
453             // bottom side is not clipped out
454         }
455         else
456         {
457             // bottom side is clipped out
458             lprPosRect->bottom = lprClipRect->bottom;
459         }
460     }
461     *lprClipRect = *lprPosRect;
462     *lprPosRect  = bounds;
463 };
464
465 HRESULT VLCPlugin::onInit(void)
466 {
467     if( 0 == _i_vlc )
468     {
469         // initialize persistable properties
470         _bstr_mrl = NULL;
471         _b_autoplay = TRUE;
472         _b_autoloop = FALSE;
473         _b_visible = TRUE;
474         _b_mute = FALSE;
475         _i_volume = 50;
476         _i_time   = 0;
477         // set default/preferred size (320x240) pixels in HIMETRIC
478         HDC hDC = CreateDevDC(NULL);
479         _extent.cx = 320;
480         _extent.cy = 240;
481         HimetricFromDP(hDC, (LPPOINT)&_extent, 1);
482         DeleteDC(hDC);
483
484         return S_OK;
485     }
486     return CO_E_ALREADYINITIALIZED;
487 };
488
489 HRESULT VLCPlugin::onLoad(void)
490 {
491     if( SysStringLen(_bstr_mrl) > 0 )
492     {
493         /*
494         ** try to combine MRL with client site moniker, which for Internet Explorer
495         ** is the URL of the page the plugin is embedded into. Hence, if the MRL
496         ** is a relative URL, we should end up with an absolute URL
497         */
498         LPOLECLIENTSITE pClientSite;
499         if( SUCCEEDED(vlcOleObject->GetClientSite(&pClientSite)) && (NULL != pClientSite) )
500         {
501             IBindCtx *pBC = 0;
502             if( SUCCEEDED(CreateBindCtx(0, &pBC)) )
503             {
504                 LPMONIKER pContMoniker = NULL;
505                 if( SUCCEEDED(pClientSite->GetMoniker(OLEGETMONIKER_ONLYIFTHERE,
506                                 OLEWHICHMK_CONTAINER, &pContMoniker)) )
507                 {
508                     LPOLESTR base_url;
509                     if( SUCCEEDED(pContMoniker->GetDisplayName(pBC, NULL, &base_url)) )
510                     {
511                         /*
512                         ** check that the moniker name is a URL
513                         */
514                         if( UrlIsW(base_url, URLIS_URL) )
515                         {
516                             DWORD len = INTERNET_MAX_URL_LENGTH;
517                             LPOLESTR abs_url = (LPOLESTR)CoTaskMemAlloc(sizeof(OLECHAR)*len);
518                             if( NULL != abs_url )
519                             {
520                                 if( SUCCEEDED(UrlCombineW(base_url, _bstr_mrl, abs_url, &len,
521                                                 URL_ESCAPE_UNSAFE|URL_PLUGGABLE_PROTOCOL)) )
522                                 {
523                                     SysFreeString(_bstr_mrl);
524                                     _bstr_mrl = SysAllocStringLen(abs_url, len);
525                                 }
526                                 CoTaskMemFree(abs_url);
527                             }
528                         }
529                         CoTaskMemFree(base_url);
530                     }
531                     pContMoniker->Release();
532                 }
533                 pBC->Release();
534             }
535             pClientSite->Release();
536         }
537     }
538     setDirty(FALSE);
539     return S_OK;
540 };
541
542 HRESULT VLCPlugin::getVLCObject(int *i_vlc)
543 {
544     if( ! isRunning() )
545     {
546         _i_vlc = VLC_Create();
547         if( _i_vlc < 0 )
548         {
549             _i_vlc = 0;
550             return E_FAIL;
551         }
552
553         /*
554         ** default initialization options
555         */
556         char *ppsz_argv[10] = { "vlc", };
557         int   ppsz_argc = 1;
558
559         HKEY h_key;
560         DWORD i_type, i_data = MAX_PATH + 1;
561         char p_data[MAX_PATH + 1];
562         if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
563                           0, KEY_READ, &h_key ) == ERROR_SUCCESS )
564         {
565              if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
566                                   (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
567              {
568                  if( i_type == REG_SZ )
569                  {
570                      strcat( p_data, "\\vlc" );
571                      ppsz_argv[0] = p_data;
572                  }
573              }
574              RegCloseKey( h_key );
575         }
576
577 #if 0
578         ppsz_argv[0] = "C:\\cygwin\\home\\Damien_Fouilleul\\dev\\videolan\\vlc-trunk\\vlc";
579 #endif
580
581         // make sure plugin isn't affected with VLC single instance mode
582         ppsz_argv[ppsz_argc++] = "--no-one-instance";
583
584         // loop mode is a configuration option only
585         if( _b_autoloop )
586             ppsz_argv[ppsz_argc++] = "--loop";
587
588         if( IsDebuggerPresent() )
589         {
590             /*
591             ** VLC default threading mechanism is designed to be as compatible
592             ** with POSIX as possible, however when debugged on win32, threads
593             ** lose signals and eventually VLC get stuck during initialization.
594             ** threading support can be configured to be more debugging friendly
595             ** but it will be less compatible with POSIX.
596             ** This is done by initializing with the following options
597             */
598             ppsz_argv[ppsz_argc++] = "--fast-mutex";
599             ppsz_argv[ppsz_argc++] = "--win9x-cv-method=1";
600         }
601
602         if( VLC_Init(_i_vlc, ppsz_argc, ppsz_argv) )
603         {
604             VLC_Destroy(_i_vlc);
605             _i_vlc = 0;
606             return E_FAIL;
607         }
608
609         VLC_VolumeSet(_i_vlc, _i_volume);
610
611         if( _b_mute )
612             VLC_VolumeMute(_i_vlc);
613
614         char *psz_mrl = CStrFromBSTR(CP_UTF8, _bstr_mrl);
615         if( NULL != psz_mrl )
616         {
617             char timeBuffer[32];
618             const char *options[1];
619             int   cOptions = 0;
620
621             if( _i_time )
622             {
623                 snprintf(timeBuffer, sizeof(timeBuffer), ":start-time=%d", _i_time);
624                 options[cOptions++] = timeBuffer;
625             }
626             // add default target to playlist
627             VLC_AddTarget(_i_vlc, psz_mrl, options, cOptions, PLAYLIST_APPEND, PLAYLIST_END);
628             CoTaskMemFree(psz_mrl);
629         }
630     }
631     *i_vlc = _i_vlc;
632     return S_OK;
633 };
634
635 HRESULT VLCPlugin::onAmbientChanged(LPUNKNOWN pContainer, DISPID dispID)
636 {
637     VARIANT v;
638     switch( dispID )
639     {
640         case DISPID_AMBIENT_BACKCOLOR:
641             break;
642         case DISPID_AMBIENT_DISPLAYNAME:
643             break;
644         case DISPID_AMBIENT_FONT:
645             break;
646         case DISPID_AMBIENT_FORECOLOR:
647             break;
648         case DISPID_AMBIENT_LOCALEID:
649             break;
650         case DISPID_AMBIENT_MESSAGEREFLECT:
651             break;
652         case DISPID_AMBIENT_SCALEUNITS:
653             break;
654         case DISPID_AMBIENT_TEXTALIGN:
655             break;
656         case DISPID_AMBIENT_USERMODE:
657             VariantInit(&v);
658             V_VT(&v) = VT_BOOL;
659             if( SUCCEEDED(GetObjectProperty(pContainer, dispID, v)) )
660             {
661                 setUserMode(V_BOOL(&v) != VARIANT_FALSE);
662             }
663             break;
664         case DISPID_AMBIENT_UIDEAD:
665             break;
666         case DISPID_AMBIENT_SHOWGRABHANDLES:
667             break;
668         case DISPID_AMBIENT_SHOWHATCHING:
669             break;
670         case DISPID_AMBIENT_DISPLAYASDEFAULT:
671             break;
672         case DISPID_AMBIENT_SUPPORTSMNEMONICS:
673             break;
674         case DISPID_AMBIENT_AUTOCLIP:
675             break;
676         case DISPID_AMBIENT_APPEARANCE:
677             break;
678         case DISPID_AMBIENT_CODEPAGE:
679             VariantInit(&v);
680             V_VT(&v) = VT_I4;
681             if( SUCCEEDED(GetObjectProperty(pContainer, dispID, v)) )
682             {
683                 setCodePage(V_I4(&v));
684             }
685             break;
686         case DISPID_AMBIENT_PALETTE:
687             break;
688         case DISPID_AMBIENT_CHARSET:
689             break;
690         case DISPID_AMBIENT_RIGHTTOLEFT:
691             break;
692         case DISPID_AMBIENT_TOPTOBOTTOM:
693             break;
694         case DISPID_UNKNOWN:
695             /*
696             ** multiple property change, look up the ones we are interested in
697             */
698             VariantInit(&v);
699             V_VT(&v) = VT_BOOL;
700             if( SUCCEEDED(GetObjectProperty(pContainer, DISPID_AMBIENT_USERMODE, v)) )
701             {
702                 setUserMode(V_BOOL(&v) != VARIANT_FALSE);
703             }
704             VariantInit(&v);
705             V_VT(&v) = VT_I4;
706             if( SUCCEEDED(GetObjectProperty(pContainer, DISPID_AMBIENT_CODEPAGE, v)) )
707             {
708                 setCodePage(V_I4(&v));
709             }
710             break;
711     }
712     return S_OK;
713 };
714
715 HRESULT VLCPlugin::onClose(DWORD dwSaveOption)
716 {
717     if( isInPlaceActive() )
718     {
719         onInPlaceDeactivate();
720     }
721     if( isRunning() )
722     {
723         int i_vlc = _i_vlc;
724
725         _i_vlc = 0;
726         vlcDataObject->onClose();
727
728         VLC_CleanUp(i_vlc);
729         VLC_Destroy(i_vlc);
730     }
731     return S_OK;
732 };
733
734 BOOL VLCPlugin::isInPlaceActive(void)
735 {
736     return (NULL != _inplacewnd);
737 };
738
739 HRESULT VLCPlugin::onActivateInPlace(LPMSG lpMesg, HWND hwndParent, LPCRECT lprcPosRect, LPCRECT lprcClipRect)
740 {
741     RECT posRect = *lprcPosRect;
742     RECT clipRect = *lprcClipRect;
743
744     /*
745     ** record keeping of control geometry within container
746     */ 
747     _posRect = posRect;
748
749     /*
750     ** convert posRect & clipRect to match control viewport coordinates
751     */
752     getViewportCoords(&posRect, &clipRect);
753
754     /*
755     ** Create a window for in place activated control.
756     ** the window geometry matches the control viewport
757     ** within container so that embedded video is always
758     ** properly clipped.
759     */
760     _inplacewnd = CreateWindow(_p_class->getInPlaceWndClassName(),
761             "VLC Plugin In-Place Window",
762             WS_CHILD|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,
763             clipRect.left,
764             clipRect.top,
765             clipRect.right-clipRect.left,
766             clipRect.bottom-clipRect.top,
767             hwndParent,
768             0,
769             _p_class->getHInstance(),
770             NULL
771            );
772
773     if( NULL == _inplacewnd )
774         return E_FAIL;
775
776     SetWindowLongPtr(_inplacewnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
777
778     /*
779     ** VLC embedded video automatically grows to cover client
780     ** area of parent window.
781     ** hence create a such a 'parent' window whose geometry
782     ** is always correct relative to the viewport bounds
783     */
784     _videownd = CreateWindow(_p_class->getVideoWndClassName(),
785             "VLC Plugin Video Window",
786             WS_CHILD|WS_CLIPCHILDREN|WS_VISIBLE,
787             posRect.left,
788             posRect.top,
789             posRect.right-posRect.left,
790             posRect.bottom-posRect.top,
791             _inplacewnd,
792             0,
793             _p_class->getHInstance(),
794             NULL
795            );
796
797     if( NULL == _videownd )
798     {
799         DestroyWindow(_inplacewnd);
800         return E_FAIL;
801     }
802
803     SetWindowLongPtr(_videownd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
804
805     if( getVisible() )
806         ShowWindow(_inplacewnd, SW_SHOW);
807
808     if( _b_usermode )
809     {
810         /* will run vlc if not done already */
811         int i_vlc;
812         HRESULT result = getVLCObject(&i_vlc);
813         if( FAILED(result) )
814             return result;
815
816         /* set internal video width and height */
817         vlc_value_t val;
818         val.i_int = posRect.right-posRect.left;
819         VLC_VariableSet(i_vlc, "conf::width", val);
820         val.i_int = posRect.bottom-posRect.top;
821         VLC_VariableSet(i_vlc, "conf::height", val);
822
823         /* set internal video parent window */
824         /* horrible cast there */
825         val.i_int = reinterpret_cast<int>(_videownd);
826         VLC_VariableSet(i_vlc, "drawable", val);
827
828         if( _b_autoplay & (VLC_PlaylistNumberOfItems(i_vlc) > 0) )
829         {
830             VLC_Play(i_vlc);
831             fireOnPlayEvent();
832         }
833     }
834     return S_OK;
835 };
836
837 HRESULT VLCPlugin::onInPlaceDeactivate(void)
838 {
839     if( isRunning() )
840     {
841         VLC_Stop(_i_vlc);
842         fireOnStopEvent();
843     }
844
845     DestroyWindow(_videownd);
846     _videownd = NULL;
847     DestroyWindow(_inplacewnd);
848     _inplacewnd = NULL;
849  
850     return S_OK;
851 };
852
853 void VLCPlugin::setVisible(BOOL fVisible)
854 {
855     if( fVisible != _b_visible )
856     {
857         _b_visible = fVisible;
858         if( isInPlaceActive() )
859         {
860             ShowWindow(_inplacewnd, fVisible ? SW_SHOW : SW_HIDE);
861             if( fVisible )
862                 InvalidateRect(_videownd, NULL, TRUE);
863         }
864         setDirty(TRUE);
865         firePropChangedEvent(DISPID_Visible);
866     }
867 };
868
869 void VLCPlugin::setVolume(int volume)
870 {
871     if( volume < 0 )
872         volume = 0;
873     else if( volume > 200 )
874         volume = 200;
875
876     if( volume != _i_volume )
877     {
878         _i_volume = volume;
879         if( isRunning() )
880         {
881             VLC_VolumeSet(_i_vlc, _i_volume);
882         }
883         setDirty(TRUE);
884     }
885 };
886
887 void VLCPlugin::setTime(int seconds)
888 {
889     if( seconds < 0 )
890         seconds = 0;
891
892     if( seconds != _i_time )
893     {
894         _i_time = seconds;
895         if( isRunning() )
896         {
897             VLC_TimeSet(_i_vlc, seconds, VLC_FALSE);
898         }
899         setDirty(TRUE);
900     }
901 };
902
903 void VLCPlugin::setFocus(BOOL fFocus)
904 {
905     if( fFocus )
906         SetActiveWindow(_inplacewnd);
907 };
908
909 BOOL VLCPlugin::hasFocus(void)
910 {
911     return GetActiveWindow() == _inplacewnd;
912 };
913
914 void VLCPlugin::onDraw(DVTARGETDEVICE * ptd, HDC hicTargetDev,
915         HDC hdcDraw, LPCRECTL lprcBounds, LPCRECTL lprcWBounds)
916 {
917     if( getVisible() )
918     {
919         long width = lprcBounds->right-lprcBounds->left;
920         long height = lprcBounds->bottom-lprcBounds->top;
921
922         RECT bounds = { lprcBounds->left, lprcBounds->top, lprcBounds->right, lprcBounds->bottom };
923         FillRect(hdcDraw, &bounds, (HBRUSH)GetStockObject(WHITE_BRUSH));
924
925         LPPICTURE pict = getPicture();
926         if( NULL != pict )
927         {
928             OLE_XSIZE_HIMETRIC picWidth;
929             OLE_YSIZE_HIMETRIC picHeight;
930
931             pict->get_Width(&picWidth);
932             pict->get_Height(&picHeight);
933
934             SIZEL picSize = { picWidth, picHeight };
935
936             if( NULL != hicTargetDev )
937             {
938                 DPFromHimetric(hicTargetDev, (LPPOINT)&picSize, 1);
939             }
940             else if( NULL != (hicTargetDev = CreateDevDC(ptd)) )
941             {
942                 DPFromHimetric(hicTargetDev, (LPPOINT)&picSize, 1);
943                 DeleteDC(hicTargetDev);
944             }
945
946             if( picSize.cx > width-4 )
947                 picSize.cx = width-4;
948             if( picSize.cy > height-4 )
949                 picSize.cy = height-4;
950
951             LONG dstX = lprcBounds->left+(width-picSize.cx)/2;
952             LONG dstY = lprcBounds->top+(height-picSize.cy)/2;
953
954             if( NULL != lprcWBounds )
955             {
956                 RECT wBounds = { lprcWBounds->left, lprcWBounds->top, lprcWBounds->right, lprcWBounds->bottom };
957                 pict->Render(hdcDraw, dstX, dstY, picSize.cx, picSize.cy,
958                         0L, picHeight, picWidth, -picHeight, &wBounds);
959             }
960             else 
961                 pict->Render(hdcDraw, dstX, dstY, picSize.cx, picSize.cy,
962                         0L, picHeight, picWidth, -picHeight, NULL);
963
964             pict->Release();
965         }
966
967         SelectObject(hdcDraw, GetStockObject(BLACK_BRUSH));
968
969         MoveToEx(hdcDraw, bounds.left, bounds.top, NULL);
970         LineTo(hdcDraw, bounds.left+width-1, bounds.top);
971         LineTo(hdcDraw, bounds.left+width-1, bounds.top+height-1);
972         LineTo(hdcDraw, bounds.left, bounds.top+height-1);
973         LineTo(hdcDraw, bounds.left, bounds.top);
974     }
975 };
976
977 void VLCPlugin::onPaint(HDC hdc, const RECT &bounds, const RECT &clipRect)
978 {
979     if( getVisible() )
980     {
981         /** if VLC is playing, it may not display any VIDEO content 
982         ** hence, draw control logo*/
983         HDC hdcDraw = CreateCompatibleDC(hdc);
984         if( NULL != hdcDraw )
985         {
986             SIZEL size = getExtent();
987             DPFromHimetric(hdc, (LPPOINT)&size, 1);
988             RECTL posRect = { 0, 0, size.cx, size.cy };
989
990             int width = bounds.right-bounds.left;
991             int height = bounds.bottom-bounds.top;
992
993             HBITMAP hBitmap = CreateCompatibleBitmap(hdc, width, height);
994             if( NULL != hBitmap )
995             {
996                 HBITMAP oldBmp = (HBITMAP)SelectObject(hdcDraw, hBitmap);
997
998                 if( (size.cx != width) || (size.cy != height) )
999                 {
1000                     // needs to scale canvas
1001                     SetMapMode(hdcDraw, MM_ANISOTROPIC);
1002                     SetWindowExtEx(hdcDraw, size.cx, size.cy, NULL);
1003                     SetViewportExtEx(hdcDraw, width, height, NULL);
1004                 }
1005
1006                 onDraw(NULL, hdc, hdcDraw, &posRect, NULL);
1007
1008                 SetMapMode(hdcDraw, MM_TEXT);
1009                 BitBlt(hdc, bounds.left, bounds.top,
1010                         width, height,
1011                         hdcDraw, 0, 0,
1012                         SRCCOPY);
1013
1014                 SelectObject(hdcDraw, oldBmp);
1015                 DeleteObject(hBitmap);
1016             }
1017             DeleteDC(hdcDraw);
1018         }
1019     }
1020 };
1021
1022 void VLCPlugin::onPositionChange(LPCRECT lprcPosRect, LPCRECT lprcClipRect)
1023 {
1024     RECT clipRect = *lprcClipRect;
1025     RECT posRect  = *lprcPosRect;
1026
1027     //RedrawWindow(GetParent(_inplacewnd), &_posRect, NULL, RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
1028
1029     /*
1030     ** record keeping of control geometry within container
1031     */
1032     _posRect = posRect;
1033
1034     /*
1035     ** convert posRect & clipRect to match control viewport coordinates
1036     */
1037     getViewportCoords(&posRect, &clipRect);
1038
1039     /*
1040     ** change in-place window geometry to match clipping region
1041     */
1042     SetWindowPos(_inplacewnd, NULL,
1043             clipRect.left,
1044             clipRect.top,
1045             clipRect.right-clipRect.left,
1046             clipRect.bottom-clipRect.top,
1047             SWP_NOACTIVATE|
1048             SWP_NOCOPYBITS|
1049             SWP_NOZORDER|
1050             SWP_NOOWNERZORDER );
1051
1052     /*
1053     ** change video window geometry to match object bounds within clipping region
1054     */
1055     SetWindowPos(_videownd, NULL,
1056             posRect.left,
1057             posRect.top,
1058             posRect.right-posRect.left,
1059             posRect.bottom-posRect.top,
1060             SWP_NOACTIVATE|
1061             SWP_NOCOPYBITS|
1062             SWP_NOZORDER|
1063             SWP_NOOWNERZORDER );
1064
1065     //RedrawWindow(_videownd, &posRect, NULL, RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
1066     vlc_value_t val;
1067     val.i_int = posRect.right-posRect.left;
1068     VLC_VariableSet(_i_vlc, "conf::width", val);
1069     val.i_int = posRect.bottom-posRect.top;
1070     VLC_VariableSet(_i_vlc, "conf::height", val);
1071 };
1072
1073 void VLCPlugin::freezeEvents(BOOL freeze)
1074 {
1075     vlcConnectionPointContainer->freezeEvents(freeze);
1076 };
1077
1078 void VLCPlugin::firePropChangedEvent(DISPID dispid)
1079 {
1080     vlcConnectionPointContainer->firePropChangedEvent(dispid); 
1081 };
1082
1083 void VLCPlugin::fireOnPlayEvent(void)
1084 {
1085     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1086     vlcConnectionPointContainer->fireEvent(DISPID_PlayEvent, &dispparamsNoArgs); 
1087 };
1088
1089 void VLCPlugin::fireOnPauseEvent(void)
1090 {
1091     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1092     vlcConnectionPointContainer->fireEvent(DISPID_PauseEvent, &dispparamsNoArgs); 
1093 };
1094
1095 void VLCPlugin::fireOnStopEvent(void)
1096 {
1097     DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
1098     vlcConnectionPointContainer->fireEvent(DISPID_StopEvent, &dispparamsNoArgs); 
1099 };
1100