xref: /aoo41x/main/xmloff/source/style/backhdl.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 #include <tools/debug.hxx>
31 #include <backhdl.hxx>
32 #include <xmloff/xmltoken.hxx>
33 #include <xmloff/xmluconv.hxx>
34 #include <com/sun/star/uno/Any.hxx>
35 #include <rtl/ustrbuf.hxx>
36 
37 using ::rtl::OUString;
38 using ::rtl::OUStringBuffer;
39 
40 using namespace ::com::sun::star;
41 using namespace ::xmloff::token;
42 
43 SvXMLEnumMapEntry pXML_BrushHorizontalPos[] =
44 {
45 	{ XML_LEFT,		        style::GraphicLocation_LEFT_MIDDLE	 },
46 	{ XML_RIGHT,		    style::GraphicLocation_RIGHT_MIDDLE },
47 	{ XML_TOKEN_INVALID,    0		}
48 };
49 
50 SvXMLEnumMapEntry pXML_BrushVerticalPos[] =
51 {
52 	{ XML_TOP,			    style::GraphicLocation_MIDDLE_TOP	},
53 	{ XML_BOTTOM,		    style::GraphicLocation_MIDDLE_BOTTOM	},
54 	{ XML_TOKEN_INVALID,    0		}
55 };
56 
57 ///////////////////////////////////////////////////////////////////////////////
58 //
59 // class XMLBackGraphicPositionPropHdl
60 //
61 
62 XMLBackGraphicPositionPropHdl::~XMLBackGraphicPositionPropHdl()
63 {
64 	// Nothing to do
65 }
66 
67 sal_Bool XMLBackGraphicPositionPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
68 {
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;
75 
76 	while( bRet && aTokenEnum.getNextToken( aToken ) )
77 	{
78 		if( bHori && bVert )
79 		{
80 			bRet = sal_False;
81 		}
82 		else if( -1 != aToken.indexOf( sal_Unicode('%') ) )
83 		{
84 			sal_Int32 nPrc = 50;
85 			if( SvXMLUnitConverter::convertPercent( nPrc, aToken ) )
86 			{
87 				if( !bHori )
88 				{
89 					ePos = nPrc < 25 ? style::GraphicLocation_LEFT_TOP :
90 				                       (nPrc < 75 ? style::GraphicLocation_MIDDLE_MIDDLE :
91 									                style::GraphicLocation_RIGHT_BOTTOM);
92 					bHori = sal_True;
93 				}
94 				else
95 				{
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;
101 				}
102 			}
103 			else
104 			{
105 				// wrong percentage
106 				bRet = sal_False;
107 			}
108 		}
109 		else if( IsXMLToken( aToken, XML_CENTER ) )
110 		{
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;
117 		}
118 		else if( SvXMLUnitConverter::convertEnum( nTmp, aToken, pXML_BrushHorizontalPos ) )
119 		{
120 			if( bVert )
121 				MergeXMLHoriPos( ePos, (style::GraphicLocation)nTmp );
122 			else if( !bHori )
123 				ePos = (style::GraphicLocation)nTmp;
124 			else
125 				bRet = sal_False;
126 
127 			bHori = sal_True;
128 		}
129 		else if( SvXMLUnitConverter::convertEnum( nTmp, aToken, pXML_BrushVerticalPos ) )
130 		{
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;
138 		}
139 		else
140 		{
141 			bRet = sal_False;
142 		}
143 	}
144 
145 	bRet &= style::GraphicLocation_NONE != ePos;
146 	if( bRet )
147 		rValue <<= (style::GraphicLocation)(sal_uInt16)ePos;
148 
149 	return bRet;
150 }
151 
152 sal_Bool XMLBackGraphicPositionPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
153 {
154 	sal_Bool bRet = sal_True;
155 	OUStringBuffer aOut;
156 
157 	style::GraphicLocation eLocation;
158 	if( !( rValue >>= eLocation ) )
159 	{
160 		sal_Int32 nValue = 0;
161 		if( rValue >>= nValue )
162 			eLocation = (style::GraphicLocation)nValue;
163 		else
164 			bRet = sal_False;
165 	}
166 
167 	if( bRet )
168 	{
169 		bRet = sal_False;
170 
171 		switch( eLocation )
172 		{
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;
193 		}
194 
195 		if( bRet )
196 		{
197 			aOut.append( sal_Unicode( ' ' ) );
198 
199 			switch( eLocation )
200 			{
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;
218 			}
219 		}
220 	}
221 
222 	rStrExpValue = aOut.makeStringAndClear();
223 
224 	return bRet;
225 }
226 
227 void XMLBackGraphicPositionPropHdl::MergeXMLVertPos( style::GraphicLocation& ePos, style::GraphicLocation eVert ) const
228 {
229 	switch( ePos )
230 	{
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;
241 
242 	case style::GraphicLocation_MIDDLE_TOP:
243 	case style::GraphicLocation_MIDDLE_MIDDLE:
244 	case style::GraphicLocation_MIDDLE_BOTTOM:
245 		ePos = eVert;
246 		break;
247 
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;
259 	}
260 }
261 
262 void XMLBackGraphicPositionPropHdl::MergeXMLHoriPos( style::GraphicLocation& ePos, style::GraphicLocation eHori ) const
263 {
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" );
266 
267 	switch( ePos )
268 	{
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;
278 
279 	case style::GraphicLocation_LEFT_MIDDLE:
280 	case style::GraphicLocation_MIDDLE_MIDDLE:
281 	case style::GraphicLocation_RIGHT_MIDDLE:
282 		ePos = eHori;
283 		break;
284 
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;
296 	}
297 }
298 
299