oleobject.cpp 11.8 KB
Newer Older
1 2 3
/*****************************************************************************
 * oleobject.cpp: ActiveX control for VLC
 *****************************************************************************
4
 * Copyright (C) 2005 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *****************************************************************************/

#include "plugin.h"
#include "oleobject.h"

#include "utils.h"

#include <docobj.h>

using namespace std;

VLCOleObject::VLCOleObject(VLCPlugin *p_instance) :
_p_clientsite(NULL), _p_instance(p_instance) 
{
    CreateOleAdviseHolder(&_p_advise_holder);
};

VLCOleObject::~VLCOleObject()
{
40 41
    SetClientSite(NULL);
    Close(OLECLOSE_NOSAVE);
42 43 44 45 46 47 48 49 50 51
    _p_advise_holder->Release();
};

STDMETHODIMP VLCOleObject::Advise(IAdviseSink *pAdvSink, DWORD *dwConnection)
{
    return _p_advise_holder->Advise(pAdvSink, dwConnection);
};

STDMETHODIMP VLCOleObject::Close(DWORD dwSaveOption)
{
52 53 54 55 56 57
    if( _p_instance->isRunning() )
    {
        _p_advise_holder->SendOnClose();
        return _p_instance->onClose(dwSaveOption);
    }
    return S_OK;
58 59 60 61 62 63 64 65 66 67
};

STDMETHODIMP VLCOleObject::DoVerb(LONG iVerb, LPMSG lpMsg, LPOLECLIENTSITE pActiveSite,
                                    LONG lIndex, HWND hwndParent, LPCRECT lprcPosRect)
{
    switch( iVerb )
    {
        case OLEIVERB_PRIMARY:
        case OLEIVERB_SHOW:
        case OLEIVERB_OPEN:
damienf's avatar
 
damienf committed
68 69
            // force control to be visible when activating in place
            _p_instance->setVisible(TRUE);
70 71
            return doInPlaceActivate(lpMsg, pActiveSite, hwndParent, lprcPosRect, TRUE);

72
        case OLEIVERB_INPLACEACTIVATE:
73
            return doInPlaceActivate(lpMsg, pActiveSite, hwndParent, lprcPosRect, FALSE);
74 75 76 77 78 79

        case OLEIVERB_HIDE:
            _p_instance->setVisible(FALSE);
            return S_OK;

        case OLEIVERB_UIACTIVATE:
80 81 82 83
            // UI activate only if visible
            if( _p_instance->isVisible() )
                return doInPlaceActivate(lpMsg, pActiveSite, hwndParent, lprcPosRect, TRUE);
            return OLEOBJ_S_CANNOT_DOVERB_NOW;
84 85 86 87 88

        case OLEIVERB_DISCARDUNDOSTATE:
            return S_OK;

        default:
damienf's avatar
 
damienf committed
89 90
            if( iVerb > 0 ) {
                _p_instance->setVisible(TRUE);
91
                doInPlaceActivate(lpMsg, pActiveSite, hwndParent, lprcPosRect, TRUE);
damienf's avatar
 
damienf committed
92 93 94
                return OLEOBJ_S_INVALIDVERB;
            }
            return E_NOTIMPL;
95 96 97
    }
};

98
HRESULT VLCOleObject::doInPlaceActivate(LPMSG lpMsg, LPOLECLIENTSITE pActiveSite, HWND hwndParent, LPCRECT lprcPosRect, BOOL uiActivate)
99 100 101 102 103
{
    RECT posRect;
    RECT clipRect;
    LPCRECT lprcClipRect = lprcPosRect;

104
    if( pActiveSite )
105 106
    {
        LPOLEINPLACESITE p_inPlaceSite;
107 108 109
        IOleInPlaceSiteEx *p_inPlaceSiteEx;
        LPOLEINPLACEFRAME p_inPlaceFrame;
        LPOLEINPLACEUIWINDOW p_inPlaceUIWindow;
110

111
        if( SUCCEEDED(pActiveSite->QueryInterface(IID_IOleInPlaceSiteEx, reinterpret_cast<void**>(&p_inPlaceSiteEx))) )
112
        {
113 114 115 116 117 118 119
            p_inPlaceSite = p_inPlaceSiteEx;
            p_inPlaceSite->AddRef();
        }
        else if FAILED(pActiveSite->QueryInterface(IID_IOleInPlaceSite, reinterpret_cast<void**>(&p_inPlaceSite)) )
        {
            p_inPlaceSite = p_inPlaceSiteEx = NULL;
        }
120

121 122
        if( p_inPlaceSite )
        {
123 124
            OLEINPLACEFRAMEINFO oleFrameInfo;

125
            oleFrameInfo.cb = sizeof(OLEINPLACEFRAMEINFO);
126 127 128 129 130
            if( SUCCEEDED(p_inPlaceSite->GetWindowContext(&p_inPlaceFrame, &p_inPlaceUIWindow, &posRect, &clipRect, &oleFrameInfo)) )
            {
                lprcPosRect = &posRect;
                lprcClipRect = &clipRect;
            }
131 132 133 134

            if( (NULL == hwndParent) && FAILED(p_inPlaceSite->GetWindow(&hwndParent)) )
            {
                p_inPlaceSite->Release();
135 136 137 138 139 140 141
                if( p_inPlaceSiteEx )
                    p_inPlaceSiteEx->Release();
                if( p_inPlaceFrame )
                    p_inPlaceFrame->Release();
                if( p_inPlaceUIWindow )
                    p_inPlaceUIWindow->Release();

142 143
                return OLEOBJ_S_INVALIDHWND;
            }
144
        }
145
        else if( NULL == hwndParent )
146
        {
147
            return OLEOBJ_S_INVALIDHWND;
148
        }
149 150 151 152 153 154
        else if( NULL == lprcPosRect )
        {
            SetRect(&posRect, 0, 0, 0, 0);
            lprcPosRect = &posRect;
            lprcClipRect = &posRect;
        }
155

156 157
        // check if not already activated
        if( ! _p_instance->isInPlaceActive() )
158
        {
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
            if( ((NULL == p_inPlaceSite) || (S_OK == p_inPlaceSite->CanInPlaceActivate()))
             && SUCCEEDED(_p_instance->onActivateInPlace(lpMsg, hwndParent, lprcPosRect, lprcClipRect)) ) 
            {
                if( p_inPlaceSiteEx )
                {
                    BOOL needsRedraw;
                    p_inPlaceSiteEx->OnInPlaceActivateEx(&needsRedraw, 0);
                }
                else if( p_inPlaceSite )
                    p_inPlaceSite->OnInPlaceActivate();
            }
            else
            {
                if( p_inPlaceSite )
                {
                    p_inPlaceSite->Release();
                    if( p_inPlaceSiteEx )
                        p_inPlaceSiteEx->Release();
                    if( p_inPlaceFrame )
                        p_inPlaceFrame->Release();
                    if( p_inPlaceUIWindow )
                        p_inPlaceUIWindow->Release();
                }
                return OLEOBJ_S_CANNOT_DOVERB_NOW;
            } 
184
        }
185

186
        if( p_inPlaceSite )
187 188
            p_inPlaceSite->OnPosRectChange(lprcPosRect);

189
        if( uiActivate )
190
        {
191
            if( (NULL == p_inPlaceSiteEx) || (S_OK == p_inPlaceSiteEx->RequestUIActivate()) )
192
            {
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
                if( p_inPlaceSite) 
                {
                    p_inPlaceSite->OnUIActivate();

                    LPOLEINPLACEACTIVEOBJECT p_inPlaceActiveObject;
                    if( SUCCEEDED(QueryInterface(IID_IOleInPlaceActiveObject, reinterpret_cast<void**>(&p_inPlaceActiveObject))) )
                    {
                        if( p_inPlaceFrame )
                            p_inPlaceFrame->SetActiveObject(p_inPlaceActiveObject, NULL);
                        if( p_inPlaceUIWindow )
                            p_inPlaceUIWindow->SetActiveObject(p_inPlaceActiveObject, NULL);
                        p_inPlaceActiveObject->Release();
                    }
                    if( p_inPlaceFrame )
                        p_inPlaceFrame->RequestBorderSpace(NULL);

                    pActiveSite->ShowObject();
                }
                _p_instance->setFocus(TRUE);
212 213 214
            }
        }

215
        if( p_inPlaceSite )
216 217
        {
            p_inPlaceSite->Release();
218 219 220 221 222 223
            if( p_inPlaceSiteEx )
                p_inPlaceSiteEx->Release();
            if( p_inPlaceFrame )
                p_inPlaceFrame->Release();
            if( p_inPlaceUIWindow )
                p_inPlaceUIWindow->Release();
224 225 226
        }
        return S_OK;
    }
227
    return OLEOBJ_S_CANNOT_DOVERB_NOW;
228 229 230 231 232 233 234 235 236
};

