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
64::sal_Int32 SAL_CALL
66{
67 // FIXME: if the view cursor is in table, field, section and frame
68 // handle if the cursor is in table
69 uno::Reference< text::XText > xCurrentText = mxViewCursor->getText();
70 uno::Reference< beans::XPropertySet > xCursorProps( mxViewCursor, uno::UNO_QUERY_THROW );
71 uno::Reference< text::XTextContent > xTextContent;
72 while( xCursorProps->getPropertyValue("TextTable") >>= xTextContent )
73 {
74 xCurrentText = xTextContent->getAnchor()->getText();
75 xCursorProps.set( xCurrentText->createTextCursor(), uno::UNO_QUERY_THROW );
76 }
77 uno::Reference< lang::XServiceInfo > xServiceInfo( xCurrentText, uno::UNO_QUERY_THROW );
78 OUString aImplName = xServiceInfo->getImplementationName();
79 if ( aImplName == "SwXBodyText" )
80 {
81 return word::WdSeekView::wdSeekMainDocument;
82 }
83 else if ( aImplName == "SwXHeadFootText" )
84 {
86 {
88 return word::WdSeekView::wdSeekFirstPageHeader;
90 return word::WdSeekView::wdSeekEvenPagesHeader;
91 else
92 return word::WdSeekView::wdSeekPrimaryHeader;
93 }
94 else
95 {
97 return word::WdSeekView::wdSeekFirstPageFooter;
99 return word::WdSeekView::wdSeekEvenPagesFooter;
100 else
101 return word::WdSeekView::wdSeekPrimaryFooter;
102 }
103 }
104 else if ( aImplName == "SwXFootnote" )
105 {
106 if( xServiceInfo->supportsService("com.sun.star.text.Endnote") )
107 return word::WdSeekView::wdSeekEndnotes;
108 else
109 return word::WdSeekView::wdSeekFootnotes;
110 }
111
112 return word::WdSeekView::wdSeekMainDocument;
113}
114
115void SAL_CALL
116SwVbaView::setSeekView( ::sal_Int32 _seekview )
117{
118 // FIXME: save the current cursor position, if the cursor is in the main
119 // document, so we can jump back to this position, if the macro sets
120 // the ViewMode back to wdSeekMainDocument
121
123 switch( _seekview )
124 {
125 case word::WdSeekView::wdSeekFirstPageFooter:
126 case word::WdSeekView::wdSeekFirstPageHeader:
127 case word::WdSeekView::wdSeekCurrentPageFooter:
128 case word::WdSeekView::wdSeekCurrentPageHeader:
129 case word::WdSeekView::wdSeekPrimaryFooter:
130 case word::WdSeekView::wdSeekPrimaryHeader:
131 case word::WdSeekView::wdSeekEvenPagesFooter:
132 case word::WdSeekView::wdSeekEvenPagesHeader:
133 {
134 // need to test
135 mxViewCursor->gotoRange( getHFTextRange( _seekview ), false );
136 break;
137 }
138 case word::WdSeekView::wdSeekFootnotes:
139 {
140 uno::Reference< text::XFootnotesSupplier > xFootnotesSupp( mxModel, uno::UNO_QUERY_THROW );
141 uno::Reference< container::XIndexAccess > xFootnotes( xFootnotesSupp->getFootnotes(), uno::UNO_SET_THROW );
142 if( xFootnotes->getCount() > 0 )
143 {
144 uno::Reference< text::XText > xText( xFootnotes->getByIndex(0), uno::UNO_QUERY_THROW );
145 mxViewCursor->gotoRange( xText->getStart(), false );
146 }
147 else
148 {
149 DebugHelper::runtimeexception( ERRCODE_BASIC_NO_ACTIVE_OBJECT );
150 }
151 break;
152 }
153 case word::WdSeekView::wdSeekEndnotes:
154 {
155 uno::Reference< text::XEndnotesSupplier > xEndnotesSupp( mxModel, uno::UNO_QUERY_THROW );
156 uno::Reference< container::XIndexAccess > xEndnotes( xEndnotesSupp->getEndnotes(), uno::UNO_SET_THROW );
157 if( xEndnotes->getCount() > 0 )
158 {
159 uno::Reference< text::XText > xText( xEndnotes->getByIndex(0), uno::UNO_QUERY_THROW );
160 mxViewCursor->gotoRange( xText->getStart(), false );
161 }
162 else
163 {
164 DebugHelper::runtimeexception( ERRCODE_BASIC_NO_ACTIVE_OBJECT );
165 }
166 break;
167 }
168 case word::WdSeekView::wdSeekMainDocument:
169 {
170 uno::Reference< text::XTextDocument > xTextDocument( mxModel, uno::UNO_QUERY_THROW );
171 uno::Reference< text::XText > xText = xTextDocument->getText();
172 mxViewCursor->gotoRange( word::getFirstObjectPosition( xText ), false );
173 break;
174 }
175 }
176}
177
178::sal_Int32 SAL_CALL
180{
181 return word::WdSpecialPane::wdPaneNone;
182}
183
184void SAL_CALL
185SwVbaView::setSplitSpecial( ::sal_Int32/* _splitspecial */)
186{
187 // not support in Writer
188}
189
190sal_Bool SAL_CALL
192{
193 bool bShowTableGridLine = false;
194 mxViewSettings->getPropertyValue("ShowTableBoundaries") >>= bShowTableGridLine;
195 return bShowTableGridLine;
196}
197
198void SAL_CALL
200{
201 mxViewSettings->setPropertyValue("ShowTableBoundaries", uno::Any( _tablegridlines ) );
202}
203
204::sal_Int32 SAL_CALL
206{
207 // FIXME: handle wdPrintPreview type
208 bool bOnlineLayout = false;
209 mxViewSettings->getPropertyValue("ShowOnlineLayout") >>= bOnlineLayout;
210 return bOnlineLayout ? word::WdViewType::wdWebView : word::WdViewType::wdPrintView;
211}
212
213void SAL_CALL
214SwVbaView::setType( ::sal_Int32 _type )
215{
216 // FIXME: handle wdPrintPreview type
217 switch( _type )
218 {
219 case word::WdViewType::wdPrintView:
220 case word::WdViewType::wdNormalView:
221 {
222 mxViewSettings->setPropertyValue("ShowOnlineLayout", uno::Any( false ) );
223 break;
224 }
225 case word::WdViewType::wdWebView:
226 {
227 mxViewSettings->setPropertyValue("ShowOnlineLayout", uno::Any( true ) );
228 break;
229 }
230 case word::WdViewType::wdPrintPreview:
231 {
233 break;
234 }
235 default:
236 DebugHelper::runtimeexception( ERRCODE_BASIC_NOT_IMPLEMENTED );
237
238 }
239}
240
241uno::Reference< text::XTextRange > SwVbaView::getHFTextRange( sal_Int32 nType )
242{
243 mxModel->lockControllers();
244
245 OUString aPropIsOn;
246 OUString aPropIsShared;
247 OUString aPropBodyDistance;
248 OUString aPropText;
249
250 switch( nType )
251 {
252 case word::WdSeekView::wdSeekCurrentPageFooter:
253 case word::WdSeekView::wdSeekFirstPageFooter:
254 case word::WdSeekView::wdSeekPrimaryFooter:
255 case word::WdSeekView::wdSeekEvenPagesFooter:
256 {
257 aPropIsOn = "FooterIsOn";
258 aPropIsShared = "FooterIsShared";
259 aPropBodyDistance = "FooterBodyDistance";
260 aPropText = "FooterText";
261 break;
262 }
263 case word::WdSeekView::wdSeekCurrentPageHeader:
264 case word::WdSeekView::wdSeekFirstPageHeader:
265 case word::WdSeekView::wdSeekPrimaryHeader:
266 case word::WdSeekView::wdSeekEvenPagesHeader:
267 {
268 aPropIsOn = "HeaderIsOn";
269 aPropIsShared = "HeaderIsShared";
270 aPropBodyDistance = "HeaderBodyDistance";
271 aPropText = "HeaderText";
272 break;
273 }
274 }
275
276 uno::Reference< text::XPageCursor > xPageCursor( mxViewCursor, uno::UNO_QUERY_THROW );
277
278 if( nType == word::WdSeekView::wdSeekFirstPageFooter
279 || nType == word::WdSeekView::wdSeekFirstPageHeader )
280 {
281 xPageCursor->jumpToFirstPage();
282 }
283
284 uno::Reference< style::XStyle > xStyle;
285 uno::Reference< text::XText > xText;
286 switch( nType )
287 {
288 case word::WdSeekView::wdSeekPrimaryFooter:
289 case word::WdSeekView::wdSeekPrimaryHeader:
290 case word::WdSeekView::wdSeekEvenPagesFooter:
291 case word::WdSeekView::wdSeekEvenPagesHeader:
292 {
293 // The primary header is the first header of the section.
294 // If the header is not shared between odd and even pages
295 // the odd page's header is the primary header. If the
296 // first page's header is different from the rest of the
297 // document, it is NOT the primary header ( the next primary
298 // header would be on page 3 )
299 // The even pages' header is only available if the header is
300 // not shared and the current style is applied to a page with
301 // an even page number
302 uno::Reference< beans::XPropertySet > xCursorProps( mxViewCursor, uno::UNO_QUERY_THROW );
303 OUString aPageStyleName;
304 xCursorProps->getPropertyValue("PageStyleName") >>= aPageStyleName;
305 if ( aPageStyleName == "First Page" )
306 {
307 // go to the beginning of where the next style is used
308 bool hasNextPage = false;
310 do
311 {
312 hasNextPage = xPageCursor->jumpToNextPage();
313 }
314 while( hasNextPage && ( xStyle == word::getCurrentPageStyle( mxModel ) ) );
315
316 if( !hasNextPage )
317 DebugHelper::basicexception( ERRCODE_BASIC_BAD_ACTION, {} );
318 }
319 break;
320 }
321 default:
322 {
323 break;
324 }
325 }
326
328 uno::Reference< beans::XPropertySet > xPageProps( xStyle, uno::UNO_QUERY_THROW );
329 bool isOn = false;
330 xPageProps->getPropertyValue( aPropIsOn ) >>= isOn;
331 bool isShared = false;
332 xPageProps->getPropertyValue( aPropIsShared ) >>= isShared;
333 if( !isOn )
334 {
335 xPageProps->setPropertyValue( aPropIsOn, uno::Any( true ) );
336 xPageProps->setPropertyValue( aPropBodyDistance, uno::Any( DEFAULT_BODY_DISTANCE ) );
337 }
338 if( !isShared )
339 {
340 OUString aTempPropText = aPropText;
341 if( nType == word::WdSeekView::wdSeekEvenPagesFooter
342 || nType == word::WdSeekView::wdSeekEvenPagesHeader )
343 {
344 aTempPropText += "Left";
345 }
346 else
347 {
348 aTempPropText += "Right";
349 }
350 xText.set( xPageProps->getPropertyValue( aTempPropText), uno::UNO_QUERY_THROW );
351 }
352 else
353 {
354 if( nType == word::WdSeekView::wdSeekEvenPagesFooter
355 || nType == word::WdSeekView::wdSeekEvenPagesHeader )
356 {
357 DebugHelper::basicexception( ERRCODE_BASIC_BAD_ACTION, {} );
358 }
359 xText.set( xPageProps->getPropertyValue( aPropText ), uno::UNO_QUERY_THROW );
360 }
361
362 mxModel->unlockControllers();
363 if( !xText.is() )
364 {
365 DebugHelper::basicexception( ERRCODE_BASIC_INTERNAL_ERROR, {} );
366 }
367 uno::Reference< text::XTextRange > xTextRange = word::getFirstObjectPosition( xText );
368 return xTextRange;
369}
370
371OUString
373{
374 return "SwVbaView";
375}
376
377uno::Sequence< OUString >
379{
380 static uno::Sequence< OUString > const aServiceNames
381 {
382 "ooo.vba.word.View"
383 };
384 return aServiceNames;
385}
386
387/* 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:185
css::uno::Reference< css::text::XTextViewCursor > mxViewCursor
Definition: vbaview.hxx:33
virtual ::sal_Int32 SAL_CALL getType() override
Definition: vbaview.cxx:205
css::uno::Reference< css::frame::XModel > mxModel
Definition: vbaview.hxx:32
virtual sal_Bool SAL_CALL getTableGridLines() override
Definition: vbaview.cxx:191
virtual ~SwVbaView() override
Definition: vbaview.cxx:60
css::uno::Reference< css::text::XTextRange > getHFTextRange(sal_Int32 nType)
Definition: vbaview.cxx:241
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:199
virtual void SAL_CALL setType(::sal_Int32 _type) override
Definition: vbaview.cxx:214
virtual css::uno::Sequence< OUString > getServiceNames() override
Definition: vbaview.cxx:378
virtual OUString getServiceImplName() override
Definition: vbaview.cxx:372
virtual void SAL_CALL setSeekView(::sal_Int32 _seekview) override
Definition: vbaview.cxx:116
virtual ::sal_Int32 SAL_CALL getSplitSpecial() override
Definition: vbaview.cxx:179
virtual ::sal_Int32 SAL_CALL getSeekView() override
Definition: vbaview.cxx:65
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