merge the formfield patch from ooo-build
[ooovba.git] / xmloff / source / style / backhdl.cxx
blob97c18a53254cf5186747d285853cda0f2b9b916f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: backhdl.cxx,v $
10 * $Revision: 1.13 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
33 #include <tools/debug.hxx>
34 #include <backhdl.hxx>
35 #include <xmloff/xmltoken.hxx>
36 #include <xmloff/xmluconv.hxx>
37 #include <com/sun/star/uno/Any.hxx>
38 #include <rtl/ustrbuf.hxx>
40 #ifndef _XMLOFF_XMLEMENT_HXX
41 #include <xmloff/xmlelement.hxx>
42 #endif
44 using ::rtl::OUString;
45 using ::rtl::OUStringBuffer;
47 using namespace ::com::sun::star;
48 using namespace ::xmloff::token;
50 SvXMLEnumMapEntry pXML_BrushHorizontalPos[] =
52 { XML_LEFT, style::GraphicLocation_LEFT_MIDDLE },
53 { XML_RIGHT, style::GraphicLocation_RIGHT_MIDDLE },
54 { XML_TOKEN_INVALID, 0 }
57 SvXMLEnumMapEntry pXML_BrushVerticalPos[] =
59 { XML_TOP, style::GraphicLocation_MIDDLE_TOP },
60 { XML_BOTTOM, style::GraphicLocation_MIDDLE_BOTTOM },
61 { XML_TOKEN_INVALID, 0 }
64 ///////////////////////////////////////////////////////////////////////////////
66 // class XMLBackGraphicPositionPropHdl
69 XMLBackGraphicPositionPropHdl::~XMLBackGraphicPositionPropHdl()
71 // Nothing to do
74 sal_Bool XMLBackGraphicPositionPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
76 sal_Bool bRet = sal_True;
77 style::GraphicLocation ePos = style::GraphicLocation_NONE, eTmp;
78 sal_uInt16 nTmp;
79 SvXMLTokenEnumerator aTokenEnum( rStrImpValue );
80 OUString aToken;
81 sal_Bool bHori = sal_False, bVert = sal_False;
83 while( bRet && aTokenEnum.getNextToken( aToken ) )
85 if( bHori && bVert )
87 bRet = sal_False;
89 else if( -1 != aToken.indexOf( sal_Unicode('%') ) )
91 sal_Int32 nPrc = 50;
92 if( SvXMLUnitConverter::convertPercent( nPrc, aToken ) )
94 if( !bHori )
96 ePos = nPrc < 25 ? style::GraphicLocation_LEFT_TOP :
97 (nPrc < 75 ? style::GraphicLocation_MIDDLE_MIDDLE :
98 style::GraphicLocation_RIGHT_BOTTOM);
99 bHori = sal_True;
101 else
103 eTmp = nPrc < 25 ? style::GraphicLocation_LEFT_TOP:
104 (nPrc < 75 ? style::GraphicLocation_LEFT_MIDDLE :
105 style::GraphicLocation_LEFT_BOTTOM);
106 MergeXMLVertPos( ePos, eTmp );
107 bVert = sal_True;
110 else
112 // wrong percentage
113 bRet = sal_False;
116 else if( IsXMLToken( aToken, XML_CENTER ) )
118 if( bHori )
119 MergeXMLVertPos( ePos, style::GraphicLocation_MIDDLE_MIDDLE );
120 else if ( bVert )
121 MergeXMLHoriPos( ePos, style::GraphicLocation_MIDDLE_MIDDLE );
122 else
123 ePos = style::GraphicLocation_MIDDLE_MIDDLE;
125 else if( SvXMLUnitConverter::convertEnum( nTmp, aToken, pXML_BrushHorizontalPos ) )
127 if( bVert )
128 MergeXMLHoriPos( ePos, (style::GraphicLocation)nTmp );
129 else if( !bHori )
130 ePos = (style::GraphicLocation)nTmp;
131 else
132 bRet = sal_False;
134 bHori = sal_True;
136 else if( SvXMLUnitConverter::convertEnum( nTmp, aToken, pXML_BrushVerticalPos ) )
138 if( bHori )
139 MergeXMLVertPos( ePos, (style::GraphicLocation)nTmp );
140 else if( !bVert )
141 ePos = (style::GraphicLocation)nTmp;
142 else
143 bRet = sal_False;
144 bVert = sal_True;
146 else
148 bRet = sal_False;
152 bRet &= style::GraphicLocation_NONE != ePos;
153 if( bRet )
154 rValue <<= (style::GraphicLocation)(sal_uInt16)ePos;
156 return bRet;
159 sal_Bool XMLBackGraphicPositionPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
161 sal_Bool bRet = sal_True;
162 OUStringBuffer aOut;
164 style::GraphicLocation eLocation;
165 if( !( rValue >>= eLocation ) )
167 sal_Int32 nValue = 0;
168 if( rValue >>= nValue )
169 eLocation = (style::GraphicLocation)nValue;
170 else
171 bRet = sal_False;
174 if( bRet )
176 bRet = sal_False;
178 switch( eLocation )
180 case style::GraphicLocation_LEFT_TOP:
181 case style::GraphicLocation_MIDDLE_TOP:
182 case style::GraphicLocation_RIGHT_TOP:
183 aOut.append( GetXMLToken(XML_TOP) );
184 bRet = sal_True;
185 break;
186 case style::GraphicLocation_LEFT_MIDDLE:
187 case style::GraphicLocation_MIDDLE_MIDDLE:
188 case style::GraphicLocation_RIGHT_MIDDLE:
189 aOut.append( GetXMLToken(XML_CENTER) );
190 bRet = sal_True;
191 break;
192 case style::GraphicLocation_LEFT_BOTTOM:
193 case style::GraphicLocation_MIDDLE_BOTTOM:
194 case style::GraphicLocation_RIGHT_BOTTOM:
195 aOut.append( GetXMLToken(XML_BOTTOM) );
196 bRet = sal_True;
197 break;
198 default:
199 break;
202 if( bRet )
204 aOut.append( sal_Unicode( ' ' ) );
206 switch( eLocation )
208 case style::GraphicLocation_LEFT_TOP:
209 case style::GraphicLocation_LEFT_BOTTOM:
210 case style::GraphicLocation_LEFT_MIDDLE:
211 aOut.append( GetXMLToken(XML_LEFT) );
212 break;
213 case style::GraphicLocation_MIDDLE_TOP:
214 case style::GraphicLocation_MIDDLE_MIDDLE:
215 case style::GraphicLocation_MIDDLE_BOTTOM:
216 aOut.append( GetXMLToken(XML_CENTER) );
217 break;
218 case style::GraphicLocation_RIGHT_MIDDLE:
219 case style::GraphicLocation_RIGHT_TOP:
220 case style::GraphicLocation_RIGHT_BOTTOM:
221 aOut.append( GetXMLToken(XML_RIGHT) );
222 break;
223 default:
224 break;
229 rStrExpValue = aOut.makeStringAndClear();
231 return bRet;
234 void XMLBackGraphicPositionPropHdl::MergeXMLVertPos( style::GraphicLocation& ePos, style::GraphicLocation eVert ) const
236 switch( ePos )
238 case style::GraphicLocation_LEFT_TOP:
239 case style::GraphicLocation_LEFT_MIDDLE:
240 case style::GraphicLocation_LEFT_BOTTOM:
241 ePos = style::GraphicLocation_MIDDLE_TOP==eVert ?
242 style::GraphicLocation_LEFT_TOP :
243 (style::GraphicLocation_MIDDLE_MIDDLE==eVert ?
244 style::GraphicLocation_LEFT_MIDDLE :
245 style::GraphicLocation_LEFT_BOTTOM);
246 ePos = eVert;
247 break;
249 case style::GraphicLocation_MIDDLE_TOP:
250 case style::GraphicLocation_MIDDLE_MIDDLE:
251 case style::GraphicLocation_MIDDLE_BOTTOM:
252 ePos = eVert;
253 break;
255 case style::GraphicLocation_RIGHT_TOP:
256 case style::GraphicLocation_RIGHT_MIDDLE:
257 case style::GraphicLocation_RIGHT_BOTTOM:
258 ePos = style::GraphicLocation_MIDDLE_TOP==eVert ?
259 style::GraphicLocation_RIGHT_TOP :
260 (style::GraphicLocation_MIDDLE_MIDDLE==eVert ?
261 style::GraphicLocation_RIGHT_MIDDLE :
262 style::GraphicLocation_RIGHT_BOTTOM);
263 break;
264 default:
265 break;
269 void XMLBackGraphicPositionPropHdl::MergeXMLHoriPos( style::GraphicLocation& ePos, style::GraphicLocation eHori ) const
271 DBG_ASSERT( style::GraphicLocation_LEFT_MIDDLE==eHori || style::GraphicLocation_MIDDLE_MIDDLE==eHori || style::GraphicLocation_RIGHT_MIDDLE==eHori,
272 "lcl_frmitems_MergeXMLHoriPos: vertical pos must be middle" );
274 switch( ePos )
276 case style::GraphicLocation_LEFT_TOP:
277 case style::GraphicLocation_MIDDLE_TOP:
278 case style::GraphicLocation_RIGHT_TOP:
279 ePos = style::GraphicLocation_LEFT_MIDDLE==eHori ?
280 style::GraphicLocation_LEFT_TOP :
281 (style::GraphicLocation_MIDDLE_MIDDLE==eHori ?
282 style::GraphicLocation_MIDDLE_TOP :
283 style::GraphicLocation_RIGHT_TOP);
284 break;
286 case style::GraphicLocation_LEFT_MIDDLE:
287 case style::GraphicLocation_MIDDLE_MIDDLE:
288 case style::GraphicLocation_RIGHT_MIDDLE:
289 ePos = eHori;
290 break;
292 case style::GraphicLocation_LEFT_BOTTOM:
293 case style::GraphicLocation_MIDDLE_BOTTOM:
294 case style::GraphicLocation_RIGHT_BOTTOM:
295 ePos = style::GraphicLocation_LEFT_MIDDLE==eHori ?
296 style::GraphicLocation_LEFT_BOTTOM :
297 (style::GraphicLocation_MIDDLE_MIDDLE==eHori ?
298 style::GraphicLocation_MIDDLE_BOTTOM :
299 style::GraphicLocation_RIGHT_BOTTOM);
300 break;
301 default:
302 break;