oleobject.cpp 11.6 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
Antoine Cellerier's avatar
Antoine Cellerier committed
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 22 23 24 25 26 27 28 29 30
 *****************************************************************************/

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

#include <docobj.h>

using namespace std;

VLCOleObject::VLCOleObject(VLCPlugin *p_instance) :
31
_p_clientsite(NULL), _p_instance(p_instance)
32 33 34 35 36 37
{
    CreateOleAdviseHolder(&_p_advise_holder);
};

VLCOleObject::~VLCOleObject()
{
38 39
    SetClientSite(NULL);
    Close(OLECLOSE_NOSAVE);
40 41 42 43 44 45
    _p_advise_holder->Release();
};

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

STDMETHODIMP VLCOleObject::Close(DWORD dwSaveOption)
{
50 51 52
    _p_advise_holder->SendOnClose();
    return _p_instance->onClose(dwSaveOption);
}
53 54

STDMETHODIMP VLCOleObject::DoVerb(LONG iVerb, LPMSG lpMsg, LPOLECLIENTSITE pActiveSite,
55
                                    LONG, HWND hwndParent, LPCRECT lprcPosRect)
56 57 58 59 60 61
{
    switch( iVerb )
    {
        case OLEIVERB_PRIMARY:
        case OLEIVERB_SHOW:
        case OLEIVERB_OPEN:
Damien Fouilleul's avatar
Damien Fouilleul committed
62 63
            // force control to be visible when activating in place
            _p_instance->setVisible(TRUE);
64 65
            return doInPlaceActivate(lpMsg, pActiveSite, hwndParent, lprcPosRect, TRUE);

66
        case OLEIVERB_INPLACEACTIVATE:
67
            return doInPlaceActivate(lpMsg, pActiveSite, hwndParent, lprcPosRect, FALSE);
68 69 70 71 72 73

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

        case OLEIVERB_UIACTIVATE:
74 75 76 77
            // UI activate only if visible
            if( _p_instance->isVisible() )
                return doInPlaceActivate(lpMsg, pActiveSite, hwndParent, lprcPosRect, TRUE);
            return OLEOBJ_S_CANNOT_DOVERB_NOW;
78 79 80 81 82

        case OLEIVERB_DISCARDUNDOSTATE:
            return S_OK;

        default:
Damien Fouilleul's avatar
Damien Fouilleul committed
83 84
            if( iVerb > 0 ) {
                _p_instance->setVisible(TRUE);
85
                doInPlaceActivate(lpMsg, pActiveSite, hwndParent, lprcPosRect, TRUE);
Damien Fouilleul's avatar
Damien Fouilleul committed
86 87 88
                return OLEOBJ_S_INVALIDVERB;
            }
            return E_NOTIMPL;
89 90 91
    }
};

92
HRESULT VLCOleObject::doInPlaceActivate(LPMSG lpMsg, LPOLECLIENTSITE pActiveSite, HWND hwndParent, LPCRECT lprcPosRect, BOOL uiActivate)
93 94 95 96 97
{
    RECT posRect;
    RECT clipRect;
    LPCRECT lprcClipRect = lprcPosRect;

98
    if( pActiveSite )
99 100
    {
        LPOLEINPLACESITE p_inPlaceSite;
101 102 103
        IOleInPlaceSiteEx *p_inPlaceSiteEx;
        LPOLEINPLACEFRAME p_inPlaceFrame;
        LPOLEINPLACEUIWINDOW p_inPlaceUIWindow;
104

105
        if( SUCCEEDED(pActiveSite->QueryInterface(IID_IOleInPlaceSiteEx, reinterpret_cast<void**>(&p_inPlaceSiteEx))) )
106
        {
107 108 109 110 111 112 113
            p_inPlaceSite = p_inPlaceSiteEx;
            p_inPlaceSite->AddRef();
        }
        else if FAILED(pActiveSite->QueryInterface(IID_IOleInPlaceSite, reinterpret_cast<void**>(&p_inPlaceSite)) )
        {
            p_inPlaceSite = p_inPlaceSiteEx = NULL;
        }
114

115 116
        if( p_inPlaceSite )
        {
117 118
            OLEINPLACEFRAMEINFO oleFrameInfo;

119
            oleFrameInfo.cb = sizeof(OLEINPLACEFRAMEINFO);
120 121 122 123 124
            if( SUCCEEDED(p_inPlaceSite->GetWindowContext(&p_inPlaceFrame, &p_inPlaceUIWindow, &posRect, &clipRect, &oleFrameInfo)) )
            {
                lprcPosRect = &posRect;
                lprcClipRect = &clipRect;
            }
125 126 127 128

            if( (NULL == hwndParent) && FAILED(p_inPlaceSite->GetWindow(&hwndParent)) )
            {
                p_inPlaceSite->Release();
129 130 131 132 133 134 135
                if( p_inPlaceSiteEx )
                    p_inPlaceSiteEx->Release();
                if( p_inPlaceFrame )
                    p_inPlaceFrame->Release();
                if( p_inPlaceUIWindow )
                    p_inPlaceUIWindow->Release();

136 137
                return OLEOBJ_S_INVALIDHWND;
            }
138
        }
139
        else if( NULL == hwndParent )
140
        {
141
            return OLEOBJ_S_INVALIDHWND;
142
        }
143 144 145 146 147 148
        else if( NULL == lprcPosRect )
        {
            SetRect(&posRect, 0, 0, 0, 0);
            lprcPosRect = &posRect;
            lprcClipRect = &posRect;
        }
149

150 151
        // check if not already activated
        if( ! _p_instance->isInPlaceActive() )
152
        {
153 154
            if( p_inPlaceSite &&
                SUCCEEDED(_p_instance->onActivateInPlace(lpMsg, hwndParent, lprcPosRect, lprcClipRect)) )
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
            {
                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;
177
            }
178
        }
179

180
        if( p_inPlaceSite )
181 182
            p_inPlaceSite->OnPosRectChange(lprcPosRect);

183
        if( uiActivate )
184
        {
185
            if( (NULL == p_inPlaceSiteEx) || (S_OK == p_inPlaceSiteEx->RequestUIActivate()) )
186
            {
187
                if( p_inPlaceSite)
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
                {
                    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);
206 207 208
            }
        }

209
        if( p_inPlaceSite )
210 211
        {
            p_inPlaceSite->Release();
212 213 214 215 216 217
            if( p_inPlaceSiteEx )
                p_inPlaceSiteEx->Release();
            if( p_inPlaceFrame )
                p_inPlaceFrame->Release();
            if( p_inPlaceUIWindow )
                p_inPlaceUIWindow->Release();
218 219 220
        }
        return S_OK;
    }
221
    return OLEOBJ_S_CANNOT_DOVERB_NOW;
222 223 224 225 226 227 228
};

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

229
STDMETHODIMP VLCOleObject::EnumVerbs(IEnumOLEVERB **ppEnumOleVerb)
230
{
Damien Fouilleul's avatar
Damien Fouilleul committed
231 232
    return OleRegEnumVerbs(_p_instance->getClassID(),
        ppEnumOleVerb);
233 234 235 236 237 238
};

STDMETHODIMP VLCOleObject::GetClientSite(LPOLECLIENTSITE *ppClientSite)
{
    if( NULL == ppClientSite )
        return E_POINTER;
239

240
    if( NULL != _p_clientsite )
241
        _p_clientsite->AddRef();
242

243
    *ppClientSite = _p_clientsite;
244 245 246
    return S_OK;
};

247
STDMETHODIMP VLCOleObject::GetClipboardData(DWORD, LPDATAOBJECT *ppDataObject)
248
{
Damien Fouilleul's avatar
Damien Fouilleul committed
249
    return _p_instance->pUnkOuter->QueryInterface(IID_IDataObject, (void **)ppDataObject);
250 251 252 253
};

STDMETHODIMP VLCOleObject::GetExtent(DWORD dwDrawAspect, SIZEL *pSizel)
{
Damien Fouilleul's avatar
Damien Fouilleul committed
254 255 256 257 258 259 260 261 262 263
    if( NULL == pSizel )
        return E_POINTER;

    if( dwDrawAspect & DVASPECT_CONTENT )
    {
        *pSizel = _p_instance->getExtent();
        return S_OK;
    }
    pSizel->cx= 0L;
    pSizel->cy= 0L;
264 265 266 267 268
    return E_NOTIMPL;
};

STDMETHODIMP VLCOleObject::GetMiscStatus(DWORD dwAspect, DWORD *pdwStatus)
{
Damien Fouilleul's avatar
Damien Fouilleul committed
269
    if( NULL == pdwStatus )
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
        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);
292

293 294 295 296 297 298 299 300
    return E_UNEXPECTED;
};

STDMETHODIMP VLCOleObject::GetUserClassID(LPCLSID pClsid)
{
    if( NULL == pClsid )
        return E_POINTER;
 
301
    *pClsid = _p_instance->getClassID();
302 303 304 305 306
    return S_OK;
};

STDMETHODIMP VLCOleObject::GetUserType(DWORD dwFormOfType, LPOLESTR *pszUserType)
{
Damien Fouilleul's avatar
Damien Fouilleul committed
307 308
    return OleRegGetUserType(_p_instance->getClassID(),
        dwFormOfType, pszUserType);
309 310
};

311
STDMETHODIMP VLCOleObject::InitFromData(LPDATAOBJECT, BOOL, DWORD)
312 313 314 315 316 317 318 319 320 321 322
{
    return E_NOTIMPL;
};

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

STDMETHODIMP VLCOleObject::SetClientSite(LPOLECLIENTSITE pClientSite)
{
323 324 325
    if( NULL != _p_clientsite )
        _p_clientsite->Release();

326
    _p_clientsite = pClientSite;
327

328 329 330 331 332
    if( NULL != pClientSite )
    {
        pClientSite->AddRef();
        _p_instance->onAmbientChanged(pClientSite, DISPID_UNKNOWN);
    }
333 334 335
    return S_OK;
};

336
STDMETHODIMP VLCOleObject::SetColorScheme(LOGPALETTE *)
337 338 339 340 341 342
{
    return E_NOTIMPL;
};

STDMETHODIMP VLCOleObject::SetExtent(DWORD dwDrawAspect, SIZEL *pSizel)
{
Damien Fouilleul's avatar
Damien Fouilleul committed
343 344 345 346 347 348 349 350 351 352 353 354 355
    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)) )
            {
356
                HWND hwnd;
Damien Fouilleul's avatar
Damien Fouilleul committed
357

358
                if( SUCCEEDED(p_inPlaceSite->GetWindow(&hwnd)) )
Damien Fouilleul's avatar
Damien Fouilleul committed
359
                {
360
                    // use HIMETRIC to pixel transform
361 362
                    RECT posRect = _p_instance->getPosRect();
                    HDC hDC = GetDC(hwnd);
Damien Fouilleul's avatar
Damien Fouilleul committed
363 364 365
                    posRect.right = (pSizel->cx*GetDeviceCaps(hDC, LOGPIXELSX)/2540L)+posRect.left;
                    posRect.bottom = (pSizel->cy*GetDeviceCaps(hDC, LOGPIXELSY)/2540L)+posRect.top;
                    DeleteDC(hDC);
366
                    p_inPlaceSite->OnPosRectChange(&posRect);
Damien Fouilleul's avatar
Damien Fouilleul committed
367 368 369 370 371 372
                }
                p_inPlaceSite->Release();
            }
        }
        return S_OK;
    }
373 374 375
    return E_NOTIMPL;
};

376
STDMETHODIMP VLCOleObject::SetHostNames(LPCOLESTR, LPCOLESTR)
377 378 379 380
{
    return S_OK;
};

381
STDMETHODIMP VLCOleObject::SetMoniker(DWORD, LPMONIKER pMoniker)
382 383 384 385 386 387 388 389 390 391 392 393 394 395
{
    return _p_advise_holder->SendOnRename(pMoniker);
};

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

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