Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / oox / source / drawingml / textfield.cxx
blobdf3be9cf4a9d904a71fb7a517bfd6face76b8f38
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 "drawingml/textfield.hxx"
22 #include <list>
24 #include <rtl/ustring.hxx>
25 #include <rtl/string.hxx>
26 #include <com/sun/star/beans/XPropertySet.hpp>
27 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <com/sun/star/text/XTextField.hpp>
30 #include "oox/helper/helper.hxx"
31 #include "oox/helper/propertyset.hxx"
32 #include "oox/core/xmlfilterbase.hxx"
33 #include "drawingml/textparagraphproperties.hxx"
34 #include "drawingml/textcharacterproperties.hxx"
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::text;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::frame;
41 using namespace ::com::sun::star::lang;
43 namespace oox { namespace drawingml {
45 TextField::TextField()
49 namespace {
51 /** intsanciate the textfields. Because of semantics difference between
52 * OpenXML and OpenOffice, some OpenXML field might cause two fields to be created.
53 * @param aFields the created fields. The list is empty if no field has been created.
54 * @param xModel the model
55 * @param sType the OpenXML field type.
57 void lclCreateTextFields( std::list< Reference< XTextField > > & aFields,
58 const Reference< XModel > & xModel, const OUString & sType )
60 Reference< XInterface > xIface;
61 Reference< XMultiServiceFactory > xFactory( xModel, UNO_QUERY_THROW );
62 if( sType.startsWith("datetime"))
64 OString s = OUStringToOString( sType, RTL_TEXTENCODING_UTF8);
65 OString p( s.pData->buffer + 8 );
66 try
68 bool bIsDate = true;
69 int idx = p.toInt32();
70 sal_uInt16 nNumFmt;
71 xIface = xFactory->createInstance( "com.sun.star.text.TextField.DateTime" );
72 aFields.push_back( Reference< XTextField > ( xIface, UNO_QUERY ) );
73 Reference< XPropertySet > xProps( xIface, UNO_QUERY_THROW );
75 // here we should format the field properly. waiting after #i81091.
76 switch( idx )
78 case 1: // Date dd/mm/yyyy
79 // this is the default format...
80 nNumFmt = 5;
81 xProps->setPropertyValue("NumberFormat", makeAny(nNumFmt));
82 break;
83 case 2: // Date Day, Month dd, yyyy
84 break;
85 case 3: // Date dd Month yyyy
86 nNumFmt = 3;
87 xProps->setPropertyValue("NumberFormat", makeAny(nNumFmt));
88 break;
89 case 4: // Date Month dd, yyyy
90 break;
91 case 5: // Date dd-Mon-yy
92 break;
93 case 6: // Date Month yy
94 break;
95 case 7: // Date Mon-yy
96 break;
97 case 8: // DateTime dd/mm/yyyy H:MM PM
98 lclCreateTextFields( aFields, xModel, "datetime12" );
99 break;
100 case 9: // DateTime dd/mm/yy H:MM:SS PM
101 lclCreateTextFields( aFields, xModel, "datetime13" );
102 break;
103 case 10: // Time H:MM
104 bIsDate = false;
105 nNumFmt = 3;
106 xProps->setPropertyValue("NumberFormat", makeAny(nNumFmt));
107 break;
108 case 11: // Time H:MM:SS
109 bIsDate = false;
110 // this is the default format
111 nNumFmt = 2;
112 xProps->setPropertyValue("NumberFormat", makeAny(nNumFmt));
113 break;
114 case 12: // Time H:MM PM
115 bIsDate = false;
116 nNumFmt = 6;
117 xProps->setPropertyValue("NumberFormat", makeAny(nNumFmt));
118 break;
119 case 13: // Time H:MM:SS PM
120 bIsDate = false;
121 nNumFmt = 7;
122 xProps->setPropertyValue("NumberFormat", makeAny(nNumFmt));
123 break;
124 default:
125 nNumFmt = 2;
126 xProps->setPropertyValue("NumberFormat", makeAny(nNumFmt));
128 xProps->setPropertyValue( "IsDate", makeAny( bIsDate ) );
129 xProps->setPropertyValue( "IsFixed", makeAny( false ) );
131 catch(Exception & e)
133 SAL_WARN("oox", "Exception " << e.Message );
136 else if ( sType == "slidenum" )
138 xIface = xFactory->createInstance( "com.sun.star.text.TextField.PageNumber" );
139 aFields.push_back( Reference< XTextField > ( xIface, UNO_QUERY ) );
141 else if ( sType == "slidecount" )
143 xIface = xFactory->createInstance( "com.sun.star.text.TextField.PageCount" );
144 aFields.push_back( Reference< XTextField > ( xIface, UNO_QUERY ) );
146 else if ( sType == "slidename" )
148 xIface = xFactory->createInstance( "com.sun.star.text.TextField.PageName" );
149 aFields.push_back( Reference< XTextField > ( xIface, uno::UNO_QUERY ) );
151 else if ( sType.startsWith("file") )
153 OString s = OUStringToOString( sType, RTL_TEXTENCODING_UTF8);
154 OString p( s.pData->buffer + 4 );
155 int idx = p.toInt32();
156 xIface = xFactory->createInstance( "com.sun.star.text.TextField.FileName" );
157 aFields.push_back( Reference< XTextField > ( xIface, UNO_QUERY ) );
158 Reference< XPropertySet > xProps( xIface, UNO_QUERY_THROW );
160 switch( idx )
162 case 1: // Path
163 xProps->setPropertyValue("FileFormat", makeAny<sal_Int16>(1));
164 break;
165 case 2: // File name without extension
166 xProps->setPropertyValue("FileFormat", makeAny<sal_Int16>(2));
167 break;
168 case 3: // File name with extension
169 xProps->setPropertyValue("FileFormat", makeAny<sal_Int16>(3));
170 break;
171 default: // Path/File name
172 xProps->setPropertyValue("FileFormat", makeAny<sal_Int16>(0));
175 else if( sType == "author" )
177 xIface = xFactory->createInstance( "com.sun.star.text.TextField.Author" );
178 aFields.push_back( Reference< XTextField > ( xIface, UNO_QUERY ) );
182 } // namespace
184 sal_Int32 TextField::insertAt(
185 const ::oox::core::XmlFilterBase& rFilterBase,
186 const Reference < XText > & xText,
187 const Reference < XTextCursor > &xAt,
188 const TextCharacterProperties& rTextCharacterStyle,
189 float /*nDefaultCharHeight*/) const
191 sal_Int32 nCharHeight = 0;
194 PropertyMap aioBulletList;
195 Reference< XPropertySet > xProps( xAt, UNO_QUERY);
196 PropertySet aPropSet( xProps );
198 maTextParagraphProperties.pushToPropSet( &rFilterBase, xProps, aioBulletList, nullptr, true, 18 );
200 TextCharacterProperties aTextCharacterProps( rTextCharacterStyle );
201 aTextCharacterProps.assignUsed( maTextParagraphProperties.getTextCharacterProperties() );
202 aTextCharacterProps.assignUsed( getTextCharacterProperties() );
203 if ( aTextCharacterProps.moHeight.has() )
204 nCharHeight = aTextCharacterProps.moHeight.get();
205 aTextCharacterProps.pushToPropSet( aPropSet, rFilterBase );
207 std::list< Reference< XTextField > > fields;
208 lclCreateTextFields( fields, rFilterBase.getModel(), msType );
209 if( !fields.empty() )
211 bool bFirst = true;
212 for( std::list< Reference< XTextField > >::iterator iter = fields.begin();
213 iter != fields.end(); ++iter )
215 if( iter->is() )
217 Reference< XTextContent > xContent( *iter, UNO_QUERY);
218 if( bFirst)
220 bFirst = false;
222 else
224 xText->insertString( xAt, " ", false );
226 xText->insertTextContent( xAt, xContent, false );
230 else
232 xText->insertString( xAt, getText(), false );
235 catch( const Exception& )
237 SAL_WARN("oox", "OOX: TextField::insertAt() exception");
240 return nCharHeight;
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */