To go beyond merely displaying an image in a draw widget and allow the user to interact in some way with the displayed image, you must configure the draw widget to generate either button, motion, wheel, or keyboard events:
- Button events are enabled by setting the BUTTON_EVENTS keyword to WIDGET_DRAW. Once enabled, button events are generated when the user clicks on the draw widget.
- Motion events are enabled by setting the MOTION_EVENTS keyword to WIDGET_DRAW. Once enabled, motion events are generated whenever the cursor moves over the draw widget.
-
Wheel events are enabled by setting the WHEEL_EVENTS keyword to WIDGET_DRAW. Once enabled, wheel events are generated when the draw widget has focus and the user rolls the scroll wheel.
Note: Wheel events are enabled only under Microsoft Windows.
- Keyboard events are enabled by setting the KEYBOARD_EVENTS keyword to WIDGET_DRAW. Once enabled, events are generated when the draw widget has focus and a keyboard key is pressed.
The following example uses motion events to update the values of several label widgets as the mouse cursor moves over an image in a draw widget. This and several other features are discussed in the section following the code.
See the file draw_widget_data.pro in the examples/doc/widgets subdirectory of the IDL distribution for the example code. Run this example procedure by entering draw_widget_data at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT draw_widget_data.pro. See Running the Example Code if IDL does not run the program as expected. You may need to enter DEVICE, DECOMPOSED=1 at the IDL command prompt before running this example.
The following things about this example are worth noting:
- Since we use the image data in both the widget creation routine (where we display the image) and the event-handler routine (where we retrieve the value of the data point under the cursor), we need access to the variable that holds the image in both places. We could pass the entire image array from the creation routine to the event-handler in the stash structure, but since the image could be large, we choose to pass a pointer to the image instead. This means we must dereference the pointer variable every time we need to use the image data.
- In this example we have set the MOTION_EVENTS keyword to WIDGET_DRAW; this causes events to be generated continuously as the cursor moves across the draw widget. We could have set the BUTTON_EVENTS keyword instead; this would force the user to click the draw widget in order to update the text fields.