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 : #include "xltoolbar.hxx"
10 : #include <rtl/ustrbuf.hxx>
11 : #include <stdarg.h>
12 : #include <com/sun/star/document/IndexedPropertyValues.hpp>
13 : #include <com/sun/star/ui/XUIConfigurationPersistence.hpp>
14 : #include <com/sun/star/ui/ModuleUIConfigurationManagerSupplier.hpp>
15 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
16 : #include <com/sun/star/lang/XSingleComponentFactory.hpp>
17 : #include <com/sun/star/lang/XMultiComponentFactory.hpp>
18 : #include <com/sun/star/ui/XImageManager.hpp>
19 : #include <com/sun/star/ui/ItemType.hpp>
20 : #include <fstream>
21 : #include <comphelper/processfactory.hxx>
22 : #include <vcl/graph.hxx>
23 : #include <map>
24 : using namespace com::sun::star;
25 :
26 : typedef std::map< sal_Int16, OUString > IdToString;
27 :
28 0 : class MSOExcelCommandConvertor : public MSOCommandConvertor
29 : {
30 : IdToString msoToOOcmd;
31 : IdToString tcidToOOcmd;
32 : public:
33 : MSOExcelCommandConvertor();
34 : virtual OUString MSOCommandToOOCommand( sal_Int16 msoCmd );
35 : virtual OUString MSOTCIDToOOCommand( sal_Int16 key );
36 : };
37 :
38 0 : MSOExcelCommandConvertor::MSOExcelCommandConvertor()
39 : {
40 : /*
41 : // mso command id to ooo command string
42 : // #FIXME and *HUNDREDS* of id's to added here
43 : msoToOOcmd[ 0x20b ] = OUString(".uno:CloseDoc");
44 : msoToOOcmd[ 0x50 ] = OUString(".uno:Open");
45 :
46 : // mso tcid to ooo command string
47 : // #FIXME and *HUNDREDS* of id's to added here
48 : tcidToOOcmd[ 0x9d9 ] = OUString(".uno:Print");
49 : */
50 0 : }
51 :
52 0 : OUString MSOExcelCommandConvertor::MSOCommandToOOCommand( sal_Int16 key )
53 : {
54 0 : OUString sResult;
55 0 : IdToString::iterator it = msoToOOcmd.find( key );
56 0 : if ( it != msoToOOcmd.end() )
57 0 : sResult = it->second;
58 0 : return sResult;
59 : }
60 :
61 0 : OUString MSOExcelCommandConvertor::MSOTCIDToOOCommand( sal_Int16 key )
62 : {
63 0 : OUString sResult;
64 0 : IdToString::iterator it = tcidToOOcmd.find( key );
65 0 : if ( it != tcidToOOcmd.end() )
66 0 : sResult = it->second;
67 0 : return sResult;
68 : }
69 :
70 :
71 :
72 27 : CTBS::CTBS() : bSignature(0), bVersion(0), reserved1(0), reserved2(0), reserved3(0), ctb(0), ctbViews(0), ictbView(0)
73 : {
74 27 : }
75 :
76 0 : ScCTB::ScCTB(sal_uInt16 nNum ) : nViews( nNum ), ectbid(0)
77 : {
78 0 : }
79 :
80 0 : bool ScCTB::Read( SvStream &rS )
81 : {
82 : OSL_TRACE("ScCTB::Read() stream pos 0x%x", rS.Tell() );
83 0 : nOffSet = rS.Tell();
84 0 : tb.Read( rS );
85 0 : for ( sal_uInt16 index = 0; index < nViews; ++index )
86 : {
87 0 : TBVisualData aVisData;
88 0 : aVisData.Read( rS );
89 0 : rVisualData.push_back( aVisData );
90 0 : }
91 0 : rS >> ectbid;
92 :
93 0 : for ( sal_Int16 index = 0; index < tb.getcCL(); ++index )
94 : {
95 0 : ScTBC aTBC;
96 0 : aTBC.Read( rS );
97 0 : rTBC.push_back( aTBC );
98 0 : }
99 0 : return true;
100 : }
101 :
102 0 : void ScCTB::Print( FILE* fp )
103 : {
104 0 : Indent a;
105 0 : indent_printf( fp, "[ 0x%x ] ScCTB -- dump\n", nOffSet );
106 0 : indent_printf( fp, " nViews 0x%x\n", nViews);
107 0 : tb.Print( fp );
108 :
109 0 : std::vector<TBVisualData>::iterator visData_end = rVisualData.end();
110 0 : sal_Int32 counter = 0;
111 0 : for ( std::vector<TBVisualData>::iterator it = rVisualData.begin(); it != visData_end; ++it )
112 : {
113 :
114 0 : indent_printf( fp, " TBVisualData [%d]\n", counter++ );
115 0 : Indent b;
116 0 : it->Print( fp );
117 0 : }
118 0 : indent_printf( fp, " ectbid 0x%x\n", ectbid);
119 0 : std::vector<ScTBC>::iterator it_end = rTBC.end();
120 0 : counter = 0;
121 0 : for ( std::vector<ScTBC>::iterator it = rTBC.begin(); it != it_end; ++it )
122 : {
123 0 : indent_printf( fp, " ScTBC [%d]\n", counter++);
124 0 : Indent c;
125 0 : it->Print( fp );
126 0 : }
127 0 : }
128 :
129 0 : bool ScCTB::IsMenuToolbar()
130 : {
131 0 : return tb.IsMenuToolbar();
132 : }
133 :
134 0 : bool ScCTB::ImportMenuTB( ScCTBWrapper& rWrapper, const css::uno::Reference< css::container::XIndexContainer >& xMenuDesc, CustomToolBarImportHelper& helper )
135 : {
136 0 : sal_Int32 index = 0;
137 0 : for ( std::vector< ScTBC >::iterator it = rTBC.begin(); it != rTBC.end(); ++it, ++index )
138 : {
139 0 : if ( !it->ImportToolBarControl( rWrapper, xMenuDesc, helper, IsMenuToolbar() ) )
140 0 : return false;
141 : }
142 0 : return true;
143 : }
144 :
145 0 : bool ScCTB::ImportCustomToolBar( ScCTBWrapper& rWrapper, CustomToolBarImportHelper& helper )
146 : {
147 :
148 0 : static OUString sToolbarPrefix( "private:resource/toolbar/custom_" );
149 0 : bool bRes = false;
150 : try
151 : {
152 0 : if ( !tb.IsEnabled() )
153 0 : return true; // didn't fail, just ignoring
154 :
155 : // Create default setting
156 0 : uno::Reference< container::XIndexContainer > xIndexContainer( helper.getCfgManager()->createSettings(), uno::UNO_QUERY_THROW );
157 0 : uno::Reference< container::XIndexAccess > xIndexAccess( xIndexContainer, uno::UNO_QUERY_THROW );
158 0 : uno::Reference< beans::XPropertySet > xProps( xIndexContainer, uno::UNO_QUERY_THROW );
159 0 : WString& name = tb.getName();
160 : // set UI name for toolbar
161 0 : xProps->setPropertyValue("UIName", uno::makeAny( name.getString() ) );
162 :
163 0 : OUString sToolBarName = sToolbarPrefix.concat( name.getString() );
164 0 : for ( std::vector< ScTBC >::iterator it = rTBC.begin(); it != rTBC.end(); ++it )
165 : {
166 0 : if ( !it->ImportToolBarControl( rWrapper, xIndexContainer, helper, IsMenuToolbar() ) )
167 0 : return false;
168 : }
169 :
170 : OSL_TRACE("Name of toolbar :-/ %s", OUStringToOString( sToolBarName, RTL_TEXTENCODING_UTF8 ).getStr() );
171 :
172 0 : helper.getCfgManager()->insertSettings( sToolBarName, xIndexAccess );
173 0 : helper.applyIcons();
174 :
175 0 : uno::Reference< ui::XUIConfigurationPersistence > xPersistence( helper.getCfgManager()->getImageManager(), uno::UNO_QUERY_THROW );
176 0 : xPersistence->store();
177 :
178 0 : xPersistence.set( helper.getCfgManager(), uno::UNO_QUERY_THROW );
179 0 : xPersistence->store();
180 :
181 0 : bRes = true;
182 : }
183 0 : catch( uno::Exception& )
184 : {
185 0 : bRes = false;
186 : }
187 0 : return bRes;
188 : }
189 27 : bool CTBS::Read( SvStream &rS )
190 : {
191 : OSL_TRACE("CTBS::Read() stream pos 0x%x", rS.Tell() );
192 27 : nOffSet = rS.Tell();
193 27 : rS >> bSignature >> bVersion >> reserved1 >> reserved2 >> reserved3 >> ctb >> ctbViews >> ictbView;
194 27 : return true;
195 : }
196 :
197 0 : void CTBS::Print( FILE* fp )
198 : {
199 0 : Indent a;
200 0 : indent_printf( fp, "[ 0x%x ] CTBS -- dump\n", nOffSet );
201 :
202 0 : indent_printf( fp, " bSignature 0x%x\n", bSignature);
203 0 : indent_printf( fp, " bVersion 0x%x\n", bVersion);
204 :
205 0 : indent_printf( fp, " reserved1 0x%x\n", reserved1 );
206 0 : indent_printf( fp, " reserved2 0x%x\n", reserved2 );
207 0 : indent_printf( fp, " reserved3 0x%x\n", reserved3 );
208 :
209 0 : indent_printf( fp, " ctb 0x%x\n", ctb );
210 0 : indent_printf( fp, " ctbViews 0x%x\n", ctbViews );
211 0 : indent_printf( fp, " ictbView 0x%x\n", ictbView );
212 0 : }
213 :
214 0 : ScTBC::ScTBC()
215 : {
216 0 : }
217 :
218 : bool
219 0 : ScTBC::Read(SvStream &rS)
220 : {
221 : OSL_TRACE("ScTBC::Read() stream pos 0x%x", rS.Tell() );
222 0 : nOffSet = rS.Tell();
223 0 : if ( !tbch.Read( rS ) )
224 0 : return false;
225 0 : sal_uInt16 tcid = tbch.getTcID();
226 0 : sal_uInt8 tct = tbch.getTct();
227 0 : if ( ( tcid != 0x0001 && tcid != 0x06CC && tcid != 0x03D8 && tcid != 0x03EC && tcid != 0x1051 ) && ( ( tct > 0 && tct < 0x0B ) || ( ( tct > 0x0B && tct < 0x10 ) || tct == 0x15 ) ) )
228 : {
229 0 : tbcCmd.reset( new TBCCmd );
230 0 : if ( ! tbcCmd->Read( rS ) )
231 0 : return false;
232 : }
233 0 : if ( tct != 0x16 )
234 : {
235 0 : tbcd.reset( new TBCData( tbch ) );
236 0 : if ( !tbcd->Read( rS ) )
237 0 : return false;
238 : }
239 0 : return true;
240 : }
241 :
242 :
243 : void
244 0 : ScTBC::Print(FILE* fp)
245 : {
246 0 : Indent a;
247 0 : indent_printf( fp, "[ 0x%x ] ScTBC -- dump\n", nOffSet );
248 0 : tbch.Print( fp );
249 0 : if ( tbcCmd.get() )
250 0 : tbcCmd->Print( fp );
251 0 : if ( tbcd.get() )
252 0 : tbcd->Print( fp );
253 0 : }
254 :
255 0 : bool ScTBC::ImportToolBarControl( ScCTBWrapper& rWrapper, const css::uno::Reference< css::container::XIndexContainer >& toolbarcontainer, CustomToolBarImportHelper& helper, bool bIsMenuToolbar )
256 : {
257 : // how to identify built-in-command ?
258 : // bool bBuiltin = false;
259 0 : if ( tbcd.get() )
260 : {
261 0 : std::vector< css::beans::PropertyValue > props;
262 0 : bool bBeginGroup = false;
263 0 : if ( ! tbcd->ImportToolBarControl( helper, props, bBeginGroup, bIsMenuToolbar ) )
264 0 : return false;
265 0 : TBCMenuSpecific* pMenu = tbcd->getMenuSpecific();
266 0 : if ( pMenu )
267 : {
268 : // search for ScCTB with the appropriate name ( it contains the
269 : // menu items, although we cannot import ( or create ) a menu on
270 : // a custom toolbar we can import the menu items in a separate
271 : // toolbar ( better than nothing )
272 0 : ScCTB* pCustTB = rWrapper.GetCustomizationData( pMenu->Name() );
273 0 : if ( pCustTB )
274 : {
275 0 : uno::Reference< container::XIndexContainer > xMenuDesc = document::IndexedPropertyValues::create( comphelper::getProcessComponentContext() );
276 0 : if ( !pCustTB->ImportMenuTB( rWrapper, xMenuDesc, helper ) )
277 0 : return false;
278 0 : if ( !bIsMenuToolbar )
279 : {
280 0 : if ( !helper.createMenu( pMenu->Name(), uno::Reference< container::XIndexAccess >( xMenuDesc, uno::UNO_QUERY ), true ) )
281 0 : return false;
282 : }
283 : else
284 : {
285 0 : beans::PropertyValue aProp;
286 0 : aProp.Name = OUString("ItemDescriptorContainer");
287 0 : aProp.Value <<= xMenuDesc;
288 0 : props.push_back( aProp );
289 0 : }
290 : }
291 : }
292 :
293 0 : if ( bBeginGroup )
294 : {
295 : // insert spacer
296 0 : uno::Sequence< beans::PropertyValue > sProps( 1 );
297 0 : sProps[ 0 ].Name = OUString("Type");
298 0 : sProps[ 0 ].Value = uno::makeAny( ui::ItemType::SEPARATOR_LINE );
299 0 : toolbarcontainer->insertByIndex( toolbarcontainer->getCount(), uno::makeAny( sProps ) );
300 : }
301 0 : uno::Sequence< beans::PropertyValue > sProps( props.size() );
302 0 : beans::PropertyValue* pProp = sProps.getArray();
303 :
304 0 : for ( std::vector< css::beans::PropertyValue >::iterator it = props.begin(); it != props.end(); ++it, ++pProp )
305 0 : *pProp = *it;
306 :
307 0 : toolbarcontainer->insertByIndex( toolbarcontainer->getCount(), uno::makeAny( sProps ) );
308 : }
309 0 : return true;
310 : }
311 :
312 : void
313 0 : TBCCmd::Print(FILE* fp)
314 : {
315 0 : Indent a;
316 0 : indent_printf( fp, " TBCCmd -- dump\n" );
317 0 : indent_printf( fp, " cmdID 0x%x\n", cmdID );
318 0 : indent_printf( fp, " A ( fHideDrawing ) %s\n", A ? "true" : "false" );
319 0 : indent_printf( fp, " B ( reserved - ignored ) %s\n", A ? "true" : "false" );
320 0 : indent_printf( fp, " cmdType 0x%x\n", cmdType );
321 0 : indent_printf( fp, " C ( reserved - ignored ) %s\n", A ? "true" : "false" );
322 0 : indent_printf( fp, " reserved3 0x%x\n", reserved3 );
323 0 : }
324 :
325 0 : bool TBCCmd::Read( SvStream &rS )
326 : {
327 : OSL_TRACE("TBCCmd::Read() stream pos 0x%x", rS.Tell() );
328 0 : nOffSet = rS.Tell();
329 0 : rS >> cmdID;
330 : sal_uInt16 temp;
331 0 : rS >> temp;
332 : OSL_TRACE("TBCmd temp = 0x%x", temp );
333 0 : A = (temp & 0x8000 ) == 0x8000;
334 0 : B = (temp & 0x4000) == 0x4000;
335 0 : cmdType = ( temp & 0x3E00 ) >> 9;
336 0 : C = ( temp & 0x100 ) == 0x100;
337 0 : reserved3 = ( temp & 0xFF );
338 0 : return true;
339 : }
340 :
341 27 : ScCTBWrapper::ScCTBWrapper()
342 : {
343 27 : }
344 :
345 27 : ScCTBWrapper::~ScCTBWrapper()
346 : {
347 27 : }
348 :
349 : bool
350 27 : ScCTBWrapper::Read( SvStream &rS)
351 : {
352 : OSL_TRACE("ScCTBWrapper::Read() stream pos 0x%x", rS.Tell() );
353 27 : nOffSet = rS.Tell();
354 27 : if ( !ctbSet.Read( rS ) )
355 0 : return false;
356 27 : for ( sal_uInt16 index = 0; index < ctbSet.ctb; ++index )
357 : {
358 0 : ScCTB aCTB( ctbSet.ctbViews );
359 0 : if ( !aCTB.Read( rS ) )
360 0 : return false;
361 0 : rCTB.push_back( aCTB );
362 0 : }
363 27 : return true;
364 : }
365 :
366 : void
367 0 : ScCTBWrapper::Print( FILE* fp )
368 : {
369 0 : Indent a;
370 0 : indent_printf( fp, "[ 0x%x ] ScCTBWrapper -- dump\n", nOffSet );
371 0 : ctbSet.Print( fp );
372 0 : std::vector<ScCTB>::iterator it_end = rCTB.end();
373 0 : for ( std::vector<ScCTB>::iterator it = rCTB.begin(); it != it_end; ++it )
374 : {
375 0 : Indent b;
376 0 : it->Print( fp );
377 0 : }
378 0 : }
379 :
380 0 : ScCTB* ScCTBWrapper::GetCustomizationData( const OUString& sTBName )
381 : {
382 0 : ScCTB* pCTB = NULL;
383 0 : for ( std::vector< ScCTB >::iterator it = rCTB.begin(); it != rCTB.end(); ++it )
384 : {
385 0 : if ( it->GetName().equals( sTBName ) )
386 : {
387 0 : pCTB = &(*it);
388 0 : break;
389 : }
390 : }
391 0 : return pCTB;
392 : }
393 :
394 27 : bool ScCTBWrapper::ImportCustomToolBar( SfxObjectShell& rDocSh )
395 : {
396 27 : if(rCTB.empty())
397 27 : return true;
398 :
399 0 : uno::Reference< uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
400 0 : uno::Reference< ui::XModuleUIConfigurationManagerSupplier > xAppCfgSupp( ui::ModuleUIConfigurationManagerSupplier::create(xContext) );
401 :
402 0 : std::vector<ScCTB>::iterator it_end = rCTB.end();
403 0 : for ( std::vector<ScCTB>::iterator it = rCTB.begin(); it != it_end; ++it )
404 : {
405 : // for each customtoolbar
406 0 : CustomToolBarImportHelper helper( rDocSh, xAppCfgSupp->getUIConfigurationManager( OUString("com.sun.star.sheet.SpreadsheetDocument" ) ) );
407 0 : helper.setMSOCommandMap( new MSOExcelCommandConvertor() );
408 : // Ignore menu toolbars, excel doesn't ( afaics ) store
409 : // menu customizations ( but you can have menus in a customtoolbar
410 : // such menus will be dealt with when they are encountered
411 : // as part of importing the appropriate MenuSpecific toolbar control )
412 :
413 :
414 0 : if ( !(*it).IsMenuToolbar() )
415 : {
416 0 : if ( !(*it).ImportCustomToolBar( *this, helper ) )
417 0 : return false;
418 : }
419 0 : }
420 0 : return true;
421 15 : }
422 :
423 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|