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 #include "common.hxx"
25 #include "misc.hxx"
26 #include <xmlscript/xmldlg_imexp.hxx>
27 #include <xmlscript/xml_helper.hxx>
28 #include <osl/diagnose.h>
29 #include <com/sun/star/xml/sax/XAttributeList.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <com/sun/star/beans/XPropertyState.hpp>
32 #include <com/sun/star/awt/FontDescriptor.hpp>
33 #include <com/sun/star/awt/FontEmphasisMark.hpp>
34 #include <com/sun/star/awt/FontRelief.hpp>
35 #include <vector>
36 
37 
38 namespace css = ::com::sun::star;
39 
40 namespace xmlscript
41 {
42 
43 struct Style
44 {
45     sal_uInt32 _backgroundColor;
46     sal_uInt32 _textColor;
47     sal_uInt32 _textLineColor;
48     sal_Int16 _border;
49     sal_Int32 _borderColor;
50     css::awt::FontDescriptor _descr;
51     sal_uInt16 _fontRelief;
52     sal_uInt16 _fontEmphasisMark;
53     sal_uInt32 _fillColor;
54     sal_Int16 _visualEffect;
55 
56     // current highest mask: 0x40
57     short _all;
58     short _set;
59 
60     ::rtl::OUString _id;
61 
62     inline Style( short all_ ) SAL_THROW( () )
63         : _fontRelief( css::awt::FontRelief::NONE )
64         , _fontEmphasisMark( css::awt::FontEmphasisMark::NONE )
65         , _all( all_ )
66         , _set( 0 )
67         {}
68 
69     css::uno::Reference< css::xml::sax::XAttributeList > createElement();
70 };
71 class StyleBag
72 {
73     ::std::vector< Style * > _styles;
74 
75 public:
76     ~StyleBag() SAL_THROW( () );
77 
78     ::rtl::OUString getStyleId( Style const & rStyle ) SAL_THROW( () );
79 
80     void dump( css::uno::Reference< css::xml::sax::XExtendedDocumentHandler >
81                const & xOut );
82 };
83 
84 class ElementDescriptor
85     : public ::xmlscript::XMLElement
86 {
87     css::uno::Reference< css::beans::XPropertySet > _xProps;
88     css::uno::Reference< css::beans::XPropertyState > _xPropState;
89 
90 public:
ElementDescriptor(css::uno::Reference<css::beans::XPropertySet> const & xProps,css::uno::Reference<css::beans::XPropertyState> const & xPropState,::rtl::OUString const & name)91     inline ElementDescriptor(
92         css::uno::Reference< css::beans::XPropertySet > const & xProps,
93         css::uno::Reference< css::beans::XPropertyState > const & xPropState,
94         ::rtl::OUString const & name )
95         SAL_THROW( () )
96         : XMLElement( name )
97         , _xProps( xProps )
98         , _xPropState( xPropState )
99         {}
100     inline ElementDescriptor(
101         ::rtl::OUString const & name )
102         SAL_THROW( () )
103         : XMLElement( name )
104         {}
105 
106     template<typename T>
107     inline void read(
108         ::rtl::OUString const & propName, ::rtl::OUString const & attrName,
109         bool forceAttribute = false );
110 
111     //
112     template<typename T>
113     inline bool readProp( T * ret, ::rtl::OUString const & rPropName );
114     css::uno::Any readProp( ::rtl::OUString const & rPropName );
115     //
116     void readDefaults( bool supportPrintable = true, bool supportVisible = true );
117     //
118     void readStringAttr(
119         ::rtl::OUString const & rPropName, ::rtl::OUString const & rAttrName );
readDoubleAttr(::rtl::OUString const & rPropName,::rtl::OUString const & rAttrName)120     inline void readDoubleAttr(
121         ::rtl::OUString const & rPropName, ::rtl::OUString const & rAttrName )
122         { read<double>( rPropName, rAttrName ); }
readLongAttr(::rtl::OUString const & rPropName,::rtl::OUString const & rAttrName,bool forceAttribute=false)123     inline void readLongAttr(
124         ::rtl::OUString const & rPropName, ::rtl::OUString const & rAttrName,
125         bool forceAttribute = false )
126         { read<sal_Int32>( rPropName, rAttrName, forceAttribute ); }
127     void readHexLongAttr(
128         ::rtl::OUString const & rPropName, ::rtl::OUString const & rAttrName );
readShortAttr(::rtl::OUString const & rPropName,::rtl::OUString const & rAttrName)129     inline void readShortAttr(
130         ::rtl::OUString const & rPropName, ::rtl::OUString const & rAttrName )
131         { read<sal_Int32>( rPropName, rAttrName ); }
readBoolAttr(::rtl::OUString const & rPropName,::rtl::OUString const & rAttrName)132     inline void readBoolAttr(
133         ::rtl::OUString const & rPropName, ::rtl::OUString const & rAttrName )
134         { read<sal_Bool>( rPropName, rAttrName ); }
135 
136     void readAlignAttr(
137         ::rtl::OUString const & rPropName, ::rtl::OUString const & rAttrName );
138     void readVerticalAlignAttr(
139         ::rtl::OUString const & rPropName, ::rtl::OUString const & rAttrName );
140     void readImageAlignAttr(
141         ::rtl::OUString const & rPropName, ::rtl::OUString const & rAttrName );
142     void readImagePositionAttr(
143         ::rtl::OUString const & rPropName, ::rtl::OUString const & rAttrName );
144     void readDateFormatAttr(
145         ::rtl::OUString const & rPropName, ::rtl::OUString const & rAttrName );
146     void readTimeFormatAttr(
147         ::rtl::OUString const & rPropName, ::rtl::OUString const & rAttrName );
148     void readOrientationAttr(
149         ::rtl::OUString const & rPropName, ::rtl::OUString const & rAttrName );
150     void readButtonTypeAttr(
151         ::rtl::OUString const & rPropName, ::rtl::OUString const & rAttrName );
152     void readLineEndFormatAttr(
153         ::rtl::OUString const & rPropName, ::rtl::OUString const & rAttrName );
154 	void readSelectionTypeAttr(
155         ::rtl::OUString const & rPropName, ::rtl::OUString const & rAttrName );
156     void readImageScaleModeAttr(
157         ::rtl::OUString const & rPropName, ::rtl::OUString const & rAttrName );
158 	//
addBoolAttr(::rtl::OUString const & rAttrName,sal_Bool bValue)159     inline void addBoolAttr(
160         ::rtl::OUString const & rAttrName, sal_Bool bValue )
161         { addAttribute( rAttrName, ::rtl::OUString::valueOf(bValue) ); }
162     void addNumberFormatAttr(
163         css::uno::Reference< css::beans::XPropertySet >
164         const & xFormatProperties,
165         ::rtl::OUString const & rAttrName );
166 
167     //
168     void readEvents() SAL_THROW( (css::uno::Exception) );
169     //
170     void readDialogModel( StyleBag * all_styles )
171         SAL_THROW( (css::uno::Exception) );
172     void readButtonModel( StyleBag * all_styles )
173         SAL_THROW( (css::uno::Exception) );
174     void readEditModel( StyleBag * all_styles )
175         SAL_THROW( (css::uno::Exception) );
176     void readCheckBoxModel( StyleBag * all_styles )
177         SAL_THROW( (css::uno::Exception) );
178     void readRadioButtonModel( StyleBag * all_styles )
179         SAL_THROW( (css::uno::Exception) );
180     void readComboBoxModel( StyleBag * all_styles )
181         SAL_THROW( (css::uno::Exception) );
182     void readCurrencyFieldModel( StyleBag * all_styles )
183         SAL_THROW( (css::uno::Exception) );
184     void readDateFieldModel( StyleBag * all_styles )
185         SAL_THROW( (css::uno::Exception) );
186     void readFileControlModel( StyleBag * all_styles )
187         SAL_THROW( (css::uno::Exception) );
188 	void readTreeControlModel( StyleBag * all_styles )
189         SAL_THROW( (css::uno::Exception) );
190     void readFixedTextModel( StyleBag * all_styles )
191         SAL_THROW( (css::uno::Exception) );
192     void readGroupBoxModel( StyleBag * all_styles )
193         SAL_THROW( (css::uno::Exception) );
194     void readImageControlModel( StyleBag * all_styles )
195         SAL_THROW( (css::uno::Exception) );
196     void readListBoxModel( StyleBag * all_styles )
197         SAL_THROW( (css::uno::Exception) );
198     void readNumericFieldModel( StyleBag * all_styles )
199         SAL_THROW( (css::uno::Exception) );
200     void readPatternFieldModel( StyleBag * all_styles )
201         SAL_THROW( (css::uno::Exception) );
202     void readFormattedFieldModel( StyleBag * all_styles )
203         SAL_THROW( (css::uno::Exception) );
204     void readTimeFieldModel( StyleBag * all_styles )
205         SAL_THROW( (css::uno::Exception) );
206     void readFixedLineModel( StyleBag * all_styles )
207         SAL_THROW( (css::uno::Exception) );
208     void readProgressBarModel( StyleBag * all_styles )
209         SAL_THROW( (css::uno::Exception) );
210     void readScrollBarModel( StyleBag * all_styles )
211         SAL_THROW( (css::uno::Exception) );
212 	void readFixedHyperLinkModel( StyleBag * all_styles )
213         SAL_THROW( (css::uno::Exception) );
214 };
215 
216 template<typename T>
read(::rtl::OUString const & propName,::rtl::OUString const & attrName,bool forceAttribute)217 inline void ElementDescriptor::read(
218     ::rtl::OUString const & propName, ::rtl::OUString const & attrName,
219     bool forceAttribute )
220 {
221     if (forceAttribute ||
222         css::beans::PropertyState_DEFAULT_VALUE !=
223         _xPropState->getPropertyState( propName ))
224     {
225         css::uno::Any a( _xProps->getPropertyValue( propName ) );
226         T v = T();
227         if (a >>= v)
228             addAttribute( attrName, ::rtl::OUString::valueOf(v) );
229         else
230             OSL_ENSURE( 0, "### unexpected property type!" );
231     }
232 }
233 
234 template<typename T>
readProp(T * ret,::rtl::OUString const & rPropName)235 inline bool ElementDescriptor::readProp(
236     T * ret, ::rtl::OUString const & rPropName )
237 {
238     _xProps->getPropertyValue( rPropName ) >>= *ret;
239     return css::beans::PropertyState_DEFAULT_VALUE !=
240         _xPropState->getPropertyState( rPropName );
241 }
242 
243 }
244