WxGrid problem with Resizing and Maximizing

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
fidodido
Knows some wx things
Knows some wx things
Posts: 28
Joined: Mon Jan 22, 2007 8:14 pm
Location: Rome

WxGrid problem with Resizing and Maximizing

Post by fidodido » Wed Apr 15, 2009 7:00 pm

I'd like to have a WxGrid with one colon stretchable.
So I add my WxGrid in a stretchable sizer:

Code: Select all

	WxBoxSizer1 = new wxBoxSizer(wxVERTICAL);
	this->SetSizer(WxBoxSizer1);
	this->SetAutoLayout(true);

	WxGrid1 = new wxGrid(this, ID_WXGRID1, wxPoint(5, 5), wxSize(320, 120));
	WxGrid1->SetDefaultColSize(50);
	WxGrid1->SetDefaultRowSize(25);
	WxGrid1->SetRowLabelSize(50);
	WxGrid1->SetColLabelSize(25);
	WxGrid1->CreateGrid(5,5,wxGrid::wxGridSelectCells);
	WxBoxSizer1->Add(WxGrid1,0,wxALIGN_CENTER | wxEXPAND | wxALL,5);

	SetTitle(wxT("WxGridTst"));
	SetIcon(wxNullIcon);
	
	GetSizer()->Layout();
	GetSizer()->Fit(this);
	GetSizer()->SetSizeHints(this);
	Center();

and then i add the Size event in the event table pointing to my grid resize function:

Code: Select all

BEGIN_EVENT_TABLE(WxGridTstDlg,wxDialog)	
	EVT_SIZE(WxGridResizeFun)
	EVT_CLOSE(WxGridTstDlg::OnClose)
END_EVENT_TABLE()

and this is the resizing function:

Code: Select all

void WxGridTstDlg::WxGridResizeFun(wxSizeEvent& event) {
	
	wxSize GridSize;
	GridSize=WxGrid1->GetSize();
	WxGrid1->SetColSize(2, GridSize.GetWidth()-290);
	event.Skip();
}

All woks fine when i resize the window by border.
But when I maximize and then unmaximize the windows i do not works properly.
It seems that the function works with the old grid size during maximize/unmaximize.

Where i'm wrong ?

van_user
Experienced Solver
Experienced Solver
Posts: 55
Joined: Wed Jun 11, 2008 9:28 pm
Location: UA

Post by van_user » Thu Apr 16, 2009 7:23 am

wxWindow::SetAutoLayout
Please note that this only happens for the windows usually used to contain children, namely wxPanel and wxTopLevelWindow (and the classes deriving from them).
(see help for more)
Win XP (SP0), mingw, wx 2.9.0

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Thu Apr 16, 2009 11:21 am

Hi,

I made this work in the past by looking at when the grid window was being resized, rather than the entire grid.
The logic below is what did the trick for me:

Code: Select all

// in the constructor of the grid...
wxWindow *myWin = this->GetTargetWindow();
myWin->Connect(myWin->GetId(),wxEVT_SIZE,wxSizeEventHandler(myGrid::OnSize),NULL,this);
.
.
.

The event handler:

//------------------------------------------
// Intercept a size event to keep the columns the correct size...
// which will ensure the last column of 3 will expands when the grid expands...
//-----------------------------------------

void myGrid::OnSize(wxSizeEvent &event)
{
	int tot = event.GetSize().GetWidth();
	tot -= this->GetColumnWidth(0);
	tot -= this->GetColumnWidth(1);
	tot -= 30;
	int curSize = this->GetColumnWidth(2);
	if (tot != curSize) {
		if (tot < 200)
			tot = 200;
		this->SetColumnWidth(2,tot);
	}

	event.Skip();
}
Hope that helps,

Jim
OS: Vista SP1, wxWidgets 2.8.7.

fidodido
Knows some wx things
Knows some wx things
Posts: 28
Joined: Mon Jan 22, 2007 8:14 pm
Location: Rome

Post by fidodido » Sat Apr 18, 2009 5:08 pm

van_user wrote:wxWindow::SetAutoLayout
Please note that this only happens for the windows usually used to contain children, namely wxPanel and wxTopLevelWindow (and the classes deriving from them).
(see help for more)
In (wxdialog) CreateGUIControls() I have:

Code: Select all

	SetAutoLayout(true);
	GetSizer()->Layout();
	GetSizer()->Fit(this);
	GetSizer()->SetSizeHints(this);
	Center();
but the problem is the same !
OS: XP SP3/VISTA, wxWidgets 2.8.9.

fidodido
Knows some wx things
Knows some wx things
Posts: 28
Joined: Mon Jan 22, 2007 8:14 pm
Location: Rome

Post by fidodido » Sat Apr 18, 2009 5:25 pm

JimFairway wrote:Hi,

I made this work in the past by looking at when the grid window was being resized, rather than the entire grid.
The logic below is what did the trick for me:

Code: Select all

// in the constructor of the grid...
wxWindow *myWin = this->GetTargetWindow();
myWin->Connect(myWin->GetId(),wxEVT_SIZE,wxSizeEventHandler(myGrid::OnSize),NULL,this);
.
.
.

The event handler:

//------------------------------------------
// Intercept a size event to keep the columns the correct size...
// which will ensure the last column of 3 will expands when the grid expands...
//-----------------------------------------

void myGrid::OnSize(wxSizeEvent &event)
{
	int tot = event.GetSize().GetWidth();
	tot -= this->GetColumnWidth(0);
	tot -= this->GetColumnWidth(1);
	tot -= 30;
	int curSize = this->GetColumnWidth(2);
	if (tot != curSize) {
		if (tot < 200)
			tot = 200;
		this->SetColumnWidth(2,tot);
	}

	event.Skip();
}
Hope that helps,

Jim
I try to add in CreateGUIControls() (the wxdialog constructor) the code:

Code: Select all

WxGrid1->Connect(WxGrid1->GetId(),wxEVT_SIZE,wxSizeEventHandler(WxGridTstDlg::WxGridResizeFun)); 
and then:

Code: Select all

void WxGridTstDlg::WxGridResizeFun(wxSizeEvent& event) {
	
	int tot = event.GetSize().GetWidth(); 
	WxGrid1->SetColSize(2, tot-290);
	
	event.Skip(); 
}
But I have an exception (Eccezione non gestita a 0x006dc30d in WxGrid.exe: 0xC0000005: Violazione di accesso nella lettura del percorso 0x000001d5.) with SetColSize.

bye
OS: XP SP3/VISTA, wxWidgets 2.8.9.

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Sat Apr 18, 2009 8:40 pm

Hi,

Looks like you have a WxGridTstDlg object dealing with a wxGrid event.
You need to change the event sink in the connect to:

Code: Select all

WxGrid1->Connect(WxGrid1->GetId(),wxEVT_SIZE,wxSizeEventHandler(WxGridTstDlg::WxGridResizeFun), NULL, this);
You may need to use the grid window instead, I'm sure I tried what you're doing using the overall grid object, and ended up concluding that it didn't work.

Jim
OS: Vista SP1, wxWidgets 2.8.7.

Post Reply