1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ready to use 'validatecommand' option of TkEntry/TkSpinbox widget

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4307 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nagai 2003-08-03 22:07:47 +00:00
parent 381ff05505
commit a09c02a600
3 changed files with 45 additions and 25 deletions

View file

@ -15,9 +15,19 @@ class TkEntry<TkLabel
class ValidateCmd
include TkComm
module Action
Insert = 1
Delete = 0
Others = -1
Focus = -1
Forced = -1
Textvariable = -1
TextVariable = -1
end
class ValidateArgs
VARG_KEY = 'disvPSVW'
VARG_TYPE = 'nnsssssw'
VARG_TYPE = 'nxsssssw'
def self.scan_args(arg_str, arg_val)
arg_cnv = []
@ -31,6 +41,13 @@ class TkEntry<TkLabel
arg_cnv << TkComm::string(arg_val[idx])
when ?w
arg_cnv << TkComm::window(arg_val[idx])
when ?x
idx = TkComm::number(arg_val[idx])
if idx < 0
arg_cnv << nil
else
arg_cnv << idx
end
else
arg_cnv << arg_val[idx]
end
@ -68,16 +85,16 @@ class TkEntry<TkLabel
if args
@id =
install_cmd(proc{|*arg|
TkUtil.eval_cmd(cmd, ValidateArgs.scan_args(args, arg))
TkUtil.eval_cmd(proc{|*v| (cmd.call(*v))? '1': '0'},
*ValidateArgs.scan_args(args, arg))
}) + " " + args
else
args = ' %d %i %s %v %P %S %V %W'
@id =
install_cmd(proc{|*arg|
TkUtil.eval_cmd(
cmd,
ValidateArgs.new(ValidateArgs.scan_args(args, arg))
)
TkUtil.eval_cmd(proc{|*v| (cmd.call(*v))? '1': '0'},
ValidateArgs.new(*ValidateArgs \
.scan_args(args,arg)))
}) + args
end
end
@ -88,10 +105,9 @@ class TkEntry<TkLabel
end
def create_self(keys)
tk_call 'entry', @path
if keys and keys != None
tk_call 'entry', @path, *hash_kv(keys)
else
tk_call 'entry', @path
configure(keys)
end
end
@ -193,28 +209,35 @@ class TkEntry<TkLabel
self
end
def invoke_validate
bool(tk_send('validate'))
end
def validate(mode = nil)
if mode
configure 'validate', mode
else
bool(tk_send('validate'))
invoke_validate
end
end
def validatecommand(cmd = ValidateCmd.new, args = nil)
def validatecommand(cmd = Proc.new, args = nil)
if cmd.kind_of?(ValidateCmd)
configure('validatecommand', cmd)
elsif args
configure('validatecommand', [cmd, args])
else
configure('validatecommand', ValidateCmd.new(cmd, args))
configure('validatecommand', cmd)
end
end
alias vcmd validatecommand
def invalidcommand(cmd = ValidateCmd.new, args = nil)
def invalidcommand(cmd = Proc.new, args = nil)
if cmd.kind_of?(ValidateCmd)
configure('invalidcommand', cmd)
elsif args
configure('invalidcommand', [cmd, args])
else
configure('invalidcommand', ValidateCmd.new(cmd, args))
configure('invalidcommand', cmd)
end
end
alias invcmd invalidcommand
@ -234,10 +257,9 @@ class TkSpinbox<TkEntry
WidgetClassNames[WidgetClassName] = self
def create_self(keys)
tk_call 'spinbox', @path
if keys and keys != None
tk_call 'spinbox', @path, *hash_kv(keys)
else
tk_call 'spinbox', @path
configure(keys)
end
end

View file

@ -53,11 +53,10 @@ australianCities = [
]
[
# Current version of Ruby/Tk (current is 1.8.0) is not enough
# to support 'validate' option and related options. This problem
# will be improved in the next or future version.
TkSpinbox.new($spin_demo, :from=>1, :to=>10, :width=>10, :validate=>:key,
:validatecommand=>"string is integer %P"),
:validatecommand=>[
proc{|s| s == '' || /^[+-]?\d+$/ =~ s }, '%P'
]),
TkSpinbox.new($spin_demo, :from=>0, :to=>3, :increment=>0.5,
:format=>'%05.2f', :width=>10),
TkSpinbox.new($spin_demo, :values=>australianCities, :width=>10)

View file

@ -57,11 +57,10 @@ australianCities = [
]
[
# 現状の Ruby/Tk (現在のバージョンは 1.8.0) では、validate オプションや
# それに関連するオプションへのサポートを十分に達成できていません。この
# 問題については、次または将来のバージョンで改善する予定です。
TkSpinbox.new($spin_demo, :from=>1, :to=>10, :width=>10, :validate=>:key,
:validatecommand=>"string is integer %P"),
:validatecommand=>[
proc{|s| s == '' || /^[+-]?\d+$/ =~ s }, '%P'
]),
TkSpinbox.new($spin_demo, :from=>0, :to=>3, :increment=>0.5,
:format=>'%05.2f', :width=>10),
TkSpinbox.new($spin_demo, :values=>australianCities, :width=>10)