|
User Interface Programming: Using the IDL GUIBuilder |
|
The following example takes you through the process of creating your first application with the IDL GUIBuilder and the IDLDE. You will create the user interface and write the event callback routines.
This simple example application contains a menu and a draw widget. When complete, the running application allows the user to open and display a graphics file in PNG format, change the color table for the image display, and perform smooth operations on the displayed image.
This example introduces you to some of the basic procedures you will use to create applications with the IDL GUIBuilder; it shows you how to define menus, create widgets, set widget properties, and write IDL code to handle events.
To define the menu, follow these steps:
New
GUI from the IDLDE menu, or click the "New GUI" button on the IDLDE toolbar.
Your entries should look like those shown in the following figure.
| Note For more information about using the Menu Editor, see Using the Menu Editor. |
Save from the IDLDE menu, which opens the "Save As" dialog.
![]() |
To create a draw area that will display PNG image files, follow these steps:
Later, you will write code to handle the display of the image in this draw area widget. Renaming the widget now will make it easier to write the code later; the "Draw" name is easy to remember and to type.
| Note The Name attribute must be unique to the widget hierarchy. |
When you first dragged out the size of the base, the Component Sizing attribute changed from Default to Explicit—you explicitly sized the widget. Now that the base widget contains items, you can return it to Default sizing, and IDL will handle the sizing of this top-level base.
Save to save your new modifications to the example.prc file. The application should look like the one shown in the following figure.
You can run the application in test mode, which allows you to test the display of widgets and menus. To run your application in test mode, do one of the following:
Both these actions display the interface as it will look when it runs. You can click on the menus, but there is no active event handling in test mode.
To exit test mode, do one of the following:
To generate the code for the example application, select File
Generate .pro. In the "Save As" dialog, find the location where you want the files saved, enter "example.pro" in the File name field, and click Save. This generates an example.pro widget definition file and an example_eventcb.pro event-handling file.
The example.pro file contains the widget definition code, and you should never modify this file. If you decide later to change your interface, you will need to regenerate this interface code, and thus overwrite the widget code file.
The example_eventcb.pro contains place holders for all the event procedures you defined in the IDL GUIBuilder Menu Editor and Properties dialog. You must complete these event procedures by filling in event callback routines. If you generate code after you have modified this file, any new event handling code will not be overwritten but will instead be appended. For information on ways to handle regenerating the *_eventcb.pro file, see Notes on Generating Code a Second Time.
For more information on interface definitions and generated code, see Generating Files.
| Note You should modify only the generated event-handling file ( *_eventcb.pro); you should never modify the generated interface code (the *.pro file). |
You can now modify the generated example_eventcb.pro file to handle the events for the application. First, you will modify the OpenFile routine.
When the user selects Open from the File menu of the example application, the appropriate event structure is sent, and the OpenFile routine handles the event. For this application, the Open menu item will launch an Open dialog to allow the user to choose a PNG file, and then the routine will check the selected file's type, read the image, and display it in the draw area.
To open the file and add the code to handle the OpenFile event, follow these steps:
Open from the IDLDE menu. In the "Open" dialog, select the example_eventcb.pro file, and click Open. This file contains the event handling routine place holders, which you will now complete.
example_eventcb.pro file, locate the OpenFile procedure, which looks like this:pro OpenFile, Event end
| Tip To easily find the OpenFile routine, select OpenFile from the Functions/Procedures drop-down list on the IDLDE toolbar. |
; If there is a file, draw it to the draw widget. sFile = DIALOG_PICKFILE(FILTER='*.png') IF(sFile NE "")THEN BEGIN ; Find the draw widget, which is named Draw. wDraw = WIDGET_INFO(Event.top, FIND_BY_UNAME='Draw'); ; Make sure something was found. IF(wDraw GT 0)THEN BEGIN ; Make the draw widget the current, active window. WIDGET_CONTROL, wDraw, GET_VALUE=idDraw WSET,idDraw ; Read in the image. im = READ_PNG(sFile, r, g, b) ; If TrueColor image, quantize image to pseudo-color: IF (SIZE(im, /N_DIM) EQ 3) THEN $ im = COLOR_QUAN(im, 1, r, g, b) ; Size the image to fill the draw area. im = CONGRID(im, !D.X_SIZE, !D.Y_SIZE) ; Handle TrueColor displays: DEVICE, DECOMPOSED=0 ; Load color table, if one exists: IF (N_ELEMENTS(r) GT 0) THEN TVLCT, r, g, b ; Display the image. TV, im ; Save the image in the uvalue of the top-level base. WIDGET_CONTROL, Event.top, SET_UVALUE=im, /NO_COPY ENDIF ENDIF
| Note In the added code, you used the FIND_BY_UNAME keyword to find the draw widget using its name attribute. In this example, the widget name, "Draw", is the one you gave the widget in the IDL GUIBuilder Properties dialog. The widget name is case-sensitive. |
To add the code that causes the example application to close when the user chooses Exit from the File menu, follow these steps:
pro OnExit, Event end
WIDGET_CONTROL, Event.top, /DESTROY
To add the code that causes the example application to open the IDL color table dialog when the user chooses Load Color Table from the Tools menu, follow these steps:
pro OnColor, Event end
XLOADCT, /BLOCK ; Find the draw widget, which is named Draw: wDraw = WIDGET_INFO(Event.top, FIND_BY_UNAME='Draw') IF(wDraw GT 0) THEN BEGIN ; Make the draw widget the current, active window: WIDGET_CONTROL, wDraw, GET_VALUE=idDraw WSET, idDraw WIDGET_CONTROL,Event.top, GET_UVALUE=im, /NO_COPY ; Make sure the image exists: IF (N_ELEMENTS(im) NE 0) THEN BEGIN ; Display the image: TV, im ; Save the image in the uvalue of the top-level base: WIDGET_CONTROL, Event.top, SET_UVALUE=im, /NO_COPY ENDIF ENDIF
This procedure opens a dialog from which the user can select from a set of predefined color tables. When the user selects a color table, it is loaded and the displayed image changes accordingly.
When the user selects Smooth from the Analyze menu, a smooth operation is performed on the displayed image. The smooth operation displays a smoothed image with a boxcar average of the specified width, which in the example code is 5.
To add the callback routines to handle the smooth operation, follow these steps:
pro DoSmooth, Event end
; Get the image stored in the uvalue of the top-level-base. WIDGET_CONTROL, Event.top, GET_UVALUE=image, /NO_COPY ; Make sure the image exists. IF(N_ELEMENTS(image) GT 0)THEN BEGIN ; Smooth the image. image = SMOOTH(image, 5) ; Display the smoothed image. TV, image ; Place the new image in the uvalue of the button widget. WIDGET_CONTROL, Event.top, SET_UVALUE=image, /NO_COPY ENDIF
Save, to save all your changes to the example_eventcb.pro file.
To compile and run your example application, type example at the IDL> command prompt. The following figure shows the example application and the IDL color table dialog.
In the running application, you can open and display a PNG file. Then, you can open the XLOADCT dialog and change the color table used in displaying the image, or you can perform the smooth procedure on the image.
![]() |
IDL Online Help (March 06, 2007)