LibreOffice Module sw (master) 1
vbabookmarks.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 "vbabookmarks.hxx"
20#include "vbabookmark.hxx"
21#include <com/sun/star/container/XNamed.hpp>
22#include <com/sun/star/lang/XMultiServiceFactory.hpp>
23#include <com/sun/star/text/XTextDocument.hpp>
24#include <com/sun/star/text/XTextViewCursor.hpp>
25#include <ooo/vba/word/WdBookmarkSortBy.hpp>
26#include "vbarange.hxx"
27#include "wordvbahelper.hxx"
29#include <utility>
30
31using namespace ::ooo::vba;
32using namespace ::com::sun::star;
33
34namespace {
35
36class BookmarksEnumeration : public EnumerationHelperImpl
37{
38 uno::Reference< frame::XModel > mxModel;
39public:
41 BookmarksEnumeration( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration, uno::Reference< frame::XModel > xModel ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ), mxModel(std::move( xModel )) {}
42
43 virtual uno::Any SAL_CALL nextElement( ) override
44 {
45 uno::Reference< container::XNamed > xNamed( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
46 OUString aName = xNamed->getName();
47 return uno::Any( uno::Reference< word::XBookmark > ( new SwVbaBookmark( m_xParent, m_xContext, mxModel, aName ) ) );
48 }
49
50};
51
52// Bookmarks use case-insensitive name lookup in MS Word.
53class BookmarkCollectionHelper : public ::cppu::WeakImplHelper< container::XNameAccess,
54 container::XIndexAccess >
55{
56private:
57 uno::Reference< container::XNameAccess > mxNameAccess;
58 uno::Reference< container::XIndexAccess > mxIndexAccess;
59 uno::Any m_cachePos;
60public:
62 explicit BookmarkCollectionHelper( uno::Reference< container::XIndexAccess > xIndexAccess ) : mxIndexAccess(std::move( xIndexAccess ))
63 {
64 mxNameAccess.set( mxIndexAccess, uno::UNO_QUERY_THROW );
65 }
66 // XElementAccess
67 virtual uno::Type SAL_CALL getElementType( ) override { return mxIndexAccess->getElementType(); }
68 virtual sal_Bool SAL_CALL hasElements( ) override { return mxIndexAccess->hasElements(); }
69 // XNameAccess
70 virtual uno::Any SAL_CALL getByName( const OUString& aName ) override
71 {
72 if ( !hasByName(aName) )
73 throw container::NoSuchElementException();
74 return m_cachePos;
75 }
76 virtual uno::Sequence< OUString > SAL_CALL getElementNames( ) override
77 {
78 return mxNameAccess->getElementNames();
79 }
80 virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override
81 {
82 if( mxNameAccess->hasByName( aName ) )
83 {
84 m_cachePos = mxNameAccess->getByName( aName );
85 return true;
86 }
87 else
88 {
89 for( sal_Int32 nIndex = 0; nIndex < mxIndexAccess->getCount(); nIndex++ )
90 {
91 uno::Reference< container::XNamed > xNamed( mxIndexAccess->getByIndex( nIndex ), uno::UNO_QUERY_THROW );
92 OUString aBookmarkName = xNamed->getName();
93 if( aName.equalsIgnoreAsciiCase( aBookmarkName ) )
94 {
95 m_cachePos <<= xNamed;
96 return true;
97 }
98 }
99 }
100 return false;
101 }
102 // XIndexAccess
103 virtual ::sal_Int32 SAL_CALL getCount( ) override
104 {
105 return mxIndexAccess->getCount();
106 }
107 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
108 {
109 return mxIndexAccess->getByIndex( Index );
110 }
111};
112
113}
114
115SwVbaBookmarks::SwVbaBookmarks( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< css::uno::XComponentContext > & xContext, const uno::Reference< container::XIndexAccess >& xBookmarks, uno::Reference< frame::XModel > xModel ): SwVbaBookmarks_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( new BookmarkCollectionHelper( xBookmarks ) ) ), mxModel(std::move( xModel ))
116{
117 mxBookmarksSupplier.set( mxModel, uno::UNO_QUERY_THROW );
118 uno::Reference< text::XTextDocument > xDocument( mxModel, uno::UNO_QUERY_THROW );
119}
120// XEnumerationAccess
123{
125}
126uno::Reference< container::XEnumeration >
128{
129 uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
130 return new BookmarksEnumeration( getParent(), mxContext,xEnumAccess->createEnumeration(), mxModel );
131}
132
134SwVbaBookmarks::createCollectionObject( const css::uno::Any& aSource )
135{
136 uno::Reference< container::XNamed > xNamed( aSource, uno::UNO_QUERY_THROW );
137 OUString aName = xNamed->getName();
138 return uno::Any( uno::Reference< word::XBookmark > ( new SwVbaBookmark( getParent(), mxContext, mxModel, aName ) ) );
139}
140
141void SwVbaBookmarks::removeBookmarkByName( const OUString& rName )
142{
143 uno::Reference< text::XTextContent > xBookmark( m_xNameAccess->getByName( rName ), uno::UNO_QUERY_THROW );
144 word::getXTextViewCursor( mxModel )->getText()->removeTextContent( xBookmark );
145}
146
147void SwVbaBookmarks::addBookmarkByName( const uno::Reference< frame::XModel >& xModel, const OUString& rName, const uno::Reference< text::XTextRange >& rTextRange )
148{
149 uno::Reference< lang::XMultiServiceFactory > xDocMSF( xModel, uno::UNO_QUERY_THROW );
150 uno::Reference< text::XTextContent > xBookmark( xDocMSF->createInstance("com.sun.star.text.Bookmark"), uno::UNO_QUERY_THROW );
151 uno::Reference< container::XNamed > xNamed( xBookmark, uno::UNO_QUERY_THROW );
152 xNamed->setName( rName );
153 rTextRange->getText()->insertTextContent( rTextRange, xBookmark, false );
154}
155
156uno::Any SAL_CALL
157SwVbaBookmarks::Add( const OUString& rName, const uno::Any& rRange )
158{
159 uno::Reference< text::XTextRange > xTextRange;
160 uno::Reference< word::XRange > xRange;
161 if( rRange >>= xRange )
162 {
163 SwVbaRange* pRange = dynamic_cast< SwVbaRange* >( xRange.get() );
164 if( pRange )
165 xTextRange = pRange->getXTextRange();
166 }
167 else
168 {
169 // FIXME: insert the bookmark into current view cursor
170 xTextRange.set( word::getXTextViewCursor( mxModel ), uno::UNO_QUERY_THROW );
171 }
172
173 // remove the exist bookmark
174 if( m_xNameAccess->hasByName( rName ) )
175 removeBookmarkByName( rName );
176
177 addBookmarkByName( mxModel, rName, xTextRange );
178
179 return uno::Any( uno::Reference< word::XBookmark >( new SwVbaBookmark( getParent(), mxContext, mxModel, rName ) ) );
180}
181
182sal_Int32 SAL_CALL
184{
185 return word::WdBookmarkSortBy::wdSortByName;
186}
187
188void SAL_CALL
190{
191 // not support in Writer
192}
193
194sal_Bool SAL_CALL
196{
197 return true;
198}
199
200void SAL_CALL
202{
203 // not support in Writer
204}
205
206sal_Bool SAL_CALL
207SwVbaBookmarks::Exists( const OUString& rName )
208{
209 bool bExist = m_xNameAccess->hasByName( rName );
210 return bExist;
211}
212
213OUString
215{
216 return "SwVbaBookmarks";
217}
218
219css::uno::Sequence<OUString>
221{
222 static uno::Sequence< OUString > const sNames
223 {
224 "ooo.vba.word.Bookmarks"
225 };
226 return sNames;
227}
228
229/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
css::uno::Reference< css::frame::XModel2 > mxModel
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual css::uno::Reference< ov::XHelperInterface > SAL_CALL getParent() override
css::uno::Reference< css::container::XNameAccess > m_xNameAccess
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
SwVbaBookmarks(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::container::XIndexAccess > &xBookmarks, css::uno::Reference< css::frame::XModel > xModel)
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
virtual sal_Bool SAL_CALL getShowHidden() override
void removeBookmarkByName(const OUString &rName)
virtual sal_Int32 SAL_CALL getDefaultSorting() override
static void addBookmarkByName(const css::uno::Reference< css::frame::XModel > &xModel, const OUString &rName, const css::uno::Reference< css::text::XTextRange > &rTextRange)
virtual css::uno::Sequence< OUString > getServiceNames() override
virtual css::uno::Type SAL_CALL getElementType() override
css::uno::Reference< css::frame::XModel > mxModel
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
virtual void SAL_CALL setShowHidden(sal_Bool _hidden) override
virtual css::uno::Any SAL_CALL Add(const OUString &rName, const css::uno::Any &rRange) override
virtual void SAL_CALL setDefaultSorting(sal_Int32 _type) override
virtual OUString getServiceImplName() override
css::uno::Reference< css::text::XBookmarksSupplier > mxBookmarksSupplier
virtual sal_Bool SAL_CALL Exists(const OUString &rName) override
virtual css::uno::Reference< css::text::XTextRange > SAL_CALL getXTextRange() override
Definition: vbarange.cxx:85
css::uno::Type const & get()
sal_Int32 nIndex
OUString aName
Reference
uno::Reference< text::XTextViewCursor > getXTextViewCursor(const uno::Reference< frame::XModel > &xModel)
Reference< XModel > xModel
unsigned char sal_Bool