xref: /aoo41x/main/sc/source/ui/vba/excelvbahelper.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "excelvbahelper.hxx"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
31*cdf0e10cSrcweir #include <com/sun/star/sheet/XSheetCellRange.hpp>
32*cdf0e10cSrcweir #include "docuno.hxx"
33*cdf0e10cSrcweir #include "tabvwsh.hxx"
34*cdf0e10cSrcweir #include "transobj.hxx"
35*cdf0e10cSrcweir #include "scmod.hxx"
36*cdf0e10cSrcweir #include "cellsuno.hxx"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir namespace ooo {
39*cdf0e10cSrcweir namespace vba {
40*cdf0e10cSrcweir namespace excel {
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir using namespace ::com::sun::star;
43*cdf0e10cSrcweir using namespace ::ooo::vba;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir // ============================================================================
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir ScDocShell* GetDocShellFromRange( const uno::Reference< uno::XInterface >& xRange ) throw ( uno::RuntimeException )
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir     ScCellRangesBase* pScCellRangesBase = ScCellRangesBase::getImplementation( xRange );
50*cdf0e10cSrcweir     if ( !pScCellRangesBase )
51*cdf0e10cSrcweir     {
52*cdf0e10cSrcweir         throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Failed to access underlying doc shell uno range object" ) ), uno::Reference< uno::XInterface >() );
53*cdf0e10cSrcweir     }
54*cdf0e10cSrcweir     return pScCellRangesBase->GetDocShell();
55*cdf0e10cSrcweir }
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir ScDocument* GetDocumentFromRange( const uno::Reference< uno::XInterface >& xRange ) throw ( uno::RuntimeException )
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir         ScDocShell* pDocShell = GetDocShellFromRange( xRange );
60*cdf0e10cSrcweir         if ( !pDocShell )
61*cdf0e10cSrcweir         {
62*cdf0e10cSrcweir                 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Failed to access underlying document from uno range object" ) ), uno::Reference< uno::XInterface >() );
63*cdf0e10cSrcweir         }
64*cdf0e10cSrcweir         return pDocShell->GetDocument();
65*cdf0e10cSrcweir }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir void implSetZoom( const uno::Reference< frame::XModel >& xModel, sal_Int16 nZoom, std::vector< SCTAB >& nTabs )
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir     ScTabViewShell* pViewSh = excel::getBestViewShell( xModel );
70*cdf0e10cSrcweir     Fraction aFract( nZoom, 100 );
71*cdf0e10cSrcweir     pViewSh->GetViewData()->SetZoom( aFract, aFract, nTabs );
72*cdf0e10cSrcweir     pViewSh->RefreshZoom();
73*cdf0e10cSrcweir }
74*cdf0e10cSrcweir bool isInPrintPreview( SfxViewFrame* pView )
75*cdf0e10cSrcweir {
76*cdf0e10cSrcweir 	sal_uInt16 nViewNo = SID_VIEWSHELL1 - SID_VIEWSHELL0;
77*cdf0e10cSrcweir 	if ( pView->GetObjectShell()->GetFactory().GetViewFactoryCount() >
78*cdf0e10cSrcweir nViewNo && !pView->GetObjectShell()->IsInPlaceActive() )
79*cdf0e10cSrcweir 	{
80*cdf0e10cSrcweir 		SfxViewFactory &rViewFactory =
81*cdf0e10cSrcweir 			pView->GetObjectShell()->GetFactory().GetViewFactory(nViewNo);
82*cdf0e10cSrcweir 		if (  pView->GetCurViewId() == rViewFactory.GetOrdinal() )
83*cdf0e10cSrcweir 			return true;
84*cdf0e10cSrcweir 	}
85*cdf0e10cSrcweir 	return false;
86*cdf0e10cSrcweir }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir const ::rtl::OUString REPLACE_CELLS_WARNING(  RTL_CONSTASCII_USTRINGPARAM( "ReplaceCellsWarning"));
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir class PasteCellsWarningReseter
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir private:
93*cdf0e10cSrcweir 	bool bInitialWarningState;
94*cdf0e10cSrcweir 	static uno::Reference< beans::XPropertySet > getGlobalSheetSettings() throw ( uno::RuntimeException )
95*cdf0e10cSrcweir 	{
96*cdf0e10cSrcweir 		static uno::Reference< beans::XPropertySet > xTmpProps( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
97*cdf0e10cSrcweir 		static uno::Reference<uno::XComponentContext > xContext( xTmpProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ))), uno::UNO_QUERY_THROW );
98*cdf0e10cSrcweir 		static uno::Reference<lang::XMultiComponentFactory > xServiceManager(
99*cdf0e10cSrcweir 				xContext->getServiceManager(), uno::UNO_QUERY_THROW );
100*cdf0e10cSrcweir 		static uno::Reference< beans::XPropertySet > xProps( xServiceManager->createInstanceWithContext( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sheet.GlobalSheetSettings" ) ) ,xContext ), uno::UNO_QUERY_THROW );
101*cdf0e10cSrcweir 		return xProps;
102*cdf0e10cSrcweir 	}
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 	bool getReplaceCellsWarning() throw ( uno::RuntimeException )
105*cdf0e10cSrcweir 	{
106*cdf0e10cSrcweir 		sal_Bool res = sal_False;
107*cdf0e10cSrcweir 		getGlobalSheetSettings()->getPropertyValue( REPLACE_CELLS_WARNING ) >>= res;
108*cdf0e10cSrcweir 		return ( res == sal_True );
109*cdf0e10cSrcweir 	}
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 	void setReplaceCellsWarning( bool bState ) throw ( uno::RuntimeException )
112*cdf0e10cSrcweir 	{
113*cdf0e10cSrcweir 		getGlobalSheetSettings()->setPropertyValue( REPLACE_CELLS_WARNING, uno::makeAny( bState ) );
114*cdf0e10cSrcweir 	}
115*cdf0e10cSrcweir public:
116*cdf0e10cSrcweir 	PasteCellsWarningReseter() throw ( uno::RuntimeException )
117*cdf0e10cSrcweir 	{
118*cdf0e10cSrcweir 		bInitialWarningState = getReplaceCellsWarning();
119*cdf0e10cSrcweir 		if ( bInitialWarningState )
120*cdf0e10cSrcweir 			setReplaceCellsWarning( false );
121*cdf0e10cSrcweir 	}
122*cdf0e10cSrcweir 	~PasteCellsWarningReseter()
123*cdf0e10cSrcweir 	{
124*cdf0e10cSrcweir 		if ( bInitialWarningState )
125*cdf0e10cSrcweir 		{
126*cdf0e10cSrcweir 			// don't allow dtor to throw
127*cdf0e10cSrcweir 			try
128*cdf0e10cSrcweir 			{
129*cdf0e10cSrcweir 				setReplaceCellsWarning( true );
130*cdf0e10cSrcweir 			}
131*cdf0e10cSrcweir 			catch ( uno::Exception& /*e*/ ){}
132*cdf0e10cSrcweir 		}
133*cdf0e10cSrcweir 	}
134*cdf0e10cSrcweir };
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir void
137*cdf0e10cSrcweir implnPaste( const uno::Reference< frame::XModel>& xModel )
138*cdf0e10cSrcweir {
139*cdf0e10cSrcweir 	PasteCellsWarningReseter resetWarningBox;
140*cdf0e10cSrcweir 	ScTabViewShell* pViewShell = getBestViewShell( xModel );
141*cdf0e10cSrcweir 	if ( pViewShell )
142*cdf0e10cSrcweir 	{
143*cdf0e10cSrcweir 		pViewShell->PasteFromSystem();
144*cdf0e10cSrcweir 		pViewShell->CellContentChanged();
145*cdf0e10cSrcweir 	}
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir void
150*cdf0e10cSrcweir implnCopy( const uno::Reference< frame::XModel>& xModel )
151*cdf0e10cSrcweir {
152*cdf0e10cSrcweir 	ScTabViewShell* pViewShell = getBestViewShell( xModel );
153*cdf0e10cSrcweir 	if ( pViewShell )
154*cdf0e10cSrcweir     {
155*cdf0e10cSrcweir 		pViewShell->CopyToClip(NULL,false,false,true);
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir         // mark the copied transfer object so it is used in ScVbaRange::Insert
158*cdf0e10cSrcweir         ScTransferObj* pClipObj = ScTransferObj::GetOwnClipboard( NULL );
159*cdf0e10cSrcweir         if (pClipObj)
160*cdf0e10cSrcweir             pClipObj->SetUseInApi( true );
161*cdf0e10cSrcweir     }
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir void
165*cdf0e10cSrcweir implnCut( const uno::Reference< frame::XModel>& xModel )
166*cdf0e10cSrcweir {
167*cdf0e10cSrcweir 	ScTabViewShell* pViewShell =  getBestViewShell( xModel );
168*cdf0e10cSrcweir 	if ( pViewShell )
169*cdf0e10cSrcweir     {
170*cdf0e10cSrcweir 		pViewShell->CutToClip( NULL, sal_True );
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir         // mark the copied transfer object so it is used in ScVbaRange::Insert
173*cdf0e10cSrcweir         ScTransferObj* pClipObj = ScTransferObj::GetOwnClipboard( NULL );
174*cdf0e10cSrcweir         if (pClipObj)
175*cdf0e10cSrcweir             pClipObj->SetUseInApi( true );
176*cdf0e10cSrcweir     }
177*cdf0e10cSrcweir }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir void implnPasteSpecial( const uno::Reference< frame::XModel>& xModel, sal_uInt16 nFlags,sal_uInt16 nFunction,sal_Bool bSkipEmpty, sal_Bool bTranspose)
180*cdf0e10cSrcweir {
181*cdf0e10cSrcweir 	PasteCellsWarningReseter resetWarningBox;
182*cdf0e10cSrcweir 	sal_Bool bAsLink(sal_False), bOtherDoc(sal_False);
183*cdf0e10cSrcweir 	InsCellCmd eMoveMode = INS_NONE;
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 	ScTabViewShell* pTabViewShell = getBestViewShell( xModel );
186*cdf0e10cSrcweir 	if ( pTabViewShell )
187*cdf0e10cSrcweir 	{
188*cdf0e10cSrcweir 		ScViewData* pView = pTabViewShell->GetViewData();
189*cdf0e10cSrcweir 		Window* pWin = ( pView != NULL ) ? pView->GetActiveWin() : NULL;
190*cdf0e10cSrcweir 		if ( pView && pWin )
191*cdf0e10cSrcweir 		{
192*cdf0e10cSrcweir 			if ( bAsLink && bOtherDoc )
193*cdf0e10cSrcweir 				pTabViewShell->PasteFromSystem(0);//SOT_FORMATSTR_ID_LINK
194*cdf0e10cSrcweir 			else
195*cdf0e10cSrcweir 			{
196*cdf0e10cSrcweir 				ScTransferObj* pOwnClip = ScTransferObj::GetOwnClipboard( pWin );
197*cdf0e10cSrcweir 				ScDocument* pDoc = NULL;
198*cdf0e10cSrcweir 				if ( pOwnClip )
199*cdf0e10cSrcweir                 {
200*cdf0e10cSrcweir 					pDoc = pOwnClip->GetDocument();
201*cdf0e10cSrcweir                     pOwnClip->SetUseInApi( false );     // don't use in Insert after it was pasted once
202*cdf0e10cSrcweir                 }
203*cdf0e10cSrcweir 				pTabViewShell->PasteFromClip( nFlags, pDoc,
204*cdf0e10cSrcweir 					nFunction, bSkipEmpty, bTranspose, bAsLink,
205*cdf0e10cSrcweir 					eMoveMode, IDF_NONE, sal_True );
206*cdf0e10cSrcweir 				pTabViewShell->CellContentChanged();
207*cdf0e10cSrcweir 			}
208*cdf0e10cSrcweir 		}
209*cdf0e10cSrcweir 	}
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir ScDocShell*
214*cdf0e10cSrcweir getDocShell( const css::uno::Reference< css::frame::XModel>& xModel )
215*cdf0e10cSrcweir {
216*cdf0e10cSrcweir 	uno::Reference< uno::XInterface > xIf( xModel, uno::UNO_QUERY_THROW );
217*cdf0e10cSrcweir 	ScModelObj* pModel = dynamic_cast< ScModelObj* >( xIf.get() );
218*cdf0e10cSrcweir 	ScDocShell* pDocShell = NULL;
219*cdf0e10cSrcweir 	if ( pModel )
220*cdf0e10cSrcweir 		pDocShell = (ScDocShell*)pModel->GetEmbeddedObject();
221*cdf0e10cSrcweir 	return pDocShell;
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir ScTabViewShell*
226*cdf0e10cSrcweir getBestViewShell( const css::uno::Reference< css::frame::XModel>& xModel )
227*cdf0e10cSrcweir {
228*cdf0e10cSrcweir 	ScDocShell* pDocShell = getDocShell( xModel );
229*cdf0e10cSrcweir 	if ( pDocShell )
230*cdf0e10cSrcweir 		return pDocShell->GetBestViewShell();
231*cdf0e10cSrcweir 	return NULL;
232*cdf0e10cSrcweir }
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir ScTabViewShell*
235*cdf0e10cSrcweir getCurrentBestViewShell(  const uno::Reference< uno::XComponentContext >& xContext )
236*cdf0e10cSrcweir {
237*cdf0e10cSrcweir 	uno::Reference< frame::XModel > xModel = getCurrentExcelDoc( xContext );
238*cdf0e10cSrcweir 	return getBestViewShell( xModel );
239*cdf0e10cSrcweir }
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir SfxViewFrame*
242*cdf0e10cSrcweir getViewFrame( const uno::Reference< frame::XModel >& xModel )
243*cdf0e10cSrcweir {
244*cdf0e10cSrcweir 	ScTabViewShell* pViewShell = getBestViewShell( xModel );
245*cdf0e10cSrcweir 	if ( pViewShell )
246*cdf0e10cSrcweir 		return pViewShell->GetViewFrame();
247*cdf0e10cSrcweir 	return NULL;
248*cdf0e10cSrcweir }
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir uno::Reference< XHelperInterface >
251*cdf0e10cSrcweir getUnoSheetModuleObj( const uno::Reference< sheet::XSpreadsheet >& xSheet ) throw ( uno::RuntimeException )
252*cdf0e10cSrcweir {
253*cdf0e10cSrcweir     uno::Reference< beans::XPropertySet > xProps( xSheet, uno::UNO_QUERY_THROW );
254*cdf0e10cSrcweir     rtl::OUString sCodeName;
255*cdf0e10cSrcweir     xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("CodeName") ) ) >>= sCodeName;
256*cdf0e10cSrcweir     // #TODO #FIXME ideally we should 'throw' here if we don't get a valid parent, but... it is possible
257*cdf0e10cSrcweir     // to create a module ( and use 'Option VBASupport 1' ) for a calc document, in this scenario there
258*cdf0e10cSrcweir     // are *NO* special document module objects ( of course being able to switch between vba/non vba mode at
259*cdf0e10cSrcweir     // the document in the future could fix this, especially IF the switching of the vba mode takes care to
260*cdf0e10cSrcweir     // create the special document module objects if they don't exist.
261*cdf0e10cSrcweir     return getUnoDocModule( sCodeName, GetDocShellFromRange( xSheet ) );
262*cdf0e10cSrcweir }
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir uno::Reference< XHelperInterface >
265*cdf0e10cSrcweir getUnoSheetModuleObj( const uno::Reference< table::XCellRange >& xRange ) throw ( uno::RuntimeException )
266*cdf0e10cSrcweir {
267*cdf0e10cSrcweir     uno::Reference< sheet::XSheetCellRange > xSheetRange( xRange, uno::UNO_QUERY_THROW );
268*cdf0e10cSrcweir     uno::Reference< sheet::XSpreadsheet > xSheet( xSheetRange->getSpreadsheet(), uno::UNO_SET_THROW );
269*cdf0e10cSrcweir     return getUnoSheetModuleObj( xSheet );
270*cdf0e10cSrcweir }
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir uno::Reference< XHelperInterface >
273*cdf0e10cSrcweir getUnoSheetModuleObj( const uno::Reference< sheet::XSheetCellRangeContainer >& xRanges ) throw ( uno::RuntimeException )
274*cdf0e10cSrcweir {
275*cdf0e10cSrcweir     uno::Reference< container::XEnumerationAccess > xEnumAccess( xRanges, uno::UNO_QUERY_THROW );
276*cdf0e10cSrcweir     uno::Reference< container::XEnumeration > xEnum = xEnumAccess->createEnumeration();
277*cdf0e10cSrcweir     uno::Reference< table::XCellRange > xRange( xEnum->nextElement(), uno::UNO_QUERY_THROW );
278*cdf0e10cSrcweir     return getUnoSheetModuleObj( xRange );
279*cdf0e10cSrcweir }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir uno::Reference< XHelperInterface >
282*cdf0e10cSrcweir getUnoSheetModuleObj( const uno::Reference< table::XCell >& xCell ) throw ( uno::RuntimeException )
283*cdf0e10cSrcweir {
284*cdf0e10cSrcweir     uno::Reference< sheet::XSheetCellRange > xSheetRange( xCell, uno::UNO_QUERY_THROW );
285*cdf0e10cSrcweir     uno::Reference< sheet::XSpreadsheet > xSheet( xSheetRange->getSpreadsheet(), uno::UNO_SET_THROW );
286*cdf0e10cSrcweir     return getUnoSheetModuleObj( xSheet );
287*cdf0e10cSrcweir }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir uno::Reference< XHelperInterface >
290*cdf0e10cSrcweir getUnoSheetModuleObj( const uno::Reference< frame::XModel >& xModel, SCTAB nTab ) throw ( uno::RuntimeException )
291*cdf0e10cSrcweir {
292*cdf0e10cSrcweir     uno::Reference< sheet::XSpreadsheetDocument > xDoc( xModel, uno::UNO_QUERY_THROW );
293*cdf0e10cSrcweir     uno::Reference< container::XIndexAccess > xSheets( xDoc->getSheets(), uno::UNO_QUERY_THROW );
294*cdf0e10cSrcweir     uno::Reference< sheet::XSpreadsheet > xSheet( xSheets->getByIndex( nTab ), uno::UNO_QUERY_THROW );
295*cdf0e10cSrcweir     return getUnoSheetModuleObj( xSheet );
296*cdf0e10cSrcweir }
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir SfxItemSet*
299*cdf0e10cSrcweir ScVbaCellRangeAccess::GetDataSet( ScCellRangesBase* pRangeObj )
300*cdf0e10cSrcweir {
301*cdf0e10cSrcweir 	return pRangeObj ? pRangeObj->GetCurrentDataSet( true ) : 0;
302*cdf0e10cSrcweir }
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir // ============================================================================
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir } // namespace excel
307*cdf0e10cSrcweir } // namespace vba
308*cdf0e10cSrcweir } // namespace ooo
309