summaryrefslogtreecommitdiff
path: root/vcl/source/control/FormattedField.cxx
blob: 6d7a6601afe181f8594a3ae241a8cf08908828d8 (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * 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 .
 */

#include <accessibility/svtaccessiblenumericfield.hxx>

#include <rtl/math.hxx>
#include <svl/numformat.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/string.hxx>
#include <tools/debug.hxx>

#include <vcl/builder.hxx>
#include <vcl/event.hxx>
#include <vcl/commandevent.hxx>
#include <vcl/toolkit/fmtfield.hxx>
#include <vcl/uitest/formattedfielduiobject.hxx>
#include <vcl/weldutils.hxx>

#include "FieldFormatter.hxx"

#include <svl/zformat.hxx>

#include <limits>

FormattedField::FormattedField(vcl::Window* pParent, WinBits nStyle)
    : SpinField(pParent, nStyle, WindowType::FORMATTEDFIELD)
    , m_pFormatter(nullptr)
{
}

void FormattedField::dispose()
{
    m_pFormatter = nullptr;
    m_xOwnFormatter.reset();
    SpinField::dispose();
}

css::uno::Reference<css::accessibility::XAccessible> FormattedField::CreateAccessible()
{
    return new SVTXAccessibleNumericField(this);
}

void FormattedField::SetText(const OUString& rStr)
{
    GetFormatter().SetFieldText(rStr, Selection(0, 0));
}

void FormattedField::SetText(const OUString& rStr, const Selection& rNewSelection)
{
    GetFormatter().SetFieldText(rStr, rNewSelection);
    SetSelection(rNewSelection);
}

bool FormattedField::set_property(const OUString &rKey, const OUString &rValue)
{
    if (rKey == "digits")
        GetFormatter().SetDecimalDigits(rValue.toInt32());
    else if (rKey == "wrap")
        GetFormatter().SetWrapOnLimits(toBool(rValue));
    else
        return SpinField::set_property(rKey, rValue);
    return true;
}

void FormattedField::Up()
{
    Formatter& rFormatter = GetFormatter();
    auto nScale = weld::SpinButton::Power10(rFormatter.GetDecimalDigits());

    sal_Int64 nValue = std::round(rFormatter.GetValue() * nScale);
    sal_Int64 nSpinSize = std::round(rFormatter.GetSpinSize() * nScale);
    assert(nSpinSize != 0);
    sal_Int64 nRemainder = rFormatter.GetDisableRemainderFactor() || nSpinSize == 0 ? 0 : nValue % nSpinSize;
    if (nValue >= 0)
        nValue = (nRemainder == 0) ? nValue + nSpinSize : nValue + nSpinSize - nRemainder;
    else
        nValue = (nRemainder == 0) ? nValue + nSpinSize : nValue - nRemainder;

    // setValue handles under- and overflows (min/max) automatically
    rFormatter.SetValue(static_cast<double>(nValue) / nScale);
    SetModifyFlag();
    Modify();

    SpinField::Up();
}

void FormattedField::Down()
{
    Formatter& rFormatter = GetFormatter();
    auto nScale = weld::SpinButton::Power10(rFormatter.GetDecimalDigits());

    sal_Int64 nValue = std::round(rFormatter.GetValue() * nScale);
    sal_Int64 nSpinSize = std::round(rFormatter.GetSpinSize() * nScale);
    assert(nSpinSize != 0);
    sal_Int64 nRemainder = rFormatter.GetDisableRemainderFactor() || nSpinSize == 0 ? 0 : nValue % nSpinSize;
    if (nValue >= 0)
        nValue = (nRemainder == 0) ? nValue - nSpinSize : nValue - nRemainder;
    else
        nValue = (nRemainder == 0) ? nValue - nSpinSize : nValue - nSpinSize - nRemainder;

    // setValue handles under- and overflows (min/max) automatically
    rFormatter.SetValue(static_cast<double>(nValue) / nScale);
    SetModifyFlag();
    Modify();

    SpinField::Down();
}

void FormattedField::First()
{
    Formatter& rFormatter = GetFormatter();
    if (rFormatter.HasMinValue())
    {
        rFormatter.SetValue(rFormatter.GetMinValue());
        SetModifyFlag();
        Modify();
    }

    SpinField::First();
}

void FormattedField::Last()
{
    Formatter& rFormatter = GetFormatter();
    if (rFormatter.HasMaxValue())
    {
        rFormatter.SetValue(rFormatter.GetMaxValue());
        SetModifyFlag();
        Modify();
    }

    SpinField::Last();
}

void FormattedField::Modify()
{
    GetFormatter().Modify();
}

bool FormattedField::PreNotify(NotifyEvent& rNEvt)
{
    if (rNEvt.GetType() == NotifyEventType::KEYINPUT)
        GetFormatter().SetLastSelection(GetSelection());
    return SpinField::PreNotify(rNEvt);
}

bool FormattedField::EventNotify(NotifyEvent& rNEvt)
{
    if ((rNEvt.GetType() == NotifyEventType::KEYINPUT) && !IsReadOnly())
    {
        const KeyEvent& rKEvt = *rNEvt.GetKeyEvent();
        sal_uInt16 nMod = rKEvt.GetKeyCode().GetModifier();
        switch ( rKEvt.GetKeyCode().GetCode() )
        {
            case KEY_UP:
            case KEY_DOWN:
            case KEY_PAGEUP:
            case KEY_PAGEDOWN:
            {
                Formatter& rFormatter = GetFormatter();
                if (!nMod && rFormatter.GetOrCreateFormatter().IsTextFormat(rFormatter.GetFormatKey()))
                {
                    // the base class would translate this into calls to Up/Down/First/Last,
                    // but we don't want this if we are text-formatted
                    return true;
                }
            }
        }
    }

    if ((rNEvt.GetType() == NotifyEventType::COMMAND) && !IsReadOnly())
    {
        const CommandEvent* pCommand = rNEvt.GetCommandEvent();
        if (pCommand->GetCommand() == CommandEventId::Wheel)
        {
            const CommandWheelData* pData = rNEvt.GetCommandEvent()->GetWheelData();
            Formatter& rFormatter = GetFormatter();
            if ((pData->GetMode() == CommandWheelMode::SCROLL) &&
                rFormatter.GetOrCreateFormatter().IsTextFormat(rFormatter.GetFormatKey()))
            {
                // same as above : prevent the base class from doing Up/Down-calls
                // (normally I should put this test into the Up/Down methods itself, shouldn't I ?)
                // FS - 71553 - 19.01.00
                return true;
            }
        }
    }

    if (rNEvt.GetType() == NotifyEventType::LOSEFOCUS && m_pFormatter)
        m_pFormatter->EntryLostFocus();

    return SpinField::EventNotify( rNEvt );
}

Formatter& FormattedField::GetFormatter()
{
    if (!m_pFormatter)
    {
        m_xOwnFormatter.reset(new FieldFormatter(*this));
        m_pFormatter = m_xOwnFormatter.get();
    }
    return *m_pFormatter;
}

void FormattedField::SetFormatter(Formatter* pFormatter)
{
    m_xOwnFormatter.reset();
    m_pFormatter = pFormatter;
}

// currently used by online
void FormattedField::SetValueFromString(const OUString& rStr)
{
    sal_Int32 nEnd;
    rtl_math_ConversionStatus eStatus;
    Formatter& rFormatter = GetFormatter();
    double fValue = ::rtl::math::stringToDouble(rStr, '.', rFormatter.GetDecimalDigits(), &eStatus, &nEnd );

    if (eStatus == rtl_math_ConversionStatus_Ok &&
        nEnd == rStr.getLength())
    {
        rFormatter.SetValue(fValue);
        SetModifyFlag();
        Modify();

        // Notify the value has changed
        SpinField::Up();
    }
    else
    {
        SAL_WARN("vcl", "fail to convert the value: " << rStr);
    }
}

void FormattedField::DumpAsPropertyTree(tools::JsonWriter& rJsonWriter)
{
    SpinField::DumpAsPropertyTree(rJsonWriter);
    Formatter& rFormatter = GetFormatter();

    if (dynamic_cast<weld::TimeFormatter*>(&rFormatter))
    {
        // weld::TimeFormatter uses h24 format
        rJsonWriter.put("type", "time");
    }
    rJsonWriter.put("min", rFormatter.GetMinValue());
    rJsonWriter.put("max", rFormatter.GetMaxValue());
    rJsonWriter.put("value", rFormatter.GetValue());
    rJsonWriter.put("step", rFormatter.GetSpinSize());
}

FactoryFunction FormattedField::GetUITestFactory() const
{
    return FormattedFieldUIObject::create;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */