This has nothing to do with wrapping, but is related to the same example. It try and fail at catching the event of the user pressing enter. Why does this not work?!?
Code: Select all
#pragma once
#include <wx/frame.h>
#include <wx/app.h>
#include <wx/grid.h>
#include "wx/spinctrl.h"
class spinNumberEditor : public wxSpinCtrl {
public:
spinNumberEditor(wxWindow *par, int min, int max) : wxSpinCtrl(par, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS | wxSP_WRAP | wxTE_PROCESS_ENTER, min, max) {
Connect(wxEVT_KEY_UP, wxKeyEventHandler(spinNumberEditor::onKey));
Connect(wxEVT_COMMAND_ENTER, wxCommandEventHandler(spinNumberEditor::onEnter));
}
void onKey(wxKeyEvent& evt) {
if(evt.GetKeyCode() == WXK_RETURN)
int breakHere = 0;
evt.Skip();
}
void onEnter(wxCommandEvent& evt) {
int breakHere = 0;
evt.Skip();
}
};
class GridCellNumberEditorWrapping : public wxGridCellNumberEditor {
public:
GridCellNumberEditorWrapping(wxWindow *par, int _min, int _max) : wxGridCellNumberEditor(min, _max) {}
void Create(wxWindow* parent, wxWindowID id, wxEvtHandler* evtHandler);
private:
int min = 0, max = 359;
};
class MyGrid : public wxGrid {
public:
MyGrid(wxWindow *_par) : wxGrid(_par, wxID_ANY) {
CreateGrid(0, 2);
Connect(wxEVT_GRID_CELL_LEFT_CLICK, wxGridEventHandler(MyGrid::onGridLeftClick));
}
void addRows(int addHowMany)
{
int rowCountBefore = GetNumberRows();
AppendRows(addHowMany, false);
int idxNewRow = 0;
wxArrayString choices;
choices.Add(wxT("A"));
choices.Add(wxT("I"));
for (int idxNewRow = rowCountBefore; idxNewRow < GetNumberRows(); idxNewRow++) {
SetCellEditor(idxNewRow, 0, new GridCellNumberEditorWrapping(this, 0, 100));
SetCellAlignment(idxNewRow, 0, wxALIGN_CENTRE, wxALIGN_CENTRE);
SetCellOverflow(idxNewRow, 0, false);
SetCellValue(idxNewRow, 0,wxT("10"));
SetCellEditor(idxNewRow, 1, new GridCellNumberEditorWrapping(this, 0, 100));
SetCellAlignment(idxNewRow, 1, wxALIGN_CENTRE, wxALIGN_CENTRE);
SetCellOverflow(idxNewRow, 1, false);
SetCellValue(idxNewRow, 1, wxT("20"));
}
};
void onGridLeftClick(wxGridEvent &evt) {
CallAfter([this]() { EnableCellEditControl(true); });
evt.Skip();
}
};
class MyFrame1 : public wxFrame
{
public:
MyFrame1( wxWindow* parent = NULL,
wxWindowID id = wxID_ANY,
const wxString& title = wxEmptyString,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxSize( 500,300 ),
long style = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );
~MyFrame1(){}
MyGrid *grid = nullptr;
wxLocale loc;
};
class MyApp : public wxApp {
virtual bool OnInit();
};
Code: Select all
#include "app.h"
#include <wx/sizer.h>
#include <wx/grid.h>
#include "wx\renderer.h"
bool MyApp::OnInit() {
MyFrame1 *mainFrame = new MyFrame1();
mainFrame->Show();
SetTopWindow(mainFrame);
return TRUE;
}
wxIMPLEMENT_APP(MyApp);
MyFrame1::MyFrame1( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) :
wxFrame( parent, id, title, pos, size, style )
{
loc.Init(wxLanguage::wxLANGUAGE_GERMAN);
SetSizer(new wxBoxSizer(wxVERTICAL));
grid = new MyGrid(this);
// Then we call CreateGrid to set the dimensions of the grid
// (100 rows and 10 columns in this example)
GetSizer()->Add(grid, 0, wxEXPAND);
grid->addRows(2);
this->SetSizeHints( wxDefaultSize, wxDefaultSize );
this->Centre( wxBOTH );
}
void GridCellNumberEditorWrapping::Create(wxWindow* parent, wxWindowID id, wxEvtHandler* evtHandler)
{
this;
spinNumberEditor *newSpin = new spinNumberEditor(parent, min, max);
SetControl(newSpin);
wxGridCellEditor::Create(parent, id, evtHandler);
}
Thank you!