riched20: Use ME_PointFromChar to calculate the caret position.
[wine] / dlls / ieframe / oleobject.c
1 /*
2  * Implementation of IOleObject interfaces for WebBrowser control
3  *
4  * - IOleObject
5  * - IOleInPlaceObject
6  * - IOleControl
7  *
8  * Copyright 2001 John R. Sheets (for CodeWeavers)
9  * Copyright 2005 Jacek Caban
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25
26 #include <string.h>
27
28 #include "ieframe.h"
29
30 #include "htiframe.h"
31 #include "idispids.h"
32 #include "mshtmdid.h"
33
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(ieframe);
37
38 /* shlwapi.dll */
39 HWND WINAPI SHSetParentHwnd(HWND hWnd, HWND hWndParent);
40
41 static ATOM shell_embedding_atom = 0;
42
43 static LRESULT resize_window(WebBrowser *This, LONG width, LONG height)
44 {
45     if(This->doc_host.hwnd)
46         SetWindowPos(This->doc_host.hwnd, NULL, 0, 0, width, height,
47                      SWP_NOZORDER | SWP_NOACTIVATE);
48
49     return 0;
50 }
51
52 static LRESULT WINAPI shell_embedding_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
53 {
54     WebBrowser *This;
55
56     static const WCHAR wszTHIS[] = {'T','H','I','S',0};
57
58     if(msg == WM_CREATE) {
59         This = *(WebBrowser**)lParam;
60         SetPropW(hwnd, wszTHIS, This);
61     }else {
62         This = GetPropW(hwnd, wszTHIS);
63     }
64
65     switch(msg) {
66     case WM_SIZE:
67         return resize_window(This, LOWORD(lParam), HIWORD(lParam));
68     case WM_DOCHOSTTASK:
69         return process_dochost_tasks(&This->doc_host);
70     }
71
72     return DefWindowProcW(hwnd, msg, wParam, lParam);
73 }
74
75 static void create_shell_embedding_hwnd(WebBrowser *This)
76 {
77     IOleInPlaceSite *inplace;
78     HWND parent = NULL;
79     HRESULT hres;
80
81     static const WCHAR wszShellEmbedding[] =
82         {'S','h','e','l','l',' ','E','m','b','e','d','d','i','n','g',0};
83
84     if(!shell_embedding_atom) {
85         static WNDCLASSEXW wndclass = {
86             sizeof(wndclass),
87             CS_DBLCLKS,
88             shell_embedding_proc,
89             0, 0 /* native uses 8 */, NULL, NULL, NULL,
90             (HBRUSH)(COLOR_WINDOW + 1), NULL,
91             wszShellEmbedding,
92             NULL
93         };
94         wndclass.hInstance = ieframe_instance;
95
96         RegisterClassExW(&wndclass);
97     }
98
99     hres = IOleClientSite_QueryInterface(This->client, &IID_IOleInPlaceSite, (void**)&inplace);
100     if(SUCCEEDED(hres)) {
101         IOleInPlaceSite_GetWindow(inplace, &parent);
102         IOleInPlaceSite_Release(inplace);
103     }
104
105     This->doc_host.frame_hwnd = This->shell_embedding_hwnd = CreateWindowExW(
106             WS_EX_WINDOWEDGE,
107             wszShellEmbedding, wszShellEmbedding,
108             WS_CLIPSIBLINGS | WS_CLIPCHILDREN
109             | (parent ? WS_CHILD | WS_TABSTOP : WS_POPUP | WS_MAXIMIZEBOX),
110             0, 0, 0, 0, parent,
111             NULL, ieframe_instance, This);
112
113     TRACE("parent=%p hwnd=%p\n", parent, This->shell_embedding_hwnd);
114 }
115
116 static HRESULT activate_inplace(WebBrowser *This, IOleClientSite *active_site)
117 {
118     HWND parent_hwnd;
119     HRESULT hres;
120
121     if(This->inplace)
122         return S_OK;
123
124     if(!active_site)
125         return E_INVALIDARG;
126
127     hres = IOleClientSite_QueryInterface(active_site, &IID_IOleInPlaceSite,
128                                          (void**)&This->inplace);
129     if(FAILED(hres)) {
130         WARN("Could not get IOleInPlaceSite\n");
131         return hres;
132     }
133
134     hres = IOleInPlaceSiteEx_CanInPlaceActivate(This->inplace);
135     if(hres != S_OK) {
136         WARN("CanInPlaceActivate returned: %08x\n", hres);
137         IOleInPlaceSiteEx_Release(This->inplace);
138         This->inplace = NULL;
139         return E_FAIL;
140     }
141
142     hres = IOleInPlaceSiteEx_GetWindow(This->inplace, &parent_hwnd);
143     if(SUCCEEDED(hres))
144         SHSetParentHwnd(This->shell_embedding_hwnd, parent_hwnd);
145
146     IOleInPlaceSiteEx_OnInPlaceActivate(This->inplace);
147
148     This->frameinfo.cb = sizeof(OLEINPLACEFRAMEINFO);
149     IOleInPlaceSiteEx_GetWindowContext(This->inplace, &This->doc_host.frame, &This->uiwindow,
150                                        &This->pos_rect, &This->clip_rect,
151                                        &This->frameinfo);
152
153     SetWindowPos(This->shell_embedding_hwnd, NULL,
154                  This->pos_rect.left, This->pos_rect.top,
155                  This->pos_rect.right-This->pos_rect.left,
156                  This->pos_rect.bottom-This->pos_rect.top,
157                  SWP_NOZORDER | SWP_SHOWWINDOW);
158
159     if(This->client) {
160         IOleContainer *container;
161
162         IOleClientSite_ShowObject(This->client);
163
164         hres = IOleClientSite_GetContainer(This->client, &container);
165         if(SUCCEEDED(hres)) {
166             if(This->container)
167                 IOleContainer_Release(This->container);
168             This->container = container;
169         }
170     }
171
172     if(This->doc_host.frame)
173         IOleInPlaceFrame_GetWindow(This->doc_host.frame, &This->frame_hwnd);
174
175     return S_OK;
176 }
177
178 static HRESULT activate_ui(WebBrowser *This, IOleClientSite *active_site)
179 {
180     HRESULT hres;
181
182     static const WCHAR wszitem[] = {'i','t','e','m',0};
183
184     if(This->inplace)
185     {
186         if(This->shell_embedding_hwnd)
187             ShowWindow(This->shell_embedding_hwnd, SW_SHOW);
188         return S_OK;
189     }
190
191     hres = activate_inplace(This, active_site);
192     if(FAILED(hres))
193         return hres;
194
195     IOleInPlaceSiteEx_OnUIActivate(This->inplace);
196
197     if(This->doc_host.frame)
198         IOleInPlaceFrame_SetActiveObject(This->doc_host.frame, &This->IOleInPlaceActiveObject_iface, wszitem);
199     if(This->uiwindow)
200         IOleInPlaceUIWindow_SetActiveObject(This->uiwindow, &This->IOleInPlaceActiveObject_iface, wszitem);
201
202     if(This->doc_host.frame)
203         IOleInPlaceFrame_SetMenu(This->doc_host.frame, NULL, NULL, This->shell_embedding_hwnd);
204
205     SetFocus(This->shell_embedding_hwnd);
206
207     return S_OK;
208 }
209
210 static HRESULT get_client_disp_property(IOleClientSite *client, DISPID dispid, VARIANT *res)
211 {
212     IDispatch *disp = NULL;
213     DISPPARAMS dispparams = {NULL, 0};
214     HRESULT hres;
215
216     VariantInit(res);
217
218     if(!client)
219         return S_OK;
220
221     hres = IOleClientSite_QueryInterface(client, &IID_IDispatch, (void**)&disp);
222     if(FAILED(hres)) {
223         TRACE("Could not get IDispatch\n");
224         return hres;
225     }
226
227     hres = IDispatch_Invoke(disp, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
228             DISPATCH_PROPERTYGET, &dispparams, res, NULL, NULL);
229
230     IDispatch_Release(disp);
231
232     return hres;
233 }
234
235 static HRESULT on_offlineconnected_change(WebBrowser *This)
236 {
237     VARIANT offline;
238
239     get_client_disp_property(This->client, DISPID_AMBIENT_OFFLINEIFNOTCONNECTED, &offline);
240
241     if(V_VT(&offline) == VT_BOOL)
242         IWebBrowser2_put_Offline(&This->IWebBrowser2_iface, V_BOOL(&offline));
243     else if(V_VT(&offline) != VT_EMPTY)
244         WARN("wrong V_VT(silent) %d\n", V_VT(&offline));
245
246     return S_OK;
247 }
248
249 static HRESULT on_silent_change(WebBrowser *This)
250 {
251     VARIANT silent;
252
253     get_client_disp_property(This->client, DISPID_AMBIENT_SILENT, &silent);
254
255     if(V_VT(&silent) == VT_BOOL)
256         IWebBrowser2_put_Silent(&This->IWebBrowser2_iface, V_BOOL(&silent));
257     else if(V_VT(&silent) != VT_EMPTY)
258         WARN("wrong V_VT(silent) %d\n", V_VT(&silent));
259
260     return S_OK;
261 }
262
263 static void release_client_site(WebBrowser *This)
264 {
265     release_dochost_client(&This->doc_host);
266
267     if(This->shell_embedding_hwnd) {
268         DestroyWindow(This->shell_embedding_hwnd);
269         This->shell_embedding_hwnd = NULL;
270     }
271
272     if(This->inplace) {
273         IOleInPlaceSiteEx_Release(This->inplace);
274         This->inplace = NULL;
275     }
276
277     if(This->container) {
278         IOleContainer_Release(This->container);
279         This->container = NULL;
280     }
281
282     if(This->uiwindow) {
283         IOleInPlaceUIWindow_Release(This->uiwindow);
284         This->uiwindow = NULL;
285     }
286
287     if(This->client) {
288         IOleClientSite_Release(This->client);
289         This->client = NULL;
290     }
291 }
292
293 typedef struct {
294     IEnumOLEVERB IEnumOLEVERB_iface;
295
296     LONG ref;
297 } EnumOLEVERB;
298
299 static inline EnumOLEVERB *impl_from_IEnumOLEVERB(IEnumOLEVERB *iface)
300 {
301     return CONTAINING_RECORD(iface, EnumOLEVERB, IEnumOLEVERB_iface);
302 }
303
304 static HRESULT WINAPI EnumOLEVERB_QueryInterface(IEnumOLEVERB *iface, REFIID riid, void **ppv)
305 {
306     EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
307
308     if(IsEqualGUID(&IID_IUnknown, riid)) {
309         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
310         *ppv = &This->IEnumOLEVERB_iface;
311     }else if(IsEqualGUID(&IID_IEnumOLEVERB, riid)) {
312         TRACE("(%p)->(IID_IEnumOLEVERB %p)\n", This, ppv);
313         *ppv = &This->IEnumOLEVERB_iface;
314     }else {
315         WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
316         *ppv = NULL;
317         return E_NOINTERFACE;
318     }
319
320     IUnknown_AddRef((IUnknown*)*ppv);
321     return S_OK;
322 }
323
324 static ULONG WINAPI EnumOLEVERB_AddRef(IEnumOLEVERB *iface)
325 {
326     EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
327     LONG ref = InterlockedIncrement(&This->ref);
328
329     TRACE("(%p) ref=%d\n", This, ref);
330
331     return ref;
332 }
333
334 static ULONG WINAPI EnumOLEVERB_Release(IEnumOLEVERB *iface)
335 {
336     EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
337     LONG ref = InterlockedDecrement(&This->ref);
338
339     TRACE("(%p) ref=%d\n", This, ref);
340
341     if(!ref)
342         heap_free(This);
343
344     return ref;
345 }
346
347 static HRESULT WINAPI EnumOLEVERB_Next(IEnumOLEVERB *iface, ULONG celt, OLEVERB *rgelt, ULONG *pceltFetched)
348 {
349     EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
350
351     TRACE("(%p)->(%u %p %p)\n", This, celt, rgelt, pceltFetched);
352
353     if(pceltFetched)
354         *pceltFetched = 0;
355     return S_OK;
356 }
357
358 static HRESULT WINAPI EnumOLEVERB_Skip(IEnumOLEVERB *iface, ULONG celt)
359 {
360     EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
361     TRACE("(%p)->(%u)\n", This, celt);
362     return S_OK;
363 }
364
365 static HRESULT WINAPI EnumOLEVERB_Reset(IEnumOLEVERB *iface)
366 {
367     EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
368     TRACE("(%p)\n", This);
369     return S_OK;
370 }
371
372 static HRESULT WINAPI EnumOLEVERB_Clone(IEnumOLEVERB *iface, IEnumOLEVERB **ppenum)
373 {
374     EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
375     FIXME("(%p)->(%p)\n", This, ppenum);
376     return E_NOTIMPL;
377 }
378
379 static const IEnumOLEVERBVtbl EnumOLEVERBVtbl = {
380     EnumOLEVERB_QueryInterface,
381     EnumOLEVERB_AddRef,
382     EnumOLEVERB_Release,
383     EnumOLEVERB_Next,
384     EnumOLEVERB_Skip,
385     EnumOLEVERB_Reset,
386     EnumOLEVERB_Clone
387 };
388
389 /**********************************************************************
390  * Implement the IOleObject interface for the WebBrowser control
391  */
392
393 static inline WebBrowser *impl_from_IOleObject(IOleObject *iface)
394 {
395     return CONTAINING_RECORD(iface, WebBrowser, IOleObject_iface);
396 }
397
398 static HRESULT WINAPI OleObject_QueryInterface(IOleObject *iface, REFIID riid, void **ppv)
399 {
400     WebBrowser *This = impl_from_IOleObject(iface);
401     return IWebBrowser2_QueryInterface(&This->IWebBrowser2_iface, riid, ppv);
402 }
403
404 static ULONG WINAPI OleObject_AddRef(IOleObject *iface)
405 {
406     WebBrowser *This = impl_from_IOleObject(iface);
407     return IWebBrowser2_AddRef(&This->IWebBrowser2_iface);
408 }
409
410 static ULONG WINAPI OleObject_Release(IOleObject *iface)
411 {
412     WebBrowser *This = impl_from_IOleObject(iface);
413     return IWebBrowser2_Release(&This->IWebBrowser2_iface);
414 }
415
416 static HRESULT WINAPI OleObject_SetClientSite(IOleObject *iface, LPOLECLIENTSITE pClientSite)
417 {
418     WebBrowser *This = impl_from_IOleObject(iface);
419     IDocHostUIHandler *hostui;
420     IOleContainer *container;
421     IDispatch *disp;
422     HRESULT hres;
423
424     TRACE("(%p)->(%p)\n", This, pClientSite);
425
426     if(This->client == pClientSite)
427         return S_OK;
428
429     release_client_site(This);
430
431     if(!pClientSite) {
432         if(This->doc_host.document)
433             deactivate_document(&This->doc_host);
434         return S_OK;
435     }
436
437     IOleClientSite_AddRef(pClientSite);
438     This->client = pClientSite;
439
440     hres = IOleClientSite_QueryInterface(This->client, &IID_IDispatch,
441             (void**)&disp);
442     if(SUCCEEDED(hres))
443         This->doc_host.client_disp = disp;
444
445     hres = IOleClientSite_QueryInterface(This->client, &IID_IDocHostUIHandler,
446             (void**)&hostui);
447     if(SUCCEEDED(hres))
448         This->doc_host.hostui = hostui;
449
450     hres = IOleClientSite_GetContainer(This->client, &container);
451     if(SUCCEEDED(hres)) {
452         ITargetContainer *target_container;
453
454         hres = IOleContainer_QueryInterface(container, &IID_ITargetContainer,
455                                             (void**)&target_container);
456         if(SUCCEEDED(hres)) {
457             FIXME("Unsupported ITargetContainer\n");
458             ITargetContainer_Release(target_container);
459         }
460
461         IOleContainer_Release(container);
462     }
463
464     create_shell_embedding_hwnd(This);
465
466     on_offlineconnected_change(This);
467     on_silent_change(This);
468
469     return S_OK;
470 }
471
472 static HRESULT WINAPI OleObject_GetClientSite(IOleObject *iface, LPOLECLIENTSITE *ppClientSite)
473 {
474     WebBrowser *This = impl_from_IOleObject(iface);
475
476     TRACE("(%p)->(%p)\n", This, ppClientSite);
477
478     if(!ppClientSite)
479         return E_INVALIDARG;
480
481     if(This->client)
482         IOleClientSite_AddRef(This->client);
483     *ppClientSite = This->client;
484
485     return S_OK;
486 }
487
488 static HRESULT WINAPI OleObject_SetHostNames(IOleObject *iface, LPCOLESTR szContainerApp,
489         LPCOLESTR szContainerObj)
490 {
491     WebBrowser *This = impl_from_IOleObject(iface);
492
493     TRACE("(%p)->(%s, %s)\n", This, debugstr_w(szContainerApp), debugstr_w(szContainerObj));
494
495     /* We have nothing to do here. */
496     return S_OK;
497 }
498
499 static HRESULT WINAPI OleObject_Close(IOleObject *iface, DWORD dwSaveOption)
500 {
501     WebBrowser *This = impl_from_IOleObject(iface);
502
503     TRACE("(%p)->(%d)\n", This, dwSaveOption);
504
505     if(dwSaveOption != OLECLOSE_NOSAVE) {
506         FIXME("unimplemented flag: %x\n", dwSaveOption);
507         return E_NOTIMPL;
508     }
509
510     if(This->doc_host.frame)
511         IOleInPlaceFrame_SetActiveObject(This->doc_host.frame, NULL, NULL);
512
513     if(This->uiwindow)
514         IOleInPlaceUIWindow_SetActiveObject(This->uiwindow, NULL, NULL);
515
516     if(This->inplace) {
517         IOleInPlaceSiteEx_OnUIDeactivate(This->inplace, FALSE);
518         IOleInPlaceSiteEx_OnInPlaceDeactivate(This->inplace);
519     }
520
521     return IOleObject_SetClientSite(iface, NULL);
522 }
523
524 static HRESULT WINAPI OleObject_SetMoniker(IOleObject *iface, DWORD dwWhichMoniker, IMoniker* pmk)
525 {
526     WebBrowser *This = impl_from_IOleObject(iface);
527     FIXME("(%p)->(%d, %p)\n", This, dwWhichMoniker, pmk);
528     return E_NOTIMPL;
529 }
530
531 static HRESULT WINAPI OleObject_GetMoniker(IOleObject *iface, DWORD dwAssign,
532         DWORD dwWhichMoniker, LPMONIKER *ppmk)
533 {
534     WebBrowser *This = impl_from_IOleObject(iface);
535     FIXME("(%p)->(%d, %d, %p)\n", This, dwAssign, dwWhichMoniker, ppmk);
536     return E_NOTIMPL;
537 }
538
539 static HRESULT WINAPI OleObject_InitFromData(IOleObject *iface, LPDATAOBJECT pDataObject,
540         BOOL fCreation, DWORD dwReserved)
541 {
542     WebBrowser *This = impl_from_IOleObject(iface);
543     FIXME("(%p)->(%p, %d, %d)\n", This, pDataObject, fCreation, dwReserved);
544     return E_NOTIMPL;
545 }
546
547 static HRESULT WINAPI OleObject_GetClipboardData(IOleObject *iface, DWORD dwReserved,
548         LPDATAOBJECT *ppDataObject)
549 {
550     WebBrowser *This = impl_from_IOleObject(iface);
551     FIXME("(%p)->(%d, %p)\n", This, dwReserved, ppDataObject);
552     return E_NOTIMPL;
553 }
554
555 static HRESULT WINAPI OleObject_DoVerb(IOleObject *iface, LONG iVerb, struct tagMSG* lpmsg,
556         LPOLECLIENTSITE pActiveSite, LONG lindex, HWND hwndParent, LPCRECT lprcPosRect)
557 {
558     WebBrowser *This = impl_from_IOleObject(iface);
559
560     TRACE("(%p)->(%d %p %p %d %p %s)\n", This, iVerb, lpmsg, pActiveSite, lindex, hwndParent,
561           wine_dbgstr_rect(lprcPosRect));
562
563     switch (iVerb)
564     {
565     case OLEIVERB_SHOW:
566         TRACE("OLEIVERB_SHOW\n");
567         return activate_ui(This, pActiveSite);
568     case OLEIVERB_UIACTIVATE:
569         TRACE("OLEIVERB_UIACTIVATE\n");
570         return activate_ui(This, pActiveSite);
571     case OLEIVERB_INPLACEACTIVATE:
572         TRACE("OLEIVERB_INPLACEACTIVATE\n");
573         return activate_inplace(This, pActiveSite);
574     case OLEIVERB_HIDE:
575         TRACE("OLEIVERB_HIDE\n");
576         if(This->inplace)
577             IOleInPlaceSiteEx_OnInPlaceDeactivate(This->inplace);
578         if(This->shell_embedding_hwnd)
579             ShowWindow(This->shell_embedding_hwnd, SW_HIDE);
580         return S_OK;
581     default:
582         FIXME("stub for %d\n", iVerb);
583         break;
584     }
585
586     return E_NOTIMPL;
587 }
588
589 static HRESULT WINAPI OleObject_EnumVerbs(IOleObject *iface, IEnumOLEVERB **ppEnumOleVerb)
590 {
591     WebBrowser *This = impl_from_IOleObject(iface);
592     EnumOLEVERB *ret;
593
594     TRACE("(%p)->(%p)\n", This, ppEnumOleVerb);
595
596     ret = heap_alloc(sizeof(*ret));
597     if(!ret)
598         return E_OUTOFMEMORY;
599
600     ret->IEnumOLEVERB_iface.lpVtbl = &EnumOLEVERBVtbl;
601     ret->ref = 1;
602
603     *ppEnumOleVerb = &ret->IEnumOLEVERB_iface;
604     return S_OK;
605 }
606
607 static HRESULT WINAPI OleObject_Update(IOleObject *iface)
608 {
609     WebBrowser *This = impl_from_IOleObject(iface);
610     FIXME("(%p)\n", This);
611     return E_NOTIMPL;
612 }
613
614 static HRESULT WINAPI OleObject_IsUpToDate(IOleObject *iface)
615 {
616     WebBrowser *This = impl_from_IOleObject(iface);
617     FIXME("(%p)\n", This);
618     return E_NOTIMPL;
619 }
620
621 static HRESULT WINAPI OleObject_GetUserClassID(IOleObject *iface, CLSID* pClsid)
622 {
623     WebBrowser *This = impl_from_IOleObject(iface);
624     FIXME("(%p)->(%p)\n", This, pClsid);
625     return E_NOTIMPL;
626 }
627
628 static HRESULT WINAPI OleObject_GetUserType(IOleObject *iface, DWORD dwFormOfType,
629         LPOLESTR* pszUserType)
630 {
631     WebBrowser *This = impl_from_IOleObject(iface);
632     TRACE("(%p, %d, %p)\n", This, dwFormOfType, pszUserType);
633     return OleRegGetUserType(&CLSID_WebBrowser, dwFormOfType, pszUserType);
634 }
635
636 static HRESULT WINAPI OleObject_SetExtent(IOleObject *iface, DWORD dwDrawAspect, SIZEL *psizel)
637 {
638     WebBrowser *This = impl_from_IOleObject(iface);
639
640     TRACE("(%p)->(%x %p)\n", This, dwDrawAspect, psizel);
641
642     /* Tests show that dwDrawAspect is ignored */
643     This->extent = *psizel;
644     return S_OK;
645 }
646
647 static HRESULT WINAPI OleObject_GetExtent(IOleObject *iface, DWORD dwDrawAspect, SIZEL *psizel)
648 {
649     WebBrowser *This = impl_from_IOleObject(iface);
650
651     TRACE("(%p)->(%x, %p)\n", This, dwDrawAspect, psizel);
652
653     /* Tests show that dwDrawAspect is ignored */
654     *psizel = This->extent;
655     return S_OK;
656 }
657
658 static HRESULT WINAPI OleObject_Advise(IOleObject *iface, IAdviseSink *pAdvSink,
659         DWORD* pdwConnection)
660 {
661     WebBrowser *This = impl_from_IOleObject(iface);
662     FIXME("(%p)->(%p, %p)\n", This, pAdvSink, pdwConnection);
663     return E_NOTIMPL;
664 }
665
666 static HRESULT WINAPI OleObject_Unadvise(IOleObject *iface, DWORD dwConnection)
667 {
668     WebBrowser *This = impl_from_IOleObject(iface);
669     FIXME("(%p)->(%d)\n", This, dwConnection);
670     return E_NOTIMPL;
671 }
672
673 static HRESULT WINAPI OleObject_EnumAdvise(IOleObject *iface, IEnumSTATDATA **ppenumAdvise)
674 {
675     WebBrowser *This = impl_from_IOleObject(iface);
676     FIXME("(%p)->(%p)\n", This, ppenumAdvise);
677     return S_OK;
678 }
679
680 static HRESULT WINAPI OleObject_GetMiscStatus(IOleObject *iface, DWORD dwAspect, DWORD *pdwStatus)
681 {
682     WebBrowser *This = impl_from_IOleObject(iface);
683
684     TRACE("(%p)->(%x, %p)\n", This, dwAspect, pdwStatus);
685
686     *pdwStatus = OLEMISC_SETCLIENTSITEFIRST|OLEMISC_ACTIVATEWHENVISIBLE|OLEMISC_INSIDEOUT
687         |OLEMISC_CANTLINKINSIDE|OLEMISC_RECOMPOSEONRESIZE;
688
689     return S_OK;
690 }
691
692 static HRESULT WINAPI OleObject_SetColorScheme(IOleObject *iface, LOGPALETTE* pLogpal)
693 {
694     WebBrowser *This = impl_from_IOleObject(iface);
695     FIXME("(%p)->(%p)\n", This, pLogpal);
696     return E_NOTIMPL;
697 }
698
699 static const IOleObjectVtbl OleObjectVtbl =
700 {
701     OleObject_QueryInterface,
702     OleObject_AddRef,
703     OleObject_Release,
704     OleObject_SetClientSite,
705     OleObject_GetClientSite,
706     OleObject_SetHostNames,
707     OleObject_Close,
708     OleObject_SetMoniker,
709     OleObject_GetMoniker,
710     OleObject_InitFromData,
711     OleObject_GetClipboardData,
712     OleObject_DoVerb,
713     OleObject_EnumVerbs,
714     OleObject_Update,
715     OleObject_IsUpToDate,
716     OleObject_GetUserClassID,
717     OleObject_GetUserType,
718     OleObject_SetExtent,
719     OleObject_GetExtent,
720     OleObject_Advise,
721     OleObject_Unadvise,
722     OleObject_EnumAdvise,
723     OleObject_GetMiscStatus,
724     OleObject_SetColorScheme
725 };
726
727 /**********************************************************************
728  * Implement the IOleInPlaceObject interface
729  */
730
731 static inline WebBrowser *impl_from_IOleInPlaceObject(IOleInPlaceObject *iface)
732 {
733     return CONTAINING_RECORD(iface, WebBrowser, IOleInPlaceObject_iface);
734 }
735
736 static HRESULT WINAPI OleInPlaceObject_QueryInterface(IOleInPlaceObject *iface,
737         REFIID riid, LPVOID *ppobj)
738 {
739     WebBrowser *This = impl_from_IOleInPlaceObject(iface);
740     return IWebBrowser2_QueryInterface(&This->IWebBrowser2_iface, riid, ppobj);
741 }
742
743 static ULONG WINAPI OleInPlaceObject_AddRef(IOleInPlaceObject *iface)
744 {
745     WebBrowser *This = impl_from_IOleInPlaceObject(iface);
746     return IWebBrowser2_AddRef(&This->IWebBrowser2_iface);
747 }
748
749 static ULONG WINAPI OleInPlaceObject_Release(IOleInPlaceObject *iface)
750 {
751     WebBrowser *This = impl_from_IOleInPlaceObject(iface);
752     return IWebBrowser2_Release(&This->IWebBrowser2_iface);
753 }
754
755 static HRESULT WINAPI OleInPlaceObject_GetWindow(IOleInPlaceObject *iface, HWND* phwnd)
756 {
757     WebBrowser *This = impl_from_IOleInPlaceObject(iface);
758
759     TRACE("(%p)->(%p)\n", This, phwnd);
760
761     *phwnd = This->shell_embedding_hwnd;
762     return S_OK;
763 }
764
765 static HRESULT WINAPI OleInPlaceObject_ContextSensitiveHelp(IOleInPlaceObject *iface,
766         BOOL fEnterMode)
767 {
768     WebBrowser *This = impl_from_IOleInPlaceObject(iface);
769     FIXME("(%p)->(%x)\n", This, fEnterMode);
770     return E_NOTIMPL;
771 }
772
773 static HRESULT WINAPI OleInPlaceObject_InPlaceDeactivate(IOleInPlaceObject *iface)
774 {
775     WebBrowser *This = impl_from_IOleInPlaceObject(iface);
776     FIXME("(%p)\n", This);
777
778     if(This->inplace) {
779         IOleInPlaceSiteEx_Release(This->inplace);
780         This->inplace = NULL;
781     }
782
783     return S_OK;
784 }
785
786 static HRESULT WINAPI OleInPlaceObject_UIDeactivate(IOleInPlaceObject *iface)
787 {
788     WebBrowser *This = impl_from_IOleInPlaceObject(iface);
789     FIXME("(%p)\n", This);
790     return E_NOTIMPL;
791 }
792
793 static HRESULT WINAPI OleInPlaceObject_SetObjectRects(IOleInPlaceObject *iface,
794         LPCRECT lprcPosRect, LPCRECT lprcClipRect)
795 {
796     WebBrowser *This = impl_from_IOleInPlaceObject(iface);
797
798     TRACE("(%p)->(%p %p)\n", This, lprcPosRect, lprcClipRect);
799
800     This->pos_rect = *lprcPosRect;
801
802     if(lprcClipRect)
803         This->clip_rect = *lprcClipRect;
804
805     if(This->shell_embedding_hwnd) {
806         SetWindowPos(This->shell_embedding_hwnd, NULL,
807                      lprcPosRect->left, lprcPosRect->top,
808                      lprcPosRect->right-lprcPosRect->left,
809                      lprcPosRect->bottom-lprcPosRect->top,
810                      SWP_NOZORDER | SWP_NOACTIVATE);
811     }
812
813     return S_OK;
814 }
815
816 static HRESULT WINAPI OleInPlaceObject_ReactivateAndUndo(IOleInPlaceObject *iface)
817 {
818     WebBrowser *This = impl_from_IOleInPlaceObject(iface);
819     FIXME("(%p)\n", This);
820     return E_NOTIMPL;
821 }
822
823 static const IOleInPlaceObjectVtbl OleInPlaceObjectVtbl =
824 {
825     OleInPlaceObject_QueryInterface,
826     OleInPlaceObject_AddRef,
827     OleInPlaceObject_Release,
828     OleInPlaceObject_GetWindow,
829     OleInPlaceObject_ContextSensitiveHelp,
830     OleInPlaceObject_InPlaceDeactivate,
831     OleInPlaceObject_UIDeactivate,
832     OleInPlaceObject_SetObjectRects,
833     OleInPlaceObject_ReactivateAndUndo
834 };
835
836 /**********************************************************************
837  * Implement the IOleControl interface
838  */
839
840 static inline WebBrowser *impl_from_IOleControl(IOleControl *iface)
841 {
842     return CONTAINING_RECORD(iface, WebBrowser, IOleControl_iface);
843 }
844
845 static HRESULT WINAPI OleControl_QueryInterface(IOleControl *iface,
846         REFIID riid, LPVOID *ppobj)
847 {
848     WebBrowser *This = impl_from_IOleControl(iface);
849     return IWebBrowser2_QueryInterface(&This->IWebBrowser2_iface, riid, ppobj);
850 }
851
852 static ULONG WINAPI OleControl_AddRef(IOleControl *iface)
853 {
854     WebBrowser *This = impl_from_IOleControl(iface);
855     return IWebBrowser2_AddRef(&This->IWebBrowser2_iface);
856 }
857
858 static ULONG WINAPI OleControl_Release(IOleControl *iface)
859 {
860     WebBrowser *This = impl_from_IOleControl(iface);
861     return IWebBrowser2_Release(&This->IWebBrowser2_iface);
862 }
863
864 static HRESULT WINAPI OleControl_GetControlInfo(IOleControl *iface, LPCONTROLINFO pCI)
865 {
866     WebBrowser *This = impl_from_IOleControl(iface);
867
868     TRACE("(%p)->(%p)\n", This, pCI);
869
870     /* Tests show that this function should be not implemented */
871     return E_NOTIMPL;
872 }
873
874 static HRESULT WINAPI OleControl_OnMnemonic(IOleControl *iface, struct tagMSG *pMsg)
875 {
876     WebBrowser *This = impl_from_IOleControl(iface);
877     FIXME("(%p)->(%p)\n", This, pMsg);
878     return E_NOTIMPL;
879 }
880
881 static HRESULT WINAPI OleControl_OnAmbientPropertyChange(IOleControl *iface, DISPID dispID)
882 {
883     WebBrowser *This = impl_from_IOleControl(iface);
884
885     TRACE("(%p)->(%d)\n", This, dispID);
886
887     switch(dispID) {
888     case DISPID_UNKNOWN:
889         /* Unknown means multiple properties changed, so check them all.
890          * BUT the Webbrowser OleControl object doesn't appear to do this.
891          */
892         return S_OK;
893     case DISPID_AMBIENT_DLCONTROL:
894         return S_OK;
895     case DISPID_AMBIENT_OFFLINEIFNOTCONNECTED:
896         return on_offlineconnected_change(This);
897     case DISPID_AMBIENT_SILENT:
898         return on_silent_change(This);
899     }
900
901     FIXME("Unknown dispID %d\n", dispID);
902     return E_NOTIMPL;
903 }
904
905 static HRESULT WINAPI OleControl_FreezeEvents(IOleControl *iface, BOOL bFreeze)
906 {
907     WebBrowser *This = impl_from_IOleControl(iface);
908     FIXME("(%p)->(%x)\n", This, bFreeze);
909     return E_NOTIMPL;
910 }
911
912 static const IOleControlVtbl OleControlVtbl =
913 {
914     OleControl_QueryInterface,
915     OleControl_AddRef,
916     OleControl_Release,
917     OleControl_GetControlInfo,
918     OleControl_OnMnemonic,
919     OleControl_OnAmbientPropertyChange,
920     OleControl_FreezeEvents
921 };
922
923 static inline WebBrowser *impl_from_IOleInPlaceActiveObject(IOleInPlaceActiveObject *iface)
924 {
925     return CONTAINING_RECORD(iface, WebBrowser, IOleInPlaceActiveObject_iface);
926 }
927
928 static HRESULT WINAPI InPlaceActiveObject_QueryInterface(IOleInPlaceActiveObject *iface,
929         REFIID riid, void **ppv)
930 {
931     WebBrowser *This = impl_from_IOleInPlaceActiveObject(iface);
932     return IWebBrowser2_QueryInterface(&This->IWebBrowser2_iface, riid, ppv);
933 }
934
935 static ULONG WINAPI InPlaceActiveObject_AddRef(IOleInPlaceActiveObject *iface)
936 {
937     WebBrowser *This = impl_from_IOleInPlaceActiveObject(iface);
938     return IWebBrowser2_AddRef(&This->IWebBrowser2_iface);
939 }
940
941 static ULONG WINAPI InPlaceActiveObject_Release(IOleInPlaceActiveObject *iface)
942 {
943     WebBrowser *This = impl_from_IOleInPlaceActiveObject(iface);
944     return IWebBrowser2_Release(&This->IWebBrowser2_iface);
945 }
946
947 static HRESULT WINAPI InPlaceActiveObject_GetWindow(IOleInPlaceActiveObject *iface,
948                                                     HWND *phwnd)
949 {
950     WebBrowser *This = impl_from_IOleInPlaceActiveObject(iface);
951     return IOleInPlaceObject_GetWindow(&This->IOleInPlaceObject_iface, phwnd);
952 }
953
954 static HRESULT WINAPI InPlaceActiveObject_ContextSensitiveHelp(IOleInPlaceActiveObject *iface,
955                                                                BOOL fEnterMode)
956 {
957     WebBrowser *This = impl_from_IOleInPlaceActiveObject(iface);
958     return IOleInPlaceObject_ContextSensitiveHelp(&This->IOleInPlaceObject_iface, fEnterMode);
959 }
960
961 static HRESULT WINAPI InPlaceActiveObject_TranslateAccelerator(IOleInPlaceActiveObject *iface,
962                                                                LPMSG lpmsg)
963 {
964     WebBrowser *This = impl_from_IOleInPlaceActiveObject(iface);
965     IOleInPlaceActiveObject *activeobj;
966     HRESULT hr = S_FALSE;
967
968     TRACE("(%p)->(%p)\n", This, lpmsg);
969
970     if(This->doc_host.document) {
971         if(SUCCEEDED(IUnknown_QueryInterface(This->doc_host.document,
972                                              &IID_IOleInPlaceActiveObject,
973                                              (void**)&activeobj))) {
974             hr = IOleInPlaceActiveObject_TranslateAccelerator(activeobj, lpmsg);
975             IOleInPlaceActiveObject_Release(activeobj);
976         }
977     }
978
979     if(SUCCEEDED(hr))
980         return hr;
981     else
982         return S_FALSE;
983 }
984
985 static HRESULT WINAPI InPlaceActiveObject_OnFrameWindowActivate(IOleInPlaceActiveObject *iface,
986                                                                 BOOL fActivate)
987 {
988     WebBrowser *This = impl_from_IOleInPlaceActiveObject(iface);
989     FIXME("(%p)->(%x)\n", This, fActivate);
990     return E_NOTIMPL;
991 }
992
993 static HRESULT WINAPI InPlaceActiveObject_OnDocWindowActivate(IOleInPlaceActiveObject *iface,
994                                                               BOOL fActivate)
995 {
996     WebBrowser *This = impl_from_IOleInPlaceActiveObject(iface);
997     FIXME("(%p)->(%x)\n", This, fActivate);
998     return E_NOTIMPL;
999 }
1000
1001 static HRESULT WINAPI InPlaceActiveObject_ResizeBorder(IOleInPlaceActiveObject *iface,
1002         LPCRECT lprcBorder, IOleInPlaceUIWindow *pUIWindow, BOOL fFrameWindow)
1003 {
1004     WebBrowser *This = impl_from_IOleInPlaceActiveObject(iface);
1005     FIXME("(%p)->(%p %p %x)\n", This, lprcBorder, pUIWindow, fFrameWindow);
1006     return E_NOTIMPL;
1007 }
1008
1009 static HRESULT WINAPI InPlaceActiveObject_EnableModeless(IOleInPlaceActiveObject *iface,
1010                                                          BOOL fEnable)
1011 {
1012     WebBrowser *This = impl_from_IOleInPlaceActiveObject(iface);
1013     FIXME("(%p)->(%x)\n", This, fEnable);
1014     return E_NOTIMPL;
1015 }
1016
1017 static const IOleInPlaceActiveObjectVtbl OleInPlaceActiveObjectVtbl = {
1018     InPlaceActiveObject_QueryInterface,
1019     InPlaceActiveObject_AddRef,
1020     InPlaceActiveObject_Release,
1021     InPlaceActiveObject_GetWindow,
1022     InPlaceActiveObject_ContextSensitiveHelp,
1023     InPlaceActiveObject_TranslateAccelerator,
1024     InPlaceActiveObject_OnFrameWindowActivate,
1025     InPlaceActiveObject_OnDocWindowActivate,
1026     InPlaceActiveObject_ResizeBorder,
1027     InPlaceActiveObject_EnableModeless
1028 };
1029
1030 static inline WebBrowser *impl_from_IOleCommandTarget(IOleCommandTarget *iface)
1031 {
1032     return CONTAINING_RECORD(iface, WebBrowser, IOleCommandTarget_iface);
1033 }
1034
1035 static HRESULT WINAPI WBOleCommandTarget_QueryInterface(IOleCommandTarget *iface,
1036         REFIID riid, void **ppv)
1037 {
1038     WebBrowser *This = impl_from_IOleCommandTarget(iface);
1039     return IWebBrowser2_QueryInterface(&This->IWebBrowser2_iface, riid, ppv);
1040 }
1041
1042 static ULONG WINAPI WBOleCommandTarget_AddRef(IOleCommandTarget *iface)
1043 {
1044     WebBrowser *This = impl_from_IOleCommandTarget(iface);
1045     return IWebBrowser2_AddRef(&This->IWebBrowser2_iface);
1046 }
1047
1048 static ULONG WINAPI WBOleCommandTarget_Release(IOleCommandTarget *iface)
1049 {
1050     WebBrowser *This = impl_from_IOleCommandTarget(iface);
1051     return IWebBrowser2_Release(&This->IWebBrowser2_iface);
1052 }
1053
1054 static HRESULT WINAPI WBOleCommandTarget_QueryStatus(IOleCommandTarget *iface,
1055         const GUID *pguidCmdGroup, ULONG cCmds, OLECMD prgCmds[], OLECMDTEXT *pCmdText)
1056 {
1057     WebBrowser *This = impl_from_IOleCommandTarget(iface);
1058     IOleCommandTarget *cmdtrg;
1059     HRESULT hres;
1060
1061     TRACE("(%p)->(%s %u %p %p)\n", This, debugstr_guid(pguidCmdGroup), cCmds, prgCmds,
1062           pCmdText);
1063
1064     if(!This->doc_host.document)
1065         return 0x80040104;
1066
1067     /* NOTE: There are probably some commands that we should handle here
1068      * instead of forwarding to document object. */
1069
1070     hres = IUnknown_QueryInterface(This->doc_host.document, &IID_IOleCommandTarget, (void**)&cmdtrg);
1071     if(FAILED(hres))
1072         return hres;
1073
1074     hres = IOleCommandTarget_QueryStatus(cmdtrg, pguidCmdGroup, cCmds, prgCmds, pCmdText);
1075     IOleCommandTarget_Release(cmdtrg);
1076
1077     return hres;
1078 }
1079
1080 static HRESULT WINAPI WBOleCommandTarget_Exec(IOleCommandTarget *iface,
1081         const GUID *pguidCmdGroup, DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn,
1082         VARIANT *pvaOut)
1083 {
1084     WebBrowser *This = impl_from_IOleCommandTarget(iface);
1085     FIXME("(%p)->(%s %d %d %p %p)\n", This, debugstr_guid(pguidCmdGroup), nCmdID,
1086           nCmdexecopt, pvaIn, pvaOut);
1087     return E_NOTIMPL;
1088 }
1089
1090 static const IOleCommandTargetVtbl OleCommandTargetVtbl = {
1091     WBOleCommandTarget_QueryInterface,
1092     WBOleCommandTarget_AddRef,
1093     WBOleCommandTarget_Release,
1094     WBOleCommandTarget_QueryStatus,
1095     WBOleCommandTarget_Exec
1096 };
1097
1098 void WebBrowser_OleObject_Init(WebBrowser *This)
1099 {
1100     DWORD dpi_x;
1101     DWORD dpi_y;
1102     HDC hdc;
1103
1104     /* default aspect ratio is 96dpi / 96dpi */
1105     hdc = GetDC(0);
1106     dpi_x = GetDeviceCaps(hdc, LOGPIXELSX);
1107     dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
1108     ReleaseDC(0, hdc);
1109
1110     This->IOleObject_iface.lpVtbl              = &OleObjectVtbl;
1111     This->IOleInPlaceObject_iface.lpVtbl       = &OleInPlaceObjectVtbl;
1112     This->IOleControl_iface.lpVtbl             = &OleControlVtbl;
1113     This->IOleInPlaceActiveObject_iface.lpVtbl = &OleInPlaceActiveObjectVtbl;
1114     This->IOleCommandTarget_iface.lpVtbl       = &OleCommandTargetVtbl;
1115
1116     /* Default size is 50x20 pixels, in himetric units */
1117     This->extent.cx = MulDiv( 50, 2540, dpi_x );
1118     This->extent.cy = MulDiv( 20, 2540, dpi_y );
1119 }
1120
1121 void WebBrowser_OleObject_Destroy(WebBrowser *This)
1122 {
1123     release_client_site(This);
1124 }