wxGrid wxGridCellNumberEditor wxSP_WRAP

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

wxGrid wxGridCellNumberEditor wxSP_WRAP

Post by mael15 »

Devil seems to be in the details with wxGrid. I have a column displaying an angle, so min == 0 and max == 359 and i want the wxGridCellNumberEditorto wrap. So when the wxSpinCtrl shows 0 and the arrow down is pressed, the value should change to 359.
I tried these thing unsuccessfully:
- Spin()->SetExtraStyle(wxSP_WRAP), wxSP_WRAP is not an extra style

Code: Select all

GridCellNumberEditorWrapping : public wxGridCellNumberEditor {
public:
	GridCellNumberEditorWrapping(wxWindow *par, int min, int max) : wxGridCellNumberEditor(min, max) {
		this;
		wxSpinCtrl *newSpin = new wxSpinCtrl(par, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS | wxSP_WRAP, min, max);
		SetControl(newSpin);		// still uses a different non wrapping spinCtrl and displays this one at position 0, 0
	}
};
Can it be done somehow?
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxGrid wxGridCellNumberEditor wxSP_WRAP

Post by doublemax »

You need to overwrite wxGridCellNumberEditor::Create().
Use the source, Luke!
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: wxGrid wxGridCellNumberEditor wxSP_WRAP

Post by mael15 »

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!
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxGrid wxGridCellNumberEditor wxSP_WRAP

Post by doublemax »

Try wxEVT_TEXT_ENTER instead of wxEVT_COMMAND_ENTER
Use the source, Luke!
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: wxGrid wxGridCellNumberEditor wxSP_WRAP

Post by mael15 »

doublemax wrote: Mon May 27, 2019 3:31 pm Try wxEVT_TEXT_ENTER instead of wxEVT_COMMAND_ENTER
Did not work, seems to be a deeper problem with wxGrid? Like in the other post viewtopic.php?f=1&t=45888 where I failed with the tab key? It is a problem to me because I consider it standard behaviour, the user expects that the value change in the control can be finished by clicking enter, and I would also say that he expects to change the next value after hitting tab.

EDIT: i just realized that the user can just enter a value when the cell is selected, that makes it more usable! I thought he first had to open the editor somehow...
Post Reply