doublemax wrote: ↑Mon May 20, 2019 4:04 pm
Can you strip together a running sample?
Yes, at least the tab behaviour can be seen, never mind the not working minus button at the end:
Code: Select all
#pragma once
#include <wx/frame.h>
#include <wx/app.h>
#include <wx/grid.h>
class MyGridCellButtonRenderer : public wxGridCellRenderer {
public:
MyGridCellButtonRenderer() {}
MyGridCellButtonRenderer(const MyGridCellButtonRenderer &other) {}
MyGridCellButtonRenderer *Clone() const {
return new MyGridCellButtonRenderer(*this);
}
void Draw(wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rect, int row, int col, bool isSelected) override;
wxSize GetBestSize(wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, int row, int col) override {
return wxSize(13, 13);
}
private:
};
class MyGrid : public wxGrid {
public:
MyGrid(wxWindow *_par) : wxGrid(_par, wxID_ANY) {
CreateGrid(0, 5);
Connect(wxEVT_GRID_CELL_LEFT_CLICK, wxGridEventHandler(MyGrid::onGridLeftClick));
Connect(wxEVT_GRID_TABBING, wxGridEventHandler(MyGrid::onGridTab));
}
void addRows(int addHowMany)
{
int rowCountBefore = GetNumberRows();
AppendRows(addHowMany, false);
int idxNewRow = 0;
wxArrayString choices;
choices.Add(wxT("A"));
for (int idxNewRow = rowCountBefore; idxNewRow < GetNumberRows(); idxNewRow++) {
// tiefe
SetCellAlignment(idxNewRow, 0, wxALIGN_CENTRE, wxALIGN_CENTRE);
SetCellOverflow(idxNewRow, 0, false);
SetCellValue(idxNewRow, 0,wxT("10"));
// code
SetCellEditor(idxNewRow, 1, new wxGridCellChoiceEditor(choices));
SetCellAlignment(idxNewRow, 1, wxALIGN_CENTRE, wxALIGN_CENTRE);
SetCellOverflow(idxNewRow, 1, false);
SetCellValue(idxNewRow, 1, wxT("I"));
// position
SetCellAlignment(idxNewRow, 2, wxALIGN_CENTRE, wxALIGN_CENTRE);
SetCellOverflow(idxNewRow, 2, false);
SetCellValue(idxNewRow, 2, wxT("123"));
// amplitude
wxString decPnt = wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER);
SetCellAlignment(idxNewRow, 3, wxALIGN_CENTRE, wxALIGN_CENTRE);
SetCellOverflow(idxNewRow, 3, false);
SetCellValue(idxNewRow, 3, wxString::Format(wxT("%4.2f"), 5.67f));
// "-" button grafik
SetCellRenderer(idxNewRow, 4, new MyGridCellButtonRenderer);
SetReadOnly(idxNewRow, 4);
}
};
void onGridLeftClick(wxGridEvent &evt) {
CallAfter([this]() { EnableCellEditControl(true); });
evt.Skip();
}
void onGridTab(wxGridEvent &evt) {
OutputDebugString(wxString::Format(wxT("grid tab on %i|%i\n"), evt.GetRow(), evt.GetCol()));
CallAfter([this]() {
if (GetGridCursorCol() < 4) {
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 MyGridCellButtonRenderer::Draw(wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rect, int row, int col, bool isSelected)
{
wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
// draw a check mark in the centre (ignoring alignment - TODO)
wxSize size = GetBestSize(grid, attr, dc, row, col);
// don't draw outside the cell
wxCoord minSize = wxMin(rect.width, rect.height);
if (size.x >= minSize || size.y >= minSize)
{
// and even leave (at least) 1 pixel margin
size.x = size.y = minSize;
}
// draw a border around checkmark
int vAlign, hAlign;
attr.GetAlignment(&hAlign, &vAlign);
wxRect rectBorder;
if (hAlign == wxALIGN_CENTRE)
{
rectBorder.x = rect.x + rect.width / 2 - size.x / 2;
rectBorder.y = rect.y + rect.height / 2 - size.y / 2;
rectBorder.width = size.x;
rectBorder.height = size.y;
}
else if (hAlign == wxALIGN_LEFT)
{
rectBorder.x = rect.x + 2;
rectBorder.y = rect.y + rect.height / 2 - size.y / 2;
rectBorder.width = size.x;
rectBorder.height = size.y;
}
else if (hAlign == wxALIGN_RIGHT)
{
rectBorder.x = rect.x + rect.width - size.x - 2;
rectBorder.y = rect.y + rect.height / 2 - size.y / 2;
rectBorder.width = size.x;
rectBorder.height = size.y;
}
bool value;
if (grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
{
value = grid.GetTable()->GetValueAsBool(row, col);
}
else
{
wxString cellval(grid.GetTable()->GetValue(row, col));
value = wxGridCellBoolEditor::IsTrueValue(cellval);
}
int flags = 0;
wxRendererNative::Get().DrawPushButton(&grid, dc, rectBorder, flags);
dc.SetPen(wxColour(74, 73, 66));
dc.DrawLine(rectBorder.x + 4, rectBorder.y + 6, rectBorder.x + 9, rectBorder.y + 6);
}