1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
23 #include <svl/eitem.hxx>
24 #include <svl/stritem.hxx>
25 #include "tpformula.hxx"
26 #include "formulaopt.hxx"
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
),
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()
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
)
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
)
128 mpBtnCustomCalcDefault
->Check(true);
129 mpBtnCustomCalcCustom
->Check(false);
130 mpBtnCustomCalcDetails
->Disable();
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.
155 if (rSep
.compareToAscii("a") >= 0 && rSep
.compareToAscii("z") <= 0)
158 if (rSep
.compareToAscii("A") >= 0 && rSep
.compareToAscii("Z") <= 0)
161 sal_Unicode c
= rSep
[0];
176 // Disallowed characters. Anything else we want to disallow ?
181 // decimal separator is not allowed.
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
)
198 IMPL_LINK( ScTpFormulaOptions
, ButtonHdl
, Button
*, pBtn
)
200 if (pBtn
== mpBtnSepReset
)
202 else if (pBtn
== mpBtnCustomCalcDefault
)
203 UpdateCustomCalcRadioButtons(true);
204 else if (pBtn
== mpBtnCustomCalcCustom
)
205 UpdateCustomCalcRadioButtons(false);
206 else if (pBtn
== mpBtnCustomCalcDetails
)
207 LaunchCustomCalcSettings();
212 IMPL_LINK( ScTpFormulaOptions
, SepModifyHdl
, Edit
*, pEdit
)
217 OUString aStr
= pEdit
->GetText();
218 if (aStr
.getLength() > 1)
220 // In case the string is more than one character long, only grab the
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
);
234 IMPL_LINK( ScTpFormulaOptions
, SepEditOnFocusHdl
, Edit
*, pEdit
)
236 OnFocusSeparatorInput(pEdit
);
240 VclPtr
<SfxTabPage
> ScTpFormulaOptions::Create(vcl::Window
* pParent
, const SfxItemSet
* rCoreSet
)
242 return VclPtr
<ScTpFormulaOptions
>::Create(pParent
, *rCoreSet
);
245 bool ScTpFormulaOptions::FillItemSet(SfxItemSet
* rCoreSet
)
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
;
277 eGram
= ::formula::FormulaGrammar::GRAM_NATIVE
;
280 eGram
= ::formula::FormulaGrammar::GRAM_NATIVE_XL_A1
;
283 eGram
= ::formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1
;
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
) );
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();
314 ::formula::FormulaGrammar::Grammar eGram
= aOpt
.GetFormulaSyntax();
318 case ::formula::FormulaGrammar::GRAM_NATIVE
:
319 mpLbFormulaSyntax
->SelectEntryPos(0);
321 case ::formula::FormulaGrammar::GRAM_NATIVE_XL_A1
:
322 mpLbFormulaSyntax
->SelectEntryPos(1);
324 case ::formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1
:
325 mpLbFormulaSyntax
->SelectEntryPos(2);
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();
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();
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 ?
380 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */