atl_ax.c 42.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * Active Template Library ActiveX functions (atl.dll)
 *
 * Copyright 2006 Andrey Turkin
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>
#include <stdio.h>

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "winerror.h"
#include "winuser.h"
#include "wine/debug.h"
#include "objbase.h"
#include "objidl.h"
#include "ole2.h"
#include "exdisp.h"
#include "atlbase.h"
#include "atliface.h"
#include "atlwin.h"
38
#include "shlwapi.h"
39 40 41 42 43

#include "wine/unicode.h"

WINE_DEFAULT_DEBUG_CHANNEL(atl);

44
typedef struct IOCS {
45 46 47 48 49
    IOleClientSite            IOleClientSite_iface;
    IOleContainer             IOleContainer_iface;
    IOleInPlaceSiteWindowless IOleInPlaceSiteWindowless_iface;
    IOleInPlaceFrame          IOleInPlaceFrame_iface;
    IOleControlSite           IOleControlSite_iface;
50 51 52 53

    LONG ref;
    HWND hWnd;
    IOleObject *control;
54
    RECT size;
55
    WNDPROC OrigWndProc;
56
    BOOL fActive, fInPlace, fWindowless;
57 58
} IOCS;

59 60
static const WCHAR wine_atl_iocsW[] = {'_','_','W','I','N','E','_','A','T','L','_','I','O','C','S','\0'};

61 62 63
/**********************************************************************
 * AtlAxWin class window procedure
 */
64
static LRESULT CALLBACK AtlAxWin_wndproc( HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
65 66 67 68 69 70 71 72
{
    if ( wMsg == WM_CREATE )
    {
            DWORD len = GetWindowTextLengthW( hWnd ) + 1;
            WCHAR *ptr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
            if (!ptr)
                return 1;
            GetWindowTextW( hWnd, ptr, len );
73
            AtlAxCreateControlEx( ptr, hWnd, NULL, NULL, NULL, NULL, NULL );
74 75 76 77 78 79 80
            HeapFree( GetProcessHeap(), 0, ptr );
            return 0;
    }
    return DefWindowProcW( hWnd, wMsg, wParam, lParam );
}

/***********************************************************************
81
 *           AtlAxWinInit          [atl100.@]
82
 * Initializes the control-hosting code: registering the AtlAxWin,
83 84 85 86 87
 * AtlAxWin7 and AtlAxWinLic7 window classes and some messages.
 *
 * RETURNS
 *  TRUE or FALSE
 */
88

89 90 91
BOOL WINAPI AtlAxWinInit(void)
{
    WNDCLASSEXW wcex;
92

93 94 95
#if _ATL_VER <= _ATL_VER_30
#define ATL_NAME_SUFFIX 0
#elif _ATL_VER == _ATL_VER_80
96 97
#define ATL_NAME_SUFFIX '8','0',0
#elif _ATL_VER == _ATL_VER_90
98 99 100
#define ATL_NAME_SUFFIX '9','0',0
#elif _ATL_VER == _ATL_VER_100
#define ATL_NAME_SUFFIX '1','0','0',0
101 102
#elif _ATL_VER == _ATL_VER_110
#define ATL_NAME_SUFFIX '1','1','0',0
103 104 105 106 107
#else
#error Unsupported version
#endif

    const WCHAR AtlAxWinW[] = {'A','t','l','A','x','W','i','n',ATL_NAME_SUFFIX};
108

109
    FIXME("version %04x semi-stub\n", _ATL_VER);
110 111 112 113 114

    if ( FAILED( OleInitialize(NULL) ) )
        return FALSE;

    wcex.cbSize        = sizeof(wcex);
115
    wcex.style         = CS_GLOBALCLASS | (_ATL_VER > _ATL_VER_30 ? CS_DBLCLKS : 0);
116 117 118 119 120 121 122 123 124 125
    wcex.cbClsExtra    = 0;
    wcex.cbWndExtra    = 0;
    wcex.hInstance     = GetModuleHandleW( NULL );
    wcex.hIcon         = NULL;
    wcex.hCursor       = NULL;
    wcex.hbrBackground = NULL;
    wcex.lpszMenuName  = NULL;
    wcex.hIconSm       = 0;

    wcex.lpfnWndProc   = AtlAxWin_wndproc;
126
    wcex.lpszClassName = AtlAxWinW;
127 128 129
    if ( !RegisterClassExW( &wcex ) )
        return FALSE;

130 131 132 133 134 135 136
    if(_ATL_VER > _ATL_VER_30) {
        const WCHAR AtlAxWinLicW[] = {'A','t','l','A','x','W','i','n','L','i','c',ATL_NAME_SUFFIX};

        wcex.lpszClassName = AtlAxWinLicW;
        if ( !RegisterClassExW( &wcex ) )
            return FALSE;
    }
137 138 139

    return TRUE;
}
140

141 142 143 144
/***********************************************************************
 *  Atl container component implementation
 */

145 146
/******      IOleClientSite    *****/
static inline IOCS *impl_from_IOleClientSite(IOleClientSite *iface)
147
{
148 149
    return CONTAINING_RECORD(iface, IOCS, IOleClientSite_iface);
}
150

151 152 153 154 155
static HRESULT IOCS_Detach( IOCS *This ) /* remove subclassing */
{
    if ( This->hWnd )
    {
        SetWindowLongPtrW( This->hWnd, GWLP_WNDPROC, (ULONG_PTR) This->OrigWndProc );
156
        RemovePropW( This->hWnd, wine_atl_iocsW);
157 158 159 160 161
        This->hWnd = NULL;
    }
    if ( This->control )
    {
        IOleObject *control = This->control;
162

163 164 165 166 167 168
        This->control = NULL;
        IOleObject_Close( control, OLECLOSE_NOSAVE );
        IOleObject_SetClientSite( control, NULL );
        IOleObject_Release( control );
    }
    return S_OK;
169 170
}

171
static HRESULT WINAPI OleClientSite_QueryInterface(IOleClientSite *iface, REFIID riid, void **ppv)
172
{
173 174 175 176
    IOCS *This = impl_from_IOleClientSite(iface);

    TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);

177 178
    *ppv = NULL;

179 180
    if (IsEqualIID(&IID_IUnknown, riid) ||
        IsEqualIID(&IID_IOleClientSite, riid))
181
    {
182 183 184
        *ppv = iface;
    }
    else if (IsEqualIID(&IID_IOleContainer, riid))
185
    {
186
        *ppv = &This->IOleContainer_iface;
187 188 189 190
    }
    else if (IsEqualIID(&IID_IOleInPlaceSite, riid) ||
             IsEqualIID(&IID_IOleInPlaceSiteEx, riid) ||
             IsEqualIID(&IID_IOleInPlaceSiteWindowless, riid))
191
    {
192
        *ppv = &This->IOleInPlaceSiteWindowless_iface;
193 194
    }
    else if (IsEqualIID(&IID_IOleInPlaceFrame, riid))
195
    {
196
        *ppv = &This->IOleInPlaceFrame_iface;
197 198
    }
    else if (IsEqualIID(&IID_IOleControlSite, riid))
199
    {
200
        *ppv = &This->IOleControlSite_iface;
201 202 203 204
    }

    if (*ppv)
    {
205
        IOleClientSite_AddRef(iface);
206 207
        return S_OK;
    }
208

209
    WARN("unsupported interface %s\n", debugstr_guid(riid));
210 211 212
    return E_NOINTERFACE;
}

213
static ULONG WINAPI OleClientSite_AddRef(IOleClientSite *iface)
214
{
215 216 217 218 219 220 221 222 223
    IOCS *This = impl_from_IOleClientSite(iface);
    ULONG ref = InterlockedIncrement(&This->ref);
    TRACE("(%p)->(%d)\n", This, ref);
    return ref;
}

static ULONG WINAPI OleClientSite_Release(IOleClientSite *iface)
{
    IOCS *This = impl_from_IOleClientSite(iface);
224 225
    ULONG ref = InterlockedDecrement(&This->ref);

226
    TRACE("(%p)->(%d)\n", This, ref);
227 228 229 230 231 232 233 234 235 236 237 238

    if (!ref)
    {
        IOCS_Detach( This );
        HeapFree( GetProcessHeap(), 0, This );
    }

    return ref;
}

static HRESULT WINAPI OleClientSite_SaveObject(IOleClientSite *iface)
{
239
    IOCS *This = impl_from_IOleClientSite(iface);
240 241 242
    FIXME( "(%p) - stub\n", This );
    return E_NOTIMPL;
}
243

244 245
static HRESULT WINAPI OleClientSite_GetMoniker(IOleClientSite *iface, DWORD dwAssign, DWORD dwWhichMoniker, IMoniker **ppmk)
{
246
    IOCS *This = impl_from_IOleClientSite(iface);
247 248 249 250

    FIXME( "(%p, 0x%x, 0x%x, %p)\n", This, dwAssign, dwWhichMoniker, ppmk );
    return E_NOTIMPL;
}
251

252
static HRESULT WINAPI OleClientSite_GetContainer(IOleClientSite *iface, IOleContainer **container)
253
{
254
    IOCS *This = impl_from_IOleClientSite(iface);
255 256
    TRACE("(%p, %p)\n", This, container);
    return IOleClientSite_QueryInterface(iface, &IID_IOleContainer, (void**)container);
257
}
258

259 260
static HRESULT WINAPI OleClientSite_ShowObject(IOleClientSite *iface)
{
261
    IOCS *This = impl_from_IOleClientSite(iface);
262 263 264
    FIXME( "(%p) - stub\n", This );
    return S_OK;
}
265

266 267
static HRESULT WINAPI OleClientSite_OnShowWindow(IOleClientSite *iface, BOOL fShow)
{
268
    IOCS *This = impl_from_IOleClientSite(iface);
269 270 271
    FIXME( "(%p, %s) - stub\n", This, fShow ? "TRUE" : "FALSE" );
    return E_NOTIMPL;
}
272

273 274
static HRESULT WINAPI OleClientSite_RequestNewObjectLayout(IOleClientSite *iface)
{
275
    IOCS *This = impl_from_IOleClientSite(iface);
276 277 278 279 280
    FIXME( "(%p) - stub\n", This );
    return E_NOTIMPL;
}


281
/******      IOleContainer     *****/
282 283 284 285 286
static inline IOCS *impl_from_IOleContainer(IOleContainer *iface)
{
    return CONTAINING_RECORD(iface, IOCS, IOleContainer_iface);
}

287 288
static HRESULT WINAPI OleContainer_QueryInterface( IOleContainer* iface, REFIID riid, void** ppv)
{
289
    IOCS *This = impl_from_IOleContainer(iface);
290
    return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
291
}
292

293 294
static ULONG WINAPI OleContainer_AddRef(IOleContainer* iface)
{
295
    IOCS *This = impl_from_IOleContainer(iface);
296
    return IOleClientSite_AddRef(&This->IOleClientSite_iface);
297
}
298

299 300
static ULONG WINAPI OleContainer_Release(IOleContainer* iface)
{
301
    IOCS *This = impl_from_IOleContainer(iface);
302
    return IOleClientSite_Release(&This->IOleClientSite_iface);
303
}
304

305 306 307
static HRESULT WINAPI OleContainer_ParseDisplayName(IOleContainer* iface, IBindCtx* pbc,
        LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut)
{
308
    IOCS *This = impl_from_IOleContainer(iface);
309 310 311
    FIXME( "(%p,%p,%s,%p,%p) - stub\n", This, pbc, debugstr_w(pszDisplayName), pchEaten, ppmkOut );
    return E_NOTIMPL;
}
312

313 314
static HRESULT WINAPI OleContainer_EnumObjects(IOleContainer* iface, DWORD grfFlags, IEnumUnknown** ppenum)
{
315
    IOCS *This = impl_from_IOleContainer(iface);
316 317 318
    FIXME( "(%p, %u, %p) - stub\n", This, grfFlags, ppenum );
    return E_NOTIMPL;
}
319

320 321
static HRESULT WINAPI OleContainer_LockContainer(IOleContainer* iface, BOOL fLock)
{
322
    IOCS *This = impl_from_IOleContainer(iface);
323 324 325 326 327
    FIXME( "(%p, %s) - stub\n", This, fLock?"TRUE":"FALSE" );
    return E_NOTIMPL;
}


328
/******    IOleInPlaceSiteWindowless   *******/
329 330 331 332 333
static inline IOCS *impl_from_IOleInPlaceSiteWindowless(IOleInPlaceSiteWindowless *iface)
{
    return CONTAINING_RECORD(iface, IOCS, IOleInPlaceSiteWindowless_iface);
}

334 335
static HRESULT WINAPI OleInPlaceSiteWindowless_QueryInterface(IOleInPlaceSiteWindowless *iface, REFIID riid, void **ppv)
{
336
    IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
337
    return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
338
}
339

340 341
static ULONG WINAPI OleInPlaceSiteWindowless_AddRef(IOleInPlaceSiteWindowless *iface)
{
342
    IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
343
    return IOleClientSite_AddRef(&This->IOleClientSite_iface);
344
}
345

346 347
static ULONG WINAPI OleInPlaceSiteWindowless_Release(IOleInPlaceSiteWindowless *iface)
{
348
    IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
349
    return IOleClientSite_Release(&This->IOleClientSite_iface);
350
}
351

352 353
static HRESULT WINAPI OleInPlaceSiteWindowless_GetWindow(IOleInPlaceSiteWindowless* iface, HWND* phwnd)
{
354
    IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
355 356 357 358 359

    TRACE("(%p,%p)\n", This, phwnd);
    *phwnd = This->hWnd;
    return S_OK;
}
360

361 362
static HRESULT WINAPI OleInPlaceSiteWindowless_ContextSensitiveHelp(IOleInPlaceSiteWindowless* iface, BOOL fEnterMode)
{
363
    IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
364 365 366
    FIXME("(%p,%d) - stub\n", This, fEnterMode);
    return E_NOTIMPL;
}
367

368 369
static HRESULT WINAPI OleInPlaceSiteWindowless_CanInPlaceActivate(IOleInPlaceSiteWindowless *iface)
{
370
    IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
371 372 373
    TRACE("(%p)\n", This);
    return S_OK;
}
374

375 376
static HRESULT WINAPI OleInPlaceSiteWindowless_OnInPlaceActivate(IOleInPlaceSiteWindowless *iface)
{
377
    IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
378 379 380 381 382 383

    TRACE("(%p)\n", This);

    This->fInPlace = TRUE;
    return S_OK;
}
384

385 386
static HRESULT WINAPI OleInPlaceSiteWindowless_OnUIActivate(IOleInPlaceSiteWindowless *iface)
{
387
    IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
388 389 390 391 392 393

    TRACE("(%p)\n", This);

    return S_OK;
}
static HRESULT WINAPI OleInPlaceSiteWindowless_GetWindowContext(IOleInPlaceSiteWindowless *iface,
394
        IOleInPlaceFrame **frame, IOleInPlaceUIWindow **ppDoc, LPRECT lprcPosRect,
395 396
        LPRECT lprcClipRect, LPOLEINPLACEFRAMEINFO lpFrameInfo)
{
397
    IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
398

399
    TRACE("(%p,%p,%p,%p,%p,%p)\n", This, frame, ppDoc, lprcPosRect, lprcClipRect, lpFrameInfo);
400 401

    if ( lprcClipRect )
402
        *lprcClipRect = This->size;
403
    if ( lprcPosRect )
404
        *lprcPosRect = This->size;
405

406
    if ( frame )
407
    {
408 409
        *frame = &This->IOleInPlaceFrame_iface;
        IOleInPlaceFrame_AddRef(*frame);
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
    }

    if ( ppDoc )
        *ppDoc = NULL;

    if ( lpFrameInfo )
    {
        lpFrameInfo->fMDIApp = FALSE;
        lpFrameInfo->hwndFrame = This->hWnd;
        lpFrameInfo->haccel = NULL;
        lpFrameInfo->cAccelEntries = 0;
    }

    return S_OK;
}
425

426 427
static HRESULT WINAPI OleInPlaceSiteWindowless_Scroll(IOleInPlaceSiteWindowless *iface, SIZE scrollExtent)
{
428
    IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
429 430 431
    FIXME("(%p) - stub\n", This);
    return E_NOTIMPL;
}
432

433 434
static HRESULT WINAPI OleInPlaceSiteWindowless_OnUIDeactivate(IOleInPlaceSiteWindowless *iface, BOOL fUndoable)
{
435
    IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
436 437 438
    FIXME("(%p,%d) - stub\n", This, fUndoable);
    return E_NOTIMPL;
}
439

440 441
static HRESULT WINAPI OleInPlaceSiteWindowless_OnInPlaceDeactivate(IOleInPlaceSiteWindowless *iface)
{
442
    IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
443 444 445 446 447 448

    TRACE("(%p)\n", This);

    This->fInPlace = This->fWindowless = FALSE;
    return S_OK;
}
449

450 451
static HRESULT WINAPI OleInPlaceSiteWindowless_DiscardUndoState(IOleInPlaceSiteWindowless *iface)
{
452
    IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
453 454 455
    FIXME("(%p) - stub\n", This);
    return E_NOTIMPL;
}
456

457 458
static HRESULT WINAPI OleInPlaceSiteWindowless_DeactivateAndUndo(IOleInPlaceSiteWindowless *iface)
{
459
    IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
460 461 462
    FIXME("(%p) - stub\n", This);
    return E_NOTIMPL;
}
463

464 465
static HRESULT WINAPI OleInPlaceSiteWindowless_OnPosRectChange(IOleInPlaceSiteWindowless *iface, LPCRECT lprcPosRect)
{
466
    IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
467 468 469
    FIXME("(%p,%p) - stub\n", This, lprcPosRect);
    return E_NOTIMPL;
}
470

471 472
static HRESULT WINAPI OleInPlaceSiteWindowless_OnInPlaceActivateEx( IOleInPlaceSiteWindowless *iface, BOOL* pfNoRedraw, DWORD dwFlags)
{
473
    IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
474 475 476 477 478 479 480 481

    TRACE("\n");

    This->fActive = This->fInPlace = TRUE;
    if ( dwFlags & ACTIVATE_WINDOWLESS )
        This->fWindowless = TRUE;
    return S_OK;
}
482

483 484
static HRESULT WINAPI OleInPlaceSiteWindowless_OnInPlaceDeactivateEx( IOleInPlaceSiteWindowless *iface, BOOL fNoRedraw)
{
485
    IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

    TRACE("\n");

    This->fActive = This->fInPlace = This->fWindowless = FALSE;
    return S_OK;
}
static HRESULT WINAPI OleInPlaceSiteWindowless_RequestUIActivate( IOleInPlaceSiteWindowless *iface)
{
    FIXME("\n");
    return E_NOTIMPL;
}
static HRESULT WINAPI OleInPlaceSiteWindowless_CanWindowlessActivate( IOleInPlaceSiteWindowless *iface)
{
    FIXME("\n");
    return S_OK;
}
static HRESULT WINAPI OleInPlaceSiteWindowless_GetCapture( IOleInPlaceSiteWindowless *iface)
{
    FIXME("\n");
    return E_NOTIMPL;
}
static HRESULT WINAPI OleInPlaceSiteWindowless_SetCapture( IOleInPlaceSiteWindowless *iface, BOOL fCapture)
{
    FIXME("\n");
    return E_NOTIMPL;
}
static HRESULT WINAPI OleInPlaceSiteWindowless_GetFocus( IOleInPlaceSiteWindowless *iface)
{
    FIXME("\n");
    return E_NOTIMPL;
}
static HRESULT WINAPI OleInPlaceSiteWindowless_SetFocus( IOleInPlaceSiteWindowless *iface, BOOL fFocus)
{
    FIXME("\n");
    return E_NOTIMPL;
}
static HRESULT WINAPI OleInPlaceSiteWindowless_GetDC( IOleInPlaceSiteWindowless *iface, LPCRECT pRect, DWORD grfFlags, HDC* phDC)
{
    FIXME("\n");
    return E_NOTIMPL;
}
static HRESULT WINAPI OleInPlaceSiteWindowless_ReleaseDC( IOleInPlaceSiteWindowless *iface, HDC hDC)
{
    FIXME("\n");
    return E_NOTIMPL;
}
static HRESULT WINAPI OleInPlaceSiteWindowless_InvalidateRect( IOleInPlaceSiteWindowless *iface, LPCRECT pRect, BOOL fErase)
{
    FIXME("\n");
    return E_NOTIMPL;
}
static HRESULT WINAPI OleInPlaceSiteWindowless_InvalidateRgn( IOleInPlaceSiteWindowless *iface, HRGN hRGN, BOOL fErase)
{
    FIXME("\n");
    return E_NOTIMPL;
}
static HRESULT WINAPI OleInPlaceSiteWindowless_ScrollRect( IOleInPlaceSiteWindowless *iface, INT dx, INT dy, LPCRECT pRectScroll, LPCRECT pRectClip)
{
    FIXME("\n");
    return E_NOTIMPL;
}
static HRESULT WINAPI OleInPlaceSiteWindowless_AdjustRect( IOleInPlaceSiteWindowless *iface, LPRECT prc)
{
    FIXME("\n");
    return E_NOTIMPL;
}
static HRESULT WINAPI OleInPlaceSiteWindowless_OnDefWindowMessage( IOleInPlaceSiteWindowless *iface, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT* plResult)
{
    FIXME("\n");
    return E_NOTIMPL;
}
557 558 559


