Class: RubyCurses::Field
Overview
Text edit field NOTE: To get value use getvalue() TODO - test text_variable TODO: some methods should return self, so chaining can be done. Not sure if the return value of the
fire_handler is being checked.
NOTE: i have just added repain_required check in Field before repaint
this may mean in some places field does not paint. repaint_require will have to be set
to true in those cases. this was since field was overriding a popup window that was not modal.
Direct Known Subclasses
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
-
#buffer ⇒ Object
readonly
actual buffer being used for storage.
-
#datatype ⇒ Object
attr_reader :curpos # cursor position in buffer current, in WIDGET.
-
#form ⇒ Object
readonly
Returns the value of attribute form.
-
#handler ⇒ Object
readonly
event handler.
-
#original_value ⇒ Object
readonly
value on entering field.
-
#overwrite_mode ⇒ Object
true or false INSERT OVERWRITE MODE.
-
#type(dtype) ⇒ Object
readonly
define a datatype, currently only influences chars allowed integer and float.
Attributes inherited from Widget
#_object_created, #col_offset, #cols_panned, #config, #curpos, #focussed, #id, #parent_component, #row_offset, #rows_panned, #state
Instance Method Summary collapse
-
#addcol(num) ⇒ Object
add a column to cursor position.
-
#cursor_backward ⇒ Object
$log.debug “ crusor FORWARD cp:#@curpos pcol:#@pcol b.l:#@[email protected] d_l:#@display_length fc:#@[email protected]”.
-
#cursor_end ⇒ Object
goto end of field, “end” is a keyword so could not use it.
- #cursor_forward ⇒ Object
-
#cursor_home ⇒ Object
position cursor at start of field.
- #delete_at(index = @curpos) ⇒ Object
-
#delete_curr_char ⇒ Object
# this is perfect if not scrolling, but now needs changes if @curpos > 0 @curpos -= 1 addcol -1 end.
-
#delete_eol ⇒ Object
$log.debug “ crusor END cp:#@curpos pcol:#@pcol b.l:#@[email protected] d_l:#@display_length fc:#@[email protected]” set_form_col @buffer.length.
- #delete_prev_char ⇒ Object
-
#getvalue ⇒ Object
converts back into original type changed to convert on 2009-01-06 23:39.
-
#handle_key(ch) ⇒ Object
field.
- #init_vars ⇒ Object
-
#initialize(form, config = {}, &block) ⇒ Field
constructor
A new instance of Field.
- #map_keys ⇒ Object
-
#modified? ⇒ Boolean
overriding widget, check for value change 2009-01-18 12:25.
-
#on_enter ⇒ Object
save original value on enter, so we can check for modified.
-
#on_leave ⇒ Object
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.
-
#putc(c) ⇒ Object
TODO : sending c>=0 allows control chars to go.
-
#putch(char) ⇒ Fixnum
add a char to field, and validate NOTE: this should return self for chaining operations and throw an exception if disabled or exceeding size.
-
#repaint ⇒ Object
Note that some older widgets like Field repaint every time the form.repaint + is called, whether updated or not.
-
#restore_original_value ⇒ Object
silently restores value without firing handlers, use if exception and you want old value.
-
#set_buffer(value) ⇒ Object
should this do a dup ?? YES set value of Field fires CHANGE handler.
- #set_focusable(tf) ⇒ Object
-
#set_label(label) ⇒ Object
create a label linked to this field Typically one passes a Label, but now we can pass just a String, a label is created.
-
#text(*val) ⇒ Object
field, a convenience method, since set_buffer sucks, it was “inspired” by ncurses itself.
- #text=(val) ⇒ Object
-
#text_variable(tv) ⇒ Object
Set Variable as value.
-
#undo_delete_eol ⇒ Object
does an undo on delete_eol, not a real undo.
Methods inherited from Widget
#changed, #click, #destroy, #enter, #event_list, #focus, #get_preferred_size, #getvalue_for_paint, #height, #height=, #hide, #leave, #move, #override_graphic, #printstring, #process_key, #remove, #repaint_all, #repaint_required, #rowcol, #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, #warn
Methods included from Utils
#_process_key, #bind_key, #clean_string!, #get_color, #keycode_tos, #repeatm, #view, #wrap_text
Methods included from ConfigSetup
#cget, #config_setup, #configure, #variable_set
Methods included from EventHandler
#bind, #fire_handler, #fire_property_change
Constructor Details
#initialize(form, config = {}, &block) ⇒ Field
Returns a new instance of Field.
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 |
# File 'lib/rbcurse/rwidget.rb', line 1667 def initialize form, config={}, &block @form = form @buffer = String.new #@type=config.fetch("type", :varchar) @display_length = 20 @maxlen = @display_length @row = 0 @col = 0 @bgcolor = $def_bg_color @color = $def_fg_color @editable = true @focusable = true @event_args = {} # arguments passed at time of binding, to use when firing event map_keys init_vars @_events ||= [] @_events.push(:CHANGE) super end |
Instance Attribute Details
#buffer ⇒ Object (readonly)
actual buffer being used for storage
1642 1643 1644 |
# File 'lib/rbcurse/rwidget.rb', line 1642 def buffer @buffer end |
#datatype ⇒ Object
attr_reader :curpos # cursor position in buffer current, in WIDGET
1663 1664 1665 |
# File 'lib/rbcurse/rwidget.rb', line 1663 def datatype @datatype end |
#form ⇒ Object (readonly)
Returns the value of attribute form.
1659 1660 1661 |
# File 'lib/rbcurse/rwidget.rb', line 1659 def form @form end |
#handler ⇒ Object (readonly)
event handler
1660 1661 1662 |
# File 'lib/rbcurse/rwidget.rb', line 1660 def handler @handler end |
#original_value ⇒ Object (readonly)
value on entering field
1664 1665 1666 |
# File 'lib/rbcurse/rwidget.rb', line 1664 def original_value @original_value end |
#overwrite_mode ⇒ Object
true or false INSERT OVERWRITE MODE
1665 1666 1667 |
# File 'lib/rbcurse/rwidget.rb', line 1665 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?
2010-09-10 20:59 changed string to symbol
1661 1662 1663 |
# File 'lib/rbcurse/rwidget.rb', line 1661 def type @type end |
Instance Method Details
#addcol(num) ⇒ Object
add a column to cursor position. Field
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 |
# File 'lib/rbcurse/rwidget.rb', line 1980 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_backward ⇒ Object
$log.debug “ crusor FORWARD cp:#@curpos pcol:#@pcol b.l:#RubyCurses::Field.@[email protected] d_l:#@display_length fc:#RubyCurses::Field.@[email protected]”
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 |
# File 'lib/rbcurse/rwidget.rb', line 1947 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_end ⇒ Object
goto end of field, “end” is a keyword so could not use it.
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 |
# File 'lib/rbcurse/rwidget.rb', line 1916 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 # $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_forward ⇒ Object
1938 1939 1940 1941 1942 1943 1944 1945 1946 |
# File 'lib/rbcurse/rwidget.rb', line 1938 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_home ⇒ Object
position cursor at start of field
1910 1911 1912 1913 |
# File 'lib/rbcurse/rwidget.rb', line 1910 def cursor_home set_form_col 0 @pcol = 0 end |
#delete_at(index = @curpos) ⇒ Object
1773 1774 1775 1776 1777 1778 1779 1780 |
# File 'lib/rbcurse/rwidget.rb', line 1773 def delete_at index=@curpos return -1 if !@editable char = @buffer.slice!(index,1) #$log.debug " delete at #{index}: #{@buffer.length}: #{@buffer}" @modified = true #fire_handler :CHANGE, self # 2008-12-09 14:51 fire_handler :CHANGE, InputDataEvent.new(@curpos,@curpos, self, :DELETE, 0, char) # 2010-09-11 13:01 end |
#delete_curr_char ⇒ Object
# this is perfect if not scrolling, but now needs changes
if @curpos > 0
@curpos -= 1
addcol -1
end
1966 1967 1968 1969 1970 |
# File 'lib/rbcurse/rwidget.rb', line 1966 def delete_curr_char return -1 unless @editable delete_at set_modified end |
#delete_eol ⇒ Object
$log.debug “ crusor END cp:#@curpos pcol:#@pcol b.l:#RubyCurses::Field.@[email protected] d_l:#@display_length fc:#RubyCurses::Field.@[email protected]” set_form_col @buffer.length
1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 |
# File 'lib/rbcurse/rwidget.rb', line 1928 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 fire_handler :CHANGE, InputDataEvent.new(@curpos,@curpos+@delete_buffer.length, self, :DELETE, 0, @delete_buffer) # 2010-09-11 13:01 return @delete_buffer end |
#delete_prev_char ⇒ Object
1971 1972 1973 1974 1975 1976 1977 1978 |
# File 'lib/rbcurse/rwidget.rb', line 1971 def delete_prev_char return -1 if !@editable return if @curpos <= 0 @curpos -= 1 if @curpos > 0 delete_at set_modified addcol -1 end |
#getvalue ⇒ Object
converts back into original type
changed to convert on 2009-01-06 23:39
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 |
# File 'lib/rbcurse/rwidget.rb', line 1807 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
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 |
# File 'lib/rbcurse/rwidget.rb', line 1886 def handle_key ch @repaint_required = true #map_keys unless @keys_mapped # moved to init case ch when 32..126 #$log.debug("FIELD: ch #{ch} ,at #{@curpos}, buffer:[#{@buffer}] bl: #{@buffer.to_s.length}") putc ch when 27 # cannot bind it 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_vars ⇒ Object
1686 1687 1688 1689 1690 1691 |
# File 'lib/rbcurse/rwidget.rb', line 1686 def init_vars @pcol = 0 # needed for horiz scrolling @curpos = 0 # current cursor position in buffer @modified = false @repaint_required = true end |
#map_keys ⇒ Object
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 |
# File 'lib/rbcurse/rwidget.rb', line 1868 def map_keys return if @keys_mapped bind_key(FFI::NCurses::KEY_LEFT){ cursor_backward } bind_key(FFI::NCurses::KEY_RIGHT){ cursor_forward } bind_key(FFI::NCurses::KEY_BACKSPACE){ delete_prev_char } bind_key(127){ delete_prev_char } bind_key(330){ delete_curr_char } bind_key(?\C-a){ cursor_home } bind_key(?\C-e){ cursor_end } bind_key(?\C-k){ delete_eol } bind_key(?\C-_){ undo_delete_eol } #bind_key(27){ set_buffer @original_value } bind_key(?\C-g){ set_buffer @original_value } # 2011-09-29 V1.3.1 ESC did not work @keys_mapped = true end |
#modified? ⇒ Boolean
overriding widget, check for value change
2009-01-18 12:25
2038 2039 2040 |
# File 'lib/rbcurse/rwidget.rb', line 2038 def modified? getvalue() != @original_value end |
#on_enter ⇒ Object
save original value on enter, so we can check for modified.
2009-01-18 12:25
2011-10-9 I have changed to take @buffer since getvalue returns a datatype
and this causes a crash in set_original on cursor forward.
2030 2031 2032 2033 2034 |
# File 'lib/rbcurse/rwidget.rb', line 2030 def on_enter #@original_value = getvalue.dup rescue getvalue @original_value = @buffer.dup # getvalue.dup rescue getvalue super end |
#on_leave ⇒ Object
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
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 |
# File 'lib/rbcurse/rwidget.rb', line 1997 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 # added valid_range for numerics 2011-09-29 if !@valid_range.nil? valid = @valid_range.include?(val.to_i) raise FieldValidationException, "Field not matching range #{@valid_range}" unless valid end end # here is where we should set the forms modified to true - 2009-01 if modified? set_modified true end # if super fails we would have still set modified to true super #return valid end |
#putc(c) ⇒ Object
TODO : sending c>=0 allows control chars to go. Should be >= ?A i think.
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 |
# File 'lib/rbcurse/rwidget.rb', line 1757 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 return 0 # 2010-09-11 12:59 else would always return -1 end end return -1 end |
#putch(char) ⇒ Fixnum
add a char to field, and validate NOTE: this should return self for chaining operations and throw an exception if disabled or exceeding size
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 |
# File 'lib/rbcurse/rwidget.rb', line 1728 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 oldchar = nil if @overwrite_mode oldchar = @buffer[@curpos] @buffer[@curpos] = char else @buffer.insert(@curpos, char) end oldcurpos = @curpos @curpos += 1 if @curpos < @maxlen @modified = true #$log.debug " FIELD FIRING CHANGE: #{char} at new #{@curpos}: bl:#{@buffer.length} buff:[#{@buffer}]" # i have no way of knowing what change happened and what char was added deleted or changed #fire_handler :CHANGE, self # 2008-12-09 14:51 if @overwrite_mode fire_handler :CHANGE, InputDataEvent.new(oldcurpos,@curpos, self, :DELETE, 0, oldchar) # 2010-09-11 12:43 end fire_handler :CHANGE, InputDataEvent.new(oldcurpos,@curpos, self, :INSERT, 0, char) # 2010-09-11 12:43 0 end |
#repaint ⇒ Object
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
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 |
# File 'lib/rbcurse/rwidget.rb', line 1843 def repaint return unless @repaint_required # 2010-11-20 13:13 its writing over a window i think TESTING #$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 acolor = @color_pair || get_color($datacolor, @color, @bgcolor) @graphic = @form.window if @graphic.nil? ## cell editor listbox hack #$log.debug " Field g:#{@graphic}. r,c,displen:#{@row}, #{@col}, #{@display_length} " @graphic.printstring row, col, sprintf("%-*s", display_length, printval), acolor, @attr @repaint_required = false end |
#restore_original_value ⇒ Object
silently restores value without firing handlers, use if exception and you want old value
1784 1785 1786 1787 1788 |
# File 'lib/rbcurse/rwidget.rb', line 1784 def restore_original_value @buffer = @original_value.dup #@curpos = 0 # this would require restting setformcol @repaint_required = true end |
#set_buffer(value) ⇒ Object
should this do a dup ?? YES set value of Field fires CHANGE handler
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 |
# File 'lib/rbcurse/rwidget.rb', line 1793 def set_buffer value @repaint_required = true @datatype = value.class #$log.debug " FIELD DATA #{@datatype}" @delete_buffer = @buffer.dup @buffer = value.to_s.dup @curpos = 0 # hope @delete_buffer is not overwritten fire_handler :CHANGE, InputDataEvent.new(@curpos,@curpos, self, :DELETE, 0, @delete_buffer) # 2010-09-11 13:01 fire_handler :CHANGE, InputDataEvent.new(@curpos,@curpos, self, :INSERT, 0, @buffer) # 2010-09-11 13:01 self # 2011-10-2 end |
#set_focusable(tf) ⇒ Object
1865 1866 1867 |
# File 'lib/rbcurse/rwidget.rb', line 1865 def set_focusable(tf) @focusable = tf end |
#set_label(label) ⇒ Object
create a label linked to this field Typically one passes a Label, but now we can pass just a String, a label is created
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 |
# File 'lib/rbcurse/rwidget.rb', line 1825 def set_label label # added case for user just using a string case label when String # what if no form at this point label = Label.new @form, {:text => label} end @label = label label.row @row if label.row == -1 label.col @col-(label.name.length+1) if label.col == -1 label.label_for(self) label end |
#text(*val) ⇒ Object
field, a convenience method, since set_buffer sucks, it was “inspired” by ncurses itself
2043 2044 2045 2046 2047 2048 2049 2050 2051 |
# File 'lib/rbcurse/rwidget.rb', line 2043 def text(*val) if val.empty? return getvalue() else return unless val # added 2010-11-17 20:11, dup will fail on nil s = val[0].dup set_buffer(s) end end |
#text=(val) ⇒ Object
2052 2053 2054 2055 |
# File 'lib/rbcurse/rwidget.rb', line 2052 def text=(val) return unless val # added 2010-11-17 20:11, dup will fail on nil set_buffer(val.dup) end |
#text_variable(tv) ⇒ Object
Set Variable as value.
This allows using Field as a proxy
@param [Variable] variable containing text value
1698 1699 1700 1701 |
# File 'lib/rbcurse/rwidget.rb', line 1698 def text_variable tv @text_variable = tv set_buffer tv.value end |
#undo_delete_eol ⇒ Object
does an undo on delete_eol, not a real undo
1902 1903 1904 1905 1906 1907 |
# File 'lib/rbcurse/rwidget.rb', line 1902 def undo_delete_eol return if @delete_buffer.nil? #oldvalue = @buffer @buffer.insert @curpos, @delete_buffer fire_handler :CHANGE, InputDataEvent.new(@curpos,@curpos+@delete_buffer.length, self, :INSERT, 0, @delete_buffer) # 2010-09-11 13:01 end |