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