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