office-gobmx/framework/inc/uielement/styletoolbarcontroller.hxx
Maxim Monastirsky b91fb08fd6 tdf#106999 Feedback for style commands
The .uno:StyleApply command takes some arguments, which make
it possible to apply any style of any type. The problem is
that it doesn't provide toggle state to be used when placing
such commands on a toolbar or a menubar. The reason is that
sfx2, by design, can provide status updates only for the
.uno:StyleApply command as a whole, ignoring any arguments
that might be specified in the UI element description. This
behavior is even documented in the XDispatch idl.

wrt solution, changing the generic UI code to handle the
specifics of an individual command can never be a good idea.
The usual approach in such cases is to create separate
commands which will handle the status updates correctly. This
is however not possible in our case because styles can be
created by users, and we can't predict their names and create
separate commands for all of them (given that now it's easy to
add style commands to toolbars and menus - see tdf#106681).

One possible solution is to create a toolbar button controller
on top of .uno:StyleApply, which will be able to translate its
status update to a state compatible with the toolbar. The downside
is that it won't work when placing such command inside a menu, and
in general the menu code in framework doesn't provide any way to
control individual menu items, only a whole sub-menus via a
popup menu controller.

To fix the menu use-case too, this commit introduces a "proxy"
dispatch implementation, which can be used by MenuBarManager
in a transparent way, as if it was the "real" application-level
dispatcher. This means that the changes needed in MenuBarManager
are minimal and do not over-complicate the code, which is a good
thing (Alternatively, such dispatcher could be registered via
the dispatch interception mechanism, but it will add no gain,
given the current requirements, and just add more complexity).
And to make it easier to reuse the code, the new dispatcher
is also used for the toolbar solution.

Change-Id: Ia73b0fa70fd4d1d59360b255aa8fd19570b971ee
Reviewed-on: https://gerrit.libreoffice.org/37590
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Maxim Monastirsky <momonasmon@gmail.com>
2017-05-14 11:08:35 +02:00

81 lines
3.4 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* 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/.
*/
#ifndef INCLUDED_FRAMEWORK_INC_UIELEMENT_STYLETOOLBARCONTROLLER_HXX
#define INCLUDED_FRAMEWORK_INC_UIELEMENT_STYLETOOLBARCONTROLLER_HXX
#include <svtools/toolboxcontroller.hxx>
#include <com/sun/star/frame/XDispatchProvider.hpp>
namespace framework {
/**
* A dispatcher that serves as a proxy for style commands with arguments
* i.e. .uno:StyleApply?... in order to provide useful status updates to
* generic UI elements such as toolbars or menubar. It listens to special
* status commands, and computes a boolean status out of them. Then it
* forwards that boolean status to the listener, as if it was the status
* of the original command.
*
* Note that the implementation is minimal: Although the UI element appears
* to be the owner of the dispatcher, it's still responsible, as usual, to
* call removeStatusListener same amount of times as addStatusListener,
* otherwise the dispatcher might not be destructed. In addition this
* implementation might hold a hard reference on the owner, and it's the
* responsibility of the owner to destroy the dispatcher first, in order
* to break the cycle.
*/
class StyleDispatcher : public cppu::WeakImplHelper< css::frame::XDispatch, css::frame::XStatusListener >
{
public:
StyleDispatcher( const css::uno::Reference< css::frame::XFrame >& rFrame,
const css::uno::Reference< css::util::XURLTransformer >& rUrlTransformer,
const css::util::URL& rURL );
// XDispatch
void SAL_CALL dispatch( const css::util::URL& rURL, const css::uno::Sequence< css::beans::PropertyValue >& rArguments ) override;
void SAL_CALL addStatusListener( const css::uno::Reference< css::frame::XStatusListener >& rListener, const css::util::URL& rURL ) override;
void SAL_CALL removeStatusListener( const css::uno::Reference< css::frame::XStatusListener >& rListener, const css::util::URL& rURL ) override;
// XStatusListener
void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
// XEventListener
void SAL_CALL disposing( const css::lang::EventObject& rSource ) override;
private:
OUString m_aStyleName, m_aCommand, m_aStatusCommand;
css::uno::Reference< css::util::XURLTransformer > m_xUrlTransformer;
css::uno::Reference< css::frame::XDispatchProvider > m_xFrame;
css::uno::Reference< css::frame::XDispatch > m_xStatusDispatch;
css::uno::Reference< css::frame::XStatusListener > m_xOwner;
};
class StyleToolbarController : public svt::ToolboxController
{
public:
StyleToolbarController( const css::uno::Reference< css::uno::XComponentContext >& rContext,
const css::uno::Reference< css::frame::XFrame >& rFrame,
const OUString& rCommand );
// XUpdatable
void SAL_CALL update() override;
// XStatusListener
void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
// XComponent
void SAL_CALL dispose() override;
};
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */