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