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/AccessibleStateType.hpp>
21 : #include <com/sun/star/accessibility/AccessibleEventId.hpp>
22 : #include <unotools/accessiblestatesethelper.hxx>
23 : #include <osl/mutex.hxx>
24 : #include <vcl/svapp.hxx>
25 : #include <vcl/window.hxx>
26 : #include <frmfmt.hxx>
27 : #include <ndnotxt.hxx>
28 : #include <flyfrm.hxx>
29 : #include <cntfrm.hxx>
30 : #include <fmtcntnt.hxx>
31 : #include <ndindex.hxx>
32 : #include "fesh.hxx"
33 : #include <hints.hxx>
34 : #include "accmap.hxx"
35 : #include "accframebase.hxx"
36 :
37 : #include <crsrsh.hxx>
38 : #include <txtfrm.hxx>
39 : #include <ndtxt.hxx>
40 : #include <dcontact.hxx>
41 : #include <fmtanchr.hxx>
42 :
43 : using namespace ::com::sun::star;
44 : using namespace ::com::sun::star::accessibility;
45 :
46 52 : bool SwAccessibleFrameBase::IsSelected()
47 : {
48 52 : bool bRet = false;
49 :
50 : OSL_ENSURE( GetMap(), "no map?" );
51 52 : const SwViewShell *pVSh = GetMap()->GetShell();
52 : OSL_ENSURE( pVSh, "no shell?" );
53 52 : if( pVSh->ISA( SwFEShell ) )
54 : {
55 52 : const SwFEShell *pFESh = static_cast< const SwFEShell * >( pVSh );
56 52 : const SwFrm *pFlyFrm = pFESh->GetCurrFlyFrm();
57 52 : if( pFlyFrm == GetFrm() )
58 0 : bRet = true;
59 : }
60 :
61 52 : return bRet;
62 : }
63 :
64 4 : void SwAccessibleFrameBase::GetStates(
65 : ::utl::AccessibleStateSetHelper& rStateSet )
66 : {
67 4 : SwAccessibleContext::GetStates( rStateSet );
68 :
69 4 : const SwViewShell *pVSh = GetMap()->GetShell();
70 : OSL_ENSURE( pVSh, "no shell?" );
71 4 : bool bSelectable = pVSh->ISA( SwFEShell );
72 :
73 : // SELECTABLE
74 4 : if( bSelectable )
75 4 : rStateSet.AddState( AccessibleStateType::SELECTABLE );
76 :
77 : // FOCUSABLE
78 4 : if( bSelectable )
79 4 : rStateSet.AddState( AccessibleStateType::FOCUSABLE );
80 :
81 : // SELECTED and FOCUSED
82 4 : if( IsSelected() )
83 : {
84 0 : rStateSet.AddState( AccessibleStateType::SELECTED );
85 : OSL_ENSURE( bIsSelected, "bSelected out of sync" );
86 0 : ::rtl::Reference < SwAccessibleContext > xThis( this );
87 0 : GetMap()->SetCursorContext( xThis );
88 :
89 0 : vcl::Window *pWin = GetWindow();
90 0 : if( pWin && pWin->HasFocus() )
91 0 : rStateSet.AddState( AccessibleStateType::FOCUSED );
92 : }
93 4 : if( GetSelectedState() )
94 0 : rStateSet.AddState( AccessibleStateType::SELECTED );
95 4 : }
96 :
97 2 : sal_uInt8 SwAccessibleFrameBase::GetNodeType( const SwFlyFrm *pFlyFrm )
98 : {
99 2 : sal_uInt8 nType = ND_TEXTNODE;
100 2 : if( pFlyFrm->Lower() )
101 : {
102 2 : if( pFlyFrm->Lower()->IsNoTxtFrm() )
103 : {
104 : const SwCntntFrm *pCntFrm =
105 2 : static_cast<const SwCntntFrm *>( pFlyFrm->Lower() );
106 2 : nType = pCntFrm->GetNode()->GetNodeType();
107 : }
108 : }
109 : else
110 : {
111 0 : const SwFrmFmt *pFrmFmt = pFlyFrm->GetFmt();
112 0 : const SwFmtCntnt& rCntnt = pFrmFmt->GetCntnt();
113 0 : const SwNodeIndex *pNdIdx = rCntnt.GetCntntIdx();
114 0 : if( pNdIdx )
115 : {
116 : const SwCntntNode *pCNd =
117 0 : (pNdIdx->GetNodes())[pNdIdx->GetIndex()+1]->GetCntntNode();
118 0 : if( pCNd )
119 0 : nType = pCNd->GetNodeType();
120 : }
121 : }
122 :
123 2 : return nType;
124 : }
125 :
126 2 : SwAccessibleFrameBase::SwAccessibleFrameBase(
127 : SwAccessibleMap* pInitMap,
128 : sal_Int16 nInitRole,
129 : const SwFlyFrm* pFlyFrm ) :
130 : SwAccessibleContext( pInitMap, nInitRole, pFlyFrm ),
131 2 : bIsSelected( false )
132 : {
133 2 : SolarMutexGuard aGuard;
134 :
135 2 : const SwFrmFmt *pFrmFmt = pFlyFrm->GetFmt();
136 2 : const_cast< SwFrmFmt * >( pFrmFmt )->Add( this );
137 :
138 2 : SetName( pFrmFmt->GetName() );
139 :
140 2 : bIsSelected = IsSelected();
141 2 : }
142 :
143 0 : void SwAccessibleFrameBase::_InvalidateCursorPos()
144 : {
145 0 : bool bNewSelected = IsSelected();
146 : bool bOldSelected;
147 :
148 : {
149 0 : osl::MutexGuard aGuard( aMutex );
150 0 : bOldSelected = bIsSelected;
151 0 : bIsSelected = bNewSelected;
152 : }
153 :
154 0 : if( bNewSelected )
155 : {
156 : // remember that object as the one that has the caret. This is
157 : // necessary to notify that object if the cursor leaves it.
158 0 : ::rtl::Reference < SwAccessibleContext > xThis( this );
159 0 : GetMap()->SetCursorContext( xThis );
160 : }
161 :
162 0 : if( bOldSelected != bNewSelected )
163 : {
164 0 : vcl::Window *pWin = GetWindow();
165 0 : if( pWin && pWin->HasFocus() && bNewSelected )
166 0 : FireStateChangedEvent( AccessibleStateType::FOCUSED, bNewSelected );
167 0 : if( pWin && pWin->HasFocus() && !bNewSelected )
168 0 : FireStateChangedEvent( AccessibleStateType::FOCUSED, bNewSelected );
169 0 : if(bNewSelected)
170 : {
171 0 : uno::Reference< XAccessible > xParent( GetWeakParent() );
172 0 : if( xParent.is() )
173 : {
174 : SwAccessibleContext *pAcc =
175 0 : static_cast <SwAccessibleContext *>( xParent.get() );
176 :
177 0 : AccessibleEventObject aEvent;
178 0 : aEvent.EventId = AccessibleEventId::SELECTION_CHANGED;
179 0 : uno::Reference< XAccessible > xChild(this);
180 0 : aEvent.NewValue <<= xChild;
181 0 : pAcc->FireAccessibleEvent( aEvent );
182 0 : }
183 : }
184 : }
185 0 : }
186 :
187 0 : void SwAccessibleFrameBase::_InvalidateFocus()
188 : {
189 0 : vcl::Window *pWin = GetWindow();
190 0 : if( pWin )
191 : {
192 : bool bSelected;
193 :
194 : {
195 0 : osl::MutexGuard aGuard( aMutex );
196 0 : bSelected = bIsSelected;
197 : }
198 : OSL_ENSURE( bSelected, "focus object should be selected" );
199 :
200 : FireStateChangedEvent( AccessibleStateType::FOCUSED,
201 0 : pWin->HasFocus() && bSelected );
202 : }
203 0 : }
204 :
205 2 : bool SwAccessibleFrameBase::HasCursor()
206 : {
207 2 : osl::MutexGuard aGuard( aMutex );
208 2 : return bIsSelected;
209 : }
210 :
211 2 : SwAccessibleFrameBase::~SwAccessibleFrameBase()
212 : {
213 2 : }
214 :
215 0 : void SwAccessibleFrameBase::Modify( const SfxPoolItem* pOld, const SfxPoolItem *pNew)
216 : {
217 0 : sal_uInt16 nWhich = pOld ? pOld->Which() : pNew ? pNew->Which() : 0 ;
218 0 : const SwFlyFrm *pFlyFrm = static_cast< const SwFlyFrm * >( GetFrm() );
219 0 : switch( nWhich )
220 : {
221 : case RES_NAME_CHANGED:
222 0 : if( pFlyFrm )
223 : {
224 0 : const SwFrmFmt *pFrmFmt = pFlyFrm->GetFmt();
225 : OSL_ENSURE( pFrmFmt == GetRegisteredIn(), "invalid frame" );
226 :
227 0 : const OUString sOldName( GetName() );
228 : OSL_ENSURE( !pOld ||
229 : static_cast < const SwStringMsgPoolItem * >( pOld )->GetString() == GetName(),
230 : "invalid old name" );
231 :
232 0 : SetName( pFrmFmt->GetName() );
233 : OSL_ENSURE( !pNew ||
234 : static_cast < const SwStringMsgPoolItem * >( pNew )->GetString() == GetName(),
235 : "invalid new name" );
236 :
237 0 : if( sOldName != GetName() )
238 : {
239 0 : AccessibleEventObject aEvent;
240 0 : aEvent.EventId = AccessibleEventId::NAME_CHANGED;
241 0 : aEvent.OldValue <<= sOldName;
242 0 : aEvent.NewValue <<= GetName();
243 0 : FireAccessibleEvent( aEvent );
244 0 : }
245 : }
246 0 : break;
247 : case RES_OBJECTDYING:
248 : // mba: it seems that this class intentionally does not call code in base class SwClient
249 0 : if( pOld && ( GetRegisteredIn() == static_cast< SwModify *>( static_cast< const SwPtrMsgPoolItem * >( pOld )->pObject ) ) )
250 0 : GetRegisteredInNonConst()->Remove( this );
251 0 : break;
252 :
253 : case RES_FMT_CHG:
254 0 : if( pOld &&
255 0 : static_cast< const SwFmtChg * >(pNew)->pChangedFmt == GetRegisteredIn() &&
256 0 : static_cast< const SwFmtChg * >(pOld)->pChangedFmt->IsFmtInDTOR() )
257 0 : GetRegisteredInNonConst()->Remove( this );
258 0 : break;
259 :
260 : default:
261 : // mba: former call to base class method removed as it is meant to handle only RES_OBJECTDYING
262 0 : break;
263 : }
264 0 : }
265 :
266 2 : void SwAccessibleFrameBase::Dispose( bool bRecursive )
267 : {
268 2 : SolarMutexGuard aGuard;
269 :
270 2 : if( GetRegisteredIn() )
271 2 : GetRegisteredInNonConst()->Remove( this );
272 :
273 2 : SwAccessibleContext::Dispose( bRecursive );
274 2 : }
275 :
276 : //Get the selection cursor of the document.
277 50 : SwPaM* SwAccessibleFrameBase::GetCrsr()
278 : {
279 : // get the cursor shell; if we don't have any, we don't have a
280 : // cursor/selection either
281 50 : SwPaM* pCrsr = NULL;
282 50 : SwCrsrShell* pCrsrShell = GetCrsrShell();
283 50 : if( pCrsrShell != NULL && !pCrsrShell->IsTableMode() )
284 : {
285 50 : SwFEShell *pFESh = pCrsrShell->ISA( SwFEShell )
286 50 : ? static_cast< SwFEShell * >( pCrsrShell ) : 0;
287 150 : if( !pFESh ||
288 100 : !(pFESh->IsFrmSelected() || pFESh->IsObjSelected() > 0) )
289 : {
290 : // get the selection, and test whether it affects our text node
291 50 : pCrsr = pCrsrShell->GetCrsr( false /* ??? */ );
292 : }
293 : }
294 :
295 50 : return pCrsr;
296 : }
297 :
298 : //Return the selected state of the object.
299 : //when the object's anchor are in the selection cursor, we should return true.
300 50 : bool SwAccessibleFrameBase::GetSelectedState( )
301 : {
302 50 : SolarMutexGuard aGuard;
303 :
304 50 : if(GetMap()->IsDocumentSelAll())
305 : {
306 0 : return true;
307 : }
308 :
309 : // SELETED.
310 50 : SwFlyFrm* pFlyFrm = getFlyFrm();
311 50 : const SwFrmFmt *pFrmFmt = pFlyFrm->GetFmt();
312 50 : const SwFmtAnchor& pAnchor = pFrmFmt->GetAnchor();
313 50 : const SwPosition *pPos = pAnchor.GetCntntAnchor();
314 50 : if( !pPos )
315 0 : return false;
316 50 : int pIndex = pPos->nContent.GetIndex();
317 50 : if( pPos->nNode.GetNode().GetTxtNode() )
318 : {
319 50 : SwPaM* pCrsr = GetCrsr();
320 50 : if( pCrsr != NULL )
321 : {
322 50 : const SwTxtNode* pNode = pPos->nNode.GetNode().GetTxtNode();
323 50 : sal_uLong nHere = pNode->GetIndex();
324 :
325 : // iterate over ring
326 50 : SwPaM* pRingStart = pCrsr;
327 50 : do
328 : {
329 : // ignore, if no mark
330 50 : if( pCrsr->HasMark() )
331 : {
332 : // check whether nHere is 'inside' pCrsr
333 0 : SwPosition* pStart = pCrsr->Start();
334 0 : sal_uLong nStartIndex = pStart->nNode.GetIndex();
335 0 : SwPosition* pEnd = pCrsr->End();
336 0 : sal_uLong nEndIndex = pEnd->nNode.GetIndex();
337 0 : if( ( nHere >= nStartIndex ) && (nHere <= nEndIndex) )
338 : {
339 0 : if( pAnchor.GetAnchorId() == FLY_AS_CHAR )
340 : {
341 0 : if( ((nHere == nStartIndex) && (pIndex >= pStart->nContent.GetIndex())) || (nHere > nStartIndex) )
342 0 : if( ((nHere == nEndIndex) && (pIndex < pEnd->nContent.GetIndex())) || (nHere < nEndIndex) )
343 0 : return true;
344 : }
345 0 : else if( pAnchor.GetAnchorId() == FLY_AT_PARA )
346 : {
347 0 : if( ((nHere > nStartIndex) || pStart->nContent.GetIndex() ==0 )
348 0 : && (nHere < nEndIndex ) )
349 0 : return true;
350 : }
351 0 : break;
352 : }
353 : // else: this PaM doesn't point to this paragraph
354 : }
355 : // else: this PaM is collapsed and doesn't select anything
356 :
357 : // next PaM in ring
358 50 : pCrsr = static_cast<SwPaM*>( pCrsr->GetNext() );
359 : }
360 : while( pCrsr != pRingStart );
361 : }
362 : }
363 50 : return false;
364 : }
365 :
366 50 : SwFlyFrm* SwAccessibleFrameBase::getFlyFrm() const
367 : {
368 50 : SwFlyFrm* pFlyFrm = NULL;
369 :
370 50 : const SwFrm* pFrm = GetFrm();
371 : DBG_ASSERT( pFrm != NULL, "frame expected" );
372 50 : if( pFrm->IsFlyFrm() )
373 : {
374 50 : pFlyFrm = static_cast<SwFlyFrm*>( const_cast<SwFrm*>( pFrm ) );
375 : }
376 :
377 50 : return pFlyFrm;
378 : }
379 :
380 46 : bool SwAccessibleFrameBase::SetSelectedState( bool )
381 : {
382 46 : bool bParaSeleted = GetSelectedState() || IsSelected();
383 :
384 46 : if(bIsSeletedInDoc != bParaSeleted)
385 : {
386 0 : bIsSeletedInDoc = bParaSeleted;
387 0 : FireStateChangedEvent( AccessibleStateType::SELECTED, bParaSeleted );
388 0 : return true;
389 : }
390 46 : return false;
391 270 : }
392 :
393 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|