Added more implementation of IDocumentView.
[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
44 static ATOM serverwnd_class = 0;
45
46 static void paint_disabled(HWND hwnd) {
47     HDC hdc;
48     PAINTSTRUCT ps;
49     HBRUSH brush;
50     RECT rect;
51     HFONT font;
52
53     font = CreateFontA(25,0,0,0,400,0,0,0,ANSI_CHARSET,0,0,DEFAULT_QUALITY,DEFAULT_PITCH,NULL);
54     brush = CreateSolidBrush(RGB(255,255,255));
55     GetClientRect(hwnd, &rect);
56
57     hdc = BeginPaint(hwnd, &ps);
58     SelectObject(hdc, font);
59     SelectObject(hdc, brush);
60     Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);
61     DrawTextA(hdc, "HTML rendering is currently disabled.",-1, &rect,
62             DT_CENTER | DT_SINGLELINE | DT_VCENTER);
63     EndPaint(hwnd, &ps);
64
65     DeleteObject(font);
66     DeleteObject(brush);
67 }
68
69 static LRESULT WINAPI serverwnd_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
70 {
71     if(msg == WM_PAINT)
72         paint_disabled(hwnd);
73         
74     return DefWindowProcW(hwnd, msg, wParam, lParam);
75 }
76
77 static void register_serverwnd_class()
78 {
79     static WNDCLASSEXW wndclass = {
80         sizeof(WNDCLASSEXW),
81         CS_DBLCLKS,
82         serverwnd_proc,
83         0, 0, NULL, NULL, NULL, NULL, NULL,
84         wszInternetExplorer_Server,
85         NULL,
86     };
87     wndclass.hInstance = hInst;
88     serverwnd_class = RegisterClassExW(&wndclass);
89 }
90
91
92 /**********************************************************
93  * IOleDocumentView implementation
94  */
95
96 #define DOCVIEW_THIS \
97         HTMLDocument* const This=(HTMLDocument*)((char*)(iface)-offsetof(HTMLDocument,lpOleDocumentViewVtbl));
98
99 static HRESULT WINAPI OleDocumentView_QueryInterface(IOleDocumentView *iface, REFIID riid, void **ppvObject)
100 {
101     DOCVIEW_THIS
102     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
103 }
104
105 static ULONG WINAPI OleDocumentView_AddRef(IOleDocumentView *iface)
106 {
107     DOCVIEW_THIS
108     return IHTMLDocument2_AddRef(HTMLDOC(This));
109 }
110
111 static ULONG WINAPI OleDocumentView_Release(IOleDocumentView *iface)
112 {
113     DOCVIEW_THIS
114     return IHTMLDocument2_Release(HTMLDOC(This));
115 }
116
117 static HRESULT WINAPI OleDocumentView_SetInPlaceSite(IOleDocumentView *iface, IOleInPlaceSite *pIPSite)
118 {
119     DOCVIEW_THIS
120     TRACE("(%p)->(%p)\n", This, pIPSite);
121
122     if(!pIPSite)
123         return E_INVALIDARG;
124
125     if(pIPSite)
126         IOleInPlaceSite_AddRef(pIPSite);
127
128     if(This->ipsite)
129         IOleInPlaceSite_Release(This->ipsite);
130
131     This->ipsite = pIPSite;
132     return S_OK;
133 }
134
135 static HRESULT WINAPI OleDocumentView_GetInPlaceSite(IOleDocumentView *iface, IOleInPlaceSite **ppIPSite)
136 {
137     DOCVIEW_THIS
138     TRACE("(%p)->(%p)\n", This, ppIPSite);
139
140     if(!ppIPSite)
141         return E_INVALIDARG;
142
143     if(This->ipsite)
144         IOleInPlaceSite_AddRef(This->ipsite);
145
146     *ppIPSite = This->ipsite;
147     return S_OK;
148 }
149
150 static HRESULT WINAPI OleDocumentView_GetDocument(IOleDocumentView *iface, IUnknown **ppunk)
151 {
152     DOCVIEW_THIS
153     TRACE("(%p)->(%p)\n", This, ppunk);
154
155     if(!ppunk)
156         return E_INVALIDARG;
157
158     IHTMLDocument2_AddRef(HTMLDOC(This));
159     *ppunk = (IUnknown*)HTMLDOC(This);
160     return S_OK;
161 }
162
163 static HRESULT WINAPI OleDocumentView_SetRect(IOleDocumentView *iface, LPRECT prcView)
164 {
165     DOCVIEW_THIS
166     RECT rect;
167
168     TRACE("(%p)->(%p)\n", This, prcView);
169
170     if(!prcView)
171         return E_INVALIDARG;
172
173     if(This->hwnd) {
174         GetClientRect(This->hwnd, &rect);
175         if(memcmp(prcView, &rect, sizeof(RECT))) {
176             InvalidateRect(This->hwnd,NULL,TRUE);
177             SetWindowPos(This->hwnd, NULL, prcView->left, prcView->top, prcView->right,
178                     prcView->bottom, SWP_NOZORDER | SWP_NOACTIVATE);
179         }
180     }
181     
182     return S_OK;
183 }
184
185 static HRESULT WINAPI OleDocumentView_GetRect(IOleDocumentView *iface, LPRECT prcView)
186 {
187     DOCVIEW_THIS
188
189     TRACE("(%p)->(%p)\n", This, prcView);
190
191     if(!prcView)
192         return E_INVALIDARG;
193
194     GetClientRect(This->hwnd, prcView);
195     return S_OK;
196 }
197
198 static HRESULT WINAPI OleDocumentView_SetRectComplex(IOleDocumentView *iface, LPRECT prcView,
199                         LPRECT prcHScroll, LPRECT prcVScroll, LPRECT prcSizeBox)
200 {
201     DOCVIEW_THIS
202     FIXME("(%p)->(%p %p %p %p)\n", This, prcView, prcHScroll, prcVScroll, prcSizeBox);
203     return E_NOTIMPL;
204 }
205
206 static HRESULT WINAPI OleDocumentView_Show(IOleDocumentView *iface, BOOL fShow)
207 {
208     DOCVIEW_THIS
209     FIXME("(%p)->(%x)\n", This, fShow);
210     return E_NOTIMPL;
211 }
212
213 static HRESULT WINAPI OleDocumentView_UIActivate(IOleDocumentView *iface, BOOL fUIActivate)
214 {
215     DOCVIEW_THIS
216     HRESULT hres;
217     IOleInPlaceUIWindow *pIPWnd;
218     IOleInPlaceFrame *pIPFrame;
219     RECT posrect, cliprect;
220     OLEINPLACEFRAMEINFO frameinfo;
221     HWND parent_hwnd, hwnd;
222
223     TRACE("(%p)->(%x)\n", This, fUIActivate);
224
225     if(!This->ipsite) {
226         FIXME("This->ipsite = NULL\n");
227         return E_FAIL;
228     }
229
230     if(fUIActivate) {
231         if(This->hwnd)
232             return S_OK;
233         if(!serverwnd_class)
234             register_serverwnd_class();
235
236         hres = IOleInPlaceSite_CanInPlaceActivate(This->ipsite);
237         if(hres != S_OK) {
238             WARN("CanInPlaceActivate returned: %08lx\n", hres);
239             return FAILED(hres) ? hres : E_FAIL;
240         }
241
242         hres = IOleInPlaceSite_GetWindowContext(This->ipsite, &pIPFrame, &pIPWnd, &posrect, &cliprect, &frameinfo);
243         if(FAILED(hres)) {
244             WARN("GetWindowContext failed: %08lx\n", hres);
245             return hres;
246         }
247         if(pIPFrame)
248             IOleInPlaceFrame_Release(pIPFrame);
249         if(pIPWnd)
250             IOleInPlaceUIWindow_Release(pIPWnd);
251         TRACE("got window context: %p %p {%ld %ld %ld %ld} {%ld %ld %ld %ld} {%d %x %p %p %d}\n",
252                 pIPFrame, pIPWnd, posrect.left, posrect.top, posrect.right, posrect.bottom,
253                 cliprect.left, cliprect.top, cliprect.right, cliprect.bottom,
254                 frameinfo.cb, frameinfo.fMDIApp, frameinfo.hwndFrame, frameinfo.haccel, frameinfo.cAccelEntries);
255
256         hres = IOleInPlaceSite_GetWindow(This->ipsite, &parent_hwnd);
257         if(FAILED(hres)) {
258             WARN("GetWindow failed: %08lx\n", hres);
259             return hres;
260         }
261
262         hwnd = CreateWindowExW(0, wszInternetExplorer_Server, NULL,
263                 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
264                 posrect.left, posrect.top, posrect.right-posrect.left, posrect.bottom-posrect.top,
265                 parent_hwnd, NULL, hInst, This);
266
267         hres = IOleInPlaceSite_OnInPlaceActivate(This->ipsite);
268         if(FAILED(hres)) {
269             WARN("OnInPlaceActivate failed: %08lx\n", hres);
270             return hres;
271         }
272
273         SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
274                 SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW | SWP_NOACTIVATE | SWP_SHOWWINDOW);
275         RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE | RDW_NOERASE | RDW_ALLCHILDREN);
276         SetFocus(hwnd);
277
278         /* NOTE:
279          * Windows implementation calls:
280          * RegisterWindowMessage("MSWHEEL_ROLLMSG");
281          * SetTimer(This->hwnd, TIMER_ID, 100, NULL);
282          */
283
284         hres = IOleInPlaceSite_OnUIActivate(This->ipsite);
285         if(SUCCEEDED(hres)) {
286             /* IOleInPlaceFrame_SetActiveObject(pIPFrame, ACTOBJ(This->pDoc), wszHTMLDocument); */
287         }else {
288             FIXME("OnUIActivate failed: %08lx\n", hres);
289             DestroyWindow(hwnd);
290             return hres;
291         }
292         This->hwnd = hwnd;
293     }else {
294         FIXME("deactivating is not supported\n");
295         return E_NOTIMPL;
296     }
297     return S_OK;
298 }
299
300 static HRESULT WINAPI OleDocumentView_Open(IOleDocumentView *iface)
301 {
302     DOCVIEW_THIS
303     FIXME("(%p)\n", This);
304     return E_NOTIMPL;
305 }
306
307 static HRESULT WINAPI OleDocumentView_CloseView(IOleDocumentView *iface, DWORD dwReserved)
308 {
309     DOCVIEW_THIS
310     FIXME("(%p)->(%lx)\n", This, dwReserved);
311     return E_NOTIMPL;
312 }
313
314 static HRESULT WINAPI OleDocumentView_SaveViewState(IOleDocumentView *iface, LPSTREAM pstm)
315 {
316     DOCVIEW_THIS
317     FIXME("(%p)->(%p)\n", This, pstm);
318     return E_NOTIMPL;
319 }
320
321 static HRESULT WINAPI OleDocumentView_ApplyViewState(IOleDocumentView *iface, LPSTREAM pstm)
322 {
323     DOCVIEW_THIS
324     FIXME("(%p)->(%p)\n", This, pstm);
325     return E_NOTIMPL;
326 }
327
328 static HRESULT WINAPI OleDocumentView_Clone(IOleDocumentView *iface, IOleInPlaceSite *pIPSiteNew,
329                                         IOleDocumentView **ppViewNew)
330 {
331     DOCVIEW_THIS
332     FIXME("(%p)->(%p %p)\n", This, pIPSiteNew, ppViewNew);
333     return E_NOTIMPL;
334 }
335
336 static IOleDocumentViewVtbl OleDocumentViewVtbl = {
337     OleDocumentView_QueryInterface,
338     OleDocumentView_AddRef,
339     OleDocumentView_Release,
340     OleDocumentView_SetInPlaceSite,
341     OleDocumentView_GetInPlaceSite,
342     OleDocumentView_GetDocument,
343     OleDocumentView_SetRect,
344     OleDocumentView_GetRect,
345     OleDocumentView_SetRectComplex,
346     OleDocumentView_Show,
347     OleDocumentView_UIActivate,
348     OleDocumentView_Open,
349     OleDocumentView_CloseView,
350     OleDocumentView_SaveViewState,
351     OleDocumentView_ApplyViewState,
352     OleDocumentView_Clone
353 };
354
355 void HTMLDocument_View_Init(HTMLDocument *This)
356 {
357     This->lpOleDocumentViewVtbl = &OleDocumentViewVtbl;
358
359     This->ipsite = NULL;
360     This->hwnd = NULL;
361 }