merge the formfield patch from ooo-build
[ooovba.git] / sc / source / ui / optdlg / tpformula.cxx
blob0c1d1431f5b497987f4e77f83bd4c755b6e79c7d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tpcalc.hxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
34 #undef SC_DLLIMPLEMENTATION
38 //------------------------------------------------------------------
40 #include "tpformula.hxx"
41 #include "optdlg.hrc"
42 #include "scresid.hxx"
43 #include "formula/grammar.hxx"
44 #include "docoptio.hxx"
45 #include "global.hxx"
47 #include <unotools/localedatawrapper.hxx>
49 #include <com/sun/star/lang/Locale.hpp>
50 #include <com/sun/star/i18n/LocaleDataItem.hpp>
52 using ::rtl::OUString;
53 using ::com::sun::star::lang::Locale;
54 using ::com::sun::star::i18n::LocaleDataItem;
56 ScTpFormulaOptions::ScTpFormulaOptions(Window* pParent, const SfxItemSet& rCoreAttrs) :
57 SfxTabPage(pParent, ScResId(RID_SCPAGE_FORMULA), rCoreAttrs),
59 maFlFormulaOpt(this, ScResId(FL_FORMULA_OPTIONS)),
60 maFtFormulaSyntax(this, ScResId(FT_FORMULA_SYNTAX)),
61 maLbFormulaSyntax(this, ScResId(LB_FORMULA_SYNTAX)),
62 maFlFormulaSeps(this, ScResId(FL_FORMULA_SEPS)),
63 maFtSepFuncArg(this, ScResId(FT_FORMULA_SEP_ARG)),
64 maEdSepFuncArg(this, ScResId(ED_FORMULA_SEP_ARG)),
65 maFtSepArrayCol(this, ScResId(FT_FORMULA_SEP_ARRAY_C)),
66 maEdSepArrayCol(this, ScResId(ED_FORMULA_SEP_ARRAY_C)),
67 maFtSepArrayRow(this, ScResId(FT_FORMULA_SEP_ARRAY_R)),
68 maEdSepArrayRow(this, ScResId(ED_FORMULA_SEP_ARRAY_R)),
69 maBtnSepReset(this, ScResId(BTN_FORMULA_SEP_RESET)),
71 mpOldOptions(NULL),
72 mpNewOptions(NULL),
73 mnDecSep(0)
75 FreeResource();
77 const ScTpCalcItem& rItem = static_cast<const ScTpCalcItem&>(
78 rCoreAttrs.Get(GetWhich(SID_SCDOCOPTIONS)));
79 mpOldOptions.reset(new ScDocOptions(rItem.GetDocOptions()));
80 mpNewOptions.reset(new ScDocOptions(rItem.GetDocOptions()));
82 Init();
85 ScTpFormulaOptions::~ScTpFormulaOptions()
89 void ScTpFormulaOptions::Init()
91 Link aLink = LINK( this, ScTpFormulaOptions, ButtonHdl );
92 maBtnSepReset.SetClickHdl(aLink);
94 aLink = LINK( this, ScTpFormulaOptions, SepModifyHdl );
95 maEdSepFuncArg.SetModifyHdl(aLink);
96 maEdSepArrayCol.SetModifyHdl(aLink);
97 maEdSepArrayRow.SetModifyHdl(aLink);
99 aLink = LINK( this, ScTpFormulaOptions, SepEditOnFocusHdl );
100 maEdSepFuncArg.SetGetFocusHdl(aLink);
101 maEdSepArrayCol.SetGetFocusHdl(aLink);
102 maEdSepArrayRow.SetGetFocusHdl(aLink);
104 // Get the decimal separator for current locale.
105 String aSep = mpOldOptions->GetLocaleDataWrapper().getNumDecimalSep();
106 mnDecSep = aSep.Len() ? aSep.GetChar(0) : sal_Unicode('.');
109 void ScTpFormulaOptions::ResetSeparators()
111 ScDocOptions aOpt;
112 maEdSepFuncArg.SetText(aOpt.GetFormulaSepArg());
113 maEdSepArrayCol.SetText(aOpt.GetFormulaSepArrayCol());
114 maEdSepArrayRow.SetText(aOpt.GetFormulaSepArrayRow());
117 void ScTpFormulaOptions::OnFocusSeparatorInput(Edit* pEdit)
119 if (!pEdit)
120 return;
122 // Make sure the entire text is selected.
123 xub_StrLen nLen = pEdit->GetText().Len();
124 Selection aSel(0, nLen);
125 pEdit->SetSelection(aSel);
126 maOldSepValue = pEdit->GetText();
129 bool ScTpFormulaOptions::IsValidSeparator(const OUString& rSep) const
131 if (rSep.getLength() != 1)
132 // Must be one-character long.
133 return false;
135 if (rSep.compareToAscii("a") >= 0 && rSep.compareToAscii("z") <= 0)
136 return false;
138 if (rSep.compareToAscii("A") >= 0 && rSep.compareToAscii("Z") <= 0)
139 return false;
141 sal_Unicode c = rSep.getStr()[0];
142 switch (c)
144 case '+':
145 case '-':
146 case '/':
147 case '*':
148 case '<':
149 case '>':
150 case '[':
151 case ']':
152 case '(':
153 case ')':
154 case '"':
155 case '\'':
156 // Disallowed characters. Anything else we want to disallow ?
157 return false;
160 if (c == mnDecSep)
161 // decimal separator is not allowed.
162 return false;
164 return true;
167 bool ScTpFormulaOptions::IsValidSeparatorSet() const
169 // Make sure the column and row separators are different.
170 String aColStr = maEdSepArrayCol.GetText();
171 String aRowStr = maEdSepArrayRow.GetText();
172 if (aColStr == aRowStr)
173 return false;
175 return true;
178 IMPL_LINK( ScTpFormulaOptions, ButtonHdl, PushButton*, pBtn )
180 if (pBtn == &maBtnSepReset)
181 ResetSeparators();
183 return 0;
186 IMPL_LINK( ScTpFormulaOptions, SepModifyHdl, Edit*, pEdit )
188 if (!pEdit)
189 return 0;
191 String aStr = pEdit->GetText();
192 if (aStr.Len() > 1)
194 // In case the string is more than one character long, only grab the
195 // first character.
196 aStr = aStr.Copy(0, 1);
197 pEdit->SetText(aStr);
200 if ((!IsValidSeparator(aStr) || !IsValidSeparatorSet()) && maOldSepValue.getLength())
201 // Invalid separator. Restore the old value.
202 pEdit->SetText(maOldSepValue);
204 OnFocusSeparatorInput(pEdit);
205 return 0;
208 IMPL_LINK( ScTpFormulaOptions, SepEditOnFocusHdl, Edit*, pEdit )
210 OnFocusSeparatorInput(pEdit);
211 return 0;
214 // static
215 SfxTabPage* ScTpFormulaOptions::Create(Window* pParent, const SfxItemSet& rCoreSet)
217 return new ScTpFormulaOptions(pParent, rCoreSet);
220 BOOL ScTpFormulaOptions::FillItemSet(SfxItemSet& rCoreSet)
222 ::formula::FormulaGrammar::Grammar eGram = ::formula::FormulaGrammar::GRAM_DEFAULT;
223 switch (maLbFormulaSyntax.GetSelectEntryPos())
225 case 0:
226 eGram = ::formula::FormulaGrammar::GRAM_NATIVE;
227 break;
228 case 1:
229 eGram = ::formula::FormulaGrammar::GRAM_NATIVE_XL_A1;
230 break;
231 case 2:
232 eGram = ::formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1;
233 break;
236 mpNewOptions->SetFormulaSyntax(eGram);
238 mpNewOptions->SetFormulaSepArg(maEdSepFuncArg.GetText());
239 mpNewOptions->SetFormulaSepArrayCol(maEdSepArrayCol.GetText());
240 mpNewOptions->SetFormulaSepArrayRow(maEdSepArrayRow.GetText());
242 if (*mpNewOptions != *mpOldOptions)
244 rCoreSet.Put(ScTpCalcItem(GetWhich(SID_SCDOCOPTIONS), *mpNewOptions));
245 return true;
247 else
248 return false;
251 void ScTpFormulaOptions::Reset(const SfxItemSet& /*rCoreSet*/)
253 ::formula::FormulaGrammar::Grammar eGram = mpNewOptions->GetFormulaSyntax();
254 switch (eGram)
256 case ::formula::FormulaGrammar::GRAM_NATIVE:
257 maLbFormulaSyntax.SelectEntryPos(0);
258 break;
259 case ::formula::FormulaGrammar::GRAM_NATIVE_XL_A1:
260 maLbFormulaSyntax.SelectEntryPos(1);
261 break;
262 case ::formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1:
263 maLbFormulaSyntax.SelectEntryPos(2);
264 break;
265 default:
266 maLbFormulaSyntax.SelectEntryPos(0);
269 OUString aSep = mpNewOptions->GetFormulaSepArg();
270 OUString aSepArrayRow = mpNewOptions->GetFormulaSepArrayRow();
271 OUString aSepArrayCol = mpNewOptions->GetFormulaSepArrayCol();
273 if (aSep.getLength() == 1 && aSepArrayRow.getLength() == 1 && aSepArrayCol.getLength() == 1)
275 // Each separator must be one character long.
276 maEdSepFuncArg.SetText(aSep);
277 maEdSepArrayCol.SetText(aSepArrayCol);
278 maEdSepArrayRow.SetText(aSepArrayRow);
280 else
281 ResetSeparators();
284 int ScTpFormulaOptions::DeactivatePage(SfxItemSet* /*pSet*/)
286 // What's this method for ?
287 return KEEP_PAGE;