sync master with lastest vba changes
[ooovba.git] / reportdesign / source / ui / misc / Undo.cxx
blob1542adfe729b5a66e50c791867c5805fb2bc0291
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: Undo.cxx,v $
10 * $Revision: 1.6 $
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 ************************************************************************/
30 #include "precompiled_reportdesign.hxx"
32 #include "Undo.hxx"
33 #ifndef REPORTDESIGN_SHARED_UISTRINGS_HRC
34 #include "uistrings.hrc"
35 #endif
36 #ifndef _RPTUI_SLOTID_HRC_
37 #include "rptui_slotid.hrc"
38 #endif
39 #ifndef RPTUI_TOOLS_HXX
40 #include "UITools.hxx"
41 #endif
42 #include <dbaccess/IController.hxx>
43 #include <com/sun/star/report/XSection.hpp>
44 #include <com/sun/star/beans/PropertyAttribute.hpp>
46 #include "UndoEnv.hxx"
47 #include <com/sun/star/awt/Point.hpp>
48 #include <com/sun/star/awt/Size.hpp>
49 #include <svx/unoshape.hxx>
50 #include <boost/bind.hpp>
51 #include <functional>
53 namespace rptui
55 using namespace ::com::sun::star;
56 using namespace uno;
57 using namespace lang;
58 using namespace script;
59 using namespace beans;
60 using namespace awt;
61 using namespace util;
62 using namespace container;
63 using namespace report;
65 //----------------------------------------------------------------------------
66 namespace
68 void lcl_collectElements(const uno::Reference< report::XSection >& _xSection,::std::vector< uno::Reference< drawing::XShape> >& _rControls)
70 if ( _xSection.is() )
72 sal_Int32 nCount = _xSection->getCount();
73 _rControls.reserve(nCount);
74 while ( nCount )
76 uno::Reference< drawing::XShape> xShape(_xSection->getByIndex(nCount-1),uno::UNO_QUERY);
77 _rControls.push_back(xShape);
78 _xSection->remove(xShape);
79 --nCount;
81 } // if ( _xSection.is() )
83 //----------------------------------------------------------------------------
84 void lcl_insertElements(const uno::Reference< report::XSection >& _xSection,const ::std::vector< uno::Reference< drawing::XShape> >& _aControls)
86 if ( _xSection.is() )
88 ::std::vector< uno::Reference< drawing::XShape> >::const_reverse_iterator aIter = _aControls.rbegin();
89 ::std::vector< uno::Reference< drawing::XShape> >::const_reverse_iterator aEnd = _aControls.rend();
90 for (; aIter != aEnd; ++aIter)
92 try
94 const awt::Point aPos = (*aIter)->getPosition();
95 const awt::Size aSize = (*aIter)->getSize();
96 _xSection->add(*aIter);
97 (*aIter)->setPosition( aPos );
98 (*aIter)->setSize( aSize );
100 catch(const uno::Exception&)
102 OSL_ENSURE(0,"lcl_insertElements:Exception caught!");
107 //----------------------------------------------------------------------------
108 void lcl_setValues(const uno::Reference< report::XSection >& _xSection,const ::std::vector< ::std::pair< ::rtl::OUString ,uno::Any> >& _aValues)
110 if ( _xSection.is() )
112 ::std::vector< ::std::pair< ::rtl::OUString ,uno::Any> >::const_iterator aIter = _aValues.begin();
113 ::std::vector< ::std::pair< ::rtl::OUString ,uno::Any> >::const_iterator aEnd = _aValues.end();
114 for (; aIter != aEnd; ++aIter)
118 _xSection->setPropertyValue(aIter->first,aIter->second);
120 catch(const uno::Exception&)
122 OSL_ENSURE(0,"lcl_setValues:Exception caught!");
128 //----------------------------------------------------------------------------
129 TYPEINIT1( OSectionUndo, OCommentUndoAction );
130 DBG_NAME(rpt_OSectionUndo)
131 //----------------------------------------------------------------------------
132 OSectionUndo::OSectionUndo(OReportModel& _rMod
133 ,sal_uInt16 _nSlot
134 ,Action _eAction
135 ,USHORT nCommentID)
136 : OCommentUndoAction(_rMod,nCommentID)
137 ,m_eAction(_eAction)
138 ,m_nSlot(_nSlot)
139 ,m_bInserted(false)
141 DBG_CTOR(rpt_OSectionUndo,NULL);
143 // -----------------------------------------------------------------------------
144 OSectionUndo::~OSectionUndo()
146 if ( !m_bInserted )
148 OXUndoEnvironment& rEnv = static_cast< OReportModel& >( rMod ).GetUndoEnv();
149 ::std::vector< uno::Reference< drawing::XShape> >::iterator aEnd = m_aControls.end();
150 for (::std::vector< uno::Reference< drawing::XShape> >::iterator aIter = m_aControls.begin(); aIter != aEnd; ++aIter)
152 uno::Reference< drawing::XShape> xShape = *aIter;
153 rEnv.RemoveElement(xShape);
155 #if OSL_DEBUG_LEVEL > 0
156 SvxShape* pShape = SvxShape::getImplementation( xShape );
157 SdrObject* pObject = pShape ? pShape->GetSdrObject() : NULL;
158 OSL_ENSURE( pShape && pShape->HasSdrObjectOwnership() && pObject && !pObject->IsInserted(),
159 "OSectionUndo::~OSectionUndo: inconsistency in the shape/object ownership!" );
160 #endif
163 comphelper::disposeComponent(xShape);
165 catch(uno::Exception)
167 OSL_ENSURE(0,"Exception caught!");
171 DBG_DTOR(rpt_OSectionUndo,NULL);
173 // -----------------------------------------------------------------------------
174 void OSectionUndo::collectControls(const uno::Reference< report::XSection >& _xSection)
176 m_aControls.clear();
179 // copy all properties for restoring
180 uno::Reference< beans::XPropertySetInfo> xInfo = _xSection->getPropertySetInfo();
181 uno::Sequence< beans::Property> aSeq = xInfo->getProperties();
182 const beans::Property* pIter = aSeq.getConstArray();
183 const beans::Property* pEnd = pIter + aSeq.getLength();
184 for(;pIter != pEnd;++pIter)
186 if ( 0 == (pIter->Attributes & beans::PropertyAttribute::READONLY) )
187 m_aValues.push_back(::std::pair< ::rtl::OUString ,uno::Any>(pIter->Name,_xSection->getPropertyValue(pIter->Name)));
189 lcl_collectElements(_xSection,m_aControls);
191 catch(uno::Exception&)
195 //----------------------------------------------------------------------------
196 void OSectionUndo::Undo()
200 switch ( m_eAction )
202 case Inserted:
203 implReRemove();
204 break;
206 case Removed:
207 implReInsert();
208 break;
211 catch( const Exception& )
213 OSL_ENSURE( sal_False, "OSectionUndo::Undo: caught an exception!" );
216 //----------------------------------------------------------------------------
217 void OSectionUndo::Redo()
221 switch ( m_eAction )
223 case Inserted:
224 implReInsert();
225 break;
227 case Removed:
228 implReRemove();
229 break;
232 catch( const Exception& )
234 OSL_ENSURE( sal_False, "OSectionUndo::Redo: caught an exception!" );
237 //----------------------------------------------------------------------------
238 TYPEINIT1( OReportSectionUndo, OSectionUndo );
239 //----------------------------------------------------------------------------
240 OReportSectionUndo::OReportSectionUndo(OReportModel& _rMod,sal_uInt16 _nSlot
241 ,::std::mem_fun_t< uno::Reference< report::XSection >
242 ,OReportHelper> _pMemberFunction
243 ,const uno::Reference< report::XReportDefinition >& _xReport
244 ,Action _eAction
245 ,USHORT nCommentID)
246 : OSectionUndo(_rMod,_nSlot,_eAction,nCommentID)
247 ,m_aReportHelper(_xReport)
248 ,m_pMemberFunction(_pMemberFunction)
250 if( m_eAction == Removed )
251 collectControls(m_pMemberFunction(&m_aReportHelper));
253 // -----------------------------------------------------------------------------
254 OReportSectionUndo::~OReportSectionUndo()
257 //----------------------------------------------------------------------------
258 void OReportSectionUndo::implReInsert( )
260 const uno::Sequence< beans::PropertyValue > aArgs;
261 m_pController->executeChecked(m_nSlot,aArgs);
262 uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aReportHelper);
263 lcl_insertElements(xSection,m_aControls);
264 lcl_setValues(xSection,m_aValues);
265 m_bInserted = true;
267 //----------------------------------------------------------------------------
268 void OReportSectionUndo::implReRemove( )
270 if( m_eAction == Removed )
271 collectControls(m_pMemberFunction(&m_aReportHelper));
272 const uno::Sequence< beans::PropertyValue > aArgs;
273 m_pController->executeChecked(m_nSlot,aArgs);
274 m_bInserted = false;
276 //----------------------------------------------------------------------------
277 TYPEINIT1( OGroupSectionUndo, OSectionUndo );
278 //----------------------------------------------------------------------------
279 OGroupSectionUndo::OGroupSectionUndo(OReportModel& _rMod,sal_uInt16 _nSlot
280 ,::std::mem_fun_t< uno::Reference< report::XSection >
281 ,OGroupHelper> _pMemberFunction
282 ,const uno::Reference< report::XGroup >& _xGroup
283 ,Action _eAction
284 ,USHORT nCommentID)
285 : OSectionUndo(_rMod,_nSlot,_eAction,nCommentID)
286 ,m_aGroupHelper(_xGroup)
287 ,m_pMemberFunction(_pMemberFunction)
289 if( m_eAction == Removed )
291 uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aGroupHelper);
292 if ( xSection.is() )
293 m_sName = xSection->getName();
294 collectControls(xSection);
297 //----------------------------------------------------------------------------
298 String OGroupSectionUndo::GetComment() const
300 if ( !m_sName.getLength() )
304 uno::Reference< report::XSection > xSection = const_cast<OGroupSectionUndo*>(this)->m_pMemberFunction(&const_cast<OGroupSectionUndo*>(this)->m_aGroupHelper);
306 if ( xSection.is() )
307 m_sName = xSection->getName();
309 catch(uno::Exception&)
312 return m_strComment + m_sName;
314 //----------------------------------------------------------------------------
315 void OGroupSectionUndo::implReInsert( )
317 uno::Sequence< beans::PropertyValue > aArgs(2);
319 aArgs[0].Name = SID_GROUPHEADER_WITHOUT_UNDO == m_nSlot? PROPERTY_HEADERON : PROPERTY_FOOTERON;
320 aArgs[0].Value <<= sal_True;
321 aArgs[1].Name = PROPERTY_GROUP;
322 aArgs[1].Value <<= m_aGroupHelper.getGroup();
323 m_pController->executeChecked(m_nSlot,aArgs);
325 uno::Reference< report::XSection > xSection = m_pMemberFunction(&m_aGroupHelper);
326 lcl_insertElements(xSection,m_aControls);
327 lcl_setValues(xSection,m_aValues);
328 m_bInserted = true;
330 //----------------------------------------------------------------------------
331 void OGroupSectionUndo::implReRemove( )
333 if( m_eAction == Removed )
334 collectControls(m_pMemberFunction(&m_aGroupHelper));
336 uno::Sequence< beans::PropertyValue > aArgs(2);
338 aArgs[0].Name = SID_GROUPHEADER_WITHOUT_UNDO == m_nSlot? PROPERTY_HEADERON : PROPERTY_FOOTERON;
339 aArgs[0].Value <<= sal_False;
340 aArgs[1].Name = PROPERTY_GROUP;
341 aArgs[1].Value <<= m_aGroupHelper.getGroup();
343 m_pController->executeChecked(m_nSlot,aArgs);
344 m_bInserted = false;
346 //----------------------------------------------------------------------------
347 TYPEINIT1( OGroupUndo, OCommentUndoAction );
348 //----------------------------------------------------------------------------
349 OGroupUndo::OGroupUndo(OReportModel& _rMod
350 ,USHORT nCommentID
351 ,Action _eAction
352 ,const uno::Reference< report::XGroup>& _xGroup
353 ,const uno::Reference< report::XReportDefinition >& _xReportDefinition)
354 : OCommentUndoAction(_rMod,nCommentID)
355 ,m_xGroup(_xGroup)
356 ,m_xReportDefinition(_xReportDefinition)
357 ,m_eAction(_eAction)
359 m_nLastPosition = getPositionInIndexAccess(m_xReportDefinition->getGroups().get(),m_xGroup);
361 //----------------------------------------------------------------------------
362 void OGroupUndo::implReInsert( )
366 m_xReportDefinition->getGroups()->insertByIndex(m_nLastPosition,uno::makeAny(m_xGroup));
368 catch(uno::Exception&)
370 OSL_ENSURE(0,"Exception catched while undoing remove group");
373 //----------------------------------------------------------------------------
374 void OGroupUndo::implReRemove( )
378 m_xReportDefinition->getGroups()->removeByIndex(m_nLastPosition);
380 catch(uno::Exception&)
382 OSL_ENSURE(0,"Exception catched while redoing remove group");
385 //----------------------------------------------------------------------------
386 void OGroupUndo::Undo()
388 switch ( m_eAction )
390 case Inserted:
391 implReRemove();
392 break;
394 case Removed:
395 implReInsert();
396 break;
400 //----------------------------------------------------------------------------
401 void OGroupUndo::Redo()
403 switch ( m_eAction )
405 case Inserted:
406 implReInsert();
407 break;
409 case Removed:
410 implReRemove();
411 break;
414 //----------------------------------------------------------------------------
415 //============================================================================
416 } // rptui
417 //============================================================================