xref: /aoo42x/main/sw/source/ui/vba/vbabookmarks.cxx (revision efeef26f)
1*efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*efeef26fSAndrew Rist  * distributed with this work for additional information
6*efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9*efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*efeef26fSAndrew Rist  *
11*efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*efeef26fSAndrew Rist  *
13*efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15*efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*efeef26fSAndrew Rist  * specific language governing permissions and limitations
18*efeef26fSAndrew Rist  * under the License.
19*efeef26fSAndrew Rist  *
20*efeef26fSAndrew Rist  *************************************************************/
21*efeef26fSAndrew Rist 
22*efeef26fSAndrew Rist 
23cdf0e10cSrcweir #include "vbabookmarks.hxx"
24cdf0e10cSrcweir #include "vbabookmark.hxx"
25cdf0e10cSrcweir #include <com/sun/star/container/XNamed.hpp>
26cdf0e10cSrcweir #include <com/sun/star/text/XTextDocument.hpp>
27cdf0e10cSrcweir #include <com/sun/star/text/XTextViewCursor.hpp>
28cdf0e10cSrcweir #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
29cdf0e10cSrcweir #include <ooo/vba/word/WdBookmarkSortBy.hpp>
30cdf0e10cSrcweir #include "vbarange.hxx"
31cdf0e10cSrcweir #include "wordvbahelper.hxx"
32cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir using namespace ::ooo::vba;
35cdf0e10cSrcweir using namespace ::com::sun::star;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir class BookmarksEnumeration : public EnumerationHelperImpl
38cdf0e10cSrcweir {
39cdf0e10cSrcweir 	uno::Reference< frame::XModel > mxModel;
40cdf0e10cSrcweir public:
BookmarksEnumeration(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<container::XEnumeration> & xEnumeration,const uno::Reference<frame::XModel> & xModel)41cdf0e10cSrcweir 	BookmarksEnumeration( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration,  const uno::Reference< frame::XModel >& xModel  ) throw ( uno::RuntimeException ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ), mxModel( xModel ) {}
42cdf0e10cSrcweir 
nextElement()43cdf0e10cSrcweir 	virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
44cdf0e10cSrcweir 	{
45cdf0e10cSrcweir 		uno::Reference< container::XNamed > xNamed( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
46cdf0e10cSrcweir         rtl::OUString aName = xNamed->getName();
47cdf0e10cSrcweir 		return uno::makeAny( uno::Reference< word::XBookmark > ( new SwVbaBookmark( m_xParent, m_xContext, mxModel, aName ) ) );
48cdf0e10cSrcweir 	}
49cdf0e10cSrcweir 
50cdf0e10cSrcweir };
51cdf0e10cSrcweir 
52cdf0e10cSrcweir // Bookmarks use case-insensitive name lookup in MS Word.
53cdf0e10cSrcweir typedef ::cppu::WeakImplHelper2< container::XNameAccess, container::XIndexAccess > BookmarkCollectionHelper_BASE;
54cdf0e10cSrcweir class BookmarkCollectionHelper : public BookmarkCollectionHelper_BASE
55cdf0e10cSrcweir {
56cdf0e10cSrcweir private:
57cdf0e10cSrcweir     uno::Reference< container::XNameAccess > mxNameAccess;
58cdf0e10cSrcweir     uno::Reference< container::XIndexAccess > mxIndexAccess;
59cdf0e10cSrcweir     uno::Any cachePos;
60cdf0e10cSrcweir public:
BookmarkCollectionHelper(const uno::Reference<container::XIndexAccess> & xIndexAccess)61cdf0e10cSrcweir     BookmarkCollectionHelper( const uno::Reference< container::XIndexAccess >& xIndexAccess ) throw (uno::RuntimeException) : mxIndexAccess( xIndexAccess )
62cdf0e10cSrcweir     {
63cdf0e10cSrcweir         mxNameAccess.set( mxIndexAccess, uno::UNO_QUERY_THROW );
64cdf0e10cSrcweir     }
65cdf0e10cSrcweir 	// XElementAccess
getElementType()66cdf0e10cSrcweir 	virtual uno::Type SAL_CALL getElementType(  ) throw (uno::RuntimeException) { return  mxIndexAccess->getElementType(); }
hasElements()67cdf0e10cSrcweir 	virtual ::sal_Bool SAL_CALL hasElements(  ) throw (uno::RuntimeException) { return mxIndexAccess->hasElements(); }
68cdf0e10cSrcweir 	// XNameAcess
getByName(const::rtl::OUString & aName)69cdf0e10cSrcweir 	virtual uno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir 		if ( !hasByName(aName) )
72cdf0e10cSrcweir 			throw container::NoSuchElementException();
73cdf0e10cSrcweir 		return cachePos;
74cdf0e10cSrcweir     }
getElementNames()75cdf0e10cSrcweir 	virtual uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames(  ) throw (uno::RuntimeException)
76cdf0e10cSrcweir 	{
77cdf0e10cSrcweir         return mxNameAccess->getElementNames();
78cdf0e10cSrcweir     }
hasByName(const::rtl::OUString & aName)79cdf0e10cSrcweir 	virtual ::sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw (uno::RuntimeException)
80cdf0e10cSrcweir 	{
81cdf0e10cSrcweir         if( mxNameAccess->hasByName( aName ) )
82cdf0e10cSrcweir         {
83cdf0e10cSrcweir             cachePos = mxNameAccess->getByName( aName );
84cdf0e10cSrcweir             return sal_True;
85cdf0e10cSrcweir         }
86cdf0e10cSrcweir         else
87cdf0e10cSrcweir         {
88cdf0e10cSrcweir             for( sal_Int32 nIndex = 0; nIndex < mxIndexAccess->getCount(); nIndex++ )
89cdf0e10cSrcweir             {
90cdf0e10cSrcweir                 uno::Reference< container::XNamed > xNamed( mxIndexAccess->getByIndex( nIndex ), uno::UNO_QUERY_THROW );
91cdf0e10cSrcweir                 rtl::OUString aBookmarkName = xNamed->getName();
92cdf0e10cSrcweir                 if( aName.equalsIgnoreAsciiCase( aBookmarkName ) )
93cdf0e10cSrcweir                 {
94cdf0e10cSrcweir                     cachePos <<= xNamed;
95cdf0e10cSrcweir                     return sal_True;
96cdf0e10cSrcweir                 }
97cdf0e10cSrcweir             }
98cdf0e10cSrcweir         }
99cdf0e10cSrcweir         return sal_False;
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir 	// XIndexAccess
getCount()102cdf0e10cSrcweir 	virtual ::sal_Int32 SAL_CALL getCount(  ) throw (uno::RuntimeException)
103cdf0e10cSrcweir     {
104cdf0e10cSrcweir         return mxIndexAccess->getCount();
105cdf0e10cSrcweir     }
getByIndex(::sal_Int32 Index)106cdf0e10cSrcweir 	virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException )
107cdf0e10cSrcweir 	{
108cdf0e10cSrcweir         return mxIndexAccess->getByIndex( Index );
109cdf0e10cSrcweir     }
110cdf0e10cSrcweir };
111cdf0e10cSrcweir 
SwVbaBookmarks(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<::com::sun::star::uno::XComponentContext> & xContext,const uno::Reference<container::XIndexAccess> & xBookmarks,const uno::Reference<frame::XModel> & xModel)112cdf0e10cSrcweir SwVbaBookmarks::SwVbaBookmarks( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< ::com::sun::star::uno::XComponentContext > & xContext, const uno::Reference< container::XIndexAccess >& xBookmarks, const uno::Reference< frame::XModel >& xModel ): SwVbaBookmarks_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( new BookmarkCollectionHelper( xBookmarks ) ) ), mxModel( xModel )
113cdf0e10cSrcweir {
114cdf0e10cSrcweir     mxBookmarksSupplier.set( mxModel, uno::UNO_QUERY_THROW );
115cdf0e10cSrcweir     uno::Reference< text::XTextDocument > xDocument( mxModel, uno::UNO_QUERY_THROW );
116cdf0e10cSrcweir     // use view cursor to insert bookmark, or it will fail if insert bookmark in table
117cdf0e10cSrcweir     // mxText = xDocument->getText();
118cdf0e10cSrcweir     mxText = word::getXTextViewCursor( mxModel )->getText();
119cdf0e10cSrcweir }
120cdf0e10cSrcweir // XEnumerationAccess
121cdf0e10cSrcweir uno::Type
getElementType()122cdf0e10cSrcweir SwVbaBookmarks::getElementType() throw (uno::RuntimeException)
123cdf0e10cSrcweir {
124cdf0e10cSrcweir 	return word::XBookmark::static_type(0);
125cdf0e10cSrcweir }
126cdf0e10cSrcweir uno::Reference< container::XEnumeration >
createEnumeration()127cdf0e10cSrcweir SwVbaBookmarks::createEnumeration() throw (uno::RuntimeException)
128cdf0e10cSrcweir {
129cdf0e10cSrcweir     uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
130cdf0e10cSrcweir 	return new BookmarksEnumeration( getParent(), mxContext,xEnumAccess->createEnumeration(), mxModel );
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir uno::Any
createCollectionObject(const css::uno::Any & aSource)134cdf0e10cSrcweir SwVbaBookmarks::createCollectionObject( const css::uno::Any& aSource )
135cdf0e10cSrcweir {
136cdf0e10cSrcweir 	uno::Reference< container::XNamed > xNamed( aSource, uno::UNO_QUERY_THROW );
137cdf0e10cSrcweir     rtl::OUString aName = xNamed->getName();
138cdf0e10cSrcweir     return uno::makeAny( uno::Reference< word::XBookmark > ( new SwVbaBookmark( getParent(), mxContext, mxModel, aName ) ) );
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
removeBookmarkByName(const rtl::OUString & rName)141cdf0e10cSrcweir void SwVbaBookmarks::removeBookmarkByName( const rtl::OUString& rName ) throw (uno::RuntimeException)
142cdf0e10cSrcweir {
143cdf0e10cSrcweir     uno::Reference< text::XTextContent > xBookmark( m_xNameAccess->getByName( rName ), uno::UNO_QUERY_THROW );
144cdf0e10cSrcweir     mxText->removeTextContent( xBookmark );
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
addBookmarkByName(const rtl::OUString & rName,const uno::Reference<text::XTextRange> & rTextRange)147cdf0e10cSrcweir void SwVbaBookmarks::addBookmarkByName( const rtl::OUString& rName, const uno::Reference< text::XTextRange >& rTextRange ) throw (uno::RuntimeException)
148cdf0e10cSrcweir {
149cdf0e10cSrcweir     uno::Reference< lang::XMultiServiceFactory > xDocMSF( mxModel, uno::UNO_QUERY_THROW );
150cdf0e10cSrcweir     uno::Reference< text::XTextContent > xBookmark( xDocMSF->createInstance(  rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.Bookmark")) ), uno::UNO_QUERY_THROW );
151cdf0e10cSrcweir     uno::Reference< container::XNamed > xNamed( xBookmark, uno::UNO_QUERY_THROW );
152cdf0e10cSrcweir     xNamed->setName( rName );
153cdf0e10cSrcweir     mxText->insertTextContent( rTextRange, xBookmark, sal_False );
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir uno::Any SAL_CALL
Add(const rtl::OUString & rName,const uno::Any & rRange)157cdf0e10cSrcweir SwVbaBookmarks::Add( const rtl::OUString& rName, const uno::Any& rRange ) throw (uno::RuntimeException)
158cdf0e10cSrcweir {
159cdf0e10cSrcweir     uno::Reference< text::XTextRange > xTextRange;
160cdf0e10cSrcweir     uno::Reference< word::XRange > xRange;
161cdf0e10cSrcweir     if( rRange >>= xRange )
162cdf0e10cSrcweir     {
163cdf0e10cSrcweir         SwVbaRange* pRange = dynamic_cast< SwVbaRange* >( xRange.get() );
164cdf0e10cSrcweir         if( pRange )
165cdf0e10cSrcweir             xTextRange = pRange->getXTextRange();
166cdf0e10cSrcweir     }
167cdf0e10cSrcweir     else
168cdf0e10cSrcweir     {
169cdf0e10cSrcweir         // FIXME: insert the bookmark into current view cursor
170cdf0e10cSrcweir         xTextRange.set( word::getXTextViewCursor( mxModel ), uno::UNO_QUERY_THROW );
171cdf0e10cSrcweir     }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir     // remove the exist bookmark
174cdf0e10cSrcweir     // rtl::OUString aName = rName.toAsciiLowerCase();
175cdf0e10cSrcweir     rtl::OUString aName = rName;
176cdf0e10cSrcweir     if( m_xNameAccess->hasByName( aName ) )
177cdf0e10cSrcweir         removeBookmarkByName( aName );
178cdf0e10cSrcweir 
179cdf0e10cSrcweir     addBookmarkByName( aName, xTextRange );
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     return uno::makeAny( uno::Reference< word::XBookmark >( new SwVbaBookmark( getParent(), mxContext, mxModel, aName ) ) );
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
184cdf0e10cSrcweir sal_Int32 SAL_CALL
getDefaultSorting()185cdf0e10cSrcweir SwVbaBookmarks::getDefaultSorting() throw (css::uno::RuntimeException)
186cdf0e10cSrcweir {
187cdf0e10cSrcweir     return word::WdBookmarkSortBy::wdSortByName;
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir void SAL_CALL
setDefaultSorting(sal_Int32)191cdf0e10cSrcweir SwVbaBookmarks::setDefaultSorting( sal_Int32/* _type*/ ) throw (css::uno::RuntimeException)
192cdf0e10cSrcweir {
193cdf0e10cSrcweir     // not support in Writer
194cdf0e10cSrcweir }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir sal_Bool SAL_CALL
getShowHidden()197cdf0e10cSrcweir SwVbaBookmarks::getShowHidden() throw (css::uno::RuntimeException)
198cdf0e10cSrcweir {
199cdf0e10cSrcweir     return sal_True;
200cdf0e10cSrcweir }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir void SAL_CALL
setShowHidden(sal_Bool)203cdf0e10cSrcweir SwVbaBookmarks::setShowHidden( sal_Bool /*_hidden*/ ) throw (css::uno::RuntimeException)
204cdf0e10cSrcweir {
205cdf0e10cSrcweir     // not support in Writer
206cdf0e10cSrcweir }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir sal_Bool SAL_CALL
Exists(const rtl::OUString & rName)209cdf0e10cSrcweir SwVbaBookmarks::Exists( const rtl::OUString& rName ) throw (css::uno::RuntimeException)
210cdf0e10cSrcweir {
211cdf0e10cSrcweir     sal_Bool bExist = m_xNameAccess->hasByName( rName );
212cdf0e10cSrcweir     return bExist;
213cdf0e10cSrcweir }
214cdf0e10cSrcweir 
215cdf0e10cSrcweir rtl::OUString&
getServiceImplName()216cdf0e10cSrcweir SwVbaBookmarks::getServiceImplName()
217cdf0e10cSrcweir {
218cdf0e10cSrcweir 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaBookmarks") );
219cdf0e10cSrcweir 	return sImplName;
220cdf0e10cSrcweir }
221cdf0e10cSrcweir 
222cdf0e10cSrcweir css::uno::Sequence<rtl::OUString>
getServiceNames()223cdf0e10cSrcweir SwVbaBookmarks::getServiceNames()
224cdf0e10cSrcweir {
225cdf0e10cSrcweir 	static uno::Sequence< rtl::OUString > sNames;
226cdf0e10cSrcweir 	if ( sNames.getLength() == 0 )
227cdf0e10cSrcweir 	{
228cdf0e10cSrcweir 		sNames.realloc( 1 );
229cdf0e10cSrcweir 		sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Bookmarks") );
230cdf0e10cSrcweir 	}
231cdf0e10cSrcweir 	return sNames;
232cdf0e10cSrcweir }
233