xref: /trunk/main/reportdesign/source/ui/dlg/CondFormat.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #include "precompiled_reportdesign.hxx"
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #include "CondFormat.hxx"
30*cdf0e10cSrcweir #include "CondFormat.hrc"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include "uistrings.hrc"
33*cdf0e10cSrcweir #include "RptResId.hrc"
34*cdf0e10cSrcweir #include "rptui_slotid.hrc"
35*cdf0e10cSrcweir #include "ModuleHelper.hxx"
36*cdf0e10cSrcweir #include "helpids.hrc"
37*cdf0e10cSrcweir #include "UITools.hxx"
38*cdf0e10cSrcweir #include "uistrings.hrc"
39*cdf0e10cSrcweir #include "ReportController.hxx"
40*cdf0e10cSrcweir #include "Condition.hxx"
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir /** === begin UNO includes === **/
43*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
45*cdf0e10cSrcweir /** === end UNO includes === **/
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #include <svx/globlmn.hrc>
48*cdf0e10cSrcweir #include <svx/svxids.hrc>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx>
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir #include <vcl/msgbox.hxx>
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir #include <tools/debug.hxx>
55*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir #include <comphelper/property.hxx>
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir #include <algorithm>
60*cdf0e10cSrcweir #include "UndoActions.hxx"
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir // .............................................................................
63*cdf0e10cSrcweir namespace rptui
64*cdf0e10cSrcweir {
65*cdf0e10cSrcweir // .............................................................................
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir     /** === begin UNO using === **/
68*cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
69*cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
70*cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY;
71*cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
72*cdf0e10cSrcweir     using ::com::sun::star::lang::IllegalArgumentException;
73*cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
74*cdf0e10cSrcweir     using ::com::sun::star::beans::PropertyValue;
75*cdf0e10cSrcweir     using ::com::sun::star::uno::Any;
76*cdf0e10cSrcweir     /** === end UNO using === **/
77*cdf0e10cSrcweir     using namespace ::com::sun::star::report;
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir     //========================================================================
80*cdf0e10cSrcweir     // UpdateLocker
81*cdf0e10cSrcweir     //========================================================================
82*cdf0e10cSrcweir     class UpdateLocker
83*cdf0e10cSrcweir     {
84*cdf0e10cSrcweir         Window& m_rWindow;
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir     public:
87*cdf0e10cSrcweir         UpdateLocker( Window& _rWindow )
88*cdf0e10cSrcweir             :m_rWindow( _rWindow )
89*cdf0e10cSrcweir         {
90*cdf0e10cSrcweir             _rWindow.SetUpdateMode( sal_False );
91*cdf0e10cSrcweir         }
92*cdf0e10cSrcweir         ~UpdateLocker()
93*cdf0e10cSrcweir         {
94*cdf0e10cSrcweir             m_rWindow.SetUpdateMode( sal_True );
95*cdf0e10cSrcweir         }
96*cdf0e10cSrcweir     };
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     //========================================================================
99*cdf0e10cSrcweir     // class ConditionalFormattingDialog
100*cdf0e10cSrcweir     //========================================================================
101*cdf0e10cSrcweir     DBG_NAME(rpt_ConditionalFormattingDialog)
102*cdf0e10cSrcweir     ConditionalFormattingDialog::ConditionalFormattingDialog(
103*cdf0e10cSrcweir             Window* _pParent, const Reference< XReportControlModel >& _rxFormatConditions, ::rptui::OReportController& _rController )
104*cdf0e10cSrcweir         :ModalDialog( _pParent, ModuleRes(RID_CONDFORMAT) )
105*cdf0e10cSrcweir         ,m_aConditionPlayground( this, ModuleRes( WND_COND_PLAYGROUND ) )
106*cdf0e10cSrcweir         ,m_aSeparator(this,     ModuleRes(FL_SEPARATOR1))
107*cdf0e10cSrcweir         ,m_aPB_OK(this,         ModuleRes(PB_OK))
108*cdf0e10cSrcweir         ,m_aPB_CANCEL(this,     ModuleRes(PB_CANCEL))
109*cdf0e10cSrcweir         ,m_aPB_Help(this,       ModuleRes(PB_HELP))
110*cdf0e10cSrcweir         ,m_aCondScroll( this,   ModuleRes( SB_ALL_CONDITIONS ) )
111*cdf0e10cSrcweir         ,m_rController( _rController )
112*cdf0e10cSrcweir         ,m_xFormatConditions( _rxFormatConditions )
113*cdf0e10cSrcweir         ,m_bDeletingCondition( false )
114*cdf0e10cSrcweir     {
115*cdf0e10cSrcweir         DBG_CTOR(rpt_ConditionalFormattingDialog,NULL);
116*cdf0e10cSrcweir         OSL_ENSURE( m_xFormatConditions.is(), "ConditionalFormattingDialog::ConditionalFormattingDialog: ReportControlModel is NULL -> Prepare for GPF!" );
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir         m_xCopy.set( m_xFormatConditions->createClone(), UNO_QUERY_THROW );
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir         m_aCondScroll.SetScrollHdl( LINK( this, ConditionalFormattingDialog, OnScroll ) );
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir         impl_initializeConditions();
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir         FreeResource();
125*cdf0e10cSrcweir     }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir     //------------------------------------------------------------------------
128*cdf0e10cSrcweir     ConditionalFormattingDialog::~ConditionalFormattingDialog()
129*cdf0e10cSrcweir     {
130*cdf0e10cSrcweir         m_aConditions.clear();
131*cdf0e10cSrcweir         DBG_DTOR(rpt_ConditionalFormattingDialog,NULL);
132*cdf0e10cSrcweir     }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
135*cdf0e10cSrcweir     void ConditionalFormattingDialog::impl_updateConditionIndicies()
136*cdf0e10cSrcweir     {
137*cdf0e10cSrcweir         sal_Int32 nIndex = 0;
138*cdf0e10cSrcweir         for (   Conditions::const_iterator cond = m_aConditions.begin();
139*cdf0e10cSrcweir                 cond != m_aConditions.end();
140*cdf0e10cSrcweir                 ++cond, ++nIndex
141*cdf0e10cSrcweir             )
142*cdf0e10cSrcweir         {
143*cdf0e10cSrcweir             (*cond)->setConditionIndex( nIndex, impl_getConditionCount() );
144*cdf0e10cSrcweir         }
145*cdf0e10cSrcweir     }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
148*cdf0e10cSrcweir     void ConditionalFormattingDialog::impl_conditionCountChanged()
149*cdf0e10cSrcweir     {
150*cdf0e10cSrcweir         if ( m_aConditions.empty() )
151*cdf0e10cSrcweir             impl_addCondition_nothrow( 0 );
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir         impl_updateScrollBarRange();
154*cdf0e10cSrcweir         impl_updateConditionIndicies();
155*cdf0e10cSrcweir         impl_layoutAll();
156*cdf0e10cSrcweir     }
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
159*cdf0e10cSrcweir     void ConditionalFormattingDialog::addCondition( size_t _nAddAfterIndex )
160*cdf0e10cSrcweir     {
161*cdf0e10cSrcweir         OSL_PRECOND( _nAddAfterIndex < impl_getConditionCount(), "ConditionalFormattingDialog::addCondition: illegal condition index!" );
162*cdf0e10cSrcweir         impl_addCondition_nothrow( _nAddAfterIndex + 1 );
163*cdf0e10cSrcweir     }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
166*cdf0e10cSrcweir     void ConditionalFormattingDialog::deleteCondition( size_t _nCondIndex )
167*cdf0e10cSrcweir     {
168*cdf0e10cSrcweir         impl_deleteCondition_nothrow( _nCondIndex );
169*cdf0e10cSrcweir     }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
172*cdf0e10cSrcweir     void ConditionalFormattingDialog::impl_addCondition_nothrow( size_t _nNewCondIndex )
173*cdf0e10cSrcweir     {
174*cdf0e10cSrcweir         UpdateLocker aLockUpdates( *this );
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir         try
177*cdf0e10cSrcweir         {
178*cdf0e10cSrcweir             if ( _nNewCondIndex > (size_t)m_xCopy->getCount() )
179*cdf0e10cSrcweir                 throw IllegalArgumentException();
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir             Reference< XFormatCondition > xCond = m_xCopy->createFormatCondition();
182*cdf0e10cSrcweir             ::comphelper::copyProperties(m_xCopy.get(),xCond.get());
183*cdf0e10cSrcweir             m_xCopy->insertByIndex( _nNewCondIndex, makeAny( xCond ) );
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir             ConditionPtr pCon( new Condition( &m_aConditionPlayground, *this, m_rController ) );
186*cdf0e10cSrcweir             pCon->setCondition( xCond );
187*cdf0e10cSrcweir             m_aConditions.insert( m_aConditions.begin() + _nNewCondIndex, pCon );
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir             pCon->SetPosSizePixel( 0, 0, impl_getConditionWidth(), 0, WINDOW_POSSIZE_WIDTH );
190*cdf0e10cSrcweir         }
191*cdf0e10cSrcweir         catch( const Exception& )
192*cdf0e10cSrcweir         {
193*cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
194*cdf0e10cSrcweir         }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir         impl_conditionCountChanged();
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir         impl_ensureConditionVisible( _nNewCondIndex );
199*cdf0e10cSrcweir     }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
202*cdf0e10cSrcweir     void ConditionalFormattingDialog::impl_focusCondition( size_t _nCondIndex )
203*cdf0e10cSrcweir     {
204*cdf0e10cSrcweir         OSL_PRECOND( _nCondIndex < impl_getConditionCount(),
205*cdf0e10cSrcweir             "ConditionalFormattingDialog::impl_focusCondition: illegal index!" );
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir         impl_ensureConditionVisible( _nCondIndex );
208*cdf0e10cSrcweir         m_aConditions[ _nCondIndex ]->GrabFocus();
209*cdf0e10cSrcweir     }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
212*cdf0e10cSrcweir     void ConditionalFormattingDialog::impl_deleteCondition_nothrow( size_t _nCondIndex )
213*cdf0e10cSrcweir     {
214*cdf0e10cSrcweir         UpdateLocker aLockUpdates( *this );
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir         OSL_PRECOND( _nCondIndex < impl_getConditionCount(),
217*cdf0e10cSrcweir             "ConditionalFormattingDialog::impl_deleteCondition_nothrow: illegal index!" );
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir         bool bLastCondition = ( impl_getConditionCount() == 1 );
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir         bool bSetNewFocus = false;
222*cdf0e10cSrcweir         size_t nNewFocusIndex( _nCondIndex );
223*cdf0e10cSrcweir         try
224*cdf0e10cSrcweir         {
225*cdf0e10cSrcweir             if ( !bLastCondition )
226*cdf0e10cSrcweir                 m_xCopy->removeByIndex( _nCondIndex );
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir             Conditions::iterator pos = m_aConditions.begin() + _nCondIndex;
229*cdf0e10cSrcweir             if ( bLastCondition )
230*cdf0e10cSrcweir             {
231*cdf0e10cSrcweir                 Reference< XFormatCondition > xFormatCondition( m_xCopy->getByIndex( 0 ), UNO_QUERY_THROW );
232*cdf0e10cSrcweir                 xFormatCondition->setFormula( ::rtl::OUString() );
233*cdf0e10cSrcweir                 (*pos)->setCondition( xFormatCondition );
234*cdf0e10cSrcweir             }
235*cdf0e10cSrcweir             else
236*cdf0e10cSrcweir             {
237*cdf0e10cSrcweir                 bSetNewFocus = (*pos)->HasChildPathFocus();
238*cdf0e10cSrcweir                 m_bDeletingCondition = true;
239*cdf0e10cSrcweir                 m_aConditions.erase( pos );
240*cdf0e10cSrcweir                 m_bDeletingCondition = false;
241*cdf0e10cSrcweir             }
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir             if ( bSetNewFocus )
244*cdf0e10cSrcweir             {
245*cdf0e10cSrcweir                 if ( nNewFocusIndex >= impl_getConditionCount() )
246*cdf0e10cSrcweir                     nNewFocusIndex = impl_getConditionCount() - 1;
247*cdf0e10cSrcweir             }
248*cdf0e10cSrcweir         }
249*cdf0e10cSrcweir         catch( const Exception& )
250*cdf0e10cSrcweir         {
251*cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
252*cdf0e10cSrcweir         }
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir         impl_conditionCountChanged();
255*cdf0e10cSrcweir         if ( bSetNewFocus )
256*cdf0e10cSrcweir             impl_focusCondition( nNewFocusIndex );
257*cdf0e10cSrcweir     }
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
260*cdf0e10cSrcweir     void ConditionalFormattingDialog::impl_moveCondition_nothrow( size_t _nCondIndex, bool _bMoveUp )
261*cdf0e10cSrcweir     {
262*cdf0e10cSrcweir         size_t nOldConditionIndex( _nCondIndex );
263*cdf0e10cSrcweir         size_t nNewConditionIndex( _bMoveUp ? _nCondIndex - 1 : _nCondIndex + 1 );
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir         // do this in two steps, so we don't become inconsistent if any of the UNO actions fails
266*cdf0e10cSrcweir         Any aMovedCondition;
267*cdf0e10cSrcweir         ConditionPtr pMovedCondition;
268*cdf0e10cSrcweir         try
269*cdf0e10cSrcweir         {
270*cdf0e10cSrcweir             aMovedCondition = m_xCopy->getByIndex( (sal_Int32)nOldConditionIndex );
271*cdf0e10cSrcweir             m_xCopy->removeByIndex( (sal_Int32)nOldConditionIndex );
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir             Conditions::iterator aRemovePos( m_aConditions.begin() + nOldConditionIndex );
274*cdf0e10cSrcweir             pMovedCondition = *aRemovePos;
275*cdf0e10cSrcweir             m_aConditions.erase( aRemovePos );
276*cdf0e10cSrcweir         }
277*cdf0e10cSrcweir         catch( const Exception& )
278*cdf0e10cSrcweir         {
279*cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
280*cdf0e10cSrcweir             return;
281*cdf0e10cSrcweir         }
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir         try
284*cdf0e10cSrcweir         {
285*cdf0e10cSrcweir             m_xCopy->insertByIndex( (sal_Int32)nNewConditionIndex, aMovedCondition );
286*cdf0e10cSrcweir             m_aConditions.insert( m_aConditions.begin() + nNewConditionIndex, pMovedCondition );
287*cdf0e10cSrcweir         }
288*cdf0e10cSrcweir         catch( const Exception& )
289*cdf0e10cSrcweir         {
290*cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
291*cdf0e10cSrcweir         }
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir         // at least the two swapped conditions need to know their new index
294*cdf0e10cSrcweir         impl_updateConditionIndicies();
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir         // re-layout all conditions
297*cdf0e10cSrcweir         Point aDummy;
298*cdf0e10cSrcweir         impl_layoutConditions( aDummy );
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir         // ensure the moved condition is visible
301*cdf0e10cSrcweir         impl_ensureConditionVisible( nNewConditionIndex );
302*cdf0e10cSrcweir     }
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
305*cdf0e10cSrcweir     long ConditionalFormattingDialog::impl_getConditionWidth() const
306*cdf0e10cSrcweir     {
307*cdf0e10cSrcweir         const Size aDialogSize( GetOutputSizePixel() );
308*cdf0e10cSrcweir         const Size aScrollBarWidth( LogicToPixel( Size( SCROLLBAR_WIDTH + UNRELATED_CONTROLS, 0 ), MAP_APPFONT ) );
309*cdf0e10cSrcweir         return aDialogSize.Width() - aScrollBarWidth.Width();
310*cdf0e10cSrcweir     }
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
313*cdf0e10cSrcweir     IMPL_LINK( ConditionalFormattingDialog, OnScroll, ScrollBar*, /*_pNotInterestedIn*/ )
314*cdf0e10cSrcweir     {
315*cdf0e10cSrcweir         size_t nFirstCondIndex( impl_getFirstVisibleConditionIndex() );
316*cdf0e10cSrcweir         size_t nFocusCondIndex = impl_getFocusedConditionIndex( nFirstCondIndex );
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir         Point aDummy;
319*cdf0e10cSrcweir         impl_layoutConditions( aDummy );
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir         if ( nFocusCondIndex < nFirstCondIndex )
322*cdf0e10cSrcweir             impl_focusCondition( nFirstCondIndex );
323*cdf0e10cSrcweir         else if ( nFocusCondIndex >= nFirstCondIndex + MAX_CONDITIONS )
324*cdf0e10cSrcweir             impl_focusCondition( nFirstCondIndex + MAX_CONDITIONS - 1 );
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir         return 0;
327*cdf0e10cSrcweir     }
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
330*cdf0e10cSrcweir     void ConditionalFormattingDialog::impl_layoutConditions( Point& _out_rBelowLastVisible )
331*cdf0e10cSrcweir     {
332*cdf0e10cSrcweir         // position the condition's playground
333*cdf0e10cSrcweir         long nConditionWidth = impl_getConditionWidth();
334*cdf0e10cSrcweir         long nConditionHeight = LogicToPixel( Size( 0, CONDITION_HEIGHT ), MAP_APPFONT ).Height();
335*cdf0e10cSrcweir         size_t nVisibleConditions = ::std::min( impl_getConditionCount(), MAX_CONDITIONS );
336*cdf0e10cSrcweir         Size aPlaygroundSize( nConditionWidth, nVisibleConditions * nConditionHeight );
337*cdf0e10cSrcweir         m_aConditionPlayground.SetSizePixel( aPlaygroundSize );
338*cdf0e10cSrcweir         _out_rBelowLastVisible = Point( 0, aPlaygroundSize.Height() );
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir         // position the single conditions
341*cdf0e10cSrcweir         Point aConditionPos( 0, -1 * nConditionHeight * impl_getFirstVisibleConditionIndex() );
342*cdf0e10cSrcweir         for (   Conditions::const_iterator cond = m_aConditions.begin();
343*cdf0e10cSrcweir                 cond != m_aConditions.end();
344*cdf0e10cSrcweir                 ++cond
345*cdf0e10cSrcweir             )
346*cdf0e10cSrcweir         {
347*cdf0e10cSrcweir             (*cond)->SetPosSizePixel( aConditionPos.X(), aConditionPos.Y(), nConditionWidth, nConditionHeight );
348*cdf0e10cSrcweir             aConditionPos.Move( 0, nConditionHeight );
349*cdf0e10cSrcweir         }
350*cdf0e10cSrcweir     }
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
353*cdf0e10cSrcweir     void ConditionalFormattingDialog::impl_layoutAll()
354*cdf0e10cSrcweir     {
355*cdf0e10cSrcweir         // condition's positions
356*cdf0e10cSrcweir         Point aPos;
357*cdf0e10cSrcweir         impl_layoutConditions( aPos );
358*cdf0e10cSrcweir 
359*cdf0e10cSrcweir         // scrollbar size and visibility
360*cdf0e10cSrcweir         m_aCondScroll.SetPosSizePixel( 0, 0, 0, aPos.Y(), WINDOW_POSSIZE_HEIGHT );
361*cdf0e10cSrcweir         if ( !impl_needScrollBar() )
362*cdf0e10cSrcweir             // normalize the position, so it can, in all situations, be used as top index
363*cdf0e10cSrcweir             m_aCondScroll.SetThumbPos( 0 );
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir         // the separator and the buttons below it
366*cdf0e10cSrcweir         aPos += LogicToPixel( Point( 0 , RELATED_CONTROLS ), MAP_APPFONT );
367*cdf0e10cSrcweir         m_aSeparator.SetPosSizePixel( 0, aPos.Y(), 0, 0, WINDOW_POSSIZE_Y );
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir         aPos += LogicToPixel( Point( 0 , UNRELATED_CONTROLS ), MAP_APPFONT );
370*cdf0e10cSrcweir         Window* pWindows[] = { &m_aPB_OK, &m_aPB_CANCEL, &m_aPB_Help };
371*cdf0e10cSrcweir         for ( size_t i= 0; i < sizeof(pWindows)/sizeof(pWindows[0]); ++i )
372*cdf0e10cSrcweir         {
373*cdf0e10cSrcweir             pWindows[i]->SetPosSizePixel( 0, aPos.Y(), 0, 0, WINDOW_POSSIZE_Y );
374*cdf0e10cSrcweir         }
375*cdf0e10cSrcweir 
376*cdf0e10cSrcweir         aPos += LogicToPixel( Point( 0, BUTTON_HEIGHT + RELATED_CONTROLS ), MAP_APPFONT );
377*cdf0e10cSrcweir         SetPosSizePixel( 0, 0, 0, aPos.Y(), WINDOW_POSSIZE_HEIGHT );
378*cdf0e10cSrcweir     }
379*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
380*cdf0e10cSrcweir     void ConditionalFormattingDialog::impl_initializeConditions()
381*cdf0e10cSrcweir     {
382*cdf0e10cSrcweir         try
383*cdf0e10cSrcweir         {
384*cdf0e10cSrcweir             sal_Int32 nCount = m_xCopy->getCount();
385*cdf0e10cSrcweir             for ( sal_Int32 i = 0; i < nCount ; ++i )
386*cdf0e10cSrcweir             {
387*cdf0e10cSrcweir                 ConditionPtr pCon( new Condition( &m_aConditionPlayground, *this, m_rController ) );
388*cdf0e10cSrcweir                 Reference< XFormatCondition > xCond( m_xCopy->getByIndex(i), UNO_QUERY );
389*cdf0e10cSrcweir                 pCon->setCondition( xCond );
390*cdf0e10cSrcweir                 pCon->updateToolbar( xCond.get() );
391*cdf0e10cSrcweir                 m_aConditions.push_back( pCon );
392*cdf0e10cSrcweir             }
393*cdf0e10cSrcweir         }
394*cdf0e10cSrcweir         catch(Exception&)
395*cdf0e10cSrcweir         {
396*cdf0e10cSrcweir             OSL_ENSURE(0,"Can not access format condition!");
397*cdf0e10cSrcweir         }
398*cdf0e10cSrcweir 
399*cdf0e10cSrcweir         impl_conditionCountChanged();
400*cdf0e10cSrcweir     }
401*cdf0e10cSrcweir 
402*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
403*cdf0e10cSrcweir     void ConditionalFormattingDialog::applyCommand( size_t _nCondIndex, sal_uInt16 _nCommandId, const ::Color _aColor )
404*cdf0e10cSrcweir     {
405*cdf0e10cSrcweir         OSL_PRECOND( _nCommandId, "ConditionalFormattingDialog::applyCommand: illegal command id!" );
406*cdf0e10cSrcweir         try
407*cdf0e10cSrcweir         {
408*cdf0e10cSrcweir             Reference< XReportControlFormat > xReportControlFormat( m_xCopy->getByIndex( _nCondIndex ), UNO_QUERY_THROW );
409*cdf0e10cSrcweir 
410*cdf0e10cSrcweir             Sequence< PropertyValue > aArgs(3);
411*cdf0e10cSrcweir 
412*cdf0e10cSrcweir             aArgs[0].Name = REPORTCONTROLFORMAT;
413*cdf0e10cSrcweir             aArgs[0].Value <<= xReportControlFormat;
414*cdf0e10cSrcweir 
415*cdf0e10cSrcweir             aArgs[1].Name = CURRENT_WINDOW;
416*cdf0e10cSrcweir             aArgs[1].Value <<= VCLUnoHelper::GetInterface(this);
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir             aArgs[2].Name = PROPERTY_FONTCOLOR;
419*cdf0e10cSrcweir             aArgs[2].Value <<= (sal_uInt32)_aColor.GetColor();
420*cdf0e10cSrcweir 
421*cdf0e10cSrcweir             // we use this way to create undo actions
422*cdf0e10cSrcweir             m_rController.executeUnChecked(_nCommandId,aArgs);
423*cdf0e10cSrcweir             m_aConditions[ _nCondIndex ]->updateToolbar(xReportControlFormat);
424*cdf0e10cSrcweir         }
425*cdf0e10cSrcweir         catch( Exception& )
426*cdf0e10cSrcweir         {
427*cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
428*cdf0e10cSrcweir         }
429*cdf0e10cSrcweir     }
430*cdf0e10cSrcweir 
431*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
432*cdf0e10cSrcweir     void ConditionalFormattingDialog::moveConditionUp( size_t _nCondIndex )
433*cdf0e10cSrcweir     {
434*cdf0e10cSrcweir         OSL_PRECOND( _nCondIndex > 0, "ConditionalFormattingDialog::moveConditionUp: cannot move up the first condition!" );
435*cdf0e10cSrcweir         if ( _nCondIndex > 0 )
436*cdf0e10cSrcweir             impl_moveCondition_nothrow( _nCondIndex, true );
437*cdf0e10cSrcweir     }
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
440*cdf0e10cSrcweir     void ConditionalFormattingDialog::moveConditionDown( size_t _nCondIndex )
441*cdf0e10cSrcweir     {
442*cdf0e10cSrcweir         OSL_PRECOND( _nCondIndex < impl_getConditionCount(), "ConditionalFormattingDialog::moveConditionDown: cannot move down the last condition!" );
443*cdf0e10cSrcweir         if ( _nCondIndex < impl_getConditionCount() )
444*cdf0e10cSrcweir             impl_moveCondition_nothrow( _nCondIndex, false );
445*cdf0e10cSrcweir     }
446*cdf0e10cSrcweir 
447*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
448*cdf0e10cSrcweir     ::rtl::OUString ConditionalFormattingDialog::getDataField() const
449*cdf0e10cSrcweir     {
450*cdf0e10cSrcweir         ::rtl::OUString sDataField;
451*cdf0e10cSrcweir         try
452*cdf0e10cSrcweir         {
453*cdf0e10cSrcweir             sDataField = m_xFormatConditions->getDataField();
454*cdf0e10cSrcweir         }
455*cdf0e10cSrcweir         catch( const Exception& )
456*cdf0e10cSrcweir         {
457*cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
458*cdf0e10cSrcweir         }
459*cdf0e10cSrcweir         return sDataField;
460*cdf0e10cSrcweir     }
461*cdf0e10cSrcweir 
462*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
463*cdf0e10cSrcweir     short ConditionalFormattingDialog::Execute()
464*cdf0e10cSrcweir     {
465*cdf0e10cSrcweir         short nRet = ModalDialog::Execute();
466*cdf0e10cSrcweir         if ( nRet == RET_OK )
467*cdf0e10cSrcweir         {
468*cdf0e10cSrcweir             const String sUndoAction( ModuleRes( RID_STR_UNDO_CONDITIONAL_FORMATTING ) );
469*cdf0e10cSrcweir             const UndoContext aUndoContext( m_rController.getUndoManager(), sUndoAction );
470*cdf0e10cSrcweir             try
471*cdf0e10cSrcweir             {
472*cdf0e10cSrcweir                 sal_Int32 j(0), i(0);;
473*cdf0e10cSrcweir                 for (   Conditions::const_iterator cond = m_aConditions.begin();
474*cdf0e10cSrcweir                         cond != m_aConditions.end();
475*cdf0e10cSrcweir                         ++cond, ++i
476*cdf0e10cSrcweir                     )
477*cdf0e10cSrcweir                 {
478*cdf0e10cSrcweir                     Reference< XFormatCondition > xCond( m_xCopy->getByIndex(i), UNO_QUERY_THROW );
479*cdf0e10cSrcweir                     (*cond)->fillFormatCondition( xCond );
480*cdf0e10cSrcweir 
481*cdf0e10cSrcweir                     if ( (*cond)->isEmpty() )
482*cdf0e10cSrcweir                         continue;
483*cdf0e10cSrcweir 
484*cdf0e10cSrcweir                     Reference< XFormatCondition > xNewCond;
485*cdf0e10cSrcweir                     sal_Bool bAppend = j >= m_xFormatConditions->getCount();
486*cdf0e10cSrcweir                     if ( bAppend )
487*cdf0e10cSrcweir                     {
488*cdf0e10cSrcweir                         xNewCond = m_xFormatConditions->createFormatCondition();
489*cdf0e10cSrcweir                         m_xFormatConditions->insertByIndex( i, makeAny( xNewCond ) );
490*cdf0e10cSrcweir                     }
491*cdf0e10cSrcweir                     else
492*cdf0e10cSrcweir                         xNewCond.set( m_xFormatConditions->getByIndex(j), UNO_QUERY );
493*cdf0e10cSrcweir                     ++j;
494*cdf0e10cSrcweir 
495*cdf0e10cSrcweir                     ::comphelper::copyProperties(xCond.get(),xNewCond.get());
496*cdf0e10cSrcweir                 }
497*cdf0e10cSrcweir 
498*cdf0e10cSrcweir                 for ( sal_Int32 k = m_xFormatConditions->getCount()-1; k >= j; --k )
499*cdf0e10cSrcweir                     m_xFormatConditions->removeByIndex(k);
500*cdf0e10cSrcweir 
501*cdf0e10cSrcweir                 ::comphelper::copyProperties( m_xCopy.get(), m_xFormatConditions.get() );
502*cdf0e10cSrcweir             }
503*cdf0e10cSrcweir             catch ( const Exception& )
504*cdf0e10cSrcweir             {
505*cdf0e10cSrcweir                 DBG_UNHANDLED_EXCEPTION();
506*cdf0e10cSrcweir                 nRet = RET_NO;
507*cdf0e10cSrcweir             }
508*cdf0e10cSrcweir         }
509*cdf0e10cSrcweir         return nRet;
510*cdf0e10cSrcweir     }
511*cdf0e10cSrcweir 
512*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
513*cdf0e10cSrcweir     long ConditionalFormattingDialog::PreNotify( NotifyEvent& _rNEvt )
514*cdf0e10cSrcweir     {
515*cdf0e10cSrcweir         switch ( _rNEvt.GetType() )
516*cdf0e10cSrcweir         {
517*cdf0e10cSrcweir         case EVENT_KEYINPUT:
518*cdf0e10cSrcweir         {
519*cdf0e10cSrcweir             const KeyEvent* pKeyEvent( _rNEvt.GetKeyEvent() );
520*cdf0e10cSrcweir             const KeyCode& rKeyCode = pKeyEvent->GetKeyCode();
521*cdf0e10cSrcweir             if ( rKeyCode.IsMod1() && rKeyCode.IsMod2() )
522*cdf0e10cSrcweir             {
523*cdf0e10cSrcweir                 if ( rKeyCode.GetCode() == 0x0508 ) // -
524*cdf0e10cSrcweir                 {
525*cdf0e10cSrcweir                     impl_deleteCondition_nothrow( impl_getFocusedConditionIndex( 0 ) );
526*cdf0e10cSrcweir                     return 1;
527*cdf0e10cSrcweir                 }
528*cdf0e10cSrcweir                 if ( rKeyCode.GetCode() == 0x0507 ) // +
529*cdf0e10cSrcweir                 {
530*cdf0e10cSrcweir                     impl_addCondition_nothrow( impl_getFocusedConditionIndex( impl_getConditionCount() - 1 ) + 1 );
531*cdf0e10cSrcweir                     return 1;
532*cdf0e10cSrcweir                 }
533*cdf0e10cSrcweir             }
534*cdf0e10cSrcweir         }
535*cdf0e10cSrcweir         break;
536*cdf0e10cSrcweir         case EVENT_GETFOCUS:
537*cdf0e10cSrcweir         {
538*cdf0e10cSrcweir             if ( m_bDeletingCondition )
539*cdf0e10cSrcweir                 break;
540*cdf0e10cSrcweir 
541*cdf0e10cSrcweir             const Window* pGetFocusWindow( _rNEvt.GetWindow() );
542*cdf0e10cSrcweir 
543*cdf0e10cSrcweir             // determine whether the new focus window is part of an (currently invisible) condition
544*cdf0e10cSrcweir             const Window* pConditionCandidate = pGetFocusWindow->GetParent();
545*cdf0e10cSrcweir             const Window* pPlaygroundCandidate = pConditionCandidate ? pConditionCandidate->GetParent() : NULL;
546*cdf0e10cSrcweir             while   (   ( pPlaygroundCandidate )
547*cdf0e10cSrcweir                     &&  ( pPlaygroundCandidate != this )
548*cdf0e10cSrcweir                     &&  ( pPlaygroundCandidate != &m_aConditionPlayground )
549*cdf0e10cSrcweir                     )
550*cdf0e10cSrcweir             {
551*cdf0e10cSrcweir                 pConditionCandidate = pConditionCandidate->GetParent();
552*cdf0e10cSrcweir                 pPlaygroundCandidate = pConditionCandidate ? pConditionCandidate->GetParent() : NULL;
553*cdf0e10cSrcweir             }
554*cdf0e10cSrcweir             if ( pPlaygroundCandidate == &m_aConditionPlayground )
555*cdf0e10cSrcweir             {
556*cdf0e10cSrcweir                 impl_ensureConditionVisible( dynamic_cast< const Condition& >( *pConditionCandidate ).getConditionIndex() );
557*cdf0e10cSrcweir             }
558*cdf0e10cSrcweir         }
559*cdf0e10cSrcweir         break;
560*cdf0e10cSrcweir         }
561*cdf0e10cSrcweir 
562*cdf0e10cSrcweir         return ModalDialog::PreNotify( _rNEvt );
563*cdf0e10cSrcweir     }
564*cdf0e10cSrcweir 
565*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
566*cdf0e10cSrcweir     size_t ConditionalFormattingDialog::impl_getFirstVisibleConditionIndex() const
567*cdf0e10cSrcweir     {
568*cdf0e10cSrcweir         return (size_t)m_aCondScroll.GetThumbPos();
569*cdf0e10cSrcweir     }
570*cdf0e10cSrcweir 
571*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
572*cdf0e10cSrcweir     size_t ConditionalFormattingDialog::impl_getLastVisibleConditionIndex() const
573*cdf0e10cSrcweir     {
574*cdf0e10cSrcweir         return ::std::min( impl_getFirstVisibleConditionIndex() + MAX_CONDITIONS, impl_getConditionCount() ) - 1;
575*cdf0e10cSrcweir     }
576*cdf0e10cSrcweir 
577*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
578*cdf0e10cSrcweir     size_t ConditionalFormattingDialog::impl_getFocusedConditionIndex( sal_Int32 _nFallBackIfNone ) const
579*cdf0e10cSrcweir     {
580*cdf0e10cSrcweir         size_t nIndex( 0 );
581*cdf0e10cSrcweir         for (   Conditions::const_iterator cond = m_aConditions.begin();
582*cdf0e10cSrcweir                 cond != m_aConditions.end();
583*cdf0e10cSrcweir                 ++cond, ++nIndex
584*cdf0e10cSrcweir             )
585*cdf0e10cSrcweir         {
586*cdf0e10cSrcweir             if ( (*cond)->HasChildPathFocus() )
587*cdf0e10cSrcweir                 return nIndex;
588*cdf0e10cSrcweir         }
589*cdf0e10cSrcweir         return _nFallBackIfNone;
590*cdf0e10cSrcweir     }
591*cdf0e10cSrcweir 
592*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
593*cdf0e10cSrcweir     void ConditionalFormattingDialog::impl_updateScrollBarRange()
594*cdf0e10cSrcweir     {
595*cdf0e10cSrcweir         long nMax = ( impl_getConditionCount() > MAX_CONDITIONS ) ? impl_getConditionCount() - MAX_CONDITIONS + 1 : 0;
596*cdf0e10cSrcweir 
597*cdf0e10cSrcweir         m_aCondScroll.SetRangeMin( 0 );
598*cdf0e10cSrcweir         m_aCondScroll.SetRangeMax( nMax );
599*cdf0e10cSrcweir         m_aCondScroll.SetVisibleSize( 1 );
600*cdf0e10cSrcweir     }
601*cdf0e10cSrcweir 
602*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
603*cdf0e10cSrcweir     void ConditionalFormattingDialog::impl_scrollTo( size_t _nTopCondIndex )
604*cdf0e10cSrcweir     {
605*cdf0e10cSrcweir         OSL_PRECOND( _nTopCondIndex + MAX_CONDITIONS <= impl_getConditionCount(),
606*cdf0e10cSrcweir             "ConditionalFormattingDialog::impl_scrollTo: illegal index!" );
607*cdf0e10cSrcweir         m_aCondScroll.SetThumbPos( _nTopCondIndex );
608*cdf0e10cSrcweir         OnScroll( &m_aCondScroll );
609*cdf0e10cSrcweir     }
610*cdf0e10cSrcweir 
611*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
612*cdf0e10cSrcweir     void ConditionalFormattingDialog::impl_ensureConditionVisible( size_t _nCondIndex )
613*cdf0e10cSrcweir     {
614*cdf0e10cSrcweir         OSL_PRECOND( _nCondIndex < impl_getConditionCount(),
615*cdf0e10cSrcweir             "ConditionalFormattingDialog::impl_ensureConditionVisible: illegal index!" );
616*cdf0e10cSrcweir 
617*cdf0e10cSrcweir         if ( _nCondIndex < impl_getFirstVisibleConditionIndex() )
618*cdf0e10cSrcweir             impl_scrollTo( _nCondIndex );
619*cdf0e10cSrcweir         else if ( _nCondIndex > impl_getLastVisibleConditionIndex() )
620*cdf0e10cSrcweir             impl_scrollTo( _nCondIndex - MAX_CONDITIONS + 1 );
621*cdf0e10cSrcweir     }
622*cdf0e10cSrcweir 
623*cdf0e10cSrcweir // .............................................................................
624*cdf0e10cSrcweir } // rptui
625*cdf0e10cSrcweir // .............................................................................
626