Object widgets
posted Wed 14 Jun 2006 by Michael Galloy under IDL, Object graphics, Objects, Widgets
A couple weeks ago, I wrote a demo program to view JPEG 2000 images as a “regular” widget program. Now I want to rewrite the same program as an “object widget,” in other words write methods of a class instead of normal functions and procedures. You need to already understand the basics of object-oriented and widget programming in IDL to follow along with this example. The following files are needed for this program: mgtilejp2__define.pro (doc), mgobjectwidget__define.pro (doc), mg_object_event_handler.pro (doc), and mg_object_cleanup.pro (doc).
This new program will have exactly the same functionality as the old program. So what are the advantages of writing the program as an object? Of course it’s a matter of preference and some will already have a preference to use or avoid objects. Here are my thoughts about the advantages of this approach:
- Cleaner: state/pstate doesn’t need to be passed around.
- Better encapsulation when multiple programs are interacting with each other and passing messages to each other. In other words, one program doesn’t need to know internal details about the other program in order to pass a message to it.
New architecture
There are several techniques to make “object widgets.” I found the following technique simple yet still has the advantages of object-oriented programming. The steps to making a simple object widget:
- The member variables of the object hold what used to be in the
stateorpstate. - The
initmethod is the widget creation part of the program. One important item: placeselfin the UVALUE of the top-level base. This will make our scheme for event handling work. XMANAGERcan’t call a method for an event handler or cleanup routine, so we will trick it. Instead write a simple two line event handler which pulls out the UVALUE of the top-level base (which is our object widget reference) and calls the real event handler method of the object. This will mean that our object widget will only have a single event handler that handles all events (but possibly dispatches events to other routines). The same must be done for the cleanup routine.
Once you’ve done this, it’s fairly easy to modify the structure a bit for your own purposes.
Code changes
Here’s what I had to do to make mg_tilejp2 into an object widget:
- Change the names of the routines from
mg_tilejp2_refreshtomgtilejp2::refresh.mgtilejp2becomesmgtilejp2::init. Make sure to change it to a function and return 1. - Add
mgtilejp2__definethat simply names the class, inherits fromMGObjectWidget, and creates the member variables with the same name and type as the fields of the old state variable. - Convert creating
pstateand putting it into the tlb’s UVALUE into putting self into the tlb’s UVALUE. Get rid ofwidget_control, event.top, get_uvalue=pstatein event handler. - Fix up passing around
pstatesince it’s no longer needed. Fix(*pstate).owindowtoself.owindow. - Added a
cleanupWidgetsmethod, fixed upcleanupmethod, andmg_tilejp2_eventtomgtilejp2::handleEvents.
See previous post about tiling JPEG 2000 images with IDLgrImage.

June 15th, 2006 at 8:53 am
Another advantage to this approach: the class MGOBJECTWIDGET provides a convenient template for other class-based widget programs.