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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26 #include <com/sun/star/text/XTextContent.hpp>
27 #ifndef _COM_SUN_STAR_TEXT_TEXTCONTENTANCHORTYPE_HPP
28 #include <com/sun/star/text/TextContentAnchorType.hpp>
29 #endif
30 
31 #ifndef _XMLOFF_XMLTIMP_HXX_
32 #include <xmloff/xmlimp.hxx>
33 #endif
34 #include <xmloff/txtimp.hxx>
35 #include <xmloff/xmluconv.hxx>
36 #include <xmloff/nmspmap.hxx>
37 #include "XMLAnchorTypePropHdl.hxx"
38 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
39 #include <com/sun/star/drawing/XShapes.hpp>
40 #include "xmloff/XMLTextShapeImportHelper.hxx"
41 
42 using ::rtl::OUString;
43 using ::rtl::OUStringBuffer;
44 
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::frame;
47 using namespace ::com::sun::star::drawing;
48 using namespace ::com::sun::star::beans;
49 using namespace ::com::sun::star::text;
50 using namespace ::com::sun::star::container;
51 using namespace ::com::sun::star::xml::sax;
52 
XMLTextShapeImportHelper(SvXMLImport & rImp)53 XMLTextShapeImportHelper::XMLTextShapeImportHelper(
54 		SvXMLImport& rImp ) :
55 	XMLShapeImportHelper( rImp, rImp.GetModel(),
56 						  XMLTextImportHelper::CreateShapeExtPropMapper(rImp) ),
57 	rImport( rImp ),
58 	sAnchorType(RTL_CONSTASCII_USTRINGPARAM("AnchorType")),
59 	sAnchorPageNo(RTL_CONSTASCII_USTRINGPARAM("AnchorPageNo")),
60 	sVertOrientPosition(RTL_CONSTASCII_USTRINGPARAM("VertOrientPosition"))
61 {
62 	Reference < XDrawPageSupplier > xDPS( rImp.GetModel(), UNO_QUERY );
63 	if( xDPS.is() )
64 	{
65 	 	Reference < XShapes > xShapes( xDPS->getDrawPage(), UNO_QUERY );
66 		pushGroupForSorting( xShapes );
67 	}
68 
69 }
70 
~XMLTextShapeImportHelper()71 XMLTextShapeImportHelper::~XMLTextShapeImportHelper()
72 {
73 	popGroupAndSort();
74 }
75 
addShape(Reference<XShape> & rShape,const Reference<XAttributeList> & xAttrList,Reference<XShapes> & rShapes)76 void XMLTextShapeImportHelper::addShape(
77 	Reference< XShape >& rShape,
78 	const Reference< XAttributeList >& xAttrList,
79 	Reference< XShapes >& rShapes )
80 {
81 	if( rShapes.is() )
82 	{
83 		// It's a group shape or 3DScene , so we have to call the base class method.
84 		XMLShapeImportHelper::addShape( rShape, xAttrList, rShapes );
85 		return;
86 	}
87 
88 	TextContentAnchorType eAnchorType = TextContentAnchorType_AT_PARAGRAPH;
89 	sal_Int16	nPage = 0;
90 	sal_Int32	nY = 0;
91 
92 	UniReference < XMLTextImportHelper > xTxtImport =
93 		rImport.GetTextImport();
94 	const SvXMLTokenMap& rTokenMap =
95 		xTxtImport->GetTextFrameAttrTokenMap();
96 
97 	sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
98 	for( sal_Int16 i=0; i < nAttrCount; i++ )
99 	{
100 		const OUString& rAttrName = xAttrList->getNameByIndex( i );
101 		const OUString& rValue = xAttrList->getValueByIndex( i );
102 
103 		OUString aLocalName;
104 		sal_uInt16 nPrefix =
105 			rImport.GetNamespaceMap().GetKeyByAttrName( rAttrName,
106 															&aLocalName );
107 		switch( rTokenMap.Get( nPrefix, aLocalName ) )
108 		{
109 		case XML_TOK_TEXT_FRAME_ANCHOR_TYPE:
110 			{
111 				TextContentAnchorType eNew;
112                 // OD 2004-06-01 #i26791# - allow all anchor types
113                 if ( XMLAnchorTypePropHdl::convert( rValue, eNew ) )
114                 {
115 					eAnchorType = eNew;
116                 }
117 			}
118 			break;
119 		case XML_TOK_TEXT_FRAME_ANCHOR_PAGE_NUMBER:
120 			{
121 				sal_Int32 nTmp;
122 			   	if( rImport.GetMM100UnitConverter().
123 								convertNumber( nTmp, rValue, 1, SHRT_MAX ) )
124 					nPage = (sal_Int16)nTmp;
125 			}
126 			break;
127 		case XML_TOK_TEXT_FRAME_Y:
128 			rImport.GetMM100UnitConverter().convertMeasure( nY, rValue );
129 			break;
130 		}
131 	}
132 
133 	Reference < XPropertySet > xPropSet( rShape, UNO_QUERY );
134 	Any aAny;
135 
136 	// anchor type
137 	aAny <<= eAnchorType;
138 	xPropSet->setPropertyValue( sAnchorType, aAny );
139 
140 	Reference < XTextContent > xTxtCntnt( rShape, UNO_QUERY );
141 	xTxtImport->InsertTextContent( xTxtCntnt );
142 
143 	// page number (must be set after the frame is inserted, because it
144 	// will be overwritten then inserting the frame.
145 	switch( eAnchorType )
146 	{
147 	case TextContentAnchorType_AT_PAGE:
148 		// only set positive page numbers
149 		if ( nPage > 0 )
150 		{
151 			aAny <<= nPage;
152 			xPropSet->setPropertyValue( sAnchorPageNo, aAny );
153 		}
154 		break;
155 	case TextContentAnchorType_AS_CHARACTER:
156 		aAny <<= nY;
157 		xPropSet->setPropertyValue( sVertOrientPosition, aAny );
158 		break;
159 	default:
160 		break;
161 	}
162 }
163