STDMETHODIMP VLCOleObject::EnumAdvise(IEnumSTATDATA **ppEnumAdvise)
{
    return _p_advise_holder->EnumAdvise(ppEnumAdvise);
};

STDMETHODIMP VLCOleObject::EnumVerbs(IEnumOleVerb **ppEnumOleVerb)
{
damienf's avatar
 
damienf committed
237 238
    return OleRegEnumVerbs(_p_instance->getClassID(),
        ppEnumOleVerb);
239 240 241 242 243 244 245 246 247 248
};

STDMETHODIMP VLCOleObject::GetClientSite(LPOLECLIENTSITE *ppClientSite)
{
    if( NULL == ppClientSite )
        return E_POINTER;
 
    if( NULL != _p_clientsite )
        _p_clientsite->AddRef(); 

249
    *ppClientSite = _p_clientsite;
250 251 252 253 254
    return S_OK;
};

STDMETHODIMP VLCOleObject::GetClipboardData(DWORD dwReserved, LPDATAOBJECT *ppDataObject)
{
damienf's avatar
 
damienf committed
255
    return _p_instance->pUnkOuter->QueryInterface(IID_IDataObject, (void **)ppDataObject);
256 257 258 259
};

STDMETHODIMP VLCOleObject::GetExtent(DWORD dwDrawAspect, SIZEL *pSizel)
{
damienf's avatar
damienf committed
260 261 262 263 264 265 266 267 268 269
    if( NULL == pSizel )
        return E_POINTER;

    if( dwDrawAspect & DVASPECT_CONTENT )
    {
        *pSizel = _p_instance->getExtent();
        return S_OK;
    }
    pSizel->cx= 0L;
    pSizel->cy= 0L;
270 271 272 273 274
    return E_NOTIMPL;
};

STDMETHODIMP VLCOleObject::GetMiscStatus(DWORD dwAspect, DWORD *pdwStatus)
{
damienf's avatar
 
damienf committed
275
    if( NULL == pdwStatus )
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
        return E_POINTER;

    switch( dwAspect )
    {
        case DVASPECT_CONTENT:
            *pdwStatus = OLEMISC_RECOMPOSEONRESIZE
                | OLEMISC_CANTLINKINSIDE
                | OLEMISC_INSIDEOUT
                | OLEMISC_ACTIVATEWHENVISIBLE
                | OLEMISC_SETCLIENTSITEFIRST;
            break;
        default:
            *pdwStatus = 0;
    }

    return S_OK;
};

STDMETHODIMP VLCOleObject::GetMoniker(DWORD dwAssign, DWORD dwWhichMoniker, LPMONIKER *ppMoniker)
{
    if( NULL != _p_clientsite )
        return _p_clientsite->GetMoniker(dwAssign,dwWhichMoniker, ppMoniker);
 
    return E_UNEXPECTED;
};

