[fxruby-users] Selection within a TextField

Patrick Fernie patrick.fernie at gmail.com
Wed Jun 15 16:03:35 EDT 2005

Hi list,

I've encountered a strange issue when trying to create a type-ahead
TextField (a textfield where the text will be autocompleted if it
matches some value in an array). Specifically, I am trying to create a
system where the "guessed at" part is automatically selected, so the
next character the user types overwrites it. i.e.:
I have this array of values: ["able", "apartment", "apple"]

The user wants to input "apple", so he types "a", and the textfield is
populated with "able", with the "ble" part selected. Now, when he
presses "p", the "ble" is automatically deleted (since it is
selected), and the textfield become "apartment", with "artment"
selected. Finally, when the user types p again, the textfield will
become "apple" with "le" selected. Hopefully that explains the intent.

My problem is this: the first time I autocomplete, the selection works
beautifully, but when the user types a character with something
selected, autocompletion works again, but the text isn't selected.
Furthermore, if the user were to hold down shift and hit left, the
selection appears (i.e. it will be going from the end of the text he
actually typed up to but not including the last character). So, it's
as if the text is "sort of" selected, but the problem is that if the
user types at this point, the text is just appended to the end.

This sounds very complicated, so here's another attempt to explain.
Consider text between < and > to be "selected".
User presses "a" -> a<ble>, presses "p" -> apartment (no selection),
presses "p" again -> apartmentp. If there is nothing selected when the
user presses a character, it will select properly.

Is there a different message I need to be responding to in the case of
"typing over" selected text? A further complication to the situation
is that I'm doing this w/ the TextField in a ComboBox. So, I set up a
map for the SEL_CHANGED event within the ComboBox. Code is below. The
TypeAheadCombo takes as its third parameter an array of autocomplete

Hopefully, people see what I'm getting at and what the problem is.


class TypeAheadCombo < FXComboBox
  include Responder

  def initialize(parent, nc, fields, *args)
    super(parent, nc, *args)

    @fields = fields
    @prev_prefix = ""


  def create

  def init_events

  def on_changed(sender, sel, data)
    prefix = data[0...sender.cursorPos]
    old_length = @prev_prefix.length
	  @prev_prefix = prefix
    re = Regexp.new(data)
    txt = @fields.detect { |val| (val =~ re) == 0 }
    if txt and (prefix.length > old_length) and (sender.cursorPos ==
      sender.text = txt
      sender.setSelection(sender.cursorPos, sender.text.length)

==Would be created thusly:==
TypeAheadCombo.new(parent, numColumns, ["able", "apartment", "apple"])

More information about the fxruby-users mailing list