Get the style color and number just once
[LibreOffice.git] / sfx2 / source / sidebar / ContextChangeBroadcaster.cxx
blobc7c7a787e6d460ce4810f5e6076b55928456d57f
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 .
19 #include <sidebar/ContextChangeBroadcaster.hxx>
20 #include <vcl/EnumContext.hxx>
21 #include <com/sun/star/ui/ContextChangeEventObject.hpp>
22 #include <com/sun/star/ui/ContextChangeEventMultiplexer.hpp>
23 #include <com/sun/star/frame/ModuleManager.hpp>
24 #include <osl/diagnose.h>
25 #include <comphelper/diagnose_ex.hxx>
26 #include <comphelper/lok.hxx>
27 #include <comphelper/processfactory.hxx>
28 #include <sfx2/lokhelper.hxx>
29 #include <sfx2/viewsh.hxx>
31 using namespace css;
32 using namespace css::uno;
34 namespace sfx2::sidebar {
36 ContextChangeBroadcaster::ContextChangeBroadcaster()
37 : mbIsBroadcasterEnabled(true)
41 ContextChangeBroadcaster::~ContextChangeBroadcaster()
45 void ContextChangeBroadcaster::Initialize (const OUString& rsContextName)
47 msContextName = rsContextName;
50 void ContextChangeBroadcaster::Activate (const css::uno::Reference<css::frame::XFrame>& rxFrame)
52 if (msContextName.getLength() > 0)
53 BroadcastContextChange(rxFrame, GetModuleName(rxFrame), msContextName);
56 void ContextChangeBroadcaster::Deactivate (const css::uno::Reference<css::frame::XFrame>& rxFrame)
58 if (msContextName.getLength() > 0 && !comphelper::LibreOfficeKit::isActive())
60 BroadcastContextChange(rxFrame, GetModuleName(rxFrame),
61 vcl::EnumContext::GetContextName(vcl::EnumContext::Context::Default));
65 bool ContextChangeBroadcaster::SetBroadcasterEnabled (const bool bIsEnabled)
67 const bool bWasEnabled (mbIsBroadcasterEnabled);
68 mbIsBroadcasterEnabled = bIsEnabled;
69 return bWasEnabled;
72 void ContextChangeBroadcaster::BroadcastContextChange (
73 const css::uno::Reference<css::frame::XFrame>& rxFrame,
74 const OUString& rsModuleName,
75 const OUString& rsContextName)
77 if ( ! mbIsBroadcasterEnabled)
78 return;
80 if (rsContextName.getLength() == 0)
81 return;
83 if ( ! rxFrame.is() || ! rxFrame->getController().is())
85 // Frame is (probably) being deleted. Broadcasting context
86 // changes is not necessary anymore.
87 return;
90 const css::ui::ContextChangeEventObject aEvent(
91 rxFrame->getController(),
92 rsModuleName,
93 rsContextName);
95 // notify the LOK too
96 if (comphelper::LibreOfficeKit::isActive())
98 SfxLokHelper::notifyContextChange(aEvent);
101 css::uno::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer (
102 css::ui::ContextChangeEventMultiplexer::get(
103 ::comphelper::getProcessComponentContext()));
104 if (xMultiplexer.is())
105 xMultiplexer->broadcastContextChangeEvent(aEvent, rxFrame->getController());
108 OUString ContextChangeBroadcaster::GetModuleName (const css::uno::Reference<css::frame::XFrame>& rxFrame)
110 if ( ! rxFrame.is() || ! rxFrame->getController().is())
111 return OUString();
114 const Reference<XComponentContext>& xContext (::comphelper::getProcessComponentContext() );
115 const Reference<frame::XModuleManager> xModuleManager = frame::ModuleManager::create( xContext );
116 return xModuleManager->identify(rxFrame);
118 catch (const Exception&)
120 TOOLS_WARN_EXCEPTION("sfx.sidebar", "can not determine module name");
122 return OUString();
125 } // end of namespace ::sfx2::sidebar
127 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */