nss: upgrade to release 3.73
[LibreOffice.git] / sc / source / ui / condformat / condformathelper.cxx
blob13078524946948be9a34197853f884fd86cf2d8e
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/.
8 */
10 #include <sal/config.h>
12 #include <o3tl/safeint.hxx>
13 #include <rtl/ustrbuf.hxx>
14 #include <condformathelper.hxx>
15 #include <globstr.hrc>
16 #include <scresid.hxx>
17 #include <conditio.hxx>
19 namespace {
21 OUString getTextForType(ScCondFormatEntryType eType)
23 switch(eType)
25 case CONDITION:
26 return ScResId(STR_COND_CONDITION);
27 case COLORSCALE:
28 return ScResId(STR_COND_COLORSCALE);
29 case DATABAR:
30 return ScResId(STR_COND_DATABAR);
31 case FORMULA:
32 return ScResId(STR_COND_FORMULA);
33 case ICONSET:
34 return ScResId(STR_COND_ICONSET);
35 case DATE:
36 return ScResId(STR_COND_DATE);
37 default:
38 break;
41 return OUString();
44 OUString getExpression(sal_Int32 nIndex)
46 switch(nIndex)
48 case 0:
49 return "=";
50 case 1:
51 return "<";
52 case 2:
53 return ">";
54 case 3:
55 return "<=";
56 case 4:
57 return ">=";
58 case 5:
59 return "!=";
60 case 6:
61 return ScResId(STR_COND_BETWEEN);
62 case 7:
63 return ScResId(STR_COND_NOTBETWEEN);
64 case 8:
65 return ScResId(STR_COND_DUPLICATE);
66 case 9:
67 return ScResId(STR_COND_UNIQUE);
69 case 11:
70 return ScResId(STR_COND_TOP10);
71 case 12:
72 return ScResId(STR_COND_BOTTOM10);
73 case 13:
74 return ScResId(STR_COND_TOP_PERCENT);
75 case 14:
76 return ScResId(STR_COND_BOTTOM_PERCENT);
77 case 15:
78 return ScResId(STR_COND_ABOVE_AVERAGE);
79 case 16:
80 return ScResId(STR_COND_BELOW_AVERAGE);
81 case 17:
82 return ScResId(STR_COND_ABOVE_EQUAL_AVERAGE);
83 case 18:
84 return ScResId(STR_COND_BELOW_EQUAL_AVERAGE);
85 case 19:
86 return ScResId(STR_COND_ERROR);
87 case 20:
88 return ScResId(STR_COND_NOERROR);
89 case 21:
90 return ScResId(STR_COND_BEGINS_WITH);
91 case 22:
92 return ScResId(STR_COND_ENDS_WITH);
93 case 23:
94 return ScResId(STR_COND_CONTAINS);
95 case 24:
96 return ScResId(STR_COND_NOT_CONTAINS);
98 case 10:
99 assert(false);
101 return OUString();
104 OUString getDateString(sal_Int32 nIndex)
106 const char* aCondStrs[] =
108 STR_COND_TODAY,
109 STR_COND_YESTERDAY,
110 STR_COND_TOMORROW,
111 STR_COND_LAST7DAYS,
112 STR_COND_THISWEEK,
113 STR_COND_LASTWEEK,
114 STR_COND_NEXTWEEK,
115 STR_COND_THISMONTH,
116 STR_COND_LASTMONTH,
117 STR_COND_NEXTMONTH,
118 STR_COND_THISYEAR,
119 STR_COND_LASTYEAR,
120 STR_COND_NEXTYEAR
123 if (nIndex >= 0 && o3tl::make_unsigned(nIndex) < SAL_N_ELEMENTS(aCondStrs))
124 return ScResId(aCondStrs[nIndex]);
125 assert(false);
126 return OUString();
131 OUString ScCondFormatHelper::GetExpression(const ScConditionalFormat& rFormat, const ScAddress& rPos)
133 OUStringBuffer aBuffer;
134 if(!rFormat.IsEmpty())
136 switch(rFormat.GetEntry(0)->GetType())
138 case ScFormatEntry::Type::Condition:
139 case ScFormatEntry::Type::ExtCondition:
141 const ScConditionEntry* pEntry = static_cast<const ScConditionEntry*>(rFormat.GetEntry(0));
142 ScConditionMode eMode = pEntry->GetOperation();
143 if(eMode == ScConditionMode::Direct)
145 aBuffer.append(getTextForType(FORMULA));
146 aBuffer.append(" ");
147 aBuffer.append(pEntry->GetExpression(rPos, 0));
149 else
151 aBuffer.append(getTextForType(CONDITION));
152 aBuffer.append(" ");
153 aBuffer.append(getExpression(static_cast<sal_Int32>(eMode)));
154 aBuffer.append(" ");
155 if(eMode == ScConditionMode::Between || eMode == ScConditionMode::NotBetween)
157 aBuffer.append(pEntry->GetExpression(rPos, 0));
158 aBuffer.append(" ");
159 aBuffer.append(ScResId(STR_COND_AND));
160 aBuffer.append(" ");
161 aBuffer.append(pEntry->GetExpression(rPos, 1));
163 else if(eMode <= ScConditionMode::NotEqual || eMode >= ScConditionMode::BeginsWith)
165 aBuffer.append(pEntry->GetExpression(rPos, 0));
170 break;
171 case ScFormatEntry::Type::Databar:
172 aBuffer.append(getTextForType(DATABAR));
173 break;
174 case ScFormatEntry::Type::Colorscale:
175 aBuffer.append(getTextForType(COLORSCALE));
176 break;
177 case ScFormatEntry::Type::Iconset:
178 aBuffer.append(getTextForType(ICONSET));
179 break;
180 case ScFormatEntry::Type::Date:
182 aBuffer.append(getTextForType(DATE));
183 aBuffer.append(" ");
184 sal_Int32 nDateEntry = static_cast<sal_Int32>(static_cast<const ScCondDateFormatEntry*>(rFormat.GetEntry(0))->GetDateType());
185 aBuffer.append(getDateString(nDateEntry));
187 break;
190 return aBuffer.makeStringAndClear();
193 OUString ScCondFormatHelper::GetExpression( ScCondFormatEntryType eType, sal_Int32 nIndex,
194 const OUString& aStr1, const OUString& aStr2 )
196 OUStringBuffer aBuffer(getTextForType(eType));
197 aBuffer.append(" ");
198 if(eType == CONDITION)
200 // workaround missing FORMULA option in the conditions case
201 // FORMULA is handled later
202 if(nIndex > 9)
203 ++nIndex;
204 aBuffer.append(getExpression(nIndex));
205 if(nIndex <= 7 || nIndex >= 19)
207 aBuffer.append(" ").append(aStr1);
208 if(nIndex == 6 || nIndex == 7)
210 aBuffer.append(" ");
211 aBuffer.append(ScResId(STR_COND_AND));
212 aBuffer.append(" ");
213 aBuffer.append(aStr2);
217 else if(eType == FORMULA)
219 aBuffer.append(" ").append(aStr1);
221 else if(eType == DATE)
223 aBuffer.append(getDateString(nIndex));
226 return aBuffer.makeStringAndClear();
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */