FXRuby simple FXTextField question


(Matthew Miller) #1

Hello,

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.

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.

I hope that someone can help me get over this hump!

TIA, Matthew

class LunchWindow < FXMainWindow

def initialize(app)
# Initialize base class
super(app, “Lunch Counter”, nil, nil, DECOR_ALL, 20, 20, 700, 460)
@ssnValue = FXDataTarget.new("")
FXLabel.new(nextFrame, “&SSN”, nil,
LAYOUT_CENTER_Y|LAYOUT_CENTER_X|JUSTIFY_LEFT|LAYOUT_FILL_ROW)

@ssnInput = FXTextField.new(nextFrame, 13, @ssnValue, FXDataTarget::ID_VALUE,
		  LAYOUT_CENTER_Y|LAYOUT_CENTER_X|FRAME_SUNKEN|FRAME_THICK|
		  LAYOUT_FILL_ROW|TEXTFIELD_ENTER_ONLY)
			    
# call onSsnInput when <return> is pressed.
@ssnInput.connect(SEL_COMMAND, method(:onSsnInput))
			      
FXLabel.new(nextFrame, "Entered SSN", nil,
       LAYOUT_CENTER_Y|LAYOUT_CENTER_X|JUSTIFY_LEFT|LAYOUT_FILL_ROW)
								       
# Test field to send output to.
@echoValue = FXDataTarget.new("");
@echoField = FXTextField.new(nextFrame, 13, @echoValue, FXDataTarget::ID_VALUE,
	         LAYOUT_CENTER_Y|LAYOUT_CENTER_X|FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_ROW)

end

SSN input field callback.

def onSsnInput( sender, sel, ptr )

@echoField.text = @ssnInput.text
return 1

end


(Lyle Johnson) #2

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 :wink:

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

inputs.rb (1.97 KB)