LibreOffice Module sw (master)  1
wordvbahelper.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 <docsh.hxx>
20 #include "wordvbahelper.hxx"
21 #include <com/sun/star/frame/XController.hpp>
22 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
23 #include <com/sun/star/text/XTextTable.hpp>
24 #include <com/sun/star/table/XCellRange.hpp>
25 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
26 #include <com/sun/star/container/XNameAccess.hpp>
27 #include <com/sun/star/container/XIndexAccess.hpp>
28 #include <com/sun/star/lang/XUnoTunnel.hpp>
29 #include <com/sun/star/view/XSelectionSupplier.hpp>
30 #include <unotxdoc.hxx>
31 #include <doc.hxx>
33 #include <view.hxx>
34 #include <viewsh.hxx>
35 
36 using namespace ::com::sun::star;
37 using namespace ::ooo::vba;
38 
39 namespace ooo
40 {
41 namespace vba
42 {
43 namespace word
44 {
45 
46 SwDocShell* getDocShell( const uno::Reference< frame::XModel>& xModel )
47 {
48  uno::Reference< lang::XUnoTunnel > xTunnel( xModel, uno::UNO_QUERY_THROW );
49  SwXTextDocument* pXDoc = reinterpret_cast< SwXTextDocument * >( sal::static_int_cast< sal_IntPtr >(xTunnel->getSomething(SwXTextDocument::getUnoTunnelId())));
50  return pXDoc ? pXDoc->GetDocShell() : nullptr;
51 }
52 
53 SwView* getView( const uno::Reference< frame::XModel>& xModel )
54 {
55  SwDocShell* pDocShell = getDocShell( xModel );
56  return pDocShell? pDocShell->GetView() : nullptr;
57 }
58 
59 uno::Reference< text::XTextViewCursor > getXTextViewCursor( const uno::Reference< frame::XModel >& xModel )
60 {
61  uno::Reference< frame::XController > xController = xModel->getCurrentController();
62  uno::Reference< text::XTextViewCursorSupplier > xTextViewCursorSupp( xController, uno::UNO_QUERY_THROW );
63  uno::Reference< text::XTextViewCursor > xTextViewCursor = xTextViewCursorSupp->getViewCursor();
64  return xTextViewCursor;
65 }
66 
67 uno::Reference< style::XStyle > getCurrentPageStyle( const uno::Reference< frame::XModel >& xModel )
68 {
69  uno::Reference< beans::XPropertySet > xCursorProps( getXTextViewCursor( xModel ), uno::UNO_QUERY_THROW );
70  return getCurrentPageStyle( xModel, xCursorProps );
71 }
72 
73 uno::Reference< style::XStyle > getCurrentPageStyle( const uno::Reference< frame::XModel >& xModel, const uno::Reference< beans::XPropertySet >& xProps )
74 {
75  OUString aPageStyleName;
76  xProps->getPropertyValue("PageStyleName") >>= aPageStyleName;
77  uno::Reference< style::XStyleFamiliesSupplier > xSytleFamSupp( xModel, uno::UNO_QUERY_THROW );
78  uno::Reference< container::XNameAccess > xSytleFamNames( xSytleFamSupp->getStyleFamilies(), uno::UNO_SET_THROW );
79  uno::Reference< container::XNameAccess > xPageStyles( xSytleFamNames->getByName("PageStyles"), uno::UNO_QUERY_THROW );
80  uno::Reference< style::XStyle > xStyle( xPageStyles->getByName( aPageStyleName ), uno::UNO_QUERY_THROW );
81 
82  return xStyle;
83 }
84 
85 sal_Int32 getPageCount( const uno::Reference< frame::XModel>& xModel )
86 {
87  SwDocShell* pDocShell = getDocShell( xModel );
88  SwViewShell* pViewSh = pDocShell ? pDocShell->GetDoc()->getIDocumentLayoutAccess().GetCurrentViewShell() : nullptr;
89  return pViewSh ? pViewSh->GetPageCount() : 0;
90 }
91 
92 uno::Reference< style::XStyle > getDefaultParagraphStyle( const uno::Reference< frame::XModel >& xModel )
93 {
94  uno::Reference< style::XStyleFamiliesSupplier > xSytleFamSupp( xModel, uno::UNO_QUERY_THROW );
95  uno::Reference< container::XNameAccess > xSytleFamNames( xSytleFamSupp->getStyleFamilies(), uno::UNO_SET_THROW );
96  uno::Reference< container::XNameAccess > xParaStyles( xSytleFamNames->getByName("ParagraphStyles"), uno::UNO_QUERY_THROW );
97  uno::Reference< style::XStyle > xStyle( xParaStyles->getByName("Standard"), uno::UNO_QUERY_THROW );
98 
99  return xStyle;
100 }
101 
102 uno::Reference< text::XTextRange > getFirstObjectPosition( const uno::Reference< text::XText >& xText )
103 {
104  // if the first object is table, get the position of first cell
105  uno::Reference< text::XTextRange > xTextRange;
106  uno::Reference< container::XEnumerationAccess > xParaAccess( xText, uno::UNO_QUERY_THROW );
107  uno::Reference< container::XEnumeration> xParaEnum = xParaAccess->createEnumeration();
108  if( xParaEnum->hasMoreElements() )
109  {
110  uno::Reference< lang::XServiceInfo > xServiceInfo( xParaEnum->nextElement(), uno::UNO_QUERY_THROW );
111  if( xServiceInfo->supportsService("com.sun.star.text.TextTable") )
112  {
113  uno::Reference< table::XCellRange > xCellRange( xServiceInfo, uno::UNO_QUERY_THROW );
114  uno::Reference< text::XText> xFirstCellText( xCellRange->getCellByPosition(0, 0), uno::UNO_QUERY_THROW );
115  xTextRange = xFirstCellText->getStart();
116  }
117  }
118  if( !xTextRange.is() )
119  xTextRange = xText->getStart();
120  return xTextRange;
121 }
122 
123 uno::Reference< text::XText > getCurrentXText( const uno::Reference< frame::XModel >& xModel )
124 {
125  uno::Reference< text::XTextRange > xTextRange;
126  uno::Reference< text::XTextContent > xTextContent( xModel->getCurrentSelection(), uno::UNO_QUERY );
127  if( !xTextContent.is() )
128  {
129  uno::Reference< container::XIndexAccess > xIndexAccess( xModel->getCurrentSelection(), uno::UNO_QUERY );
130  if( xIndexAccess.is() )
131  {
132  xTextContent.set( xIndexAccess->getByIndex(0), uno::UNO_QUERY );
133  }
134  }
135 
136  if( xTextContent.is() )
137  xTextRange = xTextContent->getAnchor();
138 
139  if( !xTextRange.is() )
140  xTextRange.set( getXTextViewCursor( xModel ), uno::UNO_QUERY_THROW );
141 
142  uno::Reference< text::XText > xText;
143  try
144  {
145  xText = xTextRange->getText();
146  }
147  catch (const uno::RuntimeException&)
148  {
149  //catch exception "no text selection"
150  }
151  uno::Reference< beans::XPropertySet > xVCProps( xTextRange, uno::UNO_QUERY_THROW );
152  while( xVCProps->getPropertyValue("TextTable") >>= xTextContent )
153  {
154  xText = xTextContent->getAnchor()->getText();
155  xVCProps.set( xText->createTextCursor(), uno::UNO_QUERY_THROW );
156  }
157 
158  if( !xText.is() )
159  throw uno::RuntimeException("no text selection" );
160 
161  return xText;
162 }
163 
164 bool gotoSelectedObjectAnchor( const uno::Reference< frame::XModel>& xModel )
165 {
166  bool isObjectSelected = false;
167  uno::Reference< text::XTextContent > xTextContent( xModel->getCurrentSelection(), uno::UNO_QUERY );
168  if( xTextContent.is() )
169  {
170  uno::Reference< text::XTextRange > xTextRange( xTextContent->getAnchor(), uno::UNO_SET_THROW );
171  uno::Reference< view::XSelectionSupplier > xSelectSupp( xModel->getCurrentController(), uno::UNO_QUERY_THROW );
172  xSelectSupp->select( uno::makeAny( xTextRange ) );
173  isObjectSelected = true;
174  }
175  return isObjectSelected;
176 }
177 
178 } // word
179 }
180 }
181 
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
uno::Reference< style::XStyle > getDefaultParagraphStyle(const uno::Reference< frame::XModel > &xModel)
Reference< XController > xController
uno::Reference< style::XStyle > getCurrentPageStyle(const uno::Reference< frame::XModel > &xModel)
uno::Reference< text::XTextViewCursor > getXTextViewCursor(const uno::Reference< frame::XModel > &xModel)
static const css::uno::Sequence< sal_Int8 > & getUnoTunnelId()
Definition: unotxdoc.cxx:294
SwDoc * GetDoc()
returns Doc. But be careful!
Definition: docsh.hxx:203
SwView * getView(const uno::Reference< frame::XModel > &xModel)
bool gotoSelectedObjectAnchor(const uno::Reference< frame::XModel > &xModel)
sal_uInt16 GetPageCount() const
Definition: viewsh.cxx:2499
SwDocShell * GetDocShell()
Definition: unotxdoc.hxx:464
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:437
sal_Int32 getPageCount(const uno::Reference< frame::XModel > &xModel)
SwDocShell * getDocShell(const uno::Reference< frame::XModel > &xModel)
virtual const SwViewShell * GetCurrentViewShell() const =0
Returns the layout set at the document.
uno::Reference< text::XText > getCurrentXText(const uno::Reference< frame::XModel > &xModel)
uno::Reference< text::XTextRange > getFirstObjectPosition(const uno::Reference< text::XText > &xText)
const SwView * GetView() const
Definition: docsh.hxx:220
Definition: view.hxx:146