Page 1 of 1

wxButton with swaping wxBitmaps

Posted: Thu Nov 16, 2017 6:34 pm
by Joelito
Hi, been a while.

I'm trying to update the bitmap and the label of the wxButton based on user clicks, for example: toggling play <-> pause button. I've reading around the some users recommend wxThings. On wxWidgets 3.0.3, still no way to change or update the wxBitmap of the wxButton other than ownerdrawing it?

Re: wxButton with swaping wxBitmaps

Posted: Thu Nov 16, 2017 7:47 pm
by PB
wxWidgets GIT master, Windows 10

It seems you are correct and seeing that the impossibility to simply change the bitmap in wxButton is not documented I would consider it a bug.

But it appears that it can be worked around by resetting the bitmap to invalid bitmap prior to assigning a new one.

Code: Select all

#include <wx/wx.h>
#include <wx/artprov.h>


class MyDlg : public wxDialog
{
public:   
    MyDlg() : wxDialog(NULL, wxID_ANY, "Player", wxDefaultPosition, wxSize(200, 200))
    { 
        m_button = new wxButton(this, wxID_ANY);
        m_playing = false;
        UpdateButton();

        Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MyDlg::OnButtonClicked, this);
    }
private:
    wxButton* m_button;
    bool m_playing;
    
    void UpdateButton()
    {
        // reset the bitmap
        m_button->SetBitmap(wxBitmap());

        if ( m_playing )
        {
            m_button->SetBitmap(wxArtProvider::GetBitmap(wxART_TICK_MARK, wxART_BUTTON));
            m_button->SetLabel("Tick");
        }
        else
        {
            m_button->SetBitmap(wxArtProvider::GetBitmap(wxART_CROSS_MARK, wxART_BUTTON));
            m_button->SetLabel("Cross");
        }
    }

    void OnButtonClicked(wxCommandEvent&)
    {         
        m_playing = !m_playing;
        UpdateButton();
    }
};

class MyApp : public wxApp
{
public:          
    bool OnInit()
    {
        MyDlg dlg;

        dlg.ShowModal();
        return false;
    }   
}; wxIMPLEMENT_APP(MyApp);

Re: wxButton with swaping wxBitmaps

Posted: Fri Nov 17, 2017 12:56 am
by Joelito
Thanks, yeah.... must use a null bitmap before setting a new one