class Rgw::RestrictedEntry
Public Class Methods
Initialize a new Rgw::RestrictedEntry
object
# File lib/rgw/restricted-entry.rb, line 33 def initialize super() @restriction = :none @lcNumeric = I18nToolbox::LcNumeric.new @beepOnError = true @isValid = false @range = nil @matchInteger = Regexp.new /[0-9-]/ @matchFloat = Regexp.new /[0-9#{@lcNumeric.delimiter.chr}-]/ @matchFloatString = Regexp.new /^\-?[0-9]*#{@lcNumeric.delimiter.chr}{,1}[0-9]*$/ signal_connect(:insert_text) {|wid, str| signal_emit_stop :insert_text unless chr_valid? str} signal_connect_after(:insert_text) {str_valid?} signal_connect_after(:delete_text) {str_valid?} end
Public Instance Methods
Enables to beep if the string does not represent a valid number, i.e. has two decimal points or is empty
@param beep [true, false] if beeping should be enabled
# File lib/rgw/restricted-entry.rb, line 81 def beep_on_error=(beep) @beepOnError = beep == true end
Set the range of valid values for the entry
param rg [Range] for a restriction of :integer or :float the range gives the minimum and
maximum values, which are allowed. for a restriction of :none the range gives the minimum or maximum length of the inserted string
# File lib/rgw/restricted-entry.rb, line 69 def range=(rg) raise ArgumentError, "invalid type %s for range" % rg.class.to_s unless rg.is_a? Range raise ArgumentError, "range has to be numeric" unless rg.first.is_a? Numeric and rg.last.is_a? Numeric raise ArgumentError, "range start has to be positive for none-restrictions" if @restriction == :none and rg.first < 0 @range = rg end
Sets the restriction to the entry. Only characters which match the selected type will show up in the entry.
@param rest [:integer, :float, :none] The entry will be limited to characters to display
Integers (:integer), Floats (:float) or has no restriction (:none)
# File lib/rgw/restricted-entry.rb, line 56 def restriction=(rest) raise ArgumentError, "invalid type %s for restriction" % rest.class.to_s unless rest.is_a? Symbol raise ArgumentError, "invalid restriction %s" % rest.to_s unless [:integer, :float, :none].include? rest @restriction = rest end
Returns the text of the entry
@return [String, nil] Returns the string, if it is valid or nil if it is not.
# File lib/rgw/restricted-entry.rb, line 89 def text return super() if str_valid? nil end
Private Instance Methods
Checks if a single character is valid for the selected restriction
# File lib/rgw/restricted-entry.rb, line 117 def chr_valid? str return true if @restriction == :none if @restriction == :integer return @matchInteger =~ str end if @restriction == :float return @matchFloat =~ str end end
Checks if the text in the entry is a valid numeric representation
# File lib/rgw/restricted-entry.rb, line 97 def str_valid? txt = self.text_parent if @restriction == :none unless @range.nil? @isValid = @range.cover? txt.length else @isValid = true end else @isValid = 0 < txt.length @isValid &= @matchFloatString =~ txt @isValid &= @range.cover? @lcNumeric.to_f txt unless @range.nil? end @isValid ? set_secondary_icon_stock('') : set_secondary_icon_stock('gtk-dialog-warning') Gdk.beep if !@isValid and @beepOnError @isValid end