fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / optdlg / tpformula.cxx
blob2e4643e5d055331802aaeb55ab3226323259fc57
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 #undef SC_DLLIMPLEMENTATION
22 #include <scmod.hxx>
23 #include <svl/eitem.hxx>
24 #include <svl/stritem.hxx>
25 #include "tpformula.hxx"
26 #include "formulaopt.hxx"
27 #include "sc.hrc"
28 #include "scresid.hxx"
29 #include <formula/grammar.hxx>
30 #include "calcoptionsdlg.hxx"
31 #include <vcl/msgbox.hxx>
33 #include <unotools/localedatawrapper.hxx>
35 #include <com/sun/star/lang/Locale.hpp>
36 #include <com/sun/star/i18n/LocaleDataItem.hpp>
38 using ::com::sun::star::lang::Locale;
39 using ::com::sun::star::i18n::LocaleDataItem;
41 ScTpFormulaOptions::ScTpFormulaOptions(vcl::Window* pParent, const SfxItemSet& rCoreAttrs) :
42 SfxTabPage(pParent, "OptFormula", "modules/scalc/ui/optformula.ui", &rCoreAttrs),
43 mnDecSep(0)
45 get(mpLbFormulaSyntax, "formulasyntax");
46 get(mpCbEnglishFuncName, "englishfuncname");
47 get(mpBtnCustomCalcDefault, "calcdefault");
48 get(mpBtnCustomCalcCustom, "calccustom");
49 get(mpBtnCustomCalcDetails, "details");
50 get(mpEdSepFuncArg, "function");
51 get(mpEdSepArrayCol, "arraycolumn");
52 get(mpEdSepArrayRow, "arrayrow");
53 get(mpBtnSepReset, "reset");
54 get(mpLbOOXMLRecalcOptions, "ooxmlrecalc");
55 get(mpLbODFRecalcOptions, "odfrecalc");
57 mpLbFormulaSyntax->InsertEntry(ScResId(SCSTR_FORMULA_SYNTAX_CALC_A1).toString());
58 mpLbFormulaSyntax->InsertEntry(ScResId(SCSTR_FORMULA_SYNTAX_XL_A1).toString());
59 mpLbFormulaSyntax->InsertEntry(ScResId(SCSTR_FORMULA_SYNTAX_XL_R1C1).toString());
61 Link<> aLink = LINK( this, ScTpFormulaOptions, ButtonHdl );
62 mpBtnSepReset->SetClickHdl(aLink);
63 mpBtnCustomCalcDefault->SetClickHdl(aLink);
64 mpBtnCustomCalcCustom->SetClickHdl(aLink);
65 mpBtnCustomCalcDetails->SetClickHdl(aLink);
67 aLink = LINK( this, ScTpFormulaOptions, SepModifyHdl );
68 mpEdSepFuncArg->SetModifyHdl(aLink);
69 mpEdSepArrayCol->SetModifyHdl(aLink);
70 mpEdSepArrayRow->SetModifyHdl(aLink);
72 aLink = LINK( this, ScTpFormulaOptions, SepEditOnFocusHdl );
73 mpEdSepFuncArg->SetGetFocusHdl(aLink);
74 mpEdSepArrayCol->SetGetFocusHdl(aLink);
75 mpEdSepArrayRow->SetGetFocusHdl(aLink);
77 // Get the decimal separator for current locale.
78 OUString aSep = ScGlobal::GetpLocaleData()->getNumDecimalSep();
79 mnDecSep = aSep.isEmpty() ? sal_Unicode('.') : aSep[0];
82 ScTpFormulaOptions::~ScTpFormulaOptions()
84 disposeOnce();
87 void ScTpFormulaOptions::dispose()
89 mpLbFormulaSyntax.clear();
90 mpCbEnglishFuncName.clear();
91 mpBtnCustomCalcDefault.clear();
92 mpBtnCustomCalcCustom.clear();
93 mpBtnCustomCalcDetails.clear();
94 mpEdSepFuncArg.clear();
95 mpEdSepArrayCol.clear();
96 mpEdSepArrayRow.clear();
97 mpBtnSepReset.clear();
98 mpLbOOXMLRecalcOptions.clear();
99 mpLbODFRecalcOptions.clear();
100 SfxTabPage::dispose();
103 void ScTpFormulaOptions::ResetSeparators()
105 OUString aFuncArg, aArrayCol, aArrayRow;
106 ScFormulaOptions::GetDefaultFormulaSeparators(aFuncArg, aArrayCol, aArrayRow);
107 mpEdSepFuncArg->SetText(aFuncArg);
108 mpEdSepArrayCol->SetText(aArrayCol);
109 mpEdSepArrayRow->SetText(aArrayRow);
112 void ScTpFormulaOptions::OnFocusSeparatorInput(Edit* pEdit)
114 if (!pEdit)
115 return;
117 // Make sure the entire text is selected.
118 sal_Int32 nLen = pEdit->GetText().getLength();
119 Selection aSel(0, (sal_uInt16)nLen);
120 pEdit->SetSelection(aSel);
121 maOldSepValue = pEdit->GetText();
124 void ScTpFormulaOptions::UpdateCustomCalcRadioButtons(bool bDefault)
126 if (bDefault)
128 mpBtnCustomCalcDefault->Check(true);
129 mpBtnCustomCalcCustom->Check(false);
130 mpBtnCustomCalcDetails->Disable();
132 else
134 mpBtnCustomCalcDefault->Check(false);
135 mpBtnCustomCalcCustom->Check(true);
136 mpBtnCustomCalcDetails->Enable();
140 void ScTpFormulaOptions::LaunchCustomCalcSettings()
142 ScopedVclPtrInstance< ScCalcOptionsDialog > aDlg(this, maCurrentConfig);
143 if (aDlg->Execute() == RET_OK)
145 maCurrentConfig = aDlg->GetConfig();
149 bool ScTpFormulaOptions::IsValidSeparator(const OUString& rSep) const
151 if (rSep.getLength() != 1)
152 // Must be one-character long.
153 return false;
155 if (rSep.compareToAscii("a") >= 0 && rSep.compareToAscii("z") <= 0)
156 return false;
158 if (rSep.compareToAscii("A") >= 0 && rSep.compareToAscii("Z") <= 0)
159 return false;
161 sal_Unicode c = rSep[0];
162 switch (c)
164 case '+':
165 case '-':
166 case '/':
167 case '*':
168 case '<':
169 case '>':
170 case '[':
171 case ']':
172 case '(':
173 case ')':
174 case '"':
175 case '\'':
176 // Disallowed characters. Anything else we want to disallow ?
177 return false;
180 if (c == mnDecSep)
181 // decimal separator is not allowed.
182 return false;
184 return true;
187 bool ScTpFormulaOptions::IsValidSeparatorSet() const
189 // Make sure the column and row separators are different.
190 OUString aColStr = mpEdSepArrayCol->GetText();
191 OUString aRowStr = mpEdSepArrayRow->GetText();
192 if (aColStr == aRowStr)
193 return false;
195 return true;
198 IMPL_LINK( ScTpFormulaOptions, ButtonHdl, Button*, pBtn )
200 if (pBtn == mpBtnSepReset)
201 ResetSeparators();
202 else if (pBtn == mpBtnCustomCalcDefault)
203 UpdateCustomCalcRadioButtons(true);
204 else if (pBtn == mpBtnCustomCalcCustom)
205 UpdateCustomCalcRadioButtons(false);
206 else if (pBtn == mpBtnCustomCalcDetails)
207 LaunchCustomCalcSettings();
209 return 0;
212 IMPL_LINK( ScTpFormulaOptions, SepModifyHdl, Edit*, pEdit )
214 if (!pEdit)
215 return 0;
217 OUString aStr = pEdit->GetText();
218 if (aStr.getLength() > 1)
220 // In case the string is more than one character long, only grab the
221 // first character.
222 aStr = aStr.copy(0, 1);
223 pEdit->SetText(aStr);
226 if ((!IsValidSeparator(aStr) || !IsValidSeparatorSet()) && !maOldSepValue.isEmpty())
227 // Invalid separator. Restore the old value.
228 pEdit->SetText(maOldSepValue);
230 OnFocusSeparatorInput(pEdit);
231 return 0;
234 IMPL_LINK( ScTpFormulaOptions, SepEditOnFocusHdl, Edit*, pEdit )
236 OnFocusSeparatorInput(pEdit);
237 return 0;
240 VclPtr<SfxTabPage> ScTpFormulaOptions::Create(vcl::Window* pParent, const SfxItemSet* rCoreSet)
242 return VclPtr<ScTpFormulaOptions>::Create(pParent, *rCoreSet);
245 bool ScTpFormulaOptions::FillItemSet(SfxItemSet* rCoreSet)
247 bool bRet = false;
248 ScFormulaOptions aOpt;
249 bool bEnglishFuncName = mpCbEnglishFuncName->IsChecked();
250 sal_Int16 aSyntaxPos = mpLbFormulaSyntax->GetSelectEntryPos();
251 OUString aSep = mpEdSepFuncArg->GetText();
252 OUString aSepArrayCol = mpEdSepArrayCol->GetText();
253 OUString aSepArrayRow = mpEdSepArrayRow->GetText();
254 sal_Int16 nOOXMLRecalcMode = mpLbOOXMLRecalcOptions->GetSelectEntryPos();
255 sal_Int16 nODFRecalcMode = mpLbODFRecalcOptions->GetSelectEntryPos();
257 if (mpBtnCustomCalcDefault->IsChecked())
259 // When Default is selected, reset all the calc config settings to default.
260 maCurrentConfig.reset();
263 if ( mpLbFormulaSyntax->GetSavedValue() != aSyntaxPos
264 || mpCbEnglishFuncName->GetSavedValue() != (bEnglishFuncName ? 1 : 0)
265 || static_cast<OUString>(mpEdSepFuncArg->GetSavedValue()) != aSep
266 || static_cast<OUString>(mpEdSepArrayCol->GetSavedValue()) != aSepArrayCol
267 || static_cast<OUString>(mpEdSepArrayRow->GetSavedValue()) != aSepArrayRow
268 || mpLbOOXMLRecalcOptions->GetSavedValue() != nOOXMLRecalcMode
269 || mpLbODFRecalcOptions->GetSavedValue() != nODFRecalcMode
270 || maSavedConfig != maCurrentConfig )
272 ::formula::FormulaGrammar::Grammar eGram = ::formula::FormulaGrammar::GRAM_DEFAULT;
274 switch (aSyntaxPos)
276 case 0:
277 eGram = ::formula::FormulaGrammar::GRAM_NATIVE;
278 break;
279 case 1:
280 eGram = ::formula::FormulaGrammar::GRAM_NATIVE_XL_A1;
281 break;
282 case 2:
283 eGram = ::formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1;
284 break;
287 ScRecalcOptions eOOXMLRecalc = static_cast<ScRecalcOptions>(nOOXMLRecalcMode);
288 ScRecalcOptions eODFRecalc = static_cast<ScRecalcOptions>(nODFRecalcMode);
290 aOpt.SetFormulaSyntax(eGram);
291 aOpt.SetUseEnglishFuncName(bEnglishFuncName);
292 aOpt.SetFormulaSepArg(aSep);
293 aOpt.SetFormulaSepArrayCol(aSepArrayCol);
294 aOpt.SetFormulaSepArrayRow(aSepArrayRow);
295 aOpt.SetCalcConfig(maCurrentConfig);
296 aOpt.SetOOXMLRecalcOptions(eOOXMLRecalc);
297 aOpt.SetODFRecalcOptions(eODFRecalc);
299 rCoreSet->Put( ScTpFormulaItem( SID_SCFORMULAOPTIONS, aOpt ) );
300 bRet = true;
302 return bRet;
305 void ScTpFormulaOptions::Reset(const SfxItemSet* rCoreSet)
307 ScFormulaOptions aOpt;
308 const SfxPoolItem* pItem = NULL;
310 if(SfxItemState::SET == rCoreSet->GetItemState(SID_SCFORMULAOPTIONS, false , &pItem))
311 aOpt = static_cast<const ScTpFormulaItem*>(pItem)->GetFormulaOptions();
313 // formula grammar.
314 ::formula::FormulaGrammar::Grammar eGram = aOpt.GetFormulaSyntax();
316 switch (eGram)
318 case ::formula::FormulaGrammar::GRAM_NATIVE:
319 mpLbFormulaSyntax->SelectEntryPos(0);
320 break;
321 case ::formula::FormulaGrammar::GRAM_NATIVE_XL_A1:
322 mpLbFormulaSyntax->SelectEntryPos(1);
323 break;
324 case ::formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1:
325 mpLbFormulaSyntax->SelectEntryPos(2);
326 break;
327 default:
328 mpLbFormulaSyntax->SelectEntryPos(0);
331 mpLbFormulaSyntax->SaveValue();
333 ScRecalcOptions eOOXMLRecalc = aOpt.GetOOXMLRecalcOptions();
334 mpLbOOXMLRecalcOptions->SelectEntryPos(static_cast<sal_uInt16>(eOOXMLRecalc));
335 mpLbOOXMLRecalcOptions->SaveValue();
337 ScRecalcOptions eODFRecalc = aOpt.GetODFRecalcOptions();
338 mpLbODFRecalcOptions->SelectEntryPos(static_cast<sal_uInt16>(eODFRecalc));
339 mpLbODFRecalcOptions->SaveValue();
341 // english function name.
342 mpCbEnglishFuncName->Check( aOpt.GetUseEnglishFuncName() );
343 mpCbEnglishFuncName->SaveValue();
345 // Separators
346 OUString aSep = aOpt.GetFormulaSepArg();
347 OUString aSepArrayRow = aOpt.GetFormulaSepArrayRow();
348 OUString aSepArrayCol = aOpt.GetFormulaSepArrayCol();
350 if (aSep.getLength() == 1 && aSepArrayRow.getLength() == 1 && aSepArrayCol.getLength() == 1)
352 // Each separator must be one character long.
353 mpEdSepFuncArg->SetText(aSep);
354 mpEdSepArrayCol->SetText(aSepArrayCol);
355 mpEdSepArrayRow->SetText(aSepArrayRow);
357 mpEdSepFuncArg->SaveValue();
358 mpEdSepArrayCol->SaveValue();
359 mpEdSepArrayRow->SaveValue();
361 else
362 ResetSeparators();
364 // detailed calc settings.
365 ScFormulaOptions aDefaults;
367 maSavedConfig = aOpt.GetCalcConfig();
368 bool bDefault = aDefaults.GetCalcConfig() == maSavedConfig;
369 UpdateCustomCalcRadioButtons(bDefault);
371 maCurrentConfig = maSavedConfig;
374 SfxTabPage::sfxpg ScTpFormulaOptions::DeactivatePage(SfxItemSet* /*pSet*/)
376 // What's this method for ?
377 return KEEP_PAGE;
380 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */