[wxPython] TAB, wxTextCtrl and EVT_SET_FOCUS

I have just upgraded to wxPython 2.3.3.1, for Python2.1, WinNT.

I have a control derived from a wxTextCtrl that has the wxTE_PROCESS_TAB
style,
and that tries to control its insertion point and selection very carefully.

However, the tab key now does something altogether unexpected when that tab
traversal on the panel goes to the control.

I get the expected EVT_SET_FOCUS event, at which point, I call
self.GetInsertionPoint()
and self.GetSelection(). These indicate that there is nothing selected, and
that
the insertion point is set to location 0. However, the control then
mysteriously
selects ALL of the text in the control.

I tried to explicitly call self.SetInsertionPoint() and self.SetSelection()
to try to prevent this, and I *even tried refraining from calling
event.Skip()
in my EVT_SET_FOCUS handler* -- to no avail; on tabbing to the control, the
entire
contents remain selected.

If I then unfocus the window, by, say clicking on another window that when
raised,
partially obscures the panel, I see the selection disappear, and on
reselecting
the window, the same EVT_SET_FOCUS handler fires, and the stuff I asked to
be
selected in the EVT_SET_FOCUS handler is properly selected.

So, what gives with the TAB key processing? I do not get an EVT_CHAR for
the
TAB event that took me to the control, so I'm not sure what default event
handling
the underlying wxTextCtrl is doing that changes the selection after the
EVT_SET_FOCUS event happens. Is there any way to prevent this behavior?

Thanks in advance,
/Will Sadkin

Will Sadkin wrote:
> However, the tab key now does something altogether unexpected when
> that tab traversal on the panel goes to the control.
>
> I get the expected EVT_SET_FOCUS event, at which point, I call
> self.GetInsertionPoint() and self.GetSelection(). These indicate
> that there is nothing selected, and that the insertion point is set
> to location 0. However, the control then mysteriously selects ALL of
> the text in the control.

Something that people have been asking for from almost day 1 is that when a field is tabbed into that the text be auto selected like in almost all other Windows apps. That feature has finally been added in 2.3.3.

> I tried to explicitly call self.SetInsertionPoint() and
> self.SetSelection() to try to prevent this, and I *even tried
> refraining from calling event.Skip() in my EVT_SET_FOCUS handler* --
> to no avail; on tabbing to the control, the entire contents remain
> selected.

Apparently this new feature is implemented after the focus event is sent. So what you want to do is reset it *after* the framework does its thing. Try this: create some other method that resets the control to be the way you want it, and then in youe EVT_SET_FOCUS handler schedult that method to be called later like this:

  wxCallAfter(self.MyOtherMethod)

then after the current event and pending events are done then MyOtherMethod will be called and you can fix things up.

···

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

Robin Dunn wrote:

Will Sadkin wrote:
> However, the tab key now does something altogether unexpected when
> that tab traversal on the panel goes to the control.
>
> I get the expected EVT_SET_FOCUS event, at which point, I call
> self.GetInsertionPoint() and self.GetSelection(). These indicate
> that there is nothing selected, and that the insertion point is set
> to location 0. However, the control then mysteriously selects ALL of
> the text in the control.

Something that people have been asking for from almost day 1 is that when a field is tabbed into that the text be auto selected like in almost all other Windows apps. That feature has finally been added in 2.3.3.

> I tried to explicitly call self.SetInsertionPoint() and
> self.SetSelection() to try to prevent this, and I *even tried
> refraining from calling event.Skip() in my EVT_SET_FOCUS handler* --
> to no avail; on tabbing to the control, the entire contents remain
> selected.

Apparently this new feature is implemented after the focus event is sent. So what you want to do is reset it *after* the framework does its thing. Try this: create some other method that resets the control to be the way you want it, and then in youe EVT_SET_FOCUS handler schedult that method to be called later like this:

    wxCallAfter(self.MyOtherMethod)

then after the current event and pending events are done then MyOtherMethod will be called and you can fix things up.

Having the text be selected when you tab into it is only a user-interface convention, so ideally there should be a simpler way to disable it when it is not appropriate. While it is a reasonable thing to do for single-line text controls, it doesn't make much sense for multi-line text controls used as editors. In the latter case, I would normally want the control to remember the old selection.

David

David C. Fox wrote:

Having the text be selected when you tab into it is only a user-interface convention, so ideally there should be a simpler way to disable it when it is not appropriate. While it is a reasonable thing to do for single-line text controls, it doesn't make much sense for multi-line text controls used as editors. In the latter case, I would normally want the control to remember the old selection.

Yes, I was surprised that there wasn't a style for it. I'd suggest entering a bug report about it (*not* wxPython-specific, and *not* assigned to me) and maybe it will get some attention for a future release.

···

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

Robin Dunn wrote:

David C. Fox wrote:

Having the text be selected when you tab into it is only a user-interface convention, so ideally there should be a simpler way to disable it when it is not appropriate. While it is a reasonable thing to do for single-line text controls, it doesn't make much sense for multi-line text controls used as editors. In the latter case, I would normally want the control to remember the old selection.

Yes, I was surprised that there wasn't a style for it. I'd suggest entering a bug report about it (*not* wxPython-specific, and *not* assigned to me) and maybe it will get some attention for a future release.

Okay, I'll do so once I've downloaded 2.3.3.

David