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 _XMLOFF_FORMS_LAYEREXPORT_HXX_
25 #define _XMLOFF_FORMS_LAYEREXPORT_HXX_
26 
27 #include <com/sun/star/container/XIndexAccess.hpp>
28 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
29 #include <com/sun/star/util/XNumberFormats.hpp>
30 #include <com/sun/star/awt/XControlModel.hpp>
31 #include "callbacks.hxx"
32 #include "ifacecompare.hxx"
33 #include <vos/ref.hxx>
34 
35 class SvXMLExport;
36 class SvXMLNumFmtExport;
37 class XMLPropertyHandlerFactory;
38 class SvXMLExportPropertyMapper;
39 
40 //.........................................................................
41 namespace xmloff
42 {
43 //.........................................................................
44 
45 	typedef ::std::set  <   ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >
46                         ,   OPropertySetCompare
47                         >   PropertySetBag;
48 
49     // maps objects (property sets) to strings, e.g. control ids.
50     typedef ::std::map  <   ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >
51                         ,   ::rtl::OUString
52                         ,   OPropertySetCompare
53                         >   MapPropertySet2String;
54 
55     // map pages to maps (of property sets to strings)
56     typedef ::std::map  <   ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage >
57                         ,   MapPropertySet2String
58                         ,   ODrawPageCompare
59                         >   MapPropertySet2Map;
60 
61 	//=====================================================================
62 	//= OFormLayerXMLExport_Impl
63 	//=====================================================================
64 	/** the implementation class for OFormLayerXMLExport
65 	*/
66 	class OFormLayerXMLExport_Impl
67 				:public IFormsExportContext
68 	{
69 		friend class OFormLayerXMLExport;
70 
71 	protected:
72 		SvXMLExport&		m_rContext;
73 		SvXMLNumFmtExport*	m_pControlNumberStyles;
74 
75 		// ignore list for control models
76 		PropertySetBag		m_aIgnoreList;
77 
78 		// style handling
79 		::vos::ORef< XMLPropertyHandlerFactory >	m_xPropertyHandlerFactory;
80 		::vos::ORef< SvXMLExportPropertyMapper >	m_xStyleExportMapper;
81 
82 		// we need our own number formats supplier:
83 		// Controls which have a number formats do not work with the formats supplier of the document they reside
84 		// in, instead they use the formats of the data source their form is associated with. If there is no
85 		// such form or no such data source, they work with an own formatter.
86 		// Even more, time and date fields do not work with a central formatter at all, they have their own one
87 		// (which is shared internally, but this is a (hidden) implementation detail.)
88 		//
89 		// To not contaminate the global (document) number formats supplier (which could be obtained from the context),
90 		// we have an own one.
91 		// (Contaminate means: If a user adds a user-defined format to a formatted field, this format is stored in
92 		// in the data source's formats supplier. To export this _and_ reuse existing structures, we would need to
93 		// add this format to the global (document) formats supplier.
94 		// In case of an export we could do some cleanup afterwards, but in case of an import, there is no such
95 		// chance, as (if other user-defined formats exist in the document as well) we can't distinguish
96 		// between user-defined formats really needed for the doc (i.e. in a calc cell) and formats only added
97 		// to the supplier because the controls needed it.
98 		::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormats >
99 													m_xControlNumberFormats;
100 
101 		MapPropertySet2Map	m_aControlIds;
102 			// the control ids of all controls on all pages we ever examined
103 
104 		MapPropertySet2Map	m_aReferringControls;
105 			// for a given page (iter->first), and a given control (iter->second->first), this is the comma-separated
106 			// lists of ids of the controls refering to the control given.
107 
108         MapPropertySet2Map::iterator
109 							m_aCurrentPageIds;
110 			// the iterator for the control id map for the page beeing handled
111 		MapPropertySet2Map::iterator
112 							m_aCurrentPageReferring;
113 			// the same for the map of referring controls
114 
115 		// TODO: To avoid this construct above, and to have a cleaner implementation, an class encapsulating the
116 		// export of a single page should be introduced.
117 
118 		DECLARE_STL_MAP( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >, sal_Int32, OPropertySetCompare, MapPropertySet2Int);
119 		MapPropertySet2Int	m_aControlNumberFormats;
120 			// maps controls to format keys, which are relative to our own formats supplier
121 
122 		MapPropertySet2String	m_aGridColumnStyles;
123 			// style names of grid columns
124 
125 	public:
126 		OFormLayerXMLExport_Impl(SvXMLExport& _rContext);
127         virtual ~OFormLayerXMLExport_Impl();
128 
129 	protected:
130 		/** exports one single grid column
131 		*/
132 		void	exportGridColumn(
133 			const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxColumn,
134 			const ::com::sun::star::uno::Sequence< ::com::sun::star::script::ScriptEventDescriptor >& _rEvents
135 			);
136 
137 		/** exports one single control
138 		*/
139 		void	exportControl(
140 			const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxControl,
141 			const ::com::sun::star::uno::Sequence< ::com::sun::star::script::ScriptEventDescriptor >& _rEvents
142 			);
143 
144 		/** exports one single form
145 		*/
146 		void	exportForm(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxProps,
147 			const ::com::sun::star::uno::Sequence< ::com::sun::star::script::ScriptEventDescriptor >& _rEvents
148 			);
149 
150 		/** seek to the page given.
151 
152 			<p>This must be called before you can retrieve any ids for controls on the page.</p>
153 
154 			@see
155 				getControlId
156 		*/
157 		sal_Bool	seekPage(
158 			const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage >& _rxDrawPage);
159 
160 		/** get the id of the given control.
161 
162 			<p>You must have sought to the page of the control before calling this.</p>
163 		*/
164 		::rtl::OUString
165 				getControlId(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxControl);
166 
167 		/** retrieves the style name for the control's number style.
168 
169 			<p>For performance reasons, this method is allowed to be called for any controls, even those which
170 			do not have a number style. In this case, an empty string is returned.</p>
171 		*/
172 		::rtl::OUString
173 				getControlNumberStyle( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxControl );
174 
175 		// IFormsExportContext
176 		virtual void										exportCollectionElements(const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess >& _rxCollection);
177 		virtual SvXMLExport&								getGlobalContext();
178 		virtual ::rtl::OUString								getObjectStyleName(
179 			const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject );
180 		virtual ::vos::ORef< SvXMLExportPropertyMapper >	getStylePropertyMapper();
181 
182 		/** clear any structures which have been build in the recent <method>examine</method> calls.
183 		*/
184 		void clear();
185 
186 		/** examine a forms collection.
187 
188 			<p>The method will collect control ids and add styles to the export context as necessary.</p>
189 
190 			<p>Every control in the object hierarchy given will be assigned to a unique id, which is stored for later
191 			use.</p>
192 
193 			<p>In addition, any references the controls may have between each other, are collected and stored for
194 			later use.</p>
195 
196 			<p>Upon calling this method, the id map will be cleared before collecting the new ids, so any ids
197 			you collected previously will be lost</p>
198 
199 			@param _rxDrawPage
200 				the draw page which's forms collection should be examined
201 
202 			@see getControlId
203 			@see exportControl
204 			@see exportForms
205 		*/
206 		void examineForms(
207 			const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage >& _rxDrawPage);
208 
209 		/** export a forms collection of a draw page
210 
211 			<p>The method will obtain the forms collection of the page and call
212 			<method>exportCollectionElements</method>.</p>
213 		*/
214 		void exportForms(
215 			const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage >& _rxDrawPage);
216 
217         /** exports the XForms model data
218         */
219         void exportXForms() const;
220 
221         /** determines whether the given page contains logical forms
222         */
223 		bool pageContainsForms( const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage >& _rxDrawPage ) const;
224 
225         /** determines whether the given page contains XForm instances
226         */
227 		bool documentContainsXForms() const;
228 
229         /** exports the controls number styles
230 		*/
231 		void	exportControlNumberStyles();
232 
233 		/** exports the automatic control number styles
234 		*/
235 		void	exportAutoControlNumberStyles();
236 
237 		/** exports the auto-styles collected during the examineForms calls
238 		*/
239 		void exportAutoStyles();
240 
241 	protected:
242 		sal_Bool impl_isFormPageContainingForms(
243 			const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage >& _rxDrawPage,
244 			::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess >& _rxForms);
245 
246 		/** moves the m_aCurrentPage* members to the positions specifying the given page.
247 
248 			@return <TRUE/> if there already were structures for the given page
249 		*/
250 		sal_Bool implMoveIterators(
251 			const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage >& _rxDrawPage,
252 			sal_Bool _bClear);
253 
254 		/** check the object given if it's a control, if so, examine it.
255 			@return <TRUE/> if the object has been handled
256 		*/
257 		sal_Bool checkExamineControl(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject);
258 
259 		/** examines the control's number format, so later the format style can be referred
260 
261 			<p>remembers the format key for the control, so it can later be asked for in getControlNumberStyle</p>
262 		*/
263 		void examineControlNumberFormat(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxControl);
264 
265 		/** examines the control's number format, so later the format style can be referred
266 
267 			<p>does not remember the information returned in any way</p>
268 		*/
269 		sal_Int32 implExamineControlNumberFormat( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject );
270 
271 		/** collects AutoStyles for grid columns
272 		*/
273 		void collectGridColumnStylesAndIds( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxControl );
274 
275 		/** ensures that the number format of the given control exist in our own formats supplier.
276 
277 			<p>The given control is examined for it's format (i.e. it's FormatKey/FormatsSupplier properties),
278 			and the format is added (if necessary) to m_xControlNumberFormats</p>.
279 
280 			@return
281 				the format key of the control's format relative to our own formats supplier
282 
283 		*/
284 		sal_Int32	ensureTranslateFormat(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxFormattedControl);
285 
286 		/// returns the instance exporting our control's number styles
287 		SvXMLNumFmtExport*	getControlNumberStyleExport();
288 
289 		/// ensures that the instance exporting our control's number styles exists
290 		void				ensureControlNumberStyleExport();
291 
292 		/** determines the number format style for the given object without remembering it
293 		*/
294 		::rtl::OUString
295 				getImmediateNumberStyle( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject );
296 
297 		/** returns the prefix to be used for control number styles
298 		*/
299 		static const ::rtl::OUString& getControlNumberStyleNamePrefix();
300 
301 		/** exclude the given control (model) from export.
302 
303 			<p>If your document contains form controls which are not to be exported for whatever reason,
304 			you need to announce the models of these controls (can be retrieved from XControlShape::getControl)
305 			to the form layer exporter.<br/>
306 			Of course you have to do this before calling <member>exportForms</member></p>
307 		*/
308 		void excludeFromExport( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > _rxControl );
309 	};
310 
311 //.........................................................................
312 }	// namespace xmloff
313 //.........................................................................
314 
315 #endif // _XMLOFF_FORMS_LAYEREXPORT_HXX_
316 
317