LibreOffice Module framework (master) 1
addonmenu.cxx
Go to the documentation of this file.
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 <addonmenu.hxx>
22#include <menuconfiguration.hxx>
23
24#include <com/sun/star/uno/Reference.hxx>
25#include <com/sun/star/uno/Sequence.hxx>
26#include <com/sun/star/beans/PropertyValue.hpp>
27
29#include <vcl/menu.hxx>
30
31using namespace ::com::sun::star::uno;
32using namespace ::com::sun::star::frame;
33using namespace ::com::sun::star::beans;
34
35namespace framework
36{
37
39{
40 return AddonsOptions().HasAddonsMenu();
41}
42
43// Create the Add-Ons menu
44VclPtr<PopupMenu> AddonMenuManager::CreateAddonMenu( const Reference< XFrame >& rFrame )
45{
46 AddonsOptions aOptions;
47 VclPtr<PopupMenu> pAddonMenu;
48
49 const Sequence< Sequence< PropertyValue > >& rAddonMenuEntries = aOptions.GetAddonsMenu();
50 if ( rAddonMenuEntries.hasElements() )
51 {
52 sal_uInt16 nUniqueMenuId = ADDONMENU_ITEMID_START;
53 pAddonMenu = VclPtr<PopupMenu>::Create();
54 OUString aModuleIdentifier = vcl::CommandInfoProvider::GetModuleIdentifier( rFrame );
55 AddonMenuManager::BuildMenu( pAddonMenu, MENU_APPEND, nUniqueMenuId, rAddonMenuEntries, rFrame, aModuleIdentifier );
56
57 // Don't return an empty Add-On menu
58 if ( pAddonMenu->GetItemCount() == 0 )
59 {
60 pAddonMenu.disposeAndClear();
61 }
62 }
63
64 return pAddonMenu;
65}
66
67// Returns the next insert position from nPos.
68sal_uInt16 AddonMenuManager::GetNextPos( sal_uInt16 nPos )
69{
70 return ( nPos == MENU_APPEND ) ? MENU_APPEND : ( nPos+1 );
71}
72
73static sal_uInt16 FindMenuId( Menu const * pMenu, std::u16string_view aCommand )
74{
75 sal_uInt16 nPos = 0;
76 OUString aCmd;
77 for ( nPos = 0; nPos < pMenu->GetItemCount(); nPos++ )
78 {
79 sal_uInt16 nId = pMenu->GetItemId( nPos );
80 aCmd = pMenu->GetItemCommand( nId );
81 if ( aCmd == aCommand )
82 return nId;
83 }
84
85 return USHRT_MAX;
86}
87
88// Merge the Add-Ons help menu items into the given menu bar at a defined pos
89void AddonMenuManager::MergeAddonHelpMenu( const Reference< XFrame >& rFrame,
90 MenuBar const * pMergeMenuBar )
91{
92 if ( !pMergeMenuBar )
93 return;
94
95 PopupMenu* pHelpMenu(nullptr);
96 sal_uInt16 nId = FindMenuId(pMergeMenuBar, u".uno:HelpMenu");
97 if ( nId != USHRT_MAX )
98 pHelpMenu = pMergeMenuBar->GetPopupMenu( nId );
99
100 if ( !pHelpMenu )
101 return;
102
103 // Add-Ons help menu items should be inserted after the "registration" menu item
104 sal_uInt16 nItemCount = pHelpMenu->GetItemCount();
105 sal_uInt16 nInsSepAfterPos = MENU_APPEND;
106 sal_uInt16 nUniqueMenuId = ADDONMENU_ITEMID_START;
107 AddonsOptions aOptions;
108
109 // try to detect the about menu item with the command URL
110 nId = FindMenuId(pHelpMenu, u".uno:About");
111 sal_uInt16 nInsPos = pHelpMenu->GetItemPos( nId );
112
113 const Sequence< Sequence< PropertyValue > >& rAddonHelpMenuEntries = aOptions.GetAddonsHelpMenu();
114
115 if ( nInsPos < nItemCount && pHelpMenu->GetItemType( nInsPos ) != MenuItemType::SEPARATOR )
116 nInsSepAfterPos = nInsPos;
117
118 OUString aModuleIdentifier = vcl::CommandInfoProvider::GetModuleIdentifier(rFrame);
119 AddonMenuManager::BuildMenu( pHelpMenu, nInsPos, nUniqueMenuId, rAddonHelpMenuEntries, rFrame, aModuleIdentifier );
120
121 if ( pHelpMenu->GetItemCount() > nItemCount )
122 {
123 if ( nInsSepAfterPos < MENU_APPEND )
124 {
125 nInsSepAfterPos += ( pHelpMenu->GetItemCount() - nItemCount );
126 if ( pHelpMenu->GetItemType( nInsSepAfterPos ) != MenuItemType::SEPARATOR )
127 pHelpMenu->InsertSeparator({}, nInsSepAfterPos);
128 }
129 pHelpMenu->InsertSeparator({}, nItemCount);
130 }
131}
132
133// Merge the addon popup menus into the given menu bar at the provided pos.
134void AddonMenuManager::MergeAddonPopupMenus( const Reference< XFrame >& rFrame,
135 sal_uInt16 nMergeAtPos,
136 MenuBar* pMergeMenuBar )
137{
138 if ( !pMergeMenuBar )
139 return;
140
141 AddonsOptions aAddonsOptions;
142 sal_uInt16 nInsertPos = nMergeAtPos;
143
144 OUString aTitle;
145 OUString aURL;
146 OUString aTarget;
147 OUString aContext;
148 Sequence< Sequence< PropertyValue > > aAddonSubMenu;
149 sal_uInt16 nUniqueMenuId = ADDONMENU_ITEMID_START;
150
151 OUString aModuleIdentifier = vcl::CommandInfoProvider::GetModuleIdentifier(rFrame);
152
153 const Sequence< Sequence< PropertyValue > >& rAddonMenuEntries = aAddonsOptions.GetAddonsMenuBarPart();
154 for ( const Sequence< PropertyValue >& rEntry : rAddonMenuEntries )
155 {
157 aTitle,
158 aURL,
159 aTarget,
160 aContext,
161 aAddonSubMenu );
162 if ( !aTitle.isEmpty() &&
163 !aURL.isEmpty() &&
164 aAddonSubMenu.hasElements() &&
165 AddonMenuManager::IsCorrectContext( aModuleIdentifier, aContext ))
166 {
167 sal_uInt16 nId = nUniqueMenuId++;
168 VclPtrInstance<PopupMenu> pAddonPopupMenu;
169
170 AddonMenuManager::BuildMenu( pAddonPopupMenu, MENU_APPEND, nUniqueMenuId, aAddonSubMenu, rFrame, aModuleIdentifier );
171
172 if ( pAddonPopupMenu->GetItemCount() > 0 )
173 {
174 pMergeMenuBar->InsertItem( nId, aTitle, MenuItemBits::NONE, {}, nInsertPos++);
175 pMergeMenuBar->SetPopupMenu( nId, pAddonPopupMenu );
176
177 // Store the command URL into the VCL menu bar for later identification
178 pMergeMenuBar->SetItemCommand( nId, aURL );
179 }
180 else
181 pAddonPopupMenu.disposeAndClear();
182 }
183 }
184}
185
186// Insert the menu and sub menu entries into pCurrentMenu with the aAddonMenuDefinition provided
187void AddonMenuManager::BuildMenu( PopupMenu* pCurrentMenu,
188 sal_uInt16 nInsPos,
189 sal_uInt16& nUniqueMenuId,
190 const Sequence< Sequence< PropertyValue > >& aAddonMenuDefinition,
191 const Reference< XFrame >& rFrame,
192 const OUString& rModuleIdentifier )
193{
194 Sequence< Sequence< PropertyValue > > aAddonSubMenu;
195 bool bInsertSeparator = false;
196 sal_uInt32 i = 0;
197 sal_uInt32 nElements = 0;
198 sal_uInt32 nCount = aAddonMenuDefinition.getLength();
199
200 OUString aTitle;
201 OUString aURL;
202 OUString aTarget;
203 OUString aContext;
204
205 for ( i = 0; i < nCount; ++i )
206 {
207 GetMenuEntry( aAddonMenuDefinition[i], aTitle, aURL, aTarget, aContext, aAddonSubMenu );
208
209 if ( !IsCorrectContext( rModuleIdentifier, aContext ) || ( aTitle.isEmpty() && aURL.isEmpty() ))
210 continue;
211
212 if ( aURL == "private:separator" )
213 bInsertSeparator = true;
214 else
215 {
216 VclPtr<PopupMenu> pSubMenu;
217 if ( aAddonSubMenu.hasElements() )
218 {
219 pSubMenu = VclPtr<PopupMenu>::Create();
220 AddonMenuManager::BuildMenu( pSubMenu, MENU_APPEND, nUniqueMenuId, aAddonSubMenu, rFrame, rModuleIdentifier );
221
222 // Don't create a menu item for an empty sub menu
223 if ( pSubMenu->GetItemCount() == 0 )
224 {
225 pSubMenu.disposeAndClear();
226 continue;
227 }
228 }
229
230 if ( bInsertSeparator && nElements > 0 )
231 {
232 // Insert a separator only when we insert a new element afterwards and we
233 // have already one before us
234 nElements = 0;
235 bInsertSeparator = false;
236 pCurrentMenu->InsertSeparator({}, nInsPos);
237 nInsPos = AddonMenuManager::GetNextPos( nInsPos );
238 }
239
240 sal_uInt16 nId = nUniqueMenuId++;
241 pCurrentMenu->InsertItem(nId, aTitle, MenuItemBits::NONE, {}, nInsPos);
242 nInsPos = AddonMenuManager::GetNextPos( nInsPos );
243
244 ++nElements;
245
246 void* nAttributePtr = MenuAttributes::CreateAttribute(aTarget, OUString());
247 pCurrentMenu->SetUserValue(nId, nAttributePtr, MenuAttributes::ReleaseAttribute);
248 pCurrentMenu->SetItemCommand( nId, aURL );
249
250 if ( pSubMenu )
251 pCurrentMenu->SetPopupMenu( nId, pSubMenu );
252 }
253 }
254}
255
256// Retrieve the menu entry property values from a sequence
257void AddonMenuManager::GetMenuEntry( const Sequence< PropertyValue >& rAddonMenuEntry,
258 OUString& rTitle,
259 OUString& rURL,
260 OUString& rTarget,
261 OUString& rContext,
262 Sequence< Sequence< PropertyValue > >& rAddonSubMenu )
263{
264 // Reset submenu parameter
265 rAddonSubMenu = Sequence< Sequence< PropertyValue > >();
266
267 for ( const PropertyValue& rEntry : rAddonMenuEntry )
268 {
269 OUString aMenuEntryPropName = rEntry.Name;
270 if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_URL )
271 rEntry.Value >>= rURL;
272 else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_TITLE )
273 rEntry.Value >>= rTitle;
274 else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_TARGET )
275 rEntry.Value >>= rTarget;
276 else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_SUBMENU )
277 rEntry.Value >>= rAddonSubMenu;
278 else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_CONTEXT )
279 rEntry.Value >>= rContext;
280 }
281}
282
283// Check if the context string matches the provided xModel context
284bool AddonMenuManager::IsCorrectContext( std::u16string_view rModuleIdentifier, std::u16string_view rContext )
285{
286 if ( rContext.empty() )
287 return true;
288
289 if ( !rModuleIdentifier.empty() )
290 {
291 return rContext.find( rModuleIdentifier ) != std::u16string_view::npos;
292 }
293
294 return false;
295}
296
297}
298
299/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define ADDONMENU_ITEMID_START
Definition: addonmenu.hxx:32
constexpr OUStringLiteral ADDONSMENUITEM_STRING_TARGET
constexpr OUStringLiteral ADDONSMENUITEM_STRING_URL
constexpr OUStringLiteral ADDONSMENUITEM_STRING_SUBMENU
constexpr OUStringLiteral ADDONSMENUITEM_STRING_CONTEXT
constexpr OUStringLiteral ADDONSMENUITEM_STRING_TITLE
void InsertSeparator(const OUString &rIdent={}, sal_uInt16 nPos=MENU_APPEND)
MenuItemType GetItemType(sal_uInt16 nPos) const
OUString GetItemCommand(sal_uInt16 nItemId) const
sal_uInt16 GetItemId(sal_uInt16 nPos) const
void SetUserValue(sal_uInt16 nItemId, void *nUserValue, MenuUserDataReleaseFunction aFunc=nullptr)
void SetItemCommand(sal_uInt16 nItemId, const OUString &rCommand)
PopupMenu * GetPopupMenu(sal_uInt16 nItemId) const
void SetPopupMenu(sal_uInt16 nItemId, PopupMenu *pMenu)
void InsertItem(sal_uInt16 nItemId, const OUString &rStr, MenuItemBits nItemBits=MenuItemBits::NONE, const OUString &rIdent={}, sal_uInt16 nPos=MENU_APPEND)
sal_uInt16 GetItemPos(sal_uInt16 nItemId) const
sal_uInt16 GetItemCount() const
void disposeAndClear()
static VclPtr< reference_type > Create(Arg &&... arg)
static bool HasAddonMenuElements()
Definition: addonmenu.cxx:38
static void BuildMenu(PopupMenu *pCurrentMenu, sal_uInt16 nInsPos, sal_uInt16 &nUniqueMenuId, const css::uno::Sequence< css::uno::Sequence< css::beans::PropertyValue > > &aAddonMenuDefinition, const css::uno::Reference< css::frame::XFrame > &rFrame, const OUString &rModuleIdentifier)
Definition: addonmenu.cxx:187
static void MergeAddonHelpMenu(const css::uno::Reference< css::frame::XFrame > &rFrame, MenuBar const *pMergeMenuBar)
Definition: addonmenu.cxx:89
static VclPtr< PopupMenu > CreateAddonMenu(const css::uno::Reference< css::frame::XFrame > &rFrame)
Definition: addonmenu.cxx:44
static sal_uInt16 GetNextPos(sal_uInt16 nPos)
Definition: addonmenu.cxx:68
sal_Int32 nElements
int nCount
URL aURL
float u
FilterGroup & rTarget
sal_uInt16 nPos
constexpr sal_uInt16 MENU_APPEND
void GetMenuEntry(const css::uno::Sequence< css::beans::PropertyValue > &rAddonMenuEntry, AddonMenuItem &aAddonMenu)
bool IsCorrectContext(std::u16string_view aContext, std::u16string_view aModuleIdentifier)
Check whether a module identifier is part of a context defined by a colon separated list of module id...
static sal_uInt16 FindMenuId(Menu const *pMenu, std::u16string_view aCommand)
Definition: addonmenu.cxx:73
int i
OUString GetModuleIdentifier(const Reference< frame::XFrame > &rxFrame)
sal_Int16 nId
OUString aCommand