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