/******    IOleInPlaceFrame   *******/
560 561 562 563 564
static inline IOCS *impl_from_IOleInPlaceFrame(IOleInPlaceFrame *iface)
{
    return CONTAINING_RECORD(iface, IOCS, IOleInPlaceFrame_iface);
}

565 566
static HRESULT WINAPI OleInPlaceFrame_QueryInterface(IOleInPlaceFrame *iface, REFIID riid, void **ppv)
{
567
    IOCS *This = impl_from_IOleInPlaceFrame(iface);
568
    return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
569
}
570

571 572
static ULONG WINAPI OleInPlaceFrame_AddRef(IOleInPlaceFrame *iface)
{
573
    IOCS *This = impl_from_IOleInPlaceFrame(iface);
574
    return IOleClientSite_AddRef(&This->IOleClientSite_iface);
575
}
576

577 578
static ULONG WINAPI OleInPlaceFrame_Release(IOleInPlaceFrame *iface)
{
579
    IOCS *This = impl_from_IOleInPlaceFrame(iface);
580
    return IOleClientSite_Release(&This->IOleClientSite_iface);
581
}
582

583 584
static HRESULT WINAPI OleInPlaceFrame_GetWindow(IOleInPlaceFrame *iface, HWND *phWnd)
{
585
    IOCS *This = impl_from_IOleInPlaceFrame(iface);
586 587 588 589 590 591 592 593 594

    TRACE( "(%p,%p)\n", This, phWnd );

    *phWnd = This->hWnd;
    return S_OK;
}

static HRESULT WINAPI OleInPlaceFrame_ContextSensitiveHelp(IOleInPlaceFrame *iface, BOOL fEnterMode)
{
595
    IOCS *This = impl_from_IOleInPlaceFrame(iface);
596 597 598 599 600 601 602

    FIXME( "(%p,%d) - stub\n", This, fEnterMode );
    return E_NOTIMPL;
}

static HRESULT WINAPI OleInPlaceFrame_GetBorder(IOleInPlaceFrame *iface, LPRECT lprectBorder)
{
603
    IOCS *This = impl_from_IOleInPlaceFrame(iface);
604 605 606 607 608 609 610

    FIXME( "(%p,%p) - stub\n", This, lprectBorder );
    return E_NOTIMPL;
}

static HRESULT WINAPI OleInPlaceFrame_RequestBorderSpace(IOleInPlaceFrame *iface, LPCBORDERWIDTHS pborderwidths)
{
611
    IOCS *This = impl_from_IOleInPlaceFrame(iface);
612 613 614 615 616 617 618

    FIXME( "(%p,%p) - stub\n", This, pborderwidths );
    return E_NOTIMPL;
}

static HRESULT WINAPI OleInPlaceFrame_SetBorderSpace(IOleInPlaceFrame *iface, LPCBORDERWIDTHS pborderwidths)
{
619
    IOCS *This = impl_from_IOleInPlaceFrame(iface);
620 621 622 623 624 625 626

    FIXME( "(%p,%p) - stub\n", This, pborderwidths );
    return E_NOTIMPL;
}

static HRESULT WINAPI OleInPlaceFrame_SetActiveObject(IOleInPlaceFrame *iface, IOleInPlaceActiveObject *pActiveObject, LPCOLESTR pszObjName)
{
627
    IOCS *This = impl_from_IOleInPlaceFrame(iface);
628 629 630 631 632 633 634

    FIXME( "(%p,%p,%s) - stub\n", This, pActiveObject, debugstr_w(pszObjName) );
    return S_OK;
}

static HRESULT WINAPI OleInPlaceFrame_InsertMenus(IOleInPlaceFrame *iface, HMENU hmenuShared, LPOLEMENUGROUPWIDTHS lpMenuWidths)
{
635
    IOCS *This = impl_from_IOleInPlaceFrame(iface);
636 637 638 639 640 641 642

    FIXME( "(%p,%p,%p) - stub\n", This, hmenuShared, lpMenuWidths );
    return E_NOTIMPL;
}

static HRESULT WINAPI OleInPlaceFrame_SetMenu(IOleInPlaceFrame *iface, HMENU hmenuShared, HOLEMENU holemenu, HWND hwndActiveObject)
{
643
    IOCS *This = impl_from_IOleInPlaceFrame(iface);
644 645 646 647

    FIXME( "(%p,%p,%p,%p) - stub\n", This, hmenuShared, holemenu, hwndActiveObject );
    return E_NOTIMPL;
}
648

649 650
static HRESULT WINAPI OleInPlaceFrame_RemoveMenus(IOleInPlaceFrame *iface, HMENU hmenuShared)
{
651
    IOCS *This = impl_from_IOleInPlaceFrame(iface);
652 653 654 655 656 657 658

    FIXME( "(%p, %p) - stub\n", This, hmenuShared );
    return E_NOTIMPL;
}

static HRESULT WINAPI OleInPlaceFrame_SetStatusText(IOleInPlaceFrame *iface, LPCOLESTR pszStatusText)
{
659
    IOCS *This = impl_from_IOleInPlaceFrame(iface);
660 661 662 663 664 665 666

    FIXME( "(%p, %s) - stub\n", This, debugstr_w( pszStatusText ) );
    return E_NOTIMPL;
}

static HRESULT WINAPI OleInPlaceFrame_EnableModeless(IOleInPlaceFrame *iface, BOOL fEnable)
{
667
    IOCS *This = impl_from_IOleInPlaceFrame(iface);
668 669 670 671 672 673 674

    FIXME( "(%p, %d) - stub\n", This, fEnable );
    return E_NOTIMPL;
}

static HRESULT WINAPI OleInPlaceFrame_TranslateAccelerator(IOleInPlaceFrame *iface, LPMSG lpmsg, WORD wID)
{
675
    IOCS *This = impl_from_IOleInPlaceFrame(iface);
676 677 678 679 680

    FIXME( "(%p, %p, %x) - stub\n", This, lpmsg, wID );
    return E_NOTIMPL;
}

681

682
/******    IOleControlSite    *******/
683 684 685 686 687
static inline IOCS *impl_from_IOleControlSite(IOleControlSite *iface)
{
    return CONTAINING_RECORD(iface, IOCS, IOleControlSite_iface);
}

688 689
static HRESULT WINAPI OleControlSite_QueryInterface(IOleControlSite *iface, REFIID riid, void **ppv)
{
690
    IOCS *This = impl_from_IOleControlSite(iface);
691
    return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
692
}
693

694 695
static ULONG WINAPI OleControlSite_AddRef(IOleControlSite *iface)
{
696
    IOCS *This = impl_from_IOleControlSite(iface);
697
    return IOleClientSite_AddRef(&This->IOleClientSite_iface);
698
}
699

700 701
static ULONG WINAPI OleControlSite_Release(IOleControlSite *iface)
{
702
    IOCS *This = impl_from_IOleControlSite(iface);
703
    return IOleClientSite_Release(&This->IOleClientSite_iface);
704
}
705

