LCOV - code coverage report
Current view: top level - sw/source/core/access - accselectionhelper.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 109 178 61.2 %
Date: 2014-11-03 Functions: 14 14 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         200 : SwAccessibleSelectionHelper::SwAccessibleSelectionHelper(
      47             :     SwAccessibleContext& rCtxt ) :
      48         200 :         rContext( rCtxt )
      49             : {
      50         200 : }
      51             : 
      52         200 : SwAccessibleSelectionHelper::~SwAccessibleSelectionHelper()
      53             : {
      54         200 : }
      55             : 
      56          42 : SwFEShell* SwAccessibleSelectionHelper::GetFEShell()
      57             : {
      58             :     OSL_ENSURE( rContext.GetMap() != NULL, "no map?" );
      59          42 :     SwViewShell* pViewShell = rContext.GetMap()->GetShell();
      60             :     OSL_ENSURE( pViewShell != NULL,
      61             :                 "No view shell? Then what are you looking at?" );
      62             : 
      63          42 :     SwFEShell* pFEShell = NULL;
      64          42 :     if( pViewShell->ISA( SwFEShell ) )
      65             :     {
      66          42 :         pFEShell = static_cast<SwFEShell*>( pViewShell );
      67             :     }
      68             : 
      69          42 :     return pFEShell;
      70             : }
      71             : 
      72          32 : void SwAccessibleSelectionHelper::throwIndexOutOfBoundsException()
      73             :         throw ( lang::IndexOutOfBoundsException )
      74             : {
      75          32 :     Reference < XAccessibleContext > xThis( &rContext );
      76          64 :     Reference < XAccessibleSelection >xSelThis( xThis, UNO_QUERY );
      77             :     lang::IndexOutOfBoundsException aExcept(
      78             :                 OUString( "index out of bounds" ),
      79          64 :                 xSelThis );                                     \
      80          64 :     throw aExcept;
      81             : }
      82             : 
      83             : // XAccessibleSelection
      84           8 : void SwAccessibleSelectionHelper::selectAccessibleChild(
      85             :     sal_Int32 nChildIndex )
      86             :     throw ( lang::IndexOutOfBoundsException,
      87             :             RuntimeException )
      88             : {
      89           8 :     SolarMutexGuard aGuard;
      90             : 
      91             :     // Get the respective child as SwFrm (also do index checking), ...
      92           8 :     const SwAccessibleChild aChild = rContext.GetChild( *(rContext.GetMap()),
      93           8 :                                                         nChildIndex );
      94           8 :     if( !aChild.IsValid() )
      95           8 :         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           8 :     }
     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          12 : static bool lcl_getSelectedState(const SwAccessibleChild& aChild,
     111             :                                      SwAccessibleContext* pContext,
     112             :                                      SwAccessibleMap* pMap)
     113             : {
     114          12 :     Reference< XAccessible > xAcc;
     115          12 :     if ( aChild.GetSwFrm() )
     116             :     {
     117          12 :         xAcc = pMap->GetContext( aChild.GetSwFrm(), false );
     118             :     }
     119           0 :     else if ( aChild.GetDrawObject() )
     120             :     {
     121           0 :         xAcc = pMap->GetContext( aChild.GetDrawObject(), pContext, false );
     122             :     }
     123             : 
     124          12 :     if( xAcc.is() )
     125             :     {
     126          12 :         Reference< XAccessibleContext > pRContext = xAcc->getAccessibleContext();
     127          12 :         if(!pRContext.is())
     128           0 :             return false;
     129          24 :         Reference<XAccessibleStateSet> pRStateSet = pRContext->getAccessibleStateSet();
     130          12 :         if( pRStateSet.is() )
     131             :         {
     132          12 :             Sequence<short> pStates = pRStateSet->getStates();
     133          12 :             long count = pStates.getLength();
     134         132 :             for( int i = 0; i < count; i++ )
     135             :             {
     136         120 :                 if( pStates[i] == AccessibleStateType::SELECTED)
     137           0 :                     return true;
     138          12 :             }
     139          12 :         }
     140             :     }
     141          12 :     return false;
     142             : }
     143             : 
     144          10 : bool SwAccessibleSelectionHelper::isAccessibleChildSelected(
     145             :     sal_Int32 nChildIndex )
     146             :     throw ( lang::IndexOutOfBoundsException,
     147             :             RuntimeException )
     148             : {
     149          10 :     SolarMutexGuard aGuard;
     150             : 
     151             :     // Get the respective child as SwFrm (also do index checking), ...
     152          10 :     const SwAccessibleChild aChild = rContext.GetChild( *(rContext.GetMap()),
     153          10 :                                                         nChildIndex );
     154          10 :     if( !aChild.IsValid() )
     155           8 :         throwIndexOutOfBoundsException();
     156             : 
     157             :     // ... and compare to the currently selected frame
     158           2 :     bool bRet = false;
     159           2 :     SwFEShell* pFEShell = GetFEShell();
     160           2 :     if( pFEShell )
     161             :     {
     162           2 :         if ( aChild.GetSwFrm() != 0 )
     163             :         {
     164           2 :             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           2 :         if( !bRet )
     172             :         {
     173           2 :             if( lcl_getSelectedState( aChild, &rContext, rContext.GetMap() ) )
     174           0 :                 bRet = true;
     175             :         }
     176             :     }
     177             : 
     178           2 :     return bRet;
     179             : }
     180             : 
     181           8 : void SwAccessibleSelectionHelper::clearAccessibleSelection(  )
     182             :     throw ( RuntimeException )
     183             : {
     184             :     // return sal_False     // we can't deselect
     185           8 : }
     186             : 
     187          12 : void SwAccessibleSelectionHelper::selectAllAccessibleChildren(  )
     188             :     throw ( RuntimeException )
     189             : {
     190          12 :     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          12 :     SwFEShell* pFEShell = GetFEShell();
     196          12 :     if( pFEShell )
     197             :     {
     198          12 :         ::std::list< SwAccessibleChild > aChildren;
     199          12 :         rContext.GetChildren( *(rContext.GetMap()), aChildren );
     200             : 
     201          12 :         ::std::list< SwAccessibleChild >::const_iterator aIter = aChildren.begin();
     202          12 :         ::std::list< SwAccessibleChild >::const_iterator aEndIter = aChildren.end();
     203          30 :         while( aIter != aEndIter )
     204             :         {
     205           6 :             const SwAccessibleChild& rChild = *aIter;
     206           6 :             const SdrObject* pObj = rChild.GetDrawObject();
     207           6 :             const SwFrm* pFrm = rChild.GetSwFrm();
     208           6 :             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           6 :             ++aIter;
     215          12 :         }
     216          12 :     }
     217          12 : }
     218             : 
     219          20 : sal_Int32 SwAccessibleSelectionHelper::getSelectedAccessibleChildCount(  )
     220             :     throw ( RuntimeException )
     221             : {
     222          20 :     SolarMutexGuard aGuard;
     223             : 
     224          20 :     sal_Int32 nCount = 0;
     225             :     // Only one frame can be selected at a time, and we only frames
     226             :     // for selectable children.
     227          20 :     SwFEShell* pFEShell = GetFEShell();
     228          20 :     if( pFEShell != 0 )
     229             :     {
     230          20 :         const SwFlyFrm* pFlyFrm = pFEShell->GetCurrFlyFrm();
     231          20 :         if( pFlyFrm )
     232             :         {
     233           0 :             nCount = 1;
     234             :         }
     235             :         else
     236             :         {
     237          20 :             sal_uInt16 nSelObjs = pFEShell->IsObjSelected();
     238          20 :             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 :                         SwAccessibleFrame::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          20 :         if( nCount == 0 )
     264             :         {
     265          20 :             ::std::list< SwAccessibleChild > aChildren;
     266          20 :             rContext.GetChildren( *(rContext.GetMap()), aChildren );
     267             :             ::std::list< SwAccessibleChild >::const_iterator aIter =
     268          20 :                 aChildren.begin();
     269             :             ::std::list< SwAccessibleChild >::const_iterator aEndIter =
     270          20 :                 aChildren.end();
     271          50 :             while( aIter != aEndIter )
     272             :             {
     273          10 :                 const SwAccessibleChild& aChild = *aIter;
     274          10 :                 if( lcl_getSelectedState( aChild, &rContext, rContext.GetMap() ) )
     275           0 :                     nCount++;
     276          10 :                 ++aIter;
     277          20 :             }
     278             :         }
     279             :     }
     280          20 :     return nCount;
     281             : }
     282             : 
     283           8 : Reference<XAccessible> SwAccessibleSelectionHelper::getSelectedAccessibleChild(
     284             :     sal_Int32 nSelectedChildIndex )
     285             :     throw ( lang::IndexOutOfBoundsException,
     286             :             RuntimeException)
     287             : {
     288           8 :     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           8 :     SwFEShell* pFEShell = GetFEShell();
     295           8 :     if( 0 == pFEShell )
     296           0 :         throwIndexOutOfBoundsException();
     297             : 
     298           8 :     SwAccessibleChild aChild;
     299           8 :     const SwFlyFrm *pFlyFrm = pFEShell->GetCurrFlyFrm();
     300           8 :     if( pFlyFrm )
     301             :     {
     302           0 :         if( 0 == nSelectedChildIndex )
     303             :         {
     304           0 :             if(SwAccessibleFrame::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 =  SwAccessibleFrame::GetParent( SwAccessibleChild(pFlyFrm), rContext.IsInPagePreview() );
     317           0 :                         aChild  = pParaFrm;
     318             :                     }
     319             :                 }
     320             :             }
     321             :         }
     322             :     }
     323             :     else
     324             :     {
     325           8 :         sal_uInt16 nSelObjs = pFEShell->IsObjSelected();
     326           8 :         if( 0 == nSelObjs || nSelectedChildIndex >= nSelObjs )
     327           8 :             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 :                 SwAccessibleFrame::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 :                 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, true )  );
     372           0 :         if( xChildImpl.is() )
     373           0 :             xChild = xChildImpl.get();
     374             :     }
     375           8 :     return xChild;
     376             : }
     377             : 
     378             : // index has to be treated as global child index.
     379           8 : void SwAccessibleSelectionHelper::deselectAccessibleChild(
     380             :     sal_Int32 nChildIndex )
     381             :     throw ( lang::IndexOutOfBoundsException,
     382             :             RuntimeException )
     383             : {
     384           8 :     SolarMutexGuard g;
     385             : 
     386          12 :     if( nChildIndex < 0 ||
     387           4 :         nChildIndex >= rContext.GetChildCount( *(rContext.GetMap()) ) )
     388          16 :         throwIndexOutOfBoundsException();
     389         270 : }
     390             : 
     391             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10