bump product version to 6.4.0.3
[LibreOffice.git] / editeng / source / items / itemtype.cxx
blob9b3996e799754ca9abd90441aedc789219f6bc7a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <osl/diagnose.h>
23 #include <vcl/outdev.hxx>
24 #include <editeng/editrids.hrc>
25 #include <unotools/intlwrapper.hxx>
26 #include <unotools/localedatawrapper.hxx>
27 #include <editeng/itemtype.hxx>
28 #include <editeng/eerdll.hxx>
29 #include <rtl/ustrbuf.hxx>
32 OUString GetMetricText( long nVal, MapUnit eSrcUnit, MapUnit eDestUnit, const IntlWrapper* pIntl )
34 bool bNeg = false;
35 bool bShowAtLeastOneDecimalDigit = true;
36 sal_Int32 nRet = 0;
38 if ( nVal < 0 )
40 bNeg = true;
41 nVal *= -1;
44 switch ( eDestUnit )
46 case MapUnit::Map100thMM:
47 case MapUnit::Map10thMM:
48 case MapUnit::MapMM:
49 case MapUnit::MapCM:
51 nRet = OutputDevice::LogicToLogic( nVal, eSrcUnit, MapUnit::Map100thMM );
53 switch ( eDestUnit )
55 case MapUnit::Map100thMM: nRet *= 1000; break;
56 case MapUnit::Map10thMM: nRet *= 100; break;
57 case MapUnit::MapMM: nRet *= 10; break;
58 default: ;//prevent warning
60 break;
63 case MapUnit::Map1000thInch:
64 case MapUnit::Map100thInch:
65 case MapUnit::Map10thInch:
66 case MapUnit::MapInch:
68 nRet = OutputDevice::LogicToLogic( nVal, eSrcUnit, MapUnit::Map1000thInch );
70 switch ( eDestUnit )
72 case MapUnit::Map1000thInch: nRet *= 1000; break;
73 case MapUnit::Map100thInch: nRet *= 100; break;
74 case MapUnit::Map10thInch: nRet *= 10; break;
75 default: ;//prevent warning
77 break;
80 case MapUnit::MapPoint:
81 // fractions of a point are used, e.g., for font size
82 nRet = OutputDevice::LogicToLogic(nVal, eSrcUnit, MapUnit::MapTwip) * 50;
83 bShowAtLeastOneDecimalDigit = false;
84 break;
86 case MapUnit::MapTwip:
87 case MapUnit::MapPixel:
88 return OUString::number( OutputDevice::LogicToLogic(
89 nVal, eSrcUnit, eDestUnit ));
91 default:
92 OSL_FAIL( "not supported mapunit" );
93 return OUString();
96 if ( MapUnit::MapCM == eDestUnit || MapUnit::MapInch == eDestUnit )
98 sal_Int32 nMod = nRet % 10;
100 if ( nMod > 4 )
101 nRet += 10 - nMod;
102 else if ( nMod > 0 )
103 nRet -= nMod;
106 OUStringBuffer sRet;
108 if ( bNeg )
109 sRet.append('-');
111 long nDiff = 1000;
112 for( int nDigits = 4; nDigits; --nDigits, nDiff /= 10 )
114 if ( nRet < nDiff )
115 sRet.append('0');
116 else
117 sRet.append(nRet / nDiff);
118 nRet %= nDiff;
119 if( 4 == nDigits && (bShowAtLeastOneDecimalDigit || nRet) )
121 if(pIntl)
122 sRet.append(pIntl->getLocaleData()->getNumDecimalSep());
123 else
124 sRet.append(',');
125 if( !nRet )
127 sRet.append('0');
128 break;
131 else if( !nRet )
132 break;
134 return sRet.makeStringAndClear();
137 OUString GetColorString( const Color& rCol )
139 if (rCol == COL_AUTO)
140 return EditResId(RID_SVXSTR_AUTOMATIC);
142 static const Color aColAry[] = {
143 COL_BLACK, COL_BLUE, COL_GREEN, COL_CYAN,
144 COL_RED, COL_MAGENTA, COL_BROWN, COL_GRAY,
145 COL_LIGHTGRAY, COL_LIGHTBLUE, COL_LIGHTGREEN, COL_LIGHTCYAN,
146 COL_LIGHTRED, COL_LIGHTMAGENTA, COL_YELLOW, COL_WHITE };
148 sal_uInt16 nColor = 0;
149 while ( nColor < SAL_N_ELEMENTS(aColAry) &&
150 aColAry[nColor] != rCol.GetRGBColor() )
152 nColor += 1;
155 static const char* RID_SVXITEMS_COLORS[] =
157 RID_SVXITEMS_COLOR_BLACK,
158 RID_SVXITEMS_COLOR_BLUE,
159 RID_SVXITEMS_COLOR_GREEN,
160 RID_SVXITEMS_COLOR_CYAN,
161 RID_SVXITEMS_COLOR_RED,
162 RID_SVXITEMS_COLOR_MAGENTA,
163 RID_SVXITEMS_COLOR_BROWN,
164 RID_SVXITEMS_COLOR_GRAY,
165 RID_SVXITEMS_COLOR_LIGHTGRAY,
166 RID_SVXITEMS_COLOR_LIGHTBLUE,
167 RID_SVXITEMS_COLOR_LIGHTGREEN,
168 RID_SVXITEMS_COLOR_LIGHTCYAN,
169 RID_SVXITEMS_COLOR_LIGHTRED,
170 RID_SVXITEMS_COLOR_LIGHTMAGENTA,
171 RID_SVXITEMS_COLOR_YELLOW,
172 RID_SVXITEMS_COLOR_WHITE
175 static_assert(SAL_N_ELEMENTS(aColAry) == SAL_N_ELEMENTS(RID_SVXITEMS_COLORS), "must match");
177 OUString sStr;
178 if ( nColor < SAL_N_ELEMENTS(aColAry) )
179 sStr = EditResId(RID_SVXITEMS_COLORS[nColor]);
181 if ( sStr.isEmpty() )
183 sStr += "RGB(" +
184 OUString::number( rCol.GetRed() ) + cpDelim +
185 OUString::number( rCol.GetGreen() ) + cpDelim +
186 OUString::number( rCol.GetBlue() ) + ")";
188 return sStr;
191 const char* GetMetricId( MapUnit eUnit )
193 const char* pId = RID_SVXITEMS_METRIC_MM;
195 switch ( eUnit )
197 case MapUnit::Map100thMM:
198 case MapUnit::Map10thMM:
199 case MapUnit::MapMM:
200 pId = RID_SVXITEMS_METRIC_MM;
201 break;
203 case MapUnit::MapCM:
204 pId = RID_SVXITEMS_METRIC_CM;
205 break;
207 case MapUnit::Map1000thInch:
208 case MapUnit::Map100thInch:
209 case MapUnit::Map10thInch:
210 case MapUnit::MapInch:
211 pId = RID_SVXITEMS_METRIC_INCH;
212 break;
214 case MapUnit::MapPoint:
215 pId = RID_SVXITEMS_METRIC_POINT;
216 break;
218 case MapUnit::MapTwip:
219 pId = RID_SVXITEMS_METRIC_TWIP;
220 break;
222 case MapUnit::MapPixel:
223 pId = RID_SVXITEMS_METRIC_PIXEL;
224 break;
226 default:
227 OSL_FAIL( "not supported mapunit" );
229 return pId;
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */