1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <osl/diagnose.h>
22 #include "ListenerHelper.h"
24 using com::sun::star::frame::XFrame
;
25 using com::sun::star::frame::XDispatch
;
26 using com::sun::star::frame::XStatusListener
;
27 using com::sun::star::lang::EventObject
;
28 using com::sun::star::uno::Reference
;
29 using com::sun::star::frame::FeatureStateEvent
;
31 static AllListeners aListeners
;
33 void ListenerHelper::AddListener(
34 const Reference
< XFrame
>& xFrame
,
35 const Reference
< XStatusListener
> xControl
,
36 const ::rtl::OUString
& aCommand
)
39 sal_uInt32 nSize
= aListeners
.size();
40 for ( i
=0; i
<nSize
; i
++ )
41 if ( aListeners
[i
].xFrame
== xFrame
)
44 OSL_ENSURE( i
<nSize
, "No dispatch found for this listener!" );
45 aListeners
[i
].aContainer
[aCommand
].push_back( xControl
);
48 void ListenerHelper::RemoveListener(
49 const Reference
< XFrame
>& xFrame
,
50 const Reference
< XStatusListener
> xControl
,
51 const ::rtl::OUString
& aCommand
)
53 sal_uInt32 nSize
= aListeners
.size();
54 for ( sal_uInt32 i
=0; i
<nSize
; i
++ )
56 if ( aListeners
[i
].xFrame
== xFrame
)
58 StatusListeners
& aL
= aListeners
[i
].aContainer
[aCommand
];
59 StatusListeners::iterator aIter
= aL
.begin();
60 while ( aIter
!= aL
.end() )
62 if ( (*aIter
) == xControl
)
74 void ListenerHelper::Notify(
75 const Reference
< XFrame
>& xFrame
,
76 const ::rtl::OUString
& aCommand
,
77 FeatureStateEvent
& rEvent
)
79 sal_uInt32 nSize
= aListeners
.size();
80 for ( sal_uInt32 i
=0; i
<nSize
; i
++ )
82 if ( aListeners
[i
].xFrame
== xFrame
)
84 rEvent
.Source
= aListeners
[i
].xDispatch
;
85 StatusListeners
& aL
= aListeners
[i
].aContainer
[aCommand
];
86 StatusListeners::iterator aIter
= aL
.begin();
87 while ( aIter
!= aL
.end() )
89 (*aIter
)->statusChanged( rEvent
);
96 com::sun::star::uno::Reference
< XDispatch
> ListenerHelper::GetDispatch(
97 const Reference
< XFrame
>& xFrame
,
98 const ::rtl::OUString
& aCommand
)
100 sal_uInt32 nSize
= aListeners
.size();
101 for ( sal_uInt32 i
=0; i
<nSize
; i
++ )
103 if ( aListeners
[i
].xFrame
== xFrame
)
104 return aListeners
[i
].xDispatch
;
107 return Reference
< XDispatch
>();
110 void ListenerHelper::AddDispatch(
111 const Reference
< XDispatch
> xDispatch
,
112 const Reference
< XFrame
>& xFrame
,
113 const ::rtl::OUString
& aCommand
)
116 aItem
.xFrame
= xFrame
;
117 aItem
.xDispatch
= xDispatch
;
118 aListeners
.push_back( aItem
);
119 xFrame
->addEventListener( new ListenerItemEventListener( xFrame
) );
122 void SAL_CALL
ListenerItemEventListener::disposing( const EventObject
& aEvent
)
124 AllListeners::iterator aIter
= aListeners
.begin();
125 while ( aIter
!= aListeners
.end() )
127 if ( (*aIter
).xFrame
== mxFrame
)
129 aListeners
.erase( aIter
);
137 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */