merge the formfield patch from ooo-build
[ooovba.git] / toolkit / source / layout / core / root.cxx
blobeafafcaced12c154f831ac38c489c02ddc5bdc15
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 "root.hxx"
34 #include <cassert>
36 #include <com/sun/star/awt/WindowAttribute.hpp>
37 #include <com/sun/star/awt/XMessageBox.hpp>
38 #include <com/sun/star/awt/MessageBoxButtons.hpp>
39 #include <com/sun/star/frame/XDesktop.hpp>
40 #include <com/sun/star/awt/XMessageBoxFactory.hpp>
41 #include <com/sun/star/xml/sax/SAXParseException.hpp>
42 #include <com/sun/star/xml/sax/XParser.hpp>
44 #include "helper.hxx"
45 #include "import.hxx"
46 #include "timer.hxx"
47 #include "translate.hxx"
49 namespace layoutimpl
52 using namespace css;
53 using ::rtl::OUString;
55 LayoutRoot::LayoutRoot( const uno::Reference< lang::XMultiServiceFactory >& xFactory )
56 : mbDisposed( sal_False )
57 , mxFactory( xFactory )
58 , mpListeners( NULL )
59 , mpToplevel( NULL )
61 if ( !xFactory.is() )
62 throw uno::RuntimeException();
63 mxLayoutUnit = uno::Reference< awt::XLayoutUnit >( new LayoutUnit() );
66 LayoutRoot::~LayoutRoot()
68 // TODO: we want to delete the top level LayoutWidget...
69 ::osl::MutexGuard aGuard( maMutex );
70 if ( !mbDisposed )
72 try
74 m_refCount++; // inhibit multiple destruction
75 dispose();
77 catch( uno::Exception& )
83 void ShowMessageBox( uno::Reference< lang::XMultiServiceFactory > const& xFactory, uno::Reference< awt::XToolkit > xToolkit, OUString const& aTitle, OUString const& aMessage )
85 uno::Reference< uno::XInterface > iDesktop = xFactory->createInstance
86 ( OUString::createFromAscii( "com.sun.star.frame.Desktop" ) );
87 uno::Reference< frame::XDesktop > xDesktop ( iDesktop, uno::UNO_QUERY );
88 uno::Reference< frame::XFrame > xFrame ( xDesktop->getCurrentFrame() );
89 uno::Reference< awt::XWindow > xContainerWindow( xFrame->getContainerWindow() );
90 uno::Reference< awt::XWindowPeer > xWindowPeer( xContainerWindow, uno::UNO_QUERY_THROW );
91 uno::Reference< awt::XMessageBoxFactory > xMessageBoxFactory( xToolkit, uno::UNO_QUERY );
93 awt::Rectangle aRectangle;
94 uno::Reference< awt::XMessageBox > xMessageBox
95 = xMessageBoxFactory->createMessageBox
96 ( xWindowPeer, aRectangle, OUString::createFromAscii( "errorbox" ),
97 awt::MessageBoxButtons::BUTTONS_OK, aTitle, aMessage );
99 if ( xMessageBox.is() )
100 xMessageBox->execute();
101 //FIXME: exceptions not caught and printed at top level??
102 //else
103 //printf( "%s\n", OUSTRING_CSTR( aMessage ) );
106 void LayoutRoot::error( OUString const& message )
108 OSL_TRACE( "%s\n", OUSTRING_CSTR( message ) );
109 ShowMessageBox( mxFactory, mxToolkit,
110 OUString::createFromAscii( "Fatal error" ),
111 message );
112 throw uno::RuntimeException( message, uno::Reference< uno::XInterface >() );
115 // XInitialization
116 void SAL_CALL LayoutRoot::initialize( const uno::Sequence< uno::Any >& aArguments )
117 throw ( uno::Exception,
118 uno::RuntimeException )
120 ::osl::MutexGuard aGuard( maMutex );
122 if ( mbDisposed )
123 throw lang::DisposedException();
125 if ( mxContainer.is() ) // only 1 init ...
126 throw uno::Exception();
128 if ( !aArguments.getLength() )
129 throw lang::IllegalArgumentException();
131 OSL_ENSURE( aArguments.getLength() == 1, "Wrong arg count\n" );
133 OUString aXMLName;
134 if ( !( aArguments[0] >>= aXMLName ) )
135 throw lang::IllegalArgumentException();
137 uno::Reference< xml::sax::XParser > xParser
138 ( mxFactory->createInstance(
139 OUString::createFromAscii( "com.sun.star.xml.sax.Parser" ) ),
140 uno::UNO_QUERY );
141 OSL_ASSERT( xParser.is() );
142 if (! xParser.is())
144 throw uno::RuntimeException(
145 OUString::createFromAscii( "cannot create sax-parser component" ),
146 uno::Reference< uno::XInterface >() );
149 // FIXME: quite possibly we want to pass this in ...
150 uno::Reference< awt::XToolkit > xToolkit;
152 mxToolkit = uno::Reference< awt::XToolkit >(
153 mxFactory->createInstance(
154 OUString::createFromAscii( "com.sun.star.awt.Toolkit" ) ),
155 uno::UNO_QUERY );
157 if ( !mxToolkit.is() )
158 throw uno::RuntimeException(
159 OUString::createFromAscii( "failed to create toolkit!" ),
160 uno::Reference< uno::XInterface >() );
162 OUString aXMLFile = readRightTranslation( aXMLName );
163 uno::Reference< io::XInputStream > xStream = getFileAsStream( aXMLFile );
164 if (! xStream.is() )
165 error( OUString::createFromAscii( "Installation problem: cannot find XML file:" ) + aXMLName );
167 // error handler, entity resolver omitted
169 ImportContext *pCtx = new ImportContext( *this );
171 uno::Reference< xml::input::XRoot > xRoot( pCtx );
172 uno::Sequence < uno::Any > aArgs( 1 );
173 aArgs[0] <<= xRoot;
174 uno::Reference< xml::sax::XDocumentHandler > xDocHandler
175 (mxFactory->createInstanceWithArguments
176 ( OUString::createFromAscii( "com.sun.star.xml.input.SaxDocumentHandler" ),
177 aArgs ), uno::UNO_QUERY );
179 if (! xDocHandler.is() )
180 error( OUString::createFromAscii( "cannot find SAx handler for document type of:") + aXMLName );
182 xParser->setDocumentHandler( xDocHandler );
184 xml::sax::InputSource source;
185 source.aInputStream = xStream;
186 source.sSystemId = OUString::createFromAscii( "virtual file" );
190 xParser->parseStream( source );
192 catch ( xml::sax::SAXParseException& e )
194 OUString c = OUString::createFromAscii( ":" );
195 error( aXMLName
196 + c + OUString::valueOf( e.LineNumber )
197 + c + OUString::valueOf( e.ColumnNumber )
198 + c + OUString::createFromAscii( "Sax parse error" ) );
202 // XLayoutContainer
203 uno::Reference< awt::XLayoutContainer > LayoutRoot::getLayoutContainer() throw (uno::RuntimeException)
205 return uno::Reference< awt::XLayoutContainer >();
208 // local helper ...
209 void LayoutRoot::addItem( const OUString &rName,
210 const uno::Reference< awt::XLayoutConstrains > &xRef )
212 maItems[ rName ] = xRef;
215 // XNameAccess
216 uno::Any SAL_CALL LayoutRoot::getByName( const OUString &rName )
217 throw ( container::NoSuchElementException,
218 lang::WrappedTargetException,
219 uno::RuntimeException )
221 ::osl::MutexGuard aGuard( maMutex );
222 if ( mbDisposed )
223 throw lang::DisposedException();
225 uno::Reference< awt::XLayoutConstrains > xItem;
226 ItemHash::iterator i = maItems.find( rName );
227 if ( i != maItems.end() )
228 xItem = i->second;
229 return uno::makeAny( xItem );
232 sal_Bool SAL_CALL LayoutRoot::hasByName( const OUString &rName )
233 throw (uno::RuntimeException)
235 ::osl::MutexGuard aGuard( maMutex );
236 if ( mbDisposed ) throw lang::DisposedException();
238 ItemHash::iterator i = maItems.find( rName );
239 return i != maItems.end();
242 uno::Sequence< OUString > SAL_CALL LayoutRoot::getElementNames()
243 throw ( uno::RuntimeException )
245 ::osl::MutexGuard aGuard( maMutex );
246 if ( mbDisposed ) throw lang::DisposedException();
248 uno::Sequence< OUString > aNames( maItems.size() );
249 sal_Int32 nPos = 0;
251 for ( ItemHash::const_iterator it = maItems.begin();
252 it != maItems.end(); it++ )
253 aNames[ nPos++ ] = it->first;
255 return aNames;
258 uno::Type SAL_CALL LayoutRoot::getElementType()
259 throw ( uno::RuntimeException )
261 return getCppuType( ( const uno::Reference< awt::XLayoutConstrains >* )NULL );
264 sal_Bool SAL_CALL LayoutRoot::hasElements()
265 throw ( uno::RuntimeException )
267 ::osl::MutexGuard aGuard( maMutex );
269 if ( mbDisposed ) throw lang::DisposedException();
271 return maItems.size() > 0;
274 // XComponent
275 void SAL_CALL LayoutRoot::dispose()
276 throw ( uno::RuntimeException )
278 ::osl::MutexGuard aGuard( maMutex );
280 if ( mbDisposed ) throw lang::DisposedException();
282 if ( mpListeners )
285 lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
286 mpListeners->disposeAndClear( aSource );
287 delete mpListeners;
288 mpListeners = NULL;
291 maItems.clear();
292 mbDisposed = sal_True;
295 void SAL_CALL LayoutRoot::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
296 throw ( uno::RuntimeException )
298 ::osl::MutexGuard aGuard( maMutex );
300 if ( mbDisposed ) throw lang::DisposedException();
302 if ( !mpListeners )
303 mpListeners = new ::cppu::OInterfaceContainerHelper( maMutex );
304 mpListeners->addInterface( xListener );
307 void SAL_CALL LayoutRoot::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
308 throw ( uno::RuntimeException )
310 ::osl::MutexGuard aGuard( maMutex );
312 if ( mbDisposed ) throw lang::DisposedException();
314 if ( mpListeners )
315 mpListeners->removeInterface( xListener );
318 // builder
320 LayoutWidget *LayoutRoot::create( OUString id, const OUString unoName, long attrbs,uno::Reference< awt::XLayoutContainer > xParent )
322 LayoutWidget *pWidget = new LayoutWidget( mxToolkit, xParent, unoName, attrbs );
323 if ( !mpToplevel )
325 mpToplevel = pWidget;
326 mxWindow = uno::Reference< awt::XWindow >( pWidget->getPeer(), uno::UNO_QUERY );
327 mxContainer = pWidget->mxContainer;
329 if ( pWidget->mxContainer.is() )
330 pWidget->mxContainer->setLayoutUnit( mxLayoutUnit );
331 if ( id.getLength() )
332 maItems[ id ] = pWidget->getPeer();
333 return pWidget;
336 #if 0
337 uno::Reference< awt::XLayoutConstrains > LayoutRoot::getToplevel()
339 if ( mpToplevel )
340 return mpToplevel->getPeer();
341 return uno::Reference< awt::XLayoutConstrains > ();
344 uno::Reference< awt::XLayoutConstrains > LayoutRoot::getById( OUString id )
346 uno::Reference< awt::XLayoutConstrains > rRef = 0;
347 ItemHash::iterator it = maItems.find( id );
348 if ( it != maItems.end() )
349 rRef = it->second;
350 return rRef;
352 #endif
354 LayoutWidget::LayoutWidget( uno::Reference< awt::XToolkit > xToolkit,
355 uno::Reference< awt::XLayoutContainer > xParent,
356 OUString unoName, long attrbs )
358 while ( xParent.is() && !uno::Reference< awt::XWindow >( xParent, uno::UNO_QUERY ).is() )
360 uno::Reference< awt::XLayoutContainer > xContainer( xParent, uno::UNO_QUERY );
361 assert( xContainer.is() );
362 xParent = uno::Reference< awt::XLayoutContainer >( xContainer->getParent(), uno::UNO_QUERY );
365 mxWidget = WidgetFactory::createWidget( xToolkit, xParent, unoName, attrbs );
366 assert( mxWidget.is() );
367 mxContainer = uno::Reference< awt::XLayoutContainer >( mxWidget, uno::UNO_QUERY );
370 LayoutWidget::~LayoutWidget()
372 /* should we dispose of the references...? */
373 // at least of its children... Or should root?
376 bool LayoutWidget::addChild( LayoutWidget *pChild )
378 if ( !mxContainer.is() )
379 return false;
383 mxContainer->addChild( pChild->mxWidget );
385 catch( awt::MaxChildrenException ex )
387 return false;
389 return true;
392 void LayoutWidget::setProperties( PropList const& rProps )
394 ::layoutimpl::setProperties( mxWidget, rProps );
397 void LayoutWidget::setProperty( OUString const& attr, OUString const& value )
399 ::layoutimpl::setProperty( mxWidget, attr, value );
402 void LayoutWidget::setChildProperties( LayoutWidget *pChild,
403 PropList const& rProps )
405 uno::Reference< beans::XPropertySet > xChildPeer;
406 xChildPeer = mxContainer->getChildProperties( pChild->mxWidget );
408 if ( xChildPeer.is() )
409 ::layoutimpl::setProperties( xChildPeer, rProps );
412 } // namespace layoutimpl