bump product version to 4.1.6.2
[LibreOffice.git] / sc / source / filter / excel / xltoolbar.cxx
blobd078272470d71a522e4a8a611552a53ab27d1525
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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;
26 typedef std::map< sal_Int16, OUString > IdToString;
28 class MSOExcelCommandConvertor : public MSOCommandConvertor
30 IdToString msoToOOcmd;
31 IdToString tcidToOOcmd;
32 public:
33 MSOExcelCommandConvertor();
34 virtual OUString MSOCommandToOOCommand( sal_Int16 msoCmd );
35 virtual OUString MSOTCIDToOOCommand( sal_Int16 key );
38 MSOExcelCommandConvertor::MSOExcelCommandConvertor()
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");
46 // mso tcid to ooo command string
47 // #FIXME and *HUNDREDS* of id's to added here
48 tcidToOOcmd[ 0x9d9 ] = OUString(".uno:Print");
52 OUString MSOExcelCommandConvertor::MSOCommandToOOCommand( sal_Int16 key )
54 OUString sResult;
55 IdToString::iterator it = msoToOOcmd.find( key );
56 if ( it != msoToOOcmd.end() )
57 sResult = it->second;
58 return sResult;
61 OUString MSOExcelCommandConvertor::MSOTCIDToOOCommand( sal_Int16 key )
63 OUString sResult;
64 IdToString::iterator it = tcidToOOcmd.find( key );
65 if ( it != tcidToOOcmd.end() )
66 sResult = it->second;
67 return sResult;
72 CTBS::CTBS() : bSignature(0), bVersion(0), reserved1(0), reserved2(0), reserved3(0), ctb(0), ctbViews(0), ictbView(0)
76 ScCTB::ScCTB(sal_uInt16 nNum ) : nViews( nNum ), ectbid(0)
80 bool ScCTB::Read( SvStream &rS )
82 OSL_TRACE("ScCTB::Read() stream pos 0x%x", rS.Tell() );
83 nOffSet = rS.Tell();
84 tb.Read( rS );
85 for ( sal_uInt16 index = 0; index < nViews; ++index )
87 TBVisualData aVisData;
88 aVisData.Read( rS );
89 rVisualData.push_back( aVisData );
91 rS >> ectbid;
93 for ( sal_Int16 index = 0; index < tb.getcCL(); ++index )
95 ScTBC aTBC;
96 aTBC.Read( rS );
97 rTBC.push_back( aTBC );
99 return true;
102 void ScCTB::Print( FILE* fp )
104 Indent a;
105 indent_printf( fp, "[ 0x%x ] ScCTB -- dump\n", nOffSet );
106 indent_printf( fp, " nViews 0x%x\n", nViews);
107 tb.Print( fp );
109 std::vector<TBVisualData>::iterator visData_end = rVisualData.end();
110 sal_Int32 counter = 0;
111 for ( std::vector<TBVisualData>::iterator it = rVisualData.begin(); it != visData_end; ++it )
114 indent_printf( fp, " TBVisualData [%d]\n", counter++ );
115 Indent b;
116 it->Print( fp );
118 indent_printf( fp, " ectbid 0x%x\n", ectbid);
119 std::vector<ScTBC>::iterator it_end = rTBC.end();
120 counter = 0;
121 for ( std::vector<ScTBC>::iterator it = rTBC.begin(); it != it_end; ++it )
123 indent_printf( fp, " ScTBC [%d]\n", counter++);
124 Indent c;
125 it->Print( fp );
129 bool ScCTB::IsMenuToolbar()
131 return tb.IsMenuToolbar();
134 bool ScCTB::ImportMenuTB( ScCTBWrapper& rWrapper, const css::uno::Reference< css::container::XIndexContainer >& xMenuDesc, CustomToolBarImportHelper& helper )
136 sal_Int32 index = 0;
137 for ( std::vector< ScTBC >::iterator it = rTBC.begin(); it != rTBC.end(); ++it, ++index )
139 if ( !it->ImportToolBarControl( rWrapper, xMenuDesc, helper, IsMenuToolbar() ) )
140 return false;
142 return true;
145 bool ScCTB::ImportCustomToolBar( ScCTBWrapper& rWrapper, CustomToolBarImportHelper& helper )
148 static OUString sToolbarPrefix( "private:resource/toolbar/custom_" );
149 bool bRes = false;
152 if ( !tb.IsEnabled() )
153 return true; // didn't fail, just ignoring
155 // Create default setting
156 uno::Reference< container::XIndexContainer > xIndexContainer( helper.getCfgManager()->createSettings(), uno::UNO_QUERY_THROW );
157 uno::Reference< container::XIndexAccess > xIndexAccess( xIndexContainer, uno::UNO_QUERY_THROW );
158 uno::Reference< beans::XPropertySet > xProps( xIndexContainer, uno::UNO_QUERY_THROW );
159 WString& name = tb.getName();
160 // set UI name for toolbar
161 xProps->setPropertyValue( OUString("UIName"), uno::makeAny( name.getString() ) );
163 OUString sToolBarName = sToolbarPrefix.concat( name.getString() );
164 for ( std::vector< ScTBC >::iterator it = rTBC.begin(); it != rTBC.end(); ++it )
166 if ( !it->ImportToolBarControl( rWrapper, xIndexContainer, helper, IsMenuToolbar() ) )
167 return false;
170 OSL_TRACE("Name of toolbar :-/ %s", OUStringToOString( sToolBarName, RTL_TEXTENCODING_UTF8 ).getStr() );
172 helper.getCfgManager()->insertSettings( sToolBarName, xIndexAccess );
173 helper.applyIcons();
175 uno::Reference< ui::XUIConfigurationPersistence > xPersistence( helper.getCfgManager()->getImageManager(), uno::UNO_QUERY_THROW );
176 xPersistence->store();
178 xPersistence.set( helper.getCfgManager(), uno::UNO_QUERY_THROW );
179 xPersistence->store();
181 bRes = true;
183 catch( uno::Exception& )
185 bRes = false;
187 return bRes;
189 bool CTBS::Read( SvStream &rS )
191 OSL_TRACE("CTBS::Read() stream pos 0x%x", rS.Tell() );
192 nOffSet = rS.Tell();
193 rS >> bSignature >> bVersion >> reserved1 >> reserved2 >> reserved3 >> ctb >> ctbViews >> ictbView;
194 return true;
197 void CTBS::Print( FILE* fp )
199 Indent a;
200 indent_printf( fp, "[ 0x%x ] CTBS -- dump\n", nOffSet );
202 indent_printf( fp, " bSignature 0x%x\n", bSignature);
203 indent_printf( fp, " bVersion 0x%x\n", bVersion);
205 indent_printf( fp, " reserved1 0x%x\n", reserved1 );
206 indent_printf( fp, " reserved2 0x%x\n", reserved2 );
207 indent_printf( fp, " reserved3 0x%x\n", reserved3 );
209 indent_printf( fp, " ctb 0x%x\n", ctb );
210 indent_printf( fp, " ctbViews 0x%x\n", ctbViews );
211 indent_printf( fp, " ictbView 0x%x\n", ictbView );
214 ScTBC::ScTBC()
218 bool
219 ScTBC::Read(SvStream &rS)
221 OSL_TRACE("ScTBC::Read() stream pos 0x%x", rS.Tell() );
222 nOffSet = rS.Tell();
223 if ( !tbch.Read( rS ) )
224 return false;
225 sal_uInt16 tcid = tbch.getTcID();
226 sal_uInt8 tct = tbch.getTct();
227 if ( ( tcid != 0x0001 && tcid != 0x06CC && tcid != 0x03D8 && tcid != 0x03EC && tcid != 0x1051 ) && ( ( tct > 0 && tct < 0x0B ) || ( ( tct > 0x0B && tct < 0x10 ) || tct == 0x15 ) ) )
229 tbcCmd.reset( new TBCCmd );
230 if ( ! tbcCmd->Read( rS ) )
231 return false;
233 if ( tct != 0x16 )
235 tbcd.reset( new TBCData( tbch ) );
236 if ( !tbcd->Read( rS ) )
237 return false;
239 return true;
243 void
244 ScTBC::Print(FILE* fp)
246 Indent a;
247 indent_printf( fp, "[ 0x%x ] ScTBC -- dump\n", nOffSet );
248 tbch.Print( fp );
249 if ( tbcCmd.get() )
250 tbcCmd->Print( fp );
251 if ( tbcd.get() )
252 tbcd->Print( fp );
255 bool ScTBC::ImportToolBarControl( ScCTBWrapper& rWrapper, const css::uno::Reference< css::container::XIndexContainer >& toolbarcontainer, CustomToolBarImportHelper& helper, bool bIsMenuToolbar )
257 // how to identify built-in-command ?
258 // bool bBuiltin = false;
259 if ( tbcd.get() )
261 std::vector< css::beans::PropertyValue > props;
262 bool bBeginGroup = false;
263 if ( ! tbcd->ImportToolBarControl( helper, props, bBeginGroup, bIsMenuToolbar ) )
264 return false;
265 TBCMenuSpecific* pMenu = tbcd->getMenuSpecific();
266 if ( pMenu )
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 ScCTB* pCustTB = rWrapper.GetCustomizationData( pMenu->Name() );
273 if ( pCustTB )
275 uno::Reference< container::XIndexContainer > xMenuDesc = document::IndexedPropertyValues::create( comphelper::getProcessComponentContext() );
276 if ( !pCustTB->ImportMenuTB( rWrapper, xMenuDesc, helper ) )
277 return false;
278 if ( !bIsMenuToolbar )
280 if ( !helper.createMenu( pMenu->Name(), uno::Reference< container::XIndexAccess >( xMenuDesc, uno::UNO_QUERY ), true ) )
281 return false;
283 else
285 beans::PropertyValue aProp;
286 aProp.Name = OUString("ItemDescriptorContainer");
287 aProp.Value <<= xMenuDesc;
288 props.push_back( aProp );
293 if ( bBeginGroup )
295 // insert spacer
296 uno::Sequence< beans::PropertyValue > sProps( 1 );
297 sProps[ 0 ].Name = OUString("Type");
298 sProps[ 0 ].Value = uno::makeAny( ui::ItemType::SEPARATOR_LINE );
299 toolbarcontainer->insertByIndex( toolbarcontainer->getCount(), uno::makeAny( sProps ) );
301 uno::Sequence< beans::PropertyValue > sProps( props.size() );
302 beans::PropertyValue* pProp = sProps.getArray();
304 for ( std::vector< css::beans::PropertyValue >::iterator it = props.begin(); it != props.end(); ++it, ++pProp )
305 *pProp = *it;
307 toolbarcontainer->insertByIndex( toolbarcontainer->getCount(), uno::makeAny( sProps ) );
309 return true;
312 void
313 TBCCmd::Print(FILE* fp)
315 Indent a;
316 indent_printf( fp, " TBCCmd -- dump\n" );
317 indent_printf( fp, " cmdID 0x%x\n", cmdID );
318 indent_printf( fp, " A ( fHideDrawing ) %s\n", A ? "true" : "false" );
319 indent_printf( fp, " B ( reserved - ignored ) %s\n", A ? "true" : "false" );
320 indent_printf( fp, " cmdType 0x%x\n", cmdType );
321 indent_printf( fp, " C ( reserved - ignored ) %s\n", A ? "true" : "false" );
322 indent_printf( fp, " reserved3 0x%x\n", reserved3 );
325 bool TBCCmd::Read( SvStream &rS )
327 OSL_TRACE("TBCCmd::Read() stream pos 0x%x", rS.Tell() );
328 nOffSet = rS.Tell();
329 rS >> cmdID;
330 sal_uInt16 temp;
331 rS >> temp;
332 OSL_TRACE("TBCmd temp = 0x%x", temp );
333 A = (temp & 0x8000 ) == 0x8000;
334 B = (temp & 0x4000) == 0x4000;
335 cmdType = ( temp & 0x3E00 ) >> 9;
336 C = ( temp & 0x100 ) == 0x100;
337 reserved3 = ( temp & 0xFF );
338 return true;
341 ScCTBWrapper::ScCTBWrapper()
345 ScCTBWrapper::~ScCTBWrapper()
349 bool
350 ScCTBWrapper::Read( SvStream &rS)
352 OSL_TRACE("ScCTBWrapper::Read() stream pos 0x%x", rS.Tell() );
353 nOffSet = rS.Tell();
354 if ( !ctbSet.Read( rS ) )
355 return false;
356 for ( sal_uInt16 index = 0; index < ctbSet.ctb; ++index )
358 ScCTB aCTB( ctbSet.ctbViews );
359 if ( !aCTB.Read( rS ) )
360 return false;
361 rCTB.push_back( aCTB );
363 return true;
366 void
367 ScCTBWrapper::Print( FILE* fp )
369 Indent a;
370 indent_printf( fp, "[ 0x%x ] ScCTBWrapper -- dump\n", nOffSet );
371 ctbSet.Print( fp );
372 std::vector<ScCTB>::iterator it_end = rCTB.end();
373 for ( std::vector<ScCTB>::iterator it = rCTB.begin(); it != it_end; ++it )
375 Indent b;
376 it->Print( fp );
380 ScCTB* ScCTBWrapper::GetCustomizationData( const OUString& sTBName )
382 ScCTB* pCTB = NULL;
383 for ( std::vector< ScCTB >::iterator it = rCTB.begin(); it != rCTB.end(); ++it )
385 if ( it->GetName().equals( sTBName ) )
387 pCTB = &(*it);
388 break;
391 return pCTB;
394 bool ScCTBWrapper::ImportCustomToolBar( SfxObjectShell& rDocSh )
396 if(rCTB.empty())
397 return true;
399 uno::Reference< uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
400 uno::Reference< ui::XModuleUIConfigurationManagerSupplier > xAppCfgSupp( ui::ModuleUIConfigurationManagerSupplier::create(xContext) );
402 std::vector<ScCTB>::iterator it_end = rCTB.end();
403 for ( std::vector<ScCTB>::iterator it = rCTB.begin(); it != it_end; ++it )
405 // for each customtoolbar
406 CustomToolBarImportHelper helper( rDocSh, xAppCfgSupp->getUIConfigurationManager( OUString("com.sun.star.sheet.SpreadsheetDocument" ) ) );
407 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 )
414 if ( !(*it).IsMenuToolbar() )
416 if ( !(*it).ImportCustomToolBar( *this, helper ) )
417 return false;
420 return true;
423 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */