LCOV - code coverage report
Current view: top level - vcl/source/control - ctrl.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 143 229 62.4 %
Date: 2015-06-13 12:38:46 Functions: 30 43 69.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       72365 : void Control::ImplInitControlData()
      37             : {
      38       72365 :     mbHasControlFocus       = false;
      39       72365 :     mpControlData   = new ImplControlData;
      40       72365 : }
      41             : 
      42       60803 : Control::Control( WindowType nType ) :
      43       60803 :     Window( nType )
      44             : {
      45       60803 :     ImplInitControlData();
      46       60803 : }
      47             : 
      48        8928 : Control::Control( vcl::Window* pParent, WinBits nStyle ) :
      49        8928 :     Window( WINDOW_CONTROL )
      50             : {
      51        8928 :     ImplInitControlData();
      52        8928 :     ImplInit( pParent, nStyle, NULL );
      53        8928 : }
      54             : 
      55        2634 : Control::Control( vcl::Window* pParent, const ResId& rResId ) :
      56        2634 :     Window( WINDOW_CONTROL )
      57             : {
      58        2634 :     ImplInitControlData();
      59        2634 :     rResId.SetRT( RSC_CONTROL );
      60        2634 :     WinBits nStyle = ImplInitRes( rResId );
      61        2634 :     ImplInit( pParent, nStyle, NULL );
      62        2634 :     ImplLoadRes( rResId );
      63             : 
      64        2634 :     if ( !(nStyle & WB_HIDE) )
      65        2618 :         Show();
      66        2634 : }
      67             : 
      68      134490 : Control::~Control()
      69             : {
      70       67245 :     disposeOnce();
      71       67245 : }
      72             : 
      73       72260 : void Control::dispose()
      74             : {
      75       72260 :     delete mpControlData, mpControlData = NULL;
      76       72260 :     Window::dispose();
      77       72260 : }
      78             : 
      79       33588 : void Control::EnableRTL( bool bEnable )
      80             : {
      81             :     // convenience: for controls also switch layout mode
      82             :     SetLayoutMode( bEnable ? TEXT_LAYOUT_BIDI_RTL | TEXT_LAYOUT_TEXTORIGIN_LEFT :
      83       33588 :                                 TEXT_LAYOUT_TEXTORIGIN_LEFT );
      84       33588 :     CompatStateChanged( StateChangedType::Mirroring );
      85       33588 :     OutputDevice::EnableRTL(bEnable);
      86       33588 : }
      87             : 
      88          27 : void Control::GetFocus()
      89             : {
      90          27 :     Window::GetFocus();
      91          27 : }
      92             : 
      93          12 : void Control::LoseFocus()
      94             : {
      95          12 :     Window::LoseFocus();
      96          12 : }
      97             : 
      98      112621 : void Control::Resize()
      99             : {
     100      112621 :     ImplClearLayoutData();
     101      112621 :     Window::Resize();
     102      112621 : }
     103             : 
     104           0 : void Control::FillLayoutData() const
     105             : {
     106           0 : }
     107             : 
     108           0 : void Control::CreateLayoutData() const
     109             : {
     110             :     DBG_ASSERT( !mpControlData->mpLayoutData, "Control::CreateLayoutData: should be called with non-existent layout data only!" );
     111           0 :     mpControlData->mpLayoutData = new vcl::ControlLayoutData();
     112           0 : }
     113             : 
     114         865 : bool Control::HasLayoutData() const
     115             : {
     116         865 :     return mpControlData && mpControlData->mpLayoutData != NULL;
     117             : }
     118             : 
     119        5069 : void Control::SetText( const OUString& rStr )
     120             : {
     121        5069 :     ImplClearLayoutData();
     122        5069 :     Window::SetText( rStr );
     123        5069 : }
     124             : 
     125         208 : ControlLayoutData::ControlLayoutData() : m_pParent( NULL )
     126             : {
     127         208 : }
     128             : 
     129          54 : Rectangle ControlLayoutData::GetCharacterBounds( long nIndex ) const
     130             : {
     131          54 :     return (nIndex >= 0 && nIndex < (long) m_aUnicodeBoundRects.size()) ? m_aUnicodeBoundRects[ nIndex ] : Rectangle();
     132             : }
     133             : 
     134          14 : Rectangle Control::GetCharacterBounds( long nIndex ) const
     135             : {
     136          14 :     if( !HasLayoutData() )
     137           1 :         FillLayoutData();
     138          14 :     return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->GetCharacterBounds( nIndex ) : Rectangle();
     139             : }
     140             : 
     141          56 : long ControlLayoutData::GetIndexForPoint( const Point& rPoint ) const
     142             : {
     143          56 :     long nIndex = -1;
     144         561 :     for( long i = m_aUnicodeBoundRects.size()-1; i >= 0; i-- )
     145             :     {
     146         559 :         Point aTopLeft = m_aUnicodeBoundRects[i].TopLeft();
     147         559 :         Point aBottomRight = m_aUnicodeBoundRects[i].BottomRight();
     148        1226 :         if (rPoint.X() >= aTopLeft.X() && rPoint.Y() >= aTopLeft.Y() &&
     149         667 :             rPoint.X() <= aBottomRight.X() && rPoint.Y() <= aBottomRight.Y())
     150             :         {
     151          54 :             nIndex = i;
     152          54 :             break;
     153             :         }
     154             :     }
     155          56 :     return nIndex;
     156             : }
     157             : 
     158          15 : long Control::GetIndexForPoint( const Point& rPoint ) const
     159             : {
     160          15 :     if( ! HasLayoutData() )
     161           0 :         FillLayoutData();
     162          15 :     return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->GetIndexForPoint( rPoint ) : -1;
     163             : }
     164             : 
     165           0 : long ControlLayoutData::GetLineCount() const
     166             : {
     167           0 :     long nLines = m_aLineIndices.size();
     168           0 :     if( nLines == 0 && !m_aDisplayText.isEmpty() )
     169           0 :         nLines = 1;
     170           0 :     return nLines;
     171             : }
     172             : 
     173           0 : Pair ControlLayoutData::GetLineStartEnd( long nLine ) const
     174             : {
     175           0 :     Pair aPair( -1, -1 );
     176             : 
     177           0 :     int nDisplayLines = m_aLineIndices.size();
     178           0 :     if( nLine >= 0 && nLine < nDisplayLines )
     179             :     {
     180           0 :         aPair.A() = m_aLineIndices[nLine];
     181           0 :         if( nLine+1 < nDisplayLines )
     182           0 :             aPair.B() = m_aLineIndices[nLine+1]-1;
     183             :         else
     184           0 :             aPair.B() = m_aDisplayText.getLength()-1;
     185             :     }
     186           0 :     else if( nLine == 0 && nDisplayLines == 0 && !m_aDisplayText.isEmpty() )
     187             :     {
     188             :         // special case for single line controls so the implementations
     189             :         // in that case do not have to fill in the line indices
     190           0 :         aPair.A() = 0;
     191           0 :         aPair.B() = m_aDisplayText.getLength()-1;
     192             :     }
     193           0 :     return aPair;
     194             : }
     195             : 
     196           0 : Pair Control::GetLineStartEnd( long nLine ) const
     197             : {
     198           0 :     if( !HasLayoutData() )
     199           0 :         FillLayoutData();
     200           0 :     return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->GetLineStartEnd( nLine ) : Pair( -1, -1 );
     201             : }
     202             : 
     203           0 : long ControlLayoutData::ToRelativeLineIndex( long nIndex ) const
     204             : {
     205             :     // is the index sensible at all ?
     206           0 :     if( nIndex >= 0 && nIndex < m_aDisplayText.getLength() )
     207             :     {
     208           0 :         int nDisplayLines = m_aLineIndices.size();
     209             :         // if only 1 line exists, then absolute and relative index are
     210             :         // identical -> do nothing
     211           0 :         if( nDisplayLines > 1 )
     212             :         {
     213             :             int nLine;
     214           0 :             for( nLine = nDisplayLines-1; nLine >= 0; nLine-- )
     215             :             {
     216           0 :                 if( m_aLineIndices[nLine] <= nIndex )
     217             :                 {
     218           0 :                     nIndex -= m_aLineIndices[nLine];
     219           0 :                     break;
     220             :                 }
     221             :             }
     222           0 :             if( nLine < 0 )
     223             :             {
     224             :                 DBG_ASSERT( nLine >= 0, "ToRelativeLineIndex failed" );
     225           0 :                 nIndex = -1;
     226             :             }
     227             :         }
     228             :     }
     229             :     else
     230           0 :         nIndex = -1;
     231             : 
     232           0 :     return nIndex;
     233             : }
     234             : 
     235           0 : long Control::ToRelativeLineIndex( long nIndex ) const
     236             : {
     237           0 :     if( !HasLayoutData() )
     238           0 :         FillLayoutData();
     239           0 :     return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->ToRelativeLineIndex( nIndex ) : -1;
     240             : }
     241             : 
     242           0 : OUString Control::GetDisplayText() const
     243             : {
     244           0 :     if( !HasLayoutData() )
     245           0 :         FillLayoutData();
     246           0 :     return mpControlData->mpLayoutData ? OUString(mpControlData->mpLayoutData->m_aDisplayText) : GetText();
     247             : }
     248             : 
     249       47977 : bool Control::Notify( NotifyEvent& rNEvt )
     250             : {
     251             :     // tdf#91081 if control is not valid, skip the emission - chaining to the parent
     252       47977 :     if (mpControlData)
     253             :     {
     254       47977 :         if ( rNEvt.GetType() == MouseNotifyEvent::GETFOCUS )
     255             :         {
     256          29 :             if ( !mbHasControlFocus )
     257             :             {
     258          27 :                 mbHasControlFocus = true;
     259          27 :                 CompatStateChanged( StateChangedType::ControlFocus );
     260          27 :                 if ( ImplCallEventListenersAndHandler( VCLEVENT_CONTROL_GETFOCUS, maGetFocusHdl, this ) )
     261             :                     // been destroyed within the handler
     262           0 :                     return true;
     263             :             }
     264             :         }
     265             :         else
     266             :         {
     267       47948 :             if ( rNEvt.GetType() == MouseNotifyEvent::LOSEFOCUS )
     268             :             {
     269          14 :                 vcl::Window* pFocusWin = Application::GetFocusWindow();
     270          14 :                 if ( !pFocusWin || !ImplIsWindowOrChild( pFocusWin ) )
     271             :                 {
     272          12 :                     mbHasControlFocus = false;
     273          12 :                     CompatStateChanged( StateChangedType::ControlFocus );
     274          12 :                     if ( ImplCallEventListenersAndHandler( VCLEVENT_CONTROL_LOSEFOCUS, maLoseFocusHdl, this ) )
     275             :                         // been destroyed within the handler
     276           0 :                         return true;
     277             :                 }
     278             :             }
     279             :         }
     280             :     }
     281       47977 :     return Window::Notify( rNEvt );
     282             : }
     283             : 
     284      670115 : void Control::StateChanged( StateChangedType nStateChange )
     285             : {
     286      670115 :     if( nStateChange == StateChangedType::InitShow   ||
     287      573563 :         nStateChange == StateChangedType::Visible    ||
     288      573509 :         nStateChange == StateChangedType::Zoom       ||
     289      573509 :         nStateChange == StateChangedType::Border     ||
     290             :         nStateChange == StateChangedType::ControlFont
     291             :         )
     292             :     {
     293       99576 :         ImplClearLayoutData();
     294             :     }
     295      670115 :     Window::StateChanged( nStateChange );
     296      670115 : }
     297             : 
     298           0 : void Control::AppendLayoutData( const Control& rSubControl ) const
     299             : {
     300           0 :     if( !rSubControl.HasLayoutData() )
     301           0 :         rSubControl.FillLayoutData();
     302           0 :     if( !rSubControl.HasLayoutData() || rSubControl.mpControlData->mpLayoutData->m_aDisplayText.isEmpty() )
     303           0 :         return;
     304             : 
     305           0 :     long nCurrentIndex = mpControlData->mpLayoutData->m_aDisplayText.getLength();
     306           0 :     mpControlData->mpLayoutData->m_aDisplayText += rSubControl.mpControlData->mpLayoutData->m_aDisplayText;
     307           0 :     int nLines = rSubControl.mpControlData->mpLayoutData->m_aLineIndices.size();
     308             :     int n;
     309           0 :     mpControlData->mpLayoutData->m_aLineIndices.push_back( nCurrentIndex );
     310           0 :     for( n = 1; n < nLines; n++ )
     311           0 :         mpControlData->mpLayoutData->m_aLineIndices.push_back( rSubControl.mpControlData->mpLayoutData->m_aLineIndices[n] + nCurrentIndex );
     312           0 :     int nRectangles = rSubControl.mpControlData->mpLayoutData->m_aUnicodeBoundRects.size();
     313           0 :         Rectangle aRel = const_cast<Control&>(rSubControl).GetWindowExtentsRelative( const_cast<Control*>(this) );
     314           0 :     for( n = 0; n < nRectangles; n++ )
     315             :     {
     316           0 :         Rectangle aRect = rSubControl.mpControlData->mpLayoutData->m_aUnicodeBoundRects[n];
     317           0 :         aRect.Move( aRel.Left(), aRel.Top() );
     318           0 :         mpControlData->mpLayoutData->m_aUnicodeBoundRects.push_back( aRect );
     319             :     }
     320             : }
     321             : 
     322        1268 : bool Control::ImplCallEventListenersAndHandler( sal_uLong nEvent, const Link<>& rHandler, void* pCaller )
     323             : {
     324        1268 :     VclPtr<Control> xThis(this);
     325             : 
     326        1268 :     CallEventListeners( nEvent );
     327             : 
     328        1268 :     if ( !xThis->IsDisposed() )
     329             :     {
     330        1268 :         rHandler.Call( pCaller );
     331             : 
     332        1268 :         if ( !xThis->IsDisposed() )
     333        1268 :             return false;
     334             :     }
     335           0 :     return true;
     336             : }
     337             : 
     338           0 : void Control::SetLayoutDataParent( const Control* pParent ) const
     339             : {
     340           0 :     if( HasLayoutData() )
     341           0 :         mpControlData->mpLayoutData->m_pParent = pParent;
     342           0 : }
     343             : 
     344      669422 : void Control::ImplClearLayoutData() const
     345             : {
     346      669422 :     if (mpControlData)
     347             :     {
     348      669422 :         delete mpControlData->mpLayoutData;
     349      669422 :         mpControlData->mpLayoutData = NULL;
     350             :     }
     351      669422 : }
     352             : 
     353           0 : void Control::ImplDrawFrame( OutputDevice* pDev, Rectangle& rRect )
     354             : {
     355             :     // use a deco view to draw the frame
     356             :     // However, since there happens a lot of magic there, we need to fake some (style) settings
     357             :     // on the device
     358           0 :     AllSettings aOriginalSettings( pDev->GetSettings() );
     359             : 
     360           0 :     AllSettings aNewSettings( aOriginalSettings );
     361           0 :     StyleSettings aStyle( aNewSettings.GetStyleSettings() );
     362             : 
     363             :     // The *only known* clients of the Draw methods of the various VCL-controls are form controls:
     364             :     // During print preview, and during printing, Draw is called. Thus, drawing always happens with a
     365             :     // mono (colored) border
     366           0 :     aStyle.SetOptions( aStyle.GetOptions() | StyleSettingsOptions::Mono );
     367           0 :     aStyle.SetMonoColor( GetSettings().GetStyleSettings().GetMonoColor() );
     368             : 
     369           0 :     aNewSettings.SetStyleSettings( aStyle );
     370             :     // #i67023# do not call data changed listeners for this fake
     371             :     // since they may understandably invalidate on settings changed
     372           0 :     pDev->OutputDevice::SetSettings( aNewSettings );
     373             : 
     374           0 :     DecorationView aDecoView( pDev );
     375           0 :     rRect = aDecoView.DrawFrame( rRect, DrawFrameStyle::Out, DrawFrameFlags::WindowBorder );
     376             : 
     377           0 :     pDev->OutputDevice::SetSettings( aOriginalSettings );
     378           0 : }
     379             : 
     380         416 : ControlLayoutData::~ControlLayoutData()
     381             : {
     382         208 :     if( m_pParent )
     383           0 :         m_pParent->ImplClearLayoutData();
     384         208 : }
     385             : 
     386           3 : Size Control::GetOptimalSize() const
     387             : {
     388           6 :     return Size( GetTextWidth( GetText() ) + 2 * 12,
     389           9 :                  GetTextHeight() + 2 * 6 );
     390             : }
     391             : 
     392         228 : void Control::SetReferenceDevice( OutputDevice* _referenceDevice )
     393             : {
     394         228 :     if ( mpControlData->mpReferenceDevice == _referenceDevice )
     395         266 :         return;
     396             : 
     397         190 :     mpControlData->mpReferenceDevice = _referenceDevice;
     398         190 :     Invalidate();
     399             : }
     400             : 
     401           0 : OutputDevice* Control::GetReferenceDevice() const
     402             : {
     403           0 :     return mpControlData->mpReferenceDevice;
     404             : }
     405             : 
     406        6061 : const vcl::Font& Control::GetCanonicalFont( const StyleSettings& _rStyle ) const
     407             : {
     408        6061 :     return _rStyle.GetLabelFont();
     409             : }
     410             : 
     411        6061 : const Color& Control::GetCanonicalTextColor( const StyleSettings& _rStyle ) const
     412             : {
     413        6061 :     return _rStyle.GetLabelTextColor();
     414             : }
     415             : 
     416       56624 : void Control::ApplySettings(vcl::RenderContext& rRenderContext)
     417             : {
     418       56624 :     const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings();
     419             : 
     420       56624 :     vcl::Font rFont(GetCanonicalFont(rStyleSettings));
     421       56624 :     ApplyControlFont(rRenderContext, rFont);
     422             : 
     423       56624 :     ApplyControlForeground(rRenderContext, GetCanonicalTextColor(rStyleSettings));
     424             : 
     425       56624 :     rRenderContext.SetTextFillColor();
     426       56624 : }
     427             : 
     428       25785 : void Control::ImplInitSettings(const bool, const bool)
     429             : {
     430       25785 :     ApplySettings(*this);
     431       25785 : }
     432             : 
     433        4153 : void Control::DrawControlText( OutputDevice& _rTargetDevice, Rectangle& _io_rRect, const OUString& _rStr,
     434             :     DrawTextFlags _nStyle, MetricVector* _pVector, OUString* _pDisplayText ) const
     435             : {
     436        4153 :     if ( !mpControlData->mpReferenceDevice || ( mpControlData->mpReferenceDevice == &_rTargetDevice ) )
     437             :     {
     438        3697 :         _io_rRect = _rTargetDevice.GetTextRect( _io_rRect, _rStr, _nStyle );
     439        3697 :         _rTargetDevice.DrawText( _io_rRect, _rStr, _nStyle, _pVector, _pDisplayText );
     440             :     }
     441             :     else
     442             :     {
     443         456 :         ControlTextRenderer aRenderer( *this, _rTargetDevice, *mpControlData->mpReferenceDevice );
     444         456 :         _io_rRect = aRenderer.DrawText( _io_rRect, _rStr, _nStyle, _pVector, _pDisplayText );
     445             :     }
     446        4153 : }
     447             : 
     448             : Font
     449         456 : Control::GetUnzoomedControlPointFont() const
     450             : {
     451         456 :     Font aFont(GetCanonicalFont(GetSettings().GetStyleSettings()));
     452         456 :     if (IsControlFont())
     453         274 :         aFont.Merge(GetControlFont());
     454         456 :     return aFont;
     455             : }
     456             : 
     457             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11