Release 1.5.29.
[wine] / dlls / atl100 / atl_ax.c
1 /*
2  * Active Template Library ActiveX functions (atl.dll)
3  *
4  * Copyright 2006 Andrey Turkin
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "winuser.h"
30 #include "wine/debug.h"
31 #include "objbase.h"
32 #include "objidl.h"
33 #include "ole2.h"
34 #include "exdisp.h"
35 #include "atlbase.h"
36 #include "atliface.h"
37 #include "atlwin.h"
38
39 #include "wine/unicode.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(atl);
42
43 typedef struct IOCS {
44     IOleClientSite            IOleClientSite_iface;
45     IOleContainer             IOleContainer_iface;
46     IOleInPlaceSiteWindowless IOleInPlaceSiteWindowless_iface;
47     IOleInPlaceFrame          IOleInPlaceFrame_iface;
48     IOleControlSite           IOleControlSite_iface;
49
50     LONG ref;
51     HWND hWnd;
52     IOleObject *control;
53     RECT size;
54     WNDPROC OrigWndProc;
55     BOOL fActive, fInPlace, fWindowless;
56 } IOCS;
57
58 /**********************************************************************
59  * AtlAxWin class window procedure
60  */
61 static LRESULT CALLBACK AtlAxWin_wndproc( HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
62 {
63     if ( wMsg == WM_CREATE )
64     {
65             DWORD len = GetWindowTextLengthW( hWnd ) + 1;
66             WCHAR *ptr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
67             if (!ptr)
68                 return 1;
69             GetWindowTextW( hWnd, ptr, len );
70             AtlAxCreateControlEx( ptr, hWnd, NULL, NULL, NULL, NULL, NULL );
71             HeapFree( GetProcessHeap(), 0, ptr );
72             return 0;
73     }
74     return DefWindowProcW( hWnd, wMsg, wParam, lParam );
75 }
76
77 /***********************************************************************
78  *           AtlAxWinInit          [atl100.@]
79  * Initializes the control-hosting code: registering the AtlAxWin,
80  * AtlAxWin7 and AtlAxWinLic7 window classes and some messages.
81  *
82  * RETURNS
83  *  TRUE or FALSE
84  */
85
86 BOOL WINAPI AtlAxWinInit(void)
87 {
88     WNDCLASSEXW wcex;
89     const WCHAR AtlAxWin100[] = {'A','t','l','A','x','W','i','n','1','0','0',0};
90     const WCHAR AtlAxWinLic100[] = {'A','t','l','A','x','W','i','n','L','i','c','1','0','0',0};
91
92     FIXME("semi-stub\n");
93
94     if ( FAILED( OleInitialize(NULL) ) )
95         return FALSE;
96
97     wcex.cbSize        = sizeof(wcex);
98     wcex.style         = CS_GLOBALCLASS | CS_DBLCLKS;
99     wcex.cbClsExtra    = 0;
100     wcex.cbWndExtra    = 0;
101     wcex.hInstance     = GetModuleHandleW( NULL );
102     wcex.hIcon         = NULL;
103     wcex.hCursor       = NULL;
104     wcex.hbrBackground = NULL;
105     wcex.lpszMenuName  = NULL;
106     wcex.hIconSm       = 0;
107
108     wcex.lpfnWndProc   = AtlAxWin_wndproc;
109     wcex.lpszClassName = AtlAxWin100;
110     if ( !RegisterClassExW( &wcex ) )
111         return FALSE;
112
113     wcex.lpszClassName = AtlAxWinLic100;
114     if ( !RegisterClassExW( &wcex ) )
115         return FALSE;
116
117     return TRUE;
118 }
119
120 /***********************************************************************
121  *  Atl container component implementation
122  */
123
124
125 static ULONG IOCS_AddRef(IOCS *This)
126 {
127     ULONG ref = InterlockedIncrement(&This->ref);
128
129     TRACE( "(%p) : AddRef from %d\n", This, ref - 1 );
130
131     return ref;
132 }
133
134 static HRESULT IOCS_QueryInterface(IOCS *This, REFIID riid, void **ppv)
135 {
136     *ppv = NULL;
137
138     if ( IsEqualIID( &IID_IUnknown, riid )
139       || IsEqualIID( &IID_IOleClientSite, riid ) )
140     {
141         *ppv = &This->IOleClientSite_iface;
142     } else if ( IsEqualIID( &IID_IOleContainer, riid ) )
143     {
144         *ppv = &This->IOleContainer_iface;
145     } else if ( IsEqualIID( &IID_IOleInPlaceSite, riid ) || IsEqualIID( &IID_IOleInPlaceSiteEx, riid ) || IsEqualIID( &IID_IOleInPlaceSiteWindowless, riid ) )
146     {
147         *ppv = &This->IOleInPlaceSiteWindowless_iface;
148     } else if ( IsEqualIID( &IID_IOleInPlaceFrame, riid ) )
149     {
150         *ppv = &This->IOleInPlaceFrame_iface;
151     } else if ( IsEqualIID( &IID_IOleControlSite, riid ) )
152     {
153         *ppv = &This->IOleControlSite_iface;
154     }
155
156     if (*ppv)
157     {
158         IOCS_AddRef( This );
159         return S_OK;
160     }
161
162     WARN("unsupported interface %s\n", debugstr_guid( riid ) );
163     *ppv = NULL;
164     return E_NOINTERFACE;
165 }
166
167 static HRESULT IOCS_Detach( IOCS *This );
168 static ULONG IOCS_Release(IOCS *This)
169 {
170     ULONG ref = InterlockedDecrement(&This->ref);
171
172     TRACE( "(%p) : ReleaseRef to %d\n", This, ref );
173
174     if (!ref)
175     {
176         IOCS_Detach( This );
177         HeapFree( GetProcessHeap(), 0, This );
178     }
179
180     return ref;
181 }
182
183 /******      IOleClientSite    *****/
184 static inline IOCS *impl_from_IOleClientSite(IOleClientSite *iface)
185 {
186     return CONTAINING_RECORD(iface, IOCS, IOleClientSite_iface);
187 }
188
189 static HRESULT WINAPI OleClientSite_QueryInterface(IOleClientSite *iface, REFIID riid, void **ppv)
190 {
191     IOCS *This = impl_from_IOleClientSite(iface);
192     return IOCS_QueryInterface(This, riid, ppv);
193 }
194
195 static ULONG WINAPI OleClientSite_AddRef(IOleClientSite *iface)
196 {
197     IOCS *This = impl_from_IOleClientSite(iface);
198     return IOCS_AddRef(This);
199 }
200
201 static ULONG WINAPI OleClientSite_Release(IOleClientSite *iface)
202 {
203     IOCS *This = impl_from_IOleClientSite(iface);
204     return IOCS_Release(This);
205 }
206
207 static HRESULT WINAPI OleClientSite_SaveObject(IOleClientSite *iface)
208 {
209     IOCS *This = impl_from_IOleClientSite(iface);
210     FIXME( "(%p) - stub\n", This );
211     return E_NOTIMPL;
212 }
213
214 static HRESULT WINAPI OleClientSite_GetMoniker(IOleClientSite *iface, DWORD dwAssign, DWORD dwWhichMoniker, IMoniker **ppmk)
215 {
216     IOCS *This = impl_from_IOleClientSite(iface);
217
218     FIXME( "(%p, 0x%x, 0x%x, %p)\n", This, dwAssign, dwWhichMoniker, ppmk );
219     return E_NOTIMPL;
220 }
221
222 static HRESULT WINAPI OleClientSite_GetContainer(IOleClientSite *iface, IOleContainer **ppContainer)
223 {
224     IOCS *This = impl_from_IOleClientSite(iface);
225     TRACE( "(%p, %p)\n", This, ppContainer );
226     return OleClientSite_QueryInterface( iface, &IID_IOleContainer, (void**)ppContainer );
227 }
228
229 static HRESULT WINAPI OleClientSite_ShowObject(IOleClientSite *iface)
230 {
231     IOCS *This = impl_from_IOleClientSite(iface);
232     FIXME( "(%p) - stub\n", This );
233     return S_OK;
234 }
235
236 static HRESULT WINAPI OleClientSite_OnShowWindow(IOleClientSite *iface, BOOL fShow)
237 {
238     IOCS *This = impl_from_IOleClientSite(iface);
239     FIXME( "(%p, %s) - stub\n", This, fShow ? "TRUE" : "FALSE" );
240     return E_NOTIMPL;
241 }
242
243 static HRESULT WINAPI OleClientSite_RequestNewObjectLayout(IOleClientSite *iface)
244 {
245     IOCS *This = impl_from_IOleClientSite(iface);
246     FIXME( "(%p) - stub\n", This );
247     return E_NOTIMPL;
248 }
249
250
251 /******      IOleContainer     *****/
252 static inline IOCS *impl_from_IOleContainer(IOleContainer *iface)
253 {
254     return CONTAINING_RECORD(iface, IOCS, IOleContainer_iface);
255 }
256
257 static HRESULT WINAPI OleContainer_QueryInterface( IOleContainer* iface, REFIID riid, void** ppv)
258 {
259     IOCS *This = impl_from_IOleContainer(iface);
260     return IOCS_QueryInterface( This, riid, ppv );
261 }
262
263 static ULONG WINAPI OleContainer_AddRef(IOleContainer* iface)
264 {
265     IOCS *This = impl_from_IOleContainer(iface);
266     return IOCS_AddRef(This);
267 }
268
269 static ULONG WINAPI OleContainer_Release(IOleContainer* iface)
270 {
271     IOCS *This = impl_from_IOleContainer(iface);
272     return IOCS_Release(This);
273 }
274
275 static HRESULT WINAPI OleContainer_ParseDisplayName(IOleContainer* iface, IBindCtx* pbc,
276         LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut)
277 {
278     IOCS *This = impl_from_IOleContainer(iface);
279     FIXME( "(%p,%p,%s,%p,%p) - stub\n", This, pbc, debugstr_w(pszDisplayName), pchEaten, ppmkOut );
280     return E_NOTIMPL;
281 }
282
283 static HRESULT WINAPI OleContainer_EnumObjects(IOleContainer* iface, DWORD grfFlags, IEnumUnknown** ppenum)
284 {
285     IOCS *This = impl_from_IOleContainer(iface);
286     FIXME( "(%p, %u, %p) - stub\n", This, grfFlags, ppenum );
287     return E_NOTIMPL;
288 }
289
290 static HRESULT WINAPI OleContainer_LockContainer(IOleContainer* iface, BOOL fLock)
291 {
292     IOCS *This = impl_from_IOleContainer(iface);
293     FIXME( "(%p, %s) - stub\n", This, fLock?"TRUE":"FALSE" );
294     return E_NOTIMPL;
295 }
296
297
298 /******    IOleInPlaceSiteWindowless   *******/
299 static inline IOCS *impl_from_IOleInPlaceSiteWindowless(IOleInPlaceSiteWindowless *iface)
300 {
301     return CONTAINING_RECORD(iface, IOCS, IOleInPlaceSiteWindowless_iface);
302 }
303
304 static HRESULT WINAPI OleInPlaceSiteWindowless_QueryInterface(IOleInPlaceSiteWindowless *iface, REFIID riid, void **ppv)
305 {
306     IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
307     return IOCS_QueryInterface(This, riid, ppv);
308 }
309
310 static ULONG WINAPI OleInPlaceSiteWindowless_AddRef(IOleInPlaceSiteWindowless *iface)
311 {
312     IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
313     return IOCS_AddRef(This);
314 }
315
316 static ULONG WINAPI OleInPlaceSiteWindowless_Release(IOleInPlaceSiteWindowless *iface)
317 {
318     IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
319     return IOCS_Release(This);
320 }
321
322 static HRESULT WINAPI OleInPlaceSiteWindowless_GetWindow(IOleInPlaceSiteWindowless* iface, HWND* phwnd)
323 {
324     IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
325
326     TRACE("(%p,%p)\n", This, phwnd);
327     *phwnd = This->hWnd;
328     return S_OK;
329 }
330
331 static HRESULT WINAPI OleInPlaceSiteWindowless_ContextSensitiveHelp(IOleInPlaceSiteWindowless* iface, BOOL fEnterMode)
332 {
333     IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
334     FIXME("(%p,%d) - stub\n", This, fEnterMode);
335     return E_NOTIMPL;
336 }
337
338 static HRESULT WINAPI OleInPlaceSiteWindowless_CanInPlaceActivate(IOleInPlaceSiteWindowless *iface)
339 {
340     IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
341     TRACE("(%p)\n", This);
342     return S_OK;
343 }
344
345 static HRESULT WINAPI OleInPlaceSiteWindowless_OnInPlaceActivate(IOleInPlaceSiteWindowless *iface)
346 {
347     IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
348
349     TRACE("(%p)\n", This);
350
351     This->fInPlace = TRUE;
352     return S_OK;
353 }
354
355 static HRESULT WINAPI OleInPlaceSiteWindowless_OnUIActivate(IOleInPlaceSiteWindowless *iface)
356 {
357     IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
358
359     TRACE("(%p)\n", This);
360
361     return S_OK;
362 }
363 static HRESULT WINAPI OleInPlaceSiteWindowless_GetWindowContext(IOleInPlaceSiteWindowless *iface,
364         IOleInPlaceFrame **ppFrame, IOleInPlaceUIWindow **ppDoc, LPRECT lprcPosRect,
365         LPRECT lprcClipRect, LPOLEINPLACEFRAMEINFO lpFrameInfo)
366 {
367     IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
368
369     TRACE("(%p,%p,%p,%p,%p,%p)\n", This, ppFrame, ppDoc, lprcPosRect, lprcClipRect, lpFrameInfo);
370
371     if ( lprcClipRect )
372         *lprcClipRect = This->size;
373     if ( lprcPosRect )
374         *lprcPosRect = This->size;
375
376     if ( ppFrame )
377     {
378         IOCS_QueryInterface( This, &IID_IOleInPlaceFrame, (void**) ppFrame );
379     }
380
381     if ( ppDoc )
382         *ppDoc = NULL;
383
384     if ( lpFrameInfo )
385     {
386         lpFrameInfo->fMDIApp = FALSE;
387         lpFrameInfo->hwndFrame = This->hWnd;
388         lpFrameInfo->haccel = NULL;
389         lpFrameInfo->cAccelEntries = 0;
390     }
391
392     return S_OK;
393 }
394
395 static HRESULT WINAPI OleInPlaceSiteWindowless_Scroll(IOleInPlaceSiteWindowless *iface, SIZE scrollExtent)
396 {
397     IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
398     FIXME("(%p) - stub\n", This);
399     return E_NOTIMPL;
400 }
401
402 static HRESULT WINAPI OleInPlaceSiteWindowless_OnUIDeactivate(IOleInPlaceSiteWindowless *iface, BOOL fUndoable)
403 {
404     IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
405     FIXME("(%p,%d) - stub\n", This, fUndoable);
406     return E_NOTIMPL;
407 }
408
409 static HRESULT WINAPI OleInPlaceSiteWindowless_OnInPlaceDeactivate(IOleInPlaceSiteWindowless *iface)
410 {
411     IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
412
413     TRACE("(%p)\n", This);
414
415     This->fInPlace = This->fWindowless = FALSE;
416     return S_OK;
417 }
418
419 static HRESULT WINAPI OleInPlaceSiteWindowless_DiscardUndoState(IOleInPlaceSiteWindowless *iface)
420 {
421     IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
422     FIXME("(%p) - stub\n", This);
423     return E_NOTIMPL;
424 }
425
426 static HRESULT WINAPI OleInPlaceSiteWindowless_DeactivateAndUndo(IOleInPlaceSiteWindowless *iface)
427 {
428     IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
429     FIXME("(%p) - stub\n", This);
430     return E_NOTIMPL;
431 }
432
433 static HRESULT WINAPI OleInPlaceSiteWindowless_OnPosRectChange(IOleInPlaceSiteWindowless *iface, LPCRECT lprcPosRect)
434 {
435     IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
436     FIXME("(%p,%p) - stub\n", This, lprcPosRect);
437     return E_NOTIMPL;
438 }
439
440 static HRESULT WINAPI OleInPlaceSiteWindowless_OnInPlaceActivateEx( IOleInPlaceSiteWindowless *iface, BOOL* pfNoRedraw, DWORD dwFlags)
441 {
442     IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
443
444     TRACE("\n");
445
446     This->fActive = This->fInPlace = TRUE;
447     if ( dwFlags & ACTIVATE_WINDOWLESS )
448         This->fWindowless = TRUE;
449     return S_OK;
450 }
451
452 static HRESULT WINAPI OleInPlaceSiteWindowless_OnInPlaceDeactivateEx( IOleInPlaceSiteWindowless *iface, BOOL fNoRedraw)
453 {
454     IOCS *This = impl_from_IOleInPlaceSiteWindowless(iface);
455
456     TRACE("\n");
457
458     This->fActive = This->fInPlace = This->fWindowless = FALSE;
459     return S_OK;
460 }
461 static HRESULT WINAPI OleInPlaceSiteWindowless_RequestUIActivate( IOleInPlaceSiteWindowless *iface)
462 {
463     FIXME("\n");
464     return E_NOTIMPL;
465 }
466 static HRESULT WINAPI OleInPlaceSiteWindowless_CanWindowlessActivate( IOleInPlaceSiteWindowless *iface)
467 {
468     FIXME("\n");
469     return S_OK;
470 }
471 static HRESULT WINAPI OleInPlaceSiteWindowless_GetCapture( IOleInPlaceSiteWindowless *iface)
472 {
473     FIXME("\n");
474     return E_NOTIMPL;
475 }
476 static HRESULT WINAPI OleInPlaceSiteWindowless_SetCapture( IOleInPlaceSiteWindowless *iface, BOOL fCapture)
477 {
478     FIXME("\n");
479     return E_NOTIMPL;
480 }
481 static HRESULT WINAPI OleInPlaceSiteWindowless_GetFocus( IOleInPlaceSiteWindowless *iface)
482 {
483     FIXME("\n");
484     return E_NOTIMPL;
485 }
486 static HRESULT WINAPI OleInPlaceSiteWindowless_SetFocus( IOleInPlaceSiteWindowless *iface, BOOL fFocus)
487 {
488     FIXME("\n");
489     return E_NOTIMPL;
490 }
491 static HRESULT WINAPI OleInPlaceSiteWindowless_GetDC( IOleInPlaceSiteWindowless *iface, LPCRECT pRect, DWORD grfFlags, HDC* phDC)
492 {
493     FIXME("\n");
494     return E_NOTIMPL;
495 }
496 static HRESULT WINAPI OleInPlaceSiteWindowless_ReleaseDC( IOleInPlaceSiteWindowless *iface, HDC hDC)
497 {
498     FIXME("\n");
499     return E_NOTIMPL;
500 }
501 static HRESULT WINAPI OleInPlaceSiteWindowless_InvalidateRect( IOleInPlaceSiteWindowless *iface, LPCRECT pRect, BOOL fErase)
502 {
503     FIXME("\n");
504     return E_NOTIMPL;
505 }
506 static HRESULT WINAPI OleInPlaceSiteWindowless_InvalidateRgn( IOleInPlaceSiteWindowless *iface, HRGN hRGN, BOOL fErase)
507 {
508     FIXME("\n");
509     return E_NOTIMPL;
510 }
511 static HRESULT WINAPI OleInPlaceSiteWindowless_ScrollRect( IOleInPlaceSiteWindowless *iface, INT dx, INT dy, LPCRECT pRectScroll, LPCRECT pRectClip)
512 {
513     FIXME("\n");
514     return E_NOTIMPL;
515 }
516 static HRESULT WINAPI OleInPlaceSiteWindowless_AdjustRect( IOleInPlaceSiteWindowless *iface, LPRECT prc)
517 {
518     FIXME("\n");
519     return E_NOTIMPL;
520 }
521 static HRESULT WINAPI OleInPlaceSiteWindowless_OnDefWindowMessage( IOleInPlaceSiteWindowless *iface, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT* plResult)
522 {
523     FIXME("\n");
524     return E_NOTIMPL;
525 }
526
527
528 /******    IOleInPlaceFrame   *******/
529 static inline IOCS *impl_from_IOleInPlaceFrame(IOleInPlaceFrame *iface)
530 {
531     return CONTAINING_RECORD(iface, IOCS, IOleInPlaceFrame_iface);
532 }
533
534 static HRESULT WINAPI OleInPlaceFrame_QueryInterface(IOleInPlaceFrame *iface, REFIID riid, void **ppv)
535 {
536     IOCS *This = impl_from_IOleInPlaceFrame(iface);
537     return IOCS_QueryInterface(This, riid, ppv);
538 }
539
540 static ULONG WINAPI OleInPlaceFrame_AddRef(IOleInPlaceFrame *iface)
541 {
542     IOCS *This = impl_from_IOleInPlaceFrame(iface);
543     return IOCS_AddRef(This);
544 }
545
546 static ULONG WINAPI OleInPlaceFrame_Release(IOleInPlaceFrame *iface)
547 {
548     IOCS *This = impl_from_IOleInPlaceFrame(iface);
549     return IOCS_Release(This);
550 }
551
552 static HRESULT WINAPI OleInPlaceFrame_GetWindow(IOleInPlaceFrame *iface, HWND *phWnd)
553 {
554     IOCS *This = impl_from_IOleInPlaceFrame(iface);
555
556     TRACE( "(%p,%p)\n", This, phWnd );
557
558     *phWnd = This->hWnd;
559     return S_OK;
560 }
561
562 static HRESULT WINAPI OleInPlaceFrame_ContextSensitiveHelp(IOleInPlaceFrame *iface, BOOL fEnterMode)
563 {
564     IOCS *This = impl_from_IOleInPlaceFrame(iface);
565
566     FIXME( "(%p,%d) - stub\n", This, fEnterMode );
567     return E_NOTIMPL;
568 }
569
570 static HRESULT WINAPI OleInPlaceFrame_GetBorder(IOleInPlaceFrame *iface, LPRECT lprectBorder)
571 {
572     IOCS *This = impl_from_IOleInPlaceFrame(iface);
573
574     FIXME( "(%p,%p) - stub\n", This, lprectBorder );
575     return E_NOTIMPL;
576 }
577
578 static HRESULT WINAPI OleInPlaceFrame_RequestBorderSpace(IOleInPlaceFrame *iface, LPCBORDERWIDTHS pborderwidths)
579 {
580     IOCS *This = impl_from_IOleInPlaceFrame(iface);
581
582     FIXME( "(%p,%p) - stub\n", This, pborderwidths );
583     return E_NOTIMPL;
584 }
585
586 static HRESULT WINAPI OleInPlaceFrame_SetBorderSpace(IOleInPlaceFrame *iface, LPCBORDERWIDTHS pborderwidths)
587 {
588     IOCS *This = impl_from_IOleInPlaceFrame(iface);
589
590     FIXME( "(%p,%p) - stub\n", This, pborderwidths );
591     return E_NOTIMPL;
592 }
593
594 static HRESULT WINAPI OleInPlaceFrame_SetActiveObject(IOleInPlaceFrame *iface, IOleInPlaceActiveObject *pActiveObject, LPCOLESTR pszObjName)
595 {
596     IOCS *This = impl_from_IOleInPlaceFrame(iface);
597
598     FIXME( "(%p,%p,%s) - stub\n", This, pActiveObject, debugstr_w(pszObjName) );
599     return S_OK;
600 }
601
602 static HRESULT WINAPI OleInPlaceFrame_InsertMenus(IOleInPlaceFrame *iface, HMENU hmenuShared, LPOLEMENUGROUPWIDTHS lpMenuWidths)
603 {
604     IOCS *This = impl_from_IOleInPlaceFrame(iface);
605
606     FIXME( "(%p,%p,%p) - stub\n", This, hmenuShared, lpMenuWidths );
607     return E_NOTIMPL;
608 }
609
610 static HRESULT WINAPI OleInPlaceFrame_SetMenu(IOleInPlaceFrame *iface, HMENU hmenuShared, HOLEMENU holemenu, HWND hwndActiveObject)
611 {
612     IOCS *This = impl_from_IOleInPlaceFrame(iface);
613
614     FIXME( "(%p,%p,%p,%p) - stub\n", This, hmenuShared, holemenu, hwndActiveObject );
615     return E_NOTIMPL;
616 }
617
618 static HRESULT WINAPI OleInPlaceFrame_RemoveMenus(IOleInPlaceFrame *iface, HMENU hmenuShared)
619 {
620     IOCS *This = impl_from_IOleInPlaceFrame(iface);
621
622     FIXME( "(%p, %p) - stub\n", This, hmenuShared );
623     return E_NOTIMPL;
624 }
625
626 static HRESULT WINAPI OleInPlaceFrame_SetStatusText(IOleInPlaceFrame *iface, LPCOLESTR pszStatusText)
627 {
628     IOCS *This = impl_from_IOleInPlaceFrame(iface);
629
630     FIXME( "(%p, %s) - stub\n", This, debugstr_w( pszStatusText ) );
631     return E_NOTIMPL;
632 }
633
634 static HRESULT WINAPI OleInPlaceFrame_EnableModeless(IOleInPlaceFrame *iface, BOOL fEnable)
635 {
636     IOCS *This = impl_from_IOleInPlaceFrame(iface);
637
638     FIXME( "(%p, %d) - stub\n", This, fEnable );
639     return E_NOTIMPL;
640 }
641
642 static HRESULT WINAPI OleInPlaceFrame_TranslateAccelerator(IOleInPlaceFrame *iface, LPMSG lpmsg, WORD wID)
643 {
644     IOCS *This = impl_from_IOleInPlaceFrame(iface);
645
646     FIXME( "(%p, %p, %x) - stub\n", This, lpmsg, wID );
647     return E_NOTIMPL;
648 }
649
650
651 /******    IOleControlSite    *******/
652 static inline IOCS *impl_from_IOleControlSite(IOleControlSite *iface)
653 {
654     return CONTAINING_RECORD(iface, IOCS, IOleControlSite_iface);
655 }
656
657 static HRESULT WINAPI OleControlSite_QueryInterface(IOleControlSite *iface, REFIID riid, void **ppv)
658 {
659     IOCS *This = impl_from_IOleControlSite(iface);
660     return IOCS_QueryInterface(This, riid, ppv);
661 }
662
663 static ULONG WINAPI OleControlSite_AddRef(IOleControlSite *iface)
664 {
665     IOCS *This = impl_from_IOleControlSite(iface);
666     return IOCS_AddRef(This);
667 }
668
669 static ULONG WINAPI OleControlSite_Release(IOleControlSite *iface)
670 {
671     IOCS *This = impl_from_IOleControlSite(iface);
672     return IOCS_Release(This);
673 }
674
675 static HRESULT WINAPI OleControlSite_OnControlInfoChanged( IOleControlSite* This)
676 {
677     FIXME( "\n" );
678     return E_NOTIMPL;
679 }
680 static HRESULT WINAPI OleControlSite_LockInPlaceActive( IOleControlSite* This, BOOL fLock)
681 {
682     FIXME( "\n" );
683     return E_NOTIMPL;
684 }
685 static HRESULT WINAPI OleControlSite_GetExtendedControl( IOleControlSite* This, IDispatch** ppDisp)
686 {
687     FIXME( "\n" );
688     return E_NOTIMPL;
689 }
690 static HRESULT WINAPI OleControlSite_TransformCoords( IOleControlSite* This, POINTL* pPtlHimetric, POINTF* pPtfContainer, DWORD dwFlags)
691 {
692     FIXME( "\n" );
693     return E_NOTIMPL;
694 }
695 static HRESULT WINAPI OleControlSite_TranslateAccelerator( IOleControlSite* This, MSG* pMsg, DWORD grfModifiers)
696 {
697     FIXME( "\n" );
698     return E_NOTIMPL;
699 }
700 static HRESULT WINAPI OleControlSite_OnFocus( IOleControlSite* This, BOOL fGotFocus)
701 {
702     FIXME( "\n" );
703     return E_NOTIMPL;
704 }
705 static HRESULT WINAPI OleControlSite_ShowPropertyFrame( IOleControlSite* This)
706 {
707     FIXME( "\n" );
708     return E_NOTIMPL;
709 }
710
711
712 static const IOleClientSiteVtbl OleClientSite_vtbl = {
713     OleClientSite_QueryInterface,
714     OleClientSite_AddRef,
715     OleClientSite_Release,
716     OleClientSite_SaveObject,
717     OleClientSite_GetMoniker,
718     OleClientSite_GetContainer,
719     OleClientSite_ShowObject,
720     OleClientSite_OnShowWindow,
721     OleClientSite_RequestNewObjectLayout
722 };
723 static const IOleContainerVtbl OleContainer_vtbl = {
724     OleContainer_QueryInterface,
725     OleContainer_AddRef,
726     OleContainer_Release,
727     OleContainer_ParseDisplayName,
728     OleContainer_EnumObjects,
729     OleContainer_LockContainer
730 };
731 static const IOleInPlaceSiteWindowlessVtbl OleInPlaceSiteWindowless_vtbl = {
732     OleInPlaceSiteWindowless_QueryInterface,
733     OleInPlaceSiteWindowless_AddRef,
734     OleInPlaceSiteWindowless_Release,
735     OleInPlaceSiteWindowless_GetWindow,
736     OleInPlaceSiteWindowless_ContextSensitiveHelp,
737     OleInPlaceSiteWindowless_CanInPlaceActivate,
738     OleInPlaceSiteWindowless_OnInPlaceActivate,
739     OleInPlaceSiteWindowless_OnUIActivate,
740     OleInPlaceSiteWindowless_GetWindowContext,
741     OleInPlaceSiteWindowless_Scroll,
742     OleInPlaceSiteWindowless_OnUIDeactivate,
743     OleInPlaceSiteWindowless_OnInPlaceDeactivate,
744     OleInPlaceSiteWindowless_DiscardUndoState,
745     OleInPlaceSiteWindowless_DeactivateAndUndo,
746     OleInPlaceSiteWindowless_OnPosRectChange,
747     OleInPlaceSiteWindowless_OnInPlaceActivateEx,
748     OleInPlaceSiteWindowless_OnInPlaceDeactivateEx,
749     OleInPlaceSiteWindowless_RequestUIActivate,
750     OleInPlaceSiteWindowless_CanWindowlessActivate,
751     OleInPlaceSiteWindowless_GetCapture,
752     OleInPlaceSiteWindowless_SetCapture,
753     OleInPlaceSiteWindowless_GetFocus,
754     OleInPlaceSiteWindowless_SetFocus,
755     OleInPlaceSiteWindowless_GetDC,
756     OleInPlaceSiteWindowless_ReleaseDC,
757     OleInPlaceSiteWindowless_InvalidateRect,
758     OleInPlaceSiteWindowless_InvalidateRgn,
759     OleInPlaceSiteWindowless_ScrollRect,
760     OleInPlaceSiteWindowless_AdjustRect,
761     OleInPlaceSiteWindowless_OnDefWindowMessage
762 };
763 static const IOleInPlaceFrameVtbl OleInPlaceFrame_vtbl =
764 {
765     OleInPlaceFrame_QueryInterface,
766     OleInPlaceFrame_AddRef,
767     OleInPlaceFrame_Release,
768     OleInPlaceFrame_GetWindow,
769     OleInPlaceFrame_ContextSensitiveHelp,
770     OleInPlaceFrame_GetBorder,
771     OleInPlaceFrame_RequestBorderSpace,
772     OleInPlaceFrame_SetBorderSpace,
773     OleInPlaceFrame_SetActiveObject,
774     OleInPlaceFrame_InsertMenus,
775     OleInPlaceFrame_SetMenu,
776     OleInPlaceFrame_RemoveMenus,
777     OleInPlaceFrame_SetStatusText,
778     OleInPlaceFrame_EnableModeless,
779     OleInPlaceFrame_TranslateAccelerator
780 };
781 static const IOleControlSiteVtbl OleControlSite_vtbl =
782 {
783     OleControlSite_QueryInterface,
784     OleControlSite_AddRef,
785     OleControlSite_Release,
786     OleControlSite_OnControlInfoChanged,
787     OleControlSite_LockInPlaceActive,
788     OleControlSite_GetExtendedControl,
789     OleControlSite_TransformCoords,
790     OleControlSite_TranslateAccelerator,
791     OleControlSite_OnFocus,
792     OleControlSite_ShowPropertyFrame
793 };
794
795 static HRESULT IOCS_Detach( IOCS *This ) /* remove subclassing */
796 {
797     if ( This->hWnd )
798     {
799         SetWindowLongPtrW( This->hWnd, GWLP_WNDPROC, (ULONG_PTR) This->OrigWndProc );
800         SetWindowLongPtrW( This->hWnd, GWLP_USERDATA, 0 );
801         This->hWnd = NULL;
802     }
803     if ( This->control )
804     {
805         IOleObject *control = This->control;
806
807         This->control = NULL;
808         IOleObject_Close( control, OLECLOSE_NOSAVE );
809         IOleObject_SetClientSite( control, NULL );
810         IOleObject_Release( control );
811     }
812     return S_OK;
813 }
814
815 static void IOCS_OnSize( IOCS* This, LPCRECT rect )
816 {
817     SIZEL inPix, inHi;
818
819     This->size.left = rect->left; This->size.right = rect->right; This->size.top = rect->top; This->size.bottom = rect->bottom;
820
821     if ( !This->control )
822         return;
823
824     inPix.cx = rect->right - rect->left;
825     inPix.cy = rect->bottom - rect->top;
826     AtlPixelToHiMetric( &inPix, &inHi );
827     IOleObject_SetExtent( This->control, DVASPECT_CONTENT, &inHi );
828
829     if ( This->fInPlace )
830     {
831         IOleInPlaceObject *wl;
832
833         if ( SUCCEEDED( IOleObject_QueryInterface( This->control, &IID_IOleInPlaceObject, (void**)&wl ) ) )
834         {
835             IOleInPlaceObject_SetObjectRects( wl, rect, rect );
836             IOleInPlaceObject_Release( wl );
837         }
838     }
839 }
840
841 static void IOCS_OnShow( IOCS *This, BOOL fShow )
842 {
843     if (!This->control || This->fActive || !fShow )
844         return;
845
846     This->fActive = TRUE;
847 }
848
849 static void IOCS_OnDraw( IOCS *This )
850 {
851     IViewObject *view;
852
853     if ( !This->control || !This->fWindowless )
854         return;
855
856     if ( SUCCEEDED( IOleObject_QueryInterface( This->control, &IID_IViewObject, (void**)&view ) ) )
857     {
858         HDC dc = GetDC( This->hWnd );
859         RECTL rect;
860
861         rect.left = This->size.left; rect.top = This->size.top;
862         rect.bottom = This->size.bottom; rect.right = This->size.right;
863
864         IViewObject_Draw( view, DVASPECT_CONTENT, ~0, NULL, NULL, 0, dc, &rect, &rect, NULL, 0 );
865         IViewObject_Release( view );
866         ReleaseDC( This->hWnd, dc );
867     }
868 }
869
870 static LRESULT IOCS_OnWndProc( IOCS *This, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
871 {
872     WNDPROC OrigWndProc = This->OrigWndProc;
873
874     switch( uMsg )
875     {
876         case WM_DESTROY:
877             IOCS_Detach( This );
878             break;
879         case WM_SIZE:
880             {
881                 RECT r;
882                 r.left = r.top = 0;
883                 r.right = LOWORD( lParam );
884                 r.bottom = HIWORD( lParam );
885                 IOCS_OnSize( This, &r );
886             }
887             break;
888         case WM_SHOWWINDOW:
889             IOCS_OnShow( This, (BOOL) wParam );
890             break;
891         case WM_PAINT:
892             IOCS_OnDraw( This );
893             break;
894     }
895
896     return CallWindowProcW( OrigWndProc, hWnd, uMsg, wParam, lParam );
897 }
898
899 static LRESULT CALLBACK AtlHost_wndproc( HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
900 {
901     IOCS *This = (IOCS*) GetWindowLongPtrW( hWnd, GWLP_USERDATA );
902     return IOCS_OnWndProc( This, hWnd, wMsg, wParam, lParam );
903 }
904
905 static HRESULT IOCS_Attach( IOCS *This, HWND hWnd, IUnknown *pUnkControl ) /* subclass hWnd */
906 {
907     This->hWnd = hWnd;
908     IUnknown_QueryInterface( pUnkControl, &IID_IOleObject, (void**)&This->control );
909     IOleObject_SetClientSite( This->control, &This->IOleClientSite_iface );
910     SetWindowLongPtrW( hWnd, GWLP_USERDATA, (ULONG_PTR) This );
911     This->OrigWndProc = (WNDPROC)SetWindowLongPtrW( hWnd, GWLP_WNDPROC, (ULONG_PTR) AtlHost_wndproc );
912
913     return S_OK;
914 }
915
916 static HRESULT IOCS_Init( IOCS *This )
917 {
918     RECT rect;
919     static const WCHAR AXWIN[] = {'A','X','W','I','N',0};
920
921     IOleObject_SetHostNames( This->control, AXWIN, AXWIN );
922
923     GetClientRect( This->hWnd, &rect );
924     IOCS_OnSize( This, &rect );
925     IOleObject_DoVerb( This->control, OLEIVERB_INPLACEACTIVATE, NULL, &This->IOleClientSite_iface,
926                        0, This->hWnd, &rect );
927
928     return S_OK;
929 }
930
931 /**********************************************************************
932  * Create new instance of Atl host component and attach it to window  *
933  */
934 static HRESULT IOCS_Create( HWND hWnd, IUnknown *pUnkControl, IOCS **ppSite )
935 {
936     HRESULT hr;
937     IOCS *This;
938
939     *ppSite = NULL;
940     This = HeapAlloc(GetProcessHeap(), 0, sizeof(IOCS));
941
942     if (!This)
943         return E_OUTOFMEMORY;
944
945     This->IOleClientSite_iface.lpVtbl = &OleClientSite_vtbl;
946     This->IOleContainer_iface.lpVtbl = &OleContainer_vtbl;
947     This->IOleInPlaceSiteWindowless_iface.lpVtbl = &OleInPlaceSiteWindowless_vtbl;
948     This->IOleInPlaceFrame_iface.lpVtbl = &OleInPlaceFrame_vtbl;
949     This->IOleControlSite_iface.lpVtbl = &OleControlSite_vtbl;
950     This->ref = 1;
951
952     This->OrigWndProc = NULL;
953     This->hWnd = NULL;
954     This->fWindowless = This->fActive = This->fInPlace = FALSE;
955
956     hr = IOCS_Attach( This, hWnd, pUnkControl );
957     if ( SUCCEEDED( hr ) )
958         hr = IOCS_Init( This );
959     if ( SUCCEEDED( hr ) )
960         *ppSite = This;
961     else
962         IOCS_Release( This );
963
964     return hr;
965 }
966
967
968 /***********************************************************************
969  *           AtlAxCreateControl           [atl100.@]
970  */
971 HRESULT WINAPI AtlAxCreateControl(LPCOLESTR lpszName, HWND hWnd,
972         IStream *pStream, IUnknown **ppUnkContainer)
973 {
974     return AtlAxCreateControlEx( lpszName, hWnd, pStream, ppUnkContainer,
975             NULL, NULL, NULL );
976 }
977
978 /***********************************************************************
979  *           AtlAxCreateControlEx            [atl100.@]
980  *
981  * REMARKS
982  *   See http://www.codeproject.com/com/cwebpage.asp for some background
983  *
984  */
985 HRESULT WINAPI AtlAxCreateControlEx(LPCOLESTR lpszName, HWND hWnd,
986         IStream *pStream, IUnknown **ppUnkContainer, IUnknown **ppUnkControl,
987         REFIID iidSink, IUnknown *punkSink)
988 {
989     CLSID controlId;
990     HRESULT hRes;
991     IOleObject *pControl;
992     IUnknown *pUnkControl;
993     IPersistStreamInit *pPSInit;
994     IUnknown *pContainer;
995     enum {IsGUID=0,IsHTML=1,IsURL=2} content;
996
997     TRACE("(%s %p %p %p %p %p %p)\n", debugstr_w(lpszName), hWnd, pStream,
998             ppUnkContainer, ppUnkControl, iidSink, punkSink);
999
1000     hRes = CLSIDFromString( lpszName, &controlId );
1001     if ( FAILED(hRes) )
1002         hRes = CLSIDFromProgID( lpszName, &controlId );
1003     if ( SUCCEEDED( hRes ) )
1004         content = IsGUID;
1005     else {
1006         /* FIXME - check for MSHTML: prefix! */
1007         content = IsURL;
1008         controlId = CLSID_WebBrowser;
1009     }
1010
1011     hRes = CoCreateInstance( &controlId, 0, CLSCTX_ALL, &IID_IOleObject,
1012             (void**) &pControl );
1013     if ( FAILED( hRes ) )
1014     {
1015         WARN( "cannot create ActiveX control %s instance - error 0x%08x\n",
1016                 debugstr_guid( &controlId ), hRes );
1017         return hRes;
1018     }
1019
1020     hRes = IOleObject_QueryInterface( pControl, &IID_IPersistStreamInit, (void**) &pPSInit );
1021     if ( SUCCEEDED( hRes ) )
1022     {
1023         if (!pStream)
1024             IPersistStreamInit_InitNew( pPSInit );
1025         else
1026             IPersistStreamInit_Load( pPSInit, pStream );
1027         IPersistStreamInit_Release( pPSInit );
1028     } else
1029         WARN("cannot get IID_IPersistStreamInit out of control\n");
1030
1031     IOleObject_QueryInterface( pControl, &IID_IUnknown, (void**) &pUnkControl );
1032     IOleObject_Release( pControl );
1033
1034
1035     hRes = AtlAxAttachControl( pUnkControl, hWnd, &pContainer );
1036     if ( FAILED( hRes ) )
1037         WARN("cannot attach control to window\n");
1038
1039     if ( content == IsURL )
1040     {
1041         IWebBrowser2 *browser;
1042
1043         hRes = IOleObject_QueryInterface( pControl, &IID_IWebBrowser2, (void**) &browser );
1044         if ( !browser )
1045             WARN( "Cannot query IWebBrowser2 interface: %08x\n", hRes );
1046         else {
1047             VARIANT url;
1048
1049             IWebBrowser2_put_Visible( browser, VARIANT_TRUE ); /* it seems that native does this on URL (but do not on MSHTML:! why? */
1050
1051             V_VT(&url) = VT_BSTR;
1052             V_BSTR(&url) = SysAllocString( lpszName );
1053
1054             hRes = IWebBrowser2_Navigate2( browser, &url, NULL, NULL, NULL, NULL );
1055             if ( FAILED( hRes ) )
1056                 WARN( "IWebBrowser2::Navigate2 failed: %08x\n", hRes );
1057             SysFreeString( V_BSTR(&url) );
1058
1059             IWebBrowser2_Release( browser );
1060         }
1061     }
1062
1063     if (ppUnkContainer)
1064     {
1065         *ppUnkContainer = pContainer;
1066         if ( pContainer )
1067             IUnknown_AddRef( pContainer );
1068     }
1069     if (ppUnkControl)
1070     {
1071         *ppUnkControl = pUnkControl;
1072         if ( pUnkControl )
1073             IUnknown_AddRef( pUnkControl );
1074     }
1075
1076     if ( pUnkControl )
1077         IUnknown_Release( pUnkControl );
1078     if ( pContainer )
1079         IUnknown_Release( pContainer );
1080
1081     return S_OK;
1082 }
1083
1084 /***********************************************************************
1085  *           AtlAxAttachControl           [atl100.@]
1086  */
1087 HRESULT WINAPI AtlAxAttachControl(IUnknown* pControl, HWND hWnd, IUnknown** ppUnkContainer)
1088 {
1089     IOCS *pUnkContainer;
1090     HRESULT hr;
1091
1092     TRACE( "%p %p %p\n", pControl, hWnd, ppUnkContainer );
1093
1094     if (!pControl)
1095         return E_INVALIDARG;
1096
1097     hr = IOCS_Create( hWnd, pControl, &pUnkContainer );
1098     if ( SUCCEEDED( hr ) && ppUnkContainer)
1099     {
1100         *ppUnkContainer = (IUnknown*) pUnkContainer;
1101     }
1102
1103     if(!hWnd)
1104         return S_FALSE;
1105
1106     return hr;
1107 }
1108
1109 /**********************************************************************
1110  * Helper function for AX_ConvertDialogTemplate
1111  */
1112 static inline BOOL advance_array(WORD **pptr, DWORD *palloc, DWORD *pfilled, const WORD *data, DWORD size)
1113 {
1114     if ( (*pfilled + size) > *palloc )
1115     {
1116         *palloc = ((*pfilled+size) + 0xFF) & ~0xFF;
1117         *pptr = HeapReAlloc( GetProcessHeap(), 0, *pptr, *palloc * sizeof(WORD) );
1118         if (!*pptr)
1119             return FALSE;
1120     }
1121     RtlMoveMemory( *pptr+*pfilled, data, size * sizeof(WORD) );
1122     *pfilled += size;
1123     return TRUE;
1124 }
1125
1126 /**********************************************************************
1127  * Convert ActiveX control templates to AtlAxWin class instances
1128  */
1129 static LPDLGTEMPLATEW AX_ConvertDialogTemplate(LPCDLGTEMPLATEW src_tmpl)
1130 {
1131 #define GET_WORD(x)  (*(const  WORD *)(x))
1132 #define GET_DWORD(x) (*(const DWORD *)(x))
1133 #define PUT_BLOCK(x,y) do {if (!advance_array(&output, &allocated, &filled, (x), (y))) return NULL;} while (0)
1134 #define PUT_WORD(x)  do {WORD w = (x);PUT_BLOCK(&w, 1);} while(0)
1135 #define PUT_DWORD(x)  do {DWORD w = (x);PUT_BLOCK(&w, 2);} while(0)
1136     const WORD *tmp, *src = (const WORD *)src_tmpl;
1137     WORD *output;
1138     DWORD allocated, filled; /* in WORDs */
1139     BOOL ext;
1140     WORD signature, dlgver, rescount;
1141     DWORD style;
1142
1143     filled = 0; allocated = 256;
1144     output = HeapAlloc( GetProcessHeap(), 0, allocated * sizeof(WORD) );
1145     if (!output)
1146         return NULL;
1147
1148     /* header */
1149     tmp = src;
1150     signature = GET_WORD(src);
1151     dlgver = GET_WORD(src + 1);
1152     if (signature == 1 && dlgver == 0xFFFF)
1153     {
1154         ext = TRUE;
1155         src += 6;
1156         style = GET_DWORD(src);
1157         src += 2;
1158         rescount = GET_WORD(src++);
1159         src += 4;
1160         if ( GET_WORD(src) == 0xFFFF ) /* menu */
1161             src += 2;
1162         else
1163             src += strlenW(src) + 1;
1164         if ( GET_WORD(src) == 0xFFFF ) /* class */
1165             src += 2;
1166         else
1167             src += strlenW(src) + 1;
1168         src += strlenW(src) + 1; /* title */
1169         if ( style & (DS_SETFONT | DS_SHELLFONT) )
1170         {
1171             src += 3;
1172             src += strlenW(src) + 1;
1173         }
1174     } else {
1175         ext = FALSE;
1176         style = GET_DWORD(src);
1177         src += 4;
1178         rescount = GET_WORD(src++);
1179         src += 4;
1180         if ( GET_WORD(src) == 0xFFFF ) /* menu */
1181             src += 2;
1182         else
1183             src += strlenW(src) + 1;
1184         if ( GET_WORD(src) == 0xFFFF ) /* class */
1185             src += 2;
1186         else
1187             src += strlenW(src) + 1;
1188         src += strlenW(src) + 1; /* title */
1189         if ( style & DS_SETFONT )
1190         {
1191             src++;
1192             src += strlenW(src) + 1;
1193         }
1194     }
1195     PUT_BLOCK(tmp, src-tmp);
1196
1197     while(rescount--)
1198     {
1199         src = (const WORD *)( ( ((ULONG_PTR)src) + 3) & ~3); /* align on DWORD boundary */
1200         filled = (filled + 1) & ~1; /* depends on DWORD-aligned allocation unit */
1201
1202         tmp = src;
1203         if (ext)
1204             src += 12;
1205         else
1206             src += 9;
1207         PUT_BLOCK(tmp, src-tmp);
1208
1209         tmp = src;
1210         if ( GET_WORD(src) == 0xFFFF ) /* class */
1211         {
1212             src += 2;
1213         } else
1214         {
1215             src += strlenW(src) + 1;
1216         }
1217         src += strlenW(src) + 1; /* title */
1218         if ( GET_WORD(tmp) == '{' ) /* all this mess created because of this line */
1219         {
1220             static const WCHAR AtlAxWin[] = {'A','t','l','A','x','W','i','n', 0};
1221             PUT_BLOCK(AtlAxWin, sizeof(AtlAxWin)/sizeof(WCHAR));
1222             PUT_BLOCK(tmp, strlenW(tmp)+1);
1223         } else
1224             PUT_BLOCK(tmp, src-tmp);
1225
1226         if ( GET_WORD(src) )
1227         {
1228             WORD size = (GET_WORD(src)+sizeof(WORD)-1) / sizeof(WORD); /* quite ugly :( Maybe use BYTE* instead of WORD* everywhere ? */
1229             PUT_BLOCK(src, size);
1230             src+=size;
1231         }
1232         else
1233         {
1234             PUT_WORD(0);
1235             src++;
1236         }
1237     }
1238     return (LPDLGTEMPLATEW) output;
1239 }
1240
1241 /***********************************************************************
1242  *           AtlAxCreateDialogA           [atl100.@]
1243  *
1244  * Creates a dialog window
1245  *
1246  * PARAMS
1247  *  hInst   [I] Application instance
1248  *  name    [I] Dialog box template name
1249  *  owner   [I] Dialog box parent HWND
1250  *  dlgProc [I] Dialog box procedure
1251  *  param   [I] This value will be passed to dlgProc as WM_INITDIALOG's message lParam
1252  *
1253  * RETURNS
1254  *  Window handle of dialog window.
1255  */
1256 HWND WINAPI AtlAxCreateDialogA(HINSTANCE hInst, LPCSTR name, HWND owner, DLGPROC dlgProc ,LPARAM param)
1257 {
1258     HWND res = NULL;
1259     int length;
1260     WCHAR *nameW;
1261
1262     if (IS_INTRESOURCE(name))
1263         return AtlAxCreateDialogW( hInst, (LPCWSTR) name, owner, dlgProc, param );
1264
1265     length = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1266     nameW = HeapAlloc( GetProcessHeap(), 0, length * sizeof(WCHAR) );
1267     if (nameW)
1268     {
1269         MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, length );
1270         res = AtlAxCreateDialogW( hInst, nameW, owner, dlgProc, param );
1271         HeapFree( GetProcessHeap(), 0, nameW );
1272     }
1273     return res;
1274 }
1275
1276 /***********************************************************************
1277  *           AtlAxCreateDialogW           [atl100.@]
1278  *
1279  * See AtlAxCreateDialogA
1280  *
1281  */
1282 HWND WINAPI AtlAxCreateDialogW(HINSTANCE hInst, LPCWSTR name, HWND owner, DLGPROC dlgProc ,LPARAM param)
1283 {
1284     HRSRC hrsrc;
1285     HGLOBAL hgl;
1286     LPCDLGTEMPLATEW ptr;
1287     LPDLGTEMPLATEW newptr;
1288     HWND res;
1289
1290     TRACE("(%p %s %p %p %lx)\n", hInst, debugstr_w(name), owner, dlgProc, param);
1291
1292     hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG );
1293     if ( !hrsrc )
1294         return NULL;
1295     hgl = LoadResource (hInst, hrsrc);
1296     if ( !hgl )
1297         return NULL;
1298     ptr = LockResource ( hgl );
1299     if (!ptr)
1300     {
1301         FreeResource( hgl );
1302         return NULL;
1303     }
1304     newptr = AX_ConvertDialogTemplate( ptr );
1305     if ( newptr )
1306     {
1307             res = CreateDialogIndirectParamW( hInst, newptr, owner, dlgProc, param );
1308             HeapFree( GetProcessHeap(), 0, newptr );
1309     } else
1310         res = NULL;
1311     FreeResource ( hrsrc );
1312     return res;
1313 }
1314
1315 /***********************************************************************
1316  *           AtlAxGetHost                 [atl100.@]
1317  *
1318  */
1319 HRESULT WINAPI AtlAxGetHost(HWND hWnd, IUnknown **pUnk)
1320 {
1321     IOCS *This;
1322
1323     TRACE( "(%p, %p)\n", hWnd, pUnk );
1324
1325     *pUnk = NULL;
1326
1327     This = (IOCS*) GetWindowLongPtrW( hWnd, GWLP_USERDATA );
1328     if ( !This )
1329     {
1330         WARN("No container attached to %p\n", hWnd );
1331         return E_FAIL;
1332     }
1333
1334     return IOCS_QueryInterface( This, &IID_IUnknown, (void**) pUnk );
1335 }
1336
1337 /***********************************************************************
1338  *           AtlAxGetControl              [atl100.@]
1339  *
1340  */
1341 HRESULT WINAPI AtlAxGetControl(HWND hWnd, IUnknown **pUnk)
1342 {
1343     IOCS *This;
1344
1345     TRACE( "(%p, %p)\n", hWnd, pUnk );
1346
1347     *pUnk = NULL;
1348
1349     This = (IOCS*) GetWindowLongPtrW( hWnd, GWLP_USERDATA );
1350     if ( !This || !This->control )
1351     {
1352         WARN("No control attached to %p\n", hWnd );
1353         return E_FAIL;
1354     }
1355
1356     return IOleObject_QueryInterface( This->control, &IID_IUnknown, (void**) pUnk );
1357 }
1358
1359 /***********************************************************************
1360  *           AtlAxDialogBoxW              [atl100.35]
1361  *
1362  */
1363 INT_PTR WINAPI AtlAxDialogBoxW(HINSTANCE hInstance, LPCWSTR lpTemplateName, HWND hWndParent, DLGPROC lpDialogProc,
1364         LPARAM dwInitParam)
1365 {
1366     FIXME("(%p %s %p %p %lx)\n", hInstance, debugstr_w(lpTemplateName), hWndParent, lpDialogProc, dwInitParam);
1367     return 0;
1368 }
1369
1370 /***********************************************************************
1371  *           AtlAxDialogBoxA              [atl100.36]
1372  *
1373  */
1374 INT_PTR WINAPI AtlAxDialogBoxA(HINSTANCE hInstance, LPCSTR lpTemplateName, HWND hWndParent, DLGPROC lpDialogProc,
1375         LPARAM dwInitParam)
1376 {
1377     FIXME("(%p %s %p %p %lx)\n", hInstance, debugstr_a(lpTemplateName), hWndParent, lpDialogProc, dwInitParam);
1378     return 0;
1379 }