wxButton with swaping wxBitmaps Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Joelito
Earned some good credits
Earned some good credits
Posts: 128
Joined: Wed Jun 18, 2008 8:35 pm
Location: Tijuana, BC, México

wxButton with swaping wxBitmaps

Post 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?
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux x86_64 with xfce desktop & wxgtk{2,3}-3.0.5.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxButton with swaping wxBitmaps

Post 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);
Joelito
Earned some good credits
Earned some good credits
Posts: 128
Joined: Wed Jun 18, 2008 8:35 pm
Location: Tijuana, BC, México

Re: wxButton with swaping wxBitmaps

Post by Joelito »

Thanks, yeah.... must use a null bitmap before setting a new one
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux x86_64 with xfce desktop & wxgtk{2,3}-3.0.5.
Post Reply