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