1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2008 by Sun Microsystems, Inc.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* $RCSfile: fmcontrolbordermanager.cxx,v $
* $Revision: 1.8 $
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_svx.hxx"
#ifndef SVX_SOURCE_FORM_FMCONTROLBORDERMANAGER_HXX
#include "fmcontrolbordermanager.hxx"
#endif
#ifndef _SVX_FMPROP_HRC
#include "fmprop.hrc"
#endif
/** === begin UNO includes === **/
#include <com/sun/star/form/validation/XValidatableFormComponent.hpp>
#include <com/sun/star/awt/XTextComponent.hpp>
#include <com/sun/star/awt/XListBox.hpp>
/** === end UNO includes === **/
#include <tools/debug.hxx>
//........................................................................
namespace svxform
{
//........................................................................
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::awt;
using namespace ::com::sun::star::form::validation;
//====================================================================
//= helper
//====================================================================
//--------------------------------------------------------------------
static void setUnderline( const Reference< XVclWindowPeer >& _rxPeer, const UnderlineDescriptor& _rUnderline )
{
OSL_ENSURE( _rxPeer.is(), "setUnderline: invalid peer!" );
// the underline type is an aspect of the font
FontDescriptor aFont;
OSL_VERIFY( _rxPeer->getProperty( FM_PROP_FONT ) >>= aFont );
aFont.Underline = _rUnderline.nUnderlineType;
_rxPeer->setProperty( FM_PROP_FONT, makeAny( aFont ) );
// the underline color is a separate property
_rxPeer->setProperty( FM_PROP_TEXTLINECOLOR, makeAny( _rUnderline.nUnderlineColor ) );
}
//--------------------------------------------------------------------
static void getUnderline( const Reference< XVclWindowPeer >& _rxPeer, UnderlineDescriptor& _rUnderline )
{
OSL_ENSURE( _rxPeer.is(), "getUnderline: invalid peer!" );
FontDescriptor aFont;
OSL_VERIFY( _rxPeer->getProperty( FM_PROP_FONT ) >>= aFont );
_rUnderline.nUnderlineType = aFont.Underline;
OSL_VERIFY( _rxPeer->getProperty( FM_PROP_TEXTLINECOLOR ) >>= _rUnderline.nUnderlineColor );
}
//--------------------------------------------------------------------
static void getBorder( const Reference< XVclWindowPeer >& _rxPeer, BorderDescriptor& _rBoder )
{
OSL_ENSURE( _rxPeer.is(), "getBorder: invalid peer!" );
OSL_VERIFY( _rxPeer->getProperty( FM_PROP_BORDER ) >>= _rBoder.nBorderType );
OSL_VERIFY( _rxPeer->getProperty( FM_PROP_BORDERCOLOR ) >>= _rBoder.nBorderColor );
}
//--------------------------------------------------------------------
static void setBorder( const Reference< XVclWindowPeer >& _rxPeer, const BorderDescriptor& _rBoder )
{
OSL_ENSURE( _rxPeer.is(), "setBorder: invalid peer!" );
_rxPeer->setProperty( FM_PROP_BORDER, makeAny( _rBoder.nBorderType ) );
_rxPeer->setProperty( FM_PROP_BORDERCOLOR, makeAny( _rBoder.nBorderColor ) );
}
//====================================================================
//= ControlBorderManager
//====================================================================
//--------------------------------------------------------------------
ControlBorderManager::ControlBorderManager()
:m_nFocusColor ( 0x000000FF )
,m_nMouseHoveColor( 0x007098BE )
,m_nInvalidColor ( 0x00FF0000 )
,m_bDynamicBorderColors( false )
{
}
//--------------------------------------------------------------------
ControlBorderManager::~ControlBorderManager()
{
}
//--------------------------------------------------------------------
bool ControlBorderManager::canColorBorder( const Reference< XVclWindowPeer >& _rxPeer )
{
OSL_PRECOND( _rxPeer.is(), "ControlBorderManager::canColorBorder: invalid peer!" );
PeerBag::const_iterator aPos = m_aColorableControls.find( _rxPeer );
if ( aPos != m_aColorableControls.end() )
return true;
aPos = m_aNonColorableControls.find( _rxPeer );
if ( aPos != m_aNonColorableControls.end() )
return false;
// this peer is not yet known
// no border coloring for controls which are not for text input
// #i37434# / 2004-11-19 / frank.schoenheit@sun.com
Reference< XTextComponent > xText( _rxPeer, UNO_QUERY );
Reference< XListBox > xListBox( _rxPeer, UNO_QUERY );
if ( xText.is() || xListBox.is() )
{
sal_Int16 nBorderStyle = VisualEffect::NONE;
OSL_VERIFY( _rxPeer->getProperty( FM_PROP_BORDER ) >>= nBorderStyle );
if ( nBorderStyle == VisualEffect::FLAT )
// if you change this to also accept LOOK3D, then this would also work, but look ugly
{
m_aColorableControls.insert( _rxPeer );
return true;
}
}
m_aNonColorableControls.insert( _rxPeer );
return false;
}
//--------------------------------------------------------------------
ControlStatus ControlBorderManager::getControlStatus( const Reference< XControl >& _rxControl ) SAL_THROW(())
{
ControlStatus nStatus = CONTROL_STATUS_NONE;
if ( _rxControl.get() == m_aFocusControl.xControl.get() )
nStatus |= CONTROL_STATUS_FOCUSED;
if ( _rxControl.get() == m_aMouseHoverControl.xControl.get() )
nStatus |= CONTROL_STATUS_MOUSE_HOVER;
if ( m_aInvalidControls.find( ControlData( _rxControl ) ) != m_aInvalidControls.end() )
nStatus |= CONTROL_STATUS_INVALID;
return nStatus;
}
//--------------------------------------------------------------------
sal_Int32 ControlBorderManager::getControlColorByStatus( ControlStatus _nStatus )
{
// "invalid" is ranked highest
if ( _nStatus & CONTROL_STATUS_INVALID )
return m_nInvalidColor;
// then, "focused" is more important than ...
if ( _nStatus & CONTROL_STATUS_FOCUSED )
return m_nFocusColor;
// ... "mouse over"
if ( _nStatus & CONTROL_STATUS_MOUSE_HOVER )
return m_nMouseHoveColor;
OSL_ENSURE( sal_False, "ControlBorderManager::getControlColorByStatus: invalid status!" );
return 0x00000000;
}
//--------------------------------------------------------------------
void ControlBorderManager::updateBorderStyle( const Reference< XControl >& _rxControl, const Reference< XVclWindowPeer >& _rxPeer, const BorderDescriptor& _rFallback ) SAL_THROW(())
{
OSL_PRECOND( _rxControl.is() && _rxPeer.is(), "ControlBorderManager::updateBorderStyle: invalid parameters!" );
ControlStatus nStatus = getControlStatus( _rxControl );
BorderDescriptor aBorder;
aBorder.nBorderType = ( nStatus == CONTROL_STATUS_NONE )
? _rFallback.nBorderType
: VisualEffect::FLAT;
aBorder.nBorderColor = ( nStatus == CONTROL_STATUS_NONE )
? _rFallback.nBorderColor
: getControlColorByStatus( nStatus );
setBorder( _rxPeer, aBorder );
}
//--------------------------------------------------------------------
void ControlBorderManager::determineOriginalBorderStyle( const Reference< XControl >& _rxControl, BorderDescriptor& _rData ) const
{
_rData = ControlData();
if ( m_aFocusControl.xControl.get() == _rxControl.get() )
{
_rData = m_aFocusControl;
}
else if ( m_aMouseHoverControl.xControl.get() == _rxControl.get() )
{
_rData = m_aMouseHoverControl;
}
else
{
ControlBag::const_iterator aPos = m_aInvalidControls.find( _rxControl );
if ( aPos != m_aInvalidControls.end() )
{
_rData = *aPos;
}
else
{
Reference< XVclWindowPeer > xPeer( _rxControl->getPeer(), UNO_QUERY );
getBorder( xPeer, _rData );
}
}
}
//--------------------------------------------------------------------
void ControlBorderManager::controlStatusGained( const Reference< XInterface >& _rxControl, ControlData& _rControlData ) SAL_THROW(())
{
if ( _rxControl == _rControlData.xControl )
// nothing to do - though suspicious
return;
Reference< XControl > xAsControl( _rxControl, UNO_QUERY );
DBG_ASSERT( xAsControl.is(), "ControlBorderManager::controlStatusGained: invalid control!" );
if ( !xAsControl.is() )
return;
try
{
Reference< XVclWindowPeer > xPeer( xAsControl->getPeer(), UNO_QUERY );
if ( xPeer.is() && canColorBorder( xPeer ) )
{
// remember the control and it's current border color
_rControlData.xControl.clear(); // so determineOriginalBorderStyle doesn't get confused
determineOriginalBorderStyle( xAsControl, _rControlData );
_rControlData.xControl = xAsControl;
updateBorderStyle( xAsControl, xPeer, _rControlData );
}
}
catch( const Exception& )
{
OSL_ENSURE( sal_False, "ControlBorderManager::controlStatusGained: caught an exception!" );
}
}
//--------------------------------------------------------------------
void ControlBorderManager::controlStatusLost( const Reference< XInterface >& _rxControl, ControlData& _rControlData ) SAL_THROW(())
{
if ( _rxControl != _rControlData.xControl )
// nothing to do
return;
OSL_PRECOND( _rControlData.xControl.is(), "ControlBorderManager::controlStatusLost: invalid control data - this will crash!" );
try
{
Reference< XVclWindowPeer > xPeer( _rControlData.xControl->getPeer(), UNO_QUERY );
if ( xPeer.is() && canColorBorder( xPeer ) )
{
ControlData aPreviousStatus( _rControlData );
_rControlData = ControlData();
updateBorderStyle( aPreviousStatus.xControl, xPeer, aPreviousStatus );
}
}
catch( const Exception& )
{
OSL_ENSURE( sal_False, "ControlBorderManager::controlStatusLost: caught an exception!" );
}
}
//--------------------------------------------------------------------
void ControlBorderManager::enableDynamicBorderColor( )
{
m_bDynamicBorderColors = true;
}
//--------------------------------------------------------------------
void ControlBorderManager::disableDynamicBorderColor( )
{
m_bDynamicBorderColors = false;
restoreAll();
}
//--------------------------------------------------------------------
void ControlBorderManager::setStatusColor( ControlStatus _nStatus, sal_Int32 _nColor )
{
switch ( _nStatus )
{
case CONTROL_STATUS_FOCUSED:
m_nFocusColor = _nColor;
break;
case CONTROL_STATUS_MOUSE_HOVER:
m_nMouseHoveColor = _nColor;
break;
case CONTROL_STATUS_INVALID:
m_nInvalidColor = _nColor;
break;
default:
OSL_ENSURE( sal_False, "ControlBorderManager::setStatusColor: invalid status!" );
}
}
//--------------------------------------------------------------------
void ControlBorderManager::restoreAll()
{
if ( m_aFocusControl.xControl.is() )
controlStatusLost( m_aFocusControl.xControl, m_aFocusControl );
if ( m_aMouseHoverControl.xControl.is() )
controlStatusLost( m_aMouseHoverControl.xControl, m_aMouseHoverControl );
ControlBag aInvalidControls;
m_aInvalidControls.swap( aInvalidControls );
for ( ControlBag::const_iterator loop = aInvalidControls.begin();
loop != aInvalidControls.end();
++loop
)
{
Reference< XVclWindowPeer > xPeer( loop->xControl->getPeer(), UNO_QUERY );
if ( xPeer.is() )
{
updateBorderStyle( loop->xControl, xPeer, *loop );
xPeer->setProperty( FM_PROP_HELPTEXT, makeAny( loop->sOriginalHelpText ) );
setUnderline( xPeer, *loop );
}
}
}
//--------------------------------------------------------------------
void ControlBorderManager::focusGained( const Reference< XInterface >& _rxControl ) SAL_THROW(())
{
if ( m_bDynamicBorderColors )
controlStatusGained( _rxControl, m_aFocusControl );
}
//--------------------------------------------------------------------
void ControlBorderManager::focusLost( const Reference< XInterface >& _rxControl ) SAL_THROW(())
{
if ( m_bDynamicBorderColors )
controlStatusLost( _rxControl, m_aFocusControl );
}
//--------------------------------------------------------------------
void ControlBorderManager::mouseEntered( const Reference< XInterface >& _rxControl ) SAL_THROW(())
{
if ( m_bDynamicBorderColors )
controlStatusGained( _rxControl, m_aMouseHoverControl );
}
//--------------------------------------------------------------------
void ControlBorderManager::mouseExited( const Reference< XInterface >& _rxControl ) SAL_THROW(())
{
if ( m_bDynamicBorderColors )
controlStatusLost( _rxControl, m_aMouseHoverControl );
}
//--------------------------------------------------------------------
void ControlBorderManager::validityChanged( const Reference< XControl >& _rxControl, const Reference< XValidatableFormComponent >& _rxValidatable ) SAL_THROW(())
{
try
{
OSL_ENSURE( _rxControl.is(), "ControlBorderManager::validityChanged: invalid control!" );
OSL_ENSURE( _rxValidatable.is(), "ControlBorderManager::validityChanged: invalid validatable!" );
Reference< XVclWindowPeer > xPeer( _rxControl.is() ? _rxControl->getPeer() : Reference< XWindowPeer >(), UNO_QUERY );
if ( !xPeer.is() || !_rxValidatable.is() )
return;
ControlData aData( _rxControl );
if ( _rxValidatable->isValid() )
{
ControlBag::iterator aPos = m_aInvalidControls.find( aData );
if ( aPos != m_aInvalidControls.end() )
{ // invalid before, valid now
ControlData aOriginalLayout( *aPos );
m_aInvalidControls.erase( aPos );
// restore all the things we used to indicate invalidity
if ( m_bDynamicBorderColors )
updateBorderStyle( _rxControl, xPeer, aOriginalLayout );
xPeer->setProperty( FM_PROP_HELPTEXT, makeAny( aOriginalLayout.sOriginalHelpText ) );
setUnderline( xPeer, aOriginalLayout );
}
return;
}
// we're here in the INVALID case
if ( m_aInvalidControls.find( _rxControl ) == m_aInvalidControls.end() )
{ // valid before, invalid now
// remember the current border
determineOriginalBorderStyle( _rxControl, aData );
// and tool tip
xPeer->getProperty( FM_PROP_HELPTEXT ) >>= aData.sOriginalHelpText;
// and font
getUnderline( xPeer, aData );
m_aInvalidControls.insert( aData );
// update the border to the new invalidity
if ( m_bDynamicBorderColors && canColorBorder( xPeer ) )
updateBorderStyle( _rxControl, xPeer, aData );
else
{
// and also the new font
setUnderline( xPeer, UnderlineDescriptor( com::sun::star::awt::FontUnderline::WAVE, m_nInvalidColor ) );
}
}
// update the explanation for invalidity (this is always done, even if the validity did not change)
Reference< XValidator > xValidator = _rxValidatable->getValidator();
OSL_ENSURE( xValidator.is(), "ControlBorderManager::validityChanged: invalid, but no validator?" );
::rtl::OUString sExplainInvalidity = xValidator.is() ? xValidator->explainInvalid( _rxValidatable->getCurrentValue() ) : ::rtl::OUString();
xPeer->setProperty( FM_PROP_HELPTEXT, makeAny( sExplainInvalidity ) );
}
catch( const Exception& )
{
OSL_ENSURE( sal_False, "ControlBorderManager::validityChanged: caught an exception!" );
}
}
//........................................................................
} // namespace svxform
//........................................................................
|