mshtml: Call attached event handlers in reversed order.
[wine] / dlls / mshtml / view.c
1 /*
2  * Copyright 2005-2006 Jacek Caban for CodeWeavers
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "commctrl.h"
30 #include "ole2.h"
31 #include "resource.h"
32
33 #include "wine/debug.h"
34
35 #include "mshtml_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38
39 #define TIMER_ID 0x1000
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 const WCHAR wszTooltipData[] = {'t','o','o','l','t','i','p','_','d','a','t','a',0};
45
46 static ATOM serverwnd_class = 0;
47
48 typedef struct {
49     HTMLDocumentObj *doc;
50     WNDPROC proc;
51 } tooltip_data;
52
53 static void paint_document(HTMLDocumentObj *This)
54 {
55     PAINTSTRUCT ps;
56     RECT rect;
57     HDC hdc;
58
59     GetClientRect(This->hwnd, &rect);
60
61     hdc = BeginPaint(This->hwnd, &ps);
62
63     if(!(This->hostinfo.dwFlags & (DOCHOSTUIFLAG_NO3DOUTERBORDER|DOCHOSTUIFLAG_NO3DBORDER)))
64         DrawEdge(hdc, &rect, EDGE_SUNKEN, BF_RECT|BF_ADJUST);
65
66     if(!This->nscontainer) {
67         WCHAR wszHTMLDisabled[100];
68         HFONT font;
69
70         LoadStringW(hInst, IDS_HTMLDISABLED, wszHTMLDisabled, sizeof(wszHTMLDisabled)/sizeof(WCHAR));
71
72         font = CreateFontA(25,0,0,0,400,0,0,0,ANSI_CHARSET,0,0,DEFAULT_QUALITY,DEFAULT_PITCH,NULL);
73
74         SelectObject(hdc, font);
75         SelectObject(hdc, GetSysColorBrush(COLOR_WINDOW));
76
77         Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);
78         DrawTextW(hdc, wszHTMLDisabled,-1, &rect, DT_CENTER | DT_SINGLELINE | DT_VCENTER);
79
80         DeleteObject(font);
81     }
82
83     EndPaint(This->hwnd, &ps);
84 }
85
86 static void activate_gecko(NSContainer *This)
87 {
88     TRACE("(%p) %p\n", This, This->window);
89
90     SetParent(This->hwnd, This->doc->hwnd);
91     ShowWindow(This->hwnd, SW_SHOW);
92
93     nsIBaseWindow_SetVisibility(This->window, TRUE);
94     nsIBaseWindow_SetEnabled(This->window, TRUE);
95     nsIWebBrowserFocus_Activate(This->focus);
96 }
97
98 void update_doc(HTMLDocument *This, DWORD flags)
99 {
100     if(!This->doc_obj->update && This->doc_obj->hwnd)
101         SetTimer(This->doc_obj->hwnd, TIMER_ID, 100, NULL);
102
103     This->doc_obj->update |= flags;
104 }
105
106 void update_title(HTMLDocumentObj *This)
107 {
108     IOleCommandTarget *olecmd;
109     HRESULT hres;
110
111     if(!(This->update & UPDATE_TITLE))
112         return;
113
114     This->update &= ~UPDATE_TITLE;
115
116     if(!This->client)
117         return;
118
119     hres = IOleClientSite_QueryInterface(This->client, &IID_IOleCommandTarget, (void**)&olecmd);
120     if(SUCCEEDED(hres)) {
121         VARIANT title;
122         WCHAR empty[] = {0};
123
124         V_VT(&title) = VT_BSTR;
125         V_BSTR(&title) = SysAllocString(empty);
126         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETTITLE, OLECMDEXECOPT_DONTPROMPTUSER,
127                                &title, NULL);
128         SysFreeString(V_BSTR(&title));
129
130         IOleCommandTarget_Release(olecmd);
131     }
132 }
133
134 static LRESULT on_timer(HTMLDocumentObj *This)
135 {
136     TRACE("(%p) %x\n", This, This->update);
137
138     KillTimer(This->hwnd, TIMER_ID);
139
140     if(!This->update)
141         return 0;
142
143     if(This->update & UPDATE_UI) {
144         if(This->hostui)
145             IDocHostUIHandler_UpdateUI(This->hostui);
146
147         if(This->client) {
148             IOleCommandTarget *cmdtrg;
149             HRESULT hres;
150
151             hres = IOleClientSite_QueryInterface(This->client, &IID_IOleCommandTarget,
152                                                  (void**)&cmdtrg);
153             if(SUCCEEDED(hres)) {
154                 IOleCommandTarget_Exec(cmdtrg, NULL, OLECMDID_UPDATECOMMANDS,
155                                        OLECMDEXECOPT_DONTPROMPTUSER, NULL, NULL);
156                 IOleCommandTarget_Release(cmdtrg);
157             }
158         }
159     }
160
161     update_title(This);
162     This->update = 0;
163     return 0;
164 }
165
166 void notif_focus(HTMLDocumentObj *This)
167 {
168     IOleControlSite *site;
169     HRESULT hres;
170
171     if(!This->client)
172         return;
173
174     hres = IOleClientSite_QueryInterface(This->client, &IID_IOleControlSite, (void**)&site);
175     if(FAILED(hres))
176         return;
177
178     IOleControlSite_OnFocus(site, This->focus);
179     IOleControlSite_Release(site);
180 }
181
182 static LRESULT WINAPI serverwnd_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
183 {
184     HTMLDocumentObj *This;
185
186     static const WCHAR wszTHIS[] = {'T','H','I','S',0};
187
188     if(msg == WM_CREATE) {
189         This = *(HTMLDocumentObj**)lParam;
190         SetPropW(hwnd, wszTHIS, This);
191     }else {
192         This = GetPropW(hwnd, wszTHIS);
193     }
194
195     switch(msg) {
196     case WM_CREATE:
197         This->hwnd = hwnd;
198         break;
199     case WM_PAINT:
200         paint_document(This);
201         break;
202     case WM_SIZE:
203         TRACE("(%p)->(WM_SIZE)\n", This);
204         if(This->nscontainer) {
205             INT ew=0, eh=0;
206
207             if(!(This->hostinfo.dwFlags & (DOCHOSTUIFLAG_NO3DOUTERBORDER|DOCHOSTUIFLAG_NO3DBORDER))) {
208                 ew = GetSystemMetrics(SM_CXEDGE);
209                 eh = GetSystemMetrics(SM_CYEDGE);
210             }
211
212             SetWindowPos(This->nscontainer->hwnd, NULL, ew, eh,
213                          LOWORD(lParam) - 2*ew, HIWORD(lParam) - 2*eh,
214                          SWP_NOZORDER | SWP_NOACTIVATE);
215         }
216         break;
217     case WM_TIMER:
218         return on_timer(This);
219     case WM_MOUSEACTIVATE:
220         return MA_ACTIVATE;
221     }
222         
223     return DefWindowProcW(hwnd, msg, wParam, lParam);
224 }
225
226 static void register_serverwnd_class(void)
227 {
228     static WNDCLASSEXW wndclass = {
229         sizeof(WNDCLASSEXW),
230         CS_DBLCLKS,
231         serverwnd_proc,
232         0, 0, NULL, NULL, NULL, NULL, NULL,
233         wszInternetExplorer_Server,
234         NULL,
235     };
236     wndclass.hInstance = hInst;
237     serverwnd_class = RegisterClassExW(&wndclass);
238 }
239
240 static HRESULT activate_window(HTMLDocumentObj *This)
241 {
242     IOleInPlaceFrame *pIPFrame;
243     IOleCommandTarget *cmdtrg;
244     IOleInPlaceSiteEx *ipsiteex;
245     RECT posrect, cliprect;
246     OLEINPLACEFRAMEINFO frameinfo;
247     HWND parent_hwnd;
248     HRESULT hres;
249
250     if(!serverwnd_class)
251         register_serverwnd_class();
252
253     hres = IOleInPlaceSite_CanInPlaceActivate(This->ipsite);
254     if(hres != S_OK) {
255         WARN("CanInPlaceActivate returned: %08x\n", hres);
256         return FAILED(hres) ? hres : E_FAIL;
257     }
258
259     hres = IOleInPlaceSite_GetWindowContext(This->ipsite, &pIPFrame, &This->ip_window,
260             &posrect, &cliprect, &frameinfo);
261     if(FAILED(hres)) {
262         WARN("GetWindowContext failed: %08x\n", hres);
263         return hres;
264     }
265
266     TRACE("got window context: %p %p {%d %d %d %d} {%d %d %d %d} {%d %x %p %p %d}\n",
267             pIPFrame, This->ip_window, posrect.left, posrect.top, posrect.right, posrect.bottom,
268             cliprect.left, cliprect.top, cliprect.right, cliprect.bottom,
269             frameinfo.cb, frameinfo.fMDIApp, frameinfo.hwndFrame, frameinfo.haccel, frameinfo.cAccelEntries);
270
271     hres = IOleInPlaceSite_GetWindow(This->ipsite, &parent_hwnd);
272     if(FAILED(hres)) {
273         WARN("GetWindow failed: %08x\n", hres);
274         return hres;
275     }
276
277     TRACE("got parent window %p\n", parent_hwnd);
278
279     if(This->hwnd) {
280         if(GetParent(This->hwnd) != parent_hwnd)
281             SetParent(This->hwnd, parent_hwnd);
282         SetWindowPos(This->hwnd, HWND_TOP,
283                 posrect.left, posrect.top, posrect.right-posrect.left, posrect.bottom-posrect.top,
284                 SWP_NOACTIVATE | SWP_SHOWWINDOW);
285     }else {
286         CreateWindowExW(0, wszInternetExplorer_Server, NULL,
287                 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
288                 posrect.left, posrect.top, posrect.right-posrect.left, posrect.bottom-posrect.top,
289                 parent_hwnd, NULL, hInst, This);
290
291         TRACE("Created window %p\n", This->hwnd);
292
293         SetWindowPos(This->hwnd, NULL, 0, 0, 0, 0,
294                 SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW | SWP_NOACTIVATE | SWP_SHOWWINDOW);
295         RedrawWindow(This->hwnd, NULL, NULL, RDW_INVALIDATE | RDW_NOERASE | RDW_ALLCHILDREN);
296
297         /* NOTE:
298          * Windows implementation calls:
299          * RegisterWindowMessage("MSWHEEL_ROLLMSG");
300          */
301         SetTimer(This->hwnd, TIMER_ID, 100, NULL);
302     }
303
304     if(This->nscontainer)
305         activate_gecko(This->nscontainer);
306
307     This->in_place_active = TRUE;
308     hres = IOleInPlaceSite_QueryInterface(This->ipsite, &IID_IOleInPlaceSiteEx, (void**)&ipsiteex);
309     if(SUCCEEDED(hres)) {
310         BOOL redraw = FALSE;
311
312         hres = IOleInPlaceSiteEx_OnInPlaceActivateEx(ipsiteex, &redraw, 0);
313         IOleInPlaceSiteEx_Release(ipsiteex);
314         if(redraw)
315             FIXME("unsupported redraw\n");
316     }else{
317         hres = IOleInPlaceSite_OnInPlaceActivate(This->ipsite);
318     }
319     if(FAILED(hres)) {
320         WARN("OnInPlaceActivate failed: %08x\n", hres);
321         This->in_place_active = FALSE;
322         return hres;
323     }
324
325     hres = IOleClientSite_QueryInterface(This->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
326     if(SUCCEEDED(hres)) {
327         VARIANT var;
328
329         IOleInPlaceFrame_SetStatusText(pIPFrame, NULL);
330
331         V_VT(&var) = VT_I4;
332         V_I4(&var) = 0;
333         IOleCommandTarget_Exec(cmdtrg, NULL, OLECMDID_SETPROGRESSMAX,
334                 OLECMDEXECOPT_DONTPROMPTUSER, &var, NULL);
335         IOleCommandTarget_Exec(cmdtrg, NULL, OLECMDID_SETPROGRESSPOS, 
336                 OLECMDEXECOPT_DONTPROMPTUSER, &var, NULL);
337
338         IOleCommandTarget_Release(cmdtrg);
339     }
340
341     if(This->frame)
342         IOleInPlaceFrame_Release(This->frame);
343     This->frame = pIPFrame;
344
345     if(!This->request_uiactivate) {
346         hres = IOleInPlaceSite_QueryInterface(This->ipsite, &IID_IOleInPlaceSiteEx, (void**)&ipsiteex);
347         if(SUCCEEDED(hres)) {
348             IOleInPlaceSiteEx_RequestUIActivate(ipsiteex);
349             IOleInPlaceSiteEx_Release(ipsiteex);
350         }
351     }
352
353     This->window_active = TRUE;
354
355     return S_OK;
356 }
357
358 static LRESULT WINAPI tooltips_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
359 {
360     tooltip_data *data = GetPropW(hwnd, wszTooltipData);
361
362     TRACE("%d %p\n", msg, data);
363
364     if(msg == TTM_WINDOWFROMPOINT) {
365         RECT rect;
366         POINT *pt = (POINT*)lParam;
367
368         TRACE("TTM_WINDOWFROMPOINT (%d,%d)\n", pt->x, pt->y);
369
370         GetWindowRect(data->doc->hwnd, &rect);
371
372         if(rect.left <= pt->x && pt->x <= rect.right
373            && rect.top <= pt->y && pt->y <= rect.bottom)
374             return (LPARAM)data->doc->hwnd;
375     }
376
377     return CallWindowProcW(data->proc, hwnd, msg, wParam, lParam);
378 }
379
380 static void create_tooltips_window(HTMLDocumentObj *This)
381 {
382     tooltip_data *data = heap_alloc(sizeof(*data));
383
384     This->tooltips_hwnd = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, TTS_NOPREFIX | WS_POPUP,
385             CW_USEDEFAULT, CW_USEDEFAULT, 10, 10, This->hwnd, NULL, hInst, NULL);
386
387     data->doc = This;
388     data->proc = (WNDPROC)GetWindowLongPtrW(This->tooltips_hwnd, GWLP_WNDPROC);
389
390     SetPropW(This->tooltips_hwnd, wszTooltipData, data);
391
392     SetWindowLongPtrW(This->tooltips_hwnd, GWLP_WNDPROC, (LONG_PTR)tooltips_proc);
393
394     SetWindowPos(This->tooltips_hwnd, HWND_TOPMOST,0, 0, 0, 0,
395                  SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
396
397 }
398
399 void show_tooltip(HTMLDocumentObj *This, DWORD x, DWORD y, LPCWSTR text)
400 {
401     TTTOOLINFOW toolinfo = {
402         sizeof(TTTOOLINFOW), 0, This->hwnd, 0xdeadbeef,
403         {x>2 ? x-2 : 0, y>0 ? y-2 : 0, x+2, y+2}, /* FIXME */
404         NULL, (LPWSTR)text, 0};
405     MSG msg = {This->hwnd, WM_MOUSEMOVE, 0, MAKELPARAM(x,y), 0, {x,y}};
406
407     TRACE("(%p)->(%d %d %s)\n", This, x, y, debugstr_w(text));
408
409     if(!This->tooltips_hwnd)
410         create_tooltips_window(This);
411
412     SendMessageW(This->tooltips_hwnd, TTM_ADDTOOLW, 0, (LPARAM)&toolinfo);
413     SendMessageW(This->tooltips_hwnd, TTM_ACTIVATE, TRUE, 0);
414     SendMessageW(This->tooltips_hwnd, TTM_RELAYEVENT, 0, (LPARAM)&msg);
415 }
416
417 void hide_tooltip(HTMLDocumentObj *This)
418 {
419     TTTOOLINFOW toolinfo = {
420         sizeof(TTTOOLINFOW), 0, This->hwnd, 0xdeadbeef,
421         {0,0,0,0}, NULL, NULL, 0};
422
423     TRACE("(%p)\n", This);
424
425     SendMessageW(This->tooltips_hwnd, TTM_DELTOOLW, 0, (LPARAM)&toolinfo);
426     SendMessageW(This->tooltips_hwnd, TTM_ACTIVATE, FALSE, 0);
427 }
428
429 HRESULT call_set_active_object(IOleInPlaceUIWindow *window, IOleInPlaceActiveObject *act_obj)
430 {
431     static WCHAR html_documentW[30];
432
433     if(act_obj && !html_documentW[0]) {
434         LoadStringW(hInst, IDS_HTMLDOCUMENT, html_documentW,
435                     sizeof(html_documentW)/sizeof(WCHAR));
436     }
437
438     return IOleInPlaceFrame_SetActiveObject(window, act_obj, act_obj ? html_documentW : NULL);
439 }
440
441 /**********************************************************
442  * IOleDocumentView implementation
443  */
444
445 #define DOCVIEW_THIS(iface) DEFINE_THIS(HTMLDocument, OleDocumentView, iface)
446
447 static HRESULT WINAPI OleDocumentView_QueryInterface(IOleDocumentView *iface, REFIID riid, void **ppvObject)
448 {
449     HTMLDocument *This = DOCVIEW_THIS(iface);
450     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
451 }
452
453 static ULONG WINAPI OleDocumentView_AddRef(IOleDocumentView *iface)
454 {
455     HTMLDocument *This = DOCVIEW_THIS(iface);
456     return IHTMLDocument2_AddRef(HTMLDOC(This));
457 }
458
459 static ULONG WINAPI OleDocumentView_Release(IOleDocumentView *iface)
460 {
461     HTMLDocument *This = DOCVIEW_THIS(iface);
462     return IHTMLDocument2_Release(HTMLDOC(This));
463 }
464
465 static HRESULT WINAPI OleDocumentView_SetInPlaceSite(IOleDocumentView *iface, IOleInPlaceSite *pIPSite)
466 {
467     HTMLDocument *This = DOCVIEW_THIS(iface);
468     TRACE("(%p)->(%p)\n", This, pIPSite);
469
470     if(pIPSite)
471         IOleInPlaceSite_AddRef(pIPSite);
472
473     if(This->doc_obj->ipsite)
474         IOleInPlaceSite_Release(This->doc_obj->ipsite);
475
476     This->doc_obj->ipsite = pIPSite;
477     This->doc_obj->request_uiactivate = TRUE;
478     return S_OK;
479 }
480
481 static HRESULT WINAPI OleDocumentView_GetInPlaceSite(IOleDocumentView *iface, IOleInPlaceSite **ppIPSite)
482 {
483     HTMLDocument *This = DOCVIEW_THIS(iface);
484     TRACE("(%p)->(%p)\n", This, ppIPSite);
485
486     if(!ppIPSite)
487         return E_INVALIDARG;
488
489     if(This->doc_obj->ipsite)
490         IOleInPlaceSite_AddRef(This->doc_obj->ipsite);
491
492     *ppIPSite = This->doc_obj->ipsite;
493     return S_OK;
494 }
495
496 static HRESULT WINAPI OleDocumentView_GetDocument(IOleDocumentView *iface, IUnknown **ppunk)
497 {
498     HTMLDocument *This = DOCVIEW_THIS(iface);
499     TRACE("(%p)->(%p)\n", This, ppunk);
500
501     if(!ppunk)
502         return E_INVALIDARG;
503
504     IHTMLDocument2_AddRef(HTMLDOC(This));
505     *ppunk = (IUnknown*)HTMLDOC(This);
506     return S_OK;
507 }
508
509 static HRESULT WINAPI OleDocumentView_SetRect(IOleDocumentView *iface, LPRECT prcView)
510 {
511     HTMLDocument *This = DOCVIEW_THIS(iface);
512     RECT rect;
513
514     TRACE("(%p)->(%p)\n", This, prcView);
515
516     if(!prcView)
517         return E_INVALIDARG;
518
519     if(This->doc_obj->hwnd) {
520         GetClientRect(This->doc_obj->hwnd, &rect);
521         if(memcmp(prcView, &rect, sizeof(RECT))) {
522             InvalidateRect(This->doc_obj->hwnd, NULL, TRUE);
523             SetWindowPos(This->doc_obj->hwnd, NULL, prcView->left, prcView->top, prcView->right,
524                     prcView->bottom, SWP_NOZORDER | SWP_NOACTIVATE);
525         }
526     }
527     
528     return S_OK;
529 }
530
531 static HRESULT WINAPI OleDocumentView_GetRect(IOleDocumentView *iface, LPRECT prcView)
532 {
533     HTMLDocument *This = DOCVIEW_THIS(iface);
534
535     TRACE("(%p)->(%p)\n", This, prcView);
536
537     if(!prcView)
538         return E_INVALIDARG;
539
540     GetClientRect(This->doc_obj->hwnd, prcView);
541     return S_OK;
542 }
543
544 static HRESULT WINAPI OleDocumentView_SetRectComplex(IOleDocumentView *iface, LPRECT prcView,
545                         LPRECT prcHScroll, LPRECT prcVScroll, LPRECT prcSizeBox)
546 {
547     HTMLDocument *This = DOCVIEW_THIS(iface);
548     FIXME("(%p)->(%p %p %p %p)\n", This, prcView, prcHScroll, prcVScroll, prcSizeBox);
549     return E_NOTIMPL;
550 }
551
552 static HRESULT WINAPI OleDocumentView_Show(IOleDocumentView *iface, BOOL fShow)
553 {
554     HTMLDocument *This = DOCVIEW_THIS(iface);
555     HRESULT hres;
556
557     TRACE("(%p)->(%x)\n", This, fShow);
558
559     if(fShow) {
560         if(!This->doc_obj->ui_active) {
561             hres = activate_window(This->doc_obj);
562             if(FAILED(hres))
563                 return hres;
564         }
565         update_doc(This, UPDATE_UI);
566         ShowWindow(This->doc_obj->hwnd, SW_SHOW);
567     }else {
568         ShowWindow(This->doc_obj->hwnd, SW_HIDE);
569
570         if(This->doc_obj->in_place_active)
571             IOleInPlaceObjectWindowless_InPlaceDeactivate(INPLACEWIN(This));
572
573         if(This->doc_obj->ip_window) {
574             IOleInPlaceUIWindow_Release(This->doc_obj->ip_window);
575             This->doc_obj->ip_window = NULL;
576         }
577     }
578
579     return S_OK;
580 }
581
582 static HRESULT WINAPI OleDocumentView_UIActivate(IOleDocumentView *iface, BOOL fUIActivate)
583 {
584     HTMLDocument *This = DOCVIEW_THIS(iface);
585     HRESULT hres;
586
587     TRACE("(%p)->(%x)\n", This, fUIActivate);
588
589     if(!This->doc_obj->ipsite) {
590         IOleClientSite *cs = This->doc_obj->client;
591         IOleInPlaceSite *ips;
592
593         if(!cs) {
594             WARN("this->ipsite = NULL\n");
595             return E_UNEXPECTED;
596         }
597
598         hres = IOleClientSite_QueryInterface(cs, &IID_IOleInPlaceSiteWindowless, (void**)&ips);
599         if(SUCCEEDED(hres))
600             This->doc_obj->ipsite = ips;
601         else {
602             hres = IOleClientSite_QueryInterface(cs, &IID_IOleInPlaceSiteEx, (void**)&ips);
603             if(SUCCEEDED(hres))
604                 This->doc_obj->ipsite = ips;
605             else {
606                 hres = IOleClientSite_QueryInterface(cs, &IID_IOleInPlaceSite, (void**)&ips);
607                 if(SUCCEEDED(hres))
608                     This->doc_obj->ipsite = ips;
609                 else {
610                     WARN("this->ipsite = NULL\n");
611                     return E_NOINTERFACE;
612                 }
613             }
614         }
615
616         IOleClientSite_AddRef(This->doc_obj->ipsite);
617         This->doc_obj->request_uiactivate = FALSE;
618         HTMLDocument_LockContainer(This->doc_obj, TRUE);
619     }
620
621     if(fUIActivate) {
622         RECT rcBorderWidths;
623
624         if(This->doc_obj->ui_active)
625             return S_OK;
626
627         if(!This->doc_obj->window_active) {
628             hres = activate_window(This->doc_obj);
629             if(FAILED(hres))
630                 return hres;
631         }
632
633         This->doc_obj->focus = TRUE;
634         if(This->doc_obj->nscontainer)
635             nsIWebBrowserFocus_Activate(This->doc_obj->nscontainer->focus);
636         notif_focus(This->doc_obj);
637
638         update_doc(This, UPDATE_UI);
639
640         hres = IOleInPlaceSite_OnUIActivate(This->doc_obj->ipsite);
641         if(SUCCEEDED(hres)) {
642             call_set_active_object((IOleInPlaceUIWindow*)This->doc_obj->frame, ACTOBJ(This));
643         }else {
644             FIXME("OnUIActivate failed: %08x\n", hres);
645             IOleInPlaceFrame_Release(This->doc_obj->frame);
646             This->doc_obj->frame = NULL;
647             This->doc_obj->ui_active = FALSE;
648             return hres;
649         }
650
651         if(This->doc_obj->hostui) {
652             hres = IDocHostUIHandler_ShowUI(This->doc_obj->hostui,
653                     This->doc_obj->usermode == EDITMODE ? DOCHOSTUITYPE_AUTHOR : DOCHOSTUITYPE_BROWSE,
654                     ACTOBJ(This), CMDTARGET(This), This->doc_obj->frame, This->doc_obj->ip_window);
655             if(FAILED(hres))
656                 IDocHostUIHandler_HideUI(This->doc_obj->hostui);
657         }
658
659         if(This->doc_obj->ip_window)
660             call_set_active_object(This->doc_obj->ip_window, ACTOBJ(This));
661
662         memset(&rcBorderWidths, 0, sizeof(rcBorderWidths));
663         IOleInPlaceFrame_SetBorderSpace(This->doc_obj->frame, &rcBorderWidths);
664
665         This->doc_obj->ui_active = TRUE;
666     }else {
667         if(This->doc_obj->ui_active) {
668             This->doc_obj->ui_active = FALSE;
669             if(This->doc_obj->ip_window)
670                 call_set_active_object(This->doc_obj->ip_window, NULL);
671             if(This->doc_obj->frame)
672                 call_set_active_object((IOleInPlaceUIWindow*)This->doc_obj->frame, NULL);
673             if(This->doc_obj->hostui)
674                 IDocHostUIHandler_HideUI(This->doc_obj->hostui);
675             if(This->doc_obj->ipsite)
676                 IOleInPlaceSite_OnUIDeactivate(This->doc_obj->ipsite, FALSE);
677         }
678     }
679     return S_OK;
680 }
681
682 static HRESULT WINAPI OleDocumentView_Open(IOleDocumentView *iface)
683 {
684     HTMLDocument *This = DOCVIEW_THIS(iface);
685     FIXME("(%p)\n", This);
686     return E_NOTIMPL;
687 }
688
689 static HRESULT WINAPI OleDocumentView_CloseView(IOleDocumentView *iface, DWORD dwReserved)
690 {
691     HTMLDocument *This = DOCVIEW_THIS(iface);
692     TRACE("(%p)->(%x)\n", This, dwReserved);
693
694     if(dwReserved)
695         WARN("dwReserved = %d\n", dwReserved);
696
697     /* NOTE:
698      * Windows implementation calls QueryInterface(IID_IOleCommandTarget),
699      * QueryInterface(IID_IOleControlSite) and KillTimer
700      */
701
702     IOleDocumentView_Show(iface, FALSE);
703
704     return S_OK;
705 }
706
707 static HRESULT WINAPI OleDocumentView_SaveViewState(IOleDocumentView *iface, LPSTREAM pstm)
708 {
709     HTMLDocument *This = DOCVIEW_THIS(iface);
710     FIXME("(%p)->(%p)\n", This, pstm);
711     return E_NOTIMPL;
712 }
713
714 static HRESULT WINAPI OleDocumentView_ApplyViewState(IOleDocumentView *iface, LPSTREAM pstm)
715 {
716     HTMLDocument *This = DOCVIEW_THIS(iface);
717     FIXME("(%p)->(%p)\n", This, pstm);
718     return E_NOTIMPL;
719 }
720
721 static HRESULT WINAPI OleDocumentView_Clone(IOleDocumentView *iface, IOleInPlaceSite *pIPSiteNew,
722                                         IOleDocumentView **ppViewNew)
723 {
724     HTMLDocument *This = DOCVIEW_THIS(iface);
725     FIXME("(%p)->(%p %p)\n", This, pIPSiteNew, ppViewNew);
726     return E_NOTIMPL;
727 }
728
729 #undef DOCVIEW_THIS
730
731 static const IOleDocumentViewVtbl OleDocumentViewVtbl = {
732     OleDocumentView_QueryInterface,
733     OleDocumentView_AddRef,
734     OleDocumentView_Release,
735     OleDocumentView_SetInPlaceSite,
736     OleDocumentView_GetInPlaceSite,
737     OleDocumentView_GetDocument,
738     OleDocumentView_SetRect,
739     OleDocumentView_GetRect,
740     OleDocumentView_SetRectComplex,
741     OleDocumentView_Show,
742     OleDocumentView_UIActivate,
743     OleDocumentView_Open,
744     OleDocumentView_CloseView,
745     OleDocumentView_SaveViewState,
746     OleDocumentView_ApplyViewState,
747     OleDocumentView_Clone
748 };
749
750 /**********************************************************
751  * IViewObject implementation
752  */
753
754 #define VIEWOBJ_THIS(iface) DEFINE_THIS(HTMLDocument, ViewObjectEx, iface)
755
756 static HRESULT WINAPI ViewObject_QueryInterface(IViewObjectEx *iface, REFIID riid, void **ppvObject)
757 {
758     HTMLDocument *This = VIEWOBJ_THIS(iface);
759     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
760 }
761
762 static ULONG WINAPI ViewObject_AddRef(IViewObjectEx *iface)
763 {
764     HTMLDocument *This = VIEWOBJ_THIS(iface);
765     return IHTMLDocument2_AddRef(HTMLDOC(This));
766 }
767
768 static ULONG WINAPI ViewObject_Release(IViewObjectEx *iface)
769 {
770     HTMLDocument *This = VIEWOBJ_THIS(iface);
771     return IHTMLDocument2_Release(HTMLDOC(This));
772 }
773
774 static HRESULT WINAPI ViewObject_Draw(IViewObjectEx *iface, DWORD dwDrawAspect, LONG lindex, void *pvAspect,
775         DVTARGETDEVICE *ptd, HDC hdcTargetDev, HDC hdcDraw, LPCRECTL lprcBounds,
776         LPCRECTL lprcWBounds, BOOL (CALLBACK *pfnContinue)(ULONG_PTR dwContinue), ULONG_PTR dwContinue)
777 {
778     HTMLDocument *This = VIEWOBJ_THIS(iface);
779     FIXME("(%p)->(%d %d %p %p %p %p %p %p %p %ld)\n", This, dwDrawAspect, lindex, pvAspect,
780             ptd, hdcTargetDev, hdcDraw, lprcBounds, lprcWBounds, pfnContinue, dwContinue);
781     return E_NOTIMPL;
782 }
783
784 static HRESULT WINAPI ViewObject_GetColorSet(IViewObjectEx *iface, DWORD dwDrawAspect, LONG lindex, void *pvAspect,
785         DVTARGETDEVICE *ptd, HDC hicTargetDev, LOGPALETTE **ppColorSet)
786 {
787     HTMLDocument *This = VIEWOBJ_THIS(iface);
788     FIXME("(%p)->(%d %d %p %p %p %p)\n", This, dwDrawAspect, lindex, pvAspect, ptd, hicTargetDev, ppColorSet);
789     return E_NOTIMPL;
790 }
791
792 static HRESULT WINAPI ViewObject_Freeze(IViewObjectEx *iface, DWORD dwDrawAspect, LONG lindex,
793         void *pvAspect, DWORD *pdwFreeze)
794 {
795     HTMLDocument *This = VIEWOBJ_THIS(iface);
796     FIXME("(%p)->(%d %d %p %p)\n", This, dwDrawAspect, lindex, pvAspect, pdwFreeze);
797     return E_NOTIMPL;
798 }
799
800 static HRESULT WINAPI ViewObject_Unfreeze(IViewObjectEx *iface, DWORD dwFreeze)
801 {
802     HTMLDocument *This = VIEWOBJ_THIS(iface);
803     FIXME("(%p)->(%d)\n", This, dwFreeze);
804     return E_NOTIMPL;
805 }
806
807 static HRESULT WINAPI ViewObject_SetAdvise(IViewObjectEx *iface, DWORD aspects, DWORD advf, IAdviseSink *pAdvSink)
808 {
809     HTMLDocument *This = VIEWOBJ_THIS(iface);
810     FIXME("(%p)->(%d %d %p)\n", This, aspects, advf, pAdvSink);
811     return E_NOTIMPL;
812 }
813
814 static HRESULT WINAPI ViewObject_GetAdvise(IViewObjectEx *iface, DWORD *pAspects, DWORD *pAdvf, IAdviseSink **ppAdvSink)
815 {
816     HTMLDocument *This = VIEWOBJ_THIS(iface);
817     FIXME("(%p)->(%p %p %p)\n", This, pAspects, pAdvf, ppAdvSink);
818     return E_NOTIMPL;
819 }
820
821 static HRESULT WINAPI ViewObject_GetExtent(IViewObjectEx *iface, DWORD dwDrawAspect, LONG lindex,
822                                 DVTARGETDEVICE* ptd, LPSIZEL lpsizel)
823 {
824     HTMLDocument *This = VIEWOBJ_THIS(iface);
825     FIXME("(%p)->(%d %d %p %p)\n", This, dwDrawAspect, lindex, ptd, lpsizel);
826     return E_NOTIMPL;
827 }
828
829 static HRESULT WINAPI ViewObject_GetRect(IViewObjectEx *iface, DWORD dwAspect, LPRECTL pRect)
830 {
831     HTMLDocument *This = VIEWOBJ_THIS(iface);
832     FIXME("(%p)->(%d %p)\n", This, dwAspect, pRect);
833     return E_NOTIMPL;
834 }
835
836 static HRESULT WINAPI ViewObject_GetViewStatus(IViewObjectEx *iface, DWORD *pdwStatus)
837 {
838     HTMLDocument *This = VIEWOBJ_THIS(iface);
839     FIXME("(%p)->(%p)\n", This, pdwStatus);
840     return E_NOTIMPL;
841 }
842
843 static HRESULT WINAPI ViewObject_QueryHitPoint(IViewObjectEx* iface, DWORD dwAspect,
844         LPCRECT pRectBounds, POINT ptlLoc, LONG lCloseHint, DWORD *pHitResult)
845 {
846     HTMLDocument *This = VIEWOBJ_THIS(iface);
847     FIXME("(%p)->(%d %p (%d %d) %d %p)\n", This, dwAspect, pRectBounds, ptlLoc.x,
848          ptlLoc.y, lCloseHint, pHitResult);
849     return E_NOTIMPL;
850 }
851
852 static HRESULT WINAPI ViewObject_QueryHitRect(IViewObjectEx *iface, DWORD dwAspect,
853         LPCRECT pRectBounds, LPCRECT pRectLoc, LONG lCloseHint, DWORD *pHitResult)
854 {
855     HTMLDocument *This = VIEWOBJ_THIS(iface);
856     FIXME("(%p)->(%d %p %p %d %p)\n", This, dwAspect, pRectBounds, pRectLoc, lCloseHint, pHitResult);
857     return E_NOTIMPL;
858 }
859
860 static HRESULT WINAPI ViewObject_GetNaturalExtent(IViewObjectEx *iface, DWORD dwAspect, LONG lindex,
861         DVTARGETDEVICE *ptd, HDC hicTargetDev, DVEXTENTINFO *pExtentInfo, LPSIZEL pSizel)
862 {
863     HTMLDocument *This = VIEWOBJ_THIS(iface);
864     FIXME("(%p)->(%d %d %p %p %p %p\n", This, dwAspect,lindex, ptd,
865             hicTargetDev, pExtentInfo, pSizel);
866     return E_NOTIMPL;
867 }
868
869 #undef VIEWOBJ_THIS
870
871 static const IViewObjectExVtbl ViewObjectVtbl = {
872     ViewObject_QueryInterface,
873     ViewObject_AddRef,
874     ViewObject_Release,
875     ViewObject_Draw,
876     ViewObject_GetColorSet,
877     ViewObject_Freeze,
878     ViewObject_Unfreeze,
879     ViewObject_SetAdvise,
880     ViewObject_GetAdvise,
881     ViewObject_GetExtent,
882     ViewObject_GetRect,
883     ViewObject_GetViewStatus,
884     ViewObject_QueryHitPoint,
885     ViewObject_QueryHitRect,
886     ViewObject_GetNaturalExtent
887 };
888
889 void HTMLDocument_View_Init(HTMLDocument *This)
890 {
891     This->lpOleDocumentViewVtbl = &OleDocumentViewVtbl;
892     This->lpViewObjectExVtbl = &ViewObjectVtbl;
893 }