update dev300-m58
[ooovba.git] / sw / source / ui / vba / vbaview.cxx
blobbc7b9b4423f8837b9d3a024b424771beb2803aba
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile:
10 * $Revision:
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include "vbaview.hxx"
31 #include <vbahelper/vbahelper.hxx>
32 #include <tools/diagnose_ex.h>
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/view/XViewSettingsSupplier.hpp>
35 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
36 #include <com/sun/star/text/XText.hpp>
37 #include <com/sun/star/text/XTextTable.hpp>
38 #include <com/sun/star/table/XCellRange.hpp>
39 #include <com/sun/star/text/XTextDocument.hpp>
40 #include <com/sun/star/text/XFootnotesSupplier.hpp>
41 #include <com/sun/star/text/XEndnotesSupplier.hpp>
42 #include <com/sun/star/container/XIndexAccess.hpp>
43 #include <com/sun/star/container/XEnumerationAccess.hpp>
44 #include <com/sun/star/container/XEnumeration.hpp>
45 #include <com/sun/star/frame/XController.hpp>
46 #include <com/sun/star/lang/XServiceInfo.hpp>
47 #include <ooo/vba/word/WdSpecialPane.hpp>
48 #include <ooo/vba/word/WdViewType.hpp>
49 #include <ooo/vba/word/WdSeekView.hpp>
51 #include "wordvbahelper.hxx"
52 #include "vbaheaderfooterhelper.hxx"
53 #include <view.hxx>
55 using namespace ::ooo::vba;
56 using namespace ::com::sun::star;
58 static const sal_Int32 DEFAULT_BODY_DISTANCE = 500;
60 SwVbaView::SwVbaView( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext,
61 const uno::Reference< frame::XModel >& rModel ) throw ( uno::RuntimeException ) :
62 SwVbaView_BASE( rParent, rContext ), mxModel( rModel )
64 uno::Reference< frame::XController > xController = mxModel->getCurrentController();
66 uno::Reference< text::XTextViewCursorSupplier > xTextViewCursorSupp( xController, uno::UNO_QUERY_THROW );
67 mxViewCursor = xTextViewCursorSupp->getViewCursor();
69 uno::Reference< view::XViewSettingsSupplier > xViewSettingSupp( xController, uno::UNO_QUERY_THROW );
70 mxViewSettings.set( xViewSettingSupp->getViewSettings(), uno::UNO_QUERY_THROW );
73 SwVbaView::~SwVbaView()
77 ::sal_Int32 SAL_CALL
78 SwVbaView::getSeekView() throw (css::uno::RuntimeException)
80 // FIXME: if the view cursor is in table, field, section and frame
81 // handle if the cursor is in table
82 uno::Reference< text::XText > xCurrentText = mxViewCursor->getText();
83 uno::Reference< beans::XPropertySet > xCursorProps( mxViewCursor, uno::UNO_QUERY_THROW );
84 uno::Reference< text::XTextContent > xTextContent;
85 while( xCursorProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("TextTable") ) ) >>= xTextContent )
87 xCurrentText = xTextContent->getAnchor()->getText();
88 xCursorProps.set( xCurrentText->createTextCursor(), uno::UNO_QUERY_THROW );
90 uno::Reference< lang::XServiceInfo > xServiceInfo( xCurrentText, uno::UNO_QUERY_THROW );
91 rtl::OUString aImplName = xServiceInfo->getImplementationName();
92 if( aImplName.equalsAscii("SwXBodyText") )
94 return word::WdSeekView::wdSeekMainDocument;
96 else if( aImplName.equalsAscii("SwXHeadFootText") )
98 if( HeaderFooterHelper::isHeader( mxModel, xCurrentText ) )
100 if( HeaderFooterHelper::isFirstPageHeader( mxModel, xCurrentText ) )
101 return word::WdSeekView::wdSeekFirstPageHeader;
102 else if( HeaderFooterHelper::isEvenPagesHeader( mxModel, xCurrentText ) )
103 return word::WdSeekView::wdSeekEvenPagesHeader;
104 else
105 return word::WdSeekView::wdSeekPrimaryHeader;
107 else
109 if( HeaderFooterHelper::isFirstPageFooter( mxModel, xCurrentText ) )
110 return word::WdSeekView::wdSeekFirstPageFooter;
111 else if( HeaderFooterHelper::isEvenPagesFooter( mxModel, xCurrentText ) )
112 return word::WdSeekView::wdSeekEvenPagesFooter;
113 else
114 return word::WdSeekView::wdSeekPrimaryFooter;
117 else if( aImplName.equalsAscii("SwXFootnote") )
119 if( xServiceInfo->supportsService( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.Endnote") ) ) )
120 return word::WdSeekView::wdSeekEndnotes;
121 else
122 return word::WdSeekView::wdSeekFootnotes;
125 return word::WdSeekView::wdSeekMainDocument;
128 void SAL_CALL
129 SwVbaView::setSeekView( ::sal_Int32 _seekview ) throw (css::uno::RuntimeException)
131 // FIXME: save the current cursor position, if the cursor is in the main
132 // document, so we can jump back to this position, if the macro sets
133 // the ViewMode back to wdSeekMainDocument
135 // if( _seekview == getSeekView() )
136 // return;
138 switch( _seekview )
140 case word::WdSeekView::wdSeekFirstPageFooter:
141 case word::WdSeekView::wdSeekFirstPageHeader:
142 case word::WdSeekView::wdSeekCurrentPageFooter:
143 case word::WdSeekView::wdSeekCurrentPageHeader:
144 case word::WdSeekView::wdSeekPrimaryFooter:
145 case word::WdSeekView::wdSeekPrimaryHeader:
146 case word::WdSeekView::wdSeekEvenPagesFooter:
147 case word::WdSeekView::wdSeekEvenPagesHeader:
149 // need to test
150 mxViewCursor->gotoRange( getHFTextRange( _seekview ), sal_False );
151 break;
153 case word::WdSeekView::wdSeekFootnotes:
155 uno::Reference< text::XFootnotesSupplier > xFootnotesSupp( mxModel, uno::UNO_QUERY_THROW );
156 uno::Reference< container::XIndexAccess > xFootnotes( xFootnotesSupp->getFootnotes(), uno::UNO_QUERY_THROW );
157 if( xFootnotes->getCount() > 0 )
159 uno::Reference< text::XText > xText( xFootnotes->getByIndex(0), uno::UNO_QUERY_THROW );
160 mxViewCursor->gotoRange( xText->getStart(), sal_False );
162 else
164 DebugHelper::exception( SbERR_NO_ACTIVE_OBJECT, rtl::OUString() );
166 break;
168 case word::WdSeekView::wdSeekEndnotes:
170 uno::Reference< text::XEndnotesSupplier > xEndnotesSupp( mxModel, uno::UNO_QUERY_THROW );
171 uno::Reference< container::XIndexAccess > xEndnotes( xEndnotesSupp->getEndnotes(), uno::UNO_QUERY_THROW );
172 if( xEndnotes->getCount() > 0 )
174 uno::Reference< text::XText > xText( xEndnotes->getByIndex(0), uno::UNO_QUERY_THROW );
175 mxViewCursor->gotoRange( xText->getStart(), sal_False );
177 else
179 DebugHelper::exception( SbERR_NO_ACTIVE_OBJECT, rtl::OUString() );
181 break;
183 case word::WdSeekView::wdSeekMainDocument:
185 uno::Reference< text::XTextDocument > xTextDocument( mxModel, uno::UNO_QUERY_THROW );
186 uno::Reference< text::XText > xText = xTextDocument->getText();
187 mxViewCursor->gotoRange( getFirstObjectPosition( xText ), sal_False );
188 break;
193 ::sal_Int32 SAL_CALL
194 SwVbaView::getSplitSpecial() throw (css::uno::RuntimeException)
196 return word::WdSpecialPane::wdPaneNone;
199 void SAL_CALL
200 SwVbaView::setSplitSpecial( ::sal_Int32/* _splitspecial */) throw (css::uno::RuntimeException)
202 // not support in Writer
205 ::sal_Bool SAL_CALL
206 SwVbaView::getTableGridLines() throw (css::uno::RuntimeException)
208 sal_Bool bShowTableGridLine = sal_False;
209 mxViewSettings->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ShowTableBoundaries"))) >>= bShowTableGridLine;
210 return bShowTableGridLine;
213 void SAL_CALL
214 SwVbaView::setTableGridLines( ::sal_Bool _tablegridlines ) throw (css::uno::RuntimeException)
216 mxViewSettings->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ShowTableBoundaries")), uno::makeAny( _tablegridlines ) );
219 ::sal_Int32 SAL_CALL
220 SwVbaView::getType() throw (css::uno::RuntimeException)
222 // FIXME: handle wdPrintPreview type
223 sal_Bool bOnlineLayout = sal_False;
224 mxViewSettings->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ShowOnlineLayout"))) >>= bOnlineLayout;
225 return bOnlineLayout ? word::WdViewType::wdWebView : word::WdViewType::wdPrintView;
228 void SAL_CALL
229 SwVbaView::setType( ::sal_Int32 _type ) throw (css::uno::RuntimeException)
231 // FIXME: handle wdPrintPreview type
232 switch( _type )
234 case word::WdViewType::wdPrintView:
235 case word::WdViewType::wdNormalView:
237 mxViewSettings->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ShowOnlineLayout")), uno::makeAny( sal_False ) );
238 break;
240 case word::WdViewType::wdWebView:
242 mxViewSettings->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ShowOnlineLayout")), uno::makeAny( sal_True ) );
243 break;
245 case word::WdViewType::wdPrintPreview:
247 PrintPreviewHelper( uno::Any(),word::getView( mxModel ) );
248 break;
250 default:
251 DebugHelper::exception( SbERR_NOT_IMPLEMENTED, rtl::OUString() );
256 uno::Reference< text::XTextRange > SwVbaView::getHFTextRange( sal_Int32 nType ) throw (uno::RuntimeException)
258 mxModel->lockControllers();
260 rtl::OUString aPropIsOn;
261 rtl::OUString aPropIsShared;
262 rtl::OUString aPropBodyDistance;
263 rtl::OUString aPropText;
265 switch( nType )
267 case word::WdSeekView::wdSeekCurrentPageFooter:
268 case word::WdSeekView::wdSeekFirstPageFooter:
269 case word::WdSeekView::wdSeekPrimaryFooter:
270 case word::WdSeekView::wdSeekEvenPagesFooter:
272 aPropIsOn = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterIsOn") );
273 aPropIsShared = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterIsShared") );
274 aPropBodyDistance = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterBodyDistance") );
275 aPropText = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterText") );
276 break;
278 case word::WdSeekView::wdSeekCurrentPageHeader:
279 case word::WdSeekView::wdSeekFirstPageHeader:
280 case word::WdSeekView::wdSeekPrimaryHeader:
281 case word::WdSeekView::wdSeekEvenPagesHeader:
283 aPropIsOn = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsOn") );
284 aPropIsShared = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsShared") );
285 aPropBodyDistance = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderBodyDistance") );
286 aPropText = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderText") );
287 break;
291 uno::Reference< text::XPageCursor > xPageCursor( mxViewCursor, uno::UNO_QUERY_THROW );
293 if( nType == word::WdSeekView::wdSeekFirstPageFooter
294 || nType == word::WdSeekView::wdSeekFirstPageHeader )
296 xPageCursor->jumpToFirstPage();
299 uno::Reference< style::XStyle > xStyle;
300 uno::Reference< text::XText > xText;
301 switch( nType )
303 case word::WdSeekView::wdSeekPrimaryFooter:
304 case word::WdSeekView::wdSeekPrimaryHeader:
305 case word::WdSeekView::wdSeekEvenPagesFooter:
306 case word::WdSeekView::wdSeekEvenPagesHeader:
308 // The primary header is the first header of the section.
309 // If the header is not shared between odd and even pages
310 // the odd page's header is the primary header. If the
311 // first page's header is different from the rest of the
312 // document, it is NOT the primary header ( the next primary
313 // header would be on page 3 )
314 // The even pages' header is only available if the header is
315 // not shared and the current style is applied to a page with
316 // an even page number
317 uno::Reference< beans::XPropertySet > xCursorProps( mxViewCursor, uno::UNO_QUERY_THROW );
318 rtl::OUString aPageStyleName;
319 xCursorProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageStyleName"))) >>= aPageStyleName;
320 if( aPageStyleName.equalsAscii("First Page") )
322 // go to the beginning of where the next style is used
323 sal_Bool hasNextPage = sal_False;
324 xStyle = word::getCurrentPageStyle( mxModel );
327 hasNextPage = xPageCursor->jumpToNextPage();
329 while( hasNextPage && ( xStyle == word::getCurrentPageStyle( mxModel ) ) );
331 if( !hasNextPage )
332 DebugHelper::exception( SbERR_BAD_ACTION, rtl::OUString() );
334 break;
336 default:
338 break;
342 xStyle = word::getCurrentPageStyle( mxModel );
343 uno::Reference< beans::XPropertySet > xPageProps( xStyle, uno::UNO_QUERY_THROW );
344 sal_Bool isOn = sal_False;
345 xPageProps->getPropertyValue( aPropIsOn ) >>= isOn;
346 sal_Bool isShared = sal_False;
347 xPageProps->getPropertyValue( aPropIsShared ) >>= isShared;
348 if( !isOn )
350 xPageProps->setPropertyValue( aPropIsOn, uno::makeAny( sal_True ) );
351 xPageProps->setPropertyValue( aPropBodyDistance, uno::makeAny( DEFAULT_BODY_DISTANCE ) );
353 if( !isShared )
355 rtl::OUString aTempPropText = aPropText;
356 if( nType == word::WdSeekView::wdSeekEvenPagesFooter
357 || nType == word::WdSeekView::wdSeekEvenPagesHeader )
359 aTempPropText += rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Left") );
361 else
363 aTempPropText += rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Right") );
365 xText.set( xPageProps->getPropertyValue( aTempPropText), uno::UNO_QUERY_THROW );
367 else
369 if( nType == word::WdSeekView::wdSeekEvenPagesFooter
370 || nType == word::WdSeekView::wdSeekEvenPagesHeader )
372 DebugHelper::exception( SbERR_BAD_ACTION, rtl::OUString() );
374 xText.set( xPageProps->getPropertyValue( aPropText ), uno::UNO_QUERY_THROW );
377 mxModel->unlockControllers();
378 if( !xText.is() )
380 DebugHelper::exception( SbERR_INTERNAL_ERROR, rtl::OUString() );
382 uno::Reference< text::XTextRange > xTextRange = getFirstObjectPosition( xText );
383 return xTextRange;
386 uno::Reference< text::XTextRange > SwVbaView::getFirstObjectPosition( const uno::Reference< text::XText >& xText ) throw (uno::RuntimeException)
388 // if the first object is table, get the position of first cell
389 uno::Reference< text::XTextRange > xTextRange;
390 uno::Reference< container::XEnumerationAccess > xParaAccess( xText, uno::UNO_QUERY_THROW );
391 uno::Reference< container::XEnumeration> xParaEnum = xParaAccess->createEnumeration();
392 if( xParaEnum->hasMoreElements() )
394 uno::Reference< lang::XServiceInfo > xServiceInfo( xParaEnum->nextElement(), uno::UNO_QUERY_THROW );
395 if( xServiceInfo->supportsService( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.TextTable") ) ) )
397 uno::Reference< table::XCellRange > xCellRange( xServiceInfo, uno::UNO_QUERY_THROW );
398 uno::Reference< text::XText> xFirstCellText( xCellRange->getCellByPosition(0, 0), uno::UNO_QUERY_THROW );
399 xTextRange = xFirstCellText->getStart();
402 if( !xTextRange.is() )
403 xTextRange = xText->getStart();
404 return xTextRange;
407 rtl::OUString&
408 SwVbaView::getServiceImplName()
410 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaView") );
411 return sImplName;
414 uno::Sequence< rtl::OUString >
415 SwVbaView::getServiceNames()
417 static uno::Sequence< rtl::OUString > aServiceNames;
418 if ( aServiceNames.getLength() == 0 )
420 aServiceNames.realloc( 1 );
421 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.View" ) );
423 return aServiceNames;