wxWidgets (1) - Creating A Toolbar (Visual C++)

wxWidgets - (1) Creating A Toolbar (Visual C++)
  1. Create a new wxWidgets project.

  2. Specify #include <wx/toolbar.h> in main header file.

  3. Declare a tool bar variable in it : wxToolBar m_toolbar

  4. Specify #include <wx/mstream.h> in main source file.

  5. Create three png glyphs(size:16 x 16 pixels with a transparent background color) for the toolbar(new, open, exit). Then convert them to xmp source data with Bin2CU :

    Then copy the generated src_glyphs_png.cpp to the project directory then include it to the project:

  6. Specify #include "src_glyphs_png.cpp" in the main source file for the xpm data inclusion.

  7. Add the following to application OnInit() method for tool button glyphs rendering :
        wxImage::AddHandler(new wxPNGHandler);
        wxImage::AddHandler(new wxXPMHandler);
        wxImage::AddHandler(new wxBMPHandler);
    

  8. Create an event table in the main source file to process main frame close event:
    //----------------------------------------------------------------------------
    wxBEGIN_EVENT_TABLE(MainFrame, wxFrame)
        EVT_CLOSE(MainFrame::OnFrameClose)
    wxEND_EVENT_TABLE()
    //----------------------------------------------------------------------------
    
    

    And declare the following inside the MainFrame class in the header file :
        wxDECLARE_EVENT_TABLE();
    

  9. Declare the following enumeration IDs, in the top area of main header file, for the toolbar buttons to be created :
    //----------------------------------------------------------------------------
    enum enToolID { ID_NEW = 0x0800, ID_OPEN, ID_EXIT
        , citool_ids = ID_EXIT - ID_NEW + 0x01 };
    //----------------------------------------------------------------------------
    

  10. Now declare a toolbar variable in the private section of the main header file:
        wxToolBar m_toolbar;
    

    Then create tool bar creation / deletion function as follows :
    
    //----------------------------------------------------------------------------
    void MainFrame::AllocFreeToolBar(bool bAlloc)
    {
        const size_t cnbts[citool_ids] = { sizeof btn_new_high_png
            , sizeof btn_file_open_png, sizeof btn_exit_png };
        const byte *cpbts[citool_ids] = { btn_new_high_png
            , btn_file_open_png, btn_exit_png };
    
        int iid = ID_NEW, n = 0x00;
    
        if (bAlloc)
        {
            wxToolBarToolBase *pbtn;
            wxImage img;
            wxBitmap bmp;
            wxSize sz(0x10, 0x10);
    
            bmp.Create(sz);
            m_toolbar.Create(this, wxID_ANY);
            m_toolbar.SetToolBitmapSize(sz);
    
            for (; n < citool_ids; iid++, n++)
            {
                wxMemoryInputStream istream(cpbts[n], cnbts[n]);
                img.LoadFile(istream, wxBITMAP_TYPE_PNG);
                bmp = wxBitmap(img);
                pbtn = m_toolbar.AddTool(iid, wxEmptyString, bmp);
                m_toolbar.Bind(wxEVT_TOOL, &MainFrame::OnTollButtonClicks, this);
            }
            m_toolbar.Realize();
            this->SetToolBar(&m_toolbar);
        }
        else
        {
            for (; n < citool_ids; iid++, n++)
                m_toolbar.DeleteTool(iid);
            this->SetToolBar(NULL);
        }
    }
    //----------------------------------------------------------------------------
    

  11. And write the following for tool button click events:
    //----------------------------------------------------------------------------
    void MainFrame::OnTollButtonClicks(wxCommandEvent &evt)
    {
        if (evt.GetId() == ID_EXIT)
        {
            Close(true);
            return;
        }
        wchar_t wsnum[0x0a];
        wxMessageBox(_itow(evt.GetId(), wsnum, 0x0a));
    }
    //----------------------------------------------------------------------------
    
    

  12. Implement the method in the frame constructor to actually create the toolbar at run time:
        AllocFreeToolBar(true);
    

  13. Finally create the frame close event handler then write code for the toolbar removal as follows :
    //----------------------------------------------------------------------------
    void MainFrame::OnFrameClose(wxCloseEvent &evt)
    {
        AllocFreeToolBar(false);
        evt.Skip();
    }
    //----------------------------------------------------------------------------
    

  14. Run the project and we could see the following application with a toolbar on the screen:

    Job Done !
Download Source code for the project (Visual C++ 2013) : src_Test_Toolbar.7z

No comments:

Post a Comment