LibreOffice Module sw (master)  1
vbabookmarks.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 #include "vbabookmarks.hxx"
20 #include "vbabookmark.hxx"
21 #include <com/sun/star/container/XNamed.hpp>
22 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 #include <com/sun/star/text/XTextDocument.hpp>
24 #include <com/sun/star/text/XTextViewCursor.hpp>
25 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
26 #include <ooo/vba/word/WdBookmarkSortBy.hpp>
27 #include "vbarange.hxx"
28 #include "wordvbahelper.hxx"
29 #include <cppuhelper/implbase.hxx>
30 
31 using namespace ::ooo::vba;
32 using namespace ::com::sun::star;
33 
35 {
36  uno::Reference< frame::XModel > mxModel;
37 public:
39  BookmarksEnumeration( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration, const uno::Reference< frame::XModel >& xModel ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ), mxModel( xModel ) {}
40 
41  virtual uno::Any SAL_CALL nextElement( ) override
42  {
43  uno::Reference< container::XNamed > xNamed( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
44  OUString aName = xNamed->getName();
45  return uno::makeAny( uno::Reference< word::XBookmark > ( new SwVbaBookmark( m_xParent, m_xContext, mxModel, aName ) ) );
46  }
47 
48 };
49 
50 // Bookmarks use case-insensitive name lookup in MS Word.
51 class BookmarkCollectionHelper : public ::cppu::WeakImplHelper< container::XNameAccess,
52  container::XIndexAccess >
53 {
54 private:
55  uno::Reference< container::XNameAccess > mxNameAccess;
56  uno::Reference< container::XIndexAccess > mxIndexAccess;
58 public:
60  explicit BookmarkCollectionHelper( const uno::Reference< container::XIndexAccess >& xIndexAccess ) : mxIndexAccess( xIndexAccess )
61  {
62  mxNameAccess.set( mxIndexAccess, uno::UNO_QUERY_THROW );
63  }
64  // XElementAccess
65  virtual uno::Type SAL_CALL getElementType( ) override { return mxIndexAccess->getElementType(); }
66  virtual sal_Bool SAL_CALL hasElements( ) override { return mxIndexAccess->hasElements(); }
67  // XNameAccess
68  virtual uno::Any SAL_CALL getByName( const OUString& aName ) override
69  {
70  if ( !hasByName(aName) )
71  throw container::NoSuchElementException();
72  return cachePos;
73  }
74  virtual uno::Sequence< OUString > SAL_CALL getElementNames( ) override
75  {
76  return mxNameAccess->getElementNames();
77  }
78  virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override
79  {
80  if( mxNameAccess->hasByName( aName ) )
81  {
82  cachePos = mxNameAccess->getByName( aName );
83  return true;
84  }
85  else
86  {
87  for( sal_Int32 nIndex = 0; nIndex < mxIndexAccess->getCount(); nIndex++ )
88  {
89  uno::Reference< container::XNamed > xNamed( mxIndexAccess->getByIndex( nIndex ), uno::UNO_QUERY_THROW );
90  OUString aBookmarkName = xNamed->getName();
91  if( aName.equalsIgnoreAsciiCase( aBookmarkName ) )
92  {
93  cachePos <<= xNamed;
94  return true;
95  }
96  }
97  }
98  return false;
99  }
100  // XIndexAccess
101  virtual ::sal_Int32 SAL_CALL getCount( ) override
102  {
103  return mxIndexAccess->getCount();
104  }
105  virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
106  {
107  return mxIndexAccess->getByIndex( Index );
108  }
109 };
110 
111 SwVbaBookmarks::SwVbaBookmarks( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< css::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 )
112 {
113  mxBookmarksSupplier.set( mxModel, uno::UNO_QUERY_THROW );
114  uno::Reference< text::XTextDocument > xDocument( mxModel, uno::UNO_QUERY_THROW );
115 }
116 // XEnumerationAccess
117 uno::Type
119 {
121 }
122 uno::Reference< container::XEnumeration >
124 {
125  uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
126  return new BookmarksEnumeration( getParent(), mxContext,xEnumAccess->createEnumeration(), mxModel );
127 }
128 
129 uno::Any
130 SwVbaBookmarks::createCollectionObject( const css::uno::Any& aSource )
131 {
132  uno::Reference< container::XNamed > xNamed( aSource, uno::UNO_QUERY_THROW );
133  OUString aName = xNamed->getName();
134  return uno::makeAny( uno::Reference< word::XBookmark > ( new SwVbaBookmark( getParent(), mxContext, mxModel, aName ) ) );
135 }
136 
137 void SwVbaBookmarks::removeBookmarkByName( const OUString& rName )
138 {
139  uno::Reference< text::XTextContent > xBookmark( m_xNameAccess->getByName( rName ), uno::UNO_QUERY_THROW );
140  word::getXTextViewCursor( mxModel )->getText()->removeTextContent( xBookmark );
141 }
142 
143 void SwVbaBookmarks::addBookmarkByName( const uno::Reference< frame::XModel >& xModel, const OUString& rName, const uno::Reference< text::XTextRange >& rTextRange )
144 {
145  uno::Reference< lang::XMultiServiceFactory > xDocMSF( xModel, uno::UNO_QUERY_THROW );
146  uno::Reference< text::XTextContent > xBookmark( xDocMSF->createInstance("com.sun.star.text.Bookmark"), uno::UNO_QUERY_THROW );
147  uno::Reference< container::XNamed > xNamed( xBookmark, uno::UNO_QUERY_THROW );
148  xNamed->setName( rName );
149  rTextRange->getText()->insertTextContent( rTextRange, xBookmark, false );
150 }
151 
152 uno::Any SAL_CALL
153 SwVbaBookmarks::Add( const OUString& rName, const uno::Any& rRange )
154 {
155  uno::Reference< text::XTextRange > xTextRange;
156  uno::Reference< word::XRange > xRange;
157  if( rRange >>= xRange )
158  {
159  SwVbaRange* pRange = dynamic_cast< SwVbaRange* >( xRange.get() );
160  if( pRange )
161  xTextRange = pRange->getXTextRange();
162  }
163  else
164  {
165  // FIXME: insert the bookmark into current view cursor
166  xTextRange.set( word::getXTextViewCursor( mxModel ), uno::UNO_QUERY_THROW );
167  }
168 
169  // remove the exist bookmark
170  if( m_xNameAccess->hasByName( rName ) )
171  removeBookmarkByName( rName );
172 
173  addBookmarkByName( mxModel, rName, xTextRange );
174 
175  return uno::makeAny( uno::Reference< word::XBookmark >( new SwVbaBookmark( getParent(), mxContext, mxModel, rName ) ) );
176 }
177 
178 sal_Int32 SAL_CALL
180 {
181  return word::WdBookmarkSortBy::wdSortByName;
182 }
183 
184 void SAL_CALL
185 SwVbaBookmarks::setDefaultSorting( sal_Int32/* _type*/ )
186 {
187  // not support in Writer
188 }
189 
190 sal_Bool SAL_CALL
192 {
193  return true;
194 }
195 
196 void SAL_CALL
198 {
199  // not support in Writer
200 }
201 
202 sal_Bool SAL_CALL
203 SwVbaBookmarks::Exists( const OUString& rName )
204 {
205  bool bExist = m_xNameAccess->hasByName( rName );
206  return bExist;
207 }
208 
209 OUString
211 {
212  return "SwVbaBookmarks";
213 }
214 
215 css::uno::Sequence<OUString>
217 {
218  static uno::Sequence< OUString > const sNames
219  {
220  "ooo.vba.word.Bookmarks"
221  };
222  return sNames;
223 }
224 
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
uno::Reference< container::XIndexAccess > mxIndexAccess
virtual uno::Any SAL_CALL nextElement() override
void removeBookmarkByName(const OUString &rName)
BookmarksEnumeration(const uno::Reference< XHelperInterface > &xParent, const uno::Reference< uno::XComponentContext > &xContext, const uno::Reference< container::XEnumeration > &xEnumeration, const uno::Reference< frame::XModel > &xModel)
css::uno::Reference< css::container::XEnumeration > m_xEnumeration
virtual sal_Bool SAL_CALL hasElements() override
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
virtual uno::Any SAL_CALL getByIndex(::sal_Int32 Index) override
virtual void SAL_CALL setShowHidden(sal_Bool _hidden) override
Reference
css::uno::Reference< css::frame::XModel2 > mxModel
virtual ::sal_Int32 SAL_CALL getCount() override
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
virtual uno::Sequence< OUString > SAL_CALL getElementNames() override
virtual sal_Bool SAL_CALL Exists(const OUString &rName) override
css::uno::Reference< css::frame::XModel > mxModel
virtual css::uno::Reference< css::text::XTextRange > SAL_CALL getXTextRange() override
Definition: vbarange.cxx:89
uno::Reference< text::XTextViewCursor > getXTextViewCursor(const uno::Reference< frame::XModel > &xModel)
uno::Reference< frame::XModel > mxModel
virtual uno::Type SAL_CALL getElementType() override
virtual OUString getServiceImplName() override
virtual css::uno::Sequence< OUString > getServiceNames() override
css::uno::Reference< css::text::XBookmarksSupplier > mxBookmarksSupplier
SwVbaBookmarks(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::container::XIndexAccess > &xBookmarks, const css::uno::Reference< css::frame::XModel > &xModel)
css::uno::Reference< css::container::XNameAccess > m_xNameAccess
unsigned char sal_Bool
css::uno::Type const & get()
BookmarkCollectionHelper(const uno::Reference< container::XIndexAccess > &xIndexAccess)
virtual sal_Bool SAL_CALL getShowHidden() override
static void addBookmarkByName(const css::uno::Reference< css::frame::XModel > &xModel, const OUString &rName, const css::uno::Reference< css::text::XTextRange > &rTextRange)
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual void SAL_CALL setDefaultSorting(sal_Int32 _type) override
virtual css::uno::Reference< ov::XHelperInterface > SAL_CALL getParent() override
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
virtual css::uno::Any SAL_CALL Add(const OUString &rName, const css::uno::Any &rRange) override
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
OString const aName
uno::Reference< container::XNameAccess > mxNameAccess
virtual uno::Any SAL_CALL getByName(const OUString &aName) override
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual sal_Int32 SAL_CALL getDefaultSorting() override
virtual css::uno::Type SAL_CALL getElementType() override
css::uno::WeakReference< ov::XHelperInterface > m_xParent