Widget Programs as Objects

Most widget programs related to sm_gui are written as objects now. In my opinion, that makes the programming more elegant, and it definitely simplifies communication between objects.

Unfortunately, IDL isn't set up to handle widgets as objects natively (which would be the most elegant way), so some workarounds are necessary.

Make sure you understand objects in IDL in general (it is not necessary to understand object graphics), in particular automatic structure definition via __define, INIT and CLEANUP methods and general object function and procedure methods.

To write a widget program called ``dummy_widget'' (this will be the class name) as object, you need the following:

The object class definition
which will be the procedure dummy_widget__define.pro (note the two underscores!):

        PRO dummy_widget__define

            struct = { DUMMY_WIDGET, $
                        varA: 0L, $
                        varB: ptr_new(), $
                        varC: obj_new(), $
                        varD: '', $
                        varE: fltarr(20) }

        END

This procedure defines a named structure (the structure name being the class name) which holds all the internal variables of the object. The variable name (struct) has no meaning.

Variables are only set up here with their type and not filled with values yet.

An INIT function method
which does the initialization of the object. For widget objects, this corresponds to the widget definition procedure, where you set up your widgets.

Also, fill your object's (widget's) private variables with their initial values here. Note that you can refer to the structure defined above as self.

Note that an object's INIT method must be a function which returns 1 on success and zero on failure.

        FUNCTION dummy_widget::INIT, par1, par2, keyw1=keyw2

            catch, error
            IF error NE 0 THEN BEGIN
                catch, /cancel
                ...
                ;; return 0 on failure!
                return, 0
            ENDIF

            ;; set up all widgets
            tlb_id = widget_base(...)

            ;; initialize all private variables
            self.varA = 5L
            self.varB = ptr_new(/allocate_heap)
            ...
            widget_control, tlb_id, /realize
            xmanager, ...

            ;; return 1 on success
            return, 1

        END

A CLEANUP procedure method
which cleans up the garbage. Also, make sure the widget is destroyed.

        PRO dummy_widget::CLEANUP

            ptr_free, self.varB
            ...
            IF widget_info(self.tlb_id, /valid_id) THEN $
                widget_control, self.tlb_id, /destroy

        END

MUCH MORE TO FOLLOW



Subsections
Holger Fleckenstein 2008-07-08