update emoji autocorrect entries from po-files
[LibreOffice.git] / embedserv / source / embed / docholder.cxx
blobf7a6a6afc4b799dfb18e7df7002a48e7401b43da
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifdef _MSC_VER
20 #pragma warning(disable : 4917 4555)
21 #endif
24 #include "xwin.hxx"
25 #include "docholder.hxx"
26 #include "embeddoc.hxx"
27 #include "intercept.hxx"
28 #include "syswinwrapper.hxx"
29 #include "iipaobj.hxx"
31 #include "common.h"
32 #include <windows.h>
33 #include <com/sun/star/awt/XTopWindow.hpp>
34 #include <com/sun/star/awt/PosSize.hpp>
35 #include <com/sun/star/awt/XView.hpp>
36 #include <com/sun/star/awt/Toolkit.hpp>
37 #include <com/sun/star/awt/XSystemChildFactory.hpp>
38 #include <com/sun/star/awt/XSystemDependentWindowPeer.hpp>
39 #include <com/sun/star/awt/XSystemDependentMenuPeer.hpp>
40 #include <com/sun/star/awt/WindowAttribute.hpp>
41 #include <com/sun/star/awt/XWindow.hpp>
42 #include <com/sun/star/beans/XPropertySet.hpp>
43 #include <com/sun/star/bridge/XBridgeSupplier2.hpp>
44 #include <com/sun/star/bridge/ModelDependent.hpp>
45 #include <com/sun/star/container/XNameAccess.hpp>
46 #include <com/sun/star/document/MacroExecMode.hpp>
47 #include <com/sun/star/embed/EmbedMapUnits.hpp>
48 #include <com/sun/star/embed/XVisualObject.hpp>
49 #include <com/sun/star/frame/XComponentLoader.hpp>
50 #include <com/sun/star/frame/Frame.hpp>
51 #include <com/sun/star/frame/XModel.hpp>
52 #include <com/sun/star/frame/Desktop.hpp>
53 #include <com/sun/star/frame/XFramesSupplier.hpp>
54 #include <com/sun/star/frame/FrameSearchFlag.hpp>
55 #include <com/sun/star/frame/XStatusListener.hpp>
56 #include <com/sun/star/frame/XDispatchProviderInterception.hpp>
57 #include <com/sun/star/lang/SystemDependent.hpp>
58 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
59 #include <com/sun/star/task/InteractionHandler.hpp>
60 #include <com/sun/star/ui/XUIElement.hpp>
61 #include <com/sun/star/util/XCloseBroadcaster.hpp>
62 #include <com/sun/star/util/XCloseAble.hpp>
63 #include <com/sun/star/util/XModifyBroadcaster.hpp>
64 #include <comphelper/processfactory.hxx>
65 #include <osl/diagnose.h>
66 #include <rtl/process.h>
68 using namespace ::com::sun::star;
70 extern OUString getFilterNameFromGUID_Impl( GUID* );
72 // add mutex locking ???
74 DocumentHolder::DocumentHolder(
75 const uno::Reference<lang::XMultiServiceFactory >& xFactory,
76 const ::rtl::Reference< EmbeddedDocumentInstanceAccess_Impl >& xOleAccess )
78 m_bAllowInPlace(true),
79 m_pIOleIPSite(0),
80 m_pIOleIPFrame(0),
81 m_pIOleIPUIWindow(0),
82 m_pCHatchWin(0),
83 m_xOleAccess( xOleAccess ),
84 m_pInterceptor(0),
85 m_xFactory( xFactory ),
86 m_bOnDeactivate(false),
87 m_hWndxWinParent(NULL),
88 m_hWndxWinCont(NULL),
89 m_nMenuHandle(NULL),
90 m_nMenuShared(NULL),
91 m_nOLEMenu(NULL),
92 m_nMacroExecMode( document::MacroExecMode::USE_CONFIG ),
93 m_bLink( sal_False )
95 uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create(comphelper::getComponentContext(m_xFactory));
96 xDesktop->addTerminateListener( (frame::XTerminateListener*)this );
100 DocumentHolder::~DocumentHolder()
102 delete m_pCHatchWin;
104 ClearInterceptorInternally();
108 void DocumentHolder::LoadDocInFrame( sal_Bool bPluginMode )
110 uno::Reference<frame::XComponentLoader> xComponentLoader(
111 m_xFrame,uno::UNO_QUERY);
112 if( xComponentLoader.is() && m_xDocument.is() )
114 uno::Reference< task::XInteractionHandler2 > xHandler(
115 task::InteractionHandler::createWithParent(comphelper::getComponentContext(m_xFactory), 0) );
117 uno::Any aAny;
118 sal_Int32 nLen = 3;
119 uno::Sequence<beans::PropertyValue> aSeq( nLen );
121 aAny <<= uno::Reference<uno::XInterface>(
122 m_xDocument, uno::UNO_QUERY);
123 aSeq[0] = beans::PropertyValue(
124 OUString("Model"),
126 aAny,
127 beans::PropertyState_DIRECT_VALUE);
129 aAny <<= sal_False;
130 aSeq[1] = beans::PropertyValue(
131 OUString("ReadOnly"),
133 aAny,
134 beans::PropertyState_DIRECT_VALUE);
136 aAny <<= (sal_Bool) sal_True;
137 aSeq[2] = beans::PropertyValue(
138 OUString("NoAutoSave"),
140 aAny,
141 beans::PropertyState_DIRECT_VALUE);
143 if ( bPluginMode )
145 aSeq.realloc( ++nLen );
146 aAny <<= (sal_Int16) 3;
147 aSeq[nLen-1] = beans::PropertyValue(
148 OUString("PluginMode"),
150 aAny,
151 beans::PropertyState_DIRECT_VALUE);
154 aSeq.realloc( nLen+=2 );
155 aAny <<= xHandler;
156 aSeq[nLen-2] = beans::PropertyValue(
157 OUString("InteractionHandler"),
159 aAny,
160 beans::PropertyState_DIRECT_VALUE);
162 aAny <<= m_nMacroExecMode;
163 aSeq[nLen-1] = beans::PropertyValue(
164 OUString("MacroExecutionMode"),
166 aAny,
167 beans::PropertyState_DIRECT_VALUE);
169 xComponentLoader->loadComponentFromURL(
170 OUString("private:object"),
171 OUString("_self"),
173 aSeq);
175 uno::Sequence< beans::PropertyValue > aResArgs = m_xDocument->getArgs();
176 for ( int nInd = 0; nInd < aResArgs.getLength(); nInd++ )
177 if ( aResArgs[nInd].Name == "MacroExecutionMode" )
179 aResArgs[nInd].Value >>= m_nMacroExecMode;
180 break;
185 void DocumentHolder::OnPosRectChanged(LPRECT lpRect) const
187 lpRect->left += m_aBorder.left;
188 lpRect->right -= m_aBorder.right;
189 lpRect->top += m_aBorder.top;
190 lpRect->bottom -= m_aBorder.bottom;
191 if(m_pIOleIPSite)
192 m_pIOleIPSite->OnPosRectChange(lpRect);
197 void DocumentHolder::DisableInplaceActivation(BOOL b)
199 m_bAllowInPlace = ! b;
202 BOOL DocumentHolder::isActive() const
204 return m_pIOleIPSite != 0;
207 HRESULT DocumentHolder::InPlaceActivate(
208 LPOLECLIENTSITE pActiveSite,
209 BOOL fIncludeUI)
211 m_bOnDeactivate = false;
213 if(!m_bAllowInPlace)
214 return ERROR;
216 HRESULT hr;
217 HWND hWndSite;
218 RECT rcPos;
219 RECT rcClip;
220 OLEINPLACEFRAMEINFO frameInfo;
222 if (NULL==pActiveSite)
223 return ResultFromScode(E_INVALIDARG);
225 if (NULL!=m_pIOleIPSite)
227 if (fIncludeUI)
228 UIActivate();
230 return NOERROR;
233 if ( !m_xDocument.is() )
234 return ERROR;
236 //1. Initialization, obtaining interfaces, OnInPlaceActivate.
237 hr=pActiveSite->QueryInterface(
238 IID_IOleInPlaceSite,
239 (void**) &m_pIOleIPSite);
241 if (FAILED(hr))
242 return hr;
244 hr=m_pIOleIPSite->CanInPlaceActivate();
246 if (NOERROR!=hr)
248 m_pIOleIPSite->Release(), m_pIOleIPSite=NULL;
249 return ResultFromScode(E_FAIL);
252 m_pIOleIPSite->OnInPlaceActivate();
254 //2. Get the site window
255 //3. and determine container frame and
256 // document window for tools and menus, as well
257 // as frameInfo for accelerators
258 m_pIOleIPSite->GetWindow(&hWndSite);
260 frameInfo.cb=sizeof(OLEINPLACEFRAMEINFO);
261 m_pIOleIPSite->GetWindowContext(
262 &m_pIOleIPFrame,&m_pIOleIPUIWindow,&rcPos,&rcClip,&frameInfo);
264 // initialize the office as, with hwnd as parentwindow
265 uno::Any aAny;
266 uno::Sequence<sal_Int8> aProcessIdent(16);
267 rtl_getGlobalProcessId((sal_uInt8*)aProcessIdent.getArray());
271 if(!m_xEditWindow.is())
272 { // determine XWindow and window handle of parent
273 HWND hWndxWinParent(0);
274 uno::Reference<awt::XWindow> xWin;
276 uno::Reference<awt::XToolkit2> xToolkit =
277 awt::Toolkit::create(comphelper::getComponentContext(m_xFactory));
279 // create system window wrapper for hwnd
280 if( !m_pCHatchWin )
281 m_pCHatchWin = new winwrap::CHatchWin(
282 m_hInstance,this);
284 if(m_pCHatchWin->Init(hWndSite,/*ID_HATCHWINDOW*/2000, NULL)) {
285 m_pCHatchWin->RectsSet(&rcPos,&rcClip); //set visible area
286 hWndxWinParent = m_pCHatchWin->Window();
287 ShowWindow(hWndxWinParent,SW_SHOW); //Make visible.
289 else {
290 // no success initializing hatch window
291 delete m_pCHatchWin, m_pCHatchWin = 0;
292 hWndxWinParent = hWndSite;
295 aAny <<= sal_Int32(hWndxWinParent);
296 xWin = uno::Reference<awt::XWindow>(
297 xToolkit->createSystemChild(
298 aAny,
299 aProcessIdent,
300 lang::SystemDependent::SYSTEM_WIN32),
301 uno::UNO_QUERY);
303 if(xWin.is()) {
304 xWin->setPosSize(
305 m_pCHatchWin ? HATCHWIN_BORDERWIDTHDEFAULT : 0,
306 m_pCHatchWin ? HATCHWIN_BORDERWIDTHDEFAULT : 0,
307 rcPos.right-rcPos.left,
308 rcPos.bottom - rcPos.top,
309 awt::PosSize::POSSIZE);
310 xWin->setVisible(sal_True);
312 m_xEditWindow = xWin;
313 m_hWndxWinParent = hWndxWinParent;
315 else
316 return ERROR;
318 else {
319 if(m_hWndxWinParent) {
320 SetParent(m_hWndxWinParent,hWndSite);
321 ShowWindow(m_hWndxWinParent,SW_SHOW); //Make visible.
324 if ( !m_xFrame.is() )
325 // initially set size to "empty", this guarantees that the final resize
326 // is always executed (will be done by "SetObjectRects" after getting internal border)
327 m_xEditWindow->setPosSize(
332 awt::PosSize::POSSIZE);
333 m_xEditWindow->setVisible(sal_True);
336 if(m_xContainerWindow.is()) {
337 if(m_hWndxWinCont) {
338 if(m_pIOleIPFrame) {
339 HWND hWndCont;
340 m_pIOleIPFrame->GetWindow(&hWndCont);
341 SetParent(m_hWndxWinCont,hWndCont);
342 ShowWindow(m_hWndxWinCont,SW_SHOW);
345 m_xContainerWindow->setVisible(true);
348 if(m_xFrame.is())
349 m_xFrame->activate();
350 else {
351 // create frame and initialize it with the created window
352 m_xFrame = frame::Frame::create( comphelper::getComponentContext(m_xFactory) );
353 m_xFrame->initialize(m_xEditWindow);
355 m_xFrame->registerDispatchProviderInterceptor( CreateNewInterceptor() );
357 m_xLayoutManager.set( m_xFrame->getLayoutManager(), uno::UNO_QUERY );
359 if(m_xLayoutManager.is())
360 m_xLayoutManager->setDockingAreaAcceptor(this);
362 // load the model into the frame
363 LoadDocInFrame( sal_True );
365 uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create(comphelper::getComponentContext(m_xFactory));
366 xDesktop->getFrames()->append(m_xFrame);
368 // determine the menuhandle to get menutitems.
369 if(m_xLayoutManager.is()) {
370 uno::Reference< ::com::sun::star::ui::XUIElement > xUIEl(
371 m_xLayoutManager->getElement(
372 OUString(
373 "private:resource/menubar/menubar")));
374 OSL_ENSURE(xUIEl.is(),"no menubar");
375 uno::Reference<awt::XSystemDependentMenuPeer> xSDMP(
376 xUIEl->getRealInterface(),
377 uno::UNO_QUERY);
378 aAny = xSDMP->getMenuHandle(
379 aProcessIdent,lang::SystemDependent::SYSTEM_WIN32);
380 sal_Int32 tmp;
381 if( aAny >>= tmp )
382 m_nMenuHandle = HMENU(tmp);
383 m_xLayoutManager->hideElement(
384 OUString(
385 "private:resource/menubar/menubar" ));
389 // TODO/cd: Workaround for status indicator bug. It always makes the
390 // document window visible, when someone tries to use the status
391 // indicator. As we save our document when we get the deactivation
392 // from OLE this conflict to hide floating windows.
393 if(m_xLayoutManager.is())
394 m_xLayoutManager->setVisible(true);
396 // get document border and resize rects according to border
397 GetDocumentBorder( &m_aBorder );
398 SetObjectRects( &rcPos, &rcClip );
400 if ( m_xOleAccess.is() )
402 LockedEmbedDocument_Impl aDocLock = m_xOleAccess->GetEmbedDocument();
403 if ( aDocLock.GetEmbedDocument() )
404 aDocLock.GetEmbedDocument()->ShowObject();
407 // setTitle(m_aDocumentNamePart);
408 if (fIncludeUI)
409 hr=UIActivate();
411 m_pIOleIPSite->DiscardUndoState();
413 catch( const uno::Exception& )
415 hr = ERROR;
418 return hr;
422 void DocumentHolder::InPlaceDeactivate()
424 m_bOnDeactivate = true;
426 UIDeactivate();
427 if(m_xFrame.is()) m_xFrame->deactivate();
429 if(m_xEditWindow.is()) {
430 m_xEditWindow->setVisible(false);
431 ShowWindow(m_hWndxWinParent,SW_HIDE);
432 SetParent(m_hWndxWinParent,0);
435 if(m_xContainerWindow.is()) {
436 m_xContainerWindow->setVisible(false);
437 ShowWindow(m_hWndxWinCont,SW_HIDE);
438 SetParent(m_hWndxWinCont,0);
441 // TODO/cd: Workaround for status indicator bug. It always makes the
442 // document window visible, when someone tries to use the status
443 // indicator. As we save our document when we get the deactivation
444 // from OLE this conflict to hide floating windows.
445 if (m_xLayoutManager.is())
446 m_xLayoutManager->setVisible(false);
448 if (NULL!=m_pIOleIPSite)
449 m_pIOleIPSite->OnInPlaceDeactivate();
451 if(m_pIOleIPFrame) m_pIOleIPFrame->Release(); m_pIOleIPFrame = 0;
452 if(m_pIOleIPUIWindow) m_pIOleIPUIWindow->Release(); m_pIOleIPUIWindow = 0;
453 if(m_pIOleIPSite) m_pIOleIPSite->Release(); m_pIOleIPSite = 0;
455 if ( m_xOleAccess.is() )
457 LockedEmbedDocument_Impl aDocLock = m_xOleAccess->GetEmbedDocument();
458 if ( aDocLock.GetEmbedDocument() )
460 aDocLock.GetEmbedDocument()->SaveObject();
464 return;
468 HRESULT DocumentHolder::UIActivate()
470 // 1. Call IOleInPlaceSite::UIActivate
471 if (NULL!=m_pIOleIPSite)
472 m_pIOleIPSite->OnUIActivate();
474 //2. Critical for accelerators to work initially.
475 SetFocus(m_pCHatchWin->Window());
476 // if(m_xEditWindow.is()) m_xEditWindow->setFocus();
478 //3. Set the active object
480 OLECHAR starOffice[] = {'S','t','a','r','O','f','f','i','c','e',0};
481 CComPtr< IOleInPlaceActiveObject > pObj = new CIIAObj( this );
483 if (NULL!=m_pIOleIPFrame)
484 m_pIOleIPFrame->SetActiveObject(
485 pObj, starOffice );
487 if (NULL!=m_pIOleIPUIWindow)
488 m_pIOleIPUIWindow->SetActiveObject(
489 pObj, starOffice );
491 //4. Create the shared menu.
492 InPlaceMenuCreate();
494 return NOERROR;
497 void DocumentHolder::UIDeactivate()
499 //1. Remove the shared menu.
500 InPlaceMenuDestroy();
502 if (NULL!=m_pIOleIPFrame)
503 m_pIOleIPFrame->SetActiveObject(NULL, NULL);
505 if (NULL!=m_pIOleIPUIWindow)
506 m_pIOleIPUIWindow->SetActiveObject(NULL, NULL);
508 //3. Call IOleInPlaceSite::OnUIDeactivate
509 if (NULL!=m_pIOleIPSite)
510 m_pIOleIPSite->OnUIDeactivate(FALSE);
512 return;
515 void CopyToOLEMenu(HMENU hOrig,WORD origPos,HMENU hDest,WORD destPos)
517 HMENU subMenu(NULL);
518 UINT uTemp = MF_BYPOSITION | MF_POPUP;
519 char buffer[256];
521 subMenu = GetSubMenu(hOrig,origPos);
522 GetMenuString(hOrig,origPos,buffer,256,MF_BYPOSITION);
523 InsertMenu(hDest,destPos,uTemp,
524 (UINT)subMenu,LPCTSTR(buffer));
526 MENUITEMINFOW mi;
527 memset(&mi,0,sizeof(mi));
528 mi.cbSize = sizeof(mi);
529 mi.fMask = MIIM_DATA;
530 if(GetMenuItemInfoW(hOrig,origPos,TRUE,&mi))
531 SetMenuItemInfoW(hDest,(WORD)destPos,TRUE,&mi);
534 BOOL DocumentHolder::InPlaceMenuCreate()
536 HMENU hMenu;
537 UINT i;
538 OLEMENUGROUPWIDTHS mgw;
540 for (i=0; i<6; i++)
541 mgw.width[i]=0;
543 //We already have popup menu handles in m_pFR->m_phMenu[]
545 //Create the new shared menu and let container do its thing
546 hMenu=CreateMenu();
547 m_pIOleIPFrame->InsertMenus(hMenu,&mgw);
549 int count = GetMenuItemCount(m_nMenuHandle);
550 int help = count-1;
552 // start with 1, because we don't include "File"
553 WORD pos = (WORD)mgw.width[0];
554 CopyToOLEMenu(m_nMenuHandle,1,hMenu,pos);
555 mgw.width[1] = 1;
557 // insert object menu here
558 pos = ((WORD)(mgw.width[0] + mgw.width[1] + mgw.width[2]));
559 for(WORD i = 2; i < help-1; ++i,++pos)
560 CopyToOLEMenu(m_nMenuHandle,i,hMenu,pos);
561 mgw.width[3] = help - 3;
563 // insert help menu
564 pos = (WORD)(mgw.width[0] + mgw.width[1] + mgw.width[2] +
565 mgw.width[3] + mgw.width[4]);
566 CopyToOLEMenu(m_nMenuHandle,WORD(help),hMenu,pos);
567 mgw.width[5] = 1;
569 m_nMenuShared = hMenu;
570 m_nOLEMenu = OleCreateMenuDescriptor(m_nMenuShared,&mgw);
572 uno::Reference<awt::XSystemDependentWindowPeer> xSysDepWin(m_xContainerWindow,uno::UNO_QUERY);
573 if(xSysDepWin.is()) {
574 uno::Sequence<sal_Int8> aProcessIdent(16);
575 rtl_getGlobalProcessId((sal_uInt8*)aProcessIdent.getArray());
576 uno::Any aAny = xSysDepWin->getWindowHandle(aProcessIdent,lang::SystemDependent::SYSTEM_WIN32);
577 sal_Int32 tmp;
578 aAny >>= tmp;
579 HWND aHwnd = (HWND) tmp;
580 m_pIOleIPFrame->SetMenu(
581 m_nMenuShared,m_nOLEMenu,aHwnd);
583 else
584 m_pIOleIPFrame->SetMenu(
585 m_nMenuShared,m_nOLEMenu,::GetWindow(m_hWndxWinParent,GW_CHILD));
586 return TRUE;
589 BOOL DocumentHolder::InPlaceMenuDestroy()
591 if( NULL == m_nMenuShared )
592 return TRUE;
594 m_pIOleIPFrame->SetMenu(NULL,NULL,NULL);
596 OleDestroyMenuDescriptor(m_nOLEMenu),m_nOLEMenu = NULL;
597 return TRUE;
600 void DocumentHolder::OpenIntoWindow()
602 // not implemented
605 BOOL DocumentHolder::Undo()
607 // not implemented
608 return false;
612 void DocumentHolder::FreeOffice()
614 uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create(comphelper::getComponentContext(m_xFactory));
615 xDesktop->removeTerminateListener(
616 (frame::XTerminateListener*)this );
619 void DocumentHolder::DisconnectFrameDocument( sal_Bool bComplete )
623 uno::Reference< util::XModifyBroadcaster > xModifiable( m_xDocument, uno::UNO_QUERY_THROW );
624 xModifiable->removeModifyListener( (util::XModifyListener*)this );
626 catch( const uno::Exception& )
631 uno::Reference< util::XCloseBroadcaster > xBroadcaster(
632 m_xDocument, uno::UNO_QUERY_THROW );
633 xBroadcaster->removeCloseListener( (util::XCloseListener*)this );
635 catch( const uno::Exception& )
640 uno::Reference< util::XCloseBroadcaster > xBroadcaster(
641 m_xFrame, uno::UNO_QUERY_THROW );
642 xBroadcaster->removeCloseListener( (util::XCloseListener*)this );
644 catch( const uno::Exception& )
647 if ( bComplete )
649 m_xFrame = uno::Reference<frame::XFrame2>();
650 m_pIDispatch = NULL;
651 m_xDocument = uno::Reference< frame::XModel >();
655 void DocumentHolder::CloseDocument()
657 DisconnectFrameDocument();
659 uno::Reference< util::XCloseable > xCloseable(
660 m_xDocument, uno::UNO_QUERY );
662 if ( xCloseable.is() )
666 xCloseable->close( sal_True );
668 catch( const uno::Exception& )
672 m_pIDispatch = NULL;
673 m_xDocument = uno::Reference< frame::XModel >();
677 void DocumentHolder::CloseFrame()
681 uno::Reference< util::XCloseBroadcaster > xBroadcaster(
682 m_xFrame, uno::UNO_QUERY_THROW );
683 xBroadcaster->removeCloseListener( (util::XCloseListener*)this );
685 catch( const uno::Exception& )
688 uno::Reference<util::XCloseable> xCloseable(
689 m_xFrame,uno::UNO_QUERY);
690 if(xCloseable.is())
691 try {
692 xCloseable->close(sal_True);
694 catch( const uno::Exception& ) {
696 else {
697 uno::Reference<lang::XComponent> xComp(m_xFrame, uno::UNO_QUERY);
698 if (xComp.is())
699 xComp->dispose();
702 m_xFrame = uno::Reference< frame::XFrame2 >();
705 void DocumentHolder::SetDocument( const uno::Reference< frame::XModel >& xDoc, sal_Bool bLink )
707 if ( m_xDocument.is() )
708 CloseDocument();
710 m_xDocument = xDoc;
711 m_bLink = bLink;
713 uno::Reference< util::XCloseBroadcaster > xBroadcaster(
714 m_xDocument, uno::UNO_QUERY );
716 if ( xBroadcaster.is() )
717 xBroadcaster->addCloseListener( (util::XCloseListener*)this );
719 if ( m_xDocument.is() && !m_bLink )
721 // set the document mode to embedded
722 uno::Sequence< beans::PropertyValue > aSeq(1);
723 aSeq[0].Name = "SetEmbedded";
724 aSeq[0].Value <<= sal_True;
725 m_xDocument->attachResource(OUString(),aSeq);
729 sal_Bool DocumentHolder::ExecuteSuspendCloseFrame()
731 if ( m_xFrame.is() && m_xFactory.is() )
735 uno::Reference< frame::XController > xController = m_xFrame->getController();
736 if ( xController.is() )
738 if ( !xController->suspend( sal_True ) )
739 return sal_False;
741 FreeOffice();
744 uno::Reference<util::XCloseable> xCloseable( m_xFrame, uno::UNO_QUERY );
745 if ( xCloseable.is() )
746 xCloseable->close(sal_True);
747 else
749 m_xFrame->dispose();
752 catch( const util::CloseVetoException& )
754 // should be called if the frame could not be closed
755 xController->suspend( sal_False );
759 catch( uno::Exception& )
763 m_xFrame = uno::Reference< frame::XFrame2 >();
766 return sal_True;
769 uno::Reference< frame::XFrame2 > DocumentHolder::DocumentFrame()
771 if(! m_xFrame.is() )
773 uno::Reference<frame::XDesktop2> xDesktop = frame::Desktop::create(comphelper::getComponentContext(m_xFactory));
775 uno::Reference<frame::XFrame> xFrame(xDesktop,uno::UNO_QUERY);
777 // the frame will be registered on desktop here, later when the document
778 // is loaded into the frame in ::show() method the terminate listener will be removed
779 // this is so only for outplace activation
780 if( xFrame.is() )
781 m_xFrame.set( xFrame->findFrame( OUString("_blank"), 0 ), uno::UNO_QUERY );
783 uno::Reference< util::XCloseBroadcaster > xBroadcaster(
784 m_xFrame, uno::UNO_QUERY );
786 if ( xBroadcaster.is() )
788 xBroadcaster->addCloseListener( (util::XCloseListener*)this );
789 FreeOffice(); // the frame is part of the desktop
793 if( m_xFrame.is() )
795 // intercept
796 m_xFrame->registerDispatchProviderInterceptor( CreateNewInterceptor() );
799 return m_xFrame;
803 uno::Reference< frame::XDispatchProviderInterceptor > DocumentHolder::CreateNewInterceptor()
805 ::osl::MutexGuard aGuard( m_aMutex );
807 ClearInterceptorInternally();
809 uno::Reference< frame::XDispatchProviderInterceptor > xInterceptor( m_pInterceptor = new Interceptor( m_xOleAccess, this, m_bLink ) );
810 m_xInterceptorLocker = xInterceptor;
811 return xInterceptor;
814 void DocumentHolder::ClearInterceptorInternally()
816 ::osl::MutexGuard aGuard( m_aMutex );
817 uno::Reference< frame::XDispatchProviderInterceptor > xInterceptor( m_xInterceptorLocker );
818 if ( xInterceptor.is() && m_pInterceptor )
819 m_pInterceptor->DisconnectDocHolder();
821 m_xInterceptorLocker = uno::Reference< frame::XDispatchProviderInterceptor >();
822 m_pInterceptor = 0;
825 void DocumentHolder::ClearInterceptor()
827 ::osl::MutexGuard aGuard( m_aMutex );
828 m_xInterceptorLocker = uno::Reference< frame::XDispatchProviderInterceptor >();
829 m_pInterceptor = 0;
833 void DocumentHolder::show()
837 if(m_xFrame.is())
839 m_xFrame->activate();
840 uno::Reference<awt::XTopWindow> xTopWindow(
841 m_xFrame->getContainerWindow(),uno::UNO_QUERY);
842 if(xTopWindow.is())
843 xTopWindow->toFront();
845 else if( DocumentFrame().is() )
847 LoadDocInFrame( sal_False );
849 // get rid of second closer if it is there
850 uno::Reference< beans::XPropertySet > xLMProps( m_xFrame->getLayoutManager(), uno::UNO_QUERY );
851 if ( xLMProps.is() )
853 xLMProps->setPropertyValue("MenuBarCloser",
854 uno::makeAny( uno::Reference< frame::XStatusListener >() ) );
857 if ( !m_bLink )
861 uno::Reference< util::XModifyBroadcaster > xModifiable( m_xDocument, uno::UNO_QUERY_THROW );
862 xModifiable->addModifyListener( (util::XModifyListener*)this );
864 catch( const uno::Exception& )
868 if ( !m_bLink )
869 setTitle(m_aDocumentNamePart);
872 catch( const uno::Exception& )
874 OSL_FAIL( "Can not show the frame!\n" );
879 void DocumentHolder::resizeWin( const SIZEL& rNewSize )
881 LockedEmbedDocument_Impl aDocLock;
883 if ( m_xOleAccess.is() )
884 aDocLock = m_xOleAccess->GetEmbedDocument();
886 if ( m_xFrame.is() && aDocLock.GetEmbedDocument() )
888 uno::Reference< awt::XWindow > xWindow(
889 m_xFrame->getContainerWindow(), uno::UNO_QUERY );
890 uno::Reference< awt::XView > xView( xWindow, uno::UNO_QUERY );
892 if ( xWindow.is() && xView.is() )
894 float fScale = 1;
895 xView->setZoom( fScale, fScale );
897 SIZEL aOldSize;
898 GetExtent( &aOldSize );
900 if ( aOldSize.cx != rNewSize.cx || aOldSize.cy != rNewSize.cy )
902 HDC hdc = GetDC( NULL );
903 SetMapMode( hdc, MM_HIMETRIC );
905 POINT aOldOffset;
906 aOldOffset.x = aOldSize.cx;
907 aOldOffset.y = aOldSize.cy;
908 BOOL bIsOk = LPtoDP( hdc, &aOldOffset, 1 );
910 POINT aNewOffset;
911 aNewOffset.x = rNewSize.cx;
912 aNewOffset.y = rNewSize.cy;
913 bIsOk = LPtoDP( hdc, &aNewOffset, 1 );
915 ReleaseDC( NULL, hdc );
917 awt::Rectangle aWinRect = xWindow->getPosSize();
919 sal_Int32 aWidthDelta = aWinRect.Width - aOldOffset.x;
920 sal_Int32 aHeightDelta = aWinRect.Height - aOldOffset.y;
922 if ( aWidthDelta > 0 && aHeightDelta > 0 )
923 xWindow->setPosSize(0,
925 aNewOffset.x + aWidthDelta,
926 aNewOffset.y + aHeightDelta,
927 awt::PosSize::SIZE );
933 void DocumentHolder::setTitle(const OUString& aDocumentName)
935 if(m_xFrame.is())
937 if(m_aFilterName.getLength() == 0)
939 OUString aFilterName;
940 uno::Sequence<beans::PropertyValue> aSeq;
941 if(m_xDocument.is())
943 aSeq =
944 m_xDocument->getArgs();
945 for(sal_Int32 j = 0; j < aSeq.getLength(); ++j)
947 if(aSeq[j].Name == "FilterName")
949 aSeq[j].Value >>= aFilterName;
950 break;
955 if(aFilterName.getLength())
957 uno::Reference<container::XNameAccess> xNameAccess(
958 m_xFactory->createInstance(
959 OUString(
960 "com.sun.star.document.FilterFactory")),
961 uno::UNO_QUERY);
962 try {
963 if(xNameAccess.is() &&
964 (xNameAccess->getByName(aFilterName) >>= aSeq))
966 for(sal_Int32 j = 0; j < aSeq.getLength(); ++j)
967 if(aSeq[j].Name ==
968 OUString("UIName"))
970 aSeq[j].Value >>= m_aFilterName;
971 break;
975 catch(const uno::Exception& ) {
976 // nothing better to do here
977 m_aFilterName = aFilterName;
981 // set the title
982 static const sal_Unicode u[] = { ' ','(',0 };
983 static const sal_Unicode c[] = { ')',0 };
984 rtl::OUString aTotalName(m_aFilterName);
985 aTotalName += rtl::OUString(u);
986 aTotalName += aDocumentName;
987 aTotalName += rtl::OUString(c);
988 try {
989 m_xFrame->setTitle( aTotalName );
991 catch( const uno::Exception& ) {
995 m_aDocumentNamePart = aDocumentName;
997 if(m_pInterceptor)
999 ::osl::ClearableMutexGuard aGuard( m_aMutex );
1001 Interceptor* pTmpInter = NULL;
1002 uno::Reference< frame::XDispatchProviderInterceptor > xLock( m_xInterceptorLocker );
1003 if ( xLock.is() && m_pInterceptor )
1004 pTmpInter = m_pInterceptor;
1006 aGuard.clear();
1008 if ( pTmpInter )
1009 pTmpInter->generateFeatureStateEvent();
1014 void DocumentHolder::setContainerName(const OUString& aContainerName)
1016 m_aContainerName = aContainerName;
1020 void DocumentHolder::hide()
1022 if(m_xFrame.is()) m_xFrame->deactivate();
1024 //todo: sendadvise
1025 // after hiding the window it is always allowed to InPlaceActivate it
1026 m_bAllowInPlace = true;
1029 IDispatch* DocumentHolder::GetIDispatch()
1031 if ( !m_pIDispatch && m_xDocument.is() )
1033 const OUString aServiceName (
1034 "com.sun.star.bridge.OleBridgeSupplier2" );
1035 uno::Reference< bridge::XBridgeSupplier2 > xSupplier(
1036 m_xFactory->createInstance( aServiceName ), uno::UNO_QUERY );
1038 if ( xSupplier.is() )
1040 uno::Sequence< sal_Int8 > aProcId( 16 );
1041 rtl_getGlobalProcessId( (sal_uInt8*)aProcId.getArray() );
1043 try {
1044 uno::Any anyResult = xSupplier->createBridge(
1045 uno::makeAny( m_xDocument ),
1046 aProcId,
1047 bridge::ModelDependent::UNO,
1048 bridge::ModelDependent::OLE );
1050 if ( anyResult.getValueTypeClass() ==
1051 cppu::UnoType<sal_uInt32>::get().getTypeClass() )
1053 VARIANT* pVariant = *(VARIANT**)anyResult.getValue();
1054 if ( pVariant->vt == VT_DISPATCH )
1055 m_pIDispatch = pVariant->pdispVal;
1057 VariantClear( pVariant );
1058 CoTaskMemFree( pVariant );
1061 catch ( const uno::Exception& )
1066 return m_pIDispatch;
1069 HRESULT DocumentHolder::GetDocumentBorder( RECT *pRect )
1071 if ( pRect && m_xDocument.is() )
1073 uno::Sequence< beans::PropertyValue > aArgs = m_xDocument->getArgs();
1074 for ( sal_Int32 nInd = 0; nInd < aArgs.getLength(); nInd++ )
1075 if ( aArgs[nInd].Name == "DocumentBorder" )
1077 uno::Sequence< sal_Int32 > aRect;
1078 if ( ( aArgs[nInd].Value >>= aRect ) && aRect.getLength() == 4 )
1080 pRect->left = aRect[0];
1081 pRect->top = aRect[1];
1082 pRect->right = aRect[2];
1083 pRect->bottom = aRect[3];
1085 return S_OK;
1088 break;
1092 return E_FAIL;
1095 HRESULT DocumentHolder::SetExtent( const SIZEL *pSize )
1097 if ( pSize )
1099 uno::Reference< embed::XVisualObject > xVisObj( m_xDocument, uno::UNO_QUERY );
1100 if ( xVisObj.is() )
1104 awt::Size aNewSize( pSize->cx, pSize->cy );
1106 sal_Int32 aMapMode = xVisObj->getMapUnit( DVASPECT_CONTENT );
1108 // TODO/LATER: in future UNO API should be used for the conversion, currently there is no
1109 if ( aMapMode == embed::EmbedMapUnits::TWIP )
1111 // conversion from ONE_100TH_MM
1112 aNewSize.Width = aNewSize.Width * 144 / 254;
1113 aNewSize.Height = aNewSize.Height * 144 / 254;
1117 xVisObj->setVisualAreaSize( DVASPECT_CONTENT, aNewSize );
1119 return S_OK;
1121 catch( const uno::Exception& )
1126 return E_FAIL;
1129 HRESULT DocumentHolder::GetExtent( SIZEL *pSize )
1131 if ( pSize )
1133 uno::Reference< embed::XVisualObject > xVisObj( m_xDocument, uno::UNO_QUERY );
1134 if ( xVisObj.is() )
1138 awt::Size aDocSize = xVisObj->getVisualAreaSize( DVASPECT_CONTENT );
1140 sal_Int32 aMapMode = xVisObj->getMapUnit( DVASPECT_CONTENT );
1142 // TODO/LATER: in future UNO API should be used for the conversion, currently there is no
1143 if ( aMapMode == embed::EmbedMapUnits::TWIP )
1145 // conversion to ONE_100TH_MM
1146 aDocSize.Width = aDocSize.Width * 254 / 144;
1147 aDocSize.Height = aDocSize.Height * 254 / 144;
1150 pSize->cx = aDocSize.Width;
1151 pSize->cy = aDocSize.Height;
1153 return S_OK;
1155 catch( const uno::Exception& )
1160 return E_FAIL;
1164 HRESULT DocumentHolder::SetContRects(LPCRECT aRect)
1166 if(m_xContainerWindow.is()) {
1167 RECT wi;
1168 memset(&wi,0,sizeof(wi));
1169 if(m_pIOleIPFrame) {
1170 m_pIOleIPFrame->GetBorder((LPRECT)&wi);
1171 m_xContainerWindow->setPosSize(
1172 0,0,
1173 wi.right - wi.left,
1174 wi.bottom - wi.top,
1175 awt::PosSize::POSSIZE);
1177 else
1178 m_xContainerWindow->setPosSize(
1179 0,0,
1180 aRect->right - aRect->left,
1181 aRect->bottom - aRect->top,
1182 awt::PosSize::POSSIZE);
1183 return NOERROR;
1185 else {
1186 return ERROR;
1191 HRESULT DocumentHolder::SetObjectRects(LPCRECT aRect, LPCRECT aClip)
1193 ((LPRECT)aRect)->left -= m_aBorder.left;
1194 ((LPRECT)aRect)->right += m_aBorder.right;
1195 ((LPRECT)aRect)->top -= m_aBorder.top;
1196 ((LPRECT)aRect)->bottom += m_aBorder.bottom;
1197 ((LPRECT)aClip)->left -= m_aBorder.left;
1198 ((LPRECT)aClip)->right += m_aBorder.right;
1199 ((LPRECT)aClip)->top -= m_aBorder.top;
1200 ((LPRECT)aClip)->bottom += m_aBorder.bottom;
1202 if(m_pCHatchWin)
1203 m_pCHatchWin->RectsSet((LPRECT)aRect, (LPRECT)aClip);
1204 if(m_xEditWindow.is()) {
1205 m_xEditWindow->setVisible(false);
1206 m_xEditWindow->setPosSize(
1207 m_pCHatchWin ? HATCHWIN_BORDERWIDTHDEFAULT : 0,
1208 m_pCHatchWin ? HATCHWIN_BORDERWIDTHDEFAULT : 0,
1209 aRect->right - aRect->left,
1210 aRect->bottom - aRect->top,
1211 awt::PosSize::POSSIZE);
1212 m_xEditWindow->setVisible(true);
1214 return NOERROR;
1218 ::com::sun::star::uno::Reference<
1219 ::com::sun::star::awt::XWindow> SAL_CALL
1220 DocumentHolder::getContainerWindow(
1222 throw (
1223 ::com::sun::star::uno::RuntimeException
1226 if(m_xContainerWindow.is())
1227 return m_xContainerWindow;
1229 uno::Reference<awt::XWindow> xWin(0);
1231 uno::Reference<awt::XToolkit2> xToolkit = awt::Toolkit::create( comphelper::getComponentContext(m_xFactory) );
1233 if(m_pIOleIPFrame) {
1234 HWND hWnd;
1235 m_pIOleIPFrame->GetWindow(&hWnd);
1237 uno::Sequence<sal_Int8> aProcessIdent(16);
1238 rtl_getGlobalProcessId((sal_uInt8*)aProcessIdent.getArray());
1240 uno::Any aAny;
1241 aAny <<= sal_Int32(hWnd);
1242 xWin = uno::Reference<awt::XWindow>(
1243 xToolkit->createSystemChild(
1244 aAny,
1245 aProcessIdent,
1246 lang::SystemDependent::SYSTEM_WIN32),
1247 uno::UNO_QUERY);
1249 RECT wi;
1250 memset(&wi,0,sizeof(wi));
1251 if(xWin.is() && m_pIOleIPFrame->GetBorder((LPRECT)&wi) == NOERROR) {
1252 xWin->setVisible(true);
1253 xWin->setPosSize(
1254 0,0,
1255 wi.right-wi.left,
1256 wi.bottom - wi.top,
1257 awt::PosSize::POSSIZE);
1259 uno::Reference<awt::XSystemDependentWindowPeer> xSysWin(
1260 xWin,uno::UNO_QUERY);
1261 if(xSysWin.is()) {
1262 aAny = xSysWin->getWindowHandle(
1263 aProcessIdent,lang::SystemDependent::SYSTEM_WIN32);
1264 sal_Int32 tmp;
1265 if( aAny >>= tmp )
1266 SetContainerWindowHandle((HWND) tmp);
1271 m_xContainerWindow= xWin;
1272 return xWin;
1277 sal_Bool SAL_CALL
1278 DocumentHolder::requestDockingAreaSpace(
1279 const ::com::sun::star::awt::Rectangle& RequestedSpace
1281 throw(
1282 ::com::sun::star::uno::RuntimeException
1285 if(m_bOnDeactivate)
1286 return sal_True;
1288 BORDERWIDTHS bw;
1289 SetRect((LPRECT)&bw,
1290 RequestedSpace.X,RequestedSpace.Y,
1291 RequestedSpace.Width,RequestedSpace.Height);
1292 if( m_pIOleIPFrame )
1293 return m_pIOleIPFrame->RequestBorderSpace(&bw) == NOERROR ;
1294 else
1295 return sal_Bool(false);
1299 void SAL_CALL
1300 DocumentHolder::setDockingAreaSpace(
1301 const ::com::sun::star::awt::Rectangle& BorderSpace
1303 throw (
1304 ::com::sun::star::uno::RuntimeException
1307 if(m_bOnDeactivate)
1308 return;
1310 BORDERWIDTHS bw;
1311 SetRect((LPRECT)&bw,
1312 BorderSpace.X,BorderSpace.Y,
1313 BorderSpace.Width,BorderSpace.Height);
1314 if( m_pIOleIPFrame ) {
1315 RECT aRect;
1316 GetClientRect(m_hWndxWinCont,&aRect);
1317 HRGN hrgn1 = CreateRectRgn(
1318 0,0,
1319 aRect.right,BorderSpace.Y);
1320 HRGN hrgn2 = CreateRectRgn(aRect.right-BorderSpace.Width,0,aRect.right,aRect.bottom);
1321 CombineRgn(hrgn1,hrgn1,hrgn2,RGN_OR);
1322 DeleteObject(hrgn2);
1323 hrgn2 = CreateRectRgn(0,aRect.bottom-BorderSpace.Height,aRect.right,aRect.bottom);
1324 CombineRgn(hrgn1,hrgn1,hrgn2,RGN_OR);
1325 DeleteObject(hrgn2);
1326 hrgn2 = CreateRectRgn(0,0,BorderSpace.X,aRect.bottom);
1327 CombineRgn(hrgn1,hrgn1,hrgn2,RGN_OR);
1328 DeleteObject(hrgn2);
1330 SetWindowRgn(m_hWndxWinCont,hrgn1,true);
1331 // not:: DeleteObject(hrgn1);
1332 m_pIOleIPFrame->SetBorderSpace(&bw);
1337 void SAL_CALL
1338 DocumentHolder::disposing(
1339 const com::sun::star::lang::EventObject& aSource
1341 throw( uno::RuntimeException )
1343 if ( m_xDocument.is() && m_xDocument == aSource.Source )
1345 m_pIDispatch = NULL;
1346 m_xDocument = uno::Reference< frame::XModel >();
1349 if( m_xFrame.is() && m_xFrame == aSource.Source )
1350 m_xFrame = uno::Reference< frame::XFrame2 >();
1354 void SAL_CALL
1355 DocumentHolder::queryClosing(
1356 const lang::EventObject& aSource,
1357 sal_Bool /*bGetsOwnership*/
1359 throw(
1360 util::CloseVetoException
1363 if ( !m_bLink
1364 && ( m_xDocument.is() && m_xDocument == aSource.Source || m_xFrame.is() && m_xFrame == aSource.Source ) )
1365 throw util::CloseVetoException();
1369 void SAL_CALL
1370 DocumentHolder::notifyClosing(
1371 const lang::EventObject& aSource )
1372 throw( uno::RuntimeException )
1376 uno::Reference< util::XCloseBroadcaster > xEventBroadcaster(
1377 aSource.Source, uno::UNO_QUERY_THROW );
1378 xEventBroadcaster->removeCloseListener( (util::XCloseListener*)this );
1380 catch( const uno::Exception& )
1383 if ( m_xDocument.is() && m_xDocument == aSource.Source )
1385 // can happen only in case of links
1386 m_pIDispatch = NULL;
1387 m_xDocument = uno::Reference< frame::XModel >();
1388 m_xFrame = uno::Reference< frame::XFrame2 >();
1390 LockedEmbedDocument_Impl aDocLock = m_xOleAccess->GetEmbedDocument();
1391 if ( aDocLock.GetEmbedDocument() )
1392 aDocLock.GetEmbedDocument()->OLENotifyClosing();
1394 else if( m_xFrame.is() && m_xFrame == aSource.Source )
1395 m_xFrame = uno::Reference< frame::XFrame2 >();
1398 void SAL_CALL
1399 DocumentHolder::queryTermination(
1400 const lang::EventObject& /*aSource*/
1402 throw(
1403 frame::TerminationVetoException
1406 if ( m_xDocument.is() )
1407 throw frame::TerminationVetoException();
1410 void SAL_CALL
1411 DocumentHolder::notifyTermination(
1412 const lang::EventObject& aSource
1414 throw( uno::RuntimeException )
1416 OSL_ENSURE( !m_xDocument.is(), "Just a disaster..." );
1417 uno::Reference< frame::XDesktop > xDesktop(
1418 aSource.Source, uno::UNO_QUERY );
1420 if ( xDesktop.is() )
1421 xDesktop->removeTerminateListener( (frame::XTerminateListener*)this );
1426 void SAL_CALL DocumentHolder::modified( const lang::EventObject& /*aEvent*/ )
1427 throw (uno::RuntimeException)
1429 if ( m_xOleAccess.is() )
1431 LockedEmbedDocument_Impl aDocLock = m_xOleAccess->GetEmbedDocument();
1432 if ( aDocLock.GetEmbedDocument() )
1433 aDocLock.GetEmbedDocument()->notify();
1437 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */