Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / oox / source / drawingml / textfield.cxx
blobce38159c5e99bb484fb7c62f665a3f83ff6ecc3a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <string_view>
24 #include <drawingml/textfield.hxx>
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>
33 #include <o3tl/string_view.hxx>
34 #include <oox/helper/helper.hxx>
35 #include <oox/helper/propertyset.hxx>
36 #include <oox/core/xmlfilterbase.hxx>
37 #include <drawingml/textparagraphproperties.hxx>
38 #include <drawingml/textcharacterproperties.hxx>
39 #include <comphelper/diagnose_ex.hxx>
40 #include <editeng/flditem.hxx>
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::text;
45 using namespace ::com::sun::star::beans;
46 using namespace ::com::sun::star::frame;
47 using namespace ::com::sun::star::lang;
49 namespace oox::drawingml {
51 TextField::TextField()
55 namespace {
57 /** instantiate the textfields. Because of semantics difference between
58 * OpenXML and OpenOffice, some OpenXML field might cause two fields to be created.
59 * @param aFields the created fields. The list is empty if no field has been created.
60 * @param xModel the model
61 * @param sType the OpenXML field type.
63 void lclCreateTextFields( std::vector< Reference< XTextField > > & aFields,
64 const Reference< XModel > & xModel, std::u16string_view sType )
66 Reference< XInterface > xIface;
67 Reference< XMultiServiceFactory > xFactory( xModel, UNO_QUERY_THROW );
68 if( o3tl::starts_with(sType, u"datetime"))
70 auto p = sType.substr(8);
71 try
73 if (o3tl::starts_with(p, u"'"))
75 xIface = xFactory->createInstance( "com.sun.star.text.TextField.Custom" );
76 aFields.emplace_back( xIface, UNO_QUERY );
77 return;
80 SvxDateFormat eDateFormat = TextField::getLODateFormat(sType);
81 if (eDateFormat != SvxDateFormat::AppDefault)
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));
91 SvxTimeFormat eTimeFormat = TextField::getLOTimeFormat(sType);
92 if (eTimeFormat != SvxTimeFormat::AppDefault)
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));
102 catch(const Exception &)
104 TOOLS_WARN_EXCEPTION("oox", "");
107 else if ( sType == u"slidenum" )
109 xIface = xFactory->createInstance( "com.sun.star.text.TextField.PageNumber" );
110 aFields.emplace_back( xIface, UNO_QUERY );
112 else if ( sType == u"slidecount" )
114 xIface = xFactory->createInstance( "com.sun.star.text.TextField.PageCount" );
115 aFields.emplace_back( xIface, UNO_QUERY );
117 else if ( sType == u"slidename" )
119 xIface = xFactory->createInstance( "com.sun.star.text.TextField.PageName" );
120 aFields.emplace_back( xIface, uno::UNO_QUERY );
122 else if ( o3tl::starts_with(sType, u"file") )
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 );
129 switch( idx )
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)));
144 else if( sType == u"author" )
146 xIface = xFactory->createInstance( "com.sun.star.text.TextField.Author" );
147 aFields.emplace_back( xIface, UNO_QUERY );
151 } // namespace
153 sal_Int32 TextField::insertAt(
154 const ::oox::core::XmlFilterBase& rFilterBase,
155 const Reference < XText > & xText,
156 const Reference < XTextCursor > &xAt,
157 const TextCharacterProperties& rTextCharacterStyle,
158 float /*nDefaultCharHeight*/) const
160 sal_Int32 nCharHeight = 0;
163 PropertyMap aioBulletList;
164 Reference< XPropertySet > xProps( xAt, UNO_QUERY);
165 PropertySet aPropSet( xProps );
167 maTextParagraphProperties.pushToPropSet( &rFilterBase, xProps, aioBulletList, nullptr, true, 18 );
169 TextCharacterProperties aTextCharacterProps( rTextCharacterStyle );
170 aTextCharacterProps.assignUsed( maTextParagraphProperties.getTextCharacterProperties() );
171 aTextCharacterProps.assignUsed( getTextCharacterProperties() );
172 if ( aTextCharacterProps.moHeight.has_value() )
173 nCharHeight = aTextCharacterProps.moHeight.value();
174 aTextCharacterProps.pushToPropSet( aPropSet, rFilterBase );
176 std::vector< Reference< XTextField > > fields;
177 lclCreateTextFields( fields, rFilterBase.getModel(), msType );
178 if( !fields.empty() )
180 bool bFirst = true;
181 for (auto const& field : fields)
183 if( field.is() )
185 Reference< XTextContent > xContent( field, UNO_QUERY);
186 if( bFirst)
188 bFirst = false;
190 else
192 xText->insertString( xAt, " ", false );
194 xText->insertTextContent( xAt, xContent, false );
198 else
200 xText->insertString( xAt, getText(), false );
203 catch( const Exception& )
205 TOOLS_WARN_EXCEPTION("oox", "OOX: TextField::insertAt()");
208 return nCharHeight;
211 SvxDateFormat TextField::getLODateFormat(std::u16string_view rDateTimeType)
213 auto aDateTimeNum = rDateTimeType.substr(8);
215 if( aDateTimeNum.empty() ) // "datetime"
216 return SvxDateFormat::StdSmall;
218 int nDateTimeNum = o3tl::toInt32(aDateTimeNum);
220 switch( nDateTimeNum )
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;
244 SvxTimeFormat TextField::getLOTimeFormat(std::u16string_view rDateTimeType)
246 auto aDateTimeNum = rDateTimeType.substr(8);
247 int nDateTimeNum = o3tl::toInt32(aDateTimeNum);
249 switch( nDateTimeNum )
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;
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */