以前にwxWidgetsを使ってウインドウを表示させたが、今回はもう少しアプリケーションらしくするために、メニューとステータスバーを追加してみようと思う。
まずは前回のおさらいで、ウインドウを表示するコードは次のようになる。
- decsapp.hpp
#ifndef __DECSAPP_HPP__ #define __DECSAPP_HPP__ #include <wx/wx.hpp> class DECSApp : public wxApp{ virtual bool OnInit(); }; #endif //__DECSAPP_HPP__
- decsapp.cpp
#include "decsapp.hpp" #include "mainwindow.hpp" IMPLEMENT_APP(DECSApp) bool DECSApp::OnInit() { MainWindow* mainWindow = new MainWindow(wxT("Menu and Statusbar"), wxPoint(100, 100), wxSize(500, 300)); mainWindow->Show(); return true; }
- mainwindow.hpp
#ifndef __MAINWINDOW_HPP__ #define __MAINWINDOW_HPP__ #include <wx/wx.h> class MainWindow : public wxFrame{ private: protected: public: MainWindow(const wxString& title, const wxPoint& pos, const wxSize& size); }; #endif //__MAINWINDOW_HPP__
- mainwindow.cpp
#include "mainwindow.hpp" MainWindow::MainWindow(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame(NULL, wxID_ANY, title, pos, size) { }
今回はこれにメニューとステータスバーと追加していく。基本的にmainwindow.hppとmainwindow.cppにメニューとステータスバーのコードを追加していけばよい。まず、メニューとステータスバーの変数をMainWindowクラスに追加する。
- class MainWindow
class MainWindow : public wxFrame{ private: wxMenuBar* menubar_; // メニューバー wxMenu* menuFile_; // ファイルメニュー wxMenu* menuHelp_; // ヘルプメニュー wxStatusBar* statusbar_; // ステータスバー protected: public: MainWindow(const wxString& title, const wxPoint& pos, const wxSize& size); };
これだけではwxMenuBarとwxMenuが未定義となってしまうので、#include <wx/menu.h>も追加する。あわせて、コンストラクタ内で追加した変数の初期化も行う。
- MainWindow::MainWindow
MainWindow::MainWindow(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame(NULL, wxID_ANY, title, pos, size) { this->menubar_ = new wxMenuBar; this->menuFile_ = new wxMenu; this->menuHelp_ = new wxMenu; this->menuFile_->Append(wxID_EXIT, wxT("&Exit\tCtrl+x")); // ファイルメニューにEXITを追加 this->menuHelp_->Append(wxID_ABOUT, wxT("&About")); //ヘルプメニューにAboutを追加 this->menubar_->Append(this->menuFile_, wxT("&File")); // メニューバーにファイルメニューを追加 this->menubar_->Append(this->menuHelp_, wxT("&Help")); // メニューバーにヘルプメニューを追加 this->SetMenuBar(this->menubar_); // メニューバーをウインドウに追加 this->statusbar_ = this->CreateStatusBar(); // ステータスバーを作成 this->statusbar_->SetStatusText(wxT("This is Status Bar.")); // ステータスバーにテキストを設定 }
wxID_EXITやwxID_ABOUTはイベントIDというもので、メニューが実行されたときに発行される。よく使う項目はwxWidgetsにはじめから定義されているので、特に自分で定義する必要はない。
ここまでのコードでメニューは表示されるが、メニューを選んだところで何も起きない。そこで、イベントIDとそれに紐付く関数、イベントハンドラを作成する必要がある。では、OnQuitとOnAbout二つのイベントハンドラを作成する。
- OnQuit
void MainWindow::OnQuit(wxCommandEvent& WXUNUSED(event)) { this->Close(true); }
- OnAbout
void MainWindow::OnAbout(wxCommandEvent& WXUNUSED(event)) { wxMessageDialog* aboutDialog = new wxMessageDialog(NULL, wxT("DECSApp with wxWidgets\nCopyright (c) INCHOKI STUDIO"), wxT("About This Application"), wxOK); aboutDialog->ShowModal(); }
そしてヘッダにも記載する。
- MainWindow
class MainWindow : public wxFrame{ private: wxMenuBar* menubar_; wxMenu* menuFile_; wxMenu* menuHelp_; wxStatusBar* statusbar_; protected: public: MainWindow(const wxString& title, const wxPoint& pos, const wxSize& size); void OnQuit(wxCommandEvent& event); // 追加 void OnAbout(wxCommandEvent& event); //追加 };
OnQuitは単純にClose処理をしてアプリケーションを終了させている。OnAboutではダイアログを表示する。
これだけでは、まだ何も起こらない。最後にイベントとイベントハンドラを結びつける必要がある。それにはConnect関数というのを使用し、MainWindowのコンストラクタに次のコードを追加する。
- MainWindow::MainWindow
MainWindow::MainWindow(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame(NULL, wxID_ANY, title, pos, size) { this->menubar_ = new wxMenuBar; this->menuFile_ = new wxMenu; this->menuHelp_ = new wxMenu; this->menuFile_->Append(wxID_EXIT, wxT("&Exit\tCtrl+x")); this->menuHelp_->Append(wxID_ABOUT, wxT("&About")); this->menubar_->Append(this->menuFile_, wxT("&File")); this->menubar_->Append(this->menuHelp_, wxT("&Help")); this->SetMenuBar(this->menubar_); this->statusbar_ = this->CreateStatusBar(); this->statusbar_->SetStatusText(wxT("This is Status Bar.")); this->Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainWindow::OnQuit)); // 追加 this->Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainWindow::OnAbout)); // 追加 }
Connect関数は引数にイベントID,、イベントの種類、イベントハンドラをとる。
また、「Help」-「About」を選択したときは、
とダイアログが表示される。