Coverage Report

Created: 2026-03-31 11:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/source/window/NotebookBarAddonsMerger.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
 * 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
 */
19
20
#include <sal/config.h>
21
22
#include <cstddef>
23
24
#include <vcl/notebookbar/NotebookBarAddonsItem.hxx>
25
#include <vcl/notebookbar/NotebookBarAddonsMerger.hxx>
26
#include <vcl/commandinfoprovider.hxx>
27
#include <vcl/image.hxx>
28
#include <vcl/menu.hxx>
29
#include <vcl/vclenum.hxx>
30
#include <vcl/toolbox.hxx>
31
#include <IPrioritable.hxx>
32
#include <OptionalBox.hxx>
33
34
// #tdf146101: For getting the app context
35
#include <com/sun/star/frame/XFrame.hpp>
36
#include <com/sun/star/frame/XModuleManager.hpp>
37
#include <com/sun/star/frame/ModuleManager.hpp>
38
#include <comphelper/processfactory.hxx>
39
#include <rtl/ustring.hxx>
40
#include <o3tl/string_view.hxx>
41
42
const char STYLE_TEXT[] = "Text";
43
const char STYLE_ICON[] = "Icon";
44
45
const char MERGE_NOTEBOOKBAR_URL[] = "URL";
46
const char MERGE_NOTEBOOKBAR_TITLE[] = "Title";
47
const char MERGE_NOTEBOOKBAR_CONTEXT[] = "Context";
48
const char MERGE_NOTEBOOKBAR_TARGET[] = "Target";
49
const char MERGE_NOTEBOOKBAR_CONTROLTYPE[] = "ControlType";
50
const char MERGE_NOTEBOOKBAR_WIDTH[] = "Width";
51
const char MERGE_NOTEBOOKBAR_STYLE[] = "Style";
52
53
// Get the current app context
54
static OUString getCurrentDocumentContext(const css::uno::Reference<css::frame::XFrame>& xFrame)
55
0
{
56
0
    css::uno::Reference<css::frame::XModuleManager> xModuleManager
57
0
        = css::frame::ModuleManager::create(comphelper::getProcessComponentContext());
58
0
    return xModuleManager->identify(xFrame); // e.g. "com.sun.star.text.TextDocument"
59
0
}
60
61
static void GetAddonNotebookBarItem(const css::uno::Sequence<css::beans::PropertyValue>& pExtension,
62
                                    AddonNotebookBarItem& aAddonNotebookBarItem)
63
0
{
64
0
    for (const auto& i : pExtension)
65
0
    {
66
0
        if (i.Name == MERGE_NOTEBOOKBAR_URL)
67
0
            i.Value >>= aAddonNotebookBarItem.sCommandURL;
68
0
        else if (i.Name == MERGE_NOTEBOOKBAR_TITLE)
69
0
            i.Value >>= aAddonNotebookBarItem.sLabel;
70
0
        else if (i.Name == MERGE_NOTEBOOKBAR_CONTEXT)
71
0
            i.Value >>= aAddonNotebookBarItem.sContext;
72
0
        else if (i.Name == MERGE_NOTEBOOKBAR_TARGET)
73
0
            i.Value >>= aAddonNotebookBarItem.sTarget;
74
0
        else if (i.Name == MERGE_NOTEBOOKBAR_CONTROLTYPE)
75
0
            i.Value >>= aAddonNotebookBarItem.sControlType;
76
0
        else if (i.Name == MERGE_NOTEBOOKBAR_WIDTH)
77
0
            i.Value >>= aAddonNotebookBarItem.nWidth;
78
0
        else if (i.Name == MERGE_NOTEBOOKBAR_STYLE)
79
0
            i.Value >>= aAddonNotebookBarItem.sStyle;
80
0
    }
81
0
}
82
83
static void CreateNotebookBarToolBox(vcl::Window* pNotebookbarToolBox,
84
                                     const css::uno::Reference<css::frame::XFrame>& m_xFrame,
85
                                     const AddonNotebookBarItem& aAddonNotebookBarItem,
86
                                     const std::vector<Image>& aImageVec, const tools::ULong nIter)
