★ 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
state
orpstate
. - The
init
method is the widget creation part of the program. One important item: placeself
in theUVALUE
of the top-level base. This will make our scheme for event handling work. XMANAGER
can’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 theUVALUE
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_refresh
tomgtilejp2::refresh
.mgtilejp2
becomesmgtilejp2::init
. Make sure to change it to a function and return 1. - Add
MGTILEJP2__DEFINE
that 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
pstate
and putting it into thetlb
’sUVALUE
into puttingself
into thetlb
’s UVALUE. Get rid of
widget_control, event.top, get_uvalue=pstate
in the event handler.
- Fix up passing around
pstate
since it’s no longer needed. Fix(*pstate).owindow
toself.owindow
. - Added a
cleanupWidgets
method, fixed upcleanup
method, andMG_TILEJP2_EVENT
tomgtilejp2::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.