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