android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / ui / vba / vbabookmarks.cxx
blob6755413796e422f0974d1b75a8f6a14a3b6311a0
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 "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"
28 #include <cppuhelper/implbase.hxx>
29 #include <utility>
31 using namespace ::ooo::vba;
32 using namespace ::com::sun::star;
34 namespace {
36 class BookmarksEnumeration : public EnumerationHelperImpl
38 uno::Reference< frame::XModel > mxModel;
39 public:
40 /// @throws uno::RuntimeException
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 )) {}
43 virtual uno::Any SAL_CALL nextElement( ) override
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 ) ) );
52 // Bookmarks use case-insensitive name lookup in MS Word.
53 class BookmarkCollectionHelper : public ::cppu::WeakImplHelper< container::XNameAccess,
54 container::XIndexAccess >
56 private:
57 uno::Reference< container::XNameAccess > mxNameAccess;
58 uno::Reference< container::XIndexAccess > mxIndexAccess;
59 uno::Any m_cachePos;
60 public:
61 /// @throws uno::RuntimeException
62 explicit BookmarkCollectionHelper( uno::Reference< container::XIndexAccess > xIndexAccess ) : mxIndexAccess(std::move( xIndexAccess ))
64 mxNameAccess.set( mxIndexAccess, uno::UNO_QUERY_THROW );
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
72 if ( !hasByName(aName) )
73 throw container::NoSuchElementException();
74 return m_cachePos;
76 virtual uno::Sequence< OUString > SAL_CALL getElementNames( ) override
78 return mxNameAccess->getElementNames();
80 virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override
82 if( mxNameAccess->hasByName( aName ) )
84 m_cachePos = mxNameAccess->getByName( aName );
85 return true;
87 else
89 for( sal_Int32 nIndex = 0; nIndex < mxIndexAccess->getCount(); nIndex++ )
91 uno::Reference< container::XNamed > xNamed( mxIndexAccess->getByIndex( nIndex ), uno::UNO_QUERY_THROW );
92 OUString aBookmarkName = xNamed->getName();
93 if( aName.equalsIgnoreAsciiCase( aBookmarkName ) )
95 m_cachePos <<= xNamed;
96 return true;
100 return false;
102 // XIndexAccess
103 virtual ::sal_Int32 SAL_CALL getCount( ) override
105 return mxIndexAccess->getCount();
107 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
109 return mxIndexAccess->getByIndex( Index );
115 SwVbaBookmarks::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 ))
117 mxBookmarksSupplier.set( mxModel, uno::UNO_QUERY_THROW );
118 uno::Reference< text::XTextDocument > xDocument( mxModel, uno::UNO_QUERY_THROW );
120 // XEnumerationAccess
121 uno::Type
122 SwVbaBookmarks::getElementType()
124 return cppu::UnoType<word::XBookmark>::get();
126 uno::Reference< container::XEnumeration >
127 SwVbaBookmarks::createEnumeration()
129 uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
130 return new BookmarksEnumeration( getParent(), mxContext,xEnumAccess->createEnumeration(), mxModel );
133 uno::Any
134 SwVbaBookmarks::createCollectionObject( const css::uno::Any& aSource )
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 ) ) );
141 void SwVbaBookmarks::removeBookmarkByName( const OUString& rName )
143 uno::Reference< text::XTextContent > xBookmark( m_xNameAccess->getByName( rName ), uno::UNO_QUERY_THROW );
144 word::getXTextViewCursor( mxModel )->getText()->removeTextContent( xBookmark );
147 void SwVbaBookmarks::addBookmarkByName( const uno::Reference< frame::XModel >& xModel, const OUString& rName, const uno::Reference< text::XTextRange >& rTextRange )
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 );
156 uno::Any SAL_CALL
157 SwVbaBookmarks::Add( const OUString& rName, const uno::Any& rRange )
159 uno::Reference< text::XTextRange > xTextRange;
160 uno::Reference< word::XRange > xRange;
161 if( rRange >>= xRange )
163 SwVbaRange* pRange = dynamic_cast< SwVbaRange* >( xRange.get() );
164 if( pRange )
165 xTextRange = pRange->getXTextRange();
167 else
169 // FIXME: insert the bookmark into current view cursor
170 xTextRange.set( word::getXTextViewCursor( mxModel ), uno::UNO_QUERY_THROW );
173 // remove the exist bookmark
174 if( m_xNameAccess->hasByName( rName ) )
175 removeBookmarkByName( rName );
177 addBookmarkByName( mxModel, rName, xTextRange );
179 return uno::Any( uno::Reference< word::XBookmark >( new SwVbaBookmark( getParent(), mxContext, mxModel, rName ) ) );
182 sal_Int32 SAL_CALL
183 SwVbaBookmarks::getDefaultSorting()
185 return word::WdBookmarkSortBy::wdSortByName;
188 void SAL_CALL
189 SwVbaBookmarks::setDefaultSorting( sal_Int32/* _type*/ )
191 // not support in Writer
194 sal_Bool SAL_CALL
195 SwVbaBookmarks::getShowHidden()
197 return true;
200 void SAL_CALL
201 SwVbaBookmarks::setShowHidden( sal_Bool /*_hidden*/ )
203 // not support in Writer
206 sal_Bool SAL_CALL
207 SwVbaBookmarks::Exists( const OUString& rName )
209 bool bExist = m_xNameAccess->hasByName( rName );
210 return bExist;
213 OUString
214 SwVbaBookmarks::getServiceImplName()
216 return "SwVbaBookmarks";
219 css::uno::Sequence<OUString>
220 SwVbaBookmarks::getServiceNames()
222 static uno::Sequence< OUString > const sNames
224 "ooo.vba.word.Bookmarks"
226 return sNames;
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */