Class: RubyCurses::Field

Inherits:
Widget
  • Object
show all
Defined in:
lib/rbcurse/rwidget.rb

Overview

Text edit field To get value use getvalue() TODO - test text_variable

Direct Known Subclasses

ComboBox

Constant Summary

Constants included from Io

Io::ERROR_COLOR_PAIR, Io::FOOTER_COLOR_PAIR, Io::LINEONE, Io::MAIN_WINDOW_COLOR_PAIR

Instance Attribute Summary collapse

Attributes inherited from Widget

#col_offset, #cols_panned, #config, #curpos, #ext_col_offset, #ext_row_offset, #id, #parent_component, #row_offset, #rows_panned, #should_create_buffer, #state

Instance Method Summary collapse

Methods inherited from Widget

#OLDbind_key, #buffer_to_screen, #buffer_to_window, #create_buffer, #destroy, #destroy_buffer, #focus, #get_buffer, #get_color, #get_preferred_size, #getvalue_for_paint, #height, #height=, #hide, #is_double_buffered?, #move, #override_graphic, #printstring, #process_key, #remove, #repaint_all, #repaint_required, #rowcol, #safe_create_buffer, #set_buffer_modified, #set_buffering, #set_form, #set_form_col, #set_form_row, #set_modified, #setformrowcol, #setrowcol, #show, #unbind_key, #width, #width=

Methods included from Io

#askchoice, #askyesno, #askyesnocancel, #clear_error, #clear_this, #get_string, #newaskyesno, #old_print_header, #old_print_top_right, #print_action, #print_error, #print_footer_help, #print_header, #print_headers, #print_help, #print_help_page, #print_in_middle, #print_key_labels, #print_key_labels_row, #print_screen_labels, #print_status, #print_this, #print_top_right, #rbgetstr

Methods included from Utils

#_process_key, #bind_key, #clean_string!, #get_color, #keycode_tos, #repeatm, #wrap_text

Methods included from ConfigSetup

#cget, #config_setup, #configure, #variable_set

Methods included from EventHandler

#bind, #fire_handler, #fire_property_change

Methods included from DSL

#OLD_method_missing

Constructor Details

#initialize(form, config = {}, &block) ⇒ Field

Returns a new instance of Field.



1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
# File 'lib/rbcurse/rwidget.rb', line 1693

def initialize form, config={}, &block
  @form = form
  @buffer = String.new
  #@type=config.fetch("type", :varchar)
  @display_length = config.fetch("display_length", 20)
  @maxlen=config.fetch("maxlen", @display_length) 
  @row = config.fetch("row", 0)
  @col = config.fetch("col", 0)
  @bgcolor = config.fetch("bgcolor", $def_bg_color)
  @color = config.fetch("color", $def_fg_color)
  @name = config.fetch("name", nil)
  @editable = config.fetch("editable", true)
  @focusable = config.fetch("focusable", true)
  @handler = {}
  @event_args = {}             # arguments passed at time of binding, to use when firing event
  init_vars
  super
end

Instance Attribute Details

#bufferObject (readonly)

actual buffer being used for storage



1669
1670
1671
# File 'lib/rbcurse/rwidget.rb', line 1669

def buffer
  @buffer
end

#datatypeObject

attr_reader :curpos # cursor position in buffer current, in WIDGET



1689
1690
1691
# File 'lib/rbcurse/rwidget.rb', line 1689

def datatype
  @datatype
end

#formObject (readonly)

Returns the value of attribute form.



1685
1686
1687
# File 'lib/rbcurse/rwidget.rb', line 1685

def form
  @form
end

#handlerObject (readonly)

event handler



1686
1687
1688
# File 'lib/rbcurse/rwidget.rb', line 1686

def handler
  @handler
end

#original_valueObject (readonly)

value on entering field



1690
1691
1692
# File 'lib/rbcurse/rwidget.rb', line 1690

def original_value
  @original_value
end

#overwrite_modeObject

true or false INSERT OVERWRITE MODE



1691
1692
1693
# File 'lib/rbcurse/rwidget.rb', line 1691

def overwrite_mode
  @overwrite_mode
end

#type(dtype) ⇒ Object (readonly)

