LCOV - code coverage report
Current view: top level - vcl/source/control - ctrl.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 153 245 62.4 %
Date: 2014-04-11 Functions: 26 42 61.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include <comphelper/processfactory.hxx>
      21             : 
      22             : #include <tools/rc.h>
      23             : #include <vcl/svapp.hxx>
      24             : #include <vcl/event.hxx>
      25             : #include <vcl/ctrl.hxx>
      26             : #include <vcl/decoview.hxx>
      27             : #include <vcl/salnativewidgets.hxx>
      28             : #include <vcl/settings.hxx>
      29             : 
      30             : #include <textlayout.hxx>
      31             : #include <svdata.hxx>
      32             : #include <controldata.hxx>
      33             : 
      34             : using namespace vcl;
      35             : 
      36       30809 : void Control::ImplInitControlData()
      37             : {
      38       30809 :     mbHasControlFocus       = false;
      39       30809 :     mpControlData   = new ImplControlData;
      40       30809 : }
      41             : 
      42       25524 : Control::Control( WindowType nType ) :
      43       25524 :     Window( nType )
      44             : {
      45       25524 :     ImplInitControlData();
      46       25524 : }
      47             : 
      48        5268 : Control::Control( Window* pParent, WinBits nStyle ) :
      49        5268 :     Window( WINDOW_CONTROL )
      50             : {
      51        5268 :     ImplInitControlData();
      52        5268 :     ImplInit( pParent, nStyle, NULL );
      53        5268 : }
      54             : 
      55          17 : Control::Control( Window* pParent, const ResId& rResId ) :
      56          17 :     Window( WINDOW_CONTROL )
      57             : {
      58          17 :     ImplInitControlData();
      59          17 :     rResId.SetRT( RSC_CONTROL );
      60          17 :     WinBits nStyle = ImplInitRes( rResId );
      61          17 :     ImplInit( pParent, nStyle, NULL );
      62          17 :     ImplLoadRes( rResId );
      63             : 
      64          17 :     if ( !(nStyle & WB_HIDE) )
      65          17 :         Show();
      66          17 : }
      67             : 
      68       61246 : Control::~Control()
      69             : {
      70       30623 :     delete mpControlData, mpControlData = NULL;
      71       30623 : }
      72             : 
      73       17781 : void Control::EnableRTL( bool bEnable )
      74             : {
      75             :     // convenience: for controls also switch layout mode
      76             :     SetLayoutMode( bEnable ? TEXT_LAYOUT_BIDI_RTL | TEXT_LAYOUT_TEXTORIGIN_LEFT :
      77       17781 :                                 TEXT_LAYOUT_BIDI_LTR | TEXT_LAYOUT_TEXTORIGIN_LEFT );
      78       17781 :     StateChanged( STATE_CHANGE_MIRRORING );
      79       17781 :     OutputDevice::EnableRTL(bEnable);
      80       17781 : }
      81             : 
      82          17 : void Control::GetFocus()
      83             : {
      84          17 :     Window::GetFocus();
      85          17 : }
      86             : 
      87           8 : void Control::LoseFocus()
      88             : {
      89           8 :     Window::LoseFocus();
      90           8 : }
      91             : 
      92       42179 : void Control::Resize()
      93             : {
      94       42179 :     ImplClearLayoutData();
      95       42179 :     Window::Resize();
      96       42179 : }
      97             : 
      98           0 : void Control::FillLayoutData() const
      99             : {
     100           0 : }
     101             : 
     102           0 : void Control::CreateLayoutData() const
     103             : {
     104             :     DBG_ASSERT( !mpControlData->mpLayoutData, "Control::CreateLayoutData: should be called with non-existent layout data only!" );
     105           0 :     mpControlData->mpLayoutData = new ::vcl::ControlLayoutData();
     106           0 : }
     107             : 
     108         466 : bool Control::HasLayoutData() const
     109             : {
     110         466 :     return mpControlData->mpLayoutData != NULL;
     111             : }
     112             : 
     113           0 : ::vcl::ControlLayoutData* Control::GetLayoutData() const
     114             : {
     115           0 :     return mpControlData->mpLayoutData;
     116             : }
     117             : 
     118         353 : void Control::SetText( const OUString& rStr )
     119             : {
     120         353 :     ImplClearLayoutData();
     121         353 :     Window::SetText( rStr );
     122         353 : }
     123             : 
     124          54 : Rectangle ControlLayoutData::GetCharacterBounds( long nIndex ) const
     125             : {
     126          54 :     return (nIndex >= 0 && nIndex < (long) m_aUnicodeBoundRects.size()) ? m_aUnicodeBoundRects[ nIndex ] : Rectangle();
     127             : }
     128             : 
     129          14 : Rectangle Control::GetCharacterBounds( long nIndex ) const
     130             : {
     131          14 :     if( !HasLayoutData() )
     132           1 :         FillLayoutData();
     133          14 :     return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->GetCharacterBounds( nIndex ) : Rectangle();
     134             : }
     135             : 
     136          56 : long ControlLayoutData::GetIndexForPoint( const Point& rPoint ) const
     137             : {
     138          56 :     long nIndex = -1;
     139         561 :     for( long i = m_aUnicodeBoundRects.size()-1; i >= 0; i-- )
     140             :     {
     141         559 :         Point aTopLeft = m_aUnicodeBoundRects[i].TopLeft();
     142         559 :         Point aBottomRight = m_aUnicodeBoundRects[i].BottomRight();
     143        1226 :         if (rPoint.X() >= aTopLeft.X() && rPoint.Y() >= aTopLeft.Y() &&
     144         667 :             rPoint.X() <= aBottomRight.X() && rPoint.Y() <= aBottomRight.Y())
     145             :         {
     146          54 :             nIndex = i;
     147          54 :             break;
     148             :         }
     149             :     }
     150          56 :     return nIndex;
     151             : }
     152             : 
     153          15 : long Control::GetIndexForPoint( const Point& rPoint ) const
     154             : {
     155          15 :     if( ! HasLayoutData() )
     156           0 :         FillLayoutData();
     157          15 :     return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->GetIndexForPoint( rPoint ) : -1;
     158             : }
     159             : 
     160           0 : long ControlLayoutData::GetLineCount() const
     161             : {
     162           0 :     long nLines = m_aLineIndices.size();
     163           0 :     if( nLines == 0 && !m_aDisplayText.isEmpty() )
     164           0 :         nLines = 1;
     165           0 :     return nLines;
     166             : }
     167             : 
     168           0 : Pair ControlLayoutData::GetLineStartEnd( long nLine ) const
     169             : {
     170           0 :     Pair aPair( -1, -1 );
     171             : 
     172           0 :     int nDisplayLines = m_aLineIndices.size();
     173           0 :     if( nLine >= 0 && nLine < nDisplayLines )
     174             :     {
     175           0 :         aPair.A() = m_aLineIndices[nLine];
     176           0 :         if( nLine+1 < nDisplayLines )
     177           0 :             aPair.B() = m_aLineIndices[nLine+1]-1;
     178             :         else
     179           0 :             aPair.B() = m_aDisplayText.getLength()-1;
     180             :     }
     181           0 :     else if( nLine == 0 && nDisplayLines == 0 && !m_aDisplayText.isEmpty() )
     182             :     {
     183             :         // special case for single line controls so the implementations
     184             :         // in that case do not have to fill in the line indices
     185           0 :         aPair.A() = 0;
     186           0 :         aPair.B() = m_aDisplayText.getLength()-1;
     187             :     }
     188           0 :     return aPair;
     189             : }
     190             : 
     191           0 : Pair Control::GetLineStartEnd( long nLine ) const
     192             : {
     193           0 :     if( !HasLayoutData() )
     194           0 :         FillLayoutData();
     195           0 :     return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->GetLineStartEnd( nLine ) : Pair( -1, -1 );
     196             : }
     197             : 
     198           0 : long ControlLayoutData::ToRelativeLineIndex( long nIndex ) const
     199             : {
     200             :     // is the index sensible at all ?
     201           0 :     if( nIndex >= 0 && nIndex < m_aDisplayText.getLength() )
     202             :     {
     203           0 :         int nDisplayLines = m_aLineIndices.size();
     204             :         // if only 1 line exists, then absolute and relative index are
     205             :         // identical -> do nothing
     206           0 :         if( nDisplayLines > 1 )
     207             :         {
     208             :             int nLine;
     209           0 :             for( nLine = nDisplayLines-1; nLine >= 0; nLine-- )
     210             :             {
     211           0 :                 if( m_aLineIndices[nLine] <= nIndex )
     212             :                 {
     213           0 :                     nIndex -= m_aLineIndices[nLine];
     214           0 :                     break;
     215             :                 }
     216             :             }
     217           0 :             if( nLine < 0 )
     218             :             {
     219             :                 DBG_ASSERT( nLine >= 0, "ToRelativeLineIndex failed" );
     220           0 :                 nIndex = -1;
     221             :             }
     222             :         }
     223             :     }
     224             :     else
     225           0 :         nIndex = -1;
     226             : 
     227           0 :     return nIndex;
     228             : }
     229             : 
     230           0 : long Control::ToRelativeLineIndex( long nIndex ) const
     231             : {
     232           0 :     if( !HasLayoutData() )
     233           0 :         FillLayoutData();
     234           0 :     return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->ToRelativeLineIndex( nIndex ) : -1;
     235             : }
     236             : 
     237           0 : OUString Control::GetDisplayText() const
     238             : {
     239           0 :     if( !HasLayoutData() )
     240           0 :         FillLayoutData();
     241           0 :     return mpControlData->mpLayoutData ? OUString(mpControlData->mpLayoutData->m_aDisplayText) : GetText();
     242             : }
     243             : 
     244        7506 : bool Control::Notify( NotifyEvent& rNEvt )
     245             : {
     246        7506 :     if ( rNEvt.GetType() == EVENT_GETFOCUS )
     247             :     {
     248          19 :         if ( !mbHasControlFocus )
     249             :         {
     250          17 :             mbHasControlFocus = true;
     251          17 :             StateChanged( STATE_CHANGE_CONTROL_FOCUS );
     252          17 :             if ( ImplCallEventListenersAndHandler( VCLEVENT_CONTROL_GETFOCUS, maGetFocusHdl, this ) )
     253             :                 // been destroyed within the handler
     254           0 :                 return true;
     255             :         }
     256             :     }
     257             :     else
     258             :     {
     259        7487 :         if ( rNEvt.GetType() == EVENT_LOSEFOCUS )
     260             :         {
     261          10 :             Window* pFocusWin = Application::GetFocusWindow();
     262          10 :             if ( !pFocusWin || !ImplIsWindowOrChild( pFocusWin ) )
     263             :             {
     264           8 :                 mbHasControlFocus = false;
     265           8 :                 StateChanged( STATE_CHANGE_CONTROL_FOCUS );
     266           8 :                 if ( ImplCallEventListenersAndHandler( VCLEVENT_CONTROL_LOSEFOCUS, maLoseFocusHdl, this ) )
     267             :                     // been destroyed within the handler
     268           0 :                     return true;
     269             :             }
     270             :         }
     271             :     }
     272             : 
     273        7506 :     return Window::Notify( rNEvt );
     274             : }
     275             : 
     276      454201 : void Control::StateChanged( StateChangedType nStateChange )
     277             : {
     278      454201 :     if( nStateChange == STATE_CHANGE_INITSHOW   ||
     279      415007 :         nStateChange == STATE_CHANGE_VISIBLE    ||
     280      414951 :         nStateChange == STATE_CHANGE_ZOOM       ||
     281      414951 :         nStateChange == STATE_CHANGE_BORDER     ||
     282             :         nStateChange == STATE_CHANGE_CONTROLFONT
     283             :         )
     284             :     {
     285       42195 :         ImplClearLayoutData();
     286             :     }
     287      454201 :     Window::StateChanged( nStateChange );
     288      454201 : }
     289             : 
     290           0 : void Control::AppendLayoutData( const Control& rSubControl ) const
     291             : {
     292           0 :     if( !rSubControl.HasLayoutData() )
     293           0 :         rSubControl.FillLayoutData();
     294           0 :     if( !rSubControl.HasLayoutData() || rSubControl.mpControlData->mpLayoutData->m_aDisplayText.isEmpty() )
     295           0 :         return;
     296             : 
     297           0 :     long nCurrentIndex = mpControlData->mpLayoutData->m_aDisplayText.getLength();
     298           0 :     mpControlData->mpLayoutData->m_aDisplayText += rSubControl.mpControlData->mpLayoutData->m_aDisplayText;
     299           0 :     int nLines = rSubControl.mpControlData->mpLayoutData->m_aLineIndices.size();
     300             :     int n;
     301           0 :     mpControlData->mpLayoutData->m_aLineIndices.push_back( nCurrentIndex );
     302           0 :     for( n = 1; n < nLines; n++ )
     303           0 :         mpControlData->mpLayoutData->m_aLineIndices.push_back( rSubControl.mpControlData->mpLayoutData->m_aLineIndices[n] + nCurrentIndex );
     304           0 :     int nRectangles = rSubControl.mpControlData->mpLayoutData->m_aUnicodeBoundRects.size();
     305           0 :         Rectangle aRel = const_cast<Control&>(rSubControl).GetWindowExtentsRelative( const_cast<Control*>(this) );
     306           0 :     for( n = 0; n < nRectangles; n++ )
     307             :     {
     308           0 :         Rectangle aRect = rSubControl.mpControlData->mpLayoutData->m_aUnicodeBoundRects[n];
     309           0 :         aRect.Move( aRel.Left(), aRel.Top() );
     310           0 :         mpControlData->mpLayoutData->m_aUnicodeBoundRects.push_back( aRect );
     311             :     }
     312             : }
     313             : 
     314         627 : bool Control::ImplCallEventListenersAndHandler(  sal_uLong nEvent, const Link& rHandler, void* pCaller )
     315             : {
     316         627 :     ImplDelData aCheckDelete;
     317         627 :     ImplAddDel( &aCheckDelete );
     318             : 
     319         627 :     ImplCallEventListeners( nEvent );
     320         627 :     if ( !aCheckDelete.IsDead() )
     321             :     {
     322         627 :         rHandler.Call( pCaller );
     323             : 
     324         627 :         if ( !aCheckDelete.IsDead() )
     325             :         {
     326         627 :             ImplRemoveDel( &aCheckDelete );
     327         627 :             return false;
     328             :         }
     329             :     }
     330           0 :     return true;
     331             : }
     332             : 
     333           0 : void Control::SetLayoutDataParent( const Control* pParent ) const
     334             : {
     335           0 :     if( HasLayoutData() )
     336           0 :         mpControlData->mpLayoutData->m_pParent = pParent;
     337           0 : }
     338             : 
     339      430417 : void Control::ImplClearLayoutData() const
     340             : {
     341      430417 :     delete mpControlData->mpLayoutData, mpControlData->mpLayoutData = NULL;
     342      430417 : }
     343             : 
     344           0 : void Control::ImplDrawFrame( OutputDevice* pDev, Rectangle& rRect )
     345             : {
     346             :     // use a deco view to draw the frame
     347             :     // However, since there happens a lot of magic there, we need to fake some (style) settings
     348             :     // on the device
     349           0 :     AllSettings aOriginalSettings( pDev->GetSettings() );
     350             : 
     351           0 :     AllSettings aNewSettings( aOriginalSettings );
     352           0 :     StyleSettings aStyle( aNewSettings.GetStyleSettings() );
     353             : 
     354             :     // The *only known* clients of the Draw methods of the various VCL-controls are form controls:
     355             :     // During print preview, and during printing, Draw is called. Thus, drawing always happens with a
     356             :     // mono (colored) border
     357           0 :     aStyle.SetOptions( aStyle.GetOptions() | STYLE_OPTION_MONO );
     358           0 :     aStyle.SetMonoColor( GetSettings().GetStyleSettings().GetMonoColor() );
     359             : 
     360           0 :     aNewSettings.SetStyleSettings( aStyle );
     361             :     // #i67023# do not call data changed listeners for this fake
     362             :     // since they may understandably invalidate on settings changed
     363           0 :     pDev->OutputDevice::SetSettings( aNewSettings );
     364             : 
     365           0 :     DecorationView aDecoView( pDev );
     366           0 :     rRect = aDecoView.DrawFrame( rRect, FRAME_DRAW_WINDOWBORDER );
     367             : 
     368           0 :     pDev->OutputDevice::SetSettings( aOriginalSettings );
     369           0 : }
     370             : 
     371        6289 : void Control::DataChanged( const DataChangedEvent& rDCEvt)
     372             : {
     373             :     // we don't want to loose some style settings for controls created with the
     374             :     // toolkit
     375       15080 :     if ( IsCreatedWithToolkit() &&
     376        8791 :          (rDCEvt.GetType() == DATACHANGED_SETTINGS) &&
     377        2502 :          (rDCEvt.GetFlags() & SETTINGS_STYLE) )
     378             :     {
     379        2196 :         const AllSettings* pOldSettings = rDCEvt.GetOldSettings();
     380             : 
     381        2196 :         AllSettings aSettings = GetSettings();
     382        4392 :         StyleSettings aStyleSettings = aSettings.GetStyleSettings();
     383        2196 :         sal_uLong nNewOptions = aStyleSettings.GetOptions();
     384             : 
     385        2196 :         if ( pOldSettings && !(nNewOptions & STYLE_OPTION_MONO) && ( pOldSettings->GetStyleSettings().GetOptions() & STYLE_OPTION_MONO ) )
     386             :         {
     387           2 :             nNewOptions |= STYLE_OPTION_MONO;
     388           2 :             aStyleSettings.SetOptions( nNewOptions );
     389           2 :             aStyleSettings.SetMonoColor( pOldSettings->GetStyleSettings().GetMonoColor() );
     390           2 :             aSettings.SetStyleSettings( aStyleSettings );
     391           2 :             SetSettings( aSettings );
     392        2196 :         }
     393             :     }
     394        6289 : }
     395             : 
     396         416 : ControlLayoutData::~ControlLayoutData()
     397             : {
     398         208 :     if( m_pParent )
     399           0 :         m_pParent->ImplClearLayoutData();
     400         208 : }
     401             : 
     402           8 : Size Control::GetOptimalSize() const
     403             : {
     404          16 :     return Size( GetTextWidth( GetText() ) + 2 * 12,
     405          24 :                  GetTextHeight() + 2 * 6 );
     406             : }
     407             : 
     408         219 : void Control::SetReferenceDevice( OutputDevice* _referenceDevice )
     409             : {
     410         219 :     if ( mpControlData->mpReferenceDevice == _referenceDevice )
     411         258 :         return;
     412             : 
     413         180 :     mpControlData->mpReferenceDevice = _referenceDevice;
     414         180 :     Invalidate();
     415             : }
     416             : 
     417           0 : OutputDevice* Control::GetReferenceDevice() const
     418             : {
     419           0 :     return mpControlData->mpReferenceDevice;
     420             : }
     421             : 
     422           0 : const Font& Control::GetCanonicalFont( const StyleSettings& _rStyle ) const
     423             : {
     424           0 :     return _rStyle.GetLabelFont();
     425             : }
     426             : 
     427           0 : const Color& Control::GetCanonicalTextColor( const StyleSettings& _rStyle ) const
     428             : {
     429           0 :     return _rStyle.GetLabelTextColor();
     430             : }
     431             : 
     432       10881 : void Control::ImplInitSettings( const bool _bFont, const bool _bForeground )
     433             : {
     434       10881 :     const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
     435             : 
     436       10881 :     if ( _bFont )
     437             :     {
     438       10815 :         Font aFont( GetCanonicalFont( rStyleSettings ) );
     439       10815 :         if ( IsControlFont() )
     440        1142 :             aFont.Merge( GetControlFont() );
     441       10815 :         SetZoomedPointFont( aFont );
     442             :     }
     443             : 
     444       10881 :     if ( _bForeground || _bFont )
     445             :     {
     446       10854 :         Color aColor;
     447       10854 :         if ( IsControlForeground() )
     448          69 :             aColor = GetControlForeground();
     449             :         else
     450       10785 :             aColor = GetCanonicalTextColor( rStyleSettings );
     451       10854 :         SetTextColor( aColor );
     452       10854 :         SetTextFillColor();
     453             :     }
     454       10881 : }
     455             : 
     456        1185 : void Control::DrawControlText( OutputDevice& _rTargetDevice, Rectangle& _io_rRect, const OUString& _rStr,
     457             :     sal_uInt16 _nStyle, MetricVector* _pVector, OUString* _pDisplayText ) const
     458             : {
     459             : #ifdef FS_DEBUG
     460             :     if ( !_pVector )
     461             :     {
     462             :         static MetricVector aCharRects;
     463             :         static OUString sDisplayText;
     464             :         aCharRects.clear();
     465             :         sDisplayText = OUString();
     466             :         _pVector = &aCharRects;
     467             :         _pDisplayText = &sDisplayText;
     468             :     }
     469             : #endif
     470             : 
     471        1185 :     if ( !mpControlData->mpReferenceDevice || ( mpControlData->mpReferenceDevice == &_rTargetDevice ) )
     472             :     {
     473         932 :         _io_rRect = _rTargetDevice.GetTextRect( _io_rRect, _rStr, _nStyle );
     474         932 :         _rTargetDevice.DrawText( _io_rRect, _rStr, _nStyle, _pVector, _pDisplayText );
     475             :     }
     476             :     else
     477             :     {
     478         253 :         ControlTextRenderer aRenderer( *this, _rTargetDevice, *mpControlData->mpReferenceDevice );
     479         253 :         _io_rRect = aRenderer.DrawText( _io_rRect, _rStr, _nStyle, _pVector, _pDisplayText );
     480             :     }
     481             : 
     482             : #ifdef FS_DEBUG
     483             :     _rTargetDevice.Push( PUSH_LINECOLOR | PUSH_FILLCOLOR );
     484             :     _rTargetDevice.SetLineColor( COL_LIGHTRED );
     485             :     _rTargetDevice.SetFillColor();
     486             :     for (   MetricVector::const_iterator cr = _pVector->begin();
     487             :             cr != _pVector->end();
     488             :             ++cr
     489             :         )
     490             :     {
     491             :         _rTargetDevice.DrawRect( *cr );
     492             :     }
     493             :     _rTargetDevice.Pop();
     494             : #endif
     495        1185 : }
     496             : 
     497             : Font
     498         253 : Control::GetUnzoomedControlPointFont() const
     499             : {
     500         253 :     Font aFont( GetCanonicalFont( GetSettings().GetStyleSettings() ) );
     501         253 :     if ( IsControlFont() )
     502         169 :         aFont.Merge( GetControlFont() );
     503         253 :     return aFont;
     504             : }
     505             : 
     506             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10