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