[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

File Selection Compound Widget Object



Hi Folks,

Last week I offered, in COYOTE_FIELD, a traditional compound widget.
I had a number of requests to turn that program into a compound
widget object. Indeed, there are some very good reasons to do so,
and I will certainly get around to it sooner or later.

But in the interest of programming enlightenment and to prove
(if anyone needs proof) that I don't to *everything* I'm told
to do, I decided to write a similar compound widget as an
object. This is a file selection compound widget named
CW_FILESELECT. It can be found here:

   http://www.dfanning.com/programs/cw_fileselect.pro

I decided to program a different example because I wanted
an example that *anyone* could see was a hell of a lot 
better written as an object than it could ever be written
as a traditional compound widget.

But, because I don't want to spring a completely new
programming paradigm on the ... I was going to say
"old fogies", but I think a fairly convincing argument
can be made that the author of the program in question 
falls into this category, so I'll say "traditional
programmers" instead. 

Because I don't want to spring a completely new programming 
paradigm on the traditional programmers, this program works
like a traditional compound widget program. That is to say,
you can call it like this:

  fileID = CW_FileSelect(tlb, Filename='cyclone.dat')

And then you can get and set its "value" in the 
traditional way:

   Widget_Control, fileID, Get_Value=theFilename
   Print, theFilename
   Widget_Control, fileID, Set_Value='C:\Pokemon\davey.dat'

But, if that were all it could do, I wouldn't be wasting
your time here. :-)

The heart of the program is an object of class FSC_FILESELECT.
You can obtain an object reference to this object class and
use (or create) all its methods to change various and sundry 
properties of the compound widget.

For example, if you want to resize the widgets, you can do 
so with methods. If you want to make sure the file name and 
directory names are valid, you can do so with methods. You 
can obtain the object reference by using the OBJECTREF 
keyword, like this:

   fileID = CW_FileSelect(tlb, Filename='cyclone.dat', $
      ObjectRef=theObject)
   theObject->SetProperty, XSize=100

(I would argue that it makes a LOT more sense to return the
object reference as the result of the function since
you will want to call object methods whenever possible, 
but--as I say--I don't want to upset anyone, so I've 
gone with this syntax.)

I've written a number of methods that will give you a foot
up on writing your own useful methods. In fact, I've 
structured the code in the file in a way that should make
it easy for you to add your own subclassed objects into
the standard compound widget structure. I think it will
be obvious how this can be done, but if not, my forthcoming
book will probably spell it out in excruciating detail. :-)

I have put rudimentary file and directory "inspection"
methods into the program to check the accuracy of
file and directory names. At the moment I don't do
much more than eliminate any leading or trailing
blank characters. I also make sure you can't use
any variation of "IDLSUCKS" in the filename. :-)

This is not the be-all and end-all of file selection
widgets. It's just an example of the kinds of programs
you *could* be writing. Alas, I've resigned myself to
being the kind of programmer who can come up with one
or two good ideas a year that I can understand well
enough to explain to someone else how they can write 
a MUCH better program later. So have at it. 

I've included an Example program at the end of the
code so you can exercise the CW_FILESELECT widget
a little bit. To run the Example program, download
the CW_FILESELECT program from the link above and
type this:

   IDL> .Compile cw_fileselect.pro
   IDL> Example

Cheers,

David
-- 
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155