summaryrefslogtreecommitdiffstats
path: root/sc/source/ui/optdlg/tpformula.cxx
blob: b4c0045d646958fa519849a98f30a3430f6e13f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#undef SC_DLLIMPLEMENTATION

#include <global.hxx>
#include <tpformula.hxx>
#include <formulaopt.hxx>
#include <sc.hrc>
#include <strings.hrc>
#include <scresid.hxx>
#include <formula/grammar.hxx>
#include <officecfg/Office/Calc.hxx>
#include "calcoptionsdlg.hxx"

#include <unotools/localedatawrapper.hxx>

ScTpFormulaOptions::ScTpFormulaOptions(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rCoreAttrs)
    : SfxTabPage(pPage, pController, "modules/scalc/ui/optformula.ui", "OptFormula", &rCoreAttrs)
    , mnDecSep(0)
    , mxLbFormulaSyntax(m_xBuilder->weld_combo_box("formulasyntax"))
    , mxCbEnglishFuncName(m_xBuilder->weld_check_button("englishfuncname"))
    , mxBtnCustomCalcDefault(m_xBuilder->weld_radio_button("calcdefault"))
    , mxBtnCustomCalcCustom(m_xBuilder->weld_radio_button("calccustom"))
    , mxBtnCustomCalcDetails(m_xBuilder->weld_button("details"))
    , mxEdSepFuncArg(m_xBuilder->weld_entry("function"))
    , mxEdSepArrayCol(m_xBuilder->weld_entry("arraycolumn"))
    , mxEdSepArrayRow(m_xBuilder->weld_entry("arrayrow"))
    , mxBtnSepReset(m_xBuilder->weld_button("reset"))
    , mxLbOOXMLRecalcOptions(m_xBuilder->weld_combo_box("ooxmlrecalc"))
    , mxLbODFRecalcOptions(m_xBuilder->weld_combo_box("odfrecalc"))
{
    mxLbFormulaSyntax->append_text(ScResId(SCSTR_FORMULA_SYNTAX_CALC_A1));
    mxLbFormulaSyntax->append_text(ScResId(SCSTR_FORMULA_SYNTAX_XL_A1));
    mxLbFormulaSyntax->append_text(ScResId(SCSTR_FORMULA_SYNTAX_XL_R1C1));

    Link<weld::Button&,void> aLink2 = LINK( this, ScTpFormulaOptions, ButtonHdl );
    mxBtnSepReset->connect_clicked(aLink2);
    mxBtnCustomCalcDefault->connect_clicked(aLink2);
    mxBtnCustomCalcCustom->connect_clicked(aLink2);
    mxBtnCustomCalcDetails->connect_clicked(aLink2);

    mxEdSepFuncArg->connect_insert_text(LINK( this, ScTpFormulaOptions, SepInsertTextHdl ));
    mxEdSepArrayCol->connect_insert_text(LINK( this, ScTpFormulaOptions, ColSepInsertTextHdl ));
    mxEdSepArrayRow->connect_insert_text(LINK( this, ScTpFormulaOptions, RowSepInsertTextHdl ));

    Link<weld::Entry&,void> aLink = LINK( this, ScTpFormulaOptions, SepModifyHdl );
    mxEdSepFuncArg->connect_changed(aLink);
    mxEdSepArrayCol->connect_changed(aLink);
    mxEdSepArrayRow->connect_changed(aLink);

    Link<weld::Widget&,void> aLink3 = LINK( this, ScTpFormulaOptions, SepEditOnFocusHdl );
    mxEdSepFuncArg->connect_focus_in(aLink3);
    mxEdSepArrayCol->connect_focus_in(aLink3);
    mxEdSepArrayRow->connect_focus_in(aLink3);

    // Get the decimal separator for current locale.
    OUString aSep = ScGlobal::getLocaleDataPtr()->getNumDecimalSep();
    mnDecSep = aSep.isEmpty() ? u'.' : aSep[0];

    maSavedDocOptions = static_cast<const ScTpCalcItem&>(rCoreAttrs.Get(
            GetWhich(SID_SCDOCOPTIONS))).GetDocOptions();
}

ScTpFormulaOptions::~ScTpFormulaOptions()
{
}

