Line data Source code
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 <uielement/menubarmerger.hxx>
21 : #include <framework/addonsoptions.hxx>
22 :
23 : using namespace ::com::sun::star;
24 :
25 : static const char SEPARATOR_STRING[] = "private:separator";
26 :
27 : static const char MERGECOMMAND_ADDAFTER[] = "AddAfter";
28 : static const char MERGECOMMAND_ADDBEFORE[] = "AddBefore";
29 : static const char MERGECOMMAND_REPLACE[] = "Replace";
30 : static const char MERGECOMMAND_REMOVE[] = "Remove";
31 :
32 : static const char MERGEFALLBACK_ADDPATH[] = "AddPath";
33 : static const char MERGEFALLBACK_IGNORE[] = "Ignore";
34 :
35 : namespace framework
36 : {
37 :
38 : /**
39 : Check whether a module identifier is part of a context
40 : defined by a colon separated list of module identifier.
41 :
42 : @param
43 : rContext
44 :
45 : Describes a context string list where all contexts
46 : are delimited by a colon. For more information about
47 : the module identifier used as context strings see the
48 : IDL description of com::sun::star::frame::XModuleManager
49 :
50 : @param
51 : rModuleIdentifier
52 :
53 : A string describing a module identifier. See IDL
54 : description of com::sun::star::frame::XModuleManager.
55 :
56 : */
57 0 : bool MenuBarMerger::IsCorrectContext( const OUString& rContext, const OUString& rModuleIdentifier )
58 : {
59 0 : return ( rContext.isEmpty() || ( rContext.indexOf( rModuleIdentifier ) >= 0 ));
60 : }
61 :
62 0 : void MenuBarMerger::RetrieveReferencePath(
63 : const OUString& rReferencePathString,
64 : ::std::vector< OUString >& rReferencePath )
65 : {
66 0 : const sal_Char aDelimiter = '\\';
67 :
68 0 : rReferencePath.clear();
69 0 : sal_Int32 nIndex( 0 );
70 0 : do
71 : {
72 0 : OUString aToken = rReferencePathString.getToken( 0, aDelimiter, nIndex );
73 0 : if ( !aToken.isEmpty() )
74 0 : rReferencePath.push_back( aToken );
75 : }
76 0 : while ( nIndex >= 0 );
77 0 : }
78 :
79 0 : ReferencePathInfo MenuBarMerger::FindReferencePath(
80 : const ::std::vector< OUString >& rReferencePath,
81 : Menu* pMenu )
82 : {
83 0 : sal_uInt32 i( 0 );
84 0 : const sal_uInt32 nCount( rReferencePath.size() );
85 :
86 : ReferencePathInfo aResult;
87 0 : if ( !nCount )
88 : {
89 0 : aResult.pPopupMenu = NULL;
90 0 : aResult.nPos = 0;
91 0 : aResult.nLevel = -1;
92 0 : aResult.eResult = RP_MENUITEM_NOT_FOUND;
93 0 : return aResult;
94 : }
95 :
96 0 : Menu* pCurrMenu( pMenu );
97 0 : RPResultInfo eResult( RP_OK );
98 :
99 0 : sal_Int32 nLevel( - 1 );
100 0 : sal_uInt16 nPos( MENU_ITEM_NOTFOUND );
101 0 : do
102 : {
103 0 : ++nLevel;
104 0 : OUString aCmd( rReferencePath[i] );
105 :
106 0 : if ( i == nCount-1 )
107 : {
108 : // Check last reference path element. Must be a leave (menu item).
109 0 : sal_uInt16 nTmpPos = FindMenuItem( aCmd, pCurrMenu );
110 0 : if ( nTmpPos != MENU_ITEM_NOTFOUND )
111 0 : nPos = nTmpPos;
112 0 : eResult = ( nTmpPos != MENU_ITEM_NOTFOUND ) ? RP_OK : RP_MENUITEM_NOT_FOUND;
113 : }
114 : else
115 : {
116 : // Check reference path element. Must be a node (popup menu)!
117 0 : sal_uInt16 nTmpPos = FindMenuItem( aCmd, pCurrMenu );
118 0 : if ( nTmpPos != MENU_ITEM_NOTFOUND )
119 : {
120 0 : sal_uInt16 nItemId = pCurrMenu->GetItemId( nTmpPos );
121 0 : Menu* pTmpMenu = pCurrMenu->GetPopupMenu( nItemId );
122 0 : if ( pTmpMenu != 0 )
123 0 : pCurrMenu = pTmpMenu;
124 : else
125 : {
126 0 : nPos = nTmpPos;
127 0 : eResult = RP_MENUITEM_INSTEAD_OF_POPUPMENU_FOUND;
128 : }
129 : }
130 : else
131 0 : eResult = RP_POPUPMENU_NOT_FOUND;
132 : }
133 0 : i++;
134 : }
135 0 : while ((i < nCount) && (eResult == RP_OK));
136 :
137 0 : aResult.pPopupMenu = pCurrMenu;
138 0 : aResult.nPos = nPos;
139 0 : aResult.nLevel = nLevel;
140 0 : aResult.eResult = eResult;
141 :
142 0 : return aResult;
143 : }
144 :
145 0 : sal_uInt16 MenuBarMerger::FindMenuItem( const OUString& rCmd, Menu* pCurrMenu )
146 : {
147 0 : for ( sal_uInt16 i = 0; i < pCurrMenu->GetItemCount(); i++ )
148 : {
149 0 : const sal_uInt16 nItemId = pCurrMenu->GetItemId( i );
150 0 : if ( nItemId > 0 )
151 : {
152 0 : if ( rCmd == pCurrMenu->GetItemCommand( nItemId ) )
153 0 : return i;
154 : }
155 : }
156 :
157 0 : return MENU_ITEM_NOTFOUND;
158 : }
159 :
160 0 : bool MenuBarMerger::CreateSubMenu(
161 : Menu* pSubMenu,
162 : sal_uInt16& nItemId,
163 : const OUString& rModuleIdentifier,
164 : const AddonMenuContainer& rAddonSubMenu )
165 : {
166 0 : const sal_uInt32 nSize = rAddonSubMenu.size();
167 0 : for ( sal_uInt32 i = 0; i < nSize; i++ )
168 : {
169 0 : const AddonMenuItem& rMenuItem = rAddonSubMenu[i];
170 :
171 0 : if ( IsCorrectContext( rMenuItem.aContext, rModuleIdentifier ))
172 : {
173 0 : if ( rMenuItem.aURL == SEPARATOR_STRING )
174 : {
175 0 : pSubMenu->InsertSeparator();
176 : }
177 : else
178 : {
179 0 : pSubMenu->InsertItem(nItemId, rMenuItem.aTitle);
180 0 : pSubMenu->SetItemCommand( nItemId, rMenuItem.aURL );
181 0 : if ( !rMenuItem.aSubMenu.empty() )
182 : {
183 0 : PopupMenu* pPopupMenu = new PopupMenu();
184 0 : pSubMenu->SetPopupMenu( nItemId, pPopupMenu );
185 0 : ++nItemId;
186 :
187 0 : CreateSubMenu( pPopupMenu, nItemId, rModuleIdentifier, rMenuItem.aSubMenu );
188 : }
189 : else
190 0 : ++nItemId;
191 : }
192 : }
193 : }
194 :
195 0 : return true;
196 : }
197 :
198 0 : bool MenuBarMerger::MergeMenuItems(
199 : Menu* pMenu,
200 : sal_uInt16 nPos,
201 : sal_uInt16 nModIndex,
202 : sal_uInt16& nItemId,
203 : const OUString& rModuleIdentifier,
204 : const AddonMenuContainer& rAddonMenuItems )
205 : {
206 0 : sal_uInt16 nIndex( 0 );
207 0 : const sal_uInt32 nSize = rAddonMenuItems.size();
208 0 : for ( sal_uInt32 i = 0; i < nSize; i++ )
209 : {
210 0 : const AddonMenuItem& rMenuItem = rAddonMenuItems[i];
211 :
212 0 : if ( IsCorrectContext( rMenuItem.aContext, rModuleIdentifier ))
213 : {
214 0 : if ( rMenuItem.aURL == SEPARATOR_STRING )
215 : {
216 0 : pMenu->InsertSeparator(OString(), nPos+nModIndex+nIndex);
217 : }
218 : else
219 : {
220 0 : pMenu->InsertItem(nItemId, rMenuItem.aTitle, MenuItemBits::NONE, OString(), nPos+nModIndex+nIndex);
221 0 : pMenu->SetItemCommand( nItemId, rMenuItem.aURL );
222 0 : if ( !rMenuItem.aSubMenu.empty() )
223 : {
224 0 : PopupMenu* pSubMenu = new PopupMenu();
225 0 : pMenu->SetPopupMenu( nItemId, pSubMenu );
226 0 : ++nItemId;
227 :
228 0 : CreateSubMenu( pSubMenu, nItemId, rModuleIdentifier, rMenuItem.aSubMenu );
229 : }
230 : else
231 0 : ++nItemId;
232 : }
233 0 : ++nIndex;
234 : }
235 : }
236 :
237 0 : return true;
238 : }
239 :
240 0 : bool MenuBarMerger::ReplaceMenuItem(
241 : Menu* pMenu,
242 : sal_uInt16 nPos,
243 : sal_uInt16& rItemId,
244 : const OUString& rModuleIdentifier,
245 : const AddonMenuContainer& rAddonMenuItems )
246 : {
247 : // There is no replace available. Therfore we first have to
248 : // remove the old menu entry,
249 0 : pMenu->RemoveItem( nPos );
250 :
251 0 : return MergeMenuItems( pMenu, nPos, 0, rItemId, rModuleIdentifier, rAddonMenuItems );
252 : }
253 :
254 0 : bool MenuBarMerger::RemoveMenuItems(
255 : Menu* pMenu,
256 : sal_uInt16 nPos,
257 : const OUString& rMergeCommandParameter )
258 : {
259 0 : const sal_uInt16 nParam( sal_uInt16( rMergeCommandParameter.toInt32() ));
260 0 : sal_uInt16 nCount( 1 );
261 :
262 0 : nCount = std::max( nParam, nCount );
263 :
264 0 : sal_uInt16 i = 0;
265 0 : while (( nPos < pMenu->GetItemCount() ) && ( i < nCount ))
266 : {
267 0 : pMenu->RemoveItem( nPos );
268 0 : ++i;
269 : }
270 :
271 0 : return true;
272 : }
273 :
274 0 : bool MenuBarMerger::ProcessMergeOperation(
275 : Menu* pMenu,
276 : sal_uInt16 nPos,
277 : sal_uInt16& nItemId,
278 : const OUString& rMergeCommand,
279 : const OUString& rMergeCommandParameter,
280 : const OUString& rModuleIdentifier,
281 : const AddonMenuContainer& rAddonMenuItems )
282 : {
283 0 : sal_uInt16 nModIndex( 0 );
284 :
285 0 : if ( rMergeCommand == MERGECOMMAND_ADDBEFORE )
286 : {
287 0 : nModIndex = 0;
288 0 : return MergeMenuItems( pMenu, nPos, nModIndex, nItemId, rModuleIdentifier, rAddonMenuItems );
289 : }
290 0 : else if ( rMergeCommand == MERGECOMMAND_ADDAFTER )
291 : {
292 0 : nModIndex = 1;
293 0 : return MergeMenuItems( pMenu, nPos, nModIndex, nItemId, rModuleIdentifier, rAddonMenuItems );
294 : }
295 0 : else if ( rMergeCommand == MERGECOMMAND_REPLACE )
296 : {
297 0 : return ReplaceMenuItem( pMenu, nPos, nItemId, rModuleIdentifier, rAddonMenuItems );
298 : }
299 0 : else if ( rMergeCommand == MERGECOMMAND_REMOVE )
300 : {
301 0 : return RemoveMenuItems( pMenu, nPos, rMergeCommandParameter );
302 : }
303 :
304 0 : return false;
305 : }
306 :
307 0 : bool MenuBarMerger::ProcessFallbackOperation(
308 : const ReferencePathInfo& aRefPathInfo,
309 : sal_uInt16& rItemId,
310 : const OUString& rMergeCommand,
311 : const OUString& rMergeFallback,
312 : const ::std::vector< OUString >& rReferencePath,
313 : const OUString& rModuleIdentifier,
314 : const AddonMenuContainer& rAddonMenuItems )
315 : {
316 0 : if (( rMergeFallback == MERGEFALLBACK_IGNORE ) ||
317 0 : ( rMergeCommand == MERGECOMMAND_REPLACE ) ||
318 0 : ( rMergeCommand == MERGECOMMAND_REMOVE ) )
319 : {
320 0 : return true;
321 : }
322 0 : else if ( rMergeFallback == MERGEFALLBACK_ADDPATH )
323 : {
324 0 : Menu* pCurrMenu( aRefPathInfo.pPopupMenu );
325 0 : sal_Int32 nLevel( aRefPathInfo.nLevel );
326 0 : const sal_Int32 nSize( rReferencePath.size() );
327 0 : bool bFirstLevel( true );
328 :
329 0 : while ( nLevel < nSize )
330 : {
331 0 : if ( nLevel == nSize-1 )
332 : {
333 0 : const sal_uInt32 nCount = rAddonMenuItems.size();
334 0 : for ( sal_uInt32 i = 0; i < nCount; ++i )
335 : {
336 0 : const AddonMenuItem& rMenuItem = rAddonMenuItems[i];
337 0 : if ( IsCorrectContext( rMenuItem.aContext, rModuleIdentifier ))
338 : {
339 0 : if ( rMenuItem.aURL == SEPARATOR_STRING )
340 0 : pCurrMenu->InsertSeparator(OString(), MENU_APPEND);
341 : else
342 : {
343 0 : pCurrMenu->InsertItem(rItemId, rMenuItem.aTitle);
344 0 : pCurrMenu->SetItemCommand( rItemId, rMenuItem.aURL );
345 0 : ++rItemId;
346 : }
347 : }
348 : }
349 : }
350 : else
351 : {
352 0 : const OUString aCmd( rReferencePath[nLevel] );
353 :
354 0 : sal_uInt16 nInsPos( MENU_APPEND );
355 0 : PopupMenu* pPopupMenu( new PopupMenu );
356 :
357 0 : if ( bFirstLevel && ( aRefPathInfo.eResult == RP_MENUITEM_INSTEAD_OF_POPUPMENU_FOUND ))
358 : {
359 : // special case: menu item without popup
360 0 : nInsPos = aRefPathInfo.nPos;
361 0 : sal_uInt16 nSetItemId = pCurrMenu->GetItemId( nInsPos );
362 0 : pCurrMenu->SetItemCommand( nSetItemId, aCmd );
363 0 : pCurrMenu->SetPopupMenu( nSetItemId, pPopupMenu );
364 : }
365 : else
366 : {
367 : // normal case: insert a new item with popup
368 0 : pCurrMenu->InsertItem(rItemId, OUString());
369 0 : pCurrMenu->SetItemCommand( rItemId, aCmd );
370 0 : pCurrMenu->SetPopupMenu( rItemId, pPopupMenu );
371 : }
372 :
373 0 : pCurrMenu = pPopupMenu;
374 0 : ++rItemId;
375 0 : bFirstLevel = false;
376 : }
377 0 : ++nLevel;
378 : }
379 0 : return true;
380 : }
381 :
382 0 : return false;
383 : }
384 :
385 0 : void MenuBarMerger::GetMenuEntry(
386 : const uno::Sequence< beans::PropertyValue >& rAddonMenuEntry,
387 : AddonMenuItem& rAddonMenuItem )
388 : {
389 : // Reset submenu member
390 0 : rAddonMenuItem.aSubMenu.clear();
391 :
392 0 : for ( sal_Int32 i = 0; i < rAddonMenuEntry.getLength(); i++ )
393 : {
394 0 : OUString aMenuEntryPropName = rAddonMenuEntry[i].Name;
395 0 : if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_URL )
396 0 : rAddonMenuEntry[i].Value >>= rAddonMenuItem.aURL;
397 0 : else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_TITLE )
398 0 : rAddonMenuEntry[i].Value >>= rAddonMenuItem.aTitle;
399 0 : else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_TARGET )
400 0 : rAddonMenuEntry[i].Value >>= rAddonMenuItem.aTarget;
401 0 : else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_SUBMENU )
402 : {
403 0 : uno::Sequence< uno::Sequence< beans::PropertyValue > > aSubMenu;
404 0 : rAddonMenuEntry[i].Value >>= aSubMenu;
405 0 : GetSubMenu( aSubMenu, rAddonMenuItem.aSubMenu );
406 : }
407 0 : else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_CONTEXT )
408 0 : rAddonMenuEntry[i].Value >>= rAddonMenuItem.aContext;
409 0 : else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_IMAGEIDENTIFIER )
410 0 : rAddonMenuEntry[i].Value >>= rAddonMenuItem.aImageId;
411 0 : }
412 0 : }
413 :
414 0 : void MenuBarMerger::GetSubMenu(
415 : const uno::Sequence< uno::Sequence< beans::PropertyValue > >& rSubMenuEntries,
416 : AddonMenuContainer& rSubMenu )
417 : {
418 0 : rSubMenu.clear();
419 :
420 0 : const sal_Int32 nCount = rSubMenuEntries.getLength();
421 0 : rSubMenu.reserve(rSubMenu.size() + nCount);
422 0 : for ( sal_Int32 i = 0; i < nCount; i++ )
423 : {
424 0 : const uno::Sequence< beans::PropertyValue >& rMenuEntry = rSubMenuEntries[ i ];
425 :
426 0 : AddonMenuItem aMenuItem;
427 0 : GetMenuEntry( rMenuEntry, aMenuItem );
428 0 : rSubMenu.push_back( aMenuItem );
429 0 : }
430 0 : }
431 :
432 : } // namespace framework
433 :
434 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|