wxTreeCtrl with multiple selection

I'm writing an application using wxPython on Windows XP.
I recently switched a wxTreeCtrl from allowing only
single selection to wxTR_MULTIPLE. This has caused all
sorts of strange behaviour with regards to selection
change events.

(I refer below to a function called SelectionChanged();
this is the selection change event handler bound with
EVT_TREE_SEL_CHANGED).

o Calling wxTreeCtrl.SelectItem() causes the correct
   item in the tree to be highlighted, but no longer
   causes a tree selection change event (i.e.
   SelectionChanged() is not called), as it did when
   it was single selection.
o After the first user selection in the tree, every
   subsequent selection results in two tree selection
   change events. I'm now assuming that this has
   something to do with the hack that allows multiple
   selection on Windows. Is this correct?
o When SelectionChanged() is called for a deselection,
   both wxTreeCtrl.IsSelected() and the list returned by
   wxTreeCtrl.GetSelections() indicate that the
   deselected item is still selected. This appears to
   be transitory; the next time SelectionChanged() is
   called, the previously deselected item doesn't show up
   in the results of GetSelections().
o I've noticed that if you select and deselect a single
   item more than once, you only receive a change event
   for that first selection -- that is, you can select
   and de-select and re-select the item, but only that
   first selection causes an event. After selecting a
   different item, you can select the original item and
   get another event for it. From what I can tell, the
   tree is keeping correct track of the state of the
   item (it is highlighted correctly, etc), it simply
   isn't raising the event.

I've found ways to work around the first three items,
but the fourth has me stumped, as I really would like
to keep track of all tree events. Does anyone have
any advice for me, or even just an explanation of
what's causing this?

Thanks,
Tom Schirmer
Interactive University Project, UC Berkeley

Thomas E Schirmer wrote:

I'm writing an application using wxPython on Windows XP.
I recently switched a wxTreeCtrl from allowing only
single selection to wxTR_MULTIPLE. This has caused all
sorts of strange behaviour with regards to selection
change events.

Since the multiple selection is not supported by the native control and is fully emulated in wx by changing the selection state of individual items, this is not too surprising...

(I refer below to a function called SelectionChanged();
this is the selection change event handler bound with
EVT_TREE_SEL_CHANGED).

o Calling wxTreeCtrl.SelectItem() causes the correct
  item in the tree to be highlighted, but no longer
  causes a tree selection change event (i.e.
  SelectionChanged() is not called), as it did when
  it was single selection.
o After the first user selection in the tree, every
  subsequent selection results in two tree selection
  change events. I'm now assuming that this has
  something to do with the hack that allows multiple
  selection on Windows. Is this correct?
o When SelectionChanged() is called for a deselection,
  both wxTreeCtrl.IsSelected() and the list returned by
  wxTreeCtrl.GetSelections() indicate that the
  deselected item is still selected. This appears to
  be transitory; the next time SelectionChanged() is
  called, the previously deselected item doesn't show up
  in the results of GetSelections().
o I've noticed that if you select and deselect a single
  item more than once, you only receive a change event
  for that first selection -- that is, you can select
  and de-select and re-select the item, but only that
  first selection causes an event. After selecting a
  different item, you can select the original item and
  get another event for it. From what I can tell, the
  tree is keeping correct track of the state of the
  item (it is highlighted correctly, etc), it simply
  isn't raising the event.

I've found ways to work around the first three items,
but the fourth has me stumped, as I really would like
to keep track of all tree events. Does anyone have
any advice for me, or even just an explanation of
what's causing this?

I think it is because the natvie control does not know that the selection is changing, so it is not sending the event. I can't think of a workaround, but it looks like it would be something fairly easy to fix (and probably the first item above at the same time.) Please create a sample app that easily shows the differences between normal and multi-select trees and submit a bug report about it.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!