Bump for 3.6-28
[LibreOffice.git] / xmloff / source / style / backhdl.cxx
blob9ce71509ccc544faefd0224b04205aad35fed8fb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <tools/debug.hxx>
30 #include <backhdl.hxx>
31 #include <xmloff/xmltoken.hxx>
32 #include <xmloff/xmluconv.hxx>
33 #include <sax/tools/converter.hxx>
34 #include <com/sun/star/uno/Any.hxx>
35 #include <rtl/ustrbuf.hxx>
37 using ::rtl::OUString;
38 using ::rtl::OUStringBuffer;
40 using namespace ::com::sun::star;
41 using namespace ::xmloff::token;
43 SvXMLEnumMapEntry pXML_BrushHorizontalPos[] =
45 { XML_LEFT, style::GraphicLocation_LEFT_MIDDLE },
46 { XML_RIGHT, style::GraphicLocation_RIGHT_MIDDLE },
47 { XML_TOKEN_INVALID, 0 }
50 SvXMLEnumMapEntry pXML_BrushVerticalPos[] =
52 { XML_TOP, style::GraphicLocation_MIDDLE_TOP },
53 { XML_BOTTOM, style::GraphicLocation_MIDDLE_BOTTOM },
54 { XML_TOKEN_INVALID, 0 }
57 ///////////////////////////////////////////////////////////////////////////////
59 // class XMLBackGraphicPositionPropHdl
62 XMLBackGraphicPositionPropHdl::~XMLBackGraphicPositionPropHdl()
64 // Nothing to do
67 sal_Bool XMLBackGraphicPositionPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
69 sal_Bool bRet = sal_True;
70 style::GraphicLocation ePos = style::GraphicLocation_NONE, eTmp;
71 sal_uInt16 nTmp;
72 SvXMLTokenEnumerator aTokenEnum( rStrImpValue );
73 OUString aToken;
74 sal_Bool bHori = sal_False, bVert = sal_False;
76 while( bRet && aTokenEnum.getNextToken( aToken ) )
78 if( bHori && bVert )
80 bRet = sal_False;
82 else if( -1 != aToken.indexOf( sal_Unicode('%') ) )
84 sal_Int32 nPrc = 50;
85 if (::sax::Converter::convertPercent( nPrc, aToken ))
87 if( !bHori )
89 ePos = nPrc < 25 ? style::GraphicLocation_LEFT_TOP :
90 (nPrc < 75 ? style::GraphicLocation_MIDDLE_MIDDLE :
91 style::GraphicLocation_RIGHT_BOTTOM);
92 bHori = sal_True;
94 else
96 eTmp = nPrc < 25 ? style::GraphicLocation_LEFT_TOP:
97 (nPrc < 75 ? style::GraphicLocation_LEFT_MIDDLE :
98 style::GraphicLocation_LEFT_BOTTOM);
99 MergeXMLVertPos( ePos, eTmp );
100 bVert = sal_True;
103 else
105 // wrong percentage
106 bRet = sal_False;
109 else if( IsXMLToken( aToken, XML_CENTER ) )
111 if( bHori )
112 MergeXMLVertPos( ePos, style::GraphicLocation_MIDDLE_MIDDLE );
113 else if ( bVert )
114 MergeXMLHoriPos( ePos, style::GraphicLocation_MIDDLE_MIDDLE );
115 else
116 ePos = style::GraphicLocation_MIDDLE_MIDDLE;
118 else if( SvXMLUnitConverter::convertEnum( nTmp, aToken, pXML_BrushHorizontalPos ) )
120 if( bVert )
121 MergeXMLHoriPos( ePos, (style::GraphicLocation)nTmp );
122 else if( !bHori )
123 ePos = (style::GraphicLocation)nTmp;
124 else
125 bRet = sal_False;
127 bHori = sal_True;
129 else if( SvXMLUnitConverter::convertEnum( nTmp, aToken, pXML_BrushVerticalPos ) )
131 if( bHori )
132 MergeXMLVertPos( ePos, (style::GraphicLocation)nTmp );
133 else if( !bVert )
134 ePos = (style::GraphicLocation)nTmp;
135 else
136 bRet = sal_False;
137 bVert = sal_True;
139 else
141 bRet = sal_False;
145 bRet &= style::GraphicLocation_NONE != ePos;
146 if( bRet )
147 rValue <<= (style::GraphicLocation)(sal_uInt16)ePos;
149 return bRet;
152 sal_Bool XMLBackGraphicPositionPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
154 sal_Bool bRet = sal_True;
155 OUStringBuffer aOut;
157 style::GraphicLocation eLocation;
158 if( !( rValue >>= eLocation ) )
160 sal_Int32 nValue = 0;
161 if( rValue >>= nValue )
162 eLocation = (style::GraphicLocation)nValue;
163 else
164 bRet = sal_False;
167 if( bRet )
169 bRet = sal_False;
171 switch( eLocation )
173 case style::GraphicLocation_LEFT_TOP:
174 case style::GraphicLocation_MIDDLE_TOP:
175 case style::GraphicLocation_RIGHT_TOP:
176 aOut.append( GetXMLToken(XML_TOP) );
177 bRet = sal_True;
178 break;
179 case style::GraphicLocation_LEFT_MIDDLE:
180 case style::GraphicLocation_MIDDLE_MIDDLE:
181 case style::GraphicLocation_RIGHT_MIDDLE:
182 aOut.append( GetXMLToken(XML_CENTER) );
183 bRet = sal_True;
184 break;
185 case style::GraphicLocation_LEFT_BOTTOM:
186 case style::GraphicLocation_MIDDLE_BOTTOM:
187 case style::GraphicLocation_RIGHT_BOTTOM:
188 aOut.append( GetXMLToken(XML_BOTTOM) );
189 bRet = sal_True;
190 break;
191 default:
192 break;
195 if( bRet )
197 aOut.append( sal_Unicode( ' ' ) );
199 switch( eLocation )
201 case style::GraphicLocation_LEFT_TOP:
202 case style::GraphicLocation_LEFT_BOTTOM:
203 case style::GraphicLocation_LEFT_MIDDLE:
204 aOut.append( GetXMLToken(XML_LEFT) );
205 break;
206 case style::GraphicLocation_MIDDLE_TOP:
207 case style::GraphicLocation_MIDDLE_MIDDLE:
208 case style::GraphicLocation_MIDDLE_BOTTOM:
209 aOut.append( GetXMLToken(XML_CENTER) );
210 break;
211 case style::GraphicLocation_RIGHT_MIDDLE:
212 case style::GraphicLocation_RIGHT_TOP:
213 case style::GraphicLocation_RIGHT_BOTTOM:
214 aOut.append( GetXMLToken(XML_RIGHT) );
215 break;
216 default:
217 break;
222 rStrExpValue = aOut.makeStringAndClear();
224 return bRet;
227 void XMLBackGraphicPositionPropHdl::MergeXMLVertPos( style::GraphicLocation& ePos, style::GraphicLocation eVert ) const
229 switch( ePos )
231 case style::GraphicLocation_LEFT_TOP:
232 case style::GraphicLocation_LEFT_MIDDLE:
233 case style::GraphicLocation_LEFT_BOTTOM:
234 ePos = style::GraphicLocation_MIDDLE_TOP==eVert ?
235 style::GraphicLocation_LEFT_TOP :
236 (style::GraphicLocation_MIDDLE_MIDDLE==eVert ?
237 style::GraphicLocation_LEFT_MIDDLE :
238 style::GraphicLocation_LEFT_BOTTOM);
239 ePos = eVert;
240 break;
242 case style::GraphicLocation_MIDDLE_TOP:
243 case style::GraphicLocation_MIDDLE_MIDDLE:
244 case style::GraphicLocation_MIDDLE_BOTTOM:
245 ePos = eVert;
246 break;
248 case style::GraphicLocation_RIGHT_TOP:
249 case style::GraphicLocation_RIGHT_MIDDLE:
250 case style::GraphicLocation_RIGHT_BOTTOM:
251 ePos = style::GraphicLocation_MIDDLE_TOP==eVert ?
252 style::GraphicLocation_RIGHT_TOP :
253 (style::GraphicLocation_MIDDLE_MIDDLE==eVert ?
254 style::GraphicLocation_RIGHT_MIDDLE :
255 style::GraphicLocation_RIGHT_BOTTOM);
256 break;
257 default:
258 break;
262 void XMLBackGraphicPositionPropHdl::MergeXMLHoriPos( style::GraphicLocation& ePos, style::GraphicLocation eHori ) const
264 DBG_ASSERT( style::GraphicLocation_LEFT_MIDDLE==eHori || style::GraphicLocation_MIDDLE_MIDDLE==eHori || style::GraphicLocation_RIGHT_MIDDLE==eHori,
265 "lcl_frmitems_MergeXMLHoriPos: vertical pos must be middle" );
267 switch( ePos )
269 case style::GraphicLocation_LEFT_TOP:
270 case style::GraphicLocation_MIDDLE_TOP:
271 case style::GraphicLocation_RIGHT_TOP:
272 ePos = style::GraphicLocation_LEFT_MIDDLE==eHori ?
273 style::GraphicLocation_LEFT_TOP :
274 (style::GraphicLocation_MIDDLE_MIDDLE==eHori ?
275 style::GraphicLocation_MIDDLE_TOP :
276 style::GraphicLocation_RIGHT_TOP);
277 break;
279 case style::GraphicLocation_LEFT_MIDDLE:
280 case style::GraphicLocation_MIDDLE_MIDDLE:
281 case style::GraphicLocation_RIGHT_MIDDLE:
282 ePos = eHori;
283 break;
285 case style::GraphicLocation_LEFT_BOTTOM:
286 case style::GraphicLocation_MIDDLE_BOTTOM:
287 case style::GraphicLocation_RIGHT_BOTTOM:
288 ePos = style::GraphicLocation_LEFT_MIDDLE==eHori ?
289 style::GraphicLocation_LEFT_BOTTOM :
290 (style::GraphicLocation_MIDDLE_MIDDLE==eHori ?
291 style::GraphicLocation_MIDDLE_BOTTOM :
292 style::GraphicLocation_RIGHT_BOTTOM);
293 break;
294 default:
295 break;
299 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */