update credits
[LibreOffice.git] / comphelper / source / misc / SelectionMultiplex.cxx
blobd4cca73a7da088a31aa74ba552dd6bc89448c617
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 .
21 #include <comphelper/SelectionMultiplex.hxx>
22 #include <osl/diagnose.h>
24 //.........................................................................
25 namespace comphelper
27 //.........................................................................
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::lang;
31 using namespace ::com::sun::star::view;
33 //========================================================================
34 //= OSelectionChangeListener
35 //========================================================================
36 //------------------------------------------------------------------------
37 OSelectionChangeListener::~OSelectionChangeListener()
39 if (m_pAdapter)
40 m_pAdapter->dispose();
43 //------------------------------------------------------------------
44 void OSelectionChangeListener::_disposing(const EventObject&) throw( RuntimeException)
46 // nothing to do here
49 //------------------------------------------------------------------
50 void OSelectionChangeListener::setAdapter(OSelectionChangeMultiplexer* pAdapter)
52 if (m_pAdapter)
54 ::osl::MutexGuard aGuard(m_rMutex);
55 m_pAdapter->release();
56 m_pAdapter = NULL;
59 if (pAdapter)
61 ::osl::MutexGuard aGuard(m_rMutex);
62 m_pAdapter = pAdapter;
63 m_pAdapter->acquire();
67 //========================================================================
68 //= OSelectionChangeMultiplexer
69 //========================================================================
70 //------------------------------------------------------------------
71 OSelectionChangeMultiplexer::OSelectionChangeMultiplexer(OSelectionChangeListener* _pListener, const Reference< XSelectionSupplier>& _rxSet, sal_Bool _bAutoReleaseSet)
72 :m_xSet(_rxSet)
73 ,m_pListener(_pListener)
74 ,m_nLockCount(0)
75 ,m_bListening(sal_False)
76 ,m_bAutoSetRelease(_bAutoReleaseSet)
78 m_pListener->setAdapter(this);
79 osl_atomic_increment(&m_refCount);
81 Reference< XSelectionChangeListener> xPreventDelete(this);
82 m_xSet->addSelectionChangeListener(xPreventDelete);
84 osl_atomic_decrement(&m_refCount);
87 //------------------------------------------------------------------
88 OSelectionChangeMultiplexer::~OSelectionChangeMultiplexer()
92 //------------------------------------------------------------------
93 void OSelectionChangeMultiplexer::lock()
95 ++m_nLockCount;
98 //------------------------------------------------------------------
99 void OSelectionChangeMultiplexer::unlock()
101 --m_nLockCount;
104 //------------------------------------------------------------------
105 void OSelectionChangeMultiplexer::dispose()
107 if (m_bListening)
109 Reference< XSelectionChangeListener> xPreventDelete(this);
111 m_xSet->removeSelectionChangeListener(xPreventDelete);
113 m_pListener->setAdapter(NULL);
115 m_pListener = NULL;
116 m_bListening = sal_False;
118 if (m_bAutoSetRelease)
119 m_xSet = NULL;
123 // XEventListener
124 //------------------------------------------------------------------
125 void SAL_CALL OSelectionChangeMultiplexer::disposing( const EventObject& _rSource) throw( RuntimeException)
127 if (m_pListener)
129 // tell the listener
130 if (!locked())
131 m_pListener->_disposing(_rSource);
132 // disconnect the listener
133 if (m_pListener) // may have been reset whilest calling into _disposing
134 m_pListener->setAdapter(NULL);
137 m_pListener = NULL;
138 m_bListening = sal_False;
140 if (m_bAutoSetRelease)
141 m_xSet = NULL;
144 // XSelectionChangeListener
145 //------------------------------------------------------------------
146 void SAL_CALL OSelectionChangeMultiplexer::selectionChanged( const EventObject& _rEvent ) throw( RuntimeException)
148 if (m_pListener && !locked())
149 m_pListener->_selectionChanged(_rEvent);
151 //.........................................................................
153 //.........................................................................
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */