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