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 "AccessibleCell.hxx"
21 : #include "scitems.hxx"
22 : #include <editeng/eeitem.hxx>
23 :
24 :
25 : #include "AccessibleText.hxx"
26 : #include "AccessibleDocument.hxx"
27 : #include "tabvwsh.hxx"
28 : #include "document.hxx"
29 : #include "attrib.hxx"
30 : #include "miscuno.hxx"
31 : #include "editsrc.hxx"
32 : #include "dociter.hxx"
33 : #include "markdata.hxx"
34 : #include "cellvalue.hxx"
35 : #include "formulaiter.hxx"
36 :
37 : #include <unotools/accessiblestatesethelper.hxx>
38 : #include <com/sun/star/accessibility/AccessibleRole.hpp>
39 : #include <com/sun/star/accessibility/AccessibleStateType.hpp>
40 : #include <com/sun/star/accessibility/AccessibleRelationType.hpp>
41 : #include <com/sun/star/accessibility/XAccessibleTable.hpp>
42 : #include <editeng/brushitem.hxx>
43 : #include <comphelper/sequence.hxx>
44 : #include <float.h>
45 : #include <vcl/svapp.hxx>
46 :
47 : using namespace ::com::sun::star;
48 : using namespace ::com::sun::star::accessibility;
49 :
50 : //===== internal ============================================================
51 :
52 5 : ScAccessibleCell::ScAccessibleCell(
53 : const uno::Reference<XAccessible>& rxParent,
54 : ScTabViewShell* pViewShell,
55 : ScAddress& rCellAddress,
56 : sal_Int32 nIndex,
57 : ScSplitPos eSplitPos,
58 : ScAccessibleDocument* pAccDoc)
59 : :
60 : ScAccessibleCellBase(rxParent, GetDocument(pViewShell), rCellAddress, nIndex),
61 : ::accessibility::AccessibleStaticTextBase(CreateEditSource(pViewShell, rCellAddress, eSplitPos)),
62 : mpViewShell(pViewShell),
63 : mpAccDoc(pAccDoc),
64 5 : meSplitPos(eSplitPos)
65 : {
66 5 : if (pViewShell)
67 5 : pViewShell->AddAccessibilityObject(*this);
68 5 : }
69 :
70 15 : ScAccessibleCell::~ScAccessibleCell()
71 : {
72 5 : if (!ScAccessibleContextBase::IsDefunc() && !rBHelper.bInDispose)
73 : {
74 : // increment refcount to prevent double call off dtor
75 0 : osl_atomic_increment( &m_refCount );
76 : // call dispose to inform object which have a weak reference to this object
77 0 : dispose();
78 : }
79 10 : }
80 :
81 11 : void ScAccessibleCell::Init()
82 : {
83 11 : ScAccessibleCellBase::Init();
84 :
85 11 : SetEventSource(this);
86 11 : }
87 :
88 5 : void SAL_CALL ScAccessibleCell::disposing()
89 : {
90 5 : SolarMutexGuard aGuard;
91 : // dispose in AccessibleStaticTextBase
92 5 : Dispose();
93 :
94 5 : if (mpViewShell)
95 : {
96 5 : mpViewShell->RemoveAccessibilityObject(*this);
97 5 : mpViewShell = NULL;
98 : }
99 5 : mpAccDoc = NULL;
100 :
101 5 : ScAccessibleCellBase::disposing();
102 5 : }
103 :
104 : //===== XInterface =====================================================
105 :
106 397 : IMPLEMENT_FORWARD_XINTERFACE2( ScAccessibleCell, ScAccessibleCellBase, AccessibleStaticTextBase )
107 :
108 : //===== XTypeProvider ===================================================
109 :
110 0 : IMPLEMENT_FORWARD_XTYPEPROVIDER2( ScAccessibleCell, ScAccessibleCellBase, AccessibleStaticTextBase )
111 :
112 : //===== XAccessibleComponent ============================================
113 :
114 0 : uno::Reference< XAccessible > SAL_CALL ScAccessibleCell::getAccessibleAtPoint(
115 : const awt::Point& rPoint )
116 : throw (uno::RuntimeException)
117 : {
118 0 : return AccessibleStaticTextBase::getAccessibleAtPoint(rPoint);
119 : }
120 :
121 1 : void SAL_CALL ScAccessibleCell::grabFocus( )
122 : throw (uno::RuntimeException)
123 : {
124 1 : SolarMutexGuard aGuard;
125 1 : IsObjectValid();
126 1 : if (getAccessibleParent().is() && mpViewShell)
127 : {
128 1 : uno::Reference<XAccessibleComponent> xAccessibleComponent(getAccessibleParent()->getAccessibleContext(), uno::UNO_QUERY);
129 1 : if (xAccessibleComponent.is())
130 : {
131 1 : xAccessibleComponent->grabFocus();
132 1 : mpViewShell->SetCursor(maCellAddress.Col(), maCellAddress.Row());
133 1 : }
134 1 : }
135 1 : }
136 :
137 1 : Rectangle ScAccessibleCell::GetBoundingBoxOnScreen(void) const
138 : throw (uno::RuntimeException)
139 : {
140 1 : Rectangle aCellRect(GetBoundingBox());
141 1 : if (mpViewShell)
142 : {
143 1 : Window* pWindow = mpViewShell->GetWindowByPos(meSplitPos);
144 1 : if (pWindow)
145 : {
146 1 : Rectangle aRect = pWindow->GetWindowExtentsRelative(NULL);
147 1 : aCellRect.setX(aCellRect.getX() + aRect.getX());
148 1 : aCellRect.setY(aCellRect.getY() + aRect.getY());
149 : }
150 : }
151 1 : return aCellRect;
152 : }
153 :
154 263 : Rectangle ScAccessibleCell::GetBoundingBox(void) const
155 : throw (uno::RuntimeException)
156 : {
157 263 : Rectangle aCellRect;
158 263 : if (mpViewShell)
159 : {
160 : long nSizeX, nSizeY;
161 : mpViewShell->GetViewData()->GetMergeSizePixel(
162 263 : maCellAddress.Col(), maCellAddress.Row(), nSizeX, nSizeY);
163 263 : aCellRect.SetSize(Size(nSizeX, nSizeY));
164 263 : aCellRect.SetPos(mpViewShell->GetViewData()->GetScrPos(maCellAddress.Col(), maCellAddress.Row(), meSplitPos, sal_True));
165 :
166 263 : Window* pWindow = mpViewShell->GetWindowByPos(meSplitPos);
167 263 : if (pWindow)
168 : {
169 263 : Rectangle aRect(pWindow->GetWindowExtentsRelative(pWindow->GetAccessibleParentWindow()));
170 263 : aRect.Move(-aRect.Left(), -aRect.Top());
171 263 : aCellRect = aRect.Intersection(aCellRect);
172 : }
173 :
174 : /* #i19430# Gnopernicus reads text partly if it sticks out of the cell
175 : boundaries. This leads to wrong results in cases where the cell
176 : text is rotated, because rotation is not taken into account when
177 : calculating the visible part of the text. In these cases we will
178 : simply expand the cell size to the width of the unrotated text. */
179 263 : if (mpDoc)
180 : {
181 : const SfxInt32Item* pItem = static_cast< const SfxInt32Item* >(
182 263 : mpDoc->GetAttr( maCellAddress.Col(), maCellAddress.Row(), maCellAddress.Tab(), ATTR_ROTATE_VALUE ) );
183 263 : if( pItem && (pItem->GetValue() != 0) )
184 : {
185 0 : Rectangle aParaRect = GetParagraphBoundingBox();
186 0 : if( !aParaRect.IsEmpty() && (aCellRect.GetWidth() < aParaRect.GetWidth()) )
187 0 : aCellRect.SetSize( Size( aParaRect.GetWidth(), aCellRect.GetHeight() ) );
188 : }
189 : }
190 : }
191 263 : if (aCellRect.IsEmpty())
192 0 : aCellRect.SetPos(Point(-1, -1));
193 263 : return aCellRect;
194 : }
195 :
196 : //===== XAccessibleContext ==============================================
197 :
198 : sal_Int32 SAL_CALL
199 8 : ScAccessibleCell::getAccessibleChildCount(void)
200 : throw (uno::RuntimeException)
201 : {
202 8 : return AccessibleStaticTextBase::getAccessibleChildCount();
203 : }
204 :
205 : uno::Reference< XAccessible > SAL_CALL
206 0 : ScAccessibleCell::getAccessibleChild(sal_Int32 nIndex)
207 : throw (uno::RuntimeException,
208 : lang::IndexOutOfBoundsException)
209 : {
210 0 : return AccessibleStaticTextBase::getAccessibleChild(nIndex);
211 : }
212 :
213 : uno::Reference<XAccessibleStateSet> SAL_CALL
214 5 : ScAccessibleCell::getAccessibleStateSet(void)
215 : throw (uno::RuntimeException)
216 : {
217 5 : SolarMutexGuard aGuard;
218 10 : uno::Reference<XAccessibleStateSet> xParentStates;
219 5 : if (getAccessibleParent().is())
220 : {
221 5 : uno::Reference<XAccessibleContext> xParentContext = getAccessibleParent()->getAccessibleContext();
222 5 : xParentStates = xParentContext->getAccessibleStateSet();
223 : }
224 5 : utl::AccessibleStateSetHelper* pStateSet = new utl::AccessibleStateSetHelper();
225 5 : if (IsDefunc(xParentStates))
226 0 : pStateSet->AddState(AccessibleStateType::DEFUNC);
227 : else
228 : {
229 5 : if (IsEditable(xParentStates))
230 : {
231 5 : pStateSet->AddState(AccessibleStateType::EDITABLE);
232 5 : pStateSet->AddState(AccessibleStateType::RESIZABLE);
233 : }
234 5 : pStateSet->AddState(AccessibleStateType::ENABLED);
235 5 : pStateSet->AddState(AccessibleStateType::MULTI_LINE);
236 5 : pStateSet->AddState(AccessibleStateType::MULTI_SELECTABLE);
237 5 : if (IsOpaque(xParentStates))
238 0 : pStateSet->AddState(AccessibleStateType::OPAQUE);
239 5 : pStateSet->AddState(AccessibleStateType::SELECTABLE);
240 5 : if (IsSelected())
241 0 : pStateSet->AddState(AccessibleStateType::SELECTED);
242 5 : if (isShowing())
243 5 : pStateSet->AddState(AccessibleStateType::SHOWING);
244 5 : pStateSet->AddState(AccessibleStateType::TRANSIENT);
245 5 : if (isVisible())
246 5 : pStateSet->AddState(AccessibleStateType::VISIBLE);
247 : }
248 10 : return pStateSet;
249 : }
250 :
251 : uno::Reference<XAccessibleRelationSet> SAL_CALL
252 1 : ScAccessibleCell::getAccessibleRelationSet(void)
253 : throw (uno::RuntimeException)
254 : {
255 1 : SolarMutexGuard aGuard;
256 1 : IsObjectValid();
257 1 : utl::AccessibleRelationSetHelper* pRelationSet = NULL;
258 1 : if (mpAccDoc)
259 1 : pRelationSet = mpAccDoc->GetRelationSet(&maCellAddress);
260 1 : if (!pRelationSet)
261 1 : pRelationSet = new utl::AccessibleRelationSetHelper();
262 1 : FillDependends(pRelationSet);
263 1 : FillPrecedents(pRelationSet);
264 1 : return pRelationSet;
265 : }
266 :
267 : //===== XServiceInfo ====================================================
268 :
269 5 : OUString SAL_CALL ScAccessibleCell::getImplementationName(void)
270 : throw (uno::RuntimeException)
271 : {
272 5 : return OUString("ScAccessibleCell");
273 : }
274 :
275 : uno::Sequence< OUString> SAL_CALL
276 0 : ScAccessibleCell::getSupportedServiceNames(void)
277 : throw (uno::RuntimeException)
278 : {
279 0 : uno::Sequence< OUString > aSequence = ScAccessibleContextBase::getSupportedServiceNames();
280 0 : sal_Int32 nOldSize(aSequence.getLength());
281 0 : aSequence.realloc(nOldSize + 1);
282 0 : OUString* pNames = aSequence.getArray();
283 :
284 0 : pNames[nOldSize] = OUString("com.sun.star.sheet.AccessibleCell");
285 :
286 0 : return aSequence;
287 : }
288 :
289 : //==== internal =========================================================
290 :
291 5 : sal_Bool ScAccessibleCell::IsDefunc(
292 : const uno::Reference<XAccessibleStateSet>& rxParentStates)
293 : {
294 20 : return ScAccessibleContextBase::IsDefunc() || (mpDoc == NULL) || (mpViewShell == NULL) || !getAccessibleParent().is() ||
295 20 : (rxParentStates.is() && rxParentStates->contains(AccessibleStateType::DEFUNC));
296 : }
297 :
298 10 : sal_Bool ScAccessibleCell::IsEditable(
299 : const uno::Reference<XAccessibleStateSet>& rxParentStates)
300 : {
301 10 : sal_Bool bEditable(sal_True);
302 10 : if (rxParentStates.is() && !rxParentStates->contains(AccessibleStateType::EDITABLE) &&
303 : mpDoc)
304 : {
305 : // here I have to test whether the protection of the table should influence this cell.
306 : const ScProtectionAttr* pItem = (const ScProtectionAttr*)mpDoc->GetAttr(
307 0 : maCellAddress.Col(), maCellAddress.Row(),
308 0 : maCellAddress.Tab(), ATTR_PROTECTION);
309 0 : if (pItem)
310 0 : bEditable = !pItem->GetProtection();
311 : }
312 10 : return bEditable;
313 : }
314 :
315 5 : sal_Bool ScAccessibleCell::IsOpaque(
316 : const uno::Reference<XAccessibleStateSet>& /* rxParentStates */)
317 : {
318 : // test whether there is a background color
319 5 : sal_Bool bOpaque(sal_True);
320 5 : if (mpDoc)
321 : {
322 : const SvxBrushItem* pItem = (const SvxBrushItem*)mpDoc->GetAttr(
323 5 : maCellAddress.Col(), maCellAddress.Row(),
324 10 : maCellAddress.Tab(), ATTR_BACKGROUND);
325 5 : if (pItem)
326 5 : bOpaque = pItem->GetColor() != COL_TRANSPARENT;
327 : }
328 5 : return bOpaque;
329 : }
330 :
331 5 : sal_Bool ScAccessibleCell::IsSelected()
332 : {
333 5 : sal_Bool bResult(false);
334 5 : if (mpViewShell && mpViewShell->GetViewData())
335 : {
336 5 : const ScMarkData& rMarkdata = mpViewShell->GetViewData()->GetMarkData();
337 5 : bResult = rMarkdata.IsCellMarked(maCellAddress.Col(), maCellAddress.Row());
338 : }
339 5 : return bResult;
340 : }
341 :
342 5 : ScDocument* ScAccessibleCell::GetDocument(ScTabViewShell* pViewShell)
343 : {
344 5 : ScDocument* pDoc = NULL;
345 5 : if (pViewShell && pViewShell->GetViewData())
346 5 : pDoc = pViewShell->GetViewData()->GetDocument();
347 5 : return pDoc;
348 : }
349 :
350 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
351 5 : ::std::auto_ptr< SvxEditSource > ScAccessibleCell::CreateEditSource(ScTabViewShell* pViewShell, ScAddress aCell, ScSplitPos eSplitPos)
352 : {
353 : ::std::auto_ptr < ScAccessibleTextData > pAccessibleCellTextData
354 5 : ( new ScAccessibleCellTextData( pViewShell, aCell, eSplitPos, this ) );
355 5 : ::std::auto_ptr< SvxEditSource > pEditSource (new ScAccessibilityEditSource(pAccessibleCellTextData));
356 :
357 5 : return pEditSource;
358 : }
359 : SAL_WNODEPRECATED_DECLARATIONS_POP
360 :
361 1 : void ScAccessibleCell::FillDependends(utl::AccessibleRelationSetHelper* pRelationSet)
362 : {
363 1 : if (mpDoc)
364 : {
365 1 : ScRange aRange(0, 0, maCellAddress.Tab(), MAXCOL, MAXROW, maCellAddress.Tab());
366 1 : ScCellIterator aCellIter(mpDoc, aRange);
367 :
368 2 : for (bool bHasCell = aCellIter.first(); bHasCell; bHasCell = aCellIter.next())
369 : {
370 1 : if (aCellIter.getType() == CELLTYPE_FORMULA)
371 : {
372 0 : bool bFound = false;
373 0 : ScDetectiveRefIter aIter(aCellIter.getFormulaCell());
374 0 : ScRange aRef;
375 0 : while ( !bFound && aIter.GetNextRef( aRef ) )
376 : {
377 0 : if (aRef.In(maCellAddress))
378 0 : bFound = true;
379 : }
380 0 : if (bFound)
381 0 : AddRelation(aCellIter.GetPos(), AccessibleRelationType::CONTROLLER_FOR, pRelationSet);
382 : }
383 1 : }
384 : }
385 1 : }
386 :
387 1 : void ScAccessibleCell::FillPrecedents(utl::AccessibleRelationSetHelper* pRelationSet)
388 : {
389 1 : if (mpDoc && mpDoc->GetCellType(maCellAddress) == CELLTYPE_FORMULA)
390 : {
391 0 : ScDetectiveRefIter aIter(mpDoc->GetFormulaCell(maCellAddress));
392 0 : ScRange aRef;
393 0 : while ( aIter.GetNextRef( aRef ) )
394 : {
395 0 : AddRelation( aRef, AccessibleRelationType::CONTROLLED_BY, pRelationSet);
396 : }
397 : }
398 1 : }
399 :
400 0 : void ScAccessibleCell::AddRelation(const ScAddress& rCell,
401 : const sal_uInt16 aRelationType,
402 : utl::AccessibleRelationSetHelper* pRelationSet)
403 : {
404 0 : AddRelation(ScRange(rCell, rCell), aRelationType, pRelationSet);
405 0 : }
406 :
407 0 : void ScAccessibleCell::AddRelation(const ScRange& rRange,
408 : const sal_uInt16 aRelationType,
409 : utl::AccessibleRelationSetHelper* pRelationSet)
410 : {
411 0 : uno::Reference < XAccessibleTable > xTable ( getAccessibleParent()->getAccessibleContext(), uno::UNO_QUERY );
412 0 : if (xTable.is())
413 : {
414 0 : sal_uInt32 nCount(static_cast<sal_uInt32>(rRange.aEnd.Col() -
415 0 : rRange.aStart.Col() + 1) * (rRange.aEnd.Row() -
416 0 : rRange.aStart.Row() + 1));
417 0 : uno::Sequence < uno::Reference < uno::XInterface > > aTargetSet( nCount );
418 0 : uno::Reference < uno::XInterface >* pTargetSet = aTargetSet.getArray();
419 0 : if (pTargetSet)
420 : {
421 0 : sal_uInt32 nPos(0);
422 0 : for (sal_uInt32 nRow = rRange.aStart.Row(); nRow <= sal::static_int_cast<sal_uInt32>(rRange.aEnd.Row()); ++nRow)
423 : {
424 0 : for (sal_uInt32 nCol = rRange.aStart.Col(); nCol <= sal::static_int_cast<sal_uInt32>(rRange.aEnd.Col()); ++nCol)
425 : {
426 0 : pTargetSet[nPos] = xTable->getAccessibleCellAt(nRow, nCol);
427 0 : ++nPos;
428 : }
429 : }
430 : OSL_ENSURE(nCount == nPos, "something wents wrong");
431 : }
432 0 : AccessibleRelation aRelation;
433 0 : aRelation.RelationType = aRelationType;
434 0 : aRelation.TargetSet = aTargetSet;
435 0 : pRelationSet->AddRelation(aRelation);
436 0 : }
437 93 : }
438 :
439 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|