Page 1 of 1

problem about the event "onsize" under xrc resourc

Posted: Sun Feb 26, 2006 12:42 pm
by chenzhengxi
Thanks all the people who attention my problem
I want to resize my widget treectrl to fit the panel size
but it bring me so many problem.
it cannot be closed successful,the frame disappear but the programe still remain in memory.I can abide the it,but another problem is annoying,If i wrote

Code: Select all

myTreeCtrl->SetSize(m_panel->GetSize());
in the onsize functor ,there will be a memory read/write err.
I don`t know where is wrong,because I do`nt know the xrc theory.
I think:
the functor LoadFrame will assign resource for all its subwidgets.
May be need LoadPanel If I have a panel.so I need not use the operator "new" for every subwidgets and just like this

Code: Select all

myTreeCtrl=XRCCTRL(*this,"treectrl_1",MyTreeCtrl);
the first parameter maybe myTreeCtrl `s father frame,I think it should be
use panel like this

Code: Select all

myTreeCtrl=XRCCTRL(*m_panel,"treectrl_1",MyTreeCtrl);
but it cannot be compiler.
Please tell me where is wrong,I think the proble may be in MyFrame`s
construct
:(

Code: Select all

#include <wx/wx.h>
#include <wx/treectrl.h>
#include <wx/xrc/xmlres.h> 
class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};
class MyTreeCtrl :public wxTreeCtrl
{
public:
	MyTreeCtrl(wxWindow *parent):wxTreeCtrl(parent)
	{
	}
};
class MyFrame : public wxFrame
{
public:
    MyFrame(wxWindow* parent);
	void OnSize(wxSizeEvent& event);
	void Resize();
private:
	wxPanel *m_panel;
	MyTreeCtrl* myTreeCtrl;
	DECLARE_EVENT_TABLE()
};
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
    wxXmlResource::Get()->InitAllHandlers();
    wxXmlResource::Get()->Load(wxT("1.xrc"));
    MyFrame *frame = new MyFrame(wxT(NULL));
    frame->Show(true);
    return true;
}
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
	EVT_SIZE(MyFrame::OnSize)
END_EVENT_TABLE()
MyFrame::MyFrame(wxWindow* parent):wxFrame(parent,-1,"hello")
{
	wxXmlResource::Get()->LoadFrame(this,parent, wxT("frame_1"));
	m_panel=wxXmlResource::Get()->LoadPanel(parent, wxT("panel_1"));
	myTreeCtrl=XRCCTRL(*this,"treectrl_1",MyTreeCtrl);
	wxTreeItemId rootID = myTreeCtrl->AddRoot(wxT("Root"),-1,-1,NULL);
}
void MyFrame::OnSize(wxSizeEvent& event)
{
	Resize();
	event.Skip();
}
void MyFrame::Resize()
{
	if(myTreeCtrl&&m_panel)
	{
		myTreeCtrl->SetSize(m_panel->GetSize());
	}
}
xrc file

Code: Select all

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- generated by wxGlade 0.4 on Sun Feb 26 13:17:04 2006 -->

<resource version="2.3.0.1">
    <object class="wxFrame" name="frame_1" subclass="MyFrame">
        <style>wxDEFAULT_FRAME_STYLE</style>
        <title>frame_1</title>
        <object class="wxBoxSizer">
            <orient>wxVERTICAL</orient>
            <object class="sizeritem">
                <option>1</option>
                <flag>wxEXPAND</flag>
                <object class="wxPanel" name="panel_1">
                    <style>wxTAB_TRAVERSAL</style>
                         <flag>wxEXPAND</flag>
                           <flag>wxADJUST_MINSIZE</flag>
                           <object class="wxTreeCtrl" name="treectrl_1">
                               <label>asdsdfhfghfgh</label>
                           </object>
                </object>
            </object>
        </object>
    </object>
</resource>

Posted: Sun Feb 26, 2006 3:17 pm
by DavidHart
Hi,

Your code has various problems.
it cannot be closed successful,the frame disappear but the programe still remain in memory
MyFrame::MyFrame(wxWindow* parent):wxFrame(parent,-1,"hello")
{
wxXmlResource::Get()->LoadFrame(this,parent, wxT("frame_1"));

You are creating the frame twice, once in each bold statement. You only need the second one.
m_panel=wxXmlResource::Get()->LoadPanel(parent, wxT("panel_1"));
You shouldn't do this. m_panel was loaded as part of the frame. You should do:
m_panel=XRCCTRL(*this,"panel_1",wxPanel);
the first parameter maybe myTreeCtrl`s father frame,I think it should be
use panel like this
myTreeCtrl=XRCCTRL(*m_panel,"treectrl_1",MyTreeCtrl);
This should now work.

I haven't tested it, but I think your code will now "work". However, you are subclassing wxFrame in the XRC file, but it isn't a dynamic class (see http://www.wxwindows.org/manuals/2.6.2/ ... ssoverview), and you need a constructor without parameters. Without doing this, subclassing fails silently. Oh, and XRC ignores constructors with parameters, so MyTreeCtrl(wxWindow *parent); won't be called.

BTW, if you add a sizer to the panel and add the treectrl to this with wxGROW and a proportion of 1, you probably won't need your resize code.

HTH,

David

XRC

Posted: Sun Feb 26, 2006 4:48 pm
by ray1341
Chenzhengxi,

I made a few changes (some already mentioned) to your program and xrc file in order to get a clean compile and have the program run and end without any strange problems:

Before:

Posted: Sun Feb 26, 2006 7:02 pm
by chenzhengxi
DavidHart
thx for the reply,now I have solved many bored problems .My project is run ok now,it is very easy when I know the theory.
ray1341
thx you too
very unlucky,it cannot run when your changed it,the debug information showed the problem is LoadObject,May be the Programe can run on your pc.It is useful to detach the xrc file.I`ll adopt it when the LoadObject problem be solved.Followed DavidHart`s advise,It is enough for me now.