Creating Grid
Creating Grid
Hello,
I am not new to C++ but new to wxWidgets. I need to draw a 2d grid such that each cell has a coordinates (1,1) (1,2)....
I need the cells to be clickable, so that when a cell is clicked, its coordinate is registered to be used in some calculations.
Also, I need to set a color to cells(set color to specific coordinate) based on the output of calculated function.
What is the suitable library or packages I could use?
Thanks a lot in advance,
I am not new to C++ but new to wxWidgets. I need to draw a 2d grid such that each cell has a coordinates (1,1) (1,2)....
I need the cells to be clickable, so that when a cell is clicked, its coordinate is registered to be used in some calculations.
Also, I need to set a color to cells(set color to specific coordinate) based on the output of calculated function.
What is the suitable library or packages I could use?
Thanks a lot in advance,
Re: Creating Grid
What would be the maximum size of that grid?
For big grids (> more than 500-1000 cells), wxGrid would be the best option, but it requires more setup code.
https://docs.wxwidgets.org/trunk/classwx_grid.html
Also check the "grid" demo that comes with wxWidgets.
For smaller grids, you could derive a class from wxPanel to be used for each cell and put them in a wxGrid[Bag]Sizer.
Another option would be just one custom drawn panel where you draw each cell, and also handle the mouse clicks yourself.
https://wiki.wxwidgets.org/Painting_your_custom_control
For big grids (> more than 500-1000 cells), wxGrid would be the best option, but it requires more setup code.
https://docs.wxwidgets.org/trunk/classwx_grid.html
Also check the "grid" demo that comes with wxWidgets.
For smaller grids, you could derive a class from wxPanel to be used for each cell and put them in a wxGrid[Bag]Sizer.
Another option would be just one custom drawn panel where you draw each cell, and also handle the mouse clicks yourself.
https://wiki.wxwidgets.org/Painting_your_custom_control
Use the source, Luke!
Re: Creating Grid
The grid could be 8X8 or 16X16 or 32X32 or 64X64
Re: Creating Grid
I meant the general solution of subclassing wxPanel for each cell and positioning them with a wxGridSizer of wxGridBagSizer. If the cells are all the same size, using a wxGridSizer should be sufficient.
Use the source, Luke!
Re: Creating Grid
I created the following layout:
vboxMap ==> contains the 2d grid in the left
vboxControls ==> contains the (ok & cancel) buttons in the right
Cell.h
Cell.cpp
vboxMap ==> contains the 2d grid in the left
vboxControls ==> contains the (ok & cancel) buttons in the right
Cell.h
Code: Select all
#include <wx/wx.h>
class Cell : public wxPanel
{
public:
Cell();
};
Code: Select all
#include "cell.h"
Cell::Cell()
:wxPanel()
{
}
Re: Creating Grid
align.h
aligh.cpp
Code: Select all
#include <wx/wx.h>
class Align : public wxFrame
{
public:
Align(const wxString& title);
};
Code: Select all
#include "align.h"
#include "cell.h"
Align::Align(const wxString& title)
: wxFrame(NULL, -1, title, wxPoint(-1, -1), wxSize(500, 500))
{
wxPanel *panel = new wxPanel(this, -1);
wxBoxSizer *vboxMap = new wxBoxSizer(wxVERTICAL);
wxBoxSizer *vboxControls = new wxBoxSizer(wxVERTICAL);
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
Cell cellsArray[2][4];
wxGridSizer *gridM = new wxGridSizer(2,4,0,0);
wxButton *ok = new wxButton(panel, -1, wxT("Ok"));
wxButton *cancel = new wxButton(panel, -1, wxT("Cancel"));
vboxControls->Add(ok);
vboxControls->Add(cancel);
//gridM->Add(new wxButton(panel, -1, wxT("1")));
//gridM->Add(new wxPanel(panel, -1));
gridM->Add(cellsArray[1][1]);
gridM->Add(new wxButton(panel, -1, wxT("2")));
gridM->Add(new wxButton(panel, -1, wxT("3")));
gridM->Add(new wxButton(panel, -1, wxT("4")));
gridM->Add(new wxButton(panel, -1, wxT("5")));
gridM->Add(new wxButton(panel, -1, wxT("6")));
gridM->Add(new wxButton(panel, -1, wxT("7")));
gridM->Add(new wxButton(panel, -1, wxT("8")));
vboxMap->Add(gridM);
hbox->Add(vboxMap, 1, wxEXPAND);
hbox->Add(vboxControls, 0, wxALIGN_RIGHT, 10);
panel->SetSizer(hbox);
Centre();
}
Re: Creating Grid
main.h
main.cpp
I created class Cell that inherits from wxPanel. Then created 2d array of Cells and wxGridSizer
for testing purposes, I added wxPanel and wxButton to the gridM, and this works perfectly.
But no border for the panel, is there any way to add it?
My main problem is:
adding cellsArray element to gridM is not working(Cell inherits from wxPanel):
it gives error: no matching function for call to 'wxGridSizer::Add(Cell&)'
Code: Select all
#include <wx/wx.h>
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
Code: Select all
#include "main.h"
#include "align.h"
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
Align *align = new Align(wxT("Align"));
align->Show(true);
return true;
}
Code: Select all
Cell cellsArray[2][4];
wxGridSizer *gridM = new wxGridSizer(2,4,0,0);
Code: Select all
//gridM->Add(new wxButton(panel, -1, wxT("1")));
//gridM->Add(new wxPanel(panel, -1));
My main problem is:
adding cellsArray element to gridM is not working(Cell inherits from wxPanel):
Code: Select all
gridM->Add(cellsArray[1][1]);
Re: Creating Grid
Code: Select all
Cell cellsArray[2][4];
Use the source, Luke!