1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include "vbaview.hxx"
21 #include <vbahelper/vbahelper.hxx>
22 #include <basic/sberrors.hxx>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <com/sun/star/view/XViewSettingsSupplier.hpp>
25 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
26 #include <com/sun/star/text/XText.hpp>
27 #include <com/sun/star/text/XTextDocument.hpp>
28 #include <com/sun/star/text/XFootnotesSupplier.hpp>
29 #include <com/sun/star/text/XEndnotesSupplier.hpp>
30 #include <com/sun/star/text/XPageCursor.hpp>
31 #include <com/sun/star/container/XIndexAccess.hpp>
32 #include <com/sun/star/frame/XController.hpp>
33 #include <com/sun/star/lang/XServiceInfo.hpp>
34 #include <ooo/vba/word/WdSpecialPane.hpp>
35 #include <ooo/vba/word/WdViewType.hpp>
36 #include <ooo/vba/word/WdSeekView.hpp>
38 #include "wordvbahelper.hxx"
39 #include "vbaheaderfooterhelper.hxx"
42 using namespace ::ooo::vba
;
43 using namespace ::com::sun::star
;
45 const sal_Int32 DEFAULT_BODY_DISTANCE
= 500;
47 SwVbaView::SwVbaView( const uno::Reference
< ooo::vba::XHelperInterface
>& rParent
, const uno::Reference
< uno::XComponentContext
>& rContext
,
48 uno::Reference
< frame::XModel
> xModel
) :
49 SwVbaView_BASE( rParent
, rContext
), mxModel(std::move( xModel
))
51 uno::Reference
< frame::XController
> xController
= mxModel
->getCurrentController();
53 uno::Reference
< text::XTextViewCursorSupplier
> xTextViewCursorSupp( xController
, uno::UNO_QUERY_THROW
);
54 mxViewCursor
= xTextViewCursorSupp
->getViewCursor();
56 uno::Reference
< view::XViewSettingsSupplier
> xViewSettingSupp( xController
, uno::UNO_QUERY_THROW
);
57 mxViewSettings
.set( xViewSettingSupp
->getViewSettings(), uno::UNO_SET_THROW
);
60 SwVbaView::~SwVbaView()
64 sal_Bool
SwVbaView::getShowAll()
66 bool bShowFormattingMarks
= false;
67 mxViewSettings
->getPropertyValue("ShowNonprintingCharacters") >>= bShowFormattingMarks
;
68 return bShowFormattingMarks
;
71 void SwVbaView::setShowAll(sal_Bool bSet
)
73 mxViewSettings
->setPropertyValue("ShowNonprintingCharacters", uno::Any(bSet
));
77 SwVbaView::getSeekView()
79 // FIXME: if the view cursor is in table, field, section and frame
80 // handle if the cursor is in table
81 uno::Reference
< text::XText
> xCurrentText
= mxViewCursor
->getText();
82 uno::Reference
< beans::XPropertySet
> xCursorProps( mxViewCursor
, uno::UNO_QUERY_THROW
);
83 uno::Reference
< text::XTextContent
> xTextContent
;
84 while( xCursorProps
->getPropertyValue("TextTable") >>= xTextContent
)
86 xCurrentText
= xTextContent
->getAnchor()->getText();
87 xCursorProps
.set( xCurrentText
->createTextCursor(), uno::UNO_QUERY_THROW
);
89 uno::Reference
< lang::XServiceInfo
> xServiceInfo( xCurrentText
, uno::UNO_QUERY_THROW
);
90 OUString aImplName
= xServiceInfo
->getImplementationName();
91 if ( aImplName
== "SwXBodyText" )
93 return word::WdSeekView::wdSeekMainDocument
;
95 else if ( aImplName
== "SwXHeadFootText" )
97 if( HeaderFooterHelper::isHeader( mxModel
) )
99 if( HeaderFooterHelper::isFirstPageHeader( mxModel
) )
100 return word::WdSeekView::wdSeekFirstPageHeader
;
101 else if( HeaderFooterHelper::isEvenPagesHeader( mxModel
) )
102 return word::WdSeekView::wdSeekEvenPagesHeader
;
104 return word::WdSeekView::wdSeekPrimaryHeader
;
108 if( HeaderFooterHelper::isFirstPageFooter( mxModel
) )
109 return word::WdSeekView::wdSeekFirstPageFooter
;
110 else if( HeaderFooterHelper::isEvenPagesFooter( mxModel
) )
111 return word::WdSeekView::wdSeekEvenPagesFooter
;
113 return word::WdSeekView::wdSeekPrimaryFooter
;
116 else if ( aImplName
== "SwXFootnote" )
118 if( xServiceInfo
->supportsService("com.sun.star.text.Endnote") )
119 return word::WdSeekView::wdSeekEndnotes
;
121 return word::WdSeekView::wdSeekFootnotes
;
124 return word::WdSeekView::wdSeekMainDocument
;
128 SwVbaView::setSeekView( ::sal_Int32 _seekview
)
130 // FIXME: save the current cursor position, if the cursor is in the main
131 // document, so we can jump back to this position, if the macro sets
132 // the ViewMode back to wdSeekMainDocument
134 word::gotoSelectedObjectAnchor( mxModel
);
137 case word::WdSeekView::wdSeekFirstPageFooter
:
138 case word::WdSeekView::wdSeekFirstPageHeader
:
139 case word::WdSeekView::wdSeekCurrentPageFooter
:
140 case word::WdSeekView::wdSeekCurrentPageHeader
:
141 case word::WdSeekView::wdSeekPrimaryFooter
:
142 case word::WdSeekView::wdSeekPrimaryHeader
:
143 case word::WdSeekView::wdSeekEvenPagesFooter
:
144 case word::WdSeekView::wdSeekEvenPagesHeader
:
147 mxViewCursor
->gotoRange( getHFTextRange( _seekview
), false );
150 case word::WdSeekView::wdSeekFootnotes
:
152 uno::Reference
< text::XFootnotesSupplier
> xFootnotesSupp( mxModel
, uno::UNO_QUERY_THROW
);
153 uno::Reference
< container::XIndexAccess
> xFootnotes( xFootnotesSupp
->getFootnotes(), uno::UNO_SET_THROW
);
154 if( xFootnotes
->getCount() > 0 )
156 uno::Reference
< text::XText
> xText( xFootnotes
->getByIndex(0), uno::UNO_QUERY_THROW
);
157 mxViewCursor
->gotoRange( xText
->getStart(), false );
161 DebugHelper::runtimeexception( ERRCODE_BASIC_NO_ACTIVE_OBJECT
);
165 case word::WdSeekView::wdSeekEndnotes
:
167 uno::Reference
< text::XEndnotesSupplier
> xEndnotesSupp( mxModel
, uno::UNO_QUERY_THROW
);
168 uno::Reference
< container::XIndexAccess
> xEndnotes( xEndnotesSupp
->getEndnotes(), uno::UNO_SET_THROW
);
169 if( xEndnotes
->getCount() > 0 )
171 uno::Reference
< text::XText
> xText( xEndnotes
->getByIndex(0), uno::UNO_QUERY_THROW
);
172 mxViewCursor
->gotoRange( xText
->getStart(), false );
176 DebugHelper::runtimeexception( ERRCODE_BASIC_NO_ACTIVE_OBJECT
);
180 case word::WdSeekView::wdSeekMainDocument
:
182 uno::Reference
< text::XTextDocument
> xTextDocument( mxModel
, uno::UNO_QUERY_THROW
);
183 uno::Reference
< text::XText
> xText
= xTextDocument
->getText();
184 mxViewCursor
->gotoRange( word::getFirstObjectPosition( xText
), false );
191 SwVbaView::getSplitSpecial()
193 return word::WdSpecialPane::wdPaneNone
;
197 SwVbaView::setSplitSpecial( ::sal_Int32
/* _splitspecial */)
199 // not support in Writer
203 SwVbaView::getTableGridLines()
205 bool bShowTableGridLine
= false;
206 mxViewSettings
->getPropertyValue("ShowTableBoundaries") >>= bShowTableGridLine
;
207 return bShowTableGridLine
;
211 SwVbaView::setTableGridLines( sal_Bool _tablegridlines
)
213 mxViewSettings
->setPropertyValue("ShowTableBoundaries", uno::Any( _tablegridlines
) );
219 // FIXME: handle wdPrintPreview type
220 bool bOnlineLayout
= false;
221 mxViewSettings
->getPropertyValue("ShowOnlineLayout") >>= bOnlineLayout
;
222 return bOnlineLayout
? word::WdViewType::wdWebView
: word::WdViewType::wdPrintView
;
226 SwVbaView::setType( ::sal_Int32 _type
)
228 // FIXME: handle wdPrintPreview type
231 case word::WdViewType::wdPrintView
:
232 case word::WdViewType::wdNormalView
:
234 mxViewSettings
->setPropertyValue("ShowOnlineLayout", uno::Any( false ) );
237 case word::WdViewType::wdWebView
:
239 mxViewSettings
->setPropertyValue("ShowOnlineLayout", uno::Any( true ) );
242 case word::WdViewType::wdPrintPreview
:
244 PrintPreviewHelper( uno::Any(),word::getView( mxModel
) );
248 DebugHelper::runtimeexception( ERRCODE_BASIC_NOT_IMPLEMENTED
);
253 uno::Reference
< text::XTextRange
> SwVbaView::getHFTextRange( sal_Int32 nType
)
255 mxModel
->lockControllers();
258 OUString aPropIsShared
;
259 OUString aPropBodyDistance
;
264 case word::WdSeekView::wdSeekCurrentPageFooter
:
265 case word::WdSeekView::wdSeekFirstPageFooter
:
266 case word::WdSeekView::wdSeekPrimaryFooter
:
267 case word::WdSeekView::wdSeekEvenPagesFooter
:
269 aPropIsOn
= "FooterIsOn";
270 aPropIsShared
= "FooterIsShared";
271 aPropBodyDistance
= "FooterBodyDistance";
272 aPropText
= "FooterText";
275 case word::WdSeekView::wdSeekCurrentPageHeader
:
276 case word::WdSeekView::wdSeekFirstPageHeader
:
277 case word::WdSeekView::wdSeekPrimaryHeader
:
278 case word::WdSeekView::wdSeekEvenPagesHeader
:
280 aPropIsOn
= "HeaderIsOn";
281 aPropIsShared
= "HeaderIsShared";
282 aPropBodyDistance
= "HeaderBodyDistance";
283 aPropText
= "HeaderText";
288 uno::Reference
< text::XPageCursor
> xPageCursor( mxViewCursor
, uno::UNO_QUERY_THROW
);
290 if( nType
== word::WdSeekView::wdSeekFirstPageFooter
291 || nType
== word::WdSeekView::wdSeekFirstPageHeader
)
293 xPageCursor
->jumpToFirstPage();
296 uno::Reference
< style::XStyle
> xStyle
;
297 uno::Reference
< text::XText
> xText
;
300 case word::WdSeekView::wdSeekPrimaryFooter
:
301 case word::WdSeekView::wdSeekPrimaryHeader
:
302 case word::WdSeekView::wdSeekEvenPagesFooter
:
303 case word::WdSeekView::wdSeekEvenPagesHeader
:
305 // The primary header is the first header of the section.
306 // If the header is not shared between odd and even pages
307 // the odd page's header is the primary header. If the
308 // first page's header is different from the rest of the
309 // document, it is NOT the primary header ( the next primary
310 // header would be on page 3 )
311 // The even pages' header is only available if the header is
312 // not shared and the current style is applied to a page with
313 // an even page number
314 uno::Reference
< beans::XPropertySet
> xCursorProps( mxViewCursor
, uno::UNO_QUERY_THROW
);
315 OUString aPageStyleName
;
316 xCursorProps
->getPropertyValue("PageStyleName") >>= aPageStyleName
;
317 if ( aPageStyleName
== "First Page" )
319 // go to the beginning of where the next style is used
320 bool hasNextPage
= false;
321 xStyle
= word::getCurrentPageStyle( mxModel
);
324 hasNextPage
= xPageCursor
->jumpToNextPage();
326 while( hasNextPage
&& ( xStyle
== word::getCurrentPageStyle( mxModel
) ) );
329 DebugHelper::basicexception( ERRCODE_BASIC_BAD_ACTION
, {} );
339 xStyle
= word::getCurrentPageStyle( mxModel
);
340 uno::Reference
< beans::XPropertySet
> xPageProps( xStyle
, uno::UNO_QUERY_THROW
);
342 xPageProps
->getPropertyValue( aPropIsOn
) >>= isOn
;
343 bool isShared
= false;
344 xPageProps
->getPropertyValue( aPropIsShared
) >>= isShared
;
347 xPageProps
->setPropertyValue( aPropIsOn
, uno::Any( true ) );
348 xPageProps
->setPropertyValue( aPropBodyDistance
, uno::Any( DEFAULT_BODY_DISTANCE
) );
352 OUString aTempPropText
= aPropText
;
353 if( nType
== word::WdSeekView::wdSeekEvenPagesFooter
354 || nType
== word::WdSeekView::wdSeekEvenPagesHeader
)
356 aTempPropText
+= "Left";
360 aTempPropText
+= "Right";
362 xText
.set( xPageProps
->getPropertyValue( aTempPropText
), uno::UNO_QUERY_THROW
);
366 if( nType
== word::WdSeekView::wdSeekEvenPagesFooter
367 || nType
== word::WdSeekView::wdSeekEvenPagesHeader
)
369 DebugHelper::basicexception( ERRCODE_BASIC_BAD_ACTION
, {} );
371 xText
.set( xPageProps
->getPropertyValue( aPropText
), uno::UNO_QUERY_THROW
);
374 mxModel
->unlockControllers();
377 DebugHelper::basicexception( ERRCODE_BASIC_INTERNAL_ERROR
, {} );
379 uno::Reference
< text::XTextRange
> xTextRange
= word::getFirstObjectPosition( xText
);
384 SwVbaView::getServiceImplName()
389 uno::Sequence
< OUString
>
390 SwVbaView::getServiceNames()
392 static uno::Sequence
< OUString
> const aServiceNames
396 return aServiceNames
;
399 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */