From: Lyle Johnson Date: 2002-06-11T23:40:01+09:00 Subject: Re: FXRuby simple FXTextField question This is a multi-part message in MIME format. --------------030206070506030104060900 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit > I've been learning to use FXRuby and it seems to be a to generate GUI apps. > I'm having a small problem with an example I'm working on. What I want is a > FXTextField that when I hit its contents are then displayed in > another textfield. The first part is easy using the TEXTFIELD_ENTER_ONLY > flag on the first text field. The trouble that I'm having is when > is pressed the text is only displayed in the second textfield for a short > time and the field then blanks itself. One problem I see is that for the @ssnInput text field, you start out by assigning it one target (a data target) and then immediately replace that with a different target (when you call FXTextField#connect). Unlike perhaps some other toolkits, FOX objects only have one message target at a time. So as your code is currently written, the @ssnValue data target is effectively unused. A workaround for this problem is to "chain" the messages by giving the data target its own message target. So replace this line in your code: @ssnInput.connect(SEL_COMMAND, method(:onSsnInput)) with this: @ssnValue.connect(SEL_COMMAND, method(:onSsnInput)) In other words, the text field continues to talk to the data target as its message target; but now, the data target sends messages to a different object when its contents change. The next problem (the disappearing text in the second text field) has to do with the question of how that text field (@echoField) gets its contents. Since it's hooked up to a data target (@echoValue) you want to work with the contents of that data target instead of setting the text field's contents directly. What's currently happening in your program is that: 1. onSsnInput() sets @echoField's contents to the current value of @ssnInput. 2. Almost immediately after this, a GUI update occurs and @echoField asks its data target (@echoValue) for an update. 3. @echoValue is still an empty string at this point and so that's what gets set into @echoField. So a better fix is to change @echoValue in onSsnInput(), e.g. def onSsnInput(sender, sel, ptr) @echoValue.value = @ssnValue.value return 1 end I think this will probably give you the result you were looking for. If not, let me know and we'll see what to do next ;) > Below is the relevent snippit of code that I'm using. I have Ruby 1.6.7, > FXRuby 1.0.10, and fox 1.0.11 installed. Also, the inputs.rb example > supplied with FXRuby doesn't work correctly; you only see the output text of > the command when you left-click and drag the mouse over the output window. Well, now, that's kinda weird. I'm seeing it too (obviously I haven't run this example lately!) Attached is an updated version that should work properly. Hope this helps, Lyle --------------030206070506030104060900 Content-Type: text/plain; name="inputs.rb" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="inputs.rb" #!/usr/bin/env ruby require 'fox' include Fox class InputHandlerWindow < FXMainWindow def initialize(app) # Initialize base class first super(app, "Input Handlers Test", nil, nil, DECOR_ALL, 0, 0, 400, 300) # Text area plus a button commands = FXHorizontalFrame.new(self, LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X) FXLabel.new(commands, "Command:") @cmdInput = FXTextField.new(commands, 30, nil, 0, FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_X) @cmdInput.connect(SEL_COMMAND, method(:onCmdText)) FXHorizontalSeparator.new(self, LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X) textFrame = FXVerticalFrame.new(self, FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_X|LAYOUT_FILL_Y) # Output will be displayed in a multiline text area @cmdOutput = FXText.new(textFrame, nil, 0, LAYOUT_FILL_X|LAYOUT_FILL_Y) # Initialize the pipe @pipe = nil end # Create and show the main window def create super show(PLACEMENT_SCREEN) end # Remove previous input (if any) def closePipe if @pipe getApp().removeInput(@pipe, INPUT_READ|INPUT_EXCEPT) @pipe = nil end end def onCmdText(sender, sel, ptr) # Stop previous command closePipe # Clean up the output window @cmdOutput.text = "" # Open a new pipe @pipe = IO.popen(@cmdInput.text) # Register input callbacks and return getApp().addInput(@pipe, INPUT_READ|INPUT_EXCEPT) { |sender, sel, ptr| case SELTYPE(sel) when SEL_IO_READ text = @pipe.read if text @cmdOutput.appendText(text) else closePipe end when SEL_IO_EXCEPT puts 'onPipeExcept' end } return 1 end end if $0 == __FILE__ # Construct an application application = FXApp.new('InputHandler', 'FoxTest') # Open the display application.init(ARGV) # Construct the main window InputHandlerWindow.new(application) # Create and show the application windows application.create # Run the application application.run end --------------030206070506030104060900--