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