LibreOffice Module sfx2 (master) 1
ContextChangeBroadcaster.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 */
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>
26#include <comphelper/lok.hxx>
28#include <sfx2/lokhelper.hxx>
29#include <sfx2/viewsh.hxx>
30
31using namespace css;
32using namespace css::uno;
33
34namespace sfx2::sidebar {
35
37 : mbIsBroadcasterEnabled(true)
38{
39}
40
42{
43}
44
45void ContextChangeBroadcaster::Initialize (const OUString& rsContextName)
46{
47 msContextName = rsContextName;
48}
49
50void ContextChangeBroadcaster::Activate (const css::uno::Reference<css::frame::XFrame>& rxFrame)
51{
52 if (msContextName.getLength() > 0)
54}
55
56void ContextChangeBroadcaster::Deactivate (const css::uno::Reference<css::frame::XFrame>& rxFrame)
57{
59 {
60 BroadcastContextChange(rxFrame, GetModuleName(rxFrame),
62 }
63}
64
66{
67 const bool bWasEnabled (mbIsBroadcasterEnabled);
69 return bWasEnabled;
70}
71
73 const css::uno::Reference<css::frame::XFrame>& rxFrame,
74 const OUString& rsModuleName,
75 const OUString& rsContextName)
76{
78 return;
79
80 if (rsContextName.getLength() == 0)
81 return;
82
83 if ( ! rxFrame.is() || ! rxFrame->getController().is())
84 {
85 // Frame is (probably) being deleted. Broadcasting context
86 // changes is not necessary anymore.
87 return;
88 }
89
90 const css::ui::ContextChangeEventObject aEvent(
91 rxFrame->getController(),
92 rsModuleName,
93 rsContextName);
94
95 // notify the LOK too
97 {
99 }
100
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());
106}
107
108OUString ContextChangeBroadcaster::GetModuleName (const css::uno::Reference<css::frame::XFrame>& rxFrame)
109{
110 if ( ! rxFrame.is() || ! rxFrame->getController().is())
111 return OUString();
112 try
113 {
114 const Reference<XComponentContext> xContext (::comphelper::getProcessComponentContext() );
115 const Reference<frame::XModuleManager> xModuleManager = frame::ModuleManager::create( xContext );
116 return xModuleManager->identify(rxFrame);
117 }
118 catch (const Exception&)
119 {
120 TOOLS_WARN_EXCEPTION("sfx.sidebar", "can not determine module name");
121 }
122 return OUString();
123}
124
125} // end of namespace ::sfx2::sidebar
126
127/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
static void notifyContextChange(const css::ui::ContextChangeEventObject &rEvent)
Notify about the editing context change.
Definition: lokhelper.cxx:711
void Deactivate(const css::uno::Reference< css::frame::XFrame > &rxFrame)
void Activate(const css::uno::Reference< css::frame::XFrame > &rxFrame)
bool SetBroadcasterEnabled(const bool bIsEnabled)
Enable or disable the broadcaster.
void BroadcastContextChange(const css::uno::Reference< css::frame::XFrame > &rxFrame, const OUString &rsModuleName, const OUString &rsContextName)
void Initialize(const OUString &rsContextName)
static OUString GetModuleName(const css::uno::Reference< css::frame::XFrame > &rxFrame)
static const OUString & GetContextName(const Context eContext)
#define TOOLS_WARN_EXCEPTION(area, stream)
@ Exception
bool bIsEnabled