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/frame/XModel2.hpp>
31 #include <com/sun/star/script/XDefaultProperty.hpp>
32 #include <com/sun/star/uno/XComponentContext.hpp>
33 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
34 #include <com/sun/star/lang/XUnoTunnel.hpp>
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <com/sun/star/beans/XIntrospection.hpp>
37 #include <com/sun/star/util/MeasureUnit.hpp>
38 #include <com/sun/star/awt/XWindow.hpp>
39 #include <com/sun/star/awt/XDialog.hpp>
40 #include <com/sun/star/awt/PosSize.hpp>
41 
42 #include <ooo/vba/msforms/XShape.hpp>
43 
44 #include <comphelper/processfactory.hxx>
45 
46 #include <sfx2/objsh.hxx>
47 #include <sfx2/viewfrm.hxx>
48 #include <sfx2/dispatch.hxx>
49 #include <sfx2/app.hxx>
50 #include <svl/stritem.hxx>
51 #include <svl/eitem.hxx>
52 #include <svl/intitem.hxx>
53 #include <svl/itemset.hxx>
54 #include <sfx2/docfac.hxx>
55 #include <sfx2/viewfac.hxx>
56 
57 #include <basic/sbx.hxx>
58 #include <basic/sbstar.hxx>
59 #include <basic/basmgr.hxx>
60 #include <basic/sbmod.hxx>
61 #include <basic/sbmeth.hxx>
62 #include <rtl/math.hxx>
63 #include <sfx2/viewsh.hxx>
64 #include <math.h>
65 #include <tools/urlobj.hxx>
66 #include <osl/file.hxx>
67 #include <toolkit/awt/vclxwindow.hxx>
68 #include <toolkit/helper/vclunohelper.hxx>
69 #include <vcl/window.hxx>
70 #include <vcl/syswin.hxx>
71 #include <tools/diagnose_ex.h>
72 
73 #ifndef OOVBA_DLLIMPLEMENTATION
74 #define OOVBA_DLLIMPLEMENTATION
75 #endif
76 
77 #include <vbahelper/vbahelper.hxx>
78 #include <sfx2/app.hxx>
79 // #FIXME needs service
80 //#include "vbashape.hxx"
81 //#include "unonames.hxx"
82 
83 using namespace ::com::sun::star;
84 using namespace ::ooo::vba;
85 
86 void unoToSbxValue( SbxVariable* pVar, const uno::Any& aValue );
87 
88 uno::Any sbxToUnoValue( SbxVariable* pVar );
89 
90 
91 namespace ooo
92 {
93 namespace vba
94 {
95 
96 namespace { const double factor =  2540.0 / 72.0; }
97 
createVBAUnoAPIService(SfxObjectShell * pShell,const sal_Char * _pAsciiName)98 css::uno::Reference< css::uno::XInterface > createVBAUnoAPIService( SfxObjectShell* pShell, const sal_Char* _pAsciiName ) throw (css::uno::RuntimeException)
99 {
100     OSL_PRECOND( pShell, "createVBAUnoAPIService: no shell!" );
101     ::rtl::OUString sVarName( ::rtl::OUString::createFromAscii( _pAsciiName ) );
102     return getVBAServiceFactory( pShell )->createInstance( sVarName );
103 }
104 
105 // helper method to determine if the view ( calc ) is in print-preview mode
isInPrintPreview(SfxViewFrame * pView)106 bool isInPrintPreview( SfxViewFrame* pView )
107 {
108 	sal_uInt16 nViewNo = SID_VIEWSHELL1 - SID_VIEWSHELL0;
109 	if ( pView->GetObjectShell()->GetFactory().GetViewFactoryCount() >
110 nViewNo && !pView->GetObjectShell()->IsInPlaceActive() )
111 	{
112 		SfxViewFactory &rViewFactory =
113 			pView->GetObjectShell()->GetFactory().GetViewFactory(nViewNo);
114 		if (  pView->GetCurViewId() == rViewFactory.GetOrdinal() )
115 			return true;
116 	}
117 	return false;
118 }
119 #if 0
120 namespace excel // all in this namespace probably can be moved to sc
121 {
122 
123 
124 const ::rtl::OUString REPLACE_CELLS_WARNING(  RTL_CONSTASCII_USTRINGPARAM( "ReplaceCellsWarning"));
125 class PasteCellsWarningReseter
126 {
127 private:
128 	bool bInitialWarningState;
129 	static uno::Reference< beans::XPropertySet > getGlobalSheetSettings() throw ( uno::RuntimeException )
130 	{
131 		static uno::Reference< beans::XPropertySet > xTmpProps( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
132 		static uno::Reference<uno::XComponentContext > xContext( xTmpProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ))), uno::UNO_QUERY_THROW );
133 		static uno::Reference<lang::XMultiComponentFactory > xServiceManager(
134 				xContext->getServiceManager(), uno::UNO_QUERY_THROW );
135 		static uno::Reference< beans::XPropertySet > xProps( xServiceManager->createInstanceWithContext( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sheet.GlobalSheetSettings" ) ) ,xContext ), uno::UNO_QUERY_THROW );
136 		return xProps;
137 	}
138 
139 	bool getReplaceCellsWarning() throw ( uno::RuntimeException )
140 	{
141 		sal_Bool res = sal_False;
142 		getGlobalSheetSettings()->getPropertyValue( REPLACE_CELLS_WARNING ) >>= res;
143 		return ( res == sal_True );
144 	}
145 
146 	void setReplaceCellsWarning( bool bState ) throw ( uno::RuntimeException )
147 	{
148 		getGlobalSheetSettings()->setPropertyValue( REPLACE_CELLS_WARNING, uno::makeAny( bState ) );
149 	}
150 public:
151 	PasteCellsWarningReseter() throw ( uno::RuntimeException )
152 	{
153 		bInitialWarningState = getReplaceCellsWarning();
154 		if ( bInitialWarningState )
155 			setReplaceCellsWarning( false );
156 	}
157 	~PasteCellsWarningReseter()
158 	{
159 		if ( bInitialWarningState )
160 		{
161 			// don't allow dtor to throw
162 			try
163 			{
164 				setReplaceCellsWarning( true );
165 			}
166 			catch ( uno::Exception& /*e*/ ){}
167 		}
168 	}
169 };
170 
171 void
172 implnPaste()
173 {
174 	PasteCellsWarningReseter resetWarningBox;
175 	ScTabViewShell* pViewShell = getCurrentBestViewShell();
176 	if ( pViewShell )
177 	{
178 		pViewShell->PasteFromSystem();
179 		pViewShell->CellContentChanged();
180 	}
181 }
182 
183 
184 void
185 implnCopy()
186 {
187 	ScTabViewShell* pViewShell = getCurrentBestViewShell();
188 	if ( pViewShell )
189 		pViewShell->CopyToClip(NULL,false,false,true);
190 }
191 
192 void
193 implnCut()
194 {
195 	ScTabViewShell* pViewShell =  getCurrentBestViewShell();
196 	if ( pViewShell )
197 		pViewShell->CutToClip( NULL, sal_True );
198 }
199 void implnPasteSpecial(SfxViewShell* pViewShell, sal_uInt16 nFlags,sal_uInt16 nFunction,sal_Bool bSkipEmpty, sal_Bool bTranspose)
200 {
201 	PasteCellsWarningReseter resetWarningBox;
202 	sal_Bool bAsLink(sal_False), bOtherDoc(sal_False);
203 	InsCellCmd eMoveMode = INS_NONE;
204 
205 	if ( !pTabViewShell )
206 		// none active, try next best
207 		pTabViewShell = getCurrentBestViewShell();
208 	if ( pTabViewShell )
209 	{
210 		ScViewData* pView = pTabViewShell->GetViewData();
211 		Window* pWin = ( pView != NULL ) ? pView->GetActiveWin() : NULL;
212 		if ( pView && pWin )
213 		{
214 			if ( bAsLink && bOtherDoc )
215 				pTabViewShell->PasteFromSystem(0);//SOT_FORMATSTR_ID_LINK
216 			else
217 			{
218 				ScTransferObj* pOwnClip = ScTransferObj::GetOwnClipboard( pWin );
219 				ScDocument* pDoc = NULL;
220 				if ( pOwnClip )
221 					pDoc = pOwnClip->GetDocument();
222 				pTabViewShell->PasteFromClip( nFlags, pDoc,
223 					nFunction, bSkipEmpty, bTranspose, bAsLink,
224 					eMoveMode, IDF_NONE, sal_True );
225 				pTabViewShell->CellContentChanged();
226 			}
227 		}
228 	}
229 
230 }
231 
232 ScDocShell*
233 getDocShell( css::uno::Reference< css::frame::XModel>& xModel )
234 {
235 	uno::Reference< uno::XInterface > xIf( xModel, uno::UNO_QUERY_THROW );
236 	ScModelObj* pModel = dynamic_cast< ScModelObj* >( xIf.get() );
237 	ScDocShell* pDocShell = NULL;
238 	if ( pModel )
239 		pDocShell = (ScDocShell*)pModel->GetEmbeddedObject();
240 	return pDocShell;
241 
242 }
243 
244 ScTabViewShell*
245 getBestViewShell(  css::uno::Reference< css::frame::XModel>& xModel )
246 {
247 	ScDocShell* pDocShell = getDocShell( xModel );
248 	if ( pDocShell )
249 		return pDocShell->GetBestViewShell();
250 	return NULL;
251 }
252 
253 ScTabViewShell*
254 getCurrentBestViewShell()
255 {
256 	uno::Reference< frame::XModel > xModel = getCurrentDocument();
257 	return getBestViewShell( xModel );
258 }
259 
260 SfxViewFrame*
261 getCurrentViewFrame()
262 {
263 	ScTabViewShell* pViewShell = getCurrentBestViewShell();
264 	if ( pViewShell )
265 		return pViewShell->GetViewFrame();
266 	return NULL;
267 }
268 };
269 
270 #endif
271 
272 uno::Reference< beans::XIntrospectionAccess >
getIntrospectionAccess(const uno::Any & aObject)273 getIntrospectionAccess( const uno::Any& aObject ) throw (uno::RuntimeException)
274 {
275 	static uno::Reference< beans::XIntrospection > xIntrospection;
276 	if( !xIntrospection.is() )
277 	{
278 		uno::Reference< lang::XMultiServiceFactory > xFactory( comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
279 		xIntrospection.set( xFactory->createInstance( rtl::OUString::createFromAscii("com.sun.star.beans.Introspection") ), uno::UNO_QUERY_THROW );
280 	}
281 	return xIntrospection->inspect( aObject );
282 }
283 
284 uno::Reference< script::XTypeConverter >
getTypeConverter(const uno::Reference<uno::XComponentContext> & xContext)285 getTypeConverter( const uno::Reference< uno::XComponentContext >& xContext ) throw (uno::RuntimeException)
286 {
287 	static uno::Reference< script::XTypeConverter > xTypeConv( xContext->getServiceManager()->createInstanceWithContext( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.script.Converter") ), xContext ), uno::UNO_QUERY_THROW );
288 	return xTypeConv;
289 }
290 const uno::Any&
aNULL()291 aNULL()
292 {
293  	static  uno::Any aNULLL = uno::makeAny( uno::Reference< uno::XInterface >() );
294 	return aNULLL;
295 }
296 
dispatchExecute(SfxViewShell * pViewShell,sal_uInt16 nSlot,SfxCallMode nCall)297 void dispatchExecute(SfxViewShell* pViewShell, sal_uInt16 nSlot, SfxCallMode nCall)
298 {
299 	SfxViewFrame* pViewFrame = NULL;
300 	if ( pViewShell )
301 		pViewFrame = pViewShell->GetViewFrame();
302 	if ( pViewFrame )
303 	{
304 		SfxDispatcher* pDispatcher = pViewFrame->GetDispatcher();
305 		if( pDispatcher )
306 		{
307 			pDispatcher->Execute( nSlot , nCall );
308 		}
309 	}
310 }
311 
312 void
dispatchRequests(const uno::Reference<frame::XModel> & xModel,const rtl::OUString & aUrl,const uno::Sequence<beans::PropertyValue> & sProps)313 dispatchRequests( const uno::Reference< frame::XModel>& xModel, const rtl::OUString& aUrl, const uno::Sequence< beans::PropertyValue >& sProps )
314 {
315 	util::URL url;
316 	url.Complete = aUrl;
317 	rtl::OUString emptyString = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "" ));
318 	uno::Reference<frame::XController> xController = xModel->getCurrentController();
319 	uno::Reference<frame::XFrame> xFrame = xController->getFrame();
320 	uno::Reference<frame::XDispatchProvider> xDispatchProvider (xFrame,uno::UNO_QUERY_THROW);
321 	try
322 	{
323 		uno::Reference< beans::XPropertySet > xProps( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
324 		uno::Reference<uno::XComponentContext > xContext( xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ))), uno::UNO_QUERY_THROW  );
325 		if ( !xContext.is() )
326 			return;
327 
328 		uno::Reference<lang::XMultiComponentFactory > xServiceManager = xContext->getServiceManager();
329 		if ( !xServiceManager.is() )
330 			return;
331 
332 		uno::Reference<util::XURLTransformer> xParser( xServiceManager->createInstanceWithContext(
333             rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.URLTransformer" ) ), xContext),
334             uno::UNO_QUERY_THROW );
335 		if (!xParser.is())
336 			return;
337 		xParser->parseStrict (url);
338 	}
339 	catch ( uno::Exception & /*e*/ )
340 	{
341 		return;
342 	}
343 
344 	uno::Reference<frame::XDispatch> xDispatcher = xDispatchProvider->queryDispatch(url,emptyString,0);
345 
346 	uno::Sequence<beans::PropertyValue> dispatchProps(1);
347 
348 	sal_Int32 nProps = sProps.getLength();
349 	beans::PropertyValue* pDest = dispatchProps.getArray();
350 	if ( nProps )
351 	{
352 		dispatchProps.realloc( nProps + 1 );
353 		// need to reaccquire pDest after realloc
354 		pDest = dispatchProps.getArray();
355 		const beans::PropertyValue* pSrc = sProps.getConstArray();
356 		for ( sal_Int32 index=0; index<nProps; ++index, ++pSrc, ++pDest )
357 			*pDest = *pSrc;
358 	}
359 
360 	(*pDest).Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Silent" ));
361 	(*pDest).Value <<= (sal_Bool)sal_True;
362 
363 	if (xDispatcher.is())
364 		xDispatcher->dispatch( url, dispatchProps );
365 }
366 
367 void
dispatchRequests(const uno::Reference<frame::XModel> & xModel,const rtl::OUString & aUrl)368 dispatchRequests( const uno::Reference< frame::XModel>& xModel, const rtl::OUString& aUrl )
369 {
370 	uno::Sequence<beans::PropertyValue> dispatchProps;
371 	dispatchRequests( xModel, aUrl, dispatchProps );
372 }
373 
374 uno::Reference< frame::XModel >
getCurrentDoc(const rtl::OUString & sKey)375 getCurrentDoc( const rtl::OUString& sKey ) throw (uno::RuntimeException)
376 {
377 	uno::Reference< frame::XModel > xModel;
378 	SbxObject* pBasic = dynamic_cast< SbxObject* > ( SFX_APP()->GetBasic() );
379 	SbxObject* basicChosen =  pBasic ;
380 	if ( basicChosen == NULL)
381 	{
382 		OSL_TRACE("getModelFromBasic() StarBASIC* is NULL" );
383 		return xModel;
384 	}
385     SbxObject* p = pBasic;
386     SbxObject* pParent = p->GetParent();
387     SbxObject* pParentParent = pParent ? pParent->GetParent() : NULL;
388 
389     if( pParentParent )
390     {
391         basicChosen = pParentParent;
392     }
393     else if( pParent )
394     {
395         basicChosen = pParent;
396     }
397 
398 
399     uno::Any aModel;
400     SbxVariable *pCompVar = basicChosen->Find(  sKey, SbxCLASS_OBJECT );
401 
402 	if ( pCompVar )
403 	{
404 		aModel = sbxToUnoValue( pCompVar );
405 		if ( sal_False == ( aModel >>= xModel ) ||
406 			!xModel.is() )
407 		{
408 			throw uno::RuntimeException(
409 				rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Can't extract model from basic ( its obviously not set yet ) therefore don't know the current documet context") ), uno::Reference< uno::XInterface >() );
410 		}
411 		else
412 		{
413 			OSL_TRACE("Have model points to url %s",
414 			::rtl::OUStringToOString( xModel->getURL(),
415 				RTL_TEXTENCODING_ASCII_US ).pData->buffer );
416 		}
417 	}
418 	else
419 	{
420 		OSL_TRACE("Failed to get %s", rtl::OUStringToOString( sKey, RTL_TEXTENCODING_UTF8 ).getStr() );
421 		throw uno::RuntimeException(
422 			rtl::OUString(
423 				RTL_CONSTASCII_USTRINGPARAM(
424 					"Can't determine the currently selected document") ),
425 			uno::Reference< uno::XInterface >() );
426 	}
427 	return xModel;
428 }
429 
430  uno::Reference< frame::XModel >
getCurrentDocCtx(const rtl::OUString & ctxName,const uno::Reference<uno::XComponentContext> & xContext)431 getCurrentDocCtx( const rtl::OUString& ctxName, const uno::Reference< uno::XComponentContext >& xContext ) throw (uno::RuntimeException)
432 {
433     uno::Reference< frame::XModel > xModel;
434      // try fallback to calling doc
435      css::uno::Reference< css::container::XNameAccess > xNameAccess( xContext, css::uno::UNO_QUERY_THROW );
436      xModel.set( xNameAccess->getByName( ctxName ), uno::UNO_QUERY_THROW );
437      return xModel;
438 }
439 
440 uno::Reference< frame::XModel >
getThisExcelDoc(const uno::Reference<uno::XComponentContext> & xContext)441 getThisExcelDoc( const uno::Reference< uno::XComponentContext >& xContext ) throw (uno::RuntimeException)
442 {
443     return getCurrentDocCtx( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ExcelDocumentContext" ) ), xContext );
444 }
445 
446 uno::Reference< frame::XModel >
getThisWordDoc(const uno::Reference<uno::XComponentContext> & xContext)447 getThisWordDoc( const uno::Reference< uno::XComponentContext >& xContext ) throw (uno::RuntimeException)
448 {
449     return getCurrentDocCtx( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("WordDocumentContext" ) ), xContext );
450 }
451 
452  uno::Reference< frame::XModel >
getCurrentExcelDoc(const uno::Reference<uno::XComponentContext> & xContext)453 getCurrentExcelDoc( const uno::Reference< uno::XComponentContext >& xContext ) throw (uno::RuntimeException)
454 {
455     static const rtl::OUString sThisExcelDoc( RTL_CONSTASCII_USTRINGPARAM("ThisExcelDoc" ) );
456     uno::Reference< frame::XModel > xModel;
457     try
458     {
459         xModel = getCurrentDoc( sThisExcelDoc );
460     }
461     catch( uno::Exception& e )
462     {
463         xModel = getThisExcelDoc( xContext );
464     }
465     return xModel;
466 }
467 
468  uno::Reference< frame::XModel >
getCurrentWordDoc(const uno::Reference<uno::XComponentContext> & xContext)469 getCurrentWordDoc( const uno::Reference< uno::XComponentContext >& xContext ) throw (uno::RuntimeException)
470 {
471     static const rtl::OUString sThisWordDoc( RTL_CONSTASCII_USTRINGPARAM("ThisWordDoc" ) );
472     uno::Reference< frame::XModel > xModel;
473     try
474     {
475         xModel = getCurrentDoc( sThisWordDoc );
476     }
477     catch( uno::Exception& e )
478     {
479         xModel = getThisWordDoc( xContext );
480     }
481     return xModel;
482 }
483 
484 sal_Int32
OORGBToXLRGB(sal_Int32 nCol)485 OORGBToXLRGB( sal_Int32 nCol )
486 {
487         sal_Int32 nAutoBits = nCol;
488         nAutoBits &= 0xFF000000;
489 	sal_Int32 nRed = nCol;
490 	nRed &= 0x00FF0000;
491 	nRed >>= 16;
492 	sal_Int32 nGreen = nCol;
493 	nGreen &= 0x0000FF00;
494 	nGreen >>= 8;
495 	sal_Int32 nBlue = nCol;
496 	nBlue &= 0x000000FF;
497 	sal_Int32 nRGB =  ( nAutoBits | (nBlue << 16) | (nGreen << 8) | nRed );
498 	return nRGB;
499 }
500 sal_Int32
XLRGBToOORGB(sal_Int32 nCol)501 XLRGBToOORGB( sal_Int32 nCol )
502 {
503         sal_Int32 nAutoBits = nCol;
504         nAutoBits &= 0xFF000000;
505 
506 	sal_Int32 nBlue = nCol;
507 	nBlue &= 0x00FF0000;
508 	nBlue >>= 16;
509 	sal_Int32 nGreen = nCol;
510 	nGreen &= 0x0000FF00;
511 	nGreen >>= 8;
512 	sal_Int32 nRed = nCol;
513 	nRed &= 0x000000FF;
514 	sal_Int32 nRGB =  ( nAutoBits | (nRed << 16) | (nGreen << 8) | nBlue );
515 	return nRGB;
516 }
517 uno::Any
OORGBToXLRGB(const uno::Any & aCol)518 OORGBToXLRGB( const uno::Any& aCol )
519 {
520 	sal_Int32 nCol(0);
521 	aCol >>= nCol;
522 	nCol = OORGBToXLRGB( nCol );
523 	return uno::makeAny( nCol );
524 }
525 uno::Any
XLRGBToOORGB(const uno::Any & aCol)526 XLRGBToOORGB(  const uno::Any& aCol )
527 {
528 	sal_Int32 nCol(0);
529 	aCol >>= nCol;
530 	nCol = XLRGBToOORGB( nCol );
531 	return uno::makeAny( nCol );
532 }
533 
PrintOutHelper(SfxViewShell * pViewShell,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,sal_Bool bUseSelection)534 void PrintOutHelper( SfxViewShell* pViewShell, 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, sal_Bool bUseSelection  )
535 {
536 	sal_Int32 nTo = 0;
537 	sal_Int32 nFrom = 0;
538 	sal_Int16 nCopies = 1;
539 	sal_Bool bPreview = sal_False;
540 	sal_Bool bCollate = sal_False;
541 	sal_Bool bSelection = bUseSelection;
542 	From >>= nFrom;
543 	To >>= nTo;
544 	Copies >>= nCopies;
545 	Preview >>= bPreview;
546 	if ( nCopies > 1 ) // Collate only useful when more that 1 copy
547 		Collate >>= bCollate;
548 
549 	rtl::OUString sRange(  RTL_CONSTASCII_USTRINGPARAM( "-" ) );
550 	rtl::OUString sFileName;
551 
552 	if (( nFrom || nTo ) )
553 	{
554 		if ( nFrom )
555 			sRange = ( ::rtl::OUString::valueOf( nFrom ) + sRange );
556 		if ( nTo )
557 			sRange += ::rtl::OUString::valueOf( nTo );
558 	}
559 
560 	if (  PrToFileName.getValue() )
561 	{
562 		PrToFileName >>= sFileName;
563 	}
564 	SfxViewFrame* pViewFrame = NULL;
565 	if ( pViewShell )
566 		pViewFrame = pViewShell->GetViewFrame();
567 	if ( pViewFrame )
568 	{
569 		SfxAllItemSet aArgs( SFX_APP()->GetPool() );
570 
571 		SfxBoolItem sfxCollate( SID_PRINT_COLLATE, bCollate );
572 		aArgs.Put( sfxCollate, sfxCollate.Which() );
573 		SfxInt16Item sfxCopies( SID_PRINT_COPIES, nCopies );
574 		aArgs.Put( sfxCopies, sfxCopies.Which() );
575 		if ( sFileName.getLength() )
576 		{
577 			SfxStringItem sfxFileName( SID_FILE_NAME, sFileName);
578 			aArgs.Put( sfxFileName, sfxFileName.Which() );
579 
580 		}
581 		if (  sRange.getLength() )
582 		{
583 			SfxStringItem sfxRange( SID_PRINT_PAGES, sRange );
584 			aArgs.Put( sfxRange, sfxRange.Which() );
585 		}
586 		SfxBoolItem sfxSelection( SID_SELECTION, bSelection );
587 		aArgs.Put( sfxSelection, sfxSelection.Which() );
588 		SfxBoolItem sfxAsync( SID_ASYNCHRON, sal_False );
589 		aArgs.Put( sfxAsync, sfxAsync.Which() );
590 		SfxDispatcher* pDispatcher = pViewFrame->GetDispatcher();
591 
592 		if ( pDispatcher )
593 		{
594 			if ( bPreview )
595 			{
596 				if ( !pViewFrame->GetFrame().IsInPlace() )
597 				{
598 					// #TODO is this necessary ( calc specific )
599 //					SC_MOD()->InputEnterHandler();
600 					pViewFrame->GetDispatcher()->Execute( SID_VIEWSHELL1, SFX_CALLMODE_SYNCHRON );
601 					while ( isInPrintPreview( pViewFrame ) )
602 						Application::Yield();
603 				}
604 			}
605 			else
606 				pDispatcher->Execute( (sal_uInt16)SID_PRINTDOC, (SfxCallMode)SFX_CALLMODE_SYNCHRON, aArgs );
607 		}
608 
609 	}
610 
611 	// #FIXME #TODO
612 	// 1 ActivePrinter ( how/can we switch a printer via API? )
613 	// 2 PrintToFile ( ms behaviour if this option is specified but no
614 	//   filename supplied 'PrToFileName' then the user will be prompted )
615 	// 3 Need to check behaviour of Selected sheets with range ( e.g. From & To
616 	//    values ) in oOO these options are mutually exclusive
617 	// 4 There is a pop up to do with transparent objects in the print source
618 	//   should be able to disable that via configuration for the duration
619 	//   of this method
620 }
621 
PrintPreviewHelper(const css::uno::Any &,SfxViewShell * pViewShell)622  void PrintPreviewHelper( const css::uno::Any& /*EnableChanges*/, SfxViewShell* pViewShell )
623 {
624 	dispatchExecute( pViewShell, SID_VIEWSHELL1 );
625 }
626 
extractIntFromAny(const uno::Any & rAny)627 sal_Int32 extractIntFromAny( const uno::Any& rAny ) throw (uno::RuntimeException)
628 {
629     switch( rAny.getValueType().getTypeClass() )
630     {
631         case uno::TypeClass_FLOAT:
632             return static_cast< sal_Int32 >( rAny.get< float >() );
633         case uno::TypeClass_DOUBLE:
634             return static_cast< sal_Int32 >( rAny.get< double >() );
635         case uno::TypeClass_BYTE:
636         case uno::TypeClass_SHORT:
637         case uno::TypeClass_LONG:
638             return rAny.get< sal_Int32 >();
639         default:;
640     }
641     throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Invalid type, cannot convert to integer." ) ), 0 );
642 }
643 
extractIntFromAny(const uno::Any & rAny,sal_Int32 nDefault)644 sal_Int32 extractIntFromAny( const uno::Any& rAny, sal_Int32 nDefault ) throw (uno::RuntimeException)
645 {
646     return rAny.hasValue() ? extractIntFromAny( rAny ) : nDefault;
647 }
648 
extractBoolFromAny(const uno::Any & rAny)649 bool extractBoolFromAny( const uno::Any& rAny ) throw (uno::RuntimeException)
650 {
651     switch( rAny.getValueType().getTypeClass() )
652     {
653         case uno::TypeClass_BOOLEAN:
654             return rAny.get< bool >();
655         case uno::TypeClass_FLOAT:
656             return rAny.get< float >() != 0.0;
657         case uno::TypeClass_DOUBLE:
658             return rAny.get< double >() != 0.0;
659         case uno::TypeClass_BYTE:
660         case uno::TypeClass_SHORT:
661         case uno::TypeClass_LONG:
662             return rAny.get< sal_Int32 >() != 0;
663         case uno::TypeClass_HYPER:
664             return rAny.get< sal_Int64 >() != 0;
665         default:;
666     }
667     throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Invalid type, cannot convert to boolean." ) ), 0 );
668 }
669 
extractBoolFromAny(const uno::Any & rAny,bool bDefault)670 bool extractBoolFromAny( const uno::Any& rAny, bool bDefault ) throw (uno::RuntimeException)
671 {
672     return rAny.hasValue() ? extractBoolFromAny( rAny ) : bDefault;
673 }
674 
extractStringFromAny(const uno::Any & rAny,bool bUppercaseBool)675 ::rtl::OUString extractStringFromAny( const uno::Any& rAny, bool bUppercaseBool ) throw (uno::RuntimeException)
676 {
677     switch( rAny.getValueType().getTypeClass() )
678     {
679         case uno::TypeClass_STRING:
680             return rAny.get< ::rtl::OUString >();
681         case uno::TypeClass_BOOLEAN:
682             return bUppercaseBool ?
683                 (rAny.get< bool >() ? ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "TRUE" ) ) : ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FALSE" ) )) :
684                 ::rtl::OUString::valueOf( (sal_Bool)rAny.get< bool >() );
685         case uno::TypeClass_FLOAT:
686             return ::rtl::OUString::valueOf( rAny.get< float >() );
687         case uno::TypeClass_DOUBLE:
688             return ::rtl::OUString::valueOf( rAny.get< double >() );
689         case uno::TypeClass_BYTE:
690         case uno::TypeClass_SHORT:
691         case uno::TypeClass_LONG:
692             return ::rtl::OUString::valueOf( rAny.get< sal_Int32 >() );
693         case uno::TypeClass_HYPER:
694             return ::rtl::OUString::valueOf( rAny.get< sal_Int64 >() );
695         default:;
696     }
697     throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Invalid type, cannot convert to string." ) ), 0 );
698 }
699 
extractStringFromAny(const uno::Any & rAny,const::rtl::OUString & rDefault,bool bUppercaseBool)700 ::rtl::OUString extractStringFromAny( const uno::Any& rAny, const ::rtl::OUString& rDefault, bool bUppercaseBool ) throw (uno::RuntimeException)
701 {
702     return rAny.hasValue() ? extractStringFromAny( rAny, bUppercaseBool ) : rDefault;
703 }
704 
getAnyAsString(const uno::Any & pvargItem)705 rtl::OUString getAnyAsString( const uno::Any& pvargItem ) throw ( uno::RuntimeException )
706 {
707     return extractStringFromAny( pvargItem );
708 }
709 
710 
711 rtl::OUString
getUniqueName(const uno::Sequence<::rtl::OUString> & _slist,const rtl::OUString & _sElementName,const::rtl::OUString & _sSuffixSeparator)712 ContainerUtilities::getUniqueName( const uno::Sequence< ::rtl::OUString >&  _slist, const rtl::OUString& _sElementName, const ::rtl::OUString& _sSuffixSeparator)
713 {
714 	return getUniqueName(_slist, _sElementName, _sSuffixSeparator, sal_Int32(2));
715 }
716 
717 rtl::OUString
getUniqueName(const uno::Sequence<rtl::OUString> & _slist,const rtl::OUString _sElementName,const rtl::OUString & _sSuffixSeparator,sal_Int32 _nStartSuffix)718 ContainerUtilities::getUniqueName( const uno::Sequence< rtl::OUString >& _slist, const rtl::OUString _sElementName, const rtl::OUString& _sSuffixSeparator, sal_Int32 _nStartSuffix)
719 {
720 	sal_Int32 a = _nStartSuffix;
721 	rtl::OUString scompname = _sElementName;
722 	bool bElementexists = true;
723 	sal_Int32 nLen = _slist.getLength();
724 	if ( nLen == 0 )
725 		return _sElementName;
726 
727 	while (bElementexists == true)
728 	{
729 		for (sal_Int32 i = 0; i < nLen; i++)
730 		{
731 			if (FieldInList(_slist, scompname) == -1)
732 			{
733 				return scompname;
734 			}
735 		}
736 		scompname = _sElementName + _sSuffixSeparator + rtl::OUString::valueOf( a++ );
737 	}
738 	return rtl::OUString();
739 }
740 
741 sal_Int32
FieldInList(const uno::Sequence<rtl::OUString> & SearchList,const rtl::OUString & SearchString)742 ContainerUtilities::FieldInList( const uno::Sequence< rtl::OUString >& SearchList, const rtl::OUString& SearchString )
743 {
744 	sal_Int32 FieldLen = SearchList.getLength();
745 	sal_Int32 retvalue = -1;
746 	for (sal_Int32 i = 0; i < FieldLen; i++)
747 	{
748 		// I wonder why comparing lexicographically is done
749 		// when its a match is whats interesting?
750 		//if (SearchList[i].compareTo(SearchString) == 0)
751 		if ( SearchList[i].equals( SearchString ) )
752 		{
753 			retvalue = i;
754 			break;
755 		}
756 	}
757 	return retvalue;
758 
759 }
NeedEsc(sal_Unicode cCode)760 bool NeedEsc(sal_Unicode cCode)
761 {
762 	String sEsc(RTL_CONSTASCII_USTRINGPARAM(".^$+\\|{}()"));
763 	return (STRING_NOTFOUND != sEsc.Search(cCode));
764 }
765 
VBAToRegexp(const rtl::OUString & rIn,bool bForLike)766 rtl::OUString VBAToRegexp(const rtl::OUString &rIn, bool bForLike )
767 {
768 	rtl::OUStringBuffer sResult;
769 	const sal_Unicode *start = rIn.getStr();
770 	const sal_Unicode *end = start + rIn.getLength();
771 
772 	int seenright = 0;
773 	if ( bForLike )
774 		sResult.append(static_cast<sal_Unicode>('^'));
775 
776 	while (start < end)
777 	{
778 		switch (*start)
779 		{
780 			case '?':
781 				sResult.append(static_cast<sal_Unicode>('.'));
782 				start++;
783 				break;
784 			case '*':
785 				sResult.append(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".*")));
786 				start++;
787 				break;
788 			case '#':
789 				sResult.append(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("[0-9]")));
790 				start++;
791 				break;
792 			case '~':
793 				sResult.append(static_cast<sal_Unicode>('\\'));
794 				sResult.append(*(++start));
795 				start++;
796 				break;
797 				// dump the ~ and escape the next characture
798 			case ']':
799 				sResult.append(static_cast<sal_Unicode>('\\'));
800 				sResult.append(*start++);
801 				break;
802 			case '[':
803 				sResult.append(*start++);
804 				seenright = 0;
805 				while (start < end && !seenright)
806 				{
807 					switch (*start)
808 					{
809 						case '[':
810 						case '?':
811 						case '*':
812 						sResult.append(static_cast<sal_Unicode>('\\'));
813 						sResult.append(*start);
814 							break;
815 						case ']':
816 						sResult.append(*start);
817 							seenright = 1;
818 							break;
819 						case '!':
820 							sResult.append(static_cast<sal_Unicode>('^'));
821 							break;
822 						default:
823 						if (NeedEsc(*start))
824 							sResult.append(static_cast<sal_Unicode>('\\'));
825 						sResult.append(*start);
826 							break;
827 					}
828 					start++;
829 				}
830 				break;
831 			default:
832 				if (NeedEsc(*start))
833 					sResult.append(static_cast<sal_Unicode>('\\'));
834 				sResult.append(*start++);
835 		}
836 	}
837 
838 	if ( bForLike )
839 		sResult.append(static_cast<sal_Unicode>('$'));
840 
841 	return sResult.makeStringAndClear( );
842 }
843 
getPixelTo100thMillimeterConversionFactor(const css::uno::Reference<css::awt::XDevice> & xDevice,sal_Bool bVertical)844 double getPixelTo100thMillimeterConversionFactor( const css::uno::Reference< css::awt::XDevice >& xDevice, sal_Bool bVertical)
845 {
846 	double fConvertFactor = 1.0;
847 	if( bVertical )
848 	{
849 		fConvertFactor = xDevice->getInfo().PixelPerMeterY/100000;
850 	}
851 	else
852 	{
853 		fConvertFactor = xDevice->getInfo().PixelPerMeterX/100000;
854 	}
855 	return fConvertFactor;
856 }
857 
PointsToPixels(const css::uno::Reference<css::awt::XDevice> & xDevice,double fPoints,sal_Bool bVertical)858 double PointsToPixels( const css::uno::Reference< css::awt::XDevice >& xDevice, double fPoints, sal_Bool bVertical)
859 {
860 	double fConvertFactor = getPixelTo100thMillimeterConversionFactor( xDevice, bVertical );
861 	return PointsToHmm( fPoints ) * fConvertFactor;
862 }
PixelsToPoints(const css::uno::Reference<css::awt::XDevice> & xDevice,double fPixels,sal_Bool bVertical)863 double PixelsToPoints( const css::uno::Reference< css::awt::XDevice >& xDevice, double fPixels, sal_Bool bVertical)
864 {
865 	double fConvertFactor = getPixelTo100thMillimeterConversionFactor( xDevice, bVertical );
866 	return HmmToPoints( fPixels/fConvertFactor );
867 }
868 
PointsToHmm(double fPoints)869 sal_Int32 PointsToHmm( double fPoints )
870 {
871     return static_cast<sal_Int32>( fPoints * factor + 0.5 );
872 }
873 
HmmToPoints(sal_Int32 nHmm)874 double HmmToPoints( sal_Int32 nHmm )
875 {
876     return nHmm / factor;
877 }
878 
ConcreteXShapeGeometryAttributes(const css::uno::Reference<css::uno::XComponentContext> &,const css::uno::Reference<css::drawing::XShape> & xShape)879 ConcreteXShapeGeometryAttributes::ConcreteXShapeGeometryAttributes( const css::uno::Reference< css::uno::XComponentContext >& /*xContext*/, const css::uno::Reference< css::drawing::XShape >& xShape )
880 {
881     m_pShapeHelper.reset( new ShapeHelper( xShape ) );
882 }
~ConcreteXShapeGeometryAttributes()883 ConcreteXShapeGeometryAttributes::~ConcreteXShapeGeometryAttributes()
884 {
885 }
886 
getPointerStyle(const uno::Reference<frame::XModel> & xModel)887 sal_Int32 getPointerStyle( const uno::Reference< frame::XModel >& xModel )
888 {
889 
890     sal_Int32 nPointerStyle( POINTER_ARROW );
891     try
892     {
893         const uno::Reference< frame::XController > xController( xModel->getCurrentController(),     uno::UNO_SET_THROW );
894         const uno::Reference< frame::XFrame >      xFrame     ( xController->getFrame(),        uno::UNO_SET_THROW );
895         const uno::Reference< awt::XWindow >       xWindow    ( xFrame->getContainerWindow(),   uno::UNO_SET_THROW );
896         // why the heck isn't there an XWindowPeer::getPointer, but a setPointer only?
897         const Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
898         if ( pWindow )
899             nPointerStyle = pWindow->GetSystemWindow()->GetPointer().GetStyle();
900     }
901     catch( const uno::Exception& )
902     {
903     	DBG_UNHANDLED_EXCEPTION();
904     }
905     return nPointerStyle;
906 }
907 
908 // #FIXME this method looks wrong, shouldn't it just affect calc *or* writer
909 // document/frame/window(s) but not both ( and depending on what api called
910 // this )
setCursorHelper(const uno::Reference<frame::XModel> & xModel,const Pointer & rPointer,sal_Bool bOverWrite)911 void setCursorHelper( const uno::Reference< frame::XModel >& xModel, const Pointer& rPointer, sal_Bool bOverWrite )
912 {
913     ::std::vector< uno::Reference< frame::XController > > aControllers;
914 
915     uno::Reference< frame::XModel2 > xModel2( xModel, uno::UNO_QUERY );
916     if ( xModel2.is() )
917     {
918         const uno::Reference< container::XEnumeration > xEnumControllers( xModel2->getControllers(), uno::UNO_SET_THROW );
919         while ( xEnumControllers->hasMoreElements() )
920         {
921             const uno::Reference< frame::XController > xController( xEnumControllers->nextElement(), uno::UNO_QUERY_THROW );
922             aControllers.push_back( xController );
923         }
924     }
925     else
926     {
927         if ( xModel.is() )
928         {
929             const uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
930             aControllers.push_back( xController );
931         }
932     }
933 
934     for (   ::std::vector< uno::Reference< frame::XController > >::const_iterator controller = aControllers.begin();
935             controller != aControllers.end();
936             ++controller
937         )
938     {
939         const uno::Reference< frame::XFrame >      xFrame     ( (*controller)->getFrame(),       uno::UNO_SET_THROW   );
940         const uno::Reference< awt::XWindow >       xWindow    ( xFrame->getContainerWindow(),    uno::UNO_SET_THROW   );
941 
942         Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
943         OSL_ENSURE( pWindow, "ScVbaApplication::setCursor: no window!" );
944         if ( !pWindow )
945             continue;
946 
947         pWindow->GetSystemWindow()->SetPointer( rPointer );
948         pWindow->GetSystemWindow()->EnableChildPointerOverwrite( bOverWrite );
949     }
950 }
951 
setDefaultPropByIntrospection(const uno::Any & aObj,const uno::Any & aValue)952 void setDefaultPropByIntrospection( const uno::Any& aObj, const uno::Any& aValue  ) throw ( uno::RuntimeException )
953 {
954     uno::Reference< beans::XIntrospectionAccess > xUnoAccess( getIntrospectionAccess( aObj ) );
955 
956     // #MAYBE #FIXME sort of a bit of a hack,
957     uno::Reference<  script::XDefaultProperty > xDflt( aObj, uno::UNO_QUERY_THROW );
958     uno::Reference< beans::XPropertySet > xPropSet;
959 
960     if ( xUnoAccess.is() )
961         xPropSet.set( xUnoAccess->queryAdapter( ::getCppuType( (const uno::Reference< beans::XPropertySet > *)0 ) ), uno::UNO_QUERY);
962 
963     if ( xPropSet.is() )
964         xPropSet->setPropertyValue( xDflt->getDefaultPropertyName(), aValue );
965     else
966         throw uno::RuntimeException();
967 }
968 
getPropertyValue(const uno::Sequence<beans::PropertyValue> & aProp,const rtl::OUString & aName)969 uno::Any getPropertyValue( const uno::Sequence< beans::PropertyValue >& aProp, const rtl::OUString& aName )
970 {
971     uno::Any result;
972     for ( sal_Int32 i = 0; i < aProp.getLength(); i++ )
973     {
974         if ( aProp[i].Name.equals(aName) )
975         {
976             aProp[i].Value >>= result;
977             return result;
978         }
979     }
980     return result;
981 }
982 
setPropertyValue(uno::Sequence<beans::PropertyValue> & aProp,const rtl::OUString & aName,const uno::Any & aValue)983 sal_Bool setPropertyValue( uno::Sequence< beans::PropertyValue >& aProp, const rtl::OUString& aName, const uno::Any& aValue )
984 {
985     for ( sal_Int32 i = 0; i < aProp.getLength(); i++ )
986     {
987         if ( aProp[i].Name.equals(aName) )
988         {
989             aProp[i].Value = aValue;
990             return sal_True;
991         }
992     }
993     return sal_False;
994 }
995 
996 // ====UserFormGeomentryHelper====
997 //---------------------------------------------
UserFormGeometryHelper(const uno::Reference<uno::XComponentContext> &,const uno::Reference<awt::XControl> & xControl,double fOffsetX,double fOffsetY)998 UserFormGeometryHelper::UserFormGeometryHelper(
999         const uno::Reference< uno::XComponentContext >& /*xContext*/,
1000         const uno::Reference< awt::XControl >& xControl,
1001         double fOffsetX, double fOffsetY ) :
1002     mfOffsetX( fOffsetX ),
1003     mfOffsetY( fOffsetY ),
1004     mbDialog( uno::Reference< awt::XDialog >( xControl, uno::UNO_QUERY ).is() )
1005 {
1006     if ( !xControl.is() )
1007         throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "No control is provided!" ) ),
1008                                      uno::Reference< uno::XInterface >() );
1009 
1010     mxWindow.set( xControl->getPeer(), uno::UNO_QUERY_THROW );
1011     mxModelProps.set( xControl->getModel(), uno::UNO_QUERY_THROW );
1012     mxUnitConv.set( mxWindow, uno::UNO_QUERY_THROW );
1013 }
1014 
getLeft() const1015 double UserFormGeometryHelper::getLeft() const
1016 {
1017     return implGetPos( false );
1018 }
1019 
setLeft(double fLeft)1020 void UserFormGeometryHelper::setLeft( double fLeft )
1021 {
1022     implSetPos( fLeft, false );
1023 }
1024 
getTop() const1025 double UserFormGeometryHelper::getTop() const
1026 {
1027     return implGetPos( true );
1028 }
1029 
setTop(double fTop)1030 void UserFormGeometryHelper::setTop( double fTop )
1031 {
1032     implSetPos( fTop, true );
1033 }
1034 
getWidth() const1035 double UserFormGeometryHelper::getWidth() const
1036 {
1037     return implGetSize( false, true );
1038 }
1039 
setWidth(double fWidth)1040 void UserFormGeometryHelper::setWidth( double fWidth )
1041 {
1042     implSetSize( fWidth, false, true );
1043 }
1044 
getHeight() const1045 double UserFormGeometryHelper::getHeight() const
1046 {
1047     return implGetSize( true, true );
1048 }
1049 
setHeight(double fHeight)1050 void UserFormGeometryHelper::setHeight( double fHeight )
1051 {
1052     implSetSize( fHeight, true, true );
1053 }
1054 
getInnerWidth() const1055 double UserFormGeometryHelper::getInnerWidth() const
1056 {
1057     return implGetSize( false, false );
1058 }
1059 
setInnerWidth(double fWidth)1060 void UserFormGeometryHelper::setInnerWidth( double fWidth )
1061 {
1062     implSetSize( fWidth, false, false );
1063 }
1064 
getInnerHeight() const1065 double UserFormGeometryHelper::getInnerHeight() const
1066 {
1067     return implGetSize( true, false );
1068 }
1069 
setInnerHeight(double fHeight)1070 void UserFormGeometryHelper::setInnerHeight( double fHeight )
1071 {
1072     implSetSize( fHeight, true, false );
1073 }
1074 
getOffsetX() const1075 double UserFormGeometryHelper::getOffsetX() const
1076 {
1077     return mfOffsetX;
1078 }
1079 
getOffsetY() const1080 double UserFormGeometryHelper::getOffsetY() const
1081 {
1082     return mfOffsetY;
1083 }
1084 
1085 // ----------------------------------------------------------------------------
1086 
1087 static const ::rtl::OUString saPosXName( RTL_CONSTASCII_USTRINGPARAM( "PositionX" ) );
1088 static const ::rtl::OUString saPosYName( RTL_CONSTASCII_USTRINGPARAM( "PositionY" ) );
1089 static const ::rtl::OUString saWidthName( RTL_CONSTASCII_USTRINGPARAM( "Width" ) );
1090 static const ::rtl::OUString saHeightName( RTL_CONSTASCII_USTRINGPARAM( "Height" ) );
1091 
implGetPos(bool bPosY) const1092 double UserFormGeometryHelper::implGetPos( bool bPosY ) const
1093 {
1094     sal_Int32 nPosAppFont = mxModelProps->getPropertyValue( bPosY ? saPosYName : saPosXName ).get< sal_Int32 >();
1095     // appfont to pixel
1096     awt::Point aPosPixel = mxUnitConv->convertPointToPixel( awt::Point( nPosAppFont, nPosAppFont ), util::MeasureUnit::APPFONT );
1097     // pixel to VBA points
1098     awt::Point aPosPoint = mxUnitConv->convertPointToLogic( aPosPixel, util::MeasureUnit::POINT );
1099     return bPosY ? (aPosPoint.Y - mfOffsetY) : (aPosPoint.X - mfOffsetX);
1100 }
1101 
implSetPos(double fPos,bool bPosY)1102 void UserFormGeometryHelper::implSetPos( double fPos, bool bPosY )
1103 {
1104     // convert passed VBA points to pixels
1105     sal_Int32 nPosPixel = static_cast< sal_Int32 >( fPos + (bPosY ? mfOffsetY : mfOffsetX) );
1106     awt::Point aPosPixel = mxUnitConv->convertPointToPixel( awt::Point( nPosPixel, nPosPixel ), util::MeasureUnit::POINT );
1107     // pixel to appfont
1108     awt::Point aPosAppFont = mxUnitConv->convertPointToLogic( aPosPixel, util::MeasureUnit::APPFONT );
1109     mxModelProps->setPropertyValue( bPosY ? saPosYName : saPosXName, uno::Any( bPosY ? aPosAppFont.Y : aPosAppFont.X ) );
1110 }
1111 
implGetSize(bool bHeight,bool bOuter) const1112 double UserFormGeometryHelper::implGetSize( bool bHeight, bool bOuter ) const
1113 {
1114     sal_Int32 nSizeAppFont = mxModelProps->getPropertyValue( bHeight ? saHeightName : saWidthName ).get< sal_Int32 >();
1115     // appfont to pixel
1116     awt::Size aSizePixel = mxUnitConv->convertSizeToPixel( awt::Size( nSizeAppFont, nSizeAppFont ), util::MeasureUnit::APPFONT );
1117 
1118     /*  The VBA symbols 'Width' and 'Height' return the outer size including
1119         window decoration (in difference to the symbols 'InnerWidth' and
1120         'InnerHeight'), but the window API returns the inner size. */
1121     if( mbDialog && bOuter )
1122     {
1123         if( const Window* pWindow = VCLUnoHelper::GetWindow( mxWindow ) )
1124         {
1125             Rectangle aOuterRect = pWindow->GetWindowExtentsRelative( NULL );
1126             aSizePixel = awt::Size( aOuterRect.getWidth(), aOuterRect.getHeight() );
1127         }
1128     }
1129 
1130     // pixel to VBA points
1131     awt::Size aSizePoint = mxUnitConv->convertSizeToLogic( aSizePixel, util::MeasureUnit::POINT );
1132     return bHeight ? aSizePoint.Height : aSizePoint.Width;
1133 }
1134 
implSetSize(double fSize,bool bHeight,bool bOuter)1135 void UserFormGeometryHelper::implSetSize( double fSize, bool bHeight, bool bOuter )
1136 {
1137     // convert passed VBA points to pixels
1138     sal_Int32 nSize = static_cast< sal_Int32 >( fSize );
1139     awt::Size aSizePixel = mxUnitConv->convertSizeToPixel( awt::Size( nSize, nSize ), util::MeasureUnit::POINT );
1140 
1141     /*  The VBA symbols 'Width' and 'Height' set the outer size (in difference
1142         to the symbols 'InnerWidth' and 'InnerHeight'), but the dialog model
1143         expects the inner size. We have to remove the window extents from the
1144         pixel height to get the same result. */
1145     if ( mbDialog && bOuter )
1146     {
1147         if( const Window* pWindow = VCLUnoHelper::GetWindow( mxWindow ) )
1148         {
1149             Rectangle aOuterRect = pWindow->GetWindowExtentsRelative( NULL );
1150             if( !aOuterRect.IsEmpty() )
1151             {
1152                 awt::Rectangle aInnerRect = mxWindow->getPosSize();
1153                 sal_Int32 nDecorWidth = aOuterRect.getWidth() - aInnerRect.Width;
1154                 sal_Int32 nDecorHeight = aOuterRect.getHeight() - aInnerRect.Height;
1155                 aSizePixel.Width = ::std::max< sal_Int32 >( aSizePixel.Width - nDecorWidth, 1 );
1156                 aSizePixel.Height = ::std::max< sal_Int32 >( aSizePixel.Height - nDecorHeight, 1 );
1157             }
1158         }
1159     }
1160 
1161     awt::Size aSizeAppFont = mxUnitConv->convertSizeToLogic( aSizePixel, util::MeasureUnit::APPFONT );
1162     mxModelProps->setPropertyValue( bHeight ? saHeightName : saWidthName, uno::Any( bHeight ? aSizeAppFont.Height : aSizeAppFont.Width ) );
1163 }
1164 
1165 // ============================================================================
1166 
getLeft() const1167     double ConcreteXShapeGeometryAttributes::getLeft() const
1168     {
1169         return m_pShapeHelper->getLeft();
1170     }
setLeft(double nLeft)1171     void ConcreteXShapeGeometryAttributes::setLeft( double nLeft )
1172     {
1173         m_pShapeHelper->setLeft( nLeft );
1174     }
getTop() const1175     double ConcreteXShapeGeometryAttributes::getTop() const
1176     {
1177         return m_pShapeHelper->getTop();
1178     }
setTop(double nTop)1179     void ConcreteXShapeGeometryAttributes::setTop( double nTop )
1180     {
1181         m_pShapeHelper->setTop( nTop );
1182     }
1183 
getHeight() const1184     double ConcreteXShapeGeometryAttributes::getHeight() const
1185     {
1186         return m_pShapeHelper->getHeight();
1187     }
setHeight(double nHeight)1188     void ConcreteXShapeGeometryAttributes::setHeight( double nHeight )
1189     {
1190         m_pShapeHelper->setHeight( nHeight );
1191     }
getWidth() const1192     double ConcreteXShapeGeometryAttributes::getWidth() const
1193     {
1194         return m_pShapeHelper->getWidth();
1195     }
setWidth(double nWidth)1196     void ConcreteXShapeGeometryAttributes::setWidth( double nWidth)
1197     {
1198         m_pShapeHelper->setWidth( nWidth );
1199     }
1200 
1201 
ShapeHelper(const css::uno::Reference<css::drawing::XShape> & _xShape)1202 	ShapeHelper::ShapeHelper( const css::uno::Reference< css::drawing::XShape >& _xShape) throw (css::script::BasicErrorException ) : xShape( _xShape )
1203 	{
1204 		if( !xShape.is() )
1205 			throw css::uno::RuntimeException( rtl::OUString::createFromAscii("No valid shape for helper"), css::uno::Reference< css::uno::XInterface >() );
1206 	}
1207 
getHeight() const1208 	double ShapeHelper::getHeight() const
1209 	{
1210         	return  Millimeter::getInPoints(xShape->getSize().Height);
1211     	}
1212 
1213 
setHeight(double _fheight)1214     	void ShapeHelper::setHeight(double _fheight) throw ( css::script::BasicErrorException )
1215 	{
1216 		try
1217 		{
1218 			css::awt::Size aSize = xShape->getSize();
1219 			aSize.Height = Millimeter::getInHundredthsOfOneMillimeter(_fheight);
1220 			xShape->setSize(aSize);
1221 		}
1222 		catch ( css::uno::Exception& /*e*/)
1223 		{
1224 			throw css::script::BasicErrorException( rtl::OUString(), css::uno::Reference< css::uno::XInterface >(), SbERR_METHOD_FAILED, rtl::OUString() );
1225     		}
1226 	}
1227 
1228 
getWidth() const1229 	double ShapeHelper::getWidth() const
1230 	{
1231 		return Millimeter::getInPoints(xShape->getSize().Width);
1232     	}
1233 
setWidth(double _fWidth)1234 	void ShapeHelper::setWidth(double _fWidth) throw ( css::script::BasicErrorException )
1235 	{
1236 		try
1237 		{
1238 			css::awt::Size aSize = xShape->getSize();
1239 			aSize.Width = Millimeter::getInHundredthsOfOneMillimeter(_fWidth);
1240 			xShape->setSize(aSize);
1241 		}
1242 		catch (css::uno::Exception& /*e*/)
1243 		{
1244 			throw css::script::BasicErrorException( rtl::OUString(), css::uno::Reference< css::uno::XInterface >(), SbERR_METHOD_FAILED, rtl::OUString() );
1245 		}
1246 	}
1247 
1248 
getLeft() const1249 	double ShapeHelper::getLeft() const
1250 	{
1251 		return Millimeter::getInPoints(xShape->getPosition().X);
1252 	}
1253 
1254 
setLeft(double _fLeft)1255 	void ShapeHelper::setLeft(double _fLeft)
1256 	{
1257 		css::awt::Point aPoint = xShape->getPosition();
1258 		aPoint.X = Millimeter::getInHundredthsOfOneMillimeter(_fLeft);
1259 		xShape->setPosition(aPoint);
1260 	}
1261 
1262 
getTop() const1263 	double ShapeHelper::getTop() const
1264 	{
1265         	return Millimeter::getInPoints(xShape->getPosition().Y);
1266 	}
1267 
1268 
setTop(double _fTop)1269 	void ShapeHelper::setTop(double _fTop)
1270 	{
1271 		css::awt::Point aPoint = xShape->getPosition();
1272 		aPoint.Y = Millimeter::getInHundredthsOfOneMillimeter(_fTop);
1273 		xShape->setPosition(aPoint);
1274 	}
1275 
exception(const rtl::OUString & DetailedMessage,const css::uno::Exception & ex,int err,const rtl::OUString &)1276 	void DebugHelper::exception( const rtl::OUString&  DetailedMessage, const css::uno::Exception& ex,  int err, const rtl::OUString& /*additionalArgument*/ ) throw( css::script::BasicErrorException )
1277 	{
1278 		// #TODO #FIXME ( do we want to support additionalArg here )
1279 		throw css::script::BasicErrorException( DetailedMessage.concat( rtl::OUString::createFromAscii(" ") ).concat( ex.Message ), css::uno::Reference< css::uno::XInterface >(), err, rtl::OUString() );
1280 	}
1281 
exception(int err,const rtl::OUString & additionalArgument)1282 	void DebugHelper::exception( int err,  const rtl::OUString& additionalArgument ) throw( css::script::BasicErrorException )
1283 	{
1284 		exception( rtl::OUString(), css::uno::Exception(), err, additionalArgument );
1285 	}
exception(css::uno::Exception & ex)1286 	void DebugHelper::exception( css::uno::Exception& ex ) throw( css::script::BasicErrorException )
1287 	{
1288 		exception( rtl::OUString(), ex, SbERR_INTERNAL_ERROR, rtl::OUString() );
1289 	}
1290 
Millimeter()1291 	Millimeter::Millimeter():m_nMillimeter(0) {}
1292 
Millimeter(double mm)1293 	Millimeter::Millimeter(double mm):m_nMillimeter(mm) {}
1294 
set(double mm)1295 	void Millimeter::set(double mm) { m_nMillimeter = mm; }
setInPoints(double points)1296 	void Millimeter::setInPoints(double points)
1297 	{
1298 		m_nMillimeter = points * factor / 100.0;
1299 	}
1300 
setInHundredthsOfOneMillimeter(double hmm)1301 	void Millimeter::setInHundredthsOfOneMillimeter(double hmm)
1302 	{
1303 		m_nMillimeter = hmm / 100;
1304 	}
1305 
get()1306 	double Millimeter::get()
1307 	{
1308 		return m_nMillimeter;
1309 	}
getInHundredthsOfOneMillimeter()1310 	double Millimeter::getInHundredthsOfOneMillimeter()
1311 	{
1312 		return m_nMillimeter * 100;
1313 	}
getInPoints()1314 	double Millimeter::getInPoints()
1315 	{
1316 		return m_nMillimeter / factor * 100.0;
1317 	}
1318 
getInHundredthsOfOneMillimeter(double points)1319 	sal_Int32 Millimeter::getInHundredthsOfOneMillimeter(double points)
1320 	{
1321 		sal_Int32 mm = static_cast<sal_Int32>(points * factor);
1322 		return mm;
1323 	}
1324 
getInPoints(int _hmm)1325 	double Millimeter::getInPoints(int _hmm)
1326 	{
1327 		double points = double( static_cast<double>(_hmm) / factor);
1328 		return points;
1329 	}
1330 
getVBADocument(const uno::Reference<frame::XModel> & xModel)1331 uno::Reference< XHelperInterface > getVBADocument( const uno::Reference< frame::XModel >& xModel )
1332 {
1333     uno::Reference< XHelperInterface > xIf;
1334     try
1335     {
1336         uno::Reference< beans::XPropertySet > xDocProps( xModel, uno::UNO_QUERY_THROW );
1337         ::rtl::OUString aCodeName;
1338         xDocProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CodeName" ) ) ) >>= aCodeName;
1339         xIf = getUnoDocModule( aCodeName, getSfxObjShell( xModel ) );
1340     }
1341     catch( uno::Exception& )
1342     {
1343     }
1344     return xIf;
1345 }
1346 
getUnoDocModule(const String & aModName,SfxObjectShell * pShell)1347 uno::Reference< XHelperInterface > getUnoDocModule( const String& aModName, SfxObjectShell* pShell )
1348 {
1349     uno::Reference< XHelperInterface > xIf;
1350     if ( pShell )
1351     {
1352         rtl::OUString sProj( RTL_CONSTASCII_USTRINGPARAM("Standard") );
1353         BasicManager* pBasMgr = pShell->GetBasicManager();
1354         if ( pBasMgr && pBasMgr->GetName().Len() )
1355             sProj = pBasMgr->GetName();
1356         if( StarBASIC* pBasic = pShell->GetBasicManager()->GetLib( sProj ) )
1357             if( SbModule* pMod = pBasic->FindModule( aModName ) )
1358                 xIf.set( pMod->GetUnoModule(), uno::UNO_QUERY );
1359     }
1360     return xIf;
1361 }
1362 
getSfxObjShell(const uno::Reference<frame::XModel> & xModel)1363 SfxObjectShell* getSfxObjShell( const uno::Reference< frame::XModel >& xModel ) throw (uno::RuntimeException)
1364 {
1365     SfxObjectShell* pFoundShell = NULL;
1366     if ( xModel.is() )
1367     {
1368         uno::Reference< lang::XUnoTunnel >  xObjShellTunnel( xModel, uno::UNO_QUERY_THROW );
1369         pFoundShell = reinterpret_cast<SfxObjectShell*>( xObjShellTunnel->getSomething(SfxObjectShell::getUnoTunnelId()));
1370     }
1371     if ( !pFoundShell )
1372         throw uno::RuntimeException();
1373     return pFoundShell;
1374 }
1375 
1376 } // openoffice
1377 } //org
1378