LCOV - code coverage report
Current view: top level - vcl/source/control - ctrl.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 138 230 60.0 %
Date: 2014-11-03 Functions: 27 43 62.8 %
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      123000 : void Control::ImplInitControlData()
      37             : {
      38      123000 :     mbHasControlFocus       = false;
      39      123000 :     mpControlData   = new ImplControlData;
      40      123000 : }
      41             : 
      42      103172 : Control::Control( WindowType nType ) :
      43      103172 :     Window( nType )
      44             : {
      45      103172 :     ImplInitControlData();
      46      103172 : }
      47             : 
      48       15874 : Control::Control( vcl::Window* pParent, WinBits nStyle ) :
      49       15874 :     Window( WINDOW_CONTROL )
      50             : {
      51       15874 :     ImplInitControlData();
      52       15874 :     ImplInit( pParent, nStyle, NULL );
      53       15874 : }
      54             : 
      55        3954 : Control::Control( vcl::Window* pParent, const ResId& rResId ) :
      56        3954 :     Window( WINDOW_CONTROL )
      57             : {
      58        3954 :     ImplInitControlData();
      59        3954 :     rResId.SetRT( RSC_CONTROL );
      60        3954 :     WinBits nStyle = ImplInitRes( rResId );
      61        3954 :     ImplInit( pParent, nStyle, NULL );
      62        3954 :     ImplLoadRes( rResId );
      63             : 
      64        3954 :     if ( !(nStyle & WB_HIDE) )
      65        3714 :         Show();
      66        3954 : }
      67             : 
      68      245796 : Control::~Control()
      69             : {
      70      122898 :     delete mpControlData, mpControlData = NULL;
      71      122898 : }
      72             : 
      73       58654 : 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       58654 :                                 TEXT_LAYOUT_TEXTORIGIN_LEFT );
      78       58654 :     StateChanged( StateChangedType::MIRRORING );
      79       58654 :     OutputDevice::EnableRTL(bEnable);
      80       58654 : }
      81             : 
      82         168 : void Control::GetFocus()
      83             : {
      84         168 :     Window::GetFocus();
      85         168 : }
      86             : 
      87          40 : void Control::LoseFocus()
      88             : {
      89          40 :     Window::LoseFocus();
      90          40 : }
      91             : 
      92      200629 : void Control::Resize()
      93             : {
      94      200629 :     ImplClearLayoutData();
      95      200629 :     Window::Resize();
      96      200629 : }
      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        1170 : bool Control::HasLayoutData() const
     109             : {
     110        1170 :     return mpControlData->mpLayoutData != NULL;
     111             : }
     112             : 
     113           0 : ::vcl::ControlLayoutData* Control::GetLayoutData() const
     114             : {
     115           0 :     return mpControlData->mpLayoutData;
     116             : }
     117             : 
     118        5888 : void Control::SetText( const OUString& rStr )
     119             : {
     120        5888 :     ImplClearLayoutData();
     121        5888 :     Window::SetText( rStr );
     122        5888 : }
     123             : 
     124         108 : Rectangle ControlLayoutData::GetCharacterBounds( long nIndex ) const
     125             : {
     126         108 :     return (nIndex >= 0 && nIndex < (long) m_aUnicodeBoundRects.size()) ? m_aUnicodeBoundRects[ nIndex ] : Rectangle();
     127             : }
     128             : 
     129          28 : Rectangle Control::GetCharacterBounds( long nIndex ) const
     130             : {
     131          28 :     if( !HasLayoutData() )
     132           2 :         FillLayoutData();
     133          28 :     return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->GetCharacterBounds( nIndex ) : Rectangle();
     134             : }
     135             : 
     136         112 : long ControlLayoutData::GetIndexForPoint( const Point& rPoint ) const
     137             : {
     138         112 :     long nIndex = -1;
     139        1122 :     for( long i = m_aUnicodeBoundRects.size()-1; i >= 0; i-- )
     140             :     {
     141        1118 :         Point aTopLeft = m_aUnicodeBoundRects[i].TopLeft();
     142        1118 :         Point aBottomRight = m_aUnicodeBoundRects[i].BottomRight();
     143        2452 :         if (rPoint.X() >= aTopLeft.X() && rPoint.Y() >= aTopLeft.Y() &&
     144        1334 :             rPoint.X() <= aBottomRight.X() && rPoint.Y() <= aBottomRight.Y())
     145             :         {
     146         108 :             nIndex = i;
     147         108 :             break;
     148             :         }
     149             :     }
     150         112 :     return nIndex;
     151             : }
     152             : 
     153          30 : long Control::GetIndexForPoint( const Point& rPoint ) const
     154             : {
     155          30 :     if( ! HasLayoutData() )
     156           0 :         FillLayoutData();
     157          30 :     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       75991 : bool Control::Notify( NotifyEvent& rNEvt )
     245             : {
     246       75991 :     if ( rNEvt.GetType() == EVENT_GETFOCUS )
     247             :     {
     248         180 :         if ( !mbHasControlFocus )
     249             :         {
     250         168 :             mbHasControlFocus = true;
     251         168 :             StateChanged( StateChangedType::CONTROL_FOCUS );
     252         168 :             if ( ImplCallEventListenersAndHandler( VCLEVENT_CONTROL_GETFOCUS, maGetFocusHdl, this ) )
     253             :                 // been destroyed within the handler
     254           0 :                 return true;
     255             :         }
     256             :     }
     257             :     else
     258             :     {
     259       75811 :         if ( rNEvt.GetType() == EVENT_LOSEFOCUS )
     260             :         {
     261          54 :             vcl::Window* pFocusWin = Application::GetFocusWindow();
     262          54 :             if ( !pFocusWin || !ImplIsWindowOrChild( pFocusWin ) )
     263             :             {
     264          40 :                 mbHasControlFocus = false;
     265          40 :                 StateChanged( StateChangedType::CONTROL_FOCUS );
     266          40 :                 if ( ImplCallEventListenersAndHandler( VCLEVENT_CONTROL_LOSEFOCUS, maLoseFocusHdl, this ) )
     267             :                     // been destroyed within the handler
     268           0 :                     return true;
     269             :             }
     270             :         }
     271             :     }
     272             : 
     273       75991 :     return Window::Notify( rNEvt );
     274             : }
     275             : 
     276     1078732 : void Control::StateChanged( StateChangedType nStateChange )
     277             : {
     278     1078732 :     if( nStateChange == StateChangedType::INITSHOW   ||
     279      914070 :         nStateChange == StateChangedType::VISIBLE    ||
     280      913985 :         nStateChange == StateChangedType::ZOOM       ||
     281      913985 :         nStateChange == StateChangedType::BORDER     ||
     282             :         nStateChange == StateChangedType::CONTROLFONT
     283             :         )
     284             :     {
     285      170742 :         ImplClearLayoutData();
     286             :     }
     287     1078732 :     Window::StateChanged( nStateChange );
     288     1078732 : }
     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        2188 : bool Control::ImplCallEventListenersAndHandler(  sal_uLong nEvent, const Link& rHandler, void* pCaller )
     315             : {
     316        2188 :     ImplDelData aCheckDelete;
     317        2188 :     ImplAddDel( &aCheckDelete );
     318             : 
     319        2188 :     ImplCallEventListeners( nEvent );
     320        2188 :     if ( !aCheckDelete.IsDead() )
     321             :     {
     322        2188 :         rHandler.Call( pCaller );
     323             : 
     324        2188 :         if ( !aCheckDelete.IsDead() )
     325             :         {
     326        2188 :             ImplRemoveDel( &aCheckDelete );
     327        2188 :             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     1103400 : void Control::ImplClearLayoutData() const
     340             : {
     341     1103400 :     delete mpControlData->mpLayoutData, mpControlData->mpLayoutData = NULL;
     342     1103400 : }
     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         832 : ControlLayoutData::~ControlLayoutData()
     372             : {
     373         416 :     if( m_pParent )
     374           0 :         m_pParent->ImplClearLayoutData();
     375         416 : }
     376             : 
     377           6 : Size Control::GetOptimalSize() const
     378             : {
     379          12 :     return Size( GetTextWidth( GetText() ) + 2 * 12,
     380          18 :                  GetTextHeight() + 2 * 6 );
     381             : }
     382             : 
     383         433 : void Control::SetReferenceDevice( OutputDevice* _referenceDevice )
     384             : {
     385         433 :     if ( mpControlData->mpReferenceDevice == _referenceDevice )
     386         501 :         return;
     387             : 
     388         365 :     mpControlData->mpReferenceDevice = _referenceDevice;
     389         365 :     Invalidate();
     390             : }
     391             : 
     392           0 : OutputDevice* Control::GetReferenceDevice() const
     393             : {
     394           0 :     return mpControlData->mpReferenceDevice;
     395             : }
     396             : 
     397           0 : const vcl::Font& Control::GetCanonicalFont( const StyleSettings& _rStyle ) const
     398             : {
     399           0 :     return _rStyle.GetLabelFont();
     400             : }
     401             : 
     402           0 : const Color& Control::GetCanonicalTextColor( const StyleSettings& _rStyle ) const
     403             : {
     404           0 :     return _rStyle.GetLabelTextColor();
     405             : }
     406             : 
     407       41960 : void Control::ImplInitSettings( const bool _bFont, const bool _bForeground )
     408             : {
     409       41960 :     const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
     410             : 
     411       41960 :     if ( _bFont )
     412             :     {
     413       41818 :         Font aFont( GetCanonicalFont( rStyleSettings ) );
     414       41818 :         if ( IsControlFont() )
     415        1675 :             aFont.Merge( GetControlFont() );
     416       41818 :         SetZoomedPointFont( aFont );
     417             :     }
     418             : 
     419       41960 :     if ( _bForeground || _bFont )
     420             :     {
     421       41902 :         Color aColor;
     422       41902 :         if ( IsControlForeground() )
     423         132 :             aColor = GetControlForeground();
     424             :         else
     425       41770 :             aColor = GetCanonicalTextColor( rStyleSettings );
     426       41902 :         SetTextColor( aColor );
     427       41902 :         SetTextFillColor();
     428             :     }
     429       41960 : }
     430             : 
     431       10726 : void Control::DrawControlText( OutputDevice& _rTargetDevice, Rectangle& _io_rRect, const OUString& _rStr,
     432             :     sal_uInt16 _nStyle, MetricVector* _pVector, OUString* _pDisplayText ) const
     433             : {
     434       10726 :     if ( !mpControlData->mpReferenceDevice || ( mpControlData->mpReferenceDevice == &_rTargetDevice ) )
     435             :     {
     436       10459 :         _io_rRect = _rTargetDevice.GetTextRect( _io_rRect, _rStr, _nStyle );
     437       10459 :         _rTargetDevice.DrawText( _io_rRect, _rStr, _nStyle, _pVector, _pDisplayText );
     438             :     }
     439             :     else
     440             :     {
     441         267 :         ControlTextRenderer aRenderer( *this, _rTargetDevice, *mpControlData->mpReferenceDevice );
     442         267 :         _io_rRect = aRenderer.DrawText( _io_rRect, _rStr, _nStyle, _pVector, _pDisplayText );
     443             :     }
     444       10726 : }
     445             : 
     446             : Font
     447         267 : Control::GetUnzoomedControlPointFont() const
     448             : {
     449         267 :     Font aFont( GetCanonicalFont( GetSettings().GetStyleSettings() ) );
     450         267 :     if ( IsControlFont() )
     451         217 :         aFont.Merge( GetControlFont() );
     452         267 :     return aFont;
     453        1233 : }
     454             : 
     455             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10