[fxruby-users] Data Targets and message handlers [was: Default double-click in FXTable?]

Joel VanderWerf vjoel at path.berkeley.edu
Tue Oct 10 18:15:30 EDT 2006


Philippe Lang wrote:
> This makes FXDataTarget objects pretty useless under FXRuby...

Have you looked at FoxTails/Observable? FoxTails subclasses some of the 
Fox/FXRuby classes and defines an initialize method that (typically) 
takes an object and an attribute name instead of the usual target 
arguments. The attribute name is used to keep that attr in sync when the 
widget is used.

FoxTails uses Fox's #connect mechanism for this functionality 
(typically, using SEL_COMMAND, SEL_FOCUSOUT, etc) rather than data 
targets. This means you can still use connect (with other messages) for 
other purposes. You can also use the #when_foo methods to register 
blocks of code to execute when the attr changes or matches a pattern.

Here's a simple combobox example that lets you select an object (in 
terms of its string representation using #inspect) and then changes a 
label to display the class of the object.

#!/usr/bin/env ruby

require 'foxtails'

include Fox
include FoxTails

class ComboWindow < FXMainWindow
   observable :items, :selected

   def initialize(*args)
     super

     self.items = [:foo, "Bar", String, 3, ["a", "b"], 1.23]
     self.selected = items[3]

     FXStatusBar.new(self,
        LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X|STATUSBAR_WITH_DRAGCORNER)

     mb = FXMenuBar.new(self, LAYOUT_SIDE_TOP|LAYOUT_FILL_X)

     file_menu = FXMenuPane.new(self)
     FXMenuTitle.new(mb, "&File", nil, file_menu)

     FXMenuCommand.new(file_menu, "&Quit\tCtl-Q\tQuit the application.").
       connect(SEL_COMMAND) { getApp().exit }

     combo = FTComboBox.new(self, 20, self, :items, :selected,
                    COMBOBOX_STATIC|FRAME_THICK)
     def combo.display(item); item.inspect; end

     label = FXLabel.new(self, "")

     when_selected CHANGES do
       label.setText("Class of item is #{selected.class}")
     end

   end

   def create
     super
     show
   end
end

class ComboApp < FTApp
   def initialize
     super("Combo", "TEST")
     FoxTails.get_standard_icons
     ComboWindow.new(self, "Combo")
   end
end

ComboApp.new.run


-- 
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


More information about the fxruby-users mailing list