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