Adsense code (2021-10-05)

Wednesday, August 20, 2014

Add Button to wxWidgets project

Create wxWidgets Project with Code::Blocks, as shown in last post.


To add a Buuton, modify HelloWxMain.h and HelloWxMain.h.cpp.

/***************************************************************
 * Name:      HelloWxMain.h
 * Purpose:   Defines Application Frame
 * Author:    Buddy ()
 * Created:   2014-08-21
 * Copyright: Buddy (linux-buddy.blogspot.com)
 * License:
 **************************************************************/

#ifndef HELLOWXMAIN_H
#define HELLOWXMAIN_H

#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif

#include "HelloWxApp.h"

class HelloWxFrame: public wxFrame
{
    public:
        HelloWxFrame(wxFrame *frame, const wxString& title);
        ~HelloWxFrame();
    private:

        //add for "Hello Button"
        static const long idButtonHello;
        wxBoxSizer* m_MainBox;
        wxButton* m_ButtonHello;
        void onClickHello(wxCommandEvent& event);
        //end of "Hello Button"

        enum
        {
            idMenuQuit = 1000,
            idMenuAbout
        };
        void OnClose(wxCloseEvent& event);
        void OnQuit(wxCommandEvent& event);
        void OnAbout(wxCommandEvent& event);
        DECLARE_EVENT_TABLE()
};


#endif // HELLOWXMAIN_H

/***************************************************************
 * Name:      HelloWxMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    Buddy ()
 * Created:   2014-08-21
 * Copyright: Buddy (linux-buddy.blogspot.com)
 * License:
 **************************************************************/

#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#include "HelloWxMain.h"

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__WXMAC__)
        wxbuild << _T("-Mac");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

const long HelloWxFrame::idButtonHello = ::wxNewId();   //Hello Button

BEGIN_EVENT_TABLE(HelloWxFrame, wxFrame)
    EVT_CLOSE(HelloWxFrame::OnClose)
    EVT_MENU(idMenuQuit, HelloWxFrame::OnQuit)
    EVT_MENU(idMenuAbout, HelloWxFrame::OnAbout)
    EVT_BUTTON(idButtonHello, HelloWxFrame::onClickHello) //Hello Button
END_EVENT_TABLE()

HelloWxFrame::HelloWxFrame(wxFrame *frame, const wxString& title)
    : wxFrame(frame, -1, title)
{
#if wxUSE_MENUS
    // create a menu bar
    wxMenuBar* mbar = new wxMenuBar();
    wxMenu* fileMenu = new wxMenu(_T(""));
    fileMenu->Append(idMenuQuit, _("&Quit\tAlt-F4"), _("Quit the application"));
    mbar->Append(fileMenu, _("&File"));

    wxMenu* helpMenu = new wxMenu(_T(""));
    helpMenu->Append(idMenuAbout, _("&About\tF1"), _("Show info about this application"));
    mbar->Append(helpMenu, _("&Help"));

    SetMenuBar(mbar);
#endif // wxUSE_MENUS

#if wxUSE_STATUSBAR
    // create a status bar with some information about the used wxWidgets version
    CreateStatusBar(2);
    SetStatusText(_("Hello Code::Blocks user!"),0);
    SetStatusText(wxbuildinfo(short_f), 1);
#endif // wxUSE_STATUSBAR

    //Hello Button
    this->SetSizeHints(wxDefaultSize, wxDefaultSize);
    m_MainBox = new wxBoxSizer(wxHORIZONTAL);

    m_ButtonHello = new wxButton(
                            this,
                            idButtonHello,
                            _T("Hello...!"),
                            wxDefaultPosition,
                            wxDefaultSize,
                            0);
    m_MainBox->Add(m_ButtonHello,
                    0,
                    wxALL,
                    5);

    this->SetSizer(m_MainBox);
    this->Layout();
}


HelloWxFrame::~HelloWxFrame()
{
}

void HelloWxFrame::OnClose(wxCloseEvent &event)
{
    Destroy();
}

void HelloWxFrame::OnQuit(wxCommandEvent &event)
{
    Destroy();
}

void HelloWxFrame::OnAbout(wxCommandEvent &event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

//Hello Button Clicked
void HelloWxFrame::onClickHello(wxCommandEvent &event)
{
    wxString msg = _T("Hello World");
    wxString info = _T("linux-buddy");
    wxMessageBox(msg,
                info,
                wxOK | wxICON_INFORMATION,
                this);
}

No modify on HelloWxApp.h and HelloWxApp.cpp

/***************************************************************
 * Name:      HelloWxApp.h
 * Purpose:   Defines Application Class
 * Author:    Buddy ()
 * Created:   2014-08-21
 * Copyright: Buddy (linux-buddy.blogspot.com)
 * License:
 **************************************************************/

#ifndef HELLOWXAPP_H
#define HELLOWXAPP_H

#include <wx/app.h>

class HelloWxApp : public wxApp
{
    public:
        virtual bool OnInit();
};

#endif // HELLOWXAPP_H

/***************************************************************
 * Name:      HelloWxApp.cpp
 * Purpose:   Code for Application Class
 * Author:    Buddy ()
 * Created:   2014-08-21
 * Copyright: Buddy (linux-buddy.blogspot.com)
 * License:
 **************************************************************/

#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#include "HelloWxApp.h"
#include "HelloWxMain.h"

IMPLEMENT_APP(HelloWxApp);

bool HelloWxApp::OnInit()
{
    HelloWxFrame* frame = new HelloWxFrame(0L, _("wxWidgets Application Template"));
    
    frame->Show();
    
    return true;
}