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
Experienced Solver
Experienced Solver
Posts: 98
Joined: Wed Jun 18, 2008 8:35 pm
Location: Tijuana, BC, México

wxButton with swaping wxBitmaps

Post by Joelito » Thu Nov 16, 2017 6:34 pm

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-i686 with openbox + tint2; Guest: WinXP SP3 + wxMSW 3.0.2 + msvc 2010.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2094
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxButton with swaping wxBitmaps

Post by PB » Thu Nov 16, 2017 7:47 pm

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
Experienced Solver
Experienced Solver
Posts: 98
Joined: Wed Jun 18, 2008 8:35 pm
Location: Tijuana, BC, México

Re: wxButton with swaping wxBitmaps

Post by Joelito » Fri Nov 17, 2017 12:56 am

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-i686 with openbox + tint2; Guest: WinXP SP3 + wxMSW 3.0.2 + msvc 2010.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

Post Reply