[INTERNAL] Event handling within widgets - keeping successive events
Anonym
Why does it seem like the event-handler loses the last event by the time the user presses the next button or performs another action that results in a call to the event handler?
Actually, it is correct that between successive events, information is lost.
This Help Article explains the basic structure of event handling in Widgets.
Widget Programming
For many people, widget programming is their first experience with event handler routines and callback style programming. This can seem strange, for example, because a routine other than the main program actually calls the application's event handler routine.
A Widget application registers event handlers (IDL routines that you write), by calling the procedure XMANAGER. The execution of a Widget application begins with events generated by a user, such as menu selections or the positioning of a slider. Whenever one of these events occurs, the XMANAGER groups information about the event, and calls the user's event handler with that information.
Local variables which are defined during the processing of one event may be unavailable when the next event occurs. This is because the event handler is a procedure which is called once for each event, and local variables only exist for the duration of the single execution of the event handler, just like any other procedure.
It is not uncommon for separate procedures, including the event handler, to need access to a variable defined outside of that procedure. This is best done by using pointers to Widget id's passed through structures from the main Widget code to the event handler.
For example, say you have a procedure with Widgets with ID's widID0,1,... etc. You want to have the information in widID0 available to other procedures and to be able to return changes to the data in widID0 to multiple procedures.
PRO widcode
..
..
tlb=WIDGET_BASE()
;define a pointer to widID0
widPTR=PTR_NEW(widID0)
;fill a structure with all widget ID's
info={widID1:widID1,widID2:widID2,...,widPTR:widPTR}
;store structure in uvalue of tlb so it can be retrieved in event handler
WIDGET_CONTROL,tlb,set_uvalue=info
..
..
END
You can then just be sure to save the info structure into the uvalue before you exit the event handler and then you will have access to the info structure back in the main procedure that called the event handler.
You could also define a common block in the event handler and any other routine that needs access to the variable, but in general it is recommended that the programmer avoid common blocks.