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