update dev300-m58
[ooovba.git] / toolkit / source / layout / core / import.cxx
blob64ffb3316735f6e11b103b9684ec4520c309d1dc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile$
11 * $Revision$
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 #include "import.hxx"
34 #include <com/sun/star/awt/XButton.hpp>
35 #include <com/sun/star/awt/XDialog2.hpp>
36 #include <vcl/image.hxx>
37 #include <tools/debug.hxx>
38 #include <layout/layout.hxx>
40 #include "root.hxx"
41 #include "helper.hxx"
42 #include "dialogbuttonhbox.hxx"
45 #define XMLNS_LAYOUT_URI "http://openoffice.org/2007/layout"
46 #define XMLNS_CONTAINER_URI "http://openoffice.org/2007/layout/container"
48 namespace layoutimpl
50 using namespace css;
52 using ::rtl::OUString;
54 ElementBase::~ElementBase()
55 SAL_THROW( () )
57 //delete mpImport;
58 //mpImport = 0;
61 //** parser
62 WidgetElement::WidgetElement ( sal_Int32 nUid, const OUString &rName,
63 uno::Reference <xml::input::XAttributes> const &attributes,
64 ElementBase *pParent,
65 ImportContext *pImport)
66 SAL_THROW (())
67 : ElementBase( nUid, rName, attributes, pParent, pImport )
69 OUString name = rName.toAsciiLowerCase();
71 PropList aProps;
72 propsFromAttributes( attributes, aProps, pImport->XMLNS_LAYOUT_UID );
74 OUString aId;
75 findAndRemove( "id", aProps, aId );
76 OUString aLang;
77 findAndRemove( "xml-lang", aProps, aLang );
80 //DEBUG
81 uno::Reference< awt::XLayoutConstrains > xParent;
82 if ( pParent )
83 xParent = ((WidgetElement *) pParent)->mpWidget->getPeer();
86 mpWidget = pImport->mrRoot.create( aId, name,
87 getAttributeProps( aProps ), uno::Reference< awt::XLayoutContainer >( xParent, uno::UNO_QUERY ) );
91 // TODO: handle with non-existing widgets
93 mpWidget->setProperties( aProps );
95 uno::Reference< awt::XDialog2 > xDialog( mpWidget->getPeer(), uno::UNO_QUERY );
96 if ( xDialog.is() )
98 OUString aTitle;
99 if ( findAndRemove( "title", aProps, aTitle ) )
101 OSL_TRACE("Setting title: %s", OUSTRING_CSTR( aTitle ) );
102 xDialog->setTitle( aTitle );
104 OUString aHelpId;
105 if ( findAndRemove( "help-id", aProps, aHelpId ) )
107 OSL_TRACE("Setting help-id: %s", OUSTRING_CSTR( aHelpId ) );
108 xDialog->setHelpId( aHelpId.toInt32 () );
110 } // DEBUG:
111 else if ( pParent == NULL )
113 DBG_ERROR( "Fatal error: top node isn't a dialog" );
116 #if 0
117 // Hack moved to proplist.cxx
118 OUString aGraphic;
119 if ( findAndRemove( "graphic", aProps, aGraphic ) )
120 //if ( layout::FixedImage *i = dynamic_cast<layout::FixedImage *> ( mpWidget->getPeer().get() ) )
121 // FIXME: huh? XImageProducer::complete( XImageConsumer )
122 //i->setImage( Image( loadGraphic( OUSTRING_CSTR( aGraphic ) ) ) );
123 mpWidget->setProperty( OUString::createFromAscii( "graphic" ),
124 loadGraphic( OUSTRING_CSTR( aGraphic ) ) );
125 #endif
127 OUString aOrdering;
128 if ( findAndRemove( "ordering", aProps, aOrdering ) )
129 if ( DialogButtonHBox *b = dynamic_cast<DialogButtonHBox *> ( mpWidget->getPeer().get() ) )
130 b->setOrdering ( aOrdering );
132 bool bSetRadioGroup;
133 OUString aRadioGroup;
134 bSetRadioGroup = findAndRemove( "radiogroup", aProps, aRadioGroup );
136 mpWidget->setProperties( aProps );
138 // we need to add radio buttons to the group after their properties are
139 // set, so we can check if they should be the one selected by default or not.
140 // And the state changed event isn't fired when changing properties.
142 uno::Reference< awt::XRadioButton > xRadio( mpWidget->getPeer(), uno::UNO_QUERY );
143 if ( xRadio.is() )
145 if (!bSetRadioGroup)
146 aRadioGroup = OUString::createFromAscii ("default");
147 pImport->mxRadioGroups.addItem( aRadioGroup, xRadio );
151 WidgetElement::~WidgetElement()
153 //delete mpWidget;
154 //mpWidget = 0;
157 uno::Reference <xml::input::XElement>
158 WidgetElement::startChildElement ( sal_Int32 nUid, OUString const &name,
159 uno::Reference <xml::input::XAttributes> const &attributes )
160 throw( xml::sax::SAXException, uno::RuntimeException )
162 // Adding a child to the widget
163 WidgetElement *pChild = new WidgetElement ( nUid, name, attributes, this, mpImport );
165 if ( !mpWidget->addChild( pChild->mpWidget ) )
167 DBG_ERROR2( "ERROR: cannot add %s to container %s, container full", OUSTRING_CSTR( name ), OUSTRING_CSTR( getLocalName() ) );
168 throw xml::sax::SAXException();
171 PropList aProps;
172 propsFromAttributes( attributes, aProps, mpImport->XMLNS_CONTAINER_UID );
173 mpWidget->setChildProperties( pChild->mpWidget, aProps );
175 return pChild;
178 // Support Ivo Hinkelmann's move label/text/title attribute to CONTENT
179 // transex3 hack.
180 void SAL_CALL
181 WidgetElement::characters( OUString const& rChars )
182 throw (xml::sax::SAXException, uno::RuntimeException)
184 if ( mpWidget && rChars.trim().getLength() )
186 uno::Reference< awt::XDialog2 > xDialog( mpWidget->getPeer(), uno::UNO_QUERY );
187 uno::Reference< awt::XButton > xButton( mpWidget->getPeer(), uno::UNO_QUERY );
188 if ( xDialog.is() )
189 xDialog->setTitle( rChars );
190 else if ( xButton.is() )
191 mpWidget->setProperty( OUString::createFromAscii( "label" ), rChars );
192 else
193 mpWidget->setProperty( OUString::createFromAscii( "text" ), rChars );
196 // ---- ElementBase ----
198 ElementBase::ElementBase( sal_Int32 nUid, OUString const & rLocalName,
199 uno::Reference< xml::input::XAttributes > const & xAttributes,
200 ElementBase* pParent,
201 ImportContext* pImport )
202 SAL_THROW(())
203 : mpImport( pImport )
204 , mpParent( pParent )
205 , mnUid( nUid )
206 , maLocalName( rLocalName )
207 , mxAttributes( xAttributes )
211 // ---- ImportContext ----
213 void ImportContext::startDocument(
214 uno::Reference< xml::input::XNamespaceMapping > const & xNamespaceMapping )
215 throw (xml::sax::SAXException, uno::RuntimeException)
217 XMLNS_LAYOUT_UID = xNamespaceMapping->getUidByUri(
218 OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_LAYOUT_URI ) ) );
219 XMLNS_CONTAINER_UID = xNamespaceMapping->getUidByUri(
220 OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_CONTAINER_URI ) ) );
223 ToplevelElement::ToplevelElement (OUString const &rName,
224 uno::Reference <xml::input::XAttributes> const &xAttributes,
225 ImportContext *pImport)
226 SAL_THROW(())
227 : WidgetElement( 0, rName, xAttributes, NULL, pImport )
231 ToplevelElement::~ToplevelElement()
235 uno::Reference< xml::input::XElement > ImportContext::startRootElement(
236 sal_Int32 nUid, OUString const & rLocalName,
237 uno::Reference< xml::input::XAttributes > const & xAttributes )
238 throw (xml::sax::SAXException, uno::RuntimeException)
240 if ( XMLNS_LAYOUT_UID != nUid )
241 throw xml::sax::SAXException(
242 OUString( RTL_CONSTASCII_USTRINGPARAM( "invalid namespace!" ) ),
243 uno::Reference< uno::XInterface >(), uno::Any() );
244 return new ToplevelElement( rLocalName, xAttributes, this );
247 RadioGroups::RadioGroups()
251 void RadioGroups::addItem( rtl::OUString id, uno::Reference< awt::XRadioButton > xRadio )
252 throw (uno::RuntimeException)
254 if ( ! xRadio.is() )
255 throw uno::RuntimeException();
257 uno::Reference< RadioGroup > group;
258 RadioGroupsMap::iterator it = mxRadioGroups.find( id );
259 if ( it == mxRadioGroups.end() )
261 group = uno::Reference< RadioGroup > ( new RadioGroup() );
262 mxRadioGroups [id] = group;
264 else
265 group = it->second;
266 group->addItem( xRadio );
269 RadioGroups::RadioGroup::RadioGroup()
273 void RadioGroups::RadioGroup::addItem( uno::Reference< awt::XRadioButton > xRadio )
275 if ( ! mxSelectedRadio.is() )
277 xRadio->setState( true );
278 mxSelectedRadio = xRadio;
280 else if ( xRadio->getState() )
282 #if 1
283 xRadio->setState( false );
284 #else // huh, why select last added?
285 mxSelectedRadio->setState( false );
286 mxSelectedRadio = xRadio;
287 #endif
290 // TOO late: actionPerformed is called before itemStateChanged.
291 // If client code (wrongly?) uses actionPerformed, it will see
292 // the previous RadioButtons' state.
293 xRadio->addItemListener( this );
295 uno::Reference< awt::XButton > xButton = uno::Reference< awt::XButton > ( xRadio, uno::UNO_QUERY );
296 xButton->addActionListener( this );
298 mxRadios.push_back (xRadio);
301 void RadioGroups::RadioGroup::handleSelected ()
302 throw (uno::RuntimeException)
304 for ( RadioButtonsList::iterator it = mxRadios.begin();
305 it != mxRadios.end(); it++ )
306 if ( *it != mxSelectedRadio && (*it)->getState() )
308 mxSelectedRadio->setState( false );
309 mxSelectedRadio = *it;
310 break;
314 // awt::XItemListener
315 void RadioGroups::RadioGroup::itemStateChanged( const awt::ItemEvent& e )
316 throw (uno::RuntimeException)
318 // TOO late: actionPerformed is called before itemStateChanged.
319 // If client code (wrongly?) uses actionPerformed, it will see
320 // the previous RadioButtons' state.
322 // Need this for initialization, though.
323 if ( e.Selected )
324 handleSelected ();
327 // awt::XActionListener
328 void RadioGroups::RadioGroup::actionPerformed( const awt::ActionEvent& )
329 throw (uno::RuntimeException)
331 handleSelected ();
334 // lang::XEventListener
335 void SAL_CALL RadioGroups::RadioGroup::disposing( const lang::EventObject& )
336 throw (uno::RuntimeException)
340 } // namespace layoutimpl