shdocvw: Added CUrlHistory stub implementation.
[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
24 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
25
26 static ATOM doc_view_atom = 0;
27
28 static void navigate_complete(DocHost *This)
29 {
30     IDispatch *disp = NULL;
31     DISPPARAMS dispparams;
32     VARIANTARG params[2];
33     VARIANT url;
34     HRESULT hres;
35
36     hres = IUnknown_QueryInterface(This->document, &IID_IDispatch, (void**)&disp);
37     if(FAILED(hres))
38         FIXME("Could not get IDispatch interface\n");
39
40     dispparams.cArgs = 2;
41     dispparams.cNamedArgs = 0;
42     dispparams.rgdispidNamedArgs = NULL;
43     dispparams.rgvarg = params;
44
45     V_VT(params) = (VT_BYREF|VT_VARIANT);
46     V_BYREF(params) = &url;
47
48     V_VT(params+1) = VT_DISPATCH;
49     V_DISPATCH(params+1) = disp;
50
51     V_VT(&url) = VT_BSTR;
52     V_BSTR(&url) = SysAllocString(This->url);
53
54     call_sink(This->cps.wbe2, DISPID_NAVIGATECOMPLETE2, &dispparams);
55     call_sink(This->cps.wbe2, DISPID_DOCUMENTCOMPLETE, &dispparams);
56
57     SysFreeString(V_BSTR(&url));
58     if(disp)
59         IDispatch_Release(disp);
60 }
61
62 static LRESULT navigate2(DocHost *This)
63 {
64     IHlinkTarget *hlink;
65     HRESULT hres;
66
67     TRACE("(%p)\n", This);
68
69     if(!This->document) {
70         WARN("document == NULL\n");
71         return 0;
72     }
73
74     hres = IUnknown_QueryInterface(This->document, &IID_IHlinkTarget, (void**)&hlink);
75     if(FAILED(hres)) {
76         FIXME("Could not get IHlinkTarget interface\n");
77         return 0;
78     }
79
80     hres = IHlinkTarget_Navigate(hlink, 0, NULL);
81     IHlinkTarget_Release(hlink);
82     if(FAILED(hres)) {
83         FIXME("Navigate failed\n");
84         return 0;
85     }
86
87     navigate_complete(This);
88
89     return 0;
90 }
91
92 static LRESULT resize_document(DocHost *This, LONG width, LONG height)
93 {
94     RECT rect = {0, 0, width, height};
95
96     TRACE("(%p)->(%d %d)\n", This, width, height);
97
98     if(This->view)
99         IOleDocumentView_SetRect(This->view, &rect);
100
101     return 0;
102 }
103
104 static LRESULT WINAPI doc_view_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
105 {
106     DocHost *This;
107
108     static const WCHAR wszTHIS[] = {'T','H','I','S',0};
109
110     if(msg == WM_CREATE) {
111         This = *(DocHost**)lParam;
112         SetPropW(hwnd, wszTHIS, This);
113     }else {
114         This = GetPropW(hwnd, wszTHIS);
115     }
116
117     switch(msg) {
118     case WM_SIZE:
119         return resize_document(This, LOWORD(lParam), HIWORD(lParam));
120     case WB_WM_NAVIGATE2:
121         return navigate2(This);
122     }
123
124     return DefWindowProcW(hwnd, msg, wParam, lParam);
125 }
126
127 void create_doc_view_hwnd(DocHost *This)
128 {
129     RECT rect;
130
131     static const WCHAR wszShell_DocObject_View[] =
132         {'S','h','e','l','l',' ','D','o','c','O','b','j','e','c','t',' ','V','i','e','w',0};
133
134     if(!doc_view_atom) {
135         static WNDCLASSEXW wndclass = {
136             sizeof(wndclass),
137             CS_PARENTDC,
138             doc_view_proc,
139             0, 0 /* native uses 4*/, NULL, NULL, NULL,
140             (HBRUSH)COLOR_WINDOWFRAME, NULL,
141             wszShell_DocObject_View,
142             NULL
143         };
144
145         wndclass.hInstance = shdocvw_hinstance;
146
147         doc_view_atom = RegisterClassExW(&wndclass);
148     }
149
150     GetClientRect(This->frame_hwnd, &rect); /* FIXME */
151     This->hwnd = CreateWindowExW(0, wszShell_DocObject_View,
152          wszShell_DocObject_View,
153          WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_TABSTOP,
154          rect.left, rect.top, rect.right, rect.bottom, This->frame_hwnd,
155          NULL, shdocvw_hinstance, This);
156 }
157
158 void deactivate_document(DocHost *This)
159 {
160     IOleInPlaceObjectWindowless *winobj;
161     IOleObject *oleobj = NULL;
162     IHlinkTarget *hlink = NULL;
163     HRESULT hres;
164
165     if(This->view)
166         IOleDocumentView_UIActivate(This->view, FALSE);
167
168     hres = IUnknown_QueryInterface(This->document, &IID_IOleInPlaceObjectWindowless,
169                                    (void**)&winobj);
170     if(SUCCEEDED(hres)) {
171         IOleInPlaceObjectWindowless_InPlaceDeactivate(winobj);
172         IOleInPlaceObjectWindowless_Release(winobj);
173     }
174
175     if(This->view) {
176         IOleDocumentView_Show(This->view, FALSE);
177         IOleDocumentView_CloseView(This->view, 0);
178         IOleDocumentView_SetInPlaceSite(This->view, NULL);
179         IOleDocumentView_Release(This->view);
180         This->view = NULL;
181     }
182
183     hres = IUnknown_QueryInterface(This->document, &IID_IOleObject, (void**)&oleobj);
184     if(SUCCEEDED(hres))
185         IOleObject_Close(oleobj, OLECLOSE_NOSAVE);
186
187     hres = IUnknown_QueryInterface(This->document, &IID_IHlinkTarget, (void**)&hlink);
188     if(SUCCEEDED(hres)) {
189         IHlinkTarget_SetBrowseContext(hlink, NULL);
190         IHlinkTarget_Release(hlink);
191     }
192
193     if(oleobj) {
194         IOleClientSite *client_site = NULL;
195
196         IOleObject_GetClientSite(oleobj, &client_site);
197         if(client_site) {
198             if(client_site == CLIENTSITE(This))
199                 IOleObject_SetClientSite(oleobj, NULL);
200             IOleClientSite_Release(client_site);
201         }
202
203         IOleObject_Release(oleobj);
204     }
205
206     IUnknown_Release(This->document);
207     This->document = NULL;
208 }
209
210 #define OLECMD_THIS(iface) DEFINE_THIS(DocHost, OleCommandTarget, iface)
211
212 static HRESULT WINAPI ClOleCommandTarget_QueryInterface(IOleCommandTarget *iface,
213         REFIID riid, void **ppv)
214 {
215     DocHost *This = OLECMD_THIS(iface);
216     return IOleClientSite_QueryInterface(CLIENTSITE(This), riid, ppv);
217 }
218
219 static ULONG WINAPI ClOleCommandTarget_AddRef(IOleCommandTarget *iface)
220 {
221     DocHost *This = OLECMD_THIS(iface);
222     return IOleClientSite_AddRef(CLIENTSITE(This));
223 }
224
225 static ULONG WINAPI ClOleCommandTarget_Release(IOleCommandTarget *iface)
226 {
227     DocHost *This = OLECMD_THIS(iface);
228     return IOleClientSite_Release(CLIENTSITE(This));
229 }
230
231 static HRESULT WINAPI ClOleCommandTarget_QueryStatus(IOleCommandTarget *iface,
232         const GUID *pguidCmdGroup, ULONG cCmds, OLECMD prgCmds[], OLECMDTEXT *pCmdText)
233 {
234     DocHost *This = OLECMD_THIS(iface);
235     FIXME("(%p)->(%s %u %p %p)\n", This, debugstr_guid(pguidCmdGroup), cCmds, prgCmds,
236           pCmdText);
237     return E_NOTIMPL;
238 }
239
240 static HRESULT WINAPI ClOleCommandTarget_Exec(IOleCommandTarget *iface,
241         const GUID *pguidCmdGroup, DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn,
242         VARIANT *pvaOut)
243 {
244     DocHost *This = OLECMD_THIS(iface);
245     FIXME("(%p)->(%s %d %d %p %p)\n", This, debugstr_guid(pguidCmdGroup), nCmdID,
246           nCmdexecopt, pvaIn, pvaOut);
247     return E_NOTIMPL;
248 }
249
250 #undef OLECMD_THIS
251
252 static const IOleCommandTargetVtbl OleCommandTargetVtbl = {
253     ClOleCommandTarget_QueryInterface,
254     ClOleCommandTarget_AddRef,
255     ClOleCommandTarget_Release,
256     ClOleCommandTarget_QueryStatus,
257     ClOleCommandTarget_Exec
258 };
259
260 #define DOCHOSTUI_THIS(iface) DEFINE_THIS(DocHost, DocHostUIHandler, iface)
261
262 static HRESULT WINAPI DocHostUIHandler_QueryInterface(IDocHostUIHandler2 *iface,
263                                                       REFIID riid, void **ppv)
264 {
265     DocHost *This = DOCHOSTUI_THIS(iface);
266     return IOleClientSite_QueryInterface(CLIENTSITE(This), riid, ppv);
267 }
268
269 static ULONG WINAPI DocHostUIHandler_AddRef(IDocHostUIHandler2 *iface)
270 {
271     DocHost *This = DOCHOSTUI_THIS(iface);
272     return IOleClientSite_AddRef(CLIENTSITE(This));
273 }
274
275 static ULONG WINAPI DocHostUIHandler_Release(IDocHostUIHandler2 *iface)
276 {
277     DocHost *This = DOCHOSTUI_THIS(iface);
278     return IOleClientSite_Release(CLIENTSITE(This));
279 }
280
281 static HRESULT WINAPI DocHostUIHandler_ShowContextMenu(IDocHostUIHandler2 *iface,
282          DWORD dwID, POINT *ppt, IUnknown *pcmdtReserved, IDispatch *pdispReserved)
283 {
284     DocHost *This = DOCHOSTUI_THIS(iface);
285     HRESULT hres;
286
287     TRACE("(%p)->(%d %p %p %p)\n", This, dwID, ppt, pcmdtReserved, pdispReserved);
288
289     if(This->hostui) {
290         hres = IDocHostUIHandler_ShowContextMenu(This->hostui, dwID, ppt, pcmdtReserved,
291                                                  pdispReserved);
292         if(hres == S_OK)
293             return S_OK;
294     }
295
296     FIXME("default action not implemented\n");
297     return E_NOTIMPL;
298 }
299
300 static HRESULT WINAPI DocHostUIHandler_GetHostInfo(IDocHostUIHandler2 *iface,
301         DOCHOSTUIINFO *pInfo)
302 {
303     DocHost *This = DOCHOSTUI_THIS(iface);
304     HRESULT hres;
305
306     TRACE("(%p)->(%p)\n", This, pInfo);
307
308     if(This->hostui) {
309         hres = IDocHostUIHandler_GetHostInfo(This->hostui, pInfo);
310         if(SUCCEEDED(hres))
311             return hres;
312     }
313
314     pInfo->dwFlags = DOCHOSTUIFLAG_DISABLE_HELP_MENU | DOCHOSTUIFLAG_OPENNEWWIN
315         | DOCHOSTUIFLAG_URL_ENCODING_ENABLE_UTF8 | DOCHOSTUIFLAG_ENABLE_INPLACE_NAVIGATION
316         | DOCHOSTUIFLAG_IME_ENABLE_RECONVERSION;
317     return S_OK;
318 }
319
320 static HRESULT WINAPI DocHostUIHandler_ShowUI(IDocHostUIHandler2 *iface, DWORD dwID,
321         IOleInPlaceActiveObject *pActiveObject, IOleCommandTarget *pCommandTarget,
322         IOleInPlaceFrame *pFrame, IOleInPlaceUIWindow *pDoc)
323 {
324     DocHost *This = DOCHOSTUI_THIS(iface);
325     FIXME("(%p)->(%d %p %p %p %p)\n", This, dwID, pActiveObject, pCommandTarget,
326           pFrame, pDoc);
327     return E_NOTIMPL;
328 }
329
330 static HRESULT WINAPI DocHostUIHandler_HideUI(IDocHostUIHandler2 *iface)
331 {
332     DocHost *This = DOCHOSTUI_THIS(iface);
333     FIXME("(%p)\n", This);
334     return E_NOTIMPL;
335 }
336
337 static HRESULT WINAPI DocHostUIHandler_UpdateUI(IDocHostUIHandler2 *iface)
338 {
339     DocHost *This = DOCHOSTUI_THIS(iface);
340     FIXME("(%p)\n", This);
341     return E_NOTIMPL;
342 }
343
344 static HRESULT WINAPI DocHostUIHandler_EnableModeless(IDocHostUIHandler2 *iface,
345                                                       BOOL fEnable)
346 {
347     DocHost *This = DOCHOSTUI_THIS(iface);
348     FIXME("(%p)->(%x)\n", This, fEnable);
349     return E_NOTIMPL;
350 }
351
352 static HRESULT WINAPI DocHostUIHandler_OnDocWindowActivate(IDocHostUIHandler2 *iface,
353                                                            BOOL fActivate)
354 {
355     DocHost *This = DOCHOSTUI_THIS(iface);
356     FIXME("(%p)->(%x)\n", This, fActivate);
357     return E_NOTIMPL;
358 }
359
360 static HRESULT WINAPI DocHostUIHandler_OnFrameWindowActivate(IDocHostUIHandler2 *iface,
361                                                              BOOL fActivate)
362 {
363     DocHost *This = DOCHOSTUI_THIS(iface);
364     FIXME("(%p)->(%x)\n", This, fActivate);
365     return E_NOTIMPL;
366 }
367
368 static HRESULT WINAPI DocHostUIHandler_ResizeBorder(IDocHostUIHandler2 *iface,
369         LPCRECT prcBorder, IOleInPlaceUIWindow *pUIWindow, BOOL fRameWindow)
370 {
371     DocHost *This = DOCHOSTUI_THIS(iface);
372     FIXME("(%p)->(%p %p %X)\n", This, prcBorder, pUIWindow, fRameWindow);
373     return E_NOTIMPL;
374 }
375
376 static HRESULT WINAPI DocHostUIHandler_TranslateAccelerator(IDocHostUIHandler2 *iface,
377         LPMSG lpMsg, const GUID *pguidCmdGroup, DWORD nCmdID)
378 {
379     DocHost *This = DOCHOSTUI_THIS(iface);
380     FIXME("(%p)->(%p %p %d)\n", This, lpMsg, pguidCmdGroup, nCmdID);
381     return E_NOTIMPL;
382 }
383
384 static HRESULT WINAPI DocHostUIHandler_GetOptionKeyPath(IDocHostUIHandler2 *iface,
385         LPOLESTR *pchKey, DWORD dw)
386 {
387     DocHost *This = DOCHOSTUI_THIS(iface);
388
389     TRACE("(%p)->(%p %d)\n", This, pchKey, dw);
390
391     if(This->hostui)
392         return IDocHostUIHandler_GetOptionKeyPath(This->hostui, pchKey, dw);
393
394     return S_OK;
395 }
396
397 static HRESULT WINAPI DocHostUIHandler_GetDropTarget(IDocHostUIHandler2 *iface,
398         IDropTarget *pDropTarget, IDropTarget **ppDropTarget)
399 {
400     DocHost *This = DOCHOSTUI_THIS(iface);
401     FIXME("(%p)\n", This);
402     return E_NOTIMPL;
403 }
404
405 static HRESULT WINAPI DocHostUIHandler_GetExternal(IDocHostUIHandler2 *iface,
406         IDispatch **ppDispatch)
407 {
408     DocHost *This = DOCHOSTUI_THIS(iface);
409     FIXME("(%p)->(%p)\n", This, ppDispatch);
410     return E_NOTIMPL;
411 }
412
413 static HRESULT WINAPI DocHostUIHandler_TranslateUrl(IDocHostUIHandler2 *iface,
414         DWORD dwTranslate, OLECHAR *pchURLIn, OLECHAR **ppchURLOut)
415 {
416     DocHost *This = DOCHOSTUI_THIS(iface);
417
418     TRACE("(%p)->(%d %s %p)\n", This, dwTranslate, debugstr_w(pchURLIn), ppchURLOut);
419
420     if(This->hostui)
421         return IDocHostUIHandler_TranslateUrl(This->hostui, dwTranslate,
422                                               pchURLIn, ppchURLOut);
423
424     return S_FALSE;
425 }
426
427 static HRESULT WINAPI DocHostUIHandler_FilterDataObject(IDocHostUIHandler2 *iface,
428         IDataObject *pDO, IDataObject **ppDORet)
429 {
430     DocHost *This = DOCHOSTUI_THIS(iface);
431     FIXME("(%p)->(%p %p)\n", This, pDO, ppDORet);
432     return E_NOTIMPL;
433 }
434
435 static HRESULT WINAPI DocHostUIHandler_GetOverrideKeyPath(IDocHostUIHandler2 *iface,
436         LPOLESTR *pchKey, DWORD dw)
437 {
438     DocHost *This = DOCHOSTUI_THIS(iface);
439     IDocHostUIHandler2 *handler;
440     HRESULT hres;
441
442     TRACE("(%p)->(%p %d)\n", This, pchKey, dw);
443
444     if(!This->hostui)
445         return S_OK;
446
447     hres = IDocHostUIHandler_QueryInterface(This->hostui, &IID_IDocHostUIHandler2,
448                                             (void**)&handler);
449     if(SUCCEEDED(hres)) {
450         hres = IDocHostUIHandler2_GetOverrideKeyPath(handler, pchKey, dw);
451         IDocHostUIHandler2_Release(handler);
452         return hres;
453     }
454
455     return S_OK;
456 }
457
458 #undef DOCHOSTUI_THIS
459
460 static const IDocHostUIHandler2Vtbl DocHostUIHandler2Vtbl = {
461     DocHostUIHandler_QueryInterface,
462     DocHostUIHandler_AddRef,
463     DocHostUIHandler_Release,
464     DocHostUIHandler_ShowContextMenu,
465     DocHostUIHandler_GetHostInfo,
466     DocHostUIHandler_ShowUI,
467     DocHostUIHandler_HideUI,
468     DocHostUIHandler_UpdateUI,
469     DocHostUIHandler_EnableModeless,
470     DocHostUIHandler_OnDocWindowActivate,
471     DocHostUIHandler_OnFrameWindowActivate,
472     DocHostUIHandler_ResizeBorder,
473     DocHostUIHandler_TranslateAccelerator,
474     DocHostUIHandler_GetOptionKeyPath,
475     DocHostUIHandler_GetDropTarget,
476     DocHostUIHandler_GetExternal,
477     DocHostUIHandler_TranslateUrl,
478     DocHostUIHandler_FilterDataObject,
479     DocHostUIHandler_GetOverrideKeyPath
480 };
481
482 void DocHost_Init(DocHost *This, IDispatch *disp)
483 {
484     This->lpDocHostUIHandlerVtbl = &DocHostUIHandler2Vtbl;
485     This->lpOleCommandTargetVtbl = &OleCommandTargetVtbl;
486
487     This->disp = disp;
488
489     This->document = NULL;
490     This->hostui = NULL;
491
492     This->hwnd = NULL;
493     This->frame_hwnd = NULL;
494     This->url = NULL;
495
496     DocHost_ClientSite_Init(This);
497     DocHost_Frame_Init(This);
498
499     ConnectionPointContainer_Init(&This->cps, (IUnknown*)disp);
500 }
501
502 void DocHost_Release(DocHost *This)
503 {
504     DocHost_ClientSite_Release(This);
505
506     ConnectionPointContainer_Destroy(&This->cps);
507
508     SysFreeString(This->url);
509 }