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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
|
/* -*- 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 <tools/diagnose_ex.h>
#include <rtl/ref.hxx>
#include <cppuhelper/compbase.hxx>
#include <cppuhelper/basemutex.hxx>
#include <com/sun/star/awt/XMouseListener.hpp>
#include <com/sun/star/awt/XMouseMotionListener.hpp>
#include <com/sun/star/presentation/XSlideShowView.hpp>
#include <basegfx/matrix/b2dhommatrix.hxx>
#include <basegfx/numeric/ftools.hxx>
#include <basegfx/point/b2dpoint.hxx>
#include <eventqueue.hxx>
#include <eventmultiplexer.hxx>
#include <listenercontainer.hxx>
#include <delayevent.hxx>
#include <unoview.hxx>
#include <unoviewcontainer.hxx>
#include <functional>
#include <memory>
#include <algorithm>
#include <type_traits>
#include <utility>
#include <vector>
using namespace ::com::sun::star;
namespace
{
// add operator== for weak_ptr, so we can use std::find over lists of them
struct ViewEventHandlerWeakPtrWrapper final {
slideshow::internal::ViewEventHandlerWeakPtr ptr;
ViewEventHandlerWeakPtrWrapper(slideshow::internal::ViewEventHandlerWeakPtr thePtr):
ptr(std::move(thePtr)) {}
bool operator ==(ViewEventHandlerWeakPtrWrapper const & other) const
{ return ptr.lock().get() == other.ptr.lock().get(); }
};
}
// Needed by ImplViewHandlers; see the ListenerOperations<std::weak_ptr<ListenerTargetT>> partial
// specialization in slideshow/source/inc/listenercontainer.hxx:
template<>
struct slideshow::internal::ListenerOperations<ViewEventHandlerWeakPtrWrapper>
{
template< typename ContainerT,
typename FuncT >
static bool notifySingleListener( ContainerT& rContainer,
FuncT func )
{
for( const auto& rCurr : rContainer )
{
std::shared_ptr<ViewEventHandler> pListener( rCurr.ptr.lock() );
if( pListener && func(pListener) )
return true;
}
return false;
}
template< typename ContainerT,
typename FuncT >
static bool notifyAllListeners( ContainerT& rContainer,
FuncT func )
{
bool bRet(false);
for( const auto& rCurr : rContainer )
{
std::shared_ptr<ViewEventHandler> pListener( rCurr.ptr.lock() );
if( pListener.get() &&
FunctionApply<typename ::std::result_of<FuncT (std::shared_ptr<ViewEventHandler> const&)>::type,
std::shared_ptr<ViewEventHandler> >::apply(func,pListener) )
{
bRet = true;
}
}
return bRet;
}
template< typename ContainerT >
static void pruneListeners( ContainerT& rContainer,
size_t nSizeThreshold )
{
if( rContainer.size() <= nSizeThreshold )
return;
ContainerT aAliveListeners;
aAliveListeners.reserve(rContainer.size());
for( const auto& rCurr : rContainer )
{
if( !rCurr.ptr.expired() )
aAliveListeners.push_back( rCurr );
}
std::swap( rContainer, aAliveListeners );
}
};
namespace slideshow::internal {
namespace {
template <typename HandlerT>
class PrioritizedHandlerEntry
{
typedef std::shared_ptr<HandlerT> HandlerSharedPtrT;
HandlerSharedPtrT mpHandler;
double mnPrio;
public:
PrioritizedHandlerEntry( HandlerSharedPtrT const& pHandler,
double nPrio ) :
mpHandler(pHandler),
mnPrio(nPrio)
{}
HandlerSharedPtrT const& getHandler() const { return mpHandler; }
/// To sort according to priority
bool operator<( PrioritizedHandlerEntry const& rRHS ) const
{
// reversed order - high prioritized entries
// should be at the beginning of the queue
return mnPrio > rRHS.mnPrio;
}
/// To permit std::remove in removeHandler template
bool operator==( PrioritizedHandlerEntry const& rRHS ) const
{
// ignore prio, for removal, only the handler ptr matters
return mpHandler == rRHS.mpHandler;
}
};
}
typedef cppu::WeakComponentImplHelper<
awt::XMouseListener,
awt::XMouseMotionListener > Listener_UnoBase;
namespace {
/** Listener class, to decouple UNO lifetime from EventMultiplexer
This class gets registered as the XMouse(Motion)Listener on the
XSlideViews, and passes on the events to the EventMultiplexer (via
EventQueue indirection, to force the events into the main thread)
*/
class EventMultiplexerListener : private cppu::BaseMutex,
public Listener_UnoBase
{
public:
EventMultiplexerListener( EventQueue& rEventQueue,
EventMultiplexerImpl& rEventMultiplexer ) :
Listener_UnoBase( m_aMutex ),
mpEventQueue( &rEventQueue ),
mpEventMultiplexer( &rEventMultiplexer )
{
}
EventMultiplexerListener( const EventMultiplexerListener& ) = delete;
EventMultiplexerListener& operator=( const EventMultiplexerListener& ) = delete;
// WeakComponentImplHelperBase::disposing
virtual void SAL_CALL disposing() override;
private:
virtual void SAL_CALL disposing( const lang::EventObject& Source ) override;
// XMouseListener implementation
virtual void SAL_CALL mousePressed( const awt::MouseEvent& e ) override;
virtual void SAL_CALL mouseReleased( const awt::MouseEvent& e ) override;
virtual void SAL_CALL mouseEntered( const awt::MouseEvent& e ) override;
virtual void SAL_CALL mouseExited( const awt::MouseEvent& e ) override;
// XMouseMotionListener implementation
virtual void SAL_CALL mouseDragged( const awt::MouseEvent& e ) override;
virtual void SAL_CALL mouseMoved( const awt::MouseEvent& e ) override;
EventQueue* mpEventQueue;
EventMultiplexerImpl* mpEventMultiplexer;
};
}
struct EventMultiplexerImpl
{
EventMultiplexerImpl( EventQueue& rEventQueue,
UnoViewContainer const& rViewContainer ) :
mrEventQueue(rEventQueue),
mrViewContainer(rViewContainer),
mxListener( new EventMultiplexerListener(rEventQueue,
*this) ),
maNextEffectHandlers(),
maSlideStartHandlers(),
maSlideEndHandlers(),
maAnimationStartHandlers(),
maAnimationEndHandlers(),
maSlideAnimationsEndHandlers(),
maAudioStoppedHandlers(),
maCommandStopAudioHandlers(),
maPauseHandlers(),
maViewHandlers(),
maViewRepaintHandlers(),
maShapeListenerHandlers(),
maUserPaintEventHandlers(),
maMouseClickHandlers(),
maMouseDoubleClickHandlers(),
maMouseMoveHandlers(),
maHyperlinkHandlers(),
mnTimeout(0.0),
mpTickEvent(),
mbIsAutoMode(false)
{}
~EventMultiplexerImpl()
{
if( mxListener.is() )
mxListener->dispose();
}
/// Remove all handlers
void clear();
// actual handler callbacks (get called from the UNO interface
// listeners via event queue)
void mousePressed( const awt::MouseEvent& e );
void mouseReleased( const awt::MouseEvent& e );
void mouseDragged( const awt::MouseEvent& e );
void mouseMoved( const awt::MouseEvent& e );
bool isMouseListenerRegistered() const;
typedef ThreadUnsafeListenerContainer<
PrioritizedHandlerEntry<EventHandler>,
std::vector<
PrioritizedHandlerEntry<EventHandler> > > ImplNextEffectHandlers;
typedef PrioritizedHandlerEntry<MouseEventHandler> ImplMouseHandlerEntry;
typedef ThreadUnsafeListenerContainer<
ImplMouseHandlerEntry,
std::vector<ImplMouseHandlerEntry> > ImplMouseHandlers;
typedef ThreadUnsafeListenerContainer<
EventHandlerSharedPtr,
std::vector<EventHandlerSharedPtr> > ImplEventHandlers;
typedef ThreadUnsafeListenerContainer<
AnimationEventHandlerSharedPtr,
std::vector<AnimationEventHandlerSharedPtr> > ImplAnimationHandlers;
typedef ThreadUnsafeListenerContainer<
PauseEventHandlerSharedPtr,
std::vector<PauseEventHandlerSharedPtr> > ImplPauseHandlers;
typedef ThreadUnsafeListenerContainer<
ViewEventHandlerWeakPtrWrapper,
std::vector<ViewEventHandlerWeakPtrWrapper> > ImplViewHandlers;
typedef ThreadUnsafeListenerContainer<
ViewRepaintHandlerSharedPtr,
std::vector<ViewRepaintHandlerSharedPtr> > ImplRepaintHandlers;
typedef ThreadUnsafeListenerContainer<
ShapeListenerEventHandlerSharedPtr,
std::vector<ShapeListenerEventHandlerSharedPtr> > ImplShapeListenerHandlers;
typedef ThreadUnsafeListenerContainer<
UserPaintEventHandlerSharedPtr,
std::vector<UserPaintEventHandlerSharedPtr> > ImplUserPaintEventHandlers;
typedef ThreadUnsafeListenerContainer<
PrioritizedHandlerEntry<HyperlinkHandler>,
std::vector<PrioritizedHandlerEntry<HyperlinkHandler> > > ImplHyperLinkHandlers;
template <typename XSlideShowViewFunc>
void forEachView( XSlideShowViewFunc pViewMethod );
UnoViewSharedPtr findUnoView(const uno::Reference<
presentation::XSlideShowView>& xView) const;
template< typename RegisterFunction >
void addMouseHandler( ImplMouseHandlers& rHandlerContainer,
const MouseEventHandlerSharedPtr& rHandler,
double nPriority,
RegisterFunction pRegisterListener );
static bool notifyAllAnimationHandlers( ImplAnimationHandlers const& rContainer,
AnimationNodeSharedPtr const& rNode );
bool notifyMouseHandlers(
const ImplMouseHandlers& rQueue,
bool (MouseEventHandler::*pHandlerMethod)(
const awt::MouseEvent& ),
const awt::MouseEvent& e );
bool notifyNextEffect();
/// Called for automatic nextEffect
void tick();
/// Schedules a tick event
void scheduleTick();
/// Schedules tick events, if mbIsAutoMode is true
void handleTicks();
EventQueue& mrEventQueue;
UnoViewContainer const& mrViewContainer;
::rtl::Reference<
EventMultiplexerListener> mxListener;
ImplNextEffectHandlers maNextEffectHandlers;
ImplEventHandlers maSlideStartHandlers;
ImplEventHandlers maSlideEndHandlers;
ImplAnimationHandlers maAnimationStartHandlers;
ImplAnimationHandlers maAnimationEndHandlers;
ImplEventHandlers maSlideAnimationsEndHandlers;
ImplAnimationHandlers maAudioStoppedHandlers;
ImplAnimationHandlers maCommandStopAudioHandlers;
ImplPauseHandlers maPauseHandlers;
ImplViewHandlers maViewHandlers;
ImplRepaintHandlers maViewRepaintHandlers;
ImplShapeListenerHandlers maShapeListenerHandlers;
ImplUserPaintEventHandlers maUserPaintEventHandlers;
ImplMouseHandlers maMouseClickHandlers;
ImplMouseHandlers maMouseDoubleClickHandlers;
ImplMouseHandlers maMouseMoveHandlers;
ImplHyperLinkHandlers maHyperlinkHandlers;
/// automatic next effect mode timeout
double mnTimeout;
/** Holds ptr to optional tick event weakly
When event queue is cleansed, the next
setAutomaticMode(true) call is then able to
regenerate the event.
*/
::std::weak_ptr< Event > mpTickEvent;
bool mbIsAutoMode;
};
void SAL_CALL EventMultiplexerListener::disposing()
{
osl::MutexGuard const guard( m_aMutex );
mpEventQueue = nullptr;
mpEventMultiplexer = nullptr;
}
void SAL_CALL EventMultiplexerListener::disposing(
const lang::EventObject& /*rSource*/ )
{
// there's no real point in acting on this message - after all,
// the event sources are the XSlideShowViews, which must be
// explicitly removed from the slideshow via
// XSlideShow::removeView(). thus, if a XSlideShowView has
// properly removed itself from the slideshow, it will not be
// found here. and if it hasn't, there'll be other references at
// other places within the slideshow, anyway...
}
void SAL_CALL EventMultiplexerListener::mousePressed(
const awt::MouseEvent& e )
{
osl::MutexGuard const guard( m_aMutex );
// notify mouse press. Don't call handlers directly, this
// might not be the main thread!
if( mpEventQueue )
mpEventQueue->addEvent(
makeEvent( std::bind( &EventMultiplexerImpl::mousePressed,
mpEventMultiplexer,
e ),
"EventMultiplexerImpl::mousePressed") );
}
void SAL_CALL EventMultiplexerListener::mouseReleased(
const awt::MouseEvent& e )
{
osl::MutexGuard const guard( m_aMutex );
// notify mouse release. Don't call handlers directly,
// this might not be the main thread!
if( mpEventQueue )
mpEventQueue->addEvent(
makeEvent( std::bind( &EventMultiplexerImpl::mouseReleased,
mpEventMultiplexer,
e ),
"EventMultiplexerImpl::mouseReleased") );
}
void SAL_CALL EventMultiplexerListener::mouseEntered(
const awt::MouseEvent& /*e*/ )
{
// not used here
}
void SAL_CALL EventMultiplexerListener::mouseExited(
const awt::MouseEvent& /*e*/ )
{
// not used here
}
// XMouseMotionListener implementation
void SAL_CALL EventMultiplexerListener::mouseDragged(
const awt::MouseEvent& e )
{
osl::MutexGuard const guard( m_aMutex );
// notify mouse drag. Don't call handlers directly, this
// might not be the main thread!
if( mpEventQueue )
mpEventQueue->addEvent(
makeEvent( std::bind( &EventMultiplexerImpl::mouseDragged,
mpEventMultiplexer,
e ),
"EventMultiplexerImpl::mouseDragged") );
}
void SAL_CALL EventMultiplexerListener::mouseMoved(
const awt::MouseEvent& e )
{
osl::MutexGuard const guard( m_aMutex );
// notify mouse move. Don't call handlers directly, this
// might not be the main thread!
if( mpEventQueue )
mpEventQueue->addEvent(
makeEvent( std::bind( &EventMultiplexerImpl::mouseMoved,
mpEventMultiplexer,
e ),
"EventMultiplexerImpl::mouseMoved") );
}
bool EventMultiplexerImpl::notifyAllAnimationHandlers( ImplAnimationHandlers const& rContainer,
AnimationNodeSharedPtr const& rNode )
{
return rContainer.applyAll(
[&rNode]( const AnimationEventHandlerSharedPtr& pEventHandler )
{ return pEventHandler->handleAnimationEvent( rNode ); } );
}
template <typename XSlideShowViewFunc>
void EventMultiplexerImpl::forEachView( XSlideShowViewFunc pViewMethod )
{
if( !pViewMethod )
return;
// (un)register mouse listener on all views
for( UnoViewVector::const_iterator aIter( mrViewContainer.begin() ),
aEnd( mrViewContainer.end() ); aIter != aEnd; ++aIter )
{
uno::Reference<presentation::XSlideShowView> xView ((*aIter)->getUnoView());
if (xView.is())
{
(xView.get()->*pViewMethod)( mxListener.get() );
}
else
{
OSL_ASSERT(xView.is());
}
}
}
UnoViewSharedPtr EventMultiplexerImpl::findUnoView(
const uno::Reference<presentation::XSlideShowView>& xView) const
{
// find view from which the change originated
UnoViewVector::const_iterator aIter;
const UnoViewVector::const_iterator aEnd ( mrViewContainer.end() );
if( (aIter=std::find_if( mrViewContainer.begin(),
aEnd,
[&xView]( const UnoViewSharedPtr& pView )
{ return xView == pView->getUnoView(); } )) == aEnd )
{
OSL_FAIL("EventMultiplexer::findUnoView(): unexpected message source" );
return UnoViewSharedPtr();
}
return *aIter;
}
template< typename RegisterFunction >
void EventMultiplexerImpl::addMouseHandler(
ImplMouseHandlers& rHandlerContainer,
const MouseEventHandlerSharedPtr& rHandler,
double nPriority,
RegisterFunction pRegisterListener )
{
ENSURE_OR_THROW(
rHandler,
"EventMultiplexer::addMouseHandler(): Invalid handler" );
// register mouse listener on all views
forEachView( pRegisterListener );
// add into sorted container:
rHandlerContainer.addSorted(
typename ImplMouseHandlers::container_type::value_type(
rHandler,
nPriority ));
}
bool EventMultiplexerImpl::isMouseListenerRegistered() const
{
return !(maMouseClickHandlers.isEmpty() &&
maMouseDoubleClickHandlers.isEmpty());
}
void EventMultiplexerImpl::tick()
{
if( !mbIsAutoMode )
return; // this event is just a left-over, ignore
notifyNextEffect();
if( !maNextEffectHandlers.isEmpty() )
{
// still handlers left, schedule next timeout
// event. Will also set mbIsTickEventOn back to true
scheduleTick();
}
}
void EventMultiplexerImpl::scheduleTick()
{
EventSharedPtr pEvent(
makeDelay( [this] () { this->tick(); },
mnTimeout,
"EventMultiplexerImpl::tick with delay"));
// store weak reference to generated event, to notice when
// the event queue gets cleansed (we then have to
// regenerate the tick event!)
mpTickEvent = pEvent;
// enabled auto mode: simply schedule a timeout event,
// which will eventually call our tick() method
mrEventQueue.addEventForNextRound( pEvent );
}
void EventMultiplexerImpl::handleTicks()
{
if( !mbIsAutoMode )
return; // nothing to do, don't need no ticks
EventSharedPtr pTickEvent( mpTickEvent.lock() );
if( pTickEvent )
return; // nothing to do, there's already a tick
// pending
// schedule initial tick (which reschedules itself
// after that, all by itself)
scheduleTick();
}
void EventMultiplexerImpl::clear()
{
// deregister from all views.
if( isMouseListenerRegistered() )
{
for( UnoViewVector::const_iterator aIter=mrViewContainer.begin(),
aEnd=mrViewContainer.end();
aIter!=aEnd;
++aIter )
{
if( (*aIter)->getUnoView().is() )
(*aIter)->getUnoView()->removeMouseListener( mxListener.get() );
}
}
if( !maMouseMoveHandlers.isEmpty() )
{
for( UnoViewVector::const_iterator aIter=mrViewContainer.begin(),
aEnd=mrViewContainer.end();
aIter!=aEnd;
++aIter )
{
if( (*aIter)->getUnoView().is() )
(*aIter)->getUnoView()->removeMouseMotionListener( mxListener.get() );
}
}
// clear all handlers (releases all references)
maNextEffectHandlers.clear();
maSlideStartHandlers.clear();
maSlideEndHandlers.clear();
maAnimationStartHandlers.clear();
maAnimationEndHandlers.clear();
maSlideAnimationsEndHandlers.clear();
maAudioStoppedHandlers.clear();
maCommandStopAudioHandlers.clear();
maPauseHandlers.clear();
maViewHandlers.clear();
maViewRepaintHandlers.clear();
maMouseClickHandlers.clear();
maMouseDoubleClickHandlers.clear();
maMouseMoveHandlers.clear();
maHyperlinkHandlers.clear();
mpTickEvent.reset();
}
// XMouseListener implementation
bool EventMultiplexerImpl::notifyMouseHandlers(
const ImplMouseHandlers& rQueue,
bool (MouseEventHandler::*pHandlerMethod)( const awt::MouseEvent& ),
const awt::MouseEvent& e )
{
uno::Reference<presentation::XSlideShowView> xView(
e.Source, uno::UNO_QUERY );
ENSURE_OR_RETURN_FALSE( xView.is(), "EventMultiplexer::notifyHandlers(): "
"event source is not an XSlideShowView" );
// find corresponding view (to map mouse position into user
// coordinate space)
UnoViewVector::const_iterator aIter;
const UnoViewVector::const_iterator aEnd ( mrViewContainer.end() );
if( (aIter=::std::find_if(
mrViewContainer.begin(),
aEnd,
[&xView]( const UnoViewSharedPtr& pView )
{ return xView == pView->getUnoView(); } )) == aEnd )
{
ENSURE_OR_RETURN_FALSE(
false, "EventMultiplexer::notifyHandlers(): "
"event source not found under registered views" );
}
// convert mouse position to user coordinate space
::basegfx::B2DPoint aPosition( e.X, e.Y );
::basegfx::B2DHomMatrix aMatrix( (*aIter)->getTransformation() );
if( !aMatrix.invert() )
ENSURE_OR_THROW( false, "EventMultiplexer::notifyHandlers():"
" view matrix singular" );
aPosition *= aMatrix;
awt::MouseEvent aEvent( e );
aEvent.X = ::basegfx::fround( aPosition.getX() );
aEvent.Y = ::basegfx::fround( aPosition.getY() );
// fire event on handlers, try in order of precedence. If
// one high-priority handler rejects the event
// (i.e. returns false), try next handler.
return rQueue.apply(
[&pHandlerMethod, &aEvent]( const ImplMouseHandlerEntry& rMouseHandler )
{ return ( ( *rMouseHandler.getHandler() ).*pHandlerMethod )( aEvent ); } );
}
void EventMultiplexerImpl::mousePressed( const awt::MouseEvent& e )
{
// fire double-click events for every second click
sal_Int32 nCurrClickCount = e.ClickCount;
while( nCurrClickCount > 1 &&
notifyMouseHandlers( maMouseDoubleClickHandlers,
&MouseEventHandler::handleMousePressed,
e ))
{
nCurrClickCount -= 2;
}
// fire single-click events for all remaining clicks
while( nCurrClickCount > 0 &&
notifyMouseHandlers( maMouseClickHandlers,
&MouseEventHandler::handleMousePressed,
e ))
{
--nCurrClickCount;
}
}
void EventMultiplexerImpl::mouseReleased( const awt::MouseEvent& e )
{
// fire double-click events for every second click
sal_Int32 nCurrClickCount = e.ClickCount;
while( nCurrClickCount > 1 &&
notifyMouseHandlers( maMouseDoubleClickHandlers,
&MouseEventHandler::handleMouseReleased,
e ))
{
nCurrClickCount -= 2;
}
// fire single-click events for all remaining clicks
while( nCurrClickCount > 0 &&
notifyMouseHandlers( maMouseClickHandlers,
&MouseEventHandler::handleMouseReleased,
e ))
{
--nCurrClickCount;
}
}
void EventMultiplexerImpl::mouseDragged( const awt::MouseEvent& e )
{
notifyMouseHandlers( maMouseMoveHandlers,
&MouseEventHandler::handleMouseDragged,
e );
}
void EventMultiplexerImpl::mouseMoved( const awt::MouseEvent& e )
{
notifyMouseHandlers( maMouseMoveHandlers,
&MouseEventHandler::handleMouseMoved,
e );
}
bool EventMultiplexerImpl::notifyNextEffect()
{
// fire event on handlers, try in order of precedence. If one
// high-priority handler rejects the event (i.e. returns false),
// try next handler.
return maNextEffectHandlers.apply(
[]( const PrioritizedHandlerEntry< EventHandler >& pHandler )
{ return pHandler.getHandler()->handleEvent(); } );
}
EventMultiplexer::EventMultiplexer( EventQueue& rEventQueue,
UnoViewContainer const& rViewContainer ) :
mpImpl( new EventMultiplexerImpl(rEventQueue, rViewContainer) )
{
}
EventMultiplexer::~EventMultiplexer()
{
// outline because of EventMultiplexerImpl's incomplete type
}
void EventMultiplexer::clear()
{
mpImpl->clear();
}
void EventMultiplexer::setAutomaticMode( bool bIsAuto )
{
if( bIsAuto == mpImpl->mbIsAutoMode )
return; // no change, nothing to do
mpImpl->mbIsAutoMode = bIsAuto;
mpImpl->handleTicks();
}
bool EventMultiplexer::getAutomaticMode() const
{
return mpImpl->mbIsAutoMode;
}
void EventMultiplexer::setAutomaticTimeout( double nTimeout )
{
mpImpl->mnTimeout = nTimeout;
}
double EventMultiplexer::getAutomaticTimeout() const
{
return mpImpl->mnTimeout;
}
void EventMultiplexer::addNextEffectHandler(
EventHandlerSharedPtr const& rHandler,
double nPriority )
{
mpImpl->maNextEffectHandlers.addSorted(
EventMultiplexerImpl::ImplNextEffectHandlers::container_type::value_type(
rHandler,
nPriority) );
// Enable tick events, if not done already
mpImpl->handleTicks();
}
void EventMultiplexer::removeNextEffectHandler(
const EventHandlerSharedPtr& rHandler )
{
mpImpl->maNextEffectHandlers.remove(
EventMultiplexerImpl::ImplNextEffectHandlers::container_type::value_type(
rHandler,
0.0) );
}
void EventMultiplexer::addSlideStartHandler(
const EventHandlerSharedPtr& rHandler )
{
mpImpl->maSlideStartHandlers.add( rHandler );
}
void EventMultiplexer::removeSlideStartHandler(
const EventHandlerSharedPtr& rHandler )
{
mpImpl->maSlideStartHandlers.remove( rHandler );
}
void EventMultiplexer::addSlideEndHandler(
const EventHandlerSharedPtr& rHandler )
{
mpImpl->maSlideEndHandlers.add( rHandler );
}
void EventMultiplexer::removeSlideEndHandler(
const EventHandlerSharedPtr& rHandler )
{
mpImpl->maSlideEndHandlers.remove( rHandler );
}
void EventMultiplexer::addAnimationStartHandler(
const AnimationEventHandlerSharedPtr& rHandler )
{
mpImpl->maAnimationStartHandlers.add( rHandler );
}
void EventMultiplexer::removeAnimationStartHandler(
const AnimationEventHandlerSharedPtr& rHandler )
{
mpImpl->maAnimationStartHandlers.remove( rHandler );
}
void EventMultiplexer::addAnimationEndHandler(
const AnimationEventHandlerSharedPtr& rHandler )
{
mpImpl->maAnimationEndHandlers.add( rHandler );
}
void EventMultiplexer::removeAnimationEndHandler(
const AnimationEventHandlerSharedPtr& rHandler )
{
mpImpl->maAnimationEndHandlers.remove( rHandler );
}
void EventMultiplexer::addSlideAnimationsEndHandler(
const EventHandlerSharedPtr& rHandler )
{
mpImpl->maSlideAnimationsEndHandlers.add( rHandler );
}
void EventMultiplexer::removeSlideAnimationsEndHandler(
const EventHandlerSharedPtr& rHandler )
{
mpImpl->maSlideAnimationsEndHandlers.remove( rHandler );
}
void EventMultiplexer::addAudioStoppedHandler(
const AnimationEventHandlerSharedPtr& rHandler )
{
mpImpl->maAudioStoppedHandlers.add( rHandler );
}
void EventMultiplexer::removeAudioStoppedHandler(
const AnimationEventHandlerSharedPtr& rHandler )
{
mpImpl->maAudioStoppedHandlers.remove( rHandler );
}
void EventMultiplexer::addCommandStopAudioHandler(
const AnimationEventHandlerSharedPtr& rHandler )
{
mpImpl->maCommandStopAudioHandlers.add( rHandler );
}
void EventMultiplexer::removeCommandStopAudioHandler(
const AnimationEventHandlerSharedPtr& rHandler )
{
mpImpl->maCommandStopAudioHandlers.remove( rHandler );
}
void EventMultiplexer::addPauseHandler(
const PauseEventHandlerSharedPtr& rHandler )
{
mpImpl->maPauseHandlers.add( rHandler );
}
void EventMultiplexer::removePauseHandler(
const PauseEventHandlerSharedPtr& rHandler )
{
mpImpl->maPauseHandlers.remove( rHandler );
}
void EventMultiplexer::addViewHandler(
const ViewEventHandlerWeakPtr& rHandler )
{
mpImpl->maViewHandlers.add( rHandler );
}
void EventMultiplexer::removeViewHandler( const ViewEventHandlerWeakPtr& rHandler )
{
mpImpl->maViewHandlers.remove( rHandler );
}
void EventMultiplexer::addViewRepaintHandler( const ViewRepaintHandlerSharedPtr& rHandler )
{
mpImpl->maViewRepaintHandlers.add( rHandler );
}
void EventMultiplexer::removeViewRepaintHandler( const ViewRepaintHandlerSharedPtr& rHandler )
{
mpImpl->maViewRepaintHandlers.remove( rHandler );
}
void EventMultiplexer::addShapeListenerHandler( const ShapeListenerEventHandlerSharedPtr& rHandler )
{
mpImpl->maShapeListenerHandlers.add( rHandler );
}
void EventMultiplexer::removeShapeListenerHandler( const ShapeListenerEventHandlerSharedPtr& rHandler )
{
mpImpl->maShapeListenerHandlers.remove( rHandler );
}
void EventMultiplexer::addUserPaintHandler( const UserPaintEventHandlerSharedPtr& rHandler )
{
mpImpl->maUserPaintEventHandlers.add( rHandler );
}
void EventMultiplexer::addClickHandler(
const MouseEventHandlerSharedPtr& rHandler,
double nPriority )
{
mpImpl->addMouseHandler(
mpImpl->maMouseClickHandlers,
rHandler,
nPriority,
mpImpl->isMouseListenerRegistered()
? nullptr
: &presentation::XSlideShowView::addMouseListener );
}
void EventMultiplexer::removeClickHandler(
const MouseEventHandlerSharedPtr& rHandler )
{
mpImpl->maMouseClickHandlers.remove(
EventMultiplexerImpl::ImplMouseHandlers::container_type::value_type(
rHandler,
0.0) );
if( !mpImpl->isMouseListenerRegistered() )
mpImpl->forEachView( &presentation::XSlideShowView::removeMouseListener );
}
void EventMultiplexer::addDoubleClickHandler(
const MouseEventHandlerSharedPtr& rHandler,
double nPriority )
{
mpImpl->addMouseHandler(
mpImpl->maMouseDoubleClickHandlers,
rHandler,
nPriority,
mpImpl->isMouseListenerRegistered()
? nullptr
: &presentation::XSlideShowView::addMouseListener );
}
void EventMultiplexer::removeDoubleClickHandler(
const MouseEventHandlerSharedPtr& rHandler )
{
mpImpl->maMouseDoubleClickHandlers.remove(
EventMultiplexerImpl::ImplMouseHandlers::container_type::value_type(
rHandler,
0.0) );
if( !mpImpl->isMouseListenerRegistered() )
mpImpl->forEachView( &presentation::XSlideShowView::removeMouseListener );
}
void EventMultiplexer::addMouseMoveHandler(
const MouseEventHandlerSharedPtr& rHandler,
double nPriority )
{
mpImpl->addMouseHandler(
mpImpl->maMouseMoveHandlers,
rHandler,
nPriority,
mpImpl->maMouseMoveHandlers.isEmpty()
? &presentation::XSlideShowView::addMouseMotionListener
: nullptr );
}
void EventMultiplexer::removeMouseMoveHandler(
const MouseEventHandlerSharedPtr& rHandler )
{
mpImpl->maMouseMoveHandlers.remove(
EventMultiplexerImpl::ImplMouseHandlers::container_type::value_type(
rHandler,
0.0) );
if( mpImpl->maMouseMoveHandlers.isEmpty() )
mpImpl->forEachView(
&presentation::XSlideShowView::removeMouseMotionListener );
}
void EventMultiplexer::addHyperlinkHandler( const HyperlinkHandlerSharedPtr& rHandler,
double nPriority )
{
mpImpl->maHyperlinkHandlers.addSorted(
EventMultiplexerImpl::ImplHyperLinkHandlers::container_type::value_type(
rHandler,
nPriority) );
}
void EventMultiplexer::removeHyperlinkHandler( const HyperlinkHandlerSharedPtr& rHandler )
{
mpImpl->maHyperlinkHandlers.remove(
EventMultiplexerImpl::ImplHyperLinkHandlers::container_type::value_type(
rHandler,
0.0) );
}
void EventMultiplexer::notifyShapeListenerAdded(
const uno::Reference<drawing::XShape>& xShape )
{
mpImpl->maShapeListenerHandlers.applyAll(
[&xShape]( const ShapeListenerEventHandlerSharedPtr& pHandler )
{ return pHandler->listenerAdded( xShape ); } );
}
void EventMultiplexer::notifyShapeListenerRemoved(
const uno::Reference<drawing::XShape>& xShape )
{
mpImpl->maShapeListenerHandlers.applyAll(
[&xShape]( const ShapeListenerEventHandlerSharedPtr& pHandler )
{ return pHandler->listenerRemoved( xShape ); } );
}
void EventMultiplexer::notifyUserPaintColor( RGBColor const& rUserColor )
{
mpImpl->maUserPaintEventHandlers.applyAll(
[&rUserColor]( const UserPaintEventHandlerSharedPtr& pHandler )
{ return pHandler->colorChanged( rUserColor ); } );
}
void EventMultiplexer::notifyUserPaintStrokeWidth( double rUserStrokeWidth )
{
mpImpl->maUserPaintEventHandlers.applyAll(
[&rUserStrokeWidth]( const UserPaintEventHandlerSharedPtr& pHandler )
{ return pHandler->widthChanged( rUserStrokeWidth ); } );
}
void EventMultiplexer::notifyUserPaintDisabled()
{
mpImpl->maUserPaintEventHandlers.applyAll(
std::mem_fn(&UserPaintEventHandler::disable));
}
void EventMultiplexer::notifySwitchPenMode(){
mpImpl->maUserPaintEventHandlers.applyAll(
std::mem_fn(&UserPaintEventHandler::switchPenMode));
}
void EventMultiplexer::notifySwitchEraserMode(){
mpImpl->maUserPaintEventHandlers.applyAll(
std::mem_fn(&UserPaintEventHandler::switchEraserMode));
}
//adding erasing all ink features with UserPaintOverlay
void EventMultiplexer::notifyEraseAllInk( bool bEraseAllInk )
{
mpImpl->maUserPaintEventHandlers.applyAll(
[&bEraseAllInk]( const UserPaintEventHandlerSharedPtr& pHandler )
{ return pHandler->eraseAllInkChanged( bEraseAllInk ); } );
}
//adding erasing features with UserPaintOverlay
void EventMultiplexer::notifyEraseInkWidth( sal_Int32 rEraseInkSize )
{
mpImpl->maUserPaintEventHandlers.applyAll(
[&rEraseInkSize]( const UserPaintEventHandlerSharedPtr& pHandler )
{ return pHandler->eraseInkWidthChanged( rEraseInkSize ); } );
}
bool EventMultiplexer::notifyNextEffect()
{
return mpImpl->notifyNextEffect();
}
void EventMultiplexer::notifySlideStartEvent()
{
mpImpl->maSlideStartHandlers.applyAll(
std::mem_fn(&EventHandler::handleEvent) );
}
bool EventMultiplexer::notifySlideEndEvent()
{
return mpImpl->maSlideEndHandlers.applyAll(
std::mem_fn(&EventHandler::handleEvent) );
}
bool EventMultiplexer::notifyAnimationStart(
const AnimationNodeSharedPtr& rNode )
{
return EventMultiplexerImpl::notifyAllAnimationHandlers( mpImpl->maAnimationStartHandlers,
rNode );
}
bool EventMultiplexer::notifyAnimationEnd(
const AnimationNodeSharedPtr& rNode )
{
return EventMultiplexerImpl::notifyAllAnimationHandlers( mpImpl->maAnimationEndHandlers,
rNode );
}
bool EventMultiplexer::notifySlideAnimationsEnd()
{
return mpImpl->maSlideAnimationsEndHandlers.applyAll(
std::mem_fn(&EventHandler::handleEvent));
}
bool EventMultiplexer::notifyAudioStopped(
const AnimationNodeSharedPtr& rNode )
{
return EventMultiplexerImpl::notifyAllAnimationHandlers(
mpImpl->maAudioStoppedHandlers,
rNode );
}
bool EventMultiplexer::notifyCommandStopAudio(
const AnimationNodeSharedPtr& rNode )
{
return EventMultiplexerImpl::notifyAllAnimationHandlers(
mpImpl->maCommandStopAudioHandlers,
rNode );
}
void EventMultiplexer::notifyPauseMode( bool bPauseShow )
{
mpImpl->maPauseHandlers.applyAll(
[&bPauseShow]( const PauseEventHandlerSharedPtr& pHandler )
{ return pHandler->handlePause( bPauseShow ); } );
}
void EventMultiplexer::notifyViewAdded( const UnoViewSharedPtr& rView )
{
ENSURE_OR_THROW( rView, "EventMultiplexer::notifyViewAdded(): Invalid view");
// register event listener
uno::Reference<presentation::XSlideShowView> const rUnoView(
rView->getUnoView() );
if( mpImpl->isMouseListenerRegistered() )
rUnoView->addMouseListener(
mpImpl->mxListener.get() );
if( !mpImpl->maMouseMoveHandlers.isEmpty() )
rUnoView->addMouseMotionListener(
mpImpl->mxListener.get() );
mpImpl->maViewHandlers.applyAll(
[&rView]( const ViewEventHandlerWeakPtr& pHandler )
{ return pHandler.lock()->viewAdded( rView ); } );
}
void EventMultiplexer::notifyViewRemoved( const UnoViewSharedPtr& rView )
{
ENSURE_OR_THROW( rView,
"EventMultiplexer::removeView(): Invalid view" );
// revoke event listeners
uno::Reference<presentation::XSlideShowView> const rUnoView(
rView->getUnoView() );
if( mpImpl->isMouseListenerRegistered() )
rUnoView->removeMouseListener(
mpImpl->mxListener.get() );
if( !mpImpl->maMouseMoveHandlers.isEmpty() )
rUnoView->removeMouseMotionListener(
mpImpl->mxListener.get() );
mpImpl->maViewHandlers.applyAll(
[&rView]( const ViewEventHandlerWeakPtr& pHandler )
{ return pHandler.lock()->viewRemoved( rView ); } );
}
void EventMultiplexer::notifyViewChanged( const UnoViewSharedPtr& rView )
{
mpImpl->maViewHandlers.applyAll(
[&rView]( const ViewEventHandlerWeakPtr& pHandler )
{ return pHandler.lock()->viewChanged( rView ); } );
}
void EventMultiplexer::notifyViewChanged( const uno::Reference<presentation::XSlideShowView>& xView )
{
UnoViewSharedPtr pView( mpImpl->findUnoView(xView) );
if( !pView )
return; // view not registered here
notifyViewChanged( pView );
}
void EventMultiplexer::notifyViewsChanged()
{
mpImpl->maViewHandlers.applyAll(
std::mem_fn( &ViewEventHandler::viewsChanged ));
}
void EventMultiplexer::notifyViewClobbered(
const uno::Reference<presentation::XSlideShowView>& xView )
{
UnoViewSharedPtr pView( mpImpl->findUnoView(xView) );
if( !pView )
return; // view not registered here
mpImpl->maViewRepaintHandlers.applyAll(
[&pView]( const ViewRepaintHandlerSharedPtr& pHandler )
{ return pHandler->viewClobbered( pView ); } );
}
void EventMultiplexer::notifyHyperlinkClicked(
OUString const& hyperLink )
{
mpImpl->maHyperlinkHandlers.apply(
[&hyperLink]( const PrioritizedHandlerEntry< HyperlinkHandler >& pHandler )
{ return pHandler.getHandler()->handleHyperlink( hyperLink ); } );
}
} // namespace presentation
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|