merge the formfield patch from ooo-build
[ooovba.git] / sw / source / filter / ww8 / ww8toolbar.cxx
blobe6b047af4d5b784b1b76a9e9d64aed5a785c48b8
1 #include "ww8toolbar.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 // no. of visual data elements in a CTB ( fixed )
19 const short nVisualData = 5;
21 typedef std::map< sal_Int16, rtl::OUString > IdToString;
23 class MSOWordCommandConvertor : public MSOCommandConvertor
25 IdToString msoToOOcmd;
26 IdToString tcidToOOcmd;
27 public:
28 MSOWordCommandConvertor();
29 virtual rtl::OUString MSOCommandToOOCommand( sal_Int16 msoCmd );
30 virtual rtl::OUString MSOTCIDToOOCommand( sal_Int16 key );
33 MSOWordCommandConvertor::MSOWordCommandConvertor()
35 // mso command id to ooo command string
36 // #FIXME and *HUNDREDS* of id's to added here
37 msoToOOcmd[ 0x20b ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(".uno:CloseDoc") );
38 msoToOOcmd[ 0x50 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(".uno:Open") );
40 // mso tcid to ooo command string
41 // #FIXME and *HUNDREDS* of id's to added here
42 tcidToOOcmd[ 0x9d9 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(".uno:Print") );
45 rtl::OUString MSOWordCommandConvertor::MSOCommandToOOCommand( sal_Int16 key )
47 rtl::OUString sResult;
48 IdToString::iterator it = msoToOOcmd.find( key );
49 if ( it != msoToOOcmd.end() )
50 sResult = it->second;
51 return sResult;
54 rtl::OUString MSOWordCommandConvertor::MSOTCIDToOOCommand( sal_Int16 key )
56 rtl::OUString sResult;
57 IdToString::iterator it = tcidToOOcmd.find( key );
58 if ( it != tcidToOOcmd.end() )
59 sResult = it->second;
60 return sResult;
64 CTBWrapper::CTBWrapper( bool bReadId ) : Tcg255SubStruct( bReadId )
65 ,reserved2(0)
66 ,reserved3(0)
67 ,reserved4(0)
68 ,reserved5(0)
69 ,cbTBD(0)
70 ,cCust(0)
71 ,cbDTBC(0)
72 ,rtbdc(0)
76 CTBWrapper::~CTBWrapper()
80 Customization* CTBWrapper::GetCustomizaton( sal_Int16 index )
82 if ( index < 0 || index >= rCustomizations.size() )
83 return NULL;
84 return &rCustomizations[ index ];
87 CTB* CTBWrapper::GetCustomizationData( const rtl::OUString& sTBName )
89 CTB* pCTB = NULL;
90 for ( std::vector< Customization >::iterator it = rCustomizations.begin(); it != rCustomizations.end(); ++it )
92 if ( it->GetCustomizationData() && it->GetCustomizationData()->GetName().equals( sTBName ) )
94 pCTB = it->GetCustomizationData();
95 break;
98 return pCTB;
101 bool CTBWrapper::Read( SvStream* pS )
103 OSL_TRACE("CTBWrapper::Read() stream pos 0x%x", pS->Tell() );
104 nOffSet = pS->Tell();
105 Tcg255SubStruct::Read( pS );
106 *pS >> reserved2 >> reserved3 >> reserved4 >> reserved5;
107 *pS >> cbTBD >> cCust >> cbDTBC;
108 if ( cbDTBC )
110 // cbDTBC is the size in bytes of the TBC array
111 // but the size of a TBC element is dynamic ( and this relates to TBDelta's
112 int nStart = pS->Tell();
114 int bytesRead = 0;
115 int bytesToRead = cbDTBC - bytesRead;
116 // cbDTBC specifies the size ( in bytes ) taken by an array ( of unspecified size )
117 // of TBC records ( TBC records have dynamic length, so we need to check our position
118 // after each read )
121 TBC aTBC;
122 if ( !aTBC.Read( pS ) )
123 return false;
124 rtbdc.push_back( aTBC );
125 bytesToRead = cbDTBC - ( pS->Tell() - nStart );
126 } while ( bytesToRead > 0 );
128 if ( cCust )
130 for ( sal_Int32 index = 0; index < cCust; ++index )
132 Customization aCust( this );
133 if ( !aCust.Read( pS ) )
134 return false;
135 rCustomizations.push_back( aCust );
138 std::vector< sal_Int16 >::iterator it_end = dropDownMenuIndices.end();
139 for ( std::vector< sal_Int16 >::iterator it = dropDownMenuIndices.begin(); it != it_end; ++it )
141 rCustomizations[ *it ].bIsDroppedMenuTB = true;
143 return true;
146 TBC* CTBWrapper::GetTBCAtOffset( sal_uInt32 nStreamOffset )
148 for ( std::vector< TBC >::iterator it = rtbdc.begin(); it != rtbdc.end(); ++it )
150 if ( (*it).GetOffset() == nStreamOffset )
151 return &(*it);
153 return NULL;
156 void CTBWrapper::Print( FILE* fp )
158 Indent a;
159 indent_printf(fp,"[ 0x%x ] CTBWrapper - dump\n", nOffSet );
160 bool bRes = ( ch == 0x12 && reserved2 == 0x0 && reserved3 == 0x7 && reserved4 == 0x6 && reserved5 == 0xC );
161 if ( bRes )
162 indent_printf(fp," sanity check ( first 8 bytes conform )\n");
163 else
165 indent_printf(fp," reserved1(0x%x)\n",ch);
166 indent_printf(fp," reserved2(0x%x)\n",reserved2);
167 indent_printf(fp," reserved3(0x%x)\n",reserved3);
168 indent_printf(fp," reserved4(0x%x)\n",reserved4);
169 indent_printf(fp," reserved5(0x%x)\n",reserved5);
170 indent_printf(fp,"Quiting dump");
171 return;
173 indent_printf(fp," size of TBDelta structures 0x%x\n", cbTBD );
174 indent_printf(fp," cCust: no. of cCust structures 0x%x\n",cCust);
175 indent_printf(fp," cbDTBC: no. of bytes in rtbdc array 0x%x\n", static_cast< unsigned int >( cbDTBC ));
177 sal_Int32 index = 0;
179 for ( std::vector< TBC >::iterator it = rtbdc.begin(); it != rtbdc.end(); ++it, ++index )
181 indent_printf(fp," Dumping rtbdc[%d]\n", static_cast< int >( index ));
182 Indent b;
183 it->Print( fp );
186 index = 0;
188 for ( std::vector< Customization >::iterator it = rCustomizations.begin(); it != rCustomizations.end(); ++it, ++index )
190 indent_printf(fp," Dumping custimization [%d]\n", static_cast< int >( index ));
191 Indent c;
192 it->Print(fp);
196 bool CTBWrapper::ImportCustomToolBar( SfxObjectShell& rDocSh )
198 for ( std::vector< Customization >::iterator it = rCustomizations.begin(); it != rCustomizations.end(); ++it )
200 uno::Reference< lang::XMultiServiceFactory > xMSF( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
201 uno::Reference< ui::XModuleUIConfigurationManagerSupplier > xAppCfgSupp( xMSF->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.ModuleUIConfigurationManagerSupplier" ) ) ), uno::UNO_QUERY_THROW );
202 CustomToolBarImportHelper helper( rDocSh, xAppCfgSupp->getUIConfigurationManager( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.TextDocument" ) ) ) );
203 helper.setMSOCommandMap( new MSOWordCommandConvertor() );
205 if ( !(*it).ImportCustomToolBar( *this, helper ) )
206 return false;
208 return false;
211 Customization::Customization( CTBWrapper* wrapper ) : tbidForTBD( 0 )
212 ,reserved1( 0 )
213 , ctbds( 0 )
214 , pWrapper( wrapper )
215 , bIsDroppedMenuTB( false )
219 Customization::~Customization()
223 bool Customization::Read( SvStream *pS)
225 OSL_TRACE("Custimization::Read() stream pos 0x%x", pS->Tell() );
226 nOffSet = pS->Tell();
227 *pS >> tbidForTBD >> reserved1 >> ctbds;
228 if ( tbidForTBD )
230 for ( sal_Int32 index = 0; index < ctbds; ++index )
232 TBDelta aTBDelta;
233 if (!aTBDelta.Read( pS ) )
234 return false;
235 customizationDataTBDelta.push_back( aTBDelta );
236 // Only set the drop down for menu's associated with standard toolbar
237 if ( aTBDelta.ControlDropsToolBar() && tbidForTBD == 0x25 )
238 pWrapper->InsertDropIndex( aTBDelta.CustomizationIndex() );
241 else
243 customizationDataCTB.reset( new CTB() );
244 if ( !customizationDataCTB->Read( pS ) )
245 return false;
247 return true;
250 void Customization::Print( FILE* fp )
252 Indent a;
253 indent_printf( fp,"[ 0x%x ] Customization -- dump \n", nOffSet );
254 indent_printf( fp," tbidForTBD 0x%x ( should be 0 for CTBs )\n", static_cast< unsigned int >( tbidForTBD ));
255 indent_printf( fp," reserved1 0x%x \n", reserved1);
256 indent_printf( fp," ctbds - number of customisations %d(0x%x) \n", ctbds, ctbds );
257 if ( !tbidForTBD && !ctbds )
258 customizationDataCTB->Print( fp );
259 else
261 const char* pToolBar = NULL;
262 switch ( tbidForTBD )
264 case 0x9:
265 pToolBar = "Standard";
266 break;
267 case 0x25:
268 pToolBar = "Builtin-Menu";
269 break;
270 default:
271 pToolBar = "Unknown toolbar";
272 break;
275 indent_printf( fp," TBDelta(s) are associated with %s toolbar.\n", pToolBar);
276 std::vector< TBDelta >::iterator it = customizationDataTBDelta.begin();
277 for ( sal_Int32 index = 0; index < ctbds; ++it,++index )
278 it->Print( fp );
283 bool Customization::ImportMenu( CTBWrapper& rWrapper, CustomToolBarImportHelper& helper )
285 if ( tbidForTBD == 0x25 ) // we can handle in a limited way additions the built-in menu bar
287 for ( std::vector< TBDelta >::iterator it = customizationDataTBDelta.begin(); it != customizationDataTBDelta.end(); ++it )
289 // for each new menu ( control that drops a toolbar )
290 // import a toolbar
291 if ( it->ControlIsInserted() && it->ControlDropsToolBar() )
293 Customization* pCust = pWrapper->GetCustomizaton( it->CustomizationIndex() );
294 if ( pCust )
296 // currently only support built-in menu
297 rtl::OUString sMenuBar( RTL_CONSTASCII_USTRINGPARAM("private:resource/menubar/") );
299 sMenuBar = sMenuBar.concat( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("menubar") ) );
300 // Get menu name
301 TBC* pTBC = pWrapper->GetTBCAtOffset( it->TBCStreamOffset() );
302 if ( !pTBC )
303 return false;
304 rtl::OUString sMenuName = pTBC->GetCustomText();
305 sMenuName = sMenuName.replace('&','~');
307 // see if the document has already setting for the menubar
309 uno::Reference< container::XIndexContainer > xIndexContainer;
310 bool bHasSettings = false;
311 if ( helper.getCfgManager()->hasSettings( sMenuBar ) )
313 xIndexContainer.set( helper.getCfgManager()->getSettings( sMenuBar, sal_True ), uno::UNO_QUERY_THROW );
314 bHasSettings = true;
316 else
318 if ( helper.getAppCfgManager()->hasSettings( sMenuBar ) )
319 xIndexContainer.set( helper.getAppCfgManager()->getSettings( sMenuBar, sal_True ), uno::UNO_QUERY_THROW );
320 else
321 xIndexContainer.set( helper.getAppCfgManager()->createSettings(), uno::UNO_QUERY_THROW );
324 uno::Reference< lang::XSingleComponentFactory > xSCF( xIndexContainer, uno::UNO_QUERY_THROW );
325 uno::Reference< beans::XPropertySet > xProps( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
326 uno::Reference< uno::XComponentContext > xContext( xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ))), uno::UNO_QUERY_THROW );
327 // create the popup menu
328 uno::Sequence< beans::PropertyValue > aPopupMenu( 4 );
329 aPopupMenu[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("CommandURL") );
330 aPopupMenu[0].Value = uno::makeAny( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("vnd.openoffice.org:") ) + sMenuName );
331 aPopupMenu[1].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Label") );
332 aPopupMenu[1].Value <<= sMenuName;
333 aPopupMenu[2].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Type" ) );
334 aPopupMenu[2].Value <<= sal_Int32( 0 );
335 aPopupMenu[3].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ItemDescriptorContainer") );
336 uno::Reference< container::XIndexContainer > xMenuContainer( xSCF->createInstanceWithContext( xContext ), uno::UNO_QUERY_THROW );
337 aPopupMenu[3].Value <<= xMenuContainer;
338 if ( pCust->customizationDataCTB.get() && !pCust->customizationDataCTB->ImportMenuTB( rWrapper, xMenuContainer, helper ) )
339 return false;
340 OSL_TRACE("** there are %d menu items on the bar, inserting after that", xIndexContainer->getCount() );
341 xIndexContainer->insertByIndex( xIndexContainer->getCount(), uno::makeAny( aPopupMenu ) );
343 if ( bHasSettings )
344 helper.getCfgManager()->replaceSettings( sMenuBar, uno::Reference< container::XIndexAccess >( xIndexContainer, uno::UNO_QUERY_THROW ) );
345 else
346 helper.getCfgManager()->insertSettings( sMenuBar, uno::Reference< container::XIndexAccess >( xIndexContainer, uno::UNO_QUERY_THROW ) );
347 uno::Reference< ui::XUIConfigurationPersistence > xPersistence( helper.getCfgManager(), uno::UNO_QUERY_THROW );
348 xPersistence->store();
352 return true;
354 return true;
357 bool Customization::ImportCustomToolBar( CTBWrapper& rWrapper, CustomToolBarImportHelper& helper )
359 if ( GetTBIDForTB() == 0x25 )
360 return ImportMenu( rWrapper, helper );
361 if ( !customizationDataCTB.get() )
362 return false;
363 if ( !customizationDataCTB->IsMenuToolbar() )
365 if ( !customizationDataCTB->ImportCustomToolBar( rWrapper, helper ) )
366 return false;
368 return true;
371 TBDelta::TBDelta() : doprfatendFlags(0)
372 ,ibts(0)
373 ,cidNext(0)
374 ,cid(0)
375 ,fc(0)
376 ,cbTBC(0)
380 bool TBDelta::ControlIsModified()
382 return ( ( doprfatendFlags & 0x3 ) == 0x2 );
385 bool TBDelta::ControlIsInserted()
387 return ( ( doprfatendFlags & 0x3 ) == 0x1 );
390 bool TBDelta::ControlIsChanged()
392 return ( ( doprfatendFlags & 0x3 ) == 0x1 );
395 bool TBDelta::ControlDropsToolBar()
397 return !( CiTBDE & 0x8000 );
400 sal_Int32 TBDelta::TBCStreamOffset()
402 return fc;
405 sal_Int16 TBDelta::CustomizationIndex()
407 sal_Int16 nIndex = CiTBDE;
408 nIndex = nIndex >> 1;
409 nIndex &= 0x1ff; // only 13 bits are relevant
410 return nIndex;
413 bool TBDelta::Read(SvStream *pS)
415 OSL_TRACE("TBDelta::Read() stream pos 0x%x", pS->Tell() );
416 nOffSet = pS->Tell();
417 *pS >> doprfatendFlags >> ibts >> cidNext >> cid >> fc ;
418 *pS >> CiTBDE >> cbTBC;
419 return true;
422 void TBDelta::Print( FILE* fp )
424 // Like most of the debug output, it's raw and little ( no )
425 // interpretation of the data is output ( e.g. flag values etc. )
426 indent_printf( fp, "[ 0x%x ] TBDelta -- dump\n", nOffSet );
427 indent_printf( fp, " doprfatendFlags 0x%x\n",doprfatendFlags );
429 indent_printf( fp, " ibts 0x%x\n",ibts );
430 indent_printf( fp, " cidNext 0x%x\n", static_cast< unsigned int >( cidNext ) );
431 indent_printf( fp, " cid 0x%x\n", static_cast< unsigned int >( cid ) );
432 indent_printf( fp, " fc 0x%x\n", static_cast< unsigned int >( fc ) );
433 indent_printf( fp, " CiTBDE 0x%x\n",CiTBDE );
434 indent_printf( fp, " cbTBC 0x%x\n", cbTBC );
435 if ( ControlDropsToolBar() )
437 indent_printf( fp, " this delta is associated with a control that drops a menu toolbar\n", cbTBC );
438 indent_printf( fp, " the menu toolbar drops the toolbar defined at index[%d] in the rCustomizations array of the CTBWRAPPER that contains this TBDelta\n", CustomizationIndex() );
442 CTB::CTB() : cbTBData( 0 )
443 ,iWCTBl( 0 )
444 ,reserved( 0 )
445 ,unused( 0 )
446 ,cCtls( 0 )
450 CTB::~CTB()
454 bool CTB::IsMenuToolbar()
456 return tb.IsMenuToolbar();
460 bool CTB::Read( SvStream *pS)
462 OSL_TRACE("CTB::Read() stream pos 0x%x", pS->Tell() );
463 nOffSet = pS->Tell();
464 if ( !name.Read( pS ) )
465 return false;
466 *pS >> cbTBData;
467 if ( !tb.Read( pS ) )
468 return false;
469 for ( short index = 0; index < nVisualData; ++index )
471 TBVisualData aVisData;
472 aVisData.Read( pS );
473 rVisualData.push_back( aVisData );
476 *pS >> iWCTBl >> reserved >> unused >> cCtls;
478 if ( cCtls )
480 for ( sal_Int32 index = 0; index < cCtls; ++index )
482 TBC aTBC;
483 if ( !aTBC.Read( pS ) )
484 return false;
485 rTBC.push_back( aTBC );
488 return true;
491 void
492 CTB::Print( FILE* fp )
494 Indent a;
495 indent_printf(fp, "[ 0x%x ] CTB - dump\n", nOffSet );
496 indent_printf(fp, " name %s\n", rtl::OUStringToOString( name.getString(), RTL_TEXTENCODING_UTF8 ).getStr() );
497 indent_printf(fp, " cbTBData size, in bytes, of this structure excluding the name, cCtls, and rTBC fields. %x\n", static_cast< unsigned int >( cbTBData ) );
499 tb.Print(fp);
500 for ( short counter = 0; counter < nVisualData; ++counter )
502 indent_printf( fp, " TBVisualData [%d]\n", counter);
503 Indent b;
504 rVisualData[ counter ].Print( fp );
506 indent_printf(fp, " iWCTBl 0x%x reserved 0x%x unused 0x%x cCtls( toolbar controls ) 0x%x \n", static_cast< unsigned int >( iWCTBl ), reserved, unused, static_cast< unsigned int >( cCtls ) );
507 if ( cCtls )
509 for ( sal_Int32 index = 0; index < cCtls; ++index )
512 indent_printf(fp, " dumping toolbar control 0x%x\n", static_cast< unsigned int >( index ) );
513 rTBC[ index ].Print( fp );
518 bool CTB::ImportCustomToolBar( CTBWrapper& rWrapper, CustomToolBarImportHelper& helper )
520 static rtl::OUString sToolbarPrefix( RTL_CONSTASCII_USTRINGPARAM( "private:resource/toolbar/custom_" ) );
521 bool bRes = false;
524 if ( !tb.IsEnabled() )
525 return true; // didn't fail, just ignoring
526 // Create default setting
527 uno::Reference< container::XIndexContainer > xIndexContainer( helper.getCfgManager()->createSettings(), uno::UNO_QUERY_THROW );
528 uno::Reference< container::XIndexAccess > xIndexAccess( xIndexContainer, uno::UNO_QUERY_THROW );
529 uno::Reference< beans::XPropertySet > xProps( xIndexContainer, uno::UNO_QUERY_THROW );
531 // set UI name for toolbar
532 xProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("UIName") ), uno::makeAny( name.getString() ) );
534 rtl::OUString sToolBarName = sToolbarPrefix.concat( name.getString() );
535 for ( std::vector< TBC >::iterator it = rTBC.begin(); it != rTBC.end(); ++it )
537 // createToolBar item for control
538 if ( !it->ImportToolBarControl( rWrapper, xIndexContainer, helper, IsMenuToolbar() ) )
539 return false;
542 OSL_TRACE("Name of toolbar :-/ %s", rtl::OUStringToOString( sToolBarName, RTL_TEXTENCODING_UTF8 ).getStr() );
544 helper.getCfgManager()->insertSettings( sToolBarName, xIndexAccess );
545 helper.applyIcons();
546 #if 1 // don't think this is necessary
547 uno::Reference< ui::XUIConfigurationPersistence > xPersistence( helper.getCfgManager()->getImageManager(), uno::UNO_QUERY_THROW );
548 xPersistence->store();
550 xPersistence.set( helper.getCfgManager(), uno::UNO_QUERY_THROW );
551 xPersistence->store();
552 #endif
553 bRes = true;
555 catch( uno::Exception& e )
557 OSL_TRACE("***** For some reason we have an exception %s", rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
558 bRes = false;
560 return bRes;
563 bool CTB::ImportMenuTB( CTBWrapper& rWrapper, const css::uno::Reference< css::container::XIndexContainer >& xIndexContainer, CustomToolBarImportHelper& rHelper )
565 for ( std::vector< TBC >::iterator it = rTBC.begin(); it != rTBC.end(); ++it )
567 // createToolBar item for control
568 if ( !it->ImportToolBarControl( rWrapper, xIndexContainer, rHelper, true ) )
569 return false;
571 return true;
574 TBC::TBC()
578 TBC::~TBC()
582 bool TBC::Read( SvStream *pS )
584 OSL_TRACE("TBC::Read() stream pos 0x%x", pS->Tell() );
585 nOffSet = pS->Tell();
586 if ( !tbch.Read( pS ) )
587 return false;
588 if ( tbch.getTcID() != 0x1 && tbch.getTcID() != 0x1051 )
590 cid.reset( new sal_uInt32 );
591 *pS >> *cid;
593 // MUST exist if tbch.tct is not equal to 0x16
594 if ( tbch.getTct() != 0x16 )
596 tbcd.reset( new TBCData( tbch ) );
597 if ( !tbcd->Read( pS ) )
598 return false;
600 return true;
603 void TBC::Print( FILE* fp )
605 Indent a;
606 indent_printf(fp,"[ 0x%x ] TBC -- dump\n", nOffSet );
607 indent_printf(fp," dumping header ( TBCHeader )\n");
608 tbch.Print( fp );
609 if ( cid.get() )
610 indent_printf(fp," cid = 0x%x\n", static_cast< unsigned int >( *cid ) );
611 if ( tbcd.get() )
613 indent_printf(fp," dumping toolbar data TBCData \n");
614 tbcd->Print(fp);
618 bool
619 TBC::ImportToolBarControl( CTBWrapper& rWrapper, const css::uno::Reference< css::container::XIndexContainer >& toolbarcontainer, CustomToolBarImportHelper& helper, bool bIsMenuBar )
621 // cmtFci 0x1 Command based on a built-in command. See CidFci.
622 // cmtMacro 0x2 Macro command. See CidMacro.
623 // cmtAllocated 0x3 Allocated command. See CidAllocated.
624 // cmtNil 0x7 No command. See Cid.
625 bool bBuiltin = false;
626 sal_uInt16 cmdId = 0;
627 if ( cid.get() )
629 sal_uInt16 arg2 = ( *( cid.get() ) & 0xFFFF );
631 sal_uInt8 cmt = ( arg2 & 0x7 );
632 arg2 = ( arg2 >> 3 );
634 switch ( cmt )
636 case 1:
637 OSL_TRACE("cmt is cmtFci builtin command 0x%x", arg2);
638 bBuiltin = true;
639 cmdId = arg2;
640 break;
641 case 2:
642 OSL_TRACE("cmt is cmtMacro macro 0x%x", arg2);
643 break;
644 case 3:
645 OSL_TRACE("cmt is cmtAllocated [???] 0x%x", arg2);
646 break;
647 case 7:
648 OSL_TRACE("cmt is cmNill no-phing 0x%x", arg2);
649 break;
650 default:
651 OSL_TRACE("illegal 0x%x", cmt);
652 break;
656 if ( tbcd.get() )
658 std::vector< css::beans::PropertyValue > props;
659 if ( bBuiltin )
661 rtl::OUString sCommand = helper.MSOCommandToOOCommand( cmdId );
662 if ( sCommand.getLength() > 0 )
664 beans::PropertyValue aProp;
666 aProp.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("CommandURL") );
667 aProp.Value <<= sCommand;
668 props.push_back( aProp );
671 bool bBeginGroup = false;
672 if ( ! tbcd->ImportToolBarControl( helper, props, bBeginGroup, bIsMenuBar ) )
673 return false;
675 TBCMenuSpecific* pMenu = tbcd->getMenuSpecific();
676 if ( pMenu )
678 OSL_TRACE("** control has a menu, name of toolbar with menu items is %s", rtl::OUStringToOString( pMenu->Name(), RTL_TEXTENCODING_UTF8 ).getStr() );
679 // search for CTB with the appropriate name ( it contains the
680 // menu items, although we cannot import ( or create ) a menu on
681 // a custom toolbar we can import the menu items in a separate
682 // toolbar ( better than nothing )
683 CTB* pCustTB = rWrapper.GetCustomizationData( pMenu->Name() );
684 if ( pCustTB )
686 uno::Reference< container::XIndexContainer > xMenuDesc;
687 uno::Reference< lang::XMultiServiceFactory > xMSF( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
688 xMenuDesc.set( xMSF->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.IndexedPropertyValues" ) ) ), uno::UNO_QUERY_THROW );
689 if ( !pCustTB->ImportMenuTB( rWrapper,xMenuDesc, helper ) )
690 return false;
691 beans::PropertyValue aProp;
693 aProp.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ItemDescriptorContainer") );
694 aProp.Value <<= xMenuDesc;
695 props.push_back( aProp );
699 if ( bBeginGroup )
701 // insert spacer
702 uno::Sequence< beans::PropertyValue > sProps( 1 );
703 sProps[ 0 ].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Type") );
704 sProps[ 0 ].Value = uno::makeAny( ui::ItemType::SEPARATOR_LINE );
705 toolbarcontainer->insertByIndex( toolbarcontainer->getCount(), uno::makeAny( sProps ) );
708 uno::Sequence< beans::PropertyValue > sProps( props.size() );
709 beans::PropertyValue* pProp = sProps.getArray();
711 for ( std::vector< css::beans::PropertyValue >::iterator it = props.begin(); it != props.end(); ++it, ++pProp )
712 *pProp = *it;
714 toolbarcontainer->insertByIndex( toolbarcontainer->getCount(), uno::makeAny( sProps ) );
716 return true;
719 rtl::OUString
720 TBC::GetCustomText()
722 rtl::OUString sCustomText;
723 if ( tbcd.get() )
724 sCustomText = tbcd->getGeneralInfo().CustomText();
725 return sCustomText;
730 bool
731 Xst::Read( SvStream* pS )
733 OSL_TRACE("Xst::Read() stream pos 0x%x", pS->Tell() );
734 nOffSet = pS->Tell();
735 sal_Int16 nChars = 0;
736 *pS >> nChars;
737 sString = readUnicodeString( pS, static_cast< sal_Int32 >( nChars ) );
738 return true;
741 void
742 Xst::Print( FILE* fp )
744 Indent a;
745 indent_printf( fp, "[ 0x%x ] Xst -- dump\n", nOffSet );
746 indent_printf( fp, " %s", rtl::OUStringToOString( sString, RTL_TEXTENCODING_UTF8 ).getStr() );
749 Tcg::Tcg() : nTcgVer( 255 )
753 bool Tcg::Read(SvStream *pS)
755 OSL_TRACE("Tcg::Read() stream pos 0x%x", pS->Tell() );
756 nOffSet = pS->Tell();
757 *pS >> nTcgVer;
758 if ( nTcgVer != (sal_Int8)255 )
759 return false;
760 tcg.reset( new Tcg255() );
761 return tcg->Read( pS );
764 void Tcg::Print( FILE* fp )
766 Indent a(true);
767 indent_printf(fp, "[ 0x%x ] Tcg - dump %d\n", nOffSet, nTcgVer);
768 indent_printf(fp," nTcgVer %d\n", nTcgVer);
769 tcg->Print( fp );
772 bool Tcg::ImportCustomToolBar( SfxObjectShell& rDocSh )
774 return tcg->ImportCustomToolBar( rDocSh );
777 Tcg255::Tcg255()
781 Tcg255::~Tcg255()
783 std::vector< Tcg255SubStruct* >::iterator it = rgtcgData.begin();
784 for ( ; it != rgtcgData.end(); ++it )
785 delete *it;
788 bool Tcg255::processSubStruct( sal_uInt8 nId, SvStream *pS )
790 Tcg255SubStruct* pSubStruct = NULL;
791 switch ( nId )
793 case 0x1:
795 pSubStruct = new PlfMcd( false ); // don't read the id
796 break;
798 case 0x2:
800 pSubStruct = new PlfAcd( false );
801 break;
803 case 0x3:
804 case 0x4:
806 pSubStruct = new PlfKme( false );
807 break;
809 case 0x10:
811 pSubStruct = new TcgSttbf( false );
812 break;
814 case 0x11:
816 pSubStruct = new MacroNames( false );
817 break;
819 case 0x12:
821 pSubStruct = new CTBWrapper( false );
822 break;
824 default:
825 OSL_TRACE("Unknown id 0x%x",nId);
826 return false;
828 pSubStruct->ch = nId;
829 if ( !pSubStruct->Read( pS ) )
830 return false;
831 rgtcgData.push_back( pSubStruct );
832 return true;
835 bool Tcg255::ImportCustomToolBar( SfxObjectShell& rDocSh )
837 // Find the CTBWrapper
838 for ( std::vector< Tcg255SubStruct* >::const_iterator it = rgtcgData.begin(); it != rgtcgData.end(); ++it )
840 if ( (*it)->id() == 0x12 )
842 // not so great, shouldn't really have to do a horror casting
843 CTBWrapper* pCTBWrapper = dynamic_cast< CTBWrapper* > ( *it );
844 if ( pCTBWrapper )
846 if ( !pCTBWrapper->ImportCustomToolBar( rDocSh ) )
847 return false;
851 return true;
855 bool Tcg255::Read(SvStream *pS)
857 OSL_TRACE("Tcg255::Read() stream pos 0x%x", pS->Tell() );
858 nOffSet = pS->Tell();
859 sal_uInt8 nId = 0; //
860 *pS >> nId;
861 while ( nId != 0x40 )
863 if ( !processSubStruct( nId, pS ) )
864 return false;
865 *pS >> nId;
867 return true;
868 // Peek at
871 void Tcg255::Print( FILE* fp)
873 Indent a;
874 indent_printf(fp, "[ 0x%x ] Tcg255 - dump\n", nOffSet );
875 indent_printf(fp, " contains %d sub records\n", rgtcgData.size() );
876 std::vector< Tcg255SubStruct* >::iterator it = rgtcgData.begin();
877 std::vector< Tcg255SubStruct* >::iterator it_end = rgtcgData.end();
879 for( sal_Int32 count = 1; it != it_end ; ++it, ++count )
881 Indent b;
882 indent_printf(fp, " [%d] Tcg255SubStruct \n", static_cast< unsigned int >( count ) );
883 (*it)->Print(fp);
888 Tcg255SubStruct::Tcg255SubStruct( bool bReadId ) : mbReadId( bReadId ), ch(0)
892 bool Tcg255SubStruct::Read(SvStream *pS)
894 OSL_TRACE("Tcg255SubStruct::Read() stream pos 0x%x", pS->Tell() );
895 nOffSet = pS->Tell();
896 if ( mbReadId )
897 *pS >> ch;
898 return true;
901 PlfMcd::PlfMcd( bool bReadId ): Tcg255SubStruct( bReadId ), rgmcd( NULL )
904 PlfMcd::~PlfMcd()
906 if ( rgmcd )
907 delete[] rgmcd;
910 bool PlfMcd::Read(SvStream *pS)
912 OSL_TRACE("PffMcd::Read() stream pos 0x%x", pS->Tell() );
913 nOffSet = pS->Tell();
914 Tcg255SubStruct::Read( pS );
915 *pS >> iMac;
916 if ( iMac )
918 rgmcd = new MCD[ iMac ];
919 for ( sal_Int32 index = 0; index < iMac; ++index )
921 if ( !rgmcd[ index ].Read( pS ) )
922 return false;
925 return true;
928 void PlfMcd::Print( FILE* fp )
930 Indent a;
931 indent_printf(fp, "[ 0x%x ] PlfMcd ( Tcg255SubStruct ) - dump\n", nOffSet );
932 indent_printf(fp, " contains %d MCD records\n", static_cast<int>( iMac ) );
933 for ( sal_Int32 count=0; count < iMac; ++count )
935 Indent b;
936 indent_printf(fp, "[%d] MCD\n", static_cast< int >( count ) );
937 rgmcd[ count ].Print( fp );
942 PlfAcd::PlfAcd( bool bReadId ) : Tcg255SubStruct( bReadId )
943 ,iMac(0)
944 ,rgacd(NULL)
949 PlfAcd::~PlfAcd()
951 if ( rgacd )
952 delete[] rgacd;
955 bool PlfAcd::Read( SvStream *pS)
957 OSL_TRACE("PffAcd::Read() stream pos 0x%x", pS->Tell() );
958 nOffSet = pS->Tell();
959 Tcg255SubStruct::Read( pS );
960 *pS >> iMac;
961 if ( iMac )
963 rgacd = new Acd[ iMac ];
964 for ( sal_Int32 index = 0; index < iMac; ++index )
966 if ( !rgacd[ index ].Read( pS ) )
967 return false;
970 return true;
972 void PlfAcd::Print( FILE* fp )
974 Indent a;
975 indent_printf(fp, "[ 0x%x ] PlfAcd ( Tcg255SubStruct ) - dump\n", nOffSet );
976 indent_printf(fp, " contains %d ACD records\n", static_cast< int >( iMac ) );
977 for ( sal_Int32 count=0; count < iMac; ++count )
979 Indent b;
980 indent_printf(fp, "[%d] ACD\n", static_cast< int >( count ) );
981 rgacd[ count ].Print( fp );
986 PlfKme::PlfKme( bool bReadId ) : Tcg255SubStruct( bReadId )
987 ,iMac( 0 )
988 ,rgkme( NULL )
992 PlfKme::~PlfKme()
994 if ( rgkme )
995 delete[] rgkme;
998 bool PlfKme::Read(SvStream *pS)
1000 OSL_TRACE("PlfKme::Read() stream pos 0x%x", pS->Tell() );
1001 nOffSet = pS->Tell();
1002 Tcg255SubStruct::Read( pS );
1003 *pS >> iMac;
1004 if ( iMac )
1006 rgkme = new Kme[ iMac ];
1007 for( sal_Int32 index=0; index<iMac; ++index )
1009 if ( !rgkme[ index ].Read( pS ) )
1010 return false;
1013 return true;
1016 void PlfKme::Print( FILE* fp )
1018 Indent a;
1019 indent_printf(fp, "[ 0x%x ] PlfKme ( Tcg255SubStruct ) - dump\n", nOffSet );
1020 indent_printf(fp, " contains %d Kme records\n", static_cast< int >( iMac ) );
1021 for ( sal_Int32 count=0; count < iMac; ++count )
1023 Indent b;
1024 indent_printf(fp, "[%d] Kme\n", static_cast< int >( count ) );
1025 rgkme[ count ].Print( fp );
1030 TcgSttbf::TcgSttbf( bool bReadId ) : Tcg255SubStruct( bReadId )
1034 bool TcgSttbf::Read( SvStream *pS)
1036 OSL_TRACE("TcgSttbf::Read() stream pos 0x%x", pS->Tell() );
1037 nOffSet = pS->Tell();
1038 Tcg255SubStruct::Read( pS );
1039 return sttbf.Read( pS );
1042 void TcgSttbf::Print( FILE* fp )
1044 Indent a;
1045 indent_printf(fp,"[ 0x%x ] TcgSttbf - dump\n", nOffSet );
1046 sttbf.Print( fp );
1049 TcgSttbfCore::TcgSttbfCore() : fExtend( 0 )
1050 ,cData( 0 )
1051 ,cbExtra( 0 )
1052 ,dataItems( NULL )
1056 TcgSttbfCore::~TcgSttbfCore()
1058 if ( dataItems )
1059 delete[] dataItems;
1062 bool TcgSttbfCore::Read( SvStream* pS )
1064 OSL_TRACE("TcgSttbfCore::Read() stream pos 0x%x", pS->Tell() );
1065 nOffSet = pS->Tell();
1066 *pS >> fExtend >> cData >> cbExtra;
1067 if ( cData )
1069 dataItems = new SBBItem[ cData ];
1070 for ( sal_Int32 index = 0; index < cData; ++index )
1072 *pS >> dataItems[ index ].cchData;
1073 dataItems[ index ].data = readUnicodeString( pS, dataItems[ index ].cchData );
1074 *pS >> dataItems[ index ].extraData;
1077 return true;
1080 void TcgSttbfCore::Print( FILE* fp )
1082 Indent a;
1083 indent_printf( fp, "[ 0x%x ] TcgSttbfCore - dump\n");
1084 indent_printf( fp, " fExtend 0x%x [expected 0xFFFF ]\n", fExtend );
1085 indent_printf( fp, " cbExtra 0x%x [expected 0x02 ]\n", cbExtra );
1086 indent_printf( fp, " cData no. or string data items %d (0x%x)\n", cData, cData );
1088 if ( cData )
1090 for ( sal_Int32 index = 0; index < cData; ++index )
1091 indent_printf(fp," string dataItem[ %d(0x%x) ] has name %s and if referenced %d times.\n", static_cast< int >( index ), static_cast< unsigned int >( index ), rtl::OUStringToOString( dataItems[ index ].data, RTL_TEXTENCODING_UTF8 ).getStr(), dataItems[ index ].extraData );
1095 MacroNames::MacroNames( bool bReadId ) : Tcg255SubStruct( bReadId )
1096 ,iMac( 0 )
1097 ,rgNames( NULL )
1101 MacroNames::~MacroNames()
1103 if ( rgNames )
1104 delete[] rgNames;
1107 bool MacroNames::Read( SvStream *pS)
1109 OSL_TRACE("MacroNames::Read() stream pos 0x%x", pS->Tell() );
1110 nOffSet = pS->Tell();
1111 Tcg255SubStruct::Read( pS );
1112 *pS >> iMac;
1113 if ( iMac )
1115 rgNames = new MacroName[ iMac ];
1116 for ( sal_Int32 index = 0; index < iMac; ++index )
1118 if ( !rgNames[ index ].Read( pS ) )
1119 return false;
1122 return true;
1125 void MacroNames::Print( FILE* fp )
1127 Indent a;
1128 indent_printf(fp, "[ 0x%x ] MacroNames ( Tcg255SubStruct ) - dump\n");
1129 indent_printf(fp, " contains %d MacroName records\n", iMac );
1130 for ( sal_Int32 count=0; count < iMac; ++count )
1132 Indent b;
1133 indent_printf(fp, "[%d] MacroName\n", static_cast<int>( count ) );
1134 rgNames[ count ].Print( fp );
1139 MacroName::MacroName():ibst(0)
1144 bool MacroName::Read(SvStream *pS)
1146 OSL_TRACE("MacroName::Read() stream pos 0x%x", pS->Tell() );
1147 nOffSet = pS->Tell();
1148 *pS >> ibst;
1149 return xstz.Read( pS );
1152 void MacroName::Print( FILE* fp )
1154 Indent a;
1155 indent_printf( fp, "[ 0x%x ] MacroName - dump");
1156 indent_printf( fp," index - 0x%x has associated following record\n", ibst );
1157 xstz.Print( fp );
1162 Xstz::Xstz():chTerm(0)
1166 bool
1167 Xstz::Read(SvStream *pS)
1169 OSL_TRACE("Xstz::Read() stream pos 0x%x", pS->Tell() );
1170 nOffSet = pS->Tell();
1171 if ( !xst.Read( pS ) )
1172 return false;
1173 *pS >> chTerm;
1174 if ( chTerm != 0 ) // should be an assert
1175 return false;
1176 return true;
1179 void Xstz::Print( FILE* fp )
1181 Indent a;
1182 indent_printf(fp,"[ 0x%x ] Xstz -- dump\n", nOffSet );
1183 indent_printf(fp," Xst\n");
1184 xst.Print( fp );
1185 indent_printf(fp," chterm 0x%x ( should be zero )\n", chTerm);
1188 Kme::Kme() : reserved1(0)
1189 ,reserved2(0)
1190 ,kcm1(0)
1191 ,kcm2(0)
1192 ,kt(0)
1193 ,param(0)
1197 Kme::~Kme()
1201 bool
1202 Kme::Read(SvStream *pS)
1204 OSL_TRACE("Kme::Read() stream pos 0x%x", pS->Tell() );
1205 nOffSet = pS->Tell();
1206 *pS >> reserved1 >> reserved2 >> kcm1 >> kcm2 >> kt >> param;
1207 return true;
1210 void Kme::Print( FILE* fp )
1212 Indent a;
1214 indent_printf( fp, "[ 0x%x ] Kme - dump\n", nOffSet );
1215 indent_printf( fp, " reserved1 0x%x [expected 0x0 ]\n", reserved1 );
1216 indent_printf( fp, " reserved2 0x%x [expected 0x0 ]\n", reserved2 );
1217 indent_printf( fp, " kcm1 0x%x [shortcut key]\n", kcm1 );
1218 indent_printf( fp, " kcm2 0x%x [shortcut key]\n", kcm2 );
1219 indent_printf( fp, " kt 0x%x \n", kt );
1220 indent_printf( fp, " param 0x%x \n", static_cast< unsigned int >( param ) );
1223 Acd::Acd() : ibst( 0 )
1224 , fciBasedOnABC( 0 )
1228 bool Acd::Read(SvStream *pS)
1230 OSL_TRACE("Acd::Read() stream pos 0x%x", pS->Tell() );
1231 nOffSet = pS->Tell();
1232 *pS >> ibst >> fciBasedOnABC;
1233 return true;
1236 void Acd::Print( FILE* fp )
1238 Indent a;
1239 indent_printf( fp,"[ 0x%x ] ACD - dump\n", nOffSet );
1240 // #TODO flesh out interpretation of these values
1241 indent_printf( fp," ibst 0x%x\n", ibst);
1242 indent_printf( fp," fciBaseObABC 0x%x\n", fciBasedOnABC);
1245 MCD::MCD() : reserved1(0x56)
1246 ,reserved2( 0 )
1247 ,ibst( 0 )
1248 ,ibstName( 0 )
1249 ,reserved3( 0xFFFF )
1250 ,reserved4( 0 )
1251 ,reserved5( 0 )
1252 ,reserved6( 0 )
1253 ,reserved7( 0 )
1257 bool MCD::Read(SvStream *pS)
1259 OSL_TRACE("MCD::Read() stream pos 0x%x", pS->Tell() );
1260 nOffSet = pS->Tell();
1261 *pS >> reserved1 >> reserved2 >> ibst >> ibstName >> reserved3;
1262 *pS >> reserved4 >> reserved5 >> reserved6 >> reserved7;
1263 return true;
1266 void MCD::Print( FILE* fp )
1268 Indent a;
1269 indent_printf( fp, "[ 0x%x ] MCD - dump\n", nOffSet );
1270 indent_printf( fp, " reserved1 0x%x [expected 0x56 ]\n", reserved1 );
1271 indent_printf( fp, " reserved2 0x%x [expected 0x0 ]\n", reserved2 );
1272 indent_printf( fp, " ibst 0x%x specifies macro with MacroName.xstz = 0x%x\n", ibst, ibst );
1273 indent_printf( fp, " ibstName 0x%x index into command string table ( TcgSttbf.sttbf )\n", ibstName );
1275 indent_printf( fp, " reserved3 0x%x [expected 0xFFFF ]\n", reserved3 );
1276 indent_printf( fp, " reserved4 0x%x\n", static_cast< unsigned int >( reserved4 ) );
1277 indent_printf( fp, " reserved5 0x%x [expected 0x0 ]\n", static_cast< unsigned int >( reserved5 ) );
1278 indent_printf( fp, " reserved6 0x%x\n", static_cast< unsigned int >( reserved6 ) );
1279 indent_printf( fp, " reserved7 0x%x\n", static_cast< unsigned int >( reserved7 ) );