Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <tools/debug.hxx>
21 : #include <backhdl.hxx>
22 : #include <xmloff/xmltoken.hxx>
23 : #include <xmloff/xmluconv.hxx>
24 : #include <sax/tools/converter.hxx>
25 : #include <com/sun/star/uno/Any.hxx>
26 : #include <rtl/ustrbuf.hxx>
27 :
28 : using ::rtl::OUString;
29 : using ::rtl::OUStringBuffer;
30 :
31 : using namespace ::com::sun::star;
32 : using namespace ::xmloff::token;
33 :
34 : SvXMLEnumMapEntry pXML_BrushHorizontalPos[] =
35 : {
36 : { XML_LEFT, style::GraphicLocation_LEFT_MIDDLE },
37 : { XML_RIGHT, style::GraphicLocation_RIGHT_MIDDLE },
38 : { XML_TOKEN_INVALID, 0 }
39 : };
40 :
41 : SvXMLEnumMapEntry pXML_BrushVerticalPos[] =
42 : {
43 : { XML_TOP, style::GraphicLocation_MIDDLE_TOP },
44 : { XML_BOTTOM, style::GraphicLocation_MIDDLE_BOTTOM },
45 : { XML_TOKEN_INVALID, 0 }
46 : };
47 :
48 : ///////////////////////////////////////////////////////////////////////////////
49 : //
50 : // class XMLBackGraphicPositionPropHdl
51 : //
52 :
53 0 : XMLBackGraphicPositionPropHdl::~XMLBackGraphicPositionPropHdl()
54 : {
55 : // Nothing to do
56 0 : }
57 :
58 0 : sal_Bool XMLBackGraphicPositionPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
59 : {
60 0 : sal_Bool bRet = sal_True;
61 0 : style::GraphicLocation ePos = style::GraphicLocation_NONE, eTmp;
62 : sal_uInt16 nTmp;
63 0 : SvXMLTokenEnumerator aTokenEnum( rStrImpValue );
64 0 : OUString aToken;
65 0 : sal_Bool bHori = sal_False, bVert = sal_False;
66 :
67 0 : while( bRet && aTokenEnum.getNextToken( aToken ) )
68 : {
69 0 : if( bHori && bVert )
70 : {
71 0 : bRet = sal_False;
72 : }
73 0 : else if( -1 != aToken.indexOf( sal_Unicode('%') ) )
74 : {
75 0 : sal_Int32 nPrc = 50;
76 0 : if (::sax::Converter::convertPercent( nPrc, aToken ))
77 : {
78 0 : if( !bHori )
79 : {
80 : ePos = nPrc < 25 ? style::GraphicLocation_LEFT_TOP :
81 : (nPrc < 75 ? style::GraphicLocation_MIDDLE_MIDDLE :
82 0 : style::GraphicLocation_RIGHT_BOTTOM);
83 0 : bHori = sal_True;
84 : }
85 : else
86 : {
87 : eTmp = nPrc < 25 ? style::GraphicLocation_LEFT_TOP:
88 : (nPrc < 75 ? style::GraphicLocation_LEFT_MIDDLE :
89 0 : style::GraphicLocation_LEFT_BOTTOM);
90 0 : MergeXMLVertPos( ePos, eTmp );
91 0 : bVert = sal_True;
92 : }
93 : }
94 : else
95 : {
96 : // wrong percentage
97 0 : bRet = sal_False;
98 : }
99 : }
100 0 : else if( IsXMLToken( aToken, XML_CENTER ) )
101 : {
102 0 : if( bHori )
103 0 : MergeXMLVertPos( ePos, style::GraphicLocation_MIDDLE_MIDDLE );
104 0 : else if ( bVert )
105 0 : MergeXMLHoriPos( ePos, style::GraphicLocation_MIDDLE_MIDDLE );
106 : else
107 0 : ePos = style::GraphicLocation_MIDDLE_MIDDLE;
108 : }
109 0 : else if( SvXMLUnitConverter::convertEnum( nTmp, aToken, pXML_BrushHorizontalPos ) )
110 : {
111 0 : if( bVert )
112 0 : MergeXMLHoriPos( ePos, (style::GraphicLocation)nTmp );
113 0 : else if( !bHori )
114 0 : ePos = (style::GraphicLocation)nTmp;
115 : else
116 0 : bRet = sal_False;
117 :
118 0 : bHori = sal_True;
119 : }
120 0 : else if( SvXMLUnitConverter::convertEnum( nTmp, aToken, pXML_BrushVerticalPos ) )
121 : {
122 0 : if( bHori )
123 0 : MergeXMLVertPos( ePos, (style::GraphicLocation)nTmp );
124 0 : else if( !bVert )
125 0 : ePos = (style::GraphicLocation)nTmp;
126 : else
127 0 : bRet = sal_False;
128 0 : bVert = sal_True;
129 : }
130 : else
131 : {
132 0 : bRet = sal_False;
133 : }
134 : }
135 :
136 0 : bRet &= style::GraphicLocation_NONE != ePos;
137 0 : if( bRet )
138 0 : rValue <<= (style::GraphicLocation)(sal_uInt16)ePos;
139 :
140 0 : return bRet;
141 : }
142 :
143 0 : sal_Bool XMLBackGraphicPositionPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
144 : {
145 0 : sal_Bool bRet = sal_True;
146 0 : OUStringBuffer aOut;
147 :
148 : style::GraphicLocation eLocation;
149 0 : if( !( rValue >>= eLocation ) )
150 : {
151 0 : sal_Int32 nValue = 0;
152 0 : if( rValue >>= nValue )
153 0 : eLocation = (style::GraphicLocation)nValue;
154 : else
155 0 : bRet = sal_False;
156 : }
157 :
158 0 : if( bRet )
159 : {
160 0 : bRet = sal_False;
161 :
162 0 : switch( eLocation )
163 : {
164 : case style::GraphicLocation_LEFT_TOP:
165 : case style::GraphicLocation_MIDDLE_TOP:
166 : case style::GraphicLocation_RIGHT_TOP:
167 0 : aOut.append( GetXMLToken(XML_TOP) );
168 0 : bRet = sal_True;
169 0 : break;
170 : case style::GraphicLocation_LEFT_MIDDLE:
171 : case style::GraphicLocation_MIDDLE_MIDDLE:
172 : case style::GraphicLocation_RIGHT_MIDDLE:
173 0 : aOut.append( GetXMLToken(XML_CENTER) );
174 0 : bRet = sal_True;
175 0 : break;
176 : case style::GraphicLocation_LEFT_BOTTOM:
177 : case style::GraphicLocation_MIDDLE_BOTTOM:
178 : case style::GraphicLocation_RIGHT_BOTTOM:
179 0 : aOut.append( GetXMLToken(XML_BOTTOM) );
180 0 : bRet = sal_True;
181 0 : break;
182 : default:
183 0 : break;
184 : }
185 :
186 0 : if( bRet )
187 : {
188 0 : aOut.append( sal_Unicode( ' ' ) );
189 :
190 0 : switch( eLocation )
191 : {
192 : case style::GraphicLocation_LEFT_TOP:
193 : case style::GraphicLocation_LEFT_BOTTOM:
194 : case style::GraphicLocation_LEFT_MIDDLE:
195 0 : aOut.append( GetXMLToken(XML_LEFT) );
196 0 : break;
197 : case style::GraphicLocation_MIDDLE_TOP:
198 : case style::GraphicLocation_MIDDLE_MIDDLE:
199 : case style::GraphicLocation_MIDDLE_BOTTOM:
200 0 : aOut.append( GetXMLToken(XML_CENTER) );
201 0 : break;
202 : case style::GraphicLocation_RIGHT_MIDDLE:
203 : case style::GraphicLocation_RIGHT_TOP:
204 : case style::GraphicLocation_RIGHT_BOTTOM:
205 0 : aOut.append( GetXMLToken(XML_RIGHT) );
206 0 : break;
207 : default:
208 0 : break;
209 : }
210 : }
211 : }
212 :
213 0 : rStrExpValue = aOut.makeStringAndClear();
214 :
215 0 : return bRet;
216 : }
217 :
218 0 : void XMLBackGraphicPositionPropHdl::MergeXMLVertPos( style::GraphicLocation& ePos, style::GraphicLocation eVert ) const
219 : {
220 0 : switch( ePos )
221 : {
222 : case style::GraphicLocation_LEFT_TOP:
223 : case style::GraphicLocation_LEFT_MIDDLE:
224 : case style::GraphicLocation_LEFT_BOTTOM:
225 : ePos = style::GraphicLocation_MIDDLE_TOP==eVert ?
226 : style::GraphicLocation_LEFT_TOP :
227 : (style::GraphicLocation_MIDDLE_MIDDLE==eVert ?
228 : style::GraphicLocation_LEFT_MIDDLE :
229 0 : style::GraphicLocation_LEFT_BOTTOM);
230 0 : break;
231 :
232 : case style::GraphicLocation_MIDDLE_TOP:
233 : case style::GraphicLocation_MIDDLE_MIDDLE:
234 : case style::GraphicLocation_MIDDLE_BOTTOM:
235 0 : ePos = eVert;
236 0 : break;
237 :
238 : case style::GraphicLocation_RIGHT_TOP:
239 : case style::GraphicLocation_RIGHT_MIDDLE:
240 : case style::GraphicLocation_RIGHT_BOTTOM:
241 : ePos = style::GraphicLocation_MIDDLE_TOP==eVert ?
242 : style::GraphicLocation_RIGHT_TOP :
243 : (style::GraphicLocation_MIDDLE_MIDDLE==eVert ?
244 : style::GraphicLocation_RIGHT_MIDDLE :
245 0 : style::GraphicLocation_RIGHT_BOTTOM);
246 0 : break;
247 : default:
248 0 : break;
249 : }
250 0 : }
251 :
252 0 : void XMLBackGraphicPositionPropHdl::MergeXMLHoriPos( style::GraphicLocation& ePos, style::GraphicLocation eHori ) const
253 : {
254 : DBG_ASSERT( style::GraphicLocation_LEFT_MIDDLE==eHori || style::GraphicLocation_MIDDLE_MIDDLE==eHori || style::GraphicLocation_RIGHT_MIDDLE==eHori,
255 : "lcl_frmitems_MergeXMLHoriPos: vertical pos must be middle" );
256 :
257 0 : switch( ePos )
258 : {
259 : case style::GraphicLocation_LEFT_TOP:
260 : case style::GraphicLocation_MIDDLE_TOP:
261 : case style::GraphicLocation_RIGHT_TOP:
262 : ePos = style::GraphicLocation_LEFT_MIDDLE==eHori ?
263 : style::GraphicLocation_LEFT_TOP :
264 : (style::GraphicLocation_MIDDLE_MIDDLE==eHori ?
265 : style::GraphicLocation_MIDDLE_TOP :
266 0 : style::GraphicLocation_RIGHT_TOP);
267 0 : break;
268 :
269 : case style::GraphicLocation_LEFT_MIDDLE:
270 : case style::GraphicLocation_MIDDLE_MIDDLE:
271 : case style::GraphicLocation_RIGHT_MIDDLE:
272 0 : ePos = eHori;
273 0 : break;
274 :
275 : case style::GraphicLocation_LEFT_BOTTOM:
276 : case style::GraphicLocation_MIDDLE_BOTTOM:
277 : case style::GraphicLocation_RIGHT_BOTTOM:
278 : ePos = style::GraphicLocation_LEFT_MIDDLE==eHori ?
279 : style::GraphicLocation_LEFT_BOTTOM :
280 : (style::GraphicLocation_MIDDLE_MIDDLE==eHori ?
281 : style::GraphicLocation_MIDDLE_BOTTOM :
282 0 : style::GraphicLocation_RIGHT_BOTTOM);
283 0 : break;
284 : default:
285 0 : break;
286 : }
287 0 : }
288 :
289 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|