xref: /trunk/main/xmloff/source/style/backhdl.cxx (revision 63bba73c)
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 <tools/debug.hxx>
27 #include <backhdl.hxx>
28 #include <xmloff/xmltoken.hxx>
29 #include <xmloff/xmluconv.hxx>
30 #include <com/sun/star/uno/Any.hxx>
31 #include <rtl/ustrbuf.hxx>
32 
33 using ::rtl::OUString;
34 using ::rtl::OUStringBuffer;
35 
36 using namespace ::com::sun::star;
37 using namespace ::xmloff::token;
38 
39 SvXMLEnumMapEntry pXML_BrushHorizontalPos[] =
40 {
41 	{ XML_LEFT,		        style::GraphicLocation_LEFT_MIDDLE	 },
42 	{ XML_RIGHT,		    style::GraphicLocation_RIGHT_MIDDLE },
43 	{ XML_TOKEN_INVALID,    0		}
44 };
45 
46 SvXMLEnumMapEntry pXML_BrushVerticalPos[] =
47 {
48 	{ XML_TOP,			    style::GraphicLocation_MIDDLE_TOP	},
49 	{ XML_BOTTOM,		    style::GraphicLocation_MIDDLE_BOTTOM	},
50 	{ XML_TOKEN_INVALID,    0		}
51 };
52 
53 ///////////////////////////////////////////////////////////////////////////////
54 //
55 // class XMLBackGraphicPositionPropHdl
56 //
57 
~XMLBackGraphicPositionPropHdl()58 XMLBackGraphicPositionPropHdl::~XMLBackGraphicPositionPropHdl()
59 {
60 	// Nothing to do
61 }
62 
importXML(const OUString & rStrImpValue,uno::Any & rValue,const SvXMLUnitConverter &) const63 sal_Bool XMLBackGraphicPositionPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
64 {
65 	sal_Bool bRet = sal_True;
66 	style::GraphicLocation ePos = style::GraphicLocation_NONE, eTmp;
67 	sal_uInt16 nTmp;
68 	SvXMLTokenEnumerator aTokenEnum( rStrImpValue );
69 	OUString aToken;
70 	sal_Bool bHori = sal_False, bVert = sal_False;
71 
72 	while( bRet && aTokenEnum.getNextToken( aToken ) )
73 	{
74 		if( bHori && bVert )
75 		{
76 			bRet = sal_False;
77 		}
78 		else if( -1 != aToken.indexOf( sal_Unicode('%') ) )
79 		{
80 			sal_Int32 nPrc = 50;
81 			if( SvXMLUnitConverter::convertPercent( nPrc, aToken ) )
82 			{
83 				if( !bHori )
84 				{
85 					ePos = nPrc < 25 ? style::GraphicLocation_LEFT_TOP :
86 				                       (nPrc < 75 ? style::GraphicLocation_MIDDLE_MIDDLE :
87 									                style::GraphicLocation_RIGHT_BOTTOM);
88 					bHori = sal_True;
89 				}
90 				else
91 				{
92 					eTmp = nPrc < 25 ? style::GraphicLocation_LEFT_TOP:
93 				                       (nPrc < 75 ? style::GraphicLocation_LEFT_MIDDLE :
94 									                style::GraphicLocation_LEFT_BOTTOM);
95 					MergeXMLVertPos( ePos, eTmp );
96 					bVert = sal_True;
97 				}
98 			}
99 			else
100 			{
101 				// wrong percentage
102 				bRet = sal_False;
103 			}
104 		}
105 		else if( IsXMLToken( aToken, XML_CENTER ) )
106 		{
107 			if( bHori )
108 				MergeXMLVertPos( ePos, style::GraphicLocation_MIDDLE_MIDDLE );
109 			else if ( bVert )
110 				MergeXMLHoriPos( ePos, style::GraphicLocation_MIDDLE_MIDDLE );
111 			else
112 				ePos = style::GraphicLocation_MIDDLE_MIDDLE;
113 		}
114 		else if( SvXMLUnitConverter::convertEnum( nTmp, aToken, pXML_BrushHorizontalPos ) )
115 		{
116 			if( bVert )
117 				MergeXMLHoriPos( ePos, (style::GraphicLocation)nTmp );
118 			else if( !bHori )
119 				ePos = (style::GraphicLocation)nTmp;
120 			else
121 				bRet = sal_False;
122 
123 			bHori = sal_True;
124 		}
125 		else if( SvXMLUnitConverter::convertEnum( nTmp, aToken, pXML_BrushVerticalPos ) )
126 		{
127 			if( bHori )
128 				MergeXMLVertPos( ePos, (style::GraphicLocation)nTmp );
129 			else if( !bVert )
130 				ePos = (style::GraphicLocation)nTmp;
131 			else
132 				bRet = sal_False;
133 			bVert = sal_True;
134 		}
135 		else
136 		{
137 			bRet = sal_False;
138 		}
139 	}
140 
141 	bRet &= style::GraphicLocation_NONE != ePos;
142 	if( bRet )
143 		rValue <<= (style::GraphicLocation)(sal_uInt16)ePos;
144 
145 	return bRet;
146 }
147 
exportXML(OUString & rStrExpValue,const uno::Any & rValue,const SvXMLUnitConverter &) const148 sal_Bool XMLBackGraphicPositionPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
149 {
150 	sal_Bool bRet = sal_True;
151 	OUStringBuffer aOut;
152 
153 	style::GraphicLocation eLocation;
154 	if( !( rValue >>= eLocation ) )
155 	{
156 		sal_Int32 nValue = 0;
157 		if( rValue >>= nValue )
158 			eLocation = (style::GraphicLocation)nValue;
159 		else
160 			bRet = sal_False;
161 	}
162 
163 	if( bRet )
164 	{
165 		bRet = sal_False;
166 
167 		switch( eLocation )
168 		{
169 		case style::GraphicLocation_LEFT_TOP:
170 		case style::GraphicLocation_MIDDLE_TOP:
171 		case style::GraphicLocation_RIGHT_TOP:
172 			aOut.append( GetXMLToken(XML_TOP) );
173 			bRet = sal_True;
174 			break;
175 		case style::GraphicLocation_LEFT_MIDDLE:
176 		case style::GraphicLocation_MIDDLE_MIDDLE:
177 		case style::GraphicLocation_RIGHT_MIDDLE:
178 			aOut.append( GetXMLToken(XML_CENTER) );
179 			bRet = sal_True;
180 			break;
181 		case style::GraphicLocation_LEFT_BOTTOM:
182 		case style::GraphicLocation_MIDDLE_BOTTOM:
183 		case style::GraphicLocation_RIGHT_BOTTOM:
184 			aOut.append( GetXMLToken(XML_BOTTOM) );
185 			bRet = sal_True;
186 			break;
187 		default:
188 			break;
189 		}
190 
191 		if( bRet )
192 		{
193 			aOut.append( sal_Unicode( ' ' ) );
194 
195 			switch( eLocation )
196 			{
197 			case style::GraphicLocation_LEFT_TOP:
198 			case style::GraphicLocation_LEFT_BOTTOM:
199 			case style::GraphicLocation_LEFT_MIDDLE:
200 				aOut.append( GetXMLToken(XML_LEFT) );
201 				break;
202 			case style::GraphicLocation_MIDDLE_TOP:
203 			case style::GraphicLocation_MIDDLE_MIDDLE:
204 			case style::GraphicLocation_MIDDLE_BOTTOM:
205 				aOut.append( GetXMLToken(XML_CENTER) );
206 				break;
207 			case style::GraphicLocation_RIGHT_MIDDLE:
208 			case style::GraphicLocation_RIGHT_TOP:
209 			case style::GraphicLocation_RIGHT_BOTTOM:
210 				aOut.append( GetXMLToken(XML_RIGHT) );
211 				break;
212 			default:
213 				break;
214 			}
215 		}
216 	}
217 
218 	rStrExpValue = aOut.makeStringAndClear();
219 
220 	return bRet;
221 }
222 
MergeXMLVertPos(style::GraphicLocation & ePos,style::GraphicLocation eVert) const223 void XMLBackGraphicPositionPropHdl::MergeXMLVertPos( style::GraphicLocation& ePos, style::GraphicLocation eVert ) const
224 {
225 	switch( ePos )
226 	{
227 	case style::GraphicLocation_LEFT_TOP:
228 	case style::GraphicLocation_LEFT_MIDDLE:
229 	case style::GraphicLocation_LEFT_BOTTOM:
230 		ePos = style::GraphicLocation_MIDDLE_TOP==eVert ?
231 			   style::GraphicLocation_LEFT_TOP :
232 		      (style::GraphicLocation_MIDDLE_MIDDLE==eVert ?
233 			   style::GraphicLocation_LEFT_MIDDLE :
234 			   style::GraphicLocation_LEFT_BOTTOM);
235 		ePos = eVert;
236 		break;
237 
238 	case style::GraphicLocation_MIDDLE_TOP:
239 	case style::GraphicLocation_MIDDLE_MIDDLE:
240 	case style::GraphicLocation_MIDDLE_BOTTOM:
241 		ePos = eVert;
242 		break;
243 
244 	case style::GraphicLocation_RIGHT_TOP:
245 	case style::GraphicLocation_RIGHT_MIDDLE:
246 	case style::GraphicLocation_RIGHT_BOTTOM:
247 		ePos = style::GraphicLocation_MIDDLE_TOP==eVert ?
248 			   style::GraphicLocation_RIGHT_TOP :
249 		      (style::GraphicLocation_MIDDLE_MIDDLE==eVert ?
250 			   style::GraphicLocation_RIGHT_MIDDLE :
251 			   style::GraphicLocation_RIGHT_BOTTOM);
252 		break;
253 	default:
254 		break;
255 	}
256 }
257 
MergeXMLHoriPos(style::GraphicLocation & ePos,style::GraphicLocation eHori) const258 void XMLBackGraphicPositionPropHdl::MergeXMLHoriPos( style::GraphicLocation& ePos, style::GraphicLocation eHori ) const
259 {
260 	DBG_ASSERT( style::GraphicLocation_LEFT_MIDDLE==eHori || style::GraphicLocation_MIDDLE_MIDDLE==eHori || style::GraphicLocation_RIGHT_MIDDLE==eHori,
261 				"lcl_frmitems_MergeXMLHoriPos: vertical pos must be middle" );
262 
263 	switch( ePos )
264 	{
265 	case style::GraphicLocation_LEFT_TOP:
266 	case style::GraphicLocation_MIDDLE_TOP:
267 	case style::GraphicLocation_RIGHT_TOP:
268 		ePos = style::GraphicLocation_LEFT_MIDDLE==eHori ?
269 			   style::GraphicLocation_LEFT_TOP :
270 		      (style::GraphicLocation_MIDDLE_MIDDLE==eHori ?
271 			   style::GraphicLocation_MIDDLE_TOP :
272 			   style::GraphicLocation_RIGHT_TOP);
273 		break;
274 
275 	case style::GraphicLocation_LEFT_MIDDLE:
276 	case style::GraphicLocation_MIDDLE_MIDDLE:
277 	case style::GraphicLocation_RIGHT_MIDDLE:
278 		ePos = eHori;
279 		break;
280 
281 	case style::GraphicLocation_LEFT_BOTTOM:
282 	case style::GraphicLocation_MIDDLE_BOTTOM:
283 	case style::GraphicLocation_RIGHT_BOTTOM:
284 		ePos = style::GraphicLocation_LEFT_MIDDLE==eHori ?
285 			   style::GraphicLocation_LEFT_BOTTOM :
286 		      (style::GraphicLocation_MIDDLE_MIDDLE==eHori ?
287 			   style::GraphicLocation_MIDDLE_BOTTOM :
288 			   style::GraphicLocation_RIGHT_BOTTOM);
289 		break;
290 	default:
291 		break;
292 	}
293 }
294 
295