xref: /aoo4110/main/sc/source/ui/vba/vbahelper.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include <cppuhelper/bootstrap.hxx>
24 #include <com/sun/star/util/XURLTransformer.hpp>
25 #include <com/sun/star/frame/XDispatchProvider.hpp>
26 #include <com/sun/star/frame/XModel.hpp>
27 #include <com/sun/star/frame/XFrame.hpp>
28 #include <com/sun/star/frame/XDesktop.hpp>
29 #include <com/sun/star/frame/XController.hpp>
30 #include <com/sun/star/uno/XComponentContext.hpp>
31 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/beans/XIntrospection.hpp>
34 
35 #include <comphelper/processfactory.hxx>
36 
37 #include <sfx2/objsh.hxx>
38 #include <sfx2/viewfrm.hxx>
39 #include <sfx2/dispatch.hxx>
40 #include <sfx2/app.hxx>
41 #include <svl/stritem.hxx>
42 
43 #include <docuno.hxx>
44 
45 #include <basic/sbx.hxx>
46 #include <basic/sbstar.hxx>
47 #include <rtl/math.hxx>
48 
49 #include <math.h>
50 #include "vbahelper.hxx"
51 #include "tabvwsh.hxx"
52 #include "transobj.hxx"
53 #include "scmod.hxx"
54 #include "vbashape.hxx"
55 #include "unonames.hxx"
56 #include "cellsuno.hxx"
57 using namespace ::com::sun::star;
58 using namespace ::ooo::vba;
59 
60 #define POINTTO100THMILLIMETERFACTOR 35.27778
61 void unoToSbxValue( SbxVariable* pVar, const uno::Any& aValue );
62 
63 uno::Any sbxToUnoValue( SbxVariable* pVar );
64 
65 
66 namespace ooo
67 {
68 namespace vba
69 {
70 
71 const double Millimeter::factor =  35.27778;
72 
73 uno::Reference< beans::XIntrospectionAccess >
getIntrospectionAccess(const uno::Any & aObject)74 getIntrospectionAccess( const uno::Any& aObject ) throw (uno::RuntimeException)
75 {
76 	static uno::Reference< beans::XIntrospection > xIntrospection;
77 	if( !xIntrospection.is() )
78 	{
79 		uno::Reference< lang::XMultiServiceFactory > xFactory( comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
80 		xIntrospection.set( xFactory->createInstance( rtl::OUString::createFromAscii("com.sun.star.beans.Introspection") ), uno::UNO_QUERY_THROW );
81 	}
82 	return xIntrospection->inspect( aObject );
83 }
84 
85 uno::Reference< script::XTypeConverter >
getTypeConverter(const uno::Reference<uno::XComponentContext> & xContext)86 getTypeConverter( const uno::Reference< uno::XComponentContext >& xContext ) throw (uno::RuntimeException)
87 {
88 	static uno::Reference< script::XTypeConverter > xTypeConv( xContext->getServiceManager()->createInstanceWithContext( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.script.Converter") ), xContext ), uno::UNO_QUERY_THROW );
89 	return xTypeConv;
90 }
91 // helper method to determine if the view ( calc ) is in print-preview mode
isInPrintPreview(SfxViewFrame * pView)92 bool isInPrintPreview( SfxViewFrame* pView )
93 {
94 	sal_uInt16 nViewNo = SID_VIEWSHELL1 - SID_VIEWSHELL0;
95 	if ( pView->GetObjectShell()->GetFactory().GetViewFactoryCount() >
96 nViewNo && !pView->GetObjectShell()->IsInPlaceActive() )
97 	{
98 		SfxViewFactory &rViewFactory =
99 			pView->GetObjectShell()->GetFactory().GetViewFactory(nViewNo);
100 		if (  pView->GetCurViewId() == rViewFactory.GetOrdinal() )
101 			return true;
102 	}
103 	return false;
104 }
105 const ::rtl::OUString REPLACE_CELLS_WARNING(  RTL_CONSTASCII_USTRINGPARAM( "ReplaceCellsWarning"));
106 const uno::Any&
aNULL()107 aNULL()
108 {
109  	static  uno::Any aNULLL = uno::makeAny( uno::Reference< uno::XInterface >() );
110 	return aNULLL;
111 }
112 
113 class PasteCellsWarningReseter
114 {
115 private:
116 	bool bInitialWarningState;
getGlobalSheetSettings()117 	static uno::Reference< beans::XPropertySet > getGlobalSheetSettings() throw ( uno::RuntimeException )
118 	{
119 		static uno::Reference< beans::XPropertySet > xTmpProps( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
120 		static uno::Reference<uno::XComponentContext > xContext( xTmpProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ))), uno::UNO_QUERY_THROW );
121 		static uno::Reference<lang::XMultiComponentFactory > xServiceManager(
122 				xContext->getServiceManager(), uno::UNO_QUERY_THROW );
123 		static uno::Reference< beans::XPropertySet > xProps( xServiceManager->createInstanceWithContext( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sheet.GlobalSheetSettings" ) ) ,xContext ), uno::UNO_QUERY_THROW );
124 		return xProps;
125 	}
126 
getReplaceCellsWarning()127 	bool getReplaceCellsWarning() throw ( uno::RuntimeException )
128 	{
129 		sal_Bool res = sal_False;
130 		getGlobalSheetSettings()->getPropertyValue( REPLACE_CELLS_WARNING ) >>= res;
131 		return ( res == sal_True );
132 	}
133 
setReplaceCellsWarning(bool bState)134 	void setReplaceCellsWarning( bool bState ) throw ( uno::RuntimeException )
135 	{
136 		getGlobalSheetSettings()->setPropertyValue( REPLACE_CELLS_WARNING, uno::makeAny( bState ) );
137 	}
138 public:
PasteCellsWarningReseter()139 	PasteCellsWarningReseter() throw ( uno::RuntimeException )
140 	{
141 		bInitialWarningState = getReplaceCellsWarning();
142 		if ( bInitialWarningState )
143 			setReplaceCellsWarning( false );
144 	}
~PasteCellsWarningReseter()145 	~PasteCellsWarningReseter()
146 	{
147 		if ( bInitialWarningState )
148 		{
149 			// don't allow dtor to throw
150 			try
151 			{
152 				setReplaceCellsWarning( true );
153 			}
154 			catch ( uno::Exception& /*e*/ ){}
155 		}
156 	}
157 };
158 
dispatchExecute(css::uno::Reference<css::frame::XModel> & xModel,sal_uInt16 nSlot,SfxCallMode nCall)159 void dispatchExecute(css::uno::Reference< css::frame::XModel>& xModel, sal_uInt16 nSlot, SfxCallMode nCall)
160 {
161 	ScTabViewShell* pViewShell = getBestViewShell( xModel );
162 	SfxViewFrame* pViewFrame = NULL;
163 	if ( pViewShell )
164 		pViewFrame = pViewShell->GetViewFrame();
165 	if ( pViewFrame )
166 	{
167 		SfxDispatcher* pDispatcher = pViewFrame->GetDispatcher();
168 		if( pDispatcher )
169 		{
170 			pDispatcher->Execute( nSlot , nCall );
171 		}
172 	}
173 }
174 
175 void
implnPaste()176 implnPaste()
177 {
178 	PasteCellsWarningReseter resetWarningBox;
179 	ScTabViewShell* pViewShell = getCurrentBestViewShell();
180 	if ( pViewShell )
181 	{
182 		pViewShell->PasteFromSystem();
183 		pViewShell->CellContentChanged();
184 	}
185 }
186 
187 
188 void
implnCopy()189 implnCopy()
190 {
191 	ScTabViewShell* pViewShell = getCurrentBestViewShell();
192 	if ( pViewShell )
193 		pViewShell->CopyToClip(NULL,false,false,true);
194 }
195 
196 void
implnCut()197 implnCut()
198 {
199 	ScTabViewShell* pViewShell =  getCurrentBestViewShell();
200 	if ( pViewShell )
201 		pViewShell->CutToClip( NULL, sal_True );
202 }
203 
implnPasteSpecial(sal_uInt16 nFlags,sal_uInt16 nFunction,sal_Bool bSkipEmpty,sal_Bool bTranspose)204 void implnPasteSpecial(sal_uInt16 nFlags,sal_uInt16 nFunction,sal_Bool bSkipEmpty, sal_Bool bTranspose)
205 {
206 	PasteCellsWarningReseter resetWarningBox;
207 	sal_Bool bAsLink(sal_False), bOtherDoc(sal_False);
208 	InsCellCmd eMoveMode = INS_NONE;
209 
210 	ScTabViewShell* pTabViewShell = ScTabViewShell::GetActiveViewShell();
211 	if ( !pTabViewShell )
212 		// none active, try next best
213 		pTabViewShell = getCurrentBestViewShell();
214 	if ( pTabViewShell )
215 	{
216 		ScViewData* pView = pTabViewShell->GetViewData();
217 		Window* pWin = ( pView != NULL ) ? pView->GetActiveWin() : NULL;
218 		if ( pView && pWin )
219 		{
220 			if ( bAsLink && bOtherDoc )
221 				pTabViewShell->PasteFromSystem(0);//SOT_FORMATSTR_ID_LINK
222 			else
223 			{
224 				ScTransferObj* pOwnClip = ScTransferObj::GetOwnClipboard( pWin );
225 				ScDocument* pDoc = NULL;
226 				if ( pOwnClip )
227 					pDoc = pOwnClip->GetDocument();
228 				pTabViewShell->PasteFromClip( nFlags, pDoc,
229 					nFunction, bSkipEmpty, bTranspose, bAsLink,
230 					eMoveMode, IDF_NONE, sal_True );
231 				pTabViewShell->CellContentChanged();
232 			}
233 		}
234 	}
235 
236 }
237 
238  uno::Reference< frame::XModel >
getCurrentDocument()239 getCurrentDocument() throw (uno::RuntimeException)
240 {
241 	uno::Reference< frame::XModel > xModel;
242 	SbxObject* pBasic = dynamic_cast< SbxObject* > ( SFX_APP()->GetBasic() );
243 	SbxObject* basicChosen =  pBasic ;
244 	if ( basicChosen == NULL)
245 	{
246 		OSL_TRACE("getModelFromBasic() StarBASIC* is NULL" );
247 		return xModel;
248 	}
249     SbxObject* p = pBasic;
250     SbxObject* pParent = p->GetParent();
251     SbxObject* pParentParent = pParent ? pParent->GetParent() : NULL;
252 
253     if( pParentParent )
254     {
255         basicChosen = pParentParent;
256     }
257     else if( pParent )
258     {
259         basicChosen = pParent;
260     }
261 
262 
263     uno::Any aModel;
264     SbxVariable *pCompVar = basicChosen->Find(  UniString(RTL_CONSTASCII_USTRINGPARAM("ThisComponent")), SbxCLASS_OBJECT );
265 
266 	if ( pCompVar )
267 	{
268 		aModel = sbxToUnoValue( pCompVar );
269 		if ( sal_False == ( aModel >>= xModel ) ||
270 			!xModel.is() )
271 		{
272 			// trying last gasp try the current component
273 			uno::Reference< beans::XPropertySet > xProps( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
274 			// test if vba service is present
275 			uno::Reference< uno::XComponentContext > xCtx( xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ))), uno::UNO_QUERY_THROW );
276 			uno::Reference<lang::XMultiComponentFactory > xSMgr( xCtx->getServiceManager(), uno::UNO_QUERY_THROW );
277 			uno::Reference< frame::XDesktop > xDesktop (xSMgr->createInstanceWithContext(::rtl::OUString::createFromAscii("com.sun.star.frame.Desktop"), xCtx), uno::UNO_QUERY_THROW );
278 			xModel.set( xDesktop->getCurrentComponent(), uno::UNO_QUERY );
279 			if ( !xModel.is() )
280 			{
281 				throw uno::RuntimeException(
282 					rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Can't extract model from basic ( its obviously not set yet ) therefore don't know the currently selected document") ), uno::Reference< uno::XInterface >() );
283 			}
284 			return xModel;
285 		}
286 		else
287 		{
288 			OSL_TRACE("Have model ThisComponent points to url %s",
289 			::rtl::OUStringToOString( xModel->getURL(),
290 				RTL_TEXTENCODING_ASCII_US ).pData->buffer );
291 		}
292 	}
293 	else
294 	{
295 		OSL_TRACE("Failed to get ThisComponent");
296 		throw uno::RuntimeException(
297 			rtl::OUString(
298 				RTL_CONSTASCII_USTRINGPARAM(
299 					"Can't determine the currently selected document") ),
300 			uno::Reference< uno::XInterface >() );
301 	}
302 	return xModel;
303 }
304 
305 ScDocShell*
getDocShell(css::uno::Reference<css::frame::XModel> & xModel)306 getDocShell( css::uno::Reference< css::frame::XModel>& xModel )
307 {
308 	uno::Reference< uno::XInterface > xIf( xModel, uno::UNO_QUERY_THROW );
309 	ScModelObj* pModel = dynamic_cast< ScModelObj* >( xIf.get() );
310 	ScDocShell* pDocShell = NULL;
311 	if ( pModel )
312 		pDocShell = (ScDocShell*)pModel->GetEmbeddedObject();
313 	return pDocShell;
314 
315 }
316 
317 ScTabViewShell*
getBestViewShell(css::uno::Reference<css::frame::XModel> & xModel)318 getBestViewShell(  css::uno::Reference< css::frame::XModel>& xModel )
319 {
320 	ScDocShell* pDocShell = getDocShell( xModel );
321 	if ( pDocShell )
322 		return pDocShell->GetBestViewShell();
323 	return NULL;
324 }
325 
326 ScTabViewShell*
getCurrentBestViewShell()327 getCurrentBestViewShell()
328 {
329 	uno::Reference< frame::XModel > xModel = getCurrentDocument();
330 	return getBestViewShell( xModel );
331 }
332 
333 SfxViewFrame*
getCurrentViewFrame()334 getCurrentViewFrame()
335 {
336 	ScTabViewShell* pViewShell = getCurrentBestViewShell();
337 	if ( pViewShell )
338 		return pViewShell->GetViewFrame();
339 	return NULL;
340 }
341 
342 sal_Int32
OORGBToXLRGB(sal_Int32 nCol)343 OORGBToXLRGB( sal_Int32 nCol )
344 {
345 	sal_Int32 nRed = nCol;
346 	nRed &= 0x00FF0000;
347 	nRed >>= 16;
348 	sal_Int32 nGreen = nCol;
349 	nGreen &= 0x0000FF00;
350 	nGreen >>= 8;
351 	sal_Int32 nBlue = nCol;
352 	nBlue &= 0x000000FF;
353 	sal_Int32 nRGB =  ( (nBlue << 16) | (nGreen << 8) | nRed );
354 	return nRGB;
355 }
356 sal_Int32
XLRGBToOORGB(sal_Int32 nCol)357 XLRGBToOORGB( sal_Int32 nCol )
358 {
359 	sal_Int32 nBlue = nCol;
360 	nBlue &= 0x00FF0000;
361 	nBlue >>= 16;
362 	sal_Int32 nGreen = nCol;
363 	nGreen &= 0x0000FF00;
364 	nGreen >>= 8;
365 	sal_Int32 nRed = nCol;
366 	nRed &= 0x000000FF;
367 	sal_Int32 nRGB =  ( (nRed << 16) | (nGreen << 8) | nBlue );
368 	return nRGB;
369 }
370 uno::Any
OORGBToXLRGB(const uno::Any & aCol)371 OORGBToXLRGB( const uno::Any& aCol )
372 {
373 	sal_Int32 nCol=0;
374 	aCol >>= nCol;
375 	nCol = OORGBToXLRGB( nCol );
376 	return uno::makeAny( nCol );
377 }
378 uno::Any
XLRGBToOORGB(const uno::Any & aCol)379 XLRGBToOORGB(  const uno::Any& aCol )
380 {
381 	sal_Int32 nCol=0;
382 	aCol >>= nCol;
383 	nCol = XLRGBToOORGB( nCol );
384 	return uno::makeAny( nCol );
385 }
386 
PrintOutHelper(const uno::Any & From,const uno::Any & To,const uno::Any & Copies,const uno::Any & Preview,const uno::Any &,const uno::Any &,const uno::Any & Collate,const uno::Any & PrToFileName,css::uno::Reference<frame::XModel> & xModel,sal_Bool bUseSelection)387 void PrintOutHelper( const uno::Any& From, const uno::Any& To, const uno::Any& Copies, const uno::Any& Preview, const uno::Any& /*ActivePrinter*/, const uno::Any& /*PrintToFile*/, const uno::Any& Collate, const uno::Any& PrToFileName, css::uno::Reference< frame::XModel >& xModel, sal_Bool bUseSelection  )
388 {
389 	sal_Int32 nTo = 0;
390 	sal_Int32 nFrom = 0;
391 	sal_Int16 nCopies = 1;
392 	sal_Bool bPreview = sal_False;
393 	sal_Bool bCollate = sal_False;
394 	sal_Bool bSelection = bUseSelection;
395 	From >>= nFrom;
396 	To >>= nTo;
397 	Copies >>= nCopies;
398 	Preview >>= bPreview;
399 	if ( nCopies > 1 ) // Collate only useful when more that 1 copy
400 		Collate >>= bCollate;
401 
402 	rtl::OUString sRange(  RTL_CONSTASCII_USTRINGPARAM( "-" ) );
403 	rtl::OUString sFileName;
404 
405 	if (( nFrom || nTo ) )
406 	{
407 		if ( nFrom )
408 			sRange = ( ::rtl::OUString::valueOf( nFrom ) + sRange );
409 		if ( nTo )
410 			sRange += ::rtl::OUString::valueOf( nTo );
411 	}
412 
413 	if (  PrToFileName.getValue() )
414 	{
415 		PrToFileName >>= sFileName;
416 	}
417 	ScTabViewShell* pViewShell = getBestViewShell( xModel );
418 	SfxViewFrame* pViewFrame = NULL;
419 	if ( pViewShell )
420 		pViewFrame = pViewShell->GetViewFrame();
421 	if ( pViewFrame )
422 	{
423 		SfxAllItemSet aArgs( SFX_APP()->GetPool() );
424 
425 		SfxBoolItem sfxCollate( SID_PRINT_COLLATE, bCollate );
426 		aArgs.Put( sfxCollate, sfxCollate.Which() );
427 		SfxInt16Item sfxCopies( SID_PRINT_COPIES, nCopies );
428 		aArgs.Put( sfxCopies, sfxCopies.Which() );
429 		if ( sFileName.getLength() )
430 		{
431 			SfxStringItem sfxFileName( SID_FILE_NAME, sFileName);
432 			aArgs.Put( sfxFileName, sfxFileName.Which() );
433 
434 		}
435 		if (  sRange.getLength() )
436 		{
437 			SfxStringItem sfxRange( SID_PRINT_PAGES, sRange );
438 			aArgs.Put( sfxRange, sfxRange.Which() );
439 		}
440 		SfxBoolItem sfxSelection( SID_SELECTION, bSelection );
441 		aArgs.Put( sfxSelection, sfxSelection.Which() );
442 		SfxBoolItem sfxAsync( SID_ASYNCHRON, sal_False );
443 		aArgs.Put( sfxAsync, sfxAsync.Which() );
444 		SfxDispatcher* pDispatcher = pViewFrame->GetDispatcher();
445 
446 		if ( pDispatcher )
447 		{
448 			if ( bPreview )
449 			{
450 				if ( !pViewFrame->GetFrame().IsInPlace() )
451 				{
452 					SC_MOD()->InputEnterHandler();
453 					pViewFrame->GetDispatcher()->Execute( SID_VIEWSHELL1, SFX_CALLMODE_SYNCHRON );
454 					while ( isInPrintPreview( pViewFrame ) )
455 						Application::Yield();
456 				}
457 			}
458 			else
459 				pDispatcher->Execute( (sal_uInt16)SID_PRINTDOC, (SfxCallMode)SFX_CALLMODE_SYNCHRON, aArgs );
460 		}
461 
462 	}
463 
464 	// #FIXME #TODO
465 	// 1 ActivePrinter ( how/can we switch a printer via API? )
466 	// 2 PrintToFile ( ms behaviour if this option is specified but no
467 	//   filename supplied 'PrToFileName' then the user will be prompted )
468 	// 3 Need to check behaviour of Selected sheets with range ( e.g. From & To
469 	//    values ) in oOO these options are mutually exclusive
470 	// 4 There is a pop up to do with transparent objects in the print source
471 	//   should be able to disable that via configuration for the duration
472 	//   of this method
473 }
474 
PrintPreviewHelper(const css::uno::Any &,css::uno::Reference<css::frame::XModel> & xModel)475  void PrintPreviewHelper( const css::uno::Any& /*EnableChanges*/, css::uno::Reference< css::frame::XModel >& xModel )
476 {
477 	dispatchExecute( xModel, SID_VIEWSHELL1 );
478 }
479 
getAnyAsString(const uno::Any & pvargItem)480 rtl::OUString getAnyAsString( const uno::Any& pvargItem ) throw ( uno::RuntimeException )
481 {
482 	uno::Type aType = pvargItem.getValueType();
483 	uno::TypeClass eTypeClass = aType.getTypeClass();
484 	rtl::OUString sString;
485 	switch ( eTypeClass )
486 	{
487 		case uno::TypeClass_BOOLEAN:
488 		{
489 			sal_Bool bBool = sal_False;
490 			pvargItem >>= bBool;
491 			sString = rtl::OUString::valueOf( bBool );
492 			break;
493 		}
494 		case uno::TypeClass_STRING:
495 			pvargItem >>= sString;
496 			break;
497 		case uno::TypeClass_FLOAT:
498 			{
499 				float aFloat = 0;
500 				pvargItem >>= aFloat;
501 				sString = rtl::OUString::valueOf( aFloat );
502 				break;
503 			}
504 		case uno::TypeClass_DOUBLE:
505 			{
506 				double aDouble = 0;
507 				pvargItem >>= aDouble;
508 				sString = rtl::OUString::valueOf( aDouble );
509 				break;
510 			}
511 		case uno::TypeClass_SHORT:
512 		case uno::TypeClass_LONG:
513 		case uno::TypeClass_BYTE:
514 			{
515 				sal_Int32 aNum = 0;
516 				pvargItem >>= aNum;
517 				sString = rtl::OUString::valueOf( aNum );
518 				break;
519 			}
520 
521 		case uno::TypeClass_HYPER:
522 			{
523 				sal_Int64 aHyper = 0;
524 				pvargItem >>= aHyper;
525 				sString = rtl::OUString::valueOf( aHyper );
526 				break;
527 			}
528 		default:
529        			throw uno::RuntimeException( rtl::OUString::createFromAscii( "Invalid type, can't convert" ), uno::Reference< uno::XInterface >() );
530 	}
531 	return sString;
532 }
533 
534 
535 rtl::OUString
getUniqueName(const uno::Sequence<::rtl::OUString> & _slist,const rtl::OUString & _sElementName,const::rtl::OUString & _sSuffixSeparator)536 ContainerUtilities::getUniqueName( const uno::Sequence< ::rtl::OUString >&  _slist, const rtl::OUString& _sElementName, const ::rtl::OUString& _sSuffixSeparator)
537 {
538 	return getUniqueName(_slist, _sElementName, _sSuffixSeparator, sal_Int32(2));
539 }
540 
541 rtl::OUString
getUniqueName(const uno::Sequence<rtl::OUString> & _slist,const rtl::OUString _sElementName,const rtl::OUString & _sSuffixSeparator,sal_Int32 _nStartSuffix)542 ContainerUtilities::getUniqueName( const uno::Sequence< rtl::OUString >& _slist, const rtl::OUString _sElementName, const rtl::OUString& _sSuffixSeparator, sal_Int32 _nStartSuffix)
543 {
544 	sal_Int32 a = _nStartSuffix;
545 	rtl::OUString scompname = _sElementName;
546 	bool bElementexists = true;
547 	sal_Int32 nLen = _slist.getLength();
548 	if ( nLen == 0 )
549 		return _sElementName;
550 
551 	while (bElementexists == true)
552 	{
553 		for (sal_Int32 i = 0; i < nLen; i++)
554 		{
555 			if (FieldInList(_slist, scompname) == -1)
556 			{
557 				return scompname;
558 			}
559 		}
560 		scompname = _sElementName + _sSuffixSeparator + rtl::OUString::valueOf( a++ );
561 	}
562 	return rtl::OUString();
563 }
564 
565 sal_Int32
FieldInList(const uno::Sequence<rtl::OUString> & SearchList,const rtl::OUString & SearchString)566 ContainerUtilities::FieldInList( const uno::Sequence< rtl::OUString >& SearchList, const rtl::OUString& SearchString )
567 {
568 	sal_Int32 FieldLen = SearchList.getLength();
569 	sal_Int32 retvalue = -1;
570 	for (sal_Int32 i = 0; i < FieldLen; i++)
571 	{
572 		// I wonder why comparing lexicographically is done
573 		// when its a match is whats interesting?
574 		//if (SearchList[i].compareTo(SearchString) == 0)
575 		if ( SearchList[i].equals( SearchString ) )
576 		{
577 			retvalue = i;
578 			break;
579 		}
580 	}
581 	return retvalue;
582 
583 }
NeedEsc(sal_Unicode cCode)584 bool NeedEsc(sal_Unicode cCode)
585 {
586 	String sEsc(RTL_CONSTASCII_USTRINGPARAM(".^$+\\|{}()"));
587 	return (STRING_NOTFOUND != sEsc.Search(cCode));
588 }
589 
VBAToRegexp(const rtl::OUString & rIn,bool bForLike)590 rtl::OUString VBAToRegexp(const rtl::OUString &rIn, bool bForLike )
591 {
592 	rtl::OUStringBuffer sResult;
593 	const sal_Unicode *start = rIn.getStr();
594 	const sal_Unicode *end = start + rIn.getLength();
595 
596 	int seenright = 0;
597 	if ( bForLike )
598 		sResult.append(static_cast<sal_Unicode>('^'));
599 
600 	while (start < end)
601 	{
602 		switch (*start)
603 		{
604 			case '?':
605 				sResult.append(static_cast<sal_Unicode>('.'));
606 				start++;
607 				break;
608 			case '*':
609 				sResult.append(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".*")));
610 				start++;
611 				break;
612 			case '#':
613 				sResult.append(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("[0-9]")));
614 				start++;
615 				break;
616 			case '~':
617 				sResult.append(static_cast<sal_Unicode>('\\'));
618 				sResult.append(*(++start));
619 				start++;
620 				break;
621 				// dump the ~ and escape the next characture
622 			case ']':
623 				sResult.append(static_cast<sal_Unicode>('\\'));
624 				sResult.append(*start++);
625 				break;
626 			case '[':
627 				sResult.append(*start++);
628 				seenright = 0;
629 				while (start < end && !seenright)
630 				{
631 					switch (*start)
632 					{
633 						case '[':
634 						case '?':
635 						case '*':
636 						sResult.append(static_cast<sal_Unicode>('\\'));
637 						sResult.append(*start);
638 							break;
639 						case ']':
640 						sResult.append(*start);
641 							seenright = 1;
642 							break;
643 						case '!':
644 							sResult.append(static_cast<sal_Unicode>('^'));
645 							break;
646 						default:
647 						if (NeedEsc(*start))
648 							sResult.append(static_cast<sal_Unicode>('\\'));
649 						sResult.append(*start);
650 							break;
651 					}
652 					start++;
653 				}
654 				break;
655 			default:
656 				if (NeedEsc(*start))
657 					sResult.append(static_cast<sal_Unicode>('\\'));
658 				sResult.append(*start++);
659 		}
660 	}
661 
662 	if ( bForLike )
663 		sResult.append(static_cast<sal_Unicode>('$'));
664 
665 	return sResult.makeStringAndClear( );
666 }
667 
getPixelTo100thMillimeterConversionFactor(css::uno::Reference<css::awt::XDevice> & xDevice,sal_Bool bVertical)668 double getPixelTo100thMillimeterConversionFactor( css::uno::Reference< css::awt::XDevice >& xDevice, sal_Bool bVertical)
669 {
670 	double fConvertFactor = 1.0;
671 	if( bVertical )
672 	{
673 		fConvertFactor = xDevice->getInfo().PixelPerMeterY/100000;
674 	}
675 	else
676 	{
677 		fConvertFactor = xDevice->getInfo().PixelPerMeterX/100000;
678 	}
679 	return fConvertFactor;
680 }
681 
PointsToPixels(css::uno::Reference<css::awt::XDevice> & xDevice,double fPoints,sal_Bool bVertical)682 double PointsToPixels( css::uno::Reference< css::awt::XDevice >& xDevice, double fPoints, sal_Bool bVertical)
683 {
684 	double fConvertFactor = getPixelTo100thMillimeterConversionFactor( xDevice, bVertical );
685 	return fPoints * POINTTO100THMILLIMETERFACTOR * fConvertFactor;
686 }
PixelsToPoints(css::uno::Reference<css::awt::XDevice> & xDevice,double fPixels,sal_Bool bVertical)687 double PixelsToPoints( css::uno::Reference< css::awt::XDevice >& xDevice, double fPixels, sal_Bool bVertical)
688 {
689 	double fConvertFactor = getPixelTo100thMillimeterConversionFactor( xDevice, bVertical );
690 	return (fPixels/fConvertFactor)/POINTTO100THMILLIMETERFACTOR;
691 }
692 
ConcreteXShapeGeometryAttributes(const css::uno::Reference<css::uno::XComponentContext> & xContext,const css::uno::Reference<css::drawing::XShape> & xShape)693 ConcreteXShapeGeometryAttributes::ConcreteXShapeGeometryAttributes( const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Reference< css::drawing::XShape >& xShape )
694 {
695 	m_xShape = new ScVbaShape( xContext, xShape );
696 }
697 
698 #define VBA_LEFT "PositionX"
699 #define VBA_TOP "PositionY"
UserFormGeometryHelper(const uno::Reference<uno::XComponentContext> &,const uno::Reference<awt::XControl> & xControl)700 UserFormGeometryHelper::UserFormGeometryHelper( const uno::Reference< uno::XComponentContext >& /*xContext*/, const uno::Reference< awt::XControl >& xControl )
701 {
702     mxModel.set( xControl->getModel(), uno::UNO_QUERY_THROW );
703 }
getLeft()704     double UserFormGeometryHelper::getLeft()
705     {
706 	sal_Int32 nLeft = 0;
707 	mxModel->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( VBA_LEFT ) ) ) >>= nLeft;
708 	return Millimeter::getInPoints( nLeft );
709     }
setLeft(double nLeft)710     void UserFormGeometryHelper::setLeft( double nLeft )
711     {
712         mxModel->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( VBA_LEFT ) ), uno::makeAny( Millimeter::getInHundredthsOfOneMillimeter( nLeft ) ) );
713     }
getTop()714     double UserFormGeometryHelper::getTop()
715     {
716 	sal_Int32 nTop = 0;
717 	mxModel->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( 	VBA_TOP ) ) ) >>= nTop;
718 	return Millimeter::getInPoints( nTop );
719     }
setTop(double nTop)720     void UserFormGeometryHelper::setTop( double nTop )
721     {
722 	mxModel->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( 	VBA_TOP ) ), uno::makeAny( Millimeter::getInHundredthsOfOneMillimeter( nTop ) ) );
723     }
getHeight()724     double UserFormGeometryHelper::getHeight()
725     {
726 	sal_Int32 nHeight = 0;
727 	mxModel->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( 	SC_UNONAME_CELLHGT ) ) ) >>= nHeight;
728 	return Millimeter::getInPoints( nHeight );
729     }
setHeight(double nHeight)730     void UserFormGeometryHelper::setHeight( double nHeight )
731     {
732 	mxModel->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( 	SC_UNONAME_CELLHGT ) ), uno::makeAny( Millimeter::getInHundredthsOfOneMillimeter( nHeight ) ) );
733     }
getWidth()734     double UserFormGeometryHelper::getWidth()
735     {
736 	sal_Int32 nWidth = 0;
737 	mxModel->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( 	SC_UNONAME_CELLWID ) ) ) >>= nWidth;
738 	return Millimeter::getInPoints( nWidth );
739     }
setWidth(double nWidth)740     void UserFormGeometryHelper::setWidth( double nWidth)
741     {
742 	mxModel->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( 	SC_UNONAME_CELLWID ) ), uno::makeAny(  Millimeter::getInHundredthsOfOneMillimeter( nWidth ) ) );
743     }
744 
745 SfxItemSet*
GetDataSet(ScCellRangeObj * pRangeObj)746 ScVbaCellRangeAccess::GetDataSet( ScCellRangeObj* pRangeObj )
747 {
748 	SfxItemSet* pDataSet = pRangeObj ? pRangeObj->GetCurrentDataSet( true ) : NULL ;
749 	return pDataSet;
750 
751 }
752 
753 } // vba
754 } // ooo
755