Line data Source code
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 :
20 : #include "sal/config.h"
21 :
22 : #include "boost/noncopyable.hpp"
23 : #include "osl/time.h"
24 :
25 : #include <com/sun/star/uno/XComponentContext.hpp>
26 : #include <com/sun/star/office/XAnnotation.hpp>
27 : #include <com/sun/star/drawing/XDrawPage.hpp>
28 :
29 : #include <comphelper/processfactory.hxx>
30 : #include <cppuhelper/propertysetmixin.hxx>
31 : #include <cppuhelper/compbase1.hxx>
32 : #include <cppuhelper/basemutex.hxx>
33 :
34 : #include "Annotation.hxx"
35 : #include "drawdoc.hxx"
36 : #include "notifydocumentevent.hxx"
37 : #include "sdpage.hxx"
38 : #include "textapi.hxx"
39 :
40 : using namespace ::com::sun::star::uno;
41 : using namespace ::com::sun::star::lang;
42 : using namespace ::com::sun::star::beans;
43 : using namespace ::com::sun::star::office;
44 : using namespace ::com::sun::star::drawing;
45 : using namespace ::com::sun::star::geometry;
46 : using namespace ::com::sun::star::text;
47 : using namespace ::com::sun::star::util;
48 : using namespace ::com::sun::star;
49 :
50 : namespace sd {
51 :
52 0 : class Annotation : private ::cppu::BaseMutex,
53 : public ::cppu::WeakComponentImplHelper1< XAnnotation>,
54 : public ::cppu::PropertySetMixin< XAnnotation >,
55 : private boost::noncopyable
56 : {
57 : public:
58 : explicit Annotation( const Reference< XComponentContext >& context, SdPage* pPage );
59 :
60 0 : SdPage* GetPage() const { return mpPage; }
61 0 : SdrModel* GetModel() { return (mpPage != 0) ? mpPage->GetModel() : 0; }
62 :
63 : // XInterface:
64 : virtual Any SAL_CALL queryInterface(Type const & type) throw (RuntimeException, std::exception) SAL_OVERRIDE;
65 0 : virtual void SAL_CALL acquire() throw () SAL_OVERRIDE { ::cppu::WeakComponentImplHelper1< XAnnotation >::acquire(); }
66 0 : virtual void SAL_CALL release() throw () SAL_OVERRIDE { ::cppu::WeakComponentImplHelper1< XAnnotation >::release(); }
67 :
68 : // ::com::sun::star::beans::XPropertySet:
69 : virtual Reference< XPropertySetInfo > SAL_CALL getPropertySetInfo() throw (RuntimeException, std::exception) SAL_OVERRIDE;
70 : virtual void SAL_CALL setPropertyValue(const OUString & aPropertyName, const Any & aValue) throw (RuntimeException, UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, std::exception) SAL_OVERRIDE;
71 : virtual Any SAL_CALL getPropertyValue(const OUString & PropertyName) throw (RuntimeException, UnknownPropertyException, WrappedTargetException, std::exception) SAL_OVERRIDE;
72 : virtual void SAL_CALL addPropertyChangeListener(const OUString & aPropertyName, const Reference< XPropertyChangeListener > & xListener) throw (RuntimeException, UnknownPropertyException, WrappedTargetException, std::exception) SAL_OVERRIDE;
73 : virtual void SAL_CALL removePropertyChangeListener(const OUString & aPropertyName, const Reference< XPropertyChangeListener > & aListener) throw (RuntimeException, UnknownPropertyException, WrappedTargetException, std::exception) SAL_OVERRIDE;
74 : virtual void SAL_CALL addVetoableChangeListener(const OUString & PropertyName, const Reference< XVetoableChangeListener > & aListener) throw (RuntimeException, UnknownPropertyException, WrappedTargetException, std::exception) SAL_OVERRIDE;
75 : virtual void SAL_CALL removeVetoableChangeListener(const OUString & PropertyName, const Reference< XVetoableChangeListener > & aListener) throw (RuntimeException, UnknownPropertyException, WrappedTargetException, std::exception) SAL_OVERRIDE;
76 :
77 : // ::com::sun::star::office::XAnnotation:
78 : virtual ::com::sun::star::uno::Any SAL_CALL getAnchor() throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
79 : virtual RealPoint2D SAL_CALL getPosition() throw (RuntimeException, std::exception) SAL_OVERRIDE;
80 : virtual void SAL_CALL setPosition(const RealPoint2D & the_value) throw (RuntimeException, std::exception) SAL_OVERRIDE;
81 : virtual ::com::sun::star::geometry::RealSize2D SAL_CALL getSize() throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
82 : virtual void SAL_CALL setSize( const ::com::sun::star::geometry::RealSize2D& _size ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
83 : virtual OUString SAL_CALL getAuthor() throw (RuntimeException, std::exception) SAL_OVERRIDE;
84 : virtual void SAL_CALL setAuthor(const OUString & the_value) throw (RuntimeException, std::exception) SAL_OVERRIDE;
85 : virtual util::DateTime SAL_CALL getDateTime() throw (RuntimeException, std::exception) SAL_OVERRIDE;
86 : virtual void SAL_CALL setDateTime(const util::DateTime & the_value) throw (RuntimeException, std::exception) SAL_OVERRIDE;
87 : virtual Reference< XText > SAL_CALL getTextRange() throw (RuntimeException, std::exception) SAL_OVERRIDE;
88 :
89 : private:
90 : // destructor is private and will be called indirectly by the release call virtual ~Annotation() {}
91 :
92 : void createChangeUndo();
93 :
94 : // overload WeakComponentImplHelperBase::disposing()
95 : // This function is called upon disposing the component,
96 : // if your component needs special work when it becomes
97 : // disposed, do it here.
98 : virtual void SAL_CALL disposing() SAL_OVERRIDE;
99 :
100 : SdPage* mpPage;
101 : Reference< XComponentContext > m_xContext;
102 : mutable ::osl::Mutex m_aMutex;
103 : RealPoint2D m_Position;
104 : RealSize2D m_Size;
105 : OUString m_Author;
106 : util::DateTime m_DateTime;
107 : rtl::Reference< TextApiObject > m_TextRange;
108 : };
109 :
110 0 : class UndoInsertOrRemoveAnnotation : public SdrUndoAction
111 : {
112 : public:
113 : UndoInsertOrRemoveAnnotation( Annotation& rAnnotation, bool bInsert );
114 :
115 : virtual void Undo() SAL_OVERRIDE;
116 : virtual void Redo() SAL_OVERRIDE;
117 :
118 : protected:
119 : rtl::Reference< Annotation > mxAnnotation;
120 : bool mbInsert;
121 : int mnIndex;
122 : };
123 :
124 0 : struct AnnotationData
125 : {
126 : RealPoint2D m_Position;
127 : RealSize2D m_Size;
128 : OUString m_Author;
129 : util::DateTime m_DateTime;
130 :
131 0 : void get( const rtl::Reference< Annotation >& xAnnotation )
132 : {
133 0 : m_Position = xAnnotation->getPosition();
134 0 : m_Size = xAnnotation->getSize();
135 0 : m_Author = xAnnotation->getAuthor();
136 0 : m_DateTime = xAnnotation->getDateTime();
137 0 : }
138 :
139 0 : void set( const rtl::Reference< Annotation >& xAnnotation )
140 : {
141 0 : xAnnotation->setPosition(m_Position);
142 0 : xAnnotation->setSize(m_Size);
143 0 : xAnnotation->setAuthor(m_Author);
144 0 : xAnnotation->setDateTime(m_DateTime);
145 0 : }
146 : };
147 :
148 0 : class UndoAnnotation : public SdrUndoAction
149 : {
150 : public:
151 : UndoAnnotation( Annotation& rAnnotation );
152 :
153 : virtual void Undo() SAL_OVERRIDE;
154 : virtual void Redo() SAL_OVERRIDE;
155 :
156 : protected:
157 : rtl::Reference< Annotation > mxAnnotation;
158 : AnnotationData maUndoData;
159 : AnnotationData maRedoData;
160 : };
161 :
162 0 : void createAnnotation( Reference< XAnnotation >& xAnnotation, SdPage* pPage )
163 : {
164 : xAnnotation.set(
165 0 : new Annotation(comphelper::getProcessComponentContext(), pPage));
166 0 : pPage->addAnnotation(xAnnotation);
167 0 : }
168 :
169 0 : Annotation::Annotation( const Reference< XComponentContext >& context, SdPage* pPage )
170 : : ::cppu::WeakComponentImplHelper1< XAnnotation >(m_aMutex)
171 : , ::cppu::PropertySetMixin< XAnnotation >(context, static_cast< Implements >(IMPLEMENTS_PROPERTY_SET), Sequence< OUString >())
172 0 : , mpPage( pPage )
173 : {
174 0 : }
175 :
176 : // overload WeakComponentImplHelperBase::disposing()
177 : // This function is called upon disposing the component,
178 : // if your component needs special work when it becomes
179 : // disposed, do it here.
180 0 : void SAL_CALL Annotation::disposing()
181 : {
182 0 : mpPage = 0;
183 0 : if( m_TextRange.is() )
184 : {
185 0 : m_TextRange->dispose();
186 0 : m_TextRange.clear();
187 : }
188 0 : }
189 :
190 0 : Any Annotation::queryInterface(Type const & type) throw (RuntimeException, std::exception)
191 : {
192 0 : return ::cppu::WeakComponentImplHelper1< XAnnotation>::queryInterface(type);
193 : }
194 :
195 : // com.sun.star.beans.XPropertySet:
196 0 : Reference< XPropertySetInfo > SAL_CALL Annotation::getPropertySetInfo() throw (RuntimeException, std::exception)
197 : {
198 0 : return ::cppu::PropertySetMixin< XAnnotation >::getPropertySetInfo();
199 : }
200 :
201 0 : void SAL_CALL Annotation::setPropertyValue(const OUString & aPropertyName, const Any & aValue) throw (RuntimeException, UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, std::exception)
202 : {
203 0 : ::cppu::PropertySetMixin< XAnnotation >::setPropertyValue(aPropertyName, aValue);
204 0 : }
205 :
206 0 : Any SAL_CALL Annotation::getPropertyValue(const OUString & aPropertyName) throw (RuntimeException, UnknownPropertyException, WrappedTargetException, std::exception)
207 : {
208 0 : return ::cppu::PropertySetMixin< XAnnotation >::getPropertyValue(aPropertyName);
209 : }
210 :
211 0 : void SAL_CALL Annotation::addPropertyChangeListener(const OUString & aPropertyName, const Reference< XPropertyChangeListener > & xListener) throw (RuntimeException, UnknownPropertyException, WrappedTargetException, std::exception)
212 : {
213 0 : ::cppu::PropertySetMixin< XAnnotation >::addPropertyChangeListener(aPropertyName, xListener);
214 0 : }
215 :
216 0 : void SAL_CALL Annotation::removePropertyChangeListener(const OUString & aPropertyName, const Reference< XPropertyChangeListener > & xListener) throw (RuntimeException, UnknownPropertyException, WrappedTargetException, std::exception)
217 : {
218 0 : ::cppu::PropertySetMixin< XAnnotation >::removePropertyChangeListener(aPropertyName, xListener);
219 0 : }
220 :
221 0 : void SAL_CALL Annotation::addVetoableChangeListener(const OUString & aPropertyName, const Reference< XVetoableChangeListener > & xListener) throw (RuntimeException, UnknownPropertyException, WrappedTargetException, std::exception)
222 : {
223 0 : ::cppu::PropertySetMixin< XAnnotation >::addVetoableChangeListener(aPropertyName, xListener);
224 0 : }
225 :
226 0 : void SAL_CALL Annotation::removeVetoableChangeListener(const OUString & aPropertyName, const Reference< XVetoableChangeListener > & xListener) throw (RuntimeException, UnknownPropertyException, WrappedTargetException, std::exception)
227 : {
228 0 : ::cppu::PropertySetMixin< XAnnotation >::removeVetoableChangeListener(aPropertyName, xListener);
229 0 : }
230 :
231 0 : Any SAL_CALL Annotation::getAnchor() throw (RuntimeException, std::exception)
232 : {
233 0 : osl::MutexGuard g(m_aMutex);
234 0 : Any aRet;
235 0 : if( mpPage )
236 : {
237 0 : Reference< XDrawPage > xPage( mpPage->getUnoPage(), UNO_QUERY );
238 0 : aRet <<= xPage;
239 : }
240 0 : return aRet;
241 : }
242 :
243 : // ::com::sun::star::office::XAnnotation:
244 0 : RealPoint2D SAL_CALL Annotation::getPosition() throw (RuntimeException, std::exception)
245 : {
246 0 : osl::MutexGuard g(m_aMutex);
247 0 : return m_Position;
248 : }
249 :
250 0 : void SAL_CALL Annotation::setPosition(const RealPoint2D & the_value) throw (RuntimeException, std::exception)
251 : {
252 0 : prepareSet("Position", Any(), Any(), 0);
253 : {
254 0 : osl::MutexGuard g(m_aMutex);
255 0 : createChangeUndo();
256 0 : m_Position = the_value;
257 : }
258 0 : }
259 :
260 : // ::com::sun::star::office::XAnnotation:
261 0 : RealSize2D SAL_CALL Annotation::getSize() throw (RuntimeException, std::exception)
262 : {
263 0 : osl::MutexGuard g(m_aMutex);
264 0 : return m_Size;
265 : }
266 :
267 0 : void SAL_CALL Annotation::setSize(const RealSize2D & the_value) throw (RuntimeException, std::exception)
268 : {
269 0 : prepareSet("Size", Any(), Any(), 0);
270 : {
271 0 : osl::MutexGuard g(m_aMutex);
272 0 : createChangeUndo();
273 0 : m_Size = the_value;
274 : }
275 0 : }
276 :
277 0 : OUString SAL_CALL Annotation::getAuthor() throw (RuntimeException, std::exception)
278 : {
279 0 : osl::MutexGuard g(m_aMutex);
280 0 : return m_Author;
281 : }
282 :
283 0 : void SAL_CALL Annotation::setAuthor(const OUString & the_value) throw (RuntimeException, std::exception)
284 : {
285 0 : prepareSet("Author", Any(), Any(), 0);
286 : {
287 0 : osl::MutexGuard g(m_aMutex);
288 0 : createChangeUndo();
289 0 : m_Author = the_value;
290 : }
291 0 : }
292 :
293 0 : util::DateTime SAL_CALL Annotation::getDateTime() throw (RuntimeException, std::exception)
294 : {
295 0 : osl::MutexGuard g(m_aMutex);
296 0 : return m_DateTime;
297 : }
298 :
299 0 : void SAL_CALL Annotation::setDateTime(const util::DateTime & the_value) throw (RuntimeException, std::exception)
300 : {
301 0 : prepareSet("DateTime", Any(), Any(), 0);
302 : {
303 0 : osl::MutexGuard g(m_aMutex);
304 0 : createChangeUndo();
305 0 : m_DateTime = the_value;
306 : }
307 0 : }
308 :
309 0 : void Annotation::createChangeUndo()
310 : {
311 0 : SdrModel* pModel = GetModel();
312 0 : if( pModel && pModel->IsUndoEnabled() )
313 0 : pModel->AddUndo( new UndoAnnotation( *this ) );
314 :
315 0 : if( pModel )
316 : {
317 0 : pModel->SetChanged();
318 0 : Reference< XInterface > xSource( static_cast<uno::XWeak*>( this ) );
319 0 : NotifyDocumentEvent( static_cast< SdDrawDocument* >( pModel ), "OnAnnotationChanged" , xSource );
320 : }
321 0 : }
322 :
323 0 : Reference< XText > SAL_CALL Annotation::getTextRange() throw (RuntimeException, std::exception)
324 : {
325 0 : osl::MutexGuard g(m_aMutex);
326 0 : if( !m_TextRange.is() && (mpPage != 0) )
327 : {
328 0 : m_TextRange = TextApiObject::create( static_cast< SdDrawDocument* >( mpPage->GetModel() ) );
329 : }
330 0 : return Reference< XText >( m_TextRange.get() );
331 : }
332 :
333 0 : SdrUndoAction* CreateUndoInsertOrRemoveAnnotation( const Reference< XAnnotation >& xAnnotation, bool bInsert )
334 : {
335 0 : Annotation* pAnnotation = dynamic_cast< Annotation* >( xAnnotation.get() );
336 0 : if( pAnnotation )
337 : {
338 0 : return new UndoInsertOrRemoveAnnotation( *pAnnotation, bInsert );
339 : }
340 : else
341 : {
342 0 : return 0;
343 : }
344 : }
345 :
346 0 : UndoInsertOrRemoveAnnotation::UndoInsertOrRemoveAnnotation( Annotation& rAnnotation, bool bInsert )
347 0 : : SdrUndoAction( *rAnnotation.GetModel() )
348 : , mxAnnotation( &rAnnotation )
349 : , mbInsert( bInsert )
350 0 : , mnIndex( 0 )
351 : {
352 0 : SdPage* pPage = rAnnotation.GetPage();
353 0 : if( pPage )
354 : {
355 0 : Reference< XAnnotation > xAnnotation( &rAnnotation );
356 :
357 0 : const AnnotationVector& rVec = pPage->getAnnotations();
358 0 : for( AnnotationVector::const_iterator iter = rVec.begin(); iter != rVec.end(); ++iter )
359 : {
360 0 : if( (*iter) == xAnnotation )
361 0 : break;
362 :
363 0 : mnIndex++;
364 0 : }
365 : }
366 0 : }
367 :
368 0 : void UndoInsertOrRemoveAnnotation::Undo()
369 : {
370 0 : SdPage* pPage = mxAnnotation->GetPage();
371 0 : SdrModel* pModel = mxAnnotation->GetModel();
372 0 : if( pPage && pModel )
373 : {
374 0 : Reference< XAnnotation > xAnnotation( mxAnnotation.get() );
375 0 : if( mbInsert )
376 : {
377 0 : pPage->removeAnnotation( xAnnotation );
378 : }
379 : else
380 : {
381 0 : pPage->addAnnotation( xAnnotation, mnIndex );
382 0 : }
383 : }
384 0 : }
385 :
386 0 : void UndoInsertOrRemoveAnnotation::Redo()
387 : {
388 0 : SdPage* pPage = mxAnnotation->GetPage();
389 0 : SdrModel* pModel = mxAnnotation->GetModel();
390 0 : if( pPage && pModel )
391 : {
392 0 : Reference< XAnnotation > xAnnotation( mxAnnotation.get() );
393 :
394 0 : if( mbInsert )
395 : {
396 0 : pPage->addAnnotation( xAnnotation, mnIndex );
397 : }
398 : else
399 : {
400 0 : pPage->removeAnnotation( xAnnotation );
401 0 : }
402 : }
403 0 : }
404 :
405 0 : UndoAnnotation::UndoAnnotation( Annotation& rAnnotation )
406 0 : : SdrUndoAction( *rAnnotation.GetModel() )
407 0 : , mxAnnotation( &rAnnotation )
408 : {
409 0 : maUndoData.get( mxAnnotation );
410 0 : }
411 :
412 0 : void UndoAnnotation::Undo()
413 : {
414 0 : maRedoData.get( mxAnnotation );
415 0 : maUndoData.set( mxAnnotation );
416 0 : }
417 :
418 0 : void UndoAnnotation::Redo()
419 : {
420 0 : maUndoData.get( mxAnnotation );
421 0 : maRedoData.set( mxAnnotation );
422 0 : }
423 :
424 114 : } // namespace sd
425 :
426 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|