xref: /trunk/main/xmloff/source/style/shadwhdl.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 "shadwhdl.hxx"
31 #include <com/sun/star/uno/Any.hxx>
32 #include <rtl/ustrbuf.hxx>
33 
34 // --
35 #include <com/sun/star/table/ShadowFormat.hpp>
36 #include <xmloff/xmluconv.hxx>
37 #include <xmloff/xmltoken.hxx>
38 
39 using ::rtl::OUString;
40 using ::rtl::OUStringBuffer;
41 
42 using namespace ::com::sun::star;
43 using namespace ::xmloff::token;
44 
45 ///////////////////////////////////////////////////////////////////////////////
46 //
47 // class XMLMeasurePropHdl
48 //
49 
50 XMLShadowPropHdl::~XMLShadowPropHdl()
51 {
52 	// nothing to do
53 }
54 
55 sal_Bool XMLShadowPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
56 {
57 	sal_Bool bRet = sal_False;
58 	table::ShadowFormat aShadow;
59 	aShadow.Location = table::ShadowLocation_BOTTOM_RIGHT;
60 
61 	sal_Bool bColorFound = sal_False;
62 	sal_Bool bOffsetFound = sal_False;
63 	SvXMLTokenEnumerator aTokenEnum( rStrImpValue );
64 	Color aColor( 128,128, 128 );
65 	OUString aToken;
66 
67 	while( aTokenEnum.getNextToken( aToken ) )
68 	{
69 		if( IsXMLToken( aToken, XML_NONE ) )
70 		{
71 			aShadow.Location = table::ShadowLocation_NONE;
72 			bRet = sal_True;
73 			break;
74 		}
75 		else if( !bColorFound && aToken.compareToAscii( "#", 1 ) == 0 )
76 		{
77 			bRet = rUnitConverter.convertColor( aColor, aToken );
78 			if( !bRet )
79 				return sal_False;
80 
81 			bColorFound = sal_True;
82 		}
83 		else if( !bOffsetFound )
84 		{
85 			sal_Int32 nX = 0, nY = 0;
86 
87 			bRet = rUnitConverter.convertMeasure( nX, aToken );
88 			if( bRet && aTokenEnum.getNextToken( aToken ) )
89 				bRet = rUnitConverter.convertMeasure( nY, aToken );
90 
91 			if( bRet )
92 			{
93 				if( nX < 0 )
94 				{
95 					if( nY < 0 )
96 						aShadow.Location = table::ShadowLocation_TOP_LEFT;
97 					else
98 						aShadow.Location = table::ShadowLocation_BOTTOM_LEFT;
99 				}
100 				else
101 				{
102 					if( nY < 0 )
103 						aShadow.Location = table::ShadowLocation_TOP_RIGHT;
104 					else
105 						aShadow.Location = table::ShadowLocation_BOTTOM_RIGHT;
106 				}
107 
108 				if( nX < 0 ) nX *= -1;
109 				if( nY < 0 ) nY *= -1;
110 
111 				aShadow.ShadowWidth = sal::static_int_cast< sal_Int16 >(
112                     (nX + nY) >> 1);
113 			}
114 		}
115 	}
116 
117 	if( bRet && ( bColorFound || bOffsetFound ) )
118 	{
119 		aShadow.IsTransparent = aColor.GetTransparency() > 0;
120 		aShadow.Color = aColor.GetColor();
121 		bRet = sal_True;
122 	}
123 
124 	rValue <<= aShadow;
125 
126 	return bRet;
127 }
128 
129 sal_Bool XMLShadowPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
130 {
131 	sal_Bool bRet = sal_False;
132   	OUStringBuffer aOut;
133 	table::ShadowFormat aShadow;
134 
135 	if( rValue >>= aShadow )
136 	{
137 		sal_Int32 nX = 1, nY = 1;
138 
139 		switch( aShadow.Location )
140 		{
141 			case table::ShadowLocation_TOP_LEFT:
142 				nX = -1;
143 				nY = -1;
144 				break;
145 			case table::ShadowLocation_TOP_RIGHT:
146 				nY = -1;
147 				break;
148 			case table::ShadowLocation_BOTTOM_LEFT:
149 				nX = -1;
150 				break;
151 			case table::ShadowLocation_BOTTOM_RIGHT:
152 				break;
153 			case table::ShadowLocation_NONE:
154 			default:
155 				rStrExpValue = GetXMLToken(XML_NONE);
156 				return sal_True;
157 		}
158 
159 		nX *= aShadow.ShadowWidth;
160 		nY *= aShadow.ShadowWidth;
161 
162 		rUnitConverter.convertColor( aOut, aShadow.Color );
163 
164 		aOut.append( sal_Unicode(' ') );
165 		rUnitConverter.convertMeasure( aOut, nX );
166 		aOut.append( sal_Unicode(' ') );
167 		rUnitConverter.convertMeasure( aOut, nY );
168 
169 		rStrExpValue = aOut.makeStringAndClear();
170 
171 		bRet = sal_True;
172 	}
173 
174 	return bRet;
175 }
176