Untitled

 avatar
unknown
c_cpp
3 years ago
2.9 kB
18
Indexable
// require: libwxgtk3.0-gtk3-dev
// compile: g++ ./wxtest.cpp $(wx-config --cppflags) $(wx-config --libs core,base) -o ./wxtestx

#include "wx/wxprec.h"

#ifndef WX_PRECOMP
#	include "wx/wx.h"
#endif
#include <wx/wx.h>

wxDEFINE_EVENT(WX_CONSOLE_REFRESH, wxCommandEvent);

class HelloWorldApp : public wxApp
{
	wxFrame *_frame;
public:
	virtual bool OnInit();
};

DECLARE_APP(HelloWorldApp)
IMPLEMENT_APP(HelloWorldApp)

class TestPanel: public wxPanel
{
	unsigned char _loop = 0;
	wxFrame *_frame = nullptr;
public:
	TestPanel(wxFrame *frame, const wxPoint& pos, const wxSize& size);
	void OnChar( wxKeyEvent& event );
	void OnPaint( wxPaintEvent& event );
	void OnEraseBackground( wxEraseEvent& event ) {}

private:
	wxDECLARE_EVENT_TABLE();
};

wxBEGIN_EVENT_TABLE(TestPanel, wxPanel)
	EVT_CHAR(TestPanel::OnChar)
	EVT_PAINT(TestPanel::OnPaint)
	EVT_ERASE_BACKGROUND(TestPanel::OnEraseBackground)
wxEND_EVENT_TABLE()

TestPanel::TestPanel(wxFrame *frame, const wxPoint& pos, const wxSize& size)
        : wxPanel(frame, wxID_ANY, pos, size, wxWANTS_CHARS | wxNO_BORDER), _frame(frame)
{
}

void TestPanel::OnChar( wxKeyEvent& event )
{
	++_loop;
#if 1 // change to 0 to stop reproduction
	std::wstring title;
	title+= ((_loop & 1) == 0) ? L"MANY " : L"ONE ";
	for (int i = 30 + rand() % 8; i >= 0; --i) {
		title+= (wchar_t)L'A' + (rand() % 52);
	}
	_frame->SetTitle(title.c_str());
	wxGetApp().SetAppDisplayName(title.c_str());
#endif
	Refresh();
	Update();
}

void TestPanel::OnPaint( wxPaintEvent& event )
{
	wxRect box = GetClientRect();
	wxPaintDC dc(this);
	wxSize char_size = dc.GetTextExtent(L"W");
	unsigned int cnt = 0;

	const char *line  = "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW";	
	const char *clear = "                                                                            ";	
	for (unsigned int y = 0; y < 80; ++y) {
		if ((_loop & 1) == 0 || y == 0) {
			dc.DrawText (line, 0, y * char_size.GetHeight());
		} else {
			dc.DrawText (clear, 0, y * char_size.GetHeight());
		}
	}
}

///////////////

class MainFrame: public wxFrame // MainFrame is the class for our window,
{
// It contains the window and all objects in it
	TestPanel *_panel = nullptr;
public:
MainFrame( const wxString &title, const wxPoint &pos, const wxSize &size );

DECLARE_EVENT_TABLE()
};

  BEGIN_EVENT_TABLE ( MainFrame, wxFrame )
  END_EVENT_TABLE() // The button is pressed

  MainFrame::MainFrame(const wxString &title, const wxPoint &pos, const wxSize
    &size): wxFrame(NULL, wxID_ANY, title, pos, size)
  {
	_panel = new TestPanel(this, wxPoint(0, 0), GetClientSize());
  }

// This is executed upon startup, like 'main()' in non-wxWidgets programs.
bool HelloWorldApp::OnInit()
{
	_frame = new MainFrame(_("Hello World!"), wxDefaultPosition, wxSize(800, 600));
	_frame->Show(true);
	SetTopWindow(_frame);
	return true;
}

Editor is loading...