xref: /aoo41x/main/sc/source/ui/vba/vbawindow.cxx (revision b3f79822)
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 
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
81 	virtual ::sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException)
82 	{
83 		return m_it != m_sheets.end();
84 	}
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:
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
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
143 	virtual ::sal_Int32 SAL_CALL getCount(  ) throw (uno::RuntimeException)
144 	{
145 		return sheets.size();
146 	}
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
157 	virtual uno::Type SAL_CALL getElementType(  ) throw (uno::RuntimeException)
158 	{
159 		return excel::XWorksheet::static_type(0);
160 	}
161 
162 	virtual ::sal_Bool SAL_CALL hasElements(  ) throw (uno::RuntimeException)
163 	{
164 		return (sheets.size() > 0);
165 	}
166 
167 	//XNameAccess
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 
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 
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 
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 
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
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 >
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 >
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 >
248 ScVbaWindow::getDevice() throw (uno::RuntimeException)
249 {
250     return uno::Reference< awt::XDevice >( getWindow(), uno::UNO_QUERY_THROW );
251 }
252 
253 void
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
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
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
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
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
311 ScVbaWindow::getCaption() throw (uno::RuntimeException)
312 {
313 	static rtl::OUString sCrud(RTL_CONSTASCII_USTRINGPARAM(" - OpenOffice.org 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 					// extention 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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( xViewSplitable->getIsWindowSplit() )
611 	{
612 		// if there is a split we freeze at the split
613 		sal_Int32 nColumn = getSplitColumn();
614 		sal_Int32 nRow = getSplitRow();
615 		xViewFreezable->freezeAtPosition( nColumn, nRow );
616 	}
617 	else
618 	{
619 		// otherwise we freeze in the center of the visible sheet
620 		table::CellRangeAddress aCellRangeAddress = xViewPane->getVisibleRange();
621 		sal_Int32 nColumn = aCellRangeAddress.StartColumn + (( aCellRangeAddress.EndColumn - aCellRangeAddress.StartColumn )/2 );
622 		sal_Int32 nRow = aCellRangeAddress.StartRow + (( aCellRangeAddress.EndRow - aCellRangeAddress.StartRow )/2 );
623 		xViewFreezable->freezeAtPosition( nColumn, nRow );
624 	}
625 }
626 
627 ::sal_Bool SAL_CALL
628 ScVbaWindow::getSplit() throw (uno::RuntimeException)
629 {
630     uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
631 	return xViewSplitable->getIsWindowSplit();
632 }
633 
634 void SAL_CALL
635 ScVbaWindow::setSplit( ::sal_Bool _bSplit ) throw (uno::RuntimeException)
636 {
637 	if( !_bSplit )
638 	{
639         uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
640 		xViewSplitable->splitAtPosition(0,0);
641 	}
642 	else
643 	{
644         uno::Reference< sheet::XViewFreezable > xViewFreezable( getController(), uno::UNO_QUERY_THROW );
645 		uno::Reference< excel::XRange > xRange = ActiveCell();
646 		sal_Int32 nRow = xRange->getRow();
647 		sal_Int32 nColumn = xRange->getColumn();
648 		xViewFreezable->freezeAtPosition( nColumn-1, nRow-1 );
649 		SplitAtDefinedPosition( sal_True );
650 	}
651 }
652 
653 sal_Int32 SAL_CALL
654 ScVbaWindow::getSplitColumn() throw (uno::RuntimeException)
655 {
656     uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
657 	return xViewSplitable->getSplitColumn();
658 }
659 
660 void SAL_CALL
661 ScVbaWindow::setSplitColumn( sal_Int32 _splitcolumn ) throw (uno::RuntimeException)
662 {
663 	if( getSplitColumn() != _splitcolumn )
664 	{
665         uno::Reference< sheet::XViewFreezable > xViewFreezable( getController(), uno::UNO_QUERY_THROW );
666 		sal_Bool bFrozen = getFreezePanes();
667 		sal_Int32 nRow = getSplitRow();
668 		xViewFreezable->freezeAtPosition( _splitcolumn, nRow );
669 		SplitAtDefinedPosition( !bFrozen );
670 	}
671 }
672 
673 double SAL_CALL
674 ScVbaWindow::getSplitHorizontal() throw (uno::RuntimeException)
675 {
676     uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
677 	return PixelsToPoints( getDevice(), xViewSplitable->getSplitHorizontal(), sal_True );
678 }
679 
680 void SAL_CALL
681 ScVbaWindow::setSplitHorizontal( double _splithorizontal ) throw (uno::RuntimeException)
682 {
683     uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
684 	double fHoriPixels = PointsToPixels( getDevice(), _splithorizontal, sal_True );
685     xViewSplitable->splitAtPosition( static_cast< sal_Int32 >( fHoriPixels ), 0 );
686 }
687 
688 sal_Int32 SAL_CALL
689 ScVbaWindow::getSplitRow() throw (uno::RuntimeException)
690 {
691     uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
692 	sal_Int32 nValue = xViewSplitable->getSplitRow();
693 	return nValue ? nValue - 1 : nValue;
694 }
695 
696 void SAL_CALL
697 ScVbaWindow::setSplitRow( sal_Int32 _splitrow ) throw (uno::RuntimeException)
698 {
699 	if( getSplitRow() != _splitrow )
700 	{
701         uno::Reference< sheet::XViewFreezable > xViewFreezable( getController(), uno::UNO_QUERY_THROW );
702 		sal_Bool bFrozen = getFreezePanes();
703 		sal_Int32 nColumn = getSplitColumn();
704 		xViewFreezable->freezeAtPosition( nColumn , _splitrow );
705 		SplitAtDefinedPosition( !bFrozen );
706 	}
707 }
708 
709 double SAL_CALL
710 ScVbaWindow::getSplitVertical() throw (uno::RuntimeException)
711 {
712     uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
713 	return PixelsToPoints( getDevice(), xViewSplitable->getSplitVertical(), sal_False );
714 }
715 
716 void SAL_CALL
717 ScVbaWindow::setSplitVertical(double _splitvertical ) throw (uno::RuntimeException)
718 {
719     uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
720 	double fVertiPixels = PointsToPixels( getDevice(), _splitvertical, sal_False );
721 	xViewSplitable->splitAtPosition( 0, static_cast<sal_Int32>( fVertiPixels ) );
722 }
723 
724 void ScVbaWindow::SplitAtDefinedPosition(sal_Bool _bUnFreezePane)
725 {
726     uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
727     uno::Reference< sheet::XViewFreezable > xViewFreezable( xViewSplitable, uno::UNO_QUERY_THROW );
728 	sal_Int32 nVertSplit = xViewSplitable->getSplitVertical();
729 	sal_Int32 nHoriSplit = xViewSplitable->getSplitHorizontal();
730 	if( _bUnFreezePane )
731 		xViewFreezable->freezeAtPosition(0,0);
732 	xViewSplitable->splitAtPosition(nHoriSplit, nVertSplit);
733 }
734 
735 uno::Any SAL_CALL
736 ScVbaWindow::getZoom() throw (uno::RuntimeException)
737 {
738 	uno::Reference< beans::XPropertySet > xProps = getControllerProps();
739 	rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_ZOOMTYPE ) );
740 	sal_Int16 nZoomType = view::DocumentZoomType::PAGE_WIDTH;
741 	xProps->getPropertyValue( sName ) >>= nZoomType;
742 	if( nZoomType == view::DocumentZoomType::PAGE_WIDTH )
743 	{
744 		return uno::makeAny( sal_True );
745 	}
746 	else if( nZoomType == view::DocumentZoomType::BY_VALUE )
747 	{
748 		sName = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(SC_UNO_ZOOMVALUE));
749 		sal_Int16 nZoom = 100;
750 		xProps->getPropertyValue( sName ) >>= nZoom;
751 		return uno::makeAny( nZoom );
752 	}
753     return uno::Any();
754 }
755 
756 void SAL_CALL
757 ScVbaWindow::setZoom( const uno::Any& _zoom ) throw (uno::RuntimeException)
758 {
759     sal_Int16 nZoom = 100;
760     _zoom >>= nZoom;
761     uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( m_xModel, uno::UNO_QUERY_THROW );
762     uno::Reference< excel::XWorksheet > xActiveSheet = ActiveSheet();
763     SCTAB nTab = 0;
764     if ( !ScVbaWorksheets::nameExists (xSpreadDoc, xActiveSheet->getName(), nTab) )
765         throw uno::RuntimeException();
766     std::vector< SCTAB > vTabs;
767     vTabs.push_back( nTab );
768     excel::implSetZoom( m_xModel, nZoom, vTabs );
769 }
770 
771 uno::Reference< excel::XWorksheet > SAL_CALL
772 ScVbaWindow::ActiveSheet(  ) throw (script::BasicErrorException, uno::RuntimeException)
773 {
774 	uno::Reference< excel::XApplication > xApplication( Application(), uno::UNO_QUERY_THROW );
775 	return xApplication->getActiveSheet();
776 }
777 
778 uno::Any SAL_CALL
779 ScVbaWindow::getView() throw (uno::RuntimeException)
780 {
781 	// not supported now
782 	sal_Int32 nWindowView = excel::XlWindowView::xlNormalView;
783 	return uno::makeAny( nWindowView );
784 }
785 
786 void SAL_CALL
787 ScVbaWindow::setView( const uno::Any& _view) throw (uno::RuntimeException)
788 {
789 	sal_Int32 nWindowView = excel::XlWindowView::xlNormalView;
790 	_view >>= nWindowView;
791 	sal_uInt16 nSlot = FID_NORMALVIEWMODE;
792 	switch ( nWindowView )
793 	{
794 		case excel::XlWindowView::xlNormalView:
795 			nSlot = FID_NORMALVIEWMODE;
796 			break;
797 		case excel::XlWindowView::xlPageBreakPreview:
798 			nSlot = FID_PAGEBREAKMODE;
799 			break;
800 		default:
801 			DebugHelper::exception(SbERR_BAD_PARAMETER, rtl::OUString() );
802 	}
803     // !! TODO !! get view shell from controller
804 	ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );
805 	if ( pViewShell )
806 		dispatchExecute( pViewShell, nSlot );
807 }
808 
809 uno::Reference< excel::XRange > SAL_CALL
810 ScVbaWindow::getVisibleRange() throw (uno::RuntimeException)
811 {
812     uno::Reference< container::XIndexAccess > xPanesIA( getController(), uno::UNO_QUERY_THROW );
813     uno::Reference< sheet::XViewPane > xTopLeftPane( xPanesIA->getByIndex( 0 ), uno::UNO_QUERY_THROW );
814     uno::Reference< excel::XPane > xPane( new ScVbaPane( this, mxContext, m_xModel, xTopLeftPane ) );
815     return xPane->getVisibleRange();
816 }
817 
818 sal_Int32 SAL_CALL
819 ScVbaWindow::PointsToScreenPixelsX(sal_Int32 _points) throw (css::script::BasicErrorException, css::uno::RuntimeException)
820 {
821 	sal_Int32 nHundredthsofOneMillimeters = Millimeter::getInHundredthsOfOneMillimeter( _points );
822 	double fConvertFactor = (getDevice()->getInfo().PixelPerMeterX/100000);
823 	return static_cast<sal_Int32>(fConvertFactor * nHundredthsofOneMillimeters );
824 }
825 
826 sal_Int32 SAL_CALL
827 ScVbaWindow::PointsToScreenPixelsY(sal_Int32 _points) throw (css::script::BasicErrorException, css::uno::RuntimeException)
828 {
829 	sal_Int32 nHundredthsofOneMillimeters = Millimeter::getInHundredthsOfOneMillimeter( _points );
830 	double fConvertFactor = (getDevice()->getInfo().PixelPerMeterY/100000);
831 	return static_cast<sal_Int32>(fConvertFactor * nHundredthsofOneMillimeters );
832 }
833 
834 void SAL_CALL
835 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)
836 {
837 	// need test, print current active sheet
838     // !! TODO !! get view shell from controller
839 	PrintOutHelper( excel::getBestViewShell( m_xModel ), From, To, Copies, Preview, ActivePrinter, PrintToFile, Collate, PrToFileName, sal_True );
840 }
841 
842 void SAL_CALL
843 ScVbaWindow::PrintPreview( const css::uno::Any& EnableChanges ) throw (css::script::BasicErrorException, css::uno::RuntimeException)
844 {
845 	// need test, print preview current active sheet
846     // !! TODO !! get view shell from controller
847 	PrintPreviewHelper( EnableChanges, excel::getBestViewShell( m_xModel ) );
848 }
849 
850 rtl::OUString&
851 ScVbaWindow::getServiceImplName()
852 {
853 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaWindow") );
854 	return sImplName;
855 }
856 
857 uno::Sequence< rtl::OUString >
858 ScVbaWindow::getServiceNames()
859 {
860 	static uno::Sequence< rtl::OUString > aServiceNames;
861 	if ( aServiceNames.getLength() == 0 )
862 	{
863 		aServiceNames.realloc( 1 );
864 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Window" ) );
865 	}
866 	return aServiceNames;
867 }
868 namespace window
869 {
870 namespace sdecl = comphelper::service_decl;
871 sdecl::vba_service_class_<ScVbaWindow, sdecl::with_args<true> > serviceImpl;
872 extern sdecl::ServiceDecl const serviceDecl(
873     serviceImpl,
874     "ScVbaWindow",
875     "ooo.vba.excel.Window" );
876 }
877