shdocvw: Get rid of CLIENTSITE_THIS.
[wine] / dlls / shdocvw / dochost.c
1 /*
2  * Copyright 2005-2006 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "wine/debug.h"
20 #include "shdocvw.h"
21 #include "hlink.h"
22 #include "exdispid.h"
23 #include "mshtml.h"
24 #include "initguid.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
27
28 DEFINE_OLEGUID(CGID_DocHostCmdPriv, 0x000214D4L, 0, 0);
29
30 #define DOCHOST_DOCCANNAVIGATE  0
31
32 static ATOM doc_view_atom = 0;
33
34 void push_dochost_task(DocHost *This, task_header_t *task, task_proc_t proc, BOOL send)
35 {
36     task->proc = proc;
37
38     /* FIXME: Don't use lParam */
39     if(send)
40         SendMessageW(This->frame_hwnd, WM_DOCHOSTTASK, 0, (LPARAM)task);
41     else
42         PostMessageW(This->frame_hwnd, WM_DOCHOSTTASK, 0, (LPARAM)task);
43 }
44
45 LRESULT process_dochost_task(DocHost *This, LPARAM lparam)
46 {
47     task_header_t *task = (task_header_t*)lparam;
48
49     task->proc(This, task);
50
51     heap_free(task);
52     return 0;
53 }
54
55 static void notif_complete(DocHost *This, DISPID dispid)
56 {
57     DISPPARAMS dispparams;
58     VARIANTARG params[2];
59     VARIANT url;
60
61     dispparams.cArgs = 2;
62     dispparams.cNamedArgs = 0;
63     dispparams.rgdispidNamedArgs = NULL;
64     dispparams.rgvarg = params;
65
66     V_VT(params) = (VT_BYREF|VT_VARIANT);
67     V_BYREF(params) = &url;
68
69     V_VT(params+1) = VT_DISPATCH;
70     V_DISPATCH(params+1) = This->disp;
71
72     V_VT(&url) = VT_BSTR;
73     V_BSTR(&url) = SysAllocString(This->url);
74
75     TRACE("%d >>>\n", dispid);
76     call_sink(This->cps.wbe2, dispid, &dispparams);
77     TRACE("%d <<<\n", dispid);
78
79     SysFreeString(V_BSTR(&url));
80     This->busy = VARIANT_FALSE;
81 }
82
83 static void object_available(DocHost *This)
84 {
85     IHlinkTarget *hlink;
86     HRESULT hres;
87
88     TRACE("(%p)\n", This);
89
90     if(!This->document) {
91         WARN("document == NULL\n");
92         return;
93     }
94
95     hres = IUnknown_QueryInterface(This->document, &IID_IHlinkTarget, (void**)&hlink);
96     if(FAILED(hres)) {
97         FIXME("Could not get IHlinkTarget interface\n");
98         return;
99     }
100
101     hres = IHlinkTarget_Navigate(hlink, 0, NULL);
102     IHlinkTarget_Release(hlink);
103     if(FAILED(hres))
104         FIXME("Navigate failed\n");
105 }
106
107 static HRESULT get_doc_ready_state(DocHost *This, READYSTATE *ret)
108 {
109     DISPPARAMS dp = {NULL,NULL,0,0};
110     IDispatch *disp;
111     EXCEPINFO ei;
112     VARIANT var;
113     HRESULT hres;
114
115     hres = IUnknown_QueryInterface(This->document, &IID_IDispatch, (void**)&disp);
116     if(FAILED(hres))
117         return hres;
118
119     hres = IDispatch_Invoke(disp, DISPID_READYSTATE, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET,
120             &dp, &var, &ei, NULL);
121     IDispatch_Release(disp);
122     if(FAILED(hres)) {
123         WARN("Invoke(DISPID_READYSTATE failed: %08x\n", hres);
124         return hres;
125     }
126
127     if(V_VT(&var) != VT_I4) {
128         WARN("V_VT(var) = %d\n", V_VT(&var));
129         VariantClear(&var);
130         return E_FAIL;
131     }
132
133     *ret = V_I4(&var);
134     return S_OK;
135 }
136
137 static void advise_prop_notif(DocHost *This, BOOL set)
138 {
139     IConnectionPointContainer *cp_container;
140     IConnectionPoint *cp;
141     HRESULT hres;
142
143     hres = IUnknown_QueryInterface(This->document, &IID_IConnectionPointContainer, (void**)&cp_container);
144     if(FAILED(hres))
145         return;
146
147     hres = IConnectionPointContainer_FindConnectionPoint(cp_container, &IID_IPropertyNotifySink, &cp);
148     IConnectionPointContainer_Release(cp_container);
149     if(FAILED(hres))
150         return;
151
152     if(set)
153         hres = IConnectionPoint_Advise(cp, (IUnknown*)PROPNOTIF(This), &This->prop_notif_cookie);
154     else
155         hres = IConnectionPoint_Unadvise(cp, This->prop_notif_cookie);
156     IConnectionPoint_Release(cp);
157
158     if(SUCCEEDED(hres))
159         This->is_prop_notif = set;
160 }
161
162 void set_doc_state(DocHost *This, READYSTATE doc_state)
163 {
164     This->doc_state = doc_state;
165     if(doc_state > This->ready_state)
166         This->ready_state = doc_state;
167 }
168
169 static void update_ready_state(DocHost *This, READYSTATE ready_state)
170 {
171     if(ready_state > READYSTATE_LOADING && This->doc_state <= READYSTATE_LOADING)
172         notif_complete(This, DISPID_NAVIGATECOMPLETE2);
173
174     if(ready_state == READYSTATE_COMPLETE && This->doc_state < READYSTATE_COMPLETE) {
175         set_doc_state(This, READYSTATE_COMPLETE);
176         notif_complete(This, DISPID_DOCUMENTCOMPLETE);
177     }else {
178         set_doc_state(This, ready_state);
179     }
180 }
181
182 typedef struct {
183     task_header_t header;
184     IUnknown *doc;
185     READYSTATE ready_state;
186 } ready_state_task_t;
187
188 static void ready_state_proc(DocHost *This, task_header_t *_task)
189 {
190     ready_state_task_t *task = (ready_state_task_t*)_task;
191
192     if(task->doc == This->document)
193         update_ready_state(This, task->ready_state);
194
195     IUnknown_Release(task->doc);
196 }
197
198 static void push_ready_state_task(DocHost *This, READYSTATE ready_state)
199 {
200     ready_state_task_t *task = heap_alloc(sizeof(ready_state_task_t));
201
202     IUnknown_AddRef(This->document);
203     task->doc = This->document;
204     task->ready_state = ready_state;
205
206     push_dochost_task(This, &task->header, ready_state_proc, FALSE);
207 }
208
209 static void object_available_proc(DocHost *This, task_header_t *task)
210 {
211     object_available(This);
212 }
213
214 HRESULT dochost_object_available(DocHost *This, IUnknown *doc)
215 {
216     READYSTATE ready_state;
217     task_header_t *task;
218     IOleObject *oleobj;
219     HRESULT hres;
220
221     IUnknown_AddRef(doc);
222     This->document = doc;
223
224     hres = IUnknown_QueryInterface(doc, &IID_IOleObject, (void**)&oleobj);
225     if(SUCCEEDED(hres)) {
226         CLSID clsid;
227
228         hres = IOleObject_GetUserClassID(oleobj, &clsid);
229         if(SUCCEEDED(hres))
230             TRACE("Got clsid %s\n",
231                   IsEqualGUID(&clsid, &CLSID_HTMLDocument) ? "CLSID_HTMLDocument" : debugstr_guid(&clsid));
232
233         hres = IOleObject_SetClientSite(oleobj, &This->IOleClientSite_iface);
234         if(FAILED(hres))
235             FIXME("SetClientSite failed: %08x\n", hres);
236
237         IOleObject_Release(oleobj);
238     }else {
239         FIXME("Could not get IOleObject iface: %08x\n", hres);
240     }
241
242     /* FIXME: Call SetAdvise */
243
244     task = heap_alloc(sizeof(*task));
245     push_dochost_task(This, task, object_available_proc, FALSE);
246
247     hres = get_doc_ready_state(This, &ready_state);
248     if(SUCCEEDED(hres)) {
249         if(ready_state == READYSTATE_COMPLETE)
250             push_ready_state_task(This, READYSTATE_COMPLETE);
251         if(ready_state != READYSTATE_COMPLETE || This->doc_navigate)
252             advise_prop_notif(This, TRUE);
253     }
254
255     return S_OK;
256 }
257
258 static LRESULT resize_document(DocHost *This, LONG width, LONG height)
259 {
260     RECT rect = {0, 0, width, height};
261
262     TRACE("(%p)->(%d %d)\n", This, width, height);
263
264     if(This->view)
265         IOleDocumentView_SetRect(This->view, &rect);
266
267     return 0;
268 }
269
270 static LRESULT WINAPI doc_view_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
271 {
272     DocHost *This;
273
274     static const WCHAR wszTHIS[] = {'T','H','I','S',0};
275
276     if(msg == WM_CREATE) {
277         This = *(DocHost**)lParam;
278         SetPropW(hwnd, wszTHIS, This);
279     }else {
280         This = GetPropW(hwnd, wszTHIS);
281     }
282
283     switch(msg) {
284     case WM_SIZE:
285         return resize_document(This, LOWORD(lParam), HIWORD(lParam));
286     }
287
288     return DefWindowProcW(hwnd, msg, wParam, lParam);
289 }
290
291 void create_doc_view_hwnd(DocHost *This)
292 {
293     RECT rect;
294
295     static const WCHAR wszShell_DocObject_View[] =
296         {'S','h','e','l','l',' ','D','o','c','O','b','j','e','c','t',' ','V','i','e','w',0};
297
298     if(!doc_view_atom) {
299         static WNDCLASSEXW wndclass = {
300             sizeof(wndclass),
301             CS_PARENTDC,
302             doc_view_proc,
303             0, 0 /* native uses 4*/, NULL, NULL, NULL,
304             (HBRUSH)(COLOR_WINDOW + 1), NULL,
305             wszShell_DocObject_View,
306             NULL
307         };
308
309         wndclass.hInstance = shdocvw_hinstance;
310
311         doc_view_atom = RegisterClassExW(&wndclass);
312     }
313
314     This->container_vtbl->GetDocObjRect(This, &rect);
315     This->hwnd = CreateWindowExW(0, wszShell_DocObject_View,
316          wszShell_DocObject_View,
317          WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_TABSTOP,
318          rect.left, rect.top, rect.right, rect.bottom, This->frame_hwnd,
319          NULL, shdocvw_hinstance, This);
320 }
321
322 void deactivate_document(DocHost *This)
323 {
324     IOleInPlaceObjectWindowless *winobj;
325     IOleObject *oleobj = NULL;
326     IHlinkTarget *hlink = NULL;
327     HRESULT hres;
328
329     if(This->doc_navigate) {
330         IUnknown_Release(This->doc_navigate);
331         This->doc_navigate = NULL;
332     }
333
334     if(This->is_prop_notif)
335         advise_prop_notif(This, FALSE);
336
337     if(This->view)
338         IOleDocumentView_UIActivate(This->view, FALSE);
339
340     hres = IUnknown_QueryInterface(This->document, &IID_IOleInPlaceObjectWindowless,
341                                    (void**)&winobj);
342     if(SUCCEEDED(hres)) {
343         IOleInPlaceObjectWindowless_InPlaceDeactivate(winobj);
344         IOleInPlaceObjectWindowless_Release(winobj);
345     }
346
347     if(This->view) {
348         IOleDocumentView_Show(This->view, FALSE);
349         IOleDocumentView_CloseView(This->view, 0);
350         IOleDocumentView_SetInPlaceSite(This->view, NULL);
351         IOleDocumentView_Release(This->view);
352         This->view = NULL;
353     }
354
355     hres = IUnknown_QueryInterface(This->document, &IID_IOleObject, (void**)&oleobj);
356     if(SUCCEEDED(hres))
357         IOleObject_Close(oleobj, OLECLOSE_NOSAVE);
358
359     hres = IUnknown_QueryInterface(This->document, &IID_IHlinkTarget, (void**)&hlink);
360     if(SUCCEEDED(hres)) {
361         IHlinkTarget_SetBrowseContext(hlink, NULL);
362         IHlinkTarget_Release(hlink);
363     }
364
365     if(oleobj) {
366         IOleClientSite *client_site = NULL;
367
368         IOleObject_GetClientSite(oleobj, &client_site);
369         if(client_site) {
370             if(client_site == &This->IOleClientSite_iface)
371                 IOleObject_SetClientSite(oleobj, NULL);
372             IOleClientSite_Release(client_site);
373         }
374
375         IOleObject_Release(oleobj);
376     }
377
378     IUnknown_Release(This->document);
379     This->document = NULL;
380 }
381
382 void release_dochost_client(DocHost *This)
383 {
384     if(This->hwnd) {
385         DestroyWindow(This->hwnd);
386         This->hwnd = NULL;
387     }
388
389     if(This->hostui) {
390         IDocHostUIHandler_Release(This->hostui);
391         This->hostui = NULL;
392     }
393
394     if(This->client_disp) {
395         IDispatch_Release(This->client_disp);
396         This->client_disp = NULL;
397     }
398
399     if(This->frame) {
400         IOleInPlaceFrame_Release(This->frame);
401         This->frame = NULL;
402     }
403 }
404
405 #define OLECMD_THIS(iface) DEFINE_THIS(DocHost, OleCommandTarget, iface)
406
407 static HRESULT WINAPI ClOleCommandTarget_QueryInterface(IOleCommandTarget *iface,
408         REFIID riid, void **ppv)
409 {
410     DocHost *This = OLECMD_THIS(iface);
411     return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
412 }
413
414 static ULONG WINAPI ClOleCommandTarget_AddRef(IOleCommandTarget *iface)
415 {
416     DocHost *This = OLECMD_THIS(iface);
417     return IOleClientSite_AddRef(&This->IOleClientSite_iface);
418 }
419
420 static ULONG WINAPI ClOleCommandTarget_Release(IOleCommandTarget *iface)
421 {
422     DocHost *This = OLECMD_THIS(iface);
423     return IOleClientSite_Release(&This->IOleClientSite_iface);
424 }
425
426 static HRESULT WINAPI ClOleCommandTarget_QueryStatus(IOleCommandTarget *iface,
427         const GUID *pguidCmdGroup, ULONG cCmds, OLECMD prgCmds[], OLECMDTEXT *pCmdText)
428 {
429     DocHost *This = OLECMD_THIS(iface);
430     ULONG i= 0;
431     FIXME("(%p)->(%s %u %p %p)\n", This, debugstr_guid(pguidCmdGroup), cCmds, prgCmds,
432           pCmdText);
433     while (prgCmds && (cCmds > i)) {
434         FIXME("command_%u: %u, 0x%x\n", i, prgCmds[i].cmdID, prgCmds[i].cmdf);
435         i++;
436     }
437     return E_NOTIMPL;
438 }
439
440 static HRESULT WINAPI ClOleCommandTarget_Exec(IOleCommandTarget *iface,
441         const GUID *pguidCmdGroup, DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn,
442         VARIANT *pvaOut)
443 {
444     DocHost *This = OLECMD_THIS(iface);
445
446     TRACE("(%p)->(%s %d %d %p %p)\n", This, debugstr_guid(pguidCmdGroup), nCmdID,
447           nCmdexecopt, debugstr_variant(pvaIn), debugstr_variant(pvaOut));
448
449     if(!pguidCmdGroup) {
450         switch(nCmdID) {
451         case OLECMDID_UPDATECOMMANDS:
452             return This->container_vtbl->exec(This, pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
453         default:
454             FIXME("Unimplemented cmdid %d\n", nCmdID);
455             return E_NOTIMPL;
456         }
457         return S_OK;
458     }
459
460     if(IsEqualGUID(pguidCmdGroup, &CGID_DocHostCmdPriv)) {
461         switch(nCmdID) {
462         case DOCHOST_DOCCANNAVIGATE:
463             if(!pvaIn || V_VT(pvaIn) != VT_UNKNOWN)
464                 return E_INVALIDARG;
465
466             if(This->doc_navigate)
467                 IUnknown_Release(This->doc_navigate);
468             IUnknown_AddRef(V_UNKNOWN(pvaIn));
469             This->doc_navigate = V_UNKNOWN(pvaIn);
470             return S_OK;
471
472         default:
473             FIXME("unsupported command %d of CGID_DocHostCmdPriv\n", nCmdID);
474             return E_NOTIMPL;
475         }
476     }
477
478     FIXME("Unimplemented group %s\n", debugstr_guid(pguidCmdGroup));
479     return E_NOTIMPL;
480 }
481
482 #undef OLECMD_THIS
483
484 static const IOleCommandTargetVtbl OleCommandTargetVtbl = {
485     ClOleCommandTarget_QueryInterface,
486     ClOleCommandTarget_AddRef,
487     ClOleCommandTarget_Release,
488     ClOleCommandTarget_QueryStatus,
489     ClOleCommandTarget_Exec
490 };
491
492 #define DOCHOSTUI_THIS(iface) DEFINE_THIS(DocHost, DocHostUIHandler, iface)
493
494 static HRESULT WINAPI DocHostUIHandler_QueryInterface(IDocHostUIHandler2 *iface,
495                                                       REFIID riid, void **ppv)
496 {
497     DocHost *This = DOCHOSTUI_THIS(iface);
498     return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
499 }
500
501 static ULONG WINAPI DocHostUIHandler_AddRef(IDocHostUIHandler2 *iface)
502 {
503     DocHost *This = DOCHOSTUI_THIS(iface);
504     return IOleClientSite_AddRef(&This->IOleClientSite_iface);
505 }
506
507 static ULONG WINAPI DocHostUIHandler_Release(IDocHostUIHandler2 *iface)
508 {
509     DocHost *This = DOCHOSTUI_THIS(iface);
510     return IOleClientSite_Release(&This->IOleClientSite_iface);
511 }
512
513 static HRESULT WINAPI DocHostUIHandler_ShowContextMenu(IDocHostUIHandler2 *iface,
514          DWORD dwID, POINT *ppt, IUnknown *pcmdtReserved, IDispatch *pdispReserved)
515 {
516     DocHost *This = DOCHOSTUI_THIS(iface);
517     HRESULT hres;
518
519     TRACE("(%p)->(%d %p %p %p)\n", This, dwID, ppt, pcmdtReserved, pdispReserved);
520
521     if(This->hostui) {
522         hres = IDocHostUIHandler_ShowContextMenu(This->hostui, dwID, ppt, pcmdtReserved,
523                                                  pdispReserved);
524         if(hres == S_OK)
525             return S_OK;
526     }
527
528     FIXME("default action not implemented\n");
529     return E_NOTIMPL;
530 }
531
532 static HRESULT WINAPI DocHostUIHandler_GetHostInfo(IDocHostUIHandler2 *iface,
533         DOCHOSTUIINFO *pInfo)
534 {
535     DocHost *This = DOCHOSTUI_THIS(iface);
536     HRESULT hres;
537
538     TRACE("(%p)->(%p)\n", This, pInfo);
539
540     if(This->hostui) {
541         hres = IDocHostUIHandler_GetHostInfo(This->hostui, pInfo);
542         if(SUCCEEDED(hres))
543             return hres;
544     }
545
546     pInfo->dwFlags = DOCHOSTUIFLAG_DISABLE_HELP_MENU | DOCHOSTUIFLAG_OPENNEWWIN
547         | DOCHOSTUIFLAG_URL_ENCODING_ENABLE_UTF8 | DOCHOSTUIFLAG_ENABLE_INPLACE_NAVIGATION
548         | DOCHOSTUIFLAG_IME_ENABLE_RECONVERSION;
549     return S_OK;
550 }
551
552 static HRESULT WINAPI DocHostUIHandler_ShowUI(IDocHostUIHandler2 *iface, DWORD dwID,
553         IOleInPlaceActiveObject *pActiveObject, IOleCommandTarget *pCommandTarget,
554         IOleInPlaceFrame *pFrame, IOleInPlaceUIWindow *pDoc)
555 {
556     DocHost *This = DOCHOSTUI_THIS(iface);
557     FIXME("(%p)->(%d %p %p %p %p)\n", This, dwID, pActiveObject, pCommandTarget,
558           pFrame, pDoc);
559     return E_NOTIMPL;
560 }
561
562 static HRESULT WINAPI DocHostUIHandler_HideUI(IDocHostUIHandler2 *iface)
563 {
564     DocHost *This = DOCHOSTUI_THIS(iface);
565     FIXME("(%p)\n", This);
566     return E_NOTIMPL;
567 }
568
569 static HRESULT WINAPI DocHostUIHandler_UpdateUI(IDocHostUIHandler2 *iface)
570 {
571     DocHost *This = DOCHOSTUI_THIS(iface);
572
573     TRACE("(%p)\n", This);
574
575     if(!This->hostui)
576         return S_FALSE;
577
578     return IDocHostUIHandler_UpdateUI(This->hostui);
579 }
580
581 static HRESULT WINAPI DocHostUIHandler_EnableModeless(IDocHostUIHandler2 *iface,
582                                                       BOOL fEnable)
583 {
584     DocHost *This = DOCHOSTUI_THIS(iface);
585     FIXME("(%p)->(%x)\n", This, fEnable);
586     return E_NOTIMPL;
587 }
588
589 static HRESULT WINAPI DocHostUIHandler_OnDocWindowActivate(IDocHostUIHandler2 *iface,
590                                                            BOOL fActivate)
591 {
592     DocHost *This = DOCHOSTUI_THIS(iface);
593     FIXME("(%p)->(%x)\n", This, fActivate);
594     return E_NOTIMPL;
595 }
596
597 static HRESULT WINAPI DocHostUIHandler_OnFrameWindowActivate(IDocHostUIHandler2 *iface,
598                                                              BOOL fActivate)
599 {
600     DocHost *This = DOCHOSTUI_THIS(iface);
601     FIXME("(%p)->(%x)\n", This, fActivate);
602     return E_NOTIMPL;
603 }
604
605 static HRESULT WINAPI DocHostUIHandler_ResizeBorder(IDocHostUIHandler2 *iface,
606         LPCRECT prcBorder, IOleInPlaceUIWindow *pUIWindow, BOOL fRameWindow)
607 {
608     DocHost *This = DOCHOSTUI_THIS(iface);
609     FIXME("(%p)->(%p %p %X)\n", This, prcBorder, pUIWindow, fRameWindow);
610     return E_NOTIMPL;
611 }
612
613 static HRESULT WINAPI DocHostUIHandler_TranslateAccelerator(IDocHostUIHandler2 *iface,
614         LPMSG lpMsg, const GUID *pguidCmdGroup, DWORD nCmdID)
615 {
616     DocHost *This = DOCHOSTUI_THIS(iface);
617     FIXME("(%p)->(%p %p %d)\n", This, lpMsg, pguidCmdGroup, nCmdID);
618     return E_NOTIMPL;
619 }
620
621 static HRESULT WINAPI DocHostUIHandler_GetOptionKeyPath(IDocHostUIHandler2 *iface,
622         LPOLESTR *pchKey, DWORD dw)
623 {
624     DocHost *This = DOCHOSTUI_THIS(iface);
625
626     TRACE("(%p)->(%p %d)\n", This, pchKey, dw);
627
628     if(This->hostui)
629         return IDocHostUIHandler_GetOptionKeyPath(This->hostui, pchKey, dw);
630
631     return S_OK;
632 }
633
634 static HRESULT WINAPI DocHostUIHandler_GetDropTarget(IDocHostUIHandler2 *iface,
635         IDropTarget *pDropTarget, IDropTarget **ppDropTarget)
636 {
637     DocHost *This = DOCHOSTUI_THIS(iface);
638     FIXME("(%p)\n", This);
639     return E_NOTIMPL;
640 }
641
642 static HRESULT WINAPI DocHostUIHandler_GetExternal(IDocHostUIHandler2 *iface,
643         IDispatch **ppDispatch)
644 {
645     DocHost *This = DOCHOSTUI_THIS(iface);
646
647     TRACE("(%p)->(%p)\n", This, ppDispatch);
648
649     if(This->hostui)
650         return IDocHostUIHandler_GetExternal(This->hostui, ppDispatch);
651
652     FIXME("default action not implemented\n");
653     return E_NOTIMPL;
654 }
655
656 static HRESULT WINAPI DocHostUIHandler_TranslateUrl(IDocHostUIHandler2 *iface,
657         DWORD dwTranslate, OLECHAR *pchURLIn, OLECHAR **ppchURLOut)
658 {
659     DocHost *This = DOCHOSTUI_THIS(iface);
660
661     TRACE("(%p)->(%d %s %p)\n", This, dwTranslate, debugstr_w(pchURLIn), ppchURLOut);
662
663     if(This->hostui)
664         return IDocHostUIHandler_TranslateUrl(This->hostui, dwTranslate,
665                                               pchURLIn, ppchURLOut);
666
667     return S_FALSE;
668 }
669
670 static HRESULT WINAPI DocHostUIHandler_FilterDataObject(IDocHostUIHandler2 *iface,
671         IDataObject *pDO, IDataObject **ppDORet)
672 {
673     DocHost *This = DOCHOSTUI_THIS(iface);
674     FIXME("(%p)->(%p %p)\n", This, pDO, ppDORet);
675     return E_NOTIMPL;
676 }
677
678 static HRESULT WINAPI DocHostUIHandler_GetOverrideKeyPath(IDocHostUIHandler2 *iface,
679         LPOLESTR *pchKey, DWORD dw)
680 {
681     DocHost *This = DOCHOSTUI_THIS(iface);
682     IDocHostUIHandler2 *handler;
683     HRESULT hres;
684
685     TRACE("(%p)->(%p %d)\n", This, pchKey, dw);
686
687     if(!This->hostui)
688         return S_OK;
689
690     hres = IDocHostUIHandler_QueryInterface(This->hostui, &IID_IDocHostUIHandler2,
691                                             (void**)&handler);
692     if(SUCCEEDED(hres)) {
693         hres = IDocHostUIHandler2_GetOverrideKeyPath(handler, pchKey, dw);
694         IDocHostUIHandler2_Release(handler);
695         return hres;
696     }
697
698     return S_OK;
699 }
700
701 #undef DOCHOSTUI_THIS
702
703 static const IDocHostUIHandler2Vtbl DocHostUIHandler2Vtbl = {
704     DocHostUIHandler_QueryInterface,
705     DocHostUIHandler_AddRef,
706     DocHostUIHandler_Release,
707     DocHostUIHandler_ShowContextMenu,
708     DocHostUIHandler_GetHostInfo,
709     DocHostUIHandler_ShowUI,
710     DocHostUIHandler_HideUI,
711     DocHostUIHandler_UpdateUI,
712     DocHostUIHandler_EnableModeless,
713     DocHostUIHandler_OnDocWindowActivate,
714     DocHostUIHandler_OnFrameWindowActivate,
715     DocHostUIHandler_ResizeBorder,
716     DocHostUIHandler_TranslateAccelerator,
717     DocHostUIHandler_GetOptionKeyPath,
718     DocHostUIHandler_GetDropTarget,
719     DocHostUIHandler_GetExternal,
720     DocHostUIHandler_TranslateUrl,
721     DocHostUIHandler_FilterDataObject,
722     DocHostUIHandler_GetOverrideKeyPath
723 };
724
725 #define PROPNOTIF_THIS(iface) DEFINE_THIS(DocHost, IPropertyNotifySink, iface)
726
727 static HRESULT WINAPI PropertyNotifySink_QueryInterface(IPropertyNotifySink *iface,
728         REFIID riid, void **ppv)
729 {
730     DocHost *This = PROPNOTIF_THIS(iface);
731     return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
732 }
733
734 static ULONG WINAPI PropertyNotifySink_AddRef(IPropertyNotifySink *iface)
735 {
736     DocHost *This = PROPNOTIF_THIS(iface);
737     return IOleClientSite_AddRef(&This->IOleClientSite_iface);
738 }
739
740 static ULONG WINAPI PropertyNotifySink_Release(IPropertyNotifySink *iface)
741 {
742     DocHost *This = PROPNOTIF_THIS(iface);
743     return IOleClientSite_Release(&This->IOleClientSite_iface);
744 }
745
746 static HRESULT WINAPI PropertyNotifySink_OnChanged(IPropertyNotifySink *iface, DISPID dispID)
747 {
748     DocHost *This = PROPNOTIF_THIS(iface);
749
750     TRACE("(%p)->(%d)\n", This, dispID);
751
752     switch(dispID) {
753     case DISPID_READYSTATE: {
754         READYSTATE ready_state;
755         HRESULT hres;
756
757         hres = get_doc_ready_state(This, &ready_state);
758         if(FAILED(hres))
759             return hres;
760
761         if(ready_state == READYSTATE_COMPLETE && !This->doc_navigate)
762             advise_prop_notif(This, FALSE);
763
764         push_ready_state_task(This, ready_state);
765         break;
766     }
767     default:
768         FIXME("unimplemented dispid %d\n", dispID);
769         return E_NOTIMPL;
770     }
771
772     return S_OK;
773 }
774
775 static HRESULT WINAPI PropertyNotifySink_OnRequestEdit(IPropertyNotifySink *iface, DISPID dispID)
776 {
777     DocHost *This = PROPNOTIF_THIS(iface);
778     FIXME("(%p)->(%d)\n", This, dispID);
779     return E_NOTIMPL;
780 }
781
782 #undef PROPNOTIF_THIS
783
784 static const IPropertyNotifySinkVtbl PropertyNotifySinkVtbl = {
785     PropertyNotifySink_QueryInterface,
786     PropertyNotifySink_AddRef,
787     PropertyNotifySink_Release,
788     PropertyNotifySink_OnChanged,
789     PropertyNotifySink_OnRequestEdit
790 };
791
792 void DocHost_Init(DocHost *This, IDispatch *disp, const IDocHostContainerVtbl* container)
793 {
794     This->lpDocHostUIHandlerVtbl = &DocHostUIHandler2Vtbl;
795     This->lpOleCommandTargetVtbl = &OleCommandTargetVtbl;
796     This->lpIPropertyNotifySinkVtbl = &PropertyNotifySinkVtbl;
797
798     This->disp = disp;
799
800     This->container_vtbl = container;
801
802     This->client_disp = NULL;
803
804     This->document = NULL;
805     This->hostui = NULL;
806     This->frame = NULL;
807
808     This->hwnd = NULL;
809     This->frame_hwnd = NULL;
810     This->url = NULL;
811
812     This->silent = VARIANT_FALSE;
813     This->offline = VARIANT_FALSE;
814
815     This->ready_state = READYSTATE_UNINITIALIZED;
816     This->is_prop_notif = FALSE;
817
818     DocHost_ClientSite_Init(This);
819     DocHost_Frame_Init(This);
820
821     ConnectionPointContainer_Init(&This->cps, (IUnknown*)disp);
822 }
823
824 void DocHost_Release(DocHost *This)
825 {
826     release_dochost_client(This);
827     DocHost_ClientSite_Release(This);
828
829     ConnectionPointContainer_Destroy(&This->cps);
830
831     heap_free(This->url);
832 }