From: Joel VanderWerf Date: 2003-07-04T06:17:24+09:00 Subject: Re: FXDataTarget not updating my variable Hans Fugal wrote: > Lyle Johnson wrote: ... >>If this is too restrictive for your application, you might want to check >>out some work that Joel VanderWerf has done with his FoxTails extension >>for FXRuby (http://raa.ruby-lang.org/list.rhtml?name=foxtails). It takes >>advantage of the "observable" module (another of Joel's creations) to >>allow you to connect GUI components to observable attributes. This is a >>potentially much more powerful approach and it may offer the kind of >>solution you're looking for. > > Looks very interesting. Indeed this is exactly the sort of thing we are > doing. Hans, here's how I'd write your original example using FoxTails: ========================== #!/usr/bin/env ruby require 'fox' require 'foxtails' include Fox include FoxTails class DataTargetWindow < FXMainWindow include Observable observable :foo def initialize(app) super(app, "Data Targets Test", nil, nil, DECOR_ALL, 20, 20, 200, 100) self.foo = "foo" # NOTE: "@foo = ..." will not trigger updates vframe = FXVerticalFrame.new(self) frame = FXHorizontalFrame.new(vframe) l = FXLabel.new(frame,"B") tf = FTTextField.new(frame,10,self,:foo) tf.dynamic = true # assuming you want foo updated while you are editing when_foo CHANGES do l.text = foo end FXButton.new(vframe,"puts").connect(SEL_COMMAND) do puts foo end show end end if __FILE__ == $0 application = FXApp.new("DataTarget", "FoxTest") window = DataTargetWindow.new(application) application.create application.run end ========================== If you don't want the label to change while you are editing, just remove the "tf.dynamic = true". Note that "when_foo" is not limited to the initialize method. You can set up observers from other methods, or even other objects: window = DataTargetWindow.new(application) window.when_foo CHANGES do |new_foo| ... end Also, CHANGES is just a synonym for Object. You can use anything with an #=== method, including regexes. The match is attempted whenever the attr writer is called. It would be possible to define a class FTLabel which lets you "tie" the label to a given observable attr, much like FTTextField works. However, it's easy enough to achieve the same effect with a "when" clause and the #text= accessor. Also, one principle of foxtails is that the initialize method of FXFooBar and FTFooBar are the same except for replacing the "tgt,sel" with an object (or a proc returning an object) and the name of an observable attr in that object. FXLabel doesn't have tgt,sel in its initialize, so there's no parallel over in foxtails land. Anyway, dynamically changing labels are kind of unusual. Let me know if you have questions with FoxTails...