Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
2.8 kB
8
Indexable
Never
// compile by:
// 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();
};

wxIMPLEMENT_APP_NO_MAIN(HelloWorldApp);

int main(int argc, char **argv)
{
	if (!wxInitialize())
		return false;

	wxEntry(argc, argv);
	wxUninitialize();
}

class WinPortPanel: public wxPanel
{
	unsigned char _loop = 0;
	wxFrame *_frame = nullptr;
public:
	WinPortPanel(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(WinPortPanel, wxPanel)
	EVT_CHAR(WinPortPanel::OnChar)
	EVT_PAINT(WinPortPanel::OnPaint)
	EVT_ERASE_BACKGROUND(WinPortPanel::OnEraseBackground)
wxEND_EVENT_TABLE()

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

void WinPortPanel::OnChar( wxKeyEvent& event )
{
	++_loop;
	std::wstring title;
	for (int i = 4 + rand() % 8; i >= 0; --i) {
		title+= (wchar_t)'A' + (rand() % 52);
	}
	wxGetApp().SetAppDisplayName(title.c_str());
	_frame->SetTitle(title.c_str());
	Refresh();
}

void WinPortPanel::OnPaint( wxPaintEvent& event )
{
	wxRect box = GetClientRect();
	wxPaintDC dc(this);
	wxSize char_size = dc.GetTextExtent(L"W");
	unsigned int cnt = 0;
	
	for (unsigned int y = 0; y < 80; ++y) {
		wchar_t wz[2] = {0};
		for (unsigned int x = 0; x < 50; ++x) {
			if ((_loop & 1) == 0 || y == 0) {
				wz[0] = L'A' + (cnt % 52);
			} else {
				wz[0] = ' ';
			}
			dc.DrawText (wz, x * char_size.GetWidth(), y * char_size.GetHeight());
			++cnt;
		}
		++y;
	}
}

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

class MainFrame: public wxFrame // MainFrame is the class for our window,
{
// It contains the window and all objects in it
	WinPortPanel *_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 WinPortPanel(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(600, 400));
	_frame->Show(true);
	SetTopWindow(_frame);
	return true;
}