xref: /trunk/main/toolkit/source/layout/core/import.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*b0724fc6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b0724fc6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b0724fc6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b0724fc6SAndrew Rist  * distributed with this work for additional information
6*b0724fc6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b0724fc6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b0724fc6SAndrew Rist  * "License"); you may not use this file except in compliance
9*b0724fc6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*b0724fc6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*b0724fc6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b0724fc6SAndrew Rist  * software distributed under the License is distributed on an
15*b0724fc6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b0724fc6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b0724fc6SAndrew Rist  * specific language governing permissions and limitations
18*b0724fc6SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*b0724fc6SAndrew Rist  *************************************************************/
21*b0724fc6SAndrew Rist 
22*b0724fc6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "import.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <com/sun/star/awt/XButton.hpp>
27cdf0e10cSrcweir #include <com/sun/star/awt/XDialog2.hpp>
28cdf0e10cSrcweir #include <vcl/image.hxx>
29cdf0e10cSrcweir #include <tools/debug.hxx>
30cdf0e10cSrcweir #include <layout/layout.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include "root.hxx"
33cdf0e10cSrcweir #include "helper.hxx"
34cdf0e10cSrcweir #include "dialogbuttonhbox.hxx"
35cdf0e10cSrcweir 
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #define XMLNS_LAYOUT_URI    "http://openoffice.org/2007/layout"
38cdf0e10cSrcweir #define XMLNS_CONTAINER_URI "http://openoffice.org/2007/layout/container"
39cdf0e10cSrcweir 
40cdf0e10cSrcweir namespace layoutimpl
41cdf0e10cSrcweir {
42cdf0e10cSrcweir using namespace css;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir using ::rtl::OUString;
45cdf0e10cSrcweir 
~ElementBase()46cdf0e10cSrcweir ElementBase::~ElementBase()
47cdf0e10cSrcweir SAL_THROW( () )
48cdf0e10cSrcweir {
49cdf0e10cSrcweir     //delete mpImport;
50cdf0e10cSrcweir     //mpImport = 0;
51cdf0e10cSrcweir }
52cdf0e10cSrcweir 
53cdf0e10cSrcweir //** parser
WidgetElement(sal_Int32 nUid,const OUString & rName,uno::Reference<xml::input::XAttributes> const & attributes,ElementBase * pParent,ImportContext * pImport)54cdf0e10cSrcweir WidgetElement::WidgetElement ( sal_Int32 nUid, const OUString &rName,
55cdf0e10cSrcweir                                uno::Reference <xml::input::XAttributes> const &attributes,
56cdf0e10cSrcweir                                ElementBase *pParent,
57cdf0e10cSrcweir                                ImportContext *pImport)
58cdf0e10cSrcweir SAL_THROW (())
59cdf0e10cSrcweir : ElementBase( nUid, rName, attributes, pParent, pImport )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir     OUString name = rName.toAsciiLowerCase();
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     PropList aProps;
64cdf0e10cSrcweir     propsFromAttributes( attributes, aProps, pImport->XMLNS_LAYOUT_UID );
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     OUString aId;
67cdf0e10cSrcweir     findAndRemove( "id", aProps, aId );
68cdf0e10cSrcweir     OUString aLang;
69cdf0e10cSrcweir     findAndRemove( "xml-lang", aProps, aLang );
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     {
72cdf0e10cSrcweir //DEBUG
73cdf0e10cSrcweir         uno::Reference< awt::XLayoutConstrains > xParent;
74cdf0e10cSrcweir         if ( pParent )
75cdf0e10cSrcweir             xParent = ((WidgetElement *) pParent)->mpWidget->getPeer();
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 
78cdf0e10cSrcweir         mpWidget = pImport->mrRoot.create( aId, name,
79cdf0e10cSrcweir                                            getAttributeProps( aProps ), uno::Reference< awt::XLayoutContainer >( xParent, uno::UNO_QUERY ) );
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     // TODO: handle with non-existing widgets
84cdf0e10cSrcweir 
85cdf0e10cSrcweir     mpWidget->setProperties( aProps );
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     uno::Reference< awt::XDialog2 > xDialog( mpWidget->getPeer(), uno::UNO_QUERY );
88cdf0e10cSrcweir     if ( xDialog.is() )
89cdf0e10cSrcweir     {
90cdf0e10cSrcweir         OUString aTitle;
91cdf0e10cSrcweir         if ( findAndRemove( "title", aProps, aTitle ) )
92cdf0e10cSrcweir         {
93cdf0e10cSrcweir             OSL_TRACE("Setting title: %s", OUSTRING_CSTR( aTitle ) );
94cdf0e10cSrcweir             xDialog->setTitle( aTitle );
95cdf0e10cSrcweir         }
96cdf0e10cSrcweir         OUString aHelpId;
97cdf0e10cSrcweir         if ( findAndRemove( "help-id", aProps, aHelpId ) )
98cdf0e10cSrcweir         {
99cdf0e10cSrcweir             OSL_TRACE("Setting help-id: %s", OUSTRING_CSTR( aHelpId ) );
100cdf0e10cSrcweir             xDialog->setHelpId( aHelpId );
101cdf0e10cSrcweir         }
102cdf0e10cSrcweir     } // DEBUG:
103cdf0e10cSrcweir     else if ( pParent == NULL )
104cdf0e10cSrcweir     {
105cdf0e10cSrcweir         DBG_ERROR( "Fatal error: top node isn't a dialog" );
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     OUString aOrdering;
109cdf0e10cSrcweir     if ( findAndRemove( "ordering", aProps, aOrdering ) )
110cdf0e10cSrcweir         if ( DialogButtonHBox *b = dynamic_cast<DialogButtonHBox *> ( mpWidget->getPeer().get() ) )
111cdf0e10cSrcweir             b->setOrdering ( aOrdering );
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     bool bSetRadioGroup;
114cdf0e10cSrcweir     OUString aRadioGroup;
115cdf0e10cSrcweir     bSetRadioGroup = findAndRemove( "radiogroup", aProps, aRadioGroup );
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     mpWidget->setProperties( aProps );
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     // we need to add radio buttons to the group after their properties are
120cdf0e10cSrcweir     // set, so we can check if they should be the one selected by default or not.
121cdf0e10cSrcweir     // And the state changed event isn't fired when changing properties.
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     uno::Reference< awt::XRadioButton > xRadio( mpWidget->getPeer(), uno::UNO_QUERY );
124cdf0e10cSrcweir     if ( xRadio.is() )
125cdf0e10cSrcweir     {
126cdf0e10cSrcweir         if (!bSetRadioGroup)
127cdf0e10cSrcweir             aRadioGroup = OUString::createFromAscii ("default");
128cdf0e10cSrcweir         pImport->mxRadioGroups.addItem( aRadioGroup, xRadio );
129cdf0e10cSrcweir     }
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
~WidgetElement()132cdf0e10cSrcweir WidgetElement::~WidgetElement()
133cdf0e10cSrcweir {
134cdf0e10cSrcweir     //delete mpWidget;
135cdf0e10cSrcweir     //mpWidget = 0;
136cdf0e10cSrcweir }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir uno::Reference <xml::input::XElement>
startChildElement(sal_Int32 nUid,OUString const & name,uno::Reference<xml::input::XAttributes> const & attributes)139cdf0e10cSrcweir WidgetElement::startChildElement ( sal_Int32 nUid, OUString const &name,
140cdf0e10cSrcweir                                    uno::Reference <xml::input::XAttributes> const &attributes )
141cdf0e10cSrcweir     throw( xml::sax::SAXException, uno::RuntimeException )
142cdf0e10cSrcweir {
143cdf0e10cSrcweir     // Adding a child to the widget
144cdf0e10cSrcweir     WidgetElement *pChild = new WidgetElement ( nUid, name, attributes, this, mpImport );
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     if ( !mpWidget->addChild( pChild->mpWidget ) )
147cdf0e10cSrcweir     {
148cdf0e10cSrcweir         DBG_ERROR2( "ERROR: cannot add %s to container %s, container full", OUSTRING_CSTR( name ), OUSTRING_CSTR( getLocalName() ) );
149cdf0e10cSrcweir         throw xml::sax::SAXException();
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     PropList aProps;
153cdf0e10cSrcweir     propsFromAttributes( attributes, aProps, mpImport->XMLNS_CONTAINER_UID );
154cdf0e10cSrcweir     mpWidget->setChildProperties( pChild->mpWidget, aProps );
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     return pChild;
157cdf0e10cSrcweir }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir // Support Ivo Hinkelmann's move label/text/title attribute to CONTENT
160cdf0e10cSrcweir // transex3 hack.
161cdf0e10cSrcweir void SAL_CALL
characters(OUString const & rChars)162cdf0e10cSrcweir WidgetElement::characters( OUString const& rChars )
163cdf0e10cSrcweir     throw (xml::sax::SAXException, uno::RuntimeException)
164cdf0e10cSrcweir {
165cdf0e10cSrcweir     if ( mpWidget && rChars.trim().getLength() )
166cdf0e10cSrcweir     {
167cdf0e10cSrcweir         uno::Reference< awt::XDialog2 > xDialog( mpWidget->getPeer(), uno::UNO_QUERY );
168cdf0e10cSrcweir         uno::Reference< awt::XButton > xButton( mpWidget->getPeer(), uno::UNO_QUERY );
169cdf0e10cSrcweir         if ( xDialog.is() )
170cdf0e10cSrcweir             xDialog->setTitle( rChars );
171cdf0e10cSrcweir         else if ( xButton.is() )
172cdf0e10cSrcweir             mpWidget->setProperty( OUString::createFromAscii( "label" ), rChars );
173cdf0e10cSrcweir         else
174cdf0e10cSrcweir             mpWidget->setProperty( OUString::createFromAscii( "text" ), rChars );
175cdf0e10cSrcweir     }
176cdf0e10cSrcweir }
177cdf0e10cSrcweir // ---- ElementBase ----
178cdf0e10cSrcweir 
ElementBase(sal_Int32 nUid,OUString const & rLocalName,uno::Reference<xml::input::XAttributes> const & xAttributes,ElementBase * pParent,ImportContext * pImport)179cdf0e10cSrcweir ElementBase::ElementBase( sal_Int32 nUid, OUString const & rLocalName,
180cdf0e10cSrcweir                           uno::Reference< xml::input::XAttributes > const & xAttributes,
181cdf0e10cSrcweir                           ElementBase* pParent,
182cdf0e10cSrcweir                           ImportContext* pImport )
183cdf0e10cSrcweir SAL_THROW(())
184cdf0e10cSrcweir : mpImport( pImport )
185cdf0e10cSrcweir     , mpParent( pParent )
186cdf0e10cSrcweir     , mnUid( nUid )
187cdf0e10cSrcweir     , maLocalName( rLocalName )
188cdf0e10cSrcweir     , mxAttributes( xAttributes )
189cdf0e10cSrcweir {
190cdf0e10cSrcweir }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir // ---- ImportContext ----
193cdf0e10cSrcweir 
startDocument(uno::Reference<xml::input::XNamespaceMapping> const & xNamespaceMapping)194cdf0e10cSrcweir void ImportContext::startDocument(
195cdf0e10cSrcweir     uno::Reference< xml::input::XNamespaceMapping > const & xNamespaceMapping )
196cdf0e10cSrcweir     throw (xml::sax::SAXException, uno::RuntimeException)
197cdf0e10cSrcweir {
198cdf0e10cSrcweir     XMLNS_LAYOUT_UID = xNamespaceMapping->getUidByUri(
199cdf0e10cSrcweir         OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_LAYOUT_URI ) ) );
200cdf0e10cSrcweir     XMLNS_CONTAINER_UID = xNamespaceMapping->getUidByUri(
201cdf0e10cSrcweir         OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_CONTAINER_URI ) ) );
202cdf0e10cSrcweir }
203cdf0e10cSrcweir 
ToplevelElement(OUString const & rName,uno::Reference<xml::input::XAttributes> const & xAttributes,ImportContext * pImport)204cdf0e10cSrcweir ToplevelElement::ToplevelElement (OUString const &rName,
205cdf0e10cSrcweir                                   uno::Reference <xml::input::XAttributes> const &xAttributes,
206cdf0e10cSrcweir                                   ImportContext *pImport)
207cdf0e10cSrcweir SAL_THROW(())
208cdf0e10cSrcweir : WidgetElement( 0, rName, xAttributes, NULL, pImport )
209cdf0e10cSrcweir {
210cdf0e10cSrcweir }
211cdf0e10cSrcweir 
~ToplevelElement()212cdf0e10cSrcweir ToplevelElement::~ToplevelElement()
213cdf0e10cSrcweir {
214cdf0e10cSrcweir }
215cdf0e10cSrcweir 
startRootElement(sal_Int32 nUid,OUString const & rLocalName,uno::Reference<xml::input::XAttributes> const & xAttributes)216cdf0e10cSrcweir uno::Reference< xml::input::XElement > ImportContext::startRootElement(
217cdf0e10cSrcweir     sal_Int32 nUid, OUString const & rLocalName,
218cdf0e10cSrcweir     uno::Reference< xml::input::XAttributes > const & xAttributes )
219cdf0e10cSrcweir     throw (xml::sax::SAXException, uno::RuntimeException)
220cdf0e10cSrcweir {
221cdf0e10cSrcweir     if ( XMLNS_LAYOUT_UID != nUid )
222cdf0e10cSrcweir         throw xml::sax::SAXException(
223cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM( "invalid namespace!" ) ),
224cdf0e10cSrcweir             uno::Reference< uno::XInterface >(), uno::Any() );
225cdf0e10cSrcweir         return new ToplevelElement( rLocalName, xAttributes, this );
226cdf0e10cSrcweir }
227cdf0e10cSrcweir 
RadioGroups()228cdf0e10cSrcweir RadioGroups::RadioGroups()
229cdf0e10cSrcweir {
230cdf0e10cSrcweir }
231cdf0e10cSrcweir 
addItem(rtl::OUString id,uno::Reference<awt::XRadioButton> xRadio)232cdf0e10cSrcweir void RadioGroups::addItem( rtl::OUString id, uno::Reference< awt::XRadioButton > xRadio )
233cdf0e10cSrcweir     throw (uno::RuntimeException)
234cdf0e10cSrcweir {
235cdf0e10cSrcweir     if ( ! xRadio.is() )
236cdf0e10cSrcweir         throw uno::RuntimeException();
237cdf0e10cSrcweir 
238cdf0e10cSrcweir     uno::Reference< RadioGroup > group;
239cdf0e10cSrcweir     RadioGroupsMap::iterator it = mxRadioGroups.find( id );
240cdf0e10cSrcweir     if ( it == mxRadioGroups.end() )
241cdf0e10cSrcweir     {
242cdf0e10cSrcweir         group = uno::Reference< RadioGroup > ( new RadioGroup() );
243cdf0e10cSrcweir         mxRadioGroups [id] = group;
244cdf0e10cSrcweir     }
245cdf0e10cSrcweir     else
246cdf0e10cSrcweir         group = it->second;
247cdf0e10cSrcweir     group->addItem( xRadio );
248cdf0e10cSrcweir }
249cdf0e10cSrcweir 
RadioGroup()250cdf0e10cSrcweir RadioGroups::RadioGroup::RadioGroup()
251cdf0e10cSrcweir {
252cdf0e10cSrcweir }
253cdf0e10cSrcweir 
addItem(uno::Reference<awt::XRadioButton> xRadio)254cdf0e10cSrcweir void RadioGroups::RadioGroup::addItem( uno::Reference< awt::XRadioButton > xRadio )
255cdf0e10cSrcweir {
256cdf0e10cSrcweir     if ( ! mxSelectedRadio.is() )
257cdf0e10cSrcweir     {
258cdf0e10cSrcweir         xRadio->setState( true );
259cdf0e10cSrcweir         mxSelectedRadio = xRadio;
260cdf0e10cSrcweir     }
261cdf0e10cSrcweir     else if ( xRadio->getState() )
262cdf0e10cSrcweir     {
263cdf0e10cSrcweir #if 1
264cdf0e10cSrcweir         xRadio->setState( false );
265cdf0e10cSrcweir #else // huh, why select last added?
266cdf0e10cSrcweir       mxSelectedRadio->setState( false );
267cdf0e10cSrcweir       mxSelectedRadio = xRadio;
268cdf0e10cSrcweir #endif
269cdf0e10cSrcweir     }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir     // TOO late: actionPerformed is called before itemStateChanged.
272cdf0e10cSrcweir     // If client code (wrongly?) uses actionPerformed, it will see
273cdf0e10cSrcweir     // the previous RadioButtons' state.
274cdf0e10cSrcweir     xRadio->addItemListener( this );
275cdf0e10cSrcweir 
276cdf0e10cSrcweir     uno::Reference< awt::XButton > xButton = uno::Reference< awt::XButton > ( xRadio, uno::UNO_QUERY );
277cdf0e10cSrcweir     xButton->addActionListener( this );
278cdf0e10cSrcweir 
279cdf0e10cSrcweir     mxRadios.push_back (xRadio);
280cdf0e10cSrcweir }
281cdf0e10cSrcweir 
handleSelected()282cdf0e10cSrcweir void RadioGroups::RadioGroup::handleSelected ()
283cdf0e10cSrcweir     throw (uno::RuntimeException)
284cdf0e10cSrcweir {
285cdf0e10cSrcweir     for ( RadioButtonsList::iterator it = mxRadios.begin();
286cdf0e10cSrcweir           it != mxRadios.end(); it++ )
287cdf0e10cSrcweir         if ( *it != mxSelectedRadio && (*it)->getState() )
288cdf0e10cSrcweir         {
289cdf0e10cSrcweir             mxSelectedRadio->setState( false );
290cdf0e10cSrcweir             mxSelectedRadio = *it;
291cdf0e10cSrcweir             break;
292cdf0e10cSrcweir         }
293cdf0e10cSrcweir }
294cdf0e10cSrcweir 
295cdf0e10cSrcweir // awt::XItemListener
itemStateChanged(const awt::ItemEvent & e)296cdf0e10cSrcweir void RadioGroups::RadioGroup::itemStateChanged( const awt::ItemEvent& e )
297cdf0e10cSrcweir     throw (uno::RuntimeException)
298cdf0e10cSrcweir {
299cdf0e10cSrcweir     // TOO late: actionPerformed is called before itemStateChanged.
300cdf0e10cSrcweir     // If client code (wrongly?) uses actionPerformed, it will see
301cdf0e10cSrcweir     // the previous RadioButtons' state.
302cdf0e10cSrcweir 
303cdf0e10cSrcweir     // Need this for initialization, though.
304cdf0e10cSrcweir     if ( e.Selected )
305cdf0e10cSrcweir         handleSelected ();
306cdf0e10cSrcweir }
307cdf0e10cSrcweir 
308cdf0e10cSrcweir // awt::XActionListener
actionPerformed(const awt::ActionEvent &)309cdf0e10cSrcweir void RadioGroups::RadioGroup::actionPerformed( const awt::ActionEvent& )
310cdf0e10cSrcweir     throw (uno::RuntimeException)
311cdf0e10cSrcweir {
312cdf0e10cSrcweir     handleSelected ();
313cdf0e10cSrcweir }
314cdf0e10cSrcweir 
315cdf0e10cSrcweir // lang::XEventListener
disposing(const lang::EventObject &)316cdf0e10cSrcweir void SAL_CALL RadioGroups::RadioGroup::disposing( const lang::EventObject& )
317cdf0e10cSrcweir     throw (uno::RuntimeException)
318cdf0e10cSrcweir {
319cdf0e10cSrcweir }
320cdf0e10cSrcweir 
321cdf0e10cSrcweir } // namespace layoutimpl
322