// Compile: // g++ `wx-config --cppflags --libs` wxhello.cpp -o wxhello // #include #include #include #include #define PI 3.141592 class HelloWorldApp : public wxApp { public: bool OnInit(); }; class MyFrame : public wxFrame { public: MyFrame(wxWindow *parent, const wxString& title); void OnMenuFileOpen(wxCommandEvent &event); void OnMenuFileSave(wxCommandEvent &event); void OnMenuFileQuit(wxCommandEvent &event); void OnMenuFileBitmap(wxCommandEvent &e); void OnMenuInfoAbout(wxCommandEvent &event); void OnMenuFileScreenShot(wxCommandEvent &e); void OnMenuFilePrint(wxCommandEvent &e); void OnPaint(wxPaintEvent &event); void OnMotion(wxMouseEvent& event); void Paint(wxDC &dc); private: wxTextCtrl *m_pTextCtrl; // Pekare till text-editor-objektet wxMenuBar *m_pMenuBar; wxMenu *m_pFileMenu; wxMenu *m_pInfoMenu; enum { MENU_FILE_OPEN, MENU_FILE_SAVE, MENU_FILE_QUIT, MENU_FILE_PRINT, MENU_FILE_SS, MENU_INFO_ABOUT, MENU_FILE_BITMAP }; protected: DECLARE_EVENT_TABLE() //Klassen har en event table }; DECLARE_APP(HelloWorldApp) // Skapar en global att nå denna appl. IMPLEMENT_APP(HelloWorldApp) // Skapar kod, antingen main() eller WinMain() beroende på plattform BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(MENU_FILE_OPEN, MyFrame::OnMenuFileOpen) EVT_MENU(MENU_FILE_SAVE, MyFrame::OnMenuFileSave) EVT_MENU(MENU_FILE_BITMAP, MyFrame::OnMenuFileBitmap) EVT_MENU(MENU_FILE_SS, MyFrame::OnMenuFileScreenShot) EVT_MENU(MENU_FILE_PRINT, MyFrame::OnMenuFilePrint) EVT_MENU(MENU_FILE_QUIT, MyFrame::OnMenuFileQuit) EVT_MENU(MENU_INFO_ABOUT, MyFrame::OnMenuInfoAbout) EVT_MOTION(MyFrame::OnMotion) EVT_PAINT(MyFrame::OnPaint) END_EVENT_TABLE() MyFrame::MyFrame(wxWindow *parent, const wxString& title) : wxFrame(parent, -1, title) { // m_pTextCtrl = new wxTextCtrl(this, -1, wxString(L"Mata in text, tack"),wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE); m_pMenuBar = new wxMenuBar(); // File Menu m_pFileMenu = new wxMenu(); // Notera, ampersand (&) ger automatisk keyboard-shortcut m_pFileMenu->Append(MENU_FILE_OPEN, L"Open"); m_pFileMenu->Append(MENU_FILE_SAVE, L"&Save"); m_pFileMenu->Append(MENU_FILE_BITMAP, L"&Bitmap"); m_pFileMenu->Append(MENU_FILE_SS, L"ScreenS&hot"); m_pFileMenu->Append(MENU_FILE_PRINT, L"&Print"); m_pFileMenu->AppendSeparator(); m_pFileMenu->Append(MENU_FILE_QUIT, L"&Quit"); m_pMenuBar->Append(m_pFileMenu, L"&File"); // About menu m_pInfoMenu = new wxMenu(); m_pInfoMenu->Append(MENU_INFO_ABOUT, L"&About"); m_pMenuBar->Append(m_pInfoMenu, L"&Info"); SetMenuBar(m_pMenuBar); //Här sätter vi ny menyrad till fönstret // Add accelerators wxAcceleratorEntry entries[4]; entries[0].Set(wxACCEL_CTRL, (int) 'O', MENU_FILE_OPEN); entries[1].Set(wxACCEL_CTRL, (int) 'S', MENU_FILE_SAVE); entries[2].Set(wxACCEL_CTRL, (int) 'X', MENU_FILE_QUIT); entries[3].Set(wxACCEL_SHIFT, (int) 'A', MENU_INFO_ABOUT); wxAcceleratorTable accel(4, entries); SetAcceleratorTable(accel); } void MyFrame::OnMenuFileOpen(wxCommandEvent &event) { SetStatusText(L"File open aktiverad"); wxFileDialog *dlg = new wxFileDialog(this, L"Open a text file", L"", L"", L"All files(*.*)|*.*|Text Files(*.txt)|*.txt", wxOPEN, wxDefaultPosition); if ( dlg->ShowModal() == wxID_OK) { m_pTextCtrl->LoadFile(dlg->GetPath()); SetStatusText(dlg->GetFilename(), 0); } dlg->Destroy(); } void MyFrame::OnMenuFileSave(wxCommandEvent &event) {SetStatusText(L"File save aktiverad"); wxFileDialog *dlg = new wxFileDialog(this, L"Save a text file", L"", L"", L"All files(*.*)|*.*|Text Files(*.txt)|*.txt", wxSAVE, wxDefaultPosition); if ( dlg->ShowModal() == wxID_OK ) { m_pTextCtrl->SaveFile(dlg->GetPath()); SetStatusText(dlg->GetFilename(), 0); } dlg->Destroy(); } void MyFrame::OnMenuFileQuit(wxCommandEvent &event) {Close(false);} void MyFrame::OnMenuInfoAbout(wxCommandEvent &event) {SetStatusText(L"About aktiverad"); wxMessageBox(L"Minimal Hello World Text Editor\nC J Bjorkqvist,2005", L"About Hello World"); } void MyFrame::OnPaint(wxPaintEvent &event) { wxPaintDC dc(this); Paint(dc); } void MyFrame::Paint(wxDC &dc) { dc.SetBackground(*wxGREEN_BRUSH); dc.Clear(); wxCoord m,n; float rad; dc.GetSize(&m, &n); for (rad=0; rad< 2*PI; rad+=(float)(2.0*PI/100)) { wxCoord x,y; x = cos(rad)*m/5+m/2; y = sin(rad)*n/5+n/2; dc.DrawCircle(x,y, mShowModal() == wxID_OK ) { bitmap.SaveFile(dlg->GetPath(), wxBITMAP_TYPE_BMP); SetStatusText(wxT("Saved bitmap in") + dlg->GetFilename(), 0); } dlg->Destroy(); } void MyFrame::OnMenuFileScreenShot(wxCommandEvent &e) { wxSize screenSize = wxGetDisplaySize(); wxBitmap bitmap(screenSize.x, screenSize.y); wxScreenDC dc; wxMemoryDC memDC; memDC.SelectObject(bitmap); memDC.Blit(0, 0, screenSize.x, screenSize.y, & dc, 0, 0); memDC.SelectObject(wxNullBitmap); wxFileDialog *dlg = new wxFileDialog(this, L"Save screenshot", L"", L"", L"All files(*.*)|*.*|Text Files(*.bmp)|*.bmp", wxSAVE, wxDefaultPosition); if ( dlg->ShowModal() == wxID_OK ) { bitmap.SaveFile(dlg->GetPath(), wxBITMAP_TYPE_BMP); SetStatusText(wxT("Saved screenshot in: ") + dlg->GetFilename(), 0); } } void MyFrame::OnMenuFilePrint(wxCommandEvent &e) { wxPrintDialog *pd = new wxPrintDialog(this); if (pd->ShowModal() == wxID_OK) { wxDC *dc = pd->GetPrintDC(); dc->StartDoc(L"wxhello"); dc->StartPage(); Paint(*dc); // Använder samma ritfunktion dc->EndPage(); dc->EndDoc(); delete dc; } } bool HelloWorldApp::OnInit() { MyFrame *frame = new MyFrame((MyFrame*) NULL, L"Hello World Title"); // Skapar ett nytt fönster frame->CreateStatusBar(); // lägger till status bar frame->SetStatusText(L"Hello World Status Bar"); // Text i status-bar frame->Show(TRUE); // Visar fönstret SetTopWindow(frame); // Sätt till topp-fönster return true; // Om returnerar false, avslutas allt genast }