cuticle/ui/textctrl.cpp
2024-06-26 20:47:26 +03:00

46 lines
1.3 KiB
C++

#include"timectrl.h"
ctTimeCtrl::ctTimeCtrl(wxWindow *parent, double seconds) : wxTextCtrl(parent, wxID_ANY) {
SetSeconds(seconds);
Bind(wxEVT_CHAR, [this](wxKeyEvent& ev){
if(ev.GetKeyCode() == WXK_LEFT || ev.GetKeyCode() == WXK_RIGHT) ev.Skip();
else if(ev.GetKeyCode() >= '0' && ev.GetKeyCode() <= '9') {
uint32_t c = GetValue()[GetInsertionPoint()].GetValue();
if(c >= '0' && c <= '9') {
// SetValue calls EVT_TEXT
wxString neu = GetValue().Clone();
neu.SetChar(GetInsertionPoint(), ev.GetUnicodeKey());
SetValue(neu);
CallAfter([this](){
SetInsertionPoint(GetInsertionPoint() + 1);
});
}
} else if(GetInsertionPoint() < (long) GetValue().Length() && GetValue()[GetInsertionPoint()].GetValue() == ev.GetUnicodeKey()) {
CallAfter([this](){
SetInsertionPoint(GetInsertionPoint() + 1);
});
}
});
Bind(wxEVT_TEXT, [this](wxCommandEvent &ev){
long h, m;
double s;
GetValue().ToCLong(&h);
GetValue().Mid(3).ToCLong(&m);
GetValue().Mid(6).ToCDouble(&s);
this->seconds = (h * 60 + m) * 60 + s;
});
}
double ctTimeCtrl::GetSeconds() {
return seconds;
}
void ctTimeCtrl::SetSeconds(double seconds) {
this->seconds = seconds;
ChangeValue(wxString::Format("%02i:%02i:%02.3g", (int) (seconds / 3600), (int) (seconds / 60), fmodf(seconds, 60)));
}