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 .
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/table/XCellRange.hpp>
24 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
25 #include <com/sun/star/container/XNameAccess.hpp>
26 #include <com/sun/star/container/XIndexAccess.hpp>
27 #include <com/sun/star/lang/XUnoTunnel.hpp>
28 #include <com/sun/star/view/XSelectionSupplier.hpp>
29 #include <unotxdoc.hxx>
31 #include <IDocumentLayoutAccess.hxx>
34 #include <comphelper/servicehelper.hxx>
36 using namespace ::com::sun::star
;
37 using namespace ::ooo::vba
;
39 namespace ooo::vba::word
42 SwDocShell
* getDocShell( const uno::Reference
< frame::XModel
>& xModel
)
44 uno::Reference
< lang::XUnoTunnel
> xTunnel( xModel
, uno::UNO_QUERY_THROW
);
45 SwXTextDocument
* pXDoc
= comphelper::getFromUnoTunnel
<SwXTextDocument
>(xTunnel
);
46 return pXDoc
? pXDoc
->GetDocShell() : nullptr;
49 SwView
* getView( const uno::Reference
< frame::XModel
>& xModel
)
51 SwDocShell
* pDocShell
= getDocShell( xModel
);
52 return pDocShell
? pDocShell
->GetView() : nullptr;
55 uno::Reference
< text::XTextViewCursor
> getXTextViewCursor( const uno::Reference
< frame::XModel
>& xModel
)
57 uno::Reference
< frame::XController
> xController
= xModel
->getCurrentController();
58 uno::Reference
< text::XTextViewCursorSupplier
> xTextViewCursorSupp( xController
, uno::UNO_QUERY_THROW
);
59 uno::Reference
< text::XTextViewCursor
> xTextViewCursor
= xTextViewCursorSupp
->getViewCursor();
60 return xTextViewCursor
;
63 uno::Reference
< style::XStyle
> getCurrentPageStyle( const uno::Reference
< frame::XModel
>& xModel
)
65 uno::Reference
< beans::XPropertySet
> xCursorProps( getXTextViewCursor( xModel
), uno::UNO_QUERY_THROW
);
66 return getCurrentPageStyle( xModel
, xCursorProps
);
69 uno::Reference
< style::XStyle
> getCurrentPageStyle( const uno::Reference
< frame::XModel
>& xModel
, const uno::Reference
< beans::XPropertySet
>& xProps
)
71 OUString aPageStyleName
;
72 xProps
->getPropertyValue("PageStyleName") >>= aPageStyleName
;
73 uno::Reference
< style::XStyleFamiliesSupplier
> xSytleFamSupp( xModel
, uno::UNO_QUERY_THROW
);
74 uno::Reference
< container::XNameAccess
> xSytleFamNames( xSytleFamSupp
->getStyleFamilies(), uno::UNO_SET_THROW
);
75 uno::Reference
< container::XNameAccess
> xPageStyles( xSytleFamNames
->getByName("PageStyles"), uno::UNO_QUERY_THROW
);
76 uno::Reference
< style::XStyle
> xStyle( xPageStyles
->getByName( aPageStyleName
), uno::UNO_QUERY_THROW
);
81 sal_Int32
getPageCount( const uno::Reference
< frame::XModel
>& xModel
)
83 SwDocShell
* pDocShell
= getDocShell( xModel
);
84 SwViewShell
* pViewSh
= pDocShell
? pDocShell
->GetDoc()->getIDocumentLayoutAccess().GetCurrentViewShell() : nullptr;
85 return pViewSh
? pViewSh
->GetPageCount() : 0;
88 uno::Reference
< style::XStyle
> getDefaultParagraphStyle( const uno::Reference
< frame::XModel
>& xModel
)
90 uno::Reference
< style::XStyleFamiliesSupplier
> xSytleFamSupp( xModel
, uno::UNO_QUERY_THROW
);
91 uno::Reference
< container::XNameAccess
> xSytleFamNames( xSytleFamSupp
->getStyleFamilies(), uno::UNO_SET_THROW
);
92 uno::Reference
< container::XNameAccess
> xParaStyles( xSytleFamNames
->getByName("ParagraphStyles"), uno::UNO_QUERY_THROW
);
93 uno::Reference
< style::XStyle
> xStyle( xParaStyles
->getByName("Standard"), uno::UNO_QUERY_THROW
);
98 uno::Reference
< text::XTextRange
> getFirstObjectPosition( const uno::Reference
< text::XText
>& xText
)
100 // if the first object is table, get the position of first cell
101 uno::Reference
< text::XTextRange
> xTextRange
;
102 uno::Reference
< container::XEnumerationAccess
> xParaAccess( xText
, uno::UNO_QUERY_THROW
);
103 uno::Reference
< container::XEnumeration
> xParaEnum
= xParaAccess
->createEnumeration();
104 if( xParaEnum
->hasMoreElements() )
106 uno::Reference
< lang::XServiceInfo
> xServiceInfo( xParaEnum
->nextElement(), uno::UNO_QUERY_THROW
);
107 if( xServiceInfo
->supportsService("com.sun.star.text.TextTable") )
109 uno::Reference
< table::XCellRange
> xCellRange( xServiceInfo
, uno::UNO_QUERY_THROW
);
110 uno::Reference
< text::XText
> xFirstCellText( xCellRange
->getCellByPosition(0, 0), uno::UNO_QUERY_THROW
);
111 xTextRange
= xFirstCellText
->getStart();
114 if( !xTextRange
.is() )
115 xTextRange
= xText
->getStart();
119 uno::Reference
< text::XText
> getCurrentXText( const uno::Reference
< frame::XModel
>& xModel
)
121 uno::Reference
< text::XTextRange
> xTextRange
;
122 uno::Reference
< text::XTextContent
> xTextContent( xModel
->getCurrentSelection(), uno::UNO_QUERY
);
123 if( !xTextContent
.is() )
125 uno::Reference
< container::XIndexAccess
> xIndexAccess( xModel
->getCurrentSelection(), uno::UNO_QUERY
);
126 if( xIndexAccess
.is() )
128 xTextContent
.set( xIndexAccess
->getByIndex(0), uno::UNO_QUERY
);
132 if( xTextContent
.is() )
133 xTextRange
= xTextContent
->getAnchor();
135 if( !xTextRange
.is() )
136 xTextRange
.set( getXTextViewCursor( xModel
), uno::UNO_QUERY_THROW
);
138 uno::Reference
< text::XText
> xText
;
141 xText
= xTextRange
->getText();
143 catch (const uno::RuntimeException
&)
145 //catch exception "no text selection"
147 uno::Reference
< beans::XPropertySet
> xVCProps( xTextRange
, uno::UNO_QUERY_THROW
);
148 while( xVCProps
->getPropertyValue("TextTable") >>= xTextContent
)
150 xText
= xTextContent
->getAnchor()->getText();
151 xVCProps
.set( xText
->createTextCursor(), uno::UNO_QUERY_THROW
);
155 throw uno::RuntimeException("no text selection" );
160 bool gotoSelectedObjectAnchor( const uno::Reference
< frame::XModel
>& xModel
)
162 bool isObjectSelected
= false;
163 uno::Reference
< text::XTextContent
> xTextContent( xModel
->getCurrentSelection(), uno::UNO_QUERY
);
164 if( xTextContent
.is() )
166 uno::Reference
< text::XTextRange
> xTextRange( xTextContent
->getAnchor(), uno::UNO_SET_THROW
);
167 uno::Reference
< view::XSelectionSupplier
> xSelectSupp( xModel
->getCurrentController(), uno::UNO_QUERY_THROW
);
168 xSelectSupp
->select( uno::Any( xTextRange
) );
169 isObjectSelected
= true;
171 return isObjectSelected
;
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */