Document active object and variant functions.
[wine] / dlls / mshtml / view.c
1 /*
2  * Copyright 2005 Jacek Caban
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 "config.h"
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "wingdi.h"
30 #include "ole2.h"
31 #include "docobj.h"
32
33 #include "mshtml.h"
34
35 #include "wine/debug.h"
36
37 #include "mshtml_private.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
40
41 static const WCHAR wszInternetExplorer_Server[] =
42     {'I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r','_','S','e','r','v','e','r',0};
43 static const WCHAR wszHTML_Document[] =
44     {'H','T','M','L',' ','D','o','c','u','m','e','n','t',0};
45
46 static ATOM serverwnd_class = 0;
47
48 static void paint_disabled(HWND hwnd) {
49     HDC hdc;
50     PAINTSTRUCT ps;
51     HBRUSH brush;
52     RECT rect;
53     HFONT font;
54
55     font = CreateFontA(25,0,0,0,400,0,0,0,ANSI_CHARSET,0,0,DEFAULT_QUALITY,DEFAULT_PITCH,NULL);
56     brush = CreateSolidBrush(RGB(255,255,255));
57     GetClientRect(hwnd, &rect);
58
59     hdc = BeginPaint(hwnd, &ps);
60     SelectObject(hdc, font);
61     SelectObject(hdc, brush);
62     Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);
63     DrawTextA(hdc, "HTML rendering is currently disabled.",-1, &rect,
64             DT_CENTER | DT_SINGLELINE | DT_VCENTER);
65     EndPaint(hwnd, &ps);
66
67     DeleteObject(font);
68     DeleteObject(brush);
69 }
70
71 static LRESULT WINAPI serverwnd_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
72 {
73     if(msg == WM_PAINT)
74         paint_disabled(hwnd);
75         
76     return DefWindowProcW(hwnd, msg, wParam, lParam);
77 }
78
79 static void register_serverwnd_class(void)
80 {
81     static WNDCLASSEXW wndclass = {
82         sizeof(WNDCLASSEXW),
83         CS_DBLCLKS,
84         serverwnd_proc,
85         0, 0, NULL, NULL, NULL, NULL, NULL,
86         wszInternetExplorer_Server,
87         NULL,
88     };
89     wndclass.hInstance = hInst;
90     serverwnd_class = RegisterClassExW(&wndclass);
91 }
92
93
94 /**********************************************************
95  * IOleDocumentView implementation
96  */
97
98 #define DOCVIEW_THIS \
99         HTMLDocument* const This=(HTMLDocument*)((char*)(iface)-offsetof(HTMLDocument,lpOleDocumentViewVtbl));
100
101 static HRESULT WINAPI OleDocumentView_QueryInterface(IOleDocumentView *iface, REFIID riid, void **ppvObject)
102 {
103     DOCVIEW_THIS
104     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
105 }
106
107 static ULONG WINAPI OleDocumentView_AddRef(IOleDocumentView *iface)
108 {
109     DOCVIEW_THIS
110     return IHTMLDocument2_AddRef(HTMLDOC(This));
111 }
112
113 static ULONG WINAPI OleDocumentView_Release(IOleDocumentView *iface)
114 {
115     DOCVIEW_THIS
116     return IHTMLDocument2_Release(HTMLDOC(This));
117 }
118
119 static HRESULT WINAPI OleDocumentView_SetInPlaceSite(IOleDocumentView *iface, IOleInPlaceSite *pIPSite)
120 {
121     DOCVIEW_THIS
122     TRACE("(%p)->(%p)\n", This, pIPSite);
123
124     if(!pIPSite)
125         return E_INVALIDARG;
126
127     if(pIPSite)
128         IOleInPlaceSite_AddRef(pIPSite);
129
130     if(This->ipsite)
131         IOleInPlaceSite_Release(This->ipsite);
132
133     This->ipsite = pIPSite;
134     return S_OK;
135 }
136
137 static HRESULT WINAPI OleDocumentView_GetInPlaceSite(IOleDocumentView *iface, IOleInPlaceSite **ppIPSite)
138 {
139     DOCVIEW_THIS
140     TRACE("(%p)->(%p)\n", This, ppIPSite);
141
142     if(!ppIPSite)
143         return E_INVALIDARG;
144
145     if(This->ipsite)
146         IOleInPlaceSite_AddRef(This->ipsite);
147
148     *ppIPSite = This->ipsite;
149     return S_OK;
150 }
151
152 static HRESULT WINAPI OleDocumentView_GetDocument(IOleDocumentView *iface, IUnknown **ppunk)
153 {
154     DOCVIEW_THIS
155     TRACE("(%p)->(%p)\n", This, ppunk);
156
157     if(!ppunk)
158         return E_INVALIDARG;
159
160     IHTMLDocument2_AddRef(HTMLDOC(This));
161     *ppunk = (IUnknown*)HTMLDOC(This);
162     return S_OK;
163 }
164
165 static HRESULT WINAPI OleDocumentView_SetRect(IOleDocumentView *iface, LPRECT prcView)
166 {
167     DOCVIEW_THIS
168     RECT rect;
169
170     TRACE("(%p)->(%p)\n", This, prcView);
171
172     if(!prcView)
173         return E_INVALIDARG;
174
175     if(This->hwnd) {
176         GetClientRect(This->hwnd, &rect);
177         if(memcmp(prcView, &rect, sizeof(RECT))) {
178             InvalidateRect(This->hwnd,NULL,TRUE);
179             SetWindowPos(This->hwnd, NULL, prcView->left, prcView->top, prcView->right,
180                     prcView->bottom, SWP_NOZORDER | SWP_NOACTIVATE);
181         }
182     }
183     
184     return S_OK;
185 }
186
187 static HRESULT WINAPI OleDocumentView_GetRect(IOleDocumentView *iface, LPRECT prcView)
188 {
189     DOCVIEW_THIS
190
191     TRACE("(%p)->(%p)\n", This, prcView);
192
193     if(!prcView)
194         return E_INVALIDARG;
195
196     GetClientRect(This->hwnd, prcView);
197     return S_OK;
198 }
199
200 static HRESULT WINAPI OleDocumentView_SetRectComplex(IOleDocumentView *iface, LPRECT prcView,
201                         LPRECT prcHScroll, LPRECT prcVScroll, LPRECT prcSizeBox)
202 {
203     DOCVIEW_THIS
204     FIXME("(%p)->(%p %p %p %p)\n", This, prcView, prcHScroll, prcVScroll, prcSizeBox);
205     return E_NOTIMPL;
206 }
207
208 static HRESULT WINAPI OleDocumentView_Show(IOleDocumentView *iface, BOOL fShow)
209 {
210     DOCVIEW_THIS
211     TRACE("(%p)->(%x)\n", This, fShow);
212
213     if(This->hwnd)
214         ShowWindow(This->hwnd, fShow);
215
216     return S_OK;
217 }
218
219 static HRESULT WINAPI OleDocumentView_UIActivate(IOleDocumentView *iface, BOOL fUIActivate)
220 {
221     DOCVIEW_THIS
222     HRESULT hres;
223     IOleInPlaceUIWindow *pIPWnd;
224     IOleInPlaceFrame *pIPFrame;
225     RECT posrect, cliprect;
226     OLEINPLACEFRAMEINFO frameinfo;
227     HWND parent_hwnd, hwnd;
228
229     TRACE("(%p)->(%x)\n", This, fUIActivate);
230
231     if(!This->ipsite) {
232         FIXME("This->ipsite = NULL\n");
233         return E_FAIL;
234     }
235
236     if(fUIActivate) {
237         if(This->hwnd)
238             return S_OK;
239         if(!serverwnd_class)
240             register_serverwnd_class();
241
242         hres = IOleInPlaceSite_CanInPlaceActivate(This->ipsite);
243         if(hres != S_OK) {
244             WARN("CanInPlaceActivate returned: %08lx\n", hres);
245             return FAILED(hres) ? hres : E_FAIL;
246         }
247
248         hres = IOleInPlaceSite_GetWindowContext(This->ipsite, &pIPFrame, &pIPWnd, &posrect, &cliprect, &frameinfo);
249         if(FAILED(hres)) {
250             WARN("GetWindowContext failed: %08lx\n", hres);
251             return hres;
252         }
253         if(pIPWnd)
254             IOleInPlaceUIWindow_Release(pIPWnd);
255         TRACE("got window context: %p %p {%ld %ld %ld %ld} {%ld %ld %ld %ld} {%d %x %p %p %d}\n",
256                 pIPFrame, pIPWnd, posrect.left, posrect.top, posrect.right, posrect.bottom,
257                 cliprect.left, cliprect.top, cliprect.right, cliprect.bottom,
258                 frameinfo.cb, frameinfo.fMDIApp, frameinfo.hwndFrame, frameinfo.haccel, frameinfo.cAccelEntries);
259
260         hres = IOleInPlaceSite_GetWindow(This->ipsite, &parent_hwnd);
261         if(FAILED(hres)) {
262             WARN("GetWindow failed: %08lx\n", hres);
263             return hres;
264         }
265
266         hwnd = CreateWindowExW(0, wszInternetExplorer_Server, NULL,
267                 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
268                 posrect.left, posrect.top, posrect.right-posrect.left, posrect.bottom-posrect.top,
269                 parent_hwnd, NULL, hInst, This);
270
271         hres = IOleInPlaceSite_OnInPlaceActivate(This->ipsite);
272         if(FAILED(hres)) {
273             WARN("OnInPlaceActivate failed: %08lx\n", hres);
274             return hres;
275         }
276
277         SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
278                 SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW | SWP_NOACTIVATE | SWP_SHOWWINDOW);
279         RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE | RDW_NOERASE | RDW_ALLCHILDREN);
280         SetFocus(hwnd);
281
282         /* NOTE:
283          * Windows implementation calls:
284          * RegisterWindowMessage("MSWHEEL_ROLLMSG");
285          * SetTimer(This->hwnd, TIMER_ID, 100, NULL);
286          */
287
288         hres = IOleInPlaceSite_OnUIActivate(This->ipsite);
289         if(SUCCEEDED(hres)) {
290             IOleInPlaceFrame_SetActiveObject(pIPFrame, ACTOBJ(This), wszHTML_Document);
291         }else {
292             FIXME("OnUIActivate failed: %08lx\n", hres);
293             DestroyWindow(hwnd);
294             return hres;
295         }
296         if(This->frame)
297             IOleInPlaceFrame_Release(This->frame);
298         This->frame = pIPFrame;
299         This->hwnd = hwnd;
300     }else {
301         static const WCHAR wszEmpty[] = {0};
302     
303         if(This->frame)
304             IOleInPlaceFrame_SetActiveObject(This->frame, NULL, wszEmpty);
305         if(This->ipsite)
306             IOleInPlaceSite_OnUIDeactivate(This->ipsite, FALSE);
307     }
308     return S_OK;
309 }
310
311 static HRESULT WINAPI OleDocumentView_Open(IOleDocumentView *iface)
312 {
313     DOCVIEW_THIS
314     FIXME("(%p)\n", This);
315     return E_NOTIMPL;
316 }
317
318 static HRESULT WINAPI OleDocumentView_CloseView(IOleDocumentView *iface, DWORD dwReserved)
319 {
320     DOCVIEW_THIS
321     TRACE("(%p)->(%lx)\n", This, dwReserved);
322
323     if(dwReserved)
324         WARN("dwReserved = %ld\n", dwReserved);
325
326     /* NOTE:
327      * Windows implementation calls QueryInterface(IID_IOleCommandTarget),
328      * QueryInterface(IID_IOleControlSite) and KillTimer
329      */
330
331     IOleDocumentView_Show(iface, FALSE);
332
333     return S_OK;
334 }
335
336 static HRESULT WINAPI OleDocumentView_SaveViewState(IOleDocumentView *iface, LPSTREAM pstm)
337 {
338     DOCVIEW_THIS
339     FIXME("(%p)->(%p)\n", This, pstm);
340     return E_NOTIMPL;
341 }
342
343 static HRESULT WINAPI OleDocumentView_ApplyViewState(IOleDocumentView *iface, LPSTREAM pstm)
344 {
345     DOCVIEW_THIS
346     FIXME("(%p)->(%p)\n", This, pstm);
347     return E_NOTIMPL;
348 }
349
350 static HRESULT WINAPI OleDocumentView_Clone(IOleDocumentView *iface, IOleInPlaceSite *pIPSiteNew,
351                                         IOleDocumentView **ppViewNew)
352 {
353     DOCVIEW_THIS
354     FIXME("(%p)->(%p %p)\n", This, pIPSiteNew, ppViewNew);
355     return E_NOTIMPL;
356 }
357
358 static const IOleDocumentViewVtbl OleDocumentViewVtbl = {
359     OleDocumentView_QueryInterface,
360     OleDocumentView_AddRef,
361     OleDocumentView_Release,
362     OleDocumentView_SetInPlaceSite,
363     OleDocumentView_GetInPlaceSite,
364     OleDocumentView_GetDocument,
365     OleDocumentView_SetRect,
366     OleDocumentView_GetRect,
367     OleDocumentView_SetRectComplex,
368     OleDocumentView_Show,
369     OleDocumentView_UIActivate,
370     OleDocumentView_Open,
371     OleDocumentView_CloseView,
372     OleDocumentView_SaveViewState,
373     OleDocumentView_ApplyViewState,
374     OleDocumentView_Clone
375 };
376
377 /**********************************************************
378  * IViewObject implementation
379  */
380
381 #define VIEWOBJ_THIS \
382     HTMLDocument* const This=(HTMLDocument*)((char*)(iface)-offsetof(HTMLDocument,lpViewObject2Vtbl));
383
384 static HRESULT WINAPI ViewObject_QueryInterface(IViewObject2 *iface, REFIID riid, void **ppvObject)
385 {
386     VIEWOBJ_THIS
387     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
388 }
389
390 static ULONG WINAPI ViewObject_AddRef(IViewObject2 *iface)
391 {
392     VIEWOBJ_THIS
393     return IHTMLDocument2_AddRef(HTMLDOC(This));
394 }
395
396 static ULONG WINAPI ViewObject_Release(IViewObject2 *iface)
397 {
398     VIEWOBJ_THIS
399     return IHTMLDocument2_Release(HTMLDOC(This));
400 }
401
402 static HRESULT WINAPI ViewObject_Draw(IViewObject2 *iface, DWORD dwDrawAspect, LONG lindex, void *pvAspect,
403         DVTARGETDEVICE *ptd, HDC hdcTargetDev, HDC hdcDraw, LPCRECTL lprcBounds,
404         LPCRECTL lprcWBounds, BOOL (CALLBACK *pfnContinue)(ULONG_PTR dwContinue), ULONG_PTR dwContinue)
405 {
406     VIEWOBJ_THIS
407     FIXME("(%p)->(%ld %ld %p %p %p %p %p %p %p %ld)\n", This, dwDrawAspect, lindex, pvAspect,
408             ptd, hdcTargetDev, hdcDraw, lprcBounds, lprcWBounds, pfnContinue, dwContinue);
409     return E_NOTIMPL;
410 }
411
412 static HRESULT WINAPI ViewObject_GetColorSet(IViewObject2 *iface, DWORD dwDrawAspect, LONG lindex, void *pvAspect,
413         DVTARGETDEVICE *ptd, HDC hicTargetDev, LOGPALETTE **ppColorSet)
414 {
415     VIEWOBJ_THIS
416     FIXME("(%p)->(%ld %ld %p %p %p %p)\n", This, dwDrawAspect, lindex, pvAspect, ptd, hicTargetDev, ppColorSet);
417     return E_NOTIMPL;
418 }
419
420 static HRESULT WINAPI ViewObject_Freeze(IViewObject2 *iface, DWORD dwDrawAspect, LONG lindex,
421         void *pvAspect, DWORD *pdwFreeze)
422 {
423     VIEWOBJ_THIS
424     FIXME("(%p)->(%ld %ld %p %p)\n", This, dwDrawAspect, lindex, pvAspect, pdwFreeze);
425     return E_NOTIMPL;
426 }
427
428 static HRESULT WINAPI ViewObject_Unfreeze(IViewObject2 *iface, DWORD dwFreeze)
429 {
430     VIEWOBJ_THIS
431     FIXME("(%p)->(%ld)\n", This, dwFreeze);
432     return E_NOTIMPL;
433 }
434
435 static HRESULT WINAPI ViewObject_SetAdvise(IViewObject2 *iface, DWORD aspects, DWORD advf, IAdviseSink *pAdvSink)
436 {
437     VIEWOBJ_THIS
438     FIXME("(%p)->(%ld %ld %p)\n", This, aspects, advf, pAdvSink);
439     return E_NOTIMPL;
440 }
441
442 static HRESULT WINAPI ViewObject_GetAdvise(IViewObject2 *iface, DWORD *pAspects, DWORD *pAdvf, IAdviseSink **ppAdvSink)
443 {
444     VIEWOBJ_THIS
445     FIXME("(%p)->(%p %p %p)\n", This, pAspects, pAdvf, ppAdvSink);
446     return E_NOTIMPL;
447 }
448
449 static HRESULT WINAPI ViewObject_GetExtent(IViewObject2 *iface, DWORD dwDrawAspect, LONG lindex,
450                                 DVTARGETDEVICE* ptd, LPSIZEL lpsizel)
451 {
452     VIEWOBJ_THIS
453     FIXME("(%p)->(%ld %ld %p %p)\n", This, dwDrawAspect, lindex, ptd, lpsizel);
454     return E_NOTIMPL;
455 }
456
457 static const IViewObject2Vtbl ViewObjectVtbl = {
458     ViewObject_QueryInterface,
459     ViewObject_AddRef,
460     ViewObject_Release,
461     ViewObject_Draw,
462     ViewObject_GetColorSet,
463     ViewObject_Freeze,
464     ViewObject_Unfreeze,
465     ViewObject_SetAdvise,
466     ViewObject_GetAdvise,
467     ViewObject_GetExtent
468 };
469
470 void HTMLDocument_View_Init(HTMLDocument *This)
471 {
472     This->lpOleDocumentViewVtbl = &OleDocumentViewVtbl;
473     This->lpViewObject2Vtbl = &ViewObjectVtbl;
474
475     This->ipsite = NULL;
476     This->frame = NULL;
477     This->hwnd = NULL;
478 }