forked from LibreOffice/core
/
accmap.cxx
3532 lines (3189 loc) · 128 KB
/
accmap.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <rtl/ref.hxx>
#include <cppuhelper/weakref.hxx>
#include <vcl/window.hxx>
#include <svx/svdmodel.hxx>
#include <svx/unomod.hxx>
#include <algorithm>
#include <map>
#include <list>
#include <vector>
#include <accmap.hxx>
#include "acccontext.hxx"
#include "accdoc.hxx"
#include <strings.hrc>
#include "accpreview.hxx"
#include "accpage.hxx"
#include "accpara.hxx"
#include "accheaderfooter.hxx"
#include "accfootnote.hxx"
#include "acctextframe.hxx"
#include "accgraphic.hxx"
#include "accembedded.hxx"
#include "acccell.hxx"
#include "acctable.hxx"
#include <fesh.hxx>
#include <rootfrm.hxx>
#include <txtfrm.hxx>
#include <hffrm.hxx>
#include <ftnfrm.hxx>
#include <cellfrm.hxx>
#include <tabfrm.hxx>
#include <pagefrm.hxx>
#include <flyfrm.hxx>
#include <ndtyp.hxx>
#include <IDocumentDrawModelAccess.hxx>
#include <svx/AccessibleShapeInfo.hxx>
#include <svx/ShapeTypeHandler.hxx>
#include <svx/SvxShapeTypes.hxx>
#include <svx/svdpage.hxx>
#include <com/sun/star/accessibility/AccessibleEventId.hpp>
#include <com/sun/star/accessibility/AccessibleStateType.hpp>
#include <com/sun/star/accessibility/AccessibleRole.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/document/XShapeEventBroadcaster.hpp>
#include <cppuhelper/implbase.hxx>
#include <comphelper/interfacecontainer2.hxx>
#include <pagepreviewlayout.hxx>
#include <dcontact.hxx>
#include <svx/unoapi.hxx>
#include <svx/svdmark.hxx>
#include <doc.hxx>
#include <drawdoc.hxx>
#include <pam.hxx>
#include <ndtxt.hxx>
#include <dflyobj.hxx>
#include <prevwpage.hxx>
#include <calbck.hxx>
#include <undobj.hxx>
#include <tools/diagnose_ex.h>
#include <tools/debug.hxx>
using namespace ::com::sun::star;
using namespace ::com::sun::star::accessibility;
using namespace ::sw::access;
namespace {
struct SwFrameFunc
{
bool operator()( const SwFrame * p1, const SwFrame * p2) const
{
return p1 < p2;
}
};
}
class SwAccessibleContextMap_Impl
{
public:
typedef const SwFrame * key_type;
typedef uno::WeakReference < XAccessible > mapped_type;
typedef std::pair<const key_type,mapped_type> value_type;
typedef SwFrameFunc key_compare;
typedef std::map<key_type,mapped_type,key_compare>::iterator iterator;
typedef std::map<key_type,mapped_type,key_compare>::const_iterator const_iterator;
private:
std::map <key_type,mapped_type,key_compare> maMap;
public:
#if OSL_DEBUG_LEVEL > 0
bool mbLocked;
#endif
SwAccessibleContextMap_Impl()
#if OSL_DEBUG_LEVEL > 0
: mbLocked( false )
#endif
{}
iterator begin() { return maMap.begin(); }
iterator end() { return maMap.end(); }
bool empty() const { return maMap.empty(); }
void clear() { maMap.clear(); }
iterator find(const key_type& key) { return maMap.find(key); }
template<class... Args>
std::pair<iterator,bool> emplace(Args&&... args) { return maMap.emplace(std::forward<Args>(args)...); }
iterator erase(const_iterator const & pos) { return maMap.erase(pos); }
};
namespace {
class SwDrawModellListener_Impl : public SfxListener,
public ::cppu::WeakImplHelper< document::XShapeEventBroadcaster >
{
mutable ::osl::Mutex maListenerMutex;
::comphelper::OInterfaceContainerHelper2 maEventListeners;
std::unordered_map<css::uno::Reference< css::drawing::XShape >, css::uno::Reference< css::document::XShapeEventListener >> maShapeListeners;
SdrModel *mpDrawModel;
protected:
virtual ~SwDrawModellListener_Impl() override;
public:
explicit SwDrawModellListener_Impl( SdrModel *pDrawModel );
// css::document::XEventBroadcaster
virtual void SAL_CALL addEventListener( const uno::Reference< document::XEventListener >& xListener ) override;
virtual void SAL_CALL removeEventListener( const uno::Reference< document::XEventListener >& xListener ) override;
// css::document::XShapeEventBroadcaster
virtual void SAL_CALL addShapeEventListener( const css::uno::Reference< css::drawing::XShape >& xShape, const css::uno::Reference< css::document::XShapeEventListener >& xListener ) override;
virtual void SAL_CALL removeShapeEventListener( const css::uno::Reference< css::drawing::XShape >& xShape, const css::uno::Reference< css::document::XShapeEventListener >& xListener ) override;
virtual void Notify( SfxBroadcaster& rBC, const SfxHint& rHint ) override;
void Dispose();
};
}
SwDrawModellListener_Impl::SwDrawModellListener_Impl( SdrModel *pDrawModel ) :
maEventListeners( maListenerMutex ),
mpDrawModel( pDrawModel )
{
StartListening( *mpDrawModel );
}
SwDrawModellListener_Impl::~SwDrawModellListener_Impl()
{
Dispose();
}
void SAL_CALL SwDrawModellListener_Impl::addEventListener( const uno::Reference< document::XEventListener >& xListener )
{
maEventListeners.addInterface( xListener );
}
void SAL_CALL SwDrawModellListener_Impl::removeEventListener( const uno::Reference< document::XEventListener >& xListener )
{
maEventListeners.removeInterface( xListener );
}
void SAL_CALL SwDrawModellListener_Impl::addShapeEventListener(
const css::uno::Reference< css::drawing::XShape >& xShape,
const uno::Reference< document::XShapeEventListener >& xListener )
{
assert(xShape.is() && "no shape?");
osl::MutexGuard aGuard(maListenerMutex);
auto rv = maShapeListeners.emplace(xShape, xListener);
assert(rv.second && "duplicate listener?");
(void)rv;
}
void SAL_CALL SwDrawModellListener_Impl::removeShapeEventListener(
const css::uno::Reference< css::drawing::XShape >& xShape,
const uno::Reference< document::XShapeEventListener >& xListener )
{
osl::MutexGuard aGuard(maListenerMutex);
auto it = maShapeListeners.find(xShape);
if (it != maShapeListeners.end())
{
assert(it->second == xListener);
(void)xListener;
maShapeListeners.erase(it);
}
}
void SwDrawModellListener_Impl::Notify( SfxBroadcaster& /*rBC*/,
const SfxHint& rHint )
{
// do not broadcast notifications for writer fly frames, because there
// are no shapes that need to know about them.
if (rHint.GetId() != SfxHintId::ThisIsAnSdrHint)
return;
const SdrHint *pSdrHint = static_cast<const SdrHint*>( &rHint );
if (pSdrHint->GetObject() &&
( dynamic_cast< const SwFlyDrawObj* >(pSdrHint->GetObject()) != nullptr ||
dynamic_cast< const SwVirtFlyDrawObj* >(pSdrHint->GetObject()) != nullptr ||
typeid(SdrObject) == typeid(pSdrHint->GetObject()) ) )
{
return;
}
OSL_ENSURE( mpDrawModel, "draw model listener is disposed" );
if( !mpDrawModel )
return;
document::EventObject aEvent;
if( !SvxUnoDrawMSFactory::createEvent( mpDrawModel, pSdrHint, aEvent ) )
return;
::comphelper::OInterfaceIteratorHelper2 aIter( maEventListeners );
while( aIter.hasMoreElements() )
{
uno::Reference < document::XEventListener > xListener( aIter.next(),
uno::UNO_QUERY );
try
{
xListener->notifyEvent( aEvent );
}
catch( uno::RuntimeException const & )
{
TOOLS_WARN_EXCEPTION("sw.a11y", "Runtime exception caught while notifying shape");
}
}
// right now, we're only handling the specific event necessary to fix this performance problem
if (pSdrHint->GetKind() == SdrHintKind::ObjectChange)
{
auto pSdrObject = const_cast<SdrObject*>(pSdrHint->GetObject());
uno::Reference<drawing::XShape> xShape(pSdrObject->getUnoShape(), uno::UNO_QUERY);
osl::MutexGuard aGuard(maListenerMutex);
auto it = maShapeListeners.find(xShape);
if (it != maShapeListeners.end())
it->second->notifyShapeEvent(aEvent);
}
}
void SwDrawModellListener_Impl::Dispose()
{
if (mpDrawModel != nullptr) {
EndListening( *mpDrawModel );
}
mpDrawModel = nullptr;
}
namespace {
struct SwShapeFunc
{
bool operator()( const SdrObject * p1, const SdrObject * p2) const
{
return p1 < p2;
}
};
}
typedef std::pair < const SdrObject *, ::rtl::Reference < ::accessibility::AccessibleShape > > SwAccessibleObjShape_Impl;
class SwAccessibleShapeMap_Impl
{
public:
typedef const SdrObject * key_type;
typedef uno::WeakReference<XAccessible> mapped_type;
typedef std::pair<const key_type,mapped_type> value_type;
typedef SwShapeFunc key_compare;
typedef std::map<key_type,mapped_type,key_compare>::iterator iterator;
typedef std::map<key_type,mapped_type,key_compare>::const_iterator const_iterator;
private:
::accessibility::AccessibleShapeTreeInfo maInfo;
std::map<key_type,mapped_type,SwShapeFunc> maMap;
public:
explicit SwAccessibleShapeMap_Impl( SwAccessibleMap const *pMap )
: maMap()
{
maInfo.SetSdrView( pMap->GetShell()->GetDrawView() );
maInfo.SetDevice( pMap->GetShell()->GetWin() );
maInfo.SetViewForwarder( pMap );
uno::Reference < document::XShapeEventBroadcaster > xModelBroadcaster =
new SwDrawModellListener_Impl(
pMap->GetShell()->getIDocumentDrawModelAccess().GetOrCreateDrawModel() );
maInfo.SetModelBroadcaster( xModelBroadcaster );
}
~SwAccessibleShapeMap_Impl();
const ::accessibility::AccessibleShapeTreeInfo& GetInfo() const { return maInfo; }
std::unique_ptr<SwAccessibleObjShape_Impl[]> Copy( size_t& rSize,
const SwFEShell *pFESh,
SwAccessibleObjShape_Impl **pSelShape ) const;
iterator end() { return maMap.end(); }
const_iterator cbegin() const { return maMap.cbegin(); }
const_iterator cend() const { return maMap.cend(); }
bool empty() const { return maMap.empty(); }
iterator find(const key_type& key) { return maMap.find(key); }
template<class... Args>
std::pair<iterator,bool> emplace(Args&&... args) { return maMap.emplace(std::forward<Args>(args)...); }
iterator erase(const_iterator const & pos) { return maMap.erase(pos); }
};
SwAccessibleShapeMap_Impl::~SwAccessibleShapeMap_Impl()
{
uno::Reference < document::XEventBroadcaster > xBrd( maInfo.GetModelBroadcaster() );
if( xBrd.is() )
static_cast < SwDrawModellListener_Impl * >( xBrd.get() )->Dispose();
}
std::unique_ptr<SwAccessibleObjShape_Impl[]>
SwAccessibleShapeMap_Impl::Copy(
size_t& rSize, const SwFEShell *pFESh,
SwAccessibleObjShape_Impl **pSelStart ) const
{
std::unique_ptr<SwAccessibleObjShape_Impl[]> pShapes;
SwAccessibleObjShape_Impl *pSelShape = nullptr;
size_t nSelShapes = pFESh ? pFESh->IsObjSelected() : 0;
rSize = maMap.size();
if( rSize > 0 )
{
pShapes.reset(new SwAccessibleObjShape_Impl[rSize]);
SwAccessibleObjShape_Impl *pShape = pShapes.get();
pSelShape = &(pShapes[rSize]);
for( const auto& rEntry : maMap )
{
const SdrObject *pObj = rEntry.first;
uno::Reference < XAccessible > xAcc( rEntry.second );
if( nSelShapes && pFESh && pFESh->IsObjSelected( *pObj ) )
{
// selected objects are inserted from the back
--pSelShape;
pSelShape->first = pObj;
pSelShape->second =
static_cast < ::accessibility::AccessibleShape* >(
xAcc.get() );
--nSelShapes;
}
else
{
pShape->first = pObj;
pShape->second =
static_cast < ::accessibility::AccessibleShape* >(
xAcc.get() );
++pShape;
}
}
assert(pSelShape == pShape);
}
if( pSelStart )
*pSelStart = pSelShape;
return pShapes;
}
struct SwAccessibleEvent_Impl
{
public:
enum EventType { CARET_OR_STATES,
INVALID_CONTENT,
POS_CHANGED,
CHILD_POS_CHANGED,
SHAPE_SELECTION,
DISPOSE,
INVALID_ATTR };
private:
SwRect maOldBox; // the old bounds for CHILD_POS_CHANGED
// and POS_CHANGED
uno::WeakReference < XAccessible > mxAcc; // The object that fires the event
SwAccessibleChild const maFrameOrObj; // the child for CHILD_POS_CHANGED and
// the same as xAcc for any other
// event type
EventType meType; // The event type
AccessibleStates mnStates; // check states or update caret pos
public:
const SwFrame* mpParentFrame; // The object that fires the event
bool IsNoXaccParentFrame() const
{
return CHILD_POS_CHANGED == meType && mpParentFrame != nullptr;
}
public:
SwAccessibleEvent_Impl( EventType eT,
SwAccessibleContext *pA,
const SwAccessibleChild& rFrameOrObj )
: mxAcc( pA ),
maFrameOrObj( rFrameOrObj ),
meType( eT ),
mnStates( AccessibleStates::NONE ),
mpParentFrame( nullptr )
{}
SwAccessibleEvent_Impl( EventType eT,
const SwAccessibleChild& rFrameOrObj )
: maFrameOrObj( rFrameOrObj ),
meType( eT ),
mnStates( AccessibleStates::NONE ),
mpParentFrame( nullptr )
{
assert(SwAccessibleEvent_Impl::DISPOSE == meType &&
"wrong event constructor, DISPOSE only");
}
explicit SwAccessibleEvent_Impl( EventType eT )
: meType( eT ),
mnStates( AccessibleStates::NONE ),
mpParentFrame( nullptr )
{
assert(SwAccessibleEvent_Impl::SHAPE_SELECTION == meType &&
"wrong event constructor, SHAPE_SELECTION only" );
}
SwAccessibleEvent_Impl( EventType eT,
SwAccessibleContext *pA,
const SwAccessibleChild& rFrameOrObj,
const SwRect& rR )
: maOldBox( rR ),
mxAcc( pA ),
maFrameOrObj( rFrameOrObj ),
meType( eT ),
mnStates( AccessibleStates::NONE ),
mpParentFrame( nullptr )
{
assert((SwAccessibleEvent_Impl::CHILD_POS_CHANGED == meType ||
SwAccessibleEvent_Impl::POS_CHANGED == meType) &&
"wrong event constructor, (CHILD_)POS_CHANGED only" );
}
SwAccessibleEvent_Impl( EventType eT,
SwAccessibleContext *pA,
const SwAccessibleChild& rFrameOrObj,
const AccessibleStates _nStates )
: mxAcc( pA ),
maFrameOrObj( rFrameOrObj ),
meType( eT ),
mnStates( _nStates ),
mpParentFrame( nullptr )
{
assert( SwAccessibleEvent_Impl::CARET_OR_STATES == meType &&
"wrong event constructor, CARET_OR_STATES only" );
}
SwAccessibleEvent_Impl( EventType eT, const SwFrame *pParentFrame,
const SwAccessibleChild& rFrameOrObj, const SwRect& rR ) :
maOldBox( rR ),
maFrameOrObj( rFrameOrObj ),
meType( eT ),
mnStates( AccessibleStates::NONE ),
mpParentFrame( pParentFrame )
{
assert( SwAccessibleEvent_Impl::CHILD_POS_CHANGED == meType &&
"wrong event constructor, CHILD_POS_CHANGED only" );
}
// <SetType(..)> only used in method <SwAccessibleMap::AppendEvent(..)>
void SetType( EventType eT )
{
meType = eT;
}
EventType GetType() const
{
return meType;
}
::rtl::Reference < SwAccessibleContext > GetContext() const
{
uno::Reference < XAccessible > xTmp( mxAcc );
::rtl::Reference < SwAccessibleContext > xAccImpl(
static_cast<SwAccessibleContext*>( xTmp.get() ) );
return xAccImpl;
}
const SwRect& GetOldBox() const
{
return maOldBox;
}
// <SetOldBox(..)> only used in method <SwAccessibleMap::AppendEvent(..)>
void SetOldBox( const SwRect& rOldBox )
{
maOldBox = rOldBox;
}
const SwAccessibleChild& GetFrameOrObj() const
{
return maFrameOrObj;
}
// <SetStates(..)> only used in method <SwAccessibleMap::AppendEvent(..)>
void SetStates( AccessibleStates _nStates )
{
mnStates |= _nStates;
}
bool IsUpdateCursorPos() const
{
return bool(mnStates & AccessibleStates::CARET);
}
bool IsInvalidateStates() const
{
return bool(mnStates & (AccessibleStates::EDITABLE | AccessibleStates::OPAQUE));
}
bool IsInvalidateRelation() const
{
return bool(mnStates & (AccessibleStates::RELATION_FROM | AccessibleStates::RELATION_TO));
}
bool IsInvalidateTextSelection() const
{
return bool( mnStates & AccessibleStates::TEXT_SELECTION_CHANGED );
}
bool IsInvalidateTextAttrs() const
{
return bool( mnStates & AccessibleStates::TEXT_ATTRIBUTE_CHANGED );
}
AccessibleStates GetStates() const
{
return mnStates;
}
AccessibleStates GetAllStates() const
{
return mnStates;
}
};
class SwAccessibleEventList_Impl
{
std::list<SwAccessibleEvent_Impl> maEvents;
bool mbFiring;
public:
SwAccessibleEventList_Impl()
: mbFiring( false )
{}
void SetFiring()
{
mbFiring = true;
}
bool IsFiring() const
{
return mbFiring;
}
void MoveMissingXAccToEnd();
size_t size() const { return maEvents.size(); }
std::list<SwAccessibleEvent_Impl>::iterator begin() { return maEvents.begin(); }
std::list<SwAccessibleEvent_Impl>::iterator end() { return maEvents.end(); }
std::list<SwAccessibleEvent_Impl>::iterator insert( const std::list<SwAccessibleEvent_Impl>::iterator& aIter,
const SwAccessibleEvent_Impl& rEvent )
{
return maEvents.insert( aIter, rEvent );
}
std::list<SwAccessibleEvent_Impl>::iterator erase( const std::list<SwAccessibleEvent_Impl>::iterator& aPos )
{
return maEvents.erase( aPos );
}
};
// see comment in SwAccessibleMap::InvalidatePosOrSize()
// last case "else if(pParent)" for why this surprising hack exists
void SwAccessibleEventList_Impl::MoveMissingXAccToEnd()
{
size_t nSize = size();
if (nSize < 2 )
{
return;
}
SwAccessibleEventList_Impl lstEvent;
for (auto li = begin(); li != end(); )
{
if (li->IsNoXaccParentFrame())
{
lstEvent.insert(lstEvent.end(), *li);
li = erase(li);
}
else
++li;
}
assert(size() + lstEvent.size() == nSize);
maEvents.insert(end(),lstEvent.begin(),lstEvent.end());
assert(size() == nSize);
}
namespace {
struct SwAccessibleChildFunc
{
bool operator()( const SwAccessibleChild& r1,
const SwAccessibleChild& r2 ) const
{
const void *p1 = r1.GetSwFrame()
? static_cast < const void * >( r1.GetSwFrame())
: ( r1.GetDrawObject()
? static_cast < const void * >( r1.GetDrawObject() )
: static_cast < const void * >( r1.GetWindow() ) );
const void *p2 = r2.GetSwFrame()
? static_cast < const void * >( r2.GetSwFrame())
: ( r2.GetDrawObject()
? static_cast < const void * >( r2.GetDrawObject() )
: static_cast < const void * >( r2.GetWindow() ) );
return p1 < p2;
}
};
}
class SwAccessibleEventMap_Impl
{
public:
typedef SwAccessibleChild key_type;
typedef std::list<SwAccessibleEvent_Impl>::iterator mapped_type;
typedef std::pair<const key_type,mapped_type> value_type;
typedef SwAccessibleChildFunc key_compare;
typedef std::map<key_type,mapped_type,key_compare>::iterator iterator;
typedef std::map<key_type,mapped_type,key_compare>::const_iterator const_iterator;
private:
std::map <key_type,mapped_type,key_compare> maMap;
public:
iterator end() { return maMap.end(); }
iterator find(const key_type& key) { return maMap.find(key); }
template<class... Args>
std::pair<iterator,bool> emplace(Args&&... args) { return maMap.emplace(std::forward<Args>(args)...); }
iterator erase(const_iterator const & pos) { return maMap.erase(pos); }
};
namespace {
struct SwAccessibleParaSelection
{
TextFrameIndex const nStartOfSelection;
TextFrameIndex const nEndOfSelection;
SwAccessibleParaSelection(const TextFrameIndex nStartOfSelection_,
const TextFrameIndex nEndOfSelection_)
: nStartOfSelection(nStartOfSelection_)
, nEndOfSelection(nEndOfSelection_)
{}
};
struct SwXAccWeakRefComp
{
bool operator()( const uno::WeakReference<XAccessible>& _rXAccWeakRef1,
const uno::WeakReference<XAccessible>& _rXAccWeakRef2 ) const
{
return _rXAccWeakRef1.get() < _rXAccWeakRef2.get();
}
};
}
class SwAccessibleSelectedParas_Impl
{
public:
typedef uno::WeakReference < XAccessible > key_type;
typedef SwAccessibleParaSelection mapped_type;
typedef std::pair<const key_type,mapped_type> value_type;
typedef SwXAccWeakRefComp key_compare;
typedef std::map<key_type,mapped_type,key_compare>::iterator iterator;
typedef std::map<key_type,mapped_type,key_compare>::const_iterator const_iterator;
private:
std::map<key_type,mapped_type,key_compare> maMap;
public:
iterator begin() { return maMap.begin(); }
iterator end() { return maMap.end(); }
iterator find(const key_type& key) { return maMap.find(key); }
template<class... Args>
std::pair<iterator,bool> emplace(Args&&... args) { return maMap.emplace(std::forward<Args>(args)...); }
iterator erase(const_iterator const & pos) { return maMap.erase(pos); }
};
// helper class that stores preview data
class SwAccPreviewData
{
typedef std::vector<tools::Rectangle> Rectangles;
Rectangles maPreviewRects;
Rectangles maLogicRects;
SwRect maVisArea;
Fraction maScale;
const SwPageFrame *mpSelPage;
/** adjust logic page rectangle to its visible part
@param _iorLogicPgSwRect
input/output parameter - reference to the logic page rectangle, which
has to be adjusted.
@param _rPreviewPgSwRect
input parameter - constant reference to the corresponding preview page
rectangle; needed to determine the visible part of the logic page rectangle.
@param _rPreviewWinSize
input parameter - constant reference to the preview window size in TWIP;
needed to determine the visible part of the logic page rectangle
*/
static void AdjustLogicPgRectToVisibleArea( SwRect& _iorLogicPgSwRect,
const SwRect& _rPreviewPgSwRect,
const Size& _rPreviewWinSize );
public:
SwAccPreviewData();
void Update( const SwAccessibleMap& rAccMap,
const std::vector<std::unique_ptr<PreviewPage>>& _rPreviewPages,
const Fraction& _rScale,
const SwPageFrame* _pSelectedPageFrame,
const Size& _rPreviewWinSize );
void InvalidateSelection( const SwPageFrame* _pSelectedPageFrame );
const SwRect& GetVisArea() const { return maVisArea;}
/** Adjust the MapMode so that the preview page appears at the
* proper position. rPoint identifies the page for which the
* MapMode should be adjusted. If bFromPreview is true, rPoint is
* a preview coordinate; else it's a document coordinate. */
void AdjustMapMode( MapMode& rMapMode,
const Point& rPoint ) const;
const SwPageFrame *GetSelPage() const { return mpSelPage; }
void DisposePage(const SwPageFrame *pPageFrame );
};
SwAccPreviewData::SwAccPreviewData() :
mpSelPage( nullptr )
{
}
void SwAccPreviewData::Update( const SwAccessibleMap& rAccMap,
const std::vector<std::unique_ptr<PreviewPage>>& _rPreviewPages,
const Fraction& _rScale,
const SwPageFrame* _pSelectedPageFrame,
const Size& _rPreviewWinSize )
{
// store preview scaling, maximal preview page size and selected page
maScale = _rScale;
mpSelPage = _pSelectedPageFrame;
// prepare loop on preview pages
maPreviewRects.clear();
maLogicRects.clear();
SwAccessibleChild aPage;
maVisArea.Clear();
// loop on preview pages to calculate <maPreviewRects>, <maLogicRects> and
// <maVisArea>
for ( auto & rpPreviewPage : _rPreviewPages )
{
aPage = rpPreviewPage->pPage;
// add preview page rectangle to <maPreviewRects>
tools::Rectangle aPreviewPgRect( rpPreviewPage->aPreviewWinPos, rpPreviewPage->aPageSize );
maPreviewRects.push_back( aPreviewPgRect );
// add logic page rectangle to <maLogicRects>
SwRect aLogicPgSwRect( aPage.GetBox( rAccMap ) );
tools::Rectangle aLogicPgRect( aLogicPgSwRect.SVRect() );
maLogicRects.push_back( aLogicPgRect );
// union visible area with visible part of logic page rectangle
if ( rpPreviewPage->bVisible )
{
if ( !rpPreviewPage->pPage->IsEmptyPage() )
{
AdjustLogicPgRectToVisibleArea( aLogicPgSwRect,
SwRect( aPreviewPgRect ),
_rPreviewWinSize );
}
if ( maVisArea.IsEmpty() )
maVisArea = aLogicPgSwRect;
else
maVisArea.Union( aLogicPgSwRect );
}
}
}
void SwAccPreviewData::InvalidateSelection( const SwPageFrame* _pSelectedPageFrame )
{
mpSelPage = _pSelectedPageFrame;
assert(mpSelPage);
}
namespace {
struct ContainsPredicate
{
const Point& mrPoint;
explicit ContainsPredicate( const Point& rPoint ) : mrPoint(rPoint) {}
bool operator() ( const tools::Rectangle& rRect ) const
{
return rRect.IsInside( mrPoint );
}
};
}
void SwAccPreviewData::AdjustMapMode( MapMode& rMapMode,
const Point& rPoint ) const
{
// adjust scale
rMapMode.SetScaleX( maScale );
rMapMode.SetScaleY( maScale );
// find proper rectangle
Rectangles::const_iterator aBegin = maLogicRects.begin();
Rectangles::const_iterator aEnd = maLogicRects.end();
Rectangles::const_iterator aFound = std::find_if( aBegin, aEnd,
ContainsPredicate( rPoint ) );
if( aFound != aEnd )
{
// found! set new origin
Point aPoint = (maPreviewRects.begin() + (aFound - aBegin))->TopLeft();
aPoint -= (maLogicRects.begin() + (aFound-aBegin))->TopLeft();
rMapMode.SetOrigin( aPoint );
}
// else: don't adjust MapMode
}
void SwAccPreviewData::DisposePage(const SwPageFrame *pPageFrame )
{
if( mpSelPage == pPageFrame )
mpSelPage = nullptr;
}
// adjust logic page rectangle to its visible part
void SwAccPreviewData::AdjustLogicPgRectToVisibleArea(
SwRect& _iorLogicPgSwRect,
const SwRect& _rPreviewPgSwRect,
const Size& _rPreviewWinSize )
{
// determine preview window rectangle
const SwRect aPreviewWinSwRect( Point( 0, 0 ), _rPreviewWinSize );
// calculate visible preview page rectangle
SwRect aVisPreviewPgSwRect( _rPreviewPgSwRect );
aVisPreviewPgSwRect.Intersection( aPreviewWinSwRect );
// adjust logic page rectangle
SwTwips nTmpDiff;
// left
nTmpDiff = aVisPreviewPgSwRect.Left() - _rPreviewPgSwRect.Left();
if ( nTmpDiff > 0 )
_iorLogicPgSwRect.Left( _iorLogicPgSwRect.Left() + nTmpDiff );
// top
nTmpDiff = aVisPreviewPgSwRect.Top() - _rPreviewPgSwRect.Top();
if ( nTmpDiff > 0 )
_iorLogicPgSwRect.Top( _iorLogicPgSwRect.Top() + nTmpDiff );
// right
nTmpDiff = _rPreviewPgSwRect.Right() - aVisPreviewPgSwRect.Right();
if ( nTmpDiff > 0 )
_iorLogicPgSwRect.Right( _iorLogicPgSwRect.Right() - nTmpDiff );
// bottom
nTmpDiff = _rPreviewPgSwRect.Bottom() - aVisPreviewPgSwRect.Bottom();
if ( nTmpDiff > 0 )
_iorLogicPgSwRect.Bottom( _iorLogicPgSwRect.Bottom() - nTmpDiff );
}
static bool AreInSameTable( const uno::Reference< XAccessible >& rAcc,
const SwFrame *pFrame )
{
bool bRet = false;
if( pFrame && pFrame->IsCellFrame() && rAcc.is() )
{
// Is it in the same table? We check that
// by comparing the last table frame in the
// follow chain, because that's cheaper than
// searching the first one.
SwAccessibleContext *pAccImpl =
static_cast< SwAccessibleContext *>( rAcc.get() );
if( pAccImpl->GetFrame()->IsCellFrame() )
{
const SwTabFrame *pTabFrame1 = pAccImpl->GetFrame()->FindTabFrame();
if (pTabFrame1)
{
while (pTabFrame1->GetFollow())
pTabFrame1 = pTabFrame1->GetFollow();
}
const SwTabFrame *pTabFrame2 = pFrame->FindTabFrame();
if (pTabFrame2)
{
while (pTabFrame2->GetFollow())
pTabFrame2 = pTabFrame2->GetFollow();
}
bRet = (pTabFrame1 == pTabFrame2);
}
}
return bRet;
}
void SwAccessibleMap::FireEvent( const SwAccessibleEvent_Impl& rEvent )
{
::rtl::Reference < SwAccessibleContext > xAccImpl( rEvent.GetContext() );
if (!xAccImpl.is() && rEvent.mpParentFrame != nullptr)
{
SwAccessibleContextMap_Impl::iterator aIter =
mpFrameMap->find( rEvent.mpParentFrame );
if( aIter != mpFrameMap->end() )
{
uno::Reference < XAccessible > xAcc( (*aIter).second );
if (xAcc.is())
{
uno::Reference < XAccessibleContext > xContext(xAcc,uno::UNO_QUERY);
if (xContext.is() && xContext->getAccessibleRole() == AccessibleRole::PARAGRAPH)
{
xAccImpl = static_cast< SwAccessibleContext *>( xAcc.get() );
}
}
}
}
if( SwAccessibleEvent_Impl::SHAPE_SELECTION == rEvent.GetType() )
{
DoInvalidateShapeSelection();
}
else if( xAccImpl.is() && xAccImpl->GetFrame() )
{
if ( rEvent.GetType() != SwAccessibleEvent_Impl::DISPOSE &&
rEvent.IsInvalidateTextAttrs() )
{
xAccImpl->InvalidateAttr();
}
switch( rEvent.GetType() )
{
case SwAccessibleEvent_Impl::INVALID_CONTENT:
xAccImpl->InvalidateContent();
break;
case SwAccessibleEvent_Impl::POS_CHANGED:
xAccImpl->InvalidatePosOrSize( rEvent.GetOldBox() );
break;
case SwAccessibleEvent_Impl::CHILD_POS_CHANGED:
xAccImpl->InvalidateChildPosOrSize( rEvent.GetFrameOrObj(),
rEvent.GetOldBox() );
break;
case SwAccessibleEvent_Impl::DISPOSE:
assert(!"dispose event has been stored");
break;
case SwAccessibleEvent_Impl::INVALID_ATTR:
// nothing to do here - handled above
break;
default:
break;
}
if( SwAccessibleEvent_Impl::DISPOSE != rEvent.GetType() )
{
if( rEvent.IsUpdateCursorPos() )
xAccImpl->InvalidateCursorPos();
if( rEvent.IsInvalidateStates() )
xAccImpl->InvalidateStates( rEvent.GetStates() );
if( rEvent.IsInvalidateRelation() )
{
// both events CONTENT_FLOWS_FROM_RELATION_CHANGED and
// CONTENT_FLOWS_TO_RELATION_CHANGED are possible
if ( rEvent.GetAllStates() & AccessibleStates::RELATION_FROM )
{
xAccImpl->InvalidateRelation(
AccessibleEventId::CONTENT_FLOWS_FROM_RELATION_CHANGED );
}
if ( rEvent.GetAllStates() & AccessibleStates::RELATION_TO )
{
xAccImpl->InvalidateRelation(
AccessibleEventId::CONTENT_FLOWS_TO_RELATION_CHANGED );
}
}
if ( rEvent.IsInvalidateTextSelection() )
{