Coverage Report

Created: 2026-03-31 11:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sfx2/source/toolbox/weldutils.cxx
Line
Count
Source
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
10
#include <officecfg/Office/Common.hxx>
11
#include <com/sun/star/frame/XFrame.hpp>
12
#include <com/sun/star/frame/XSubToolbarController.hpp>
13
#include <sidebar/ControllerFactory.hxx>
14
#include <sfx2/weldutils.hxx>
15
#include <vcl/commandinfoprovider.hxx>
16
#include <vcl/settings.hxx>
17
#include <vcl/weld/Toolbar.hxx>
18
#include <vcl/weld/weldutils.hxx>
19
20
namespace
21
{
22
bool lcl_RTLizeCommandURL(OUString& rCommandURL)
23
0
{
24
0
    if (rCommandURL == ".uno:ParaLeftToRight")
25
0
    {
26
0
        rCommandURL = ".uno:ParaRightToLeft";
27
0
        return true;
28
0
    }
29
0
    if (rCommandURL == ".uno:ParaRightToLeft")
30
0
    {
31
0
        rCommandURL = ".uno:ParaLeftToRight";
32
0
        return true;
33
0
    }
34
0
    if (rCommandURL == ".uno:LeftPara")
35
0
    {
36
0
        rCommandURL = ".uno:RightPara";
37
0
        return true;
38
0
    }
39
0
    if (rCommandURL == ".uno:RightPara")
40
0
    {
41
0
        rCommandURL = ".uno:LeftPara";
42
0
        return true;
43
0
    }
44
0
    if (rCommandURL == ".uno:AlignLeft")
45
0
    {
46
0
        rCommandURL = ".uno:AlignRight";
47
0
        return true;
48
0
    }
49
0
    if (rCommandURL == ".uno:AlignRight")
50
0
    {
51
0
        rCommandURL = ".uno:AlignLeft";
52
0
        return true;
53
0
    }
54
0
    return false;
55
0
}
56
}
57
58
// for now all controllers are in the sidebar
59
vcl::ImageType ToolbarUnoDispatcher::GetIconSize()
60
0
{
61
0
    vcl::ImageType eType = vcl::ImageType::Size16;
62
0
    switch (static_cast<ToolBoxButtonSize>(officecfg::Office::Common::Misc::SidebarIconSize::get()))
63
0
    {
64
0
        case ToolBoxButtonSize::Large:
65
0
            eType = vcl::ImageType::Size26;
66
0
            break;
67
0
        case ToolBoxButtonSize::Size32:
68
0
            eType = vcl::ImageType::Size32;
69
0
            break;
70
0
        case ToolBoxButtonSize::DontCare:
71
0
        case ToolBoxButtonSize::Small:
72
0
            break;
73
0
    }
74
0
    return eType;
75
0
}
76
77
ToolbarUnoDispatcher::ToolbarUnoDispatcher(weld::Toolbar& rToolbar, weld::Builder& rBuilder,
78
                                           const css::uno::Reference<css::frame::XFrame>& rFrame,
79
                                           bool bSideBar)
80
0
    : m_xFrame(rFrame)
81
0
    , m_pToolbar(&rToolbar)
82
0
    , m_pBuilder(&rBuilder)
83
0
    , m_bSideBar(bSideBar)
