Bump version to 6.4-15
[LibreOffice.git] / odk / examples / cpp / complextoolbarcontrols / ListenerHelper.cxx
blob0ee09734a09aaadadae47ac10bd20c29fafed4b5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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::uno::RuntimeException;
30 using com::sun::star::frame::FeatureStateEvent;
32 static AllListeners aListeners;
34 void ListenerHelper::AddListener(
35 const Reference < XFrame >& xFrame,
36 const Reference < XStatusListener > xControl,
37 const ::rtl::OUString& aCommand )
39 sal_uInt32 i=0;
40 sal_uInt32 nSize = aListeners.size();
41 for ( i=0; i<nSize; i++ )
42 if ( aListeners[i].xFrame == xFrame )
43 break;
45 OSL_ENSURE( i<nSize, "No dispatch found for this listener!" );
46 aListeners[i].aContainer[aCommand].push_back( xControl );
49 void ListenerHelper::RemoveListener(
50 const Reference < XFrame >& xFrame,
51 const Reference < XStatusListener > xControl,
52 const ::rtl::OUString& aCommand )
54 sal_uInt32 nSize = aListeners.size();
55 for ( sal_uInt32 i=0; i<nSize; i++ )
57 if ( aListeners[i].xFrame == xFrame )
59 StatusListeners& aL = aListeners[i].aContainer[aCommand];
60 StatusListeners::iterator aIter = aL.begin();
61 while ( aIter != aL.end() )
63 if ( (*aIter) == xControl )
65 aL.erase( aIter );
66 break;
69 ++aIter;
75 void ListenerHelper::Notify(
76 const Reference < XFrame >& xFrame,
77 const ::rtl::OUString& aCommand,
78 FeatureStateEvent& rEvent )
80 sal_uInt32 nSize = aListeners.size();
81 for ( sal_uInt32 i=0; i<nSize; i++ )
83 if ( aListeners[i].xFrame == xFrame )
85 rEvent.Source = aListeners[i].xDispatch;
86 StatusListeners& aL = aListeners[i].aContainer[aCommand];
87 StatusListeners::iterator aIter = aL.begin();
88 while ( aIter != aL.end() )
90 (*aIter)->statusChanged( rEvent );
91 ++aIter;
97 com::sun::star::uno::Reference < XDispatch > ListenerHelper::GetDispatch(
98 const Reference < XFrame >& xFrame,
99 const ::rtl::OUString& aCommand )
101 sal_uInt32 nSize = aListeners.size();
102 for ( sal_uInt32 i=0; i<nSize; i++ )
104 if ( aListeners[i].xFrame == xFrame )
105 return aListeners[i].xDispatch;
108 return Reference < XDispatch >();
111 void ListenerHelper::AddDispatch(
112 const Reference < XDispatch > xDispatch,
113 const Reference < XFrame >& xFrame,
114 const ::rtl::OUString& aCommand )
116 ListenerItem aItem;
117 aItem.xFrame = xFrame;
118 aItem.xDispatch = xDispatch;
119 aListeners.push_back( aItem );
120 xFrame->addEventListener( new ListenerItemEventListener( xFrame ) );
123 void SAL_CALL ListenerItemEventListener::disposing( const EventObject& aEvent) throw (RuntimeException)
125 AllListeners::iterator aIter = aListeners.begin();
126 while ( aIter != aListeners.end() )
128 if ( (*aIter).xFrame == mxFrame )
130 aListeners.erase( aIter );
131 break;
134 ++aIter;
138 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */