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