Getting data from clickable panel

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
Earned a small fee
Earned a small fee
Posts: 13
Joined: Sat Aug 31, 2019 2:11 pm

Getting data from clickable panel

Post by Eman » Tue Sep 03, 2019 10:53 am

Hello,
I have class Cell that inherits from wxPanel, and it has its own data members( int x and int y set by i and j in the for loop below)
I used Connect to make each cell clicakble and trigger the click event

Code: Select all

  cellsArray[i][j]->Connect(idd, wxEVT_LEFT_DOWN, wxMouseEventHandler(GUI::OnLeftClick),NULL,this);
OnLeftClick sucessfully show window when a panel is clicked

Code: Select all

void GUI::OnLeftClick(wxMouseEvent& event)
{
    wxMessageBox( wxT("Left mousebutton clicked") );
}
Here is the code:

Code: Select all

GUI.cpp

 ///////////////////////////////////////////////////////////
  int row = 8;
  int col = 8;
  wxGridSizer *gridM = new wxGridSizer(row,col,0,0);
  // 2d array of (cells)panels
  Cell ***cellsArray = new Cell**[row]; // allocate rows
  for(int i = 0; i < row; i++)
  {
	  cellsArray[i] = new Cell*[col]; // allocate columns
  }
  // create (Cells) panles
  wxWindowID idd = 1;
  for(int i=0 ; i < row ; i++)
	  for(int j=0 ; j < col ; j++)
	  {
		  //  wxEVT_LEFT_DCLICK 
		  cellsArray[i][j] = new Cell(i,j,"free",panel, idd, wxDefaultPosition, wxSize(32,32),wxBORDER_SIMPLE);
		  cellsArray[i][j]->Connect(idd, wxEVT_LEFT_DOWN, wxMouseEventHandler(GUI::OnLeftClick),NULL,this);
		  // add wxPanel to wxGridSizer
		  gridM->Add(cellsArray[i][j]);
		  ++idd;
	  }
///////////////////////////////////////////////////////////
But, what I actually need is when a panel is clicked, x and y value must be retrieved from the Cell object.
Any one has an idea how to achieve this?

User avatar
doublemax
Moderator
Moderator
Posts: 13985
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Getting data from clickable panel

Post by doublemax » Tue Sep 03, 2019 11:41 am

Code: Select all

void GUI::OnLeftClick(wxMouseEvent& event)
{
   Cell * cell =  wxDynamicCast( event.GetEventObject(), Cell );
   if( cell != NULL )
   {
   	// access cell here
   }
    wxMessageBox( wxT("Left mousebutton clicked") );
}
Another option would be to handle the mouse click in each Cell, then send a (custom) event upwards to the frame (or whoever needs to process it).
Use the source, Luke!

Post Reply