706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
static HRESULT WINAPI OleControlSite_OnControlInfoChanged( IOleControlSite* This)
{
    FIXME( "\n" );
    return E_NOTIMPL;
}
static HRESULT WINAPI OleControlSite_LockInPlaceActive( IOleControlSite* This, BOOL fLock)
{
    FIXME( "\n" );
    return E_NOTIMPL;
}
static HRESULT WINAPI OleControlSite_GetExtendedControl( IOleControlSite* This, IDispatch** ppDisp)
{
    FIXME( "\n" );
    return E_NOTIMPL;
}
static HRESULT WINAPI OleControlSite_TransformCoords( IOleControlSite* This, POINTL* pPtlHimetric, POINTF* pPtfContainer, DWORD dwFlags)
{
    FIXME( "\n" );
    return E_NOTIMPL;
}
static HRESULT WINAPI OleControlSite_TranslateAccelerator( IOleControlSite* This, MSG* pMsg, DWORD grfModifiers)
{
    FIXME( "\n" );
    return E_NOTIMPL;
}
static HRESULT WINAPI OleControlSite_OnFocus( IOleControlSite* This, BOOL fGotFocus)
{
    FIXME( "\n" );
    return E_NOTIMPL;
}
static HRESULT WINAPI OleControlSite_ShowPropertyFrame( IOleControlSite* This)
{
    FIXME( "\n" );
    return E_NOTIMPL;
}

742 743 744 745 746 747 748 749 750 751 752 753

static const IOleClientSiteVtbl OleClientSite_vtbl = {
    OleClientSite_QueryInterface,
    OleClientSite_AddRef,
    OleClientSite_Release,
    OleClientSite_SaveObject,
    OleClientSite_GetMoniker,
    OleClientSite_GetContainer,
    OleClientSite_ShowObject,
    OleClientSite_OnShowWindow,
    OleClientSite_RequestNewObjectLayout
};
754 755 756 757 758 759 760 761
static const IOleContainerVtbl OleContainer_vtbl = {
    OleContainer_QueryInterface,
    OleContainer_AddRef,
    OleContainer_Release,
    OleContainer_ParseDisplayName,
    OleContainer_EnumObjects,
    OleContainer_LockContainer
};
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
static const IOleInPlaceSiteWindowlessVtbl OleInPlaceSiteWindowless_vtbl = {
    OleInPlaceSiteWindowless_QueryInterface,
    OleInPlaceSiteWindowless_AddRef,
    OleInPlaceSiteWindowless_Release,
    OleInPlaceSiteWindowless_GetWindow,
    OleInPlaceSiteWindowless_ContextSensitiveHelp,
    OleInPlaceSiteWindowless_CanInPlaceActivate,
    OleInPlaceSiteWindowless_OnInPlaceActivate,
    OleInPlaceSiteWindowless_OnUIActivate,
    OleInPlaceSiteWindowless_GetWindowContext,
    OleInPlaceSiteWindowless_Scroll,
    OleInPlaceSiteWindowless_OnUIDeactivate,
    OleInPlaceSiteWindowless_OnInPlaceDeactivate,
    OleInPlaceSiteWindowless_DiscardUndoState,
    OleInPlaceSiteWindowless_DeactivateAndUndo,
    OleInPlaceSiteWindowless_OnPosRectChange,
    OleInPlaceSiteWindowless_OnInPlaceActivateEx,
    OleInPlaceSiteWindowless_OnInPlaceDeactivateEx,
    OleInPlaceSiteWindowless_RequestUIActivate,
    OleInPlaceSiteWindowless_CanWindowlessActivate,
    OleInPlaceSiteWindowless_GetCapture,
    OleInPlaceSiteWindowless_SetCapture,
    OleInPlaceSiteWindowless_GetFocus,
    OleInPlaceSiteWindowless_SetFocus,
    OleInPlaceSiteWindowless_GetDC,
    OleInPlaceSiteWindowless_ReleaseDC,
    OleInPlaceSiteWindowless_InvalidateRect,
    OleInPlaceSiteWindowless_InvalidateRgn,
    OleInPlaceSiteWindowless_ScrollRect,
    OleInPlaceSiteWindowless_AdjustRect,
    OleInPlaceSiteWindowless_OnDefWindowMessage
};
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
static const IOleInPlaceFrameVtbl OleInPlaceFrame_vtbl =
{
    OleInPlaceFrame_QueryInterface,
    OleInPlaceFrame_AddRef,
    OleInPlaceFrame_Release,
    OleInPlaceFrame_GetWindow,
    OleInPlaceFrame_ContextSensitiveHelp,
    OleInPlaceFrame_GetBorder,
    OleInPlaceFrame_RequestBorderSpace,
    OleInPlaceFrame_SetBorderSpace,
    OleInPlaceFrame_SetActiveObject,
    OleInPlaceFrame_InsertMenus,
    OleInPlaceFrame_SetMenu,
    OleInPlaceFrame_RemoveMenus,
    OleInPlaceFrame_SetStatusText,
    OleInPlaceFrame_EnableModeless,
    OleInPlaceFrame_TranslateAccelerator
};
812 813 814 815 816 817 818 819 820 821 822 823 824
static const IOleControlSiteVtbl OleControlSite_vtbl =
{
    OleControlSite_QueryInterface,
    OleControlSite_AddRef,
    OleControlSite_Release,
    OleControlSite_OnControlInfoChanged,
    OleControlSite_LockInPlaceActive,
    OleControlSite_GetExtendedControl,
    OleControlSite_TransformCoords,
    OleControlSite_TranslateAccelerator,
    OleControlSite_OnFocus,
    OleControlSite_ShowPropertyFrame
};
825 826 827 828 829

static void IOCS_OnSize( IOCS* This, LPCRECT rect )
{
    SIZEL inPix, inHi;

830
    This->size = *rect;
831

832 833 834 835 836 837 838
    if ( !This->control )
        return;

    inPix.cx = rect->right - rect->left;
    inPix.cy = rect->bottom - rect->top;
    AtlPixelToHiMetric( &inPix, &inHi );
    IOleObject_SetExtent( This->control, DVASPECT_CONTENT, &inHi );
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878

    if ( This->fInPlace )
    {
        IOleInPlaceObject *wl;

        if ( SUCCEEDED( IOleObject_QueryInterface( This->control, &IID_IOleInPlaceObject, (void**)&wl ) ) )
        {
            IOleInPlaceObject_SetObjectRects( wl, rect, rect );
            IOleInPlaceObject_Release( wl );
        }
    }
}

static void IOCS_OnShow( IOCS *This, BOOL fShow )
{
    if (!This->control || This->fActive || !fShow )
        return;

    This->fActive = TRUE;
}

static void IOCS_OnDraw( IOCS *This )
{
    IViewObject *view;

    if ( !This->control || !This->fWindowless )
        return;

    if ( SUCCEEDED( IOleObject_QueryInterface( This->control, &IID_IViewObject, (void**)&view ) ) )
    {
        HDC dc = GetDC( This->hWnd );
        RECTL rect;

        rect.left = This->size.left; rect.top = This->size.top;
        rect.bottom = This->size.bottom; rect.right = This->size.right;

        IViewObject_Draw( view, DVASPECT_CONTENT, ~0, NULL, NULL, 0, dc, &rect, &rect, NULL, 0 );
        IViewObject_Release( view );
        ReleaseDC( This->hWnd, dc );
    }
879 880 881 882 883 884 885 886 887 888 889 890 891 892
}

