LCOV - code coverage report
Current view: top level - libreoffice/sc/source/ui/Accessibility - AccessiblePageHeader.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 1 198 0.5 %
Date: 2012-12-17 Functions: 2 24 8.3 %
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             : #include "AccessiblePageHeader.hxx"
      22             : #include "AccessiblePageHeaderArea.hxx"
      23             : #include "AccessibilityHints.hxx"
      24             : #include "prevwsh.hxx"
      25             : #include "miscuno.hxx"
      26             : #include "prevloc.hxx"
      27             : #include "document.hxx"
      28             : #include "stlpool.hxx"
      29             : #include "scitems.hxx"
      30             : #include "attrib.hxx"
      31             : #include "scresid.hxx"
      32             : #include "sc.hrc"
      33             : 
      34             : #include <com/sun/star/accessibility/AccessibleRole.hpp>
      35             : #include <com/sun/star/accessibility/AccessibleStateType.hpp>
      36             : #include <com/sun/star/accessibility/AccessibleEventId.hpp>
      37             : 
      38             : #include <vcl/window.hxx>
      39             : #include <svl/smplhint.hxx>
      40             : #include <vcl/svapp.hxx>
      41             : #include <unotools/accessiblestatesethelper.hxx>
      42             : #include <svl/style.hxx>
      43             : #include <svl/itempool.hxx>
      44             : #include <editeng/editobj.hxx>
      45             : #include <toolkit/helper/convert.hxx>
      46             : 
      47             : #include <algorithm>
      48             : 
      49             : using namespace ::com::sun::star;
      50             : using namespace ::com::sun::star::accessibility;
      51             : 
      52             : const sal_uInt8     MAX_AREAS = 3;
      53             : 
      54             : //=====  internal  ============================================================
      55             : struct Acquire
      56             : {
      57           0 :     void operator() (ScAccessiblePageHeaderArea* pArea)
      58             :     {
      59           0 :         if (pArea)
      60           0 :             pArea->acquire();
      61           0 :     }
      62             : };
      63             : 
      64             : struct Release
      65             : {
      66           0 :     void operator() (ScAccessiblePageHeaderArea*& pArea)
      67             :     {
      68           0 :         if (pArea)
      69           0 :             pArea->release();
      70           0 :     }
      71             : };
      72             : 
      73             : struct Dispose
      74             : {
      75           0 :     void operator() (ScAccessiblePageHeaderArea*& pArea)
      76             :     {
      77           0 :         if (pArea)
      78             :         {
      79           0 :             pArea->dispose();
      80           0 :             pArea->release();
      81             :         }
      82           0 :         pArea = NULL;
      83           0 :     }
      84             : };
      85             : 
      86           0 : ScAccessiblePageHeader::ScAccessiblePageHeader( const ::com::sun::star::uno::Reference<
      87             :                                 ::com::sun::star::accessibility::XAccessible>& rxParent,
      88             :                             ScPreviewShell* pViewShell, sal_Bool bHeader, sal_Int32 nIndex ) :
      89             : ScAccessibleContextBase( rxParent, bHeader ? AccessibleRole::HEADER : AccessibleRole::FOOTER ),
      90             :     mpViewShell( pViewShell ),
      91             :     mnIndex( nIndex ),
      92             :     mbHeader( bHeader ),
      93             :     maAreas(MAX_AREAS, NULL),
      94           0 :     mnChildCount(-1)
      95             : {
      96           0 :     if (mpViewShell)
      97           0 :         mpViewShell->AddAccessibilityObject(*this);
      98           0 : }
      99             : 
     100           0 : ScAccessiblePageHeader::~ScAccessiblePageHeader()
     101             : {
     102           0 :     if (!ScAccessibleContextBase::IsDefunc() && !rBHelper.bInDispose)
     103             :     {
     104             :         // increment refcount to prevent double call off dtor
     105           0 :         osl_atomic_increment( &m_refCount );
     106           0 :         dispose();
     107             :     }
     108           0 : }
     109             : 
     110           0 : void SAL_CALL ScAccessiblePageHeader::disposing()
     111             : {
     112           0 :     SolarMutexGuard aGuard;
     113           0 :     if (mpViewShell)
     114             :     {
     115           0 :         mpViewShell->RemoveAccessibilityObject(*this);
     116           0 :         mpViewShell = NULL;
     117             :     }
     118           0 :     std::for_each(maAreas.begin(), maAreas.end(), Dispose());
     119             : 
     120           0 :     ScAccessibleContextBase::disposing();
     121           0 : }
     122             : 
     123             : //=====  SfxListener  =====================================================
     124             : 
     125           0 : void ScAccessiblePageHeader::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
     126             : {
     127           0 :     if (rHint.ISA( SfxSimpleHint ) )
     128             :     {
     129           0 :         const SfxSimpleHint& rRef = (const SfxSimpleHint&)rHint;
     130             :         // only notify if child exist, otherwise it is not necessary
     131           0 :         if ((rRef.GetId() == SC_HINT_DATACHANGED))
     132             :         {
     133           0 :             ScHFAreas aOldAreas(maAreas);
     134           0 :             std::for_each(aOldAreas.begin(), aOldAreas.end(), Acquire());
     135           0 :             mnChildCount = -1;
     136           0 :             getAccessibleChildCount();
     137           0 :             for (sal_uInt8 i = 0; i < MAX_AREAS; ++i)
     138             :             {
     139           0 :                 if ((aOldAreas[i] && maAreas[i] && !ScGlobal::EETextObjEqual(aOldAreas[i]->GetEditTextObject(), maAreas[i]->GetEditTextObject())) ||
     140           0 :                     (aOldAreas[i] && !maAreas[i]) || (!aOldAreas[i] && maAreas[i]))
     141             :                 {
     142           0 :                     if (aOldAreas[i] && aOldAreas[i]->GetEditTextObject())
     143             :                     {
     144           0 :                         AccessibleEventObject aEvent;
     145           0 :                         aEvent.EventId = AccessibleEventId::CHILD;
     146           0 :                         aEvent.Source = uno::Reference< XAccessibleContext >(this);
     147           0 :                         aEvent.OldValue = uno::makeAny(uno::Reference<XAccessible>(aOldAreas[i]));
     148             : 
     149           0 :                         CommitChange(aEvent); // child gone - event
     150           0 :                         aOldAreas[i]->dispose();
     151             :                     }
     152           0 :                     if (maAreas[i] && maAreas[i]->GetEditTextObject())
     153             :                     {
     154           0 :                         AccessibleEventObject aEvent;
     155           0 :                         aEvent.EventId = AccessibleEventId::CHILD;
     156           0 :                         aEvent.Source = uno::Reference< XAccessibleContext >(this);
     157           0 :                         aEvent.NewValue = uno::makeAny(uno::Reference<XAccessible>(maAreas[i]));
     158             : 
     159           0 :                         CommitChange(aEvent); // new child - event
     160             :                     }
     161             :                 }
     162             :             }
     163           0 :             std::for_each(aOldAreas.begin(), aOldAreas.end(), Release());
     164             :         }
     165           0 :         else if (rRef.GetId() == SC_HINT_ACC_VISAREACHANGED)
     166             :         {
     167           0 :             AccessibleEventObject aEvent;
     168           0 :             aEvent.EventId = AccessibleEventId::VISIBLE_DATA_CHANGED;
     169           0 :             aEvent.Source = uno::Reference< XAccessibleContext >(this);
     170           0 :             CommitChange(aEvent);
     171             :         }
     172             :     }
     173             : 
     174           0 :     ScAccessibleContextBase::Notify(rBC, rHint);
     175           0 : }
     176             : 
     177             : //=====  XAccessibleComponent  ============================================
     178             : 
     179           0 : uno::Reference< XAccessible > SAL_CALL ScAccessiblePageHeader::getAccessibleAtPoint( const awt::Point& aPoint )
     180             :                                 throw (uno::RuntimeException)
     181             : {
     182           0 :     uno::Reference<XAccessible> xRet;
     183             : 
     184           0 :     if (containsPoint(aPoint))
     185             :     {
     186           0 :         SolarMutexGuard aGuard;
     187           0 :         IsObjectValid();
     188             : 
     189           0 :         sal_Int32 nCount(getAccessibleChildCount()); // fill the areas
     190             : 
     191           0 :         if (nCount)
     192             :         {
     193             :             // return the first with content, because they have all the same Bounding Box
     194           0 :             sal_uInt8 i(0);
     195           0 :             while(!xRet.is() && i < MAX_AREAS)
     196             :             {
     197           0 :                 if (maAreas[i])
     198           0 :                     xRet = maAreas[i];
     199             :                 else
     200           0 :                     ++i;
     201             :             }
     202           0 :         }
     203             :     }
     204             : 
     205           0 :     return xRet;
     206             : }
     207             : 
     208           0 : void SAL_CALL ScAccessiblePageHeader::grabFocus() throw (uno::RuntimeException)
     209             : {
     210           0 :      SolarMutexGuard aGuard;
     211           0 :     IsObjectValid();
     212           0 :     if (getAccessibleParent().is())
     213             :     {
     214           0 :         uno::Reference<XAccessibleComponent> xAccessibleComponent(getAccessibleParent()->getAccessibleContext(), uno::UNO_QUERY);
     215           0 :         if (xAccessibleComponent.is())
     216           0 :             xAccessibleComponent->grabFocus();
     217           0 :     }
     218           0 : }
     219             : 
     220             : //=====  XAccessibleContext  ==============================================
     221             : 
     222           0 : sal_Int32 SAL_CALL ScAccessiblePageHeader::getAccessibleChildCount() throw (uno::RuntimeException)
     223             : {
     224           0 :     SolarMutexGuard aGuard;
     225           0 :     IsObjectValid();
     226             : 
     227           0 :     if((mnChildCount < 0) && mpViewShell)
     228             :     {
     229           0 :         mnChildCount = 0;
     230           0 :         ScDocument* pDoc = mpViewShell->GetDocument();
     231           0 :         if (pDoc)
     232             :         {
     233             :             // find out how many regions (left,center, right) are with content
     234             : 
     235           0 :             SfxStyleSheetBase* pStyle = pDoc->GetStyleSheetPool()->Find(pDoc->GetPageStyle(mpViewShell->GetLocationData().GetPrintTab()), SFX_STYLE_FAMILY_PAGE);
     236           0 :             if (pStyle)
     237             :             {
     238           0 :                 sal_uInt16 nPageWhichId(0);
     239           0 :                 if (mbHeader)
     240           0 :                     nPageWhichId = mpViewShell->GetLocationData().IsHeaderLeft() ? ATTR_PAGE_HEADERLEFT : ATTR_PAGE_HEADERRIGHT;
     241             :                 else
     242           0 :                     nPageWhichId = mpViewShell->GetLocationData().IsFooterLeft() ? ATTR_PAGE_FOOTERLEFT : ATTR_PAGE_FOOTERRIGHT;
     243             : 
     244           0 :                 const ScPageHFItem& rPageItem = static_cast<const ScPageHFItem&>(pStyle->GetItemSet().Get(nPageWhichId));
     245           0 :                 AddChild(rPageItem.GetLeftArea(), 0, SVX_ADJUST_LEFT);
     246           0 :                 AddChild(rPageItem.GetCenterArea(), 1, SVX_ADJUST_CENTER);
     247           0 :                 AddChild(rPageItem.GetRightArea(), 2, SVX_ADJUST_RIGHT);
     248             :             }
     249             :         }
     250             :     }
     251             : 
     252           0 :     return mnChildCount;
     253             : }
     254             : 
     255           0 : uno::Reference< XAccessible > SAL_CALL ScAccessiblePageHeader::getAccessibleChild( sal_Int32 nIndex )
     256             :                                 throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
     257             : {
     258           0 :     SolarMutexGuard aGuard;
     259           0 :     IsObjectValid();
     260             : 
     261           0 :     uno::Reference<XAccessible> xRet;
     262             : 
     263           0 :     if(mnChildCount < 0)
     264           0 :         getAccessibleChildCount();
     265             : 
     266           0 :     ScHFAreas::iterator aItr = maAreas.begin();
     267           0 :     ScHFAreas::iterator aEndItr = maAreas.end();
     268           0 :     while (!xRet.is() && (nIndex >= 0) && (aItr != aEndItr))
     269             :     {
     270           0 :         if (*aItr)
     271             :         {
     272           0 :             if (nIndex == 0)
     273           0 :                 xRet = *aItr;
     274             :             else
     275           0 :                 --nIndex;
     276             :         }
     277             :         else
     278           0 :             ++aItr;
     279             :     }
     280             : 
     281           0 :     if ( !xRet.is() )
     282           0 :         throw lang::IndexOutOfBoundsException();
     283             : 
     284           0 :     return xRet;
     285             : }
     286             : 
     287           0 : sal_Int32 SAL_CALL ScAccessiblePageHeader::getAccessibleIndexInParent() throw (uno::RuntimeException)
     288             : {
     289           0 :     return mnIndex;
     290             : }
     291             : 
     292           0 : uno::Reference< XAccessibleStateSet > SAL_CALL ScAccessiblePageHeader::getAccessibleStateSet()
     293             :                                 throw (uno::RuntimeException)
     294             : {
     295           0 :     SolarMutexGuard aGuard;
     296           0 :     uno::Reference<XAccessibleStateSet> xParentStates;
     297           0 :     if (getAccessibleParent().is())
     298             :     {
     299           0 :         uno::Reference<XAccessibleContext> xParentContext = getAccessibleParent()->getAccessibleContext();
     300           0 :         xParentStates = xParentContext->getAccessibleStateSet();
     301             :     }
     302           0 :     utl::AccessibleStateSetHelper* pStateSet = new utl::AccessibleStateSetHelper();
     303           0 :     if (IsDefunc(xParentStates))
     304           0 :         pStateSet->AddState(AccessibleStateType::DEFUNC);
     305             :     else
     306             :     {
     307           0 :         pStateSet->AddState(AccessibleStateType::ENABLED);
     308           0 :         pStateSet->AddState(AccessibleStateType::OPAQUE);
     309           0 :         if (isShowing())
     310           0 :             pStateSet->AddState(AccessibleStateType::SHOWING);
     311           0 :         if (isVisible())
     312           0 :             pStateSet->AddState(AccessibleStateType::VISIBLE);
     313             :     }
     314           0 :     return pStateSet;
     315             : }
     316             : 
     317             : //=====  XServiceInfo  ====================================================
     318             : 
     319           0 : rtl::OUString SAL_CALL ScAccessiblePageHeader::getImplementationName() throw(uno::RuntimeException)
     320             : {
     321           0 :     return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ScAccessiblePageHeader"));
     322             : }
     323             : 
     324           0 : uno::Sequence<rtl::OUString> SAL_CALL ScAccessiblePageHeader::getSupportedServiceNames()
     325             :                                                     throw(uno::RuntimeException)
     326             : {
     327           0 :     uno::Sequence< ::rtl::OUString > aSequence = ScAccessibleContextBase::getSupportedServiceNames();
     328           0 :     sal_Int32 nOldSize(aSequence.getLength());
     329           0 :     aSequence.realloc(nOldSize + 1);
     330           0 :     ::rtl::OUString* pNames = aSequence.getArray();
     331             : 
     332           0 :     pNames[nOldSize] = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.AccessibleHeaderFooterView"));
     333             : 
     334           0 :     return aSequence;
     335             : }
     336             : 
     337             : //====  internal  =========================================================
     338             : 
     339           0 : ::rtl::OUString SAL_CALL ScAccessiblePageHeader::createAccessibleDescription(void)
     340             :                     throw (uno::RuntimeException)
     341             : {
     342           0 :     String sDesc(ScResId(mbHeader ? STR_ACC_HEADER_DESCR : STR_ACC_FOOTER_DESCR));
     343           0 :     sDesc.SearchAndReplaceAscii("%1", String(ScResId(SCSTR_UNKNOWN)));
     344           0 :     return rtl::OUString( sDesc );
     345             : }
     346             : 
     347           0 : ::rtl::OUString SAL_CALL ScAccessiblePageHeader::createAccessibleName(void)
     348             :                     throw (uno::RuntimeException)
     349             : {
     350           0 :     String sName(ScResId(mbHeader ? STR_ACC_HEADER_NAME : STR_ACC_FOOTER_NAME));
     351           0 :     sName.SearchAndReplaceAscii("%1", String(ScResId(SCSTR_UNKNOWN)));
     352           0 :     return rtl::OUString( sName );
     353             : }
     354             : 
     355           0 : Rectangle ScAccessiblePageHeader::GetBoundingBoxOnScreen() const throw (uno::RuntimeException)
     356             : {
     357           0 :     Rectangle aCellRect(GetBoundingBox());
     358           0 :     if (mpViewShell)
     359             :     {
     360           0 :         Window* pWindow = mpViewShell->GetWindow();
     361           0 :         if (pWindow)
     362             :         {
     363           0 :             Rectangle aRect = pWindow->GetWindowExtentsRelative(NULL);
     364           0 :             aCellRect.setX(aCellRect.getX() + aRect.getX());
     365           0 :             aCellRect.setY(aCellRect.getY() + aRect.getY());
     366             :         }
     367             :     }
     368           0 :     return aCellRect;
     369             : }
     370             : 
     371           0 : Rectangle ScAccessiblePageHeader::GetBoundingBox() const throw (uno::RuntimeException)
     372             : {
     373           0 :     Rectangle aRect;
     374           0 :     if (mpViewShell)
     375             :     {
     376           0 :         const ScPreviewLocationData& rData = mpViewShell->GetLocationData();
     377           0 :         if ( mbHeader )
     378           0 :             rData.GetHeaderPosition( aRect );
     379             :         else
     380           0 :             rData.GetFooterPosition( aRect );
     381             : 
     382             :         // the Rectangle could contain negative coordinates so it should be cliped
     383           0 :         Rectangle aClipRect(Point(0, 0), aRect.GetSize());
     384           0 :         Window* pWindow = mpViewShell->GetWindow();
     385           0 :         if (pWindow)
     386           0 :             aClipRect = pWindow->GetWindowExtentsRelative(pWindow->GetAccessibleParentWindow());
     387           0 :         aRect = aClipRect.GetIntersection(aRect);
     388             :     }
     389           0 :     if (aRect.IsEmpty())
     390           0 :         aRect.SetSize(Size(-1, -1));
     391             : 
     392           0 :     return aRect;
     393             : }
     394             : 
     395           0 : sal_Bool ScAccessiblePageHeader::IsDefunc( const uno::Reference<XAccessibleStateSet>& rxParentStates )
     396             : {
     397           0 :     return ScAccessibleContextBase::IsDefunc() || (mpViewShell == NULL) || !getAccessibleParent().is() ||
     398           0 :         (rxParentStates.is() && rxParentStates->contains(AccessibleStateType::DEFUNC));
     399             : }
     400             : 
     401           0 : void ScAccessiblePageHeader::AddChild(const EditTextObject* pArea, sal_uInt32 nIndex, SvxAdjust eAdjust)
     402             : {
     403           0 :     if (pArea && (pArea->GetText(0).Len() || (pArea->GetParagraphCount() > 1)))
     404             :     {
     405           0 :         if (maAreas[nIndex])
     406             :         {
     407           0 :             if (!ScGlobal::EETextObjEqual(maAreas[nIndex]->GetEditTextObject(), pArea))
     408             :             {
     409           0 :                 maAreas[nIndex]->release();
     410           0 :                 maAreas[nIndex] = new ScAccessiblePageHeaderArea(this, mpViewShell, pArea, mbHeader, eAdjust);
     411           0 :                 maAreas[nIndex]->acquire();
     412             :             }
     413             :         }
     414             :         else
     415             :         {
     416           0 :             maAreas[nIndex] = new ScAccessiblePageHeaderArea(this, mpViewShell, pArea, mbHeader, eAdjust);
     417           0 :             maAreas[nIndex]->acquire();
     418             :         }
     419           0 :         ++mnChildCount;
     420             :     }
     421             :     else
     422             :     {
     423           0 :         if (maAreas[nIndex])
     424             :         {
     425           0 :             maAreas[nIndex]->release();
     426           0 :             maAreas[nIndex] = NULL;
     427             :         }
     428             :     }
     429         102 : }
     430             : 
     431             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10