android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / ui / vba / vbaview.cxx
blob822c4f93e50169b1d3f3de5fb866db40e3e70f9d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
20 #include <utility>
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"
40 #include <view.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));
76 ::sal_Int32 SAL_CALL
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;
103 else
104 return word::WdSeekView::wdSeekPrimaryHeader;
106 else
108 if( HeaderFooterHelper::isFirstPageFooter( mxModel ) )
109 return word::WdSeekView::wdSeekFirstPageFooter;
110 else if( HeaderFooterHelper::isEvenPagesFooter( mxModel ) )
111 return word::WdSeekView::wdSeekEvenPagesFooter;
112 else
113 return word::WdSeekView::wdSeekPrimaryFooter;
116 else if ( aImplName == "SwXFootnote" )
118 if( xServiceInfo->supportsService("com.sun.star.text.Endnote") )
119 return word::WdSeekView::wdSeekEndnotes;
120 else
121 return word::WdSeekView::wdSeekFootnotes;
124 return word::WdSeekView::wdSeekMainDocument;
127 void SAL_CALL
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 );
135 switch( _seekview )
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:
146 // need to test
147 mxViewCursor->gotoRange( getHFTextRange( _seekview ), false );
148 break;
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 );
159 else
161 DebugHelper::runtimeexception( ERRCODE_BASIC_NO_ACTIVE_OBJECT );
163 break;
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 );
174 else
176 DebugHelper::runtimeexception( ERRCODE_BASIC_NO_ACTIVE_OBJECT );
178 break;
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 );
185 break;
190 ::sal_Int32 SAL_CALL
191 SwVbaView::getSplitSpecial()
193 return word::WdSpecialPane::wdPaneNone;
196 void SAL_CALL
197 SwVbaView::setSplitSpecial( ::sal_Int32/* _splitspecial */)
199 // not support in Writer
202 sal_Bool SAL_CALL
203 SwVbaView::getTableGridLines()
205 bool bShowTableGridLine = false;
206 mxViewSettings->getPropertyValue("ShowTableBoundaries") >>= bShowTableGridLine;
207 return bShowTableGridLine;
210 void SAL_CALL
211 SwVbaView::setTableGridLines( sal_Bool _tablegridlines )
213 mxViewSettings->setPropertyValue("ShowTableBoundaries", uno::Any( _tablegridlines ) );
216 ::sal_Int32 SAL_CALL
217 SwVbaView::getType()
219 // FIXME: handle wdPrintPreview type
220 bool bOnlineLayout = false;
221 mxViewSettings->getPropertyValue("ShowOnlineLayout") >>= bOnlineLayout;
222 return bOnlineLayout ? word::WdViewType::wdWebView : word::WdViewType::wdPrintView;
225 void SAL_CALL
226 SwVbaView::setType( ::sal_Int32 _type )
228 // FIXME: handle wdPrintPreview type
229 switch( _type )
231 case word::WdViewType::wdPrintView:
232 case word::WdViewType::wdNormalView:
234 mxViewSettings->setPropertyValue("ShowOnlineLayout", uno::Any( false ) );
235 break;
237 case word::WdViewType::wdWebView:
239 mxViewSettings->setPropertyValue("ShowOnlineLayout", uno::Any( true ) );
240 break;
242 case word::WdViewType::wdPrintPreview:
244 PrintPreviewHelper( uno::Any(),word::getView( mxModel ) );
245 break;
247 default:
248 DebugHelper::runtimeexception( ERRCODE_BASIC_NOT_IMPLEMENTED );
253 uno::Reference< text::XTextRange > SwVbaView::getHFTextRange( sal_Int32 nType )
255 mxModel->lockControllers();
257 OUString aPropIsOn;
258 OUString aPropIsShared;
259 OUString aPropBodyDistance;
260 OUString aPropText;
262 switch( nType )
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";
273 break;
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";
284 break;
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;
298 switch( nType )
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 ) ) );
328 if( !hasNextPage )
329 DebugHelper::basicexception( ERRCODE_BASIC_BAD_ACTION, {} );
331 break;
333 default:
335 break;
339 xStyle = word::getCurrentPageStyle( mxModel );
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 )
347 xPageProps->setPropertyValue( aPropIsOn, uno::Any( true ) );
348 xPageProps->setPropertyValue( aPropBodyDistance, uno::Any( DEFAULT_BODY_DISTANCE ) );
350 if( !isShared )
352 OUString aTempPropText = aPropText;
353 if( nType == word::WdSeekView::wdSeekEvenPagesFooter
354 || nType == word::WdSeekView::wdSeekEvenPagesHeader )
356 aTempPropText += "Left";
358 else
360 aTempPropText += "Right";
362 xText.set( xPageProps->getPropertyValue( aTempPropText), uno::UNO_QUERY_THROW );
364 else
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();
375 if( !xText.is() )
377 DebugHelper::basicexception( ERRCODE_BASIC_INTERNAL_ERROR, {} );
379 uno::Reference< text::XTextRange > xTextRange = word::getFirstObjectPosition( xText );
380 return xTextRange;
383 OUString
384 SwVbaView::getServiceImplName()
386 return "SwVbaView";
389 uno::Sequence< OUString >
390 SwVbaView::getServiceNames()
392 static uno::Sequence< OUString > const aServiceNames
394 "ooo.vba.word.View"
396 return aServiceNames;
399 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */