views:

29

answers:

2

I am using wxpython's CustumTreeCtrl. Since some of the items in my tree-hierarchy are supposed to have different textcolours it would useful if these items also keep their textcolours when selected. However, when an item is selected the background colour is automatically changed to blue (that can be controlled with SetHilightFocusColour()) and also the colour of the text is changed to white. But in my case I dont want it to change to white. Is there a way that I can change the text colour of an item when in selected state? SetItemTextColour() only sets the text colour for non-selected items...

Cheers.

A: 

Are you on a Mac? I found this code in the (extremely long...) PaintItem method:

        if wx.Platform == "__WXMAC__" and item.IsSelected() and self._hasFocus:
            dc.SetTextForeground(wx.WHITE)
        dc.DrawLabel(item.GetText(), textrect)

I couldn't be sure for other platforms, but it appears to use the system defaults.

So it looks like the only thing to do is to modify the class to add an internal highlight foreground color, or subclass it and override the OnPaintItem method (with lots of copy pasta, unfortunately).

Edit

A quick hack would be to add this to the __init__ method:

    self.highlight_fgc = wx.WHITE

Then in the OnPaintItem method, you would add this code immediately before the dc.DrawLabel calls at the end of the method:

    dc.SetTextForeground(self.highlight_fgc)

Finally, in your own code, you would set the highlight foreground color:

    self.tree.highlight_fgc = wx.RED # etc...

Or if you want each item to have its own color, you would modify the item (isn't there a "SetItemData" method or similar) to hold the color, and then do:

    dc.SetTextForeground(item.GetItemData()) # or whatever...
Ryan Ginstrom
Thanks for the quick reply. I am on Windows at the moment. So you reckon there is no simpler way...? How exactly would you add an internal highlight foreground color?
dom
I just had a look at this extremely long paintItem method. I am just not really sure how to integrate this into my code...
dom
A: 

Try the latest code from SVN - it may be fixed.

Steven Sproat