xref: /trunk/main/sc/source/ui/Accessibility/AccessibleDocument.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 
28 #include "AccessibleDocument.hxx"
29 #include "AccessibleSpreadsheet.hxx"
30 #include "tabvwsh.hxx"
31 #include "AccessibilityHints.hxx"
32 #include "document.hxx"
33 #include "drwlayer.hxx"
34 #include "unoguard.hxx"
35 #include "shapeuno.hxx"
36 #include "DrawModelBroadcaster.hxx"
37 #include "drawview.hxx"
38 #include "gridwin.hxx"
39 #include "AccessibleEditObject.hxx"
40 #include "scresid.hxx"
41 #ifndef SC_SC_HRC
42 #include "sc.hrc"
43 #endif
44 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
45 #ifndef _COM_SUN_STAR_ACCESSIBILITY_XACCESSIBLESTATETYPE_HPP_
46 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
47 #endif
48 #ifndef _COM_SUN_STAR_ACCESSIBILITY_XACCESSIBLERELATIONTYPE_HPP_
49 #include <com/sun/star/accessibility/AccessibleRelationType.hpp>
50 #endif
51 #include <com/sun/star/view/XSelectionSupplier.hpp>
52 #include <com/sun/star/drawing/XShape.hpp>
53 #include <com/sun/star/drawing/XShapes.hpp>
54 
55 #ifndef _UTL_ACCESSIBLESTATESETHELPER_HXX
56 #include <unotools/accessiblestatesethelper.hxx>
57 #endif
58 #include <tools/debug.hxx>
59 #include <tools/gen.hxx>
60 #include <svx/svdpage.hxx>
61 #include <svx/svdobj.hxx>
62 #include <svx/ShapeTypeHandler.hxx>
63 #include <svx/AccessibleShape.hxx>
64 #include <svx/AccessibleShapeTreeInfo.hxx>
65 #include <svx/AccessibleShapeInfo.hxx>
66 #include <comphelper/sequence.hxx>
67 #include <sfx2/viewfrm.hxx>
68 #include <svx/unoshcol.hxx>
69 #include <svx/unoshape.hxx>
70 #include <unotools/accessiblerelationsethelper.hxx>
71 #include <toolkit/helper/convert.hxx>
72 
73 #include <svx/AccessibleControlShape.hxx>
74 #include <svx/AccessibleShape.hxx>
75 #include <svx/ShapeTypeHandler.hxx>
76 #include <svx/SvxShapeTypes.hxx>
77 #include <sfx2/objsh.hxx>
78 #include <editeng/editview.hxx>
79 #include <editeng/editeng.hxx>
80 #include <list>
81 #include <algorithm>
82 #include "AccessibleCell.hxx"
83 
84 #include "svx/unoapi.hxx"
85 #include "scmod.hxx"
86 using namespace ::com::sun::star;
87 using namespace ::com::sun::star::accessibility;
88 using ::std::for_each;
89 
90     //=====  internal  ========================================================
91 
92 struct ScAccessibleShapeData
93 {
ScAccessibleShapeDataScAccessibleShapeData94     ScAccessibleShapeData() : pAccShape(NULL), pRelationCell(NULL), bSelected(sal_False), bSelectable(sal_True) {}
95     ~ScAccessibleShapeData();
96     mutable ::accessibility::AccessibleShape* pAccShape;
97     mutable ScAddress*          pRelationCell; // if it is NULL this shape is anchored on the table
98 //    SdrObject*                  pShape;
99     com::sun::star::uno::Reference< com::sun::star::drawing::XShape > xShape;
100     mutable sal_Bool            bSelected;
101     sal_Bool                    bSelectable;
102 };
103 
~ScAccessibleShapeData()104 ScAccessibleShapeData::~ScAccessibleShapeData()
105 {
106     if (pAccShape)
107     {
108         pAccShape->dispose();
109         pAccShape->release();
110     }
111 }
112 
113 struct ScShapeDataLess
114 {
115     rtl::OUString msLayerId;
116     rtl::OUString msZOrder;
ScShapeDataLessScShapeDataLess117     ScShapeDataLess()
118         : msLayerId(RTL_CONSTASCII_USTRINGPARAM( "LayerID" )),
119         msZOrder(RTL_CONSTASCII_USTRINGPARAM( "ZOrder" ))
120     {
121     }
ConvertLayerIdScShapeDataLess122     void ConvertLayerId(sal_Int16& rLayerID) const // changes the number of the LayerId so it the accessibility order
123     {
124         switch (rLayerID)
125         {
126         case SC_LAYER_FRONT:
127             rLayerID = 1;
128             break;
129         case SC_LAYER_BACK:
130             rLayerID = 0;
131             break;
132         case SC_LAYER_INTERN:
133             rLayerID = 2;
134             break;
135         case SC_LAYER_CONTROLS:
136             rLayerID = 3;
137             break;
138         }
139     }
LessThanSheetScShapeDataLess140     sal_Bool LessThanSheet(const ScAccessibleShapeData* pData) const
141     {
142         sal_Bool bResult(sal_False);
143         uno::Reference< beans::XPropertySet> xProps(pData->xShape, uno::UNO_QUERY);
144         if (xProps.is())
145         {
146             uno::Any aPropAny = xProps->getPropertyValue(msLayerId);
147             sal_Int16 nLayerID = 0;
148             if( (aPropAny >>= nLayerID) )
149             {
150                 if (nLayerID == SC_LAYER_BACK)
151                     bResult = sal_True;
152             }
153         }
154         return bResult;
155     }
operator ()ScShapeDataLess156     sal_Bool operator()(const ScAccessibleShapeData* pData1, const ScAccessibleShapeData* pData2) const
157     {
158         sal_Bool bResult(sal_False);
159         if (pData1 && pData2)
160         {
161             uno::Reference< beans::XPropertySet> xProps1(pData1->xShape, uno::UNO_QUERY);
162             uno::Reference< beans::XPropertySet> xProps2(pData2->xShape, uno::UNO_QUERY);
163             if (xProps1.is() && xProps2.is())
164             {
165                 uno::Any aPropAny1 = xProps1->getPropertyValue(msLayerId);
166                 uno::Any aPropAny2 = xProps2->getPropertyValue(msLayerId);
167                 sal_Int16 nLayerID1(0);
168                 sal_Int16 nLayerID2(0);
169                 if( (aPropAny1 >>= nLayerID1) && (aPropAny2 >>= nLayerID2) )
170                 {
171                     if (nLayerID1 == nLayerID2)
172                     {
173                         uno::Any aAny1 = xProps1->getPropertyValue(msZOrder);
174                         sal_Int32 nZOrder1 = 0;
175                         uno::Any aAny2 = xProps2->getPropertyValue(msZOrder);
176                         sal_Int32 nZOrder2 = 0;
177                         if ( (aAny1 >>= nZOrder1) && (aAny2 >>= nZOrder2) )
178                             bResult = (nZOrder1 < nZOrder2);
179                     }
180                     else
181                     {
182                         ConvertLayerId(nLayerID1);
183                         ConvertLayerId(nLayerID2);
184                         bResult = (nLayerID1 < nLayerID2);
185                     }
186                 }
187             }
188         }
189         else if (pData1 && !pData2)
190             bResult = LessThanSheet(pData1);
191         else if (!pData1 && pData2)
192             bResult = !LessThanSheet(pData2);
193         else
194             bResult = sal_False;
195         return bResult;
196     }
197 };
198 
199 struct DeselectShape
200 {
operator ()DeselectShape201     void operator() (const ScAccessibleShapeData* pAccShapeData) const
202     {
203         if (pAccShapeData)
204         {
205             pAccShapeData->bSelected = sal_False;
206             if (pAccShapeData->pAccShape)
207                 pAccShapeData->pAccShape->ResetState(AccessibleStateType::SELECTED);
208         }
209     }
210 };
211 
212 struct SelectShape
213 {
214     uno::Reference < drawing::XShapes > xShapes;
SelectShapeSelectShape215     SelectShape(uno::Reference<drawing::XShapes>& xTemp) : xShapes(xTemp) {}
operator ()SelectShape216     void operator() (const ScAccessibleShapeData* pAccShapeData) const
217     {
218         if (pAccShapeData && pAccShapeData->bSelectable)
219         {
220             pAccShapeData->bSelected = sal_True;
221             if (pAccShapeData->pAccShape)
222                 pAccShapeData->pAccShape->SetState(AccessibleStateType::SELECTED);
223             if (xShapes.is())
224                 xShapes->add(pAccShapeData->xShape);
225         }
226     }
227 };
228 
229 struct Destroy
230 {
operator ()Destroy231     void operator() (ScAccessibleShapeData* pData)
232     {
233         if (pData)
234             DELETEZ(pData);
235     }
236 };
237 
238 class ScChildrenShapes : public SfxListener,
239     public ::accessibility::IAccessibleParent
240 {
241 public:
242     ScChildrenShapes(ScAccessibleDocument* pAccessibleDocument, ScTabViewShell* pViewShell, ScSplitPos eSplitPos);
243     ~ScChildrenShapes();
244 
245     ///=====  SfxListener  =====================================================
246 
247     virtual void Notify( SfxBroadcaster& rBC, const SfxHint& rHint );
248 
249     ///=====  IAccessibleParent  ===============================================
250 
251     virtual sal_Bool ReplaceChild (
252         ::accessibility::AccessibleShape* pCurrentChild,
253         const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape >& _rxShape,
254         const long _nIndex,
255         const ::accessibility::AccessibleShapeTreeInfo& _rShapeTreeInfo
256     );
257 
258     virtual ::accessibility::AccessibleControlShape* GetAccControlShapeFromModel
259         (::com::sun::star::beans::XPropertySet* pSet);
260     virtual  ::com::sun::star::uno::Reference<
261             ::com::sun::star::accessibility::XAccessible>
262         GetAccessibleCaption (const ::com::sun::star::uno::Reference<
263             ::com::sun::star::drawing::XShape>& xShape);
264     ///=====  Internal  ========================================================
265     void SetDrawBroadcaster();
266 
267     sal_Int32 GetCount() const;
268     uno::Reference< XAccessible > Get(const ScAccessibleShapeData* pData) const;
269     uno::Reference< XAccessible > Get(sal_Int32 nIndex) const;
270     uno::Reference< XAccessible > GetAt(const awt::Point& rPoint) const;
271 
272     // gets the index of the shape starting on 0 (without the index of the table)
273     // returns the selected shape
274     sal_Bool IsSelected(sal_Int32 nIndex,
275         com::sun::star::uno::Reference<com::sun::star::drawing::XShape>& rShape) const;
276 
277     sal_Bool SelectionChanged();
278 
279     void Select(sal_Int32 nIndex);
280     void DeselectAll(); // deselect also the table
281     void SelectAll();
282     sal_Int32 GetSelectedCount() const;
283     uno::Reference< XAccessible > GetSelected(sal_Int32 nSelectedChildIndex, sal_Bool bTabSelected) const;
284     void Deselect(sal_Int32 nChildIndex);
285 
286     SdrPage* GetDrawPage() const;
287 
288     utl::AccessibleRelationSetHelper* GetRelationSet(const ScAddress* pAddress) const;
289 
290     void VisAreaChanged() const;
291 private:
292     typedef std::vector<ScAccessibleShapeData*> SortedShapes;
293 
294     mutable SortedShapes maZOrderedShapes; // a null pointer represents the sheet in the correct order
295 
296     mutable ::accessibility::AccessibleShapeTreeInfo maShapeTreeInfo;
297     mutable com::sun::star::uno::Reference<com::sun::star::view::XSelectionSupplier> xSelectionSupplier;
298     mutable sal_uInt32 mnSdrObjCount;
299     mutable sal_uInt32 mnShapesSelected;
300     ScTabViewShell* mpViewShell;
301     ScAccessibleDocument* mpAccessibleDocument;
302     ScSplitPos meSplitPos;
303 
304     void FillShapes(std::vector < uno::Reference < drawing::XShape > >& rShapes) const;
305     sal_Bool FindSelectedShapesChanges(const com::sun::star::uno::Reference<com::sun::star::drawing::XShapes>& xShapes, sal_Bool bCommitChange) const;
306     void FillSelectionSupplier() const;
307 
308     ScAddress* GetAnchor(const uno::Reference<drawing::XShape>& xShape) const;
309     uno::Reference<XAccessibleRelationSet> GetRelationSet(const ScAccessibleShapeData* pData) const;
310     void CheckWhetherAnchorChanged(const uno::Reference<drawing::XShape>& xShape) const;
311     void SetAnchor(const uno::Reference<drawing::XShape>& xShape, ScAccessibleShapeData* pData) const;
312     void AddShape(const uno::Reference<drawing::XShape>& xShape, sal_Bool bCommitChange) const;
313     void RemoveShape(const uno::Reference<drawing::XShape>& xShape) const;
314 
315     sal_Bool FindShape(const uno::Reference<drawing::XShape>& xShape, SortedShapes::iterator& rItr) const;
316 
317     sal_Int8 Compare(const ScAccessibleShapeData* pData1,
318         const ScAccessibleShapeData* pData2) const;
319 };
320 
ScChildrenShapes(ScAccessibleDocument * pAccessibleDocument,ScTabViewShell * pViewShell,ScSplitPos eSplitPos)321 ScChildrenShapes::ScChildrenShapes(ScAccessibleDocument* pAccessibleDocument, ScTabViewShell* pViewShell, ScSplitPos eSplitPos)
322     :
323     mnShapesSelected(0),
324     mpViewShell(pViewShell),
325     mpAccessibleDocument(pAccessibleDocument),
326     meSplitPos(eSplitPos)
327 {
328     FillSelectionSupplier();
329     maZOrderedShapes.push_back(NULL); // add an element which represents the table
330 
331     GetCount(); // fill list with filtered shapes (no internal shapes)
332 
333     if (mnShapesSelected)
334     {
335         //set flag on every selected shape
336         if (!xSelectionSupplier.is())
337             throw uno::RuntimeException();
338 
339         uno::Reference<drawing::XShapes> xShapes(xSelectionSupplier->getSelection(), uno::UNO_QUERY);
340         if (xShapes.is())
341             FindSelectedShapesChanges(xShapes, sal_False);
342     }
343     if (pViewShell)
344     {
345         SfxBroadcaster* pDrawBC = pViewShell->GetViewData()->GetDocument()->GetDrawBroadcaster();
346         if (pDrawBC)
347         {
348             StartListening(*pDrawBC);
349 
350             maShapeTreeInfo.SetModelBroadcaster( new ScDrawModelBroadcaster(pViewShell->GetViewData()->GetDocument()->GetDrawLayer()) );
351             maShapeTreeInfo.SetSdrView(pViewShell->GetViewData()->GetScDrawView());
352             maShapeTreeInfo.SetController(NULL);
353             maShapeTreeInfo.SetWindow(pViewShell->GetWindowByPos(meSplitPos));
354             maShapeTreeInfo.SetViewForwarder(mpAccessibleDocument);
355         }
356     }
357 }
358 
~ScChildrenShapes()359 ScChildrenShapes::~ScChildrenShapes()
360 {
361     std::for_each(maZOrderedShapes.begin(), maZOrderedShapes.end(), Destroy());
362     if (mpViewShell)
363     {
364         SfxBroadcaster* pDrawBC = mpViewShell->GetViewData()->GetDocument()->GetDrawBroadcaster();
365         if (pDrawBC)
366             EndListening(*pDrawBC);
367     }
368 }
369 
SetDrawBroadcaster()370 void ScChildrenShapes::SetDrawBroadcaster()
371 {
372     if (mpViewShell)
373     {
374         SfxBroadcaster* pDrawBC = mpViewShell->GetViewData()->GetDocument()->GetDrawBroadcaster();
375         if (pDrawBC)
376         {
377             StartListening(*pDrawBC, sal_True);
378 
379             maShapeTreeInfo.SetModelBroadcaster( new ScDrawModelBroadcaster(mpViewShell->GetViewData()->GetDocument()->GetDrawLayer()) );
380             maShapeTreeInfo.SetSdrView(mpViewShell->GetViewData()->GetScDrawView());
381             maShapeTreeInfo.SetController(NULL);
382             maShapeTreeInfo.SetWindow(mpViewShell->GetWindowByPos(meSplitPos));
383             maShapeTreeInfo.SetViewForwarder(mpAccessibleDocument);
384         }
385     }
386 }
387 
Notify(SfxBroadcaster &,const SfxHint & rHint)388 void ScChildrenShapes::Notify(SfxBroadcaster&, const SfxHint& rHint)
389 {
390     if ( rHint.ISA( SdrHint ) )
391     {
392         const SdrHint* pSdrHint = PTR_CAST( SdrHint, &rHint );
393         if (pSdrHint)
394         {
395             SdrObject* pObj = const_cast<SdrObject*>(pSdrHint->GetObject());
396             if (pObj && /*(pObj->GetLayer() != SC_LAYER_INTERN) && */(pObj->GetPage() == GetDrawPage()) &&
397                 (pObj->GetPage() == pObj->GetObjList()) ) //#108480# only do something if the object lies direct on the page
398             {
399                 switch (pSdrHint->GetKind())
400                 {
401                     case HINT_OBJCHG :         // Objekt geaendert
402                     {
403                         uno::Reference<drawing::XShape> xShape (pObj->getUnoShape(), uno::UNO_QUERY);
404                         if (xShape.is())
405                         {
406                             ScShapeDataLess aLess;
407                             std::sort(maZOrderedShapes.begin(), maZOrderedShapes.end(), aLess); // sort, because the z index or layer could be changed
408                             CheckWhetherAnchorChanged(xShape);
409                         }
410                     }
411                     break;
412                     case HINT_OBJINSERTED :    // Neues Zeichenobjekt eingefuegt
413                     {
414                         uno::Reference<drawing::XShape> xShape (pObj->getUnoShape(), uno::UNO_QUERY);
415                         if (xShape.is())
416                             AddShape(xShape, sal_True);
417                     }
418                     break;
419                     case HINT_OBJREMOVED :     // Zeichenobjekt aus Liste entfernt
420                     {
421                         uno::Reference<drawing::XShape> xShape (pObj->getUnoShape(), uno::UNO_QUERY);
422                         if (xShape.is())
423                             RemoveShape(xShape);
424                     }
425                     break;
426                     default :
427                     {
428                         // other events are not interesting
429                     }
430                     break;
431                 }
432             }
433         }
434     }
435 }
436 
ReplaceChild(::accessibility::AccessibleShape * pCurrentChild,const::com::sun::star::uno::Reference<::com::sun::star::drawing::XShape> & _rxShape,const long _nIndex,const::accessibility::AccessibleShapeTreeInfo & _rShapeTreeInfo)437 sal_Bool ScChildrenShapes::ReplaceChild (::accessibility::AccessibleShape* pCurrentChild,
438         const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape >& _rxShape,
439         const long _nIndex, const ::accessibility::AccessibleShapeTreeInfo& _rShapeTreeInfo)
440 {
441     // create the new child
442     ::accessibility::AccessibleShape* pReplacement = ::accessibility::ShapeTypeHandler::Instance().CreateAccessibleObject (
443         ::accessibility::AccessibleShapeInfo ( _rxShape, pCurrentChild->getAccessibleParent(), this, _nIndex ),
444         _rShapeTreeInfo
445     );
446     ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessible > xNewChild( pReplacement ); // keep this alive (do this before calling Init!)
447     if ( pReplacement )
448         pReplacement->Init();
449 
450     sal_Bool bResult(sal_False);
451     if (pCurrentChild && pReplacement)
452     {
453         DBG_ASSERT(pCurrentChild->GetXShape().get() == pReplacement->GetXShape().get(), "XShape changes and should be inserted sorted");
454         SortedShapes::iterator aItr;
455         FindShape(pCurrentChild->GetXShape(), aItr);
456         if (aItr != maZOrderedShapes.end() && (*aItr))
457         {
458             if ((*aItr)->pAccShape)
459             {
460                 DBG_ASSERT((*aItr)->pAccShape == pCurrentChild, "wrong child found");
461                 AccessibleEventObject aEvent;
462                 aEvent.EventId = AccessibleEventId::CHILD;
463                 aEvent.Source = uno::Reference< XAccessibleContext >(mpAccessibleDocument);
464                 aEvent.OldValue <<= uno::makeAny(uno::Reference<XAccessible>(pCurrentChild));
465 
466                 mpAccessibleDocument->CommitChange(aEvent); // child is gone - event
467 
468                 pCurrentChild->dispose();
469             }
470             (*aItr)->pAccShape = pReplacement;
471             AccessibleEventObject aEvent;
472             aEvent.EventId = AccessibleEventId::CHILD;
473             aEvent.Source = uno::Reference< XAccessibleContext >(mpAccessibleDocument);
474             aEvent.NewValue <<= uno::makeAny(uno::Reference<XAccessible>(pReplacement));
475 
476             mpAccessibleDocument->CommitChange(aEvent); // child is new - event
477             bResult = sal_True;
478         }
479     }
480     return bResult;
481 }
482 
GetAccControlShapeFromModel(::com::sun::star::beans::XPropertySet * pSet)483 ::accessibility::AccessibleControlShape * ScChildrenShapes::GetAccControlShapeFromModel(::com::sun::star::beans::XPropertySet* pSet)
484 {
485     sal_Int32 count = GetCount();
486     for (sal_Int32 index=0;index<count;index++)
487     {
488         ScAccessibleShapeData* pShape = maZOrderedShapes[index];
489                 if (pShape)
490             {
491                 ::accessibility::AccessibleShape* pAccShape = pShape->pAccShape;
492                 if (pAccShape  && ::accessibility::ShapeTypeHandler::Instance().GetTypeId (pAccShape->GetXShape()) == ::accessibility::DRAWING_CONTROL)
493                 {
494                 ::accessibility::AccessibleControlShape *pCtlAccShape = static_cast < ::accessibility::AccessibleControlShape* >(pAccShape);
495                 if (pCtlAccShape && pCtlAccShape->GetControlModel() == pSet)
496                     return pCtlAccShape;
497               }
498                 }
499     }
500     return NULL;
501 }
502 ::com::sun::star::uno::Reference < ::com::sun::star::accessibility::XAccessible >
GetAccessibleCaption(const::com::sun::star::uno::Reference<::com::sun::star::drawing::XShape> & xShape)503 ScChildrenShapes::GetAccessibleCaption (const ::com::sun::star::uno::Reference < ::com::sun::star::drawing::XShape>& xShape)
504 {
505     sal_Int32 count = GetCount();
506     for (sal_Int32 index=0;index<count;index++)
507     {
508         ScAccessibleShapeData* pShape = maZOrderedShapes[index];
509             if (pShape && pShape->xShape == xShape )
510             {
511                 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessible > xNewChild(  pShape->pAccShape );
512 //              uno::Reference<XAccessible> xNewChild( pShape->pAccShape , uno::UNO_QUERY );
513                 if(xNewChild.get())
514                 return xNewChild;
515             }
516     }
517     return NULL;
518 }
GetCount() const519 sal_Int32 ScChildrenShapes::GetCount() const
520 {
521     SdrPage* pDrawPage = GetDrawPage();
522     if (pDrawPage && (maZOrderedShapes.size() == 1)) // the table is always in
523     {
524         mnSdrObjCount = pDrawPage->GetObjCount();
525         maZOrderedShapes.reserve(mnSdrObjCount + 1); // the table is always in
526         for (sal_uInt32 i = 0; i < mnSdrObjCount; ++i)
527         {
528             SdrObject* pObj = pDrawPage->GetObj(i);
529             if (pObj/* && (pObj->GetLayer() != SC_LAYER_INTERN)*/)
530             {
531                 uno::Reference< drawing::XShape > xShape (pObj->getUnoShape(), uno::UNO_QUERY);
532                 AddShape(xShape, sal_False); //inserts in the correct order
533             }
534         }
535     }
536     return maZOrderedShapes.size();
537 }
538 
Get(const ScAccessibleShapeData * pData) const539 uno::Reference< XAccessible > ScChildrenShapes::Get(const ScAccessibleShapeData* pData) const
540 {
541     if (!pData)
542         return NULL;
543 
544     if (!pData->pAccShape)
545     {
546         ::accessibility::ShapeTypeHandler& rShapeHandler = ::accessibility::ShapeTypeHandler::Instance();
547         ::accessibility::AccessibleShapeInfo aShapeInfo(pData->xShape, mpAccessibleDocument, const_cast<ScChildrenShapes*>(this));
548         pData->pAccShape = rShapeHandler.CreateAccessibleObject(
549             aShapeInfo, maShapeTreeInfo);
550         if (pData->pAccShape)
551         {
552             pData->pAccShape->acquire();
553             pData->pAccShape->Init();
554             if (pData->bSelected)
555                 pData->pAccShape->SetState(AccessibleStateType::SELECTED);
556             if (!pData->bSelectable)
557                 pData->pAccShape->ResetState(AccessibleStateType::SELECTABLE);
558             pData->pAccShape->SetRelationSet(GetRelationSet(pData));
559         }
560     }
561     return pData->pAccShape;
562  }
563 
Get(sal_Int32 nIndex) const564 uno::Reference< XAccessible > ScChildrenShapes::Get(sal_Int32 nIndex) const
565 {
566     if (maZOrderedShapes.size() <= 1)
567         GetCount(); // fill list with filtered shapes (no internal shapes)
568 
569     if (static_cast<sal_uInt32>(nIndex) >= maZOrderedShapes.size())
570         return NULL;
571 
572     return Get(maZOrderedShapes[nIndex]);
573 }
574 
GetAt(const awt::Point & rPoint) const575 uno::Reference< XAccessible > ScChildrenShapes::GetAt(const awt::Point& rPoint) const
576 {
577     uno::Reference<XAccessible> xAccessible;
578     if(mpViewShell)
579     {
580         sal_Int32 i(maZOrderedShapes.size() - 1);
581         sal_Bool bFound(sal_False);
582         while (!bFound && i >= 0)
583         {
584             ScAccessibleShapeData* pShape = maZOrderedShapes[i];
585             if (pShape)
586             {
587                 if (!pShape->pAccShape)
588                     Get(pShape);
589 
590                 if (pShape->pAccShape)
591                 {
592                     Point aPoint(VCLPoint(rPoint));
593                     aPoint -= VCLRectangle(pShape->pAccShape->getBounds()).TopLeft();
594                     if (pShape->pAccShape->containsPoint(AWTPoint(aPoint)))
595                     {
596                         xAccessible = pShape->pAccShape;
597                         bFound = sal_True;
598                     }
599                 }
600                 else
601                 {
602                     DBG_ERRORFILE("I should have an accessible shape now!");
603                 }
604             }
605             else
606                 bFound = sal_True; // this is the sheet and it lies before the rest of the shapes which are background shapes
607 
608             --i;
609         }
610     }
611     return xAccessible;
612 }
613 
IsSelected(sal_Int32 nIndex,uno::Reference<drawing::XShape> & rShape) const614 sal_Bool ScChildrenShapes::IsSelected(sal_Int32 nIndex,
615                         uno::Reference<drawing::XShape>& rShape) const
616 {
617     sal_Bool bResult (sal_False);
618     if (maZOrderedShapes.size() <= 1)
619         GetCount(); // fill list with filtered shapes (no internal shapes)
620 
621     if (!xSelectionSupplier.is())
622         throw uno::RuntimeException();
623 
624     if (!maZOrderedShapes[nIndex])
625         return sal_False;
626 
627     bResult = maZOrderedShapes[nIndex]->bSelected;
628     rShape = maZOrderedShapes[nIndex]->xShape;
629 
630 #ifdef DBG_UTIL // test whether it is truly selected by a slower method
631     uno::Reference< drawing::XShape > xReturnShape;
632     sal_Bool bDebugResult(sal_False);
633     uno::Reference<container::XIndexAccess> xIndexAccess;
634     xSelectionSupplier->getSelection() >>= xIndexAccess;
635 
636     if (xIndexAccess.is())
637     {
638         sal_Int32 nCount(xIndexAccess->getCount());
639         if (nCount)
640         {
641             uno::Reference< drawing::XShape > xShape;
642             uno::Reference< drawing::XShape > xIndexShape = maZOrderedShapes[nIndex]->xShape;
643             sal_Int32 i(0);
644             while (!bDebugResult && (i < nCount))
645             {
646                 xIndexAccess->getByIndex(i) >>= xShape;
647                 if (xShape.is() && (xIndexShape.get() == xShape.get()))
648                 {
649                     bDebugResult = sal_True;
650                     xReturnShape = xShape;
651                 }
652                 else
653                     ++i;
654             }
655         }
656     }
657     DBG_ASSERT((bResult == bDebugResult) && ((bResult && (rShape.get() == xReturnShape.get())) || !bResult), "found the wrong shape or result");
658 #endif
659 
660     return bResult;
661 }
662 
SelectionChanged()663 sal_Bool ScChildrenShapes::SelectionChanged()
664 {
665     sal_Bool bResult(sal_False);
666     if (!xSelectionSupplier.is())
667         throw uno::RuntimeException();
668 
669     uno::Reference<drawing::XShapes> xShapes(xSelectionSupplier->getSelection(), uno::UNO_QUERY);
670 
671     bResult = FindSelectedShapesChanges(xShapes, sal_True);
672 
673     return bResult;
674 }
675 
Select(sal_Int32 nIndex)676 void ScChildrenShapes::Select(sal_Int32 nIndex)
677 {
678     if (maZOrderedShapes.size() <= 1)
679         GetCount(); // fill list with filtered shapes (no internal shapes)
680 
681     if (!xSelectionSupplier.is())
682         throw uno::RuntimeException();
683 
684     if (!maZOrderedShapes[nIndex])
685         return;
686 
687     uno::Reference<drawing::XShape> xShape;
688     if (!IsSelected(nIndex, xShape) && maZOrderedShapes[nIndex]->bSelectable)
689     {
690         uno::Reference<drawing::XShapes> xShapes;
691         xSelectionSupplier->getSelection() >>= xShapes;
692 
693         if (!xShapes.is())
694             xShapes = new SvxShapeCollection();
695 
696         xShapes->add(maZOrderedShapes[nIndex]->xShape);
697 
698         try
699         {
700             xSelectionSupplier->select(uno::makeAny(xShapes));
701             maZOrderedShapes[nIndex]->bSelected = sal_True;
702             if (maZOrderedShapes[nIndex]->pAccShape)
703                 maZOrderedShapes[nIndex]->pAccShape->SetState(AccessibleStateType::SELECTED);
704         }
705         catch (lang::IllegalArgumentException&)
706         {
707         }
708     }
709 }
710 
DeselectAll()711 void ScChildrenShapes::DeselectAll()
712 {
713     if (!xSelectionSupplier.is())
714         throw uno::RuntimeException();
715 
716     sal_Bool bSomethingSelected(sal_True);
717     try
718     {
719         xSelectionSupplier->select(uno::Any()); //deselects all
720     }
721     catch (lang::IllegalArgumentException&)
722     {
723         DBG_ERRORFILE("nothing selected before");
724         bSomethingSelected = sal_False;
725     }
726 
727     if (bSomethingSelected)
728         std::for_each(maZOrderedShapes.begin(), maZOrderedShapes.end(), DeselectShape());
729 }
730 
SelectAll()731 void ScChildrenShapes::SelectAll()
732 {
733     if (!xSelectionSupplier.is())
734         throw uno::RuntimeException();
735 
736     if (maZOrderedShapes.size() <= 1)
737         GetCount(); // fill list with filtered shapes (no internal shapes)
738 
739     if (maZOrderedShapes.size() > 1)
740     {
741         uno::Reference<drawing::XShapes> xShapes;
742         xShapes = new SvxShapeCollection();
743 
744         try
745         {
746             std::for_each(maZOrderedShapes.begin(), maZOrderedShapes.end(), SelectShape(xShapes));
747             xSelectionSupplier->select(uno::makeAny(xShapes));
748         }
749         catch (lang::IllegalArgumentException&)
750         {
751             SelectionChanged(); // find all selected shapes and set the flags
752         }
753     }
754 }
755 
FillShapes(std::vector<uno::Reference<drawing::XShape>> & rShapes) const756 void ScChildrenShapes::FillShapes(std::vector < uno::Reference < drawing::XShape > >& rShapes) const
757 {
758     uno::Reference<container::XIndexAccess> xIndexAccess;
759     xSelectionSupplier->getSelection() >>= xIndexAccess;
760 
761     if (xIndexAccess.is())
762     {
763         sal_uInt32 nCount(xIndexAccess->getCount());
764         for (sal_uInt32 i = 0; i < nCount; ++i)
765         {
766             uno::Reference<drawing::XShape> xShape;
767             xIndexAccess->getByIndex(i) >>= xShape;
768             if (xShape.is())
769                 rShapes.push_back(xShape);
770         }
771     }
772 }
773 
GetSelectedCount() const774 sal_Int32 ScChildrenShapes::GetSelectedCount() const
775 {
776     if (!xSelectionSupplier.is())
777         throw uno::RuntimeException();
778 
779     std::vector < uno::Reference < drawing::XShape > > aShapes;
780     FillShapes(aShapes);
781 
782     return aShapes.size();
783 }
784 
GetSelected(sal_Int32 nSelectedChildIndex,sal_Bool bTabSelected) const785 uno::Reference< XAccessible > ScChildrenShapes::GetSelected(sal_Int32 nSelectedChildIndex, sal_Bool bTabSelected) const
786 {
787     uno::Reference< XAccessible > xAccessible;
788 
789     if (maZOrderedShapes.size() <= 1)
790         GetCount(); // fill list with shapes
791 
792     if (!bTabSelected)
793     {
794         std::vector < uno::Reference < drawing::XShape > > aShapes;
795         FillShapes(aShapes);
796 
797         if(aShapes.size()<=0) return xAccessible;
798         SortedShapes::iterator aItr;
799         if (FindShape(aShapes[nSelectedChildIndex], aItr))
800             xAccessible = Get(aItr - maZOrderedShapes.begin());
801     }
802     else
803     {
804         SortedShapes::iterator aItr = maZOrderedShapes.begin();
805         SortedShapes::iterator aEndItr = maZOrderedShapes.end();
806         sal_Bool bFound(sal_False);
807         while(!bFound && aItr != aEndItr)
808         {
809             if (*aItr)
810             {
811                 if ((*aItr)->bSelected)
812                 {
813                     if (nSelectedChildIndex == 0)
814                         bFound = sal_True;
815                     else
816                         --nSelectedChildIndex;
817                 }
818             }
819             else
820             {
821                 if (nSelectedChildIndex == 0)
822                     bFound = sal_True;
823                 else
824                     --nSelectedChildIndex;
825             }
826             if (!bFound)
827                 ++aItr;
828         }
829         if (bFound && *aItr)
830             xAccessible = (*aItr)->pAccShape;
831     }
832 
833     return xAccessible;
834 }
835 
Deselect(sal_Int32 nChildIndex)836 void ScChildrenShapes::Deselect(sal_Int32 nChildIndex)
837 {
838     uno::Reference<drawing::XShape> xShape;
839     if (IsSelected(nChildIndex, xShape)) // returns false if it is the sheet
840     {
841         if (xShape.is())
842         {
843             uno::Reference<drawing::XShapes> xShapes;
844             xSelectionSupplier->getSelection() >>= xShapes;
845             if (xShapes.is())
846                 xShapes->remove(xShape);
847 
848             try
849             {
850                 xSelectionSupplier->select(uno::makeAny(xShapes));
851             }
852             catch (lang::IllegalArgumentException&)
853             {
854                 DBG_ERRORFILE("something not selectable");
855             }
856 
857             maZOrderedShapes[nChildIndex]->bSelected = sal_False;
858             if (maZOrderedShapes[nChildIndex]->pAccShape)
859                 maZOrderedShapes[nChildIndex]->pAccShape->ResetState(AccessibleStateType::SELECTED);
860         }
861     }
862 }
863 
864 
GetDrawPage() const865 SdrPage* ScChildrenShapes::GetDrawPage() const
866 {
867     SCTAB nTab(mpAccessibleDocument->getVisibleTable());
868     SdrPage* pDrawPage = NULL;
869     if (mpViewShell)
870     {
871         ScDocument* pDoc = mpViewShell->GetViewData()->GetDocument();
872         if (pDoc && pDoc->GetDrawLayer())
873         {
874             ScDrawLayer* pDrawLayer = pDoc->GetDrawLayer();
875             if (pDrawLayer->HasObjects() && (pDrawLayer->GetPageCount() > nTab))
876                 pDrawPage = pDrawLayer->GetPage(static_cast<sal_uInt16>(static_cast<sal_Int16>(nTab)));
877         }
878     }
879     return pDrawPage;
880 }
881 
882 struct SetRelation
883 {
884     const ScChildrenShapes* mpChildrenShapes;
885     mutable utl::AccessibleRelationSetHelper* mpRelationSet;
886     const ScAddress* mpAddress;
SetRelationSetRelation887     SetRelation(const ScChildrenShapes* pChildrenShapes, const ScAddress* pAddress)
888         :
889         mpChildrenShapes(pChildrenShapes),
890         mpRelationSet(NULL),
891         mpAddress(pAddress)
892     {
893     }
operator ()SetRelation894     void operator() (const ScAccessibleShapeData* pAccShapeData) const
895     {
896         if (pAccShapeData &&
897             ((!pAccShapeData->pRelationCell && !mpAddress) ||
898             (pAccShapeData->pRelationCell && mpAddress && (*(pAccShapeData->pRelationCell) == *mpAddress))))
899         {
900             if (!mpRelationSet)
901                 mpRelationSet = new utl::AccessibleRelationSetHelper();
902 
903             AccessibleRelation aRelation;
904             aRelation.TargetSet.realloc(1);
905             aRelation.TargetSet[0] = mpChildrenShapes->Get(pAccShapeData);
906             aRelation.RelationType = AccessibleRelationType::CONTROLLER_FOR;
907 
908             mpRelationSet->AddRelation(aRelation);
909         }
910     }
911 };
912 
GetRelationSet(const ScAddress * pAddress) const913 utl::AccessibleRelationSetHelper* ScChildrenShapes::GetRelationSet(const ScAddress* pAddress) const
914 {
915     SetRelation aSetRelation(this, pAddress);
916     ::std::for_each(maZOrderedShapes.begin(), maZOrderedShapes.end(), aSetRelation);
917     return aSetRelation.mpRelationSet;
918 }
919 
FindSelectedShapesChanges(const uno::Reference<drawing::XShapes> & xShapes,sal_Bool) const920 sal_Bool ScChildrenShapes::FindSelectedShapesChanges(const uno::Reference<drawing::XShapes>& xShapes, sal_Bool /* bCommitChange */) const
921 {
922     sal_Bool bResult(sal_False);
923     SortedShapes aShapesList;
924     uno::Reference<container::XIndexAccess> xIndexAcc(xShapes, uno::UNO_QUERY);
925     if (xIndexAcc.is())
926     {
927         mnShapesSelected = xIndexAcc->getCount();
928         for (sal_uInt32 i = 0; i < mnShapesSelected; ++i)
929         {
930             uno::Reference< drawing::XShape > xShape;
931             xIndexAcc->getByIndex(i) >>= xShape;
932             if (xShape.is())
933             {
934                 ScAccessibleShapeData* pShapeData = new ScAccessibleShapeData();
935                 pShapeData->xShape = xShape;
936                 aShapesList.push_back(pShapeData);
937             }
938         }
939     }
940     else
941         mnShapesSelected = 0;
942     SdrObject *pFocusedObj = NULL;
943     if( mnShapesSelected == 1 && aShapesList.size() == 1)
944     {
945         pFocusedObj = GetSdrObjectFromXShape(aShapesList[0]->xShape);
946     }
947     ScShapeDataLess aLess;
948     std::sort(aShapesList.begin(), aShapesList.end(), aLess);
949     SortedShapes vecSelectedShapeAdd;
950     SortedShapes vecSelectedShapeRemove;
951     sal_Bool bHasSelect=sal_False;
952     SortedShapes::iterator aXShapesItr(aShapesList.begin());
953     SortedShapes::const_iterator aXShapesEndItr(aShapesList.end());
954     SortedShapes::iterator aDataItr(maZOrderedShapes.begin());
955     SortedShapes::const_iterator aDataEndItr(maZOrderedShapes.end());
956     SortedShapes::const_iterator aFocusedItr = aDataEndItr;
957     while((aDataItr != aDataEndItr))
958     {
959         if (*aDataItr) // is it really a shape or only the sheet
960         {
961             sal_Int8 nComp(0);
962             if (aXShapesItr == aXShapesEndItr)
963                 nComp = -1; // simulate that the Shape is lower, so the selection state will be removed
964             else
965                 nComp = Compare(*aDataItr, *aXShapesItr);
966             if (nComp == 0)
967             {
968                 if (!(*aDataItr)->bSelected)
969                 {
970                     (*aDataItr)->bSelected = sal_True;
971                     if ((*aDataItr)->pAccShape)
972                     {
973                         (*aDataItr)->pAccShape->SetState(AccessibleStateType::SELECTED);
974                         (*aDataItr)->pAccShape->ResetState(AccessibleStateType::FOCUSED);
975                         bResult = sal_True;
976                         vecSelectedShapeAdd.push_back((*aDataItr));
977                     }
978                     aFocusedItr = aDataItr;
979                 }
980                 else
981                 {
982                      bHasSelect = sal_True;
983                 }
984                 ++aDataItr;
985                 ++aXShapesItr;
986             }
987             else if (nComp < 0)
988             {
989                 if ((*aDataItr)->bSelected)
990                 {
991                     (*aDataItr)->bSelected = sal_False;
992                     if ((*aDataItr)->pAccShape)
993                     {
994                         (*aDataItr)->pAccShape->ResetState(AccessibleStateType::SELECTED);
995                         (*aDataItr)->pAccShape->ResetState(AccessibleStateType::FOCUSED);
996                         bResult = sal_True;
997                         vecSelectedShapeRemove.push_back(*aDataItr);
998                     }
999                 }
1000                 ++aDataItr;
1001             }
1002             else
1003             {
1004                 DBG_ERRORFILE("here is a selected shape which is not in the childlist");
1005                 ++aXShapesItr;
1006                 --mnShapesSelected;
1007             }
1008         }
1009         else
1010             ++aDataItr;
1011     }
1012     bool bWinFocus=false;
1013     if (mpViewShell)
1014     {
1015         ScGridWindow* pWin = static_cast<ScGridWindow*>(mpViewShell->GetWindowByPos(meSplitPos));
1016         if (pWin)
1017         {
1018             bWinFocus = pWin->HasFocus();
1019         }
1020     }
1021     const SdrMarkList* pMarkList = NULL;
1022     SdrObject* pMarkedObj = NULL;
1023     SdrObject* pUpObj = NULL;
1024     sal_Bool bIsFocuseMarked = sal_True;
1025     if( mpViewShell && mnShapesSelected == 1 && bWinFocus)
1026     {
1027         ScDrawView* pScDrawView = mpViewShell->GetViewData()->GetScDrawView();
1028         if( pScDrawView )
1029         {
1030             if( pScDrawView->GetMarkedObjectList().GetMarkCount() == 1 )
1031             {
1032                 pMarkList = &(pScDrawView->GetMarkedObjectList());
1033                 pMarkedObj = pMarkList->GetMark(0)->GetMarkedSdrObj();
1034                 uno::Reference< drawing::XShape > xMarkedXShape (pMarkedObj->getUnoShape(), uno::UNO_QUERY);
1035                 if( aFocusedItr != aDataEndItr &&
1036                     (*aFocusedItr)->xShape.is() &&
1037                     xMarkedXShape.is() &&
1038                     (*aFocusedItr)->xShape != xMarkedXShape )
1039                     bIsFocuseMarked = sal_False;
1040             }
1041         }
1042     }
1043     //if ((aFocusedItr != aDataEndItr) && (*aFocusedItr)->pAccShape && (mnShapesSelected == 1))
1044     if ( bIsFocuseMarked && (aFocusedItr != aDataEndItr) && (*aFocusedItr)->pAccShape && (mnShapesSelected == 1) && bWinFocus)
1045     {
1046         (*aFocusedItr)->pAccShape->SetState(AccessibleStateType::FOCUSED);
1047     }
1048     else if( pFocusedObj && bWinFocus && pMarkList && pMarkList->GetMarkCount() == 1 && mnShapesSelected == 1 )
1049     {
1050         if( pMarkedObj )
1051         {
1052             uno::Reference< drawing::XShape > xMarkedXShape (pMarkedObj->getUnoShape(), uno::UNO_QUERY);
1053             pUpObj = pMarkedObj->GetUpGroup();
1054 
1055             if( pMarkedObj == pFocusedObj )
1056             {
1057                 if( pUpObj )
1058                 {
1059                     uno::Reference< drawing::XShape > xUpGroupXShape (pUpObj->getUnoShape(), uno::UNO_QUERY);
1060                     uno::Reference < XAccessible > xAccGroupShape =
1061                         const_cast<ScChildrenShapes*>(this)->GetAccessibleCaption( xUpGroupXShape );
1062                     if( xAccGroupShape.is() )
1063                     {
1064                         ::accessibility::AccessibleShape* pAccGroupShape =
1065                             static_cast< ::accessibility::AccessibleShape* >(xAccGroupShape.get());
1066                         if( pAccGroupShape )
1067                         {
1068                             sal_Int32 nCount =  pAccGroupShape->getAccessibleChildCount();
1069                             for( sal_Int32 i = 0; i < nCount; i++ )
1070                             {
1071                                 uno::Reference<XAccessible> xAccShape = pAccGroupShape->getAccessibleChild(i);
1072                                 if (xAccShape.is())
1073                                 {
1074                                     ::accessibility::AccessibleShape* pChildAccShape =  static_cast< ::accessibility::AccessibleShape* >(xAccShape.get());
1075                                     uno::Reference< drawing::XShape > xChildShape = pChildAccShape->GetXShape();
1076                                     if (xChildShape == xMarkedXShape)
1077                                     {
1078                                         pChildAccShape->SetState(AccessibleStateType::FOCUSED);
1079                                     }
1080                                     else
1081                                     {
1082                                         pChildAccShape->ResetState(AccessibleStateType::FOCUSED);
1083                                     }
1084                                 }
1085                             }
1086                         }
1087                     }
1088                 }
1089             }
1090         }
1091     }
1092     if (vecSelectedShapeAdd.size() >= 10 )
1093     {
1094         AccessibleEventObject aEvent;
1095         aEvent.EventId = AccessibleEventId::SELECTION_CHANGED_WITHIN;
1096         aEvent.Source = uno::Reference< XAccessible >(mpAccessibleDocument);
1097         mpAccessibleDocument->CommitChange(aEvent);
1098     }
1099     else
1100     {
1101         SortedShapes::iterator vi = vecSelectedShapeAdd.begin();
1102         for (; vi != vecSelectedShapeAdd.end() ; ++vi )
1103         {
1104             AccessibleEventObject aEvent;
1105             if (bHasSelect)
1106             {
1107                 aEvent.EventId = AccessibleEventId::SELECTION_CHANGED_ADD;
1108             }
1109             else
1110             {
1111                 aEvent.EventId = AccessibleEventId::SELECTION_CHANGED;
1112             }
1113             aEvent.Source = uno::Reference< XAccessible >(mpAccessibleDocument);
1114             uno::Reference< XAccessible > xChild( (*vi)->pAccShape);
1115             aEvent.NewValue <<= xChild;
1116             mpAccessibleDocument->CommitChange(aEvent);
1117         }
1118     }
1119     SortedShapes::iterator vi = vecSelectedShapeRemove.begin();
1120     for (; vi != vecSelectedShapeRemove.end() ; ++vi )
1121     {
1122         AccessibleEventObject aEvent;
1123         aEvent.EventId =  AccessibleEventId::SELECTION_CHANGED_REMOVE;
1124         aEvent.Source = uno::Reference< XAccessible >(mpAccessibleDocument);
1125         uno::Reference< XAccessible > xChild( (*vi)->pAccShape);
1126         aEvent.NewValue <<= xChild;
1127         mpAccessibleDocument->CommitChange(aEvent);
1128     }
1129     std::for_each(aShapesList.begin(), aShapesList.end(), Destroy());
1130 
1131     return bResult;
1132 }
1133 
FillSelectionSupplier() const1134 void ScChildrenShapes::FillSelectionSupplier() const
1135 {
1136     if (!xSelectionSupplier.is() && mpViewShell)
1137     {
1138         SfxViewFrame* pViewFrame = mpViewShell->GetViewFrame();
1139         if (pViewFrame)
1140         {
1141             xSelectionSupplier = uno::Reference<view::XSelectionSupplier>(pViewFrame->GetFrame().GetController(), uno::UNO_QUERY);
1142             if (xSelectionSupplier.is())
1143             {
1144                 if (mpAccessibleDocument)
1145                     xSelectionSupplier->addSelectionChangeListener(mpAccessibleDocument);
1146                 uno::Reference<drawing::XShapes> xShapes (xSelectionSupplier->getSelection(), uno::UNO_QUERY);
1147                 if (xShapes.is())
1148                     mnShapesSelected = xShapes->getCount();
1149             }
1150         }
1151     }
1152 }
1153 
GetAnchor(const uno::Reference<drawing::XShape> & xShape) const1154 ScAddress* ScChildrenShapes::GetAnchor(const uno::Reference<drawing::XShape>& xShape) const
1155 {
1156     ScAddress* pAddress = NULL;
1157     if (mpViewShell)
1158     {
1159         SvxShape* pShapeImp = SvxShape::getImplementation(xShape);
1160         uno::Reference<beans::XPropertySet> xShapeProp(xShape, uno::UNO_QUERY);
1161         if (pShapeImp && xShapeProp.is())
1162         {
1163             SdrObject *pSdrObj = pShapeImp->GetSdrObject();
1164             if (pSdrObj)
1165             {
1166                 if (ScDrawLayer::GetAnchor(pSdrObj) == SCA_CELL)
1167                 {
1168                     ScDocument* pDoc = mpViewShell->GetViewData()->GetDocument();
1169                     if (pDoc)
1170                     {
1171                         rtl::OUString sCaptionShape(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.CaptionShape"));
1172                         awt::Point aPoint(xShape->getPosition());
1173                         awt::Size aSize(xShape->getSize());
1174                         rtl::OUString sType(xShape->getShapeType());
1175                         Rectangle aRectangle(aPoint.X, aPoint.Y, aPoint.X + aSize.Width, aPoint.Y + aSize.Height);
1176                         if ( sType.equals(sCaptionShape) )
1177                         {
1178                             awt::Point aRelativeCaptionPoint;
1179                             rtl::OUString sCaptionPoint( RTL_CONSTASCII_USTRINGPARAM( "CaptionPoint" ));
1180                             xShapeProp->getPropertyValue( sCaptionPoint ) >>= aRelativeCaptionPoint;
1181                             Point aCoreRelativeCaptionPoint(aRelativeCaptionPoint.X, aRelativeCaptionPoint.Y);
1182                             Point aCoreAbsoluteCaptionPoint(aPoint.X, aPoint.Y);
1183                             aCoreAbsoluteCaptionPoint += aCoreRelativeCaptionPoint;
1184                             aRectangle.Union(Rectangle(aCoreAbsoluteCaptionPoint, aCoreAbsoluteCaptionPoint));
1185                         }
1186                         ScRange aRange = pDoc->GetRange(mpAccessibleDocument->getVisibleTable(), aRectangle);
1187                         pAddress = new ScAddress(aRange.aStart);
1188                     }
1189                 }
1190 //              else
1191 //                  do nothing, because it is always a NULL Pointer
1192             }
1193         }
1194     }
1195 
1196     return pAddress;
1197 }
1198 
GetRelationSet(const ScAccessibleShapeData * pData) const1199 uno::Reference<XAccessibleRelationSet> ScChildrenShapes::GetRelationSet(const ScAccessibleShapeData* pData) const
1200 {
1201     utl::AccessibleRelationSetHelper* pRelationSet = new utl::AccessibleRelationSetHelper();
1202 
1203     if(pData && pRelationSet && mpAccessibleDocument)
1204     {
1205         uno::Reference<XAccessible> xAccessible = mpAccessibleDocument->GetAccessibleSpreadsheet(); // should be the current table
1206         if (pData->pRelationCell && xAccessible.is())
1207         {
1208             uno::Reference<XAccessibleTable> xAccTable (xAccessible->getAccessibleContext(), uno::UNO_QUERY);
1209             if (xAccTable.is())
1210                 xAccessible = xAccTable->getAccessibleCellAt(pData->pRelationCell->Row(), pData->pRelationCell->Col());
1211         }
1212         AccessibleRelation aRelation;
1213         aRelation.TargetSet.realloc(1);
1214         aRelation.TargetSet[0] = xAccessible;
1215         aRelation.RelationType = AccessibleRelationType::CONTROLLED_BY;
1216         pRelationSet->AddRelation(aRelation);
1217     }
1218 
1219     return pRelationSet;
1220 }
1221 
CheckWhetherAnchorChanged(const uno::Reference<drawing::XShape> & xShape) const1222 void ScChildrenShapes::CheckWhetherAnchorChanged(const uno::Reference<drawing::XShape>& xShape) const
1223 {
1224     SortedShapes::iterator aItr;
1225     if (FindShape(xShape, aItr))
1226         SetAnchor(xShape, *aItr);
1227 }
1228 
SetAnchor(const uno::Reference<drawing::XShape> & xShape,ScAccessibleShapeData * pData) const1229 void ScChildrenShapes::SetAnchor(const uno::Reference<drawing::XShape>& xShape, ScAccessibleShapeData* pData) const
1230 {
1231     if (pData)
1232     {
1233         ScAddress* pAddress = GetAnchor(xShape);
1234         if ((pAddress && pData->pRelationCell && (*pAddress != *(pData->pRelationCell))) ||
1235             (!pAddress && pData->pRelationCell) || (pAddress && !pData->pRelationCell))
1236         {
1237             if (pData->pRelationCell)
1238                 delete pData->pRelationCell;
1239             pData->pRelationCell = pAddress;
1240             if (pData->pAccShape)
1241                 pData->pAccShape->SetRelationSet(GetRelationSet(pData));
1242         }
1243     }
1244 }
1245 
AddShape(const uno::Reference<drawing::XShape> & xShape,sal_Bool bCommitChange) const1246 void ScChildrenShapes::AddShape(const uno::Reference<drawing::XShape>& xShape, sal_Bool bCommitChange) const
1247 {
1248     SortedShapes::iterator aFindItr;
1249     if (!FindShape(xShape, aFindItr))
1250     {
1251         ScAccessibleShapeData* pShape = new ScAccessibleShapeData();
1252         pShape->xShape = xShape;
1253         SortedShapes::iterator aNewItr = maZOrderedShapes.insert(aFindItr, pShape);
1254         SetAnchor(xShape, pShape);
1255 
1256         uno::Reference< beans::XPropertySet > xShapeProp(xShape, uno::UNO_QUERY);
1257         if (xShapeProp.is())
1258         {
1259             uno::Any aPropAny = xShapeProp->getPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(  "LayerID" )));
1260             sal_Int16 nLayerID = 0;
1261             if( aPropAny >>= nLayerID )
1262             {
1263                 if( (nLayerID == SC_LAYER_INTERN) || (nLayerID == SC_LAYER_HIDDEN) )
1264                     pShape->bSelectable = sal_False;
1265                 else
1266                     pShape->bSelectable = sal_True;
1267             }
1268         }
1269 
1270 
1271         if (!xSelectionSupplier.is())
1272             throw uno::RuntimeException();
1273 
1274         uno::Reference<container::XEnumerationAccess> xEnumAcc(xSelectionSupplier->getSelection(), uno::UNO_QUERY);
1275         if (xEnumAcc.is())
1276         {
1277             uno::Reference<container::XEnumeration> xEnum = xEnumAcc->createEnumeration();
1278             if (xEnum.is())
1279             {
1280                 uno::Reference<drawing::XShape> xSelectedShape;
1281                 sal_Bool bFound(sal_False);
1282                 while (!bFound && xEnum->hasMoreElements())
1283                 {
1284                     xEnum->nextElement() >>= xSelectedShape;
1285                     if (xShape.is() && (xShape.get() == xSelectedShape.get()))
1286                     {
1287                         pShape->bSelected = sal_True;
1288                         bFound = sal_True;
1289                     }
1290                 }
1291             }
1292         }
1293         if (mpAccessibleDocument && bCommitChange)
1294         {
1295             AccessibleEventObject aEvent;
1296             aEvent.EventId = AccessibleEventId::CHILD;
1297             aEvent.Source = uno::Reference< XAccessibleContext >(mpAccessibleDocument);
1298             aEvent.NewValue <<= Get(aNewItr - maZOrderedShapes.begin());
1299 
1300             mpAccessibleDocument->CommitChange(aEvent); // new child - event
1301         }
1302     }
1303     else
1304     {
1305         DBG_ERRORFILE("shape is always in the list");
1306     }
1307 }
1308 
RemoveShape(const uno::Reference<drawing::XShape> & xShape) const1309 void ScChildrenShapes::RemoveShape(const uno::Reference<drawing::XShape>& xShape) const
1310 {
1311     SortedShapes::iterator aItr;
1312     if (FindShape(xShape, aItr))
1313     {
1314         if (mpAccessibleDocument)
1315         {
1316             uno::Reference<XAccessible> xOldAccessible (Get(aItr - maZOrderedShapes.begin()));
1317 
1318             delete *aItr;
1319             maZOrderedShapes.erase(aItr);
1320 
1321             AccessibleEventObject aEvent;
1322             aEvent.EventId = AccessibleEventId::CHILD;
1323             aEvent.Source = uno::Reference< XAccessibleContext >(mpAccessibleDocument);
1324             aEvent.OldValue <<= uno::makeAny(xOldAccessible);
1325 
1326             mpAccessibleDocument->CommitChange(aEvent); // child is gone - event
1327         }
1328         else
1329         {
1330             delete *aItr;
1331             maZOrderedShapes.erase(aItr);
1332         }
1333     }
1334     else
1335     {
1336         DBG_ERRORFILE("shape was not in internal list");
1337     }
1338 }
1339 
FindShape(const uno::Reference<drawing::XShape> & xShape,ScChildrenShapes::SortedShapes::iterator & rItr) const1340 sal_Bool ScChildrenShapes::FindShape(const uno::Reference<drawing::XShape>& xShape, ScChildrenShapes::SortedShapes::iterator& rItr) const
1341 {
1342     sal_Bool bResult(sal_False);
1343     ScAccessibleShapeData aShape;
1344     aShape.xShape = xShape;
1345     ScShapeDataLess aLess;
1346     rItr = std::lower_bound(maZOrderedShapes.begin(), maZOrderedShapes.end(), &aShape, aLess);
1347     if ((rItr != maZOrderedShapes.end()) && (*rItr != NULL) && ((*rItr)->xShape.get() == xShape.get()))
1348         bResult = sal_True; // if the shape is found
1349 
1350 #ifdef DBG_UTIL // test whether it finds truly the correct shape (perhaps it is not really sorted)
1351     SortedShapes::iterator aDebugItr = maZOrderedShapes.begin();
1352     SortedShapes::iterator aEndItr = maZOrderedShapes.end();
1353     sal_Bool bFound(sal_False);
1354     while (!bFound && aDebugItr != aEndItr)
1355     {
1356         if (*aDebugItr && ((*aDebugItr)->xShape.get() == xShape.get()))
1357             bFound = sal_True;
1358         else
1359             ++aDebugItr;
1360     }
1361     sal_Bool bResult2 = (aDebugItr != maZOrderedShapes.end());
1362     DBG_ASSERT((bResult == bResult2) && ((bResult && (rItr == aDebugItr)) || !bResult), "wrong Shape found");
1363 #endif
1364     return bResult;
1365 }
1366 
Compare(const ScAccessibleShapeData * pData1,const ScAccessibleShapeData * pData2) const1367 sal_Int8 ScChildrenShapes::Compare(const ScAccessibleShapeData* pData1,
1368         const ScAccessibleShapeData* pData2) const
1369 {
1370     ScShapeDataLess aLess;
1371 
1372     sal_Bool bResult1(aLess(pData1, pData2));
1373     sal_Bool bResult2(aLess(pData2, pData1));
1374 
1375     sal_Int8 nResult(0);
1376     if (!bResult1 && bResult2)
1377         nResult = 1;
1378     else if (bResult1 && !bResult2)
1379         nResult = -1;
1380 
1381     return nResult;
1382 }
1383 
1384 struct ScVisAreaChanged
1385 {
1386     ScAccessibleDocument* mpAccDoc;
ScVisAreaChangedScVisAreaChanged1387     ScVisAreaChanged(ScAccessibleDocument* pAccDoc) : mpAccDoc(pAccDoc) {}
operator ()ScVisAreaChanged1388     void operator() (const ScAccessibleShapeData* pAccShapeData) const
1389     {
1390         if (pAccShapeData && pAccShapeData->pAccShape)
1391         {
1392             pAccShapeData->pAccShape->ViewForwarderChanged(::accessibility::IAccessibleViewForwarderListener::VISIBLE_AREA, mpAccDoc);
1393         }
1394     }
1395 };
1396 
VisAreaChanged() const1397 void ScChildrenShapes::VisAreaChanged() const
1398 {
1399     ScVisAreaChanged aVisAreaChanged(mpAccessibleDocument);
1400     std::for_each(maZOrderedShapes.begin(), maZOrderedShapes.end(), aVisAreaChanged);
1401 }
1402 
1403 // ============================================================================
1404 
ScAccessibleDocument(const uno::Reference<XAccessible> & rxParent,ScTabViewShell * pViewShell,ScSplitPos eSplitPos)1405 ScAccessibleDocument::ScAccessibleDocument(
1406         const uno::Reference<XAccessible>& rxParent,
1407         ScTabViewShell* pViewShell,
1408         ScSplitPos eSplitPos)
1409     : ScAccessibleDocumentBase(rxParent),
1410     mpViewShell(pViewShell),
1411     meSplitPos(eSplitPos),
1412     mpAccessibleSpreadsheet(NULL),
1413     mpChildrenShapes(NULL),
1414     mpTempAccEdit(NULL),
1415     mbCompleteSheetSelected(sal_False)
1416 {
1417     if (pViewShell)
1418     {
1419         pViewShell->AddAccessibilityObject(*this);
1420         Window *pWin = pViewShell->GetWindowByPos(eSplitPos);
1421         if( pWin )
1422         {
1423             pWin->AddChildEventListener( LINK( this, ScAccessibleDocument, WindowChildEventListener ));
1424             sal_uInt16 nCount =   pWin->GetChildCount();
1425             for( sal_uInt16 i=0; i < nCount; ++i )
1426             {
1427                 Window *pChildWin = pWin->GetChild( i );
1428                 if( pChildWin &&
1429                     AccessibleRole::EMBEDDED_OBJECT == pChildWin->GetAccessibleRole() )
1430                     AddChild( pChildWin->GetAccessible(), sal_False );
1431             }
1432         }
1433         if (pViewShell->GetViewData()->HasEditView( eSplitPos ))
1434         {
1435             uno::Reference<XAccessible> xAcc = new ScAccessibleEditObject(this, pViewShell->GetViewData()->GetEditView(eSplitPos),
1436                 pViewShell->GetWindowByPos(eSplitPos), GetCurrentCellName(), GetCurrentCellDescription(),
1437                 CellInEditMode);
1438             AddChild(xAcc, sal_False);
1439         }
1440     }
1441     maVisArea = GetVisibleArea_Impl();
1442 }
1443 
Init()1444 void ScAccessibleDocument::Init()
1445 {
1446     if(!mpChildrenShapes)
1447         mpChildrenShapes = new ScChildrenShapes(this, mpViewShell, meSplitPos);
1448 }
1449 
~ScAccessibleDocument(void)1450 ScAccessibleDocument::~ScAccessibleDocument(void)
1451 {
1452     if (!ScAccessibleContextBase::IsDefunc() && !rBHelper.bInDispose)
1453     {
1454         // increment refcount to prevent double call off dtor
1455         osl_incrementInterlockedCount( &m_refCount );
1456         dispose();
1457     }
1458 }
1459 
disposing()1460 void SAL_CALL ScAccessibleDocument::disposing()
1461 {
1462     ScUnoGuard aGuard;
1463     FreeAccessibleSpreadsheet();
1464     if (mpViewShell)
1465     {
1466         Window *pWin = mpViewShell->GetWindowByPos(meSplitPos);
1467         if( pWin )
1468             pWin->RemoveChildEventListener( LINK( this, ScAccessibleDocument, WindowChildEventListener ));
1469 
1470         mpViewShell->RemoveAccessibilityObject(*this);
1471         mpViewShell = NULL;
1472     }
1473     if (mpChildrenShapes)
1474         DELETEZ(mpChildrenShapes);
1475 
1476     ScAccessibleDocumentBase::disposing();
1477 }
1478 
disposing(const lang::EventObject &)1479 void SAL_CALL ScAccessibleDocument::disposing( const lang::EventObject& /* Source */ )
1480 {
1481     disposing();
1482 }
1483 
1484     //=====  SfxListener  =====================================================
1485 
IMPL_LINK(ScAccessibleDocument,WindowChildEventListener,VclSimpleEvent *,pEvent)1486 IMPL_LINK( ScAccessibleDocument, WindowChildEventListener, VclSimpleEvent*, pEvent )
1487 {
1488     DBG_ASSERT( pEvent && pEvent->ISA( VclWindowEvent ), "Unknown WindowEvent!" );
1489     if ( pEvent && pEvent->ISA( VclWindowEvent ) )
1490     {
1491         VclWindowEvent *pVclEvent = static_cast< VclWindowEvent * >( pEvent );
1492         DBG_ASSERT( pVclEvent->GetWindow(), "Window???" );
1493         switch ( pVclEvent->GetId() )
1494         {
1495         case VCLEVENT_WINDOW_SHOW:  // send create on show for direct accessible children
1496             {
1497                 Window* pChildWin = static_cast < Window * >( pVclEvent->GetData() );
1498                 if( pChildWin && AccessibleRole::EMBEDDED_OBJECT == pChildWin->GetAccessibleRole() )
1499                 {
1500                     AddChild( pChildWin->GetAccessible(), sal_True );
1501                 }
1502             }
1503             break;
1504         case VCLEVENT_WINDOW_HIDE:  // send destroy on hide for direct accessible children
1505             {
1506                 Window* pChildWin = static_cast < Window * >( pVclEvent->GetData() );
1507                 if( pChildWin && AccessibleRole::EMBEDDED_OBJECT == pChildWin->GetAccessibleRole() )
1508                 {
1509                     RemoveChild( pChildWin->GetAccessible(), sal_True );
1510                 }
1511             }
1512             break;
1513         }
1514     }
1515     return 0;
1516 }
1517 
Notify(SfxBroadcaster & rBC,const SfxHint & rHint)1518 void ScAccessibleDocument::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
1519 {
1520     if (rHint.ISA( ScAccGridWinFocusLostHint ) )
1521     {
1522         const ScAccGridWinFocusLostHint& rRef = (const ScAccGridWinFocusLostHint&)rHint;
1523         if (rRef.GetOldGridWin() == meSplitPos)
1524         {
1525             if (mxTempAcc.is() && mpTempAccEdit)
1526                 mpTempAccEdit->LostFocus();
1527             else if (mpAccessibleSpreadsheet)
1528                 mpAccessibleSpreadsheet->LostFocus();
1529             else
1530                 CommitFocusLost();
1531         }
1532     }
1533     else if (rHint.ISA( ScAccGridWinFocusGotHint ) )
1534     {
1535         const ScAccGridWinFocusGotHint& rRef = (const ScAccGridWinFocusGotHint&)rHint;
1536         if (rRef.GetNewGridWin() == meSplitPos)
1537         {
1538             uno::Reference<XAccessible> xAccessible;
1539             if (mpChildrenShapes)
1540             {
1541                 sal_Bool bTabMarked(IsTableSelected());
1542                 xAccessible = mpChildrenShapes->GetSelected(0, bTabMarked);
1543             }
1544             if( xAccessible.is() )
1545             {
1546                 uno::Any aNewValue;
1547                 aNewValue<<=AccessibleStateType::FOCUSED;
1548                 static_cast< ::accessibility::AccessibleShape* >(xAccessible.get())->
1549                     CommitChange(AccessibleEventId::STATE_CHANGED,
1550                                 aNewValue,
1551                                 uno::Any() );
1552             }
1553             else
1554             {
1555             if (mxTempAcc.is() && mpTempAccEdit)
1556                 mpTempAccEdit->GotFocus();
1557             else if (mpAccessibleSpreadsheet)
1558                 mpAccessibleSpreadsheet->GotFocus();
1559             else
1560                 CommitFocusGained();
1561             }
1562         }
1563     }
1564     else if (rHint.ISA( SfxSimpleHint ))
1565     {
1566         const SfxSimpleHint& rRef = (const SfxSimpleHint&)rHint;
1567         // only notify if child exist, otherwise it is not necessary
1568         if ((rRef.GetId() == SC_HINT_ACC_TABLECHANGED) &&
1569             mpAccessibleSpreadsheet)
1570         {
1571             FreeAccessibleSpreadsheet();
1572             if (mpChildrenShapes)
1573                 DELETEZ(mpChildrenShapes);
1574 
1575             // #124567# Accessibility: Shapes / form controls after reload not accessible
1576             if ( !mpChildrenShapes )
1577             {
1578                 mpChildrenShapes = new ScChildrenShapes( this, mpViewShell, meSplitPos );
1579             }
1580             //Invoke Init() to rebuild the mpChildrenShapes variable
1581             this->Init();
1582             AccessibleEventObject aEvent;
1583             aEvent.EventId = AccessibleEventId::INVALIDATE_ALL_CHILDREN;
1584             aEvent.Source = uno::Reference< XAccessibleContext >(this);
1585             CommitChange(aEvent); // all childs changed
1586             if (mpAccessibleSpreadsheet)
1587                 mpAccessibleSpreadsheet->FireFirstCellFocus();
1588         }
1589         else if (rRef.GetId() == SC_HINT_ACC_MAKEDRAWLAYER)
1590         {
1591             if (mpChildrenShapes)
1592                 mpChildrenShapes->SetDrawBroadcaster();
1593         }
1594         else if ((rRef.GetId() == SC_HINT_ACC_ENTEREDITMODE)) // this event comes only on creating edit field of a cell
1595         {
1596             if (mpViewShell && mpViewShell->GetViewData()->HasEditView(meSplitPos))
1597             {
1598                 EditEngine* pEditEng = mpViewShell->GetViewData()->GetEditView(meSplitPos)->GetEditEngine();
1599                 if (pEditEng && pEditEng->GetUpdateMode())
1600                 {
1601                     mpTempAccEdit = new ScAccessibleEditObject(this, mpViewShell->GetViewData()->GetEditView(meSplitPos),
1602                         mpViewShell->GetWindowByPos(meSplitPos), GetCurrentCellName(),
1603                         rtl::OUString(String(ScResId(STR_ACC_EDITLINE_DESCR))), CellInEditMode);
1604                     uno::Reference<XAccessible> xAcc = mpTempAccEdit;
1605 
1606                     AddChild(xAcc, sal_True);
1607 
1608                     if (mpAccessibleSpreadsheet)
1609                         mpAccessibleSpreadsheet->LostFocus();
1610                     else
1611                         CommitFocusLost();
1612 
1613                     mpTempAccEdit->GotFocus();
1614                 }
1615             }
1616         }
1617         else if (rRef.GetId() == SC_HINT_ACC_LEAVEEDITMODE)
1618         {
1619             if (mxTempAcc.is())
1620             {
1621                 if (mpTempAccEdit)
1622                     mpTempAccEdit->LostFocus();
1623 
1624                 mpTempAccEdit = NULL;
1625                 RemoveChild(mxTempAcc, sal_True);
1626                 if (mpAccessibleSpreadsheet && mpViewShell->IsActive())
1627                     mpAccessibleSpreadsheet->GotFocus();
1628                 else if( mpViewShell->IsActive())
1629                     CommitFocusGained();
1630             }
1631         }
1632         else if ((rRef.GetId() == SC_HINT_ACC_VISAREACHANGED) || (rRef.GetId() == SC_HINT_ACC_WINDOWRESIZED))
1633         {
1634             Rectangle aOldVisArea(maVisArea);
1635             maVisArea = GetVisibleArea_Impl();
1636 
1637             if (maVisArea != aOldVisArea)
1638             {
1639                 if (maVisArea.GetSize() != aOldVisArea.GetSize())
1640                 {
1641                     AccessibleEventObject aEvent;
1642                     aEvent.EventId = AccessibleEventId::BOUNDRECT_CHANGED;
1643                     aEvent.Source = uno::Reference< XAccessibleContext >(this);
1644 
1645                     CommitChange(aEvent);
1646 
1647                     if (mpAccessibleSpreadsheet)
1648                         mpAccessibleSpreadsheet->BoundingBoxChanged();
1649                     if (mpAccessibleSpreadsheet && mpViewShell->IsActive())
1650                         mpAccessibleSpreadsheet->FireFirstCellFocus();
1651                 }
1652                 else if (mpAccessibleSpreadsheet)
1653                 {
1654                     mpAccessibleSpreadsheet->VisAreaChanged();
1655                 }
1656                 if (mpChildrenShapes)
1657                     mpChildrenShapes->VisAreaChanged();
1658             }
1659         }
1660     }
1661 
1662     ScAccessibleDocumentBase::Notify(rBC, rHint);
1663 }
1664 
selectionChanged(const lang::EventObject &)1665 void SAL_CALL ScAccessibleDocument::selectionChanged( const lang::EventObject& /* aEvent */ )
1666 {
1667     sal_Bool bSelectionChanged(sal_False);
1668     if (mpAccessibleSpreadsheet)
1669     {
1670         sal_Bool bOldSelected(mbCompleteSheetSelected);
1671         mbCompleteSheetSelected = IsTableSelected();
1672         if (bOldSelected != mbCompleteSheetSelected)
1673         {
1674             mpAccessibleSpreadsheet->CompleteSelectionChanged(mbCompleteSheetSelected);
1675             bSelectionChanged = sal_True;
1676         }
1677     }
1678 
1679     if (mpChildrenShapes && mpChildrenShapes->SelectionChanged())
1680         bSelectionChanged = sal_True;
1681 
1682     if (bSelectionChanged)
1683     {
1684         AccessibleEventObject aEvent;
1685         aEvent.EventId = AccessibleEventId::SELECTION_CHANGED;
1686         aEvent.Source = uno::Reference< XAccessibleContext >(this);
1687 
1688         CommitChange(aEvent);
1689     }
1690     if(mpChildrenShapes )
1691     {
1692         mpChildrenShapes->SelectionChanged();
1693     }
1694 }
1695 
1696     //=====  XInterface  =====================================================
1697 
queryInterface(uno::Type const & rType)1698 uno::Any SAL_CALL ScAccessibleDocument::queryInterface( uno::Type const & rType )
1699 {
1700     uno::Any aAnyTmp;
1701     if(rType == ::getCppuType((com::sun::star::uno::Reference<XAccessibleGetAccFlowTo> *)NULL) )
1702        {
1703          com::sun::star::uno::Reference<XAccessibleGetAccFlowTo> AccFromXShape = this;
1704             aAnyTmp <<= AccFromXShape;
1705          return aAnyTmp;
1706        }
1707     uno::Any aAny (ScAccessibleDocumentImpl::queryInterface(rType));
1708     return aAny.hasValue() ? aAny : ScAccessibleContextBase::queryInterface(rType);
1709 }
1710 
acquire()1711 void SAL_CALL ScAccessibleDocument::acquire()
1712     throw ()
1713 {
1714     ScAccessibleContextBase::acquire();
1715 }
1716 
release()1717 void SAL_CALL ScAccessibleDocument::release()
1718     throw ()
1719 {
1720     ScAccessibleContextBase::release();
1721 }
1722 
1723     //=====  XAccessibleComponent  ============================================
1724 
getAccessibleAtPoint(const awt::Point & rPoint)1725 uno::Reference< XAccessible > SAL_CALL ScAccessibleDocument::getAccessibleAtPoint(
1726         const awt::Point& rPoint )
1727 {
1728     uno::Reference<XAccessible> xAccessible = NULL;
1729     if (containsPoint(rPoint))
1730     {
1731         ScUnoGuard aGuard;
1732         IsObjectValid();
1733         if (mpChildrenShapes)
1734             xAccessible = mpChildrenShapes->GetAt(rPoint);
1735         if(!xAccessible.is())
1736         {
1737             if (mxTempAcc.is())
1738             {
1739                 uno::Reference< XAccessibleContext > xCont(mxTempAcc->getAccessibleContext());
1740                 uno::Reference< XAccessibleComponent > xComp(xCont, uno::UNO_QUERY);
1741                 if (xComp.is())
1742                 {
1743                     Rectangle aBound(VCLRectangle(xComp->getBounds()));
1744                     if (aBound.IsInside(VCLPoint(rPoint)))
1745                         xAccessible = mxTempAcc;
1746                 }
1747             }
1748             if (!xAccessible.is())
1749                 xAccessible = GetAccessibleSpreadsheet();
1750         }
1751     }
1752     return xAccessible;
1753 }
1754 
grabFocus()1755 void SAL_CALL ScAccessibleDocument::grabFocus(  )
1756 {
1757     ScUnoGuard aGuard;
1758     IsObjectValid();
1759     if (getAccessibleParent().is())
1760     {
1761         uno::Reference<XAccessibleComponent> xAccessibleComponent(getAccessibleParent()->getAccessibleContext(), uno::UNO_QUERY);
1762         if (xAccessibleComponent.is())
1763         {
1764             xAccessibleComponent->grabFocus();
1765             // grab only focus if it does not have the focus and it is not hidden
1766             if (mpViewShell && mpViewShell->GetViewData() &&
1767                 (mpViewShell->GetViewData()->GetActivePart() != meSplitPos) &&
1768                 mpViewShell->GetWindowByPos(meSplitPos)->IsVisible())
1769             {
1770                 mpViewShell->ActivatePart(meSplitPos);
1771             }
1772         }
1773     }
1774 }
1775 
1776     //=====  XAccessibleContext  ==============================================
1777 
1778     /// Return the number of currently visible children.
1779 sal_Int32 SAL_CALL
getAccessibleChildCount(void)1780     ScAccessibleDocument::getAccessibleChildCount(void)
1781 {
1782     ScUnoGuard aGuard;
1783     IsObjectValid();
1784     sal_Int32 nCount(1);
1785     if (mpChildrenShapes)
1786         nCount = mpChildrenShapes->GetCount(); // returns the count of the shapes inclusive the table
1787 
1788     if (mxTempAcc.is())
1789         ++nCount;
1790 
1791     return nCount;
1792 }
1793 
1794     /// Return the specified child or NULL if index is invalid.
1795 uno::Reference<XAccessible> SAL_CALL
getAccessibleChild(sal_Int32 nIndex)1796     ScAccessibleDocument::getAccessibleChild(sal_Int32 nIndex)
1797 {
1798     ScUnoGuard aGuard;
1799     IsObjectValid();
1800     uno::Reference<XAccessible> xAccessible;
1801     if (nIndex >= 0)
1802     {
1803         sal_Int32 nCount(1);
1804         if (mpChildrenShapes)
1805         {
1806             xAccessible = mpChildrenShapes->Get(nIndex); // returns NULL if it is the table or out of range
1807             nCount = mpChildrenShapes->GetCount(); //there is always a table
1808         }
1809         if (!xAccessible.is())
1810         {
1811             if (nIndex < nCount)
1812                 xAccessible = GetAccessibleSpreadsheet();
1813             else if (nIndex == nCount && mxTempAcc.is())
1814                 xAccessible = mxTempAcc;
1815         }
1816     }
1817 
1818     if (!xAccessible.is())
1819         throw lang::IndexOutOfBoundsException();
1820 
1821     return xAccessible;
1822 }
1823 
1824     /// Return the set of current states.
1825 uno::Reference<XAccessibleStateSet> SAL_CALL
getAccessibleStateSet(void)1826     ScAccessibleDocument::getAccessibleStateSet(void)
1827 {
1828     ScUnoGuard aGuard;
1829     uno::Reference<XAccessibleStateSet> xParentStates;
1830     if (getAccessibleParent().is())
1831     {
1832         uno::Reference<XAccessibleContext> xParentContext = getAccessibleParent()->getAccessibleContext();
1833         xParentStates = xParentContext->getAccessibleStateSet();
1834     }
1835     utl::AccessibleStateSetHelper* pStateSet = new utl::AccessibleStateSetHelper();
1836     if (IsDefunc(xParentStates))
1837         pStateSet->AddState(AccessibleStateType::DEFUNC);
1838     else
1839     {
1840         if (IsEditable(xParentStates))
1841             pStateSet->AddState(AccessibleStateType::EDITABLE);
1842         pStateSet->AddState(AccessibleStateType::ENABLED);
1843         pStateSet->AddState(AccessibleStateType::OPAQUE);
1844         if (isShowing())
1845             pStateSet->AddState(AccessibleStateType::SHOWING);
1846         if (isVisible())
1847             pStateSet->AddState(AccessibleStateType::VISIBLE);
1848     }
1849     return pStateSet;
1850 }
1851 
1852 ::rtl::OUString SAL_CALL
getAccessibleName(void)1853     ScAccessibleDocument::getAccessibleName(void)
1854 {
1855     rtl::OUString sName = String(ScResId(STR_ACC_DOC_SPREADSHEET));
1856     ScDocument* pScDoc = GetDocument();
1857     if ( pScDoc )
1858     {
1859         rtl::OUString sFileName = pScDoc->getDocAccTitle();
1860         if ( !sFileName.getLength() )
1861         {
1862             SfxObjectShell* pObjSh = pScDoc->GetDocumentShell();
1863             if ( pObjSh )
1864             {
1865                 sFileName = pObjSh->GetTitle( SFX_TITLE_APINAME );
1866             }
1867         }
1868         rtl::OUString sReadOnly;
1869         if (pScDoc->getDocReadOnly())
1870         {
1871             sReadOnly = String(ScResId(STR_ACC_DOC_SPREADSHEET_READONLY));
1872         }
1873         if ( sFileName.getLength() )
1874         {
1875             sName = sFileName + sReadOnly + rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" - ")) + sName;
1876         }
1877     }
1878     return sName;
1879 }
1880     ///=====  XAccessibleSelection  ===========================================
1881 
1882 void SAL_CALL
selectAccessibleChild(sal_Int32 nChildIndex)1883     ScAccessibleDocument::selectAccessibleChild( sal_Int32 nChildIndex )
1884 {
1885     ScUnoGuard aGuard;
1886     IsObjectValid();
1887 
1888     if (mpChildrenShapes)
1889     {
1890         sal_Int32 nCount(mpChildrenShapes->GetCount()); //all shapes and the table
1891         if (mxTempAcc.is())
1892             ++nCount;
1893         if (nChildIndex < 0 || nChildIndex >= nCount)
1894             throw lang::IndexOutOfBoundsException();
1895 
1896         uno::Reference < XAccessible > xAccessible = mpChildrenShapes->Get(nChildIndex);
1897         if (xAccessible.is())
1898         {
1899             sal_Bool bWasTableSelected(IsTableSelected());
1900 
1901             if (mpChildrenShapes)
1902                 mpChildrenShapes->Select(nChildIndex); // throws no lang::IndexOutOfBoundsException if Index is to high
1903 
1904             if (bWasTableSelected)
1905                 mpViewShell->SelectAll();
1906         }
1907         else
1908         {
1909             if (mpViewShell)
1910                 mpViewShell->SelectAll();
1911         }
1912     }
1913 }
1914 
1915 sal_Bool SAL_CALL
isAccessibleChildSelected(sal_Int32 nChildIndex)1916     ScAccessibleDocument::isAccessibleChildSelected( sal_Int32 nChildIndex )
1917 {
1918     ScUnoGuard aGuard;
1919     IsObjectValid();
1920     sal_Bool bResult(sal_False);
1921 
1922     if (mpChildrenShapes)
1923     {
1924         sal_Int32 nCount(mpChildrenShapes->GetCount()); //all shapes and the table
1925         if (mxTempAcc.is())
1926             ++nCount;
1927         if (nChildIndex < 0 || nChildIndex >= nCount)
1928             throw lang::IndexOutOfBoundsException();
1929 
1930         uno::Reference < XAccessible > xAccessible = mpChildrenShapes->Get(nChildIndex);
1931         if (xAccessible.is())
1932         {
1933             uno::Reference<drawing::XShape> xShape;
1934             bResult = mpChildrenShapes->IsSelected(nChildIndex, xShape); // throws no lang::IndexOutOfBoundsException if Index is to high
1935         }
1936         else
1937         {
1938             if (mxTempAcc.is() && nChildIndex == nCount)
1939                 bResult = sal_True;
1940             else
1941                 bResult = IsTableSelected();
1942         }
1943     }
1944     return bResult;
1945 }
1946 
1947 void SAL_CALL
clearAccessibleSelection()1948     ScAccessibleDocument::clearAccessibleSelection(  )
1949 {
1950     ScUnoGuard aGuard;
1951     IsObjectValid();
1952 
1953     if (mpChildrenShapes)
1954         mpChildrenShapes->DeselectAll(); //deselects all (also the table)
1955 }
1956 
1957 void SAL_CALL
selectAllAccessibleChildren()1958     ScAccessibleDocument::selectAllAccessibleChildren(  )
1959 {
1960     ScUnoGuard aGuard;
1961     IsObjectValid();
1962 
1963     if (mpChildrenShapes)
1964         mpChildrenShapes->SelectAll();
1965 
1966     // select table after shapes, because while selecting shapes the table will be deselected
1967     if (mpViewShell)
1968     {
1969         mpViewShell->SelectAll();
1970     }
1971 }
1972 
1973 sal_Int32 SAL_CALL
getSelectedAccessibleChildCount()1974     ScAccessibleDocument::getSelectedAccessibleChildCount(  )
1975 {
1976     ScUnoGuard aGuard;
1977     IsObjectValid();
1978     sal_Int32 nCount(0);
1979 
1980     if (mpChildrenShapes)
1981         nCount = mpChildrenShapes->GetSelectedCount();
1982 
1983     if (IsTableSelected())
1984         ++nCount;
1985 
1986     if (mxTempAcc.is())
1987         ++nCount;
1988 
1989     return nCount;
1990 }
1991 
1992 uno::Reference<XAccessible > SAL_CALL
getSelectedAccessibleChild(sal_Int32 nSelectedChildIndex)1993     ScAccessibleDocument::getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex )
1994 {
1995     ScUnoGuard aGuard;
1996     IsObjectValid();
1997     uno::Reference<XAccessible> xAccessible;
1998     if (mpChildrenShapes)
1999     {
2000         sal_Int32 nCount(getSelectedAccessibleChildCount()); //all shapes and the table
2001         if (nSelectedChildIndex < 0 || nSelectedChildIndex >= nCount)
2002             throw lang::IndexOutOfBoundsException();
2003 
2004         sal_Bool bTabMarked(IsTableSelected());
2005 
2006         if (mpChildrenShapes)
2007             xAccessible = mpChildrenShapes->GetSelected(nSelectedChildIndex, bTabMarked); // throws no lang::IndexOutOfBoundsException if Index is to high
2008         if (mxTempAcc.is() && nSelectedChildIndex == nCount - 1)
2009             xAccessible = mxTempAcc;
2010         else if (bTabMarked)
2011             xAccessible = GetAccessibleSpreadsheet();
2012     }
2013 
2014     DBG_ASSERT(xAccessible.is(), "here should always be an accessible object or a exception throwed");
2015 
2016     return xAccessible;
2017 }
2018 
2019 void SAL_CALL
deselectAccessibleChild(sal_Int32 nChildIndex)2020     ScAccessibleDocument::deselectAccessibleChild( sal_Int32 nChildIndex )
2021 {
2022     ScUnoGuard aGuard;
2023     IsObjectValid();
2024 
2025     if (mpChildrenShapes)
2026     {
2027         sal_Int32 nCount(mpChildrenShapes->GetCount()); //all shapes and the table
2028         if (mxTempAcc.is())
2029             ++nCount;
2030         if (nChildIndex < 0 || nChildIndex >= nCount)
2031             throw lang::IndexOutOfBoundsException();
2032 
2033         sal_Bool bTabMarked(IsTableSelected());
2034 
2035         uno::Reference < XAccessible > xAccessible = mpChildrenShapes->Get(nChildIndex);
2036         if (xAccessible.is())
2037         {
2038             if (mpChildrenShapes)
2039                 mpChildrenShapes->Deselect(nChildIndex); // throws no lang::IndexOutOfBoundsException if Index is to high
2040 
2041             if (bTabMarked)
2042                 mpViewShell->SelectAll(); // select the table again
2043         }
2044         else if (bTabMarked)
2045             mpViewShell->Unmark();
2046     }
2047 }
2048 
2049     //=====  XServiceInfo  ====================================================
2050 
2051 ::rtl::OUString SAL_CALL
getImplementationName(void)2052     ScAccessibleDocument::getImplementationName(void)
2053 {
2054     return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM ("ScAccessibleDocument"));
2055 }
2056 
2057 uno::Sequence< ::rtl::OUString> SAL_CALL
getSupportedServiceNames(void)2058     ScAccessibleDocument::getSupportedServiceNames(void)
2059 {
2060     uno::Sequence< ::rtl::OUString > aSequence = ScAccessibleContextBase::getSupportedServiceNames();
2061     sal_Int32 nOldSize(aSequence.getLength());
2062     aSequence.realloc(nOldSize + 1);
2063     ::rtl::OUString* pNames = aSequence.getArray();
2064 
2065     pNames[nOldSize] = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.AccessibleSpreadsheetDocumentView"));
2066 
2067     return aSequence;
2068 }
2069 
2070 //=====  XTypeProvider  =======================================================
2071 
getTypes()2072 uno::Sequence< uno::Type > SAL_CALL ScAccessibleDocument::getTypes()
2073 {
2074     return comphelper::concatSequences(ScAccessibleDocumentImpl::getTypes(), ScAccessibleContextBase::getTypes());
2075 }
2076 
2077 uno::Sequence<sal_Int8> SAL_CALL
getImplementationId(void)2078     ScAccessibleDocument::getImplementationId(void)
2079 {
2080     ScUnoGuard aGuard;
2081     IsObjectValid();
2082     static uno::Sequence<sal_Int8> aId;
2083     if (aId.getLength() == 0)
2084     {
2085         aId.realloc (16);
2086         rtl_createUuid (reinterpret_cast<sal_uInt8 *>(aId.getArray()), 0, sal_True);
2087     }
2088     return aId;
2089 }
2090 
2091 ///=====  IAccessibleViewForwarder  ========================================
2092 
IsValid(void) const2093 sal_Bool ScAccessibleDocument::IsValid (void) const
2094 {
2095     ScUnoGuard aGuard;
2096     IsObjectValid();
2097     return (!ScAccessibleContextBase::IsDefunc() && !rBHelper.bInDispose);
2098 }
2099 
GetVisibleArea_Impl() const2100 Rectangle ScAccessibleDocument::GetVisibleArea_Impl() const
2101 {
2102     Rectangle aVisRect(GetBoundingBox());
2103 
2104     Point aPoint(mpViewShell->GetViewData()->GetPixPos(meSplitPos)); // returns a negative Point
2105     aPoint.setX(-aPoint.getX());
2106     aPoint.setY(-aPoint.getY());
2107     aVisRect.SetPos(aPoint);
2108 
2109     ScGridWindow* pWin = static_cast<ScGridWindow*>(mpViewShell->GetWindowByPos(meSplitPos));
2110     if (pWin)
2111         aVisRect = pWin->PixelToLogic(aVisRect, pWin->GetDrawMapMode());
2112 
2113     return aVisRect;
2114 }
2115 
GetVisibleArea() const2116 Rectangle ScAccessibleDocument::GetVisibleArea() const
2117 {
2118     ScUnoGuard aGuard;
2119     IsObjectValid();
2120     return maVisArea;
2121 }
2122 
LogicToPixel(const Point & rPoint) const2123 Point ScAccessibleDocument::LogicToPixel (const Point& rPoint) const
2124 {
2125     ScUnoGuard aGuard;
2126     IsObjectValid();
2127     Point aPoint;
2128     ScGridWindow* pWin = static_cast<ScGridWindow*>(mpViewShell->GetWindowByPos(meSplitPos));
2129     if (pWin)
2130     {
2131         aPoint = pWin->LogicToPixel(rPoint, pWin->GetDrawMapMode());
2132         aPoint += pWin->GetWindowExtentsRelative(NULL).TopLeft();
2133     }
2134     return aPoint;
2135 }
2136 
LogicToPixel(const Size & rSize) const2137 Size ScAccessibleDocument::LogicToPixel (const Size& rSize) const
2138 {
2139     ScUnoGuard aGuard;
2140     IsObjectValid();
2141     Size aSize;
2142     ScGridWindow* pWin = static_cast<ScGridWindow*>(mpViewShell->GetWindowByPos(meSplitPos));
2143     if (pWin)
2144         aSize = pWin->LogicToPixel(rSize, pWin->GetDrawMapMode());
2145     return aSize;
2146 }
2147 
PixelToLogic(const Point & rPoint) const2148 Point ScAccessibleDocument::PixelToLogic (const Point& rPoint) const
2149 {
2150     ScUnoGuard aGuard;
2151     IsObjectValid();
2152     Point aPoint;
2153     ScGridWindow* pWin = static_cast<ScGridWindow*>(mpViewShell->GetWindowByPos(meSplitPos));
2154     if (pWin)
2155     {
2156         aPoint -= pWin->GetWindowExtentsRelative(NULL).TopLeft();
2157         aPoint = pWin->PixelToLogic(rPoint, pWin->GetDrawMapMode());
2158     }
2159     return aPoint;
2160 }
2161 
PixelToLogic(const Size & rSize) const2162 Size ScAccessibleDocument::PixelToLogic (const Size& rSize) const
2163 {
2164     ScUnoGuard aGuard;
2165     IsObjectValid();
2166     Size aSize;
2167     ScGridWindow* pWin = static_cast<ScGridWindow*>(mpViewShell->GetWindowByPos(meSplitPos));
2168     if (pWin)
2169         aSize = pWin->PixelToLogic(rSize, pWin->GetDrawMapMode());
2170     return aSize;
2171 }
2172 
2173     //=====  internal  ========================================================
2174 
GetRelationSet(const ScAddress * pAddress) const2175 utl::AccessibleRelationSetHelper* ScAccessibleDocument::GetRelationSet(const ScAddress* pAddress) const
2176 {
2177     utl::AccessibleRelationSetHelper* pRelationSet = NULL;
2178     if (mpChildrenShapes)
2179         pRelationSet = mpChildrenShapes->GetRelationSet(pAddress);
2180     return pRelationSet;
2181 }
2182 
2183 ::rtl::OUString SAL_CALL
createAccessibleDescription(void)2184     ScAccessibleDocument::createAccessibleDescription(void)
2185 {
2186     rtl::OUString sDescription = String(ScResId(STR_ACC_DOC_DESCR));
2187     return sDescription;
2188 }
2189 
2190 ::rtl::OUString SAL_CALL
createAccessibleName(void)2191     ScAccessibleDocument::createAccessibleName(void)
2192 {
2193     ScUnoGuard aGuard;
2194     IsObjectValid();
2195     rtl::OUString sName = String(ScResId(STR_ACC_DOC_NAME));
2196     sal_Int32 nNumber(sal_Int32(meSplitPos) + 1);
2197     sName += rtl::OUString::valueOf(nNumber);
2198     return sName;
2199 }
2200 
GetBoundingBoxOnScreen() const2201 Rectangle ScAccessibleDocument::GetBoundingBoxOnScreen() const
2202 {
2203     Rectangle aRect;
2204     if (mpViewShell)
2205     {
2206         Window* pWindow = mpViewShell->GetWindowByPos(meSplitPos);
2207         if (pWindow)
2208             aRect = pWindow->GetWindowExtentsRelative(NULL);
2209     }
2210     return aRect;
2211 }
2212 
GetBoundingBox() const2213 Rectangle ScAccessibleDocument::GetBoundingBox() const
2214 {
2215     Rectangle aRect;
2216     if (mpViewShell)
2217     {
2218         Window* pWindow = mpViewShell->GetWindowByPos(meSplitPos);
2219         if (pWindow)
2220             aRect = pWindow->GetWindowExtentsRelative(pWindow->GetAccessibleParentWindow());
2221     }
2222     return aRect;
2223 }
2224 
getVisibleTable() const2225 SCTAB ScAccessibleDocument::getVisibleTable() const
2226 {
2227     SCTAB nVisibleTable(0);
2228     if (mpViewShell && mpViewShell->GetViewData())
2229         nVisibleTable = mpViewShell->GetViewData()->GetTabNo();
2230     return nVisibleTable;
2231 }
2232 
2233 uno::Reference < XAccessible >
GetAccessibleSpreadsheet()2234     ScAccessibleDocument::GetAccessibleSpreadsheet()
2235 {
2236     if (!mpAccessibleSpreadsheet && mpViewShell)
2237     {
2238         mpAccessibleSpreadsheet = new ScAccessibleSpreadsheet(this, mpViewShell, getVisibleTable(), meSplitPos);
2239         mpAccessibleSpreadsheet->acquire();
2240         mpAccessibleSpreadsheet->Init();
2241         mbCompleteSheetSelected = IsTableSelected();
2242     }
2243     return mpAccessibleSpreadsheet;
2244 }
2245 
FreeAccessibleSpreadsheet()2246 void ScAccessibleDocument::FreeAccessibleSpreadsheet()
2247 {
2248     if (mpAccessibleSpreadsheet)
2249     {
2250         mpAccessibleSpreadsheet->dispose();
2251         mpAccessibleSpreadsheet->release();
2252         mpAccessibleSpreadsheet = NULL;
2253     }
2254 }
2255 
IsTableSelected() const2256 sal_Bool ScAccessibleDocument::IsTableSelected() const
2257 {
2258     sal_Bool bResult (sal_False);
2259     if(mpViewShell)
2260     {
2261         SCTAB nTab(getVisibleTable());
2262         //#103800#; use a copy of MarkData
2263         ScMarkData aMarkData(mpViewShell->GetViewData()->GetMarkData());
2264         aMarkData.MarkToMulti();
2265         if (aMarkData.IsAllMarked(ScRange(ScAddress(0, 0, nTab),ScAddress(MAXCOL, MAXROW, nTab))))
2266             bResult = sal_True;
2267     }
2268     return bResult;
2269 }
2270 
IsDefunc(const uno::Reference<XAccessibleStateSet> & rxParentStates)2271 sal_Bool ScAccessibleDocument::IsDefunc(
2272     const uno::Reference<XAccessibleStateSet>& rxParentStates)
2273 {
2274     return ScAccessibleContextBase::IsDefunc() || (mpViewShell == NULL) || !getAccessibleParent().is() ||
2275         (rxParentStates.is() && rxParentStates->contains(AccessibleStateType::DEFUNC));
2276 }
2277 
IsEditable(const uno::Reference<XAccessibleStateSet> &)2278 sal_Bool ScAccessibleDocument::IsEditable(
2279     const uno::Reference<XAccessibleStateSet>& /* rxParentStates */)
2280 {
2281     // what is with document protection or readonly documents?
2282     return sal_True;
2283 }
2284 
AddChild(const uno::Reference<XAccessible> & xAcc,sal_Bool bFireEvent)2285 void ScAccessibleDocument::AddChild(const uno::Reference<XAccessible>& xAcc, sal_Bool bFireEvent)
2286 {
2287     DBG_ASSERT(!mxTempAcc.is(), "this object should be removed before");
2288     if (xAcc.is())
2289     {
2290         mxTempAcc = xAcc;
2291         if( bFireEvent )
2292         {
2293             AccessibleEventObject aEvent;
2294                         aEvent.Source = uno::Reference<XAccessibleContext>(this);
2295             aEvent.EventId = AccessibleEventId::CHILD;
2296             aEvent.NewValue <<= mxTempAcc;
2297             CommitChange( aEvent );
2298         }
2299     }
2300 }
2301 
RemoveChild(const uno::Reference<XAccessible> & xAcc,sal_Bool bFireEvent)2302 void ScAccessibleDocument::RemoveChild(const uno::Reference<XAccessible>& xAcc, sal_Bool bFireEvent)
2303 {
2304     DBG_ASSERT(mxTempAcc.is(), "this object should be added before");
2305     if (xAcc.is())
2306     {
2307         DBG_ASSERT(xAcc.get() == mxTempAcc.get(), "only the same object should be removed");
2308         if( bFireEvent )
2309         {
2310             AccessibleEventObject aEvent;
2311                         aEvent.Source = uno::Reference<XAccessibleContext>(this);
2312             aEvent.EventId = AccessibleEventId::CHILD;
2313             aEvent.OldValue <<= mxTempAcc;
2314             CommitChange( aEvent );
2315         }
2316         mxTempAcc = NULL;
2317     }
2318 }
2319 
GetCurrentCellName() const2320 rtl::OUString ScAccessibleDocument::GetCurrentCellName() const
2321 {
2322     String sName( ScResId(STR_ACC_CELL_NAME) );
2323     if (mpViewShell)
2324     {
2325         String sAddress;
2326         // Document not needed, because only the cell address, but not the tablename is needed
2327         mpViewShell->GetViewData()->GetCurPos().Format( sAddress, SCA_VALID, NULL );
2328         sName.SearchAndReplaceAscii("%1", sAddress);
2329     }
2330     return rtl::OUString(sName);
2331 }
2332 
GetCurrentCellDescription() const2333 rtl::OUString ScAccessibleDocument::GetCurrentCellDescription() const
2334 {
2335     return rtl::OUString();
2336 }
GetDocument() const2337 ScDocument *ScAccessibleDocument::GetDocument() const
2338 {
2339     return mpViewShell ? mpViewShell->GetViewData()->GetDocument() : NULL;
2340 }
GetCurCellAddress() const2341 ScAddress   ScAccessibleDocument::GetCurCellAddress() const
2342 {
2343     return mpViewShell ? mpViewShell->GetViewData()->GetCurPos() :ScAddress();
2344 }
getExtendedAttributes()2345 uno::Any SAL_CALL ScAccessibleDocument::getExtendedAttributes()
2346 {
2347 
2348     uno::Any anyAtrribute;
2349 
2350     rtl::OUString sName;
2351     rtl::OUString sValue;
2352     sal_uInt16 sheetIndex;
2353     String sSheetName;
2354     sheetIndex = getVisibleTable();
2355     if(GetDocument()==NULL)
2356         return anyAtrribute;
2357     GetDocument()->GetName(sheetIndex,sSheetName);
2358     sName = rtl::OUString::createFromAscii("page-name:");
2359     sValue = sName + sSheetName ;
2360     sName = rtl::OUString::createFromAscii(";page-number:");
2361     sValue += sName;
2362     sValue += String::CreateFromInt32(sheetIndex+1) ;
2363     sName = rtl::OUString::createFromAscii(";total-pages:");
2364     sValue += sName;
2365     sValue += String::CreateFromInt32(GetDocument()->GetTableCount());
2366     sValue +=  rtl::OUString::createFromAscii(";");
2367     anyAtrribute <<= sValue;
2368     return anyAtrribute;
2369 }
GetScAccFlowToSequence()2370 com::sun::star::uno::Sequence< com::sun::star::uno::Any > ScAccessibleDocument::GetScAccFlowToSequence()
2371 {
2372     if ( getAccessibleChildCount() )
2373     {
2374         uno::Reference < XAccessible > xSCTableAcc = getAccessibleChild( 0 ); // table
2375         if ( xSCTableAcc.is() )
2376         {
2377             uno::Reference < XAccessibleSelection > xAccSelection( xSCTableAcc, uno::UNO_QUERY );
2378             sal_Int32 nSelCount = xAccSelection->getSelectedAccessibleChildCount();
2379             if( nSelCount )
2380             {
2381                 uno::Reference < XAccessible > xSel = xAccSelection->getSelectedAccessibleChild( 0 ); // selected cell
2382                 if ( xSel.is() )
2383                 {
2384                     uno::Reference < XAccessibleContext > xSelContext( xSel->getAccessibleContext() );
2385                     if ( xSelContext.is() )
2386                     {
2387                         if ( xSelContext->getAccessibleRole() == AccessibleRole::TABLE_CELL )
2388                         {
2389                             sal_Int32 nParaCount = 0;
2390                             uno::Sequence <uno::Any> aSequence(nSelCount);
2391                             for ( sal_Int32 i = 0; i < nSelCount; i++ )
2392                             {
2393                                 xSel = xAccSelection->getSelectedAccessibleChild( i )   ;
2394                                 if ( xSel.is() )
2395                                 {
2396                                     xSelContext = xSel->getAccessibleContext();
2397                                     if ( xSelContext.is() )
2398                                     {
2399                                         if ( xSelContext->getAccessibleRole() == AccessibleRole::TABLE_CELL )
2400                                         {
2401                                             aSequence[nParaCount] = uno::makeAny( xSel );
2402                                             nParaCount++;
2403                                         }
2404                                     }
2405                                 }
2406                             }
2407                             return aSequence;
2408                         }
2409                     }
2410                 }
2411             }
2412         }
2413     }
2414     uno::Sequence <uno::Any> aEmpty;
2415     return aEmpty;
2416 }
2417 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >
get_AccFlowTo(const::com::sun::star::uno::Any & rAny,sal_Int32 nType)2418         SAL_CALL ScAccessibleDocument::get_AccFlowTo(const ::com::sun::star::uno::Any& rAny, sal_Int32 nType)
2419 {
2420     const sal_Int32 SPELLCHECKFLOWTO = 1;
2421     const sal_Int32 FINDREPLACEFLOWTO = 2;
2422     if ( nType == SPELLCHECKFLOWTO )
2423     {
2424         uno::Reference< ::com::sun::star::drawing::XShape > xShape;
2425         rAny >>= xShape;
2426         if ( xShape.is() )
2427         {
2428             uno::Reference < XAccessible > xAcc = mpChildrenShapes->GetAccessibleCaption(xShape);
2429             uno::Reference < XAccessibleSelection > xAccSelection( xAcc, uno::UNO_QUERY );
2430             if ( xAccSelection.is() )
2431             {
2432                 if ( xAccSelection->getSelectedAccessibleChildCount() )
2433                 {
2434                     uno::Reference < XAccessible > xSel = xAccSelection->getSelectedAccessibleChild( 0 );
2435                     if ( xSel.is() )
2436                     {
2437                         uno::Reference < XAccessibleContext > xSelContext( xSel->getAccessibleContext() );
2438                         if ( xSelContext.is() )
2439                         {
2440                             //if in sw we find the selected paragraph here
2441                             if ( xSelContext->getAccessibleRole() == AccessibleRole::PARAGRAPH )
2442                             {
2443                                 uno::Sequence<uno::Any> aRet( 1 );
2444                                 aRet[0] = uno::makeAny( xSel );
2445                                 return aRet;
2446                             }
2447                         }
2448                     }
2449                 }
2450             }
2451         }
2452         else
2453         {
2454             if ( getSelectedAccessibleChildCount() )
2455             {
2456                 uno::Reference < XAccessible > xSel = getSelectedAccessibleChild( 0 );
2457                 if ( xSel.is() )
2458                 {
2459                     uno::Reference < XAccessibleContext > xSelContext( xSel->getAccessibleContext() );
2460                     if ( xSelContext.is() )
2461                     {
2462                         uno::Reference < XAccessibleSelection > xAccChildSelection( xSel, uno::UNO_QUERY );
2463                         if ( xAccChildSelection.is() )
2464                         {
2465                             if ( xAccChildSelection->getSelectedAccessibleChildCount() )
2466                             {
2467                                 uno::Reference < XAccessible > xChildSel = xAccChildSelection->getSelectedAccessibleChild( 0 );
2468                                 if ( xChildSel.is() )
2469                                 {
2470                                     uno::Reference < ::com::sun::star::accessibility::XAccessibleContext > xChildSelContext( xChildSel->getAccessibleContext() );
2471                                     if ( xChildSelContext.is() &&
2472                                         xChildSelContext->getAccessibleRole() == ::com::sun::star::accessibility::AccessibleRole::PARAGRAPH )
2473                                     {
2474                                         uno::Sequence<uno::Any> aRet( 1 );
2475                                         aRet[0] = uno::makeAny( xChildSel );
2476                                         return aRet;
2477                                     }
2478                                 }
2479                             }
2480                         }
2481                     }
2482                 }
2483             }
2484         }
2485     }
2486     else if ( nType == FINDREPLACEFLOWTO )
2487     {
2488         sal_Bool bSuccess(sal_False);
2489         rAny >>= bSuccess;
2490         if ( bSuccess )
2491         {
2492             uno::Sequence< uno::Any> aSeq = GetScAccFlowToSequence();
2493             if ( aSeq.getLength() )
2494             {
2495                 return aSeq;
2496             }
2497             else if( mpAccessibleSpreadsheet )
2498             {
2499                 uno::Reference < XAccessible > xFindCellAcc = mpAccessibleSpreadsheet->GetActiveCell();
2500                 // add xFindCellAcc to the return the Sequence
2501                 uno::Sequence< uno::Any> aSeq2(1);
2502                 aSeq2[0] = uno::makeAny( xFindCellAcc );
2503                 return aSeq2;
2504             }
2505         }
2506     }
2507     uno::Sequence< uno::Any> aEmpty;
2508     return aEmpty;
2509 }
SwitchViewFireFocus()2510 void ScAccessibleDocument::SwitchViewFireFocus()
2511 {
2512     if (mpAccessibleSpreadsheet)
2513     {
2514         mpAccessibleSpreadsheet->FireFirstCellFocus();
2515     }
2516 }
2517 
getForeground()2518 sal_Int32 SAL_CALL ScAccessibleDocument::getForeground(  )
2519 {
2520     return COL_BLACK;
2521 }
2522 
getBackground()2523 sal_Int32 SAL_CALL ScAccessibleDocument::getBackground(  )
2524 {
2525     ScUnoGuard aGuard;
2526     IsObjectValid();
2527     return SC_MOD()->GetColorConfig().GetColorValue( ::svtools::DOCCOLOR ).nColor;
2528 }
2529