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