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