Page 1 of 1

Question about Grid (again :D)

Posted: Tue Mar 10, 2009 5:53 am
by violethill
Hello all

I have MyGrid derived from wxGrid. I created function to recieve mouse event(when right click on white space)

Code: Select all

MyGrid* m_Grid;
wxMenu* myMenu= new wxMenu(wxEmptyString, 0);
	for(int col = 0; col < m_Grid->GetNumberCols(); col++ ) {
		myMenu->AppendCheckItem(wdCOL_LIST_0 + col
			, m_Grid->GetColLabelValue(col)
			, wxEmptyString);
		myMenu->Check(wdCOL_LIST_0 + col, false);
	}

this->GetGridWindow()->Connect( GetGridWindow()->GetId(), wxEVT_RIGHT_DOWN, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction) &MyGrid::onWhiteSpaceRightClick);


void MyGrid::onWhiteSpaceRightClick(wxMouseEvent &event)
{
	wxWindow* window = (wxWindow*)event.GetEventObject();
	wxGrid* grid = (wxGrid*)window->GetParent();

	int x = event.GetX();
	int y = event.GetY();

	// Get real coordinates for grid
	int sX, sY;
	( ( wxScrolledWindow* ) grid )->CalcUnscrolledPosition( x, y, &sX, &sY );

	int row = grid->YToRow( sY );
	int col = grid->XToCol( sX );

	DEBUG("row = %d col = %d", row, col);
	//right click on white space
    if( col < 0 || row < 0) {
		doPopupMenu(event.GetPosition());
		return;
	}
	else {
		event.Skip(true);
		return;
	}
}

void MyGrid::doPopupMenu(wxPoint pos)
{
	for(int i = 0 ; i < m_Grid->GetNumberCols(); i++) {	
		if(m_Grid->GetColSize(i) > 0)
			myMenu->Check(wdCOL_LIST_0 + i , true);
		else
			myMenu->Check(wdCOL_LIST_0 + i , false);
	}
	PopupMenu(myMenu, pos);

}

My problem is when DoPopupMenu m_Grid has no data(0x00000000) it brokes
and cannot get GetNumberCols()... and I lost myMenu too

So Any idea how to fix it or where my fault is ?

Thanks,

violethiLL

Posted: Tue Mar 10, 2009 8:49 am
by Muetdhiver
This is not a wxWidgets problem but I don't see where the m_Grid pointer is initialized ? It is always null ?
And why it is a global variable ?

And since you are in the Grid object derived from wxGrid, why don't you use directly GetNumberCols() on "this" ?

Posted: Tue Mar 10, 2009 9:10 am
by lester
maybe grid->doPopupMenu(event.GetPosition());( and MyGrid* grid = (MyGrid*)window->GetParent(); ) instead of doPopupMenu(event.GetPosition());?