Update ooo320-m1
[ooovba.git] / sd / source / core / annotations / Annotation.cxx
blob292abc8f42cea4eddd19e65c9ab2930bf90486a3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: unopage.cxx,v $
10 * $Revision: 1.96 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
34 #include "osl/time.h"
35 #include "sal/config.h"
37 #include <com/sun/star/uno/XComponentContext.hpp>
38 #include <com/sun/star/office/XAnnotation.hpp>
39 #include <com/sun/star/drawing/XDrawPage.hpp>
41 #include <comphelper/processfactory.hxx>
42 #include <cppuhelper/propertysetmixin.hxx>
43 #include <cppuhelper/compbase1.hxx>
44 #include <cppuhelper/basemutex.hxx>
46 #include "drawdoc.hxx"
47 #include "sdpage.hxx"
48 #include "textapi.hxx"
50 using ::rtl::OUString;
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::lang;
53 using namespace ::com::sun::star::beans;
54 using namespace ::com::sun::star::office;
55 using namespace ::com::sun::star::drawing;
56 using namespace ::com::sun::star::geometry;
57 using namespace ::com::sun::star::text;
58 using namespace ::com::sun::star::util;
59 using namespace ::com::sun::star;
61 extern void NotifyDocumentEvent( SdDrawDocument* pDocument, const rtl::OUString& rEventName, const Reference< XInterface >& xSource );
63 namespace sd {
65 class Annotation : private ::cppu::BaseMutex,
66 public ::cppu::WeakComponentImplHelper1< XAnnotation>,
67 public ::cppu::PropertySetMixin< XAnnotation >
69 public:
70 explicit Annotation( const Reference< XComponentContext >& context, SdPage* pPage );
72 SdPage* GetPage() const { return mpPage; }
73 SdrModel* GetModel() { return (mpPage != 0) ? mpPage->GetModel() : 0; }
75 // XInterface:
76 virtual Any SAL_CALL queryInterface(Type const & type) throw (RuntimeException);
77 virtual void SAL_CALL acquire() throw () { ::cppu::WeakComponentImplHelper1< XAnnotation >::acquire(); }
78 virtual void SAL_CALL release() throw () { ::cppu::WeakComponentImplHelper1< XAnnotation >::release(); }
80 // ::com::sun::star::beans::XPropertySet:
81 virtual Reference< XPropertySetInfo > SAL_CALL getPropertySetInfo() throw (RuntimeException);
82 virtual void SAL_CALL setPropertyValue(const OUString & aPropertyName, const Any & aValue) throw (RuntimeException, UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException);
83 virtual Any SAL_CALL getPropertyValue(const OUString & PropertyName) throw (RuntimeException, UnknownPropertyException, WrappedTargetException);
84 virtual void SAL_CALL addPropertyChangeListener(const OUString & aPropertyName, const Reference< XPropertyChangeListener > & xListener) throw (RuntimeException, UnknownPropertyException, WrappedTargetException);
85 virtual void SAL_CALL removePropertyChangeListener(const OUString & aPropertyName, const Reference< XPropertyChangeListener > & aListener) throw (RuntimeException, UnknownPropertyException, WrappedTargetException);
86 virtual void SAL_CALL addVetoableChangeListener(const OUString & PropertyName, const Reference< XVetoableChangeListener > & aListener) throw (RuntimeException, UnknownPropertyException, WrappedTargetException);
87 virtual void SAL_CALL removeVetoableChangeListener(const OUString & PropertyName, const Reference< XVetoableChangeListener > & aListener) throw (RuntimeException, UnknownPropertyException, WrappedTargetException);
89 // ::com::sun::star::office::XAnnotation:
90 virtual ::com::sun::star::uno::Any SAL_CALL getAnchor() throw (::com::sun::star::uno::RuntimeException);
91 virtual RealPoint2D SAL_CALL getPosition() throw (RuntimeException);
92 virtual void SAL_CALL setPosition(const RealPoint2D & the_value) throw (RuntimeException);
93 virtual ::com::sun::star::geometry::RealSize2D SAL_CALL getSize() throw (::com::sun::star::uno::RuntimeException);
94 virtual void SAL_CALL setSize( const ::com::sun::star::geometry::RealSize2D& _size ) throw (::com::sun::star::uno::RuntimeException);
95 virtual OUString SAL_CALL getAuthor() throw (RuntimeException);
96 virtual void SAL_CALL setAuthor(const OUString & the_value) throw (RuntimeException);
97 virtual util::DateTime SAL_CALL getDateTime() throw (RuntimeException);
98 virtual void SAL_CALL setDateTime(const util::DateTime & the_value) throw (RuntimeException);
99 virtual Reference< XText > SAL_CALL getTextRange() throw (RuntimeException);
101 private:
102 Annotation(const Annotation &); // not defined
103 Annotation& operator=(const Annotation &); // not defined
105 // destructor is private and will be called indirectly by the release call virtual ~Annotation() {}
107 void createChangeUndo();
109 // overload WeakComponentImplHelperBase::disposing()
110 // This function is called upon disposing the component,
111 // if your component needs special work when it becomes
112 // disposed, do it here.
113 virtual void SAL_CALL disposing();
115 SdPage* mpPage;
116 Reference< XComponentContext > m_xContext;
117 mutable ::osl::Mutex m_aMutex;
118 RealPoint2D m_Position;
119 RealSize2D m_Size;
120 OUString m_Author;
121 util::DateTime m_DateTime;
122 rtl::Reference< TextApiObject > m_TextRange;
125 class UndoInsertOrRemoveAnnotation : public SdrUndoAction
127 public:
128 UndoInsertOrRemoveAnnotation( Annotation& rAnnotation, bool bInsert );
130 virtual void Undo();
131 virtual void Redo();
133 protected:
134 rtl::Reference< Annotation > mxAnnotation;
135 bool mbInsert;
136 int mnIndex;
139 struct AnnotationData
141 RealPoint2D m_Position;
142 RealSize2D m_Size;
143 OUString m_Author;
144 util::DateTime m_DateTime;
146 void get( const rtl::Reference< Annotation >& xAnnotation )
148 m_Position = xAnnotation->getPosition();
149 m_Size = xAnnotation->getSize();
150 m_Author = xAnnotation->getAuthor();
151 m_DateTime = xAnnotation->getDateTime();
154 void set( const rtl::Reference< Annotation >& xAnnotation )
156 xAnnotation->setPosition(m_Position);
157 xAnnotation->setSize(m_Size);
158 xAnnotation->setAuthor(m_Author);
159 xAnnotation->setDateTime(m_DateTime);
163 class UndoAnnotation : public SdrUndoAction
165 public:
166 UndoAnnotation( Annotation& rAnnotation );
168 virtual void Undo();
169 virtual void Redo();
171 protected:
172 rtl::Reference< Annotation > mxAnnotation;
173 AnnotationData maUndoData;
174 AnnotationData maRedoData;
177 void createAnnotation( Reference< XAnnotation >& xAnnotation, SdPage* pPage )
179 Reference<XComponentContext> xContext (comphelper_getProcessComponentContext());
180 xAnnotation.set( new Annotation(xContext, pPage) );
181 pPage->addAnnotation(xAnnotation);
184 Annotation::Annotation( const Reference< XComponentContext >& context, SdPage* pPage )
185 : ::cppu::WeakComponentImplHelper1< XAnnotation >(m_aMutex)
186 , ::cppu::PropertySetMixin< XAnnotation >(context, static_cast< Implements >(IMPLEMENTS_PROPERTY_SET), Sequence< ::rtl::OUString >())
187 , mpPage( pPage )
191 // overload WeakComponentImplHelperBase::disposing()
192 // This function is called upon disposing the component,
193 // if your component needs special work when it becomes
194 // disposed, do it here.
195 void SAL_CALL Annotation::disposing()
197 mpPage = 0;
198 if( m_TextRange.is() )
200 m_TextRange->dispose();
201 m_TextRange.clear();
205 Any Annotation::queryInterface(Type const & type) throw (RuntimeException)
207 return ::cppu::WeakComponentImplHelper1< XAnnotation>::queryInterface(type);
210 // com.sun.star.beans.XPropertySet:
211 Reference< XPropertySetInfo > SAL_CALL Annotation::getPropertySetInfo() throw (RuntimeException)
213 return ::cppu::PropertySetMixin< XAnnotation >::getPropertySetInfo();
216 void SAL_CALL Annotation::setPropertyValue(const OUString & aPropertyName, const Any & aValue) throw (RuntimeException, UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException)
218 ::cppu::PropertySetMixin< XAnnotation >::setPropertyValue(aPropertyName, aValue);
221 Any SAL_CALL Annotation::getPropertyValue(const OUString & aPropertyName) throw (RuntimeException, UnknownPropertyException, WrappedTargetException)
223 return ::cppu::PropertySetMixin< XAnnotation >::getPropertyValue(aPropertyName);
226 void SAL_CALL Annotation::addPropertyChangeListener(const OUString & aPropertyName, const Reference< XPropertyChangeListener > & xListener) throw (RuntimeException, UnknownPropertyException, WrappedTargetException)
228 ::cppu::PropertySetMixin< XAnnotation >::addPropertyChangeListener(aPropertyName, xListener);
231 void SAL_CALL Annotation::removePropertyChangeListener(const OUString & aPropertyName, const Reference< XPropertyChangeListener > & xListener) throw (RuntimeException, UnknownPropertyException, WrappedTargetException)
233 ::cppu::PropertySetMixin< XAnnotation >::removePropertyChangeListener(aPropertyName, xListener);
236 void SAL_CALL Annotation::addVetoableChangeListener(const OUString & aPropertyName, const Reference< XVetoableChangeListener > & xListener) throw (RuntimeException, UnknownPropertyException, WrappedTargetException)
238 ::cppu::PropertySetMixin< XAnnotation >::addVetoableChangeListener(aPropertyName, xListener);
241 void SAL_CALL Annotation::removeVetoableChangeListener(const OUString & aPropertyName, const Reference< XVetoableChangeListener > & xListener) throw (RuntimeException, UnknownPropertyException, WrappedTargetException)
243 ::cppu::PropertySetMixin< XAnnotation >::removeVetoableChangeListener(aPropertyName, xListener);
246 Any SAL_CALL Annotation::getAnchor() throw (RuntimeException)
248 osl::MutexGuard g(m_aMutex);
249 Any aRet;
250 if( mpPage )
252 Reference< XDrawPage > xPage( mpPage->getUnoPage(), UNO_QUERY );
253 aRet <<= xPage;
255 return aRet;
258 // ::com::sun::star::office::XAnnotation:
259 RealPoint2D SAL_CALL Annotation::getPosition() throw (RuntimeException)
261 osl::MutexGuard g(m_aMutex);
262 return m_Position;
265 void SAL_CALL Annotation::setPosition(const RealPoint2D & the_value) throw (RuntimeException)
267 prepareSet(
268 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Position")),
269 Any(), Any(), 0);
271 osl::MutexGuard g(m_aMutex);
272 createChangeUndo();
273 m_Position = the_value;
277 // ::com::sun::star::office::XAnnotation:
278 RealSize2D SAL_CALL Annotation::getSize() throw (RuntimeException)
280 osl::MutexGuard g(m_aMutex);
281 return m_Size;
284 void SAL_CALL Annotation::setSize(const RealSize2D & the_value) throw (RuntimeException)
286 prepareSet(
287 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Size")),
288 Any(), Any(), 0);
290 osl::MutexGuard g(m_aMutex);
291 createChangeUndo();
292 m_Size = the_value;
296 OUString SAL_CALL Annotation::getAuthor() throw (RuntimeException)
298 osl::MutexGuard g(m_aMutex);
299 return m_Author;
302 void SAL_CALL Annotation::setAuthor(const OUString & the_value) throw (RuntimeException)
304 prepareSet(
305 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Author")),
306 Any(), Any(), 0);
308 osl::MutexGuard g(m_aMutex);
309 createChangeUndo();
310 m_Author = the_value;
314 util::DateTime SAL_CALL Annotation::getDateTime() throw (RuntimeException)
316 osl::MutexGuard g(m_aMutex);
317 return m_DateTime;
320 void SAL_CALL Annotation::setDateTime(const util::DateTime & the_value) throw (RuntimeException)
322 prepareSet(
323 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DateTime")),
324 Any(), Any(), 0);
326 osl::MutexGuard g(m_aMutex);
327 createChangeUndo();
328 m_DateTime = the_value;
332 void Annotation::createChangeUndo()
334 SdrModel* pModel = GetModel();
335 if( pModel && pModel->IsUndoEnabled() )
336 pModel->AddUndo( new UndoAnnotation( *this ) );
338 if( pModel )
340 pModel->SetChanged();
341 Reference< XInterface > xSource( static_cast<uno::XWeak*>( this ) );
342 NotifyDocumentEvent( static_cast< SdDrawDocument* >( pModel ), OUString( RTL_CONSTASCII_USTRINGPARAM("OnAnnotationChanged") ), xSource );
346 Reference< XText > SAL_CALL Annotation::getTextRange() throw (RuntimeException)
348 osl::MutexGuard g(m_aMutex);
349 if( !m_TextRange.is() && (mpPage != 0) )
351 m_TextRange = TextApiObject::create( static_cast< SdDrawDocument* >( mpPage->GetModel() ) );
353 return Reference< XText >( m_TextRange.get() );
356 SdrUndoAction* CreateUndoInsertOrRemoveAnnotation( const Reference< XAnnotation >& xAnnotation, bool bInsert )
358 Annotation* pAnnotation = dynamic_cast< Annotation* >( xAnnotation.get() );
359 if( pAnnotation )
361 return new UndoInsertOrRemoveAnnotation( *pAnnotation, bInsert );
363 else
365 return 0;
369 UndoInsertOrRemoveAnnotation::UndoInsertOrRemoveAnnotation( Annotation& rAnnotation, bool bInsert )
370 : SdrUndoAction( *rAnnotation.GetModel() )
371 , mxAnnotation( &rAnnotation )
372 , mbInsert( bInsert )
373 , mnIndex( 0 )
375 SdPage* pPage = rAnnotation.GetPage();
376 if( pPage )
378 Reference< XAnnotation > xAnnotation( &rAnnotation );
380 const AnnotationVector& rVec = pPage->getAnnotations();
381 for( AnnotationVector::const_iterator iter = rVec.begin(); iter != rVec.end(); iter++ )
383 if( (*iter) == xAnnotation )
384 break;
386 mnIndex++;
391 void UndoInsertOrRemoveAnnotation::Undo()
393 SdPage* pPage = mxAnnotation->GetPage();
394 SdrModel* pModel = mxAnnotation->GetModel();
395 if( pPage && pModel )
397 Reference< XAnnotation > xAnnotation( mxAnnotation.get() );
398 if( mbInsert )
400 pPage->removeAnnotation( xAnnotation );
402 else
404 pPage->addAnnotation( xAnnotation, mnIndex );
409 void UndoInsertOrRemoveAnnotation::Redo()
411 SdPage* pPage = mxAnnotation->GetPage();
412 SdrModel* pModel = mxAnnotation->GetModel();
413 if( pPage && pModel )
415 Reference< XAnnotation > xAnnotation( mxAnnotation.get() );
417 if( mbInsert )
419 pPage->addAnnotation( xAnnotation, mnIndex );
421 else
423 pPage->removeAnnotation( xAnnotation );
428 UndoAnnotation::UndoAnnotation( Annotation& rAnnotation )
429 : SdrUndoAction( *rAnnotation.GetModel() )
430 , mxAnnotation( &rAnnotation )
432 maUndoData.get( mxAnnotation );
435 void UndoAnnotation::Undo()
437 maRedoData.get( mxAnnotation );
438 maUndoData.set( mxAnnotation );
441 void UndoAnnotation::Redo()
443 maUndoData.get( mxAnnotation );
444 maRedoData.set( mxAnnotation );
447 } // namespace sd