Skip to content

Instantly share code, notes, and snippets.

@vinnydiehl
Created September 23, 2012 02:19
Show Gist options
  • Save vinnydiehl/cf3219eb47e2bfe85b38 to your computer and use it in GitHub Desktop.
Save vinnydiehl/cf3219eb47e2bfe85b38 to your computer and use it in GitHub Desktop.
Sample wxWidgets app for testing the CodeLineInput widget.
#include "CodeLineInput.h"
const wxRegEx CodeLineInput::HEXCHAR(_T("\\A[0-9A-Fa-f]\\Z"), wxRE_ADVANCED);
CodeLineInput::CodeLineInput(wxWindow *parent, wxWindowID id,
const wxString &value, const wxPoint &pos,
const wxSize &size, long style,
const wxValidator &validator,
const wxString &name)
: wxTextCtrl(parent, id, value, pos, size,
style, validator, name)
{
// Line of code with a space- 17 characters long.
SetMaxLength(17);
Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(CodeLineInput::mOnKeyDown));
Connect(wxEVT_COMMAND_TEXT_UPDATED,
wxCommandEventHandler(CodeLineInput::mOnTextUpdated));
}
/** Events **/
void CodeLineInput::mOnKeyDown(wxKeyEvent &evt)
{
// If the user clicks right after the space and tries to delete it, skip
// the insertion point to the character right before the space, so it
// deletes that.
if (GetInsertionPoint() == 9 && evt.GetKeyCode() == WXK_BACK)
SetInsertionPoint(8);
evt.Skip();
}
void CodeLineInput::mOnTextUpdated(wxCommandEvent &WXUNUSED(evt))
{
mFormatText();
}
/** Helpers **/
void CodeLineInput::mFormatText(void)
{
wxString text = mStripToHex(GetValue());
wxString final;
for (size_t i = 0; i < text.Len(); ++i)
{
if (i == 8)
final += _T(' ');
final += text[i];
}
// An attempt at a hack to get it to just work, but no good.
// For some reason when you enter that 9th character, this method runs
// again, not going into the conditional the second time. Maybe it's the
// ChangeValue() that does it for some reason?
if (final.Len() == 10 && GetValue().Len() == 10)
{
SetInsertionPointEnd();
}
// Most of the time this won't even change anything, so don't apply the
// space adjustment unless it needs to be done.
if (final != GetValue())
{
int pos = GetInsertionPoint();
ChangeValue(final);
SetInsertionPoint(pos);
}
}
wxString CodeLineInput::mStripToHex(wxString str)
{
/**
* Strip all non-hex characters from str and return the result.
**/
wxString output;
for (size_t i = 0; i < str.Len(); ++i)
if (HEXCHAR.Matches(str[i]))
output += str[i];
return output.Upper().Left(16);
}
#ifndef CODELINEINPUT_H_INCLUDED
#define CODELINEINPUT_H_INCLUDED
#include <wx/textctrl.h>
#include <wx/regex.h>
class CodeLineInput : public wxTextCtrl
{
public:
CodeLineInput(wxWindow *parent,
wxWindowID id,
const wxString &value=_T(""),
const wxPoint &pos=wxDefaultPosition,
const wxSize &size=wxDefaultSize,
long style=0,
const wxValidator &validator=wxDefaultValidator,
const wxString &name=wxTextCtrlNameStr);
private:
static const wxRegEx HEXCHAR;
void mOnKeyDown(wxKeyEvent &evt);
void mOnTextUpdated(wxCommandEvent &evt);
void mFormatText(void);
wxString mStripToHex(wxString str);
};
#endif // CODELINEINPUT_H_INCLUDED
#include <wx/app.h>
#include <wx/frame.h>
#include <wx/panel.h>
#include <wx/sizer.h>
#include "CodeLineInput.h"
class TestApp : public wxApp
{
public:
virtual bool OnInit(void);
};
class TestFrame : public wxFrame
{
public:
TestFrame(void);
};
TestFrame::TestFrame(void) : wxFrame(NULL, wxID_ANY, _T("CodeLineInput Test"))
{
wxPanel *panel = new wxPanel(this, wxID_ANY);
wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
CodeLineInput *input = new CodeLineInput(panel, wxID_ANY);
input->SetMinSize(wxSize(250, input->GetSize().GetY()));
sizer->Add(input, 1, wxEXPAND | wxALL, 5);
panel->SetSizer(sizer);
sizer->SetSizeHints(this);
Center();
}
IMPLEMENT_APP(TestApp)
bool TestApp::OnInit()
{
TestFrame *frame = new TestFrame();
frame->Show(true);
return true;
}
all: clean
all: test
all: run
clean:
@touch test
@rm test
run:
@./test
test:
g++ `wx-config --cxxflags --libs` main.cpp CodeLineInput.cpp -o test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment