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