Re: Using Snit options
- From: JHJL <jhjl@xxxxxxxxxxxxxx>
- Date: Wed, 11 Nov 2009 07:12:14 -0800 (PST)
On Nov 11, 2:16 pm, Will Duquette <w...@xxxxxxxxxxxxxx> wrote:
On Nov 11, 3:25 am, JHJL <j...@xxxxxxxxxxxxxx> wrote:
Hi
I am doing my first experiments with Snit and was definately enjoying
the experience until I got stuck trying to use options with a -
configuremethod...
In my constructor I want to use an option, say -radius, to initialise
the size of a circle. I then calculate the x,y coordinates based on
the value $options(-radius). Fine.
But I also need to recalculate if I configure -radius to another
value, so I set a -configuremethod callback on the -radius option.
However this then causes the constructor to fail (invalid command name
"") becuase, I assume, $self configurelist $args fires the callbacks
snit::type Circle {
option -radius -default 1.0 -configuremethod Recalc
option -x -default 0.0 -configuremethod Recalc
option -y -default 0.0 -configuremethod Recalc
typevariable count 0
component x
component y
constructor {args} {
$self configurelist $args
install x using rbc::vector create circle_${count}_X
install y using rbc::vector create circle_${count}_Y
incr count
# fill x,y vectors here using circle algorithm based on -
radius -x -y option values
}
method Recalc {option value} {
set options($option) $value
# clear x,y vectors
# fill x,y vectors here using circle algorithm based on -
radius -x -y option values
}
}
#############
The above is just pseudo code (I cannot access my actual development
code due to security restrictions) so forgive any typos.
I want to be able to configure any combination of the options at
consruction time and during the instances lifetime but in the case of
multiple options I want to do the recreation only once, not upto 3
times:
e.g
Circle c -radius 100 -x 50 -y 50 ;# create circle once using
constructor
c -radius 50 -x 20 ;# recalc circle once (not twice)
I could use set methods to do this but I wanted to use the Tk option
style...
Is there an obvoius way of doing this that I am missing? Am I going
about this in the wrong way?
As always, any help gratefully received
kind regards
Julian H J Loaring
Julian,
I see two issues here. First, there's an issue with using -
configuremethod: you need to write it to work properly post-
construction, which means that making it work during the constructor
can be tricky. Sometimes you can handle this by moving the "$self
configurelist $args" line further down into the constructor; other
times you can bypass it for particular options by using the "from"
command (documented in the Snit man page) to extract the options from
$args and handle them explicitly, so that "$self configurelist $args"
doesn't even see them in the constructor.
Second, you've got the problem that you have two options that will
almost always change together, and you'd like to do any needed update
only once. Note that this isn't really a problem with options; you'd
have the same trouble if you defined "setx" and "sety" methods. Here,
as I see it, are your choices:
1. Define a single -xy option whose value is a pair of numbers. Then
they always change together, and there's no problem.
2. Don't Recalc in the -configuremethod. Instead, set a flag that
indicates that a Recalc needs to be done. There are two sub-options
here, depending on what the results of the Recalc will be used for.
a. In each routine that uses the results of Recalc, begin by calling
Recalc if the flag is set.
b. Call Recalc in an "after idle" callback. Only schedule the
callback on setting the flag the first time.
Note that option 2 will let you change both values as often as you
want without recalculating any more than necessary.
Hope this helps!
Will Duquette- Hide quoted text -
- Show quoted text -
Thanks Will,
These are the very options I have been exploring this afternoon
although I had missed the "from" command and will check this out now:)
I suppose what I was looking for was a way of chaining in a proc to be
called after configure has done its job...
I must congratulate you on producing Snit - it really simplifies
adapting widgets and creating reusable types; it has definately got my
creative juices flowing today (hampered only by my forgetting that
Tkcon hides the error stack trace until you click the link - makes
debugging very confusing!)
Many thanks
Julian
.
- Follow-Ups:
- Re: Using Snit options
- From: Will Duquette
- Re: Using Snit options
- References:
- Using Snit options
- From: JHJL
- Re: Using Snit options
- From: Will Duquette
- Using Snit options
- Prev by Date: Re: tcl ssh extension library?
- Next by Date: Re: O_DIRECT and tcl
- Previous by thread: Re: Using Snit options
- Next by thread: Re: Using Snit options
- Index(es):
Relevant Pages
|