Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / cpp / complextoolbarcontrols / ListenerHelper.cxx
blob8a39becd63a7209a3cbe614645594497f986b82d
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::XEventListener;
28 using com::sun::star::lang::EventObject;
29 using com::sun::star::uno::Reference;
30 using com::sun::star::uno::RuntimeException;
31 using com::sun::star::frame::FeatureStateEvent;
33 static AllListeners aListeners;
35 void ListenerHelper::AddListener(
36 const Reference < XFrame >& xFrame,
37 const Reference < XStatusListener > xControl,
38 const ::rtl::OUString& aCommand )
40 sal_uInt32 i=0;
41 sal_uInt32 nSize = aListeners.size();
42 for ( i=0; i<nSize; i++ )
43 if ( aListeners[i].xFrame == xFrame )
44 break;
46 OSL_ENSURE( i<nSize, "No dispatch found for this listener!" );
47 aListeners[i].aContainer[aCommand].push_back( xControl );
50 void ListenerHelper::RemoveListener(
51 const Reference < XFrame >& xFrame,
52 const Reference < XStatusListener > xControl,
53 const ::rtl::OUString& aCommand )
55 sal_uInt32 nSize = aListeners.size();
56 for ( sal_uInt32 i=0; i<nSize; i++ )
58 if ( aListeners[i].xFrame == xFrame )
60 StatusListeners& aL = aListeners[i].aContainer[aCommand];
61 StatusListeners::iterator aIter = aL.begin();
62 while ( aIter != aL.end() )
64 if ( (*aIter) == xControl )
66 aL.erase( aIter );
67 break;
70 ++aIter;
76 void ListenerHelper::Notify(
77 const Reference < XFrame >& xFrame,
78 const ::rtl::OUString& aCommand,
79 FeatureStateEvent& rEvent )
81 sal_uInt32 nSize = aListeners.size();
82 for ( sal_uInt32 i=0; i<nSize; i++ )
84 if ( aListeners[i].xFrame == xFrame )
86 rEvent.Source = aListeners[i].xDispatch;
87 StatusListeners& aL = aListeners[i].aContainer[aCommand];
88 StatusListeners::iterator aIter = aL.begin();
89 while ( aIter != aL.end() )
91 (*aIter)->statusChanged( rEvent );
92 ++aIter;
98 com::sun::star::uno::Reference < XDispatch > ListenerHelper::GetDispatch(
99 const Reference < XFrame >& xFrame,
100 const ::rtl::OUString& aCommand )
102 sal_uInt32 nSize = aListeners.size();
103 for ( sal_uInt32 i=0; i<nSize; i++ )
105 if ( aListeners[i].xFrame == xFrame )
106 return aListeners[i].xDispatch;
109 return Reference < XDispatch >();
112 void ListenerHelper::AddDispatch(
113 const Reference < XDispatch > xDispatch,
114 const Reference < XFrame >& xFrame,
115 const ::rtl::OUString& aCommand )
117 ListenerItem aItem;
118 aItem.xFrame = xFrame;
119 aItem.xDispatch = xDispatch;
120 aListeners.push_back( aItem );
121 xFrame->addEventListener( new ListenerItemEventListener( xFrame ) );
124 void SAL_CALL ListenerItemEventListener::disposing( const EventObject& aEvent) throw (RuntimeException)
126 AllListeners::iterator aIter = aListeners.begin();
127 while ( aIter != aListeners.end() )
129 if ( (*aIter).xFrame == mxFrame )
131 aListeners.erase( aIter );
132 break;
135 ++aIter;
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */