|
Getting Started with IDL: Creating Interfaces with the IDL GUIBuilder |
|
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 a smooth operation on the displayed image.
The following example shows in various steps how to create a widget, 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.
To view the property values, right-click on the base, and choose Properties from menu. In the Properties dialog, scroll down to view the X Size and Y Size property values.
To create a draw area that will display PNG image files, follow these steps:
| Note This Properties dialog floats and is resizeable. |
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.
| Note The Name property must be unique to the widget hierarchy. |
When the size of the base was first dragged out, the Component Sizing property 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.
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:
To exit test mode:
To generate the code for the example application, follow these steps:
This action 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. This file will only be appended to when new event handlers are added so changes made will not be lost.
| 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:
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 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 property. 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. |
Now re-save example.pro to be sure that the changes are retained.
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 clicks the name of a color table, it is loaded and the displayed image changes appropriately.
| Note The XLOADCT color table dialog affects only 8-bit display devices. |
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
example_eventcb.pro file.To compile and run your example application, follow these steps:
example at the IDL> command prompt. This compiles and runs the example application, opening the GUI interface that has been created.
IDL Online Help (March 06, 2007)