Creating Grid

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
Eman
Experienced Solver
Experienced Solver
Posts: 68
Joined: Sat Aug 31, 2019 2:11 pm

Creating Grid

Post by Eman »

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

Re: Creating Grid

Post by doublemax »

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
Use the source, Luke!
Eman
Experienced Solver
Experienced Solver
Posts: 68
Joined: Sat Aug 31, 2019 2:11 pm

Re: Creating Grid

Post by Eman »

The grid could be 8X8 or 16X16 or 32X32 or 64X64
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Creating Grid

Post by doublemax »

Eman wrote: Sat Aug 31, 2019 2:43 pm The grid could be 8X8 or 16X16 or 32X32 or 64X64
Without knowing any other requirements you might have, i'd go for option #2.
Use the source, Luke!
Eman
Experienced Solver
Experienced Solver
Posts: 68
Joined: Sat Aug 31, 2019 2:11 pm

Re: Creating Grid

Post by Eman »

doublemax wrote: Sat Aug 31, 2019 2:29 pm For smaller grids, you could derive a class from wxPanel to be used for each cell and put them in a wxGrid[Bag]Sizer.
Do you mean using wxGridBagSizer instead of using wxGridSizer?
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Creating Grid

Post by doublemax »

Eman wrote: Sat Aug 31, 2019 3:29 pm
doublemax wrote: Sat Aug 31, 2019 2:29 pm For smaller grids, you could derive a class from wxPanel to be used for each cell and put them in a wxGrid[Bag]Sizer.
Do you mean using wxGridBagSizer instead of using wxGridSizer?
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!
Eman
Experienced Solver
Experienced Solver
Posts: 68
Joined: Sat Aug 31, 2019 2:11 pm

Re: Creating Grid

Post by Eman »

I created the following layout:
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();

};
Cell.cpp

Code: Select all

#include "cell.h"

Cell::Cell()
	:wxPanel()
{
}
Eman
Experienced Solver
Experienced Solver
Posts: 68
Joined: Sat Aug 31, 2019 2:11 pm

Re: Creating Grid

Post by Eman »

align.h

Code: Select all

#include <wx/wx.h>

class Align : public wxFrame
{
public:
    Align(const wxString& title);

};
aligh.cpp

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();
}
Eman
Experienced Solver
Experienced Solver
Posts: 68
Joined: Sat Aug 31, 2019 2:11 pm

Re: Creating Grid

Post by Eman »

main.h

Code: Select all

#include <wx/wx.h>

class MyApp : public wxApp
{
  public:
    virtual bool OnInit();
};
main.cpp

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;
}
I created class Cell that inherits from wxPanel. Then created 2d array of Cells and wxGridSizer

Code: Select all

Cell cellsArray[2][4];
 wxGridSizer *gridM = new wxGridSizer(2,4,0,0);
for testing purposes, I added wxPanel and wxButton to the gridM, and this works perfectly.

Code: Select all

//gridM->Add(new wxButton(panel, -1, wxT("1")));
//gridM->Add(new wxPanel(panel, -1));
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):

Code: Select all

 gridM->Add(cellsArray[1][1]);
it gives error: no matching function for call to 'wxGridSizer::Add(Cell&)'
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Creating Grid

Post by doublemax »

Code: Select all

Cell cellsArray[2][4];
This won't work. First of all, all windows must be created on the heap and they must be properly created with a parent.. And you probably need to use a member variable for this, as you'll have to access it later.
Use the source, Luke!
Post Reply