GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/inttextfield.cpp Lines: 27 57 47.4 %
Date: 2017-11-29 Branches: 22 58 37.9 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "gui/widgets/inttextfield.h"
24
25
#ifdef USE_SDL2
26
#include "enums/input/keyvalue.h"
27
#endif  // USE_SDL2
28
29
#include "utils/stringutils.h"
30
31
#include <sstream>
32
33
#include "debug.h"
34
35
68
IntTextField::IntTextField(const Widget2 *const widget,
36
                           const int def,
37
                           const int min,
38
                           const int max,
39
                           const Enable enabled,
40
68
                           const int width) :
41
136
    TextField(widget, toString(def)),
42
    mMin(0),
43
    mMax(0),
44
    mDefault(def),
45

340
    mValue(def)
46
{
47
68
    if (min != 0 || max != 0)
48
        setRange(min, max);
49
50
136
    setEnabled(enabled == Enable_true);
51
68
    if (width != 0)
52
44
        setWidth(width);
53
68
}
54
55
void IntTextField::keyPressed(KeyEvent &event)
56
{
57
    const InputActionT action = event.getActionId();
58
59
    if (action == InputAction::GUI_DELETE
60
        || action == InputAction::GUI_BACKSPACE)
61
    {
62
        setText(std::string());
63
        if (mSendAlwaysEvents)
64
            distributeActionEvent();
65
66
        event.consume();
67
    }
68
69
#ifdef USE_SDL2
70
    const int val = event.getKey().getValue();
71
    if (val != KeyValue::TEXTINPUT)
72
        return;
73
74
    const std::string str = event.getText();
75
    if (str.empty())
76
        return;
77
    const size_t sz = str.size();
78
    for (size_t f = 0; f < sz; f ++)
79
    {
80
        const char chr = str[f];
81
        if (chr < '0' || chr > '9')
82
            return;
83
    }
84
#else  // USE_SDL2
85
86
    if (!event.getKey().isNumber())
87
        return;
88
#endif  // USE_SDL2
89
90
    TextField::keyPressed(event);
91
92
    std::istringstream s(getText());
93
    int i;
94
    s >> i;
95
    setValue(i);
96
    if (mSendAlwaysEvents)
97
        distributeActionEvent();
98
}
99
100
8
void IntTextField::setRange(const int min, const int max)
101
{
102
66
    mMin = min;
103
66
    mMax = max;
104
105

66
    if (mValue < mMin)
106
        mValue = mMin;
107

66
    else if (mValue > mMax)
108
4
        mValue = mMax;
109
110

66
    if (mDefault < mMin)
111
        mDefault = mMin;
112

66
    else if (mDefault > mMax)
113
4
        mDefault = mMax;
114
8
}
115
116
int IntTextField::getValue() const
117
{
118
    return getText().empty() ? mMin : mValue;
119
}
120
121
10
void IntTextField::setValue(const int i)
122
{
123
10
    if (i < mMin)
124
        mValue = mMin;
125
10
    else if (i > mMax)
126
        mValue = mMax;
127
    else
128
10
        mValue = i;
129
130
20
    const std::string valStr = toString(mValue);
131
10
    setText(valStr);
132
10
    setCaretPosition(CAST_U32(valStr.length()) + 1);
133
10
}
134
135
void IntTextField::setDefaultValue(const int value)
136
{
137
    if (value < mMin)
138
        mDefault = mMin;
139
    else if (value > mMax)
140
        mDefault = mMax;
141
    else
142
        mDefault = value;
143
}
144
145
void IntTextField::reset()
146
{
147
    setValue(mDefault);
148
}