void ScTpFormulaOptions::ResetSeparators()
{
    OUString aFuncArg, aArrayCol, aArrayRow;
    ScFormulaOptions::GetDefaultFormulaSeparators(aFuncArg, aArrayCol, aArrayRow);
    mxEdSepFuncArg->set_text(aFuncArg);
    mxEdSepArrayCol->set_text(aArrayCol);
    mxEdSepArrayRow->set_text(aArrayRow);
}

void ScTpFormulaOptions::OnFocusSeparatorInput(weld::Entry* pEdit)
{
    if (!pEdit)
        return;

    // Make sure the entire text is selected.
    pEdit->select_region(0, -1);
    OUString sSepValue = pEdit->get_text();
    if (!sSepValue.isEmpty())
        maOldSepValue = sSepValue;
}

void ScTpFormulaOptions::UpdateCustomCalcRadioButtons(bool bDefault)
{
    if (bDefault)
    {
        mxBtnCustomCalcDefault->set_active(true);
        mxBtnCustomCalcCustom->set_active(false);
        mxBtnCustomCalcDetails->set_sensitive(false);
    }
    else
    {
        mxBtnCustomCalcDefault->set_active(false);
        mxBtnCustomCalcCustom->set_active(true);
        mxBtnCustomCalcDetails->set_sensitive(true);
    }
}

void ScTpFormulaOptions::LaunchCustomCalcSettings()
{
    ScCalcOptionsDialog aDlg(GetFrameWeld(), maCurrentConfig, maCurrentDocOptions.IsWriteCalcConfig());
    if (aDlg.run() == RET_OK)
    {
        maCurrentConfig = aDlg.GetConfig();
        maCurrentDocOptions.SetWriteCalcConfig(aDlg.GetWriteCalcConfig());
    }
}

bool ScTpFormulaOptions::IsValidSeparator(const OUString& rSep, bool bArray) const
{
    if (rSep.getLength() != 1)
        // Must be one-character long.
        return false;

    const sal_Unicode c = rSep[0];

    if (c == mnDecSep)
        // decimal separator is not allowed.
        return false;

    if (c <= 0x20 || c == 0x7f)
        // Disallow non-printables including space and DEL.
        return false;

    if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9'))
        // Disallow alphanumeric.
        return false;

    if (bArray)
    {
        switch (c)
        {
            case '+':
            case '-':
            case '{':
            case '}':
            case '"':
                // All following just to prevent confusion, they are not
                // evaluated in inline arrays and theoretically would be
                // possible.
            case '%':
            case '/':
            case '*':
            case '=':
            case '<':
            case '>':
            case '[':
            case ']':
            case '(':
            case ')':
            case '\'':
                // Disallowed characters.  Anything else we want to disallow ?
                return false;
        }
    }
    else if (c <= 0x7f)
    {
        switch (c)
        {
            default:
                // Anything bad except the knowns.
                return false;
            case ';':
            case ',':
                ; // nothing
        }
    }
    else
    {
        // Any Unicode character, would have to ask the compiler's localized
        // symbol map whether it's a known symbol but not a separator
        // (ocSep,ocArrayRowSep,ocArrayColSep), which we're about to set here.
        // But really..
        return false;
    }

    return true;
}

IMPL_LINK( ScTpFormulaOptions, ButtonHdl, weld::Button&, rBtn, void )
{
    if (&rBtn == mxBtnSepReset.get())
        ResetSeparators();
    else if (&rBtn == mxBtnCustomCalcDefault.get())
        UpdateCustomCalcRadioButtons(true);
    else if (&rBtn == mxBtnCustomCalcCustom.get())
        UpdateCustomCalcRadioButtons(false);
    else if (&rBtn == mxBtnCustomCalcDetails.get())
        LaunchCustomCalcSettings();
}

IMPL_LINK(ScTpFormulaOptions, SepInsertTextHdl, OUString&, rTest, bool)
{
    if (!IsValidSeparator(rTest, false) && !maOldSepValue.isEmpty())
        // Invalid separator.  Restore the old value.
        rTest = maOldSepValue;
    return true;
}

IMPL_LINK(ScTpFormulaOptions, RowSepInsertTextHdl, OUString&, rTest, bool)
{
    // Invalid separator or same as ColStr - Restore the old value.
    if ((!IsValidSeparator(rTest, true) || rTest == mxEdSepArrayCol->get_text()) && !maOldSepValue.isEmpty())
        rTest = maOldSepValue;
    return true;
}

