LibreOffice Module sc (master)  1
DrawModelBroadcaster.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <DrawModelBroadcaster.hxx>
21 #include <sal/log.hxx>
22 #include <svx/svdmodel.hxx>
23 #include <svx/unomod.hxx>
24 #include <svx/svdobj.hxx>
25 #include <tools/diagnose_ex.h>
26 
27 using namespace ::com::sun::star;
28 
30  maEventListeners( maListenerMutex ),
31  mpDrawModel( pDrawModel )
32 {
33  if (mpDrawModel)
35 }
36 
38 {
39  if (mpDrawModel)
41 }
42 
43 void SAL_CALL ScDrawModelBroadcaster::addEventListener( const uno::Reference< document::XEventListener >& xListener )
44 {
45  maEventListeners.addInterface( xListener );
46 }
47 
48 void SAL_CALL ScDrawModelBroadcaster::removeEventListener( const uno::Reference< document::XEventListener >& xListener )
49 {
50  maEventListeners.removeInterface( xListener );
51 }
52 
54  const css::uno::Reference< css::drawing::XShape >& xShape,
55  const uno::Reference< document::XShapeEventListener >& xListener )
56 {
57  assert(xShape.is() && "no shape?");
58  osl::MutexGuard aGuard(maListenerMutex);
59  auto rv = maShapeListeners.emplace(xShape, xListener);
60  assert(rv.second && "duplicate listener?");
61  (void)rv;
62 }
63 
65  const css::uno::Reference< css::drawing::XShape >& xShape,
66  const uno::Reference< document::XShapeEventListener >& xListener )
67 {
68  osl::MutexGuard aGuard(maListenerMutex);
69  auto it = maShapeListeners.find(xShape);
70  if (it != maShapeListeners.end())
71  {
72  assert(it->second == xListener && "removing wrong listener?");
73  (void)xListener;
74  maShapeListeners.erase(it);
75  }
76 }
77 
79  const SfxHint& rHint )
80 {
81  if (rHint.GetId() != SfxHintId::ThisIsAnSdrHint)
82  return;
83  const SdrHint* pSdrHint = static_cast<const SdrHint*>(&rHint);
84 
85  document::EventObject aEvent;
86  if( !SvxUnoDrawMSFactory::createEvent( mpDrawModel, pSdrHint, aEvent ) )
87  return;
88 
90  while( aIter.hasMoreElements() )
91  {
92  const uno::Reference < document::XEventListener >& xListener = aIter.next();
93  try
94  {
95  xListener->notifyEvent( aEvent );
96  }
97  catch( const uno::RuntimeException& )
98  {
99  TOOLS_WARN_EXCEPTION("sc.ui", "Runtime exception caught while notifying shape");
100  }
101  }
102 
103  // right now, we're only handling the specific event necessary to fix this performance problem
104  if (pSdrHint->GetKind() == SdrHintKind::ObjectChange)
105  {
106  auto pSdrObject = const_cast<SdrObject*>(pSdrHint->GetObject());
107  uno::Reference<drawing::XShape> xShape(pSdrObject->getUnoShape(), uno::UNO_QUERY);
108  osl::MutexGuard aGuard(maListenerMutex);
109  auto it = maShapeListeners.find(xShape);
110  if (it != maShapeListeners.end())
111  it->second->notifyShapeEvent(aEvent);
112  }
113 }
114 
115 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void SAL_CALL removeShapeEventListener(const css::uno::Reference< css::drawing::XShape > &xShape, const css::uno::Reference< css::document::XShapeEventListener > &xListener) override
static bool createEvent(const SdrModel *pDoc, const SdrHint *pSdrHint, css::document::EventObject &aEvent)
SdrHintKind GetKind() const
sal_Int32 addInterface(const css::uno::Reference< ListenerT > &rxIFace)
ScDrawModelBroadcaster(SdrModel *pDrawModel)
SfxHintId GetId() const
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
#define TOOLS_WARN_EXCEPTION(area, stream)
sal_Int32 removeInterface(const css::uno::Reference< ListenerT > &rxIFace)
css::uno::Reference< ListenerT > const & next()
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
void StartListening(SfxBroadcaster &rBroadcaster, DuplicateHandling eDuplicateHanding=DuplicateHandling::Unexpected)
mutable::osl::Mutex maListenerMutex
virtual void SAL_CALL addEventListener(const css::uno::Reference< css::document::XEventListener > &xListener) override
void EndListening(SfxBroadcaster &rBroadcaster, bool bRemoveAllDuplicates=false)
virtual void SAL_CALL removeEventListener(const css::uno::Reference< css::document::XEventListener > &xListener) override
virtual ~ScDrawModelBroadcaster() override
virtual void SAL_CALL addShapeEventListener(const css::uno::Reference< css::drawing::XShape > &xShape, const css::uno::Reference< css::document::XShapeEventListener > &xListener) override
::comphelper::OInterfaceContainerHelper3< css::document::XEventListener > maEventListeners
AnyEventRef aEvent
std::unordered_map< css::uno::Reference< css::drawing::XShape >, css::uno::Reference< css::document::XShapeEventListener > > maShapeListeners
const SdrObject * GetObject() const
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo