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
435
436
437
438
439
440
441
442
443
444
445
446
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* Version: MPL 1.1 / GPLv3+ / LGPLv3+
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License or as specified alternatively below. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Initial Developer of the Original Code is
* Noel Power <noel.power@novell.com>
* Portions created by the Initial Developer are Copyright (C) 2010 the
* Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Noel Power <noel.power@novell.com>
*
* For minor contributions see the git repository.
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 3 or later (the "GPLv3+"), or
* the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
* in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
* instead of those above.
*/
#include "xltoolbar.hxx"
#include <rtl/ustrbuf.hxx>
#include <stdarg.h>
#include <com/sun/star/ui/XUIConfigurationPersistence.hpp>
#include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XSingleComponentFactory.hpp>
#include <com/sun/star/lang/XMultiComponentFactory.hpp>
#include <com/sun/star/ui/XImageManager.hpp>
#include <com/sun/star/ui/ItemType.hpp>
#include <fstream>
#include <comphelper/processfactory.hxx>
#include <vcl/graph.hxx>
#include <map>
using namespace com::sun::star;
typedef std::map< sal_Int16, rtl::OUString > IdToString;
class MSOExcelCommandConvertor : public MSOCommandConvertor
{
IdToString msoToOOcmd;
IdToString tcidToOOcmd;
public:
MSOExcelCommandConvertor();
virtual rtl::OUString MSOCommandToOOCommand( sal_Int16 msoCmd );
virtual rtl::OUString MSOTCIDToOOCommand( sal_Int16 key );
};
MSOExcelCommandConvertor::MSOExcelCommandConvertor()
{
/*
// mso command id to ooo command string
// #FIXME and *HUNDREDS* of id's to added here
msoToOOcmd[ 0x20b ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(".uno:CloseDoc") );
msoToOOcmd[ 0x50 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(".uno:Open") );
// mso tcid to ooo command string
// #FIXME and *HUNDREDS* of id's to added here
tcidToOOcmd[ 0x9d9 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(".uno:Print") );
*/
}
rtl::OUString MSOExcelCommandConvertor::MSOCommandToOOCommand( sal_Int16 key )
{
rtl::OUString sResult;
IdToString::iterator it = msoToOOcmd.find( key );
if ( it != msoToOOcmd.end() )
sResult = it->second;
return sResult;
}
rtl::OUString MSOExcelCommandConvertor::MSOTCIDToOOCommand( sal_Int16 key )
{
rtl::OUString sResult;
IdToString::iterator it = tcidToOOcmd.find( key );
if ( it != tcidToOOcmd.end() )
sResult = it->second;
return sResult;
}
CTBS::CTBS() : bSignature(0), bVersion(0), reserved1(0), reserved2(0), reserved3(0), ctb(0), ctbViews(0), ictbView(0)
{
}
CTB::CTB() : nViews( 0 ), ectbid(0)
{
}
CTB::CTB(sal_uInt16 nNum ) : nViews( nNum ), ectbid(0)
{
}
bool CTB::Read( SvStream &rS )
{
OSL_TRACE("CTB::Read() stream pos 0x%x", rS.Tell() );
nOffSet = rS.Tell();
tb.Read( rS );
for ( sal_uInt16 index = 0; index < nViews; ++index )
{
TBVisualData aVisData;
aVisData.Read( rS );
rVisualData.push_back( aVisData );
}
rS >> ectbid;
for ( sal_Int16 index = 0; index < tb.getcCL(); ++index )
{
TBC aTBC;
aTBC.Read( rS );
rTBC.push_back( aTBC );
}
return true;
}
void CTB::Print( FILE* fp )
{
Indent a;
indent_printf( fp, "[ 0x%x ] CTB -- dump\n", nOffSet );
indent_printf( fp, " nViews 0x%x\n", nViews);
tb.Print( fp );
std::vector<TBVisualData>::iterator visData_end = rVisualData.end();
sal_Int32 counter = 0;
for ( std::vector<TBVisualData>::iterator it = rVisualData.begin(); it != visData_end; ++it )
{
indent_printf( fp, " TBVisualData [%d]\n", counter++ );
Indent b;
it->Print( fp );
}
indent_printf( fp, " ectbid 0x%x\n", ectbid);
std::vector<TBC>::iterator it_end = rTBC.end();
counter = 0;
for ( std::vector<TBC>::iterator it = rTBC.begin(); it != it_end; ++it )
{
indent_printf( fp, " TBC [%d]\n", counter++);
Indent c;
it->Print( fp );
}
}
bool CTB::IsMenuToolbar()
{
return tb.IsMenuToolbar();
}
bool CTB::ImportMenuTB( CTBWrapper& rWrapper, const css::uno::Reference< css::container::XIndexContainer >& xMenuDesc, CustomToolBarImportHelper& helper )
{
sal_Int32 index = 0;
for ( std::vector< TBC >::iterator it = rTBC.begin(); it != rTBC.end(); ++it, ++index )
{
if ( !it->ImportToolBarControl( rWrapper, xMenuDesc, helper, IsMenuToolbar() ) )
return false;
}
return true;
}
bool CTB::ImportCustomToolBar( CTBWrapper& rWrapper, CustomToolBarImportHelper& helper )
{
static rtl::OUString sToolbarPrefix( RTL_CONSTASCII_USTRINGPARAM( "private:resource/toolbar/custom_" ) );
bool bRes = false;
try
{
if ( !tb.IsEnabled() )
return true; // didn't fail, just ignoring
// Create default setting
uno::Reference< container::XIndexContainer > xIndexContainer( helper.getCfgManager()->createSettings(), uno::UNO_QUERY_THROW );
uno::Reference< container::XIndexAccess > xIndexAccess( xIndexContainer, uno::UNO_QUERY_THROW );
uno::Reference< beans::XPropertySet > xProps( xIndexContainer, uno::UNO_QUERY_THROW );
WString& name = tb.getName();
// set UI name for toolbar
xProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("UIName") ), uno::makeAny( name.getString() ) );
rtl::OUString sToolBarName = sToolbarPrefix.concat( name.getString() );
for ( std::vector< TBC >::iterator it = rTBC.begin(); it != rTBC.end(); ++it )
{
if ( !it->ImportToolBarControl( rWrapper, xIndexContainer, helper, IsMenuToolbar() ) )
return false;
}
OSL_TRACE("Name of toolbar :-/ %s", rtl::OUStringToOString( sToolBarName, RTL_TEXTENCODING_UTF8 ).getStr() );
helper.getCfgManager()->insertSettings( sToolBarName, xIndexAccess );
helper.applyIcons();
uno::Reference< ui::XUIConfigurationPersistence > xPersistence( helper.getCfgManager()->getImageManager(), uno::UNO_QUERY_THROW );
xPersistence->store();
xPersistence.set( helper.getCfgManager(), uno::UNO_QUERY_THROW );
xPersistence->store();
bRes = true;
}
catch( uno::Exception& )
{
bRes = false;
}
return bRes;
}
bool CTBS::Read( SvStream &rS )
{
OSL_TRACE("CTBS::Read() stream pos 0x%x", rS.Tell() );
nOffSet = rS.Tell();
rS >> bSignature >> bVersion >> reserved1 >> reserved2 >> reserved3 >> ctb >> ctbViews >> ictbView;
return true;
}
void CTBS::Print( FILE* fp )
{
Indent a;
indent_printf( fp, "[ 0x%x ] CTBS -- dump\n", nOffSet );
indent_printf( fp, " bSignature 0x%x\n", bSignature);
indent_printf( fp, " bVersion 0x%x\n", bVersion);
indent_printf( fp, " reserved1 0x%x\n", reserved1 );
indent_printf( fp, " reserved2 0x%x\n", reserved2 );
indent_printf( fp, " reserved3 0x%x\n", reserved3 );
indent_printf( fp, " ctb 0x%x\n", ctb );
indent_printf( fp, " ctbViews 0x%x\n", ctbViews );
indent_printf( fp, " ictbView 0x%x\n", ictbView );
}
TBC::TBC()
{
}
bool
TBC::Read(SvStream &rS)
{
OSL_TRACE("TBC::Read() stream pos 0x%x", rS.Tell() );
nOffSet = rS.Tell();
if ( !tbch.Read( rS ) )
return false;
sal_uInt16 tcid = tbch.getTcID();
sal_uInt8 tct = tbch.getTct();
if ( ( tcid != 0x0001 && tcid != 0x06CC && tcid != 0x03D8 && tcid != 0x03EC && tcid != 0x1051 ) && ( ( tct > 0 && tct < 0x0B ) || ( ( tct > 0x0B && tct < 0x10 ) || tct == 0x15 ) ) )
{
tbcCmd.reset( new TBCCmd );
if ( ! tbcCmd->Read( rS ) )
return false;
}
if ( tct != 0x16 )
{
tbcd.reset( new TBCData( tbch ) );
if ( !tbcd->Read( rS ) )
return false;
}
return true;
}
void
TBC::Print(FILE* fp)
{
Indent a;
indent_printf( fp, "[ 0x%x ] TBC -- dump\n", nOffSet );
tbch.Print( fp );
if ( tbcCmd.get() )
tbcCmd->Print( fp );
if ( tbcd.get() )
tbcd->Print( fp );
}
bool TBC::ImportToolBarControl( CTBWrapper& rWrapper, const css::uno::Reference< css::container::XIndexContainer >& toolbarcontainer, CustomToolBarImportHelper& helper, bool bIsMenuToolbar )
{
// how to identify built-in-command ?
// bool bBuiltin = false;
if ( tbcd.get() )
{
std::vector< css::beans::PropertyValue > props;
bool bBeginGroup = false;
if ( ! tbcd->ImportToolBarControl( helper, props, bBeginGroup, bIsMenuToolbar ) )
return false;
TBCMenuSpecific* pMenu = tbcd->getMenuSpecific();
if ( pMenu )
{
// search for CTB with the appropriate name ( it contains the
// menu items, although we cannot import ( or create ) a menu on
// a custom toolbar we can import the menu items in a separate
// toolbar ( better than nothing )
CTB* pCustTB = rWrapper.GetCustomizationData( pMenu->Name() );
if ( pCustTB )
{
uno::Reference< container::XIndexContainer > xMenuDesc;
uno::Reference< lang::XMultiServiceFactory > xMSF( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
xMenuDesc.set( xMSF->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.IndexedPropertyValues" ) ) ), uno::UNO_QUERY_THROW );
if ( !pCustTB->ImportMenuTB( rWrapper, xMenuDesc, helper ) )
return false;
if ( !bIsMenuToolbar )
{
if ( !helper.createMenu( pMenu->Name(), uno::Reference< container::XIndexAccess >( xMenuDesc, uno::UNO_QUERY ), true ) )
return false;
}
else
{
beans::PropertyValue aProp;
aProp.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ItemDescriptorContainer") );
aProp.Value <<= xMenuDesc;
props.push_back( aProp );
}
}
}
if ( bBeginGroup )
{
// insert spacer
uno::Sequence< beans::PropertyValue > sProps( 1 );
sProps[ 0 ].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Type") );
sProps[ 0 ].Value = uno::makeAny( ui::ItemType::SEPARATOR_LINE );
toolbarcontainer->insertByIndex( toolbarcontainer->getCount(), uno::makeAny( sProps ) );
}
uno::Sequence< beans::PropertyValue > sProps( props.size() );
beans::PropertyValue* pProp = sProps.getArray();
for ( std::vector< css::beans::PropertyValue >::iterator it = props.begin(); it != props.end(); ++it, ++pProp )
*pProp = *it;
toolbarcontainer->insertByIndex( toolbarcontainer->getCount(), uno::makeAny( sProps ) );
}
return true;
}
void
TBCCmd::Print(FILE* fp)
{
Indent a;
indent_printf( fp, " TBCCmd -- dump\n" );
indent_printf( fp, " cmdID 0x%x\n", cmdID );
indent_printf( fp, " A ( fHideDrawing ) %s\n", A ? "true" : "false" );
indent_printf( fp, " B ( reserved - ignored ) %s\n", A ? "true" : "false" );
indent_printf( fp, " cmdType 0x%x\n", cmdType );
indent_printf( fp, " C ( reserved - ignored ) %s\n", A ? "true" : "false" );
indent_printf( fp, " reserved3 0x%x\n", reserved3 );
}
bool TBCCmd::Read( SvStream &rS )
{
OSL_TRACE("TBCCmd::Read() stream pos 0x%x", rS.Tell() );
nOffSet = rS.Tell();
rS >> cmdID;
sal_uInt16 temp;
rS >> temp;
OSL_TRACE("TBCmd temp = 0x%x", temp );
A = (temp & 0x8000 ) == 0x8000;
B = (temp & 0x4000) == 0x4000;
cmdType = ( temp & 0x3E00 ) >> 9;
C = ( temp & 0x100 ) == 0x100;
reserved3 = ( temp & 0xFF );
return true;
}
CTBWrapper::CTBWrapper()
{
}
CTBWrapper::~CTBWrapper()
{
}
bool
CTBWrapper::Read( SvStream &rS)
{
OSL_TRACE("CTBWrapper::Read() stream pos 0x%x", rS.Tell() );
nOffSet = rS.Tell();
if ( !ctbSet.Read( rS ) )
return false;
for ( sal_uInt16 index = 0; index < ctbSet.ctb; ++index )
{
CTB aCTB( ctbSet.ctbViews );
if ( !aCTB.Read( rS ) )
return false;
rCTB.push_back( aCTB );
}
return true;
}
void
CTBWrapper::Print( FILE* fp )
{
Indent a;
indent_printf( fp, "[ 0x%x ] CTBWrapper -- dump\n", nOffSet );
ctbSet.Print( fp );
std::vector<CTB>::iterator it_end = rCTB.end();
for ( std::vector<CTB>::iterator it = rCTB.begin(); it != it_end; ++it )
{
Indent b;
it->Print( fp );
}
}
CTB* CTBWrapper::GetCustomizationData( const rtl::OUString& sTBName )
{
CTB* pCTB = NULL;
for ( std::vector< CTB >::iterator it = rCTB.begin(); it != rCTB.end(); ++it )
{
if ( it->GetName().equals( sTBName ) )
{
pCTB = &(*it);
break;
}
}
return pCTB;
}
bool CTBWrapper::ImportCustomToolBar( SfxObjectShell& rDocSh )
{
std::vector<CTB>::iterator it_end = rCTB.end();
for ( std::vector<CTB>::iterator it = rCTB.begin(); it != it_end; ++it )
{
// for each customtoolbar
uno::Reference< lang::XMultiServiceFactory > xMSF( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
uno::Reference< ui::XModuleUIConfigurationManagerSupplier > xAppCfgSupp( xMSF->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.ModuleUIConfigurationManagerSupplier" ) ) ), uno::UNO_QUERY_THROW );
CustomToolBarImportHelper helper( rDocSh, xAppCfgSupp->getUIConfigurationManager( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sheet.SpreadsheetDocument" ) ) ) );
helper.setMSOCommandMap( new MSOExcelCommandConvertor() );
// Ignore menu toolbars, excel doesn't ( afaics ) store
// menu customizations ( but you can have menus in a customtoolbar
// such menus will be dealt with when they are encountered
// as part of importing the appropriate MenuSpecific toolbar control )
if ( !(*it).IsMenuToolbar() )
{
if ( !(*it).ImportCustomToolBar( *this, helper ) )
return false;
}
}
return true;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|