1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "fmcontrolbordermanager.hxx"
34 /** === begin UNO includes === **/
35 #include <com/sun/star/form/validation/XValidatableFormComponent.hpp>
36 #include <com/sun/star/awt/XTextComponent.hpp>
37 #include <com/sun/star/awt/XListBox.hpp>
38 /** === end UNO includes === **/
39 #include <tools/debug.hxx>
41 //........................................................................
44 //........................................................................
46 using namespace ::com::sun::star::uno
;
47 using namespace ::com::sun::star::awt
;
48 using namespace ::com::sun::star::form::validation
;
50 //====================================================================
52 //====================================================================
53 //--------------------------------------------------------------------
54 static void setUnderline( const Reference
< XVclWindowPeer
>& _rxPeer
, const UnderlineDescriptor
& _rUnderline
)
56 OSL_ENSURE( _rxPeer
.is(), "setUnderline: invalid peer!" );
58 // the underline type is an aspect of the font
60 OSL_VERIFY( _rxPeer
->getProperty( FM_PROP_FONT
) >>= aFont
);
61 aFont
.Underline
= _rUnderline
.nUnderlineType
;
62 _rxPeer
->setProperty( FM_PROP_FONT
, makeAny( aFont
) );
63 // the underline color is a separate property
64 _rxPeer
->setProperty( FM_PROP_TEXTLINECOLOR
, makeAny( _rUnderline
.nUnderlineColor
) );
67 //--------------------------------------------------------------------
68 static void getUnderline( const Reference
< XVclWindowPeer
>& _rxPeer
, UnderlineDescriptor
& _rUnderline
)
70 OSL_ENSURE( _rxPeer
.is(), "getUnderline: invalid peer!" );
73 OSL_VERIFY( _rxPeer
->getProperty( FM_PROP_FONT
) >>= aFont
);
74 _rUnderline
.nUnderlineType
= aFont
.Underline
;
76 OSL_VERIFY( _rxPeer
->getProperty( FM_PROP_TEXTLINECOLOR
) >>= _rUnderline
.nUnderlineColor
);
79 //--------------------------------------------------------------------
80 static void getBorder( const Reference
< XVclWindowPeer
>& _rxPeer
, BorderDescriptor
& _rBoder
)
82 OSL_ENSURE( _rxPeer
.is(), "getBorder: invalid peer!" );
84 OSL_VERIFY( _rxPeer
->getProperty( FM_PROP_BORDER
) >>= _rBoder
.nBorderType
);
85 OSL_VERIFY( _rxPeer
->getProperty( FM_PROP_BORDERCOLOR
) >>= _rBoder
.nBorderColor
);
88 //--------------------------------------------------------------------
89 static void setBorder( const Reference
< XVclWindowPeer
>& _rxPeer
, const BorderDescriptor
& _rBoder
)
91 OSL_ENSURE( _rxPeer
.is(), "setBorder: invalid peer!" );
93 _rxPeer
->setProperty( FM_PROP_BORDER
, makeAny( _rBoder
.nBorderType
) );
94 _rxPeer
->setProperty( FM_PROP_BORDERCOLOR
, makeAny( _rBoder
.nBorderColor
) );
97 //====================================================================
98 //= ControlBorderManager
99 //====================================================================
100 //--------------------------------------------------------------------
101 ControlBorderManager::ControlBorderManager()
102 :m_nFocusColor ( 0x000000FF )
103 ,m_nMouseHoveColor( 0x007098BE )
104 ,m_nInvalidColor ( 0x00FF0000 )
105 ,m_bDynamicBorderColors( false )
109 //--------------------------------------------------------------------
110 ControlBorderManager::~ControlBorderManager()
114 //--------------------------------------------------------------------
115 bool ControlBorderManager::canColorBorder( const Reference
< XVclWindowPeer
>& _rxPeer
)
117 OSL_PRECOND( _rxPeer
.is(), "ControlBorderManager::canColorBorder: invalid peer!" );
119 PeerBag::const_iterator aPos
= m_aColorableControls
.find( _rxPeer
);
120 if ( aPos
!= m_aColorableControls
.end() )
123 aPos
= m_aNonColorableControls
.find( _rxPeer
);
124 if ( aPos
!= m_aNonColorableControls
.end() )
127 // this peer is not yet known
129 // no border coloring for controls which are not for text input
130 // #i37434# / 2004-11-19 / frank.schoenheit@sun.com
131 Reference
< XTextComponent
> xText( _rxPeer
, UNO_QUERY
);
132 Reference
< XListBox
> xListBox( _rxPeer
, UNO_QUERY
);
133 if ( xText
.is() || xListBox
.is() )
135 sal_Int16 nBorderStyle
= VisualEffect::NONE
;
136 OSL_VERIFY( _rxPeer
->getProperty( FM_PROP_BORDER
) >>= nBorderStyle
);
137 if ( nBorderStyle
== VisualEffect::FLAT
)
138 // if you change this to also accept LOOK3D, then this would also work, but look ugly
140 m_aColorableControls
.insert( _rxPeer
);
145 m_aNonColorableControls
.insert( _rxPeer
);
149 //--------------------------------------------------------------------
150 ControlStatus
ControlBorderManager::getControlStatus( const Reference
< XControl
>& _rxControl
) SAL_THROW(())
152 ControlStatus nStatus
= CONTROL_STATUS_NONE
;
154 if ( _rxControl
.get() == m_aFocusControl
.xControl
.get() )
155 nStatus
|= CONTROL_STATUS_FOCUSED
;
157 if ( _rxControl
.get() == m_aMouseHoverControl
.xControl
.get() )
158 nStatus
|= CONTROL_STATUS_MOUSE_HOVER
;
160 if ( m_aInvalidControls
.find( ControlData( _rxControl
) ) != m_aInvalidControls
.end() )
161 nStatus
|= CONTROL_STATUS_INVALID
;
166 //--------------------------------------------------------------------
167 sal_Int32
ControlBorderManager::getControlColorByStatus( ControlStatus _nStatus
)
169 // "invalid" is ranked highest
170 if ( _nStatus
& CONTROL_STATUS_INVALID
)
171 return m_nInvalidColor
;
173 // then, "focused" is more important than ...
174 if ( _nStatus
& CONTROL_STATUS_FOCUSED
)
175 return m_nFocusColor
;
178 if ( _nStatus
& CONTROL_STATUS_MOUSE_HOVER
)
179 return m_nMouseHoveColor
;
181 OSL_FAIL( "ControlBorderManager::getControlColorByStatus: invalid status!" );
185 //--------------------------------------------------------------------
186 void ControlBorderManager::updateBorderStyle( const Reference
< XControl
>& _rxControl
, const Reference
< XVclWindowPeer
>& _rxPeer
, const BorderDescriptor
& _rFallback
) SAL_THROW(())
188 OSL_PRECOND( _rxControl
.is() && _rxPeer
.is(), "ControlBorderManager::updateBorderStyle: invalid parameters!" );
190 ControlStatus nStatus
= getControlStatus( _rxControl
);
191 BorderDescriptor aBorder
;
192 aBorder
.nBorderType
= ( nStatus
== CONTROL_STATUS_NONE
)
193 ? _rFallback
.nBorderType
194 : VisualEffect::FLAT
;
195 aBorder
.nBorderColor
= ( nStatus
== CONTROL_STATUS_NONE
)
196 ? _rFallback
.nBorderColor
197 : getControlColorByStatus( nStatus
);
198 setBorder( _rxPeer
, aBorder
);
201 //--------------------------------------------------------------------
202 void ControlBorderManager::determineOriginalBorderStyle( const Reference
< XControl
>& _rxControl
, BorderDescriptor
& _rData
) const
204 _rData
= ControlData();
205 if ( m_aFocusControl
.xControl
.get() == _rxControl
.get() )
207 _rData
= m_aFocusControl
;
209 else if ( m_aMouseHoverControl
.xControl
.get() == _rxControl
.get() )
211 _rData
= m_aMouseHoverControl
;
215 ControlBag::const_iterator aPos
= m_aInvalidControls
.find( _rxControl
);
216 if ( aPos
!= m_aInvalidControls
.end() )
222 Reference
< XVclWindowPeer
> xPeer( _rxControl
->getPeer(), UNO_QUERY
);
223 getBorder( xPeer
, _rData
);
228 //--------------------------------------------------------------------
229 void ControlBorderManager::controlStatusGained( const Reference
< XInterface
>& _rxControl
, ControlData
& _rControlData
) SAL_THROW(())
231 if ( _rxControl
== _rControlData
.xControl
)
232 // nothing to do - though suspicious
235 Reference
< XControl
> xAsControl( _rxControl
, UNO_QUERY
);
236 DBG_ASSERT( xAsControl
.is(), "ControlBorderManager::controlStatusGained: invalid control!" );
237 if ( !xAsControl
.is() )
242 Reference
< XVclWindowPeer
> xPeer( xAsControl
->getPeer(), UNO_QUERY
);
243 if ( xPeer
.is() && canColorBorder( xPeer
) )
245 // remember the control and it's current border color
246 _rControlData
.xControl
.clear(); // so determineOriginalBorderStyle doesn't get confused
248 determineOriginalBorderStyle( xAsControl
, _rControlData
);
250 _rControlData
.xControl
= xAsControl
;
252 updateBorderStyle( xAsControl
, xPeer
, _rControlData
);
255 catch( const Exception
& )
257 OSL_FAIL( "ControlBorderManager::controlStatusGained: caught an exception!" );
261 //--------------------------------------------------------------------
262 void ControlBorderManager::controlStatusLost( const Reference
< XInterface
>& _rxControl
, ControlData
& _rControlData
) SAL_THROW(())
264 if ( _rxControl
!= _rControlData
.xControl
)
268 OSL_PRECOND( _rControlData
.xControl
.is(), "ControlBorderManager::controlStatusLost: invalid control data - this will crash!" );
271 Reference
< XVclWindowPeer
> xPeer( _rControlData
.xControl
->getPeer(), UNO_QUERY
);
272 if ( xPeer
.is() && canColorBorder( xPeer
) )
274 ControlData
aPreviousStatus( _rControlData
);
275 _rControlData
= ControlData();
276 updateBorderStyle( aPreviousStatus
.xControl
, xPeer
, aPreviousStatus
);
279 catch( const Exception
& )
281 OSL_FAIL( "ControlBorderManager::controlStatusLost: caught an exception!" );
285 //--------------------------------------------------------------------
286 void ControlBorderManager::enableDynamicBorderColor( )
288 m_bDynamicBorderColors
= true;
291 //--------------------------------------------------------------------
292 void ControlBorderManager::disableDynamicBorderColor( )
294 m_bDynamicBorderColors
= false;
298 //--------------------------------------------------------------------
299 void ControlBorderManager::setStatusColor( ControlStatus _nStatus
, sal_Int32 _nColor
)
303 case CONTROL_STATUS_FOCUSED
:
304 m_nFocusColor
= _nColor
;
306 case CONTROL_STATUS_MOUSE_HOVER
:
307 m_nMouseHoveColor
= _nColor
;
309 case CONTROL_STATUS_INVALID
:
310 m_nInvalidColor
= _nColor
;
313 OSL_FAIL( "ControlBorderManager::setStatusColor: invalid status!" );
317 //--------------------------------------------------------------------
318 void ControlBorderManager::restoreAll()
320 if ( m_aFocusControl
.xControl
.is() )
321 controlStatusLost( m_aFocusControl
.xControl
, m_aFocusControl
);
322 if ( m_aMouseHoverControl
.xControl
.is() )
323 controlStatusLost( m_aMouseHoverControl
.xControl
, m_aMouseHoverControl
);
325 ControlBag aInvalidControls
;
326 m_aInvalidControls
.swap( aInvalidControls
);
328 for ( ControlBag::const_iterator loop
= aInvalidControls
.begin();
329 loop
!= aInvalidControls
.end();
333 Reference
< XVclWindowPeer
> xPeer( loop
->xControl
->getPeer(), UNO_QUERY
);
336 updateBorderStyle( loop
->xControl
, xPeer
, *loop
);
337 xPeer
->setProperty( FM_PROP_HELPTEXT
, makeAny( loop
->sOriginalHelpText
) );
338 setUnderline( xPeer
, *loop
);
343 //--------------------------------------------------------------------
344 void ControlBorderManager::focusGained( const Reference
< XInterface
>& _rxControl
) SAL_THROW(())
346 if ( m_bDynamicBorderColors
)
347 controlStatusGained( _rxControl
, m_aFocusControl
);
350 //--------------------------------------------------------------------
351 void ControlBorderManager::focusLost( const Reference
< XInterface
>& _rxControl
) SAL_THROW(())
353 if ( m_bDynamicBorderColors
)
354 controlStatusLost( _rxControl
, m_aFocusControl
);
357 //--------------------------------------------------------------------
358 void ControlBorderManager::mouseEntered( const Reference
< XInterface
>& _rxControl
) SAL_THROW(())
360 if ( m_bDynamicBorderColors
)
361 controlStatusGained( _rxControl
, m_aMouseHoverControl
);
364 //--------------------------------------------------------------------
365 void ControlBorderManager::mouseExited( const Reference
< XInterface
>& _rxControl
) SAL_THROW(())
367 if ( m_bDynamicBorderColors
)
368 controlStatusLost( _rxControl
, m_aMouseHoverControl
);
371 //--------------------------------------------------------------------
372 void ControlBorderManager::validityChanged( const Reference
< XControl
>& _rxControl
, const Reference
< XValidatableFormComponent
>& _rxValidatable
) SAL_THROW(())
376 OSL_ENSURE( _rxControl
.is(), "ControlBorderManager::validityChanged: invalid control!" );
377 OSL_ENSURE( _rxValidatable
.is(), "ControlBorderManager::validityChanged: invalid validatable!" );
379 Reference
< XVclWindowPeer
> xPeer( _rxControl
.is() ? _rxControl
->getPeer() : Reference
< XWindowPeer
>(), UNO_QUERY
);
380 if ( !xPeer
.is() || !_rxValidatable
.is() )
383 ControlData
aData( _rxControl
);
385 if ( _rxValidatable
->isValid() )
387 ControlBag::iterator aPos
= m_aInvalidControls
.find( aData
);
388 if ( aPos
!= m_aInvalidControls
.end() )
389 { // invalid before, valid now
390 ControlData
aOriginalLayout( *aPos
);
391 m_aInvalidControls
.erase( aPos
);
393 // restore all the things we used to indicate invalidity
394 if ( m_bDynamicBorderColors
)
395 updateBorderStyle( _rxControl
, xPeer
, aOriginalLayout
);
396 xPeer
->setProperty( FM_PROP_HELPTEXT
, makeAny( aOriginalLayout
.sOriginalHelpText
) );
397 setUnderline( xPeer
, aOriginalLayout
);
402 // we're here in the INVALID case
403 if ( m_aInvalidControls
.find( _rxControl
) == m_aInvalidControls
.end() )
404 { // valid before, invalid now
406 // remember the current border
407 determineOriginalBorderStyle( _rxControl
, aData
);
409 xPeer
->getProperty( FM_PROP_HELPTEXT
) >>= aData
.sOriginalHelpText
;
411 getUnderline( xPeer
, aData
);
413 m_aInvalidControls
.insert( aData
);
415 // update the border to the new invalidity
416 if ( m_bDynamicBorderColors
&& canColorBorder( xPeer
) )
417 updateBorderStyle( _rxControl
, xPeer
, aData
);
420 // and also the new font
421 setUnderline( xPeer
, UnderlineDescriptor( com::sun::star::awt::FontUnderline::WAVE
, m_nInvalidColor
) );
425 // update the explanation for invalidity (this is always done, even if the validity did not change)
426 Reference
< XValidator
> xValidator
= _rxValidatable
->getValidator();
427 OSL_ENSURE( xValidator
.is(), "ControlBorderManager::validityChanged: invalid, but no validator?" );
428 ::rtl::OUString sExplainInvalidity
= xValidator
.is() ? xValidator
->explainInvalid( _rxValidatable
->getCurrentValue() ) : ::rtl::OUString();
429 xPeer
->setProperty( FM_PROP_HELPTEXT
, makeAny( sExplainInvalidity
) );
431 catch( const Exception
& )
433 OSL_FAIL( "ControlBorderManager::validityChanged: caught an exception!" );
437 //........................................................................
438 } // namespace svxform
439 //........................................................................
441 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */