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 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* 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.
*
************************************************************************/
#include <comphelper/processfactory.hxx>
#include <tools/rc.h>
#include <vcl/svapp.hxx>
#include <vcl/event.hxx>
#include <vcl/ctrl.hxx>
#include <vcl/decoview.hxx>
#include <vcl/salnativewidgets.hxx>
#include <textlayout.hxx>
#include <svdata.hxx>
#include <controldata.hxx>
using namespace vcl;
// =======================================================================
void Control::ImplInitControlData()
{
mbHasFocus = sal_False;
mpControlData = new ImplControlData;
}
// -----------------------------------------------------------------------
Control::Control( WindowType nType ) :
Window( nType )
{
ImplInitControlData();
}
// -----------------------------------------------------------------------
Control::Control( Window* pParent, WinBits nStyle ) :
Window( WINDOW_CONTROL )
{
ImplInitControlData();
Window::ImplInit( pParent, nStyle, NULL );
}
// -----------------------------------------------------------------------
Control::Control( Window* pParent, const ResId& rResId ) :
Window( WINDOW_CONTROL )
{
ImplInitControlData();
rResId.SetRT( RSC_CONTROL );
WinBits nStyle = ImplInitRes( rResId );
ImplInit( pParent, nStyle, NULL );
ImplLoadRes( rResId );
if ( !(nStyle & WB_HIDE) )
Show();
}
// -----------------------------------------------------------------------
Control::~Control()
{
delete mpControlData, mpControlData = NULL;
}
// -----------------------------------------------------------------------
void Control::GetFocus()
{
Window::GetFocus();
}
// -----------------------------------------------------------------------
void Control::LoseFocus()
{
Window::LoseFocus();
}
// -----------------------------------------------------------------------
void Control::Resize()
{
ImplClearLayoutData();
Window::Resize();
}
// -----------------------------------------------------------------------
void Control::FillLayoutData() const
{
}
// -----------------------------------------------------------------------
void Control::CreateLayoutData() const
{
DBG_ASSERT( !mpControlData->mpLayoutData, "Control::CreateLayoutData: should be called with non-existent layout data only!" );
mpControlData->mpLayoutData = new ::vcl::ControlLayoutData();
}
// -----------------------------------------------------------------------
bool Control::HasLayoutData() const
{
return mpControlData->mpLayoutData != NULL;
}
// -----------------------------------------------------------------------
::vcl::ControlLayoutData* Control::GetLayoutData() const
{
return mpControlData->mpLayoutData;
}
// -----------------------------------------------------------------------
void Control::SetText( const String& rStr )
{
ImplClearLayoutData();
Window::SetText( rStr );
}
// -----------------------------------------------------------------------
Rectangle ControlLayoutData::GetCharacterBounds( long nIndex ) const
{
return (nIndex >= 0 && nIndex < (long) m_aUnicodeBoundRects.size()) ? m_aUnicodeBoundRects[ nIndex ] : Rectangle();
}
// -----------------------------------------------------------------------
Rectangle Control::GetCharacterBounds( long nIndex ) const
{
if( !HasLayoutData() )
FillLayoutData();
return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->GetCharacterBounds( nIndex ) : Rectangle();
}
// -----------------------------------------------------------------------
long ControlLayoutData::GetIndexForPoint( const Point& rPoint ) const
{
long nIndex = -1;
for( long i = m_aUnicodeBoundRects.size()-1; i >= 0; i-- )
{
if( m_aUnicodeBoundRects[ i ].IsInside( rPoint ) )
{
nIndex = i;
break;
}
}
return nIndex;
}
// -----------------------------------------------------------------------
long Control::GetIndexForPoint( const Point& rPoint ) const
{
if( ! HasLayoutData() )
FillLayoutData();
return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->GetIndexForPoint( rPoint ) : -1;
}
// -----------------------------------------------------------------------
long ControlLayoutData::GetLineCount() const
{
long nLines = m_aLineIndices.size();
if( nLines == 0 && m_aDisplayText.Len() )
nLines = 1;
return nLines;
}
// -----------------------------------------------------------------------
long Control::GetLineCount() const
{
if( !HasLayoutData() )
FillLayoutData();
return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->GetLineCount() : 0;
}
// -----------------------------------------------------------------------
Pair ControlLayoutData::GetLineStartEnd( long nLine ) const
{
Pair aPair( -1, -1 );
int nDisplayLines = m_aLineIndices.size();
if( nLine >= 0 && nLine < nDisplayLines )
{
aPair.A() = m_aLineIndices[nLine];
if( nLine+1 < nDisplayLines )
aPair.B() = m_aLineIndices[nLine+1]-1;
else
aPair.B() = m_aDisplayText.Len()-1;
}
else if( nLine == 0 && nDisplayLines == 0 && m_aDisplayText.Len() )
{
// special case for single line controls so the implementations
// in that case do not have to fill in the line indices
aPair.A() = 0;
aPair.B() = m_aDisplayText.Len()-1;
}
return aPair;
}
// -----------------------------------------------------------------------
Pair Control::GetLineStartEnd( long nLine ) const
{
if( !HasLayoutData() )
FillLayoutData();
return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->GetLineStartEnd( nLine ) : Pair( -1, -1 );
}
// -----------------------------------------------------------------------
long ControlLayoutData::ToRelativeLineIndex( long nIndex ) const
{
// is the index sensible at all ?
if( nIndex >= 0 && nIndex < m_aDisplayText.Len() )
{
int nDisplayLines = m_aLineIndices.size();
// if only 1 line exists, then absolute and relative index are
// identical -> do nothing
if( nDisplayLines > 1 )
{
int nLine;
for( nLine = nDisplayLines-1; nLine >= 0; nLine-- )
{
if( m_aLineIndices[nLine] <= nIndex )
{
nIndex -= m_aLineIndices[nLine];
break;
}
}
if( nLine < 0 )
{
DBG_ASSERT( nLine >= 0, "ToRelativeLineIndex failed" );
nIndex = -1;
}
}
}
else
nIndex = -1;
return nIndex;
}
// -----------------------------------------------------------------------
long Control::ToRelativeLineIndex( long nIndex ) const
{
if( !HasLayoutData() )
FillLayoutData();
return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->ToRelativeLineIndex( nIndex ) : -1;
}
// -----------------------------------------------------------------------
String Control::GetDisplayText() const
{
if( !HasLayoutData() )
FillLayoutData();
return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->m_aDisplayText : GetText();
}
// -----------------------------------------------------------------------
long Control::Notify( NotifyEvent& rNEvt )
{
if ( rNEvt.GetType() == EVENT_GETFOCUS )
{
if ( !mbHasFocus )
{
mbHasFocus = sal_True;
if ( ImplCallEventListenersAndHandler( VCLEVENT_CONTROL_GETFOCUS, maGetFocusHdl, this ) )
// been destroyed within the handler
return sal_True;
}
}
else
{
if ( rNEvt.GetType() == EVENT_LOSEFOCUS )
{
Window* pFocusWin = Application::GetFocusWindow();
if ( !pFocusWin || !ImplIsWindowOrChild( pFocusWin ) )
{
mbHasFocus = sal_False;
if ( ImplCallEventListenersAndHandler( VCLEVENT_CONTROL_LOSEFOCUS, maLoseFocusHdl, this ) )
// been destroyed within the handler
return sal_True;
}
}
}
return Window::Notify( rNEvt );
}
// -----------------------------------------------------------------------
void Control::StateChanged( StateChangedType nStateChange )
{
if( nStateChange == STATE_CHANGE_INITSHOW ||
nStateChange == STATE_CHANGE_VISIBLE ||
nStateChange == STATE_CHANGE_FORMAT ||
nStateChange == STATE_CHANGE_ZOOM ||
nStateChange == STATE_CHANGE_BORDER ||
nStateChange == STATE_CHANGE_CONTROLFONT
)
{
ImplClearLayoutData();
}
Window::StateChanged( nStateChange );
}
// -----------------------------------------------------------------------
void Control::AppendLayoutData( const Control& rSubControl ) const
{
if( !rSubControl.HasLayoutData() )
rSubControl.FillLayoutData();
if( !rSubControl.HasLayoutData() || !rSubControl.mpControlData->mpLayoutData->m_aDisplayText.Len() )
return;
long nCurrentIndex = mpControlData->mpLayoutData->m_aDisplayText.Len();
mpControlData->mpLayoutData->m_aDisplayText.Append( rSubControl.mpControlData->mpLayoutData->m_aDisplayText );
int nLines = rSubControl.mpControlData->mpLayoutData->m_aLineIndices.size();
int n;
mpControlData->mpLayoutData->m_aLineIndices.push_back( nCurrentIndex );
for( n = 1; n < nLines; n++ )
mpControlData->mpLayoutData->m_aLineIndices.push_back( rSubControl.mpControlData->mpLayoutData->m_aLineIndices[n] + nCurrentIndex );
int nRectangles = rSubControl.mpControlData->mpLayoutData->m_aUnicodeBoundRects.size();
Rectangle aRel = const_cast<Control&>(rSubControl).GetWindowExtentsRelative( const_cast<Control*>(this) );
for( n = 0; n < nRectangles; n++ )
{
Rectangle aRect = rSubControl.mpControlData->mpLayoutData->m_aUnicodeBoundRects[n];
aRect.Move( aRel.Left(), aRel.Top() );
mpControlData->mpLayoutData->m_aUnicodeBoundRects.push_back( aRect );
}
}
// -----------------------------------------------------------------
sal_Bool Control::ImplCallEventListenersAndHandler( sal_uLong nEvent, const Link& rHandler, void* pCaller )
{
ImplDelData aCheckDelete;
ImplAddDel( &aCheckDelete );
ImplCallEventListeners( nEvent );
if ( !aCheckDelete.IsDelete() )
{
rHandler.Call( pCaller );
if ( !aCheckDelete.IsDelete() )
{
ImplRemoveDel( &aCheckDelete );
return sal_False;
}
}
return sal_True;
}
// -----------------------------------------------------------------
void Control::SetLayoutDataParent( const Control* pParent ) const
{
if( HasLayoutData() )
mpControlData->mpLayoutData->m_pParent = pParent;
}
// -----------------------------------------------------------------
void Control::ImplClearLayoutData() const
{
delete mpControlData->mpLayoutData, mpControlData->mpLayoutData = NULL;
}
// -----------------------------------------------------------------------
void Control::ImplDrawFrame( OutputDevice* pDev, Rectangle& rRect )
{
// use a deco view to draw the frame
// However, since there happens a lot of magic there, we need to fake some (style) settings
// on the device
AllSettings aOriginalSettings( pDev->GetSettings() );
AllSettings aNewSettings( aOriginalSettings );
StyleSettings aStyle( aNewSettings.GetStyleSettings() );
// The *only known* clients of the Draw methods of the various VCL-controls are form controls:
// During print preview, and during printing, Draw is called. Thus, drawing always happens with a
// mono (colored) border
aStyle.SetOptions( aStyle.GetOptions() | STYLE_OPTION_MONO );
aStyle.SetMonoColor( GetSettings().GetStyleSettings().GetMonoColor() );
aNewSettings.SetStyleSettings( aStyle );
// #i67023# do not call data changed listeners for this fake
// since they may understandably invalidate on settings changed
pDev->OutputDevice::SetSettings( aNewSettings );
DecorationView aDecoView( pDev );
rRect = aDecoView.DrawFrame( rRect, FRAME_DRAW_WINDOWBORDER );
pDev->OutputDevice::SetSettings( aOriginalSettings );
}
// -----------------------------------------------------------------------
void Control::DataChanged( const DataChangedEvent& rDCEvt)
{
// we don't want to loose some style settings for controls created with the
// toolkit
if ( IsCreatedWithToolkit() &&
(rDCEvt.GetType() == DATACHANGED_SETTINGS) &&
(rDCEvt.GetFlags() & SETTINGS_STYLE) )
{
AllSettings aSettings = GetSettings();
StyleSettings aStyleSettings = aSettings.GetStyleSettings();
sal_uLong nOldOptions = rDCEvt.GetOldSettings()->GetStyleSettings().GetOptions();
sal_uLong nNewOptions = aStyleSettings.GetOptions();
if ( !(nNewOptions & STYLE_OPTION_MONO) && ( nOldOptions & STYLE_OPTION_MONO ) )
{
nNewOptions |= STYLE_OPTION_MONO;
aStyleSettings.SetOptions( nNewOptions );
aStyleSettings.SetMonoColor( rDCEvt.GetOldSettings()->GetStyleSettings().GetMonoColor() );
aSettings.SetStyleSettings( aStyleSettings );
SetSettings( aSettings );
}
}
}
// -----------------------------------------------------------------
ControlLayoutData::~ControlLayoutData()
{
if( m_pParent )
m_pParent->ImplClearLayoutData();
}
// -----------------------------------------------------------------
Size Control::GetOptimalSize(WindowSizeType eType) const
{
switch (eType) {
case WINDOWSIZE_MINIMUM:
return Size( GetTextWidth( GetText() ) + 2 * 12,
GetTextHeight() + 2 * 6 );
case WINDOWSIZE_PREFERRED:
return GetOptimalSize( WINDOWSIZE_MINIMUM );
case WINDOWSIZE_MAXIMUM:
default:
return Size( LONG_MAX, LONG_MAX );
}
}
// -----------------------------------------------------------------
void Control::SetReferenceDevice( OutputDevice* _referenceDevice )
{
if ( mpControlData->mpReferenceDevice == _referenceDevice )
return;
mpControlData->mpReferenceDevice = _referenceDevice;
Invalidate();
}
// -----------------------------------------------------------------
OutputDevice* Control::GetReferenceDevice() const
{
return mpControlData->mpReferenceDevice;
}
// -----------------------------------------------------------------
const Font& Control::GetCanonicalFont( const StyleSettings& _rStyle ) const
{
return _rStyle.GetLabelFont();
}
// -----------------------------------------------------------------
const Color& Control::GetCanonicalTextColor( const StyleSettings& _rStyle ) const
{
return _rStyle.GetLabelTextColor();
}
// -----------------------------------------------------------------
void Control::ImplInitSettings( const sal_Bool _bFont, const sal_Bool _bForeground )
{
const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
if ( _bFont )
{
Font aFont( GetCanonicalFont( rStyleSettings ) );
if ( IsControlFont() )
aFont.Merge( GetControlFont() );
SetZoomedPointFont( aFont );
}
if ( _bForeground || _bFont )
{
Color aColor;
if ( IsControlForeground() )
aColor = GetControlForeground();
else
aColor = GetCanonicalTextColor( rStyleSettings );
SetTextColor( aColor );
SetTextFillColor();
}
}
// -----------------------------------------------------------------
void Control::DrawControlText( OutputDevice& _rTargetDevice, Rectangle& _io_rRect, const XubString& _rStr,
sal_uInt16 _nStyle, MetricVector* _pVector, String* _pDisplayText ) const
{
#ifdef FS_DEBUG
if ( !_pVector )
{
static MetricVector aCharRects;
static String sDisplayText;
aCharRects.clear();
sDisplayText = String();
_pVector = &aCharRects;
_pDisplayText = &sDisplayText;
}
#endif
if ( !mpControlData->mpReferenceDevice || ( mpControlData->mpReferenceDevice == &_rTargetDevice ) )
{
_io_rRect = _rTargetDevice.GetTextRect( _io_rRect, _rStr, _nStyle );
_rTargetDevice.DrawText( _io_rRect, _rStr, _nStyle, _pVector, _pDisplayText );
}
else
{
ControlTextRenderer aRenderer( *this, _rTargetDevice, *mpControlData->mpReferenceDevice );
_io_rRect = aRenderer.DrawText( _io_rRect, _rStr, _nStyle, _pVector, _pDisplayText );
}
#ifdef FS_DEBUG
_rTargetDevice.Push( PUSH_LINECOLOR | PUSH_FILLCOLOR );
_rTargetDevice.SetLineColor( COL_LIGHTRED );
_rTargetDevice.SetFillColor();
for ( MetricVector::const_iterator cr = _pVector->begin();
cr != _pVector->end();
++cr
)
{
_rTargetDevice.DrawRect( *cr );
}
_rTargetDevice.Pop();
#endif
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|