merge the formfield patch from ooo-build
[ooovba.git] / reportdesign / source / ui / dlg / CondFormat.cxx
blob95fe0dec1fd97b9577842163b11dc515608fb8a0
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: CondFormat.cxx,v $
10 * $Revision: 1.4 $
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 "CondFormat.hxx"
33 #include "CondFormat.hrc"
35 #include "uistrings.hrc"
36 #include "RptResId.hrc"
37 #include "rptui_slotid.hrc"
38 #include "ModuleHelper.hxx"
39 #include "helpids.hrc"
40 #include "UITools.hxx"
41 #include "uistrings.hrc"
42 #include "ReportController.hxx"
43 #include "Condition.hxx"
45 /** === begin UNO includes === **/
46 #include <com/sun/star/beans/XPropertySet.hpp>
47 #include <com/sun/star/lang/IllegalArgumentException.hpp>
48 /** === end UNO includes === **/
50 #include <svx/globlmn.hrc>
51 #include <svx/svxids.hrc>
53 #include <toolkit/helper/vclunohelper.hxx>
55 #include <vcl/msgbox.hxx>
57 #include <tools/debug.hxx>
58 #include <tools/diagnose_ex.h>
60 #include <comphelper/property.hxx>
62 #include <algorithm>
63 #include "UndoActions.hxx"
65 // .............................................................................
66 namespace rptui
68 // .............................................................................
70 /** === begin UNO using === **/
71 using ::com::sun::star::uno::Reference;
72 using ::com::sun::star::uno::UNO_QUERY_THROW;
73 using ::com::sun::star::uno::UNO_QUERY;
74 using ::com::sun::star::uno::Exception;
75 using ::com::sun::star::lang::IllegalArgumentException;
76 using ::com::sun::star::uno::Sequence;
77 using ::com::sun::star::beans::PropertyValue;
78 using ::com::sun::star::uno::Any;
79 /** === end UNO using === **/
80 using namespace ::com::sun::star::report;
82 //========================================================================
83 // UpdateLocker
84 //========================================================================
85 class UpdateLocker
87 Window& m_rWindow;
89 public:
90 UpdateLocker( Window& _rWindow )
91 :m_rWindow( _rWindow )
93 _rWindow.SetUpdateMode( FALSE );
95 ~UpdateLocker()
97 m_rWindow.SetUpdateMode( TRUE );
101 //========================================================================
102 // class ConditionalFormattingDialog
103 //========================================================================
104 DBG_NAME(rpt_ConditionalFormattingDialog)
105 ConditionalFormattingDialog::ConditionalFormattingDialog(
106 Window* _pParent, const Reference< XReportControlModel >& _rxFormatConditions, ::rptui::OReportController& _rController )
107 :ModalDialog( _pParent, ModuleRes(RID_CONDFORMAT) )
108 ,m_aConditionPlayground( this, ModuleRes( WND_COND_PLAYGROUND ) )
109 ,m_aSeparator(this, ModuleRes(FL_SEPARATOR1))
110 ,m_aPB_OK(this, ModuleRes(PB_OK))
111 ,m_aPB_CANCEL(this, ModuleRes(PB_CANCEL))
112 ,m_aPB_Help(this, ModuleRes(PB_HELP))
113 ,m_aCondScroll( this, ModuleRes( SB_ALL_CONDITIONS ) )
114 ,m_rController( _rController )
115 ,m_xFormatConditions( _rxFormatConditions )
116 ,m_bDeletingCondition( false )
118 DBG_CTOR(rpt_ConditionalFormattingDialog,NULL);
119 OSL_ENSURE( m_xFormatConditions.is(), "ConditionalFormattingDialog::ConditionalFormattingDialog: ReportControlModel is NULL -> Prepare for GPF!" );
121 m_xCopy.set( m_xFormatConditions->createClone(), UNO_QUERY_THROW );
123 m_aCondScroll.SetScrollHdl( LINK( this, ConditionalFormattingDialog, OnScroll ) );
125 impl_initializeConditions();
127 FreeResource();
130 //------------------------------------------------------------------------
131 ConditionalFormattingDialog::~ConditionalFormattingDialog()
133 m_aConditions.clear();
134 DBG_DTOR(rpt_ConditionalFormattingDialog,NULL);
137 // -----------------------------------------------------------------------------
138 void ConditionalFormattingDialog::impl_updateConditionIndicies()
140 sal_Int32 nIndex = 0;
141 for ( Conditions::const_iterator cond = m_aConditions.begin();
142 cond != m_aConditions.end();
143 ++cond, ++nIndex
146 (*cond)->setConditionIndex( nIndex, impl_getConditionCount() );
150 // -----------------------------------------------------------------------------
151 void ConditionalFormattingDialog::impl_conditionCountChanged()
153 if ( m_aConditions.empty() )
154 impl_addCondition_nothrow( 0 );
156 impl_updateScrollBarRange();
157 impl_updateConditionIndicies();
158 impl_layoutAll();
161 // -----------------------------------------------------------------------------
162 void ConditionalFormattingDialog::addCondition( size_t _nAddAfterIndex )
164 OSL_PRECOND( _nAddAfterIndex < impl_getConditionCount(), "ConditionalFormattingDialog::addCondition: illegal condition index!" );
165 impl_addCondition_nothrow( _nAddAfterIndex + 1 );
168 // -----------------------------------------------------------------------------
169 void ConditionalFormattingDialog::deleteCondition( size_t _nCondIndex )
171 impl_deleteCondition_nothrow( _nCondIndex );
174 // -----------------------------------------------------------------------------
175 void ConditionalFormattingDialog::impl_addCondition_nothrow( size_t _nNewCondIndex )
177 UpdateLocker aLockUpdates( *this );
181 if ( _nNewCondIndex > (size_t)m_xCopy->getCount() )
182 throw IllegalArgumentException();
184 Reference< XFormatCondition > xCond = m_xCopy->createFormatCondition();
185 ::comphelper::copyProperties(m_xCopy.get(),xCond.get());
186 m_xCopy->insertByIndex( _nNewCondIndex, makeAny( xCond ) );
188 ConditionPtr pCon( new Condition( &m_aConditionPlayground, *this, m_rController ) );
189 pCon->setCondition( xCond );
190 m_aConditions.insert( m_aConditions.begin() + _nNewCondIndex, pCon );
192 pCon->SetPosSizePixel( 0, 0, impl_getConditionWidth(), 0, WINDOW_POSSIZE_WIDTH );
194 catch( const Exception& )
196 DBG_UNHANDLED_EXCEPTION();
199 impl_conditionCountChanged();
201 impl_ensureConditionVisible( _nNewCondIndex );
204 // -----------------------------------------------------------------------------
205 void ConditionalFormattingDialog::impl_focusCondition( size_t _nCondIndex )
207 OSL_PRECOND( _nCondIndex < impl_getConditionCount(),
208 "ConditionalFormattingDialog::impl_focusCondition: illegal index!" );
210 impl_ensureConditionVisible( _nCondIndex );
211 m_aConditions[ _nCondIndex ]->GrabFocus();
214 // -----------------------------------------------------------------------------
215 void ConditionalFormattingDialog::impl_deleteCondition_nothrow( size_t _nCondIndex )
217 UpdateLocker aLockUpdates( *this );
219 OSL_PRECOND( _nCondIndex < impl_getConditionCount(),
220 "ConditionalFormattingDialog::impl_deleteCondition_nothrow: illegal index!" );
222 bool bLastCondition = ( impl_getConditionCount() == 1 );
224 bool bSetNewFocus = false;
225 size_t nNewFocusIndex( _nCondIndex );
228 if ( !bLastCondition )
229 m_xCopy->removeByIndex( _nCondIndex );
231 Conditions::iterator pos = m_aConditions.begin() + _nCondIndex;
232 if ( bLastCondition )
234 Reference< XFormatCondition > xFormatCondition( m_xCopy->getByIndex( 0 ), UNO_QUERY_THROW );
235 xFormatCondition->setFormula( ::rtl::OUString() );
236 (*pos)->setCondition( xFormatCondition );
238 else
240 bSetNewFocus = (*pos)->HasChildPathFocus();
241 m_bDeletingCondition = true;
242 m_aConditions.erase( pos );
243 m_bDeletingCondition = false;
246 if ( bSetNewFocus )
248 if ( nNewFocusIndex >= impl_getConditionCount() )
249 nNewFocusIndex = impl_getConditionCount() - 1;
252 catch( const Exception& )
254 DBG_UNHANDLED_EXCEPTION();
257 impl_conditionCountChanged();
258 if ( bSetNewFocus )
259 impl_focusCondition( nNewFocusIndex );
262 // -----------------------------------------------------------------------------
263 void ConditionalFormattingDialog::impl_moveCondition_nothrow( size_t _nCondIndex, bool _bMoveUp )
265 size_t nOldConditionIndex( _nCondIndex );
266 size_t nNewConditionIndex( _bMoveUp ? _nCondIndex - 1 : _nCondIndex + 1 );
268 // do this in two steps, so we don't become inconsistent if any of the UNO actions fails
269 Any aMovedCondition;
270 ConditionPtr pMovedCondition;
273 aMovedCondition = m_xCopy->getByIndex( (sal_Int32)nOldConditionIndex );
274 m_xCopy->removeByIndex( (sal_Int32)nOldConditionIndex );
276 Conditions::iterator aRemovePos( m_aConditions.begin() + nOldConditionIndex );
277 pMovedCondition = *aRemovePos;
278 m_aConditions.erase( aRemovePos );
280 catch( const Exception& )
282 DBG_UNHANDLED_EXCEPTION();
283 return;
288 m_xCopy->insertByIndex( (sal_Int32)nNewConditionIndex, aMovedCondition );
289 m_aConditions.insert( m_aConditions.begin() + nNewConditionIndex, pMovedCondition );
291 catch( const Exception& )
293 DBG_UNHANDLED_EXCEPTION();
296 // at least the two swapped conditions need to know their new index
297 impl_updateConditionIndicies();
299 // re-layout all conditions
300 Point aDummy;
301 impl_layoutConditions( aDummy );
303 // ensure the moved condition is visible
304 impl_ensureConditionVisible( nNewConditionIndex );
307 // -----------------------------------------------------------------------------
308 long ConditionalFormattingDialog::impl_getConditionWidth() const
310 const Size aDialogSize( GetOutputSizePixel() );
311 const Size aScrollBarWidth( LogicToPixel( Size( SCROLLBAR_WIDTH + UNRELATED_CONTROLS, 0 ), MAP_APPFONT ) );
312 return aDialogSize.Width() - aScrollBarWidth.Width();
315 // -----------------------------------------------------------------------------
316 IMPL_LINK( ConditionalFormattingDialog, OnScroll, ScrollBar*, /*_pNotInterestedIn*/ )
318 size_t nFirstCondIndex( impl_getFirstVisibleConditionIndex() );
319 size_t nFocusCondIndex = impl_getFocusedConditionIndex( nFirstCondIndex );
321 Point aDummy;
322 impl_layoutConditions( aDummy );
324 if ( nFocusCondIndex < nFirstCondIndex )
325 impl_focusCondition( nFirstCondIndex );
326 else if ( nFocusCondIndex >= nFirstCondIndex + MAX_CONDITIONS )
327 impl_focusCondition( nFirstCondIndex + MAX_CONDITIONS - 1 );
329 return 0;
332 // -----------------------------------------------------------------------------
333 void ConditionalFormattingDialog::impl_layoutConditions( Point& _out_rBelowLastVisible )
335 // position the condition's playground
336 long nConditionWidth = impl_getConditionWidth();
337 long nConditionHeight = LogicToPixel( Size( 0, CONDITION_HEIGHT ), MAP_APPFONT ).Height();
338 size_t nVisibleConditions = ::std::min( impl_getConditionCount(), MAX_CONDITIONS );
339 Size aPlaygroundSize( nConditionWidth, nVisibleConditions * nConditionHeight );
340 m_aConditionPlayground.SetSizePixel( aPlaygroundSize );
341 _out_rBelowLastVisible = Point( 0, aPlaygroundSize.Height() );
343 // position the single conditions
344 Point aConditionPos( 0, -1 * nConditionHeight * impl_getFirstVisibleConditionIndex() );
345 for ( Conditions::const_iterator cond = m_aConditions.begin();
346 cond != m_aConditions.end();
347 ++cond
350 (*cond)->SetPosSizePixel( aConditionPos.X(), aConditionPos.Y(), nConditionWidth, nConditionHeight );
351 aConditionPos.Move( 0, nConditionHeight );
355 // -----------------------------------------------------------------------------
356 void ConditionalFormattingDialog::impl_layoutAll()
358 // condition's positions
359 Point aPos;
360 impl_layoutConditions( aPos );
362 // scrollbar size and visibility
363 m_aCondScroll.SetPosSizePixel( 0, 0, 0, aPos.Y(), WINDOW_POSSIZE_HEIGHT );
364 if ( !impl_needScrollBar() )
365 // normalize the position, so it can, in all situations, be used as top index
366 m_aCondScroll.SetThumbPos( 0 );
368 // the separator and the buttons below it
369 aPos += LogicToPixel( Point( 0 , RELATED_CONTROLS ), MAP_APPFONT );
370 m_aSeparator.SetPosSizePixel( 0, aPos.Y(), 0, 0, WINDOW_POSSIZE_Y );
372 aPos += LogicToPixel( Point( 0 , UNRELATED_CONTROLS ), MAP_APPFONT );
373 Window* pWindows[] = { &m_aPB_OK, &m_aPB_CANCEL, &m_aPB_Help };
374 for ( size_t i= 0; i < sizeof(pWindows)/sizeof(pWindows[0]); ++i )
376 pWindows[i]->SetPosSizePixel( 0, aPos.Y(), 0, 0, WINDOW_POSSIZE_Y );
379 aPos += LogicToPixel( Point( 0, BUTTON_HEIGHT + RELATED_CONTROLS ), MAP_APPFONT );
380 SetPosSizePixel( 0, 0, 0, aPos.Y(), WINDOW_POSSIZE_HEIGHT );
382 // -----------------------------------------------------------------------------
383 void ConditionalFormattingDialog::impl_initializeConditions()
387 sal_Int32 nCount = m_xCopy->getCount();
388 for ( sal_Int32 i = 0; i < nCount ; ++i )
390 ConditionPtr pCon( new Condition( &m_aConditionPlayground, *this, m_rController ) );
391 Reference< XFormatCondition > xCond( m_xCopy->getByIndex(i), UNO_QUERY );
392 pCon->setCondition( xCond );
393 pCon->updateToolbar( xCond.get() );
394 m_aConditions.push_back( pCon );
397 catch(Exception&)
399 OSL_ENSURE(0,"Can not access format condition!");
402 impl_conditionCountChanged();
405 // -----------------------------------------------------------------------------
406 void ConditionalFormattingDialog::applyCommand( size_t _nCondIndex, USHORT _nCommandId, const ::Color _aColor )
408 OSL_PRECOND( _nCommandId, "ConditionalFormattingDialog::applyCommand: illegal command id!" );
411 Reference< XReportControlFormat > xReportControlFormat( m_xCopy->getByIndex( _nCondIndex ), UNO_QUERY_THROW );
413 Sequence< PropertyValue > aArgs(3);
415 aArgs[0].Name = REPORTCONTROLFORMAT;
416 aArgs[0].Value <<= xReportControlFormat;
418 aArgs[1].Name = CURRENT_WINDOW;
419 aArgs[1].Value <<= VCLUnoHelper::GetInterface(this);
421 aArgs[2].Name = PROPERTY_FONTCOLOR;
422 aArgs[2].Value <<= (sal_uInt32)_aColor.GetColor();
424 // we use this way to create undo actions
425 m_rController.executeUnChecked(_nCommandId,aArgs);
426 m_aConditions[ _nCondIndex ]->updateToolbar(xReportControlFormat);
428 catch( Exception& )
430 DBG_UNHANDLED_EXCEPTION();
434 // -----------------------------------------------------------------------------
435 void ConditionalFormattingDialog::moveConditionUp( size_t _nCondIndex )
437 OSL_PRECOND( _nCondIndex > 0, "ConditionalFormattingDialog::moveConditionUp: cannot move up the first condition!" );
438 if ( _nCondIndex > 0 )
439 impl_moveCondition_nothrow( _nCondIndex, true );
442 // -----------------------------------------------------------------------------
443 void ConditionalFormattingDialog::moveConditionDown( size_t _nCondIndex )
445 OSL_PRECOND( _nCondIndex < impl_getConditionCount(), "ConditionalFormattingDialog::moveConditionDown: cannot move down the last condition!" );
446 if ( _nCondIndex < impl_getConditionCount() )
447 impl_moveCondition_nothrow( _nCondIndex, false );
450 // -----------------------------------------------------------------------------
451 ::rtl::OUString ConditionalFormattingDialog::getDataField() const
453 ::rtl::OUString sDataField;
456 sDataField = m_xFormatConditions->getDataField();
458 catch( const Exception& )
460 DBG_UNHANDLED_EXCEPTION();
462 return sDataField;
465 // -----------------------------------------------------------------------------
466 short ConditionalFormattingDialog::Execute()
468 short nRet = ModalDialog::Execute();
469 if ( nRet == RET_OK )
471 String sUndoAction( ModuleRes( RID_STR_UNDO_CONDITIONAL_FORMATTING ) );
472 UndoManagerListAction aListAction(*m_rController.getUndoMgr(),sUndoAction);
475 sal_Int32 j(0), i(0);;
476 for ( Conditions::const_iterator cond = m_aConditions.begin();
477 cond != m_aConditions.end();
478 ++cond, ++i
481 Reference< XFormatCondition > xCond( m_xCopy->getByIndex(i), UNO_QUERY_THROW );
482 (*cond)->fillFormatCondition( xCond );
484 if ( (*cond)->isEmpty() )
485 continue;
487 Reference< XFormatCondition > xNewCond;
488 sal_Bool bAppend = j >= m_xFormatConditions->getCount();
489 if ( bAppend )
491 xNewCond = m_xFormatConditions->createFormatCondition();
492 m_xFormatConditions->insertByIndex( i, makeAny( xNewCond ) );
494 else
495 xNewCond.set( m_xFormatConditions->getByIndex(j), UNO_QUERY );
496 ++j;
498 ::comphelper::copyProperties(xCond.get(),xNewCond.get());
501 for ( sal_Int32 k = m_xFormatConditions->getCount()-1; k >= j; --k )
502 m_xFormatConditions->removeByIndex(k);
504 ::comphelper::copyProperties( m_xCopy.get(), m_xFormatConditions.get() );
506 catch ( const Exception& )
508 DBG_UNHANDLED_EXCEPTION();
509 nRet = RET_NO;
512 return nRet;
515 // -----------------------------------------------------------------------------
516 long ConditionalFormattingDialog::PreNotify( NotifyEvent& _rNEvt )
518 switch ( _rNEvt.GetType() )
520 case EVENT_KEYINPUT:
522 const KeyEvent* pKeyEvent( _rNEvt.GetKeyEvent() );
523 const KeyCode& rKeyCode = pKeyEvent->GetKeyCode();
524 if ( rKeyCode.IsMod1() && rKeyCode.IsMod2() )
526 if ( rKeyCode.GetCode() == 0x0508 ) // -
528 impl_deleteCondition_nothrow( impl_getFocusedConditionIndex( 0 ) );
529 return 1;
531 if ( rKeyCode.GetCode() == 0x0507 ) // +
533 impl_addCondition_nothrow( impl_getFocusedConditionIndex( impl_getConditionCount() - 1 ) + 1 );
534 return 1;
538 break;
539 case EVENT_GETFOCUS:
541 if ( m_bDeletingCondition )
542 break;
544 const Window* pGetFocusWindow( _rNEvt.GetWindow() );
546 // determine whether the new focus window is part of an (currently invisible) condition
547 const Window* pConditionCandidate = pGetFocusWindow->GetParent();
548 const Window* pPlaygroundCandidate = pConditionCandidate ? pConditionCandidate->GetParent() : NULL;
549 while ( ( pPlaygroundCandidate )
550 && ( pPlaygroundCandidate != this )
551 && ( pPlaygroundCandidate != &m_aConditionPlayground )
554 pConditionCandidate = pConditionCandidate->GetParent();
555 pPlaygroundCandidate = pConditionCandidate ? pConditionCandidate->GetParent() : NULL;
557 if ( pPlaygroundCandidate == &m_aConditionPlayground )
559 impl_ensureConditionVisible( dynamic_cast< const Condition& >( *pConditionCandidate ).getConditionIndex() );
562 break;
565 return ModalDialog::PreNotify( _rNEvt );
568 // -----------------------------------------------------------------------------
569 size_t ConditionalFormattingDialog::impl_getFirstVisibleConditionIndex() const
571 return (size_t)m_aCondScroll.GetThumbPos();
574 // -----------------------------------------------------------------------------
575 size_t ConditionalFormattingDialog::impl_getLastVisibleConditionIndex() const
577 return ::std::min( impl_getFirstVisibleConditionIndex() + MAX_CONDITIONS, impl_getConditionCount() ) - 1;
580 // -----------------------------------------------------------------------------
581 size_t ConditionalFormattingDialog::impl_getFocusedConditionIndex( sal_Int32 _nFallBackIfNone ) const
583 size_t nIndex( 0 );
584 for ( Conditions::const_iterator cond = m_aConditions.begin();
585 cond != m_aConditions.end();
586 ++cond, ++nIndex
589 if ( (*cond)->HasChildPathFocus() )
590 return nIndex;
592 return _nFallBackIfNone;
595 // -----------------------------------------------------------------------------
596 void ConditionalFormattingDialog::impl_updateScrollBarRange()
598 long nMax = ( impl_getConditionCount() > MAX_CONDITIONS ) ? impl_getConditionCount() - MAX_CONDITIONS + 1 : 0;
600 m_aCondScroll.SetRangeMin( 0 );
601 m_aCondScroll.SetRangeMax( nMax );
602 m_aCondScroll.SetVisibleSize( 1 );
605 // -----------------------------------------------------------------------------
606 void ConditionalFormattingDialog::impl_scrollTo( size_t _nTopCondIndex )
608 OSL_PRECOND( _nTopCondIndex + MAX_CONDITIONS <= impl_getConditionCount(),
609 "ConditionalFormattingDialog::impl_scrollTo: illegal index!" );
610 m_aCondScroll.SetThumbPos( _nTopCondIndex );
611 OnScroll( &m_aCondScroll );
614 // -----------------------------------------------------------------------------
615 void ConditionalFormattingDialog::impl_ensureConditionVisible( size_t _nCondIndex )
617 OSL_PRECOND( _nCondIndex < impl_getConditionCount(),
618 "ConditionalFormattingDialog::impl_ensureConditionVisible: illegal index!" );
620 if ( _nCondIndex < impl_getFirstVisibleConditionIndex() )
621 impl_scrollTo( _nCondIndex );
622 else if ( _nCondIndex > impl_getLastVisibleConditionIndex() )
623 impl_scrollTo( _nCondIndex - MAX_CONDITIONS + 1 );
626 // .............................................................................
627 } // rptui
628 // .............................................................................