summaryrefslogtreecommitdiff
path: root/slideshow/source/engine/eventmultiplexer.cxx
blob: 1abecdeccbe78e361029b74f8b672d22bea961ce (plain)
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
/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: eventmultiplexer.cxx,v $
 *
 *  $Revision: 1.8 $
 *
 *  last change: $Author: rt $ $Date: 2006-07-26 07:25:16 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

// must be first
#include "canvas/debug.hxx"
#include "tools.hxx"
#include "eventmultiplexer.hxx"
#include "com/sun/star/awt/SystemPointer.hpp"
#include "com/sun/star/awt/XWindow.hpp"
#include "com/sun/star/awt/MouseButton.hpp"
#include "com/sun/star/presentation/XSlideShowView.hpp"
#include "basegfx/numeric/ftools.hxx"
#include "delayevent.hxx"
#include "boost/bind.hpp"
#include <vector>
#include <hash_map>
#include <algorithm>

namespace css = ::com::sun::star;
using namespace css;

/* Implementation of EventMultiplexer class */

namespace presentation {
namespace internal {

template <typename HandlerT>
class EventMultiplexer::PrioritizedHandlerEntry
{
    typedef PrioritizedHandlerEntry<HandlerT> thisT;
    typedef boost::shared_ptr<HandlerT> HandlerSharedPtrT;
    HandlerSharedPtrT m_pHandler;
    double m_nPrio;

public:
    PrioritizedHandlerEntry( HandlerSharedPtrT const& pHandler, double nPrio )
        : m_pHandler(pHandler), m_nPrio(nPrio) {}

    HandlerSharedPtrT const& getHandler() const { return m_pHandler; }

    /// To sort according to priority
    bool operator<( thisT const& rRHS ) const {
        // reversed order - high prioritized entries
        // should be at the beginning of the queue
        return m_nPrio > rRHS.m_nPrio;
    }

    /// To permit ::std::remove in removeHandler template
    bool operator==( HandlerSharedPtrT const& rRHS ) const {
        return m_pHandler == rRHS;
    }
};

template <typename ContainerT, typename FuncT>
bool EventMultiplexer::notifyAllHandlers( ContainerT const& rContainer,
                                          FuncT const& func,
                                          bool bOperateOnCopy )
{
    if (bOperateOnCopy) {
        osl::ClearableMutexGuard guard( m_aMutex );
        // generate a local copy of all handlers, since we have to
        // release the object mutex before firing.
        ContainerT const local( rContainer );
        guard.clear();
        // true: at least one handler returned true
        // false: not a single handler returned true
        return (std::count_if( local.begin(), local.end(), func ) > 0);
    }
    else {
        // true: at least one handler returned true
        // false: not a single handler returned true
        return (std::count_if( rContainer.begin(), rContainer.end(), func ) >0);
    }
}

template <typename ContainerT, typename FuncT>
bool EventMultiplexer::notifySingleHandler( ContainerT const& rContainer,
                                            FuncT const& func,
                                            bool bOperateOnCopy )
{
    // fire event on handlers, try in order of precedence. If
    // one high-priority handler rejects the event
    // (i.e. returns false), try next handler.

    if (bOperateOnCopy) {
        osl::ClearableMutexGuard guard( m_aMutex );
        // generate a local copy of all handlers, since we have to
        // release the object mutex before firing.
        ContainerT const localHandlers( rContainer );
        guard.clear();
        typename ContainerT::const_iterator const iEnd( localHandlers.end() );
        // true: a handler in this queue processed the event
        // false: no handler in this queue finally processed the event
        return (std::find_if( localHandlers.begin(), iEnd, func ) != iEnd);
    }
    else
    {
        typename ContainerT::const_iterator const iEnd( rContainer.end() );
        // true: a handler in this queue processed the event
        // false: no handler in this queue finally processed the event
        return (std::find_if( rContainer.begin(), iEnd, func ) != iEnd);
    }
}

template <typename ContainerT, typename HandlerT>
void EventMultiplexer::addHandler( ContainerT & rContainer,
                                   boost::shared_ptr<HandlerT> const& pHandler )
{
    ENSURE_AND_THROW(
        pHandler.get() != 0,
        "EventMultiplexer::addHandler(): Invalid handler" );

    osl::MutexGuard const guard( m_aMutex );
    rContainer.push_back( pHandler );
}

template <typename ContainerT, typename HandlerT>
void EventMultiplexer::addPrioritizedHandler(
    ContainerT & rContainer,
    boost::shared_ptr<HandlerT> const& pHandler,
    double nPriority )
{
    ENSURE_AND_THROW(
        pHandler.get() != 0,
        "EventMultiplexer::addHandler(): Invalid handler" );

    osl::MutexGuard const guard( m_aMutex );
    // insert into queue (not sorted yet)
    rContainer.push_back( typename ContainerT::value_type( pHandler,
                                                           nPriority ) );
    if (rContainer.size() > 1) {
        // element was inserted, but now we have to keep the
        // entries sorted
        std::inplace_merge(
            rContainer.begin(), rContainer.end() - 1, rContainer.end() );
    }
    // no need to call inplace_merge otherwise, added
    // entry is the only one on the heap.
}

template< typename Container, typename Handler >
void EventMultiplexer::removeHandler(
    Container & rContainer,
    const ::boost::shared_ptr<Handler>& rHandler )
{
    ::osl::MutexGuard aGuard( m_aMutex );

    ENSURE_AND_THROW(
        rHandler.get(),
        "EventMultiplexer::removeHandler(): Invalid handler" );

    const typename Container::iterator aEnd( rContainer.end() );
    rContainer.erase( ::std::remove(rContainer.begin(), aEnd, rHandler), aEnd );
}

template <typename XSlideShowViewFunc>
void EventMultiplexer::forEachView( XSlideShowViewFunc pViewMethod )
{
    if (pViewMethod) {
        // (un)register mouse listener on all views
        for( UnoViewVector::const_iterator aIter( maViews.begin() ),
                 aEnd( maViews.end() ); aIter != aEnd; ++aIter ) {
            ((*aIter)->getUnoView().get()->*pViewMethod)( this );
        }
    }
}

template< typename RegisterFunction >
void EventMultiplexer::addMouseHandler(
    ImplMouseHandlers&            rHandlerContainer,
    const MouseEventHandlerSharedPtr& rHandler,
    double                        nPriority,
    RegisterFunction              pRegisterListener )
{
    ::osl::MutexGuard aGuard( m_aMutex );

    ENSURE_AND_THROW(
        rHandler.get(),
        "EventMultiplexer::addMouseHandler(): Invalid handler" );

    // register mouse listener on all views
    forEachView( pRegisterListener );

    // sort into container:
    addPrioritizedHandler( rHandlerContainer, rHandler, nPriority );
}

bool EventMultiplexer::isMouseListenerRegistered() const
{
    return !(maMouseClickHandlers.empty() &&
             maMouseDoubleClickHandlers.empty());
}

void EventMultiplexer::tick()
{
    ::osl::MutexGuard aGuard( m_aMutex );

    if( !mbIsAutoMode )
        return; // this event is just a left-over, ignore

    notifyNextEffect();

    if( !maNextEffectHandlers.empty() )
    {
        // still handlers left, schedule next timeout
        // event. Will also set mbIsTickEventOn back to true
        scheduleTick();
    }
}

void EventMultiplexer::scheduleTick()
{
    // TODO(Q3): make robust (no boost::ref()) when
    //           get_pointer(uno::Ref...) is available
    EventSharedPtr pEvent(
        makeDelay( boost::bind( &EventMultiplexer::tick, this ),
                   mnTimeout ) );

    // 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 EventMultiplexer::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 EventMultiplexer::implSetMouseCursor( sal_Int16 nCursor ) const
{
    // change all views to the requested cursor ID
    std::for_each( maViews.begin(), maViews.end(),
                   boost::bind( &View::setMouseCursor, _1, nCursor ) );
}

bool EventMultiplexer::addView( const UnoViewSharedPtr& rView )
{
    ENSURE_AND_THROW( rView.get(), "EventMultiplexer::addView(): Invalid view");

    ::osl::MutexGuard aGuard( m_aMutex );

    // check whether same view is already added
    const UnoViewVector::iterator aEnd( maViews.end() );

    // already added?
    if( ::std::find( maViews.begin(),
                     aEnd,
                     rView ) != aEnd )
    {
        // yes, nothing to do
        return false;
    }

    maViews.push_back( rView );

    return true;
}

bool EventMultiplexer::removeView( const UnoViewSharedPtr& rView )
{
    ENSURE_AND_THROW( rView.get(),
                      "EventMultiplexer::removeView(): Invalid view" );

    ::osl::MutexGuard aGuard( m_aMutex );

    // check whether same view is already added
    const UnoViewVector::iterator aEnd( maViews.end() );
    UnoViewVector::iterator aIter;

    // view added?
    if( (aIter=::std::find( maViews.begin(),
                            aEnd,
                            rView )) == aEnd )
    {
        // nope, nothing to do
        return false;
    }

    // revoke event listeners
    uno::Reference<css::presentation::XSlideShowView> const rUnoView(
        rView->getUnoView() );

    if( isMouseListenerRegistered() )
        rUnoView->removeMouseListener( this );

    if( !maMouseMoveHandlers.empty() )
        rUnoView->removeMouseMotionListener( this );

    // actually erase from container
    maViews.erase( aIter );

    return true;
}

void EventMultiplexer::clear()
{
    ::osl::MutexGuard aGuard( m_aMutex );

    // deregister from all views.
    if( isMouseListenerRegistered() )
    {
        for( UnoViewVector::iterator aIter=maViews.begin(), aEnd=maViews.end();
             aIter!=aEnd;
             ++aIter )
        {
            (*aIter)->getUnoView()->removeMouseListener( this );
        }
    }

    if( !maMouseMoveHandlers.empty() )
    {
        for( UnoViewVector::iterator aIter=maViews.begin(), aEnd=maViews.end();
             aIter!=aEnd;
             ++aIter )
        {
            (*aIter)->getUnoView()->removeMouseMotionListener( this );
        }
    }

    // release all references
    maViews.clear();

    maNextEffectHandlers.clear();
    maSlideStartHandlers.clear();
    maSlideEndHandlers.clear();
    maAnimationStartHandlers.clear();
    maAnimationEndHandlers.clear();
    maSlideAnimationsEndHandlers.clear();
    maAudioStoppedHandlers.clear();
    maCommandStopAudioHandlers.clear();
    maPauseHandlers.clear();
    maMouseClickHandlers.clear();
    maMouseDoubleClickHandlers.clear();
    maMouseMoveHandlers.clear();
    maHyperlinkHandlers.clear();
    mpLayerManager.reset();
}

void EventMultiplexer::setMouseCursor( sal_Int16 nCursor )
{
    ::osl::MutexGuard aGuard( m_aMutex );

    mnMouseCursor = nCursor;

    implSetMouseCursor( mnMouseCursor );
}

void EventMultiplexer::setVolatileMouseCursor( sal_Int16 nCursor )
{
    ::osl::MutexGuard aGuard( m_aMutex );

    // only change, if volatile cursor is not already
    // set. This effectively prioritizes handlers with the
    // highest priority (otherwise, the handler with the
    // lowest prio would determine the mouse cursor -
    // unwanted).
    if( mnVolatileMouseCursor == -1 )
        mnVolatileMouseCursor = nCursor;
}

void EventMultiplexer::setLayerManager(
    const LayerManagerSharedPtr& rMgr )
{
    mpLayerManager = rMgr;
}

void EventMultiplexer::updateScreenContent( bool bForceUpdate )
{
    // perform screen update (either if we're forced to do it,
    // or if the layer manager signals that it needs one).
    if( bForceUpdate ||
        (mpLayerManager.get() &&
         mpLayerManager->isUpdatePending() ) )
    {
        // call update() on the registered
        // LayerManager. This will only update the
        // backbuffer, not flush anything to screen
        if( mpLayerManager.get() )
            mpLayerManager->update();

        // call updateScreen() on all registered views (which
        // will copy the backbuffers to the front). Do NOT use
        // LayerManager::updateScreen(), we might need screen
        // updates independent from a valid LayerManager!
        ::std::for_each( maViews.begin(),
                         maViews.end(),
                         ::boost::mem_fn( &View::updateScreen ) );
    }
}

void EventMultiplexer::setAutomaticMode( bool bIsAuto )
{
    ::osl::MutexGuard aGuard( m_aMutex );

    if( bIsAuto == mbIsAutoMode )
        return; // no change, nothing to do

    mbIsAutoMode = bIsAuto;

    handleTicks();
}

bool EventMultiplexer::getAutomaticMode() const
{
    ::osl::MutexGuard aGuard( m_aMutex );

    return mbIsAutoMode;
}

void EventMultiplexer::setAutomaticTimeout( double nTimeout )
{
    ::osl::MutexGuard aGuard( m_aMutex );

    mnTimeout = nTimeout;
}

double EventMultiplexer::getAutomaticTimeout() const
{
    ::osl::MutexGuard aGuard( m_aMutex );

    return mnTimeout;
}

void EventMultiplexer::addNextEffectHandler(
    EventHandlerSharedPtr const& rHandler,
    double                       nPriority )
{
    addPrioritizedHandler( maNextEffectHandlers, rHandler, nPriority );

    // Enable tick events, if not done already
    handleTicks();
}

void EventMultiplexer::removeNextEffectHandler(
    const EventHandlerSharedPtr& rHandler )
{
    removeHandler( maNextEffectHandlers, rHandler );
}

void EventMultiplexer::addSlideStartHandler(
    const EventHandlerSharedPtr& rHandler )
{
    addHandler( maSlideStartHandlers, rHandler );
}

void EventMultiplexer::removeSlideStartHandler(
    const EventHandlerSharedPtr& rHandler )
{
    removeHandler( maSlideStartHandlers, rHandler );
}

void EventMultiplexer::addSlideEndHandler(
    const EventHandlerSharedPtr& rHandler )
{
    addHandler( maSlideEndHandlers, rHandler );
}

void EventMultiplexer::removeSlideEndHandler(
    const EventHandlerSharedPtr& rHandler )
{
    removeHandler( maSlideEndHandlers, rHandler );
}

void EventMultiplexer::addAnimationStartHandler(
    const AnimationEventHandlerSharedPtr& rHandler )
{
    addHandler( maAnimationStartHandlers, rHandler );
}

void EventMultiplexer::removeAnimationStartHandler(
    const AnimationEventHandlerSharedPtr& rHandler )
{
    removeHandler( maAnimationStartHandlers, rHandler );
}

void EventMultiplexer::addAnimationEndHandler(
    const AnimationEventHandlerSharedPtr& rHandler )
{
    addHandler( maAnimationEndHandlers, rHandler );
}

void EventMultiplexer::removeAnimationEndHandler(
    const AnimationEventHandlerSharedPtr& rHandler )
{
    removeHandler( maAnimationEndHandlers, rHandler );
}

void EventMultiplexer::addSlideAnimationsEndHandler(
    const EventHandlerSharedPtr& rHandler )
{
    addHandler( maSlideAnimationsEndHandlers, rHandler );
}

void EventMultiplexer::removeSlideAnimationsEndHandler(
    const EventHandlerSharedPtr& rHandler )
{
    removeHandler( maSlideAnimationsEndHandlers, rHandler );
}

void EventMultiplexer::addAudioStoppedHandler(
    const AnimationEventHandlerSharedPtr& rHandler )
{
    addHandler( maAudioStoppedHandlers, rHandler );
}

void EventMultiplexer::removeAudioStoppedHandler(
    const AnimationEventHandlerSharedPtr& rHandler )
{
    removeHandler( maAudioStoppedHandlers, rHandler );
}

void EventMultiplexer::addCommandStopAudioHandler(
    const AnimationEventHandlerSharedPtr& rHandler )
{
    addHandler( maCommandStopAudioHandlers, rHandler );
}

void EventMultiplexer::removeCommandStopAudioHandler(
    const AnimationEventHandlerSharedPtr& rHandler )
{
    removeHandler( maCommandStopAudioHandlers, rHandler );
}

void EventMultiplexer::addPauseHandler(
    const PauseEventHandlerSharedPtr& rHandler )
{
    addHandler( maPauseHandlers, rHandler );
}

void EventMultiplexer::removePauseHandler(
    const PauseEventHandlerSharedPtr&  rHandler )
{
    removeHandler( maPauseHandlers, rHandler );
}

void EventMultiplexer::addClickHandler(
    const MouseEventHandlerSharedPtr& rHandler,
    double                            nPriority )
{
    addMouseHandler(
        maMouseClickHandlers,
        rHandler,
        nPriority,
        isMouseListenerRegistered()
        ? NULL
        : &css::presentation::XSlideShowView::addMouseListener );
}

void EventMultiplexer::removeClickHandler(
    const MouseEventHandlerSharedPtr&  rHandler )
{
    ::osl::MutexGuard aGuard( m_aMutex );

    removeHandler( maMouseClickHandlers,
                   rHandler );

    if( !isMouseListenerRegistered() ) {
        forEachView( &css::presentation::XSlideShowView::removeMouseListener );
    }
}

void EventMultiplexer::addDoubleClickHandler(
    const MouseEventHandlerSharedPtr&   rHandler,
    double                              nPriority )
{
    addMouseHandler(
        maMouseDoubleClickHandlers,
        rHandler,
        nPriority,
        isMouseListenerRegistered()
        ? NULL
        : &css::presentation::XSlideShowView::addMouseListener );
}

void EventMultiplexer::removeDoubleClickHandler(
    const MouseEventHandlerSharedPtr&    rHandler )
{
    ::osl::MutexGuard aGuard( m_aMutex );

    removeHandler( maMouseDoubleClickHandlers,
                   rHandler );

    if( !isMouseListenerRegistered() ) {
        forEachView( &css::presentation::XSlideShowView::removeMouseListener );
    }
}

void EventMultiplexer::addMouseMoveHandler(
    const MouseEventHandlerSharedPtr& rHandler,
    double                            nPriority )
{
    addMouseHandler(
        maMouseMoveHandlers,
        rHandler,
        nPriority,
        maMouseMoveHandlers.empty()
        ? &css::presentation::XSlideShowView::addMouseMotionListener
        : NULL );
}

void EventMultiplexer::removeMouseMoveHandler(
    const MouseEventHandlerSharedPtr&  rHandler )
{
    ::osl::MutexGuard aGuard( m_aMutex );

    removeHandler( maMouseMoveHandlers,
                   rHandler );

    if( maMouseMoveHandlers.empty() ) {
        forEachView(
            &css::presentation::XSlideShowView::removeMouseMotionListener );
    }
}

void EventMultiplexer::addHyperlinkHandler(
    HyperLinkHandlerFunc const& func, HandlerId id )
{
    osl::MutexGuard const guard(m_aMutex);
    maHyperlinkHandlers[id] = func; // second add will overwrite previous func
}

void EventMultiplexer::removeHyperlinkHandler( HandlerId id )
{
    osl::MutexGuard const guard(m_aMutex);
    maHyperlinkHandlers.erase(id);
}

bool EventMultiplexer::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 notifySingleHandler(
        maNextEffectHandlers,
        boost::bind(
            &EventHandler::handleEvent,
            boost::bind( &ImplNextEffectHandlers::value_type::getHandler,
                         _1 ) ) );
}

bool EventMultiplexer::notifySlideStartEvent()
{
    return notifyAllEventHandlers( maSlideStartHandlers );
}

bool EventMultiplexer::notifySlideEndEvent()
{
    return notifyAllEventHandlers( maSlideEndHandlers );
}

bool EventMultiplexer::notifyAnimationStart(
    const AnimationNodeSharedPtr& rNode )
{
    return notifyAllAnimationHandlers( maAnimationStartHandlers, rNode );
}

bool EventMultiplexer::notifyAnimationEnd(
    const AnimationNodeSharedPtr& rNode )
{
    return notifyAllAnimationHandlers( maAnimationEndHandlers, rNode );
}

bool EventMultiplexer::notifySlideAnimationsEnd()
{
    return notifyAllEventHandlers( maSlideAnimationsEndHandlers );
}

bool EventMultiplexer::notifyAudioStopped(
    const AnimationNodeSharedPtr& rNode )
{
    return notifyAllAnimationHandlers( maAudioStoppedHandlers, rNode );
}

bool EventMultiplexer::notifyCommandStopAudio(
    const AnimationNodeSharedPtr& rNode )
{
    return notifyAllAnimationHandlers( maCommandStopAudioHandlers, rNode );
}

bool EventMultiplexer::notifyPauseMode( bool bPauseShow )
{
    return notifyAllHandlers( maPauseHandlers,
                              boost::bind( &PauseEventHandler::handlePause,
                                           _1, bPauseShow ) );
}

struct EventMultiplexer::HyperLinkNotifier {
    rtl::OUString const& m_rLink;
    HyperLinkNotifier( rtl::OUString const& link )
        : m_rLink(link) {}
    bool operator()( ImplHyperLinkHandlers::value_type const& v ) const {
        return v.second( m_rLink );
    }
};

bool EventMultiplexer::notifyHyperlinkClicked(
    rtl::OUString const& hyperLink )
{
    return notifySingleHandler( maHyperlinkHandlers,
                                HyperLinkNotifier( hyperLink ) );
}

void EventMultiplexer::disposing()
{
    // release all references
    clear();
    WeakComponentImplHelperBase::disposing();
}

// XMouseListener implementation
void SAL_CALL EventMultiplexer::disposing(
    const lang::EventObject& rSource ) throw (uno::RuntimeException)
{
    ::osl::MutexGuard aGuard( m_aMutex );

    // remove given event source
    for( UnoViewVector::iterator aIter=maViews.begin(), aEnd=maViews.end();
         aIter!=aEnd;
         ++aIter )
    {
        if( (*aIter)->getUnoView() == rSource.Source )
        {
            // one instance found, remove
            aIter = maViews.erase( aIter );
            aEnd = maViews.end(); // erase might invalidate all iterators!
        }
    }
}

bool EventMultiplexer::notifyMouseHandlers(
    const ImplMouseHandlers& rQueue,
    bool (MouseEventHandler::*pHandlerMethod)( const awt::MouseEvent& ),
    const awt::MouseEvent& e )
{
    uno::Reference<css::presentation::XSlideShowView> xView(
        e.Source, uno::UNO_QUERY );

    ENSURE_AND_RETURN( xView.is(), "EventMultiplexer::notifyHandlers(): "
                       "event source is not an XSlideShowView" );

    // find corresponding view (to mouse position into user coordinate space)
    UnoViewVector::iterator         aIter;
    const UnoViewVector::iterator   aEnd( maViews.end() );
    if( (aIter=::std::find_if(
             maViews.begin(), aEnd,
             boost::bind( std::equal_to< uno::Reference<
                          css::presentation::XSlideShowView > >(),
                          boost::cref( xView ),
                          boost::bind( &UnoView::getUnoView, _1 ) ) ) ) == aEnd)
    {
        ENSURE_AND_RETURN(
            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_AND_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 notifySingleHandler(
        rQueue, boost::bind(
            pHandlerMethod,
            boost::bind( &ImplMouseHandlers::value_type::getHandler, _1 ),
            aEvent ),
        false /* no separate copy: already operating on copy made in
                 implMouseXXX() */ );
}

void EventMultiplexer::implMousePressed( const awt::MouseEvent& e )
{
    osl::ClearableMutexGuard guard( m_aMutex );

    // generate a local copy of all handlers, since we have to
    // release the object mutex before firing.
    ImplMouseHandlers const localDoubleClickHandlers(
        maMouseDoubleClickHandlers );
    ImplMouseHandlers const localClickHandlers(
        maMouseClickHandlers );

    guard.clear();

    // fire double-click events for every second click
    sal_Int32 nCurrClickCount = e.ClickCount;
    while( nCurrClickCount > 1 &&
           notifyMouseHandlers( localDoubleClickHandlers,
                                &MouseEventHandler::handleMousePressed,
                                e ) )
    {
        nCurrClickCount -= 2;
    }

    // fire single-click events for all remaining clicks
    while( nCurrClickCount > 0 &&
           notifyMouseHandlers( localClickHandlers,
                                &MouseEventHandler::handleMousePressed,
                                e ) )
    {
        --nCurrClickCount;
    }
}

void EventMultiplexer::implMouseReleased( const awt::MouseEvent& e )
{
    // generate a local copy of all handlers, since we have to
    // release the object mutex before firing.
    osl::ClearableMutexGuard guard( m_aMutex );
    ImplMouseHandlers const localDoubleClickHandlers(
        maMouseDoubleClickHandlers );
    ImplMouseHandlers const localClickHandlers(
        maMouseClickHandlers );
    guard.clear();

    // fire double-click events for every second click
    sal_Int32 nCurrClickCount = e.ClickCount;
    while( nCurrClickCount > 1 &&
           notifyMouseHandlers( localDoubleClickHandlers,
                                &MouseEventHandler::handleMouseReleased,
                                e ) )
    {
        nCurrClickCount -= 2;
    }

    // fire single-click events for all remaining clicks
    while( nCurrClickCount > 0 &&
           notifyMouseHandlers( localClickHandlers,
                                &MouseEventHandler::handleMouseReleased,
                                e ) )
    {
        --nCurrClickCount;
    }
}

void EventMultiplexer::implMouseDragged( const awt::MouseEvent& e )
{
    osl::ResettableMutexGuard guard( m_aMutex );

    // generate a local copy of all handlers, since we have to
    // release the object mutex before firing.
    ImplMouseHandlers const localHandlers( maMouseMoveHandlers );

    guard.clear();

    notifyMouseHandlers(
        localHandlers, &MouseEventHandler::handleMouseDragged, e );

    // re-acquire mutex
    guard.reset();

    if( mnVolatileMouseCursor != -1 )
    {
        // handlers requested a volatile mouse cursor - set
        // one
        mnLastVolatileMouseCursor = mnVolatileMouseCursor;
        implSetMouseCursor( mnVolatileMouseCursor );

        mnVolatileMouseCursor = -1;
    }
    else if( mnMouseCursor != mnLastVolatileMouseCursor )
    {
        // handlers did not request a volatile mouse cursor -
        // clear to default, if necessary
        implSetMouseCursor( mnMouseCursor );

        mnLastVolatileMouseCursor = mnMouseCursor;
    }
}

void EventMultiplexer::implMouseMoved( const awt::MouseEvent& e )
{
    osl::ResettableMutexGuard guard( m_aMutex );
    // generate a local copy of all handlers, since we have to
    // release the object mutex before firing.
    ImplMouseHandlers const localHandlers( maMouseMoveHandlers );

    guard.clear();

    notifyMouseHandlers(
        localHandlers, &MouseEventHandler::handleMouseMoved, e );

    // re-acquire mutex
    guard.reset();

    if( mnVolatileMouseCursor != -1 )
    {
        // handlers requested a volatile mouse cursor - set
        // one
        mnLastVolatileMouseCursor = mnVolatileMouseCursor;
        implSetMouseCursor( mnVolatileMouseCursor );

        mnVolatileMouseCursor = -1;
    }
    else if( mnMouseCursor != mnLastVolatileMouseCursor )
    {
        // handlers did not request a volatile mouse cursor -
        // clear to default, if necessary
        implSetMouseCursor( mnMouseCursor );

        mnLastVolatileMouseCursor = mnMouseCursor;
    }
}

void SAL_CALL EventMultiplexer::mousePressed(
    const awt::MouseEvent& e ) throw (uno::RuntimeException)
{
    osl::MutexGuard const guard( m_aMutex );

    // notify mouse press. Don't call handlers directly, this
    // might not be the main thread!
    mrEventQueue.addEvent(
        makeEvent( boost::bind( &EventMultiplexer::implMousePressed,
                                this, e ) ) );
}

void SAL_CALL EventMultiplexer::mouseReleased(
    const awt::MouseEvent& e ) throw (uno::RuntimeException)
{
    osl::MutexGuard const guard( m_aMutex );

    // notify mouse release. Don't call handlers directly,
    // this might not be the main thread!
    mrEventQueue.addEvent(
        makeEvent( boost::bind( &EventMultiplexer::implMouseReleased,
                                this, e ) ) );
}

void SAL_CALL EventMultiplexer::mouseEntered(
    const awt::MouseEvent& e ) throw (uno::RuntimeException)
{
    // not used here
}

void SAL_CALL EventMultiplexer::mouseExited(
    const awt::MouseEvent& e ) throw (uno::RuntimeException)
{
    // not used here
}

// XMouseMotionListener implementation
void SAL_CALL EventMultiplexer::mouseDragged(
    const awt::MouseEvent& e ) throw (uno::RuntimeException)
{
    osl::MutexGuard const guard( m_aMutex );

    // notify mouse drag. Don't call handlers directly, this
    // might not be the main thread!
    mrEventQueue.addEvent(
        makeEvent( boost::bind( &EventMultiplexer::implMouseDragged,
                                this, e ) ) );
}

void SAL_CALL EventMultiplexer::mouseMoved(
    const awt::MouseEvent& e ) throw (uno::RuntimeException)
{
    osl::MutexGuard const guard( m_aMutex );

    // notify mouse move. Don't call handlers directly, this
    // might not be the main thread!
    mrEventQueue.addEvent(
        makeEvent( boost::bind( &EventMultiplexer::implMouseMoved,
                                this, e ) ) );
}

EventMultiplexer::~EventMultiplexer()
{
    // WeakComponentImplHelperBase calls disposing() upon destruction
    // if object has not been disposed yet.
}

EventMultiplexer::EventMultiplexer( EventQueue & rEventQueue )
    : Listener_UnoBase( m_aMutex ),
      mrEventQueue( rEventQueue ),
      maViews(),
      maNextEffectHandlers(),
      maSlideStartHandlers(),
      maSlideEndHandlers(),
      maAnimationStartHandlers(),
      maAnimationEndHandlers(),
      maSlideAnimationsEndHandlers(),
      maAudioStoppedHandlers(),
      maCommandStopAudioHandlers(),
      maPauseHandlers(),
      maMouseClickHandlers(),
      maMouseDoubleClickHandlers(),
      maMouseMoveHandlers(),
      maHyperlinkHandlers(),
      mnTimeout( 0.0 ),
      mpTickEvent(),
      mnMouseCursor( awt::SystemPointer::ARROW ),
      mnVolatileMouseCursor( -1 ),
      mnLastVolatileMouseCursor( mnMouseCursor ),
      mbIsAutoMode( false )
{
}

rtl::Reference<EventMultiplexer> EventMultiplexer::create(
    EventQueue & rEventQueue )
{
    return new EventMultiplexer(rEventQueue);
}

} // namespace internal
} // namespace presentation