define a datatype, currently only influences chars allowed integer and float. what about allowing a minus sign? XXX



1687
1688
1689
# File 'lib/rbcurse/rwidget.rb', line 1687

def type
  @type
end

Instance Method Details

#addcol(num) ⇒ Object

add a column to cursor position. Field



1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
# File 'lib/rbcurse/rwidget.rb', line 1955

def addcol num
  if num < 0
    if @form.col <= @col + @col_offset
     # $log.debug " error trying to cursor back #{@form.col}"
      return -1
    end
  elsif num > 0
    if @form.col >= @col + @col_offset + @display_length
  #    $log.debug " error trying to cursor forward #{@form.col}"
      return -1
    end
  end
  @form.addcol num
end

#cursor_backwardObject

$log.debug “ crusor FORWARD cp:#@curpos pcol:#@pcol b.l:#RubyCurses::Field.@[email protected] d_l:#@display_length fc:#RubyCurses::Field.@[email protected]



1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
# File 'lib/rbcurse/rwidget.rb', line 1922

def cursor_backward
  if @curpos > 0
    @curpos -= 1
    if @pcol > 0 and @form.col == @col + @col_offset
      @pcol -= 1
    end
    addcol -1
  elsif @pcol > 0 #  added 2008-11-26 23:05 
    @pcol -= 1   
  end
 #   $log.debug " crusor back cp:#{@curpos} pcol:#{@pcol} b.l:#{@buffer.length} d_l:#{@display_length} fc:#{@form.col}"
=begin
# this is perfect if not scrolling, but now needs changes
  if @curpos > 0
    @curpos -= 1
    addcol -1
  end
=end

end

#cursor_endObject

goto end of field, “end” is a keyword so could not use it.



1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
# File 'lib/rbcurse/rwidget.rb', line 1892

def cursor_end
      blen = @buffer.rstrip.length
      if blen < @display_length
        set_form_col blen
      else
        @pcol = blen-@display_length
        set_form_col @display_length-1
      end
      @curpos = blen # HACK XXX
#  $log.debug " crusor END cp:#{@curpos} pcol:#{@pcol} b.l:#{@buffer.length} d_l:#{@display_length} fc:#{@form.col}"
  #set_form_col @buffer.length
end

#cursor_forwardObject



1913
1914
1915
1916
1917
1918
1919
1920
1921
# File 'lib/rbcurse/rwidget.rb', line 1913

def cursor_forward
  if @curpos < @buffer.length 
    if addcol(1)==-1  # go forward if you can, else scroll
      @pcol += 1 if @pcol < @display_length 
    end
    @curpos += 1
  end
 # $log.debug " crusor FORWARD cp:#{@curpos} pcol:#{@pcol} b.l:#{@buffer.length} d_l:#{@display_length} fc:#{@form.col}"
end

#cursor_homeObject

position cursor at start of field



1886
1887
1888
1889
# File 'lib/rbcurse/rwidget.rb', line 1886

def cursor_home
  set_form_col 0
  @pcol = 0
end

#delete_at(index = @curpos) ⇒ Object



1771
1772
1773
1774
1775
1776
1777
# File 'lib/rbcurse/rwidget.rb', line 1771

def delete_at index=@curpos
  return -1 if !@editable 
  @buffer.slice!(index,1)
  $log.debug " delete at #{index}: #{@buffer.length}: #{@buffer}"
  @modified = true
  fire_handler :CHANGE, self    # 2008-12-09 14:51 
end

#delete_curr_charObject

# this is perfect if not scrolling, but now needs changes

if @curpos > 0
  @curpos -= 1
  addcol -1
end


1941
1942
1943
1944
1945
# File 'lib/rbcurse/rwidget.rb', line 1941

def delete_curr_char
  return -1 unless @editable
  delete_at
  set_modified 
end

#delete_eolObject

set_form_col @buffer.length



1904
1905
1906
1907
1908
1909
1910
1911
1912
# File 'lib/rbcurse/rwidget.rb', line 1904

def delete_eol
  return -1 unless @editable
  pos = @curpos-1
  @delete_buffer = @buffer[@curpos..-1]
  # if pos is 0, pos-1 becomes -1, end of line!
  @buffer = pos == -1 ? "" : @buffer[0..pos]
  fire_handler :CHANGE, self    # 2008-12-09 14:51 
  return @delete_buffer
end

#delete_prev_charObject



1946
1947
1948
1949
1950
1951
1952
1953
# File 'lib/rbcurse/rwidget.rb', line 1946

def delete_prev_char
  return -1 if !@editable 
  return if @curpos <= 0
  @curpos -= 1 if @curpos > 0
  delete_at
  set_modified 
  addcol -1
end

#getvalueObject

converts back into original type

changed to convert on 2009-01-06 23:39


1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
# File 'lib/rbcurse/rwidget.rb', line 1788

def getvalue
  dt = @datatype || String
  case dt.to_s
  when "String"
    return @buffer
  when "Fixnum"
    return @buffer.to_i
  when "Float"
    return @buffer.to_f
  else
    return @buffer.to_s
  end
end

#handle_key(ch) ⇒ Object

field



1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
# File 'lib/rbcurse/rwidget.rb', line 1842

def handle_key ch
  case ch
  when KEY_LEFT
    cursor_backward
  when KEY_RIGHT
    cursor_forward
  when KEY_BACKSPACE, 127
    delete_prev_char if @editable
  #when KEY_UP
  #  $log.debug " FIELD GOT KEY_UP, NOW IGNORING 2009-01-16 17:52 "
    #@form.select_prev_field # in a table this should not happen 2009-01-16 17:47 
  #  return :UNHANDLED
  #when KEY_DOWN
  #  $log.debug " FIELD GOT KEY_DOWN, NOW IGNORING 2009-01-16 17:52 "
    #@form.select_next_field # in a table this should not happen 2009-01-16 17:47 
  #  return :UNHANDLED
  when KEY_ENTER, 10, 13
    if respond_to? :fire
      fire
    end
  when 330
    delete_curr_char if @editable
  when ?\C-a.getbyte(0)
    cursor_home 
  when ?\C-e.getbyte(0)
    cursor_end 
  when ?\C-k.getbyte(0)
    delete_eol if @editable
  when ?\C-_.getbyte(0) # changed on 2010-02-26 14:44 so C-u can be used as numeric arg
    @buffer.insert @curpos, @delete_buffer unless @delete_buffer.nil?
  when 32..126
    #$log.debug("FIELD: ch #{ch} ,at #{@curpos}, buffer:[#{@buffer}] bl: #{@buffer.to_s.length}")
    putc ch
  when 27 # escape
    $log.debug " ADDED FIELD ESCAPE on 2009-01-18 12:27 XXX #{@original_value}"
    set_buffer @original_value 
  else
    ret = super
    return ret
  end
  0 # 2008-12-16 23:05 without this -1 was going back so no repaint
end

#init_varsObject



1711
1712
1713
1714
1715
# File 'lib/rbcurse/rwidget.rb', line 1711

def init_vars
  @pcol = 0   # needed for horiz scrolling
  @curpos = 0                  # current cursor position in buffer
  @modified = false
end

#modified?Boolean

overriding widget, check for value change

2009-01-18 12:25

Returns:

  • (Boolean)


2004
2005
2006
# File 'lib/rbcurse/rwidget.rb', line 2004

def modified?
  getvalue() != @original_value
end

#on_enterObject

save original value on enter, so we can check for modified.

2009-01-18 12:25


1997
1998
1999
2000
# File 'lib/rbcurse/rwidget.rb', line 1997

def on_enter
  @original_value = getvalue.dup rescue getvalue
  super
end

#on_leaveObject

upon leaving a field returns false if value not valid as per values or valid_regex 2008-12-22 12:40 if null_allowed, don’t validate, but do fire_handlers



1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
# File 'lib/rbcurse/rwidget.rb', line 1972

def on_leave
  val = getvalue
  #$log.debug " FIELD ON LEAVE:#{val}. #{@values.inspect}"
  valid = true
  if val.to_s.empty? and @null_allowed
    $log.debug " empty and null allowed"
  else
    if !@values.nil?
      valid = @values.include? val
      raise FieldValidationException, "Field value (#{val}) not in values: #{@values.join(',')}" unless valid
    end
    if !@valid_regex.nil?
      valid = @valid_regex.match(val.to_s)
      raise FieldValidationException, "Field not matching regex #{@valid_regex}" unless valid
    end
  end
  # here is where we should set the forms modified to true - 2009-01-18 12:36 XXX
  if modified?
    set_modified true
  end
  super
  #return valid
