ITT Rule IDL Version 7.0 Manuale Utente

Pagina di 430
Chapter 15: Creating a Custom iTool Widget Interface
357
iTool Developer’s Guide
Handling Shutdown Events
Your code should not assume that the top-level base widget will actually be 
destroyed, because the user may decide to cancel the close operation. Since the 
process of actually destroying the widget hierarchy is divorced from the generation of 
the WIDGET_KILL_REQUEST event, you may also need to supply a cleanup 
routine that is invoked only when the widget hierarchy is actually destroyed. 
Writing a Cleanup Routine
A cleanup routine is necessary if your widget interface uses heap variables (pointers 
or objects) to store information about itself — the heap variables will need to be 
cleaned up separately when the interface itself is destroyed. The following code, from 
the cleanup routine in the 
example2_wdtool.pro
 interface definition (developed 
in 
 on page 360), frees the pointer used to store 
the widget interface’s state structure.
PRO example2_wdtool_cleanup, wChild
; Make sure we have a valid widget ID.
IF (~WIDGET_INFO(wChild, /VALID)) THEN $
RETURN
; Retrieve the pointer to the state structure, and
; free it.
WIDGET_CONTROL, wChild, GET_UVALUE = pState
IF (PTR_VALID(pState)) THEN $
PTR_FREE, pState
END
Calling the Cleanup Routine
The final step is to specify when the cleanup routine should be called. Since the user 
can cancel out of the shutdown operation, the cleanup routine should only be called 
when the widget hierarchy is actually destroyed, not when the 
WIDGET_KILL_REQUEST event is handled. We accomplish this by specifying the 
cleanup routine as the value of the KILL_NOTIFY keyword to the WIDGET_BASE 
function.
In the standard iTool widget interface and in our example code, we set the 
KILL_NOTIFY keyword on the first child widget of the top-level base widget. The 
following statement, near the end of the interface definition routine, specifies the 
name of the cleanup routine in the 
example2_wdtool.pro
 interface definition 
(developed in 
WIDGET_CONTROL, wChild, KILL_NOTIFY = "example2_wdtool_cleanup"