wxListItem background colour in wxListView 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
SalmonsSteve
Knows some wx things
Knows some wx things
Posts: 27
Joined: Sun Jun 24, 2012 6:00 pm

wxListItem background colour in wxListView

Post by SalmonsSteve »

The wxListView adventure continues...
I am trying to change the background colour of list items based on whether the panels they are associated with are active. I have tried a number of ways without success. I thought that the code below would work:

Code: Select all

void GUI::SetPanelState(long index, bool active)
{
    Panels.at(index)->SetActive(active);
    if(active)
    {
        m_list->SetItemBackgroundColour(index,*wxWHITE);
    }
    else
    {
        m_list->SetItemBackgroundColour(index,*wxLIGHT_GREY);
    }
    m_list->RefreshItem(index);
}
Two things, first I don't think I should be using *wxWHITE but *wxNullColour doesn't work with the function, should it just be wxNullColour? It works in the function call but I'm not sure if that is the default background colour for list items.
Second, the code above doesn't have any effect on the control. The code below also seemed useless in this regard:

Code: Select all

void GUI::SetPanelState(long index, bool active)
{
    wxListItem item; 
    Panels.at(index)->SetActive(active);
    item = Panels.at(index)->GetListItem();
    m_list->SetItem(item);
    m_list->RefreshItem(index)  // RefreshItem is actually called in wxListView::SetItem() if the colour has changed
}
In this second bit of code SetActive does m_info.SetBackgroundColour(wxNullColour) or info.SetBackgroundColour(*wxLIGHT_GREY) depending on the value of active. m_info also carries the index (set earlier using SetId()) as well as other information required for this GUI. GetListItem() returns a wxListItem.
It doesn't seem like using SetBackgroundColour() on a list item or SetItemBackgroundColour on the wxListView control should present any challenges, in fact it seems like a pretty basic function call. Does anybody see the problem in the code above?
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxListItem background colour in wxListView

Post by doublemax »

I'm not sure why "m_list->SetItemBackgroundColour(index,*wxWHITE);" doesn't work, but here's some code from the listctrl sample:

Code: Select all

wxListItem item;
item.m_itemId = 0;
item.SetTextColour(*wxRED);
m_listCtrl->SetItem( item );
But this only works if the control is wxLC_REPORT mode.

When setting the state (or any other value) through wxListItem, you also need to set the respective mask flag to indicate which values of the wxListItem structure are valid.
https://docs.wxwidgets.org/trunk/classwx_list_item.html

Or use:

Code: Select all

m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
Use the source, Luke!
SalmonsSteve
Knows some wx things
Knows some wx things
Posts: 27
Joined: Sun Jun 24, 2012 6:00 pm

Re: wxListItem background colour in wxListView

Post by SalmonsSteve »

doublemax wrote: Wed Apr 24, 2019 5:14 am I'm not sure why "m_list->SetItemBackgroundColour(index,*wxWHITE);" doesn't work, but here's some code from the listctrl sample:

Code: Select all

wxListItem item;
item.m_itemId = 0;
item.SetTextColour(*wxRED);
m_listCtrl->SetItem( item );
But this only works if the control is wxLC_REPORT mode.
Sorry for the confustion, it was *wxNullColour that wasn't working. It needed to be wxNullColour.
doublemax wrote: Wed Apr 24, 2019 5:14 am When setting the state (or any other value) through wxListItem, you also need to set the respective mask flag to indicate which values of the wxListItem structure are valid.
https://docs.wxwidgets.org/trunk/classwx_list_item.html

Or use:

Code: Select all

m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
This worked out for me. The unfortunate thing is that when I add a column to the listview (just one column added) it has a header which I can have as an empty string creating a space at the top of the control or I can hide. When I hide the header the control creates a second column that can't be removed because it is out of bounds as it has never actually been added. Does that make sense? This started when I changed the style to wxLC_REPORT as I didn't need to manually add columns prior to that. I need to put together a sample to demonstrate this. If I can't work out that issue I'll start a new thread and post a sample.
Thanks again for your help on this one.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxListItem background colour in wxListView

Post by doublemax »

When I hide the header the control creates a second column that can't be removed because it is out of bounds as it has never actually been added. Does that make sense?
Sorry, no.
Use the source, Luke!
Post Reply