Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <math.h>
21 :
22 : #include <com/sun/star/awt/XControlModel.hpp>
23 : #include <com/sun/star/beans/XPropertySet.hpp>
24 : #include <com/sun/star/container/XIndexContainer.hpp>
25 : #include <com/sun/star/container/XNamed.hpp>
26 : #include <com/sun/star/drawing/XControlShape.hpp>
27 : #include <com/sun/star/drawing/XDrawPage.hpp>
28 : #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
29 : #include <com/sun/star/form/XForm.hpp>
30 : #include <com/sun/star/form/XFormComponent.hpp>
31 : #include <com/sun/star/form/XFormsSupplier.hpp>
32 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 : #include <com/sun/star/text/TextContentAnchorType.hpp>
34 : #include <com/sun/star/text/VertOrientation.hpp>
35 : #include <com/sun/star/uno/Any.hxx>
36 : #include <com/sun/star/uno/Type.hxx>
37 :
38 : #include "FormControlHelper.hxx"
39 : #include <xmloff/odffields.hxx>
40 : #include <comphelper/stlunosequence.hxx>
41 :
42 : namespace writerfilter {
43 : namespace dmapper {
44 :
45 : using namespace ::com::sun::star;
46 :
47 4 : struct FormControlHelper::FormControlHelper_Impl
48 : {
49 : FieldId m_eFieldId;
50 : awt::Size aSize;
51 : uno::Reference<drawing::XDrawPage> rDrawPage;
52 : uno::Reference<form::XForm> rForm;
53 : uno::Reference<form::XFormComponent> rFormComponent;
54 : uno::Reference<lang::XMultiServiceFactory> rServiceFactory;
55 : uno::Reference<text::XTextDocument> rTextDocument;
56 :
57 : uno::Reference<drawing::XDrawPage> getDrawPage();
58 : uno::Reference<lang::XMultiServiceFactory> getServiceFactory();
59 : uno::Reference<form::XForm> getForm();
60 : uno::Reference<container::XIndexContainer> getFormComps();
61 : };
62 :
63 0 : uno::Reference<drawing::XDrawPage> FormControlHelper::FormControlHelper_Impl::getDrawPage()
64 : {
65 0 : if (! rDrawPage.is())
66 : {
67 : uno::Reference<drawing::XDrawPageSupplier>
68 0 : xDrawPageSupplier(rTextDocument, uno::UNO_QUERY);
69 0 : if (xDrawPageSupplier.is())
70 0 : rDrawPage = xDrawPageSupplier->getDrawPage();
71 : }
72 :
73 0 : return rDrawPage;
74 : }
75 :
76 0 : uno::Reference<lang::XMultiServiceFactory> FormControlHelper::FormControlHelper_Impl::getServiceFactory()
77 : {
78 0 : if (! rServiceFactory.is())
79 0 : rServiceFactory = uno::Reference<lang::XMultiServiceFactory>(rTextDocument, uno::UNO_QUERY);
80 :
81 0 : return rServiceFactory;
82 : }
83 :
84 0 : uno::Reference<form::XForm> FormControlHelper::FormControlHelper_Impl::getForm()
85 : {
86 0 : if (! rForm.is())
87 : {
88 0 : uno::Reference<form::XFormsSupplier> xFormsSupplier(getDrawPage(), uno::UNO_QUERY);
89 :
90 0 : if (xFormsSupplier.is())
91 : {
92 0 : uno::Reference<container::XNameContainer> xFormsNamedContainer(xFormsSupplier->getForms());
93 0 : static OUString sDOCXForm("DOCX-Standard");
94 :
95 0 : OUString sFormName(sDOCXForm);
96 0 : sal_uInt16 nUnique = 0;
97 :
98 0 : while (xFormsNamedContainer->hasByName(sFormName))
99 : {
100 0 : ++nUnique;
101 0 : sFormName = sDOCXForm;
102 0 : sFormName += OUString::valueOf(nUnique);
103 : }
104 :
105 0 : uno::Reference<uno::XInterface> xForm(getServiceFactory()->createInstance("com.sun.star.form.component.Form"));
106 0 : if (xForm.is())
107 : {
108 : uno::Reference<beans::XPropertySet>
109 0 : xFormProperties(xForm, uno::UNO_QUERY);
110 0 : uno::Any aAny(sFormName);
111 0 : xFormProperties->setPropertyValue("Name", aAny);
112 : }
113 :
114 0 : rForm = uno::Reference<form::XForm>(xForm, uno::UNO_QUERY);
115 :
116 0 : uno::Reference<container::XIndexContainer> xForms(xFormsNamedContainer, uno::UNO_QUERY);
117 0 : uno::Any aAny(xForm);
118 0 : xForms->insertByIndex(xForms->getCount(), aAny);
119 0 : }
120 : }
121 :
122 0 : return rForm;
123 : }
124 :
125 0 : uno::Reference<container::XIndexContainer> FormControlHelper::FormControlHelper_Impl::getFormComps()
126 : {
127 0 : uno::Reference<container::XIndexContainer> xIndexContainer(getForm(), uno::UNO_QUERY);
128 :
129 0 : return xIndexContainer;
130 : }
131 :
132 2 : FormControlHelper::FormControlHelper(FieldId eFieldId,
133 : uno::Reference<text::XTextDocument> rTextDocument,
134 : FFDataHandler::Pointer_t pFFData)
135 2 : : m_pFFData(pFFData), m_pImpl(new FormControlHelper_Impl)
136 : {
137 2 : m_pImpl->m_eFieldId = eFieldId;
138 2 : m_pImpl->rTextDocument = rTextDocument;
139 2 : }
140 :
141 2 : FormControlHelper::~FormControlHelper()
142 : {
143 2 : }
144 :
145 0 : bool FormControlHelper::createCheckbox(uno::Reference<text::XTextRange> xTextRange,
146 : const OUString & rControlName)
147 : {
148 0 : if ( !m_pFFData )
149 0 : return false;
150 : uno::Reference<lang::XMultiServiceFactory>
151 0 : xServiceFactory(m_pImpl->getServiceFactory());
152 :
153 0 : if (! xServiceFactory.is())
154 0 : return false;
155 :
156 0 : uno::Reference<uno::XInterface> xInterface = xServiceFactory->createInstance("com.sun.star.form.component.CheckBox");
157 :
158 0 : if (!xInterface.is())
159 0 : return false;
160 :
161 0 : m_pImpl->rFormComponent = uno::Reference<form::XFormComponent>(xInterface, uno::UNO_QUERY);
162 0 : if (!m_pImpl->rFormComponent.is())
163 0 : return false;
164 :
165 0 : uno::Reference<beans::XPropertySet> xPropSet(xInterface, uno::UNO_QUERY);
166 :
167 0 : sal_uInt32 nCheckBoxHeight = 16 * m_pFFData->getCheckboxHeight();
168 :
169 0 : if (m_pFFData->getCheckboxAutoHeight())
170 : {
171 0 : uno::Reference<beans::XPropertySet> xTextRangeProps(xTextRange, uno::UNO_QUERY);
172 :
173 : try
174 : {
175 0 : float fCheckBoxHeight = 0.0;
176 0 : xTextRangeProps->getPropertyValue("CharHeight") >>= fCheckBoxHeight;
177 0 : nCheckBoxHeight = static_cast<sal_uInt32>(floor(fCheckBoxHeight * 35.3));
178 : }
179 0 : catch (beans::UnknownPropertyException &)
180 : {
181 0 : }
182 : }
183 :
184 0 : m_pImpl->aSize.Width = nCheckBoxHeight;
185 0 : m_pImpl->aSize.Height = m_pImpl->aSize.Width;
186 :
187 0 : uno::Any aAny;
188 0 : if (!m_pFFData->getStatusText().isEmpty())
189 : {
190 0 : aAny <<= m_pFFData->getStatusText();
191 :
192 0 : xPropSet->setPropertyValue("HelpText", aAny);
193 : }
194 :
195 0 : aAny <<= m_pFFData->getCheckboxChecked();
196 0 : xPropSet->setPropertyValue("DefaultState", aAny);
197 :
198 0 : if (!m_pFFData->getHelpText().isEmpty())
199 : {
200 0 : aAny <<= m_pFFData->getHelpText();
201 0 : xPropSet->setPropertyValue("HelpF1Text", aAny);
202 : }
203 :
204 0 : aAny <<= rControlName;
205 0 : xPropSet->setPropertyValue("Name", aAny);
206 :
207 0 : return true;
208 : }
209 :
210 2 : bool FormControlHelper::processField(uno::Reference<text::XFormField> xFormField)
211 : {
212 2 : bool bRes = true;
213 2 : uno::Reference<container::XNameContainer> xNameCont = xFormField->getParameters();
214 2 : uno::Reference<container::XNamed> xNamed( xFormField, uno::UNO_QUERY );
215 2 : if ( m_pFFData && xNamed.is() && xNameCont.is() )
216 : {
217 :
218 2 : if (m_pImpl->m_eFieldId == FIELD_FORMTEXT )
219 : {
220 0 : xFormField->setFieldType(ODF_FORMTEXT);
221 0 : if ( !m_pFFData->getName().isEmpty() )
222 : {
223 0 : xNamed->setName( m_pFFData->getName() );
224 : }
225 : }
226 2 : else if (m_pImpl->m_eFieldId == FIELD_FORMCHECKBOX )
227 : {
228 2 : xFormField->setFieldType(ODF_FORMCHECKBOX);
229 2 : uno::Reference<beans::XPropertySet> xPropSet(xFormField, uno::UNO_QUERY);
230 2 : uno::Any aAny;
231 2 : aAny <<= m_pFFData->getCheckboxChecked();
232 2 : if ( xPropSet.is() )
233 2 : xPropSet->setPropertyValue("Checked", aAny);
234 : }
235 0 : else if (m_pImpl->m_eFieldId == FIELD_FORMDROPDOWN )
236 : {
237 0 : xFormField->setFieldType(ODF_FORMDROPDOWN);
238 0 : uno::Sequence< OUString > sItems;
239 0 : sItems.realloc( m_pFFData->getDropDownEntries().size() );
240 0 : ::std::copy( m_pFFData->getDropDownEntries().begin(), m_pFFData->getDropDownEntries().end(), ::comphelper::stl_begin(sItems));
241 0 : if ( sItems.getLength() )
242 : {
243 0 : if ( xNameCont->hasByName(ODF_FORMDROPDOWN_LISTENTRY) )
244 0 : xNameCont->replaceByName(ODF_FORMDROPDOWN_LISTENTRY, uno::makeAny( sItems ) );
245 : else
246 0 : xNameCont->insertByName(ODF_FORMDROPDOWN_LISTENTRY, uno::makeAny( sItems ) );
247 :
248 0 : sal_Int32 nResult = m_pFFData->getDropDownResult().toInt32();
249 0 : if ( nResult )
250 : {
251 0 : if ( xNameCont->hasByName(ODF_FORMDROPDOWN_RESULT) )
252 0 : xNameCont->replaceByName(ODF_FORMDROPDOWN_RESULT, uno::makeAny( nResult ) );
253 : else
254 0 : xNameCont->insertByName(ODF_FORMDROPDOWN_RESULT, uno::makeAny( nResult ) );
255 : }
256 0 : }
257 : }
258 : }
259 : else
260 0 : bRes = false;
261 2 : return bRes;
262 : }
263 :
264 0 : bool FormControlHelper::insertControl(uno::Reference<text::XTextRange> xTextRange)
265 : {
266 0 : bool bCreated = false;
267 0 : if ( !m_pFFData )
268 0 : return false;
269 0 : uno::Reference<container::XNameContainer> xFormCompsByName(m_pImpl->getForm(), uno::UNO_QUERY);
270 0 : uno::Reference<container::XIndexContainer> xFormComps(m_pImpl->getFormComps());
271 0 : if (! xFormComps.is())
272 0 : return false;
273 :
274 0 : static OUString sControl("Control");
275 :
276 0 : sal_Int32 nControl = 0;
277 0 : bool bDone = false;
278 0 : OUString sControlName;
279 :
280 0 : do
281 : {
282 0 : OUString sTmp(sControl);
283 0 : sTmp += OUString::valueOf(nControl);
284 :
285 0 : nControl++;
286 0 : if (! xFormCompsByName->hasByName(sTmp))
287 : {
288 0 : sControlName = sTmp;
289 0 : bDone = true;
290 0 : }
291 : }
292 0 : while (! bDone);
293 :
294 0 : switch (m_pImpl->m_eFieldId)
295 : {
296 : case FIELD_FORMCHECKBOX:
297 0 : bCreated = createCheckbox(xTextRange, sControlName);
298 0 : break;
299 : default:
300 0 : break;
301 : }
302 :
303 0 : if (!bCreated)
304 0 : return false;
305 :
306 0 : uno::Any aAny(m_pImpl->rFormComponent);
307 0 : xFormComps->insertByIndex(xFormComps->getCount(), aAny);
308 :
309 0 : if (! m_pImpl->getServiceFactory().is())
310 0 : return false;
311 :
312 0 : uno::Reference<uno::XInterface> xInterface = m_pImpl->getServiceFactory()->createInstance("com.sun.star.drawing.ControlShape");
313 :
314 0 : if (! xInterface.is())
315 0 : return false;
316 :
317 0 : uno::Reference<drawing::XShape> xShape(xInterface, uno::UNO_QUERY);
318 :
319 0 : if (! xShape.is())
320 0 : return false;
321 :
322 0 : xShape->setSize(m_pImpl->aSize);
323 :
324 0 : uno::Reference<beans::XPropertySet> xShapeProps(xShape, uno::UNO_QUERY);
325 :
326 0 : sal_uInt16 nTmp = text::TextContentAnchorType_AS_CHARACTER;
327 0 : aAny <<= nTmp;
328 :
329 0 : xShapeProps->setPropertyValue("AnchorType", aAny);
330 :
331 0 : nTmp = text::VertOrientation::CENTER;
332 0 : aAny <<= nTmp;
333 0 : xShapeProps->setPropertyValue("VertOrient", aAny);
334 :
335 0 : aAny <<= xTextRange;
336 :
337 0 : xShapeProps->setPropertyValue("TextRange", aAny);
338 :
339 0 : uno::Reference<drawing::XControlShape> xControlShape(xShape, uno::UNO_QUERY);
340 0 : uno::Reference<awt::XControlModel> xControlModel(m_pImpl->rFormComponent, uno::UNO_QUERY);
341 0 : xControlShape->setControl(xControlModel);
342 :
343 0 : m_pImpl->getDrawPage()->add(xShape);
344 :
345 0 : return true;
346 : }
347 :
348 30 : }}
349 :
350 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|