xref: /trunk/main/sw/source/ui/vba/vbabookmark.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #include "vbabookmark.hxx"
28 #include <vbahelper/vbahelper.hxx>
29 #include <tools/diagnose_ex.h>
30 #include <com/sun/star/text/XTextDocument.hpp>
31 #include <com/sun/star/text/XTextContent.hpp>
32 #include <com/sun/star/text/XTextViewCursor.hpp>
33 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
34 #include "vbarange.hxx"
35 
36 using namespace ::ooo::vba;
37 using namespace ::com::sun::star;
38 
39 SwVbaBookmark::SwVbaBookmark( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext,
40     const css::uno::Reference< frame::XModel >& rModel, const rtl::OUString& rName ) throw ( css::uno::RuntimeException ) :
41     SwVbaBookmark_BASE( rParent, rContext ), mxModel( rModel ), maName( rName ), mbValid( sal_True )
42 {
43     uno::Reference< text::XBookmarksSupplier > xBookmarksSupplier( mxModel, uno::UNO_QUERY_THROW );
44     mxBookmark.set( xBookmarksSupplier->getBookmarks()->getByName( maName ), uno::UNO_QUERY_THROW );
45 }
46 
47 SwVbaBookmark::~SwVbaBookmark()
48 {
49 }
50 
51 void SwVbaBookmark::checkVality() throw ( uno::RuntimeException )
52 {
53     if( !mbValid )
54         throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("The bookmark is not valid" ) ), uno::Reference< uno::XInterface >() );
55 }
56 
57 void SAL_CALL SwVbaBookmark::Delete() throw ( uno::RuntimeException )
58 {
59     checkVality();
60     uno::Reference< text::XTextDocument > xTextDocument( mxModel, uno::UNO_QUERY_THROW );
61     xTextDocument->getText()->removeTextContent( mxBookmark );
62     mbValid = sal_False;
63 }
64 
65 void SAL_CALL SwVbaBookmark::Select() throw ( uno::RuntimeException )
66 {
67     checkVality();
68     uno::Reference< text::XTextViewCursorSupplier > xViewCursorSupplier( mxModel->getCurrentController(), uno::UNO_QUERY_THROW );
69     xViewCursorSupplier->getViewCursor()->gotoRange( mxBookmark->getAnchor(),sal_False );
70 }
71 
72 rtl::OUString SAL_CALL SwVbaBookmark::getName() throw ( uno::RuntimeException )
73 {
74     return maName;
75 }
76 
77 void SAL_CALL SwVbaBookmark::setName( const rtl::OUString& _name ) throw ( uno::RuntimeException )
78 {
79     uno::Reference< container::XNamed > xNamed( mxBookmark, uno::UNO_QUERY_THROW );
80     xNamed->setName( _name );
81 }
82 
83 uno::Any SAL_CALL SwVbaBookmark::Range() throw ( uno::RuntimeException )
84 {
85     uno::Reference< text::XTextContent > xTextContent( mxBookmark, uno::UNO_QUERY_THROW );
86     uno::Reference< text::XTextDocument > xTextDocument( mxModel, uno::UNO_QUERY_THROW );
87     uno::Reference< text::XTextRange > xTextRange( xTextContent->getAnchor(), uno::UNO_QUERY_THROW );
88     return uno::makeAny( uno::Reference< word::XRange>(  new SwVbaRange( this, mxContext, xTextDocument, xTextRange->getStart(), xTextRange->getEnd(), xTextRange->getText() ) ) );
89 }
90 
91 rtl::OUString&
92 SwVbaBookmark::getServiceImplName()
93 {
94 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaBookmark") );
95 	return sImplName;
96 }
97 
98 uno::Sequence< rtl::OUString >
99 SwVbaBookmark::getServiceNames()
100 {
101 	static uno::Sequence< rtl::OUString > aServiceNames;
102 	if ( aServiceNames.getLength() == 0 )
103 	{
104 		aServiceNames.realloc( 1 );
105 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Bookmark" ) );
106 	}
107 	return aServiceNames;
108 }
109 
110