1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23 #include "vbabookmark.hxx"
24 #include <vbahelper/vbahelper.hxx>
25 #include <tools/diagnose_ex.h>
26 #include <com/sun/star/text/XTextDocument.hpp>
27 #include <com/sun/star/text/XTextContent.hpp>
28 #include <com/sun/star/text/XTextViewCursor.hpp>
29 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
30 #include "vbarange.hxx"
31
32 using namespace ::ooo::vba;
33 using namespace ::com::sun::star;
34
SwVbaBookmark(const uno::Reference<ooo::vba::XHelperInterface> & rParent,const uno::Reference<uno::XComponentContext> & rContext,const css::uno::Reference<frame::XModel> & rModel,const rtl::OUString & rName)35 SwVbaBookmark::SwVbaBookmark( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext,
36 const css::uno::Reference< frame::XModel >& rModel, const rtl::OUString& rName ) throw ( css::uno::RuntimeException ) :
37 SwVbaBookmark_BASE( rParent, rContext ), mxModel( rModel ), maName( rName ), mbValid( sal_True )
38 {
39 uno::Reference< text::XBookmarksSupplier > xBookmarksSupplier( mxModel, uno::UNO_QUERY_THROW );
40 mxBookmark.set( xBookmarksSupplier->getBookmarks()->getByName( maName ), uno::UNO_QUERY_THROW );
41 }
42
~SwVbaBookmark()43 SwVbaBookmark::~SwVbaBookmark()
44 {
45 }
46
checkVality()47 void SwVbaBookmark::checkVality() throw ( uno::RuntimeException )
48 {
49 if( !mbValid )
50 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("The bookmark is not valid" ) ), uno::Reference< uno::XInterface >() );
51 }
52
Delete()53 void SAL_CALL SwVbaBookmark::Delete() throw ( uno::RuntimeException )
54 {
55 checkVality();
56 uno::Reference< text::XTextDocument > xTextDocument( mxModel, uno::UNO_QUERY_THROW );
57 xTextDocument->getText()->removeTextContent( mxBookmark );
58 mbValid = sal_False;
59 }
60
Select()61 void SAL_CALL SwVbaBookmark::Select() throw ( uno::RuntimeException )
62 {
63 checkVality();
64 uno::Reference< text::XTextViewCursorSupplier > xViewCursorSupplier( mxModel->getCurrentController(), uno::UNO_QUERY_THROW );
65 xViewCursorSupplier->getViewCursor()->gotoRange( mxBookmark->getAnchor(),sal_False );
66 }
67
getName()68 rtl::OUString SAL_CALL SwVbaBookmark::getName() throw ( uno::RuntimeException )
69 {
70 return maName;
71 }
72
setName(const rtl::OUString & _name)73 void SAL_CALL SwVbaBookmark::setName( const rtl::OUString& _name ) throw ( uno::RuntimeException )
74 {
75 uno::Reference< container::XNamed > xNamed( mxBookmark, uno::UNO_QUERY_THROW );
76 xNamed->setName( _name );
77 }
78
Range()79 uno::Any SAL_CALL SwVbaBookmark::Range() throw ( uno::RuntimeException )
80 {
81 uno::Reference< text::XTextContent > xTextContent( mxBookmark, uno::UNO_QUERY_THROW );
82 uno::Reference< text::XTextDocument > xTextDocument( mxModel, uno::UNO_QUERY_THROW );
83 uno::Reference< text::XTextRange > xTextRange( xTextContent->getAnchor(), uno::UNO_QUERY_THROW );
84 return uno::makeAny( uno::Reference< word::XRange>( new SwVbaRange( this, mxContext, xTextDocument, xTextRange->getStart(), xTextRange->getEnd(), xTextRange->getText() ) ) );
85 }
86
87 rtl::OUString&
getServiceImplName()88 SwVbaBookmark::getServiceImplName()
89 {
90 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaBookmark") );
91 return sImplName;
92 }
93
94 uno::Sequence< rtl::OUString >
getServiceNames()95 SwVbaBookmark::getServiceNames()
96 {
97 static uno::Sequence< rtl::OUString > aServiceNames;
98 if ( aServiceNames.getLength() == 0 )
99 {
100 aServiceNames.realloc( 1 );
101 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Bookmark" ) );
102 }
103 return aServiceNames;
104 }
105
106