Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #undef SC_DLLIMPLEMENTATION
21 :
22 : //------------------------------------------------------------------
23 :
24 : #include <scmod.hxx>
25 : #include <svl/eitem.hxx>
26 : #include <svl/stritem.hxx>
27 : #include "tpformula.hxx"
28 : #include "formulaopt.hxx"
29 : #include "optdlg.hrc"
30 : #include "scresid.hxx"
31 : #include "formula/grammar.hxx"
32 : #include "calcoptionsdlg.hxx"
33 : #include "vcl/msgbox.hxx"
34 :
35 : #include <unotools/localedatawrapper.hxx>
36 :
37 : #include <com/sun/star/lang/Locale.hpp>
38 : #include <com/sun/star/i18n/LocaleDataItem.hpp>
39 :
40 : using ::com::sun::star::lang::Locale;
41 : using ::com::sun::star::i18n::LocaleDataItem;
42 :
43 0 : ScTpFormulaOptions::ScTpFormulaOptions(Window* pParent, const SfxItemSet& rCoreAttrs) :
44 : SfxTabPage(pParent, "OptFormula", "modules/scalc/ui/optformula.ui", rCoreAttrs),
45 0 : mnDecSep(0)
46 : {
47 0 : get(mpLbFormulaSyntax, "formulasyntax");
48 0 : get(mpCbEnglishFuncName, "englishfuncname");
49 0 : get(mpBtnCustomCalcDefault, "calcdefault");
50 0 : get(mpBtnCustomCalcCustom, "calccustom");
51 0 : get(mpBtnCustomCalcDetails, "details");
52 0 : get(mpEdSepFuncArg, "function");
53 0 : get(mpEdSepArrayCol, "arraycolumn");
54 0 : get(mpEdSepArrayRow, "arrayrow");
55 0 : get(mpBtnSepReset, "reset");
56 0 : get(mpLbOOXMLRecalcOptions, "ooxmlrecalc");
57 0 : get(mpLbODFRecalcOptions, "odfrecalc");
58 :
59 0 : mpLbFormulaSyntax->InsertEntry(ScResId(SCSTR_FORMULA_SYNTAX_CALC_A1).toString());
60 0 : mpLbFormulaSyntax->InsertEntry(ScResId(SCSTR_FORMULA_SYNTAX_XL_A1).toString());
61 0 : mpLbFormulaSyntax->InsertEntry(ScResId(SCSTR_FORMULA_SYNTAX_XL_R1C1).toString());
62 :
63 0 : Link aLink = LINK( this, ScTpFormulaOptions, ButtonHdl );
64 0 : mpBtnSepReset->SetClickHdl(aLink);
65 0 : mpBtnCustomCalcDefault->SetClickHdl(aLink);
66 0 : mpBtnCustomCalcCustom->SetClickHdl(aLink);
67 0 : mpBtnCustomCalcDetails->SetClickHdl(aLink);
68 :
69 0 : aLink = LINK( this, ScTpFormulaOptions, SepModifyHdl );
70 0 : mpEdSepFuncArg->SetModifyHdl(aLink);
71 0 : mpEdSepArrayCol->SetModifyHdl(aLink);
72 0 : mpEdSepArrayRow->SetModifyHdl(aLink);
73 :
74 0 : aLink = LINK( this, ScTpFormulaOptions, SepEditOnFocusHdl );
75 0 : mpEdSepFuncArg->SetGetFocusHdl(aLink);
76 0 : mpEdSepArrayCol->SetGetFocusHdl(aLink);
77 0 : mpEdSepArrayRow->SetGetFocusHdl(aLink);
78 :
79 : // Get the decimal separator for current locale.
80 0 : OUString aSep = ScGlobal::GetpLocaleData()->getNumDecimalSep();
81 0 : mnDecSep = aSep.isEmpty() ? sal_Unicode('.') : aSep[0];
82 0 : }
83 :
84 0 : ScTpFormulaOptions::~ScTpFormulaOptions()
85 : {
86 0 : }
87 :
88 0 : void ScTpFormulaOptions::ResetSeparators()
89 : {
90 0 : OUString aFuncArg, aArrayCol, aArrayRow;
91 0 : ScFormulaOptions::GetDefaultFormulaSeparators(aFuncArg, aArrayCol, aArrayRow);
92 0 : mpEdSepFuncArg->SetText(aFuncArg);
93 0 : mpEdSepArrayCol->SetText(aArrayCol);
94 0 : mpEdSepArrayRow->SetText(aArrayRow);
95 0 : }
96 :
97 0 : void ScTpFormulaOptions::OnFocusSeparatorInput(Edit* pEdit)
98 : {
99 0 : if (!pEdit)
100 0 : return;
101 :
102 : // Make sure the entire text is selected.
103 0 : sal_Int32 nLen = pEdit->GetText().getLength();
104 0 : Selection aSel(0, (sal_uInt16)nLen);
105 0 : pEdit->SetSelection(aSel);
106 0 : maOldSepValue = pEdit->GetText();
107 : }
108 :
109 0 : void ScTpFormulaOptions::UpdateCustomCalcRadioButtons(bool bDefault)
110 : {
111 0 : if (bDefault)
112 : {
113 0 : mpBtnCustomCalcDefault->Check(true);
114 0 : mpBtnCustomCalcCustom->Check(false);
115 0 : mpBtnCustomCalcDetails->Disable();
116 : }
117 : else
118 : {
119 0 : mpBtnCustomCalcDefault->Check(false);
120 0 : mpBtnCustomCalcCustom->Check(true);
121 0 : mpBtnCustomCalcDetails->Enable();
122 : }
123 0 : }
124 :
125 0 : void ScTpFormulaOptions::LaunchCustomCalcSettings()
126 : {
127 0 : ScCalcOptionsDialog aDlg(this, maCurrentConfig);
128 0 : if (aDlg.Execute() == RET_OK)
129 : {
130 0 : maCurrentConfig = aDlg.GetConfig();
131 0 : }
132 0 : }
133 :
134 0 : bool ScTpFormulaOptions::IsValidSeparator(const OUString& rSep) const
135 : {
136 0 : if (rSep.getLength() != 1)
137 : // Must be one-character long.
138 0 : return false;
139 :
140 0 : if (rSep.compareToAscii("a") >= 0 && rSep.compareToAscii("z") <= 0)
141 0 : return false;
142 :
143 0 : if (rSep.compareToAscii("A") >= 0 && rSep.compareToAscii("Z") <= 0)
144 0 : return false;
145 :
146 0 : sal_Unicode c = rSep.getStr()[0];
147 0 : switch (c)
148 : {
149 : case '+':
150 : case '-':
151 : case '/':
152 : case '*':
153 : case '<':
154 : case '>':
155 : case '[':
156 : case ']':
157 : case '(':
158 : case ')':
159 : case '"':
160 : case '\'':
161 : // Disallowed characters. Anything else we want to disallow ?
162 0 : return false;
163 : }
164 :
165 0 : if (c == mnDecSep)
166 : // decimal separator is not allowed.
167 0 : return false;
168 :
169 0 : return true;
170 : }
171 :
172 0 : bool ScTpFormulaOptions::IsValidSeparatorSet() const
173 : {
174 : // Make sure the column and row separators are different.
175 0 : String aColStr = mpEdSepArrayCol->GetText();
176 0 : String aRowStr = mpEdSepArrayRow->GetText();
177 0 : if (aColStr == aRowStr)
178 0 : return false;
179 :
180 0 : return true;
181 : }
182 :
183 0 : IMPL_LINK( ScTpFormulaOptions, ButtonHdl, Button*, pBtn )
184 : {
185 0 : if (pBtn == mpBtnSepReset)
186 0 : ResetSeparators();
187 0 : else if (pBtn == mpBtnCustomCalcDefault)
188 0 : UpdateCustomCalcRadioButtons(true);
189 0 : else if (pBtn == mpBtnCustomCalcCustom)
190 0 : UpdateCustomCalcRadioButtons(false);
191 0 : else if (pBtn == mpBtnCustomCalcDetails)
192 0 : LaunchCustomCalcSettings();
193 :
194 0 : return 0;
195 : }
196 :
197 0 : IMPL_LINK( ScTpFormulaOptions, SepModifyHdl, Edit*, pEdit )
198 : {
199 0 : if (!pEdit)
200 0 : return 0;
201 :
202 0 : String aStr = pEdit->GetText();
203 0 : if (aStr.Len() > 1)
204 : {
205 : // In case the string is more than one character long, only grab the
206 : // first character.
207 0 : aStr = aStr.Copy(0, 1);
208 0 : pEdit->SetText(aStr);
209 : }
210 :
211 0 : if ((!IsValidSeparator(aStr) || !IsValidSeparatorSet()) && !maOldSepValue.isEmpty())
212 : // Invalid separator. Restore the old value.
213 0 : pEdit->SetText(maOldSepValue);
214 :
215 0 : OnFocusSeparatorInput(pEdit);
216 0 : return 0;
217 : }
218 :
219 0 : IMPL_LINK( ScTpFormulaOptions, SepEditOnFocusHdl, Edit*, pEdit )
220 : {
221 0 : OnFocusSeparatorInput(pEdit);
222 0 : return 0;
223 : }
224 :
225 0 : SfxTabPage* ScTpFormulaOptions::Create(Window* pParent, const SfxItemSet& rCoreSet)
226 : {
227 0 : return new ScTpFormulaOptions(pParent, rCoreSet);
228 : }
229 :
230 0 : sal_Bool ScTpFormulaOptions::FillItemSet(SfxItemSet& rCoreSet)
231 : {
232 0 : bool bRet = false;
233 0 : ScFormulaOptions aOpt;
234 0 : sal_Bool bEnglishFuncName = mpCbEnglishFuncName->IsChecked();
235 0 : sal_Int16 aSyntaxPos = mpLbFormulaSyntax->GetSelectEntryPos();
236 0 : OUString aSep = mpEdSepFuncArg->GetText();
237 0 : OUString aSepArrayCol = mpEdSepArrayCol->GetText();
238 0 : OUString aSepArrayRow = mpEdSepArrayRow->GetText();
239 0 : sal_Int16 nOOXMLRecalcMode = mpLbOOXMLRecalcOptions->GetSelectEntryPos();
240 0 : sal_Int16 nODFRecalcMode = mpLbODFRecalcOptions->GetSelectEntryPos();
241 :
242 0 : if (mpBtnCustomCalcDefault->IsChecked())
243 : {
244 : // When Default is selected, reset all the calc config settings to default.
245 0 : maCurrentConfig.reset();
246 : }
247 :
248 0 : if ( mpLbFormulaSyntax->GetSavedValue() != aSyntaxPos
249 0 : || mpCbEnglishFuncName->GetSavedValue() != bEnglishFuncName
250 0 : || static_cast<OUString>(mpEdSepFuncArg->GetSavedValue()) != aSep
251 0 : || static_cast<OUString>(mpEdSepArrayCol->GetSavedValue()) != aSepArrayCol
252 0 : || static_cast<OUString>(mpEdSepArrayRow->GetSavedValue()) != aSepArrayRow
253 0 : || mpLbOOXMLRecalcOptions->GetSavedValue() != nOOXMLRecalcMode
254 0 : || mpLbODFRecalcOptions->GetSavedValue() != nODFRecalcMode
255 0 : || maSavedConfig != maCurrentConfig )
256 : {
257 0 : ::formula::FormulaGrammar::Grammar eGram = ::formula::FormulaGrammar::GRAM_DEFAULT;
258 :
259 0 : switch (aSyntaxPos)
260 : {
261 : case 0:
262 0 : eGram = ::formula::FormulaGrammar::GRAM_NATIVE;
263 0 : break;
264 : case 1:
265 0 : eGram = ::formula::FormulaGrammar::GRAM_NATIVE_XL_A1;
266 0 : break;
267 : case 2:
268 0 : eGram = ::formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1;
269 0 : break;
270 : }
271 :
272 0 : ScRecalcOptions eOOXMLRecalc = static_cast<ScRecalcOptions>(nOOXMLRecalcMode);
273 0 : ScRecalcOptions eODFRecalc = static_cast<ScRecalcOptions>(nODFRecalcMode);
274 :
275 0 : aOpt.SetFormulaSyntax(eGram);
276 0 : aOpt.SetUseEnglishFuncName(bEnglishFuncName);
277 0 : aOpt.SetFormulaSepArg(aSep);
278 0 : aOpt.SetFormulaSepArrayCol(aSepArrayCol);
279 0 : aOpt.SetFormulaSepArrayRow(aSepArrayRow);
280 0 : aOpt.SetCalcConfig(maCurrentConfig);
281 0 : aOpt.SetOOXMLRecalcOptions(eOOXMLRecalc);
282 0 : aOpt.SetODFRecalcOptions(eODFRecalc);
283 :
284 0 : rCoreSet.Put( ScTpFormulaItem( SID_SCFORMULAOPTIONS, aOpt ) );
285 0 : bRet = true;
286 : }
287 0 : return bRet;
288 : }
289 :
290 0 : void ScTpFormulaOptions::Reset(const SfxItemSet& rCoreSet)
291 : {
292 0 : ScFormulaOptions aOpt;
293 0 : const SfxPoolItem* pItem = NULL;
294 :
295 0 : if(SFX_ITEM_SET == rCoreSet.GetItemState(SID_SCFORMULAOPTIONS, false , &pItem))
296 0 : aOpt = ((const ScTpFormulaItem*)pItem)->GetFormulaOptions();
297 :
298 : // formula grammar.
299 0 : ::formula::FormulaGrammar::Grammar eGram = aOpt.GetFormulaSyntax();
300 :
301 0 : switch (eGram)
302 : {
303 : case ::formula::FormulaGrammar::GRAM_NATIVE:
304 0 : mpLbFormulaSyntax->SelectEntryPos(0);
305 0 : break;
306 : case ::formula::FormulaGrammar::GRAM_NATIVE_XL_A1:
307 0 : mpLbFormulaSyntax->SelectEntryPos(1);
308 0 : break;
309 : case ::formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1:
310 0 : mpLbFormulaSyntax->SelectEntryPos(2);
311 0 : break;
312 : default:
313 0 : mpLbFormulaSyntax->SelectEntryPos(0);
314 : }
315 :
316 0 : mpLbFormulaSyntax->SaveValue();
317 :
318 0 : ScRecalcOptions eOOXMLRecalc = aOpt.GetOOXMLRecalcOptions();
319 0 : mpLbOOXMLRecalcOptions->SelectEntryPos(static_cast<sal_uInt16>(eOOXMLRecalc));
320 0 : mpLbOOXMLRecalcOptions->SaveValue();
321 :
322 0 : ScRecalcOptions eODFRecalc = aOpt.GetODFRecalcOptions();
323 0 : mpLbODFRecalcOptions->SelectEntryPos(static_cast<sal_uInt16>(eODFRecalc));
324 0 : mpLbODFRecalcOptions->SaveValue();
325 :
326 : // english function name.
327 0 : mpCbEnglishFuncName->Check( aOpt.GetUseEnglishFuncName() );
328 0 : mpCbEnglishFuncName->SaveValue();
329 :
330 : // Separators
331 0 : OUString aSep = aOpt.GetFormulaSepArg();
332 0 : OUString aSepArrayRow = aOpt.GetFormulaSepArrayRow();
333 0 : OUString aSepArrayCol = aOpt.GetFormulaSepArrayCol();
334 :
335 0 : if (aSep.getLength() == 1 && aSepArrayRow.getLength() == 1 && aSepArrayCol.getLength() == 1)
336 : {
337 : // Each separator must be one character long.
338 0 : mpEdSepFuncArg->SetText(aSep);
339 0 : mpEdSepArrayCol->SetText(aSepArrayCol);
340 0 : mpEdSepArrayRow->SetText(aSepArrayRow);
341 :
342 0 : mpEdSepFuncArg->SaveValue();
343 0 : mpEdSepArrayCol->SaveValue();
344 0 : mpEdSepArrayRow->SaveValue();
345 : }
346 : else
347 0 : ResetSeparators();
348 :
349 : // detailed calc settings.
350 0 : ScFormulaOptions aDefaults;
351 :
352 0 : maSavedConfig = aOpt.GetCalcConfig();
353 0 : bool bDefault = aDefaults.GetCalcConfig() == maSavedConfig;
354 0 : UpdateCustomCalcRadioButtons(bDefault);
355 :
356 0 : maCurrentConfig = maSavedConfig;
357 0 : }
358 :
359 0 : int ScTpFormulaOptions::DeactivatePage(SfxItemSet* /*pSet*/)
360 : {
361 : // What's this method for ?
362 0 : return KEEP_PAGE;
363 0 : }
364 :
365 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|