2013-06-16 02:48:58 +01:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2013-04-19 08:51:45 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
|
|
|
|
2013-11-11 22:11:22 -06:00
|
|
|
#include <sfx2/sidebar/CommandInfoProvider.hxx>
|
2013-04-19 08:51:45 +00:00
|
|
|
|
|
|
|
#include <comphelper/processfactory.hxx>
|
|
|
|
#include <svtools/acceleratorexecute.hxx>
|
2015-09-01 18:13:22 +09:00
|
|
|
#include <cppuhelper/compbase.hxx>
|
2013-04-19 08:51:45 +00:00
|
|
|
#include <cppuhelper/basemutex.hxx>
|
|
|
|
|
2013-05-27 14:52:39 +02:00
|
|
|
#include <com/sun/star/frame/ModuleManager.hpp>
|
2014-01-28 15:27:32 +01:00
|
|
|
#include <com/sun/star/frame/theUICommandDescription.hpp>
|
2013-05-27 14:52:39 +02:00
|
|
|
#include <com/sun/star/ui/GlobalAcceleratorConfiguration.hpp>
|
2013-04-19 08:51:45 +00:00
|
|
|
#include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
|
2014-01-29 18:58:18 +01:00
|
|
|
#include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
|
2013-04-19 08:51:45 +00:00
|
|
|
|
|
|
|
using namespace css;
|
2014-09-02 17:38:00 +00:00
|
|
|
using namespace css::uno;
|
2013-04-19 08:51:45 +00:00
|
|
|
using ::rtl::OUString;
|
|
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2015-09-01 18:13:22 +09:00
|
|
|
typedef ::cppu::WeakComponentImplHelper <
|
2013-04-19 08:51:45 +00:00
|
|
|
css::lang::XEventListener
|
|
|
|
> FrameListenerInterfaceBase;
|
|
|
|
class FrameListener
|
|
|
|
: public ::cppu::BaseMutex,
|
|
|
|
public FrameListenerInterfaceBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FrameListener (sfx2::sidebar::CommandInfoProvider& rInfoProvider, const Reference<frame::XFrame>& rxFrame)
|
|
|
|
: FrameListenerInterfaceBase(m_aMutex),
|
|
|
|
mrInfoProvider(rInfoProvider),
|
|
|
|
mxFrame(rxFrame)
|
|
|
|
{
|
|
|
|
if (mxFrame.is())
|
|
|
|
mxFrame->addEventListener(this);
|
|
|
|
}
|
2015-04-14 12:44:47 +02:00
|
|
|
virtual ~FrameListener()
|
2013-04-19 08:51:45 +00:00
|
|
|
{
|
|
|
|
}
|
2015-04-14 12:44:47 +02:00
|
|
|
virtual void SAL_CALL disposing() SAL_OVERRIDE
|
2013-04-19 08:51:45 +00:00
|
|
|
{
|
|
|
|
if (mxFrame.is())
|
|
|
|
mxFrame->removeEventListener(this);
|
|
|
|
}
|
|
|
|
virtual void SAL_CALL disposing (const css::lang::EventObject& rEvent)
|
2014-09-02 17:38:00 +00:00
|
|
|
throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
|
2013-04-19 08:51:45 +00:00
|
|
|
{
|
|
|
|
(void)rEvent;
|
|
|
|
mrInfoProvider.SetFrame(NULL);
|
|
|
|
mxFrame = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
sfx2::sidebar::CommandInfoProvider& mrInfoProvider;
|
|
|
|
Reference<frame::XFrame> mxFrame;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace sfx2 { namespace sidebar {
|
|
|
|
|
2015-04-14 12:44:47 +02:00
|
|
|
CommandInfoProvider& CommandInfoProvider::Instance()
|
2013-04-19 08:51:45 +00:00
|
|
|
{
|
|
|
|
static CommandInfoProvider aProvider;
|
|
|
|
return aProvider;
|
|
|
|
}
|
|
|
|
|
2015-04-14 12:44:47 +02:00
|
|
|
CommandInfoProvider::CommandInfoProvider()
|
2013-05-27 14:52:39 +02:00
|
|
|
: mxContext(comphelper::getProcessComponentContext()),
|
2013-04-19 08:51:45 +00:00
|
|
|
mxCachedDataFrame(),
|
|
|
|
mxCachedDocumentAcceleratorConfiguration(),
|
|
|
|
mxCachedModuleAcceleratorConfiguration(),
|
|
|
|
mxCachedGlobalAcceleratorConfiguration(),
|
|
|
|
msCachedModuleIdentifier(),
|
|
|
|
mxFrameListener()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-04-14 12:44:47 +02:00
|
|
|
CommandInfoProvider::~CommandInfoProvider()
|
2013-04-19 08:51:45 +00:00
|
|
|
{
|
|
|
|
if (mxFrameListener.is())
|
|
|
|
{
|
|
|
|
mxFrameListener->dispose();
|
|
|
|
mxFrameListener = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OUString CommandInfoProvider::GetLabelForCommand (
|
|
|
|
const OUString& rsCommandName,
|
|
|
|
const Reference<frame::XFrame>& rxFrame)
|
|
|
|
{
|
|
|
|
SetFrame(rxFrame);
|
|
|
|
|
|
|
|
const OUString sLabel (GetCommandLabel(rsCommandName));
|
|
|
|
const OUString sShortCut (GetCommandShortcut(rsCommandName));
|
|
|
|
if (sShortCut.getLength() > 0)
|
2013-11-12 09:33:27 +02:00
|
|
|
return sLabel + " (" + sShortCut + ")";
|
2013-04-19 08:51:45 +00:00
|
|
|
else
|
|
|
|
return sLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandInfoProvider::SetFrame (const Reference<frame::XFrame>& rxFrame)
|
|
|
|
{
|
|
|
|
if (rxFrame != mxCachedDataFrame)
|
|
|
|
{
|
|
|
|
// Detach from the old frame.
|
|
|
|
if (mxFrameListener.is())
|
|
|
|
{
|
|
|
|
mxFrameListener->dispose();
|
|
|
|
mxFrameListener = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release objects that are tied to the old frame.
|
|
|
|
mxCachedDocumentAcceleratorConfiguration = NULL;
|
|
|
|
mxCachedModuleAcceleratorConfiguration = NULL;
|
2014-11-12 14:24:10 +05:30
|
|
|
msCachedModuleIdentifier.clear();
|
2013-04-19 08:51:45 +00:00
|
|
|
mxCachedDataFrame = rxFrame;
|
|
|
|
|
|
|
|
// Connect to the new frame.
|
|
|
|
if (rxFrame.is())
|
|
|
|
mxFrameListener = new FrameListener(*this, rxFrame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-14 12:44:47 +02:00
|
|
|
Reference<ui::XAcceleratorConfiguration> CommandInfoProvider::GetDocumentAcceleratorConfiguration()
|
2013-04-19 08:51:45 +00:00
|
|
|
{
|
|
|
|
if ( ! mxCachedDocumentAcceleratorConfiguration.is())
|
|
|
|
{
|
|
|
|
// Get the accelerator configuration for the document.
|
|
|
|
if (mxCachedDataFrame.is())
|
|
|
|
{
|
|
|
|
Reference<frame::XController> xController = mxCachedDataFrame->getController();
|
|
|
|
if (xController.is())
|
|
|
|
{
|
|
|
|
Reference<frame::XModel> xModel (xController->getModel());
|
|
|
|
if (xModel.is())
|
|
|
|
{
|
|
|
|
Reference<ui::XUIConfigurationManagerSupplier> xSupplier (xModel, UNO_QUERY);
|
|
|
|
if (xSupplier.is())
|
|
|
|
{
|
|
|
|
Reference<ui::XUIConfigurationManager> xConfigurationManager(
|
|
|
|
xSupplier->getUIConfigurationManager(),
|
|
|
|
UNO_QUERY);
|
|
|
|
if (xConfigurationManager.is())
|
|
|
|
{
|
2013-05-27 14:52:39 +02:00
|
|
|
mxCachedDocumentAcceleratorConfiguration = xConfigurationManager->getShortCutManager();
|
2013-04-19 08:51:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mxCachedDocumentAcceleratorConfiguration;
|
|
|
|
}
|
|
|
|
|
2015-04-14 12:44:47 +02:00
|
|
|
Reference<ui::XAcceleratorConfiguration> CommandInfoProvider::GetModuleAcceleratorConfiguration()
|
2013-04-19 08:51:45 +00:00
|
|
|
{
|
|
|
|
if ( ! mxCachedModuleAcceleratorConfiguration.is())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-01-29 18:58:18 +01:00
|
|
|
Reference<ui::XModuleUIConfigurationManagerSupplier> xSupplier = ui::theModuleUIConfigurationManagerSupplier::get(mxContext);
|
2013-04-19 08:51:45 +00:00
|
|
|
Reference<ui::XUIConfigurationManager> xManager (
|
|
|
|
xSupplier->getUIConfigurationManager(GetModuleIdentifier()));
|
|
|
|
if (xManager.is())
|
|
|
|
{
|
2013-05-27 14:52:39 +02:00
|
|
|
mxCachedModuleAcceleratorConfiguration = xManager->getShortCutManager();
|
2013-04-19 08:51:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mxCachedModuleAcceleratorConfiguration;
|
|
|
|
}
|
|
|
|
|
2015-04-14 12:44:47 +02:00
|
|
|
Reference<ui::XAcceleratorConfiguration> CommandInfoProvider::GetGlobalAcceleratorConfiguration()
|
2013-04-19 08:51:45 +00:00
|
|
|
{
|
|
|
|
// Get the global accelerator configuration.
|
|
|
|
if ( ! mxCachedGlobalAcceleratorConfiguration.is())
|
|
|
|
{
|
2013-05-27 14:52:39 +02:00
|
|
|
mxCachedGlobalAcceleratorConfiguration = ui::GlobalAcceleratorConfiguration::create(mxContext);
|
2013-04-19 08:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return mxCachedGlobalAcceleratorConfiguration;
|
|
|
|
}
|
|
|
|
|
2015-04-14 12:44:47 +02:00
|
|
|
OUString CommandInfoProvider::GetModuleIdentifier()
|
2013-04-19 08:51:45 +00:00
|
|
|
{
|
|
|
|
if (msCachedModuleIdentifier.getLength() == 0)
|
|
|
|
{
|
2013-05-27 14:52:39 +02:00
|
|
|
Reference<frame::XModuleManager2> xModuleManager = frame::ModuleManager::create(mxContext);
|
|
|
|
msCachedModuleIdentifier = xModuleManager->identify(mxCachedDataFrame);
|
2013-04-19 08:51:45 +00:00
|
|
|
}
|
|
|
|
return msCachedModuleIdentifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
OUString CommandInfoProvider::GetCommandShortcut (const OUString& rsCommandName)
|
|
|
|
{
|
|
|
|
OUString sShortcut;
|
|
|
|
|
|
|
|
sShortcut = RetrieveShortcutsFromConfiguration(GetDocumentAcceleratorConfiguration(), rsCommandName);
|
|
|
|
if (sShortcut.getLength() > 0)
|
|
|
|
return sShortcut;
|
|
|
|
|
|
|
|
sShortcut = RetrieveShortcutsFromConfiguration(GetModuleAcceleratorConfiguration(), rsCommandName);
|
|
|
|
if (sShortcut.getLength() > 0)
|
|
|
|
return sShortcut;
|
|
|
|
|
|
|
|
sShortcut = RetrieveShortcutsFromConfiguration(GetGlobalAcceleratorConfiguration(), rsCommandName);
|
|
|
|
if (sShortcut.getLength() > 0)
|
|
|
|
return sShortcut;
|
|
|
|
|
|
|
|
return OUString();
|
|
|
|
}
|
|
|
|
|
|
|
|
OUString CommandInfoProvider::RetrieveShortcutsFromConfiguration(
|
|
|
|
const Reference<ui::XAcceleratorConfiguration>& rxConfiguration,
|
|
|
|
const OUString& rsCommandName)
|
|
|
|
{
|
|
|
|
if (rxConfiguration.is())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Sequence<OUString> aCommands(1);
|
|
|
|
aCommands[0] = rsCommandName;
|
|
|
|
|
|
|
|
Sequence<Any> aKeyCodes (rxConfiguration->getPreferredKeyEventsForCommandList(aCommands));
|
|
|
|
if (aCommands.getLength() == 1)
|
|
|
|
{
|
|
|
|
css::awt::KeyEvent aKeyEvent;
|
|
|
|
if (aKeyCodes[0] >>= aKeyEvent)
|
|
|
|
{
|
|
|
|
return svt::AcceleratorExecute::st_AWTKey2VCLKey(aKeyEvent).GetName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (lang::IllegalArgumentException&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return OUString();
|
|
|
|
}
|
|
|
|
|
|
|
|
Sequence<beans::PropertyValue> CommandInfoProvider::GetCommandProperties (const OUString& rsCommandName)
|
|
|
|
{
|
|
|
|
Sequence<beans::PropertyValue> aProperties;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const OUString sModuleIdentifier (GetModuleIdentifier());
|
|
|
|
if (sModuleIdentifier.getLength() > 0)
|
|
|
|
{
|
2014-01-28 15:27:32 +01:00
|
|
|
Reference<container::XNameAccess> xNameAccess = frame::theUICommandDescription::get(mxContext);
|
2013-04-19 08:51:45 +00:00
|
|
|
Reference<container::XNameAccess> xUICommandLabels;
|
2013-05-27 14:52:39 +02:00
|
|
|
if (xNameAccess->getByName(sModuleIdentifier) >>= xUICommandLabels)
|
|
|
|
xUICommandLabels->getByName(rsCommandName) >>= aProperties;
|
2013-04-19 08:51:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
return aProperties;
|
|
|
|
}
|
|
|
|
|
|
|
|
OUString CommandInfoProvider::GetCommandLabel (const OUString& rsCommandName)
|
|
|
|
{
|
|
|
|
const Sequence<beans::PropertyValue> aProperties (GetCommandProperties(rsCommandName));
|
|
|
|
for (sal_Int32 nIndex=0; nIndex<aProperties.getLength(); ++nIndex)
|
|
|
|
{
|
2014-12-12 17:52:47 +01:00
|
|
|
if (aProperties[nIndex].Name == "Name")
|
2013-04-19 08:51:45 +00:00
|
|
|
{
|
|
|
|
OUString sLabel;
|
|
|
|
aProperties[nIndex].Value >>= sLabel;
|
|
|
|
return sLabel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return OUString();
|
|
|
|
}
|
|
|
|
|
|
|
|
} } // end of namespace sfx2/framework
|
2013-06-16 02:48:58 +01:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|