require 'foxtails/FTTargeted'

module FoxTails

class FTTextField < Fox::FXTextField
  include FTStateTargeted

  # Overrides the class's display method, if assigned.
  attr_accessor :display_format

  # Update the target value during editing or only when done?
  # The default is update when editing done (when field loses focus).
  attr_accessor :dynamic

  def target_arg_range; 2..3;  end

  def connect_to_target
    connect(SEL_FOCUSOUT) do
      set_target_state(get_value()) unless @dynamic
    end

    connect(SEL_CHANGED) do
      set_target_state(get_value()) if @dynamic
    end

    when_target_state CHANGES do |value|
      str = @display_format ?
        @display_format % value :
        display(value)
      setText(str)
    end
  end

  def get_value
    getText()
  end

  def display(value)
    value.to_s
  end

end

FTStringField = FTTextField

## it would be nice to validate input as it is typed...

class FTIntegerField < FTTextField
  def get_value
    super.to_i
  end
  def display(value)
    "%i" % value
  end
end

class FTFloatField < FTTextField
  def get_value
    super.to_f
  end
  def display(value)
    "%.2f" % value
  end
end

## FTIntegerArrayField, ...

end