mshtml: Properly handle navigation in containers supporting IWebBrowserApp.
[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(ieframe);
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 && !This->browser_service /* FIXME */)
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         if(!This->browser_service) /* FIXME: Not fully correct */
201             notif_complete(This, DISPID_DOCUMENTCOMPLETE);
202     }else {
203         set_doc_state(This, ready_state);
204     }
205 }
206
207 typedef struct {
208     task_header_t header;
209     IUnknown *doc;
210     READYSTATE ready_state;
211 } ready_state_task_t;
212
213 static void ready_state_task_destr(task_header_t *_task)
214 {
215     ready_state_task_t *task = (ready_state_task_t*)_task;
216
217     IUnknown_Release(task->doc);
218     heap_free(task);
219 }
220
221 static void ready_state_proc(DocHost *This, task_header_t *_task)
222 {
223     ready_state_task_t *task = (ready_state_task_t*)_task;
224
225     if(task->doc == This->document)
226         update_ready_state(This, task->ready_state);
227 }
228
229 static void push_ready_state_task(DocHost *This, READYSTATE ready_state)
230 {
231     ready_state_task_t *task = heap_alloc(sizeof(ready_state_task_t));
232
233     IUnknown_AddRef(This->document);
234     task->doc = This->document;
235     task->ready_state = ready_state;
236
237     push_dochost_task(This, &task->header, ready_state_proc, ready_state_task_destr, FALSE);
238 }
239
240 static void object_available_task_destr(task_header_t *task)
241 {
242     heap_free(task);
243 }
244
245 static void object_available_proc(DocHost *This, task_header_t *task)
246 {
247     object_available(This);
248 }
249
250 HRESULT dochost_object_available(DocHost *This, IUnknown *doc)
251 {
252     READYSTATE ready_state;
253     task_header_t *task;
254     IOleObject *oleobj;
255     HRESULT hres;
256
257     IUnknown_AddRef(doc);
258     This->document = doc;
259
260     hres = IUnknown_QueryInterface(doc, &IID_IOleObject, (void**)&oleobj);
261     if(SUCCEEDED(hres)) {
262         CLSID clsid;
263
264         hres = IOleObject_GetUserClassID(oleobj, &clsid);
265         if(SUCCEEDED(hres))
266             TRACE("Got clsid %s\n",
267                   IsEqualGUID(&clsid, &CLSID_HTMLDocument) ? "CLSID_HTMLDocument" : debugstr_guid(&clsid));
268
269         hres = IOleObject_SetClientSite(oleobj, &This->IOleClientSite_iface);
270         if(FAILED(hres))
271             FIXME("SetClientSite failed: %08x\n", hres);
272
273         IOleObject_Release(oleobj);
274     }else {
275         FIXME("Could not get IOleObject iface: %08x\n", hres);
276     }
277
278     /* FIXME: Call SetAdvise */
279
280     task = heap_alloc(sizeof(*task));
281     push_dochost_task(This, task, object_available_proc, object_available_task_destr, FALSE);
282
283     hres = get_doc_ready_state(This, &ready_state);
284     if(SUCCEEDED(hres)) {
285         if(ready_state == READYSTATE_COMPLETE)
286             push_ready_state_task(This, READYSTATE_COMPLETE);
287         if(ready_state != READYSTATE_COMPLETE || This->doc_navigate)
288             advise_prop_notif(This, TRUE);
289     }
290
291     return S_OK;
292 }
293
294 static LRESULT resize_document(DocHost *This, LONG width, LONG height)
295 {
296     RECT rect = {0, 0, width, height};
297
298     TRACE("(%p)->(%d %d)\n", This, width, height);
299
300     if(This->view)
301         IOleDocumentView_SetRect(This->view, &rect);
302
303     return 0;
304 }
305
306 static LRESULT WINAPI doc_view_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
307 {
308     DocHost *This;
309
310     static const WCHAR wszTHIS[] = {'T','H','I','S',0};
311
312     if(msg == WM_CREATE) {
313         This = *(DocHost**)lParam;
314         SetPropW(hwnd, wszTHIS, This);
315     }else {
316         This = GetPropW(hwnd, wszTHIS);
317     }
318
319     switch(msg) {
320     case WM_SIZE:
321         return resize_document(This, LOWORD(lParam), HIWORD(lParam));
322     }
323
324     return DefWindowProcW(hwnd, msg, wParam, lParam);
325 }
326
327 void create_doc_view_hwnd(DocHost *This)
328 {
329     RECT rect;
330
331     static const WCHAR wszShell_DocObject_View[] =
332         {'S','h','e','l','l',' ','D','o','c','O','b','j','e','c','t',' ','V','i','e','w',0};
333
334     if(!doc_view_atom) {
335         static WNDCLASSEXW wndclass = {
336             sizeof(wndclass),
337             CS_PARENTDC,
338             doc_view_proc,
339             0, 0 /* native uses 4*/, NULL, NULL, NULL,
340             (HBRUSH)(COLOR_WINDOW + 1), NULL,
341             wszShell_DocObject_View,
342             NULL
343         };
344
345         wndclass.hInstance = ieframe_instance;
346
347         doc_view_atom = RegisterClassExW(&wndclass);
348     }
349
350     This->container_vtbl->GetDocObjRect(This, &rect);
351     This->hwnd = CreateWindowExW(0, wszShell_DocObject_View,
352          wszShell_DocObject_View,
353          WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_TABSTOP,
354          rect.left, rect.top, rect.right, rect.bottom, This->frame_hwnd,
355          NULL, ieframe_instance, This);
356 }
357
358 void deactivate_document(DocHost *This)
359 {
360     IOleInPlaceObjectWindowless *winobj;
361     IOleObject *oleobj = NULL;
362     IHlinkTarget *hlink = NULL;
363     HRESULT hres;
364
365     if(!This->document) return;
366
367     if(This->doc_navigate) {
368         IUnknown_Release(This->doc_navigate);
369         This->doc_navigate = NULL;
370     }
371
372     if(This->is_prop_notif)
373         advise_prop_notif(This, FALSE);
374
375     if(This->view)
376         IOleDocumentView_UIActivate(This->view, FALSE);
377
378     hres = IUnknown_QueryInterface(This->document, &IID_IOleInPlaceObjectWindowless,
379                                    (void**)&winobj);
380     if(SUCCEEDED(hres)) {
381         IOleInPlaceObjectWindowless_InPlaceDeactivate(winobj);
382         IOleInPlaceObjectWindowless_Release(winobj);
383     }
384
385     if(This->view) {
386         IOleDocumentView_Show(This->view, FALSE);
387         IOleDocumentView_CloseView(This->view, 0);
388         IOleDocumentView_SetInPlaceSite(This->view, NULL);
389         IOleDocumentView_Release(This->view);
390         This->view = NULL;
391     }
392
393     hres = IUnknown_QueryInterface(This->document, &IID_IOleObject, (void**)&oleobj);
394     if(SUCCEEDED(hres))
395         IOleObject_Close(oleobj, OLECLOSE_NOSAVE);
396
397     hres = IUnknown_QueryInterface(This->document, &IID_IHlinkTarget, (void**)&hlink);
398     if(SUCCEEDED(hres)) {
399         IHlinkTarget_SetBrowseContext(hlink, NULL);
400         IHlinkTarget_Release(hlink);
401     }
402
403     if(oleobj) {
404         IOleClientSite *client_site = NULL;
405
406         IOleObject_GetClientSite(oleobj, &client_site);
407         if(client_site) {
408             if(client_site == &This->IOleClientSite_iface)
409                 IOleObject_SetClientSite(oleobj, NULL);
410             IOleClientSite_Release(client_site);
411         }
412
413         IOleObject_Release(oleobj);
414     }
415
416     IUnknown_Release(This->document);
417     This->document = NULL;
418 }
419
420 void release_dochost_client(DocHost *This)
421 {
422     if(This->hwnd) {
423         DestroyWindow(This->hwnd);
424         This->hwnd = NULL;
425     }
426
427     if(This->hostui) {
428         IDocHostUIHandler_Release(This->hostui);
429         This->hostui = NULL;
430     }
431
432     if(This->client_disp) {
433         IDispatch_Release(This->client_disp);
434         This->client_disp = NULL;
435     }
436
437     if(This->frame) {
438         IOleInPlaceFrame_Release(This->frame);
439         This->frame = NULL;
440     }
441 }
442
443 static inline DocHost *impl_from_IOleCommandTarget(IOleCommandTarget *iface)
444 {
445     return CONTAINING_RECORD(iface, DocHost, IOleCommandTarget_iface);
446 }
447
448 static HRESULT WINAPI ClOleCommandTarget_QueryInterface(IOleCommandTarget *iface,
449         REFIID riid, void **ppv)
450 {
451     DocHost *This = impl_from_IOleCommandTarget(iface);
452     return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
453 }
454
455 static ULONG WINAPI ClOleCommandTarget_AddRef(IOleCommandTarget *iface)
456 {
457     DocHost *This = impl_from_IOleCommandTarget(iface);
458     return IOleClientSite_AddRef(&This->IOleClientSite_iface);
459 }
460
461 static ULONG WINAPI ClOleCommandTarget_Release(IOleCommandTarget *iface)
462 {
463     DocHost *This = impl_from_IOleCommandTarget(iface);
464     return IOleClientSite_Release(&This->IOleClientSite_iface);
465 }
466
467 static HRESULT WINAPI ClOleCommandTarget_QueryStatus(IOleCommandTarget *iface,
468         const GUID *pguidCmdGroup, ULONG cCmds, OLECMD prgCmds[], OLECMDTEXT *pCmdText)
469 {
470     DocHost *This = impl_from_IOleCommandTarget(iface);
471     ULONG i= 0;
472     FIXME("(%p)->(%s %u %p %p)\n", This, debugstr_guid(pguidCmdGroup), cCmds, prgCmds,
473           pCmdText);
474     while (prgCmds && (cCmds > i)) {
475         FIXME("command_%u: %u, 0x%x\n", i, prgCmds[i].cmdID, prgCmds[i].cmdf);
476         i++;
477     }
478     return E_NOTIMPL;
479 }
480
481 static HRESULT WINAPI ClOleCommandTarget_Exec(IOleCommandTarget *iface,
482         const GUID *pguidCmdGroup, DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn,
483         VARIANT *pvaOut)
484 {
485     DocHost *This = impl_from_IOleCommandTarget(iface);
486
487     TRACE("(%p)->(%s %d %d %p %p)\n", This, debugstr_guid(pguidCmdGroup), nCmdID,
488           nCmdexecopt, debugstr_variant(pvaIn), debugstr_variant(pvaOut));
489
490     if(!pguidCmdGroup) {
491         switch(nCmdID) {
492         case OLECMDID_UPDATECOMMANDS:
493             return This->container_vtbl->exec(This, pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
494         default:
495             FIXME("Unimplemented cmdid %d\n", nCmdID);
496             return E_NOTIMPL;
497         }
498         return S_OK;
499     }
500
501     if(IsEqualGUID(pguidCmdGroup, &CGID_DocHostCmdPriv)) {
502         switch(nCmdID) {
503         case DOCHOST_DOCCANNAVIGATE:
504             if(!pvaIn || V_VT(pvaIn) != VT_UNKNOWN)
505                 return E_INVALIDARG;
506
507             if(This->doc_navigate)
508                 IUnknown_Release(This->doc_navigate);
509             IUnknown_AddRef(V_UNKNOWN(pvaIn));
510             This->doc_navigate = V_UNKNOWN(pvaIn);
511             return S_OK;
512
513         case 1: {
514             IHTMLWindow2 *win2;
515             SAFEARRAY *sa = V_ARRAY(pvaIn);
516             VARIANT status_code, url, htmlwindow;
517             LONG ind;
518             HRESULT hres;
519
520             if(V_VT(pvaIn) != VT_ARRAY || !sa || (SafeArrayGetDim(sa) != 1))
521                 return E_INVALIDARG;
522
523             ind = 0;
524             hres = SafeArrayGetElement(sa, &ind, &status_code);
525             if(FAILED(hres) || V_VT(&status_code)!=VT_I4)
526                 return E_INVALIDARG;
527
528             ind = 1;
529             hres = SafeArrayGetElement(sa, &ind, &url);
530             if(FAILED(hres) || V_VT(&url)!=VT_BSTR)
531                 return E_INVALIDARG;
532
533             ind = 3;
534             hres = SafeArrayGetElement(sa, &ind, &htmlwindow);
535             if(FAILED(hres) || V_VT(&htmlwindow)!=VT_UNKNOWN || !V_UNKNOWN(&htmlwindow))
536                 return E_INVALIDARG;
537
538             hres = IUnknown_QueryInterface(V_UNKNOWN(&htmlwindow), &IID_IHTMLWindow2, (void**)&win2);
539             if(FAILED(hres))
540                 return E_INVALIDARG;
541
542             handle_navigation_error(This, V_I4(&status_code), V_BSTR(&url), win2);
543             IHTMLWindow2_Release(win2);
544             return S_OK;
545         }
546
547         default:
548             FIXME("unsupported command %d of CGID_DocHostCmdPriv\n", nCmdID);
549             return E_NOTIMPL;
550         }
551     }
552
553     FIXME("Unimplemented group %s\n", debugstr_guid(pguidCmdGroup));
554     return E_NOTIMPL;
555 }
556
557 #undef impl_from_IOleCommandTarget
558
559 static const IOleCommandTargetVtbl OleCommandTargetVtbl = {
560     ClOleCommandTarget_QueryInterface,
561     ClOleCommandTarget_AddRef,
562     ClOleCommandTarget_Release,
563     ClOleCommandTarget_QueryStatus,
564     ClOleCommandTarget_Exec
565 };
566
567 static inline DocHost *impl_from_IDocHostUIHandler2(IDocHostUIHandler2 *iface)
568 {
569     return CONTAINING_RECORD(iface, DocHost, IDocHostUIHandler2_iface);
570 }
571
572 static HRESULT WINAPI DocHostUIHandler_QueryInterface(IDocHostUIHandler2 *iface,
573                                                       REFIID riid, void **ppv)
574 {
575     DocHost *This = impl_from_IDocHostUIHandler2(iface);
576     return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
577 }
578
579 static ULONG WINAPI DocHostUIHandler_AddRef(IDocHostUIHandler2 *iface)
580 {
581     DocHost *This = impl_from_IDocHostUIHandler2(iface);
582     return IOleClientSite_AddRef(&This->IOleClientSite_iface);
583 }
584
585 static ULONG WINAPI DocHostUIHandler_Release(IDocHostUIHandler2 *iface)
586 {
587     DocHost *This = impl_from_IDocHostUIHandler2(iface);
588     return IOleClientSite_Release(&This->IOleClientSite_iface);
589 }
590
591 static HRESULT WINAPI DocHostUIHandler_ShowContextMenu(IDocHostUIHandler2 *iface,
592          DWORD dwID, POINT *ppt, IUnknown *pcmdtReserved, IDispatch *pdispReserved)
593 {
594     DocHost *This = impl_from_IDocHostUIHandler2(iface);
595     HRESULT hres;
596
597     TRACE("(%p)->(%d %p %p %p)\n", This, dwID, ppt, pcmdtReserved, pdispReserved);
598
599     if(This->hostui) {
600         hres = IDocHostUIHandler_ShowContextMenu(This->hostui, dwID, ppt, pcmdtReserved,
601                                                  pdispReserved);
602         if(hres == S_OK)
603             return S_OK;
604     }
605
606     FIXME("default action not implemented\n");
607     return E_NOTIMPL;
608 }
609
610 static HRESULT WINAPI DocHostUIHandler_GetHostInfo(IDocHostUIHandler2 *iface,
611         DOCHOSTUIINFO *pInfo)
612 {
613     DocHost *This = impl_from_IDocHostUIHandler2(iface);
614     HRESULT hres;
615
616     TRACE("(%p)->(%p)\n", This, pInfo);
617
618     if(This->hostui) {
619         hres = IDocHostUIHandler_GetHostInfo(This->hostui, pInfo);
620         if(SUCCEEDED(hres))
621             return hres;
622     }
623
624     pInfo->dwFlags = DOCHOSTUIFLAG_DISABLE_HELP_MENU | DOCHOSTUIFLAG_OPENNEWWIN
625         | DOCHOSTUIFLAG_URL_ENCODING_ENABLE_UTF8 | DOCHOSTUIFLAG_ENABLE_INPLACE_NAVIGATION
626         | DOCHOSTUIFLAG_IME_ENABLE_RECONVERSION;
627     return S_OK;
628 }
629
630 static HRESULT WINAPI DocHostUIHandler_ShowUI(IDocHostUIHandler2 *iface, DWORD dwID,
631         IOleInPlaceActiveObject *pActiveObject, IOleCommandTarget *pCommandTarget,
632         IOleInPlaceFrame *pFrame, IOleInPlaceUIWindow *pDoc)
633 {
634     DocHost *This = impl_from_IDocHostUIHandler2(iface);
635     FIXME("(%p)->(%d %p %p %p %p)\n", This, dwID, pActiveObject, pCommandTarget,
636           pFrame, pDoc);
637     return E_NOTIMPL;
638 }
639
640 static HRESULT WINAPI DocHostUIHandler_HideUI(IDocHostUIHandler2 *iface)
641 {
642     DocHost *This = impl_from_IDocHostUIHandler2(iface);
643     FIXME("(%p)\n", This);
644     return E_NOTIMPL;
645 }
646
647 static HRESULT WINAPI DocHostUIHandler_UpdateUI(IDocHostUIHandler2 *iface)
648 {
649     DocHost *This = impl_from_IDocHostUIHandler2(iface);
650
651     TRACE("(%p)\n", This);
652
653     if(!This->hostui)
654         return S_FALSE;
655
656     return IDocHostUIHandler_UpdateUI(This->hostui);
657 }
658
659 static HRESULT WINAPI DocHostUIHandler_EnableModeless(IDocHostUIHandler2 *iface,
660                                                       BOOL fEnable)
661 {
662     DocHost *This = impl_from_IDocHostUIHandler2(iface);
663     FIXME("(%p)->(%x)\n", This, fEnable);
664     return E_NOTIMPL;
665 }
666
667 static HRESULT WINAPI DocHostUIHandler_OnDocWindowActivate(IDocHostUIHandler2 *iface,
668                                                            BOOL fActivate)
669 {
670     DocHost *This = impl_from_IDocHostUIHandler2(iface);
671     FIXME("(%p)->(%x)\n", This, fActivate);
672     return E_NOTIMPL;
673 }
674
675 static HRESULT WINAPI DocHostUIHandler_OnFrameWindowActivate(IDocHostUIHandler2 *iface,
676                                                              BOOL fActivate)
677 {
678     DocHost *This = impl_from_IDocHostUIHandler2(iface);
679     FIXME("(%p)->(%x)\n", This, fActivate);
680     return E_NOTIMPL;
681 }
682
683 static HRESULT WINAPI DocHostUIHandler_ResizeBorder(IDocHostUIHandler2 *iface,
684         LPCRECT prcBorder, IOleInPlaceUIWindow *pUIWindow, BOOL fRameWindow)
685 {
686     DocHost *This = impl_from_IDocHostUIHandler2(iface);
687     FIXME("(%p)->(%p %p %X)\n", This, prcBorder, pUIWindow, fRameWindow);
688     return E_NOTIMPL;
689 }
690
691 static HRESULT WINAPI DocHostUIHandler_TranslateAccelerator(IDocHostUIHandler2 *iface,
692         LPMSG lpMsg, const GUID *pguidCmdGroup, DWORD nCmdID)
693 {
694     DocHost *This = impl_from_IDocHostUIHandler2(iface);
695     HRESULT hr = S_FALSE;
696     TRACE("(%p)->(%p %p %d)\n", This, lpMsg, pguidCmdGroup, nCmdID);
697
698     if(This->hostui)
699         hr = IDocHostUIHandler_TranslateAccelerator(This->hostui, lpMsg, pguidCmdGroup, nCmdID);
700
701     return hr;
702 }
703
704 static HRESULT WINAPI DocHostUIHandler_GetOptionKeyPath(IDocHostUIHandler2 *iface,
705         LPOLESTR *pchKey, DWORD dw)
706 {
707     DocHost *This = impl_from_IDocHostUIHandler2(iface);
708
709     TRACE("(%p)->(%p %d)\n", This, pchKey, dw);
710
711     if(This->hostui)
712         return IDocHostUIHandler_GetOptionKeyPath(This->hostui, pchKey, dw);
713
714     return S_OK;
715 }
716
717 static HRESULT WINAPI DocHostUIHandler_GetDropTarget(IDocHostUIHandler2 *iface,
718         IDropTarget *pDropTarget, IDropTarget **ppDropTarget)
719 {
720     DocHost *This = impl_from_IDocHostUIHandler2(iface);
721     FIXME("(%p)\n", This);
722     return E_NOTIMPL;
723 }
724
725 static HRESULT WINAPI DocHostUIHandler_GetExternal(IDocHostUIHandler2 *iface,
726         IDispatch **ppDispatch)
727 {
728     DocHost *This = impl_from_IDocHostUIHandler2(iface);
729
730     TRACE("(%p)->(%p)\n", This, ppDispatch);
731
732     if(This->hostui)
733         return IDocHostUIHandler_GetExternal(This->hostui, ppDispatch);
734
735     FIXME("default action not implemented\n");
736     return E_NOTIMPL;
737 }
738
739 static HRESULT WINAPI DocHostUIHandler_TranslateUrl(IDocHostUIHandler2 *iface,
740         DWORD dwTranslate, OLECHAR *pchURLIn, OLECHAR **ppchURLOut)
741 {
742     DocHost *This = impl_from_IDocHostUIHandler2(iface);
743
744     TRACE("(%p)->(%d %s %p)\n", This, dwTranslate, debugstr_w(pchURLIn), ppchURLOut);
745
746     if(This->hostui)
747         return IDocHostUIHandler_TranslateUrl(This->hostui, dwTranslate,
748                                               pchURLIn, ppchURLOut);
749
750     return S_FALSE;
751 }
752
753 static HRESULT WINAPI DocHostUIHandler_FilterDataObject(IDocHostUIHandler2 *iface,
754         IDataObject *pDO, IDataObject **ppDORet)
755 {
756     DocHost *This = impl_from_IDocHostUIHandler2(iface);
757     FIXME("(%p)->(%p %p)\n", This, pDO, ppDORet);
758     return E_NOTIMPL;
759 }
760
761 static HRESULT WINAPI DocHostUIHandler_GetOverrideKeyPath(IDocHostUIHandler2 *iface,
762         LPOLESTR *pchKey, DWORD dw)
763 {
764     DocHost *This = impl_from_IDocHostUIHandler2(iface);
765     IDocHostUIHandler2 *handler;
766     HRESULT hres;
767
768     TRACE("(%p)->(%p %d)\n", This, pchKey, dw);
769
770     if(!This->hostui)
771         return S_OK;
772
773     hres = IDocHostUIHandler_QueryInterface(This->hostui, &IID_IDocHostUIHandler2,
774                                             (void**)&handler);
775     if(SUCCEEDED(hres)) {
776         hres = IDocHostUIHandler2_GetOverrideKeyPath(handler, pchKey, dw);
777         IDocHostUIHandler2_Release(handler);
778         return hres;
779     }
780
781     return S_OK;
782 }
783
784 static const IDocHostUIHandler2Vtbl DocHostUIHandler2Vtbl = {
785     DocHostUIHandler_QueryInterface,
786     DocHostUIHandler_AddRef,
787     DocHostUIHandler_Release,
788     DocHostUIHandler_ShowContextMenu,
789     DocHostUIHandler_GetHostInfo,
790     DocHostUIHandler_ShowUI,
791     DocHostUIHandler_HideUI,
792     DocHostUIHandler_UpdateUI,
793     DocHostUIHandler_EnableModeless,
794     DocHostUIHandler_OnDocWindowActivate,
795     DocHostUIHandler_OnFrameWindowActivate,
796     DocHostUIHandler_ResizeBorder,
797     DocHostUIHandler_TranslateAccelerator,
798     DocHostUIHandler_GetOptionKeyPath,
799     DocHostUIHandler_GetDropTarget,
800     DocHostUIHandler_GetExternal,
801     DocHostUIHandler_TranslateUrl,
802     DocHostUIHandler_FilterDataObject,
803     DocHostUIHandler_GetOverrideKeyPath
804 };
805
806 static inline DocHost *impl_from_IPropertyNotifySink(IPropertyNotifySink *iface)
807 {
808     return CONTAINING_RECORD(iface, DocHost, IPropertyNotifySink_iface);
809 }
810
811 static HRESULT WINAPI PropertyNotifySink_QueryInterface(IPropertyNotifySink *iface,
812         REFIID riid, void **ppv)
813 {
814     DocHost *This = impl_from_IPropertyNotifySink(iface);
815     return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
816 }
817
818 static ULONG WINAPI PropertyNotifySink_AddRef(IPropertyNotifySink *iface)
819 {
820     DocHost *This = impl_from_IPropertyNotifySink(iface);
821     return IOleClientSite_AddRef(&This->IOleClientSite_iface);
822 }
823
824 static ULONG WINAPI PropertyNotifySink_Release(IPropertyNotifySink *iface)
825 {
826     DocHost *This = impl_from_IPropertyNotifySink(iface);
827     return IOleClientSite_Release(&This->IOleClientSite_iface);
828 }
829
830 static HRESULT WINAPI PropertyNotifySink_OnChanged(IPropertyNotifySink *iface, DISPID dispID)
831 {
832     DocHost *This = impl_from_IPropertyNotifySink(iface);
833
834     TRACE("(%p)->(%d)\n", This, dispID);
835
836     switch(dispID) {
837     case DISPID_READYSTATE: {
838         READYSTATE ready_state;
839         HRESULT hres;
840
841         hres = get_doc_ready_state(This, &ready_state);
842         if(FAILED(hres))
843             return hres;
844
845         if(ready_state == READYSTATE_COMPLETE && !This->doc_navigate)
846             advise_prop_notif(This, FALSE);
847
848         update_ready_state(This, ready_state);
849         break;
850     }
851     default:
852         FIXME("unimplemented dispid %d\n", dispID);
853         return E_NOTIMPL;
854     }
855
856     return S_OK;
857 }
858
859 static HRESULT WINAPI PropertyNotifySink_OnRequestEdit(IPropertyNotifySink *iface, DISPID dispID)
860 {
861     DocHost *This = impl_from_IPropertyNotifySink(iface);
862     FIXME("(%p)->(%d)\n", This, dispID);
863     return E_NOTIMPL;
864 }
865
866 static const IPropertyNotifySinkVtbl PropertyNotifySinkVtbl = {
867     PropertyNotifySink_QueryInterface,
868     PropertyNotifySink_AddRef,
869     PropertyNotifySink_Release,
870     PropertyNotifySink_OnChanged,
871     PropertyNotifySink_OnRequestEdit
872 };
873
874 void DocHost_Init(DocHost *This, IDispatch *disp, const IDocHostContainerVtbl* container)
875 {
876     This->IDocHostUIHandler2_iface.lpVtbl  = &DocHostUIHandler2Vtbl;
877     This->IOleCommandTarget_iface.lpVtbl   = &OleCommandTargetVtbl;
878     This->IPropertyNotifySink_iface.lpVtbl = &PropertyNotifySinkVtbl;
879
880     This->disp = disp;
881     This->container_vtbl = container;
882
883     This->ready_state = READYSTATE_UNINITIALIZED;
884     list_init(&This->task_queue);
885
886     DocHost_ClientSite_Init(This);
887     DocHost_Frame_Init(This);
888
889     ConnectionPointContainer_Init(&This->cps, (IUnknown*)disp);
890 }
891
892 void DocHost_Release(DocHost *This)
893 {
894     abort_dochost_tasks(This, NULL);
895     release_dochost_client(This);
896     DocHost_ClientSite_Release(This);
897
898     ConnectionPointContainer_Destroy(&This->cps);
899
900     heap_free(This->url);
901 }