Question about Grid (again :D)

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
violethill
Earned a small fee
Earned a small fee
Posts: 11
Joined: Tue Feb 03, 2009 8:39 am

Question about Grid (again :D)

Post by violethill » Tue Mar 10, 2009 5:53 am

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

Muetdhiver
Super wx Problem Solver
Super wx Problem Solver
Posts: 323
Joined: Sun Jun 08, 2008 11:59 am
Location: Bordeaux, France

Post by Muetdhiver » Tue Mar 10, 2009 8:49 am

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" ?
OS: Ubuntu 11.10
Compiler: g++ 4.6.1 (Eclipse CDT Indigo)
wxWidgets: 2.9.3

lester
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Sep 02, 2006 7:24 pm
Location: Ukraine

Post by lester » Tue Mar 10, 2009 9:10 am

maybe grid->doPopupMenu(event.GetPosition());( and MyGrid* grid = (MyGrid*)window->GetParent(); ) instead of doPopupMenu(event.GetPosition());?

Post Reply