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