xref: /aoo42x/main/sc/source/ui/vba/vbawindow.cxx (revision 86e1cf34)
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 <vbahelper/helperdecl.hxx>
24 #include "vbawindow.hxx"
25 #include "vbaworksheets.hxx"
26 #include "vbaworksheet.hxx"
27 #include "vbaglobals.hxx"
28 #include "vbapane.hxx"
29 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
30 #include <com/sun/star/sheet/XSpreadsheet.hpp>
31 #include <com/sun/star/container/XNamed.hpp>
32 #include <com/sun/star/view/DocumentZoomType.hpp>
33 #include <com/sun/star/table/CellRangeAddress.hpp>
34 #include <ooo/vba/excel/XlWindowState.hpp>
35 #include <ooo/vba/excel/XlWindowView.hpp>
36 #include <ooo/vba/excel/Constants.hpp>
37 #include <com/sun/star/awt/XWindow.hpp>
38 #include <com/sun/star/awt/XWindow2.hpp>
39 #include <com/sun/star/awt/PosSize.hpp>
40 
41 #include <docsh.hxx>
42 #include <tabvwsh.hxx>
43 #include <docuno.hxx>
44 #include <sc.hrc>
45 #include <hash_map>
46 #include <sfx2/viewfrm.hxx>
47 #include <vcl/wrkwin.hxx>
48 #include "unonames.hxx"
49 
50 using namespace ::com::sun::star;
51 using namespace ::ooo::vba;
52 using namespace ::ooo::vba::excel::XlWindowState;
53 
54 typedef  std::hash_map< rtl::OUString,
55 SCTAB, ::rtl::OUStringHash,
56 ::std::equal_to< ::rtl::OUString > > NameIndexHash;
57 
58 typedef std::vector< uno::Reference< sheet::XSpreadsheet > > Sheets;
59 
60 typedef ::cppu::WeakImplHelper1< container::XEnumeration > Enumeration_BASE;
61 
62 typedef ::cppu::WeakImplHelper3< container::XEnumerationAccess
63 	, com::sun::star::container::XIndexAccess
64 	, com::sun::star::container::XNameAccess
65 	> SelectedSheets_BASE;
66 
67 
68 class SelectedSheetsEnum : public Enumeration_BASE
69 {
70 public:
71 	uno::Reference< uno::XComponentContext > m_xContext;
72 	Sheets m_sheets;
73 	uno::Reference< frame::XModel > m_xModel;
74 	Sheets::const_iterator m_it;
75 
SelectedSheetsEnum(const uno::Reference<uno::XComponentContext> & xContext,const Sheets & sheets,const uno::Reference<frame::XModel> & xModel)76 	SelectedSheetsEnum( const uno::Reference< uno::XComponentContext >& xContext, const Sheets& sheets, const uno::Reference< frame::XModel >& xModel ) throw ( uno::RuntimeException ) :  m_xContext( xContext ), m_sheets( sheets ), m_xModel( xModel )
77 	{
78 		m_it = m_sheets.begin();
79 	}
80 	// XEnumeration
hasMoreElements()81 	virtual ::sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException)
82 	{
83 		return m_it != m_sheets.end();
84 	}
nextElement()85 	virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
86 	{
87 		if ( !hasMoreElements() )
88 		{
89 			throw container::NoSuchElementException();
90 		}
91 		// #FIXME needs ThisWorkbook as parent
92 		return uno::makeAny( uno::Reference< excel::XWorksheet > ( new ScVbaWorksheet( uno::Reference< XHelperInterface >(), m_xContext, *(m_it++), m_xModel ) ) );
93 	}
94 
95 
96 };
97 
98 class SelectedSheetsEnumAccess : public SelectedSheets_BASE
99 {
100 	uno::Reference< uno::XComponentContext > m_xContext;
101 	NameIndexHash namesToIndices;
102 	Sheets sheets;
103 	uno::Reference< frame::XModel > m_xModel;
104 public:
SelectedSheetsEnumAccess(const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<frame::XModel> & xModel)105 	SelectedSheetsEnumAccess( const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xModel ):m_xContext( xContext ), m_xModel( xModel )
106 	{
107 		ScModelObj* pModel = static_cast< ScModelObj* >( m_xModel.get() );
108 		if ( !pModel )
109 			throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Cannot obtain current document" ) ), uno::Reference< uno::XInterface >() );
110 		ScDocShell* pDocShell = (ScDocShell*)pModel->GetEmbeddedObject();
111 		if ( !pDocShell )
112 			throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Cannot obtain docshell" ) ), uno::Reference< uno::XInterface >() );
113 		ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );
114 		if ( !pViewShell )
115 			throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Cannot obtain view shell" ) ), uno::Reference< uno::XInterface >() );
116 
117 		SCTAB nTabCount = pDocShell->GetDocument()->GetTableCount();
118 		uno::Sequence<sal_Int32> aSheets( nTabCount );
119 		SCTAB nIndex = 0;
120 		const ScMarkData& rMarkData = pViewShell->GetViewData()->GetMarkData();
121 		sheets.reserve( nTabCount );
122 		uno::Reference <sheet::XSpreadsheetDocument> xSpreadSheet( m_xModel, uno::UNO_QUERY_THROW );
123 		uno::Reference <container::XIndexAccess> xIndex( xSpreadSheet->getSheets(), uno::UNO_QUERY_THROW );
124 		for ( SCTAB nTab=0; nTab<nTabCount; nTab++ )
125 		{
126 			if ( rMarkData.GetTableSelect(nTab) )
127 			{
128 				uno::Reference< sheet::XSpreadsheet > xSheet( xIndex->getByIndex( nTab ), uno::UNO_QUERY_THROW );
129 				uno::Reference< container::XNamed > xNamed( xSheet, uno::UNO_QUERY_THROW );
130 				sheets.push_back( xSheet );
131 				namesToIndices[ xNamed->getName() ] = nIndex++;
132 			}
133 		}
134 
135 	}
136 
137 	//XEnumerationAccess
createEnumeration()138 	virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration(  ) throw (uno::RuntimeException)
139 	{
140 		return new SelectedSheetsEnum( m_xContext, sheets, m_xModel  );
141 	}
142 	// XIndexAccess
getCount()143 	virtual ::sal_Int32 SAL_CALL getCount(  ) throw (uno::RuntimeException)
144 	{
145 		return sheets.size();
146 	}
getByIndex(::sal_Int32 Index)147 	virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw ( lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
148 	{
149 		if ( Index < 0
150 		|| static_cast< Sheets::size_type >( Index ) >= sheets.size() )
151 			throw lang::IndexOutOfBoundsException();
152 
153 		return uno::makeAny( sheets[ Index ] );
154 	}
155 
156 	//XElementAccess
getElementType()157 	virtual uno::Type SAL_CALL getElementType(  ) throw (uno::RuntimeException)
158 	{
159 		return excel::XWorksheet::static_type(0);
160 	}
161 
hasElements()162 	virtual ::sal_Bool SAL_CALL hasElements(  ) throw (uno::RuntimeException)
163 	{
164 		return (sheets.size() > 0);
165 	}
166 
167 	//XNameAccess
getByName(const::rtl::OUString & aName)168 	virtual uno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
169 	{
170 		NameIndexHash::const_iterator it = namesToIndices.find( aName );
171 		if ( it == namesToIndices.end() )
172 			throw container::NoSuchElementException();
173 		return uno::makeAny( sheets[ it->second ] );
174 
175 	}
176 
getElementNames()177 	virtual uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames(  ) throw (uno::RuntimeException)
178 	{
179 		uno::Sequence< ::rtl::OUString > names( namesToIndices.size() );
180 		::rtl::OUString* pString = names.getArray();
181 		NameIndexHash::const_iterator it = namesToIndices.begin();
182 		NameIndexHash::const_iterator it_end = namesToIndices.end();
183 		for ( ; it != it_end; ++it, ++pString )
184 			*pString = it->first;
185 		return names;
186 	}
187 
hasByName(const::rtl::OUString & aName)188 	virtual ::sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw (uno::RuntimeException)
189 	{
190 		NameIndexHash::const_iterator it = namesToIndices.find( aName );
191 		return (it != namesToIndices.end());
192 	}
193 
194 
195 };
196 
ScVbaWindow(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<frame::XModel> & xModel,const uno::Reference<frame::XController> & xController)197 ScVbaWindow::ScVbaWindow(
198         const uno::Reference< XHelperInterface >& xParent,
199         const uno::Reference< uno::XComponentContext >& xContext,
200         const uno::Reference< frame::XModel >& xModel,
201         const uno::Reference< frame::XController >& xController ) throw (uno::RuntimeException) :
202     WindowImpl_BASE( xParent, xContext, xModel, xController )
203 {
204     init();
205 }
206 
ScVbaWindow(const uno::Sequence<uno::Any> & args,const uno::Reference<uno::XComponentContext> & xContext)207 ScVbaWindow::ScVbaWindow(
208         const uno::Sequence< uno::Any >& args,
209         const uno::Reference< uno::XComponentContext >& xContext ) throw (uno::RuntimeException) :
210     WindowImpl_BASE( args, xContext )
211 {
212     init();
213 }
214 
215 void
init()216 ScVbaWindow::init()
217 {
218     /*  This method is called from the constructor, thus the own refcount is
219         still zero. The implementation of ActivePane() uses a UNO reference of
220         this (to set this window as parent of the pane obejct). This requires
221         the own refcount to be non-zero, otherwise this instance will be
222         desctructed immediately! Guard the call to ActivePane() in try/catch to
223         not miss the decrementation of the reference count on exception. */
224     osl_incrementInterlockedCount( &m_refCount );
225     try
226     {
227 	   m_xPane = ActivePane();
228 	}
229     catch( uno::Exception& )
230     {
231     }
232     osl_decrementInterlockedCount( &m_refCount );
233 }
234 
235 uno::Reference< beans::XPropertySet >
getControllerProps()236 ScVbaWindow::getControllerProps() throw (uno::RuntimeException)
237 {
238 	return uno::Reference< beans::XPropertySet >( getController(), uno::UNO_QUERY_THROW );
239 }
240 
241 uno::Reference< beans::XPropertySet >
getFrameProps()242 ScVbaWindow::getFrameProps() throw (uno::RuntimeException)
243 {
244 	return uno::Reference< beans::XPropertySet >( getController()->getFrame(), uno::UNO_QUERY_THROW );
245 }
246 
247 uno::Reference< awt::XDevice >
getDevice()248 ScVbaWindow::getDevice() throw (uno::RuntimeException)
249 {
250     return uno::Reference< awt::XDevice >( getWindow(), uno::UNO_QUERY_THROW );
251 }
252 
253 void
Scroll(const uno::Any & Down,const uno::Any & Up,const uno::Any & ToRight,const uno::Any & ToLeft,bool bLargeScroll)254 ScVbaWindow::Scroll( const uno::Any& Down, const uno::Any& Up, const uno::Any& ToRight, const uno::Any& ToLeft, bool bLargeScroll ) throw (uno::RuntimeException)
255 {
256     if( !m_xPane.is() )
257         throw uno::RuntimeException();
258 	if( bLargeScroll )
259 		m_xPane->LargeScroll( Down, Up, ToRight, ToLeft );
260 	else
261 		m_xPane->SmallScroll( Down, Up, ToRight, ToLeft );
262 }
263 
264 void SAL_CALL
SmallScroll(const uno::Any & Down,const uno::Any & Up,const uno::Any & ToRight,const uno::Any & ToLeft)265 ScVbaWindow::SmallScroll( const uno::Any& Down, const uno::Any& Up, const uno::Any& ToRight, const uno::Any& ToLeft ) throw (uno::RuntimeException)
266 {
267 	Scroll( Down, Up, ToRight, ToLeft );
268 }
269 
270 void SAL_CALL
LargeScroll(const uno::Any & Down,const uno::Any & Up,const uno::Any & ToRight,const uno::Any & ToLeft)271 ScVbaWindow::LargeScroll( const uno::Any& Down, const uno::Any& Up, const uno::Any& ToRight, const uno::Any& ToLeft ) throw (uno::RuntimeException)
272 {
273 	Scroll( Down, Up, ToRight, ToLeft, true );
274 }
275 
276 uno::Any SAL_CALL
SelectedSheets(const uno::Any & aIndex)277 ScVbaWindow::SelectedSheets( const uno::Any& aIndex ) throw (uno::RuntimeException)
278 {
279 	uno::Reference< container::XEnumerationAccess > xEnumAccess( new SelectedSheetsEnumAccess( mxContext, m_xModel ) );
280 	// #FIXME needs a workbook as a parent
281 	uno::Reference< excel::XWorksheets > xSheets(  new ScVbaWorksheets( uno::Reference< XHelperInterface >(), mxContext, xEnumAccess, m_xModel ) );
282 	if ( aIndex.hasValue() )
283 	{
284 		uno::Reference< XCollection > xColl( xSheets, uno::UNO_QUERY_THROW );
285 		return xColl->Item( aIndex, uno::Any() );
286 	}
287 	return uno::makeAny( xSheets );
288 }
289 
290 void SAL_CALL
ScrollWorkbookTabs(const uno::Any &,const uno::Any &)291 ScVbaWindow::ScrollWorkbookTabs( const uno::Any& /*Sheets*/, const uno::Any& /*Position*/ ) throw (uno::RuntimeException)
292 {
293 // #TODO #FIXME need some implementation to scroll through the tabs
294 // but where is this done?
295 /*
296 	sal_Int32 nSheets = 0;
297 	sal_Int32 nPosition = 0;
298 	throw uno::RuntimeException( rtl::OUString::createFromAscii("No Implemented" ), uno::Reference< uno::XInterface >() );
299 	sal_Bool bSheets = ( Sheets >>= nSheets );
300 	sal_Bool bPosition = ( Position >>= nPosition );
301 	if ( bSheets || bPosition ) // at least one param specified
302 		if ( bSheets )
303 			;// use sheets
304 		else if ( bPosition )
305 			; //use position
306 */
307 
308 }
309 
310 uno::Any SAL_CALL
getCaption()311 ScVbaWindow::getCaption() throw (uno::RuntimeException)
312 {
313 	static rtl::OUString sCrud(RTL_CONSTASCII_USTRINGPARAM(" - OpenOffice Calc" ) );
314 	static sal_Int32 nCrudLen = sCrud.getLength();
315 
316 	rtl::OUString sTitle;
317 	getFrameProps()->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( SC_UNONAME_TITLE ) ) ) >>= sTitle;
318 	sal_Int32 nCrudIndex = sTitle.indexOf( sCrud );
319 	// adjust title ( by removing crud )
320 	// sCrud string present
321 	if ( nCrudIndex != -1 )
322 	{
323 		// and ends with sCrud
324 		if ( ( nCrudLen + nCrudIndex ) == sTitle.getLength() )
325 		{
326 			sTitle = sTitle.copy( 0, nCrudIndex );
327 			ScVbaWorkbook workbook( uno::Reference< XHelperInterface >( Application(), uno::UNO_QUERY_THROW ), mxContext, m_xModel );
328 			rtl::OUString sName = workbook.getName();
329 			// rather bizare hack to make sure the name behavior
330 			// is like XL
331 			// if the adjusted title == workbook name, use name
332 			// if the adjusted title != workbook name but ...
333 			// 	name == title + extension ( .csv, ,odt, .xls )
334 			//	etc. then also use the name
335 
336 			if ( !sTitle.equals( sName ) )
337 			{
338 				static rtl::OUString sDot( RTL_CONSTASCII_USTRINGPARAM(".") );
339 				// starts with title
340 				if ( sName.indexOf( sTitle ) == 0 )
341 					// extension starts immediately after
342 					if ( sName.match( sDot, sTitle.getLength() ) )
343 						sTitle = sName;
344 			}
345 		}
346 	}
347 	return uno::makeAny( sTitle );
348 }
349 
350 void SAL_CALL
setCaption(const uno::Any & _caption)351 ScVbaWindow::setCaption( const uno::Any& _caption ) throw (uno::RuntimeException)
352 {
353 	getFrameProps()->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_TITLE ) ), _caption );
354 }
355 
356 uno::Any SAL_CALL
getScrollRow()357 ScVbaWindow::getScrollRow() throw (uno::RuntimeException)
358 {
359     sal_Int32 nValue = 0;
360     // !! TODO !! get view shell from controller
361     ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );
362 	if ( pViewShell )
363 	{
364 	    ScSplitPos eWhich = pViewShell->GetViewData()->GetActivePart();
365 	    nValue = pViewShell->GetViewData()->GetPosY(WhichV(eWhich));
366 	}
367 
368     return uno::makeAny( nValue + 1);
369 }
370 
371 void SAL_CALL
setScrollRow(const uno::Any & _scrollrow)372 ScVbaWindow::setScrollRow( const uno::Any& _scrollrow ) throw (uno::RuntimeException)
373 {
374     // !! TODO !! get view shell from controller
375 	ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );
376 	if ( pViewShell )
377 	{
378 		sal_Int32 scrollRow = 0;
379 	    _scrollrow >>= scrollRow;
380 	    ScSplitPos eWhich = pViewShell->GetViewData()->GetActivePart();
381 	    sal_Int32 nOldValue = pViewShell->GetViewData()->GetPosY(WhichV(eWhich)) + 1;
382 		pViewShell->ScrollLines(0, scrollRow - nOldValue);
383 	}
384 }
385 
386 uno::Any SAL_CALL
getScrollColumn()387 ScVbaWindow::getScrollColumn() throw (uno::RuntimeException)
388 {
389     sal_Int32 nValue = 0;
390     // !! TODO !! get view shell from controller
391     ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );
392 	if ( pViewShell )
393 	{
394 	    ScSplitPos eWhich = pViewShell->GetViewData()->GetActivePart();
395 	    nValue = pViewShell->GetViewData()->GetPosX(WhichH(eWhich));
396 	}
397 
398     return uno::makeAny( nValue + 1);
399 }
400 
401 void SAL_CALL
setScrollColumn(const uno::Any & _scrollcolumn)402 ScVbaWindow::setScrollColumn( const uno::Any& _scrollcolumn ) throw (uno::RuntimeException)
403 {
404     // !! TODO !! get view shell from controller
405 	ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );
406 	if ( pViewShell )
407 	{
408 		sal_Int32 scrollColumn = 0;
409 	    _scrollcolumn >>= scrollColumn;
410 	    ScSplitPos eWhich = pViewShell->GetViewData()->GetActivePart();
411 	    sal_Int32 nOldValue = pViewShell->GetViewData()->GetPosX(WhichH(eWhich)) + 1;
412 		pViewShell->ScrollLines(scrollColumn - nOldValue, 0);
413 	}
414 }
415 
416 uno::Any SAL_CALL
getWindowState()417 ScVbaWindow::getWindowState() throw (uno::RuntimeException)
418 {
419     sal_Int32 nwindowState = xlNormal;
420     // !! TODO !! get view shell from controller
421     ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );
422     SfxViewFrame* pViewFrame = pViewShell -> GetViewFrame();
423     WorkWindow* pWork = (WorkWindow*) pViewFrame->GetFrame().GetSystemWindow();
424     if ( pWork )
425     {
426         if ( pWork -> IsMaximized())
427             nwindowState = xlMaximized;
428         else if (pWork -> IsMinimized())
429             nwindowState = xlMinimized;
430     }
431     return uno::makeAny( nwindowState );
432 }
433 
434 void SAL_CALL
setWindowState(const uno::Any & _windowstate)435 ScVbaWindow::setWindowState( const uno::Any& _windowstate ) throw (uno::RuntimeException)
436 {
437 	sal_Int32 nwindowState = xlMaximized;
438 	_windowstate >>= nwindowState;
439     // !! TODO !! get view shell from controller
440 	ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );
441 	SfxViewFrame* pViewFrame = pViewShell -> GetViewFrame();
442     WorkWindow* pWork = (WorkWindow*) pViewFrame->GetFrame().GetSystemWindow();
443     if ( pWork )
444     {
445         if ( nwindowState == xlMaximized)
446             pWork -> Maximize();
447         else if (nwindowState == xlMinimized)
448             pWork -> Minimize();
449         else if (nwindowState == xlNormal)
450             pWork -> Restore();
451         else
452             throw uno::RuntimeException( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Invalid Parameter" ) ), uno::Reference< uno::XInterface >() );
453     }
454 }
455 
456 void
Activate()457 ScVbaWindow::Activate() throw (css::uno::RuntimeException)
458 {
459 	ScVbaWorkbook workbook( uno::Reference< XHelperInterface >( Application(), uno::UNO_QUERY_THROW ), mxContext, m_xModel );
460 
461 	workbook.Activate();
462 }
463 
464 void
Close(const uno::Any & SaveChanges,const uno::Any & FileName,const uno::Any & RouteWorkBook)465 ScVbaWindow::Close( const uno::Any& SaveChanges, const uno::Any& FileName, const uno::Any& RouteWorkBook ) throw (uno::RuntimeException)
466 {
467 	ScVbaWorkbook workbook( uno::Reference< XHelperInterface >( Application(), uno::UNO_QUERY_THROW ), mxContext, m_xModel );
468 	workbook.Close(SaveChanges, FileName, RouteWorkBook );
469 }
470 
471 uno::Reference< excel::XPane > SAL_CALL
ActivePane()472 ScVbaWindow::ActivePane() throw (script::BasicErrorException, uno::RuntimeException)
473 {
474     uno::Reference< sheet::XViewPane > xViewPane( getController(), uno::UNO_QUERY_THROW );
475 	return new ScVbaPane( this, mxContext, m_xModel, xViewPane );
476 }
477 
478 uno::Reference< excel::XRange > SAL_CALL
ActiveCell()479 ScVbaWindow::ActiveCell(  ) throw (script::BasicErrorException, uno::RuntimeException)
480 {
481 	uno::Reference< excel::XApplication > xApplication( Application(), uno::UNO_QUERY_THROW );
482 	return xApplication->getActiveCell();
483 }
484 
485 uno::Any SAL_CALL
Selection()486 ScVbaWindow::Selection(  ) throw (script::BasicErrorException, uno::RuntimeException)
487 {
488 	uno::Reference< excel::XApplication > xApplication( Application(), uno::UNO_QUERY_THROW );
489 	return xApplication->getSelection();
490 }
491 
492 uno::Reference< excel::XRange > SAL_CALL
RangeSelection()493 ScVbaWindow::RangeSelection() throw (script::BasicErrorException, uno::RuntimeException)
494 {
495     /*  TODO / FIXME: According to documentation, this method returns the range
496         selection even if shapes are selected. */
497     return uno::Reference< excel::XRange >( Selection(), uno::UNO_QUERY_THROW );
498 }
499 
500 ::sal_Bool SAL_CALL
getDisplayGridlines()501 ScVbaWindow::getDisplayGridlines() throw (uno::RuntimeException)
502 {
503 	rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_SHOWGRID ) );
504 	sal_Bool bGrid = sal_True;
505 	getControllerProps()->getPropertyValue( sName ) >>= bGrid;
506 	return bGrid;
507 }
508 
509 
510 void SAL_CALL
setDisplayGridlines(::sal_Bool _displaygridlines)511 ScVbaWindow::setDisplayGridlines( ::sal_Bool _displaygridlines ) throw (uno::RuntimeException)
512 {
513 	rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_SHOWGRID ) );
514 	getControllerProps()->setPropertyValue( sName, uno::makeAny( _displaygridlines ));
515 }
516 
517 ::sal_Bool SAL_CALL
getDisplayHeadings()518 ScVbaWindow::getDisplayHeadings() throw (uno::RuntimeException)
519 {
520 	rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_COLROWHDR ) );
521 	sal_Bool bHeading = sal_True;
522 	getControllerProps()->getPropertyValue( sName ) >>= bHeading;
523 	return bHeading;
524 }
525 
526 void SAL_CALL
setDisplayHeadings(::sal_Bool _bDisplayHeadings)527 ScVbaWindow::setDisplayHeadings( ::sal_Bool _bDisplayHeadings ) throw (uno::RuntimeException)
528 {
529 	rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_COLROWHDR ) );
530 	getControllerProps()->setPropertyValue( sName, uno::makeAny( _bDisplayHeadings ));
531 }
532 
533 ::sal_Bool SAL_CALL
getDisplayHorizontalScrollBar()534 ScVbaWindow::getDisplayHorizontalScrollBar() throw (uno::RuntimeException)
535 {
536 	rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_HORSCROLL ) );
537 	sal_Bool bHorizontalScrollBar = sal_True;
538 	getControllerProps()->getPropertyValue( sName ) >>= bHorizontalScrollBar;
539 	return bHorizontalScrollBar;
540 }
541 
542 void SAL_CALL
setDisplayHorizontalScrollBar(::sal_Bool _bDisplayHorizontalScrollBar)543 ScVbaWindow::setDisplayHorizontalScrollBar( ::sal_Bool _bDisplayHorizontalScrollBar ) throw (uno::RuntimeException)
544 {
545 	rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_HORSCROLL ) );
546 	getControllerProps()->setPropertyValue( sName, uno::makeAny( _bDisplayHorizontalScrollBar ));
547 }
548 
549 ::sal_Bool SAL_CALL
getDisplayOutline()550 ScVbaWindow::getDisplayOutline() throw (uno::RuntimeException)
551 {
552 	rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_OUTLSYMB ) );
553 	sal_Bool bOutline = sal_True;
554 	getControllerProps()->getPropertyValue( sName ) >>= bOutline;
555 	return bOutline;
556 }
557 
558 void SAL_CALL
setDisplayOutline(::sal_Bool _bDisplayOutline)559 ScVbaWindow::setDisplayOutline( ::sal_Bool _bDisplayOutline ) throw (uno::RuntimeException)
560 {
561 	rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_OUTLSYMB ) );
562 	getControllerProps()->setPropertyValue( sName, uno::makeAny( _bDisplayOutline ));
563 }
564 
565 ::sal_Bool SAL_CALL
getDisplayVerticalScrollBar()566 ScVbaWindow::getDisplayVerticalScrollBar() throw (uno::RuntimeException)
567 {
568 	rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_VERTSCROLL ) );
569 	sal_Bool bVerticalScrollBar = sal_True;
570 	getControllerProps()->getPropertyValue( sName ) >>= bVerticalScrollBar;
571 	return bVerticalScrollBar;
572 }
573 
574 void SAL_CALL
setDisplayVerticalScrollBar(::sal_Bool _bDisplayVerticalScrollBar)575 ScVbaWindow::setDisplayVerticalScrollBar( ::sal_Bool _bDisplayVerticalScrollBar ) throw (uno::RuntimeException)
576 {
577 	rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_VERTSCROLL ) );
578 	getControllerProps()->setPropertyValue( sName, uno::makeAny( _bDisplayVerticalScrollBar ));
579 }
580 
581 ::sal_Bool SAL_CALL
getDisplayWorkbookTabs()582 ScVbaWindow::getDisplayWorkbookTabs() throw (uno::RuntimeException)
583 {
584 	rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_SHEETTABS ) );
585 	sal_Bool bWorkbookTabs = sal_True;
586 	getControllerProps()->getPropertyValue( sName ) >>= bWorkbookTabs;
587 	return bWorkbookTabs;
588 }
589 
590 void SAL_CALL
setDisplayWorkbookTabs(::sal_Bool _bDisplayWorkbookTabs)591 ScVbaWindow::setDisplayWorkbookTabs( ::sal_Bool _bDisplayWorkbookTabs ) throw (uno::RuntimeException)
592 {
593 	rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_SHEETTABS ) );
594 	getControllerProps()->setPropertyValue( sName, uno::makeAny( _bDisplayWorkbookTabs ));
595 }
596 
597 ::sal_Bool SAL_CALL
getFreezePanes()598 ScVbaWindow::getFreezePanes() throw (uno::RuntimeException)
599 {
600     uno::Reference< sheet::XViewFreezable > xViewFreezable( getController(), uno::UNO_QUERY_THROW );
601 	return xViewFreezable->hasFrozenPanes();
602 }
603 
604 void SAL_CALL
setFreezePanes(::sal_Bool _bFreezePanes)605 ScVbaWindow::setFreezePanes( ::sal_Bool _bFreezePanes ) throw (uno::RuntimeException)
606 {
607     uno::Reference< sheet::XViewPane > xViewPane( getController(), uno::UNO_QUERY_THROW );
608     uno::Reference< sheet::XViewSplitable > xViewSplitable( xViewPane, uno::UNO_QUERY_THROW );
609     uno::Reference< sheet::XViewFreezable > xViewFreezable( xViewPane, uno::UNO_QUERY_THROW );
610 	if( _bFreezePanes )
611 	{
612 		if( xViewSplitable->getIsWindowSplit() )
613 		{
614 			// if there is a split we freeze at the split
615 			sal_Int32 nColumn = getSplitColumn();
616 			sal_Int32 nRow = getSplitRow();
617 			xViewFreezable->freezeAtPosition( nColumn, nRow );
618 		}
619 		else
620 		{
621 			// otherwise we freeze in the center of the visible sheet
622 			table::CellRangeAddress aCellRangeAddress = xViewPane->getVisibleRange();
623 			sal_Int32 nColumn = aCellRangeAddress.StartColumn + (( aCellRangeAddress.EndColumn - aCellRangeAddress.StartColumn )/2 );
624 			sal_Int32 nRow = aCellRangeAddress.StartRow + (( aCellRangeAddress.EndRow - aCellRangeAddress.StartRow )/2 );
625 			xViewFreezable->freezeAtPosition( nColumn, nRow );
626 		}
627 	}
628 	else
629 	{
630 		//remove the freeze panes
631 		xViewSplitable->splitAtPosition(0,0);
632 	}
633 }
634 
635 ::sal_Bool SAL_CALL
getSplit()636 ScVbaWindow::getSplit() throw (uno::RuntimeException)
637 {
638     uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
639 	return xViewSplitable->getIsWindowSplit();
640 }
641 
642 void SAL_CALL
setSplit(::sal_Bool _bSplit)643 ScVbaWindow::setSplit( ::sal_Bool _bSplit ) throw (uno::RuntimeException)
644 {
645 	if( !_bSplit )
646 	{
647         uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
648 		xViewSplitable->splitAtPosition(0,0);
649 	}
650 	else
651 	{
652         uno::Reference< sheet::XViewFreezable > xViewFreezable( getController(), uno::UNO_QUERY_THROW );
653 		uno::Reference< excel::XRange > xRange = ActiveCell();
654 		sal_Int32 nRow = xRange->getRow();
655 		sal_Int32 nColumn = xRange->getColumn();
656 		SplitAtDefinedPosition( nColumn-1, nRow-1 );
657 	}
658 }
659 
660 sal_Int32 SAL_CALL
getSplitColumn()661 ScVbaWindow::getSplitColumn() throw (uno::RuntimeException)
662 {
663     uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
664 	return xViewSplitable->getSplitColumn();
665 }
666 
667 void SAL_CALL
setSplitColumn(sal_Int32 _splitcolumn)668 ScVbaWindow::setSplitColumn( sal_Int32 _splitcolumn ) throw (uno::RuntimeException)
669 {
670 	if( getSplitColumn() != _splitcolumn )
671 	{
672         uno::Reference< sheet::XViewFreezable > xViewFreezable( getController(), uno::UNO_QUERY_THROW );
673 		sal_Int32 nRow = getSplitRow();
674 		SplitAtDefinedPosition( _splitcolumn, nRow );
675 	}
676 }
677 
678 double SAL_CALL
getSplitHorizontal()679 ScVbaWindow::getSplitHorizontal() throw (uno::RuntimeException)
680 {
681     uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
682 	return PixelsToPoints( getDevice(), xViewSplitable->getSplitHorizontal(), sal_True );
683 }
684 
685 void SAL_CALL
setSplitHorizontal(double _splithorizontal)686 ScVbaWindow::setSplitHorizontal( double _splithorizontal ) throw (uno::RuntimeException)
687 {
688     uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
689 	double fHoriPixels = PointsToPixels( getDevice(), _splithorizontal, sal_True );
690     xViewSplitable->splitAtPosition( static_cast< sal_Int32 >( fHoriPixels ), 0 );
691 }
692 
693 sal_Int32 SAL_CALL
getSplitRow()694 ScVbaWindow::getSplitRow() throw (uno::RuntimeException)
695 {
696     uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
697 	return xViewSplitable->getSplitRow();
698 }
699 
700 void SAL_CALL
setSplitRow(sal_Int32 _splitrow)701 ScVbaWindow::setSplitRow( sal_Int32 _splitrow ) throw (uno::RuntimeException)
702 {
703 	if( getSplitRow() != _splitrow )
704 	{
705         uno::Reference< sheet::XViewFreezable > xViewFreezable( getController(), uno::UNO_QUERY_THROW );
706 		sal_Int32 nColumn = getSplitColumn();
707 		SplitAtDefinedPosition( nColumn, _splitrow );
708 	}
709 }
710 
711 double SAL_CALL
getSplitVertical()712 ScVbaWindow::getSplitVertical() throw (uno::RuntimeException)
713 {
714     uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
715 	return PixelsToPoints( getDevice(), xViewSplitable->getSplitVertical(), sal_False );
716 }
717 
718 void SAL_CALL
setSplitVertical(double _splitvertical)719 ScVbaWindow::setSplitVertical(double _splitvertical ) throw (uno::RuntimeException)
720 {
721     uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
722 	double fVertiPixels = PointsToPixels( getDevice(), _splitvertical, sal_False );
723 	xViewSplitable->splitAtPosition( 0, static_cast<sal_Int32>( fVertiPixels ) );
724 }
725 
SplitAtDefinedPosition(sal_Int32 nColumns,sal_Int32 nRows)726 void ScVbaWindow::SplitAtDefinedPosition( sal_Int32 nColumns, sal_Int32 nRows )
727 {
728     uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
729     uno::Reference< sheet::XViewFreezable > xViewFreezable( xViewSplitable, uno::UNO_QUERY_THROW );
730 	// nColumns and nRows means split columns/rows
731 	if( nColumns == 0 && nRows == 0 )
732 		return;
733 
734 	sal_Int32 cellColumn = nColumns + 1;
735 	sal_Int32 cellRow = nRows + 1;
736 
737 	ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );
738 	if ( pViewShell )
739 	{
740 		//firstly remove the old splitter
741 		xViewSplitable->splitAtPosition(0,0);
742 
743 		uno::Reference< excel::XApplication > xApplication( Application(), uno::UNO_QUERY_THROW );
744 		uno::Reference< excel::XWorksheet > xSheet( xApplication->getActiveSheet(), uno::UNO_QUERY_THROW );
745 		xSheet->Cells(uno::makeAny(cellRow), uno::makeAny(cellColumn))->Select();
746 
747 		//pViewShell->FreezeSplitters( FALSE );
748 		dispatchExecute( pViewShell, SID_WINDOW_SPLIT );
749 	}
750 }
751 
752 uno::Any SAL_CALL
getZoom()753 ScVbaWindow::getZoom() throw (uno::RuntimeException)
754 {
755 	uno::Reference< beans::XPropertySet > xProps = getControllerProps();
756 	rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_ZOOMTYPE ) );
757 	sal_Int16 nZoomType = view::DocumentZoomType::PAGE_WIDTH;
758 	xProps->getPropertyValue( sName ) >>= nZoomType;
759 	if( nZoomType == view::DocumentZoomType::PAGE_WIDTH )
760 	{
761 		return uno::makeAny( sal_True );
762 	}
763 	else if( nZoomType == view::DocumentZoomType::BY_VALUE )
764 	{
765 		sName = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(SC_UNO_ZOOMVALUE));
766 		sal_Int16 nZoom = 100;
767 		xProps->getPropertyValue( sName ) >>= nZoom;
768 		return uno::makeAny( nZoom );
769 	}
770     return uno::Any();
771 }
772 
773 void SAL_CALL
setZoom(const uno::Any & _zoom)774 ScVbaWindow::setZoom( const uno::Any& _zoom ) throw (uno::RuntimeException)
775 {
776     sal_Int16 nZoom = 100;
777     _zoom >>= nZoom;
778     uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( m_xModel, uno::UNO_QUERY_THROW );
779     uno::Reference< excel::XWorksheet > xActiveSheet = ActiveSheet();
780     SCTAB nTab = 0;
781     if ( !ScVbaWorksheets::nameExists (xSpreadDoc, xActiveSheet->getName(), nTab) )
782         throw uno::RuntimeException();
783     std::vector< SCTAB > vTabs;
784     vTabs.push_back( nTab );
785     excel::implSetZoom( m_xModel, nZoom, vTabs );
786 }
787 
788 uno::Reference< excel::XWorksheet > SAL_CALL
ActiveSheet()789 ScVbaWindow::ActiveSheet(  ) throw (script::BasicErrorException, uno::RuntimeException)
790 {
791 	uno::Reference< excel::XApplication > xApplication( Application(), uno::UNO_QUERY_THROW );
792 	return xApplication->getActiveSheet();
793 }
794 
795 uno::Any SAL_CALL
getView()796 ScVbaWindow::getView() throw (uno::RuntimeException)
797 {
798 	sal_Bool bPageBreak = sal_False;
799 	sal_Int32 nWindowView = excel::XlWindowView::xlNormalView;
800 
801 	ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );
802 	if (pViewShell)
803 		bPageBreak = pViewShell->GetViewData()->IsPagebreakMode();
804 
805 	if( bPageBreak )
806 		nWindowView = excel::XlWindowView::xlPageBreakPreview;
807 	else
808 		nWindowView = excel::XlWindowView::xlNormalView;
809 
810 	return uno::makeAny( nWindowView );
811 }
812 
813 void SAL_CALL
setView(const uno::Any & _view)814 ScVbaWindow::setView( const uno::Any& _view) throw (uno::RuntimeException)
815 {
816 	sal_Int32 nWindowView = excel::XlWindowView::xlNormalView;
817 	_view >>= nWindowView;
818 	sal_uInt16 nSlot = FID_NORMALVIEWMODE;
819 	switch ( nWindowView )
820 	{
821 		case excel::XlWindowView::xlNormalView:
822 			nSlot = FID_NORMALVIEWMODE;
823 			break;
824 		case excel::XlWindowView::xlPageBreakPreview:
825 			nSlot = FID_PAGEBREAKMODE;
826 			break;
827 		default:
828 			DebugHelper::exception(SbERR_BAD_PARAMETER, rtl::OUString() );
829 	}
830     // !! TODO !! get view shell from controller
831 	ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );
832 	if ( pViewShell )
833 		dispatchExecute( pViewShell, nSlot );
834 }
835 
836 uno::Reference< excel::XRange > SAL_CALL
getVisibleRange()837 ScVbaWindow::getVisibleRange() throw (uno::RuntimeException)
838 {
839     uno::Reference< container::XIndexAccess > xPanesIA( getController(), uno::UNO_QUERY_THROW );
840     uno::Reference< sheet::XViewPane > xTopLeftPane( xPanesIA->getByIndex( 0 ), uno::UNO_QUERY_THROW );
841     uno::Reference< excel::XPane > xPane( new ScVbaPane( this, mxContext, m_xModel, xTopLeftPane ) );
842     return xPane->getVisibleRange();
843 }
844 
845 sal_Int32 SAL_CALL
PointsToScreenPixelsX(sal_Int32 _points)846 ScVbaWindow::PointsToScreenPixelsX(sal_Int32 _points) throw (css::script::BasicErrorException, css::uno::RuntimeException)
847 {
848 	sal_Int32 nHundredthsofOneMillimeters = Millimeter::getInHundredthsOfOneMillimeter( _points );
849 	double fConvertFactor = (getDevice()->getInfo().PixelPerMeterX/100000);
850 	return static_cast<sal_Int32>(fConvertFactor * nHundredthsofOneMillimeters );
851 }
852 
853 sal_Int32 SAL_CALL
PointsToScreenPixelsY(sal_Int32 _points)854 ScVbaWindow::PointsToScreenPixelsY(sal_Int32 _points) throw (css::script::BasicErrorException, css::uno::RuntimeException)
855 {
856 	sal_Int32 nHundredthsofOneMillimeters = Millimeter::getInHundredthsOfOneMillimeter( _points );
857 	double fConvertFactor = (getDevice()->getInfo().PixelPerMeterY/100000);
858 	return static_cast<sal_Int32>(fConvertFactor * nHundredthsofOneMillimeters );
859 }
860 
861 void SAL_CALL
PrintOut(const css::uno::Any & From,const css::uno::Any & To,const css::uno::Any & Copies,const css::uno::Any & Preview,const css::uno::Any & ActivePrinter,const css::uno::Any & PrintToFile,const css::uno::Any & Collate,const css::uno::Any & PrToFileName)862 ScVbaWindow::PrintOut( const css::uno::Any& From, const css::uno::Any&To, const css::uno::Any& Copies, const css::uno::Any& Preview, const css::uno::Any& ActivePrinter, const css::uno::Any& PrintToFile, const css::uno::Any& Collate, const css::uno::Any& PrToFileName ) throw (css::script::BasicErrorException, css::uno::RuntimeException)
863 {
864 	// need test, print current active sheet
865     // !! TODO !! get view shell from controller
866 	PrintOutHelper( excel::getBestViewShell( m_xModel ), From, To, Copies, Preview, ActivePrinter, PrintToFile, Collate, PrToFileName, sal_True );
867 }
868 
869 void SAL_CALL
PrintPreview(const css::uno::Any & EnableChanges)870 ScVbaWindow::PrintPreview( const css::uno::Any& EnableChanges ) throw (css::script::BasicErrorException, css::uno::RuntimeException)
871 {
872 	// need test, print preview current active sheet
873     // !! TODO !! get view shell from controller
874 	PrintPreviewHelper( EnableChanges, excel::getBestViewShell( m_xModel ) );
875 }
876 
877 rtl::OUString&
getServiceImplName()878 ScVbaWindow::getServiceImplName()
879 {
880 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaWindow") );
881 	return sImplName;
882 }
883 
884 uno::Sequence< rtl::OUString >
getServiceNames()885 ScVbaWindow::getServiceNames()
886 {
887 	static uno::Sequence< rtl::OUString > aServiceNames;
888 	if ( aServiceNames.getLength() == 0 )
889 	{
890 		aServiceNames.realloc( 1 );
891 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Window" ) );
892 	}
893 	return aServiceNames;
894 }
895 namespace window
896 {
897 namespace sdecl = comphelper::service_decl;
898 sdecl::vba_service_class_<ScVbaWindow, sdecl::with_args<true> > serviceImpl;
899 extern sdecl::ServiceDecl const serviceDecl(
900     serviceImpl,
901     "ScVbaWindow",
902     "ooo.vba.excel.Window" );
903 }
904