1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "CondFormat.hxx"
21 #include "CondFormat.hrc"
23 #include "uistrings.hrc"
24 #include "RptResId.hrc"
25 #include "rptui_slotid.hrc"
26 #include "ModuleHelper.hxx"
27 #include "helpids.hrc"
28 #include "UITools.hxx"
29 #include "ReportController.hxx"
30 #include "Condition.hxx"
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/lang/IllegalArgumentException.hpp>
35 #include <toolkit/helper/vclunohelper.hxx>
37 #include <vcl/msgbox.hxx>
38 #include <vcl/settings.hxx>
40 #include <tools/debug.hxx>
41 #include <tools/diagnose_ex.h>
43 #include <comphelper/property.hxx>
46 #include "UndoActions.hxx"
53 using ::com::sun::star::uno::Reference
;
54 using ::com::sun::star::uno::UNO_QUERY_THROW
;
55 using ::com::sun::star::uno::UNO_QUERY
;
56 using ::com::sun::star::uno::Exception
;
57 using ::com::sun::star::lang::IllegalArgumentException
;
58 using ::com::sun::star::uno::Sequence
;
59 using ::com::sun::star::beans::PropertyValue
;
60 using ::com::sun::star::uno::Any
;
62 using namespace ::com::sun::star::report
;
69 vcl::Window
& m_rWindow
;
72 UpdateLocker( vcl::Window
& _rWindow
)
73 :m_rWindow( _rWindow
)
75 _rWindow
.SetUpdateMode( false );
79 m_rWindow
.SetUpdateMode( true );
83 void ConditionalFormattingDialog::impl_setPrefHeight(bool bFirst
)
85 if (!m_bConstructed
&& !bFirst
)
88 //allow dialog to resize itself
89 size_t nCount
= impl_getConditionCount();
92 long nHeight
= m_aConditions
[0]->get_preferred_size().Height();
93 size_t nVisibleConditions
= ::std::min(nCount
, MAX_CONDITIONS
);
94 nHeight
*= nVisibleConditions
;
95 if (nHeight
!= m_pScrollWindow
->get_height_request())
97 m_pScrollWindow
->set_height_request(nHeight
);
98 if (!isCalculatingInitialLayoutSize() && !bFirst
)
99 setOptimalLayoutSize();
104 // class ConditionalFormattingDialog
105 ConditionalFormattingDialog::ConditionalFormattingDialog(
106 vcl::Window
* _pParent
, const Reference
< XReportControlModel
>& _rxFormatConditions
, ::rptui::OReportController
& _rController
)
107 :ModalDialog( _pParent
, "CondFormat", "modules/dbreport/ui/condformatdialog.ui" )
108 ,m_rController( _rController
)
109 ,m_xFormatConditions( _rxFormatConditions
)
110 ,m_bDeletingCondition( false )
111 ,m_bConstructed( false )
113 get(m_pConditionPlayground
, "condPlaygroundDrawingarea");
114 get(m_pScrollWindow
, "scrolledwindow");
115 m_pScrollWindow
->setUserManagedScrolling(true);
116 m_pCondScroll
= &(m_pScrollWindow
->getVertScrollBar());
118 OSL_ENSURE( m_xFormatConditions
.is(), "ConditionalFormattingDialog::ConditionalFormattingDialog: ReportControlModel is NULL -> Prepare for GPF!" );
120 m_xCopy
.set( m_xFormatConditions
->createClone(), UNO_QUERY_THROW
);
122 m_pCondScroll
->SetScrollHdl( LINK( this, ConditionalFormattingDialog
, OnScroll
) );
124 impl_initializeConditions();
126 impl_setPrefHeight(true);
128 m_bConstructed
= true;
131 ConditionalFormattingDialog::~ConditionalFormattingDialog()
136 void ConditionalFormattingDialog::dispose()
138 m_aConditions
.clear();
139 m_pConditionPlayground
.clear();
140 m_pScrollWindow
.clear();
141 m_pCondScroll
.clear();
142 ModalDialog::dispose();
145 void ConditionalFormattingDialog::impl_updateConditionIndicies()
147 sal_Int32 nIndex
= 0;
148 for ( Conditions::const_iterator cond
= m_aConditions
.begin();
149 cond
!= m_aConditions
.end();
153 (*cond
)->setConditionIndex( nIndex
, impl_getConditionCount() );
157 void ConditionalFormattingDialog::impl_conditionCountChanged()
159 if ( m_aConditions
.empty() )
160 impl_addCondition_nothrow( 0 );
162 impl_setPrefHeight(false);
164 impl_updateScrollBarRange();
165 impl_updateConditionIndicies();
169 void ConditionalFormattingDialog::addCondition( size_t _nAddAfterIndex
)
171 OSL_PRECOND( _nAddAfterIndex
< impl_getConditionCount(), "ConditionalFormattingDialog::addCondition: illegal condition index!" );
172 impl_addCondition_nothrow( _nAddAfterIndex
+ 1 );
176 void ConditionalFormattingDialog::deleteCondition( size_t _nCondIndex
)
178 impl_deleteCondition_nothrow( _nCondIndex
);
182 void ConditionalFormattingDialog::impl_addCondition_nothrow( size_t _nNewCondIndex
)
184 UpdateLocker
aLockUpdates( *this );
188 if ( _nNewCondIndex
> (size_t)m_xCopy
->getCount() )
189 throw IllegalArgumentException();
191 Reference
< XFormatCondition
> xCond
= m_xCopy
->createFormatCondition();
192 ::comphelper::copyProperties(m_xCopy
.get(),xCond
.get());
193 m_xCopy
->insertByIndex( _nNewCondIndex
, makeAny( xCond
) );
195 VclPtrInstance
<Condition
> pCon( m_pConditionPlayground
, *this, m_rController
);
196 pCon
->setCondition( xCond
);
197 pCon
->reorderWithinParent(_nNewCondIndex
);
198 m_aConditions
.insert( m_aConditions
.begin() + _nNewCondIndex
, pCon
);
200 catch( const Exception
& )
202 DBG_UNHANDLED_EXCEPTION();
205 impl_conditionCountChanged();
207 impl_ensureConditionVisible( _nNewCondIndex
);
211 void ConditionalFormattingDialog::impl_focusCondition( size_t _nCondIndex
)
213 OSL_PRECOND( _nCondIndex
< impl_getConditionCount(),
214 "ConditionalFormattingDialog::impl_focusCondition: illegal index!" );
216 impl_ensureConditionVisible( _nCondIndex
);
217 m_aConditions
[ _nCondIndex
]->GrabFocus();
221 void ConditionalFormattingDialog::impl_deleteCondition_nothrow( size_t _nCondIndex
)
223 UpdateLocker
aLockUpdates( *this );
225 OSL_PRECOND( _nCondIndex
< impl_getConditionCount(),
226 "ConditionalFormattingDialog::impl_deleteCondition_nothrow: illegal index!" );
228 bool bLastCondition
= ( impl_getConditionCount() == 1 );
230 bool bSetNewFocus
= false;
231 size_t nNewFocusIndex( _nCondIndex
);
234 if ( !bLastCondition
)
235 m_xCopy
->removeByIndex( _nCondIndex
);
237 Conditions::iterator pos
= m_aConditions
.begin() + _nCondIndex
;
238 if ( bLastCondition
)
240 Reference
< XFormatCondition
> xFormatCondition( m_xCopy
->getByIndex( 0 ), UNO_QUERY_THROW
);
241 xFormatCondition
->setFormula( OUString() );
242 (*pos
)->setCondition( xFormatCondition
);
246 bSetNewFocus
= (*pos
)->HasChildPathFocus();
247 m_bDeletingCondition
= true;
248 m_aConditions
.erase( pos
);
249 m_bDeletingCondition
= false;
254 if ( nNewFocusIndex
>= impl_getConditionCount() )
255 nNewFocusIndex
= impl_getConditionCount() - 1;
258 catch( const Exception
& )
260 DBG_UNHANDLED_EXCEPTION();
263 impl_conditionCountChanged();
265 impl_focusCondition( nNewFocusIndex
);
269 void ConditionalFormattingDialog::impl_moveCondition_nothrow( size_t _nCondIndex
, bool _bMoveUp
)
271 size_t nOldConditionIndex( _nCondIndex
);
272 size_t nNewConditionIndex( _bMoveUp
? _nCondIndex
- 1 : _nCondIndex
+ 1 );
274 // do this in two steps, so we don't become inconsistent if any of the UNO actions fails
276 Condition
*pMovedCondition
;
279 aMovedCondition
= m_xCopy
->getByIndex( (sal_Int32
)nOldConditionIndex
);
280 m_xCopy
->removeByIndex( (sal_Int32
)nOldConditionIndex
);
282 Conditions::iterator
aRemovePos( m_aConditions
.begin() + nOldConditionIndex
);
283 pMovedCondition
= *aRemovePos
;
284 m_aConditions
.erase( aRemovePos
);
286 catch( const Exception
& )
288 DBG_UNHANDLED_EXCEPTION();
294 m_xCopy
->insertByIndex( (sal_Int32
)nNewConditionIndex
, aMovedCondition
);
295 m_aConditions
.insert( m_aConditions
.begin() + nNewConditionIndex
, pMovedCondition
);
297 catch( const Exception
& )
299 DBG_UNHANDLED_EXCEPTION();
302 // at least the two swapped conditions need to know their new index
303 impl_updateConditionIndicies();
305 // re-layout all conditions
306 impl_layoutConditions();
308 // ensure the moved condition is visible
309 impl_ensureConditionVisible( nNewConditionIndex
);
312 IMPL_LINK( ConditionalFormattingDialog
, OnScroll
, ScrollBar
*, /*_pNotInterestedIn*/ )
314 size_t nFirstCondIndex( impl_getFirstVisibleConditionIndex() );
315 size_t nFocusCondIndex
= impl_getFocusedConditionIndex( nFirstCondIndex
);
317 impl_layoutConditions();
319 if ( nFocusCondIndex
< nFirstCondIndex
)
320 impl_focusCondition( nFirstCondIndex
);
321 else if ( nFocusCondIndex
>= nFirstCondIndex
+ MAX_CONDITIONS
)
322 impl_focusCondition( nFirstCondIndex
+ MAX_CONDITIONS
- 1 );
327 void ConditionalFormattingDialog::impl_layoutConditions()
329 if (m_aConditions
.empty())
331 long nConditionHeight
= m_aConditions
[0]->get_preferred_size().Height();
332 Point
aConditionPos(0, -1 * nConditionHeight
* impl_getFirstVisibleConditionIndex());
333 m_pConditionPlayground
->SetPosPixel(aConditionPos
);
336 void ConditionalFormattingDialog::impl_layoutAll()
338 // condition's positions
339 impl_layoutConditions();
341 // scrollbar visibility
342 if ( !impl_needScrollBar() )
343 // normalize the position, so it can, in all situations, be used as top index
344 m_pCondScroll
->SetThumbPos( 0 );
347 void ConditionalFormattingDialog::impl_initializeConditions()
351 sal_Int32 nCount
= m_xCopy
->getCount();
352 for ( sal_Int32 i
= 0; i
< nCount
; ++i
)
354 VclPtrInstance
<Condition
> pCon( m_pConditionPlayground
, *this, m_rController
);
355 Reference
< XFormatCondition
> xCond( m_xCopy
->getByIndex(i
), UNO_QUERY
);
356 pCon
->reorderWithinParent(i
);
357 pCon
->setCondition( xCond
);
358 pCon
->updateToolbar( xCond
.get() );
359 m_aConditions
.push_back( pCon
);
364 OSL_FAIL("Can not access format condition!");
367 impl_conditionCountChanged();
370 void ConditionalFormattingDialog::applyCommand(size_t _nCondIndex
, sal_uInt16 _nCommandId
, const ::Color
& rColor
)
372 OSL_PRECOND( _nCommandId
, "ConditionalFormattingDialog::applyCommand: illegal command id!" );
375 Reference
< XReportControlFormat
> xReportControlFormat( m_xCopy
->getByIndex( _nCondIndex
), UNO_QUERY_THROW
);
377 Sequence
< PropertyValue
> aArgs(3);
379 aArgs
[0].Name
= REPORTCONTROLFORMAT
;
380 aArgs
[0].Value
<<= xReportControlFormat
;
382 aArgs
[1].Name
= CURRENT_WINDOW
;
383 aArgs
[1].Value
<<= VCLUnoHelper::GetInterface(this);
385 aArgs
[2].Name
= PROPERTY_FONTCOLOR
;
386 aArgs
[2].Value
<<= (sal_uInt32
)rColor
.GetColor();
388 // we use this way to create undo actions
389 m_rController
.executeUnChecked(_nCommandId
,aArgs
);
390 m_aConditions
[ _nCondIndex
]->updateToolbar(xReportControlFormat
);
394 DBG_UNHANDLED_EXCEPTION();
399 void ConditionalFormattingDialog::moveConditionUp( size_t _nCondIndex
)
401 OSL_PRECOND( _nCondIndex
> 0, "ConditionalFormattingDialog::moveConditionUp: cannot move up the first condition!" );
402 if ( _nCondIndex
> 0 )
403 impl_moveCondition_nothrow( _nCondIndex
, true );
407 void ConditionalFormattingDialog::moveConditionDown( size_t _nCondIndex
)
409 OSL_PRECOND( _nCondIndex
< impl_getConditionCount(), "ConditionalFormattingDialog::moveConditionDown: cannot move down the last condition!" );
410 if ( _nCondIndex
< impl_getConditionCount() )
411 impl_moveCondition_nothrow( _nCondIndex
, false );
415 OUString
ConditionalFormattingDialog::getDataField() const
420 sDataField
= m_xFormatConditions
->getDataField();
422 catch( const Exception
& )
424 DBG_UNHANDLED_EXCEPTION();
430 short ConditionalFormattingDialog::Execute()
432 short nRet
= ModalDialog::Execute();
433 if ( nRet
== RET_OK
)
435 const OUString
sUndoAction( ModuleRes( RID_STR_UNDO_CONDITIONAL_FORMATTING
) );
436 const UndoContext
aUndoContext( m_rController
.getUndoManager(), sUndoAction
);
439 sal_Int32
j(0), i(0);;
440 for ( Conditions::const_iterator cond
= m_aConditions
.begin();
441 cond
!= m_aConditions
.end();
445 Reference
< XFormatCondition
> xCond( m_xCopy
->getByIndex(i
), UNO_QUERY_THROW
);
446 (*cond
)->fillFormatCondition( xCond
);
448 if ( (*cond
)->isEmpty() )
451 Reference
< XFormatCondition
> xNewCond
;
452 bool bAppend
= j
>= m_xFormatConditions
->getCount();
455 xNewCond
= m_xFormatConditions
->createFormatCondition();
456 m_xFormatConditions
->insertByIndex( i
, makeAny( xNewCond
) );
459 xNewCond
.set( m_xFormatConditions
->getByIndex(j
), UNO_QUERY
);
462 ::comphelper::copyProperties(xCond
.get(),xNewCond
.get());
465 for ( sal_Int32 k
= m_xFormatConditions
->getCount()-1; k
>= j
; --k
)
466 m_xFormatConditions
->removeByIndex(k
);
468 ::comphelper::copyProperties( m_xCopy
.get(), m_xFormatConditions
.get() );
470 catch ( const Exception
& )
472 DBG_UNHANDLED_EXCEPTION();
480 bool ConditionalFormattingDialog::PreNotify( NotifyEvent
& _rNEvt
)
482 switch ( _rNEvt
.GetType() )
484 case MouseNotifyEvent::KEYINPUT
:
486 const KeyEvent
* pKeyEvent( _rNEvt
.GetKeyEvent() );
487 const vcl::KeyCode
& rKeyCode
= pKeyEvent
->GetKeyCode();
488 if ( rKeyCode
.IsMod1() && rKeyCode
.IsMod2() )
490 if ( rKeyCode
.GetCode() == 0x0508 )
492 impl_deleteCondition_nothrow( impl_getFocusedConditionIndex( 0 ) );
495 if ( rKeyCode
.GetCode() == 0x0507 ) // +
497 impl_addCondition_nothrow( impl_getFocusedConditionIndex( impl_getConditionCount() - 1 ) + 1 );
503 case MouseNotifyEvent::GETFOCUS
:
505 if ( m_bDeletingCondition
)
508 const vcl::Window
* pGetFocusWindow( _rNEvt
.GetWindow() );
510 // determine whether the new focus window is part of an (currently invisible) condition
511 const vcl::Window
* pConditionCandidate
= pGetFocusWindow
->GetParent();
512 const vcl::Window
* pPlaygroundCandidate
= pConditionCandidate
? pConditionCandidate
->GetParent() : NULL
;
513 while ( ( pPlaygroundCandidate
)
514 && ( pPlaygroundCandidate
!= this )
515 && ( pPlaygroundCandidate
!= m_pConditionPlayground
)
518 pConditionCandidate
= pConditionCandidate
->GetParent();
519 pPlaygroundCandidate
= pConditionCandidate
? pConditionCandidate
->GetParent() : NULL
;
521 if (pConditionCandidate
&& pPlaygroundCandidate
== m_pConditionPlayground
)
523 impl_ensureConditionVisible( dynamic_cast< const Condition
& >( *pConditionCandidate
).getConditionIndex() );
531 return ModalDialog::PreNotify( _rNEvt
);
535 size_t ConditionalFormattingDialog::impl_getFirstVisibleConditionIndex() const
537 return (size_t)m_pCondScroll
->GetThumbPos();
541 size_t ConditionalFormattingDialog::impl_getLastVisibleConditionIndex() const
543 return ::std::min( impl_getFirstVisibleConditionIndex() + MAX_CONDITIONS
, impl_getConditionCount() ) - 1;
547 size_t ConditionalFormattingDialog::impl_getFocusedConditionIndex( sal_Int32 _nFallBackIfNone
) const
550 for ( Conditions::const_iterator cond
= m_aConditions
.begin();
551 cond
!= m_aConditions
.end();
555 if ( (*cond
)->HasChildPathFocus() )
558 return _nFallBackIfNone
;
562 void ConditionalFormattingDialog::impl_updateScrollBarRange()
564 long nMax
= ( impl_getConditionCount() > MAX_CONDITIONS
) ? impl_getConditionCount() - MAX_CONDITIONS
+ 1 : 0;
566 m_pCondScroll
->SetRangeMin( 0 );
567 m_pCondScroll
->SetRangeMax( nMax
);
568 m_pCondScroll
->SetVisibleSize( 1 );
572 void ConditionalFormattingDialog::impl_scrollTo( size_t _nTopCondIndex
)
574 OSL_PRECOND( _nTopCondIndex
+ MAX_CONDITIONS
<= impl_getConditionCount(),
575 "ConditionalFormattingDialog::impl_scrollTo: illegal index!" );
576 m_pCondScroll
->SetThumbPos( _nTopCondIndex
);
577 OnScroll( m_pCondScroll
);
581 void ConditionalFormattingDialog::impl_ensureConditionVisible( size_t _nCondIndex
)
583 OSL_PRECOND( _nCondIndex
< impl_getConditionCount(),
584 "ConditionalFormattingDialog::impl_ensureConditionVisible: illegal index!" );
586 if ( _nCondIndex
< impl_getFirstVisibleConditionIndex() )
587 impl_scrollTo( _nCondIndex
);
588 else if ( _nCondIndex
> impl_getLastVisibleConditionIndex() )
589 impl_scrollTo( _nCondIndex
- MAX_CONDITIONS
+ 1 );
596 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */