LCOV - code coverage report
Current view: top level - sw/source/core/access - accselectionhelper.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 108 178 60.7 %
Date: 2014-04-11 Functions: 12 12 100.0 %
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 <com/sun/star/accessibility/XAccessibleSelection.hpp>
      21             : #include <accselectionhelper.hxx>
      22             : 
      23             : #include <acccontext.hxx>
      24             : #include <accmap.hxx>
      25             : #include <svx/AccessibleShape.hxx>
      26             : #include <viewsh.hxx>
      27             : #include <fesh.hxx>
      28             : #include <vcl/svapp.hxx>
      29             : #include <flyfrm.hxx>
      30             : 
      31             : #include <com/sun/star/uno/Reference.hxx>
      32             : #include <com/sun/star/accessibility/AccessibleStateType.hpp>
      33             : #include <com/sun/star/accessibility/XAccessibleStateSet.hpp>
      34             : #include <fmtanchr.hxx>
      35             : 
      36             : using namespace ::com::sun::star::accessibility;
      37             : using namespace ::com::sun::star;
      38             : using namespace ::com::sun::star::uno;
      39             : 
      40             : using ::com::sun::star::accessibility::XAccessible;
      41             : using ::com::sun::star::accessibility::XAccessibleContext;
      42             : using ::com::sun::star::accessibility::XAccessibleSelection;
      43             : 
      44             : using namespace ::sw::access;
      45             : 
      46          69 : SwAccessibleSelectionHelper::SwAccessibleSelectionHelper(
      47             :     SwAccessibleContext& rCtxt ) :
      48          69 :         rContext( rCtxt )
      49             : {
      50          69 : }
      51             : 
      52          69 : SwAccessibleSelectionHelper::~SwAccessibleSelectionHelper()
      53             : {
      54          69 : }
      55             : 
      56          21 : SwFEShell* SwAccessibleSelectionHelper::GetFEShell()
      57             : {
      58             :     OSL_ENSURE( rContext.GetMap() != NULL, "no map?" );
      59          21 :     SwViewShell* pViewShell = rContext.GetMap()->GetShell();
      60             :     OSL_ENSURE( pViewShell != NULL,
      61             :                 "No view shell? Then what are you looking at?" );
      62             : 
      63          21 :     SwFEShell* pFEShell = NULL;
      64          21 :     if( pViewShell->ISA( SwFEShell ) )
      65             :     {
      66          21 :         pFEShell = static_cast<SwFEShell*>( pViewShell );
      67             :     }
      68             : 
      69          21 :     return pFEShell;
      70             : }
      71             : 
      72          16 : void SwAccessibleSelectionHelper::throwIndexOutOfBoundsException()
      73             :         throw ( lang::IndexOutOfBoundsException )
      74             : {
      75          16 :     Reference < XAccessibleContext > xThis( &rContext );
      76          32 :     Reference < XAccessibleSelection >xSelThis( xThis, UNO_QUERY );
      77             :     lang::IndexOutOfBoundsException aExcept(
      78             :                 OUString( "index out of bounds" ),
      79          32 :                 xSelThis );                                     \
      80          32 :     throw aExcept;
      81             : }
      82             : 
      83             : // XAccessibleSelection
      84           4 : void SwAccessibleSelectionHelper::selectAccessibleChild(
      85             :     sal_Int32 nChildIndex )
      86             :     throw ( lang::IndexOutOfBoundsException,
      87             :             RuntimeException )
      88             : {
      89           4 :     SolarMutexGuard aGuard;
      90             : 
      91             :     // Get the respective child as SwFrm (also do index checking), ...
      92           4 :     const SwAccessibleChild aChild = rContext.GetChild( *(rContext.GetMap()),
      93           4 :                                                         nChildIndex );
      94           4 :     if( !aChild.IsValid() )
      95           4 :         throwIndexOutOfBoundsException();
      96             : 
      97             :     // we can only select fly frames, so we ignore (should: return
      98             :     // false) all other attempts at child selection
      99           0 :     SwFEShell* pFEShell = GetFEShell();
     100           0 :     if( pFEShell != NULL )
     101             :     {
     102           0 :         const SdrObject *pObj = aChild.GetDrawObject();
     103           0 :         if( pObj )
     104           0 :             rContext.Select( const_cast< SdrObject *>( pObj ), 0==aChild.GetSwFrm());
     105           4 :     }
     106             :     // no frame shell, or no frame, or no fly frame -> can't select
     107           0 : }
     108             : 
     109             : //When the selected state of the SwFrmOrObj is setted, return true.
     110           6 : static sal_Bool lcl_getSelectedState(const SwAccessibleChild& aChild,
     111             :                                      SwAccessibleContext* pContext,
     112             :                                      SwAccessibleMap* pMap)
     113             : {
     114           6 :     Reference< XAccessible > xAcc;
     115           6 :     if ( aChild.GetSwFrm() )
     116             :     {
     117           6 :         xAcc = pMap->GetContext( aChild.GetSwFrm(), sal_False );
     118             :     }
     119           0 :     else if ( aChild.GetDrawObject() )
     120             :     {
     121           0 :         xAcc = pMap->GetContext( aChild.GetDrawObject(), pContext, sal_False );
     122             :     }
     123             : 
     124           6 :     if( xAcc.is() )
     125             :     {
     126           6 :         Reference< XAccessibleContext > pRContext = xAcc->getAccessibleContext();
     127           6 :         if(!pRContext.is())
     128           0 :             return sal_False;
     129          12 :         Reference<XAccessibleStateSet> pRStateSet = pRContext->getAccessibleStateSet();
     130           6 :         if( pRStateSet.is() )
     131             :         {
     132           6 :             Sequence<short> pStates = pRStateSet->getStates();
     133           6 :             long count = pStates.getLength();
     134          66 :             for( int i = 0; i < count; i++ )
     135             :             {
     136          60 :                 if( pStates[i] == AccessibleStateType::SELECTED)
     137           0 :                     return sal_True;
     138           6 :             }
     139           6 :         }
     140             :     }
     141           6 :     return sal_False;
     142             : }
     143             : 
     144           5 : sal_Bool SwAccessibleSelectionHelper::isAccessibleChildSelected(
     145             :     sal_Int32 nChildIndex )
     146             :     throw ( lang::IndexOutOfBoundsException,
     147             :             RuntimeException )
     148             : {
     149           5 :     SolarMutexGuard aGuard;
     150             : 
     151             :     // Get the respective child as SwFrm (also do index checking), ...
     152           5 :     const SwAccessibleChild aChild = rContext.GetChild( *(rContext.GetMap()),
     153           5 :                                                         nChildIndex );
     154           5 :     if( !aChild.IsValid() )
     155           4 :         throwIndexOutOfBoundsException();
     156             : 
     157             :     // ... and compare to the currently selected frame
     158           1 :     sal_Bool bRet = sal_False;
     159           1 :     SwFEShell* pFEShell = GetFEShell();
     160           1 :     if( pFEShell )
     161             :     {
     162           1 :         if ( aChild.GetSwFrm() != 0 )
     163             :         {
     164           1 :             bRet = (pFEShell->GetCurrFlyFrm() == aChild.GetSwFrm());
     165             :         }
     166           0 :         else if ( aChild.GetDrawObject() )
     167             :         {
     168           0 :             bRet = pFEShell->IsObjSelected( *aChild.GetDrawObject() );
     169             :         }
     170             :         //If the SwFrmOrObj is not selected directly in the UI, we should check whether it is selected in the selection cursor.
     171           1 :         if( !bRet )
     172             :         {
     173           1 :             if( lcl_getSelectedState( aChild, &rContext, rContext.GetMap() ) == sal_True)
     174           0 :                 bRet = sal_True;
     175             :         }
     176             :     }
     177             : 
     178           1 :     return bRet;
     179             : }
     180             : 
     181           4 : void SwAccessibleSelectionHelper::clearAccessibleSelection(  )
     182             :     throw ( RuntimeException )
     183             : {
     184             :     // return sal_False     // we can't deselect
     185           4 : }
     186             : 
     187           6 : void SwAccessibleSelectionHelper::selectAllAccessibleChildren(  )
     188             :     throw ( RuntimeException )
     189             : {
     190           6 :     SolarMutexGuard aGuard;
     191             : 
     192             :     // We can select only one. So iterate over the children to find
     193             :     // the first we can select, and select it.
     194             : 
     195           6 :     SwFEShell* pFEShell = GetFEShell();
     196           6 :     if( pFEShell )
     197             :     {
     198           6 :         ::std::list< SwAccessibleChild > aChildren;
     199           6 :         rContext.GetChildren( *(rContext.GetMap()), aChildren );
     200             : 
     201           6 :         ::std::list< SwAccessibleChild >::const_iterator aIter = aChildren.begin();
     202           6 :         ::std::list< SwAccessibleChild >::const_iterator aEndIter = aChildren.end();
     203          15 :         while( aIter != aEndIter )
     204             :         {
     205           3 :             const SwAccessibleChild& rChild = *aIter;
     206           3 :             const SdrObject* pObj = rChild.GetDrawObject();
     207           3 :             const SwFrm* pFrm = rChild.GetSwFrm();
     208           3 :             if( pObj && !(pFrm != 0 && pFEShell->IsObjSelected()) )
     209             :             {
     210           0 :                 rContext.Select( const_cast< SdrObject *>( pObj ), 0==pFrm );
     211           0 :                 if( pFrm )
     212           0 :                     break;
     213             :             }
     214           3 :             ++aIter;
     215           6 :         }
     216           6 :     }
     217           6 : }
     218             : 
     219          10 : sal_Int32 SwAccessibleSelectionHelper::getSelectedAccessibleChildCount(  )
     220             :     throw ( RuntimeException )
     221             : {
     222          10 :     SolarMutexGuard aGuard;
     223             : 
     224          10 :     sal_Int32 nCount = 0;
     225             :     // Only one frame can be selected at a time, and we only frames
     226             :     // for selectable children.
     227          10 :     SwFEShell* pFEShell = GetFEShell();
     228          10 :     if( pFEShell != 0 )
     229             :     {
     230          10 :         const SwFlyFrm* pFlyFrm = pFEShell->GetCurrFlyFrm();
     231          10 :         if( pFlyFrm )
     232             :         {
     233           0 :             nCount = 1;
     234             :         }
     235             :         else
     236             :         {
     237          10 :             sal_uInt16 nSelObjs = pFEShell->IsObjSelected();
     238          10 :             if( nSelObjs > 0 )
     239             :             {
     240           0 :                 ::std::list< SwAccessibleChild > aChildren;
     241           0 :                 rContext.GetChildren( *(rContext.GetMap()), aChildren );
     242             : 
     243             :                 ::std::list< SwAccessibleChild >::const_iterator aIter =
     244           0 :                     aChildren.begin();
     245             :                 ::std::list< SwAccessibleChild >::const_iterator aEndIter =
     246           0 :                     aChildren.end();
     247           0 :                 while( aIter != aEndIter && nCount < nSelObjs )
     248             :                 {
     249           0 :                     const SwAccessibleChild& rChild = *aIter;
     250           0 :                     if( rChild.GetDrawObject() && !rChild.GetSwFrm() &&
     251           0 :                         rContext.GetParent(rChild, rContext.IsInPagePreview())
     252           0 :                            == rContext.GetFrm() &&
     253           0 :                         pFEShell->IsObjSelected( *rChild.GetDrawObject() ) )
     254             :                     {
     255           0 :                         nCount++;
     256             :                     }
     257           0 :                     ++aIter;
     258           0 :                 }
     259             :             }
     260             :         }
     261             :         //If the SwFrmOrObj is not selected directly in the UI,
     262             :         //we should check whether it is selected in the selection cursor.
     263          10 :         if( nCount == 0 )
     264             :         {
     265          10 :             ::std::list< SwAccessibleChild > aChildren;
     266          10 :             rContext.GetChildren( *(rContext.GetMap()), aChildren );
     267             :             ::std::list< SwAccessibleChild >::const_iterator aIter =
     268          10 :                 aChildren.begin();
     269             :             ::std::list< SwAccessibleChild >::const_iterator aEndIter =
     270          10 :                 aChildren.end();
     271          25 :             while( aIter != aEndIter )
     272             :             {
     273           5 :                 const SwAccessibleChild& aChild = *aIter;
     274           5 :                 if( lcl_getSelectedState( aChild, &rContext, rContext.GetMap() ) )
     275           0 :                     nCount++;
     276           5 :                 ++aIter;
     277          10 :             }
     278             :         }
     279             :     }
     280          10 :     return nCount;
     281             : }
     282             : 
     283           4 : Reference<XAccessible> SwAccessibleSelectionHelper::getSelectedAccessibleChild(
     284             :     sal_Int32 nSelectedChildIndex )
     285             :     throw ( lang::IndexOutOfBoundsException,
     286             :             RuntimeException)
     287             : {
     288           4 :     SolarMutexGuard aGuard;
     289             : 
     290             :     // Since the index is relative to the selected children, and since
     291             :     // there can be at most one selected frame child, the index must
     292             :     // be 0, and a selection must exist, otherwise we have to throw an
     293             :     // lang::IndexOutOfBoundsException
     294           4 :     SwFEShell* pFEShell = GetFEShell();
     295           4 :     if( 0 == pFEShell )
     296           0 :         throwIndexOutOfBoundsException();
     297             : 
     298           4 :     SwAccessibleChild aChild;
     299           4 :     const SwFlyFrm *pFlyFrm = pFEShell->GetCurrFlyFrm();
     300           4 :     if( pFlyFrm )
     301             :     {
     302           0 :         if( 0 == nSelectedChildIndex )
     303             :         {
     304           0 :             if(rContext.GetParent( SwAccessibleChild(pFlyFrm), rContext.IsInPagePreview()) == rContext.GetFrm() )
     305             :             {
     306           0 :                 aChild = pFlyFrm;
     307             :             }
     308             :             else
     309             :             {
     310           0 :                 const SwFrmFmt *pFrmFmt = pFlyFrm->GetFmt();
     311           0 :                 if (pFrmFmt)
     312             :                 {
     313           0 :                     const SwFmtAnchor& pAnchor = pFrmFmt->GetAnchor();
     314           0 :                     if( pAnchor.GetAnchorId() == FLY_AS_CHAR )
     315             :                     {
     316           0 :                         const SwFrm  *pParaFrm =  rContext.GetParent( SwAccessibleChild(pFlyFrm), rContext.IsInPagePreview() );
     317           0 :                         aChild  = pParaFrm;
     318             :                     }
     319             :                 }
     320             :             }
     321             :         }
     322             :     }
     323             :     else
     324             :     {
     325           4 :         sal_uInt16 nSelObjs = pFEShell->IsObjSelected();
     326           4 :         if( 0 == nSelObjs || nSelectedChildIndex >= nSelObjs )
     327           4 :             throwIndexOutOfBoundsException();
     328             : 
     329           0 :         ::std::list< SwAccessibleChild > aChildren;
     330           0 :         rContext.GetChildren( *(rContext.GetMap()), aChildren );
     331             : 
     332           0 :         ::std::list< SwAccessibleChild >::const_iterator aIter = aChildren.begin();
     333           0 :         ::std::list< SwAccessibleChild >::const_iterator aEndIter = aChildren.end();
     334           0 :         while( aIter != aEndIter && !aChild.IsValid() )
     335             :         {
     336           0 :             const SwAccessibleChild& rChild = *aIter;
     337           0 :             if( rChild.GetDrawObject() && !rChild.GetSwFrm() &&
     338           0 :                 rContext.GetParent(rChild, rContext.IsInPagePreview()) ==
     339           0 :                     rContext.GetFrm() &&
     340           0 :                 pFEShell->IsObjSelected( *rChild.GetDrawObject() ) )
     341             :             {
     342           0 :                 if( 0 == nSelectedChildIndex )
     343           0 :                     aChild = rChild;
     344             :                 else
     345           0 :                     --nSelectedChildIndex;
     346             :             }
     347           0 :             ++aIter;
     348           0 :         }
     349             :     }
     350             : 
     351           0 :     if( !aChild.IsValid() )
     352           0 :         throwIndexOutOfBoundsException();
     353             : 
     354             :     OSL_ENSURE( rContext.GetMap() != NULL, "We need the map." );
     355           0 :     Reference< XAccessible > xChild;
     356           0 :     if( aChild.GetSwFrm() )
     357             :     {
     358             :         ::rtl::Reference < SwAccessibleContext > xChildImpl(
     359             :                 rContext.GetMap()->GetContextImpl( aChild.GetSwFrm(),
     360           0 :                 sal_True ) );
     361           0 :         if( xChildImpl.is() )
     362             :         {
     363           0 :             xChildImpl->SetParent( &rContext );
     364           0 :             xChild = xChildImpl.get();
     365           0 :         }
     366             :     }
     367           0 :     else if ( aChild.GetDrawObject() )
     368             :     {
     369             :         ::rtl::Reference < ::accessibility::AccessibleShape > xChildImpl(
     370             :                 rContext.GetMap()->GetContextImpl( aChild.GetDrawObject(),
     371           0 :                                           &rContext, sal_True )  );
     372           0 :         if( xChildImpl.is() )
     373           0 :             xChild = xChildImpl.get();
     374             :     }
     375           4 :     return xChild;
     376             : }
     377             : 
     378             : // index has to be treated as global child index.
     379           4 : void SwAccessibleSelectionHelper::deselectAccessibleChild(
     380             :     sal_Int32 nChildIndex )
     381             :     throw ( lang::IndexOutOfBoundsException,
     382             :             RuntimeException )
     383             : {
     384           4 :     SolarMutexGuard g;
     385             : 
     386           6 :     if( nChildIndex < 0 ||
     387           2 :         nChildIndex >= rContext.GetChildCount( *(rContext.GetMap()) ) )
     388           8 :         throwIndexOutOfBoundsException();
     389           0 : }
     390             : 
     391             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10