- Added IOleCommandTerget interface to the test.
[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
32 #include "wine/debug.h"
33
34 #include "mshtml_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 static const WCHAR wszInternetExplorer_Server[] =
39     {'I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r','_','S','e','r','v','e','r',0};
40 static const WCHAR wszHTML_Document[] =
41     {'H','T','M','L',' ','D','o','c','u','m','e','n','t',0};
42
43 static ATOM serverwnd_class = 0;
44
45 static void paint_disabled(HWND hwnd) {
46     HDC hdc;
47     PAINTSTRUCT ps;
48     HBRUSH brush;
49     RECT rect;
50     HFONT font;
51
52     font = CreateFontA(25,0,0,0,400,0,0,0,ANSI_CHARSET,0,0,DEFAULT_QUALITY,DEFAULT_PITCH,NULL);
53     brush = CreateSolidBrush(RGB(255,255,255));
54     GetClientRect(hwnd, &rect);
55
56     hdc = BeginPaint(hwnd, &ps);
57     SelectObject(hdc, font);
58     SelectObject(hdc, brush);
59     Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);
60     DrawTextA(hdc, "HTML rendering is currently disabled.",-1, &rect,
61             DT_CENTER | DT_SINGLELINE | DT_VCENTER);
62     EndPaint(hwnd, &ps);
63
64     DeleteObject(font);
65     DeleteObject(brush);
66 }
67
68 static void activate_gecko(HTMLDocument *This)
69 {
70     TRACE("(%p) %p\n", This, This->nscontainer->window);
71
72     SetParent(This->nscontainer->hwnd, This->hwnd);
73     ShowWindow(This->nscontainer->hwnd, SW_SHOW);
74
75     nsIBaseWindow_SetVisibility(This->nscontainer->window, TRUE);
76     nsIBaseWindow_SetEnabled(This->nscontainer->window, TRUE);
77 }
78
79 static LRESULT WINAPI serverwnd_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
80 {
81     HTMLDocument *This;
82
83     static const WCHAR wszTHIS[] = {'T','H','I','S',0};
84
85     if(msg == WM_CREATE) {
86         This = *(HTMLDocument**)lParam;
87         SetPropW(hwnd, wszTHIS, This);
88     }else {
89         This = (HTMLDocument*)GetPropW(hwnd, wszTHIS);
90     }
91
92     switch(msg) {
93     case WM_CREATE:
94         This->hwnd = hwnd;
95         if(This->nscontainer)
96             activate_gecko(This);
97         break;
98     case WM_PAINT:
99         if(!This->nscontainer)
100             paint_disabled(hwnd);
101         break;
102     case WM_SIZE:
103         TRACE("(%p)->(WM_SIZE)\n", This);
104         if(This->nscontainer)
105             SetWindowPos(This->nscontainer->hwnd, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam),
106                     SWP_NOZORDER | SWP_NOACTIVATE);
107     }
108         
109     return DefWindowProcW(hwnd, msg, wParam, lParam);
110 }
111
112 static void register_serverwnd_class(void)
113 {
114     static WNDCLASSEXW wndclass = {
115         sizeof(WNDCLASSEXW),
116         CS_DBLCLKS,
117         serverwnd_proc,
118         0, 0, NULL, NULL, NULL, NULL, NULL,
119         wszInternetExplorer_Server,
120         NULL,
121     };
122     wndclass.hInstance = hInst;
123     serverwnd_class = RegisterClassExW(&wndclass);
124 }
125
126 static HRESULT activate_window(HTMLDocument *This)
127 {
128     IOleInPlaceUIWindow *pIPWnd;
129     IOleInPlaceFrame *pIPFrame;
130     IOleCommandTarget *cmdtrg;
131     RECT posrect, cliprect;
132     OLEINPLACEFRAMEINFO frameinfo;
133     HWND parent_hwnd;
134     HRESULT hres;
135
136     if(!serverwnd_class)
137         register_serverwnd_class();
138
139     hres = IOleInPlaceSite_CanInPlaceActivate(This->ipsite);
140     if(hres != S_OK) {
141         WARN("CanInPlaceActivate returned: %08lx\n", hres);
142         return FAILED(hres) ? hres : E_FAIL;
143     }
144
145     hres = IOleInPlaceSite_GetWindowContext(This->ipsite, &pIPFrame, &pIPWnd, &posrect, &cliprect, &frameinfo);
146     if(FAILED(hres)) {
147         WARN("GetWindowContext failed: %08lx\n", hres);
148         return hres;
149     }
150     if(pIPWnd)
151         IOleInPlaceUIWindow_Release(pIPWnd);
152     TRACE("got window context: %p %p {%ld %ld %ld %ld} {%ld %ld %ld %ld} {%d %x %p %p %d}\n",
153             pIPFrame, pIPWnd, posrect.left, posrect.top, posrect.right, posrect.bottom,
154             cliprect.left, cliprect.top, cliprect.right, cliprect.bottom,
155             frameinfo.cb, frameinfo.fMDIApp, frameinfo.hwndFrame, frameinfo.haccel, frameinfo.cAccelEntries);
156
157     hres = IOleInPlaceSite_GetWindow(This->ipsite, &parent_hwnd);
158     if(FAILED(hres)) {
159         WARN("GetWindow failed: %08lx\n", hres);
160         return hres;
161     }
162
163     TRACE("got parent window %p\n", parent_hwnd);
164
165     if(This->hwnd) {
166         if(GetParent(This->hwnd) != parent_hwnd)
167             SetParent(This->hwnd, parent_hwnd);
168         SetWindowPos(This->hwnd, HWND_TOP,
169                 posrect.left, posrect.top, posrect.right-posrect.left, posrect.bottom-posrect.top,
170                 SWP_NOACTIVATE | SWP_SHOWWINDOW);
171     }else {
172         CreateWindowExW(0, wszInternetExplorer_Server, NULL,
173                 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
174                 posrect.left, posrect.top, posrect.right-posrect.left, posrect.bottom-posrect.top,
175                 parent_hwnd, NULL, hInst, This);
176
177         TRACE("Created window %p\n", This->hwnd);
178
179         SetWindowPos(This->hwnd, NULL, 0, 0, 0, 0,
180                 SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW | SWP_NOACTIVATE | SWP_SHOWWINDOW);
181         RedrawWindow(This->hwnd, NULL, NULL, RDW_INVALIDATE | RDW_NOERASE | RDW_ALLCHILDREN);
182         SetFocus(This->hwnd);
183
184         /* NOTE:
185          * Windows implementation calls:
186          * RegisterWindowMessage("MSWHEEL_ROLLMSG");
187          * SetTimer(This->hwnd, TIMER_ID, 100, NULL);
188          */
189     }
190
191     This->in_place_active = TRUE;
192     hres = IOleInPlaceSite_OnInPlaceActivate(This->ipsite);
193     if(FAILED(hres)) {
194         WARN("OnInPlaceActivate failed: %08lx\n", hres);
195         This->in_place_active = FALSE;
196         return hres;
197     }
198
199     hres = IOleClientSite_QueryInterface(This->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
200     if(SUCCEEDED(hres)) {
201         VARIANT var;
202
203         IOleInPlaceFrame_SetStatusText(pIPFrame, NULL);
204
205         V_VT(&var) = VT_I4;
206         V_I4(&var) = 0;
207         IOleCommandTarget_Exec(cmdtrg, NULL, OLECMDID_SETPROGRESSMAX, 0, &var, NULL);
208         IOleCommandTarget_Exec(cmdtrg, NULL, OLECMDID_SETPROGRESSPOS, 0, &var, NULL);
209
210         IOleCommandTarget_Release(cmdtrg);
211     }
212
213     if(This->frame)
214         IOleInPlaceFrame_Release(This->frame);
215     This->frame = pIPFrame;
216
217     This->window_active = TRUE;
218
219     return S_OK;
220 }
221
222 /**********************************************************
223  * IOleDocumentView implementation
224  */
225
226 #define DOCVIEW_THIS(iface) DEFINE_THIS(HTMLDocument, OleDocumentView, iface)
227
228 static HRESULT WINAPI OleDocumentView_QueryInterface(IOleDocumentView *iface, REFIID riid, void **ppvObject)
229 {
230     HTMLDocument *This = DOCVIEW_THIS(iface);
231     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
232 }
233
234 static ULONG WINAPI OleDocumentView_AddRef(IOleDocumentView *iface)
235 {
236     HTMLDocument *This = DOCVIEW_THIS(iface);
237     return IHTMLDocument2_AddRef(HTMLDOC(This));
238 }
239
240 static ULONG WINAPI OleDocumentView_Release(IOleDocumentView *iface)
241 {
242     HTMLDocument *This = DOCVIEW_THIS(iface);
243     return IHTMLDocument2_Release(HTMLDOC(This));
244 }
245
246 static HRESULT WINAPI OleDocumentView_SetInPlaceSite(IOleDocumentView *iface, IOleInPlaceSite *pIPSite)
247 {
248     HTMLDocument *This = DOCVIEW_THIS(iface);
249     TRACE("(%p)->(%p)\n", This, pIPSite);
250
251     if(pIPSite)
252         IOleInPlaceSite_AddRef(pIPSite);
253
254     if(This->ipsite)
255         IOleInPlaceSite_Release(This->ipsite);
256
257     This->ipsite = pIPSite;
258     return S_OK;
259 }
260
261 static HRESULT WINAPI OleDocumentView_GetInPlaceSite(IOleDocumentView *iface, IOleInPlaceSite **ppIPSite)
262 {
263     HTMLDocument *This = DOCVIEW_THIS(iface);
264     TRACE("(%p)->(%p)\n", This, ppIPSite);
265
266     if(!ppIPSite)
267         return E_INVALIDARG;
268
269     if(This->ipsite)
270         IOleInPlaceSite_AddRef(This->ipsite);
271
272     *ppIPSite = This->ipsite;
273     return S_OK;
274 }
275
276 static HRESULT WINAPI OleDocumentView_GetDocument(IOleDocumentView *iface, IUnknown **ppunk)
277 {
278     HTMLDocument *This = DOCVIEW_THIS(iface);
279     TRACE("(%p)->(%p)\n", This, ppunk);
280
281     if(!ppunk)
282         return E_INVALIDARG;
283
284     IHTMLDocument2_AddRef(HTMLDOC(This));
285     *ppunk = (IUnknown*)HTMLDOC(This);
286     return S_OK;
287 }
288
289 static HRESULT WINAPI OleDocumentView_SetRect(IOleDocumentView *iface, LPRECT prcView)
290 {
291     HTMLDocument *This = DOCVIEW_THIS(iface);
292     RECT rect;
293
294     TRACE("(%p)->(%p)\n", This, prcView);
295
296     if(!prcView)
297         return E_INVALIDARG;
298
299     if(This->hwnd) {
300         GetClientRect(This->hwnd, &rect);
301         if(memcmp(prcView, &rect, sizeof(RECT))) {
302             InvalidateRect(This->hwnd,NULL,TRUE);
303             SetWindowPos(This->hwnd, NULL, prcView->left, prcView->top, prcView->right,
304                     prcView->bottom, SWP_NOZORDER | SWP_NOACTIVATE);
305         }
306     }
307     
308     return S_OK;
309 }
310
311 static HRESULT WINAPI OleDocumentView_GetRect(IOleDocumentView *iface, LPRECT prcView)
312 {
313     HTMLDocument *This = DOCVIEW_THIS(iface);
314
315     TRACE("(%p)->(%p)\n", This, prcView);
316
317     if(!prcView)
318         return E_INVALIDARG;
319
320     GetClientRect(This->hwnd, prcView);
321     return S_OK;
322 }
323
324 static HRESULT WINAPI OleDocumentView_SetRectComplex(IOleDocumentView *iface, LPRECT prcView,
325                         LPRECT prcHScroll, LPRECT prcVScroll, LPRECT prcSizeBox)
326 {
327     HTMLDocument *This = DOCVIEW_THIS(iface);
328     FIXME("(%p)->(%p %p %p %p)\n", This, prcView, prcHScroll, prcVScroll, prcSizeBox);
329     return E_NOTIMPL;
330 }
331
332 static HRESULT WINAPI OleDocumentView_Show(IOleDocumentView *iface, BOOL fShow)
333 {
334     HTMLDocument *This = DOCVIEW_THIS(iface);
335     HRESULT hres;
336
337     TRACE("(%p)->(%x)\n", This, fShow);
338
339     if(fShow) {
340         if(!This->ui_active) {
341             hres = activate_window(This);
342             if(FAILED(hres))
343                 return hres;
344         }
345         ShowWindow(This->hwnd, SW_SHOW);
346     }else {
347         ShowWindow(This->hwnd, SW_HIDE);
348     }
349
350     return S_OK;
351 }
352
353 static HRESULT WINAPI OleDocumentView_UIActivate(IOleDocumentView *iface, BOOL fUIActivate)
354 {
355     HTMLDocument *This = DOCVIEW_THIS(iface);
356     HRESULT hres;
357
358     TRACE("(%p)->(%x)\n", This, fUIActivate);
359
360     if(!This->ipsite) {
361         FIXME("This->ipsite = NULL\n");
362         return E_FAIL;
363     }
364
365     if(fUIActivate) {
366         if(This->ui_active)
367             return S_OK;
368
369         if(!This->window_active) {
370             hres = activate_window(This);
371             if(FAILED(hres))
372                 return hres;
373         }
374
375         hres = IOleInPlaceSite_OnUIActivate(This->ipsite);
376         if(SUCCEEDED(hres)) {
377             IOleInPlaceFrame_SetActiveObject(This->frame, ACTOBJ(This), wszHTML_Document);
378         }else {
379             FIXME("OnUIActivate failed: %08lx\n", hres);
380             IOleInPlaceFrame_Release(This->frame);
381             This->frame = NULL;
382             This->ui_active = FALSE;
383             return hres;
384         }
385
386         hres = IDocHostUIHandler_ShowUI(This->hostui, 0, ACTOBJ(This), CMDTARGET(This),
387                 This->frame, NULL);
388         if(FAILED(hres))
389             IDocHostUIHandler_HideUI(This->hostui);
390
391         This->ui_active = TRUE;
392     }else {
393         This->window_active = FALSE;
394         if(This->ui_active) {
395             This->ui_active = FALSE;
396             if(This->frame)
397                 IOleInPlaceFrame_SetActiveObject(This->frame, NULL, NULL);
398             if(This->hostui)
399                 IDocHostUIHandler_HideUI(This->hostui);
400             if(This->ipsite)
401                 IOleInPlaceSite_OnUIDeactivate(This->ipsite, FALSE);
402         }
403     }
404     return S_OK;
405 }
406
407 static HRESULT WINAPI OleDocumentView_Open(IOleDocumentView *iface)
408 {
409     HTMLDocument *This = DOCVIEW_THIS(iface);
410     FIXME("(%p)\n", This);
411     return E_NOTIMPL;
412 }
413
414 static HRESULT WINAPI OleDocumentView_CloseView(IOleDocumentView *iface, DWORD dwReserved)
415 {
416     HTMLDocument *This = DOCVIEW_THIS(iface);
417     TRACE("(%p)->(%lx)\n", This, dwReserved);
418
419     if(dwReserved)
420         WARN("dwReserved = %ld\n", dwReserved);
421
422     /* NOTE:
423      * Windows implementation calls QueryInterface(IID_IOleCommandTarget),
424      * QueryInterface(IID_IOleControlSite) and KillTimer
425      */
426
427     IOleDocumentView_Show(iface, FALSE);
428
429     return S_OK;
430 }
431
432 static HRESULT WINAPI OleDocumentView_SaveViewState(IOleDocumentView *iface, LPSTREAM pstm)
433 {
434     HTMLDocument *This = DOCVIEW_THIS(iface);
435     FIXME("(%p)->(%p)\n", This, pstm);
436     return E_NOTIMPL;
437 }
438
439 static HRESULT WINAPI OleDocumentView_ApplyViewState(IOleDocumentView *iface, LPSTREAM pstm)
440 {
441     HTMLDocument *This = DOCVIEW_THIS(iface);
442     FIXME("(%p)->(%p)\n", This, pstm);
443     return E_NOTIMPL;
444 }
445
446 static HRESULT WINAPI OleDocumentView_Clone(IOleDocumentView *iface, IOleInPlaceSite *pIPSiteNew,
447                                         IOleDocumentView **ppViewNew)
448 {
449     HTMLDocument *This = DOCVIEW_THIS(iface);
450     FIXME("(%p)->(%p %p)\n", This, pIPSiteNew, ppViewNew);
451     return E_NOTIMPL;
452 }
453
454 #undef DOCVIEW_THIS
455
456 static const IOleDocumentViewVtbl OleDocumentViewVtbl = {
457     OleDocumentView_QueryInterface,
458     OleDocumentView_AddRef,
459     OleDocumentView_Release,
460     OleDocumentView_SetInPlaceSite,
461     OleDocumentView_GetInPlaceSite,
462     OleDocumentView_GetDocument,
463     OleDocumentView_SetRect,
464     OleDocumentView_GetRect,
465     OleDocumentView_SetRectComplex,
466     OleDocumentView_Show,
467     OleDocumentView_UIActivate,
468     OleDocumentView_Open,
469     OleDocumentView_CloseView,
470     OleDocumentView_SaveViewState,
471     OleDocumentView_ApplyViewState,
472     OleDocumentView_Clone
473 };
474
475 /**********************************************************
476  * IViewObject implementation
477  */
478
479 #define VIEWOBJ_THIS(iface) DEFINE_THIS(HTMLDocument, ViewObject2, iface)
480
481 static HRESULT WINAPI ViewObject_QueryInterface(IViewObject2 *iface, REFIID riid, void **ppvObject)
482 {
483     HTMLDocument *This = VIEWOBJ_THIS(iface);
484     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
485 }
486
487 static ULONG WINAPI ViewObject_AddRef(IViewObject2 *iface)
488 {
489     HTMLDocument *This = VIEWOBJ_THIS(iface);
490     return IHTMLDocument2_AddRef(HTMLDOC(This));
491 }
492
493 static ULONG WINAPI ViewObject_Release(IViewObject2 *iface)
494 {
495     HTMLDocument *This = VIEWOBJ_THIS(iface);
496     return IHTMLDocument2_Release(HTMLDOC(This));
497 }
498
499 static HRESULT WINAPI ViewObject_Draw(IViewObject2 *iface, DWORD dwDrawAspect, LONG lindex, void *pvAspect,
500         DVTARGETDEVICE *ptd, HDC hdcTargetDev, HDC hdcDraw, LPCRECTL lprcBounds,
501         LPCRECTL lprcWBounds, BOOL (CALLBACK *pfnContinue)(ULONG_PTR dwContinue), ULONG_PTR dwContinue)
502 {
503     HTMLDocument *This = VIEWOBJ_THIS(iface);
504     FIXME("(%p)->(%ld %ld %p %p %p %p %p %p %p %ld)\n", This, dwDrawAspect, lindex, pvAspect,
505             ptd, hdcTargetDev, hdcDraw, lprcBounds, lprcWBounds, pfnContinue, dwContinue);
506     return E_NOTIMPL;
507 }
508
509 static HRESULT WINAPI ViewObject_GetColorSet(IViewObject2 *iface, DWORD dwDrawAspect, LONG lindex, void *pvAspect,
510         DVTARGETDEVICE *ptd, HDC hicTargetDev, LOGPALETTE **ppColorSet)
511 {
512     HTMLDocument *This = VIEWOBJ_THIS(iface);
513     FIXME("(%p)->(%ld %ld %p %p %p %p)\n", This, dwDrawAspect, lindex, pvAspect, ptd, hicTargetDev, ppColorSet);
514     return E_NOTIMPL;
515 }
516
517 static HRESULT WINAPI ViewObject_Freeze(IViewObject2 *iface, DWORD dwDrawAspect, LONG lindex,
518         void *pvAspect, DWORD *pdwFreeze)
519 {
520     HTMLDocument *This = VIEWOBJ_THIS(iface);
521     FIXME("(%p)->(%ld %ld %p %p)\n", This, dwDrawAspect, lindex, pvAspect, pdwFreeze);
522     return E_NOTIMPL;
523 }
524
525 static HRESULT WINAPI ViewObject_Unfreeze(IViewObject2 *iface, DWORD dwFreeze)
526 {
527     HTMLDocument *This = VIEWOBJ_THIS(iface);
528     FIXME("(%p)->(%ld)\n", This, dwFreeze);
529     return E_NOTIMPL;
530 }
531
532 static HRESULT WINAPI ViewObject_SetAdvise(IViewObject2 *iface, DWORD aspects, DWORD advf, IAdviseSink *pAdvSink)
533 {
534     HTMLDocument *This = VIEWOBJ_THIS(iface);
535     FIXME("(%p)->(%ld %ld %p)\n", This, aspects, advf, pAdvSink);
536     return E_NOTIMPL;
537 }
538
539 static HRESULT WINAPI ViewObject_GetAdvise(IViewObject2 *iface, DWORD *pAspects, DWORD *pAdvf, IAdviseSink **ppAdvSink)
540 {
541     HTMLDocument *This = VIEWOBJ_THIS(iface);
542     FIXME("(%p)->(%p %p %p)\n", This, pAspects, pAdvf, ppAdvSink);
543     return E_NOTIMPL;
544 }
545
546 static HRESULT WINAPI ViewObject_GetExtent(IViewObject2 *iface, DWORD dwDrawAspect, LONG lindex,
547                                 DVTARGETDEVICE* ptd, LPSIZEL lpsizel)
548 {
549     HTMLDocument *This = VIEWOBJ_THIS(iface);
550     FIXME("(%p)->(%ld %ld %p %p)\n", This, dwDrawAspect, lindex, ptd, lpsizel);
551     return E_NOTIMPL;
552 }
553
554 #undef VIEWOBJ_THIS
555
556 static const IViewObject2Vtbl ViewObjectVtbl = {
557     ViewObject_QueryInterface,
558     ViewObject_AddRef,
559     ViewObject_Release,
560     ViewObject_Draw,
561     ViewObject_GetColorSet,
562     ViewObject_Freeze,
563     ViewObject_Unfreeze,
564     ViewObject_SetAdvise,
565     ViewObject_GetAdvise,
566     ViewObject_GetExtent
567 };
568
569 void HTMLDocument_View_Init(HTMLDocument *This)
570 {
571     This->lpOleDocumentViewVtbl = &OleDocumentViewVtbl;
572     This->lpViewObject2Vtbl = &ViewObjectVtbl;
573
574     This->ipsite = NULL;
575     This->frame = NULL;
576     This->hwnd = NULL;
577
578     This->in_place_active = FALSE;
579     This->ui_active = FALSE;
580     This->window_active = FALSE;
581 }