Skip to main content

wxWidgets: Adding menu and status bar

Steps to add menu:

  1. Define IDs for our menu items
    enum
    {
    ID_QUIT,
    ID_ABOUT,
    ID_POPUP
    };

  2. Create instances of wxMenu inside the constructor of the main windows
    wxMenu *MenuFile = new wxMenu;
    MenuFile->Append( ID_ABOUT , _T("&About"));
    MenuFile->Append( ID_QUIT , _T("&Quit"));

    wxMenu *MenuData = new wxMenu;
    MenuData->Append( ID_POPUP , _T( "Another &Form"));

  3. Create an instance of wxMenuBar
    wxMenuBar *MenuBar = new wxMenuBar;

  4. Add our instances of wxMenus to the menubar
    MenuBar->Append( MenuFile , _T("&File") );
    MenuBar->Append( MenuData , _T("&Data") );

  5. Assign the menubar to the main windows
    this->SetMenuBar( MenuBar);
  6. Declare event function in our main windows class declaration
    class MainWindow: public wxFrame
    {
    public:
    MainWindow(const wxString& title , const wxPoint& pos , const wxSize& size );
    void MenuClickQuit( wxCommandEvent& event);
    void MenuClickAbout( wxCommandEvent& event);
    void MenuClickPopup( wxCommandEvent& event);

    DECLARE_EVENT_TABLE()
    };
    Don't forget to put the magic macro: DECLARE_EVENT_TABLE()
  7. Write implementation of the event functions.
    void MainWindow::MenuClickQuit( wxCommandEvent& event)
    {
    cout << "Quit\n"; this->Close( TRUE );
    }

    void MainWindow::MenuClickAbout( wxCommandEvent& event)
    {
    wxMessageBox( _T("Learning wxWidgets") , _T( "About" ), wxOK | wxCANCEL | wxICON_INFORMATION , this );
    }

    void MainWindow::MenuClickPopup( wxCommandEvent& event)
    {
    FormData *f = new FormData( this , _T("FormData"), wxPoint(-1,-1) , wxSize(-1,-1) );
    f->Show( TRUE);
    }
    ...and again a magic macro which glues our event to the system:
    BEGIN_EVENT_TABLE( MainWindow , wxFrame)
    EVT_MENU( ID_QUIT , MainWindow::MenuClickQuit)
    EVT_MENU( ID_ABOUT , MainWindow::MenuClickAbout )
    EVT_MENU( ID_POPUP , MainWindow::MenuClickPopup )
    END_EVENT_TABLE()

In our MenuClickPopup event we create a child form. We pass the main window as it's parent in the first parameter. By doing so, if the main window is closed, the child windows will be all closed to.

Here is the complete source:

#include <wx/wx.h>
#include <iostream>
using namespace std;

enum
{
ID_QUIT,
ID_ABOUT,
ID_POPUP
};

// Create our Application class derived from wxApp
class MyApp: public wxApp
{
public:
bool OnInit(); // have to override onInit
};

class MainWindow: public wxFrame
{
public:
MainWindow(const wxString& title , const wxPoint& pos , const wxSize& size );
void MenuClickQuit( wxCommandEvent& event);
void MenuClickAbout( wxCommandEvent& event);
void MenuClickPopup( wxCommandEvent& event);

DECLARE_EVENT_TABLE()
};

class FormData : public wxFrame
{
public:
FormData( wxWindow* Parent,const wxString& Title , const wxPoint& Pos , const wxSize& size);
};


MainWindow::MainWindow(const wxString& title , const wxPoint& pos , const wxSize& size )
: wxFrame( NULL, -1, title, pos, size)
{
this->Show( TRUE);
wxMenu *MenuFile = new wxMenu;
MenuFile->Append( ID_ABOUT , _T("&About"));
MenuFile->Append( ID_QUIT , _T("&Quit"));

wxMenu *MenuData = new wxMenu;
MenuData->Append( ID_POPUP , _T( "Another &Form"));

wxMenuBar *MenuBar = new wxMenuBar;
MenuBar->Append( MenuFile , _T("&File") );
MenuBar->Append( MenuData , _T("&Data") );
this->SetMenuBar( MenuBar);

// status bar
this->CreateStatusBar();
this->SetStatusText( _T("Bambang is learning wxWidgets") );
};

bool MyApp::OnInit()
{
cout << "start\n";
MainWindow *mainFrame = new MainWindow( _T("Hello empty world"),wxPoint(-1,-1),wxSize(-1,-1));
this->SetTopWindow( mainFrame);
//mainFrame->Show( TRUE);
return TRUE;
};

void MainWindow::MenuClickQuit( wxCommandEvent& event)
{
cout << "Quit\n";
this->Close( TRUE );
}

void MainWindow::MenuClickAbout( wxCommandEvent& event)
{
wxMessageBox( _T("Learning wxWidgets") , _T( "About" ), wxOK | wxCANCEL | wxICON_INFORMATION , this );
}

void MainWindow::MenuClickPopup( wxCommandEvent& event)
{
FormData *f = new FormData( this , _T("FormData"), wxPoint(-1,-1) , wxSize(-1,-1) );
f->Show( TRUE);
}



FormData::FormData( wxWindow* Parent,const wxString& Title , const wxPoint& Pos , const wxSize& Size)
: wxFrame( Parent , -1, Title , Pos, Size)
{
};

IMPLEMENT_APP(MyApp);

BEGIN_EVENT_TABLE( MainWindow , wxFrame)
EVT_MENU( ID_QUIT , MainWindow::MenuClickQuit)
EVT_MENU( ID_ABOUT , MainWindow::MenuClickAbout )
EVT_MENU( ID_POPUP , MainWindow::MenuClickPopup )
END_EVENT_TABLE()

Comments

  1. Previewnya om... biar ada semangat ikut mencobaaaa

    ReplyDelete
  2. Maksudnya, screenshotnya? ini masih windows kosong dengan menu bar.... masih belum menarik

    ReplyDelete

Post a Comment