distinguishing between user generated and program generated changes

Hi,

I'm wondering what is the recommended method to distinguish between
events generated by user input and those generated programmatically.

I understand (source event handling overview wxWidgets help docs) that
for some, but not all, controls there are method calls that allow a
program to modify a control without generating events - but what
should one do for those cases when there is no "silent" method - ie
for TextCtrl?

For a simple model system, using a TextCtrl, I can distinguish if
the program or a user changed the text inside the EVT_TEXT event
handler by checking (and then resetting) a flag whose state is set
prior to calling mytextCtrl.SetValue( newStrValue).

Although the above method appears to work, I have my concerns about
it. Particularly, could I end up with user inputs being mistaken for
programmatically driven ones and vice verse if some user input event
gets generated between the time the flag is set and the production of
the event associated with the programmatic change?

Dominic

DomB wrote:

Hi,

I'm wondering what is the recommended method to distinguish between
events generated by user input and those generated programmatically.

I understand (source event handling overview wxWidgets help docs) that
for some, but not all, controls there are method calls that allow a
program to modify a control without generating events -

Correct. The policy is that calling methods to change values should not result in events being sent, but there are some things that were already doing it before the policy was put into effect so they were kept as-is.

but what
should one do for those cases when there is no "silent" method - ie
for TextCtrl?

TextCtrl now has the ChangeValue method.

For a simple model system, using a TextCtrl, I can distinguish if
the program or a user changed the text inside the EVT_TEXT event
handler by checking (and then resetting) a flag whose state is set
prior to calling mytextCtrl.SetValue( newStrValue).

Using a guard flag like this is typically how it is done.

Although the above method appears to work, I have my concerns about
it. Particularly, could I end up with user inputs being mistaken for
programmatically driven ones and vice verse if some user input event
gets generated between the time the flag is set and the production of
the event associated with the programmatic change?

Since those events won't be processed until after your current event handler returns (unless you call one of the Yield functions) then it isn't possible for pending user input events to short-circuit the processing of the event resulting from calling SetValue.

···

--
Robin Dunn
Software Craftsman

Hi Robin,

Thank you for the feedback - much appreciated.

Dominic

···

On Jul 17, 2:16 pm, Robin Dunn <ro...@alldunn.com> wrote:

DomB wrote:
> Hi,

> I'm wondering what is the recommended method to distinguish between
> events generated by user input and those generated programmatically.

> I understand (source event handling overview wxWidgets help docs) that
> for some, but not all, controls there are method calls that allow a
> program to modify a control without generating events -

Correct. The policy is that calling methods to change values should not
result in events being sent, but there are some things that were already
doing it before the policy was put into effect so they were kept as-is.

> but what
> should one do for those cases when there is no "silent" method - ie
> for TextCtrl?

TextCtrl now has the ChangeValue method.

> For a simple model system, using a TextCtrl, I can distinguish if
> the program or a user changed the text inside the EVT_TEXT event
> handler by checking (and then resetting) a flag whose state is set
> prior to calling mytextCtrl.SetValue( newStrValue).

Using a guard flag like this is typically how it is done.

> Although the above method appears to work, I have my concerns about
> it. Particularly, could I end up with user inputs being mistaken for
> programmatically driven ones and vice verse if some user input event
> gets generated between the time the flag is set and the production of
> the event associated with the programmatic change?

Since those events won't be processed until after your current event
handler returns (unless you call one of the Yield functions) then it
isn't possible for pending user input events to short-circuit the
processing of the event resulting from calling SetValue.

--
Robin Dunn
Software Craftsmanhttp://wxPython.org