ITEM: Refactor ItemType
[LibreOffice.git] / sw / source / core / access / acctextframe.cxx
blobc9eaf34dd94d1798fd96de2a46d377ec6fd025bd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/accessibility/XAccessibleContext.hpp>
21 #include <cppuhelper/queryinterface.hxx>
22 #include <cppuhelper/supportsservice.hxx>
23 #include <vcl/svapp.hxx>
24 #include <sal/log.hxx>
25 #include <com/sun/star/accessibility/AccessibleRole.hpp>
26 #include <com/sun/star/accessibility/AccessibleRelation.hpp>
27 #include <com/sun/star/accessibility/AccessibleRelationType.hpp>
28 #include <com/sun/star/accessibility/XAccessibleRelationSet.hpp>
29 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
30 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
31 #include <frmfmt.hxx>
32 #include <flyfrm.hxx>
33 #include <accmap.hxx>
34 #include <unotools/accessiblerelationsethelper.hxx>
35 #include <hints.hxx>
36 #include "acctextframe.hxx"
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::accessibility;
41 using utl::AccessibleRelationSetHelper;
42 using ::com::sun::star::accessibility::XAccessibleContext;
44 SwAccessibleTextFrame::SwAccessibleTextFrame(
45 std::shared_ptr<SwAccessibleMap> const& pInitMap,
46 const SwFlyFrame& rFlyFrame ) :
47 SwAccessibleFrameBase( pInitMap, AccessibleRole::TEXT_FRAME, &rFlyFrame )
49 const SwFlyFrameFormat* pFlyFrameFormat = rFlyFrame.GetFormat();
50 msTitle = pFlyFrameFormat->GetObjTitle();
52 msDesc = pFlyFrameFormat->GetObjDescription();
53 if ( msDesc.isEmpty() &&
54 msTitle != GetName() )
56 msDesc = msTitle;
60 SwAccessibleTextFrame::~SwAccessibleTextFrame()
64 void SwAccessibleTextFrame::Notify(const SfxHint& rHint)
66 const SwFlyFrame* pFlyFrame = static_cast<const SwFlyFrame*>(GetFrame());
67 const SwFlyFrameFormat* pFlyFrameFormat = pFlyFrame ? pFlyFrame->GetFormat() : nullptr;
68 switch(rHint.GetId())
70 case SfxHintId::Dying:
71 EndListeningAll();
72 return;
73 default:
74 return;
75 // #i73249#
76 case SfxHintId::SwTitleChanged:
78 auto rTitleChanged = static_cast<const sw::TitleChanged&>(rHint);
79 msTitle = rTitleChanged.m_sNew;
80 AccessibleEventObject aEvent;
81 aEvent.EventId = AccessibleEventId::NAME_CHANGED;
82 aEvent.OldValue <<= rTitleChanged.m_sOld;
83 aEvent.NewValue <<= msTitle;
84 FireAccessibleEvent( aEvent );
85 if(!pFlyFrameFormat || !pFlyFrameFormat->GetObjDescription().isEmpty())
86 break;
87 [[fallthrough]];
89 case SfxHintId::SwDescriptionChanged:
91 if(!pFlyFrame)
92 return;
93 const OUString sOldDesc(msDesc);
94 msDesc = pFlyFrameFormat->GetObjDescription();
95 if(msDesc.isEmpty() && msTitle != GetName())
96 msDesc = msTitle;
97 if(msDesc == sOldDesc)
98 return;
99 AccessibleEventObject aEvent;
100 aEvent.EventId = AccessibleEventId::DESCRIPTION_CHANGED;
101 aEvent.OldValue <<= sOldDesc;
102 aEvent.NewValue <<= msDesc;
103 FireAccessibleEvent(aEvent);
104 return;
109 // XInterface
111 css::uno::Any SAL_CALL
112 SwAccessibleTextFrame::queryInterface (const css::uno::Type & rType)
114 css::uno::Any aReturn = SwAccessibleContext::queryInterface (rType);
115 if ( ! aReturn.hasValue())
116 aReturn = ::cppu::queryInterface (rType,
117 static_cast< css::accessibility::XAccessibleSelection* >(this)
119 return aReturn;
122 void SAL_CALL
123 SwAccessibleTextFrame::acquire()
124 noexcept
126 SwAccessibleContext::acquire ();
129 void SAL_CALL
130 SwAccessibleTextFrame::release()
131 noexcept
133 SwAccessibleContext::release ();
136 // XAccessibleSelection
138 void SAL_CALL SwAccessibleTextFrame::selectAccessibleChild( sal_Int64 )
140 SAL_WARN("sw.a11y", "SwAccessibleTextFrame::selectAccessibleChild - missing implementation");
143 sal_Bool SAL_CALL SwAccessibleTextFrame::isAccessibleChildSelected( sal_Int64 nChildIndex )
145 SolarMutexGuard g;
147 uno::Reference<XAccessible> xAcc = getAccessibleChild( nChildIndex );
148 uno::Reference<XAccessibleContext> xContext;
149 if( xAcc.is() )
150 xContext = xAcc->getAccessibleContext();
152 if( xContext.is() )
154 const sal_Int16 nRole = xContext->getAccessibleRole();
155 if (nRole == AccessibleRole::PARAGRAPH || nRole == AccessibleRole::BLOCK_QUOTE)
157 uno::Reference< css::accessibility::XAccessibleText >
158 xText(xAcc, uno::UNO_QUERY);
159 if( xText.is() )
161 if( xText->getSelectionStart() >= 0 ) return true;
166 return false;
169 void SAL_CALL SwAccessibleTextFrame::clearAccessibleSelection( )
171 SAL_WARN("sw.a11y", "<SwAccessibleTextFrame::clearAccessibleSelection()> - missing implementation");
174 void SAL_CALL SwAccessibleTextFrame::selectAllAccessibleChildren( )
176 SAL_WARN("sw.a11y", "<SwAccessibleTextFrame::selectAllAccessibleChildren()> - missing implementation");
179 sal_Int64 SAL_CALL SwAccessibleTextFrame::getSelectedAccessibleChildCount()
181 sal_Int64 nCount = 0;
182 sal_Int64 TotalCount = getAccessibleChildCount();
183 for( sal_Int64 i = 0; i < TotalCount; i++ )
184 if( isAccessibleChildSelected(i) ) nCount++;
186 return nCount;
189 uno::Reference<XAccessible> SAL_CALL SwAccessibleTextFrame::getSelectedAccessibleChild( sal_Int64 nSelectedChildIndex )
191 SolarMutexGuard g;
193 if ( nSelectedChildIndex > getSelectedAccessibleChildCount() )
194 throw lang::IndexOutOfBoundsException();
196 for(sal_Int64 i1 = 0, i2 = 0; i1 < getAccessibleChildCount(); i1++ )
197 if( isAccessibleChildSelected(i1) )
199 if( i2 == nSelectedChildIndex )
200 return getAccessibleChild( i1 );
201 i2++;
203 return uno::Reference<XAccessible>();
206 void SAL_CALL SwAccessibleTextFrame::deselectAccessibleChild( sal_Int64 )
208 SAL_WARN("sw.a11y", "SwAccessibleTextFrame::selectAllAccessibleChildren - missing implementation");
211 // #i73249#
212 OUString SAL_CALL SwAccessibleTextFrame::getAccessibleName()
214 SolarMutexGuard aGuard;
216 ThrowIfDisposed();
218 if ( !msTitle.isEmpty() )
220 return msTitle;
223 return SwAccessibleFrameBase::getAccessibleName();
226 OUString SAL_CALL SwAccessibleTextFrame::getAccessibleDescription()
228 SolarMutexGuard aGuard;
230 ThrowIfDisposed();
232 return msDesc;
236 OUString SAL_CALL SwAccessibleTextFrame::getImplementationName()
238 return u"com.sun.star.comp.Writer.SwAccessibleTextFrameView"_ustr;
241 sal_Bool SAL_CALL SwAccessibleTextFrame::supportsService(const OUString& sTestServiceName)
243 return cppu::supportsService(this, sTestServiceName);
246 uno::Sequence< OUString > SAL_CALL SwAccessibleTextFrame::getSupportedServiceNames()
248 return { u"com.sun.star.text.AccessibleTextFrameView"_ustr, sAccessibleServiceName };
251 uno::Sequence< sal_Int8 > SAL_CALL SwAccessibleTextFrame::getImplementationId()
253 return css::uno::Sequence<sal_Int8>();
256 // XAccessibleRelationSet
258 SwFlyFrame* SwAccessibleTextFrame::getFlyFrame() const
260 SwFlyFrame* pFlyFrame = nullptr;
262 const SwFrame* pFrame = GetFrame();
263 assert(pFrame);
264 if( pFrame->IsFlyFrame() )
266 pFlyFrame = static_cast<SwFlyFrame*>( const_cast<SwFrame*>( pFrame ) );
269 return pFlyFrame;
272 AccessibleRelation SwAccessibleTextFrame::makeRelation(AccessibleRelationType eType, const SwFlyFrame* pFrame )
274 uno::Sequence<uno::Reference<XAccessible>> aSequence { GetMap()->GetContext(pFrame) };
275 return AccessibleRelation(eType, aSequence);
278 uno::Reference<XAccessibleRelationSet> SAL_CALL SwAccessibleTextFrame::getAccessibleRelationSet( )
280 SolarMutexGuard aGuard;
282 ThrowIfDisposed();
284 // get the frame, and insert prev/next relations into helper
286 rtl::Reference<AccessibleRelationSetHelper> pHelper = new AccessibleRelationSetHelper();
288 SwFlyFrame* pFlyFrame = getFlyFrame();
289 assert(pFlyFrame);
291 const SwFlyFrame* pPrevFrame = pFlyFrame->GetPrevLink();
292 if( pPrevFrame != nullptr )
293 pHelper->AddRelation( makeRelation(
294 AccessibleRelationType_CONTENT_FLOWS_FROM, pPrevFrame ) );
296 const SwFlyFrame* pNextFrame = pFlyFrame->GetNextLink();
297 if( pNextFrame != nullptr )
298 pHelper->AddRelation( makeRelation(
299 AccessibleRelationType_CONTENT_FLOWS_TO, pNextFrame ) );
301 return pHelper;
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */