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