1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #if defined(_MSC_VER) && (_MSC_VER > 1310)
28 #pragma warning(disable : 4917 4555)
29 #endif
30 
31 
32 #include "xwin.hxx"
33 #include "docholder.hxx"
34 #include "embeddoc.hxx"
35 #include "intercept.hxx"
36 #include "syswinwrapper.hxx"
37 #include "iipaobj.hxx"
38 
39 #include "common.h"
40 #include <Windows.h>
41 #include <com/sun/star/lang/SystemDependent.hpp>
42 #include <com/sun/star/awt/XSystemChildFactory.hpp>
43 #ifndef _COM_SUN_STAR_AWT_XSYSTEMDEPENDENTWINDOWPERR_HPP_
44 #include <com/sun/star/awt/XSystemDependentWindowPeer.hpp>
45 #endif
46 #include <com/sun/star/awt/XSystemDependentMenuPeer.hpp>
47 #include <com/sun/star/ui/XUIElement.hpp>
48 #include <com/sun/star/awt/WindowAttribute.hpp>
49 #include <com/sun/star/awt/XWindow.hpp>
50 #include <com/sun/star/frame/XComponentLoader.hpp>
51 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
52 #include <com/sun/star/util/XCloseBroadcaster.hpp>
53 #include <com/sun/star/util/XCloseAble.hpp>
54 #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACESS_HPP_
55 #include <com/sun/star/container/XNameAccess.hpp>
56 #endif
57 #include <com/sun/star/beans/XPropertySet.hpp>
58 #include <com/sun/star/frame/XModel.hpp>
59 #include <com/sun/star/frame/XDesktop.hpp>
60 #include <com/sun/star/frame/XFramesSupplier.hpp>
61 #include <com/sun/star/frame/FrameSearchFlag.hpp>
62 #include <com/sun/star/frame/XStatusListener.hpp>
63 #include <com/sun/star/util/XModifyBroadcaster.hpp>
64 #include <com/sun/star/frame/XDispatchProviderInterception.hpp>
65 #include <com/sun/star/awt/XTopWindow.hpp>
66 #include <com/sun/star/awt/PosSize.hpp>
67 #include <com/sun/star/awt/XView.hpp>
68 #include <com/sun/star/bridge/XBridgeSupplier2.hpp>
69 #include <com/sun/star/bridge/ModelDependent.hpp>
70 #include <com/sun/star/embed/EmbedMapUnits.hpp>
71 #include <com/sun/star/embed/XVisualObject.hpp>
72 #include <com/sun/star/document/MacroExecMode.hpp>
73 #include <com/sun/star/task/XInteractionHandler.hpp>
74 #include <osl/diagnose.h>
75 #include <rtl/process.h>
76 
77 using namespace ::com::sun::star;
78 
79 extern ::rtl::OUString	getFilterNameFromGUID_Impl( GUID* );
80 
81 // add mutex locking ???
82 
83 DocumentHolder::DocumentHolder(
84 	const uno::Reference<lang::XMultiServiceFactory >& xFactory,
85 	const ::rtl::Reference< EmbeddedDocumentInstanceAccess_Impl >& xOleAccess )
86 	:
87 	m_bAllowInPlace(true),
88 	m_pIOleIPSite(0),
89 	m_pIOleIPFrame(0),
90 	m_pIOleIPUIWindow(0),
91 	m_pCHatchWin(0),
92 	m_xOleAccess( xOleAccess ),
93 	m_pInterceptor(0),
94 	m_xFactory( xFactory ),
95     m_bOnDeactivate(false),
96     m_hWndxWinParent(NULL),
97     m_hWndxWinCont(NULL),
98     m_nMenuHandle(NULL),
99     m_nMenuShared(NULL),
100     m_nOLEMenu(NULL),
101     m_nMacroExecMode( document::MacroExecMode::USE_CONFIG ),
102 	m_bLink( sal_False )
103 {
104 	static const ::rtl::OUString aServiceName (
105 		RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.frame.Desktop" ) );
106 	uno::Reference< frame::XDesktop > xDesktop(
107 		m_xFactory->createInstance( aServiceName ),
108 		uno::UNO_QUERY );
109 	if ( xDesktop.is() )
110 		xDesktop->addTerminateListener( (frame::XTerminateListener*)this );
111 }
112 
113 
114 DocumentHolder::~DocumentHolder()
115 {
116 	delete m_pCHatchWin;
117 
118 	ClearInterceptorInternally();
119 }
120 
121 
122 void DocumentHolder::LoadDocInFrame( sal_Bool bPluginMode )
123 {
124     uno::Reference<frame::XComponentLoader> xComponentLoader(
125         m_xFrame,uno::UNO_QUERY);
126     if( xComponentLoader.is() && m_xDocument.is() )
127     {
128         uno::Reference< task::XInteractionHandler > xHandler(
129             m_xFactory->createInstance(
130                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.task.InteractionHandler" ) ) ),
131             uno::UNO_QUERY );
132 
133         uno::Any aAny;
134         sal_Int32 nLen = 3;
135         uno::Sequence<beans::PropertyValue> aSeq( nLen );
136 
137         aAny <<= uno::Reference<uno::XInterface>(
138             m_xDocument, uno::UNO_QUERY);
139         aSeq[0] = beans::PropertyValue(
140             rtl::OUString(
141                 RTL_CONSTASCII_USTRINGPARAM("Model")),
142             -1,
143             aAny,
144             beans::PropertyState_DIRECT_VALUE);
145 
146         aAny <<= sal_False;
147         aSeq[1] = beans::PropertyValue(
148             rtl::OUString(
149                 RTL_CONSTASCII_USTRINGPARAM("ReadOnly")),
150             -1,
151             aAny,
152             beans::PropertyState_DIRECT_VALUE);
153 
154         aAny <<= (sal_Bool) sal_True;
155         aSeq[2] = beans::PropertyValue(
156             rtl::OUString(
157                 RTL_CONSTASCII_USTRINGPARAM("NoAutoSave")),
158             -1,
159             aAny,
160             beans::PropertyState_DIRECT_VALUE);
161 
162         if ( bPluginMode )
163         {
164             aSeq.realloc( ++nLen );
165             aAny <<= (sal_Int16) 3;
166             aSeq[nLen-1] = beans::PropertyValue(
167                 rtl::OUString(
168                     RTL_CONSTASCII_USTRINGPARAM("PluginMode")),
169                 -1,
170                 aAny,
171                 beans::PropertyState_DIRECT_VALUE);
172         }
173 
174         if ( xHandler.is() )
175         {
176             aSeq.realloc( nLen+=2 );
177             aAny <<= xHandler;
178             aSeq[nLen-2] = beans::PropertyValue(
179                 rtl::OUString(
180                     RTL_CONSTASCII_USTRINGPARAM("InteractionHandler")),
181                 -1,
182                 aAny,
183                 beans::PropertyState_DIRECT_VALUE);
184 
185             aAny <<= m_nMacroExecMode;
186             aSeq[nLen-1] = beans::PropertyValue(
187                 rtl::OUString(
188                     RTL_CONSTASCII_USTRINGPARAM("MacroExecutionMode")),
189                 -1,
190                 aAny,
191                 beans::PropertyState_DIRECT_VALUE);
192         }
193 
194         xComponentLoader->loadComponentFromURL(
195             rtl::OUString(
196                 RTL_CONSTASCII_USTRINGPARAM("private:object")),
197             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("_self")),
198             0,
199             aSeq);
200 
201         uno::Sequence< beans::PropertyValue > aResArgs = m_xDocument->getArgs();
202         for ( int nInd = 0; nInd < aResArgs.getLength(); nInd++ )
203             if ( aResArgs[nInd].Name.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MacroExecutionMode" ) ) ) )
204             {
205                 aResArgs[nInd].Value >>= m_nMacroExecMode;
206                 break;
207             }
208     }
209 }
210 
211 void DocumentHolder::OnPosRectChanged(LPRECT lpRect) const
212 {
213     lpRect->left += m_aBorder.left;
214     lpRect->right -= m_aBorder.right;
215     lpRect->top += m_aBorder.top;
216     lpRect->bottom -= m_aBorder.bottom;
217     if(m_pIOleIPSite)
218         m_pIOleIPSite->OnPosRectChange(lpRect);
219 }
220 
221 
222 
223 void DocumentHolder::DisableInplaceActivation(BOOL b)
224 {
225 	m_bAllowInPlace = ! b;
226 }
227 
228 BOOL DocumentHolder::isActive() const
229 {
230 	return m_pIOleIPSite != 0;
231 }
232 
233 HRESULT DocumentHolder::InPlaceActivate(
234 	LPOLECLIENTSITE pActiveSite,
235 	BOOL fIncludeUI)
236 {
237 	m_bOnDeactivate = false;
238 
239 	if(!m_bAllowInPlace)
240 		return ERROR;
241 
242     HRESULT                 hr;
243     HWND                    hWndSite;
244     RECT                    rcPos;
245     RECT                    rcClip;
246     OLEINPLACEFRAMEINFO     frameInfo;
247 
248     if (NULL==pActiveSite)
249         return ResultFromScode(E_INVALIDARG);
250 
251     if (NULL!=m_pIOleIPSite)
252 	{
253         if (fIncludeUI)
254             UIActivate();
255 
256         return NOERROR;
257 	}
258 
259     if ( !m_xDocument.is() )
260         return ERROR;
261 
262     //1.  Initialization, obtaining interfaces, OnInPlaceActivate.
263     hr=pActiveSite->QueryInterface(
264 		IID_IOleInPlaceSite,
265 		(void**) &m_pIOleIPSite);
266 
267     if (FAILED(hr))
268         return hr;
269 
270     hr=m_pIOleIPSite->CanInPlaceActivate();
271 
272     if (NOERROR!=hr)
273 	{
274         m_pIOleIPSite->Release(), m_pIOleIPSite=NULL;
275         return ResultFromScode(E_FAIL);
276 	}
277 
278     m_pIOleIPSite->OnInPlaceActivate();
279 
280     //2. Get the site window
281     //3. and determine container frame and
282     //   document window for tools and menus, as well
283     //   as frameInfo for accelerators
284     m_pIOleIPSite->GetWindow(&hWndSite);
285 
286     frameInfo.cb=sizeof(OLEINPLACEFRAMEINFO);
287     m_pIOleIPSite->GetWindowContext(
288 		&m_pIOleIPFrame,&m_pIOleIPUIWindow,&rcPos,&rcClip,&frameInfo);
289 
290 	// initialize the office as, with hwnd as parentwindow
291     uno::Any                      aAny;
292     uno::Sequence<sal_Int8> aProcessIdent(16);
293     rtl_getGlobalProcessId((sal_uInt8*)aProcessIdent.getArray());
294 
295     try
296     {
297         if(!m_xEditWindow.is())
298         {   // determine XWindow and window handle of parent
299             HWND                          hWndxWinParent(0);
300             uno::Reference<awt::XWindow>  xWin;
301 
302             static const ::rtl::OUString aToolkitServiceName(
303                 RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.awt.Toolkit" ) );
304             uno::Reference<awt::XSystemChildFactory> xToolkit(
305                 m_xFactory->createInstance(aToolkitServiceName ),uno::UNO_QUERY);
306 
307             if(xToolkit.is()) {
308                 // create system window wrapper for hwnd
309                 if( !m_pCHatchWin )
310                     m_pCHatchWin = new winwrap::CHatchWin(
311                         m_hInstance,this);
312 
313                 if(m_pCHatchWin->Init(hWndSite,/*ID_HATCHWINDOW*/2000, NULL)) {
314                     m_pCHatchWin->RectsSet(&rcPos,&rcClip); //set visible area
315                     hWndxWinParent = m_pCHatchWin->Window();
316                     ShowWindow(hWndxWinParent,SW_SHOW);  //Make visible.
317                 }
318                 else {
319                     // no success initializing hatch window
320                     delete m_pCHatchWin, m_pCHatchWin = 0;
321                     hWndxWinParent = hWndSite;
322                 }
323 
324                 aAny <<= sal_Int32(hWndxWinParent);
325                 xWin = uno::Reference<awt::XWindow>(
326                     xToolkit->createSystemChild(
327                         aAny,
328                         aProcessIdent,
329                         lang::SystemDependent::SYSTEM_WIN32),
330                     uno::UNO_QUERY);
331             }
332 
333             if(xWin.is()) {
334                 xWin->setPosSize(
335                     m_pCHatchWin ? HATCHWIN_BORDERWIDTHDEFAULT : 0,
336                     m_pCHatchWin ? HATCHWIN_BORDERWIDTHDEFAULT : 0,
337                     rcPos.right-rcPos.left,
338                     rcPos.bottom - rcPos.top,
339                     awt::PosSize::POSSIZE);
340                 xWin->setVisible(sal_True);
341 
342                 m_xEditWindow = xWin;
343                 m_hWndxWinParent = hWndxWinParent;
344             }
345             else
346                 return ERROR;
347         }
348         else {
349             if(m_hWndxWinParent) {
350                 SetParent(m_hWndxWinParent,hWndSite);
351                 ShowWindow(m_hWndxWinParent,SW_SHOW);  //Make visible.
352             }
353 
354             if ( !m_xFrame.is() )
355                 // initially set size to "empty", this guarantees that the final resize
356                 // is always executed (will be done by "SetObjectRects" after getting internal border)
357                 m_xEditWindow->setPosSize(
358                     0,
359                     0,
360                     0,
361                     0,
362                     awt::PosSize::POSSIZE);
363             m_xEditWindow->setVisible(sal_True);
364         }
365 
366         if(m_xContainerWindow.is()) {
367             if(m_hWndxWinCont) {
368                 if(m_pIOleIPFrame) {
369                     HWND  hWndCont;
370                     m_pIOleIPFrame->GetWindow(&hWndCont);
371                     SetParent(m_hWndxWinCont,hWndCont);
372                     ShowWindow(m_hWndxWinCont,SW_SHOW);
373                 }
374             }
375             m_xContainerWindow->setVisible(true);
376         }
377 
378         if(m_xFrame.is())
379             m_xFrame->activate();
380         else {
381             // create frame and initialize it with with the created window
382             static const ::rtl::OUString aFrameServiceName(
383                 RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.frame.Frame" ) );
384             m_xFrame = uno::Reference<frame::XFrame>(
385                 m_xFactory->createInstance(aFrameServiceName),
386                 uno::UNO_QUERY);
387 
388             if(!m_xFrame.is())
389                 return ERROR;
390 
391             m_xFrame->initialize(m_xEditWindow);
392 
393             uno::Reference<frame::XDispatchProviderInterception>
394                 xDPI(m_xFrame,uno::UNO_QUERY);
395             if(xDPI.is())
396                 xDPI->registerDispatchProviderInterceptor( CreateNewInterceptor() );
397 
398             uno::Reference<beans::XPropertySet> xPS(m_xFrame,uno::UNO_QUERY);
399             if( xPS.is() )
400             {
401                 aAny = xPS->getPropertyValue(
402                     rtl::OUString::createFromAscii("LayoutManager"));
403                 aAny >>= m_xLayoutManager;
404             }
405 
406             if(m_xLayoutManager.is())
407                 m_xLayoutManager->setDockingAreaAcceptor(this);
408 
409             // load the model into the frame
410             LoadDocInFrame( sal_True );
411 
412             static const ::rtl::OUString aDesktopServiceName (
413                 RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.frame.Desktop" ) );
414             uno::Reference< frame::XFramesSupplier > xDesktop(
415                 m_xFactory->createInstance( aDesktopServiceName ),
416                 uno::UNO_QUERY );
417             if(xDesktop.is())
418                 xDesktop->getFrames()->append(m_xFrame);
419 
420             // determine the menuhandle to get menutitems.
421             if(m_xLayoutManager.is()) {
422                 uno::Reference< ::com::sun::star::ui::XUIElement > xUIEl(
423                     m_xLayoutManager->getElement(
424                         rtl::OUString::createFromAscii(
425                             "private:resource/menubar/menubar")));
426                 OSL_ENSURE(xUIEl.is(),"no menubar");
427                 uno::Reference<awt::XSystemDependentMenuPeer> xSDMP(
428                     xUIEl->getRealInterface(),
429                     uno::UNO_QUERY);
430                 aAny = xSDMP->getMenuHandle(
431                     aProcessIdent,lang::SystemDependent::SYSTEM_WIN32);
432                 sal_Int32 tmp;
433                 if( aAny >>= tmp )
434                     m_nMenuHandle = HMENU(tmp);
435                 m_xLayoutManager->hideElement(
436                     rtl::OUString(
437                         RTL_CONSTASCII_USTRINGPARAM(
438                             "private:resource/menubar/menubar" )));
439             }
440         }
441 
442         // TODO/cd: Workaround for status indicator bug. It always makes the
443         // document window visible, when someone tries to use the status
444         // indicator. As we save our document when we get the deactivation
445         // from OLE this conflict to hide floating windows.
446         if(m_xLayoutManager.is())
447             m_xLayoutManager->setVisible(true);
448 
449         // get document border and resize rects according to border
450         GetDocumentBorder( &m_aBorder );
451         SetObjectRects( &rcPos, &rcClip );
452 
453         if ( m_xOleAccess.is() )
454         {
455             LockedEmbedDocument_Impl aDocLock = m_xOleAccess->GetEmbedDocument();
456             if ( aDocLock.GetEmbedDocument() )
457                 aDocLock.GetEmbedDocument()->ShowObject();
458         }
459 
460         // setTitle(m_aDocumentNamePart);
461         if (fIncludeUI)
462             hr=UIActivate();
463 
464         m_pIOleIPSite->DiscardUndoState();
465     }
466     catch( uno::Exception& )
467     {
468         hr = ERROR;
469     }
470 
471     return hr;
472 }
473 
474 
475 void DocumentHolder::InPlaceDeactivate(void)
476 {
477     m_bOnDeactivate = true;
478 
479 	UIDeactivate();
480 	if(m_xFrame.is()) m_xFrame->deactivate();
481 
482     if(m_xEditWindow.is()) {
483         m_xEditWindow->setVisible(false);
484         ShowWindow(m_hWndxWinParent,SW_HIDE);
485         SetParent(m_hWndxWinParent,0);
486     }
487 
488     if(m_xContainerWindow.is()) {
489         m_xContainerWindow->setVisible(false);
490         ShowWindow(m_hWndxWinCont,SW_HIDE);
491         SetParent(m_hWndxWinCont,0);
492     }
493 
494     // TODO/cd: Workaround for status indicator bug. It always makes the
495     // document window visible, when someone tries to use the status
496     // indicator. As we save our document when we get the deactivation
497     // from OLE this conflict to hide floating windows.
498     if (m_xLayoutManager.is())
499         m_xLayoutManager->setVisible(false);
500 
501     if (NULL!=m_pIOleIPSite)
502     {
503         // The following workaround should let the object be stored in case of inplace editing
504 //        CComPtr< IOleClientSite > pClientSite;
505 //
506 //        m_pIOleIPSite->QueryInterface(
507 //		        IID_IOleClientSite, (void**)&pClientSite );
508 //        if ( pClientSite )
509 //            pClientSite->SaveObject();
510 
511         m_pIOleIPSite->OnInPlaceDeactivate();
512     }
513 
514     if(m_pIOleIPFrame) m_pIOleIPFrame->Release(); m_pIOleIPFrame = 0;
515     if(m_pIOleIPUIWindow) m_pIOleIPUIWindow->Release(); m_pIOleIPUIWindow = 0;
516 	if(m_pIOleIPSite) m_pIOleIPSite->Release(); m_pIOleIPSite = 0;
517 
518 	if ( m_xOleAccess.is() )
519 	{
520 		LockedEmbedDocument_Impl aDocLock = m_xOleAccess->GetEmbedDocument();
521 		if ( aDocLock.GetEmbedDocument() )
522         {
523             aDocLock.GetEmbedDocument()->SaveObject();
524         }
525 	}
526 
527     return;
528 }
529 
530 
531 HRESULT DocumentHolder::UIActivate()
532 {
533     // 1.  Call IOleInPlaceSite::UIActivate
534     if (NULL!=m_pIOleIPSite)
535         m_pIOleIPSite->OnUIActivate();
536 
537     //2.  Critical for accelerators to work initially.
538     SetFocus(m_pCHatchWin->Window());
539 	// if(m_xEditWindow.is()) m_xEditWindow->setFocus();
540 
541     //3.  Set the active object
542 
543     OLECHAR starOffice[] = {'S','t','a','r','O','f','f','i','c','e',0};
544 	CComPtr< IOleInPlaceActiveObject > pObj = new CIIAObj( this );
545 
546     if (NULL!=m_pIOleIPFrame)
547         m_pIOleIPFrame->SetActiveObject(
548 			pObj, starOffice );
549 
550     if (NULL!=m_pIOleIPUIWindow)
551 		m_pIOleIPUIWindow->SetActiveObject(
552 			pObj, starOffice );
553 
554     //4.  Create the shared menu.
555     InPlaceMenuCreate();
556 
557 	return NOERROR;
558 }
559 
560 void DocumentHolder::UIDeactivate()
561 {
562     //1.  Remove the shared menu.
563     InPlaceMenuDestroy();
564 
565     if (NULL!=m_pIOleIPFrame)
566         m_pIOleIPFrame->SetActiveObject(NULL, NULL);
567 
568     if (NULL!=m_pIOleIPUIWindow)
569         m_pIOleIPUIWindow->SetActiveObject(NULL, NULL);
570 
571     //3.  Call IOleInPlaceSite::OnUIDeactivate
572     if (NULL!=m_pIOleIPSite)
573         m_pIOleIPSite->OnUIDeactivate(FALSE);
574 
575     return;
576 }
577 
578 void CopyToOLEMenu(HMENU hOrig,WORD origPos,HMENU hDest,WORD destPos)
579 {
580     HMENU subMenu(NULL);
581     UINT uTemp = MF_BYPOSITION | MF_POPUP;
582     char buffer[256];
583 
584     subMenu = GetSubMenu(hOrig,origPos);
585     GetMenuString(hOrig,origPos,buffer,256,MF_BYPOSITION);
586     InsertMenu(hDest,destPos,uTemp,
587                (UINT)subMenu,LPCTSTR(buffer));
588 
589     MENUITEMINFOW mi;
590     memset(&mi,0,sizeof(mi));
591     mi.cbSize = sizeof(mi);
592     mi.fMask = MIIM_DATA;
593     if(GetMenuItemInfoW(hOrig,origPos,TRUE,&mi))
594         SetMenuItemInfoW(hDest,(WORD)destPos,TRUE,&mi);
595 }
596 
597 BOOL DocumentHolder::InPlaceMenuCreate(void)
598 {
599     HMENU               hMenu;
600     UINT                i;
601     OLEMENUGROUPWIDTHS  mgw;
602 
603     for (i=0; i<6; i++)
604         mgw.width[i]=0;
605 
606     //We already have popup menu handles in m_pFR->m_phMenu[]
607 
608     //Create the new shared menu and let container do its thing
609     hMenu=CreateMenu();
610     m_pIOleIPFrame->InsertMenus(hMenu,&mgw);
611 
612     int count = GetMenuItemCount(m_nMenuHandle);
613     int help = count-1;
614 
615     // start with 1, because we don't include "File"
616     WORD pos = (WORD)mgw.width[0];
617     CopyToOLEMenu(m_nMenuHandle,1,hMenu,pos);
618     mgw.width[1] = 1;
619 
620     // insert object menu here
621     pos = ((WORD)(mgw.width[0] + mgw.width[1] + mgw.width[2]));
622     for(WORD i = 2; i < help-1; ++i,++pos)
623         CopyToOLEMenu(m_nMenuHandle,i,hMenu,pos);
624     mgw.width[3] = help - 3;
625 
626     // insert help menu
627     pos = (WORD)(mgw.width[0] + mgw.width[1] + mgw.width[2] +
628                  mgw.width[3] + mgw.width[4]);
629     CopyToOLEMenu(m_nMenuHandle,WORD(help),hMenu,pos);
630     mgw.width[5] = 1;
631 
632     m_nMenuShared = hMenu;
633     m_nOLEMenu = OleCreateMenuDescriptor(m_nMenuShared,&mgw);
634 
635     uno::Reference<awt::XSystemDependentWindowPeer> xSysDepWin(m_xContainerWindow,uno::UNO_QUERY);
636     if(xSysDepWin.is()) {
637         uno::Sequence<sal_Int8> aProcessIdent(16);
638         rtl_getGlobalProcessId((sal_uInt8*)aProcessIdent.getArray());
639         uno::Any aAny = xSysDepWin->getWindowHandle(aProcessIdent,lang::SystemDependent::SYSTEM_WIN32);
640         sal_Int32 tmp;
641         aAny >>= tmp;
642         HWND aHwnd = (HWND) tmp;
643         m_pIOleIPFrame->SetMenu(
644             m_nMenuShared,m_nOLEMenu,aHwnd);
645     }
646     else
647         m_pIOleIPFrame->SetMenu(
648             m_nMenuShared,m_nOLEMenu,::GetWindow(m_hWndxWinParent,GW_CHILD));
649     return TRUE;
650 }
651 
652 BOOL DocumentHolder::InPlaceMenuDestroy(void)
653 {
654     if( NULL == m_nMenuShared )
655         return TRUE;
656 
657     m_pIOleIPFrame->SetMenu(NULL,NULL,NULL);
658 
659     OleDestroyMenuDescriptor(m_nOLEMenu),m_nOLEMenu = NULL;
660 	return TRUE;
661 }
662 
663 void DocumentHolder::OpenIntoWindow(void)
664 {
665 	// not implemented
666 }
667 
668 BOOL DocumentHolder::Undo(void)
669 {
670 	// not implemented
671 	return false;
672 }
673 
674 
675 void DocumentHolder::FreeOffice()
676 {
677 	const ::rtl::OUString aServiceName(
678 		RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.frame.Desktop" ) );
679 	uno::Reference< frame::XDesktop > xDesktop(
680 		m_xFactory->createInstance( aServiceName ), uno::UNO_QUERY );
681 	if ( xDesktop.is() )
682 	{
683 		xDesktop->removeTerminateListener(
684 			(frame::XTerminateListener*)this );
685 	}
686 }
687 
688 void DocumentHolder::DisconnectFrameDocument( sal_Bool bComplete )
689 {
690 	try
691 	{
692 		uno::Reference< util::XModifyBroadcaster > xModifiable( m_xDocument, uno::UNO_QUERY_THROW );
693 		xModifiable->removeModifyListener( (util::XModifyListener*)this );
694 	}
695 	catch( uno::Exception& )
696 	{}
697 
698 	try
699 	{
700 		uno::Reference< util::XCloseBroadcaster > xBroadcaster(
701 			m_xDocument, uno::UNO_QUERY_THROW );
702 		xBroadcaster->removeCloseListener( (util::XCloseListener*)this );
703 	}
704 	catch( uno::Exception& )
705 	{}
706 
707 	try
708 	{
709 		uno::Reference< util::XCloseBroadcaster > xBroadcaster(
710 			m_xFrame, uno::UNO_QUERY_THROW );
711 		xBroadcaster->removeCloseListener( (util::XCloseListener*)this );
712 	}
713 	catch( uno::Exception& )
714 	{}
715 
716     if ( bComplete )
717     {
718         m_xFrame = uno::Reference< frame::XFrame>();
719 	    m_pIDispatch = NULL;
720 	    m_xDocument = uno::Reference< frame::XModel >();
721     }
722 }
723 
724 void DocumentHolder::CloseDocument()
725 {
726 	DisconnectFrameDocument();
727 
728 	uno::Reference< util::XCloseable > xCloseable(
729 			m_xDocument, uno::UNO_QUERY );
730 
731 	if ( xCloseable.is() )
732 	{
733 		try
734 		{
735 			xCloseable->close( sal_True );
736 		}
737 		catch( uno::Exception& )
738 		{}
739 	}
740 
741 	m_pIDispatch = NULL;
742 	m_xDocument = uno::Reference< frame::XModel >();
743 }
744 
745 
746 void DocumentHolder::CloseFrame()
747 {
748 	try
749 	{
750 		uno::Reference< util::XCloseBroadcaster > xBroadcaster(
751 			m_xFrame, uno::UNO_QUERY_THROW );
752 		xBroadcaster->removeCloseListener( (util::XCloseListener*)this );
753 	}
754 	catch( uno::Exception& )
755 	{}
756 
757 	uno::Reference<util::XCloseable> xCloseable(
758 		m_xFrame,uno::UNO_QUERY);
759 	if(xCloseable.is())
760 		try {
761 			xCloseable->close(sal_True);
762 		}
763 		catch( const uno::Exception& ) {
764 		}
765 	else {
766 		uno::Reference<lang::XComponent> xComp(m_xFrame,uno::UNO_QUERY);
767 		if(xComp.is())
768 			xComp->dispose();
769 	}
770 
771 	m_xFrame = uno::Reference< frame::XFrame >();
772 }
773 
774 void DocumentHolder::SetDocument( const uno::Reference< frame::XModel >& xDoc, sal_Bool bLink )
775 {
776 	if ( m_xDocument.is() )
777 		CloseDocument();
778 
779 	m_xDocument = xDoc;
780 	m_bLink = bLink;
781 
782 	uno::Reference< util::XCloseBroadcaster > xBroadcaster(
783 		m_xDocument, uno::UNO_QUERY );
784 
785 	if ( xBroadcaster.is() )
786 		xBroadcaster->addCloseListener( (util::XCloseListener*)this );
787 
788 	if ( m_xDocument.is() && !m_bLink )
789 	{
790 		// set the document mode to embedded
791 		uno::Sequence< beans::PropertyValue > aSeq(1);
792 		aSeq[0].Name = ::rtl::OUString::createFromAscii( "SetEmbedded" );
793 		aSeq[0].Value <<= sal_True;
794 		m_xDocument->attachResource(::rtl::OUString(),aSeq);
795 	}
796 }
797 
798 sal_Bool DocumentHolder::ExecuteSuspendCloseFrame()
799 {
800 	if ( m_xFrame.is() && m_xFactory.is() )
801 	{
802 		try
803 		{
804 			uno::Reference< frame::XController > xController = m_xFrame->getController();
805 			if ( xController.is() )
806 			{
807 				if ( !xController->suspend( sal_True ) )
808 					return sal_False;
809 
810 				FreeOffice();
811 				try
812 				{
813 					uno::Reference<util::XCloseable> xCloseable( m_xFrame, uno::UNO_QUERY );
814 					if ( xCloseable.is() )
815 						xCloseable->close(sal_True);
816 					else
817 					{
818 						uno::Reference<lang::XComponent> xComp( m_xFrame, uno::UNO_QUERY_THROW );
819 						if( xComp.is() )
820 							xComp->dispose();
821 					}
822 				}
823 				catch( const util::CloseVetoException& )
824 				{
825 					// should be called if the frame could not be closed
826 					xController->suspend( sal_False );
827 				}
828 			}
829 		}
830 		catch( uno::Exception& )
831 		{
832 		}
833 
834 		m_xFrame = uno::Reference< frame::XFrame >();
835 	}
836 
837 	return sal_True;
838 }
839 
840 uno::Reference< frame::XFrame > DocumentHolder::DocumentFrame()
841 {
842 	if(! m_xFrame.is() )
843 	{
844 		rtl::OUString aDesktopSrvNm(
845 			RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop"));
846 
847 		uno::Reference<frame::XDesktop> xDesktop(
848 			m_xFactory->createInstance(aDesktopSrvNm),
849 			uno::UNO_QUERY);
850 
851 		uno::Reference<frame::XFrame> xFrame(
852 			xDesktop,uno::UNO_QUERY);
853 
854 		// the frame will be registered on desktop here, later when the document
855 		// is loaded into the frame in ::show() method the terminate listener will be removed
856 		// this is so only for outplace activation
857 		if( xFrame.is() )
858 			m_xFrame = xFrame->findFrame(
859 				rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("_blank")),0);
860 
861 		uno::Reference< util::XCloseBroadcaster > xBroadcaster(
862 			m_xFrame, uno::UNO_QUERY );
863 
864 		if ( xBroadcaster.is() )
865 		{
866 			xBroadcaster->addCloseListener( (util::XCloseListener*)this );
867 			FreeOffice(); // the frame is part of the desktop
868 		}
869 	}
870 
871 	if( m_xFrame.is() )
872 	{
873 		// intercept
874 		uno::Reference<frame::XDispatchProviderInterception>
875 			xDPI(m_xFrame,uno::UNO_QUERY);
876 		if(xDPI.is())
877 			xDPI->registerDispatchProviderInterceptor( CreateNewInterceptor() );
878 	}
879 
880 	return m_xFrame;
881 }
882 
883 
884 uno::Reference< frame::XDispatchProviderInterceptor > DocumentHolder::CreateNewInterceptor()
885 {
886 	::osl::MutexGuard aGuard( m_aMutex );
887 
888 	ClearInterceptorInternally();
889 
890 	uno::Reference< frame::XDispatchProviderInterceptor > xInterceptor( m_pInterceptor = new Interceptor( m_xOleAccess, this, m_bLink ) );
891 	m_xInterceptorLocker = xInterceptor;
892 	return xInterceptor;
893 }
894 
895 void DocumentHolder::ClearInterceptorInternally()
896 {
897 	::osl::MutexGuard aGuard( m_aMutex );
898 	uno::Reference< frame::XDispatchProviderInterceptor > xInterceptor( m_xInterceptorLocker );
899 	if ( xInterceptor.is() && m_pInterceptor )
900 		m_pInterceptor->DisconnectDocHolder();
901 
902 	m_xInterceptorLocker = uno::Reference< frame::XDispatchProviderInterceptor >();
903 	m_pInterceptor = 0;
904 }
905 
906 void DocumentHolder::ClearInterceptor()
907 {
908 	::osl::MutexGuard aGuard( m_aMutex );
909 	m_xInterceptorLocker = uno::Reference< frame::XDispatchProviderInterceptor >();
910 	m_pInterceptor = 0;
911 }
912 
913 
914 void DocumentHolder::show()
915 {
916     try
917     {
918         if(m_xFrame.is())
919         {
920             m_xFrame->activate();
921             uno::Reference<awt::XTopWindow> xTopWindow(
922                 m_xFrame->getContainerWindow(),uno::UNO_QUERY);
923             if(xTopWindow.is())
924                 xTopWindow->toFront();
925         }
926         else if( DocumentFrame().is() )
927         {
928             LoadDocInFrame( sal_False );
929 
930             // get rid of second closer if it is there
931             uno::Reference< beans::XPropertySet > xProps( m_xFrame, uno::UNO_QUERY );
932             if ( xProps.is() )
933             {
934                 uno::Reference< frame::XLayoutManager > xLayoutManager;
935                 xProps->getPropertyValue( rtl::OUString::createFromAscii( "LayoutManager" ) ) >>= xLayoutManager;
936                 uno::Reference< beans::XPropertySet > xLMProps( xLayoutManager, uno::UNO_QUERY );
937                 if ( xLMProps.is() )
938                 {
939                     xLMProps->setPropertyValue( ::rtl::OUString::createFromAscii( "MenuBarCloser" ),
940                                                 uno::makeAny( uno::Reference< frame::XStatusListener >() ) );
941                 }
942             }
943 
944             if ( !m_bLink )
945             {
946                 try
947                 {
948                     uno::Reference< util::XModifyBroadcaster > xModifiable( m_xDocument, uno::UNO_QUERY_THROW );
949                     xModifiable->addModifyListener( (util::XModifyListener*)this );
950                 }
951                 catch( uno::Exception& )
952                 {}
953             }
954 
955             if ( !m_bLink )
956                 setTitle(m_aDocumentNamePart);
957         }
958 	}
959     catch( uno::Exception& )
960     {
961         OSL_ENSURE( sal_False, "Can not show the frame!\n" );
962     }
963 
964 }
965 
966 void DocumentHolder::resizeWin( const SIZEL& rNewSize )
967 {
968 	LockedEmbedDocument_Impl aDocLock;
969 
970 	if ( m_xOleAccess.is() )
971 		aDocLock = m_xOleAccess->GetEmbedDocument();
972 
973 	if ( m_xFrame.is() && aDocLock.GetEmbedDocument() )
974 	{
975 		uno::Reference< awt::XWindow > xWindow(
976 			m_xFrame->getContainerWindow(), uno::UNO_QUERY );
977 		uno::Reference< awt::XView > xView( xWindow, uno::UNO_QUERY );
978 
979 		if ( xWindow.is() && xView.is() )
980 		{
981 			float fScale = 1;
982 			xView->setZoom( fScale, fScale );
983 
984 			SIZEL aOldSize;
985 			GetExtent( &aOldSize );
986 
987 			if ( aOldSize.cx != rNewSize.cx || aOldSize.cy != rNewSize.cy )
988 			{
989 				HDC hdc = GetDC( NULL );
990 				SetMapMode( hdc, MM_HIMETRIC );
991 
992 				POINT aOldOffset;
993 				aOldOffset.x = aOldSize.cx;
994 				aOldOffset.y = aOldSize.cy;
995 				BOOL bIsOk = LPtoDP( hdc, &aOldOffset, 1 );
996 
997 				POINT aNewOffset;
998 				aNewOffset.x = rNewSize.cx;
999 				aNewOffset.y = rNewSize.cy;
1000 				bIsOk = LPtoDP( hdc, &aNewOffset, 1 );
1001 
1002 				ReleaseDC( NULL, hdc );
1003 
1004 				awt::Rectangle aWinRect = xWindow->getPosSize();
1005 
1006 				sal_Int32 aWidthDelta = aWinRect.Width - aOldOffset.x;
1007 				sal_Int32 aHeightDelta = aWinRect.Height - aOldOffset.y;
1008 
1009 				if ( aWidthDelta > 0 && aHeightDelta > 0 )
1010 					xWindow->setPosSize(0,
1011 										0,
1012 										aNewOffset.x + aWidthDelta,
1013 										aNewOffset.y + aHeightDelta,
1014 										awt::PosSize::SIZE );
1015 			}
1016 		}
1017 	}
1018 }
1019 
1020 void DocumentHolder::setTitle(const rtl::OUString& aDocumentName)
1021 {
1022 	if(m_xFrame.is())
1023 	{
1024 		if(m_aFilterName.getLength() == 0)
1025 		{
1026 			rtl::OUString aFilterName;
1027 			uno::Sequence<beans::PropertyValue> aSeq;
1028 			if(m_xDocument.is())
1029 			{
1030 				aSeq =
1031 					m_xDocument->getArgs();
1032 				for(sal_Int32 j = 0; j < aSeq.getLength(); ++j)
1033 				{
1034 					if(aSeq[j].Name ==
1035 					   rtl::OUString(
1036 						   RTL_CONSTASCII_USTRINGPARAM("FilterName")))
1037 					{
1038 						aSeq[j].Value >>= aFilterName;
1039 						break;
1040 					}
1041 				}
1042 			}
1043 
1044 			if(aFilterName.getLength())
1045 			{
1046 				uno::Reference<container::XNameAccess> xNameAccess(
1047 					m_xFactory->createInstance(
1048 						rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
1049 							"com.sun.star.document.FilterFactory"))),
1050 					uno::UNO_QUERY);
1051 				try {
1052 					if(xNameAccess.is() &&
1053 					   (xNameAccess->getByName(aFilterName) >>= aSeq))
1054 					{
1055 						for(sal_Int32 j = 0; j < aSeq.getLength(); ++j)
1056 							if(aSeq[j].Name ==
1057 							   rtl::OUString(
1058 								   RTL_CONSTASCII_USTRINGPARAM("UIName")))
1059 							{
1060 								aSeq[j].Value >>= m_aFilterName;
1061 								break;
1062 							}
1063 					}
1064 				}
1065 				catch(const uno::Exception& ) {
1066 					// nothing better to do here
1067 					m_aFilterName = aFilterName;
1068 				}
1069 			}
1070 		}
1071 		// set the title
1072 		uno::Reference<beans::XPropertySet> xPropSet(
1073 			m_xFrame,uno::UNO_QUERY);
1074 		if(xPropSet.is()) {
1075 			uno::Any aAny;
1076 			static const sal_Unicode u[] = { ' ','(',0 };
1077 			static const sal_Unicode c[] = { ')',0 };
1078 			rtl::OUString aTotalName(m_aFilterName);
1079 			aTotalName += rtl::OUString(u);
1080 			aTotalName += aDocumentName;
1081 			aTotalName += rtl::OUString(c);
1082 			aAny <<= aTotalName;
1083 			try {
1084 				xPropSet->setPropertyValue(
1085 					rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Title")),
1086 					aAny);
1087 			}
1088 			catch( const uno::Exception& ) {
1089 			}
1090 		}
1091 	}
1092 
1093 	m_aDocumentNamePart = aDocumentName;
1094 
1095 	if(m_pInterceptor)
1096 	{
1097 		::osl::ClearableMutexGuard aGuard( m_aMutex );
1098 
1099 		Interceptor* pTmpInter = NULL;
1100 		uno::Reference< frame::XDispatchProviderInterceptor > xLock( m_xInterceptorLocker );
1101 		if ( xLock.is() && m_pInterceptor )
1102 			pTmpInter = m_pInterceptor;
1103 
1104 		aGuard.clear();
1105 
1106 		if ( pTmpInter )
1107 			pTmpInter->generateFeatureStateEvent();
1108 	}
1109 }
1110 
1111 
1112 void DocumentHolder::setContainerName(const rtl::OUString& aContainerName)
1113 {
1114 	m_aContainerName = aContainerName;
1115 }
1116 
1117 
1118 void DocumentHolder::hide()
1119 {
1120 	if(m_xFrame.is()) m_xFrame->deactivate();
1121 
1122 	//todo: sendadvise
1123 	// after hiding the window it is always allowed to InPlaceActivate it
1124 	m_bAllowInPlace = true;
1125 }
1126 
1127 IDispatch* DocumentHolder::GetIDispatch()
1128 {
1129 	if ( !m_pIDispatch && m_xDocument.is() )
1130 	{
1131 		const ::rtl::OUString aServiceName (
1132 			RTL_CONSTASCII_USTRINGPARAM (
1133 				"com.sun.star.bridge.OleBridgeSupplier2" ) );
1134 		uno::Reference< bridge::XBridgeSupplier2 > xSupplier(
1135 			m_xFactory->createInstance( aServiceName ), uno::UNO_QUERY );
1136 
1137 		if ( xSupplier.is() )
1138 		{
1139 			uno::Sequence< sal_Int8 > aProcId( 16 );
1140 			rtl_getGlobalProcessId( (sal_uInt8*)aProcId.getArray() );
1141 
1142 			try {
1143 				uno::Any anyResult = xSupplier->createBridge(
1144 					uno::makeAny( m_xDocument ),
1145 					aProcId,
1146 					bridge::ModelDependent::UNO,
1147 					bridge::ModelDependent::OLE );
1148 
1149 				if ( anyResult.getValueTypeClass() ==
1150 					 getCppuType((sal_uInt32*) 0).getTypeClass() )
1151 				{
1152 					VARIANT* pVariant = *(VARIANT**)anyResult.getValue();
1153 					if ( pVariant->vt == VT_DISPATCH )
1154 						m_pIDispatch = pVariant->pdispVal;
1155 
1156 					VariantClear( pVariant );
1157 					CoTaskMemFree( pVariant );
1158 				}
1159 			}
1160 			catch ( uno::Exception& )
1161 			{}
1162 		}
1163 	}
1164 
1165 	return m_pIDispatch;
1166 }
1167 
1168 #if 0
1169 HRESULT DocumentHolder::SetVisArea( const RECTL *pRect )
1170 {
1171 	if ( pRect && m_xDocument.is() )
1172 	{
1173 		uno::Sequence< beans::PropertyValue > aArgs = m_xDocument->getArgs();
1174 		for ( sal_Int32 nInd = 0; nInd < aArgs.getLength(); nInd++ )
1175 			if ( aArgs[nInd].Name.equalsAscii( "WinExtent" ) )
1176 			{
1177 				// should allways be there
1178 				uno::Sequence< sal_Int32 > aRect(4);
1179 
1180 				aRect[0] = pRect->left;
1181 				aRect[1] = pRect->top;
1182 				aRect[2] = pRect->right;
1183 				aRect[3] = pRect->bottom;
1184 
1185 				aArgs[nInd].Value <<= aRect;
1186 
1187 				m_xDocument->attachResource( m_xDocument->getURL(), aArgs );
1188 				return S_OK;
1189 			}
1190 
1191 		OSL_ENSURE( sal_False, "WinExtent seems not to be implemented!\n" );
1192 	}
1193 
1194 	return E_FAIL;
1195 }
1196 
1197 HRESULT DocumentHolder::GetVisArea( RECTL *pRect )
1198 {
1199 	if ( pRect && m_xDocument.is() )
1200 	{
1201 		uno::Sequence< beans::PropertyValue > aArgs = m_xDocument->getArgs();
1202 		for ( sal_Int32 nInd = 0; nInd < aArgs.getLength(); nInd++ )
1203 			if ( aArgs[nInd].Name.equalsAscii( "WinExtent" ) )
1204 			{
1205 				uno::Sequence< sal_Int32 > aRect;
1206 				if ( ( aArgs[nInd].Value >>= aRect ) && aRect.getLength() == 4 )
1207 				{
1208 					pRect->left   = aRect[0];
1209 					pRect->top    = aRect[1];
1210 					pRect->right  = aRect[2];
1211 					pRect->bottom = aRect[3];
1212 
1213 					return S_OK;
1214 				}
1215 
1216 				break;
1217 			}
1218 	}
1219 
1220 	return E_FAIL;
1221 }
1222 #endif
1223 
1224 HRESULT DocumentHolder::GetDocumentBorder( RECT *pRect )
1225 {
1226 	if ( pRect && m_xDocument.is() )
1227 	{
1228 		uno::Sequence< beans::PropertyValue > aArgs = m_xDocument->getArgs();
1229 		for ( sal_Int32 nInd = 0; nInd < aArgs.getLength(); nInd++ )
1230             if ( aArgs[nInd].Name.equalsAscii( "DocumentBorder" ) )
1231 			{
1232 				uno::Sequence< sal_Int32 > aRect;
1233 				if ( ( aArgs[nInd].Value >>= aRect ) && aRect.getLength() == 4 )
1234 				{
1235 					pRect->left   = aRect[0];
1236 					pRect->top    = aRect[1];
1237 					pRect->right  = aRect[2];
1238 					pRect->bottom = aRect[3];
1239 
1240 					return S_OK;
1241 				}
1242 
1243 				break;
1244 			}
1245 	}
1246 
1247 	return E_FAIL;
1248 }
1249 
1250 HRESULT DocumentHolder::SetExtent( const SIZEL *pSize )
1251 {
1252 	if ( pSize )
1253 	{
1254 		uno::Reference< embed::XVisualObject > xVisObj( m_xDocument, uno::UNO_QUERY );
1255 		if ( xVisObj.is() )
1256 		{
1257 			try
1258 			{
1259 				awt::Size aNewSize( pSize->cx, pSize->cy );
1260 
1261 				sal_Int32 aMapMode = xVisObj->getMapUnit( DVASPECT_CONTENT );
1262 
1263 				// TODO/LATER: in future UNO API should be used for the conversion, currently there is no
1264 				if ( aMapMode == embed::EmbedMapUnits::TWIP )
1265 				{
1266 					// convertion from ONE_100TH_MM
1267 					aNewSize.Width = aNewSize.Width * 144 / 254;
1268 					aNewSize.Height = aNewSize.Height * 144 / 254;
1269 				}
1270 
1271 
1272 				xVisObj->setVisualAreaSize( DVASPECT_CONTENT, aNewSize );
1273 
1274 				return S_OK;
1275 			}
1276 			catch( uno::Exception& )
1277 			{}
1278 		}
1279 	}
1280 
1281 	return E_FAIL;
1282 }
1283 
1284 HRESULT DocumentHolder::GetExtent( SIZEL *pSize )
1285 {
1286 	if ( pSize )
1287 	{
1288 		uno::Reference< embed::XVisualObject > xVisObj( m_xDocument, uno::UNO_QUERY );
1289 		if ( xVisObj.is() )
1290 		{
1291 			try
1292 			{
1293 				awt::Size aDocSize = xVisObj->getVisualAreaSize( DVASPECT_CONTENT );
1294 
1295 				sal_Int32 aMapMode = xVisObj->getMapUnit( DVASPECT_CONTENT );
1296 
1297 				// TODO/LATER: in future UNO API should be used for the conversion, currently there is no
1298 				if ( aMapMode == embed::EmbedMapUnits::TWIP )
1299 				{
1300 					// convertion to ONE_100TH_MM
1301 					aDocSize.Width = aDocSize.Width * 254 / 144;
1302 					aDocSize.Height = aDocSize.Height * 254 / 144;
1303 				}
1304 
1305 				pSize->cx = aDocSize.Width;
1306 				pSize->cy = aDocSize.Height;
1307 
1308 				return S_OK;
1309 			}
1310 			catch( uno::Exception& )
1311 			{}
1312 		}
1313 	}
1314 
1315 	return E_FAIL;
1316 }
1317 
1318 
1319 HRESULT DocumentHolder::SetContRects(LPCRECT aRect)
1320 {
1321 	if(m_xContainerWindow.is()) {
1322 		RECT wi;
1323         memset(&wi,0,sizeof(wi));
1324 		if(m_pIOleIPFrame) {
1325 			m_pIOleIPFrame->GetBorder((LPRECT)&wi);
1326 			m_xContainerWindow->setPosSize(
1327 				0,0,
1328 				wi.right - wi.left,
1329 				wi.bottom - wi.top,
1330 				awt::PosSize::POSSIZE);
1331 		}
1332 		else
1333 	       m_xContainerWindow->setPosSize(
1334 			0,0,
1335             aRect->right - aRect->left,
1336             aRect->bottom - aRect->top,
1337             awt::PosSize::POSSIZE);
1338         return NOERROR;
1339     }
1340     else {
1341         return ERROR;
1342     }
1343 }
1344 
1345 
1346 HRESULT DocumentHolder::SetObjectRects(LPCRECT aRect, LPCRECT aClip)
1347 {
1348     ((LPRECT)aRect)->left -= m_aBorder.left;
1349     ((LPRECT)aRect)->right += m_aBorder.right;
1350     ((LPRECT)aRect)->top -= m_aBorder.top;
1351     ((LPRECT)aRect)->bottom += m_aBorder.bottom;
1352     ((LPRECT)aClip)->left -= m_aBorder.left;
1353     ((LPRECT)aClip)->right += m_aBorder.right;
1354     ((LPRECT)aClip)->top -= m_aBorder.top;
1355     ((LPRECT)aClip)->bottom += m_aBorder.bottom;
1356 
1357 	if(m_pCHatchWin)
1358 		m_pCHatchWin->RectsSet((LPRECT)aRect, (LPRECT)aClip);
1359 	if(m_xEditWindow.is()) {
1360         m_xEditWindow->setVisible(false);
1361         m_xEditWindow->setPosSize(
1362             m_pCHatchWin ? HATCHWIN_BORDERWIDTHDEFAULT : 0,
1363             m_pCHatchWin ? HATCHWIN_BORDERWIDTHDEFAULT : 0,
1364             aRect->right - aRect->left,
1365             aRect->bottom - aRect->top,
1366             awt::PosSize::POSSIZE);
1367         m_xEditWindow->setVisible(true);
1368     }
1369 	return NOERROR;
1370 }
1371 
1372 
1373 ::com::sun::star::uno::Reference<
1374     ::com::sun::star::awt::XWindow> SAL_CALL
1375 DocumentHolder::getContainerWindow(
1376 )
1377     throw (
1378         ::com::sun::star::uno::RuntimeException
1379     )
1380 {
1381     if(m_xContainerWindow.is())
1382         return m_xContainerWindow;
1383 
1384     uno::Reference<awt::XWindow> xWin(0);
1385 
1386     static const ::rtl::OUString aToolkitServiceName(
1387         RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.awt.Toolkit" ) );
1388     uno::Reference<awt::XSystemChildFactory> xToolkit(
1389         m_xFactory->createInstance(aToolkitServiceName ),uno::UNO_QUERY);
1390 
1391     if(xToolkit.is() && m_pIOleIPFrame) {
1392         HWND hWnd;
1393         m_pIOleIPFrame->GetWindow(&hWnd);
1394 
1395         uno::Sequence<sal_Int8> aProcessIdent(16);
1396         rtl_getGlobalProcessId((sal_uInt8*)aProcessIdent.getArray());
1397 
1398         uno::Any aAny;
1399         aAny <<= sal_Int32(hWnd);
1400         xWin = uno::Reference<awt::XWindow>(
1401             xToolkit->createSystemChild(
1402                 aAny,
1403                 aProcessIdent,
1404                 lang::SystemDependent::SYSTEM_WIN32),
1405             uno::UNO_QUERY);
1406 
1407         RECT wi;
1408         memset(&wi,0,sizeof(wi));
1409 		if(xWin.is() && m_pIOleIPFrame->GetBorder((LPRECT)&wi) == NOERROR) {
1410             xWin->setVisible(true);
1411             xWin->setPosSize(
1412                 0,0,
1413                 wi.right-wi.left,
1414                 wi.bottom - wi.top,
1415                 awt::PosSize::POSSIZE);
1416 
1417             uno::Reference<awt::XSystemDependentWindowPeer> xSysWin(
1418                 xWin,uno::UNO_QUERY);
1419             if(xSysWin.is()) {
1420                 aAny = xSysWin->getWindowHandle(
1421                     aProcessIdent,lang::SystemDependent::SYSTEM_WIN32);
1422                 sal_Int32 tmp;
1423                 if( aAny >>= tmp )
1424                     SetContainerWindowHandle((HWND) tmp);
1425             }
1426         }
1427     }
1428 
1429     m_xContainerWindow= xWin;
1430     return xWin;
1431 }
1432 
1433 
1434 
1435 sal_Bool SAL_CALL
1436 DocumentHolder::requestDockingAreaSpace(
1437     const ::com::sun::star::awt::Rectangle& RequestedSpace
1438 )
1439     throw(
1440         ::com::sun::star::uno::RuntimeException
1441     )
1442 {
1443     if(m_bOnDeactivate)
1444         return sal_True;
1445 
1446     BORDERWIDTHS bw;
1447     SetRect((LPRECT)&bw,
1448             RequestedSpace.X,RequestedSpace.Y,
1449             RequestedSpace.Width,RequestedSpace.Height);
1450     if( m_pIOleIPFrame )
1451         return m_pIOleIPFrame->RequestBorderSpace(&bw) == NOERROR ;
1452     else
1453         return sal_Bool(false);
1454 }
1455 
1456 
1457 void SAL_CALL
1458 DocumentHolder::setDockingAreaSpace(
1459     const ::com::sun::star::awt::Rectangle& BorderSpace
1460 )
1461     throw (
1462         ::com::sun::star::uno::RuntimeException
1463     )
1464 {
1465     if(m_bOnDeactivate)
1466         return;
1467 
1468     BORDERWIDTHS bw;
1469     SetRect((LPRECT)&bw,
1470             BorderSpace.X,BorderSpace.Y,
1471             BorderSpace.Width,BorderSpace.Height);
1472     if( m_pIOleIPFrame ) {
1473         RECT aRect;
1474         GetClientRect(m_hWndxWinCont,&aRect);
1475         HRGN hrgn1 = CreateRectRgn(
1476             0,0,
1477             aRect.right,BorderSpace.Y);
1478         HRGN hrgn2 = CreateRectRgn(aRect.right-BorderSpace.Width,0,aRect.right,aRect.bottom);
1479         CombineRgn(hrgn1,hrgn1,hrgn2,RGN_OR);
1480         DeleteObject(hrgn2);
1481         hrgn2 = CreateRectRgn(0,aRect.bottom-BorderSpace.Height,aRect.right,aRect.bottom);
1482         CombineRgn(hrgn1,hrgn1,hrgn2,RGN_OR);
1483         DeleteObject(hrgn2);
1484         hrgn2 = CreateRectRgn(0,0,BorderSpace.X,aRect.bottom);
1485         CombineRgn(hrgn1,hrgn1,hrgn2,RGN_OR);
1486         DeleteObject(hrgn2);
1487 
1488         SetWindowRgn(m_hWndxWinCont,hrgn1,true);
1489         // not:: DeleteObject(hrgn1);
1490         m_pIOleIPFrame->SetBorderSpace(&bw);
1491     }
1492 }
1493 
1494 
1495 void SAL_CALL
1496 DocumentHolder::disposing(
1497 	const com::sun::star::lang::EventObject& aSource
1498 )
1499 		throw( uno::RuntimeException )
1500 {
1501 	if ( m_xDocument.is() && m_xDocument == aSource.Source )
1502 	{
1503 		m_pIDispatch = NULL;
1504 		m_xDocument = uno::Reference< frame::XModel >();
1505 	}
1506 
1507 	if( m_xFrame.is() && m_xFrame == aSource.Source )
1508 		m_xFrame = uno::Reference< frame::XFrame >();
1509 }
1510 
1511 
1512 void SAL_CALL
1513 DocumentHolder::queryClosing(
1514 	const lang::EventObject& aSource,
1515 	sal_Bool /*bGetsOwnership*/
1516 )
1517 	throw(
1518 		util::CloseVetoException
1519 	)
1520 {
1521 	if ( !m_bLink
1522 	  && ( m_xDocument.is() && m_xDocument == aSource.Source || m_xFrame.is() && m_xFrame == aSource.Source ) )
1523 		throw util::CloseVetoException();
1524 }
1525 
1526 
1527 void SAL_CALL
1528 DocumentHolder::notifyClosing(
1529 	const lang::EventObject& aSource )
1530 		throw( uno::RuntimeException )
1531 {
1532 	try
1533 	{
1534 		uno::Reference< util::XCloseBroadcaster > xEventBroadcaster(
1535 			aSource.Source, uno::UNO_QUERY_THROW );
1536 		xEventBroadcaster->removeCloseListener( (util::XCloseListener*)this );
1537 	}
1538 	catch( uno::Exception& )
1539 	{}
1540 
1541 	if ( m_xDocument.is() && m_xDocument == aSource.Source )
1542 	{
1543 		// can happen only in case of links
1544 		m_pIDispatch = NULL;
1545 		m_xDocument = uno::Reference< frame::XModel >();
1546 		m_xFrame = uno::Reference< frame::XFrame >();
1547 
1548 		LockedEmbedDocument_Impl aDocLock = m_xOleAccess->GetEmbedDocument();
1549 		if ( aDocLock.GetEmbedDocument() )
1550             aDocLock.GetEmbedDocument()->OLENotifyClosing();
1551 	}
1552 	else if( m_xFrame.is() && m_xFrame == aSource.Source )
1553 		m_xFrame = uno::Reference< frame::XFrame >();
1554 }
1555 
1556 void SAL_CALL
1557 DocumentHolder::queryTermination(
1558     const lang::EventObject& /*aSource*/
1559 )
1560 	throw(
1561 		frame::TerminationVetoException
1562 	)
1563 {
1564 	if ( m_xDocument.is() )
1565 		throw frame::TerminationVetoException();
1566 }
1567 
1568 void SAL_CALL
1569 DocumentHolder::notifyTermination(
1570 	const lang::EventObject& aSource
1571 )
1572 		throw( uno::RuntimeException )
1573 {
1574 	OSL_ENSURE( !m_xDocument.is(), "Just a disaster..." );
1575 	uno::Reference< frame::XDesktop > xDesktop(
1576         aSource.Source, uno::UNO_QUERY );
1577 
1578 	if ( xDesktop.is() )
1579 		xDesktop->removeTerminateListener( (frame::XTerminateListener*)this );
1580 }
1581 
1582 
1583 
1584 void SAL_CALL DocumentHolder::modified( const lang::EventObject& /*aEvent*/ )
1585 	throw (uno::RuntimeException)
1586 {
1587 	if ( m_xOleAccess.is() )
1588 	{
1589 		LockedEmbedDocument_Impl aDocLock = m_xOleAccess->GetEmbedDocument();
1590 		if ( aDocLock.GetEmbedDocument() )
1591             aDocLock.GetEmbedDocument()->notify();
1592 	}
1593 }
1594 
1595 
1596 
1597 //     if(m_pOLEInterface->GetGUID() == OID_WriterTextServer) {
1598 //         // edit group
1599 //         CopyToOLEMenu(m_nMenuHandle,1,hMenu,(WORD)mgw.width[0]);
1600 //         CopyToOLEMenu(m_nMenuHandle,2,hMenu,1+(WORD)mgw.width[0]);
1601 //         CopyToOLEMenu(m_nMenuHandle,3,hMenu,2+(WORD)mgw.width[0]);
1602 //         CopyToOLEMenu(m_nMenuHandle,4,hMenu,3+(WORD)mgw.width[0]);
1603 //         mgw.width[1]=4;
1604 
1605 //         // object group
1606 //         CopyToOLEMenu(
1607 //             m_nMenuHandle,5,
1608 //             hMenu,4+(WORD)mgw.width[0]+(WORD)mgw.width[2]);
1609 //         mgw.width[3]=1;
1610 
1611 //         // help group
1612 //         CopyToOLEMenu(
1613 //             m_nMenuHandle,7,
1614 //             hMenu,5+(WORD)mgw.width[0]+(WORD)mgw.width[2]+(WORD)mgw.width[4]);
1615 //         mgw.width[5]=1;
1616 //     }
1617 //     else if(m_pOLEInterface->GetGUID() == OID_CalcServer) {
1618 //         // edit group
1619 //         CopyToOLEMenu(m_nMenuHandle,1,hMenu,(WORD)mgw.width[0]);
1620 //         CopyToOLEMenu(m_nMenuHandle,2,hMenu,1+(WORD)mgw.width[0]);
1621 //         CopyToOLEMenu(m_nMenuHandle,3,hMenu,2+(WORD)mgw.width[0]);
1622 //         CopyToOLEMenu(m_nMenuHandle,4,hMenu,3+(WORD)mgw.width[0]);
1623 //         mgw.width[1]=4;
1624 
1625 //         // object group
1626 //         CopyToOLEMenu(
1627 //             m_nMenuHandle,5,
1628 //             hMenu,4+(WORD)mgw.width[0]+(WORD)mgw.width[2]);
1629 //         CopyToOLEMenu(
1630 //             m_nMenuHandle,6,
1631 //             hMenu,5+(WORD)mgw.width[0]+(WORD)mgw.width[2]);
1632 //         mgw.width[3]=2;
1633 
1634 //         // help group
1635 //         CopyToOLEMenu(
1636 //             m_nMenuHandle,8,
1637 //             hMenu,6+(WORD)mgw.width[0]+(WORD)mgw.width[2]+(WORD)mgw.width[4]);
1638 //         mgw.width[5]=1;
1639 //     }
1640 
1641 // Fix strange warnings about some
1642 // ATL::CAxHostWindow::QueryInterface|AddRef|Releae functions.
1643 // warning C4505: 'xxx' : unreferenced local function has been removed
1644 #if defined(_MSC_VER)
1645 #pragma warning(disable: 4505)
1646 #endif
1647