LCOV - code coverage report
Current view: top level - editeng/source/accessibility - AccessibleParaManager.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 77 146 52.7 %
Date: 2015-06-13 12:38:46 Functions: 18 31 58.1 %
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             : 
      21             : 
      22             : 
      23             : // Global header
      24             : 
      25             : 
      26             : #include <com/sun/star/uno/Any.hxx>
      27             : #include <com/sun/star/uno/Reference.hxx>
      28             : #include <cppuhelper/weakref.hxx>
      29             : #include <com/sun/star/accessibility/XAccessible.hpp>
      30             : #include <com/sun/star/accessibility/AccessibleStateType.hpp>
      31             : 
      32             : 
      33             : 
      34             : // Project-local header
      35             : 
      36             : 
      37             : 
      38             : #include <editeng/unoedhlp.hxx>
      39             : #include <editeng/unopracc.hxx>
      40             : #include <editeng/unoedsrc.hxx>
      41             : #include "editeng/AccessibleParaManager.hxx"
      42             : #include "editeng/AccessibleEditableTextPara.hxx"
      43             : 
      44             : 
      45             : using namespace ::com::sun::star;
      46             : using namespace ::com::sun::star::accessibility;
      47             : 
      48             : 
      49             : 
      50             : namespace accessibility
      51             : {
      52          31 :     AccessibleParaManager::AccessibleParaManager() :
      53             :         maChildren(1),
      54             :         maEEOffset( 0, 0 ),
      55             :         mnFocusedChild( -1 ),
      56          31 :         mbActive( false )
      57             :     {
      58          31 :     }
      59             : 
      60          23 :     AccessibleParaManager::~AccessibleParaManager()
      61             :     {
      62             :         // owner is responsible for possible child defuncs
      63          23 :     }
      64             : 
      65           7 :     void AccessibleParaManager::SetAdditionalChildStates( const VectorOfStates& rChildStates )
      66             :     {
      67           7 :         maChildStates = rChildStates;
      68           7 :     }
      69             : 
      70          64 :     void AccessibleParaManager::SetNum( sal_Int32 nNumParas )
      71             :     {
      72          64 :         if( (size_t)nNumParas < maChildren.size() )
      73          33 :             Release( nNumParas, maChildren.size() );
      74             : 
      75          64 :         maChildren.resize( nNumParas );
      76             : 
      77          64 :         if( mnFocusedChild >= nNumParas )
      78           0 :             mnFocusedChild = -1;
      79          64 :     }
      80             : 
      81          17 :     sal_Int32 AccessibleParaManager::GetNum() const
      82             :     {
      83          17 :         size_t nSize = maChildren.size();
      84          17 :         if (nSize > SAL_MAX_INT32)
      85             :         {
      86             :             SAL_WARN( "editeng", "AccessibleParaManager::GetNum - overflow " << nSize);
      87           0 :             return SAL_MAX_INT32;
      88             :         }
      89          17 :         return static_cast<sal_Int32>(nSize);
      90             :     }
      91             : 
      92         111 :     AccessibleParaManager::VectorOfChildren::iterator AccessibleParaManager::begin()
      93             :     {
      94         111 :         return maChildren.begin();
      95             :     }
      96             : 
      97          87 :     AccessibleParaManager::VectorOfChildren::iterator AccessibleParaManager::end()
      98             :     {
      99          87 :         return maChildren.end();
     100             :     }
     101             : 
     102           0 :     AccessibleParaManager::VectorOfChildren::const_iterator AccessibleParaManager::begin() const
     103             :     {
     104           0 :         return maChildren.begin();
     105             :     }
     106             : 
     107           0 :     AccessibleParaManager::VectorOfChildren::const_iterator AccessibleParaManager::end() const
     108             :     {
     109           0 :         return maChildren.end();
     110             :     }
     111             : 
     112           0 :     void AccessibleParaManager::FireEvent( sal_Int32 nPara,
     113             :                                            const sal_Int16 nEventId,
     114             :                                            const uno::Any& rNewValue,
     115             :                                            const uno::Any& rOldValue ) const
     116             :     {
     117             :         DBG_ASSERT( 0 <= nPara && maChildren.size() > static_cast<size_t>(nPara),
     118             :                 "AccessibleParaManager::FireEvent: invalid index" );
     119             : 
     120           0 :         if( 0 <= nPara && maChildren.size() > static_cast<size_t>(nPara) )
     121             :         {
     122           0 :             WeakPara::HardRefType maChild( GetChild( nPara ).first.get() );
     123           0 :             if( maChild.is() )
     124           0 :                 maChild->FireEvent( nEventId, rNewValue, rOldValue );
     125             :         }
     126           0 :     }
     127             : 
     128          71 :     bool AccessibleParaManager::IsReferencable( WeakPara::HardRefType aChild )
     129             :     {
     130          71 :         return aChild.is();
     131             :     }
     132             : 
     133          38 :     bool AccessibleParaManager::IsReferencable( sal_Int32 nChild ) const
     134             :     {
     135             :         DBG_ASSERT( 0 <= nChild && maChildren.size() > static_cast<size_t>(nChild),
     136             :                 "AccessibleParaManager::IsReferencable: invalid index" );
     137             : 
     138          38 :         if( 0 <= nChild && maChildren.size() > static_cast<size_t>(nChild) )
     139             :         {
     140             :             // retrieve hard reference from weak one
     141          38 :             return IsReferencable( GetChild( nChild ).first.get() );
     142             :         }
     143             :         else
     144             :         {
     145           0 :             return false;
     146             :         }
     147             :     }
     148             : 
     149         169 :     AccessibleParaManager::WeakChild AccessibleParaManager::GetChild( sal_Int32 nParagraphIndex ) const
     150             :     {
     151             :         DBG_ASSERT( 0 <= nParagraphIndex && maChildren.size() > static_cast<size_t>(nParagraphIndex),
     152             :                 "AccessibleParaManager::GetChild: invalid index" );
     153             : 
     154         169 :         if( 0 <= nParagraphIndex && maChildren.size() > static_cast<size_t>(nParagraphIndex) )
     155             :         {
     156         169 :             return maChildren[ nParagraphIndex ];
     157             :         }
     158             :         else
     159             :         {
     160           0 :             return WeakChild();
     161             :         }
     162             :     }
     163             : 
     164          38 :     AccessibleParaManager::Child AccessibleParaManager::CreateChild( sal_Int32                              nChild,
     165             :                                                                      const uno::Reference< XAccessible >&   xFrontEnd,
     166             :                                                                      SvxEditSourceAdapter&                  rEditSource,
     167             :                                                                      sal_Int32                              nParagraphIndex )
     168             :     {
     169             :         DBG_ASSERT( 0 <= nParagraphIndex && maChildren.size() > static_cast<size_t>(nParagraphIndex),
     170             :                 "AccessibleParaManager::CreateChild: invalid index" );
     171             : 
     172          38 :         if( 0 <= nParagraphIndex && maChildren.size() > static_cast<size_t>(nParagraphIndex) )
     173             :         {
     174             :             // retrieve hard reference from weak one
     175          38 :             WeakPara::HardRefType aChild( GetChild( nParagraphIndex ).first.get() );
     176             : 
     177          38 :             if( !IsReferencable( nParagraphIndex ) )
     178             :             {
     179             :                 // there is no hard reference available, create object then
     180             :                 // #i27138#
     181          27 :                 AccessibleEditableTextPara* pChild = new AccessibleEditableTextPara( xFrontEnd, this );
     182          27 :                 uno::Reference< XAccessible > xChild( static_cast< ::cppu::OWeakObject* > (pChild), uno::UNO_QUERY );
     183             : 
     184          27 :                 if( !xChild.is() )
     185           0 :                     throw uno::RuntimeException("Child creation failed", xFrontEnd);
     186             : 
     187          27 :                 aChild = WeakPara::HardRefType( xChild, pChild );
     188             : 
     189          27 :                 InitChild( *aChild, rEditSource, nChild, nParagraphIndex );
     190             : 
     191          27 :                 maChildren[ nParagraphIndex ] = WeakChild( aChild, pChild->getBounds() );
     192             :             }
     193             : 
     194          38 :             return Child( aChild.getRef(), GetChild( nParagraphIndex ).second );
     195             :         }
     196             :         else
     197             :         {
     198           0 :             return Child();
     199             :         }
     200             :     }
     201             : 
     202           0 :     void AccessibleParaManager::SetEEOffset( const Point& rOffset )
     203             :     {
     204           0 :         maEEOffset = rOffset;
     205             : 
     206           0 :         MemFunAdapter< const Point& > aAdapter( &::accessibility::AccessibleEditableTextPara::SetEEOffset, rOffset );
     207           0 :         ::std::for_each( begin(), end(), aAdapter );
     208           0 :     }
     209             : 
     210           0 :     void AccessibleParaManager::SetActive( bool bActive )
     211             :     {
     212           0 :         mbActive = bActive;
     213             : 
     214           0 :         if( bActive )
     215             :         {
     216           0 :             SetState( AccessibleStateType::ACTIVE );
     217           0 :             SetState( AccessibleStateType::EDITABLE );
     218             :         }
     219             :         else
     220             :         {
     221           0 :             UnSetState( AccessibleStateType::ACTIVE );
     222           0 :             UnSetState( AccessibleStateType::EDITABLE );
     223             :         }
     224           0 :     }
     225             : 
     226           0 :     void AccessibleParaManager::SetFocus( sal_Int32 nChild )
     227             :     {
     228           0 :         if( mnFocusedChild != -1 )
     229           0 :             UnSetState( mnFocusedChild, AccessibleStateType::FOCUSED );
     230             : 
     231           0 :         mnFocusedChild = nChild;
     232             : 
     233           0 :         if( mnFocusedChild != -1 )
     234           0 :             SetState( mnFocusedChild, AccessibleStateType::FOCUSED );
     235           0 :     }
     236             : 
     237          27 :     void AccessibleParaManager::InitChild( AccessibleEditableTextPara&  rChild,
     238             :                                            SvxEditSourceAdapter&        rEditSource,
     239             :                                            sal_Int32                    nChild,
     240             :                                            sal_Int32                    nParagraphIndex ) const
     241             :     {
     242          27 :         rChild.SetEditSource( &rEditSource );
     243          27 :         rChild.SetIndexInParent( nChild );
     244          27 :         rChild.SetParagraphIndex( nParagraphIndex );
     245             : 
     246          27 :         rChild.SetEEOffset( maEEOffset );
     247             : 
     248          27 :         if( mbActive )
     249             :         {
     250           0 :             rChild.SetState( AccessibleStateType::ACTIVE );
     251           0 :             rChild.SetState( AccessibleStateType::EDITABLE );
     252             :         }
     253             : 
     254          27 :         if( mnFocusedChild == nParagraphIndex )
     255           0 :             rChild.SetState( AccessibleStateType::FOCUSED );
     256             : 
     257             :         // add states passed from outside
     258          33 :         for( VectorOfStates::const_iterator aIt = maChildStates.begin(), aEnd = maChildStates.end(); aIt != aEnd; ++aIt )
     259           6 :             rChild.SetState( *aIt );
     260          27 :     }
     261             : 
     262           0 :     void AccessibleParaManager::SetState( sal_Int32 nChild, const sal_Int16 nStateId )
     263             :     {
     264             :         MemFunAdapter< const sal_Int16 > aFunc( &AccessibleEditableTextPara::SetState,
     265           0 :                                                 nStateId );
     266           0 :         aFunc( GetChild(nChild) );
     267           0 :     }
     268             : 
     269           0 :     void AccessibleParaManager::SetState( const sal_Int16 nStateId )
     270             :     {
     271             :         ::std::for_each( begin(), end(),
     272             :                          MemFunAdapter< const sal_Int16 >( &AccessibleEditableTextPara::SetState,
     273           0 :                                                            nStateId ) );
     274           0 :     }
     275             : 
     276           0 :     void AccessibleParaManager::UnSetState( sal_Int32 nChild, const sal_Int16 nStateId )
     277             :     {
     278             :         MemFunAdapter< const sal_Int16 > aFunc( &AccessibleEditableTextPara::UnSetState,
     279           0 :                                                 nStateId );
     280           0 :         aFunc( GetChild(nChild) );
     281           0 :     }
     282             : 
     283           0 :     void AccessibleParaManager::UnSetState( const sal_Int16 nStateId )
     284             :     {
     285             :         ::std::for_each( begin(), end(),
     286             :                          MemFunAdapter< const sal_Int16 >( &AccessibleEditableTextPara::UnSetState,
     287           0 :                                                            nStateId ) );
     288           0 :     }
     289             : 
     290             :     // not generic yet, no arguments...
     291             :     class AccessibleParaManager_DisposeChildren : public ::std::unary_function< ::accessibility::AccessibleEditableTextPara&, void >
     292             :     {
     293             :     public:
     294          62 :         AccessibleParaManager_DisposeChildren() {}
     295          20 :         void operator()( ::accessibility::AccessibleEditableTextPara& rPara )
     296             :         {
     297          20 :             rPara.Dispose();
     298          20 :         }
     299             :     };
     300             : 
     301          62 :     void AccessibleParaManager::Dispose()
     302             :     {
     303          62 :         AccessibleParaManager_DisposeChildren aFunctor;
     304             : 
     305             :         ::std::for_each( begin(), end(),
     306          62 :                          WeakChildAdapter< AccessibleParaManager_DisposeChildren > (aFunctor) );
     307          62 :     }
     308             : 
     309             :     // not generic yet, too many method arguments...
     310             :     class StateChangeEvent : public ::std::unary_function< ::accessibility::AccessibleEditableTextPara&, void >
     311             :     {
     312             :     public:
     313             :         typedef void return_type;
     314           0 :         StateChangeEvent( const sal_Int16 nEventId,
     315             :                           const uno::Any& rNewValue,
     316             :                           const uno::Any& rOldValue ) :
     317             :             mnEventId( nEventId ),
     318             :             mrNewValue( rNewValue ),
     319           0 :             mrOldValue( rOldValue ) {}
     320           0 :         void operator()( ::accessibility::AccessibleEditableTextPara& rPara )
     321             :         {
     322           0 :             rPara.FireEvent( mnEventId, mrNewValue, mrOldValue );
     323           0 :         }
     324             : 
     325             :     private:
     326             :         const sal_Int16 mnEventId;
     327             :         const uno::Any& mrNewValue;
     328             :         const uno::Any& mrOldValue;
     329             :     };
     330             : 
     331           0 :     void AccessibleParaManager::FireEvent( sal_Int32 nStartPara,
     332             :                                            sal_Int32 nEndPara,
     333             :                                            const sal_Int16 nEventId,
     334             :                                            const uno::Any& rNewValue,
     335             :                                            const uno::Any& rOldValue ) const
     336             :     {
     337             :         DBG_ASSERT( 0 <= nStartPara && 0 <= nEndPara &&
     338             :                     maChildren.size() > static_cast<size_t>(nStartPara) &&
     339             :                     maChildren.size() >= static_cast<size_t>(nEndPara) &&
     340             :                     nEndPara >= nStartPara, "AccessibleParaManager::FireEvent: invalid index" );
     341             : 
     342             : 
     343             : 
     344           0 :         if( 0 <= nStartPara && 0 <= nEndPara &&
     345           0 :                 maChildren.size() > static_cast<size_t>(nStartPara) &&
     346           0 :                 maChildren.size() >= static_cast<size_t>(nEndPara) &&
     347             :                 nEndPara >= nStartPara )
     348             :         {
     349           0 :             VectorOfChildren::const_iterator front = maChildren.begin();
     350           0 :             VectorOfChildren::const_iterator back = front;
     351             : 
     352           0 :             ::std::advance( front, nStartPara );
     353           0 :             ::std::advance( back, nEndPara );
     354             : 
     355           0 :             StateChangeEvent aFunctor( nEventId, rNewValue, rOldValue );
     356             : 
     357           0 :             ::std::for_each( front, back, AccessibleParaManager::WeakChildAdapter< StateChangeEvent >( aFunctor ) );
     358             :         }
     359           0 :     }
     360             : 
     361             :     class ReleaseChild : public ::std::unary_function< const AccessibleParaManager::WeakChild&, AccessibleParaManager::WeakChild >
     362             :     {
     363             :     public:
     364          33 :         AccessibleParaManager::WeakChild operator()( const AccessibleParaManager::WeakChild& rPara )
     365             :         {
     366          33 :             AccessibleParaManager::ShutdownPara( rPara );
     367             : 
     368             :             // clear reference
     369          33 :             return AccessibleParaManager::WeakChild();
     370             :         }
     371             :     };
     372             : 
     373          33 :     void AccessibleParaManager::Release( sal_Int32 nStartPara, sal_Int32 nEndPara )
     374             :     {
     375             :         DBG_ASSERT( 0 <= nStartPara && 0 <= nEndPara &&
     376             :                     maChildren.size() > static_cast<size_t>(nStartPara) &&
     377             :                     maChildren.size() >= static_cast<size_t>(nEndPara),
     378             :                     "AccessibleParaManager::Release: invalid index" );
     379             : 
     380          99 :         if( 0 <= nStartPara && 0 <= nEndPara &&
     381          99 :                 maChildren.size() > static_cast<size_t>(nStartPara) &&
     382          33 :                 maChildren.size() >= static_cast<size_t>(nEndPara) )
     383             :         {
     384          33 :             VectorOfChildren::iterator front = maChildren.begin();
     385          33 :             VectorOfChildren::iterator back = front;
     386             : 
     387          33 :             ::std::advance( front, nStartPara );
     388          33 :             ::std::advance( back, nEndPara );
     389             : 
     390          33 :             ::std::transform( front, back, front, ReleaseChild() );
     391             :         }
     392          33 :     }
     393             : 
     394          33 :     void AccessibleParaManager::ShutdownPara( const WeakChild& rChild )
     395             :     {
     396          33 :         WeakPara::HardRefType aChild( rChild.first.get() );
     397             : 
     398          33 :         if( IsReferencable( aChild ) )
     399           2 :             aChild->SetEditSource( NULL );
     400          33 :     }
     401             : 
     402             : }
     403             : 
     404             : 
     405             : 
     406             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11