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