Double EVT_LIST_ITEM_SELECTED Events

I'm seeing some strange behavior, and I'd like to find
out if it's a known bug with a known workaround or if
I should try to create a simplified test case.

Environment: Win2K, Python 2.3.2, wxPython 2.5.2.8.

I have a ListCtrl in a window.
Selecting an item in the ListCtrl populates an edit panel.

The window implements a handler for wx.EVT_LIST_ITEM_SELECTED.
Handler pseudocode:

def OnItemSelected( self, evt ):
    selectedIdx = evt.GetIndex()
    if editPanel.Validate() \
          and editPanel.TransferDataFromWindow():
       ### Switch to new item
       ### Tried with or without evt.Skip(); makes no difference
       ### because the problem is in the else: case.
    else:
       # Don't allow change
       # Unselect clicked item
       listctrl.SetItemState( selectedIdx, 0, wx.LIST_STATE_SELECTED )
       # Reselect original item
       wx.CallAfter( selector.SetItemState, self._currentIdx, \
             setflags, setmask )
       ### Tried with or without evt.Skip(); makes no difference

Expected behavior:
   When a validation fails the call to editPanel.Validate()
results in a dialog box. As a result of OnItemSelected,
the old item remains selected and the clicked item remains
unselected.

The problem:
If the switch to the new item is by selecting the listctrl
and hitting the up-arrow or down-arrow key, it works fine.
I get an OnItemSelected event for the change followed by
an OnItemSelected event from the wx.CallAfter (reselecting
the original item). E.g., if item 8 is active and fails
validation when up-arrow is pressed:
     OnItemSelected item 7 --> Error dialog from validator
     OnItemSelected item 8

If the switch to the new item is by mouse-click on the
new listctrl item, I get an *extra* OnItemSelected event
for the requested change. E.g., if item 8 is active
and fails validation when item 7 is clicked on:
     OnItemSelected item 7 --> Error dialog from validator
     OnItemSelected item 8
     OnItemSelected item 7 --> Error dialog from validator
     OnItemSelected item 8

So it seems to generate a duplicate OnItemSelected for
the new item when it originates from a mouse click.

The duplicate error dialog is *really* annoying,
and I *don't* want to release my app to customers
without a fix or workaround.

Is this a known problem?
Is there a fix or workaround?

- Sam

···

__________________________________________________________
Spinward Stars, LLC Samuel Reynolds
Software Consulting and Development 303-805-1446
http://SpinwardStars.com/ sam@SpinwardStars.com

Samuel Reynolds wrote:

The problem:
If the switch to the new item is by selecting the listctrl
and hitting the up-arrow or down-arrow key, it works fine.
I get an OnItemSelected event for the change followed by
an OnItemSelected event from the wx.CallAfter (reselecting
the original item). E.g., if item 8 is active and fails
validation when up-arrow is pressed:
    OnItemSelected item 7 --> Error dialog from validator
    OnItemSelected item 8

If the switch to the new item is by mouse-click on the
new listctrl item, I get an *extra* OnItemSelected event
for the requested change. E.g., if item 8 is active
and fails validation when item 7 is clicked on:
    OnItemSelected item 7 --> Error dialog from validator
    OnItemSelected item 8
    OnItemSelected item 7 --> Error dialog from validator
    OnItemSelected item 8

So it seems to generate a duplicate OnItemSelected for
the new item when it originates from a mouse click.

The duplicate error dialog is *really* annoying,
and I *don't* want to release my app to customers
without a fix or workaround.

Is this a known problem?
Is there a fix or workaround?

It probably has something to do with the listctrl not getting the mouse up event because the dialog is being shown modally and so the listctrl is not getting further events. So when the dialog is closed and focus returns to the listctl it tries to finish what it was doing before....

Try moving more of the OnItemSelected out to another function invoked with wx.CallAfter, so that all pending events will be processed first.

···

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

I moved presentation of an error notification dialog to
a separate routine, and invoked it via wx.CallAfter.
That took care of the doubled event. I guess this change
allows mouse event processing to complete before the
dialog opens.

Now if I can just figure out the correct sequence of
selects and Focus() calls to select the guilty control....

Anyway, thanks!

- Sam

···

At 2004-09-29 06:12 PM -0700, you wrote:

Try moving more of the OnItemSelected out to another function invoked with wx.CallAfter, so that all pending events will be processed first.

__________________________________________________________
Spinward Stars, LLC Samuel Reynolds
Software Consulting and Development 303-805-1446
http://SpinwardStars.com/ sam@SpinwardStars.com