Bump version to 4.3-4
[LibreOffice.git] / comphelper / source / misc / SelectionMultiplex.cxx
blobc01135ead0981231183cd1a2c829648450ccf194
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>
25 namespace comphelper
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::lang;
31 using namespace ::com::sun::star::view;
34 //= OSelectionChangeListener
37 OSelectionChangeListener::~OSelectionChangeListener()
39 if (m_pAdapter)
40 m_pAdapter->dispose();
44 void OSelectionChangeListener::_disposing(const EventObject&)
45 throw (RuntimeException, std::exception)
47 // nothing to do here
51 void OSelectionChangeListener::setAdapter(OSelectionChangeMultiplexer* pAdapter)
53 if (m_pAdapter)
55 ::osl::MutexGuard aGuard(m_rMutex);
56 m_pAdapter->release();
57 m_pAdapter = NULL;
60 if (pAdapter)
62 ::osl::MutexGuard aGuard(m_rMutex);
63 m_pAdapter = pAdapter;
64 m_pAdapter->acquire();
69 //= OSelectionChangeMultiplexer
72 OSelectionChangeMultiplexer::OSelectionChangeMultiplexer(OSelectionChangeListener* _pListener, const Reference< XSelectionSupplier>& _rxSet, bool _bAutoReleaseSet)
73 :m_xSet(_rxSet)
74 ,m_pListener(_pListener)
75 ,m_nLockCount(0)
76 ,m_bListening(false)
77 ,m_bAutoSetRelease(_bAutoReleaseSet)
79 m_pListener->setAdapter(this);
80 osl_atomic_increment(&m_refCount);
82 Reference< XSelectionChangeListener> xPreventDelete(this);
83 m_xSet->addSelectionChangeListener(xPreventDelete);
85 osl_atomic_decrement(&m_refCount);
89 OSelectionChangeMultiplexer::~OSelectionChangeMultiplexer()
94 void OSelectionChangeMultiplexer::lock()
96 ++m_nLockCount;
100 void OSelectionChangeMultiplexer::unlock()
102 --m_nLockCount;
106 void OSelectionChangeMultiplexer::dispose()
108 if (m_bListening)
110 Reference< XSelectionChangeListener> xPreventDelete(this);
112 m_xSet->removeSelectionChangeListener(xPreventDelete);
114 m_pListener->setAdapter(NULL);
116 m_pListener = NULL;
117 m_bListening = false;
119 if (m_bAutoSetRelease)
120 m_xSet = NULL;
124 // XEventListener
126 void SAL_CALL OSelectionChangeMultiplexer::disposing( const EventObject& _rSource) throw( RuntimeException, std::exception)
128 if (m_pListener)
130 // tell the listener
131 if (!locked())
132 m_pListener->_disposing(_rSource);
133 // disconnect the listener
134 if (m_pListener) // may have been reset whilest calling into _disposing
135 m_pListener->setAdapter(NULL);
138 m_pListener = NULL;
139 m_bListening = false;
141 if (m_bAutoSetRelease)
142 m_xSet = NULL;
145 // XSelectionChangeListener
147 void SAL_CALL OSelectionChangeMultiplexer::selectionChanged( const EventObject& _rEvent ) throw( RuntimeException, std::exception)
149 if (m_pListener && !locked())
150 m_pListener->_selectionChanged(_rEvent);
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */