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 <ooo/vba/word/WdBookmarkSortBy.hpp>
26 #include "vbarange.hxx"
27 #include "wordvbahelper.hxx"
28 #include <cppuhelper/implbase.hxx>
29 
30 using namespace ::ooo::vba;
31 using namespace ::com::sun::star;
32 
33 namespace {
34 
35 class BookmarksEnumeration : public EnumerationHelperImpl
36 {
37  uno::Reference< frame::XModel > mxModel;
38 public:
40  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 ) {}
41 
42  virtual uno::Any SAL_CALL nextElement( ) override
43  {
44  uno::Reference< container::XNamed > xNamed( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
45  OUString aName = xNamed->getName();
46  return uno::makeAny( uno::Reference< word::XBookmark > ( new SwVbaBookmark( m_xParent, m_xContext, mxModel, aName ) ) );
47  }
48 
49 };
50 
51 // Bookmarks use case-insensitive name lookup in MS Word.
52 class BookmarkCollectionHelper : public ::cppu::WeakImplHelper< container::XNameAccess,
53  container::XIndexAccess >
54 {
55 private:
56  uno::Reference< container::XNameAccess > mxNameAccess;
57  uno::Reference< container::XIndexAccess > mxIndexAccess;
58  uno::Any cachePos;
59 public:
61  explicit BookmarkCollectionHelper( const uno::Reference< container::XIndexAccess >& xIndexAccess ) : mxIndexAccess( xIndexAccess )
62  {
63  mxNameAccess.set( mxIndexAccess, uno::UNO_QUERY_THROW );
64  }
65  // XElementAccess
66  virtual uno::Type SAL_CALL getElementType( ) override { return mxIndexAccess->getElementType(); }
67  virtual sal_Bool SAL_CALL hasElements( ) override { return mxIndexAccess->hasElements(); }
68  // XNameAccess
69  virtual uno::Any SAL_CALL getByName( const OUString& aName ) override
70  {
71  if ( !hasByName(aName) )
72  throw container::NoSuchElementException();
73  return cachePos;
74  }
75  virtual uno::Sequence< OUString > SAL_CALL getElementNames( ) override
76  {
77  return mxNameAccess->getElementNames();
78  }
79  virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override
80  {
81  if( mxNameAccess->hasByName( aName ) )
82  {
83  cachePos = mxNameAccess->getByName( aName );
84  return true;
85  }
86  else
87  {
88  for( sal_Int32 nIndex = 0; nIndex < mxIndexAccess->getCount(); nIndex++ )
89  {
90  uno::Reference< container::XNamed > xNamed( mxIndexAccess->getByIndex( nIndex ), uno::UNO_QUERY_THROW );
91  OUString aBookmarkName = xNamed->getName();
92  if( aName.equalsIgnoreAsciiCase( aBookmarkName ) )
93  {
94  cachePos <<= xNamed;
95  return true;
96  }
97  }
98  }
99  return false;
100  }
101  // XIndexAccess
102  virtual ::sal_Int32 SAL_CALL getCount( ) override
103  {
104  return mxIndexAccess->getCount();
105  }
106  virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
107  {
108  return mxIndexAccess->getByIndex( Index );
109  }
110 };
111 
112 }
113 
114 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 )
115 {
116  mxBookmarksSupplier.set( mxModel, uno::UNO_QUERY_THROW );
117  uno::Reference< text::XTextDocument > xDocument( mxModel, uno::UNO_QUERY_THROW );
118 }
119 // XEnumerationAccess
120 uno::Type
122 {
124 }
125 uno::Reference< container::XEnumeration >
127 {
128  uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
129  return new BookmarksEnumeration( getParent(), mxContext,xEnumAccess->createEnumeration(), mxModel );
130 }
131 
132 uno::Any
133 SwVbaBookmarks::createCollectionObject( const css::uno::Any& aSource )
134 {
135  uno::Reference< container::XNamed > xNamed( aSource, uno::UNO_QUERY_THROW );
136  OUString aName = xNamed->getName();
137  return uno::makeAny( uno::Reference< word::XBookmark > ( new SwVbaBookmark( getParent(), mxContext, mxModel, aName ) ) );
138 }
139 
140 void SwVbaBookmarks::removeBookmarkByName( const OUString& rName )
141 {
142  uno::Reference< text::XTextContent > xBookmark( m_xNameAccess->getByName( rName ), uno::UNO_QUERY_THROW );
143  word::getXTextViewCursor( mxModel )->getText()->removeTextContent( xBookmark );
144 }
145 
146 void SwVbaBookmarks::addBookmarkByName( const uno::Reference< frame::XModel >& xModel, const OUString& rName, const uno::Reference< text::XTextRange >& rTextRange )
147 {
148  uno::Reference< lang::XMultiServiceFactory > xDocMSF( xModel, uno::UNO_QUERY_THROW );
149  uno::Reference< text::XTextContent > xBookmark( xDocMSF->createInstance("com.sun.star.text.Bookmark"), uno::UNO_QUERY_THROW );
150  uno::Reference< container::XNamed > xNamed( xBookmark, uno::UNO_QUERY_THROW );
151  xNamed->setName( rName );
152  rTextRange->getText()->insertTextContent( rTextRange, xBookmark, false );
153 }
154 
155 uno::Any SAL_CALL
156 SwVbaBookmarks::Add( const OUString& rName, const uno::Any& rRange )
157 {
158  uno::Reference< text::XTextRange > xTextRange;
159  uno::Reference< word::XRange > xRange;
160  if( rRange >>= xRange )
161  {
162  SwVbaRange* pRange = dynamic_cast< SwVbaRange* >( xRange.get() );
163  if( pRange )
164  xTextRange = pRange->getXTextRange();
165  }
166  else
167  {
168  // FIXME: insert the bookmark into current view cursor
169  xTextRange.set( word::getXTextViewCursor( mxModel ), uno::UNO_QUERY_THROW );
170  }
171 
172  // remove the exist bookmark
173  if( m_xNameAccess->hasByName( rName ) )
174  removeBookmarkByName( rName );
175 
176  addBookmarkByName( mxModel, rName, xTextRange );
177 
178  return uno::makeAny( uno::Reference< word::XBookmark >( new SwVbaBookmark( getParent(), mxContext, mxModel, rName ) ) );
179 }
180 
181 sal_Int32 SAL_CALL
183 {
184  return word::WdBookmarkSortBy::wdSortByName;
185 }
186 
187 void SAL_CALL
188 SwVbaBookmarks::setDefaultSorting( sal_Int32/* _type*/ )
189 {
190  // not support in Writer
191 }
192 
193 sal_Bool SAL_CALL
195 {
196  return true;
197 }
198 
199 void SAL_CALL
201 {
202  // not support in Writer
203 }
204 
205 sal_Bool SAL_CALL
206 SwVbaBookmarks::Exists( const OUString& rName )
207 {
208  bool bExist = m_xNameAccess->hasByName( rName );
209  return bExist;
210 }
211 
212 OUString
214 {
215  return "SwVbaBookmarks";
216 }
217 
218 css::uno::Sequence<OUString>
220 {
221  static uno::Sequence< OUString > const sNames
222  {
223  "ooo.vba.word.Bookmarks"
224  };
225  return sNames;
226 }
227 
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void removeBookmarkByName(const OUString &rName)
sal_Int32 nIndex
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
virtual void SAL_CALL setShowHidden(sal_Bool _hidden) override
Reference
css::uno::Reference< css::frame::XModel2 > mxModel
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
exports com.sun.star. container
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:83
uno::Reference< text::XTextViewCursor > getXTextViewCursor(const uno::Reference< frame::XModel > &xModel)
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()
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
OUString aName
virtual css::uno::Any SAL_CALL Add(const OUString &rName, const css::uno::Any &rRange) override
virtual sal_Int32 SAL_CALL getDefaultSorting() override
struct _ADOIndex Index
virtual css::uno::Type SAL_CALL getElementType() override