
#include <wx/wx.h>
#include <wx/dialog.h>
#include <list>

using namespace std;

class MyWnd : public wxFrame
{
	list<wxDialog *>	wndlist;

	void OnClose(wxCloseEvent& ev);
	void OnButton(wxCommandEvent& ev);
public:
	MyWnd(wxWindow *parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size) 
		: wxFrame(parent, id, title, pos, size) {}
	virtual ~MyWnd() {}

	DECLARE_EVENT_TABLE()
};

void MyWnd::OnButton(wxCommandEvent& ev)
{
	switch (ev.GetId()) {
		case 1000:		//exit
			Close();
			break;
		case 1001: {		//create window
			wxDialog	*dlg = new wxDialog(NULL, -1, "whatever window", 
						wxPoint(5 + 50 * wndlist.size(), 5 + 50 * wndlist.size()), wxDefaultSize, wxDIALOG_NO_PARENT | wxDEFAULT_DIALOG_STYLE);

			dlg->Show();
			wndlist.push_back(dlg);
			break;
		}
		default:
			break;
	}
}

void MyWnd::OnClose(wxCloseEvent& ev)
{
	while (wndlist.size() > 0) {
		wndlist.front()->Destroy();
		wndlist.pop_front();
	}
	Destroy();
}

BEGIN_EVENT_TABLE(MyWnd, wxFrame)
	EVT_BUTTON(-1, MyWnd::OnButton)
	EVT_CLOSE(MyWnd::OnClose)
END_EVENT_TABLE()

class MyApp : public wxApp
{
	virtual bool OnInit();
	virtual int OnExit();
public:
	DECLARE_EVENT_TABLE()
};

bool MyApp::OnInit()
{
	MyWnd	*w = new MyWnd(NULL, -1, "Main window", wxDefaultPosition, wxSize(400, 300));

	new wxButton(w, 1000, "Exit", wxPoint(10, 10), wxSize(100, 30));
	new wxButton(w, 1001, "Create window", wxPoint(10, 50), wxSize(100, 30));

	w->Show();
	SetTopWindow(w);

	return TRUE;
}

int MyApp::OnExit()
{
	return 0;
}

BEGIN_EVENT_TABLE(MyApp, wxApp)
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
