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 const char STYLE_TEXT[] = "Text";
33 const char STYLE_ICON[] = "Icon";
34 
35 const char MERGE_NOTEBOOKBAR_URL[] = "URL";
36 const char MERGE_NOTEBOOKBAR_TITLE[] = "Title";
37 const char MERGE_NOTEBOOKBAR_CONTEXT[] = "Context";
38 const char MERGE_NOTEBOOKBAR_TARGET[] = "Target";
39 const char MERGE_NOTEBOOKBAR_CONTROLTYPE[] = "ControlType";
40 const char MERGE_NOTEBOOKBAR_WIDTH[] = "Width";
41 const char MERGE_NOTEBOOKBAR_STYLE[] = "Style";
42 
GetAddonNotebookBarItem(const css::uno::Sequence<css::beans::PropertyValue> & pExtension,AddonNotebookBarItem & aAddonNotebookBarItem)43 static void GetAddonNotebookBarItem(const css::uno::Sequence<css::beans::PropertyValue>& pExtension,
44                                     AddonNotebookBarItem& aAddonNotebookBarItem)
45 {
46     for (const auto& i : pExtension)
47     {
48         if (i.Name == MERGE_NOTEBOOKBAR_URL)
49             i.Value >>= aAddonNotebookBarItem.sCommandURL;
50         else if (i.Name == MERGE_NOTEBOOKBAR_TITLE)
51             i.Value >>= aAddonNotebookBarItem.sLabel;
52         else if (i.Name == MERGE_NOTEBOOKBAR_CONTEXT)
53             i.Value >>= aAddonNotebookBarItem.sContext;
54         else if (i.Name == MERGE_NOTEBOOKBAR_TARGET)
55             i.Value >>= aAddonNotebookBarItem.sTarget;
56         else if (i.Name == MERGE_NOTEBOOKBAR_CONTROLTYPE)
57             i.Value >>= aAddonNotebookBarItem.sControlType;
58         else if (i.Name == MERGE_NOTEBOOKBAR_WIDTH)
59             i.Value >>= aAddonNotebookBarItem.nWidth;
60         else if (i.Name == MERGE_NOTEBOOKBAR_STYLE)
61             i.Value >>= aAddonNotebookBarItem.sStyle;
62     }
63 }
64 
CreateNotebookBarToolBox(vcl::Window * pNotebookbarToolBox,const css::uno::Reference<css::frame::XFrame> & m_xFrame,const AddonNotebookBarItem & aAddonNotebookBarItem,const std::vector<Image> & aImageVec,const tools::ULong nIter)65 static void CreateNotebookBarToolBox(vcl::Window* pNotebookbarToolBox,
66                                      const css::uno::Reference<css::frame::XFrame>& m_xFrame,
67                                      const AddonNotebookBarItem& aAddonNotebookBarItem,
68                                      const std::vector<Image>& aImageVec, const tools::ULong nIter)
69 {
70     ToolBoxItemId nItemId(0);
71     ToolBox* pToolbox = dynamic_cast<ToolBox*>(pNotebookbarToolBox);
72     if (!pToolbox)
73         return;
74 
75     pToolbox->InsertSeparator();
76     pToolbox->Show();
77     Size aSize(0, 0);
78     Image sImage;
79     pToolbox->InsertItem(aAddonNotebookBarItem.sCommandURL, m_xFrame, ToolBoxItemBits::NONE, aSize);
80     nItemId = pToolbox->GetItemId(aAddonNotebookBarItem.sCommandURL);
81     pToolbox->SetItemCommand(nItemId, aAddonNotebookBarItem.sCommandURL);
82     pToolbox->SetQuickHelpText(nItemId, aAddonNotebookBarItem.sLabel);
83 
84     if (nIter < aImageVec.size())
85     {
86         sImage = aImageVec[nIter];
87         if (!sImage)
88         {
89             sImage = vcl::CommandInfoProvider::GetImageForCommand(aAddonNotebookBarItem.sCommandURL,
90                                                                   m_xFrame);
91         }
92     }
93 
94     if (aAddonNotebookBarItem.sStyle == STYLE_TEXT)
95         pToolbox->SetItemText(nItemId, aAddonNotebookBarItem.sLabel);
96     else if (aAddonNotebookBarItem.sStyle == STYLE_ICON)
97         pToolbox->SetItemImage(nItemId, sImage);
98     else
99     {
100         pToolbox->SetItemText(nItemId, aAddonNotebookBarItem.sLabel);
101         pToolbox->SetItemImage(nItemId, sImage);
102     }
103     pToolbox->Show();
104 }
105 
106 namespace NotebookBarAddonsMerger
107 {
MergeNotebookBarAddons(vcl::Window * pParent,const VclBuilder::customMakeWidget & pFunction,const css::uno::Reference<css::frame::XFrame> & m_xFrame,const NotebookBarAddonsItem & aNotebookBarAddonsItem,VclBuilder::stringmap & rMap)108 void MergeNotebookBarAddons(vcl::Window* pParent, const VclBuilder::customMakeWidget& pFunction,
109                             const css::uno::Reference<css::frame::XFrame>& m_xFrame,
110                             const NotebookBarAddonsItem& aNotebookBarAddonsItem,
111                             VclBuilder::stringmap& rMap)
112 {
113     std::vector<Image> aImageVec = aNotebookBarAddonsItem.aImageValues;
114     tools::ULong nIter = 0;
115     sal_uInt16 nPriorityIdx = aImageVec.size();
116     css::uno::Sequence<css::uno::Sequence<css::beans::PropertyValue>> aExtension;
117     for (std::size_t nIdx = 0; nIdx < aNotebookBarAddonsItem.aAddonValues.size(); nIdx++)
118     {
119         aExtension = aNotebookBarAddonsItem.aAddonValues[nIdx];
120 
121         for (const css::uno::Sequence<css::beans::PropertyValue>& pExtension :
122              std::as_const(aExtension))
123         {
124             VclPtr<vcl::Window> pOptionalParent;
125             pOptionalParent = VclPtr<OptionalBox>::Create(pParent);
126             pOptionalParent->Show();
127 
128             vcl::IPrioritable* pPrioritable
129                 = dynamic_cast<vcl::IPrioritable*>(pOptionalParent.get());
130             if (pPrioritable)
131                 pPrioritable->SetPriority(nPriorityIdx - nIter);
132 
133             VclPtr<vcl::Window> pNotebookbarToolBox;
134             pFunction(pNotebookbarToolBox, pOptionalParent, rMap);
135 
136             AddonNotebookBarItem aAddonNotebookBarItem;
137             GetAddonNotebookBarItem(pExtension, aAddonNotebookBarItem);
138 
139             CreateNotebookBarToolBox(pNotebookbarToolBox, m_xFrame, aAddonNotebookBarItem,
140                                      aImageVec, nIter);
141             nIter++;
142         }
143     }
144 }
145 
MergeNotebookBarMenuAddons(Menu * pPopupMenu,sal_Int16 nItemId,const OString & sItemIdName,NotebookBarAddonsItem & aNotebookBarAddonsItem)146 void MergeNotebookBarMenuAddons(Menu* pPopupMenu, sal_Int16 nItemId, const OString& sItemIdName,
147                                 NotebookBarAddonsItem& aNotebookBarAddonsItem)
148 {
149     std::vector<Image> aImageVec = aNotebookBarAddonsItem.aImageValues;
150     tools::ULong nIter = 0;
151     css::uno::Sequence<css::uno::Sequence<css::beans::PropertyValue>> aExtension;
152     for (std::size_t nIdx = 0; nIdx < aNotebookBarAddonsItem.aAddonValues.size(); nIdx++)
153     {
154         aExtension = aNotebookBarAddonsItem.aAddonValues[nIdx];
155 
156         for (int nSecIdx = 0; nSecIdx < aExtension.getLength(); nSecIdx++)
157         {
158             AddonNotebookBarItem aAddonNotebookBarItem;
159             Image sImage;
160             MenuItemBits nBits = MenuItemBits::ICON;
161             const css::uno::Sequence<css::beans::PropertyValue> pExtension = aExtension[nSecIdx];
162 
163             GetAddonNotebookBarItem(pExtension, aAddonNotebookBarItem);
164 
165             pPopupMenu->InsertItem(nItemId, aAddonNotebookBarItem.sLabel, nBits, sItemIdName);
166             pPopupMenu->SetItemCommand(nItemId, aAddonNotebookBarItem.sCommandURL);
167 
168             if (nIter < aImageVec.size())
169             {
170                 sImage = aImageVec[nIter];
171                 nIter++;
172             }
173             pPopupMenu->SetItemImage(nItemId, sImage);
174 
175             if (nSecIdx == aExtension.getLength() - 1)
176                 pPopupMenu->InsertSeparator();
177 
178             ++nItemId;
179         }
180     }
181 }
182 }
183 
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
185