1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 #include "oox/drawingml/textfield.hxx"
25
26 #include <list>
27
28 #include <rtl/ustring.hxx>
29 #include <rtl/string.hxx>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <com/sun/star/text/XTextField.hpp>
33
34 #include "oox/helper/helper.hxx"
35 #include "oox/helper/propertyset.hxx"
36 #include "oox/core/xmlfilterbase.hxx"
37 #include "oox/drawingml/textparagraphproperties.hxx"
38 #include "oox/drawingml/textcharacterproperties.hxx"
39
40 using ::rtl::OString;
41 using ::rtl::OUString;
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 { namespace drawingml {
50
TextField()51 TextField::TextField()
52 {
53 }
54
55 namespace {
56
57 /** intsanciate 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.
62 */
lclCreateTextFields(std::list<Reference<XTextField>> & aFields,const Reference<XModel> & xModel,const OUString & sType)63 void lclCreateTextFields( std::list< Reference< XTextField > > & aFields,
64 const Reference< XModel > & xModel, const OUString & sType )
65 {
66 Reference< XInterface > xIface;
67 Reference< XMultiServiceFactory > xFactory( xModel, UNO_QUERY_THROW );
68
69 if( sType.compareToAscii( "datetime", 8 ) == 0)
70 {
71 OString s = ::rtl::OUStringToOString( sType, RTL_TEXTENCODING_UTF8);
72 OString p( s.pData->buffer + 8 );
73 try
74 {
75 bool bIsDate = true;
76 int idx = p.toInt32();
77 // OSL_TRACE( "OOX: p = %s, %d", p.pData->buffer, idx );
78 xIface = xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.text.TextField.DateTime" ) );
79 aFields.push_back( Reference< XTextField > ( xIface, UNO_QUERY ) );
80 Reference< XPropertySet > xProps( xIface, UNO_QUERY_THROW );
81
82 // here we should format the field properly. waiting after #i81091.
83 switch( idx )
84 {
85 case 1: // Date dd/mm/yyyy
86 // this is the default format...
87 break;
88 case 2: // Date Day, Month dd, yyyy
89 break;
90 case 3: // Date dd Month yyyy
91 break;
92 case 4: // Date Month dd, yyyy
93 break;
94 case 5: // Date dd-Mon-yy
95 break;
96 case 6: // Date Month yy
97 break;
98 case 7: // Date Mon-yy
99 break;
100 case 8: // DateTime dd/mm/yyyy H:MM PM
101 lclCreateTextFields( aFields, xModel, CREATE_OUSTRING( "datetime12" ) );
102 break;
103 case 9: // DateTime dd/mm/yy H:MM:SS PM
104 lclCreateTextFields( aFields, xModel, CREATE_OUSTRING( "datetime13" ) );
105 break;
106 case 10: // Time H:MM
107 bIsDate = false;
108 break;
109 case 11: // Time H:MM:SS
110 bIsDate = false;
111 // this is the default format
112 break;
113 case 12: // Time H:MM PM
114 bIsDate = false;
115 break;
116 case 13: // Time H:MM:SS PM
117 bIsDate = false;
118 break;
119 }
120 xProps->setPropertyValue( CREATE_OUSTRING( "IsDate" ), makeAny( bIsDate ) );
121 xProps->setPropertyValue( CREATE_OUSTRING( "IsFixed" ), makeAny( false ) );
122 }
123 catch(Exception & e)
124 {
125 OSL_TRACE( "Exception %s", OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
126 }
127 }
128 else if ( sType.compareToAscii( "slidenum" ) == 0 )
129 {
130 xIface = xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.text.TextField.PageNumber" ) );
131 aFields.push_back( Reference< XTextField > ( xIface, UNO_QUERY ) );
132 }
133 }
134
135 } // namespace
136
insertAt(const::oox::core::XmlFilterBase & rFilterBase,const Reference<XText> & xText,const Reference<XTextCursor> & xAt,const TextCharacterProperties & rTextCharacterStyle) const137 void TextField::insertAt(
138 const ::oox::core::XmlFilterBase& rFilterBase,
139 const Reference < XText > & xText,
140 const Reference < XTextCursor > &xAt,
141 const TextCharacterProperties& rTextCharacterStyle ) const
142 {
143 try
144 {
145 PropertyMap aioBulletList;
146 Reference< XTextRange > xStart( xAt, UNO_QUERY );
147 Reference< XPropertySet > xProps( xStart, UNO_QUERY);
148 PropertySet aPropSet( xProps );
149
150 maTextParagraphProperties.pushToPropSet( rFilterBase, xProps, aioBulletList, NULL, sal_True, 18 );
151
152 TextCharacterProperties aTextCharacterProps( rTextCharacterStyle );
153 aTextCharacterProps.assignUsed( maTextParagraphProperties.getTextCharacterProperties() );
154 aTextCharacterProps.assignUsed( getTextCharacterProperties() );
155 aTextCharacterProps.pushToPropSet( aPropSet, rFilterBase );
156
157 std::list< Reference< XTextField > > fields;
158 lclCreateTextFields( fields, rFilterBase.getModel(), msType );
159 if( !fields.empty() )
160 {
161 bool bFirst = true;
162 for( std::list< Reference< XTextField > >::iterator iter = fields.begin();
163 iter != fields.end(); ++iter )
164 {
165 if( iter->is() )
166 {
167 Reference< XTextContent > xContent( *iter, UNO_QUERY);
168 if( bFirst)
169 {
170 bFirst = false;
171 }
172 else
173 {
174 xText->insertString( xStart, CREATE_OUSTRING( " " ), sal_False );
175 }
176 xText->insertTextContent( xStart, xContent, sal_False );
177 }
178 }
179 }
180 else
181 {
182 xText->insertString( xStart, getText(), sal_False );
183 }
184 }
185 catch( const Exception& )
186 {
187 OSL_TRACE("OOX: TextField::insertAt() exception");
188 }
189 }
190
191 } }
192