IMPL_LINK(ScTpFormulaOptions, ColSepInsertTextHdl, OUString&, rTest, bool)
{
    // Invalid separator or same as RowStr - Restore the old value.
    if ((!IsValidSeparator(rTest, true) || rTest == mxEdSepArrayRow->get_text()) && !maOldSepValue.isEmpty())
        rTest = maOldSepValue;
    return true;
}

IMPL_LINK( ScTpFormulaOptions, SepModifyHdl, weld::Entry&, rEdit, void )
{
    OnFocusSeparatorInput(&rEdit);
}

IMPL_LINK( ScTpFormulaOptions, SepEditOnFocusHdl, weld::Widget&, rControl, void )
{
    OnFocusSeparatorInput(dynamic_cast<weld::Entry*>(&rControl));
}

std::unique_ptr<SfxTabPage> ScTpFormulaOptions::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rCoreSet)
{
    return std::make_unique<ScTpFormulaOptions>(pPage, pController, *rCoreSet);
}

bool ScTpFormulaOptions::FillItemSet(SfxItemSet* rCoreSet)
{
    bool bRet = false;
    ScFormulaOptions aOpt;
    bool bEnglishFuncName = mxCbEnglishFuncName->get_active();
    sal_Int16 aSyntaxPos      = mxLbFormulaSyntax->get_active();
    OUString aSep             = mxEdSepFuncArg->get_text();
    OUString aSepArrayCol     = mxEdSepArrayCol->get_text();
    OUString aSepArrayRow     = mxEdSepArrayRow->get_text();
    sal_Int16 nOOXMLRecalcMode = mxLbOOXMLRecalcOptions->get_active();
    sal_Int16 nODFRecalcMode = mxLbODFRecalcOptions->get_active();

    if (mxBtnCustomCalcDefault->get_active())
    {
        // When Default is selected, reset all the calc config settings to default.
        maCurrentConfig.reset();
    }

    if ( mxLbFormulaSyntax->get_saved_value() != mxLbFormulaSyntax->get_text(aSyntaxPos)
         || mxCbEnglishFuncName->get_saved_state() != (bEnglishFuncName ? 1 : 0)
         || mxEdSepFuncArg->get_saved_value() != aSep
         || mxEdSepArrayCol->get_saved_value() != aSepArrayCol
         || mxEdSepArrayRow->get_saved_value() != aSepArrayRow
         || mxLbOOXMLRecalcOptions->get_saved_value() != mxLbOOXMLRecalcOptions->get_text(nOOXMLRecalcMode)
         || mxLbODFRecalcOptions->get_saved_value() != mxLbODFRecalcOptions->get_text(nODFRecalcMode)
         || maSavedConfig != maCurrentConfig
         || maSavedDocOptions != maCurrentDocOptions )
    {
        ::formula::FormulaGrammar::Grammar eGram = ::formula::FormulaGrammar::GRAM_DEFAULT;

        switch (aSyntaxPos)
        {
        case 0:
            eGram = ::formula::FormulaGrammar::GRAM_NATIVE;
        break;
        case 1:
            eGram = ::formula::FormulaGrammar::GRAM_NATIVE_XL_A1;
        break;
        case 2:
            eGram = ::formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1;
        break;
        }

        ScRecalcOptions eOOXMLRecalc = static_cast<ScRecalcOptions>(nOOXMLRecalcMode);
        ScRecalcOptions eODFRecalc = static_cast<ScRecalcOptions>(nODFRecalcMode);

        aOpt.SetFormulaSyntax(eGram);
        aOpt.SetUseEnglishFuncName(bEnglishFuncName);
        aOpt.SetFormulaSepArg(aSep);
        aOpt.SetFormulaSepArrayCol(aSepArrayCol);
        aOpt.SetFormulaSepArrayRow(aSepArrayRow);
        aOpt.SetCalcConfig(maCurrentConfig);
        aOpt.SetOOXMLRecalcOptions(eOOXMLRecalc);
        aOpt.SetODFRecalcOptions(eODFRecalc);
        aOpt.SetWriteCalcConfig( maCurrentDocOptions.IsWriteCalcConfig());

        rCoreSet->Put( ScTpFormulaItem( aOpt ) );
        rCoreSet->Put( ScTpCalcItem( SID_SCDOCOPTIONS, maCurrentDocOptions ) );

        bRet = true;
    }
    return bRet;
}

void ScTpFormulaOptions::Reset(const SfxItemSet* rCoreSet)
{
    ScFormulaOptions aOpt;
    const SfxPoolItem* pItem = nullptr;

    if(SfxItemState::SET == rCoreSet->GetItemState(SID_SCFORMULAOPTIONS, false , &pItem))
        aOpt = static_cast<const ScTpFormulaItem*>(pItem)->GetFormulaOptions();

    // formula grammar.
    ::formula::FormulaGrammar::Grammar eGram = aOpt.GetFormulaSyntax();

    switch (eGram)
    {
    case ::formula::FormulaGrammar::GRAM_NATIVE:
        mxLbFormulaSyntax->set_active(0);
        break;
    case ::formula::FormulaGrammar::GRAM_NATIVE_XL_A1:
        mxLbFormulaSyntax->set_active(1);
        break;
    case ::formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1:
        mxLbFormulaSyntax->set_active(2);
        break;
    default:
        mxLbFormulaSyntax->set_active(0);
    }

    mxLbFormulaSyntax->save_value();
    mxLbFormulaSyntax->set_sensitive( !officecfg::Office::Calc::Formula::Syntax::Grammar::isReadOnly() );

    ScRecalcOptions eOOXMLRecalc = aOpt.GetOOXMLRecalcOptions();
    mxLbOOXMLRecalcOptions->set_active(static_cast<sal_uInt16>(eOOXMLRecalc));
    mxLbOOXMLRecalcOptions->save_value();
    mxLbOOXMLRecalcOptions->set_sensitive( !officecfg::Office::Calc::Formula::Load::OOXMLRecalcMode::isReadOnly() );

    ScRecalcOptions eODFRecalc = aOpt.GetODFRecalcOptions();
    mxLbODFRecalcOptions->set_active(static_cast<sal_uInt16>(eODFRecalc));
    mxLbODFRecalcOptions->save_value();
    mxLbODFRecalcOptions->set_sensitive( !officecfg::Office::Calc::Formula::Load::ODFRecalcMode::isReadOnly() );

    // english function name.
    mxCbEnglishFuncName->set_active( aOpt.GetUseEnglishFuncName() );
    mxCbEnglishFuncName->save_state();
    mxCbEnglishFuncName->set_sensitive( !officecfg::Office::Calc::Formula::Syntax::EnglishFunctionName::isReadOnly() );

    // Separators
    OUString aSep = aOpt.GetFormulaSepArg();
    OUString aSepArrayRow = aOpt.GetFormulaSepArrayRow();
    OUString aSepArrayCol = aOpt.GetFormulaSepArrayCol();

    if (IsValidSeparator(aSep, false) && IsValidSeparator(aSepArrayRow, true) && IsValidSeparator(aSepArrayCol, true))
    {
        // Each and all separators must be valid.
        mxEdSepFuncArg->set_text(aSep);
        mxEdSepArrayCol->set_text(aSepArrayCol);
        mxEdSepArrayRow->set_text(aSepArrayRow);

        mxEdSepFuncArg->save_value();
        mxEdSepArrayCol->save_value();
        mxEdSepArrayRow->save_value();
    }
    else
        ResetSeparators();

    mxEdSepFuncArg->set_sensitive( !officecfg::Office::Calc::Formula::Syntax::SeparatorArg::isReadOnly() );
    mxEdSepArrayCol->set_sensitive( !officecfg::Office::Calc::Formula::Syntax::SeparatorArrayCol::isReadOnly() );
    mxEdSepArrayRow->set_sensitive( !officecfg::Office::Calc::Formula::Syntax::SeparatorArrayRow::isReadOnly() );
    mxBtnSepReset->set_sensitive ( !officecfg::Office::Calc::Formula::Syntax::SeparatorArg::isReadOnly()  &&
                            !officecfg::Office::Calc::Formula::Syntax::SeparatorArrayCol::isReadOnly() &&
                            !officecfg::Office::Calc::Formula::Syntax::SeparatorArrayRow::isReadOnly() );

    // detailed calc settings.
    ScFormulaOptions aDefaults;

    maSavedConfig = aOpt.GetCalcConfig();
    bool bDefault = aDefaults.GetCalcConfig() == maSavedConfig;
    UpdateCustomCalcRadioButtons(bDefault);

    maCurrentConfig = maSavedConfig;

    maCurrentDocOptions = maSavedDocOptions;
}

DeactivateRC ScTpFormulaOptions::DeactivatePage(SfxItemSet* /*pSet*/)
{
    // What's this method for ?
    return DeactivateRC::KeepPage;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */