1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include "vbabookmarks.hxx"
31 #include "vbabookmark.hxx"
32 #include <com/sun/star/container/XNamed.hpp>
33 #include <com/sun/star/text/XTextDocument.hpp>
34 #include <com/sun/star/text/XTextViewCursor.hpp>
35 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
36 #include <ooo/vba/word/WdBookmarkSortBy.hpp>
37 #include "vbarange.hxx"
38 #include "wordvbahelper.hxx"
39 #include <cppuhelper/implbase2.hxx>
41 using namespace ::ooo::vba
;
42 using namespace ::com::sun::star
;
44 class BookmarksEnumeration
: public EnumerationHelperImpl
46 uno::Reference
< frame::XModel
> mxModel
;
47 uno::WeakReference
< XHelperInterface
> mxParent
;
49 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( xContext
, xEnumeration
), mxModel( xModel
), mxParent( xParent
) {}
51 virtual uno::Any SAL_CALL
nextElement( ) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
53 uno::Reference
< container::XNamed
> xNamed( m_xEnumeration
->nextElement(), uno::UNO_QUERY_THROW
);
54 rtl::OUString aName
= xNamed
->getName();
55 return uno::makeAny( uno::Reference
< word::XBookmark
> ( new SwVbaBookmark( mxParent
, m_xContext
, mxModel
, aName
) ) );
60 // Bookmarks use case-insensitive name lookup in MS Word.
61 typedef ::cppu::WeakImplHelper2
< container::XNameAccess
, container::XIndexAccess
> BookmarkCollectionHelper_BASE
;
62 class BookmarkCollectionHelper
: public BookmarkCollectionHelper_BASE
65 uno::Reference
< container::XNameAccess
> mxNameAccess
;
66 uno::Reference
< container::XIndexAccess
> mxIndexAccess
;
69 BookmarkCollectionHelper( const uno::Reference
< container::XIndexAccess
>& xIndexAccess
) throw (uno::RuntimeException
) : mxIndexAccess( xIndexAccess
)
71 mxNameAccess
.set( mxIndexAccess
, uno::UNO_QUERY_THROW
);
74 virtual uno::Type SAL_CALL
getElementType( ) throw (uno::RuntimeException
) { return mxIndexAccess
->getElementType(); }
75 virtual ::sal_Bool SAL_CALL
hasElements( ) throw (uno::RuntimeException
) { return mxIndexAccess
->hasElements(); }
77 virtual uno::Any SAL_CALL
getByName( const ::rtl::OUString
& aName
) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
79 if ( !hasByName(aName
) )
80 throw container::NoSuchElementException();
83 virtual uno::Sequence
< ::rtl::OUString
> SAL_CALL
getElementNames( ) throw (uno::RuntimeException
)
85 return mxNameAccess
->getElementNames();
87 virtual ::sal_Bool SAL_CALL
hasByName( const ::rtl::OUString
& aName
) throw (uno::RuntimeException
)
89 if( mxNameAccess
->hasByName( aName
) )
91 cachePos
= mxNameAccess
->getByName( aName
);
96 for( sal_Int32 nIndex
= 0; nIndex
< mxIndexAccess
->getCount(); nIndex
++ )
98 uno::Reference
< container::XNamed
> xNamed( mxIndexAccess
->getByIndex( nIndex
), uno::UNO_QUERY_THROW
);
99 rtl::OUString aBookmarkName
= xNamed
->getName();
100 if( aName
.equalsIgnoreAsciiCase( aBookmarkName
) )
110 virtual ::sal_Int32 SAL_CALL
getCount( ) throw (uno::RuntimeException
)
112 return mxIndexAccess
->getCount();
114 virtual uno::Any SAL_CALL
getByIndex( ::sal_Int32 Index
) throw (lang::IndexOutOfBoundsException
, lang::WrappedTargetException
, uno::RuntimeException
)
116 return mxIndexAccess
->getByIndex( Index
);
120 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
)
122 mxBookmarksSupplier
.set( mxModel
, uno::UNO_QUERY_THROW
);
123 uno::Reference
< text::XTextDocument
> xDocument( mxModel
, uno::UNO_QUERY_THROW
);
124 // use view cursor to insert bookmark, or it will fail if insert bookmark in table
125 // mxText = xDocument->getText();
126 mxText
= word::getXTextViewCursor( mxModel
)->getText();
128 // XEnumerationAccess
130 SwVbaBookmarks::getElementType() throw (uno::RuntimeException
)
132 return word::XBookmark::static_type(0);
134 uno::Reference
< container::XEnumeration
>
135 SwVbaBookmarks::createEnumeration() throw (uno::RuntimeException
)
137 uno::Reference
< container::XEnumerationAccess
> xEnumAccess( m_xIndexAccess
, uno::UNO_QUERY_THROW
);
138 return new BookmarksEnumeration( getParent(), mxContext
,xEnumAccess
->createEnumeration(), mxModel
);
142 SwVbaBookmarks::createCollectionObject( const css::uno::Any
& aSource
)
144 uno::Reference
< container::XNamed
> xNamed( aSource
, uno::UNO_QUERY_THROW
);
145 rtl::OUString aName
= xNamed
->getName();
146 return uno::makeAny( uno::Reference
< word::XBookmark
> ( new SwVbaBookmark( getParent(), mxContext
, mxModel
, aName
) ) );
149 void SwVbaBookmarks::removeBookmarkByName( const rtl::OUString
& rName
) throw (uno::RuntimeException
)
151 uno::Reference
< text::XTextContent
> xBookmark( m_xNameAccess
->getByName( rName
), uno::UNO_QUERY_THROW
);
152 mxText
->removeTextContent( xBookmark
);
155 void SwVbaBookmarks::addBookmarkByName( const rtl::OUString
& rName
, const uno::Reference
< text::XTextRange
>& rTextRange
) throw (uno::RuntimeException
)
157 uno::Reference
< lang::XMultiServiceFactory
> xDocMSF( mxModel
, uno::UNO_QUERY_THROW
);
158 uno::Reference
< text::XTextContent
> xBookmark( xDocMSF
->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.Bookmark")) ), uno::UNO_QUERY_THROW
);
159 uno::Reference
< container::XNamed
> xNamed( xBookmark
, uno::UNO_QUERY_THROW
);
160 xNamed
->setName( rName
);
161 mxText
->insertTextContent( rTextRange
, xBookmark
, sal_False
);
165 SwVbaBookmarks::Add( const rtl::OUString
& rName
, const uno::Any
& rRange
) throw (uno::RuntimeException
)
167 uno::Reference
< text::XTextRange
> xTextRange
;
168 uno::Reference
< word::XRange
> xRange
;
169 if( rRange
>>= xRange
)
171 SwVbaRange
* pRange
= dynamic_cast< SwVbaRange
* >( xRange
.get() );
173 xTextRange
= pRange
->getXTextRange();
177 // FIXME: insert the bookmark into current view cursor
178 xTextRange
.set( word::getXTextViewCursor( mxModel
), uno::UNO_QUERY_THROW
);
181 // remove the exist bookmark
182 // rtl::OUString aName = rName.toAsciiLowerCase();
183 rtl::OUString aName
= rName
;
184 if( m_xNameAccess
->hasByName( aName
) )
185 removeBookmarkByName( aName
);
187 addBookmarkByName( aName
, xTextRange
);
189 return uno::makeAny( uno::Reference
< word::XBookmark
>( new SwVbaBookmark( getParent(), mxContext
, mxModel
, aName
) ) );
193 SwVbaBookmarks::getDefaultSorting() throw (css::uno::RuntimeException
)
195 return word::WdBookmarkSortBy::wdSortByName
;
199 SwVbaBookmarks::setDefaultSorting( sal_Int32
/* _type*/ ) throw (css::uno::RuntimeException
)
201 // not support in Writer
205 SwVbaBookmarks::getShowHidden() throw (css::uno::RuntimeException
)
211 SwVbaBookmarks::setShowHidden( sal_Bool
/*_hidden*/ ) throw (css::uno::RuntimeException
)
213 // not support in Writer
217 SwVbaBookmarks::Exists( const rtl::OUString
& rName
) throw (css::uno::RuntimeException
)
219 sal_Bool bExist
= m_xNameAccess
->hasByName( rName
);
224 SwVbaBookmarks::getServiceImplName()
226 static rtl::OUString
sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaBookmarks") );
230 css::uno::Sequence
<rtl::OUString
>
231 SwVbaBookmarks::getServiceNames()
233 static uno::Sequence
< rtl::OUString
> sNames
;
234 if ( sNames
.getLength() == 0 )
237 sNames
[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Bookmarks") );