shell32/tests: Avoid SHDeleteKeyA() because shlwapi.dll is missing on Windows 95.
[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
341     TRACE("(%p)\n", This);
342
343     if(!This->hostui)
344         return S_FALSE;
345
346     return IDocHostUIHandler_UpdateUI(This->hostui);
347 }
348
349 static HRESULT WINAPI DocHostUIHandler_EnableModeless(IDocHostUIHandler2 *iface,
350                                                       BOOL fEnable)
351 {
352     DocHost *This = DOCHOSTUI_THIS(iface);
353     FIXME("(%p)->(%x)\n", This, fEnable);
354     return E_NOTIMPL;
355 }
356
357 static HRESULT WINAPI DocHostUIHandler_OnDocWindowActivate(IDocHostUIHandler2 *iface,
358                                                            BOOL fActivate)
359 {
360     DocHost *This = DOCHOSTUI_THIS(iface);
361     FIXME("(%p)->(%x)\n", This, fActivate);
362     return E_NOTIMPL;
363 }
364
365 static HRESULT WINAPI DocHostUIHandler_OnFrameWindowActivate(IDocHostUIHandler2 *iface,
366                                                              BOOL fActivate)
367 {
368     DocHost *This = DOCHOSTUI_THIS(iface);
369     FIXME("(%p)->(%x)\n", This, fActivate);
370     return E_NOTIMPL;
371 }
372
373 static HRESULT WINAPI DocHostUIHandler_ResizeBorder(IDocHostUIHandler2 *iface,
374         LPCRECT prcBorder, IOleInPlaceUIWindow *pUIWindow, BOOL fRameWindow)
375 {
376     DocHost *This = DOCHOSTUI_THIS(iface);
377     FIXME("(%p)->(%p %p %X)\n", This, prcBorder, pUIWindow, fRameWindow);
378     return E_NOTIMPL;
379 }
380
381 static HRESULT WINAPI DocHostUIHandler_TranslateAccelerator(IDocHostUIHandler2 *iface,
382         LPMSG lpMsg, const GUID *pguidCmdGroup, DWORD nCmdID)
383 {
384     DocHost *This = DOCHOSTUI_THIS(iface);
385     FIXME("(%p)->(%p %p %d)\n", This, lpMsg, pguidCmdGroup, nCmdID);
386     return E_NOTIMPL;
387 }
388
389 static HRESULT WINAPI DocHostUIHandler_GetOptionKeyPath(IDocHostUIHandler2 *iface,
390         LPOLESTR *pchKey, DWORD dw)
391 {
392     DocHost *This = DOCHOSTUI_THIS(iface);
393
394     TRACE("(%p)->(%p %d)\n", This, pchKey, dw);
395
396     if(This->hostui)
397         return IDocHostUIHandler_GetOptionKeyPath(This->hostui, pchKey, dw);
398
399     return S_OK;
400 }
401
402 static HRESULT WINAPI DocHostUIHandler_GetDropTarget(IDocHostUIHandler2 *iface,
403         IDropTarget *pDropTarget, IDropTarget **ppDropTarget)
404 {
405     DocHost *This = DOCHOSTUI_THIS(iface);
406     FIXME("(%p)\n", This);
407     return E_NOTIMPL;
408 }
409
410 static HRESULT WINAPI DocHostUIHandler_GetExternal(IDocHostUIHandler2 *iface,
411         IDispatch **ppDispatch)
412 {
413     DocHost *This = DOCHOSTUI_THIS(iface);
414
415     TRACE("(%p)->(%p)\n", This, ppDispatch);
416
417     if(This->hostui)
418         return IDocHostUIHandler_GetExternal(This->hostui, ppDispatch);
419
420     FIXME("default action not implemented\n");
421     return E_NOTIMPL;
422 }
423
424 static HRESULT WINAPI DocHostUIHandler_TranslateUrl(IDocHostUIHandler2 *iface,
425         DWORD dwTranslate, OLECHAR *pchURLIn, OLECHAR **ppchURLOut)
426 {
427     DocHost *This = DOCHOSTUI_THIS(iface);
428
429     TRACE("(%p)->(%d %s %p)\n", This, dwTranslate, debugstr_w(pchURLIn), ppchURLOut);
430
431     if(This->hostui)
432         return IDocHostUIHandler_TranslateUrl(This->hostui, dwTranslate,
433                                               pchURLIn, ppchURLOut);
434
435     return S_FALSE;
436 }
437
438 static HRESULT WINAPI DocHostUIHandler_FilterDataObject(IDocHostUIHandler2 *iface,
439         IDataObject *pDO, IDataObject **ppDORet)
440 {
441     DocHost *This = DOCHOSTUI_THIS(iface);
442     FIXME("(%p)->(%p %p)\n", This, pDO, ppDORet);
443     return E_NOTIMPL;
444 }
445
446 static HRESULT WINAPI DocHostUIHandler_GetOverrideKeyPath(IDocHostUIHandler2 *iface,
447         LPOLESTR *pchKey, DWORD dw)
448 {
449     DocHost *This = DOCHOSTUI_THIS(iface);
450     IDocHostUIHandler2 *handler;
451     HRESULT hres;
452
453     TRACE("(%p)->(%p %d)\n", This, pchKey, dw);
454
455     if(!This->hostui)
456         return S_OK;
457
458     hres = IDocHostUIHandler_QueryInterface(This->hostui, &IID_IDocHostUIHandler2,
459                                             (void**)&handler);
460     if(SUCCEEDED(hres)) {
461         hres = IDocHostUIHandler2_GetOverrideKeyPath(handler, pchKey, dw);
462         IDocHostUIHandler2_Release(handler);
463         return hres;
464     }
465
466     return S_OK;
467 }
468
469 #undef DOCHOSTUI_THIS
470
471 static const IDocHostUIHandler2Vtbl DocHostUIHandler2Vtbl = {
472     DocHostUIHandler_QueryInterface,
473     DocHostUIHandler_AddRef,
474     DocHostUIHandler_Release,
475     DocHostUIHandler_ShowContextMenu,
476     DocHostUIHandler_GetHostInfo,
477     DocHostUIHandler_ShowUI,
478     DocHostUIHandler_HideUI,
479     DocHostUIHandler_UpdateUI,
480     DocHostUIHandler_EnableModeless,
481     DocHostUIHandler_OnDocWindowActivate,
482     DocHostUIHandler_OnFrameWindowActivate,
483     DocHostUIHandler_ResizeBorder,
484     DocHostUIHandler_TranslateAccelerator,
485     DocHostUIHandler_GetOptionKeyPath,
486     DocHostUIHandler_GetDropTarget,
487     DocHostUIHandler_GetExternal,
488     DocHostUIHandler_TranslateUrl,
489     DocHostUIHandler_FilterDataObject,
490     DocHostUIHandler_GetOverrideKeyPath
491 };
492
493 void DocHost_Init(DocHost *This, IDispatch *disp)
494 {
495     This->lpDocHostUIHandlerVtbl = &DocHostUIHandler2Vtbl;
496     This->lpOleCommandTargetVtbl = &OleCommandTargetVtbl;
497
498     This->disp = disp;
499
500     This->client_disp = NULL;
501
502     This->document = NULL;
503     This->hostui = NULL;
504     This->frame = NULL;
505
506     This->hwnd = NULL;
507     This->frame_hwnd = NULL;
508     This->url = NULL;
509
510     This->silent = VARIANT_FALSE;
511     This->offline = VARIANT_FALSE;
512
513     DocHost_ClientSite_Init(This);
514     DocHost_Frame_Init(This);
515
516     ConnectionPointContainer_Init(&This->cps, (IUnknown*)disp);
517 }
518
519 void DocHost_Release(DocHost *This)
520 {
521     if(This->client_disp)
522         IDispatch_Release(This->client_disp);
523     if(This->frame)
524         IOleInPlaceFrame_Release(This->frame);
525
526     DocHost_ClientSite_Release(This);
527
528     ConnectionPointContainer_Destroy(&This->cps);
529
530     SysFreeString(This->url);
531 }