LibreOffice Module writerfilter (master) 1
FormControlHelper.cxx
Go to the documentation of this file.
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
37#include <o3tl/safeint.hxx>
38
39#include "FormControlHelper.hxx"
40#include <utility>
41#include <xmloff/odffields.hxx>
44
45namespace writerfilter::dmapper {
46
47using namespace ::com::sun::star;
48
50{
52 awt::Size aSize;
58
63};
64
66{
67 if (! rDrawPage.is())
68 {
70 xDrawPageSupplier(rTextDocument, uno::UNO_QUERY);
71 if (xDrawPageSupplier.is())
72 rDrawPage = xDrawPageSupplier->getDrawPage();
73 }
74
75 return rDrawPage;
76}
77
79{
80 if (! rServiceFactory.is())
81 rServiceFactory.set(rTextDocument, uno::UNO_QUERY);
82
83 return rServiceFactory;
84}
85
87{
88 if (! rForm.is())
89 {
90 uno::Reference<form::XFormsSupplier> xFormsSupplier(getDrawPage(), uno::UNO_QUERY);
91
92 if (xFormsSupplier.is())
93 {
94 uno::Reference<container::XNameContainer> xFormsNamedContainer(xFormsSupplier->getForms());
95 static constexpr OUStringLiteral sDOCXForm = u"DOCX-Standard";
96
97 OUString sFormName(sDOCXForm);
98 sal_uInt16 nUnique = 0;
99
100 while (xFormsNamedContainer->hasByName(sFormName))
101 {
102 ++nUnique;
103 sFormName = sDOCXForm + OUString::number(nUnique);
104 }
105
106 uno::Reference<uno::XInterface> xForm(getServiceFactory()->createInstance("com.sun.star.form.component.Form"));
107 if (xForm.is())
108 {
110 xFormProperties(xForm, uno::UNO_QUERY);
111 uno::Any aAny(sFormName);
112 xFormProperties->setPropertyValue("Name", aAny);
113 }
114
115 rForm.set(xForm, uno::UNO_QUERY);
116
117 uno::Reference<container::XIndexContainer> xForms(xFormsNamedContainer, uno::UNO_QUERY);
118 uno::Any aAny(xForm);
119 xForms->insertByIndex(xForms->getCount(), aAny);
120 }
121 }
122
123 return rForm;
124}
125
127{
128 uno::Reference<container::XIndexContainer> xIndexContainer(getForm(), uno::UNO_QUERY);
129
130 return xIndexContainer;
131}
132
134 uno::Reference<text::XTextDocument> const& xTextDocument,
136 : m_pFFData(std::move(pFFData)), m_pImpl(new FormControlHelper_Impl)
137{
138 m_pImpl->m_eFieldId = eFieldId;
139 m_pImpl->rTextDocument = xTextDocument;
140}
141
143{
144}
145
147 const OUString & rControlName)
148{
149 if ( !m_pFFData )
150 return false;
152 xServiceFactory(m_pImpl->getServiceFactory());
153
154 if (! xServiceFactory.is())
155 return false;
156
157 uno::Reference<uno::XInterface> xInterface = xServiceFactory->createInstance("com.sun.star.form.component.CheckBox");
158
159 if (!xInterface.is())
160 return false;
161
162 m_pImpl->rFormComponent.set(xInterface, uno::UNO_QUERY);
163 if (!m_pImpl->rFormComponent.is())
164 return false;
165
166 uno::Reference<beans::XPropertySet> xPropSet(xInterface, uno::UNO_QUERY);
167
168 sal_uInt32 nCheckBoxHeight = 16 * m_pFFData->getCheckboxHeight();
169
170 if (m_pFFData->getCheckboxAutoHeight())
171 {
172 uno::Reference<beans::XPropertySet> xTextRangeProps(xTextRange, uno::UNO_QUERY);
173
174 try
175 {
176 float fCheckBoxHeight = 0.0;
177 xTextRangeProps->getPropertyValue("CharHeight") >>= fCheckBoxHeight;
178 nCheckBoxHeight = static_cast<sal_uInt32>(floor(fCheckBoxHeight * 35.3));
179 }
180 catch (beans::UnknownPropertyException &)
181 {
182 }
183 }
184
185 m_pImpl->aSize.Width = nCheckBoxHeight;
186 m_pImpl->aSize.Height = m_pImpl->aSize.Width;
187
188 if (!m_pFFData->getStatusText().isEmpty())
189 {
190 xPropSet->setPropertyValue("HelpText", uno::Any(m_pFFData->getStatusText()));
191 }
192
193 xPropSet->setPropertyValue("DefaultState", uno::Any(m_pFFData->getCheckboxChecked()));
194
195 if (!m_pFFData->getHelpText().isEmpty())
196 {
197 xPropSet->setPropertyValue("HelpF1Text", uno::Any(m_pFFData->getHelpText()));
198 }
199
200 xPropSet->setPropertyValue("Name", uno::Any(rControlName));
201
202 return true;
203}
204
206{
207 // Set field type first before adding parameters.
208 if (m_pImpl->m_eFieldId == FIELD_FORMTEXT )
209 {
210 xFormField->setFieldType(ODF_FORMTEXT);
211 }
212 else if (m_pImpl->m_eFieldId == FIELD_FORMCHECKBOX )
213 {
214 xFormField->setFieldType(ODF_FORMCHECKBOX);
215 }
216 else if (m_pImpl->m_eFieldId == FIELD_FORMDROPDOWN )
217 {
218 xFormField->setFieldType(ODF_FORMDROPDOWN);
219 }
220
221 uno::Reference<container::XNameContainer> xNameCont = xFormField->getParameters();
222 uno::Reference<container::XNamed> xNamed( xFormField, uno::UNO_QUERY );
223 if ( !(m_pFFData && xNamed.is() && xNameCont.is()) )
224 return;
225
226 OUString sTmp = m_pFFData->getEntryMacro();
227 if ( !sTmp.isEmpty() )
228 xNameCont->insertByName( "EntryMacro", uno::Any(sTmp) );
229 sTmp = m_pFFData->getExitMacro();
230 if ( !sTmp.isEmpty() )
231 xNameCont->insertByName( "ExitMacro", uno::Any(sTmp) );
232
233 sTmp = m_pFFData->getHelpText();
234 if ( !sTmp.isEmpty() )
235 xNameCont->insertByName( "Help", uno::Any(sTmp) );
236
237 sTmp = m_pFFData->getStatusText();
238 if ( !sTmp.isEmpty() )
239 xNameCont->insertByName( "Hint", uno::Any(sTmp) );
240
241 if (m_pImpl->m_eFieldId == FIELD_FORMTEXT )
242 {
243 sTmp = m_pFFData->getName();
244 try
245 {
246 if ( !sTmp.isEmpty() )
247 xNamed->setName( sTmp );
248 }
249 catch ( uno::Exception& )
250 {
251 TOOLS_INFO_EXCEPTION("writerfilter", "Set Formfield name failed");
252 }
253
254 sTmp = m_pFFData->getTextType();
255 if ( !sTmp.isEmpty() )
256 xNameCont->insertByName( "Type", uno::Any(sTmp) );
257
258 const sal_uInt16 nMaxLength = m_pFFData->getTextMaxLength();
259 if ( nMaxLength )
260 {
261 xNameCont->insertByName( "MaxLength", uno::Any(nMaxLength) );
262 }
263
264 sTmp = m_pFFData->getTextDefault();
265 if ( !sTmp.isEmpty() )
266 xNameCont->insertByName( "Content", uno::Any(sTmp) );
267
268 sTmp = m_pFFData->getTextFormat();
269 if ( !sTmp.isEmpty() )
270 xNameCont->insertByName( "Format", uno::Any(sTmp) );
271 }
272 else if (m_pImpl->m_eFieldId == FIELD_FORMCHECKBOX )
273 {
274 uno::Reference<beans::XPropertySet> xPropSet(xFormField, uno::UNO_QUERY);
275 uno::Any aAny;
276 aAny <<= m_pFFData->getCheckboxChecked();
277 if ( xPropSet.is() )
278 xPropSet->setPropertyValue("Checked", aAny);
279 }
280 else if (m_pImpl->m_eFieldId == FIELD_FORMDROPDOWN )
281 {
282 const FFDataHandler::DropDownEntries_t& rEntries = m_pFFData->getDropDownEntries();
283 if (!rEntries.empty())
284 {
285 if ( xNameCont->hasByName(ODF_FORMDROPDOWN_LISTENTRY) )
286 xNameCont->replaceByName(ODF_FORMDROPDOWN_LISTENTRY, uno::Any(comphelper::containerToSequence(rEntries)));
287 else
288 xNameCont->insertByName(ODF_FORMDROPDOWN_LISTENTRY, uno::Any(comphelper::containerToSequence(rEntries)));
289
290 sal_Int32 nResult = m_pFFData->getDropDownResult().toInt32();
291 // 0 is valid, but also how toInt32 reports parse error, but it's a sensible default...
292 if (0 <= nResult && o3tl::make_unsigned(nResult) < rEntries.size())
293 {
294 if ( xNameCont->hasByName(ODF_FORMDROPDOWN_RESULT) )
295 xNameCont->replaceByName(ODF_FORMDROPDOWN_RESULT, uno::Any( nResult ) );
296 else
297 xNameCont->insertByName(ODF_FORMDROPDOWN_RESULT, uno::Any( nResult ) );
298 }
299 }
300 }
301}
302
304{
305 bool bCreated = false;
306 if ( !m_pFFData )
307 return;
308 uno::Reference<container::XNameContainer> xFormCompsByName(m_pImpl->getForm(), uno::UNO_QUERY);
309 uno::Reference<container::XIndexContainer> xFormComps(m_pImpl->getFormComps());
310 if (! xFormComps.is())
311 return;
312
313 sal_Int32 nControl = 0;
314 bool bDone = false;
315 OUString sControlName;
316
317 do
318 {
319 OUString sTmp = "Control" + OUString::number(nControl);
320
321 nControl++;
322 if (! xFormCompsByName->hasByName(sTmp))
323 {
324 sControlName = sTmp;
325 bDone = true;
326 }
327 }
328 while (! bDone);
329
330 switch (m_pImpl->m_eFieldId)
331 {
333 bCreated = createCheckbox(xTextRange, sControlName);
334 break;
335 default:
336 break;
337 }
338
339 if (!bCreated)
340 return;
341
342 uno::Any aAny(m_pImpl->rFormComponent);
343 xFormComps->insertByIndex(xFormComps->getCount(), aAny);
344
345 if (! m_pImpl->getServiceFactory().is())
346 return;
347
348 uno::Reference<uno::XInterface> xInterface = m_pImpl->getServiceFactory()->createInstance("com.sun.star.drawing.ControlShape");
349
350 if (! xInterface.is())
351 return;
352
353 uno::Reference<drawing::XShape> xShape(xInterface, uno::UNO_QUERY);
354
355 if (! xShape.is())
356 return;
357
358 xShape->setSize(m_pImpl->aSize);
359
360 uno::Reference<beans::XPropertySet> xShapeProps(xShape, uno::UNO_QUERY);
361
362 sal_uInt16 nTmp = sal_uInt16(text::TextContentAnchorType_AS_CHARACTER);
363 xShapeProps->setPropertyValue("AnchorType", uno::Any(sal_uInt16(nTmp)));
364
365 nTmp = text::VertOrientation::CENTER;
366 xShapeProps->setPropertyValue("VertOrient", uno::Any(sal_uInt16(nTmp)));
367
368 xShapeProps->setPropertyValue("TextRange", uno::Any(xTextRange));
369
370 uno::Reference<drawing::XControlShape> xControlShape(xShape, uno::UNO_QUERY);
371 uno::Reference<awt::XControlModel> xControlModel(m_pImpl->rFormComponent, uno::UNO_QUERY);
372 xControlShape->setControl(xControlModel);
373
374 m_pImpl->getDrawPage()->add(xShape);
375}
376
377}
378
379/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
FieldId eFieldId
HRESULT createInstance(REFIID iid, Ifc **ppIfc)
::std::vector< OUString > DropDownEntries_t
void processField(css::uno::Reference< css::text::XFormField > const &xFormField)
void insertControl(css::uno::Reference< css::text::XTextRange > const &xTextRange)
tools::SvRef< FormControlHelper_Impl > m_pImpl
FormControlHelper(FieldId eFieldId, css::uno::Reference< css::text::XTextDocument > const &rTextDocument, FFDataHandler::Pointer_t pFFData)
bool createCheckbox(css::uno::Reference< css::text::XTextRange > const &xTextRange, const OUString &rControlName)
#define TOOLS_INFO_EXCEPTION(area, stream)
float u
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
constexpr OUStringLiteral ODF_FORMTEXT
constexpr OUStringLiteral ODF_FORMDROPDOWN_RESULT
constexpr OUStringLiteral ODF_FORMDROPDOWN_LISTENTRY
constexpr OUStringLiteral ODF_FORMCHECKBOX
constexpr OUStringLiteral ODF_FORMDROPDOWN
uno::Reference< drawing::XDrawPage > const & getDrawPage()
uno::Reference< container::XIndexContainer > getFormComps()
uno::Reference< lang::XMultiServiceFactory > const & getServiceFactory()
uno::Reference< lang::XMultiServiceFactory > rServiceFactory