static LRESULT IOCS_OnWndProc( IOCS *This, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
    WNDPROC OrigWndProc = This->OrigWndProc;

    switch( uMsg )
    {
        case WM_DESTROY:
            IOCS_Detach( This );
            break;
        case WM_SIZE:
            {
                RECT r;
893
                SetRect(&r, 0, 0, LOWORD(lParam), HIWORD(lParam));
894 895 896
                IOCS_OnSize( This, &r );
            }
            break;
897 898 899 900 901 902
        case WM_SHOWWINDOW:
            IOCS_OnShow( This, (BOOL) wParam );
            break;
        case WM_PAINT:
            IOCS_OnDraw( This );
            break;
903 904
    }

905
    return CallWindowProcW( OrigWndProc, hWnd, uMsg, wParam, lParam );
906 907 908 909
}

static LRESULT CALLBACK AtlHost_wndproc( HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
{
910
    IOCS *This = (IOCS*) GetPropW( hWnd, wine_atl_iocsW );
911 912 913 914 915 916 917
    return IOCS_OnWndProc( This, hWnd, wMsg, wParam, lParam );
}

static HRESULT IOCS_Attach( IOCS *This, HWND hWnd, IUnknown *pUnkControl ) /* subclass hWnd */
{
    This->hWnd = hWnd;
    IUnknown_QueryInterface( pUnkControl, &IID_IOleObject, (void**)&This->control );
918
    IOleObject_SetClientSite( This->control, &This->IOleClientSite_iface );
919
    SetPropW( hWnd, wine_atl_iocsW, This );
920
    This->OrigWndProc = (WNDPROC)SetWindowLongPtrW( hWnd, GWLP_WNDPROC, (ULONG_PTR) AtlHost_wndproc );
921 922 923 924 925 926 927 928 929 930 931 932 933

    return S_OK;
}

static HRESULT IOCS_Init( IOCS *This )
{
    RECT rect;
    static const WCHAR AXWIN[] = {'A','X','W','I','N',0};

    IOleObject_SetHostNames( This->control, AXWIN, AXWIN );

    GetClientRect( This->hWnd, &rect );
    IOCS_OnSize( This, &rect );
934 935
    IOleObject_DoVerb( This->control, OLEIVERB_INPLACEACTIVATE, NULL, &This->IOleClientSite_iface,
                       0, This->hWnd, &rect );
936 937 938 939 940 941 942

    return S_OK;
}

/**********************************************************************
 * Create new instance of Atl host component and attach it to window  *
 */
943
static HRESULT IOCS_Create( HWND hWnd, IUnknown *pUnkControl, IUnknown **container )
944 945 946
{
    HRESULT hr;
    IOCS *This;
947

948 949 950 951
    if (!container)
        return S_OK;

    *container = NULL;
952 953 954 955 956
    This = HeapAlloc(GetProcessHeap(), 0, sizeof(IOCS));

    if (!This)
        return E_OUTOFMEMORY;

957 958 959 960 961
    This->IOleClientSite_iface.lpVtbl = &OleClientSite_vtbl;
    This->IOleContainer_iface.lpVtbl = &OleContainer_vtbl;
    This->IOleInPlaceSiteWindowless_iface.lpVtbl = &OleInPlaceSiteWindowless_vtbl;
    This->IOleInPlaceFrame_iface.lpVtbl = &OleInPlaceFrame_vtbl;
    This->IOleControlSite_iface.lpVtbl = &OleControlSite_vtbl;
962 963 964 965
    This->ref = 1;

    This->OrigWndProc = NULL;
    This->hWnd = NULL;
966
    This->fWindowless = This->fActive = This->fInPlace = FALSE;
967 968 969 970 971

    hr = IOCS_Attach( This, hWnd, pUnkControl );
    if ( SUCCEEDED( hr ) )
        hr = IOCS_Init( This );
    if ( SUCCEEDED( hr ) )
972
        *container = (IUnknown*)&This->IOleClientSite_iface;
973
    else
974 975 976 977
    {
        IOCS_Detach( This );
        HeapFree(GetProcessHeap(), 0, This);
    }
978 979 980 981 982

    return hr;
}


983
/***********************************************************************
984
 *           AtlAxCreateControl           [atl100.@]
985 986 987 988 989 990 991 992
 */
HRESULT WINAPI AtlAxCreateControl(LPCOLESTR lpszName, HWND hWnd,
        IStream *pStream, IUnknown **ppUnkContainer)
{
    return AtlAxCreateControlEx( lpszName, hWnd, pStream, ppUnkContainer,
            NULL, NULL, NULL );
}

993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
enum content
{
    IsEmpty = 0,
    IsGUID = 1,
    IsHTML = 2,
    IsURL = 3,
    IsUnknown = 4
};

static enum content get_content_type(LPCOLESTR name, CLSID *control_id)
{
    WCHAR new_urlW[MAX_PATH];
    DWORD size = MAX_PATH;
    WCHAR mshtml_prefixW[] = {'m','s','h','t','m','l',':','\0'};

    if (!name || !name[0])
    {
        WARN("name %s\n", wine_dbgstr_w(name));
        return IsEmpty;
    }

    if (CLSIDFromString(name, control_id) == S_OK ||
        CLSIDFromProgID(name, control_id) == S_OK)
        return IsGUID;

    if (PathIsURLW (name) ||
        UrlApplySchemeW(name, new_urlW, &size, URL_APPLY_GUESSSCHEME|URL_APPLY_GUESSFILE) == S_OK)
    {
        *control_id = CLSID_WebBrowser;
        return IsURL;
    }

    if (!strncmpiW(name, mshtml_prefixW, 7))
    {
        FIXME("mshtml prefix not implemented\n");
        *control_id = CLSID_WebBrowser;
        return IsHTML;
    }

    return IsUnknown;
}

1035
/***********************************************************************
1036
 *           AtlAxCreateControlLicEx         [atl100.@]
1037 1038 1039 1040 1041
 *
 * REMARKS
 *   See http://www.codeproject.com/com/cwebpage.asp for some background
 *
 */
1042
HRESULT WINAPI AtlAxCreateControlLicEx(LPCOLESTR lpszName, HWND hWnd,
1043
        IStream *pStream, IUnknown **ppUnkContainer, IUnknown **ppUnkControl,
1044
        REFIID iidSink, IUnknown *punkSink, BSTR lic)
1045 1046 1047 1048
{
    CLSID controlId;
    HRESULT hRes;
    IOleObject *pControl;
1049
    IUnknown *pUnkControl = NULL;
1050
    IPersistStreamInit *pPSInit;
1051 1052
    IUnknown *pContainer = NULL;
    enum content content;
1053

1054 1055 1056 1057 1058
    TRACE("(%s %p %p %p %p %p %p %s)\n", debugstr_w(lpszName), hWnd, pStream,
            ppUnkContainer, ppUnkControl, iidSink, punkSink, debugstr_w(lic));

    if (lic)
        FIXME("semi stub\n");
1059

1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
    if (ppUnkContainer) *ppUnkContainer = NULL;
    if (ppUnkControl) *ppUnkControl = NULL;

    content = get_content_type(lpszName, &controlId);

    if (content == IsEmpty)
        return S_OK;

    if (content == IsUnknown)
        return CO_E_CLASSSTRING;
1070

1071
    hRes = CoCreateInstance( &controlId, 0, CLSCTX_ALL, &IID_IOleObject,
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
            (void**) &pControl );
    if ( FAILED( hRes ) )
    {
        WARN( "cannot create ActiveX control %s instance - error 0x%08x\n",
                debugstr_guid( &controlId ), hRes );
        return hRes;
    }

    hRes = IOleObject_QueryInterface( pControl, &IID_IPersistStreamInit, (void**) &pPSInit );
    if ( SUCCEEDED( hRes ) )
    {
        if (!pStream)
            IPersistStreamInit_InitNew( pPSInit );
        else
            IPersistStreamInit_Load( pPSInit, pStream );
        IPersistStreamInit_Release( pPSInit );
    } else
        WARN("cannot get IID_IPersistStreamInit out of control\n");

    IOleObject_QueryInterface( pControl, &IID_IUnknown, (void**) &pUnkControl );
    IOleObject_Release( pControl );
1093

1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107

    hRes = AtlAxAttachControl( pUnkControl, hWnd, &pContainer );
    if ( FAILED( hRes ) )
        WARN("cannot attach control to window\n");

    if ( content == IsURL )
    {
        IWebBrowser2 *browser;

        hRes = IOleObject_QueryInterface( pControl, &IID_IWebBrowser2, (void**) &browser );
        if ( !browser )
            WARN( "Cannot query IWebBrowser2 interface: %08x\n", hRes );
        else {
            VARIANT url;
1108

1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
            IWebBrowser2_put_Visible( browser, VARIANT_TRUE ); /* it seems that native does this on URL (but do not on MSHTML:! why? */

            V_VT(&url) = VT_BSTR;
            V_BSTR(&url) = SysAllocString( lpszName );

            hRes = IWebBrowser2_Navigate2( browser, &url, NULL, NULL, NULL, NULL );
            if ( FAILED( hRes ) )
                WARN( "IWebBrowser2::Navigate2 failed: %08x\n", hRes );
            SysFreeString( V_BSTR(&url) );

            IWebBrowser2_Release( browser );
        }
    }

    if (ppUnkContainer)
    {
        *ppUnkContainer = pContainer;
        if ( pContainer )
            IUnknown_AddRef( pContainer );
    }
    if (ppUnkControl)
    {
        *ppUnkControl = pUnkControl;
        if ( pUnkControl )
            IUnknown_AddRef( pUnkControl );
    }

Marcus Meissner's avatar
Marcus Meissner committed
1136 1137
    if ( pUnkControl )
        IUnknown_Release( pUnkControl );
1138 1139 1140 1141 1142 1143 1144
    if ( pContainer )
        IUnknown_Release( pContainer );

    return S_OK;
}

/***********************************************************************
1145
 *           AtlAxAttachControl           [atl100.@]
1146
 */
1147
HRESULT WINAPI AtlAxAttachControl(IUnknown *control, HWND hWnd, IUnknown **container)
1148
{
1149 1150
    HRESULT hr;

1151
    TRACE("(%p %p %p)\n", control, hWnd, container);
1152

1153
    if (!control)
1154
        return E_INVALIDARG;
1155

1156 1157
    hr = IOCS_Create( hWnd, control, container );
    return hWnd ? hr : S_FALSE;
1158
}
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186

/**********************************************************************
 * Helper function for AX_ConvertDialogTemplate
 */
static inline BOOL advance_array(WORD **pptr, DWORD *palloc, DWORD *pfilled, const WORD *data, DWORD size)
{
    if ( (*pfilled + size) > *palloc )
    {
        *palloc = ((*pfilled+size) + 0xFF) & ~0xFF;
        *pptr = HeapReAlloc( GetProcessHeap(), 0, *pptr, *palloc * sizeof(WORD) );
        if (!*pptr)
            return FALSE;
    }
    RtlMoveMemory( *pptr+*pfilled, data, size * sizeof(WORD) );
    *pfilled += size;
    return TRUE;
}

/**********************************************************************
 * Convert ActiveX control templates to AtlAxWin class instances
 */
static LPDLGTEMPLATEW AX_ConvertDialogTemplate(LPCDLGTEMPLATEW src_tmpl)
{
#define GET_WORD(x)  (*(const  WORD *)(x))
#define GET_DWORD(x) (*(const DWORD *)(x))
#define PUT_BLOCK(x,y) do {if (!advance_array(&output, &allocated, &filled, (x), (y))) return NULL;} while (0)
#define PUT_WORD(x)  do {WORD w = (x);PUT_BLOCK(&w, 1);} while(0)
    const WORD *tmp, *src = (const WORD *)src_tmpl;
1187
    WORD *output;
1188 1189 1190 1191 1192 1193 1194 1195 1196
    DWORD allocated, filled; /* in WORDs */
    BOOL ext;
    WORD signature, dlgver, rescount;
    DWORD style;

    filled = 0; allocated = 256;
    output = HeapAlloc( GetProcessHeap(), 0, allocated * sizeof(WORD) );
    if (!output)
        return NULL;
1197

1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
    /* header */
    tmp = src;
    signature = GET_WORD(src);
    dlgver = GET_WORD(src + 1);
    if (signature == 1 && dlgver == 0xFFFF)
    {
        ext = TRUE;
        src += 6;
        style = GET_DWORD(src);
        src += 2;
        rescount = GET_WORD(src++);
        src += 4;
        if ( GET_WORD(src) == 0xFFFF ) /* menu */
            src += 2;
        else
            src += strlenW(src) + 1;
        if ( GET_WORD(src) == 0xFFFF ) /* class */
            src += 2;
        else
            src += strlenW(src) + 1;
        src += strlenW(src) + 1; /* title */
        if ( style & (DS_SETFONT | DS_SHELLFONT) )
        {
            src += 3;
            src += strlenW(src) + 1;
        }
    } else {
        ext = FALSE;
        style = GET_DWORD(src);
        src += 4;
        rescount = GET_WORD(src++);
        src += 4;
        if ( GET_WORD(src) == 0xFFFF ) /* menu */
            src += 2;
        else
            src += strlenW(src) + 1;
        if ( GET_WORD(src) == 0xFFFF ) /* class */
            src += 2;
        else
            src += strlenW(src) + 1;
        src += strlenW(src) + 1; /* title */
        if ( style & DS_SETFONT )
        {
            src++;
            src += strlenW(src) + 1;
        }
    }
    PUT_BLOCK(tmp, src-tmp);

    while(rescount--)
    {
1249 1250
        src = (const WORD *)( ( ((ULONG_PTR)src) + 3) & ~3); /* align on DWORD boundary */
        filled = (filled + 1) & ~1; /* depends on DWORD-aligned allocation unit */
1251 1252 1253

        tmp = src;
        if (ext)
1254
            src += 12;
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
        else
            src += 9;
        PUT_BLOCK(tmp, src-tmp);

        tmp = src;
        if ( GET_WORD(src) == 0xFFFF ) /* class */
        {
            src += 2;
        } else
        {
            src += strlenW(src) + 1;
        }
        src += strlenW(src) + 1; /* title */
        if ( GET_WORD(tmp) == '{' ) /* all this mess created because of this line */
        {
1270
            static const WCHAR AtlAxWin[] = {'A','t','l','A','x','W','i','n', 0};
1271
            PUT_BLOCK(AtlAxWin, ARRAY_SIZE(AtlAxWin));
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
            PUT_BLOCK(tmp, strlenW(tmp)+1);
        } else
            PUT_BLOCK(tmp, src-tmp);

        if ( GET_WORD(src) )
        {
            WORD size = (GET_WORD(src)+sizeof(WORD)-1) / sizeof(WORD); /* quite ugly :( Maybe use BYTE* instead of WORD* everywhere ? */
            PUT_BLOCK(src, size);
            src+=size;
        }
        else
        {
            PUT_WORD(0);
            src++;
        }
    }
    return (LPDLGTEMPLATEW) output;
}

/***********************************************************************
1292
 *           AtlAxCreateDialogA           [atl100.@]
1293 1294 1295 1296 1297 1298 1299 1300
 *
 * Creates a dialog window
 *
 * PARAMS
 *  hInst   [I] Application instance
 *  name    [I] Dialog box template name
 *  owner   [I] Dialog box parent HWND
 *  dlgProc [I] Dialog box procedure
1301
 *  param   [I] This value will be passed to dlgProc as WM_INITDIALOG's message lParam
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
 *
 * RETURNS
 *  Window handle of dialog window.
 */
HWND WINAPI AtlAxCreateDialogA(HINSTANCE hInst, LPCSTR name, HWND owner, DLGPROC dlgProc ,LPARAM param)
{
    HWND res = NULL;
    int length;
    WCHAR *nameW;

1312
    if (IS_INTRESOURCE(name))
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
        return AtlAxCreateDialogW( hInst, (LPCWSTR) name, owner, dlgProc, param );

    length = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
    nameW = HeapAlloc( GetProcessHeap(), 0, length * sizeof(WCHAR) );
    if (nameW)
    {
        MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, length );
        res = AtlAxCreateDialogW( hInst, nameW, owner, dlgProc, param );
        HeapFree( GetProcessHeap(), 0, nameW );
    }
    return res;
}

/***********************************************************************
1327
 *           AtlAxCreateDialogW           [atl100.@]
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
 *
 * See AtlAxCreateDialogA
 *
 */
HWND WINAPI AtlAxCreateDialogW(HINSTANCE hInst, LPCWSTR name, HWND owner, DLGPROC dlgProc ,LPARAM param)
{
    HRSRC hrsrc;
    HGLOBAL hgl;
    LPCDLGTEMPLATEW ptr;
    LPDLGTEMPLATEW newptr;
    HWND res;

1340
    TRACE("(%p %s %p %p %lx)\n", hInst, debugstr_w(name), owner, dlgProc, param);
1341 1342 1343 1344 1345 1346 1347

    hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG );
    if ( !hrsrc )
        return NULL;
    hgl = LoadResource (hInst, hrsrc);
    if ( !hgl )
        return NULL;
1348
    ptr = LockResource ( hgl );
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
    if (!ptr)
    {
        FreeResource( hgl );
        return NULL;
    }
    newptr = AX_ConvertDialogTemplate( ptr );
    if ( newptr )
    {
            res = CreateDialogIndirectParamW( hInst, newptr, owner, dlgProc, param );
            HeapFree( GetProcessHeap(), 0, newptr );
    } else
        res = NULL;
    FreeResource ( hrsrc );
    return res;
}
1364 1365

/***********************************************************************
1366
 *           AtlAxGetHost                 [atl100.@]
1367 1368
 *
 */
1369
HRESULT WINAPI AtlAxGetHost(HWND hWnd, IUnknown **host)
1370 1371 1372
{
    IOCS *This;

1373
    TRACE("(%p, %p)\n", hWnd, host);
1374

1375
    *host = NULL;
1376

1377
    This = (IOCS*) GetPropW( hWnd, wine_atl_iocsW );
1378 1379 1380 1381 1382 1383
    if ( !This )
    {
        WARN("No container attached to %p\n", hWnd );
        return E_FAIL;
    }

1384
    return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, &IID_IUnknown, (void**)host);
1385 1386 1387
}

/***********************************************************************
1388
 *           AtlAxGetControl              [atl100.@]
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
 *
 */
HRESULT WINAPI AtlAxGetControl(HWND hWnd, IUnknown **pUnk)
{
    IOCS *This;

    TRACE( "(%p, %p)\n", hWnd, pUnk );

    *pUnk = NULL;

1399
    This = (IOCS*) GetPropW( hWnd, wine_atl_iocsW );
1400 1401 1402 1403 1404 1405 1406 1407
    if ( !This || !This->control )
    {
        WARN("No control attached to %p\n", hWnd );
        return E_FAIL;
    }

    return IOleObject_QueryInterface( This->control, &IID_IUnknown, (void**) pUnk );
}
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429

/***********************************************************************
 *           AtlAxDialogBoxW              [atl100.35]
 *
 */
INT_PTR WINAPI AtlAxDialogBoxW(HINSTANCE hInstance, LPCWSTR lpTemplateName, HWND hWndParent, DLGPROC lpDialogProc,
        LPARAM dwInitParam)
{
    FIXME("(%p %s %p %p %lx)\n", hInstance, debugstr_w(lpTemplateName), hWndParent, lpDialogProc, dwInitParam);
    return 0;
}

/***********************************************************************
 *           AtlAxDialogBoxA              [atl100.36]
 *
 */
INT_PTR WINAPI AtlAxDialogBoxA(HINSTANCE hInstance, LPCSTR lpTemplateName, HWND hWndParent, DLGPROC lpDialogProc,
        LPARAM dwInitParam)
{
    FIXME("(%p %s %p %p %lx)\n", hInstance, debugstr_a(lpTemplateName), hWndParent, lpDialogProc, dwInitParam);
    return 0;
}
1430 1431 1432 1433 1434 1435 1436

/***********************************************************************
 *           AtlAxCreateControlLic        [atl100.59]
 *
 */
HRESULT WINAPI AtlAxCreateControlLic(const WCHAR *lpTricsData, HWND hwnd, IStream *stream, IUnknown **container, BSTR lic)
{
1437
    return AtlAxCreateControlLicEx(lpTricsData, hwnd, stream, container, NULL, NULL, NULL, lic);
1438 1439 1440
}

/***********************************************************************
1441
 *           AtlAxCreateControlEx         [atl100.@]
1442 1443
 *
 */
1444 1445
HRESULT WINAPI AtlAxCreateControlEx(const WCHAR *lpTricsData, HWND hwnd, IStream *stream,
        IUnknown **container, IUnknown **control, REFIID iidSink, IUnknown *punkSink)
1446
{
1447
    return AtlAxCreateControlLicEx(lpTricsData, hwnd, stream, container, control, iidSink, punkSink, NULL);
1448
}