end

#putc(c) ⇒ Object

TODO : sending c>=0 allows control chars to go. Should be >= ?A i think.



1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
# File 'lib/rbcurse/rwidget.rb', line 1756

def putc c
  if c >= 0 and c <= 127
    ret = putch c.chr
    if ret == 0
      if addcol(1) == -1  # if can't go forward, try scrolling
        # scroll if exceeding display len but less than max len
        if @curpos > @display_length and @curpos <= @maxlen
          @pcol += 1 if @pcol < @display_length 
        end
      end
      set_modified 
    end
  end
  return -1
end

#putch(char) ⇒ Object



1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
# File 'lib/rbcurse/rwidget.rb', line 1735

def putch char
  return -1 if !@editable 
  return -1 if !@overwrite_mode and @buffer.length >= @maxlen
  if @chars_allowed != nil
    return if char.match(@chars_allowed).nil?
  end
  # added insert or overwrite mode 2010-03-17 20:11 
  if @overwrite_mode
    @buffer[@curpos] = char
  else
    @buffer.insert(@curpos, char)
  end
  @curpos += 1 if @curpos < @maxlen
  @modified = true
  $log.debug " FIELD FIRING CHANGE: #{char} at new #{@curpos}: bl:#{@buffer.length} buff:[#{@buffer}]"
  fire_handler :CHANGE, self    # 2008-12-09 14:51 
  0
end

#repaintObject

Note that some older widgets like Field repaint every time the form.repaint + is called, whether updated or not. I can’t remember why this is, but + currently I’ve not implemented events with these widgets. 2010-01-03 15:00



1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
# File 'lib/rbcurse/rwidget.rb', line 1813

def repaint
  $log.debug("repaint FIELD: #{id}, #{name},  #{focusable}")
  #return if display_length <= 0 # added 2009-02-17 00:17 sometimes editor comp has 0 and that
  # becomes negative below, no because editing still happens
  @display_length = 1 if display_length == 0
  printval = getvalue_for_paint().to_s # added 2009-01-06 23:27 
  printval = show()*printval.length unless @show.nil?
  if !printval.nil? 
    if printval.length > display_length # only show maxlen
      printval = printval[@pcol..@pcol+display_length-1] 
    else
      printval = printval[@pcol..-1]
    end
  end
  #printval = printval[0..display_length-1] if printval.length > display_length
  if @bgcolor.is_a? String and @color.is_a? String
    acolor = ColorMap.get_color(@color, @bgcolor)
  else
    acolor = $datacolor
  end
  @graphic = @form.window if @graphic.nil? ## cell editor listbox hack XXX fix in correct place
  $log.debug " Field g:#{@graphic}. r,c,displen:#{@row}, #{@col}, #{@display_length} "
  @graphic.printstring  row, col, sprintf("%-*s", display_length, printval), acolor, @attr
end

#set_buffer(value) ⇒ Object

should this do a dup ??



1780
1781
1782
1783
1784
1785
# File 'lib/rbcurse/rwidget.rb', line 1780

def set_buffer value
  @datatype = value.class
  #$log.debug " FIELD DATA #{@datatype}"
  @buffer = value.to_s
  @curpos = 0
end

#set_focusable(tf) ⇒ Object



1837
1838
1839
# File 'lib/rbcurse/rwidget.rb', line 1837

def set_focusable(tf)
  @focusable = tf
end

#set_label(label) ⇒ Object



1802
1803
1804
1805
1806
1807
# File 'lib/rbcurse/rwidget.rb', line 1802

def set_label label
  @label = label
  label.row  @row if label.row == -1
  label.col  @col-(label.name.length+1) if label.col == -1
  label.label_for(self)
end

#text_variable(tv) ⇒ Object



1716
1717
1718
1719
# File 'lib/rbcurse/rwidget.rb', line 1716

def text_variable tv
  @text_variable = tv
  set_buffer tv.value
end