LibreOffice Module oox (master) 1
textfield.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 <sal/config.h>
21
22#include <string_view>
23
25
26#include <rtl/ustring.hxx>
27#include <rtl/string.hxx>
28#include <com/sun/star/beans/XPropertySet.hpp>
29#include <com/sun/star/frame/XModel.hpp>
30#include <com/sun/star/lang/XMultiServiceFactory.hpp>
31#include <com/sun/star/text/XTextField.hpp>
32
33#include <o3tl/string_view.hxx>
34#include <oox/helper/helper.hxx>
40#include <editeng/flditem.hxx>
41
42using namespace ::com::sun::star;
43using namespace ::com::sun::star::uno;
44using namespace ::com::sun::star::text;
45using namespace ::com::sun::star::beans;
46using namespace ::com::sun::star::frame;
47using namespace ::com::sun::star::lang;
48
49namespace oox::drawingml {
50
52{
53}
54
55namespace {
56
63void lclCreateTextFields( std::vector< Reference< XTextField > > & aFields,
64 const Reference< XModel > & xModel, std::u16string_view sType )
65{
67 Reference< XMultiServiceFactory > xFactory( xModel, UNO_QUERY_THROW );
68 if( o3tl::starts_with(sType, u"datetime"))
69 {
70 auto p = sType.substr(8);
71 try
72 {
73 if (o3tl::starts_with(p, u"'"))
74 {
75 xIface = xFactory->createInstance( "com.sun.star.text.TextField.Custom" );
76 aFields.emplace_back( xIface, UNO_QUERY );
77 return;
78 }
79
80 SvxDateFormat eDateFormat = TextField::getLODateFormat(sType);
81 if (eDateFormat != SvxDateFormat::AppDefault)
82 {
83 xIface = xFactory->createInstance( "com.sun.star.text.TextField.DateTime" );
84 aFields.emplace_back( xIface, UNO_QUERY );
85 Reference< XPropertySet > xProps( xIface, UNO_QUERY_THROW );
86 xProps->setPropertyValue("NumberFormat", Any(static_cast<sal_Int32>(eDateFormat)));
87 xProps->setPropertyValue("IsDate", Any(true));
88 xProps->setPropertyValue("IsFixed", Any(false));
89 }
90
91 SvxTimeFormat eTimeFormat = TextField::getLOTimeFormat(sType);
92 if (eTimeFormat != SvxTimeFormat::AppDefault)
93 {
94 xIface = xFactory->createInstance( "com.sun.star.text.TextField.DateTime" );
95 aFields.emplace_back( xIface, UNO_QUERY );
96 Reference< XPropertySet > xProps( xIface, UNO_QUERY_THROW );
97 xProps->setPropertyValue("NumberFormat", Any(static_cast<sal_Int32>(eTimeFormat)));
98 xProps->setPropertyValue("IsDate", Any(false));
99 xProps->setPropertyValue("IsFixed", Any(false));
100 }
101 }
102 catch(const Exception &)
103 {
104 TOOLS_WARN_EXCEPTION("oox", "");
105 }
106 }
107 else if ( sType == u"slidenum" )
108 {
109 xIface = xFactory->createInstance( "com.sun.star.text.TextField.PageNumber" );
110 aFields.emplace_back( xIface, UNO_QUERY );
111 }
112 else if ( sType == u"slidecount" )
113 {
114 xIface = xFactory->createInstance( "com.sun.star.text.TextField.PageCount" );
115 aFields.emplace_back( xIface, UNO_QUERY );
116 }
117 else if ( sType == u"slidename" )
118 {
119 xIface = xFactory->createInstance( "com.sun.star.text.TextField.PageName" );
120 aFields.emplace_back( xIface, uno::UNO_QUERY );
121 }
122 else if ( o3tl::starts_with(sType, u"file") )
123 {
124 int idx = o3tl::toInt32(sType.substr(4));
125 xIface = xFactory->createInstance( "com.sun.star.text.TextField.FileName" );
126 aFields.emplace_back( xIface, UNO_QUERY );
127 Reference< XPropertySet > xProps( xIface, UNO_QUERY_THROW );
128
129 switch( idx )
130 {
131 case 1: // Path
132 xProps->setPropertyValue("FileFormat", Any(sal_Int16(1)));
133 break;
134 case 2: // File name without extension
135 xProps->setPropertyValue("FileFormat", Any(sal_Int16(2)));
136 break;
137 case 3: // File name with extension
138 xProps->setPropertyValue("FileFormat", Any(sal_Int16(3)));
139 break;
140 default: // Path/File name
141 xProps->setPropertyValue("FileFormat", Any(sal_Int16(0)));
142 }
143 }
144 else if( sType == u"author" )
145 {
146 xIface = xFactory->createInstance( "com.sun.star.text.TextField.Author" );
147 aFields.emplace_back( xIface, UNO_QUERY );
148 }
149}
150
151} // namespace
152
154 const ::oox::core::XmlFilterBase& rFilterBase,
155 const Reference < XText > & xText,
156 const Reference < XTextCursor > &xAt,
157 const TextCharacterProperties& rTextCharacterStyle,
158 float /*nDefaultCharHeight*/) const
159{
160 sal_Int32 nCharHeight = 0;
161 try
162 {
163 PropertyMap aioBulletList;
164 Reference< XPropertySet > xProps( xAt, UNO_QUERY);
165 PropertySet aPropSet( xProps );
166
167 maTextParagraphProperties.pushToPropSet( &rFilterBase, xProps, aioBulletList, nullptr, true, 18 );
168
169 TextCharacterProperties aTextCharacterProps( rTextCharacterStyle );
171 aTextCharacterProps.assignUsed( getTextCharacterProperties() );
172 if ( aTextCharacterProps.moHeight.has_value() )
173 nCharHeight = aTextCharacterProps.moHeight.value();
174 aTextCharacterProps.pushToPropSet( aPropSet, rFilterBase );
175
176 std::vector< Reference< XTextField > > fields;
177 lclCreateTextFields( fields, rFilterBase.getModel(), msType );
178 if( !fields.empty() )
179 {
180 bool bFirst = true;
181 for (auto const& field : fields)
182 {
183 if( field.is() )
184 {
185 Reference< XTextContent > xContent( field, UNO_QUERY);
186 if( bFirst)
187 {
188 bFirst = false;
189 }
190 else
191 {
192 xText->insertString( xAt, " ", false );
193 }
194 xText->insertTextContent( xAt, xContent, false );
195 }
196 }
197 }
198 else
199 {
200 xText->insertString( xAt, getText(), false );
201 }
202 }
203 catch( const Exception& )
204 {
205 TOOLS_WARN_EXCEPTION("oox", "OOX: TextField::insertAt()");
206 }
207
208 return nCharHeight;
209}
210
211SvxDateFormat TextField::getLODateFormat(std::u16string_view rDateTimeType)
212{
213 auto aDateTimeNum = rDateTimeType.substr(8);
214
215 if( aDateTimeNum.empty() ) // "datetime"
216 return SvxDateFormat::StdSmall;
217
218 int nDateTimeNum = o3tl::toInt32(aDateTimeNum);
219
220 switch( nDateTimeNum )
221 {
222 case 1: // Date dd/mm/yyyy
223 case 8: // DateTime dd/mm/yyyy H:MM PM
224 case 9: // DateTime dd/mm/yyyy H:MM:SS PM
225 return SvxDateFormat::B;
226 case 2: // Date Day, Month dd, yyyy
227 return SvxDateFormat::StdBig;
228 case 3: // Date dd Month yyyy
229 case 4: // Date Month dd, yyyy - no exact map
230 case 6: // Date Month yy - no exact map
231 return SvxDateFormat::D;
232 case 5: // Date dd-Mon-yy - no exact map
233 case 7: // Date Mon-yy - no exact map
234 return SvxDateFormat::C;
235 case 10: // Time H:MM - not a date format
236 case 11: // Time H:MM:SS - not a date format
237 case 12: // Time H:MM PM - not a date format
238 case 13: // Time H:MM:SS PM - not a date format
239 default:
240 return SvxDateFormat::AppDefault;
241 }
242}
243
244SvxTimeFormat TextField::getLOTimeFormat(std::u16string_view rDateTimeType)
245{
246 auto aDateTimeNum = rDateTimeType.substr(8);
247 int nDateTimeNum = o3tl::toInt32(aDateTimeNum);
248
249 switch( nDateTimeNum )
250 {
251 case 8: // DateTime dd/mm/yyyy H:MM PM
252 case 12: // Time H:MM PM
253 return SvxTimeFormat::HH12_MM;
254 case 9: // DateTime dd/mm/yyyy H:MM:SS PM
255 case 13: // Time H:MM:SS PM
256 return SvxTimeFormat::HH12_MM_SS;
257 case 10: // Time H:MM
258 return SvxTimeFormat::HH24_MM;
259 case 11: // Time H:MM:SS
260 return SvxTimeFormat::Standard;
261 case 1: // Date dd/mm/yyyy
262 case 2: // Date Day, Month dd, yyyy
263 case 3: // Date dd Month yyyy
264 case 4: // Date Month dd, yyyy
265 case 5: // Date dd-Mon-yy
266 case 6: // Date Month yy
267 case 7: // Date Mon-yy
268 default:
269 return SvxTimeFormat::AppDefault;
270 }
271}
272}
273
274/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OptionalString sType
A helper that maps property identifiers to property values.
Definition: propertymap.hxx:52
A wrapper for a UNO property set.
Definition: propertyset.hxx:58
TextParagraphProperties maTextParagraphProperties
Definition: textfield.hxx:65
static SvxDateFormat getLODateFormat(std::u16string_view rDateTimeType)
Gets the corresponding LO Date format for given OOXML datetime field type.
Definition: textfield.cxx:211
virtual sal_Int32 insertAt(const ::oox::core::XmlFilterBase &rFilterBase, const css::uno::Reference< css::text::XText > &xText, const css::uno::Reference< css::text::XTextCursor > &xAt, const TextCharacterProperties &rTextCharacterStyle, float nDefaultCharHeight) const override
Definition: textfield.cxx:153
static SvxTimeFormat getLOTimeFormat(std::u16string_view rDateTimeType)
Gets the corresponding LO Time format for given OOXML datetime field type.
Definition: textfield.cxx:244
void pushToPropSet(const ::oox::core::XmlFilterBase *pFilterBase, const css::uno::Reference< css::beans::XPropertySet > &xPropSet, PropertyMap &rioBulletList, const BulletList *pMasterBuList, bool bApplyBulletList, float fFontSize, sal_Int32 nAutofitFontScale=100000, bool bPushDefaultValues=false) const
TextCharacterProperties & getTextCharacterProperties()
TextCharacterProperties & getTextCharacterProperties()
Definition: textrun.hxx:40
OUString & getText()
Definition: textrun.hxx:37
#define TOOLS_WARN_EXCEPTION(area, stream)
Reference< XSingleServiceFactory > xFactory
SvxTimeFormat
SvxDateFormat
const sal_uInt16 idx[]
void * p
@ Exception
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
constexpr bool starts_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
void pushToPropSet(PropertySet &rPropSet, const ::oox::core::XmlFilterBase &rFilter) const
Writes the properties to the passed property set.
void assignUsed(const TextCharacterProperties &rSourceProps)
Overwrites all members that are explicitly set in rSourceProps.