STDMETHODIMP VLCOleObject::GetUserClassID(LPCLSID pClsid)
{
    if( NULL == pClsid )
        return E_POINTER;
 
damienf's avatar
 
damienf committed
307
    *pClsid = _p_instance->getClassID(); 
308 309 310 311 312
    return S_OK;
};

STDMETHODIMP VLCOleObject::GetUserType(DWORD dwFormOfType, LPOLESTR *pszUserType)
{
damienf's avatar
 
damienf committed
313 314
    return OleRegGetUserType(_p_instance->getClassID(),
        dwFormOfType, pszUserType);
315 316 317 318 319 320 321 322 323 324 325 326 327 328
};

STDMETHODIMP VLCOleObject::InitFromData(LPDATAOBJECT pDataObject, BOOL fCreation, DWORD dwReserved)
{
    return E_NOTIMPL;
};

STDMETHODIMP VLCOleObject::IsUpToDate(void)
{
    return S_OK;
};

STDMETHODIMP VLCOleObject::SetClientSite(LPOLECLIENTSITE pClientSite)
{
329 330 331
    if( NULL != _p_clientsite )
        _p_clientsite->Release();

332
    _p_clientsite = pClientSite;
333

334 335 336 337 338
    if( NULL != pClientSite )
    {
        pClientSite->AddRef();
        _p_instance->onAmbientChanged(pClientSite, DISPID_UNKNOWN);
    }
339 340 341 342 343 344 345 346 347 348
    return S_OK;
};

STDMETHODIMP VLCOleObject::SetColorScheme(LOGPALETTE *pLogpal)
{
    return E_NOTIMPL;
};

STDMETHODIMP VLCOleObject::SetExtent(DWORD dwDrawAspect, SIZEL *pSizel)
{
damienf's avatar
damienf committed
349 350 351 352 353 354 355 356 357 358 359 360 361
    if( NULL == pSizel )
        return E_POINTER;

    if( dwDrawAspect & DVASPECT_CONTENT )
    {
        _p_instance->setExtent(*pSizel);

        if( _p_instance->isInPlaceActive() )
        {
            LPOLEINPLACESITE p_inPlaceSite;

            if( SUCCEEDED(_p_clientsite->QueryInterface(IID_IOleInPlaceSite, (void**)&p_inPlaceSite)) )
            {
362
                HWND hwnd;
damienf's avatar
damienf committed
363

364
                if( SUCCEEDED(p_inPlaceSite->GetWindow(&hwnd)) )
damienf's avatar
damienf committed
365
                {
366 367 368
                    // use HIMETRIC to pixel transform 
                    RECT posRect = _p_instance->getPosRect();
                    HDC hDC = GetDC(hwnd);
damienf's avatar
damienf committed
369 370 371
                    posRect.right = (pSizel->cx*GetDeviceCaps(hDC, LOGPIXELSX)/2540L)+posRect.left;
                    posRect.bottom = (pSizel->cy*GetDeviceCaps(hDC, LOGPIXELSY)/2540L)+posRect.top;
                    DeleteDC(hDC);
372
                    p_inPlaceSite->OnPosRectChange(&posRect);
damienf's avatar
damienf committed
373 374 375 376 377 378
                }
                p_inPlaceSite->Release();
            }
        }
        return S_OK;
    }
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
    return E_NOTIMPL;
};

STDMETHODIMP VLCOleObject::SetHostNames(LPCOLESTR szContainerApp, LPCOLESTR szContainerObj)
{
    return S_OK;
};

STDMETHODIMP VLCOleObject::SetMoniker(DWORD dwWhichMoniker, LPMONIKER pMoniker)
{
    return _p_advise_holder->SendOnRename(pMoniker);
};

STDMETHODIMP VLCOleObject::Unadvise(DWORD dwConnection)
{
    return _p_advise_holder->Unadvise(dwConnection);
};

STDMETHODIMP VLCOleObject::Update(void)
{
    return S_OK;
};