ruby-changes:4917
From: ko1@a...
Date: Wed, 14 May 2008 09:54:58 +0900 (JST)
Subject: [ruby-changes:4917] nagai - Ruby:r16410 (ruby_1_8): * ext/tk/tkutil/tkutil.c: improve handling callback-subst-keys.
nagai 2008-05-14 09:54:31 +0900 (Wed, 14 May 2008) New Revision: 16410 Modified files: branches/ruby_1_8/ChangeLog branches/ruby_1_8/ext/tk/lib/tk/event.rb branches/ruby_1_8/ext/tk/lib/tk/spinbox.rb branches/ruby_1_8/ext/tk/lib/tk/validation.rb branches/ruby_1_8/ext/tk/lib/tk.rb branches/ruby_1_8/ext/tk/lib/tkextlib/blt/dragdrop.rb branches/ruby_1_8/ext/tk/lib/tkextlib/blt/treeview.rb branches/ruby_1_8/ext/tk/lib/tkextlib/iwidgets/calendar.rb branches/ruby_1_8/ext/tk/lib/tkextlib/iwidgets/entryfield.rb branches/ruby_1_8/ext/tk/lib/tkextlib/iwidgets/hierarchy.rb branches/ruby_1_8/ext/tk/lib/tkextlib/iwidgets/spinner.rb branches/ruby_1_8/ext/tk/lib/tkextlib/tkDND/tkdnd.rb branches/ruby_1_8/ext/tk/lib/tkextlib/tktable/tktable.rb branches/ruby_1_8/ext/tk/lib/tkextlib/treectrl/tktreectrl.rb branches/ruby_1_8/ext/tk/lib/tkextlib/version.rb branches/ruby_1_8/ext/tk/lib/tkextlib/winico/winico.rb branches/ruby_1_8/ext/tk/tkutil/tkutil.c Log: * ext/tk/tkutil/tkutil.c: improve handling callback-subst-keys. Now, support longnam-keys (e.g. '%CTT' on tkdnd-2.0; however, still not support tkdnd-2.0 on tkextlib), and symbols of parameters (e.g. :widget=>'%W', :keycode=>'%k', '%x'=>:x, '%X'=>:root_x, and so on; those are attributes of event object). It means that Ruby/Tk accepts not only "widget.bind(ev, '%W', '%k', ...){|w, k, ...| ... }", but also "widget.bind(ev, :widget, :keycode, ...){|w, k, ...| ... }". It is potentially incompatible, when user passes symbols to the arguments of the callback block (the block receives the symbols as strings). I think that is very rare case (probably, used by Ruby/Tk experts only). When causes such trouble, please give strings instead of such symbol parameters (e.g. call Symbol#to_s method). * ext/tk/lib/tk/event.rb, ext/tk/lib/tk/validation.rb, ext/tk/lib/tkextlib/blt/treeview.rb, ext/tk/lib/tkextlib/winico/winico.rb: ditto. * ext/tk/tkutil/tkutil.c: strings are available on subst_tables on TkUtil::CallbackSubst class (it is useful on Ruby 1.9). * ext/tk/lib/tk/spinbox.rb, ext/tk/lib/tkextlib/iwidgets/hierarchy.rb, ext/tk/lib/tkextlib/iwidgets/spinner.rb, ext/tk/lib/tkextlib/iwidgets/entryfield.rb, ext/tk/lib/tkextlib/iwidgets/calendar.rb, ext/tk/lib/tkextlib/blt/dragdrop.rb, ext/tk/lib/tkextlib/tkDND/tkdnd.rb, ext/tk/lib/tkextlib/treectrl/tktreectrl.rb, ext/tk/lib/tkextlib/tktable/tktable.rb: disable code piece became unnecessary by reason of the changes of ext/tk/tkutil/tkutil.c. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/tk/lib/tkextlib/blt/dragdrop.rb?r1=16410&r2=16409&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/tk/lib/tk/validation.rb?r1=16410&r2=16409&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/tk/lib/tkextlib/iwidgets/calendar.rb?r1=16410&r2=16409&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ChangeLog?r1=16410&r2=16409&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/tk/lib/tkextlib/treectrl/tktreectrl.rb?r1=16410&r2=16409&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/tk/lib/tkextlib/iwidgets/spinner.rb?r1=16410&r2=16409&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/tk/lib/tkextlib/winico/winico.rb?r1=16410&r2=16409&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/tk/lib/tkextlib/blt/treeview.rb?r1=16410&r2=16409&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/tk/lib/tk/spinbox.rb?r1=16410&r2=16409&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/tk/lib/tkextlib/version.rb?r1=16410&r2=16409&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/tk/lib/tkextlib/iwidgets/entryfield.rb?r1=16410&r2=16409&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/tk/lib/tkextlib/tkDND/tkdnd.rb?r1=16410&r2=16409&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/tk/lib/tkextlib/iwidgets/hierarchy.rb?r1=16410&r2=16409&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/tk/lib/tk.rb?r1=16410&r2=16409&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/tk/lib/tk/event.rb?r1=16410&r2=16409&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/tk/tkutil/tkutil.c?r1=16410&r2=16409&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/tk/lib/tkextlib/tktable/tktable.rb?r1=16410&r2=16409&diff_format=u Index: ruby_1_8/ext/tk/lib/tk.rb =================================================================== --- ruby_1_8/ext/tk/lib/tk.rb (revision 16409) +++ ruby_1_8/ext/tk/lib/tk.rb (revision 16410) @@ -5359,7 +5359,7 @@ #Tk.freeze module Tk - RELEASE_DATE = '2008-05-12'.freeze + RELEASE_DATE = '2008-05-14'.freeze autoload :AUTO_PATH, 'tk/variable' autoload :TCL_PACKAGE_PATH, 'tk/variable' Index: ruby_1_8/ext/tk/lib/tkextlib/iwidgets/hierarchy.rb =================================================================== --- ruby_1_8/ext/tk/lib/tkextlib/iwidgets/hierarchy.rb (revision 16409) +++ ruby_1_8/ext/tk/lib/tkextlib/iwidgets/hierarchy.rb (revision 16410) @@ -31,6 +31,7 @@ KEY_TBL = [ [?n, ?s, :node], nil ] PROC_TBL = [ [?s, TkComm.method(:string) ], nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -46,6 +47,7 @@ end inf } +=end _setup_subst_table(KEY_TBL, PROC_TBL); @@ -74,6 +76,7 @@ nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -89,6 +92,7 @@ end inf } +=end _setup_subst_table(KEY_TBL, PROC_TBL); @@ -112,6 +116,7 @@ ] PROC_TBL = [ [ ?s, TkComm.method(:string) ], nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -127,6 +132,7 @@ end inf } +=end _setup_subst_table(KEY_TBL, PROC_TBL); Index: ruby_1_8/ext/tk/lib/tkextlib/iwidgets/spinner.rb =================================================================== --- ruby_1_8/ext/tk/lib/tkextlib/iwidgets/spinner.rb (revision 16409) +++ ruby_1_8/ext/tk/lib/tkextlib/iwidgets/spinner.rb (revision 16410) @@ -38,6 +38,7 @@ nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -53,6 +54,7 @@ end inf } +=end _setup_subst_table(KEY_TBL, PROC_TBL); end Index: ruby_1_8/ext/tk/lib/tkextlib/iwidgets/entryfield.rb =================================================================== --- ruby_1_8/ext/tk/lib/tkextlib/iwidgets/entryfield.rb (revision 16409) +++ ruby_1_8/ext/tk/lib/tkextlib/iwidgets/entryfield.rb (revision 16410) @@ -43,6 +43,7 @@ nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -58,6 +59,7 @@ end inf } +=end _setup_subst_table(KEY_TBL, PROC_TBL); end Index: ruby_1_8/ext/tk/lib/tkextlib/iwidgets/calendar.rb =================================================================== --- ruby_1_8/ext/tk/lib/tkextlib/iwidgets/calendar.rb (revision 16409) +++ ruby_1_8/ext/tk/lib/tkextlib/iwidgets/calendar.rb (revision 16410) @@ -46,6 +46,7 @@ KEY_TBL = [ [?d, ?s, :date], nil ] PROC_TBL = [ [?s, TkComm.method(:string) ], nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -61,6 +62,7 @@ end inf } +=end _setup_subst_table(KEY_TBL, PROC_TBL); Index: ruby_1_8/ext/tk/lib/tkextlib/blt/treeview.rb =================================================================== --- ruby_1_8/ext/tk/lib/tkextlib/blt/treeview.rb (revision 16409) +++ ruby_1_8/ext/tk/lib/tkextlib/blt/treeview.rb (revision 16410) @@ -239,6 +239,7 @@ nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -254,6 +255,7 @@ end inf } +=end _setup_subst_table(KEY_TBL, PROC_TBL); @@ -492,6 +494,7 @@ nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -507,6 +510,7 @@ end inf } +=end _setup_subst_table(KEY_TBL, PROC_TBL); @@ -523,7 +527,8 @@ def _find_exec_flag_value(val) if val.kind_of?(Array) cmd, *args = val - FindExecFlagValue.new(cmd, args.join(' ')) + #FindExecFlagValue.new(cmd, args.join(' ')) + FindExecFlagValue.new(cmd, *args) elsif TkComm._callback_entry?(val) FindExecFlagValue.new(val) else Index: ruby_1_8/ext/tk/lib/tkextlib/blt/dragdrop.rb =================================================================== --- ruby_1_8/ext/tk/lib/tkextlib/blt/dragdrop.rb (revision 16409) +++ ruby_1_8/ext/tk/lib/tkextlib/blt/dragdrop.rb (revision 16410) @@ -81,6 +81,7 @@ nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -96,6 +97,7 @@ end inf } +=end _setup_subst_table(KEY_TBL, PROC_TBL) @@ -123,6 +125,7 @@ nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -138,6 +141,7 @@ end inf } +=end _setup_subst_table(KEY_TBL, PROC_TBL) @@ -177,6 +181,7 @@ nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -192,6 +197,7 @@ end inf } +=end _setup_subst_table(KEY_TBL, PROC_TBL) end Index: ruby_1_8/ext/tk/lib/tkextlib/winico/winico.rb =================================================================== --- ruby_1_8/ext/tk/lib/tkextlib/winico/winico.rb (revision 16409) +++ ruby_1_8/ext/tk/lib/tkextlib/winico/winico.rb (revision 16410) @@ -150,6 +150,7 @@ nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -165,6 +166,7 @@ end inf } +=end _setup_subst_table(KEY_TBL, PROC_TBL); @@ -185,7 +187,8 @@ Winico_callback._config_keys.each{|k| if keys[k].kind_of?(Array) cmd, *args = keys[k] - keys[k] = Winico_callback.new(cmd, args.join(' ')) + #keys[k] = Winico_callback.new(cmd, args.join(' ')) + keys[k] = Winico_callback.new(cmd, *args) # elsif keys[k].kind_of?(Proc) elsif TkComm._callback_entry?(keys[k]) keys[k] = Winico_callback.new(keys[k]) @@ -201,7 +204,8 @@ Winico_callback._config_keys.each{|k| if keys[k].kind_of?(Array) cmd, *args = keys[k] - keys[k] = Winico_callback.new(cmd, args.join(' ')) + #keys[k] = Winico_callback.new(cmd, args.join(' ')) + keys[k] = Winico_callback.new(cmd, *args) # elsif keys[k].kind_of?(Proc) elsif TkComm._callback_entry?(keys[k]) keys[k] = Winico_callback.new(keys[k]) Index: ruby_1_8/ext/tk/lib/tkextlib/tkDND/tkdnd.rb =================================================================== --- ruby_1_8/ext/tk/lib/tkextlib/tkDND/tkdnd.rb (revision 16409) +++ ruby_1_8/ext/tk/lib/tkextlib/tkDND/tkdnd.rb (revision 16410) @@ -57,6 +57,7 @@ nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -72,6 +73,7 @@ end inf } +=end # setup tables _setup_subst_table(KEY_TBL, PROC_TBL); Index: ruby_1_8/ext/tk/lib/tkextlib/treectrl/tktreectrl.rb =================================================================== --- ruby_1_8/ext/tk/lib/tkextlib/treectrl/tktreectrl.rb (revision 16409) +++ ruby_1_8/ext/tk/lib/tkextlib/treectrl/tktreectrl.rb (revision 16410) @@ -137,6 +137,7 @@ nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -152,6 +153,7 @@ end inf } +=end # setup tables to be used by scan_args, _get_subst_key, _get_all_subst_keys # Index: ruby_1_8/ext/tk/lib/tkextlib/tktable/tktable.rb =================================================================== --- ruby_1_8/ext/tk/lib/tkextlib/tktable/tktable.rb (revision 16409) +++ ruby_1_8/ext/tk/lib/tkextlib/tktable/tktable.rb (revision 16410) @@ -291,6 +291,7 @@ nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -306,6 +307,7 @@ end inf } +=end _setup_subst_table(KEY_TBL, PROC_TBL); @@ -340,6 +342,7 @@ nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -355,6 +358,7 @@ end inf } +=end _setup_subst_table(KEY_TBL, PROC_TBL); @@ -387,6 +391,7 @@ nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -402,6 +407,7 @@ end inf } +=end _setup_subst_table(KEY_TBL, PROC_TBL); @@ -437,6 +443,7 @@ nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -452,6 +459,7 @@ end inf } +=end _setup_subst_table(KEY_TBL, PROC_TBL); end Index: ruby_1_8/ext/tk/lib/tkextlib/version.rb =================================================================== --- ruby_1_8/ext/tk/lib/tkextlib/version.rb (revision 16409) +++ ruby_1_8/ext/tk/lib/tkextlib/version.rb (revision 16410) @@ -2,5 +2,5 @@ # release date of tkextlib # module Tk - Tkextlib_RELEASE_DATE = '2008-05-12'.freeze + Tkextlib_RELEASE_DATE = '2008-05-14'.freeze end Index: ruby_1_8/ext/tk/lib/tk/validation.rb =================================================================== --- ruby_1_8/ext/tk/lib/tk/validation.rb (revision 16409) +++ ruby_1_8/ext/tk/lib/tk/validation.rb (revision 16410) @@ -50,7 +50,8 @@ key2class.each{|key, klass| if keys[key].kind_of?(Array) cmd, *args = keys[key] - keys[key] = klass.new(cmd, args.join(' ')) + #keys[key] = klass.new(cmd, args.join(' ')) + keys[key] = klass.new(cmd, *args) # elsif keys[key].kind_of?(Proc) || keys[key].kind_of?(Method) elsif TkComm._callback_entry?(keys[key]) keys[key] = klass.new(keys[key]) @@ -151,7 +152,8 @@ key2class.each{|key, klass| if keys[key].kind_of?(Array) cmd, *args = keys[key] - keys[key] = klass.new(cmd, args.join(' ')) + #keys[key] = klass.new(cmd, args.join(' ')) + keys[key] = klass.new(cmd, *args) # elsif keys[key].kind_of?(Proc) || keys[key].kind_of?(Method) elsif TkComm._callback_entry?(keys[key]) keys[key] = klass.new(keys[key]) @@ -249,6 +251,7 @@ nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -264,6 +267,7 @@ end inf } +=end _setup_subst_table(KEY_TBL, PROC_TBL); @@ -293,6 +297,7 @@ extra_args_tbl = klass._get_extra_args_tbl if args.compact.size > 0 + args.map!{|arg| klass._sym2subst(arg)} args = args.join(' ') keys = klass._get_subst_key(args) if cmd.kind_of?(String) Index: ruby_1_8/ext/tk/lib/tk/spinbox.rb =================================================================== --- ruby_1_8/ext/tk/lib/tk/spinbox.rb (revision 16409) +++ ruby_1_8/ext/tk/lib/tk/spinbox.rb (revision 16410) @@ -37,6 +37,7 @@ nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -52,6 +53,7 @@ end inf } +=end _setup_subst_table(KEY_TBL, PROC_TBL); Index: ruby_1_8/ext/tk/lib/tk/event.rb =================================================================== --- ruby_1_8/ext/tk/lib/tk/event.rb (revision 16409) +++ ruby_1_8/ext/tk/lib/tk/event.rb (revision 16410) @@ -352,6 +352,14 @@ nil ] + # [ <'%' subst-key str>, <proc type char>, <instance var (accessor) name>] + # the subst-key string will be converted to a bytecode (128+idx). + LONGKEY_TBL = [ + # for example, for %CTT and %CST subst-key on tkdnd-2.0 + # ['CTT', ?l, :drop_target_type], + # ['CST', ?l, :drop_source_type], + ] + # [ <proc type char>, <proc/method to convert tcl-str to ruby-obj>] PROC_TBL = [ [ ?n, TkComm.method(:num_or_str) ], @@ -371,6 +379,7 @@ nil ] +=begin # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) ) KEY_TBL.map!{|inf| if inf.kind_of?(Array) @@ -386,6 +395,7 @@ end inf } +=end # setup tables to be used by scan_args, _get_subst_key, _get_all_subst_keys # @@ -399,7 +409,8 @@ # ( which are Tcl strings ) to ruby objects based on the key string # that is generated by _get_subst_key() or _get_all_subst_keys(). # - _setup_subst_table(KEY_TBL, PROC_TBL); + _setup_subst_table(KEY_TBL, PROC_TBL) + # _setup_subst_table(KEY_TBL, LONGKEY_TBL, PROC_TBL) # if use longname-keys # # NOTE: The order of parameters which passed to callback procedure is @@ -447,6 +458,7 @@ extra_args_tbl = klass._get_extra_args_tbl if args.compact.size > 0 + args.map!{|arg| klass._sym2subst(arg)} args = args.join(' ') keys = klass._get_subst_key(args) Index: ruby_1_8/ext/tk/tkutil/tkutil.c =================================================================== --- ruby_1_8/ext/tk/tkutil/tkutil.c (revision 16409) +++ ruby_1_8/ext/tk/tkutil/tkutil.c (revision 16410) @@ -7,7 +7,7 @@ ************************************************/ -#define TKUTIL_RELEASE_DATE "2008-03-29" +#define TKUTIL_RELEASE_DATE "2008-05-14" #include "ruby.h" @@ -1073,11 +1073,13 @@ /*************************************/ +#define CBSUBST_TBL_MAX (256) struct cbsubst_info { - int size; - char *key; - char *type; - ID *ivar; + int full_subst_length; + int keylen[CBSUBST_TBL_MAX]; + unsigned char *key[CBSUBST_TBL_MAX]; + unsigned char type[CBSUBST_TBL_MAX]; + ID ivar[CBSUBST_TBL_MAX]; VALUE proc; VALUE aliases; }; @@ -1094,42 +1096,49 @@ subst_free(ptr) struct cbsubst_info *ptr; { + int i; + if (ptr) { - if (ptr->key != (char*)NULL) free(ptr->key); - if (ptr->type != (char*)NULL) free(ptr->type); - if (ptr->ivar != (ID*)NULL) free(ptr->ivar); - free(ptr); + for(i = 0; i < CBSUBST_TBL_MAX; i++) { + if (ptr->key[i] != (unsigned char *)NULL) free(ptr->key[i]); + } + free(ptr); } } -static void -cbsubst_init() +static struct cbsubst_info * +allocate_cbsubst_info() { - struct cbsubst_info *inf; - ID *ivar; - volatile VALUE proc, aliases; + struct cbsubst_info *inf; + volatile VALUE proc, aliases; + int idx; - inf = ALLOC(struct cbsubst_info); + inf = ALLOC(struct cbsubst_info); - inf->size = 0; + inf->full_subst_length = 0; - inf->key = ALLOC_N(char, 1); - inf->key[0] = '\0'; + for(idx = 0; idx < CBSUBST_TBL_MAX; idx++) { + inf->keylen[idx] = 0; + inf->key[idx] = (unsigned char *) NULL; + inf->type[idx] = '\0'; + inf->ivar[idx] = (ID) 0; + } - inf->type = ALLOC_N(char, 1); - inf->type[0] = '\0'; + proc = rb_hash_new(); + inf->proc = proc; - ivar = ALLOC_N(ID, 1); - inf->ivar = ivar; + aliases = rb_hash_new(); + inf->aliases = aliases; - proc = rb_hash_new(); - inf->proc = proc; + return inf; +} - aliases = rb_hash_new(); - inf->aliases = aliases; - +static void +cbsubst_init() +{ rb_const_set(cCB_SUBST, ID_SUBST_INFO, - Data_Wrap_Struct(cSUBST_INFO, subst_mark, subst_free, inf)); + Data_Wrap_Struct(cSUBST_INFO, subst_mark, subst_free, + allocate_cbsubst_info())); } static VALUE @@ -1139,24 +1148,29 @@ VALUE self; { struct cbsubst_info *inf; - int idx; + int idx, iv_idx; Data_Get_Struct(rb_const_get(rb_obj_class(self), ID_SUBST_INFO), struct cbsubst_info, inf); - for(idx = 0; idx < argc; idx++) { - rb_ivar_set(self, inf->ivar[idx], argv[idx]); + idx = 0; + for(iv_idx = 0; iv_idx < CBSUBST_TBL_MAX; iv_idx++) { + if ( inf->ivar[iv_idx] == (ID) 0 ) continue; + rb_ivar_set(self, inf->ivar[iv_idx], argv[idx++]); + if (idx >= argc) break; } return self; } - static VALUE cbsubst_ret_val(self, val) VALUE self; VALUE val; { + /* This method may be overwritten on some sub-classes. */ + /* This method is used for converting from ruby's callback-return-value */ + /* to tcl's value (e.g. validation procedure of entry widget). */ return val; } @@ -1217,6 +1231,59 @@ } static VALUE +cbsubst_sym_to_subst(self, sym) + VALUE self; + VALUE sym; +{ + struct cbsubst_info *inf; + const char *str; + unsigned char *buf, *ptr; + int idx, len; + ID id; + volatile VALUE ret; + + if (TYPE(sym) != T_SYMBOL) return sym; + + Data_Get_Struct(rb_const_get(self, ID_SUBST_INFO), + struct cbsubst_info, inf); + + if (!NIL_P(ret = rb_hash_aref(inf->aliases, sym))) { + str = rb_id2name(SYM2ID(ret)); + } else { + str = rb_id2name(SYM2ID(sym)); + } + + id = rb_intern(RSTRING_PTR(rb_str_cat2(rb_str_new2("@"), str))); + + for(idx = 0; idx < CBSUBST_TBL_MAX; idx++) { + if (inf->ivar[idx] == id) break; + } + if (idx >= CBSUBST_TBL_MAX) return sym; + + ptr = buf = ALLOC_N(char, inf->full_subst_length + 1); + + *(ptr++) = '%'; + + if (len = inf->keylen[idx]) { + /* longname */ + strncpy(ptr, inf->key[idx], len); + ptr += len; + } else { + /* single char */ + *(ptr++) = idx; + } + + *(ptr++) = ' '; + *(ptr++) = '\0'; + + ret = rb_str_new2(buf); + + free(buf); + + return ret; +} + +static VALUE cbsubst_get_subst_arg(argc, argv, self) int argc; VALUE *argv; @@ -1224,17 +1291,15 @@ { struct cbsubst_info *inf; const char *str; - char *buf, *ptr; - int i, j, len; + unsigned char *buf, *ptr; + int i, idx, len; ID id; volatile VALUE arg_sym, ret; Data_Get_Struct(rb_const_get(self, ID_SUBST_INFO), struct cbsubst_info, inf); - buf = ALLOC_N(char, 3*argc + 1); - ptr = buf; - len = strlen(inf->key); + ptr = buf = ALLOC_N(char, inf->full_subst_length + 1); for(i = 0; i < argc; i++) { switch(TYPE(argv[i])) { @@ -1256,17 +1321,25 @@ id = rb_intern(RSTRING_PTR(rb_str_cat2(rb_str_new2("@"), str))); - for(j = 0; j < len; j++) { - if (inf->ivar[j] == id) break; - } - - if (j >= len) { + for(idx = 0; idx < CBSUBST_TBL_MAX; idx++) { + if (inf->ivar[idx] == id) break; + } + if (idx >= CBSUBST_TBL_MAX) { rb_raise(rb_eArgError, "cannot find attribute :%s", str); } - *(ptr++) = '%'; - *(ptr++) = *(inf->key + j); - *(ptr++) = ' '; + *(ptr++) = '%'; + + if (len = inf->keylen[idx]) { + /* longname */ + strncpy(ptr, inf->key[idx], len); + ptr += len; + } else { + /* single char */ + *(ptr++) = idx; + } + + *(ptr++) = ' '; } *ptr = '\0'; @@ -1283,27 +1356,50 @@ VALUE self; VALUE str; { + struct cbsubst_info *inf; volatile VALUE list; volatile VALUE ret; - int i, len; - char *buf, *ptr; + VALUE keyval; + int i, len, keylen, idx; + unsigned char *buf, *ptr, *key; list = rb_funcall(cTclTkLib, ID_split_tklist, 1, str); - len = RARRAY_LEN(list); - buf = ALLOC_N(char, len + 1); + Data_Get_Struct(rb_const_get(self, ID_SUBST_INFO), + struct cbsubst_info, inf); + + ptr = buf = ALLOC_N(unsigned char, inf->full_subst_length + len + 1); + for(i = 0; i < len; i++) { - ptr = RSTRING_PTR(RARRAY_PTR(list)[i]); - if (*ptr == '%' && *(ptr + 2) == '\0') { - *(buf + i) = *(ptr + 1); - } else { - *(buf + i) = ' '; - } + keyval = RARRAY_PTR(list)[i]; + key = (unsigned char*)RSTRING_PTR(keyval); + if (*key == '%') { + if (*(key + 2) == '\0') { + /* single char */ + *(ptr++) = *(key + 1); + } else { + /* search longname-key */ + keylen = RSTRING_LEN(keyval) - 1; + for(idx = 0; idx < CBSUBST_TBL_MAX; idx++) { + if (inf->keylen[idx] != keylen) continue; + if (inf->key[idx][0] != *(key + 1)) continue; + if (strncmp(inf->key[idx], key + 1, keylen)) continue; + break; + } + if (idx < CBSUBST_TBL_MAX) { + *(ptr++) = (unsigned char)idx; + } else { + *(ptr++) = ' '; + } + } + } else { + *(ptr++) = ' '; + } } - *(buf + len) = '\0'; + *ptr = '\0'; - ret = rb_str_new2(buf); + ret = rb_str_new2((const char*)buf); free(buf); return ret; } @@ -1313,100 +1409,156 @@ VALUE self; { struct cbsubst_info *inf; - char *buf, *ptr; - int i, len; + unsigned char *buf, *ptr; + unsigned char *keys_buf, *keys_ptr; + int idx, len; volatile VALUE ret; Data_Get_Struct(rb_const_get(self, ID_SUBST_INFO), struct cbsubst_info, inf); - len = strlen(inf->key); - buf = ALLOC_N(char, 3*len + 1); - ptr = buf; - for(i = 0; i < len; i++) { - *(ptr++) = '%'; - *(ptr++) = *(inf->key + i); - *(ptr++) = ' '; + ptr = buf = ALLOC_N(unsigned char, inf->full_subst_length + 1); + keys_ptr = keys_buf = ALLOC_N(unsigned char, CBSUBST_TBL_MAX + 1); + + for(idx = 0; idx < CBSUBST_TBL_MAX; idx++) { + if (inf->ivar[idx] == (ID) 0) continue; + + *(keys_ptr++) = (unsigned char)idx; + + *(ptr++) = '%'; + + if (len = inf->keylen[idx]) { + /* longname */ + strncpy(ptr, inf->key[idx], len); + ptr += len; + } else { + /* single char */ + *(ptr++) = (unsigned char)idx; + } + + *(ptr++) = ' '; } - *(buf + 3*len) = '\0'; - ret = rb_ary_new3(2, rb_str_new2(inf->key), rb_str_new2(buf)); + *ptr = '\0'; + *keys_ptr = '\0'; + ret = rb_ary_new3(2, rb_str_new2(keys_buf), rb_str_new2((const char*)buf)); + free(buf); return ret; } static VALUE -cbsubst_table_setup(self, key_inf, proc_inf) - VALUE self; - VALUE key_inf; - VALUE proc_inf; +cbsubst_table_setup(argc, argv, self) + int argc; + VALUE *argv; + VALUE self; { - struct cbsubst_info *subst_inf; - int idx; - int len = RARRAY_LEN(key_inf); - int real_len = 0; - char *key = ALLOC_N(char, len + 1); - char *type = ALLOC_N(char, len + 1); - ID *ivar = ALLOC_N(ID, len + 1); - volatile VALUE proc = rb_hash_new(); - volatile VALUE aliases = rb_hash_new(); - volatile VALUE inf; + volatile VALUE key_inf; + volatile VALUE longkey_inf; + volatile VALUE proc_inf; + VALUE inf; + ID id; + struct cbsubst_info *subst_inf; + int idx, len; + unsigned char chr; - /* init */ - subst_inf = ALLOC(struct cbsubst_info); - /* subst_inf->size = len; */ - subst_inf->key = key; - subst_inf->type = type; - subst_inf->ivar = ivar; - subst_inf->proc = proc; - subst_inf->aliases = aliases; + /* accept (key_inf, proc_inf) or (key_inf, longkey_inf, procinf) */ + if (rb_scan_args(argc, argv, "21", &key_inf, &longkey_inf, &proc_inf) == 2) { + proc_inf = longkey_inf; + longkey_inf = rb_ary_new(); + } - /* - * keys : array of [subst, type, ivar] - * subst ==> char code - * type ==> char code - * ivar ==> symbol - */ - for(idx = 0; idx < len; idx++) { - inf = RARRAY_PTR(key_inf)[idx]; - if (TYPE(inf) != T_ARRAY) continue; - *(key + real_len) = NUM2CHR(RARRAY_PTR(inf)[0]); - *(type + real_len) = NUM2CHR(RARRAY_PTR(inf)[1]); + /* check the number of longkeys */ + if (RARRAY_LEN(longkey_inf) > 125 /* from 0x80 to 0xFD */) { + rb_raise(rb_eArgError, "too many longname-key definitions"); + } - *(ivar + real_len) - = rb_intern( - RSTRING_PTR( - rb_str_cat2(rb_str_new2("@"), - rb_id2name(SYM2ID(RARRAY_PTR(inf)[2]))) - ) - ); + /* init */ + subst_inf = allocate_cbsubst_info(); - rb_attr(self, SYM2ID(RARRAY_PTR(inf)[2]), 1, 0, Qtrue); - real_len++; + /* + * keys : array of [subst, type, ivar] + * subst ==> char code or string + * type ==> char code or string + * ivar ==> symbol + */ + len = RARRAY_LEN(key_inf); + for(idx = 0; idx < len; idx++) { + inf = RARRAY_PTR(key_inf)[idx]; + if (TYPE(inf) != T_ARRAY) continue; + + if (TYPE(RARRAY_PTR(inf)[0]) == T_STRING) { + chr = *(RSTRING_PTR(RARRAY_PTR(inf)[0])); + } else { + chr = NUM2CHR(RARRAY_PTR(inf)[0]); } - *(key + real_len) = '\0'; - *(type + real_len) = '\0'; - subst_inf->size = real_len; + if (TYPE(RARRAY_PTR(inf)[1]) == T_STRING) { + subst_inf->type[chr] = *(RSTRING_PTR(RARRAY_PTR(inf)[1])); + } else { + subst_inf->type[chr] = NUM2CHR(RARRAY_PTR(inf)[1]); + } - /* - * procs : array of [type, proc] - * type ==> char code - * proc ==> proc/method/obj (must respond to 'call') - */ - len = RARRAY_LEN(proc_inf); - for(idx = 0; idx < len; idx++) { - inf = RARRAY_PTR(proc_inf)[idx]; - if (TYPE(inf) != T_ARRAY) continue; - rb_hash_aset(proc, RARRAY_PTR(inf)[0], RARRAY_PTR(inf)[1]); + subst_inf->full_subst_length += 3; + + id = SYM2ID(RARRAY_PTR(inf)[2]); + subst_inf->ivar[chr] = rb_intern(RSTRING_PTR(rb_str_cat2(rb_str_new2("@"), rb_id2name(id)))); + + rb_attr(self, id, 1, 0, Qtrue); + } + + + /* + * longkeys : array of [name, type, ivar] + * name ==> longname key string + * type ==> char code or string + * ivar ==> symbol + */ + len = RARRAY_LEN(longkey_inf); + for(idx = 0; idx < len; idx++) { + inf = RARRAY_PTR(longkey_inf)[idx]; + if (TYPE(inf) != T_ARRAY) continue; + + chr = (unsigned char)(0x80 + idx); + subst_inf->keylen[chr] = RSTRING_LEN(RARRAY_PTR(inf)[0]); + subst_inf->key[chr] = strndup(RSTRING_PTR(RARRAY_PTR(inf)[0]), + RSTRING_LEN(RARRAY_PTR(inf)[0])); + if (TYPE(RARRAY_PTR(inf)[1]) == T_STRING) { + subst_inf->type[chr] = *(RSTRING_PTR(RARRAY_PTR(inf)[1])); + } else { + subst_inf->type[chr] = NUM2CHR(RARRAY_PTR(inf)[1]); } - rb_const_set(self, ID_SUBST_INFO, - Data_Wrap_Struct(cSUBST_INFO, subst_mark, - subst_free, subst_inf)); + subst_inf->full_subst_length += (subst_inf->keylen[chr] + 2); - return self; + id = SYM2ID(RARRAY_PTR(inf)[2]); + subst_inf->ivar[chr] = rb_intern(RSTRING_PTR(rb_str_cat2(rb_str_new2("@"), rb_id2name(id)))); + + rb_attr(self, id, 1, 0, Qtrue); + } + + /* + * procs : array of [type, proc] + * type ==> char code or string + * proc ==> proc/method/obj (must respond to 'call') + */ + len = RARRAY_LEN(proc_inf); + for(idx = 0; idx < len; idx++) { + inf = RARRAY_PTR(proc_inf)[idx]; + if (TYPE(inf) != T_ARRAY) continue; + rb_hash_aset(subst_inf->proc, + ((TYPE(RARRAY_PTR(inf)[0]) == T_STRING)? + INT2FIX(*(RSTRING_PTR(RARRAY_PTR(inf)[0]))) : + RARRAY_PTR(inf)[0]), + RARRAY_PTR(inf)[1]); + } + + rb_const_set(self, ID_SUBST_INFO, + Data_Wrap_Struct(cSUBST_INFO, subst_mark, + subst_free, subst_inf)); + + return self; } static VALUE @@ -1424,10 +1576,11 @@ { struct cbsubst_info *inf; int idx; - int len = RARRAY_LEN(val_ary); - char c; - char *ptr; - volatile VALUE dst = rb_ary_new2(len); + unsigned char *keyptr = (unsigned char*)RSTRING_PTR(arg_key); + int keylen = RSTRING_LEN(arg_key); + int vallen = RARRAY_LEN(val_ary); + unsigned char type_chr; + volatile VALUE dst = rb_ary_new2(vallen); volatile VALUE proc; int thr_crit_bup; VALUE old_gc; @@ -1440,26 +1593,25 @@ Data_Get_Struct(rb_const_get(self, ID_SUBST_INFO), struct cbsubst_info, inf); - for(idx = 0; idx < len; idx++) { - if (idx >= RSTRING_LEN(arg_key)) { - proc = Qnil; - } else if (*(RSTRING_PTR(arg_key) + idx) == ' ') { - proc = Qnil; - } else { - ptr = strchr(inf->key, *(RSTRING_PTR(arg_key) + idx)); - if (ptr == (char*)NULL) { - proc = Qnil; - } else { - c = *(inf->type + (ptr - inf->key)); - proc = rb_hash_aref(inf->proc, INT2FIX(c)); - } - } + for(idx = 0; idx < vallen; idx++) { + if (idx >= keylen) { + proc = Qnil; + } else if (*(keyptr + idx) == ' ') { + proc = Qnil; + } else { + if (type_chr = inf->type[*(keyptr + idx)]) { + proc = rb_hash_aref(inf->proc, INT2FIX((int)type_chr)); + } else { + proc = Qnil; + } + } - if (NIL_P(proc)) { - rb_ary_push(dst, RARRAY_PTR(val_ary)[idx]); - } else { - rb_ary_push(dst, rb_funcall(proc, ID_call, 1, RARRAY_PTR(val_ary)[idx])); - } + if (NIL_P(proc)) { + rb_ary_push(dst, RARRAY_PTR(val_ary)[idx]); + } else { + rb_ary_push(dst, rb_funcall(proc, ID_call, 1, + RARRAY_PTR(val_ary)[idx])); + } } if (old_gc == Qfalse) rb_gc_enable(); @@ -1543,6 +1695,8 @@ ID_SUBST_INFO = rb_intern("SUBST_INFO"); rb_define_singleton_method(cCB_SUBST, "ret_val", cbsubst_ret_val, 1); rb_define_singleton_method(cCB_SUBST, "scan_args", cbsubst_scan_args, 2); + rb_define_singleton_method(cCB_SUBST, "_sym2subst", + cbsubst_sym_to_subst, 1); rb_define_singleton_method(cCB_SUBST, "subst_arg", cbsubst_get_subst_arg, -1); rb_define_singleton_method(cCB_SUBST, "_get_subst_key", @@ -1550,7 +1704,7 @@ rb_define_singleton_method(cCB_SUBST, "_get_all_subst_keys", cbsubst_get_all_subst_keys, 0); rb_define_singleton_method(cCB_SUBST, "_setup_subst_table", - cbsubst_table_setup, 2); + cbsubst_table_setup, -1); rb_define_singleton_method(cCB_SUBST, "_get_extra_args_tbl", cbsubst_get_extra_args_tbl, 0); rb_define_singleton_method(cCB_SUBST, "_define_attribute_aliases", Index: ruby_1_8/ChangeLog =================================================================== --- ruby_1_8/ChangeLog (revision 16409) +++ ruby_1_8/ChangeLog (revision 16410) @@ -1,3 +1,35 @@ +Wed May 14 09:52:02 2008 Hidetoshi NAGAI <nagai@a...> + + * ext/tk/tkutil/tkutil.c: improve handling callback-subst-keys. + Now, support longnam-keys (e.g. '%CTT' on tkdnd-2.0; however, still + not support tkdnd-2.0 on tkextlib), and symbols of parameters (e.g. + :widget=>'%W', :keycode=>'%k', '%x'=>:x, '%X'=>:root_x, and so on; + those are attributes of event object). It means that Ruby/Tk accepts + not only "widget.bind(ev, '%W', '%k', ...){|w, k, ...| ... }", but + also "widget.bind(ev, :widget, :keycode, ...){|w, k, ...| ... }". + It is potentially incompatible, when user passes symbols to the + arguments of the callback block (the block receives the symbols as + strings). I think that is very rare case (probably, used by Ruby/Tk + experts only). When causes such trouble, please give strings instead + of such symbol parameters (e.g. call Symbol#to_s method). + + * ext/tk/lib/tk/event.rb, ext/tk/lib/tk/validation.rb, + ext/tk/lib/tkextlib/blt/treeview.rb, + ext/tk/lib/tkextlib/winico/winico.rb: ditto. + + * ext/tk/tkutil/tkutil.c: strings are available on subst_tables on + TkUtil::CallbackSubst class (it is useful on Ruby 1.9). + + * ext/tk/lib/tk/spinbox.rb, ext/tk/lib/tkextlib/iwidgets/hierarchy.rb, + ext/tk/lib/tkextlib/iwidgets/spinner.rb, + ext/tk/lib/tkextlib/iwidgets/entryfield.rb, + ext/tk/lib/tkextlib/iwidgets/calendar.rb, + ext/tk/lib/tkextlib/blt/dragdrop.rb, + ext/tk/lib/tkextlib/tkDND/tkdnd.rb, + ext/tk/lib/tkextlib/treectrl/tktreectrl.rb, + ext/tk/lib/tkextlib/tktable/tktable.rb: disable code piece became + unnecessary by reason of the changes of ext/tk/tkutil/tkutil.c. + Tue May 13 15:10:50 2008 Akinori MUSHA <knu@i...> * enumerator.c: Update rdoc. -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/