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