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

Popular posts from this blog

Install Sketchup 2017 64 bits on Linux Ubuntu 16.04 64 bits

Install Sketchup 2017 64 bits on Linux Ubuntu 16.04 64 bits: 1.Enable 32 bit architecture: $sudo dpkg --add-architecture i386  2. Set wine PPA $sudo add-apt-repository ppa:wine/wine-builds Update repository $sudo apt-get update 3. Install wine newest staging branch version $sudo apt-get install --install-recommends winehq-staging 4. Ensure we get a 64bits wine, edit file ~/.profile and locate for text: export WINEARCH= If the value is win64 you are good, if the value is win32 change it to win64. Save the file. Ensure the environment variable also set to win64 by typing command: $export WINEARCH=win64 5. Download winetricks: $wget https://raw.githubusercontent.com/Winetricks/winetricks/master/src/winetricks    Don't forget to set the attribute so it is executable $chmod +x winetricks 6. Run winetricks $./winetricks Inside winetricks: Choose Select the default wineprefix, click OK Choose Install a Windows DLL or component, click OK Choos

NOTEPAD++ SOURCECOOKIFIER SETTING FOR CLIPPER LANGUAGE

Notepad++ is a very excellent programmer editor for Windows. It has plugin system which enhances it's functionality. Sourcecookifier is a plugin I like very much, it is a simple plugin which shows the outline of your source code. The screenshot below shows a pascal (delphi) source code. We can easily see the structure of the source code, and we can go to a particular method/function just by one click. Sourcecookifier recognizes several programming languages, but it does not support Clipper language. Fortunately, it is very easy to add Clipper configure. Follow this step: Click setting icon (the yellow gear icon) on the sourcecookiefier window. Click Language Settings On the setting form, type Clipper on the language box and click add. Set the file extension setting, add .prg to the extenson box On Tag Type choose any letter you like. Here I add “f” for “function” (any letter will be fine) On Appearance box, you can write the de

Disabling middle click paste and close in ubuntu 18.04 by enabling /etc/rc.local

How to disable middle click button in ubuntu 18.04 by enabling /etc/rc.local Create or edit/etc/rc.local file #!/bin/bash xinput set-button-map 11 1 0 3 exit 0 Make sure it is executable $sudo chmod 755 /etc/rc.local Edit rc-local.service file sudo vim /etc/systemd/system/rc-local.service   Add this section [Install]  WantedBy=multi-user.target Enable the service sudo systemctl enable rc-local