forked from d-j-hirst/aus-polling-analyser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntInput.cpp
More file actions
54 lines (49 loc) · 1.68 KB
/
IntInput.cpp
File metadata and controls
54 lines (49 loc) · 1.68 KB
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
#include "IntInput.h"
#include "General.h"
IntInput::IntInput(wxWindow* parent, wxWindowID textCtrlId, std::string labelText, int initialValue, wxPoint topLeft,
TextChangeFunc textChangeFunc, IntValidationFunc intValidationFunc,
int labelWidth, int textInputWidth)
: textChangeFunc(textChangeFunc), intValidationFunc(intValidationFunc), parent(parent)
{
staticText = new wxStaticText(parent, 0, labelText, topLeft, wxSize(labelWidth, Height));
textCtrl = new wxTextCtrl(parent, textCtrlId, std::to_string(initialValue),
topLeft + wxSize(labelWidth, 0), wxSize(textInputWidth, Height));
parent->Bind(wxEVT_TEXT, &IntInput::updateText, this, textCtrl->GetId());
}
void IntInput::updateText(wxCommandEvent & event)
{
if (currentlyUpdating) return;
currentlyUpdating = true;
int value = 0;
std::string str = event.GetString().ToStdString();
// updates the preliminary project data with the string from the event.
// This code effectively acts as a pseudo-validator
// (can't get the standard one to work properly with pre-initialized values)
try {
// An empty string can be interpreted as zero or the preset null value, so it's ok.
if (str.empty()) {
value = 0;
}
else if (str == "-") {
value = 0;
}
else {
value = std::stoi(str);
int validated = intValidationFunc(value);
if (validated != value) {
value = validated;
textCtrl->SetLabel(std::to_string(value));
}
}
// save this valid string in case the next text entry gives an error.
lastText = str;
}
catch (std::logic_error err) {
// Set the text to the last valid string.
textCtrl->SetLabel(lastText);
currentlyUpdating = false;
return;
}
textChangeFunc(value);
currentlyUpdating = false;
}