xref: /trunk/main/xmloff/source/style/XMLClipPropertyHandler.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 "XMLClipPropertyHandler.hxx"
31 #include <com/sun/star/uno/Any.hxx>
32 #include <rtl/ustrbuf.hxx>
33 #include <com/sun/star/text/GraphicCrop.hpp>
34 #include <xmloff/xmluconv.hxx>
35 #include <xmloff/xmltoken.hxx>
36 
37 using ::rtl::OUString;
38 using ::rtl::OUStringBuffer;
39 
40 using namespace ::com::sun::star;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::text;
43 using namespace ::xmloff::token;
44 
45 ///////////////////////////////////////////////////////////////////////////////
46 //
47 // class XMLMeasurePropHdl
48 //
49 
50 XMLClipPropertyHandler::XMLClipPropertyHandler( sal_Bool bODF11 ) :
51     m_bODF11( bODF11 )
52 {
53 }
54 
55 XMLClipPropertyHandler::~XMLClipPropertyHandler()
56 {
57     // nothing to do
58 }
59 
60 bool XMLClipPropertyHandler::equals(
61         const Any& r1,
62         const Any& r2 ) const
63 {
64     GraphicCrop aCrop1, aCrop2;
65     r1 >>= aCrop1;
66     r2 >>= aCrop2;
67 
68     return aCrop1.Top == aCrop2.Top &&
69            aCrop1.Bottom == aCrop2.Bottom &&
70            aCrop1.Left == aCrop2.Left &&
71            aCrop1.Right == aCrop2.Right;
72 }
73 
74 sal_Bool XMLClipPropertyHandler::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
75 {
76     sal_Bool bRet = sal_False;
77     sal_Int32 nLen = rStrImpValue.getLength();
78     if( nLen > 6 &&
79         0 == rStrImpValue.compareTo( GetXMLToken(XML_RECT), 4 ) &&
80         rStrImpValue[4] == '(' &&
81         rStrImpValue[nLen-1] == ')' )
82     {
83         GraphicCrop aCrop;
84         OUString sTmp( rStrImpValue.copy( 5, nLen-6 ) );
85 
86         sal_Bool bHasComma = sTmp.indexOf( ',' ) != -1;
87         SvXMLTokenEnumerator aTokenEnum( sTmp, bHasComma ? ',' : ' ' );
88 
89         sal_uInt16 nPos = 0;
90         OUString aToken;
91         while( aTokenEnum.getNextToken( aToken ) )
92         {
93             sal_Int32 nVal = 0;
94             if( !IsXMLToken(aToken, XML_AUTO) &&
95                 !rUnitConverter.convertMeasure( nVal, aToken ) )
96                 break;
97 
98             switch( nPos )
99             {
100             case 0: aCrop.Top = nVal;   break;
101             case 1: aCrop.Right = nVal; break;
102             case 2: aCrop.Bottom = nVal;    break;
103             case 3: aCrop.Left = nVal;  break;
104             }
105             nPos++;
106         }
107 
108         bRet = (4 == nPos );
109         if( bRet )
110             rValue <<= aCrop;
111     }
112 
113     return bRet;
114 }
115 
116 sal_Bool XMLClipPropertyHandler::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
117 {
118     sal_Bool bRet = sal_False;
119     OUStringBuffer aOut(30);
120     GraphicCrop aCrop;
121 
122     if( rValue >>= aCrop )
123     {
124         aOut.append( GetXMLToken(XML_RECT) );
125         aOut.append( (sal_Unicode)'(' );
126         rUnitConverter.convertMeasure( aOut, aCrop.Top );
127         if( !m_bODF11 )
128             aOut.append( (sal_Unicode)',' );
129         aOut.append( (sal_Unicode)' ' );
130         rUnitConverter.convertMeasure( aOut, aCrop.Right );
131         if( !m_bODF11 )
132             aOut.append( (sal_Unicode)',' );
133         aOut.append( (sal_Unicode)' ' );
134         rUnitConverter.convertMeasure( aOut, aCrop.Bottom );
135         if( !m_bODF11 )
136             aOut.append( (sal_Unicode)',' );
137         aOut.append( (sal_Unicode)' ' );
138         rUnitConverter.convertMeasure( aOut, aCrop.Left );
139         aOut.append( (sal_Unicode)')' );
140         rStrExpValue = aOut.makeStringAndClear();
141 
142         bRet = sal_True;
143     }
144 
145     return bRet;
146 }
147