87
0
{
88
0
    ToolBoxItemId nItemId(0);
89
0
    ToolBox* pToolbox = dynamic_cast<ToolBox*>(pNotebookbarToolBox);
90
0
    if (!pToolbox)
91
0
        return;
92
93
0
    pToolbox->InsertSeparator();
94
0
    pToolbox->Show();
95
0
    Size aSize(0, 0);
96
0
    Image sImage;
97
0
    pToolbox->InsertItem(aAddonNotebookBarItem.sCommandURL, m_xFrame, ToolBoxItemBits::NONE, aSize);
98
0
    nItemId = pToolbox->GetItemId(aAddonNotebookBarItem.sCommandURL);
99
0
    pToolbox->SetQuickHelpText(nItemId, aAddonNotebookBarItem.sLabel);
100
101
0
    if (nIter < aImageVec.size())
102
0
    {
103
0
        sImage = aImageVec[nIter];
104
0
        if (!sImage)
105
0
        {
106
0
            sImage = vcl::CommandInfoProvider::GetImageForCommand(aAddonNotebookBarItem.sCommandURL,
107
0
                                                                  m_xFrame);
108
0
        }
109
0
    }
110
111
0
    if (aAddonNotebookBarItem.sStyle == STYLE_TEXT)
112
0
        pToolbox->SetItemText(nItemId, aAddonNotebookBarItem.sLabel);
113
0
    else if (aAddonNotebookBarItem.sStyle == STYLE_ICON)
114
0
        pToolbox->SetItemImage(nItemId, sImage);
115
0
    else
116
0
    {
117
0
        pToolbox->SetItemText(nItemId, aAddonNotebookBarItem.sLabel);
118
0
        pToolbox->SetItemImage(nItemId, sImage);
119
0
    }
120
0
    pToolbox->Show();
121
0
}
122
123
namespace NotebookBarAddonsMerger
124
{
125
void MergeNotebookBarAddons(vcl::Window* pParent, const VclBuilder::customMakeWidget& pFunction,
126
                            const css::uno::Reference<css::frame::XFrame>& m_xFrame,
127
                            const NotebookBarAddonsItem& aNotebookBarAddonsItem,
128
                            VclBuilder::stringmap& rMap)
129
0
{
130
0
    std::vector<Image> aImageVec = aNotebookBarAddonsItem.aImageValues;
131
0
    tools::ULong nIter = 0;
132
0
    sal_uInt16 nPriorityIdx = aImageVec.size();
133
134
0
    for (const auto& aExtension : aNotebookBarAddonsItem.aAddonValues)
135
0
    {
136
0
        for (const auto& pExtension : aExtension)
137
0
        {
138
0
            VclPtr<vcl::Window> pOptionalParent;
139
0
            pOptionalParent = VclPtr<OptionalBox>::Create(pParent);
140
0
            pOptionalParent->Show();
141
142
0
            vcl::IPrioritable* pPrioritable
143
0
                = dynamic_cast<vcl::IPrioritable*>(pOptionalParent.get());
144
0
            if (pPrioritable)
145
0
                pPrioritable->SetPriority(nPriorityIdx - nIter);
146
147
0
            VclPtr<vcl::Window> pNotebookbarToolBox;
148
0
            pFunction(pNotebookbarToolBox, pOptionalParent, rMap);
149
150
0
            AddonNotebookBarItem aAddonNotebookBarItem;
151
0
            GetAddonNotebookBarItem(pExtension, aAddonNotebookBarItem);
152
            // #tdf146101: Filter context
153
0
            if (!aAddonNotebookBarItem.sContext.isEmpty())
154
0
            {
155
0
                OUString currentContext = getCurrentDocumentContext(m_xFrame);
156
0
                bool bMatch = false;
157
0
                std::u16string_view contextView = aAddonNotebookBarItem.sContext;
158
0
                sal_Int32 nIndex = 0;
159
0
                do
160
0
                {
161
                    // might be multiple contexts, separated by comma
162
0
                    std::u16string_view ctx
163
0
                        = o3tl::trim(o3tl::getToken(contextView, 0, ',', nIndex));
164
0
                    if (ctx == currentContext)
165
0
                    {
166
0
                        bMatch = true;
167
0
                        break;
168
0
                    }
169
0
                } while (nIndex != -1);
170
0
                if (!bMatch)
171
0
                {
172
0
                    nIter++; // or icons would be wrong
173
0
                    continue;
174
0
                }
175
0
            }
176
0
            CreateNotebookBarToolBox(pNotebookbarToolBox, m_xFrame, aAddonNotebookBarItem,
177
0
                                     aImageVec, nIter);
178
0
            nIter++;
179
0
        }
180
0
    }
181
0
}
182
183
void MergeNotebookBarMenuAddons(Menu* pPopupMenu, sal_Int16 nItemId, const OUString& sItemIdName,
184
                                const css::uno::Reference<css::frame::XFrame>& m_xFrame,
185
                                NotebookBarAddonsItem& aNotebookBarAddonsItem)
186
0
{
187
0
    std::vector<Image> aImageVec = aNotebookBarAddonsItem.aImageValues;
188
0
    tools::ULong nIter = 0;
189
0
    for (const auto& aExtension : aNotebookBarAddonsItem.aAddonValues)
190
0
    {
191
0
        for (int nSecIdx = 0; nSecIdx < aExtension.getLength(); nSecIdx++)
192
0
        {
193
0
            AddonNotebookBarItem aAddonNotebookBarItem;
194
0
            Image sImage;
195
0
            MenuItemBits nBits = MenuItemBits::ICON;
196
0
            const css::uno::Sequence<css::beans::PropertyValue>& pExtension = aExtension[nSecIdx];
197
198
0
            GetAddonNotebookBarItem(pExtension, aAddonNotebookBarItem);
199
200
            // #tdf146101: Filter context
201
0
            if (!aAddonNotebookBarItem.sContext.isEmpty())
202
0
            {
203
0
                OUString currentContext = getCurrentDocumentContext(m_xFrame);
204
0
                bool bMatch = false;
205
0
                std::u16string_view contextView = aAddonNotebookBarItem.sContext;
206
0
                sal_Int32 nIndex = 0;
207
0
                do
208
0
                {
209
                    // might be multiple contexts, separated by comma
210
0
                    std::u16string_view ctx
211
0
                        = o3tl::trim(o3tl::getToken(contextView, 0, ',', nIndex));
212
0
                    if (ctx == currentContext)
213
0
                    {
214
0
                        bMatch = true;
215
0
                        break;
216
0
                    }
217
0
                } while (nIndex != -1);
218
0
                if (!bMatch)
219
0
                {
220
0
                    nIter++; // or icons would be wrong
221
0
                    continue;
222
0
                }
223
0
            }
224
225
0
            pPopupMenu->InsertItem(nItemId, aAddonNotebookBarItem.sLabel, nBits, sItemIdName);
226
0
            pPopupMenu->SetItemCommand(nItemId, aAddonNotebookBarItem.sCommandURL);
227
228
0
            if (nIter < aImageVec.size())
229
0
            {
230
0
                sImage = aImageVec[nIter];
231
0
                nIter++;
232
0
            }
233
0
            pPopupMenu->SetItemImage(nItemId, sImage);
234
235
0
            if (nSecIdx == aExtension.getLength() - 1)
236
0
                pPopupMenu->InsertSeparator();
237
238
0
            ++nItemId;
239
0
        }
240
0
    }
241
0
}
242
}
243
244
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */