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