1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir #include "vbabookmarks.hxx" 28*cdf0e10cSrcweir #include "vbabookmark.hxx" 29*cdf0e10cSrcweir #include <com/sun/star/container/XNamed.hpp> 30*cdf0e10cSrcweir #include <com/sun/star/text/XTextDocument.hpp> 31*cdf0e10cSrcweir #include <com/sun/star/text/XTextViewCursor.hpp> 32*cdf0e10cSrcweir #include <com/sun/star/text/XTextViewCursorSupplier.hpp> 33*cdf0e10cSrcweir #include <ooo/vba/word/WdBookmarkSortBy.hpp> 34*cdf0e10cSrcweir #include "vbarange.hxx" 35*cdf0e10cSrcweir #include "wordvbahelper.hxx" 36*cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx> 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir using namespace ::ooo::vba; 39*cdf0e10cSrcweir using namespace ::com::sun::star; 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir class BookmarksEnumeration : public EnumerationHelperImpl 42*cdf0e10cSrcweir { 43*cdf0e10cSrcweir uno::Reference< frame::XModel > mxModel; 44*cdf0e10cSrcweir public: 45*cdf0e10cSrcweir 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 ) {} 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException) 48*cdf0e10cSrcweir { 49*cdf0e10cSrcweir uno::Reference< container::XNamed > xNamed( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW ); 50*cdf0e10cSrcweir rtl::OUString aName = xNamed->getName(); 51*cdf0e10cSrcweir return uno::makeAny( uno::Reference< word::XBookmark > ( new SwVbaBookmark( m_xParent, m_xContext, mxModel, aName ) ) ); 52*cdf0e10cSrcweir } 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir }; 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir // Bookmarks use case-insensitive name lookup in MS Word. 57*cdf0e10cSrcweir typedef ::cppu::WeakImplHelper2< container::XNameAccess, container::XIndexAccess > BookmarkCollectionHelper_BASE; 58*cdf0e10cSrcweir class BookmarkCollectionHelper : public BookmarkCollectionHelper_BASE 59*cdf0e10cSrcweir { 60*cdf0e10cSrcweir private: 61*cdf0e10cSrcweir uno::Reference< container::XNameAccess > mxNameAccess; 62*cdf0e10cSrcweir uno::Reference< container::XIndexAccess > mxIndexAccess; 63*cdf0e10cSrcweir uno::Any cachePos; 64*cdf0e10cSrcweir public: 65*cdf0e10cSrcweir BookmarkCollectionHelper( const uno::Reference< container::XIndexAccess >& xIndexAccess ) throw (uno::RuntimeException) : mxIndexAccess( xIndexAccess ) 66*cdf0e10cSrcweir { 67*cdf0e10cSrcweir mxNameAccess.set( mxIndexAccess, uno::UNO_QUERY_THROW ); 68*cdf0e10cSrcweir } 69*cdf0e10cSrcweir // XElementAccess 70*cdf0e10cSrcweir virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException) { return mxIndexAccess->getElementType(); } 71*cdf0e10cSrcweir virtual ::sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException) { return mxIndexAccess->hasElements(); } 72*cdf0e10cSrcweir // XNameAcess 73*cdf0e10cSrcweir virtual uno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException) 74*cdf0e10cSrcweir { 75*cdf0e10cSrcweir if ( !hasByName(aName) ) 76*cdf0e10cSrcweir throw container::NoSuchElementException(); 77*cdf0e10cSrcweir return cachePos; 78*cdf0e10cSrcweir } 79*cdf0e10cSrcweir virtual uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames( ) throw (uno::RuntimeException) 80*cdf0e10cSrcweir { 81*cdf0e10cSrcweir return mxNameAccess->getElementNames(); 82*cdf0e10cSrcweir } 83*cdf0e10cSrcweir virtual ::sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw (uno::RuntimeException) 84*cdf0e10cSrcweir { 85*cdf0e10cSrcweir if( mxNameAccess->hasByName( aName ) ) 86*cdf0e10cSrcweir { 87*cdf0e10cSrcweir cachePos = mxNameAccess->getByName( aName ); 88*cdf0e10cSrcweir return sal_True; 89*cdf0e10cSrcweir } 90*cdf0e10cSrcweir else 91*cdf0e10cSrcweir { 92*cdf0e10cSrcweir for( sal_Int32 nIndex = 0; nIndex < mxIndexAccess->getCount(); nIndex++ ) 93*cdf0e10cSrcweir { 94*cdf0e10cSrcweir uno::Reference< container::XNamed > xNamed( mxIndexAccess->getByIndex( nIndex ), uno::UNO_QUERY_THROW ); 95*cdf0e10cSrcweir rtl::OUString aBookmarkName = xNamed->getName(); 96*cdf0e10cSrcweir if( aName.equalsIgnoreAsciiCase( aBookmarkName ) ) 97*cdf0e10cSrcweir { 98*cdf0e10cSrcweir cachePos <<= xNamed; 99*cdf0e10cSrcweir return sal_True; 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir } 103*cdf0e10cSrcweir return sal_False; 104*cdf0e10cSrcweir } 105*cdf0e10cSrcweir // XIndexAccess 106*cdf0e10cSrcweir virtual ::sal_Int32 SAL_CALL getCount( ) throw (uno::RuntimeException) 107*cdf0e10cSrcweir { 108*cdf0e10cSrcweir return mxIndexAccess->getCount(); 109*cdf0e10cSrcweir } 110*cdf0e10cSrcweir virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException ) 111*cdf0e10cSrcweir { 112*cdf0e10cSrcweir return mxIndexAccess->getByIndex( Index ); 113*cdf0e10cSrcweir } 114*cdf0e10cSrcweir }; 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir 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 ) 117*cdf0e10cSrcweir { 118*cdf0e10cSrcweir mxBookmarksSupplier.set( mxModel, uno::UNO_QUERY_THROW ); 119*cdf0e10cSrcweir uno::Reference< text::XTextDocument > xDocument( mxModel, uno::UNO_QUERY_THROW ); 120*cdf0e10cSrcweir // use view cursor to insert bookmark, or it will fail if insert bookmark in table 121*cdf0e10cSrcweir // mxText = xDocument->getText(); 122*cdf0e10cSrcweir mxText = word::getXTextViewCursor( mxModel )->getText(); 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir // XEnumerationAccess 125*cdf0e10cSrcweir uno::Type 126*cdf0e10cSrcweir SwVbaBookmarks::getElementType() throw (uno::RuntimeException) 127*cdf0e10cSrcweir { 128*cdf0e10cSrcweir return word::XBookmark::static_type(0); 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir uno::Reference< container::XEnumeration > 131*cdf0e10cSrcweir SwVbaBookmarks::createEnumeration() throw (uno::RuntimeException) 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xIndexAccess, uno::UNO_QUERY_THROW ); 134*cdf0e10cSrcweir return new BookmarksEnumeration( getParent(), mxContext,xEnumAccess->createEnumeration(), mxModel ); 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir uno::Any 138*cdf0e10cSrcweir SwVbaBookmarks::createCollectionObject( const css::uno::Any& aSource ) 139*cdf0e10cSrcweir { 140*cdf0e10cSrcweir uno::Reference< container::XNamed > xNamed( aSource, uno::UNO_QUERY_THROW ); 141*cdf0e10cSrcweir rtl::OUString aName = xNamed->getName(); 142*cdf0e10cSrcweir return uno::makeAny( uno::Reference< word::XBookmark > ( new SwVbaBookmark( getParent(), mxContext, mxModel, aName ) ) ); 143*cdf0e10cSrcweir } 144*cdf0e10cSrcweir 145*cdf0e10cSrcweir void SwVbaBookmarks::removeBookmarkByName( const rtl::OUString& rName ) throw (uno::RuntimeException) 146*cdf0e10cSrcweir { 147*cdf0e10cSrcweir uno::Reference< text::XTextContent > xBookmark( m_xNameAccess->getByName( rName ), uno::UNO_QUERY_THROW ); 148*cdf0e10cSrcweir mxText->removeTextContent( xBookmark ); 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir void SwVbaBookmarks::addBookmarkByName( const rtl::OUString& rName, const uno::Reference< text::XTextRange >& rTextRange ) throw (uno::RuntimeException) 152*cdf0e10cSrcweir { 153*cdf0e10cSrcweir uno::Reference< lang::XMultiServiceFactory > xDocMSF( mxModel, uno::UNO_QUERY_THROW ); 154*cdf0e10cSrcweir uno::Reference< text::XTextContent > xBookmark( xDocMSF->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.Bookmark")) ), uno::UNO_QUERY_THROW ); 155*cdf0e10cSrcweir uno::Reference< container::XNamed > xNamed( xBookmark, uno::UNO_QUERY_THROW ); 156*cdf0e10cSrcweir xNamed->setName( rName ); 157*cdf0e10cSrcweir mxText->insertTextContent( rTextRange, xBookmark, sal_False ); 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir uno::Any SAL_CALL 161*cdf0e10cSrcweir SwVbaBookmarks::Add( const rtl::OUString& rName, const uno::Any& rRange ) throw (uno::RuntimeException) 162*cdf0e10cSrcweir { 163*cdf0e10cSrcweir uno::Reference< text::XTextRange > xTextRange; 164*cdf0e10cSrcweir uno::Reference< word::XRange > xRange; 165*cdf0e10cSrcweir if( rRange >>= xRange ) 166*cdf0e10cSrcweir { 167*cdf0e10cSrcweir SwVbaRange* pRange = dynamic_cast< SwVbaRange* >( xRange.get() ); 168*cdf0e10cSrcweir if( pRange ) 169*cdf0e10cSrcweir xTextRange = pRange->getXTextRange(); 170*cdf0e10cSrcweir } 171*cdf0e10cSrcweir else 172*cdf0e10cSrcweir { 173*cdf0e10cSrcweir // FIXME: insert the bookmark into current view cursor 174*cdf0e10cSrcweir xTextRange.set( word::getXTextViewCursor( mxModel ), uno::UNO_QUERY_THROW ); 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir // remove the exist bookmark 178*cdf0e10cSrcweir // rtl::OUString aName = rName.toAsciiLowerCase(); 179*cdf0e10cSrcweir rtl::OUString aName = rName; 180*cdf0e10cSrcweir if( m_xNameAccess->hasByName( aName ) ) 181*cdf0e10cSrcweir removeBookmarkByName( aName ); 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir addBookmarkByName( aName, xTextRange ); 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir return uno::makeAny( uno::Reference< word::XBookmark >( new SwVbaBookmark( getParent(), mxContext, mxModel, aName ) ) ); 186*cdf0e10cSrcweir } 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir sal_Int32 SAL_CALL 189*cdf0e10cSrcweir SwVbaBookmarks::getDefaultSorting() throw (css::uno::RuntimeException) 190*cdf0e10cSrcweir { 191*cdf0e10cSrcweir return word::WdBookmarkSortBy::wdSortByName; 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir void SAL_CALL 195*cdf0e10cSrcweir SwVbaBookmarks::setDefaultSorting( sal_Int32/* _type*/ ) throw (css::uno::RuntimeException) 196*cdf0e10cSrcweir { 197*cdf0e10cSrcweir // not support in Writer 198*cdf0e10cSrcweir } 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir sal_Bool SAL_CALL 201*cdf0e10cSrcweir SwVbaBookmarks::getShowHidden() throw (css::uno::RuntimeException) 202*cdf0e10cSrcweir { 203*cdf0e10cSrcweir return sal_True; 204*cdf0e10cSrcweir } 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir void SAL_CALL 207*cdf0e10cSrcweir SwVbaBookmarks::setShowHidden( sal_Bool /*_hidden*/ ) throw (css::uno::RuntimeException) 208*cdf0e10cSrcweir { 209*cdf0e10cSrcweir // not support in Writer 210*cdf0e10cSrcweir } 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir sal_Bool SAL_CALL 213*cdf0e10cSrcweir SwVbaBookmarks::Exists( const rtl::OUString& rName ) throw (css::uno::RuntimeException) 214*cdf0e10cSrcweir { 215*cdf0e10cSrcweir sal_Bool bExist = m_xNameAccess->hasByName( rName ); 216*cdf0e10cSrcweir return bExist; 217*cdf0e10cSrcweir } 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir rtl::OUString& 220*cdf0e10cSrcweir SwVbaBookmarks::getServiceImplName() 221*cdf0e10cSrcweir { 222*cdf0e10cSrcweir static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaBookmarks") ); 223*cdf0e10cSrcweir return sImplName; 224*cdf0e10cSrcweir } 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir css::uno::Sequence<rtl::OUString> 227*cdf0e10cSrcweir SwVbaBookmarks::getServiceNames() 228*cdf0e10cSrcweir { 229*cdf0e10cSrcweir static uno::Sequence< rtl::OUString > sNames; 230*cdf0e10cSrcweir if ( sNames.getLength() == 0 ) 231*cdf0e10cSrcweir { 232*cdf0e10cSrcweir sNames.realloc( 1 ); 233*cdf0e10cSrcweir sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Bookmarks") ); 234*cdf0e10cSrcweir } 235*cdf0e10cSrcweir return sNames; 236*cdf0e10cSrcweir } 237