7660 Rate this article:
No rating

Using (New) Graphics in an IDL widget program, part II


Last week, I demonstrated how to set up a widget interface that embeds an IDL 8 (aka New) Graphics (NG) window with the WIDGET_WINDOW function. This week, let’s see how event handling works in this program. (Get the full program here.) Start with the event handler for the slider widget:

 pro widget_window_ex1_index_event, event compile_opt idl2 ; Get the state variable. widget_control, event.top, get_uvalue=w state = w.uvalue ; Store the new image index and update the display. state['index'] = event.value widget_window_ex1_display, w end 

This event handler follows the standard IDL widget programming model: it’s a procedure that accepts one parameter, the event structure, from XMANAGER. Recall that I chose to store the NG window reference in the user value of the top-level base, with the actual state variable for the program in the user value of this NG window. This is nonstandard, but it gives one way to pass state information to both the widget event handler and the NG event handler. To unpack the state variable, we reverse these steps in the event handler. In the last two statements, we get the value of the slider from the widget system and store it in the state variable, then call the helper _DISPLAY routine to refresh the window. We’ll look at this helper routine shortly, but let’s next review the event handler for mouse wheel events in the NG window:

 function widget_window_ex1_mouse_wheel_event, w, x, y, d, k compile_opt idl2 ; Get the state variable. state = w.uvalue ; Update the image index based on the value of "d". Also make ; sure the image index stays within [0, n_images-1]. state['index'] += d state['index'] = (state['index'] > 0) < (state['n_images']-1) ; Update the display with the new image. widget_window_ex1_display, w ; Update the value of the slider. widget_control, state['index slider'], set_value=state['index'] ; Cancel the default NG zoom behavior of the scroll wheel. return, 0 end 

Note that this NG event handler is a function. It accepts five parameters: w is the NG window reference, x and y give the event location in device coordinates, d gives the magnitude and direction (positive or negative) of the scroll wheel movement, while k stores the value of any modifier keys used. For more information on these parameters, as well as those for other types of NG window events, please see the entries for WIDGET_WINDOW and “Creating Mouse Event Functions” in the IDL Help. We start this event handler by unpacking the state variable from the NG window reference. Next, we increment or decrement the index of the image by adding the value of the scroll wheel parameter d. This value is usually on [-1,1], but I recall once getting a value of -8 (try adding a PRINT statement to see the values of d). Ensure that the index is in the range [0,56] to avoid a subscripting error. The NG window can then be updated with the newly indexed image from the stack with the helper _DISPLAY routine. The slider widget also needs to be synced up with the new image index value. Last, NG event handlers are expected to return a value of 1, indicating that the default event handling action (for the scroll wheel, this is zooming) should be performed, or 0, which short-circuits the default handling and returns control to the standard widget event loop. I chose here to turn off the default zoom action. Finally, the helper _DISPLAY routine:

 pro widget_window_ex1_display, w compile_opt idl2 state = w.uvalue w['head display'].setdata, (state['image'])[*,*,state['index']] end 

This routine isn’t an event handler, but it helps in the event handling process. I chose to write it as a procedure and accept the NG window reference (which includes the state variable) as a parameter. Here’s where the NG NAME property is handy: the expression w['head display'] is the NG reference returned by IMAGE in the widget creation routine (where it’s called g). Here, the SetData method, introduced in IDL 8.1, is used to update the data used by IMAGE to the current image in the stack. The display in the NG window is automatically updated. Here’s an animated GIF (made with IDL’s WRITE_GIF procedure) showing the program in action!

The widget program WIDGET_WINDOW_EX1 in action!

(I've been enjoying the recent resurgence of animated GIFs in simple scientific visualizations, like here – check out the IDL Direct Graphics!)