1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef LAYOUT_CORE_IMPORT_HXX
25 #define LAYOUT_CORE_IMPORT_HXX
26 
27 #include <map>
28 #include <list>
29 #define _BACKWARD_BACKWARD_WARNING_H 1
30 #include <hash_map>
31 
32 
33 #include <com/sun/star/xml/input/XRoot.hpp>
34 #include <cppuhelper/implbase1.hxx>
35 #include <com/sun/star/awt/XButton.hpp>
36 #include <com/sun/star/awt/XRadioButton.hpp>
37 
38 namespace layoutimpl
39 {
40 class LayoutRoot;
41 class LayoutWidget;
42 namespace css = ::com::sun::star;
43 
44 class RadioGroups
45 {
46 public:
47     RadioGroups();
48 
49     void addItem( rtl::OUString id, css::uno::Reference< css::awt::XRadioButton > xRadio )
50         throw (css::uno::RuntimeException);
51 
52 private:
53     class RadioGroup : public ::cppu::WeakImplHelper1< css::awt::XItemListener >
54         , public ::cppu::WeakImplHelper1< css::awt::XActionListener >
55     {
56     public:
57         RadioGroup();
58         void addItem( css::uno::Reference< css::awt::XRadioButton > xRadio );
59 
60     private:
61         typedef std::list< css::uno::Reference< css::awt::XRadioButton > > RadioButtonsList;
62         RadioButtonsList mxRadios;
63         css::uno::Reference< css::awt::XRadioButton > mxSelectedRadio;
64 
65         void handleSelected ()
66             throw (css::uno::RuntimeException);
67 
68         // awt::XItemListener
69         void SAL_CALL itemStateChanged( const css::awt::ItemEvent& e )
70             throw (css::uno::RuntimeException);
71 
72         // awt::XActionListener
73         void SAL_CALL actionPerformed( const css::awt::ActionEvent& e )
74             throw (css::uno::RuntimeException);
75 
76         // lang::XEventListener
77         void SAL_CALL disposing( const css::lang::EventObject& )
78             throw (css::uno::RuntimeException);
79     };
80 
81     // each RadioGroup will stay alive after RadioGroups die with the ImportContext
82     // because they are referenced by every XRadioButton through the listener
83     typedef std::map< rtl::OUString, css::uno::Reference< RadioGroup > > RadioGroupsMap;
84     RadioGroupsMap mxRadioGroups;
85 };
86 
87 #if 0
88 // generator
89 class Widget
90 {
91 public:
92     Widget( css::uno::Reference< css::awt::XToolkit > xToolkit,
93             css::uno::Reference< css::awt::XWindow > xToplevel,
94             rtl::OUString unoName, long attrbs );
95     virtual ~Widget();
96 
97     virtual void setProperties( const PropList &rProps );
98 
99     virtual bool addChild( Widget *pChild );
100     virtual void setChildProperties( Widget *pChild, const PropList &rProps );
101 
102     inline css::uno::Reference< css::awt::XLayoutConstrains > getPeer()
103     { return mxWidget; }
104 
105     inline css::uno::Reference< css::awt::XLayoutConstrains > getContainer()
106     { return mxContainer; }
107 
108 protected:
109     css::uno::Reference< css::awt::XLayoutConstrains > mxWidget;
110     css::uno::Reference< css::awt::XLayoutContainer > mxContainer;
111 };
112 
113 class Root
114 {
115 public:
116     Root( css::uno::Reference< css::awt::XToolkit > xToolkit )
117         : mxToolkit( xToolkit ) {}
118     ~Root();
119 
120     virtual Widget *create( rtl::OUString id, const rtl::OUString unoName, long attrbs );
121 
122     css::uno::Reference< css::awt::XLayoutConstrains > getById( rtl::OUString id );
123     inline css::uno::Reference< css::awt::XLayoutConstrains > getToplevel();
124 
125 protected:
126     css::uno::Reference< css::awt::XToolkit > mxToolkit;
127     Widget *mpToplevel;
128 
129     typedef std::hash_map< rtl::OUString, css::uno::Reference< css::awt::XLayoutConstrains >,
130                            rtl::OUStringHash > ItemHash;
131     ItemHash maItems;
132 };
133 #endif
134 
135 // parser
136 class ImportContext : public ::cppu::WeakImplHelper1< css::xml::input::XRoot >
137 {
138 public:
139     sal_Int32 XMLNS_LAYOUT_UID, XMLNS_CONTAINER_UID;
140     LayoutRoot &mrRoot; // switch to XNameContainer ref ?
141     RadioGroups mxRadioGroups;
142 
143     inline ImportContext( LayoutRoot &rRoot ) SAL_THROW( () )
144         : mrRoot( rRoot ) {}
~ImportContext()145     virtual ~ImportContext() {}
146 
147     // XRoot
148     virtual void SAL_CALL startDocument(
149         css::uno::Reference< css::xml::input::XNamespaceMapping >
150         const & xNamespaceMapping )
151         throw (css::xml::sax::SAXException, css::uno::RuntimeException);
endDocument()152     virtual void SAL_CALL endDocument()
153         throw (css::xml::sax::SAXException, css::uno::RuntimeException)
154     { /* ignore */ }
processingInstruction(::rtl::OUString const &,::rtl::OUString const &)155     virtual void SAL_CALL processingInstruction(
156         ::rtl::OUString const & /* rTarget */, ::rtl::OUString const & /* rData */ )
157         throw (css::xml::sax::SAXException, css::uno::RuntimeException)
158     { /* ignore */ }
setDocumentLocator(css::uno::Reference<css::xml::sax::XLocator> const &)159     virtual void SAL_CALL setDocumentLocator(
160         css::uno::Reference< css::xml::sax::XLocator > const & /* xLocator */ )
161         throw (css::xml::sax::SAXException, css::uno::RuntimeException)
162     { /* ignore */ }
163     virtual css::uno::Reference< css::xml::input::XElement >
164     SAL_CALL startRootElement(
165         sal_Int32 nUid, ::rtl::OUString const & rLocalName,
166         css::uno::Reference<css::xml::input::XAttributes > const & xAttributes )
167         throw (css::xml::sax::SAXException, css::uno::RuntimeException);
168 };
169 
170 class ElementBase : public ::cppu::WeakImplHelper1< css::xml::input::XElement >
171 {
172 protected:
173     ImportContext *mpImport;
174 /* TODO: check if all this memebers are needed. */
175     ElementBase   *mpParent;
176     sal_Int32      mnUid;
177 
178     ::rtl::OUString maLocalName;
179     css::uno::Reference< css::xml::input::XAttributes > mxAttributes;
180 public:
181     ElementBase(
182         sal_Int32 nUid, ::rtl::OUString const & rLocalName,
183         css::uno::Reference< css::xml::input::XAttributes > const & xAttributes,
184         ElementBase * pParent, ImportContext * pImport )
185     SAL_THROW( () );
186     virtual ~ElementBase() SAL_THROW(());
187 
188     // XElement
getParent()189     virtual css::uno::Reference<css::xml::input::XElement> SAL_CALL getParent()
190         throw (css::uno::RuntimeException)
191     { return static_cast< css::xml::input::XElement * >( mpParent ); }
getLocalName()192     virtual ::rtl::OUString SAL_CALL getLocalName() throw (css::uno::RuntimeException)
193     { return maLocalName; }
getUid()194     virtual sal_Int32 SAL_CALL getUid() throw (css::uno::RuntimeException)
195     { return mnUid; }
196     virtual css::uno::Reference< css::xml::input::XAttributes >
getAttributes()197     SAL_CALL getAttributes() throw (css::uno::RuntimeException)
198     { return mxAttributes; }
199 
ignorableWhitespace(::rtl::OUString const &)200     virtual void SAL_CALL ignorableWhitespace(
201         ::rtl::OUString const & /* rWhitespaces */ )
202         throw (css::xml::sax::SAXException, css::uno::RuntimeException)
203     { /* ignore */ }
characters(::rtl::OUString const &)204     virtual void SAL_CALL characters( ::rtl::OUString const & /* rChars */ )
205         throw (css::xml::sax::SAXException, css::uno::RuntimeException)
206     { /* ignore */ }
processingInstruction(::rtl::OUString const &,::rtl::OUString const &)207     virtual void SAL_CALL processingInstruction(
208         ::rtl::OUString const & /* Target */, ::rtl::OUString const & /* Data */ )
209         throw (css::xml::sax::SAXException, css::uno::RuntimeException)
210     { /* ignore */ }
211 
212     virtual css::uno::Reference< css::xml::input::XElement >
213     SAL_CALL startChildElement(
214         sal_Int32 nUid, ::rtl::OUString const & rLocalName,
215         css::uno::Reference<css::xml::input::XAttributes > const & xAttributes )
216         throw (css::xml::sax::SAXException, css::uno::RuntimeException) = 0;
endElement()217     virtual void SAL_CALL endElement()
218         throw (css::xml::sax::SAXException, css::uno::RuntimeException)
219     { /* ignore */ }
220 };
221 
222 class WidgetElement : public ElementBase
223 {
224 protected:
225     LayoutWidget *mpWidget;
226 
227 public:
228     WidgetElement( sal_Int32 nUid, rtl::OUString const &name,
229                    css::uno::Reference< css::xml::input::XAttributes > const &attributes,
230                    ElementBase *parent, ImportContext *import ) SAL_THROW (());
231 
232     ~WidgetElement();
233 
234 
235     virtual css::uno::Reference< css::xml::input::XElement> SAL_CALL
236     startChildElement (sal_Int32 id, rtl::OUString const &name,
237                        css::uno::Reference< css::xml::input::XAttributes > const &attributes)
238         throw( css::xml::sax::SAXException, css::uno::RuntimeException );
239     virtual void SAL_CALL characters( ::rtl::OUString const & /* rChars */ )
240         throw (css::xml::sax::SAXException, css::uno::RuntimeException);
241 };
242 
243 class ToplevelElement : public WidgetElement
244 {
245 public:
246     ToplevelElement( rtl::OUString const &name,
247                      css::uno::Reference< css::xml::input::XAttributes > const &attributes,
248                      ImportContext *import ) SAL_THROW (());
249     ~ToplevelElement();
250 };
251 
252 
253 } // namespace layoutimpl
254 
255 #endif /* LAYOUT_CORE_IMPORT_HXX */
256