From ad21e0c91aedec7524c795726357921cf379eadf Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Sat, 9 May 2015 23:13:35 -0400 Subject: [PATCH] Esc/Enter trap, input filtering, clipboard filtering --- src/FrequencyDialog.cpp | 57 ++++++++++++++++++++++++++++++++++------- src/FrequencyDialog.h | 3 +++ 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/FrequencyDialog.cpp b/src/FrequencyDialog.cpp index ce3c5d1..01d2fd2 100644 --- a/src/FrequencyDialog.cpp +++ b/src/FrequencyDialog.cpp @@ -1,23 +1,62 @@ #include "FrequencyDialog.h" -wxBEGIN_EVENT_TABLE(FrequencyDialog, wxDialog) wxEND_EVENT_TABLE() +#include "wx/clipbrd.h" + +wxBEGIN_EVENT_TABLE(FrequencyDialog, wxDialog) +EVT_CHAR_HOOK(FrequencyDialog::OnChar) +wxEND_EVENT_TABLE() FrequencyDialog::FrequencyDialog(wxWindow * parent, wxWindowID id, const wxString & title, const wxPoint & position, const wxSize & size, long style) : - wxDialog(parent, id, title, position, size, style) { +wxDialog(parent, id, title, position, size, style) { wxString freqStr = "105.7Mhz"; - dialogText = new wxTextCtrl(this, -1, freqStr, wxPoint(6, 1), wxSize(size.GetWidth() - 20, size.GetHeight() - 70), wxTE_PROCESS_ENTER); + dialogText = new wxTextCtrl(this, wxID_FREQ_INPUT, freqStr, wxPoint(6, 1), wxSize(size.GetWidth() - 20, size.GetHeight() - 70), wxTE_PROCESS_ENTER); dialogText->SetFont(wxFont(20, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD)); - Connect(wxEVT_TEXT_ENTER, wxCommandEventHandler(FrequencyDialog::OnEnter)); - - SetEscapeId(wxID_CANCEL); Centre(); dialogText->SetSelection(-1, -1); } -void FrequencyDialog::OnEnter(wxCommandEvent &event) { - std::cout << dialogText->GetValue().ToStdString() << std::endl; - Close(); +std::string& FrequencyDialog::filterChars(std::string& s, const std::string& allowed) { + s.erase(remove_if(s.begin(), s.end(), [&allowed](const char& c) { + return allowed.find(c) == std::string::npos; + }), s.end()); + return s; +} + +void FrequencyDialog::OnChar(wxKeyEvent& event) { + wxChar c = event.GetKeyCode(); + + switch (c) { + case WXK_RETURN: + case WXK_NUMPAD_ENTER: + // Do Stuff + + Close(); + break; + case WXK_ESCAPE: + Close(); + break; + } + + std::string allowed("0123456789.MKGHZmkghz"); + + if (allowed.find_first_of(c) != std::string::npos || c == WXK_BACK) { + event.DoAllowNextEvent(); + } else if (event.ControlDown() && c == 'V') { + // Alter clipboard contents to remove unwanted chars + wxTheClipboard->Open(); + wxTextDataObject data; + wxTheClipboard->GetData(data); + std::string clipText = data.GetText().ToStdString(); + std::string pasteText = filterChars(clipText,std::string(allowed)); + wxTheClipboard->SetData(new wxTextDataObject(pasteText)); + wxTheClipboard->Close(); + event.Skip(); + } else if (c == WXK_RIGHT || c == WXK_LEFT || event.ControlDown()) { + event.Skip(); + } else { + std::cout << (int) c << std::endl; + } } diff --git a/src/FrequencyDialog.h b/src/FrequencyDialog.h index 37a83ff..b496a53 100644 --- a/src/FrequencyDialog.h +++ b/src/FrequencyDialog.h @@ -5,6 +5,7 @@ #include "wx/string.h" #include "wx/button.h" +#define wxID_FREQ_INPUT 3001 class FrequencyDialog: public wxDialog { @@ -20,6 +21,8 @@ public: private: void OnEnter ( wxCommandEvent &event ); + void OnChar ( wxKeyEvent &event ); + std::string& filterChars(std::string& s, const std::string& allowed); DECLARE_EVENT_TABLE() };