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.eResult = RP_MENUITEM_NOT_FOUND;
92 0 : return aResult;
93 : }
94 :
95 0 : Menu* pCurrMenu( pMenu );
96 0 : RPResultInfo eResult( RP_OK );
97 :
98 0 : sal_Int32 nLevel( - 1 );
99 0 : sal_uInt16 nPos( MENU_ITEM_NOTFOUND );
100 0 : do
101 : {
102 0 : ++nLevel;
103 0 : OUString aCmd( rReferencePath[i] );
104 :
105 0 : if ( i == nCount-1 )
106 : {
107 : // Check last reference path element. Must be a leave (menu item).
108 0 : sal_uInt16 nTmpPos = FindMenuItem( aCmd, pCurrMenu );
109 0 : if ( nTmpPos != MENU_ITEM_NOTFOUND )
110 0 : nPos = nTmpPos;
111 0 : eResult = ( nTmpPos != MENU_ITEM_NOTFOUND ) ? RP_OK : RP_MENUITEM_NOT_FOUND;
112 : }
113 : else
114 : {
115 : // Check reference path element. Must be a node (popup menu)!
116 0 : sal_uInt16 nTmpPos = FindMenuItem( aCmd, pCurrMenu );
117 0 : if ( nTmpPos != MENU_ITEM_NOTFOUND )
118 : {
119 0 : sal_uInt16 nItemId = pCurrMenu->GetItemId( nTmpPos );
120 0 : Menu* pTmpMenu = pCurrMenu->GetPopupMenu( nItemId );
121 0 : if ( pTmpMenu != 0 )
122 0 : pCurrMenu = pTmpMenu;
123 : else
124 : {
125 0 : nPos = nTmpPos;
126 0 : eResult = RP_MENUITEM_INSTEAD_OF_POPUPMENU_FOUND;
127 : }
128 : }
129 : else
130 0 : eResult = RP_POPUPMENU_NOT_FOUND;
131 : }
132 0 : i++;
133 : }
134 0 : while ((i < nCount) && (eResult == RP_OK));
135 :
136 0 : aResult.pPopupMenu = pCurrMenu;
137 0 : aResult.nPos = nPos;
138 0 : aResult.nLevel = nLevel;
139 0 : aResult.eResult = eResult;
140 :
141 0 : return aResult;
142 : }
143 :
144 0 : sal_uInt16 MenuBarMerger::FindMenuItem( const OUString& rCmd, Menu* pCurrMenu )
145 : {
146 0 : for ( sal_uInt16 i = 0; i < pCurrMenu->GetItemCount(); i++ )
147 : {
148 0 : const sal_uInt16 nItemId = pCurrMenu->GetItemId( i );
149 0 : if ( nItemId > 0 )
150 : {
151 0 : if ( rCmd == pCurrMenu->GetItemCommand( nItemId ) )
152 0 : return i;
153 : }
154 : }
155 :
156 0 : return MENU_ITEM_NOTFOUND;
157 : }
158 :
159 0 : bool MenuBarMerger::CreateSubMenu(
160 : Menu* pSubMenu,
161 : sal_uInt16& nItemId,
162 : const OUString& rModuleIdentifier,
163 : const AddonMenuContainer& rAddonSubMenu )
164 : {
165 0 : const sal_uInt32 nSize = rAddonSubMenu.size();
166 0 : for ( sal_uInt32 i = 0; i < nSize; i++ )
167 : {
168 0 : const AddonMenuItem& rMenuItem = rAddonSubMenu[i];
169 :
170 0 : if ( IsCorrectContext( rMenuItem.aContext, rModuleIdentifier ))
171 : {
172 0 : if ( rMenuItem.aURL == SEPARATOR_STRING )
173 : {
174 0 : pSubMenu->InsertSeparator();
175 : }
176 : else
177 : {
178 0 : pSubMenu->InsertItem(nItemId, rMenuItem.aTitle);
179 0 : pSubMenu->SetItemCommand( nItemId, rMenuItem.aURL );
180 0 : if ( !rMenuItem.aSubMenu.empty() )
181 : {
182 0 : PopupMenu* pPopupMenu = new PopupMenu();
183 0 : pSubMenu->SetPopupMenu( nItemId, pPopupMenu );
184 0 : ++nItemId;
185 :
186 0 : CreateSubMenu( pPopupMenu, nItemId, rModuleIdentifier, rMenuItem.aSubMenu );
187 : }
188 : else
189 0 : ++nItemId;
190 : }
191 : }
192 : }
193 :
194 0 : return true;
195 : }
196 :
197 0 : bool MenuBarMerger::MergeMenuItems(
198 : Menu* pMenu,
199 : sal_uInt16 nPos,
200 : sal_uInt16 nModIndex,
201 : sal_uInt16& nItemId,
202 : const OUString& rModuleIdentifier,
203 : const AddonMenuContainer& rAddonMenuItems )
204 : {
205 0 : sal_uInt16 nIndex( 0 );
206 0 : const sal_uInt32 nSize = rAddonMenuItems.size();
207 0 : for ( sal_uInt32 i = 0; i < nSize; i++ )
208 : {
209 0 : const AddonMenuItem& rMenuItem = rAddonMenuItems[i];
210 :
211 0 : if ( IsCorrectContext( rMenuItem.aContext, rModuleIdentifier ))
212 : {
213 0 : if ( rMenuItem.aURL == SEPARATOR_STRING )
214 : {
215 0 : pMenu->InsertSeparator(OString(), nPos+nModIndex+nIndex);
216 : }
217 : else
218 : {
219 0 : pMenu->InsertItem(nItemId, rMenuItem.aTitle, 0, OString(), nPos+nModIndex+nIndex);
220 0 : pMenu->SetItemCommand( nItemId, rMenuItem.aURL );
221 0 : if ( !rMenuItem.aSubMenu.empty() )
222 : {
223 0 : PopupMenu* pSubMenu = new PopupMenu();
224 0 : pMenu->SetPopupMenu( nItemId, pSubMenu );
225 0 : ++nItemId;
226 :
227 0 : CreateSubMenu( pSubMenu, nItemId, rModuleIdentifier, rMenuItem.aSubMenu );
228 : }
229 : else
230 0 : ++nItemId;
231 : }
232 0 : ++nIndex;
233 : }
234 : }
235 :
236 0 : return true;
237 : }
238 :
239 0 : bool MenuBarMerger::ReplaceMenuItem(
240 : Menu* pMenu,
241 : sal_uInt16 nPos,
242 : sal_uInt16& rItemId,
243 : const OUString& rModuleIdentifier,
244 : const AddonMenuContainer& rAddonMenuItems )
245 : {
246 : // There is no replace available. Therfore we first have to
247 : // remove the old menu entry,
248 0 : pMenu->RemoveItem( nPos );
249 :
250 0 : return MergeMenuItems( pMenu, nPos, 0, rItemId, rModuleIdentifier, rAddonMenuItems );
251 : }
252 :
253 0 : bool MenuBarMerger::RemoveMenuItems(
254 : Menu* pMenu,
255 : sal_uInt16 nPos,
256 : const OUString& rMergeCommandParameter )
257 : {
258 0 : const sal_uInt16 nParam( sal_uInt16( rMergeCommandParameter.toInt32() ));
259 0 : sal_uInt16 nCount( 1 );
260 :
261 0 : nCount = std::max( nParam, nCount );
262 :
263 0 : sal_uInt16 i = 0;
264 0 : while (( nPos < pMenu->GetItemCount() ) && ( i < nCount ))
265 : {
266 0 : pMenu->RemoveItem( nPos );
267 0 : ++i;
268 : }
269 :
270 0 : return true;
271 : }
272 :
273 0 : bool MenuBarMerger::ProcessMergeOperation(
274 : Menu* pMenu,
275 : sal_uInt16 nPos,
276 : sal_uInt16& nItemId,
277 : const OUString& rMergeCommand,
278 : const OUString& rMergeCommandParameter,
279 : const OUString& rModuleIdentifier,
280 : const AddonMenuContainer& rAddonMenuItems )
281 : {
282 0 : sal_uInt16 nModIndex( 0 );
283 :
284 0 : if ( rMergeCommand == MERGECOMMAND_ADDBEFORE )
285 : {
286 0 : nModIndex = 0;
287 0 : return MergeMenuItems( pMenu, nPos, nModIndex, nItemId, rModuleIdentifier, rAddonMenuItems );
288 : }
289 0 : else if ( rMergeCommand == MERGECOMMAND_ADDAFTER )
290 : {
291 0 : nModIndex = 1;
292 0 : return MergeMenuItems( pMenu, nPos, nModIndex, nItemId, rModuleIdentifier, rAddonMenuItems );
293 : }
294 0 : else if ( rMergeCommand == MERGECOMMAND_REPLACE )
295 : {
296 0 : return ReplaceMenuItem( pMenu, nPos, nItemId, rModuleIdentifier, rAddonMenuItems );
297 : }
298 0 : else if ( rMergeCommand == MERGECOMMAND_REMOVE )
299 : {
300 0 : return RemoveMenuItems( pMenu, nPos, rMergeCommandParameter );
301 : }
302 :
303 0 : return false;
304 : }
305 :
306 0 : bool MenuBarMerger::ProcessFallbackOperation(
307 : const ReferencePathInfo& aRefPathInfo,
308 : sal_uInt16& rItemId,
309 : const OUString& rMergeCommand,
310 : const OUString& rMergeFallback,
311 : const ::std::vector< OUString >& rReferencePath,
312 : const OUString& rModuleIdentifier,
313 : const AddonMenuContainer& rAddonMenuItems )
314 : {
315 0 : if (( rMergeFallback == MERGEFALLBACK_IGNORE ) ||
316 0 : ( rMergeCommand == MERGECOMMAND_REPLACE ) ||
317 0 : ( rMergeCommand == MERGECOMMAND_REMOVE ) )
318 : {
319 0 : return true;
320 : }
321 0 : else if ( rMergeFallback == MERGEFALLBACK_ADDPATH )
322 : {
323 0 : Menu* pCurrMenu( aRefPathInfo.pPopupMenu );
324 0 : sal_Int32 nLevel( aRefPathInfo.nLevel );
325 0 : const sal_Int32 nSize( rReferencePath.size() );
326 0 : bool bFirstLevel( true );
327 :
328 0 : while ( nLevel < nSize )
329 : {
330 0 : if ( nLevel == nSize-1 )
331 : {
332 0 : const sal_uInt32 nCount = rAddonMenuItems.size();
333 0 : for ( sal_uInt32 i = 0; i < nCount; ++i )
334 : {
335 0 : const AddonMenuItem& rMenuItem = rAddonMenuItems[i];
336 0 : if ( IsCorrectContext( rMenuItem.aContext, rModuleIdentifier ))
337 : {
338 0 : if ( rMenuItem.aURL == SEPARATOR_STRING )
339 0 : pCurrMenu->InsertSeparator(OString(), MENU_APPEND);
340 : else
341 : {
342 0 : pCurrMenu->InsertItem(rItemId, rMenuItem.aTitle);
343 0 : pCurrMenu->SetItemCommand( rItemId, rMenuItem.aURL );
344 0 : ++rItemId;
345 : }
346 : }
347 : }
348 : }
349 : else
350 : {
351 0 : const OUString aCmd( rReferencePath[nLevel] );
352 :
353 0 : sal_uInt16 nInsPos( MENU_APPEND );
354 0 : PopupMenu* pPopupMenu( new PopupMenu );
355 :
356 0 : if ( bFirstLevel && ( aRefPathInfo.eResult == RP_MENUITEM_INSTEAD_OF_POPUPMENU_FOUND ))
357 : {
358 : // special case: menu item without popup
359 0 : nInsPos = aRefPathInfo.nPos;
360 0 : sal_uInt16 nSetItemId = pCurrMenu->GetItemId( nInsPos );
361 0 : pCurrMenu->SetItemCommand( nSetItemId, aCmd );
362 0 : pCurrMenu->SetPopupMenu( nSetItemId, pPopupMenu );
363 : }
364 : else
365 : {
366 : // normal case: insert a new item with popup
367 0 : pCurrMenu->InsertItem(rItemId, OUString());
368 0 : pCurrMenu->SetItemCommand( rItemId, aCmd );
369 0 : pCurrMenu->SetPopupMenu( rItemId, pPopupMenu );
370 : }
371 :
372 0 : pCurrMenu = pPopupMenu;
373 0 : ++rItemId;
374 0 : bFirstLevel = false;
375 : }
376 0 : ++nLevel;
377 : }
378 0 : return true;
379 : }
380 :
381 0 : return false;
382 : }
383 :
384 0 : void MenuBarMerger::GetMenuEntry(
385 : const uno::Sequence< beans::PropertyValue >& rAddonMenuEntry,
386 : AddonMenuItem& rAddonMenuItem )
387 : {
388 : // Reset submenu member
389 0 : rAddonMenuItem.aSubMenu.clear();
390 :
391 0 : for ( sal_Int32 i = 0; i < rAddonMenuEntry.getLength(); i++ )
392 : {
393 0 : OUString aMenuEntryPropName = rAddonMenuEntry[i].Name;
394 0 : if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_URL )
395 0 : rAddonMenuEntry[i].Value >>= rAddonMenuItem.aURL;
396 0 : else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_TITLE )
397 0 : rAddonMenuEntry[i].Value >>= rAddonMenuItem.aTitle;
398 0 : else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_TARGET )
399 0 : rAddonMenuEntry[i].Value >>= rAddonMenuItem.aTarget;
400 0 : else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_SUBMENU )
401 : {
402 0 : uno::Sequence< uno::Sequence< beans::PropertyValue > > aSubMenu;
403 0 : rAddonMenuEntry[i].Value >>= aSubMenu;
404 0 : GetSubMenu( aSubMenu, rAddonMenuItem.aSubMenu );
405 : }
406 0 : else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_CONTEXT )
407 0 : rAddonMenuEntry[i].Value >>= rAddonMenuItem.aContext;
408 0 : else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_IMAGEIDENTIFIER )
409 0 : rAddonMenuEntry[i].Value >>= rAddonMenuItem.aImageId;
410 0 : }
411 0 : }
412 :
413 0 : void MenuBarMerger::GetSubMenu(
414 : const uno::Sequence< uno::Sequence< beans::PropertyValue > >& rSubMenuEntries,
415 : AddonMenuContainer& rSubMenu )
416 : {
417 0 : rSubMenu.clear();
418 :
419 0 : const sal_Int32 nCount = rSubMenuEntries.getLength();
420 0 : rSubMenu.reserve(rSubMenu.size() + nCount);
421 0 : for ( sal_Int32 i = 0; i < nCount; i++ )
422 : {
423 0 : const uno::Sequence< beans::PropertyValue >& rMenuEntry = rSubMenuEntries[ i ];
424 :
425 0 : AddonMenuItem aMenuItem;
426 0 : GetMenuEntry( rMenuEntry, aMenuItem );
427 0 : rSubMenu.push_back( aMenuItem );
428 0 : }
429 0 : }
430 :
431 : } // namespace framework
432 :
433 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|