★ Object widgets
posted Wed 14 Jun 2006 by Michael Galloy under IDL, Object graphics, Objects, WidgetsA 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 , and mg_object_cleanup.pro.

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 theUVALUEof 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 theUVALUEof 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 thetlbâsUVALUEinto puttingselfinto thetlbâs UVALUE. Get rid of
widget_control, event.top, get_uvalue=pstate
in the 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.
UPDATED 10/25/13: Updated links of library source code.

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.
December 9th, 2008 at 6:39 pm
Interesting approach. I’m hoping to apply this approach to create object oriented compound widgets, as these constitute a case where modularity is already very important.
December 9th, 2008 at 8:32 pm
Yes, I’ve found this very useful for compound widgets. You can only do so much with WIDGET_CONTROL with a compound widget, but if the “value” of the compound widget is an object, you can have arbitrary control over it.
October 25th, 2013 at 11:23 am
I was checking out your examples, but some of the documents seemed to have vanished. Could they be reposted?
October 25th, 2013 at 4:21 pm
I updated the links to point to my GitHub repo.