diff options
author | nagai <nagai@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-08-03 22:07:47 +0000 |
---|---|---|
committer | nagai <nagai@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-08-03 22:07:47 +0000 |
commit | a09c02a600e06aa4f90bc4cb5ccd5f5f1a56a97a (patch) | |
tree | 31fe3f70e93ec2024d7eae6a6f25c9bd69ddcfff /ext/tk | |
parent | 381ff055059337befcdf33ef8590ac2c065cb9cc (diff) | |
download | ruby-a09c02a600e06aa4f90bc4cb5ccd5f5f1a56a97a.tar.gz |
* 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
Diffstat (limited to 'ext/tk')
-rw-r--r-- | ext/tk/lib/tkentry.rb | 56 | ||||
-rw-r--r-- | ext/tk/sample/demos-en/spin.rb | 7 | ||||
-rw-r--r-- | ext/tk/sample/demos-jp/spin.rb | 7 |
3 files changed, 45 insertions, 25 deletions
diff --git a/ext/tk/lib/tkentry.rb b/ext/tk/lib/tkentry.rb index ab69e76550..fc52b9435d 100644 --- a/ext/tk/lib/tkentry.rb +++ b/ext/tk/lib/tkentry.rb @@ -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 diff --git a/ext/tk/sample/demos-en/spin.rb b/ext/tk/sample/demos-en/spin.rb index 882772ad4d..5d32454170 100644 --- a/ext/tk/sample/demos-en/spin.rb +++ b/ext/tk/sample/demos-en/spin.rb @@ -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) diff --git a/ext/tk/sample/demos-jp/spin.rb b/ext/tk/sample/demos-jp/spin.rb index 909d2b4818..4f6edc13c0 100644 --- a/ext/tk/sample/demos-jp/spin.rb +++ b/ext/tk/sample/demos-jp/spin.rb @@ -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) |