Re: IDLitComponent - Introspection [message #44037 is a reply to message #44015] |
Wed, 11 May 2005 06:39   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Robert Barnett writes:
> I've been trying to write my own classes which incorporate IDLitComponent and widget_propertysheet. This allows the user to tinker with object properties via a GUI,
>
> I'm very confused by IDLitComponent::GetPropertyByIdentifier. The method allows you to do a "ClassName::GetProperty, Name=Value",
> however "Name" is only known at runtime.
>
> For example
>
> obj -> GetPropertyByIdentifier("BoringProperty", empty_variable)
>
> somehow triggers
>
> obj -> GetProperty, BORINGPROPERTY=empty_variable
>
> This is something that I would very much like to do with my own classes without having to inherit IDLitComponent.
>
> To be more precise, does anyone know how IDLitComponent::GetPropertyByIdentifier tricks the arg_present()
> function into thinking that a keyword needs to be set?
Here is a quick and dirty explanation of how this can be done.
The actual code is a *bit* more complicated then this, but not
much more complicated. In any case, this will be enough to get
you started.
The main trick is to define an output variable on the *__DEFINE
procedure so you can get the class structure easily. All depends
on that. :-)
;*********************************************************** ****
PRO Object_Trick::GetProperty, _Ref_Extra=extra
Object_Trick__Define, class
properties = Tag_Names(class)
keyword = (extra)[0]
index = Where(properties EQ keyword, count)
IF count GT 0 THEN BEGIN
(Scope_Varfetch(keyword, /Ref_Extra)) = self.(index)
ENDIF
END
FUNCTION Object_Trick::INIT
self.thick = 2
self.linestyle = 4
RETURN, 1
END
PRO Object_Trick__Define, class
class = { Object_Trick, $
THICK:0, $
LINESTYLE:0 }
END
;*********************************************************** ****
Then,
IDL> obj = Obj_New('Object_Trick')
IDL> obj -> GetProperty, Linestyle=linestyle & Print, linestyle
4
IDL> obj -> GetProperty, Thick=thick & Print, thick
2
I'll leave it as an exercise for the reader to write the generic
SetProperty method. :-)
Cheers,
David
(Hint: Google the IDL archives for SCOPE_VARFETCH.)
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|