1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3 *
4 * The Contents of this file are made available subject to the terms of
5 * either of the following licenses
6 *
7 * - GNU Lesser General Public License Version 2.1
8 * - Sun Industry Standards Source License Version 1.1
9 *
10 * Sun Microsystems Inc., October, 2000
11 *
12 * GNU Lesser General Public License Version 2.1
13 * =============================================
14 * Copyright 2000 by Sun Microsystems, Inc.
15 * 901 San Antonio Road, Palo Alto, CA 94303, USA
16 *
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License version 2.1, as published by the Free Software Foundation.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * MA 02111-1307 USA
30 *
31 *
32 * Sun Industry Standards Source License Version 1.1
33 * =================================================
34 * The contents of this file are subject to the Sun Industry Standards
35 * Source License Version 1.1 (the "License"); You may not use this file
36 * except in compliance with the License. You may obtain a copy of the
37 * License at http://www.openoffice.org/license.html.
38 *
39 * Software provided under this License is provided on an "AS IS" basis,
40 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
41 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
42 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
43 * See the License for the specific provisions governing your rights and
44 * obligations concerning the Software.
45 *
46 * The Initial Developer of the Original Code is: IBM Corporation
47 *
48 * Copyright: 2008 by IBM Corporation
49 *
50 * All Rights Reserved.
51 *
52 * Contributor(s): _______________________________________
53 *
54 *
55 ************************************************************************/
56 /*************************************************************************
57 * @file
58 * Number style for table cell.
59 ************************************************************************/
60 #include <xfilter/xfnumberstyle.hxx>
61
XFNumberStyle()62 XFNumberStyle::XFNumberStyle()
63 : m_eType(enumXFNumberNumber)
64 , m_nDecimalDigits(0)
65 , m_bGroup(false)
66 , m_aColor(0,0,0)
67 , m_bRedIfNegative(false)
68 , m_aNegativeColor(255,0,0)
69 {}
70
GetStyleFamily()71 enumXFStyle XFNumberStyle::GetStyleFamily()
72 {
73 return enumXFStyleNumber;
74 }
75
Equal(IXFStyle * pStyle)76 bool XFNumberStyle::Equal(IXFStyle *pStyle)
77 {
78 if( !pStyle || pStyle->GetStyleFamily() != enumXFStyleNumber )
79 return false;
80 XFNumberStyle *pOther = dynamic_cast<XFNumberStyle*>(pStyle);
81 if( !pOther )
82 return false;
83
84 if( m_eType != pOther->m_eType )
85 return false;
86 if( m_nDecimalDigits != pOther->m_nDecimalDigits )
87 return false;
88 if( m_bRedIfNegative != pOther->m_bRedIfNegative )
89 return false;
90 if( m_bGroup != pOther->m_bGroup )
91 return false;
92 if( m_aColor != pOther->m_aColor )
93 return false;
94 if( m_strPrefix != pOther->m_strPrefix )
95 return false;
96 if( m_strSuffix != pOther->m_strSuffix )
97 return false;
98
99 if( m_bRedIfNegative )
100 {
101 if( m_aNegativeColor != pOther->m_aNegativeColor )
102 return false;
103 if( m_strNegativePrefix != pOther->m_strNegativePrefix )
104 return false;
105 if( m_strNegativeSuffix != pOther->m_strNegativeSuffix )
106 return false;
107 }
108
109 if( m_eType == enuMXFNumberCurrency )
110 {
111 if( m_strCurrencySymbol != pOther->m_strCurrencySymbol )
112 return false;
113 }
114
115 return true;
116 }
117
ToXml(IXFStream * pStrm)118 void XFNumberStyle::ToXml(IXFStream *pStrm)
119 {
120 // for Text content number format
121 if (m_eType == enumXFText)
122 {
123 ToXml_StartElement(pStrm);
124 ToXml_EndElement(pStrm);
125 return;
126 }
127
128 if( !m_bRedIfNegative )
129 {
130 ToXml_Normal(pStrm);
131 }
132 else
133 {
134 ToXml_Negative(pStrm);
135 }
136 }
137
ToXml_StartElement(IXFStream * pStrm)138 void XFNumberStyle::ToXml_StartElement(IXFStream *pStrm)
139 {
140 IXFAttrList *pAttrList = pStrm->GetAttrList();
141 pAttrList->Clear();
142
143 pAttrList->AddAttribute( "style:name", GetStyleName() );
144 if( !GetParentStyleName().isEmpty() )
145 pAttrList->AddAttribute("style:parent-style-name",GetParentStyleName());
146
147 pAttrList->AddAttribute( "style:family", "data-style" );
148
149 if( m_eType == enumXFNumberNumber )
150 {
151 pStrm->StartElement( "number:number-style" );
152 }
153 else if( m_eType == enumXFNumberPercent )
154 {
155 pStrm->StartElement( "number:percentage-style" );
156 }
157 else if( m_eType == enuMXFNumberCurrency )
158 {
159 pStrm->StartElement( "number:currency-style" );
160 }
161 else if( m_eType == enumXFNumberScientific )
162 {
163 pStrm->StartElement( "number:number-style" );
164 }
165 // for Text content number format
166 else if (m_eType == enumXFText)
167 {
168 pStrm->StartElement( "number:text-content");
169 }
170
171 }
172
ToXml_EndElement(IXFStream * pStrm)173 void XFNumberStyle::ToXml_EndElement(IXFStream *pStrm)
174 {
175 IXFAttrList *pAttrList = pStrm->GetAttrList();
176 pAttrList->Clear();
177
178 pAttrList->AddAttribute( "style:name", GetStyleName() );
179 pAttrList->AddAttribute( "style:family", "data-style" );
180
181 if( m_eType == enumXFNumberNumber )
182 {
183 pStrm->EndElement( "number:number-style" );
184 }
185 else if( m_eType == enumXFNumberPercent )
186 {
187 pStrm->EndElement( "number:percentage-style" );
188 }
189 else if( m_eType == enuMXFNumberCurrency )
190 {
191 pStrm->EndElement( "number:currency-style" );
192 }
193 else if( m_eType == enumXFNumberScientific )
194 {
195 pStrm->EndElement( "number:number-style" );
196 }
197 // for Text content number format
198 else if (m_eType == enumXFText)
199 {
200 pStrm->EndElement( "number:text-content");
201 }
202 // END for Text content number format
203
204 }
ToXml_Normal(IXFStream * pStrm)205 void XFNumberStyle::ToXml_Normal(IXFStream *pStrm)
206 {
207 ToXml_StartElement(pStrm);
208
209 ToXml_Content(pStrm,false);
210
211 ToXml_EndElement(pStrm);
212 }
213
ToXml_Negative(IXFStream * pStrm)214 void XFNumberStyle::ToXml_Negative(IXFStream *pStrm)
215 {
216 IXFAttrList *pAttrList = pStrm->GetAttrList();
217 pAttrList->Clear();
218
219 OUString strStyleName = GetStyleName();
220 OUString strGEStyle = strStyleName + "PO";
221
222 SetStyleName(strGEStyle);
223 ToXml_Normal(pStrm);
224 SetStyleName(strStyleName);
225
226 ToXml_StartElement(pStrm);
227
228 ToXml_Content(pStrm,true);
229
230 pAttrList->Clear();
231 pAttrList->AddAttribute( "style:condition", "value()>=0" );
232 pAttrList->AddAttribute( "style:apply-style-name", strGEStyle );
233 pStrm->StartElement( "style:map" );
234 pStrm->EndElement( "style:map" );
235
236 ToXml_EndElement(pStrm);
237 }
238
ToXml_Content(IXFStream * pStrm,bool nagetive)239 void XFNumberStyle::ToXml_Content(IXFStream *pStrm, bool nagetive)
240 {
241 IXFAttrList *pAttrList = pStrm->GetAttrList();
242 pAttrList->Clear();
243 //color:
244 if( !nagetive )
245 pAttrList->AddAttribute( "fo:color", m_aColor.ToString() );
246 else
247 pAttrList->AddAttribute( "fo:color", m_aNegativeColor.ToString() );
248
249 pStrm->StartElement( "style:properties" );
250 pStrm->EndElement( "style:properties" );
251
252 if( !nagetive )
253 {
254 if( !m_strPrefix.isEmpty() )
255 {
256 pStrm->StartElement( "number:text" );
257 pStrm->Characters(m_strPrefix);
258 pStrm->EndElement( "number:text" );
259 }
260 }
261 else
262 {
263 if( m_strNegativePrefix.isEmpty() )
264 m_strNegativePrefix = m_strPrefix;
265 if( !m_strNegativePrefix.isEmpty() )
266 {
267 pStrm->StartElement( "number:text" );
268 // pStrm->Characters(m_strNegativePrefix);
269 pStrm->Characters(m_strNegativePrefix + "-");
270 pStrm->EndElement( "number:text" );
271 }
272 else
273 {
274 pStrm->StartElement( "number:text" );
275 pStrm->Characters("-");
276 pStrm->EndElement( "number:text" );
277 }
278 }
279
280 if( m_eType == enuMXFNumberCurrency )
281 {
282 if( !m_strCurrencySymbol.isEmpty() )
283 {
284 pStrm->StartElement( "number:currency-symbol" );
285 pStrm->Characters(m_strCurrencySymbol);
286 pStrm->EndElement( "number:currency-symbol" );
287 }
288 }
289
290 //When category of number format is scientific, the number can not be displayed normally in table.
291 if ( m_eType == enumXFNumberScientific )
292 {
293 pAttrList->Clear();
294 pAttrList->AddAttribute("number:decimal-places", OUString::number(m_nDecimalDigits));
295 pAttrList->AddAttribute("number:min-integer-digits", OUString::number(1));
296 pAttrList->AddAttribute("number:min-exponent-digits", OUString::number(2));
297 pStrm->StartElement( "number:scientific-number" );
298 pStrm->EndElement( "number:scientific-number" );
299 }
300 else
301 {
302 pAttrList->Clear();
303 pAttrList->AddAttribute("number:decimal-places", OUString::number(m_nDecimalDigits));
304 pAttrList->AddAttribute("number:min-integer-digits", OUString::number(1));
305
306 if( m_bGroup )
307 pAttrList->AddAttribute("number:grouping","true");
308 else
309 pAttrList->AddAttribute("number:grouping","false");
310
311 pStrm->StartElement( "number:number" );
312 pStrm->EndElement( "number:number" );
313 }
314
315 if( !nagetive )
316 {
317 if( !m_strSuffix.isEmpty() )
318 {
319 pStrm->StartElement( "number:text" );
320 pStrm->Characters(m_strSuffix);
321 pStrm->EndElement( "number:text" );
322 }
323 else
324 {
325 if( m_eType == enumXFNumberPercent )
326 {
327 pStrm->StartElement( "number:text" );
328 pStrm->Characters("%");
329 pStrm->EndElement( "number:text" );
330 }
331 }
332 }
333 else
334 {
335 if( m_strNegativeSuffix.isEmpty() )
336 m_strNegativeSuffix = m_strSuffix;
337 if( !m_strNegativeSuffix.isEmpty() )
338 {
339 pStrm->StartElement( "number:text" );
340 pStrm->Characters(m_strNegativeSuffix);
341 pStrm->EndElement( "number:text" );
342 }
343 else
344 {
345 if( m_eType == enumXFNumberPercent )
346 {
347 pStrm->StartElement( "number:text" );
348 pStrm->Characters("%");
349 pStrm->EndElement( "number:text" );
350 }
351 }
352 }
353 }
354
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
356