merge the formfield patch from ooo-build
[ooovba.git] / sc / source / filter / excel / xltoolbar.cxx
blobb7eacb12529ee094fdd909b69e490dc2d159a1e4
1 #include "xltoolbar.hxx"
2 #include <rtl/ustrbuf.hxx>
3 #include <stdarg.h>
4 #include <com/sun/star/ui/XUIConfigurationPersistence.hpp>
5 #include <com/sun/star/ui/XUIConfigurationPersistence.hpp>
6 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
7 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
8 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
9 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
10 #include <com/sun/star/ui/XImageManager.hpp>
11 #include <com/sun/star/ui/ItemType.hpp>
12 #include <fstream>
13 #include <comphelper/processfactory.hxx>
14 #include <vcl/graph.hxx>
15 #include <map>
16 using namespace com::sun::star;
18 typedef std::map< sal_Int16, rtl::OUString > IdToString;
20 class MSOExcelCommandConvertor : public MSOCommandConvertor
22 IdToString msoToOOcmd;
23 IdToString tcidToOOcmd;
24 public:
25 MSOExcelCommandConvertor();
26 virtual rtl::OUString MSOCommandToOOCommand( sal_Int16 msoCmd );
27 virtual rtl::OUString MSOTCIDToOOCommand( sal_Int16 key );
30 MSOExcelCommandConvertor::MSOExcelCommandConvertor()
33 // mso command id to ooo command string
34 // #FIXME and *HUNDREDS* of id's to added here
35 msoToOOcmd[ 0x20b ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(".uno:CloseDoc") );
36 msoToOOcmd[ 0x50 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(".uno:Open") );
38 // mso tcid to ooo command string
39 // #FIXME and *HUNDREDS* of id's to added here
40 tcidToOOcmd[ 0x9d9 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(".uno:Print") );
44 rtl::OUString MSOExcelCommandConvertor::MSOCommandToOOCommand( sal_Int16 key )
46 rtl::OUString sResult;
47 IdToString::iterator it = msoToOOcmd.find( key );
48 if ( it != msoToOOcmd.end() )
49 sResult = it->second;
50 return sResult;
53 rtl::OUString MSOExcelCommandConvertor::MSOTCIDToOOCommand( sal_Int16 key )
55 rtl::OUString sResult;
56 IdToString::iterator it = tcidToOOcmd.find( key );
57 if ( it != tcidToOOcmd.end() )
58 sResult = it->second;
59 return sResult;
64 CTBS::CTBS() : bSignature(0), bVersion(0), reserved1(0), reserved2(0), reserved3(0), ctb(0), ctbViews(0), ictbView(0)
68 CTB::CTB() : nViews( 0 ), ectbid(0)
72 CTB::CTB(sal_uInt16 nNum ) : nViews( nNum ), ectbid(0)
76 bool CTB::Read( SvStream *pS )
78 OSL_TRACE("CTB::Read() stream pos 0x%x", pS->Tell() );
79 nOffSet = pS->Tell();
80 tb.Read( pS );
81 for ( sal_uInt16 index = 0; index < nViews; ++index )
83 TBVisualData aVisData;
84 aVisData.Read( pS );
85 rVisualData.push_back( aVisData );
87 *pS >> ectbid;
89 for ( sal_Int16 index = 0; index < tb.getcCL(); ++index )
91 TBC aTBC;
92 aTBC.Read( pS );
93 rTBC.push_back( aTBC );
95 return true;
98 void CTB::Print( FILE* fp )
100 Indent a;
101 indent_printf( fp, "[ 0x%x ] CTB -- dump\n", nOffSet );
102 indent_printf( fp, " nViews 0x%x\n", nViews);
103 tb.Print( fp );
105 std::vector<TBVisualData>::iterator visData_end = rVisualData.end();
106 sal_Int32 counter = 0;
107 for ( std::vector<TBVisualData>::iterator it = rVisualData.begin(); it != visData_end; ++it )
110 indent_printf( fp, " TBVisualData [%d]\n", counter++ );
111 Indent b;
112 it->Print( fp );
114 indent_printf( fp, " ectbid 0x%x\n", ectbid);
115 std::vector<TBC>::iterator it_end = rTBC.end();
116 counter = 0;
117 for ( std::vector<TBC>::iterator it = rTBC.begin(); it != it_end; ++it )
119 indent_printf( fp, " TBC [%d]\n", counter++);
120 Indent c;
121 it->Print( fp );
125 bool CTB::IsMenuToolbar()
127 return tb.IsMenuToolbar();
130 bool CTB::ImportMenuTB( CTBWrapper& rWrapper, const css::uno::Reference< css::container::XIndexContainer >& xMenuDesc, CustomToolBarImportHelper& helper )
132 sal_Int32 index = 0;
133 for ( std::vector< TBC >::iterator it = rTBC.begin(); it != rTBC.end(); ++it, ++index )
135 if ( !it->ImportToolBarControl( rWrapper, xMenuDesc, helper, IsMenuToolbar() ) )
136 return false;
138 return true;
141 bool CTB::ImportCustomToolBar( CTBWrapper& rWrapper, CustomToolBarImportHelper& helper )
144 static rtl::OUString sToolbarPrefix( RTL_CONSTASCII_USTRINGPARAM( "private:resource/toolbar/custom_" ) );
145 bool bRes = false;
148 if ( !tb.IsEnabled() )
149 return true; // didn't fail, just ignoring
151 // Create default setting
152 uno::Reference< container::XIndexContainer > xIndexContainer( helper.getCfgManager()->createSettings(), uno::UNO_QUERY_THROW );
153 uno::Reference< container::XIndexAccess > xIndexAccess( xIndexContainer, uno::UNO_QUERY_THROW );
154 uno::Reference< beans::XPropertySet > xProps( xIndexContainer, uno::UNO_QUERY_THROW );
155 WString& name = tb.getName();
156 // set UI name for toolbar
157 xProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("UIName") ), uno::makeAny( name.getString() ) );
159 rtl::OUString sToolBarName = sToolbarPrefix.concat( name.getString() );
160 for ( std::vector< TBC >::iterator it = rTBC.begin(); it != rTBC.end(); ++it )
162 if ( !it->ImportToolBarControl( rWrapper, xIndexContainer, helper, IsMenuToolbar() ) )
163 return false;
166 OSL_TRACE("Name of toolbar :-/ %s", rtl::OUStringToOString( sToolBarName, RTL_TEXTENCODING_UTF8 ).getStr() );
168 helper.getCfgManager()->insertSettings( sToolBarName, xIndexAccess );
169 helper.applyIcons();
170 #if 0
171 uno::Reference< ui::XUIConfigurationPersistence > xPersistence( helper.getCfgManager()->getImageManager(), uno::UNO_QUERY_THROW );
172 xPersistence->store();
174 xPersistence.set( helper.getCfgManager(), uno::UNO_QUERY_THROW );
175 xPersistence->store();
176 #endif
178 bRes = true;
180 catch( uno::Exception& )
182 bRes = false;
184 return bRes;
186 bool CTBS::Read( SvStream *pS )
188 OSL_TRACE("CTBS::Read() stream pos 0x%x", pS->Tell() );
189 nOffSet = pS->Tell();
190 *pS >> bSignature >> bVersion >> reserved1 >> reserved2 >> reserved3 >> ctb >> ctbViews >> ictbView;
191 return true;
194 void CTBS::Print( FILE* fp )
196 Indent a;
197 indent_printf( fp, "[ 0x%x ] CTBS -- dump\n", nOffSet );
199 indent_printf( fp, " bSignature 0x%x\n", bSignature);
200 indent_printf( fp, " bVersion 0x%x\n", bVersion);
202 indent_printf( fp, " reserved1 0x%x\n", reserved1 );
203 indent_printf( fp, " reserved2 0x%x\n", reserved2 );
204 indent_printf( fp, " reserved3 0x%x\n", reserved3 );
206 indent_printf( fp, " ctb 0x%x\n", ctb );
207 indent_printf( fp, " ctbViews 0x%x\n", ctbViews );
208 indent_printf( fp, " ictbView 0x%x\n", ictbView );
211 TBC::TBC()
215 bool
216 TBC::Read(SvStream *pS)
218 OSL_TRACE("TBC::Read() stream pos 0x%x", pS->Tell() );
219 nOffSet = pS->Tell();
220 if ( !tbch.Read( pS ) )
221 return false;
222 sal_uInt16 tcid = tbch.getTcID();
223 sal_uInt8 tct = tbch.getTct();
224 if ( ( tcid != 0x0001 && tcid != 0x06CC && tcid != 0x03D8 && tcid != 0x03EC && tcid != 0x1051 ) && ( ( tct > 0 && tct < 0x0B ) || ( ( tct > 0x0B && tct < 0x10 ) || tct == 0x15 ) ) )
226 tbcCmd.reset( new TBCCmd );
227 if ( ! tbcCmd->Read( pS ) )
228 return false;
230 if ( tct != 0x16 )
232 tbcd.reset( new TBCData( tbch ) );
233 if ( !tbcd->Read( pS ) )
234 return false;
236 return true;
240 void
241 TBC::Print(FILE* fp)
243 Indent a;
244 indent_printf( fp, "[ 0x%x ] TBC -- dump\n", nOffSet );
245 tbch.Print( fp );
246 if ( tbcCmd.get() )
247 tbcCmd->Print( fp );
248 if ( tbcd.get() )
249 tbcd->Print( fp );
252 bool TBC::ImportToolBarControl( CTBWrapper& rWrapper, const css::uno::Reference< css::container::XIndexContainer >& toolbarcontainer, CustomToolBarImportHelper& helper, bool bIsMenuToolbar )
254 // how to identify built-in-command ?
255 // bool bBuiltin = false;
256 if ( tbcd.get() )
258 std::vector< css::beans::PropertyValue > props;
260 if ( bBuiltin )
262 rtl::OUString sCommand = helper.MSOCommandToOOCommand( cmdId );
263 if ( sCommand.getLength() > 0 )
265 beans::PropertyValue aProp;
267 aProp.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("CommandURL") );
268 aProp.Value <<= sCommand;
269 props.push_back( aProp );
274 bool bBeginGroup = false;
275 if ( ! tbcd->ImportToolBarControl( helper, props, bBeginGroup, bIsMenuToolbar ) )
276 return false;
277 TBCMenuSpecific* pMenu = tbcd->getMenuSpecific();
278 if ( pMenu )
280 // search for CTB with the appropriate name ( it contains the
281 // menu items, although we cannot import ( or create ) a menu on
282 // a custom toolbar we can import the menu items in a separate
283 // toolbar ( better than nothing )
284 CTB* pCustTB = rWrapper.GetCustomizationData( pMenu->Name() );
285 if ( pCustTB )
287 uno::Reference< container::XIndexContainer > xMenuDesc;
288 uno::Reference< lang::XMultiServiceFactory > xMSF( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
289 xMenuDesc.set( xMSF->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.IndexedPropertyValues" ) ) ), uno::UNO_QUERY_THROW );
290 if ( !pCustTB->ImportMenuTB( rWrapper, xMenuDesc, helper ) )
291 return false;
292 OSL_TRACE("*** Read menu, no. items is %d", xMenuDesc->getCount() );
293 beans::PropertyValue aProp;
295 aProp.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ItemDescriptorContainer") );
296 aProp.Value <<= xMenuDesc;
297 props.push_back( aProp );
301 if ( bBeginGroup )
303 // insert spacer
304 uno::Sequence< beans::PropertyValue > sProps( 1 );
305 sProps[ 0 ].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Type") );
306 sProps[ 0 ].Value = uno::makeAny( ui::ItemType::SEPARATOR_LINE );
307 toolbarcontainer->insertByIndex( toolbarcontainer->getCount(), uno::makeAny( sProps ) );
309 uno::Sequence< beans::PropertyValue > sProps( props.size() );
310 beans::PropertyValue* pProp = sProps.getArray();
312 for ( std::vector< css::beans::PropertyValue >::iterator it = props.begin(); it != props.end(); ++it, ++pProp )
313 *pProp = *it;
315 toolbarcontainer->insertByIndex( toolbarcontainer->getCount(), uno::makeAny( sProps ) );
317 return true;
320 void
321 TBCCmd::Print(FILE* fp)
323 Indent a;
324 indent_printf( fp, " TBCCmd -- dump\n" );
325 indent_printf( fp, " cmdID 0x%x\n", cmdID );
326 indent_printf( fp, " A ( fHideDrawing ) %s\n", A ? "true" : "false" );
327 indent_printf( fp, " B ( reserved - ignored ) %s\n", A ? "true" : "false" );
328 indent_printf( fp, " cmdType 0x%x\n", cmdType );
329 indent_printf( fp, " C ( reserved - ignored ) %s\n", A ? "true" : "false" );
330 indent_printf( fp, " reserved3 0x%x\n", reserved3 );
333 bool TBCCmd::Read( SvStream *pS )
335 OSL_TRACE("TBCCmd::Read() stream pos 0x%x", pS->Tell() );
336 nOffSet = pS->Tell();
337 *pS >> cmdID;
338 sal_uInt16 temp;
339 *pS >> temp;
340 OSL_TRACE("TBCmd temp = 0x%x", temp );
341 A = (temp & 0x8000 ) == 0x8000;
342 B = (temp & 0x4000) == 0x4000;
343 cmdType = ( temp & 0x3E00 ) >> 9;
344 C = ( temp & 0x100 ) == 0x100;
345 reserved3 = ( temp & 0xFF );
346 return true;
349 CTBWrapper::CTBWrapper()
353 CTBWrapper::~CTBWrapper()
357 bool
358 CTBWrapper::Read( SvStream *pS)
360 OSL_TRACE("CTBWrapper::Read() stream pos 0x%x", pS->Tell() );
361 nOffSet = pS->Tell();
362 if ( !ctbSet.Read( pS ) )
363 return false;
364 for ( sal_uInt16 index = 0; index < ctbSet.ctb; ++index )
366 CTB aCTB( ctbSet.ctbViews );
367 if ( !aCTB.Read( pS ) )
368 return false;
369 rCTB.push_back( aCTB );
371 return true;
374 void
375 CTBWrapper::Print( FILE* fp )
377 Indent a;
378 indent_printf( fp, "[ 0x%x ] CTBWrapper -- dump\n", nOffSet );
379 ctbSet.Print( fp );
380 std::vector<CTB>::iterator it_end = rCTB.end();
381 for ( std::vector<CTB>::iterator it = rCTB.begin(); it != it_end; ++it )
383 Indent b;
384 it->Print( fp );
388 CTB* CTBWrapper::GetCustomizationData( const rtl::OUString& sTBName )
390 CTB* pCTB = NULL;
391 for ( std::vector< CTB >::iterator it = rCTB.begin(); it != rCTB.end(); ++it )
393 if ( it->GetName().equals( sTBName ) )
395 pCTB = &(*it);
396 break;
399 return pCTB;
402 bool CTBWrapper::ImportCustomToolBar( SfxObjectShell& rDocSh )
404 std::vector<CTB>::iterator it_end = rCTB.end();
405 for ( std::vector<CTB>::iterator it = rCTB.begin(); it != it_end; ++it )
407 // for each customtoolbar
408 uno::Reference< lang::XMultiServiceFactory > xMSF( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
409 uno::Reference< ui::XModuleUIConfigurationManagerSupplier > xAppCfgSupp( xMSF->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.ModuleUIConfigurationManagerSupplier" ) ) ), uno::UNO_QUERY_THROW );
410 CustomToolBarImportHelper helper( rDocSh, xAppCfgSupp->getUIConfigurationManager( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sheet.SpreadsheetDocument" ) ) ) );
411 helper.setMSOCommandMap( new MSOExcelCommandConvertor() );
412 // Ignore menu toolbars, excel doesn't ( afaics ) store
413 // menu customizations ( but you can have menus in a customtoolbar
414 // such menus will be dealt with when they are encountered
415 // as part of importing the appropriate MenuSpecific toolbar control )
418 if ( !(*it).IsMenuToolbar() )
420 if ( !(*it).ImportCustomToolBar( *this, helper ) )
421 return false;
424 return true;