class PPCurses::InputElement

Attributes

cursor_location[RW]

Stores the X location of the cursor relative to the value being displayed. If the cursor is in the middle of the string then subsequent keys will be added from this location, etc.

filter[RW]
label[RW]
selected[RW]
size[RW]
value[RW]
value_start_point[RW]

Public Class Methods

new(label, size ) click to toggle source
# File lib/ppcurses/form/input_element.rb, line 22
def initialize(label, size )
  @label = label
  @size = size
  @selected = false
  @filter = nil
  self.clear
end
new_decimal_only( label, size) click to toggle source

Creates an InputElement that only allows a number but allows a decimal point. I.E. 10.2

# File lib/ppcurses/form/input_element.rb, line 41
def InputElement.new_decimal_only( label, size)
  i_only = PPCurses::InputElement.new(label, size)
  i_only.filter = PPCurses::DecimalFilter.new
  i_only
end
new_integer_only( label, size) click to toggle source

Creates an InputElement that only allows integer input

# File lib/ppcurses/form/input_element.rb, line 32
def InputElement.new_integer_only( label, size)
  i_only = PPCurses::InputElement.new(label, size)
  i_only.filter = PPCurses::IntegerFilter.new
  i_only
end
new_time_only( label, size) click to toggle source

Creates an InputElement that only allows time data E.G. 20:10.20 == 20 hours, 10 minutes and 20 seconds

# File lib/ppcurses/form/input_element.rb, line 50
def InputElement.new_time_only( label, size)
  i_only = PPCurses::InputElement.new(label, size)
  i_only.filter = PPCurses::TimeFilter.new
  i_only
end

Public Instance Methods

clear() click to toggle source
# File lib/ppcurses/form/input_element.rb, line 135
def clear
  @value = ''
  @cursor_location = 0
end
height() click to toggle source
# File lib/ppcurses/form/input_element.rb, line 131
def height
  1
end
key_down( key ) click to toggle source
# File lib/ppcurses/form/input_element.rb, line 66
def key_down( key )
  if key == DELETE
    handle_delete
    return
  end

  if key == KEY_LEFT
    @cursor_location -= 1 unless @cursor_location == 0
    return
  end

  if key == KEY_RIGHT
    @cursor_location += 1 unless @cursor_location == @value.length
    return
  end


  # Ignore control characters
  if key.is_a?(Fixnum)
    return
  end

  # Adding new characters to the string

  # Check size of string before adding another character
  if @value.length >= @size
    # Ignore input
    return
  end

  #
  # Get a temporary version of the current value
  # with the new character added so that we can
  # test it against the filter.
  temp_val = value_with(key)

  unless passes_filter(temp_val)
    return
  end

  #
  # Actually add the new character if the filter passes
  #
  add_character(key)

end
passes_filter( key ) click to toggle source
# File lib/ppcurses/form/input_element.rb, line 114
def passes_filter( key )
  if @filter.nil?
    return true
  end

  @filter.passes_filter( key )
end
set_curs_pos(screen) click to toggle source
# File lib/ppcurses/form/input_element.rb, line 123
def set_curs_pos(screen)
  Curses.curs_set(VISIBLE)
  x =  @value_start_point.x  + @cursor_location

  screen.setpos( @value_start_point.y, x )
end
show(screen) click to toggle source
# File lib/ppcurses/form/input_element.rb, line 56
def show(screen)
  print_label( screen )

  @value_start_point = screen.cur_point

  print_value( screen )
end

Protected Instance Methods

add_character( char ) click to toggle source
# File lib/ppcurses/form/input_element.rb, line 213
def add_character ( char )    
  @value = value_with(char)
  @cursor_location += 1
end
handle_delete() click to toggle source
# File lib/ppcurses/form/input_element.rb, line 160
def handle_delete
  # Cursor is at the front of the string, nothing in
  # front of it to delete, or there is nothing to delete
  if @cursor_location == 0 or @value.length == 0
    return
  end

  if @value.length == 1
    @value = ''
    @cursor_location = 0
    return
  end

  # Cursor is at the end of the string, remove the last character
  if @cursor_location == @value.length
    @value = @value.slice(0..@cursor_location-2)
  elsif @cursor_location == 1
    # cursor is right after the first character
    @value = @value.slice(1..@value.length-1)
  else
    # Cursor is in the middle of the string, remove the character at the cursor location
    # Example:
    #
    # abcdefg
    # 1234567
    #   ^ -> cursor-location = 3
    #
    @value = @value.slice(0..@cursor_location-2) + @value.slice(@cursor_location..@value.length-1)
  end

  @cursor_location -= 1
end
print_label( screen ) click to toggle source

print_value( screen ) click to toggle source
value_with(char) click to toggle source

Returns what the value of the input element would be, if the given character was added. Does not modify the internal state.

# File lib/ppcurses/form/input_element.rb, line 200
def value_with (char)
  temp_val = @value

  if @cursor_location == @value.length
    temp_val = @value + char
  else
    temp_val = @value.slice(0..@cursor_location-1) + char + @value.slice(@cursor_location..@value.length-1)
  end
  
  temp_val
end