LibreOffice Module sw (master) 1
vbaview.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19#include "vbaview.hxx"
20#include <utility>
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>
37
38#include "wordvbahelper.hxx"
40#include <view.hxx>
41
42using namespace ::ooo::vba;
43using namespace ::com::sun::star;
44
45const sal_Int32 DEFAULT_BODY_DISTANCE = 500;
46
47SwVbaView::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 ))
50{
51 uno::Reference< frame::XController > xController = mxModel->getCurrentController();
52
53 uno::Reference< text::XTextViewCursorSupplier > xTextViewCursorSupp( xController, uno::UNO_QUERY_THROW );
54 mxViewCursor = xTextViewCursorSupp->getViewCursor();
55
56 uno::Reference< view::XViewSettingsSupplier > xViewSettingSupp( xController, uno::UNO_QUERY_THROW );
57 mxViewSettings.set( xViewSettingSupp->getViewSettings(), uno::UNO_SET_THROW );
58}
59
61{
62}
63
65{
66 bool bShowFormattingMarks = false;
67 mxViewSettings->getPropertyValue("ShowNonprintingCharacters") >>= bShowFormattingMarks;
68 return bShowFormattingMarks;
69}
70
72{
73 mxViewSettings->setPropertyValue("ShowNonprintingCharacters", uno::Any(bSet));
74}
75
76::sal_Int32 SAL_CALL
78{
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 )
85 {
86 xCurrentText = xTextContent->getAnchor()->getText();
87 xCursorProps.set( xCurrentText->createTextCursor(), uno::UNO_QUERY_THROW );
88 }
89 uno::Reference< lang::XServiceInfo > xServiceInfo( xCurrentText, uno::UNO_QUERY_THROW );
90 OUString aImplName = xServiceInfo->getImplementationName();
91 if ( aImplName == "SwXBodyText" )
92 {
93 return word::WdSeekView::wdSeekMainDocument;
94 }
95 else if ( aImplName == "SwXHeadFootText" )
96 {
98 {
100 return word::WdSeekView::wdSeekFirstPageHeader;
102 return word::WdSeekView::wdSeekEvenPagesHeader;
103 else
104 return word::WdSeekView::wdSeekPrimaryHeader;
105 }
106 else
107 {
109 return word::WdSeekView::wdSeekFirstPageFooter;
111 return word::WdSeekView::wdSeekEvenPagesFooter;
112 else
113 return word::WdSeekView::wdSeekPrimaryFooter;
114 }
115 }
116 else if ( aImplName == "SwXFootnote" )
117 {
118 if( xServiceInfo->supportsService("com.sun.star.text.Endnote") )
119 return word::WdSeekView::wdSeekEndnotes;
120 else
121 return word::WdSeekView::wdSeekFootnotes;
122 }
123
124 return word::WdSeekView::wdSeekMainDocument;
125}
126
127void SAL_CALL
128SwVbaView::setSeekView( ::sal_Int32 _seekview )
129{
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
133
135 switch( _seekview )
136 {
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:
145 {
146 // need to test
147 mxViewCursor->gotoRange( getHFTextRange( _seekview ), false );
148 break;
149 }
150 case word::WdSeekView::wdSeekFootnotes:
151 {
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 )
155 {
156 uno::Reference< text::XText > xText( xFootnotes->getByIndex(0), uno::UNO_QUERY_THROW );
157 mxViewCursor->gotoRange( xText->getStart(), false );
158 }
159 else
160 {
161 DebugHelper::runtimeexception( ERRCODE_BASIC_NO_ACTIVE_OBJECT );
162 }
163 break;
164 }
165 case word::WdSeekView::wdSeekEndnotes:
166 {
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 )
170 {
171 uno::Reference< text::XText > xText( xEndnotes->getByIndex(0), uno::UNO_QUERY_THROW );
172 mxViewCursor->gotoRange( xText->getStart(), false );
173 }
174 else
175 {
176 DebugHelper::runtimeexception( ERRCODE_BASIC_NO_ACTIVE_OBJECT );
177 }
178 break;
179 }
180 case word::WdSeekView::wdSeekMainDocument:
181 {
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 );
185 break;
186 }
187 }
188}
189
190::sal_Int32 SAL_CALL
192{
193 return word::WdSpecialPane::wdPaneNone;
194}
195
196void SAL_CALL
197SwVbaView::setSplitSpecial( ::sal_Int32/* _splitspecial */)
198{
199 // not support in Writer
200}
201
202sal_Bool SAL_CALL
204{
205 bool bShowTableGridLine = false;
206 mxViewSettings->getPropertyValue("ShowTableBoundaries") >>= bShowTableGridLine;
207 return bShowTableGridLine;
208}
209
210void SAL_CALL
212{
213 mxViewSettings->setPropertyValue("ShowTableBoundaries", uno::Any( _tablegridlines ) );
214}
215
216::sal_Int32 SAL_CALL
218{
219 // FIXME: handle wdPrintPreview type
220 bool bOnlineLayout = false;
221 mxViewSettings->getPropertyValue("ShowOnlineLayout") >>= bOnlineLayout;
222 return bOnlineLayout ? word::WdViewType::wdWebView : word::WdViewType::wdPrintView;
223}
224
225void SAL_CALL
226SwVbaView::setType( ::sal_Int32 _type )
227{
228 // FIXME: handle wdPrintPreview type
229 switch( _type )
230 {
231 case word::WdViewType::wdPrintView:
232 case word::WdViewType::wdNormalView:
233 {
234 mxViewSettings->setPropertyValue("ShowOnlineLayout", uno::Any( false ) );
235 break;
236 }
237 case word::WdViewType::wdWebView:
238 {
239 mxViewSettings->setPropertyValue("ShowOnlineLayout", uno::Any( true ) );
240 break;
241 }
242 case word::WdViewType::wdPrintPreview:
243 {
245 break;
246 }
247 default:
248 DebugHelper::runtimeexception( ERRCODE_BASIC_NOT_IMPLEMENTED );
249
250 }
251}
252
253uno::Reference< text::XTextRange > SwVbaView::getHFTextRange( sal_Int32 nType )
254{
255 mxModel->lockControllers();
256
257 OUString aPropIsOn;
258 OUString aPropIsShared;
259 OUString aPropBodyDistance;
260 OUString aPropText;
261
262 switch( nType )
263 {
264 case word::WdSeekView::wdSeekCurrentPageFooter:
265 case word::WdSeekView::wdSeekFirstPageFooter:
266 case word::WdSeekView::wdSeekPrimaryFooter:
267 case word::WdSeekView::wdSeekEvenPagesFooter:
268 {
269 aPropIsOn = "FooterIsOn";
270 aPropIsShared = "FooterIsShared";
271 aPropBodyDistance = "FooterBodyDistance";
272 aPropText = "FooterText";
273 break;
274 }
275 case word::WdSeekView::wdSeekCurrentPageHeader:
276 case word::WdSeekView::wdSeekFirstPageHeader:
277 case word::WdSeekView::wdSeekPrimaryHeader:
278 case word::WdSeekView::wdSeekEvenPagesHeader:
279 {
280 aPropIsOn = "HeaderIsOn";
281 aPropIsShared = "HeaderIsShared";
282 aPropBodyDistance = "HeaderBodyDistance";
283 aPropText = "HeaderText";
284 break;
285 }
286 }
287
288 uno::Reference< text::XPageCursor > xPageCursor( mxViewCursor, uno::UNO_QUERY_THROW );
289
290 if( nType == word::WdSeekView::wdSeekFirstPageFooter
291 || nType == word::WdSeekView::wdSeekFirstPageHeader )
292 {
293 xPageCursor->jumpToFirstPage();
294 }
295
296 uno::Reference< style::XStyle > xStyle;
297 uno::Reference< text::XText > xText;
298 switch( nType )
299 {
300 case word::WdSeekView::wdSeekPrimaryFooter:
301 case word::WdSeekView::wdSeekPrimaryHeader:
302 case word::WdSeekView::wdSeekEvenPagesFooter:
303 case word::WdSeekView::wdSeekEvenPagesHeader:
304 {
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" )
318 {
319 // go to the beginning of where the next style is used
320 bool hasNextPage = false;
322 do
323 {
324 hasNextPage = xPageCursor->jumpToNextPage();
325 }
326 while( hasNextPage && ( xStyle == word::getCurrentPageStyle( mxModel ) ) );
327
328 if( !hasNextPage )
329 DebugHelper::basicexception( ERRCODE_BASIC_BAD_ACTION, {} );
330 }
331 break;
332 }
333 default:
334 {
335 break;
336 }
337 }
338
340 uno::Reference< beans::XPropertySet > xPageProps( xStyle, uno::UNO_QUERY_THROW );
341 bool isOn = false;
342 xPageProps->getPropertyValue( aPropIsOn ) >>= isOn;
343 bool isShared = false;
344 xPageProps->getPropertyValue( aPropIsShared ) >>= isShared;
345 if( !isOn )
346 {
347 xPageProps->setPropertyValue( aPropIsOn, uno::Any( true ) );
348 xPageProps->setPropertyValue( aPropBodyDistance, uno::Any( DEFAULT_BODY_DISTANCE ) );
349 }
350 if( !isShared )
351 {
352 OUString aTempPropText = aPropText;
353 if( nType == word::WdSeekView::wdSeekEvenPagesFooter
354 || nType == word::WdSeekView::wdSeekEvenPagesHeader )
355 {
356 aTempPropText += "Left";
357 }
358 else
359 {
360 aTempPropText += "Right";
361 }
362 xText.set( xPageProps->getPropertyValue( aTempPropText), uno::UNO_QUERY_THROW );
363 }
364 else
365 {
366 if( nType == word::WdSeekView::wdSeekEvenPagesFooter
367 || nType == word::WdSeekView::wdSeekEvenPagesHeader )
368 {
369 DebugHelper::basicexception( ERRCODE_BASIC_BAD_ACTION, {} );
370 }
371 xText.set( xPageProps->getPropertyValue( aPropText ), uno::UNO_QUERY_THROW );
372 }
373
374 mxModel->unlockControllers();
375 if( !xText.is() )
376 {
377 DebugHelper::basicexception( ERRCODE_BASIC_INTERNAL_ERROR, {} );
378 }
379 uno::Reference< text::XTextRange > xTextRange = word::getFirstObjectPosition( xText );
380 return xTextRange;
381}
382
383OUString
385{
386 return "SwVbaView";
387}
388
389uno::Sequence< OUString >
391{
392 static uno::Sequence< OUString > const aServiceNames
393 {
394 "ooo.vba.word.View"
395 };
396 return aServiceNames;
397}
398
399/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::frame::XModel2 > mxModel
static bool isEvenPagesHeader(const css::uno::Reference< css::frame::XModel > &xModel)
static bool isEvenPagesFooter(const css::uno::Reference< css::frame::XModel > &xModel)
static bool isHeader(const css::uno::Reference< css::frame::XModel > &xModel)
static bool isFirstPageFooter(const css::uno::Reference< css::frame::XModel > &xModel)
static bool isFirstPageHeader(const css::uno::Reference< css::frame::XModel > &xModel)
virtual void SAL_CALL setSplitSpecial(::sal_Int32 _splitspecial) override
Definition: vbaview.cxx:197
css::uno::Reference< css::text::XTextViewCursor > mxViewCursor
Definition: vbaview.hxx:33
virtual ::sal_Int32 SAL_CALL getType() override
Definition: vbaview.cxx:217
css::uno::Reference< css::frame::XModel > mxModel
Definition: vbaview.hxx:32
virtual sal_Bool SAL_CALL getTableGridLines() override
Definition: vbaview.cxx:203
virtual ~SwVbaView() override
Definition: vbaview.cxx:60
css::uno::Reference< css::text::XTextRange > getHFTextRange(sal_Int32 nType)
Definition: vbaview.cxx:253
css::uno::Reference< css::beans::XPropertySet > mxViewSettings
Definition: vbaview.hxx:34
SwVbaView(const css::uno::Reference< ooo::vba::XHelperInterface > &rParent, const css::uno::Reference< css::uno::XComponentContext > &rContext, css::uno::Reference< css::frame::XModel > xModel)
Definition: vbaview.cxx:47
virtual void SAL_CALL setTableGridLines(sal_Bool _tablegridlines) override
Definition: vbaview.cxx:211
virtual void SAL_CALL setType(::sal_Int32 _type) override
Definition: vbaview.cxx:226
virtual css::uno::Sequence< OUString > getServiceNames() override
Definition: vbaview.cxx:390
virtual OUString getServiceImplName() override
Definition: vbaview.cxx:384
virtual void SAL_CALL setSeekView(::sal_Int32 _seekview) override
Definition: vbaview.cxx:128
void SAL_CALL setShowAll(sal_Bool bSet) override
Definition: vbaview.cxx:71
virtual ::sal_Int32 SAL_CALL getSplitSpecial() override
Definition: vbaview.cxx:191
sal_Bool SAL_CALL getShowAll() override
Definition: vbaview.cxx:64
virtual ::sal_Int32 SAL_CALL getSeekView() override
Definition: vbaview.cxx:77
Sequence< OUString > aServiceNames
bool gotoSelectedObjectAnchor(const uno::Reference< frame::XModel > &xModel)
uno::Reference< text::XTextRange > getFirstObjectPosition(const uno::Reference< text::XText > &xText)
uno::Reference< style::XStyle > getCurrentPageStyle(const uno::Reference< frame::XModel > &xModel)
SwView * getView(const uno::Reference< frame::XModel > &xModel)
void PrintPreviewHelper(const css::uno::Any &, SfxViewShell const *pViewShell)
QPRO_FUNC_TYPE nType
#define ERRCODE_BASIC_NO_ACTIVE_OBJECT
#define ERRCODE_BASIC_BAD_ACTION
#define ERRCODE_BASIC_INTERNAL_ERROR
#define ERRCODE_BASIC_NOT_IMPLEMENTED
Reference< XController > xController
Reference< XModel > xModel
unsigned char sal_Bool
const sal_Int32 DEFAULT_BODY_DISTANCE
Definition: vbaview.cxx:45