84
0
{
85
0
    rToolbar.connect_clicked(LINK(this, ToolbarUnoDispatcher, SelectHdl));
86
0
    rToolbar.connect_menu_toggled(LINK(this, ToolbarUnoDispatcher, ToggleMenuHdl));
87
88
0
    OUString aModuleName(vcl::CommandInfoProvider::GetModuleIdentifier(rFrame));
89
0
    vcl::ImageType eSize = GetIconSize();
90
0
    rToolbar.set_icon_size(eSize);
91
92
0
    bool bRTL = AllSettings::GetLayoutRTL();
93
94
0
    for (int i = 0, nItems = rToolbar.get_n_items(); i < nItems; ++i)
95
0
    {
96
0
        OUString sIdent(rToolbar.get_item_ident(i));
97
0
        if (!sIdent.startsWith(".uno:"))
98
0
            continue;
99
0
        OUString sCommand = sIdent;
100
0
        if (bRTL && lcl_RTLizeCommandURL(sCommand))
101
0
            rToolbar.set_item_ident(i, sCommand);
102
103
0
        auto aProperties = vcl::CommandInfoProvider::GetCommandProperties(sCommand, aModuleName);
104
0
        OUString aLabel(vcl::CommandInfoProvider::GetLabelForCommand(aProperties));
105
0
        rToolbar.set_item_label(i, aLabel);
106
0
        OUString aTooltip(
107
0
            vcl::CommandInfoProvider::GetTooltipForCommand(sCommand, aProperties, rFrame));
108
0
        rToolbar.set_item_tooltip_text(i, aTooltip);
109
0
        auto xImage(vcl::CommandInfoProvider::GetXGraphicForCommand(sCommand, rFrame, eSize));
110
0
        rToolbar.set_item_image(i, xImage);
111
112
0
        CreateController(sCommand);
113
0
    }
114
115
0
    rtl::Reference xWidget(new weld::TransportAsXWindow(m_pToolbar, m_pBuilder));
116
0
    m_xImageController = sfx2::sidebar::ControllerFactory::CreateImageController(m_xFrame, xWidget);
117
0
    m_aToolbarOptions.AddListenerLink(LINK(this, ToolbarUnoDispatcher, ChangedIconSizeHandler));
118
0
}
119
120
void ToolbarUnoDispatcher::CreateController(const OUString& rCommand)
121
0
{
122
0
    css::uno::Reference<css::frame::XToolbarController> xController(
123
0
        sfx2::sidebar::ControllerFactory::CreateToolBoxController(
124
0
            *m_pToolbar, *m_pBuilder, rCommand, m_xFrame, m_xFrame->getController(), m_bSideBar));
125
126
0
    if (xController.is())
127
0
        maControllers.insert(std::make_pair(rCommand, xController));
128
0
}
129
130
css::uno::Reference<css::frame::XToolbarController>
131
ToolbarUnoDispatcher::GetControllerForCommand(const OUString& rCommand) const
132
0
{
133
0
    ControllerContainer::const_iterator iController(maControllers.find(rCommand));
134
0
    if (iController != maControllers.end())
135
0
        return iController->second;
136
137
0
    return css::uno::Reference<css::frame::XToolbarController>();
138
0
}
139
140
IMPL_LINK(ToolbarUnoDispatcher, SelectHdl, const OUString&, rCommand, void)
141
0
{
142
0
    css::uno::Reference<css::frame::XToolbarController> xController(
143
0
        GetControllerForCommand(rCommand));
144
145
0
    if (xController.is())
146
0
        xController->execute(0);
147
0
}
148
149
IMPL_LINK(ToolbarUnoDispatcher, ToggleMenuHdl, const OUString&, rCommand, void)
150
0
{
151
0
    css::uno::Reference<css::frame::XToolbarController> xController(
152
0
        GetControllerForCommand(rCommand));
153
154
0
    if (xController.is())
155
0
        xController->click();
156
0
}
157
158
IMPL_LINK_NOARG(ToolbarUnoDispatcher, ChangedIconSizeHandler, LinkParamNone*, void)
159
0
{
160
0
    vcl::ImageType eSize = GetIconSize();
161
0
    m_pToolbar->set_icon_size(eSize);
162
163
0
    for (int i = 0, nItems = m_pToolbar->get_n_items(); i < nItems; ++i)
164
0
    {
165
0
        OUString sIdent(m_pToolbar->get_item_ident(i));
166
0
        auto xImage(vcl::CommandInfoProvider::GetXGraphicForCommand(sIdent, m_xFrame, eSize));
167
0
        m_pToolbar->set_item_image(sIdent, xImage);
168
0
    }
169
170
0
    for (auto const& it : maControllers)
171
0
    {
172
0
        css::uno::Reference<css::frame::XSubToolbarController> xController(it.second,
173
0
                                                                           css::uno::UNO_QUERY);
174
0
        if (xController.is() && xController->opensSubToolbar())
175
0
        {
176
            // The button should show the last function that was selected from the
177
            // dropdown. The controller should know better than us what it was.
178
0
            xController->updateImage();
179
0
        }
180
0
    }
181
0
}
182
183
void ToolbarUnoDispatcher::dispose()
184
0
{
185
0
    if (!m_pToolbar)
186
0
        return;
187
188
0
    m_aToolbarOptions.RemoveListenerLink(LINK(this, ToolbarUnoDispatcher, ChangedIconSizeHandler));
189
190
0
    ControllerContainer aControllers;
191
0
    aControllers.swap(maControllers);
192
0
    for (auto const& controller : aControllers)
193
0
    {
194
0
        css::uno::Reference<css::lang::XComponent> xComponent(controller.second,
195
0
                                                              css::uno::UNO_QUERY);
196
0
        if (xComponent.is())
197
0
            xComponent->dispose();
198
0
    }
199
200
0
    m_xImageController->dispose();
201
0
    m_pToolbar->connect_clicked(Link<const OUString&, void>());
202
0
    m_pToolbar = nullptr;
203
0
    m_pBuilder = nullptr;
204
0
}
205
206
0
ToolbarUnoDispatcher::~ToolbarUnoDispatcher() { dispose(); }
207
208
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */