Finishing the Widget Program
The final steps of the program involve closing the widgets and cleaning up the pointers. The event handler for the Done button closes the TLB widget, which closes all the other widgets. But the pointers you designated in the state structure still exist within IDL until you free them, which is done in the cleanup event handler.
Creating the Done Button Event Handler
This event handler is very simple, because it just destroys the top-level widget, which destroys all the other widgets within it. Place this event handling routine after the droplist procedure and above the primary procedure. (The primary procedure must always be the last procedure in the file.)
; Event handler for the Done button. PRO basic_draw_doc_done, event ; Destroy the top-level widget ; (which destroys all other widgets). WIDGET_CONTROL, event.top, /DESTROY END
In addition to this routine, you must also add some information to the WIDGET_BUTTON definition, telling it which event handler to use.
You can now choose images from the droplist, resize, and close the widget. In the next section, we will add the final necessary event handler to clean up the pointers for the widget program.
Creating the Cleanup Procedure
The cleanup procedure frees the pointers on closing the widget. If you do not clean up the pointers, they are still available to IDL and may cause problems if you run another instance of the program.
Place this routine after the basic_draw_doc_done procedure and above the primary procedure, basic_draw_doc. (The primary procedure must always be the last procedure in the file.)
; Cleanup procedure cleans up the pointers on closing the widget. PRO basic_draw_doc_cleanup, base ; Clean up pointers. WIDGET_CONTROL, base, GET_UVALUE=state IF N_Elements(state) EQ 0 THEN RETURN PTR_FREE, state.image END
In addition to this routine, you must also edit the XMANAGER statement, defining the cleanup procedure to use.
Congratulations - you have just written an IDL widget program!
The program is now complete and should run as intended. You can choose any image from the droplist, resize the window and image, and cleanly close the program.
See IDL Pointers and Freeing Pointers for more information.
Note
If the program does not work correctly, check your code against the different routines shown in this tutorial. It's easy to miss adding the extra lines in the primary routine after you add the event handling routines. You can also compare your code to the full example program (draw_basic_doc.pro) in the examples\doc\widgets subfolder of your IDL distribution. Open the program by typing the following command on the IDL command line (or simply click on the link): .edit draw_basic_doc.pro