fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / framework / source / fwe / xml / toolboxdocumenthandler.cxx
bloba294ca9d788c54c6d56850e25848f79b593dba31
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <stdio.h>
22 #include <xml/toolboxdocumenthandler.hxx>
23 #include <xml/toolboxconfigurationdefines.hxx>
25 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
26 #include <com/sun/star/ui/ItemType.hpp>
27 #include <com/sun/star/ui/ItemStyle.hpp>
28 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <sal/config.h>
31 #include <sal/macros.h>
32 #include <vcl/svapp.hxx>
33 #include <vcl/toolbox.hxx>
34 #include <vcl/settings.hxx>
35 #include <rtl/ustrbuf.hxx>
37 #include <comphelper/attributelist.hxx>
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::container;
42 using namespace ::com::sun::star::xml::sax;
44 #define TOOLBAR_DOCTYPE "<!DOCTYPE toolbar:toolbar PUBLIC \"-//OpenOffice.org//DTD OfficeDocument 1.0//EN\" \"toolbar.dtd\">"
46 namespace framework
49 // Property names of a menu/menu item ItemDescriptor
50 static const char ITEM_DESCRIPTOR_COMMANDURL[] = "CommandURL";
51 static const char ITEM_DESCRIPTOR_HELPURL[] = "HelpURL";
52 static const char ITEM_DESCRIPTOR_TOOLTIP[] = "Tooltip";
53 static const char ITEM_DESCRIPTOR_LABEL[] = "Label";
54 static const char ITEM_DESCRIPTOR_TYPE[] = "Type";
55 static const char ITEM_DESCRIPTOR_STYLE[] = "Style";
56 static const char ITEM_DESCRIPTOR_VISIBLE[] = "IsVisible";
58 static void ExtractToolbarParameters( const Sequence< PropertyValue >& rProp,
59 OUString& rCommandURL,
60 OUString& rLabel,
61 OUString& rHelpURL,
62 OUString& rTooltip,
63 sal_Int16& rStyle,
64 sal_Int16& rWidth,
65 bool& rVisible,
66 sal_Int16& rType )
68 for ( sal_Int32 i = 0; i < rProp.getLength(); i++ )
70 if ( rProp[i].Name == ITEM_DESCRIPTOR_COMMANDURL )
72 rProp[i].Value >>= rCommandURL;
73 rCommandURL = rCommandURL.intern();
75 else if ( rProp[i].Name == ITEM_DESCRIPTOR_HELPURL )
76 rProp[i].Value >>= rHelpURL;
77 else if ( rProp[i].Name == ITEM_DESCRIPTOR_TOOLTIP )
78 rProp[i].Value >>= rTooltip;
79 else if ( rProp[i].Name == ITEM_DESCRIPTOR_LABEL )
80 rProp[i].Value >>= rLabel;
81 else if ( rProp[i].Name == ITEM_DESCRIPTOR_TYPE )
82 rProp[i].Value >>= rType;
83 else if ( rProp[i].Name == ITEM_DESCRIPTOR_VISIBLE )
84 rProp[i].Value >>= rVisible;
85 else if ( rProp[i].Name == "Width" )
86 rProp[i].Value >>= rWidth;
87 else if ( rProp[i].Name == ITEM_DESCRIPTOR_STYLE )
88 rProp[i].Value >>= rStyle;
92 struct ToolboxStyleItem
94 sal_Int16 nBit;
95 const char* attrName;
98 ToolboxStyleItem Styles[ ] = {
99 { ::com::sun::star::ui::ItemStyle::RADIO_CHECK, ATTRIBUTE_ITEMSTYLE_RADIO },
100 { ::com::sun::star::ui::ItemStyle::ALIGN_LEFT, ATTRIBUTE_ITEMSTYLE_LEFT },
101 { ::com::sun::star::ui::ItemStyle::AUTO_SIZE, ATTRIBUTE_ITEMSTYLE_AUTO },
102 { ::com::sun::star::ui::ItemStyle::REPEAT, ATTRIBUTE_ITEMSTYLE_REPEAT },
103 { ::com::sun::star::ui::ItemStyle::DROPDOWN_ONLY, ATTRIBUTE_ITEMSTYLE_DROPDOWNONLY },
104 { ::com::sun::star::ui::ItemStyle::DROP_DOWN, ATTRIBUTE_ITEMSTYLE_DROPDOWN },
105 { ::com::sun::star::ui::ItemStyle::ICON, ATTRIBUTE_ITEMSTYLE_IMAGE },
106 { ::com::sun::star::ui::ItemStyle::TEXT, ATTRIBUTE_ITEMSTYLE_TEXT },
109 sal_Int32 nStyleItemEntries = sizeof (Styles) / sizeof (Styles[0]);
111 struct ToolBarEntryProperty
113 OReadToolBoxDocumentHandler::ToolBox_XML_Namespace nNamespace;
114 char aEntryName[20];
117 ToolBarEntryProperty ToolBoxEntries[OReadToolBoxDocumentHandler::TB_XML_ENTRY_COUNT] =
119 { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ELEMENT_TOOLBAR },
120 { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ELEMENT_TOOLBARITEM },
121 { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ELEMENT_TOOLBARSPACE },
122 { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ELEMENT_TOOLBARBREAK },
123 { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ELEMENT_TOOLBARSEPARATOR },
124 { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ATTRIBUTE_TEXT },
125 { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ATTRIBUTE_BITMAP },
126 { OReadToolBoxDocumentHandler::TB_NS_XLINK, ATTRIBUTE_URL },
127 { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ATTRIBUTE_ITEMBITS },
128 { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ATTRIBUTE_VISIBLE },
129 { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ATTRIBUTE_WIDTH },
130 { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ATTRIBUTE_USER },
131 { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ATTRIBUTE_HELPID },
132 { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ATTRIBUTE_ITEMSTYLE },
133 { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ATTRIBUTE_UINAME },
134 { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ATTRIBUTE_TOOLTIP },
137 OReadToolBoxDocumentHandler::OReadToolBoxDocumentHandler( const Reference< XIndexContainer >& rItemContainer ) :
138 m_rItemContainer( rItemContainer ),
139 m_aType( ITEM_DESCRIPTOR_TYPE ),
140 m_aLabel( ITEM_DESCRIPTOR_LABEL ),
141 m_aStyle( ITEM_DESCRIPTOR_STYLE ),
142 m_aHelpURL( ITEM_DESCRIPTOR_HELPURL ),
143 m_aTooltip( ITEM_DESCRIPTOR_TOOLTIP ),
144 m_aIsVisible( ITEM_DESCRIPTOR_VISIBLE ),
145 m_aCommandURL( ITEM_DESCRIPTOR_COMMANDURL )
147 OUString aNamespaceToolBar( XMLNS_TOOLBAR );
148 OUString aNamespaceXLink( XMLNS_XLINK );
149 OUString aSeparator( XMLNS_FILTER_SEPARATOR );
151 // create hash map
152 for ( int i = 0; i < (int)TB_XML_ENTRY_COUNT; i++ )
154 if ( ToolBoxEntries[i].nNamespace == TB_NS_TOOLBAR )
156 OUString temp( aNamespaceToolBar );
157 temp += aSeparator;
158 temp += OUString::createFromAscii( ToolBoxEntries[i].aEntryName );
159 m_aToolBoxMap.insert( ToolBoxHashMap::value_type( temp, (ToolBox_XML_Entry)i ) );
161 else
163 OUString temp( aNamespaceXLink );
164 temp += aSeparator;
165 temp += OUString::createFromAscii( ToolBoxEntries[i].aEntryName );
166 m_aToolBoxMap.insert( ToolBoxHashMap::value_type( temp, (ToolBox_XML_Entry)i ) );
170 // pre-calculate a hash code for all style strings to speed up xml read process
171 m_nHashCode_Style_Radio = OUString( ATTRIBUTE_ITEMSTYLE_RADIO ).hashCode();
172 m_nHashCode_Style_Auto = OUString( ATTRIBUTE_ITEMSTYLE_AUTO ).hashCode();
173 m_nHashCode_Style_Left = OUString( ATTRIBUTE_ITEMSTYLE_LEFT ).hashCode();
174 m_nHashCode_Style_AutoSize = OUString( ATTRIBUTE_ITEMSTYLE_AUTOSIZE ).hashCode();
175 m_nHashCode_Style_DropDown = OUString( ATTRIBUTE_ITEMSTYLE_DROPDOWN ).hashCode();
176 m_nHashCode_Style_Repeat = OUString( ATTRIBUTE_ITEMSTYLE_REPEAT ).hashCode();
177 m_nHashCode_Style_DropDownOnly = OUString( ATTRIBUTE_ITEMSTYLE_DROPDOWNONLY ).hashCode();
178 m_nHashCode_Style_Text = OUString( ATTRIBUTE_ITEMSTYLE_TEXT ).hashCode();
179 m_nHashCode_Style_Image = OUString( ATTRIBUTE_ITEMSTYLE_IMAGE ).hashCode();
181 m_bToolBarStartFound = false;
182 m_bToolBarEndFound = false;
183 m_bToolBarItemStartFound = false;
184 m_bToolBarSpaceStartFound = false;
185 m_bToolBarBreakStartFound = false;
186 m_bToolBarSeparatorStartFound = false;
189 OReadToolBoxDocumentHandler::~OReadToolBoxDocumentHandler()
193 // XDocumentHandler
194 void SAL_CALL OReadToolBoxDocumentHandler::startDocument()
195 throw ( SAXException, RuntimeException, std::exception )
199 void SAL_CALL OReadToolBoxDocumentHandler::endDocument()
200 throw( SAXException, RuntimeException, std::exception )
202 SolarMutexGuard g;
204 if (( m_bToolBarStartFound && !m_bToolBarEndFound ) ||
205 ( !m_bToolBarStartFound && m_bToolBarEndFound ) )
207 OUString aErrorMessage = getErrorLineString();
208 aErrorMessage += "No matching start or end element 'toolbar' found!";
209 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
213 void SAL_CALL OReadToolBoxDocumentHandler::startElement(
214 const OUString& aName, const Reference< XAttributeList > &xAttribs )
215 throw( SAXException, RuntimeException, std::exception )
217 SolarMutexGuard g;
219 ToolBoxHashMap::const_iterator pToolBoxEntry = m_aToolBoxMap.find( aName );
220 if ( pToolBoxEntry != m_aToolBoxMap.end() )
222 switch ( pToolBoxEntry->second )
224 case TB_ELEMENT_TOOLBAR:
226 if ( m_bToolBarStartFound )
228 OUString aErrorMessage = getErrorLineString();
229 aErrorMessage += "Element 'toolbar:toolbar' cannot be embedded into 'toolbar:toolbar'!";
230 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
232 else
234 // Check if we have a UI name set in our XML file
235 OUString aUIName;
236 for ( sal_Int16 n = 0; n < xAttribs->getLength(); n++ )
238 pToolBoxEntry = m_aToolBoxMap.find( xAttribs->getNameByIndex( n ) );
239 if ( pToolBoxEntry != m_aToolBoxMap.end() )
241 switch ( pToolBoxEntry->second )
243 case TB_ATTRIBUTE_UINAME:
244 aUIName = xAttribs->getValueByIndex( n );
245 break;
246 default:
247 break;
252 if ( !aUIName.isEmpty() )
254 // Try to set UI name as a container property
255 Reference< XPropertySet > xPropSet( m_rItemContainer, UNO_QUERY );
256 if ( xPropSet.is() )
260 xPropSet->setPropertyValue("UIName", makeAny( aUIName ) );
262 catch ( const UnknownPropertyException& )
269 m_bToolBarStartFound = true;
271 break;
273 case TB_ELEMENT_TOOLBARITEM:
275 if ( !m_bToolBarStartFound )
277 OUString aErrorMessage = getErrorLineString();
278 aErrorMessage += "Element 'toolbar:toolbaritem' must be embedded into element 'toolbar:toolbar'!";
279 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
282 if ( m_bToolBarSeparatorStartFound ||
283 m_bToolBarBreakStartFound ||
284 m_bToolBarSpaceStartFound ||
285 m_bToolBarItemStartFound )
287 OUString aErrorMessage = getErrorLineString();
288 aErrorMessage += "Element toolbar:toolbaritem is not a container!";
289 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
292 bool bAttributeURL = false;
294 m_bToolBarItemStartFound = true;
295 OUString aLabel;
296 OUString aCommandURL;
297 OUString aHelpURL;
298 OUString aTooltip;
299 OUString aBitmapName;
300 sal_uInt16 nItemBits( 0 );
301 bool bVisible( true );
303 for ( sal_Int16 n = 0; n < xAttribs->getLength(); n++ )
305 pToolBoxEntry = m_aToolBoxMap.find( xAttribs->getNameByIndex( n ) );
306 if ( pToolBoxEntry != m_aToolBoxMap.end() )
308 switch ( pToolBoxEntry->second )
310 case TB_ATTRIBUTE_TEXT:
312 aLabel = xAttribs->getValueByIndex( n );
314 break;
316 case TB_ATTRIBUTE_BITMAP:
318 aBitmapName = xAttribs->getValueByIndex( n );
320 break;
322 case TB_ATTRIBUTE_URL:
324 bAttributeURL = true;
325 aCommandURL = xAttribs->getValueByIndex( n ).intern();
327 break;
329 case TB_ATTRIBUTE_ITEMBITS:
331 nItemBits = (sal_uInt16)(xAttribs->getValueByIndex( n ).toInt32());
333 break;
335 case TB_ATTRIBUTE_VISIBLE:
337 if ( xAttribs->getValueByIndex( n ) == ATTRIBUTE_BOOLEAN_TRUE )
338 bVisible = true;
339 else if ( xAttribs->getValueByIndex( n ) == ATTRIBUTE_BOOLEAN_FALSE )
340 bVisible = false;
341 else
343 OUString aErrorMessage = getErrorLineString();
344 aErrorMessage += "Attribute toolbar:visible must have value 'true' or 'false'!";
345 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
348 break;
350 case TB_ATTRIBUTE_HELPID:
352 aHelpURL = xAttribs->getValueByIndex( n );
354 break;
356 case TB_ATTRIBUTE_TOOLTIP:
358 aTooltip = xAttribs->getValueByIndex( n );
360 break;
362 case TB_ATTRIBUTE_STYLE:
364 // read space separated item style list
365 OUString aTemp = xAttribs->getValueByIndex( n );
366 sal_Int32 nIndex = 0;
370 OUString aToken = aTemp.getToken( 0, ' ', nIndex );
371 if ( !aToken.isEmpty() )
373 sal_Int32 nHashCode = aToken.hashCode();
374 if ( nHashCode == m_nHashCode_Style_Radio )
375 nItemBits |= ::com::sun::star::ui::ItemStyle::RADIO_CHECK;
376 else if ( nHashCode == m_nHashCode_Style_Left )
377 nItemBits |= ::com::sun::star::ui::ItemStyle::ALIGN_LEFT;
378 else if ( nHashCode == m_nHashCode_Style_AutoSize )
379 nItemBits |= ::com::sun::star::ui::ItemStyle::AUTO_SIZE;
380 else if ( nHashCode == m_nHashCode_Style_Repeat )
381 nItemBits |= ::com::sun::star::ui::ItemStyle::REPEAT;
382 else if ( nHashCode == m_nHashCode_Style_DropDownOnly )
383 nItemBits |= ::com::sun::star::ui::ItemStyle::DROPDOWN_ONLY;
384 else if ( nHashCode == m_nHashCode_Style_DropDown )
385 nItemBits |= ::com::sun::star::ui::ItemStyle::DROP_DOWN;
386 else if ( nHashCode == m_nHashCode_Style_Text )
387 nItemBits |= ::com::sun::star::ui::ItemStyle::TEXT;
388 else if ( nHashCode == m_nHashCode_Style_Image )
389 nItemBits |= ::com::sun::star::ui::ItemStyle::ICON;
392 while ( nIndex >= 0 );
394 break;
395 case TB_ATTRIBUTE_USER:
396 case TB_ATTRIBUTE_WIDTH:
397 default:
398 break;
401 } // for
403 if ( !bAttributeURL )
405 OUString aErrorMessage = getErrorLineString();
406 aErrorMessage += "Required attribute toolbar:url must have a value!";
407 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
410 if ( !aCommandURL.isEmpty() )
412 Sequence< PropertyValue > aToolbarItemProp( 7 );
413 aToolbarItemProp[0].Name = m_aCommandURL;
414 aToolbarItemProp[1].Name = m_aHelpURL;
415 aToolbarItemProp[2].Name = m_aLabel;
416 aToolbarItemProp[3].Name = m_aType;
417 aToolbarItemProp[4].Name = m_aStyle;
418 aToolbarItemProp[5].Name = m_aIsVisible;
419 aToolbarItemProp[6].Name = m_aTooltip;
421 //fix for fdo#39370
422 /// check whether RTL interface or not
423 if(AllSettings::GetLayoutRTL()){
424 if (aCommandURL == ".uno:ParaLeftToRight")
425 aCommandURL = ".uno:ParaRightToLeft";
426 else if (aCommandURL == ".uno:ParaRightToLeft")
427 aCommandURL = ".uno:ParaLeftToRight";
428 else if (aCommandURL == ".uno:LeftPara")
429 aCommandURL = ".uno:RightPara";
430 else if (aCommandURL == ".uno:RightPara")
431 aCommandURL = ".uno:LeftPara";
432 else if (aCommandURL == ".uno:AlignLeft")
433 aCommandURL = ".uno:AlignRight";
434 else if (aCommandURL == ".uno:AlignRight")
435 aCommandURL = ".uno:AlignLeft";
438 aToolbarItemProp[0].Value <<= aCommandURL;
439 aToolbarItemProp[1].Value <<= aHelpURL;
440 aToolbarItemProp[2].Value <<= aLabel;
441 aToolbarItemProp[3].Value = makeAny( ::com::sun::star::ui::ItemType::DEFAULT );
442 aToolbarItemProp[4].Value <<= nItemBits;
443 aToolbarItemProp[5].Value <<= bVisible;
444 aToolbarItemProp[6].Value <<= aTooltip;
446 m_rItemContainer->insertByIndex( m_rItemContainer->getCount(), makeAny( aToolbarItemProp ) );
449 break;
451 case TB_ELEMENT_TOOLBARSPACE:
453 if ( m_bToolBarSeparatorStartFound ||
454 m_bToolBarBreakStartFound ||
455 m_bToolBarSpaceStartFound ||
456 m_bToolBarItemStartFound )
458 OUString aErrorMessage = getErrorLineString();
459 aErrorMessage += "Element toolbar:toolbarspace is not a container!";
460 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
463 m_bToolBarSpaceStartFound = true;
465 Sequence< PropertyValue > aToolbarItemProp( 2 );
466 aToolbarItemProp[0].Name = m_aCommandURL;
467 aToolbarItemProp[1].Name = m_aType;
469 aToolbarItemProp[0].Value <<= OUString();
470 aToolbarItemProp[1].Value <<= ::com::sun::star::ui::ItemType::SEPARATOR_SPACE;
472 m_rItemContainer->insertByIndex( m_rItemContainer->getCount(), makeAny( aToolbarItemProp ) );
474 break;
476 case TB_ELEMENT_TOOLBARBREAK:
478 if ( m_bToolBarSeparatorStartFound ||
479 m_bToolBarBreakStartFound ||
480 m_bToolBarSpaceStartFound ||
481 m_bToolBarItemStartFound )
483 OUString aErrorMessage = getErrorLineString();
484 aErrorMessage += "Element toolbar:toolbarbreak is not a container!";
485 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
488 m_bToolBarBreakStartFound = true;
490 Sequence< PropertyValue > aToolbarItemProp( 2 );
491 aToolbarItemProp[0].Name = m_aCommandURL;
492 aToolbarItemProp[1].Name = m_aType;
494 aToolbarItemProp[0].Value <<= OUString();
495 aToolbarItemProp[1].Value <<= ::com::sun::star::ui::ItemType::SEPARATOR_LINEBREAK;
497 m_rItemContainer->insertByIndex( m_rItemContainer->getCount(), makeAny( aToolbarItemProp ) );
499 break;
501 case TB_ELEMENT_TOOLBARSEPARATOR:
503 if ( m_bToolBarSeparatorStartFound ||
504 m_bToolBarBreakStartFound ||
505 m_bToolBarSpaceStartFound ||
506 m_bToolBarItemStartFound )
508 OUString aErrorMessage = getErrorLineString();
509 aErrorMessage += "Element toolbar:toolbarseparator is not a container!";
510 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
513 m_bToolBarSeparatorStartFound = true;
515 Sequence< PropertyValue > aToolbarItemProp( 2 );
516 aToolbarItemProp[0].Name = m_aCommandURL;
517 aToolbarItemProp[1].Name = m_aType;
519 aToolbarItemProp[0].Value <<= OUString();
520 aToolbarItemProp[1].Value <<= ::com::sun::star::ui::ItemType::SEPARATOR_LINE;
522 m_rItemContainer->insertByIndex( m_rItemContainer->getCount(), makeAny( aToolbarItemProp ) );
524 break;
526 default:
527 break;
532 void SAL_CALL OReadToolBoxDocumentHandler::endElement(const OUString& aName)
533 throw( SAXException, RuntimeException, std::exception )
535 SolarMutexGuard g;
537 ToolBoxHashMap::const_iterator pToolBoxEntry = m_aToolBoxMap.find( aName );
538 if ( pToolBoxEntry != m_aToolBoxMap.end() )
540 switch ( pToolBoxEntry->second )
542 case TB_ELEMENT_TOOLBAR:
544 if ( !m_bToolBarStartFound )
546 OUString aErrorMessage = getErrorLineString();
547 aErrorMessage += "End element 'toolbar' found, but no start element 'toolbar'";
548 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
551 m_bToolBarStartFound = false;
553 break;
555 case TB_ELEMENT_TOOLBARITEM:
557 if ( !m_bToolBarItemStartFound )
559 OUString aErrorMessage = getErrorLineString();
560 aErrorMessage += "End element 'toolbar:toolbaritem' found, but no start element 'toolbar:toolbaritem'";
561 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
564 m_bToolBarItemStartFound = false;
566 break;
568 case TB_ELEMENT_TOOLBARBREAK:
570 if ( !m_bToolBarBreakStartFound )
572 OUString aErrorMessage = getErrorLineString();
573 aErrorMessage += "End element 'toolbar:toolbarbreak' found, but no start element 'toolbar:toolbarbreak'";
574 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
577 m_bToolBarBreakStartFound = false;
579 break;
581 case TB_ELEMENT_TOOLBARSPACE:
583 if ( !m_bToolBarSpaceStartFound )
585 OUString aErrorMessage = getErrorLineString();
586 aErrorMessage += "End element 'toolbar:toolbarspace' found, but no start element 'toolbar:toolbarspace'";
587 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
590 m_bToolBarSpaceStartFound = false;
592 break;
594 case TB_ELEMENT_TOOLBARSEPARATOR:
596 if ( !m_bToolBarSeparatorStartFound )
598 OUString aErrorMessage = getErrorLineString();
599 aErrorMessage += "End element 'toolbar:toolbarseparator' found, but no start element 'toolbar:toolbarseparator'";
600 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
603 m_bToolBarSeparatorStartFound = false;
605 break;
607 default:
608 break;
613 void SAL_CALL OReadToolBoxDocumentHandler::characters(const OUString&)
614 throw( SAXException, RuntimeException, std::exception )
618 void SAL_CALL OReadToolBoxDocumentHandler::ignorableWhitespace(const OUString&)
619 throw( SAXException, RuntimeException, std::exception )
623 void SAL_CALL OReadToolBoxDocumentHandler::processingInstruction(
624 const OUString& /*aTarget*/, const OUString& /*aData*/ )
625 throw( SAXException, RuntimeException, std::exception )
629 void SAL_CALL OReadToolBoxDocumentHandler::setDocumentLocator(
630 const Reference< XLocator > &xLocator)
631 throw( SAXException, RuntimeException, std::exception )
633 SolarMutexGuard g;
635 m_xLocator = xLocator;
638 OUString OReadToolBoxDocumentHandler::getErrorLineString()
640 SolarMutexGuard g;
642 if ( m_xLocator.is() )
644 char buffer[32];
645 snprintf( buffer, sizeof(buffer), "Line: %ld - ", static_cast<long>( m_xLocator->getLineNumber() ));
646 return OUString::createFromAscii( buffer );
648 else
649 return OUString();
652 // OWriteToolBoxDocumentHandler
654 OWriteToolBoxDocumentHandler::OWriteToolBoxDocumentHandler(
655 const Reference< XIndexAccess >& rItemAccess,
656 Reference< XDocumentHandler >& rWriteDocumentHandler ) :
657 m_xWriteDocumentHandler( rWriteDocumentHandler ),
658 m_rItemAccess( rItemAccess )
660 ::comphelper::AttributeList* pList = new ::comphelper::AttributeList;
661 m_xEmptyList = Reference< XAttributeList >( (XAttributeList *) pList, UNO_QUERY );
662 m_aAttributeType = ATTRIBUTE_TYPE_CDATA;
663 m_aXMLXlinkNS = XMLNS_XLINK_PREFIX;
664 m_aXMLToolbarNS = XMLNS_TOOLBAR_PREFIX;
667 OWriteToolBoxDocumentHandler::~OWriteToolBoxDocumentHandler()
671 void OWriteToolBoxDocumentHandler::WriteToolBoxDocument() throw
672 ( SAXException, RuntimeException )
674 SolarMutexGuard g;
676 m_xWriteDocumentHandler->startDocument();
678 // write DOCTYPE line!
679 Reference< XExtendedDocumentHandler > xExtendedDocHandler( m_xWriteDocumentHandler, UNO_QUERY );
680 if ( xExtendedDocHandler.is() )
682 xExtendedDocHandler->unknown( OUString( TOOLBAR_DOCTYPE ) );
683 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
686 OUString aUIName;
687 Reference< XPropertySet > xPropSet( m_rItemAccess, UNO_QUERY );
688 if ( xPropSet.is() )
692 xPropSet->getPropertyValue("UIName") >>= aUIName;
694 catch ( const UnknownPropertyException& )
699 ::comphelper::AttributeList* pList = new ::comphelper::AttributeList;
700 Reference< XAttributeList > xList( (XAttributeList *) pList , UNO_QUERY );
702 pList->AddAttribute( OUString( ATTRIBUTE_XMLNS_TOOLBAR ),
703 m_aAttributeType,
704 OUString( XMLNS_TOOLBAR ) );
706 pList->AddAttribute( OUString( ATTRIBUTE_XMLNS_XLINK ),
707 m_aAttributeType,
708 OUString( XMLNS_XLINK ) );
710 if ( !aUIName.isEmpty() )
711 pList->AddAttribute( m_aXMLToolbarNS + ATTRIBUTE_UINAME,
712 m_aAttributeType,
713 aUIName );
715 m_xWriteDocumentHandler->startElement( OUString( ELEMENT_NS_TOOLBAR ), pList );
716 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
718 sal_Int32 nItemCount = m_rItemAccess->getCount();
719 Any aAny;
721 for ( sal_Int32 nItemPos = 0; nItemPos < nItemCount; nItemPos++ )
723 Sequence< PropertyValue > aProps;
724 aAny = m_rItemAccess->getByIndex( nItemPos );
725 if ( aAny >>= aProps )
727 OUString aCommandURL;
728 OUString aLabel;
729 OUString aHelpURL;
730 OUString aTooltip;
731 bool bVisible( true );
732 sal_Int16 nType( ::com::sun::star::ui::ItemType::DEFAULT );
733 sal_Int16 nWidth( 0 );
734 sal_Int16 nStyle( 0 );
736 ExtractToolbarParameters( aProps, aCommandURL, aLabel, aHelpURL, aTooltip, nStyle, nWidth, bVisible, nType );
737 if ( nType == ::com::sun::star::ui::ItemType::DEFAULT )
738 WriteToolBoxItem( aCommandURL, aLabel, aHelpURL, aTooltip, nStyle, nWidth, bVisible );
739 else if ( nType == ::com::sun::star::ui::ItemType::SEPARATOR_SPACE )
740 WriteToolBoxSpace();
741 else if ( nType == ::com::sun::star::ui::ItemType::SEPARATOR_LINE )
742 WriteToolBoxSeparator();
743 else if ( nType == ::com::sun::star::ui::ItemType::SEPARATOR_LINEBREAK )
744 WriteToolBoxBreak();
748 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
749 m_xWriteDocumentHandler->endElement( OUString( ELEMENT_NS_TOOLBAR ) );
750 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
751 m_xWriteDocumentHandler->endDocument();
754 // protected member functions
756 void OWriteToolBoxDocumentHandler::WriteToolBoxItem(
757 const OUString& rCommandURL,
758 const OUString& rLabel,
759 const OUString& rHelpURL,
760 const OUString& rTooltip,
761 sal_Int16 nStyle,
762 sal_Int16 nWidth,
763 bool bVisible )
764 throw ( SAXException, RuntimeException )
766 ::comphelper::AttributeList* pList = new ::comphelper::AttributeList;
767 Reference< XAttributeList > xList( (XAttributeList *) pList , UNO_QUERY );
769 if ( m_aAttributeURL.isEmpty() )
771 m_aAttributeURL = m_aXMLXlinkNS;
772 m_aAttributeURL += OUString( ATTRIBUTE_URL );
775 // save required attribute (URL)
776 pList->AddAttribute( m_aAttributeURL, m_aAttributeType, rCommandURL );
778 if ( !rLabel.isEmpty() )
780 pList->AddAttribute( m_aXMLToolbarNS + ATTRIBUTE_TEXT,
781 m_aAttributeType,
782 rLabel );
785 if ( !bVisible )
787 pList->AddAttribute( m_aXMLToolbarNS + ATTRIBUTE_VISIBLE,
788 m_aAttributeType,
789 OUString( ATTRIBUTE_BOOLEAN_FALSE ) );
792 if ( !rHelpURL.isEmpty() )
794 pList->AddAttribute( m_aXMLToolbarNS + ATTRIBUTE_HELPID,
795 m_aAttributeType,
796 rHelpURL );
799 if ( !rTooltip.isEmpty() )
801 pList->AddAttribute( m_aXMLToolbarNS + ATTRIBUTE_TOOLTIP,
802 m_aAttributeType,
803 rTooltip );
806 if ( nStyle > 0 )
808 OUString aValue;
809 ToolboxStyleItem* pStyle = Styles;
811 for ( sal_Int32 nIndex = 0; nIndex < nStyleItemEntries; ++nIndex, ++pStyle )
813 if ( nStyle & pStyle->nBit )
815 if ( !aValue.isEmpty() )
816 aValue = aValue.concat( OUString( " " ) );
817 aValue += OUString::createFromAscii( pStyle->attrName );
820 pList->AddAttribute( m_aXMLToolbarNS + ATTRIBUTE_ITEMSTYLE,
821 m_aAttributeType,
822 aValue );
825 if ( nWidth > 0 )
827 pList->AddAttribute( m_aXMLToolbarNS + ATTRIBUTE_WIDTH,
828 m_aAttributeType,
829 OUString::number( nWidth) );
832 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
833 m_xWriteDocumentHandler->startElement( OUString( ELEMENT_NS_TOOLBARITEM ), xList );
834 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
835 m_xWriteDocumentHandler->endElement( OUString( ELEMENT_NS_TOOLBARITEM ) );
838 void OWriteToolBoxDocumentHandler::WriteToolBoxSpace() throw
839 ( SAXException, RuntimeException )
841 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
842 m_xWriteDocumentHandler->startElement( OUString( ELEMENT_NS_TOOLBARSPACE ), m_xEmptyList );
843 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
844 m_xWriteDocumentHandler->endElement( OUString( ELEMENT_NS_TOOLBARSPACE ) );
847 void OWriteToolBoxDocumentHandler::WriteToolBoxBreak() throw
848 ( SAXException, RuntimeException )
850 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
851 m_xWriteDocumentHandler->startElement( OUString( ELEMENT_NS_TOOLBARBREAK ), m_xEmptyList );
852 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
853 m_xWriteDocumentHandler->endElement( OUString( ELEMENT_NS_TOOLBARBREAK ) );
856 void OWriteToolBoxDocumentHandler::WriteToolBoxSeparator() throw
857 ( SAXException, RuntimeException )
859 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
860 m_xWriteDocumentHandler->startElement( OUString( ELEMENT_NS_TOOLBARSEPARATOR ), m_xEmptyList );
861 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
862 m_xWriteDocumentHandler->endElement( OUString( ELEMENT_NS_TOOLBARSEPARATOR ) );
865 } // namespace framework
867 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */