module TkComm - tk-0.2.0 Documentation

# module TkComm

define TkComm module (step 1: basic functions)

define TkComm module (step 2: event binding)

### Constants

GET_CONFIGINFO_AS_ARRAY

  GET_CONFIGINFO_AS_ARRAY = false => returns a Hash { opt =>val, … }

      true  => returns an Array [[opt,val], ... ]

  val is a list which includes resource info.

GET_CONFIGINFOwoRES_AS_ARRAY

  for configinfo without resource info; list of [opt, value] pair

      false => returns a Hash { opt=>val, ... }
      true  => returns an Array [[opt,val], ... ]

TkExtlibAutoloadModule
Tk_CMDTBL

  for backward compatibility

Tk_IDs

  Tk_CMDTBL = {} Tk_WINDOWS = {}

Tk_WINDOWS
USE_TCLs_LIST_FUNCTIONS
WidgetClassNames

### Public Class Methods

_at(x,y=nil) click to toggle source

    # File lib/tk.rb, line 222
    def _at(x,y=nil)
      if y
        "@#{Integer(x)},#{Integer(y)}"
      else
        "@#{Integer(x)}"
      end
    end

_callback_entry?(obj) click to toggle source

    # File lib/tk.rb, line 690
    def _callback_entry?(obj)
      obj.kind_of?(Proc) || obj.kind_of?(Method) || obj.kind_of?(TkCallbackEntry)
    end

_callback_entry_class?(cls) click to toggle source

    # File lib/tk.rb, line 684
    def _callback_entry_class?(cls)
      cls <= Proc || cls <= Method || cls <= TkCallbackEntry
    end

_curr_cmd_id() click to toggle source

# begin¶ ↑

    ### --> definition is moved to TkUtil module
    def _conv_args(args, enc_mode, *src_args)
      conv_args = []
      src_args.each{|arg|
        conv_args << _get_eval_string(arg, enc_mode) unless arg == None
        # if arg.kind_of?(Hash)
        # arg.each{|k, v|
        #   args << '-' + k.to_s
        #   args << _get_eval_string(v, enc_mode)
        # }
        # elsif arg != None
        #   args << _get_eval_string(arg, enc_mode)
        # end
      }
      args + conv_args
    end
    private :_conv_args

# end¶ ↑

    # File lib/tk.rb, line 819
    def _curr_cmd_id
      #id = format("c%.4d", Tk_IDs[0])
      id = "c" + TkCore::INTERP._ip_id_ + TkComm::Tk_IDs[0]
    end

_fromUTF8(str, encoding = nil) click to toggle source

    # File lib/tk.rb, line 678
    def _fromUTF8(str, encoding = nil)
      TkCore::INTERP._fromUTF8(str, encoding)
    end

_genobj_for_tkwidget(path) click to toggle source

    # File lib/tk.rb, line 106
      def _genobj_for_tkwidget(path)
        return TkRoot.new if path == '.'

        begin
          #tk_class = TkCore::INTERP._invoke('winfo', 'class', path)
          tk_class = Tk.ip_invoke_without_enc('winfo', 'class', path)
        rescue
          return path
        end

        if ruby_class = WidgetClassNames[tk_class]
          ruby_class_name = ruby_class.name
          # gen_class_name = ruby_class_name + 'GeneratedOnTk'
          gen_class_name = ruby_class_name
          classname_def = ''
        else # ruby_class == nil
          if Tk.const_defined?(tk_class)
            Tk.const_get(tk_class)  # auto_load
            ruby_class = WidgetClassNames[tk_class]
          end

          unless ruby_class
            mods = TkExtlibAutoloadModule.find_all{|m| m.const_defined?(tk_class)}
            mods.each{|mod|
              begin
                mod.const_get(tk_class)  # auto_load
                break if (ruby_class = WidgetClassNames[tk_class])
              rescue LoadError
                # ignore load error
              end
            }
          end

          unless ruby_class
            std_class = 'Tk' << tk_class
            if Object.const_defined?(std_class)
              Object.const_get(std_class)  # auto_load
              ruby_class = WidgetClassNames[tk_class]
            end
          end

          unless ruby_class
            if Tk.const_defined?('TOPLEVEL_ALIASES') &&
                Tk::TOPLEVEL_ALIASES.const_defined?(std_class)
              Tk::TOPLEVEL_ALIASES.const_get(std_class)  # auto_load
              ruby_class = WidgetClassNames[tk_class]
            end
          end

          if ruby_class
            # found
            ruby_class_name = ruby_class.name
            gen_class_name = ruby_class_name
            classname_def = ''
          else
            # unknown
            ruby_class_name = 'TkWindow'
            gen_class_name = 'TkWidget_' + tk_class
            classname_def = "WidgetClassName = '#{tk_class}'.freeze"
          end
        end

    ###################################
    =begin
        if ruby_class = WidgetClassNames[tk_class]
          ruby_class_name = ruby_class.name
          # gen_class_name = ruby_class_name + 'GeneratedOnTk'
          gen_class_name = ruby_class_name
          classname_def = ''
        else
          mod = TkExtlibAutoloadModule.find{|m| m.const_defined?(tk_class)}
          if mod
            ruby_class_name = mod.name + '::' + tk_class
            gen_class_name = ruby_class_name
            classname_def = ''
          elsif Object.const_defined?('Tk' + tk_class)
            ruby_class_name = 'Tk' + tk_class
            # gen_class_name = ruby_class_name + 'GeneratedOnTk'
            gen_class_name = ruby_class_name
            classname_def = ''
          else
            ruby_class_name = 'TkWindow'
            # gen_class_name = ruby_class_name + tk_class + 'GeneratedOnTk'
            gen_class_name = 'TkWidget_' + tk_class
            classname_def = "WidgetClassName = '#{tk_class}'.freeze"
          end
        end
    =end

    =begin
        unless Object.const_defined? gen_class_name
          Object.class_eval "class #{gen_class_name}<#{ruby_class_name}
                               #{classname_def}
                             end"
        end
        Object.class_eval "#{gen_class_name}.new('widgetname'=>'#{path}',
                                                 'without_creating'=>true)"
    =end
        base = Object
        gen_class_name.split('::').each{|klass|
          next if klass == ''
          if base.const_defined?(klass)
            base = base.class_eval klass
          else
            base = base.class_eval "class #{klass}<#{ruby_class_name}
                                      #{classname_def}
                                    end
                                    #{klass}"
          end
        }
        base.class_eval "#{gen_class_name}.new('widgetname'=>'#{path}',
                                               'without_creating'=>true)"
      end

_next_cmd_id() click to toggle source

    # File lib/tk.rb, line 823
    def _next_cmd_id
      TkComm::Tk_IDs.mutex.synchronize{
        id = _curr_cmd_id
        #Tk_IDs[0] += 1
        TkComm::Tk_IDs[0].succ!
        id
      }
    end

_toUTF8(str, encoding = nil) click to toggle source

    # File lib/tk.rb, line 675
    def _toUTF8(str, encoding = nil)
      TkCore::INTERP._toUTF8(str, encoding)
    end

array2tk_list(ary, enc=nil) click to toggle source

    # File lib/tk.rb, line 356
    def array2tk_list(ary, enc=nil)
      return "" if ary.size == 0

      sys_enc = TkCore::INTERP.encoding
      sys_enc = TclTkLib.encoding_system unless sys_enc

      dst_enc = (enc == nil)? sys_enc: enc

      dst = ary.collect{|e|
        if e.kind_of? Array
          s = array2tk_list(e, enc)
        elsif e.kind_of? Hash
          tmp_ary = []
          #e.each{|k,v| tmp_ary << k << v }
          e.each{|k,v| tmp_ary << "-#{_get_eval_string(k)}" << v }
          s = array2tk_list(tmp_ary, enc)
        else
          s = _get_eval_string(e, enc)
        end

        if dst_enc != true && dst_enc != false
          if (s_enc = s.instance_variable_get(:@encoding))
            s_enc = s_enc.to_s
          elsif TkCore::WITH_ENCODING
            s_enc = s.encoding.name
          else
            s_enc = sys_enc
          end
          dst_enc = true if s_enc != dst_enc
        end

        s
      }

      if sys_enc && dst_enc
        dst.map!{|s| _toUTF8(s)}
        ret = TkCore::INTERP._merge_tklist(*dst)
        if TkCore::WITH_ENCODING
          if dst_enc.kind_of?(String)
            ret = _fromUTF8(ret, dst_enc)
            ret.force_encoding(dst_enc)
          else
            ret.force_encoding('utf-8')
          end
        else # without encoding
          if dst_enc.kind_of?(String)
            ret = _fromUTF8(ret, dst_enc)
            ret.instance_variable_set(:@encoding, dst_enc)
          else
            ret.instance_variable_set(:@encoding, 'utf-8')
          end
        end
        ret
      else
        TkCore::INTERP._merge_tklist(*dst)
      end
    end

bool(p1) click to toggle source

/

    static VALUE
    tcl2rb_bool(self, value)
        VALUE self;
        VALUE value;
    {
        if (RB_TYPE_P(value, T_FIXNUM)) {
            if (NUM2INT(value) == 0) {
                return Qfalse;
            } else {
                return Qtrue;
            }
        }

        if (value == Qtrue || value == Qfalse) {
            return value;
        }

        rb_check_type(value, T_STRING);

        value = rb_funcall(value, ID_downcase, 0);

        if (RSTRING_PTR(value) == (char*)NULL) return Qnil;

        if (RSTRING_PTR(value)[0] == '\0'
            || strcmp(RSTRING_PTR(value), "0") == 0
            || strcmp(RSTRING_PTR(value), "no") == 0
            || strcmp(RSTRING_PTR(value), "off") == 0
            || strcmp(RSTRING_PTR(value), "false") == 0) {
            return Qfalse;
        } else {
            return Qtrue;
        }
    }

image_obj(val) click to toggle source

    # File lib/tk.rb, line 614
    def image_obj(val)
      if val =~ /^i(_\d+_)?\d+$/
        TkImage::Tk_IMGTBL.mutex.synchronize{
          TkImage::Tk_IMGTBL[val]? TkImage::Tk_IMGTBL[val] : val
        }
      else
        val
      end
    end

install_cmd(cmd, local_cmdtbl=nil) click to toggle source

    # File lib/tk.rb, line 834
    def TkComm.install_cmd(cmd, local_cmdtbl=nil)
      return '' if cmd == ''
      begin
        ns = TkCore::INTERP._invoke_without_enc('namespace', 'current')
        ns = nil if ns == '::' # for backward compatibility
      rescue
        # probably, Tcl7.6
        ns = nil
      end
      id = _next_cmd_id
      #Tk_CMDTBL[id] = cmd
      if cmd.kind_of?(TkCallbackEntry)
        TkCore::INTERP.tk_cmd_tbl[id] = cmd
      else
        TkCore::INTERP.tk_cmd_tbl[id] = TkCore::INTERP.get_cb_entry(cmd)
      end
      @cmdtbl = [] unless defined? @cmdtbl
      TkUtil.untrust(@cmdtbl) unless @cmdtbl.tainted?
      @cmdtbl.push id

      if local_cmdtbl && local_cmdtbl.kind_of?(Array)
        begin
          local_cmdtbl << id
        rescue Exception
          # ignore
        end
      end

      #return Kernel.format("rb_out %s", id);
      if ns
        'rb_out' << TkCore::INTERP._ip_id_ << ' ' << ns << ' ' << id
      else
        'rb_out' << TkCore::INTERP._ip_id_ << ' ' << id
      end
    end

list(val, depth=0, enc=true) click to toggle source

# begin¶ ↑

    ### --> definition is moved to TkUtil module
    def bool(val)
      case val
      when "1", 1, 'yes', 'true'
        true
      else
        false
      end
    end

    def number(val)
      case val
      when /^-?\d+$/
        val.to_i
      when /^-?\d+\.?\d*(e[-+]?\d+)?$/
        val.to_f
      else
        fail(ArgumentError, "invalid value for Number:'#{val}'")
      end
    end
    def string(val)
      if val == "{}"
        ''
      elsif val[0] == ?{ && val[-1] == ?}
        val[1..-2]
      else
        val
      end
    end
    def num_or_str(val)
      begin
        number(val)
      rescue ArgumentError
        string(val)
      end
    end

# end¶ ↑

    # File lib/tk.rb, line 599
    def list(val, depth=0, enc=true)
      tk_split_list(val, depth, enc, enc)
    end

num_or_nil(p1) click to toggle source

    static VALUE
    tcl2rb_num_or_nil(self, value)
        VALUE self;
        VALUE value;
    {
        rb_check_type(value, T_STRING);

        if (RSTRING_LEN(value) == 0) return Qnil;

        return tkstr_to_number(value);
    }

num_or_str(p1) click to toggle source

    static VALUE
    tcl2rb_num_or_str(self, value)
        VALUE self;
        VALUE value;
    {
        rb_check_type(value, T_STRING);

        if (RSTRING_PTR(value) == (char*)NULL) return rb_tainted_str_new2("");

        return rb_rescue2(tkstr_to_number, value,
                          tkstr_to_str, value,
                          rb_eArgError, 0);
    }

number(p1) click to toggle source

    static VALUE
    tcl2rb_number(self, value)
        VALUE self;
        VALUE value;
    {
        return tkstr_to_number(value);
    }

procedure(val) click to toggle source

    # File lib/tk.rb, line 623
      def procedure(val)
    =begin
        if val =~ /^rb_out\S* (c(_\d+_)?\d+)/
          #Tk_CMDTBL[$1]
          #TkCore::INTERP.tk_cmd_tbl[$1]
          TkCore::INTERP.tk_cmd_tbl[$1].cmd
    =end
        if val =~ /rb_out\S*(?:\s+(::\S*|[{](::.*)[}]|["](::.*)["]))? (c(_\d+_)?(\d+))/
          return TkCore::INTERP.tk_cmd_tbl[$4].cmd
        else
          #nil
          val
        end
      end

simplelist(val, src_enc=true, dst_enc=true) click to toggle source

    # File lib/tk.rb, line 602
    def simplelist(val, src_enc=true, dst_enc=true)
      tk_split_simplelist(val, src_enc, dst_enc)
    end

slice_ary(ary, size) { |sub_ary| ... } click to toggle source

    # File lib/tk.rb, line 643
    def slice_ary(ary, size)
      sliced = []
      wk_ary = ary.dup
      until wk_ary.size.zero?
        sub_ary = []
        size.times{ sub_ary << wk_ary.shift }
        yield(sub_ary) if block_given?
        sliced << sub_ary
      end
      (block_given?)? ary: sliced
    end

string(p1) click to toggle source

    static VALUE
    tcl2rb_string(self, value)
        VALUE self;
        VALUE value;
    {
        rb_check_type(value, T_STRING);

        if (RSTRING_PTR(value) == (char*)NULL) return rb_tainted_str_new2("");

        return tkstr_to_str(value);
    }

tk_tcl2ruby(val, enc_mode = false, listobj = true) click to toggle source

    # File lib/tk.rb, line 231
      def tk_tcl2ruby(val, enc_mode = false, listobj = true)
    =begin
        if val =~ /^rb_out\S* (c(_\d+_)?\d+)/
          #return Tk_CMDTBL[$1]
          return TkCore::INTERP.tk_cmd_tbl[$1]
          #cmd_obj = TkCore::INTERP.tk_cmd_tbl[$1]
          #if cmd_obj.kind_of?(Proc) || cmd_obj.kind_of?(Method)
          #  cmd_obj
          #else
          #  cmd_obj.cmd
          #end
        end
    =end
        if val =~ /rb_out\S*(?:\s+(::\S*|[{](::.*)[}]|["](::.*)["]))? (c(_\d+_)?(\d+))/
          return TkCore::INTERP.tk_cmd_tbl[$4]
        end
        #if val.include? ?\s
        #  return val.split.collect{|v| tk_tcl2ruby(v)}
        #end
        case val
        when /\A@font\S+\z/
          TkFont.get_obj(val)
        when /\A-?\d+\z/
          val.to_i
        when /\A\.\S*\z/
          #Tk_WINDOWS[val] ? Tk_WINDOWS[val] : _genobj_for_tkwidget(val)
          TkCore::INTERP.tk_windows[val]?
               TkCore::INTERP.tk_windows[val] : _genobj_for_tkwidget(val)
        when /\Ai(_\d+_)?\d+\z/
          TkImage::Tk_IMGTBL.mutex.synchronize{
            TkImage::Tk_IMGTBL[val]? TkImage::Tk_IMGTBL[val] : val
          }
        when /\A-?\d+\.?\d*(e[-+]?\d+)?\z/
          val.to_f
        when /\\ /
          val.gsub(/\\ /, ' ')
        when /[^\\] /
          if listobj
            #tk_split_escstr(val).collect{|elt|
            #  tk_tcl2ruby(elt, enc_mode, listobj)
            #}
            val = _toUTF8(val) unless enc_mode
            tk_split_escstr(val, false, false).collect{|elt|
              tk_tcl2ruby(elt, true, listobj)
            }
          elsif enc_mode
            _fromUTF8(val)
          else
            val
          end
        else
          if enc_mode
            _fromUTF8(val)
          else
            val
          end
        end
      end

uninstall_cmd(id, local_cmdtbl=nil) click to toggle source

    # File lib/tk.rb, line 869
    def TkComm.uninstall_cmd(id, local_cmdtbl=nil)
      #id = $1 if /rb_out\S* (c(_\d+_)?\d+)/ =~ id
      id = $4 if id =~ /rb_out\S*(?:\s+(::\S*|[{](::.*)[}]|["](::.*)["]))? (c(_\d+_)?(\d+))/

      if local_cmdtbl && local_cmdtbl.kind_of?(Array)
        begin
          local_cmdtbl.delete(id)
        rescue Exception
          # ignore
        end
      end
      @cmdtbl.delete(id)

      #Tk_CMDTBL.delete(id)
      TkCore::INTERP.tk_cmd_tbl.delete(id)
    end

window(val) click to toggle source

    # File lib/tk.rb, line 605
    def window(val)
      if val =~ /^\./
        #Tk_WINDOWS[val]? Tk_WINDOWS[val] : _genobj_for_tkwidget(val)
        TkCore::INTERP.tk_windows[val]?
             TkCore::INTERP.tk_windows[val] : _genobj_for_tkwidget(val)
      else
        nil
      end
    end

### Private Class Methods

tk_split_escstr(str, src_enc=true, dst_enc=true) click to toggle source

use Tcl function version of split_list

    # File lib/tk.rb, line 303
    def tk_split_escstr(str, src_enc=true, dst_enc=true)
      str = _toUTF8(str) if src_enc
      if dst_enc
        TkCore::INTERP._split_tklist(str).map!{|s| _fromUTF8(s)}
      else
        TkCore::INTERP._split_tklist(str)
      end
    end

tk_split_list(str, depth=0, src_enc=true, dst_enc=true) click to toggle source

    # File lib/tk.rb, line 332
    def tk_split_list(str, depth=0, src_enc=true, dst_enc=true)
      return [] if str == ""
      str = _toUTF8(str) if src_enc
      TkCore::INTERP._split_tklist(str).map!{|token|
        tk_split_sublist(token, depth - 1, false, dst_enc)
      }
    end

tk_split_simplelist(str, src_enc=true, dst_enc=true) click to toggle source

    # File lib/tk.rb, line 340
    def tk_split_simplelist(str, src_enc=true, dst_enc=true)
      #lst = TkCore::INTERP._split_tklist(str)
      #if (lst.size == 1 && lst =~ /^\{.*\}$/)
      #  TkCore::INTERP._split_tklist(str[1..-2])
      #else
      #  lst
      #end

      str = _toUTF8(str) if src_enc
      if dst_enc
        TkCore::INTERP._split_tklist(str).map!{|s| _fromUTF8(s)}
      else
        TkCore::INTERP._split_tklist(str)
      end
    end

tk_split_sublist(str, depth=-1, src_enc=true, dst_enc=true) click to toggle source

    # File lib/tk.rb, line 312
    def tk_split_sublist(str, depth=-1, src_enc=true, dst_enc=true)
      # return [] if str == ""
      # list = TkCore::INTERP._split_tklist(str)
      str = _toUTF8(str) if src_enc

      if depth == 0
        return "" if str == ""
        list = [str]
      else
        return [] if str == ""
        list = TkCore::INTERP._split_tklist(str)
      end
      if list.size == 1
        # tk_tcl2ruby(list[0], nil, false)
        tk_tcl2ruby(list[0], dst_enc, false)
      else
        list.collect{|token| tk_split_sublist(token, depth - 1, false, dst_enc)}
      end
    end

### Public Instance Methods

bind(tagOrClass, context, *args) click to toggle source

def bind(tagOrClass, context, cmd=Proc.new, *args)

    _bind(["bind", tagOrClass], context, cmd, *args)
    tagOrClass

end

    # File lib/tk.rb, line 1077
    def bind(tagOrClass, context, *args)
      # if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
      if TkComm._callback_entry?(args[0]) || !block_given?
        cmd = args.shift
      else
        cmd = Proc.new
      end
      _bind(["bind", tagOrClass], context, cmd, *args)
      tagOrClass
    end

bind_all(context, *args) click to toggle source

def #bind_all(context, cmd=Proc.new, *args)

    _bind(['bind', 'all'], context, cmd, *args)
    TkBindTag::ALL

end

    # File lib/tk.rb, line 1116
    def bind_all(context, *args)
      # if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
      if TkComm._callback_entry?(args[0]) || !block_given?
        cmd = args.shift
      else
        cmd = Proc.new
      end
      _bind(['bind', 'all'], context, cmd, *args)
      TkBindTag::ALL
    end

bind_append(tagOrClass, context, *args) click to toggle source

def #bind_append(tagOrClass, context, cmd=Proc.new, *args)

    _bind_append(["bind", tagOrClass], context, cmd, *args)
    tagOrClass

end

    # File lib/tk.rb, line 1092
    def bind_append(tagOrClass, context, *args)
      # if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
      if TkComm._callback_entry?(args[0]) || !block_given?
        cmd = args.shift
      else
        cmd = Proc.new
      end
      _bind_append(["bind", tagOrClass], context, cmd, *args)
      tagOrClass
    end

bind_append_all(context, *args) click to toggle source

def #bind_append_all(context, cmd=Proc.new, *args)

    _bind_append(['bind', 'all'], context, cmd, *args)
    TkBindTag::ALL

end

    # File lib/tk.rb, line 1131
    def bind_append_all(context, *args)
      # if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
      if TkComm._callback_entry?(args[0]) || !block_given?
        cmd = args.shift
      else
        cmd = Proc.new
      end
      _bind_append(['bind', 'all'], context, cmd, *args)
      TkBindTag::ALL
    end

bind_remove(tagOrClass, context) click to toggle source

    # File lib/tk.rb, line 1103
    def bind_remove(tagOrClass, context)
      _bind_remove(['bind', tagOrClass], context)
      tagOrClass
    end

bind_remove_all(context) click to toggle source

    # File lib/tk.rb, line 1142
    def bind_remove_all(context)
      _bind_remove(['bind', 'all'], context)
      TkBindTag::ALL
    end

bindinfo(tagOrClass, context=nil) click to toggle source

    # File lib/tk.rb, line 1108
    def bindinfo(tagOrClass, context=nil)
      _bindinfo(['bind', tagOrClass], context)
    end

bindinfo_all(context=nil) click to toggle source

    # File lib/tk.rb, line 1147
    def bindinfo_all(context=nil)
      _bindinfo(['bind', 'all'], context)
    end

install_cmd(cmd) click to toggle source

private :install_cmd, :uninstall_cmd module_function :install_cmd, :uninstall_cmd

    # File lib/tk.rb, line 887
    def install_cmd(cmd)
      TkComm.install_cmd(cmd, @cmdtbl)
    end

mutex() click to toggle source

    # File lib/tk.rb, line 62
    def mutex; @mutex; end

subst(str, *opts) click to toggle source

    # File lib/tk.rb, line 666
    def subst(str, *opts)
      # opts := :nobackslashes | :nocommands | novariables
      tk_call('subst',
              *(opts.collect{|opt|
                  opt = opt.to_s
                  (opt[0] == ?-)? opt: '-' << opt
                } << str))
    end

uninstall_cmd(id) click to toggle source

    # File lib/tk.rb, line 890
    def uninstall_cmd(id)
      TkComm.uninstall_cmd(id, @cmdtbl)
    end

### Private Instance Methods

_at(x,y=nil) click to toggle source

    # File lib/tk.rb, line 222
    def _at(x,y=nil)
      if y
        "@#{Integer(x)},#{Integer(y)}"
      else
        "@#{Integer(x)}"
      end
    end

_bind(what, context, cmd, *args) click to toggle source

    # File lib/tk.rb, line 992
    def _bind(what, context, cmd, *args)
      _bind_core('', what, context, cmd, *args)
    end

_bind_append(what, context, cmd, *args) click to toggle source

    # File lib/tk.rb, line 996
    def _bind_append(what, context, cmd, *args)
      _bind_core('+', what, context, cmd, *args)
    end

_bind_append_for_event_class(klass, what, context, cmd, *args) click to toggle source

    # File lib/tk.rb, line 1055
    def _bind_append_for_event_class(klass, what, context, cmd, *args)
      _bind_core_for_event_class(klass, '+', what, context, cmd, *args)
    end

_bind_core(mode, what, context, cmd, *args) click to toggle source

    # File lib/tk.rb, line 981
    def _bind_core(mode, what, context, cmd, *args)
      id = install_bind(cmd, *args) if cmd
      begin
        tk_call_without_enc(*(what + ["<#{tk_event_sequence(context)}>",
                                mode + id]))
      rescue
        uninstall_cmd(id) if cmd
        fail
      end
    end

_bind_core_for_event_class(klass, mode, what, context, cmd, *args) click to toggle source

    # File lib/tk.rb, line 1040
    def _bind_core_for_event_class(klass, mode, what, context, cmd, *args)
      id = install_bind_for_event_class(klass, cmd, *args) if cmd
      begin
        tk_call_without_enc(*(what + ["<#{tk_event_sequence(context)}>",
                                mode + id]))
      rescue
        uninstall_cmd(id) if cmd
        fail
      end
    end

_bind_for_event_class(klass, what, context, cmd, *args) click to toggle source

    # File lib/tk.rb, line 1051
    def _bind_for_event_class(klass, what, context, cmd, *args)
      _bind_core_for_event_class(klass, '', what, context, cmd, *args)
    end

_bind_remove(what, context) click to toggle source

    # File lib/tk.rb, line 1000
    def _bind_remove(what, context)
      tk_call_without_enc(*(what + ["<#{tk_event_sequence(context)}>", '']))
    end

_bind_remove_for_event_class(klass, what, context) click to toggle source

    # File lib/tk.rb, line 1059
    def _bind_remove_for_event_class(klass, what, context)
      _bind_remove(what, context)
    end

_bindinfo(what, context=nil) click to toggle source

    # File lib/tk.rb, line 1004
      def _bindinfo(what, context=nil)
        if context
          if TkCore::WITH_RUBY_VM  ### Ruby 1.9 !!!!
            enum_obj = tk_call_without_enc(*what+["<#{tk_event_sequence(context)}>"]).each_line
          else
            enum_obj = tk_call_without_enc(*what+["<#{tk_event_sequence(context)}>"])
          end
          enum_obj.collect {|cmdline|
    =begin
            if cmdline =~ /^rb_out\S* (c(?:_\d+_)?\d+)\s+(.*)$/
              #[Tk_CMDTBL[$1], $2]
              [TkCore::INTERP.tk_cmd_tbl[$1], $2]
    =end
            if cmdline =~ /rb_out\S*(?:\s+(::\S*|[{](::.*)[}]|["](::.*)["]))? (c(_\d+_)?(\d+))/
              [TkCore::INTERP.tk_cmd_tbl[$4], $5]
            else
              cmdline
            end
          }
        else
          tk_split_simplelist(tk_call_without_enc(*what)).collect!{|seq|
            l = seq.scan(/<*[^<>]+>*/).collect!{|subseq|
              case (subseq)
              when /^<<[^<>]+>>$/
                TkVirtualEvent.getobj(subseq[1..-2])
              when /^<[^<>]+>$/
                subseq[1..-2]
              else
                subseq.split('')
              end
            }.flatten
            (l.size == 1) ? l[0] : l
          }
        end
      end

_bindinfo_for_event_class(klass, what, context=nil) click to toggle source

    # File lib/tk.rb, line 1063
    def _bindinfo_for_event_class(klass, what, context=nil)
      _bindinfo(what, context)
    end

_callback_entry?(obj) click to toggle source

    # File lib/tk.rb, line 690
    def _callback_entry?(obj)
      obj.kind_of?(Proc) || obj.kind_of?(Method) || obj.kind_of?(TkCallbackEntry)
    end

_callback_entry_class?(cls) click to toggle source

    # File lib/tk.rb, line 684
    def _callback_entry_class?(cls)
      cls <= Proc || cls <= Method || cls <= TkCallbackEntry
    end

_curr_cmd_id() click to toggle source

# begin¶ ↑

    ### --> definition is moved to TkUtil module
    def _conv_args(args, enc_mode, *src_args)
      conv_args = []
      src_args.each{|arg|
        conv_args << _get_eval_string(arg, enc_mode) unless arg == None
        # if arg.kind_of?(Hash)
        # arg.each{|k, v|
        #   args << '-' + k.to_s
        #   args << _get_eval_string(v, enc_mode)
        # }
        # elsif arg != None
        #   args << _get_eval_string(arg, enc_mode)
        # end
      }
      args + conv_args
    end
    private :_conv_args

# end¶ ↑

    # File lib/tk.rb, line 819
    def _curr_cmd_id
      #id = format("c%.4d", Tk_IDs[0])
      id = "c" + TkCore::INTERP._ip_id_ + TkComm::Tk_IDs[0]
    end

_epath(win) click to toggle source

    # File lib/tk.rb, line 944
    def _epath(win)
      if win.kind_of?(TkObject)
        win.epath
      elsif win.respond_to?(:epath)
        win.epath
      else
        win
      end
    end

_fromUTF8(str, encoding = nil) click to toggle source

    # File lib/tk.rb, line 678
    def _fromUTF8(str, encoding = nil)
      TkCore::INTERP._fromUTF8(str, encoding)
    end

_genobj_for_tkwidget(path) click to toggle source

    # File lib/tk.rb, line 106
      def _genobj_for_tkwidget(path)
        return TkRoot.new if path == '.'

        begin
          #tk_class = TkCore::INTERP._invoke('winfo', 'class', path)
          tk_class = Tk.ip_invoke_without_enc('winfo', 'class', path)
        rescue
          return path
        end

        if ruby_class = WidgetClassNames[tk_class]
          ruby_class_name = ruby_class.name
          # gen_class_name = ruby_class_name + 'GeneratedOnTk'
          gen_class_name = ruby_class_name
          classname_def = ''
        else # ruby_class == nil
          if Tk.const_defined?(tk_class)
            Tk.const_get(tk_class)  # auto_load
            ruby_class = WidgetClassNames[tk_class]
          end

          unless ruby_class
            mods = TkExtlibAutoloadModule.find_all{|m| m.const_defined?(tk_class)}
            mods.each{|mod|
              begin
                mod.const_get(tk_class)  # auto_load
                break if (ruby_class = WidgetClassNames[tk_class])
              rescue LoadError
                # ignore load error
              end
            }
          end

          unless ruby_class
            std_class = 'Tk' << tk_class
            if Object.const_defined?(std_class)
              Object.const_get(std_class)  # auto_load
              ruby_class = WidgetClassNames[tk_class]
            end
          end

          unless ruby_class
            if Tk.const_defined?('TOPLEVEL_ALIASES') &&
                Tk::TOPLEVEL_ALIASES.const_defined?(std_class)
              Tk::TOPLEVEL_ALIASES.const_get(std_class)  # auto_load
              ruby_class = WidgetClassNames[tk_class]
            end
          end

          if ruby_class
            # found
            ruby_class_name = ruby_class.name
            gen_class_name = ruby_class_name
            classname_def = ''
          else
            # unknown
            ruby_class_name = 'TkWindow'
            gen_class_name = 'TkWidget_' + tk_class
            classname_def = "WidgetClassName = '#{tk_class}'.freeze"
          end
        end

    ###################################
    =begin
        if ruby_class = WidgetClassNames[tk_class]
          ruby_class_name = ruby_class.name
          # gen_class_name = ruby_class_name + 'GeneratedOnTk'
          gen_class_name = ruby_class_name
          classname_def = ''
        else
          mod = TkExtlibAutoloadModule.find{|m| m.const_defined?(tk_class)}
          if mod
            ruby_class_name = mod.name + '::' + tk_class
            gen_class_name = ruby_class_name
            classname_def = ''
          elsif Object.const_defined?('Tk' + tk_class)
            ruby_class_name = 'Tk' + tk_class
            # gen_class_name = ruby_class_name + 'GeneratedOnTk'
            gen_class_name = ruby_class_name
            classname_def = ''
          else
            ruby_class_name = 'TkWindow'
            # gen_class_name = ruby_class_name + tk_class + 'GeneratedOnTk'
            gen_class_name = 'TkWidget_' + tk_class
            classname_def = "WidgetClassName = '#{tk_class}'.freeze"
          end
        end
    =end

    =begin
        unless Object.const_defined? gen_class_name
          Object.class_eval "class #{gen_class_name}<#{ruby_class_name}
                               #{classname_def}
                             end"
        end
        Object.class_eval "#{gen_class_name}.new('widgetname'=>'#{path}',
                                                 'without_creating'=>true)"
    =end
        base = Object
        gen_class_name.split('::').each{|klass|
          next if klass == ''
          if base.const_defined?(klass)
            base = base.class_eval klass
          else
            base = base.class_eval "class #{klass}<#{ruby_class_name}
                                      #{classname_def}
                                    end
                                    #{klass}"
          end
        }
        base.class_eval "#{gen_class_name}.new('widgetname'=>'#{path}',
                                               'without_creating'=>true)"
      end

_next_cmd_id() click to toggle source

    # File lib/tk.rb, line 823
    def _next_cmd_id
      TkComm::Tk_IDs.mutex.synchronize{
        id = _curr_cmd_id
        #Tk_IDs[0] += 1
        TkComm::Tk_IDs[0].succ!
        id
      }
    end

_toUTF8(str, encoding = nil) click to toggle source

    # File lib/tk.rb, line 675
    def _toUTF8(str, encoding = nil)
      TkCore::INTERP._toUTF8(str, encoding)
    end

array2tk_list(ary, enc=nil) click to toggle source

    # File lib/tk.rb, line 356
    def array2tk_list(ary, enc=nil)
      return "" if ary.size == 0

      sys_enc = TkCore::INTERP.encoding
      sys_enc = TclTkLib.encoding_system unless sys_enc

      dst_enc = (enc == nil)? sys_enc: enc

      dst = ary.collect{|e|
        if e.kind_of? Array
          s = array2tk_list(e, enc)
        elsif e.kind_of? Hash
          tmp_ary = []
          #e.each{|k,v| tmp_ary << k << v }
          e.each{|k,v| tmp_ary << "-#{_get_eval_string(k)}" << v }
          s = array2tk_list(tmp_ary, enc)
        else
          s = _get_eval_string(e, enc)
        end

        if dst_enc != true && dst_enc != false
          if (s_enc = s.instance_variable_get(:@encoding))
            s_enc = s_enc.to_s
          elsif TkCore::WITH_ENCODING
            s_enc = s.encoding.name
          else
            s_enc = sys_enc
          end
          dst_enc = true if s_enc != dst_enc
        end

        s
      }

      if sys_enc && dst_enc
        dst.map!{|s| _toUTF8(s)}
        ret = TkCore::INTERP._merge_tklist(*dst)
        if TkCore::WITH_ENCODING
          if dst_enc.kind_of?(String)
            ret = _fromUTF8(ret, dst_enc)
            ret.force_encoding(dst_enc)
          else
            ret.force_encoding('utf-8')
          end
        else # without encoding
          if dst_enc.kind_of?(String)
            ret = _fromUTF8(ret, dst_enc)
            ret.instance_variable_set(:@encoding, dst_enc)
          else
            ret.instance_variable_set(:@encoding, 'utf-8')
          end
        end
        ret
      else
        TkCore::INTERP._merge_tklist(*dst)
      end
    end

error_at() click to toggle source

    *** ATTENTION ***

'current_configinfo' method always returns a Hash under all cases of above.

    # File lib/tk.rb, line 97
    def error_at
      frames = caller()
      frames.delete_if do |c|
        c =~ %r!/tk(|core|thcore|canvas|text|entry|scrollbox)\.rb:\d+!
      end
      frames
    end

image_obj(val) click to toggle source

    # File lib/tk.rb, line 614
    def image_obj(val)
      if val =~ /^i(_\d+_)?\d+$/
        TkImage::Tk_IMGTBL.mutex.synchronize{
          TkImage::Tk_IMGTBL[val]? TkImage::Tk_IMGTBL[val] : val
        }
      else
        val
      end
    end

install_win(ppath,name=nil) click to toggle source

# begin¶ ↑

    def install_win(ppath,name=nil)
      if !name or name == ''
        #name = format("w%.4d", Tk_IDs[1])
        #Tk_IDs[1] += 1
        name = "w" + Tk_IDs[1]
        Tk_IDs[1].succ!
      end
      if name[0] == ?.
        @path = name.dup
      elsif !ppath or ppath == "."
        @path = Kernel.format(".%s", name);
      else
        @path = Kernel.format("%s.%s", ppath, name)
      end
      #Tk_WINDOWS[@path] = self
      TkCore::INTERP.tk_windows[@path] = self
    end

# end¶ ↑

    # File lib/tk.rb, line 913
    def install_win(ppath,name=nil)
      if name
        if name == ''
          raise ArgumentError, "invalid widget-name '#{name}'"
        end
        if name[0] == ?.
          @path = '' + name
          @path.freeze
          return TkCore::INTERP.tk_windows[@path] = self
        end
      else
        Tk_IDs.mutex.synchronize{
          name = "w" + TkCore::INTERP._ip_id_ + Tk_IDs[1]
          Tk_IDs[1].succ!
        }
      end
      if !ppath or ppath == '.'
        @path = '.' + name
      else
        @path = ppath + '.' + name
      end
      @path.freeze
      TkCore::INTERP.tk_windows[@path] = self
    end

list(val, depth=0, enc=true) click to toggle source

# begin¶ ↑

    ### --> definition is moved to TkUtil module
    def bool(val)
      case val
      when "1", 1, 'yes', 'true'
        true
      else
        false
      end
    end

    def number(val)
      case val
      when /^-?\d+$/
        val.to_i
      when /^-?\d+\.?\d*(e[-+]?\d+)?$/
        val.to_f
      else
        fail(ArgumentError, "invalid value for Number:'#{val}'")
      end
    end
    def string(val)
      if val == "{}"
        ''
      elsif val[0] == ?{ && val[-1] == ?}
        val[1..-2]
      else
        val
      end
    end
    def num_or_str(val)
      begin
        number(val)
      rescue ArgumentError
        string(val)
      end
    end

# end¶ ↑

    # File lib/tk.rb, line 599
    def list(val, depth=0, enc=true)
      tk_split_list(val, depth, enc, enc)
    end

procedure(val) click to toggle source

    # File lib/tk.rb, line 623
      def procedure(val)
    =begin
        if val =~ /^rb_out\S* (c(_\d+_)?\d+)/
          #Tk_CMDTBL[$1]
          #TkCore::INTERP.tk_cmd_tbl[$1]
          TkCore::INTERP.tk_cmd_tbl[$1].cmd
    =end
        if val =~ /rb_out\S*(?:\s+(::\S*|[{](::.*)[}]|["](::.*)["]))? (c(_\d+_)?(\d+))/
          return TkCore::INTERP.tk_cmd_tbl[$4].cmd
        else
          #nil
          val
        end
      end

simplelist(val, src_enc=true, dst_enc=true) click to toggle source

    # File lib/tk.rb, line 602
    def simplelist(val, src_enc=true, dst_enc=true)
      tk_split_simplelist(val, src_enc, dst_enc)
    end

slice_ary(ary, size) { |sub_ary| ... } click to toggle source

    # File lib/tk.rb, line 643
    def slice_ary(ary, size)
      sliced = []
      wk_ary = ary.dup
      until wk_ary.size.zero?
        sub_ary = []
        size.times{ sub_ary << wk_ary.shift }
        yield(sub_ary) if block_given?
        sliced << sub_ary
      end
      (block_given?)? ary: sliced
    end

tk_event_sequence(context) click to toggle source

    # File lib/tk.rb, line 961
    def tk_event_sequence(context)
      if context.kind_of? TkVirtualEvent
        context = context.path
      end
      if context.kind_of? Array
        context = context.collect{|ev|
          if ev.kind_of? TkVirtualEvent
            ev.path
          else
            ev
          end
        }.join("><")
      end
      if /,/ =~ context
        context = context.split(/\s*,\s*/).join("><")
      else
        context
      end
    end

tk_split_escstr(str, src_enc=true, dst_enc=true) click to toggle source

use Tcl function version of split_list

    # File lib/tk.rb, line 303
    def tk_split_escstr(str, src_enc=true, dst_enc=true)
      str = _toUTF8(str) if src_enc
      if dst_enc
        TkCore::INTERP._split_tklist(str).map!{|s| _fromUTF8(s)}
      else
        TkCore::INTERP._split_tklist(str)
      end
    end

tk_split_list(str, depth=0, src_enc=true, dst_enc=true) click to toggle source

    # File lib/tk.rb, line 332
    def tk_split_list(str, depth=0, src_enc=true, dst_enc=true)
      return [] if str == ""
      str = _toUTF8(str) if src_enc
      TkCore::INTERP._split_tklist(str).map!{|token|
        tk_split_sublist(token, depth - 1, false, dst_enc)
      }
    end

tk_split_simplelist(str, src_enc=true, dst_enc=true) click to toggle source

    # File lib/tk.rb, line 340
    def tk_split_simplelist(str, src_enc=true, dst_enc=true)
      #lst = TkCore::INTERP._split_tklist(str)
      #if (lst.size == 1 && lst =~ /^\{.*\}$/)
      #  TkCore::INTERP._split_tklist(str[1..-2])
      #else
      #  lst
      #end

      str = _toUTF8(str) if src_enc
      if dst_enc
        TkCore::INTERP._split_tklist(str).map!{|s| _fromUTF8(s)}
      else
        TkCore::INTERP._split_tklist(str)
      end
    end

tk_split_sublist(str, depth=-1, src_enc=true, dst_enc=true) click to toggle source

    # File lib/tk.rb, line 312
    def tk_split_sublist(str, depth=-1, src_enc=true, dst_enc=true)
      # return [] if str == ""
      # list = TkCore::INTERP._split_tklist(str)
      str = _toUTF8(str) if src_enc

      if depth == 0
        return "" if str == ""
        list = [str]
      else
        return [] if str == ""
        list = TkCore::INTERP._split_tklist(str)
      end
      if list.size == 1
        # tk_tcl2ruby(list[0], nil, false)
        tk_tcl2ruby(list[0], dst_enc, false)
      else
        list.collect{|token| tk_split_sublist(token, depth - 1, false, dst_enc)}
      end
    end

tk_tcl2ruby(val, enc_mode = false, listobj = true) click to toggle source

    # File lib/tk.rb, line 231
      def tk_tcl2ruby(val, enc_mode = false, listobj = true)
    =begin
        if val =~ /^rb_out\S* (c(_\d+_)?\d+)/
          #return Tk_CMDTBL[$1]
          return TkCore::INTERP.tk_cmd_tbl[$1]
          #cmd_obj = TkCore::INTERP.tk_cmd_tbl[$1]
          #if cmd_obj.kind_of?(Proc) || cmd_obj.kind_of?(Method)
          #  cmd_obj
          #else
          #  cmd_obj.cmd
          #end
        end
    =end
        if val =~ /rb_out\S*(?:\s+(::\S*|[{](::.*)[}]|["](::.*)["]))? (c(_\d+_)?(\d+))/
          return TkCore::INTERP.tk_cmd_tbl[$4]
        end
        #if val.include? ?\s
        #  return val.split.collect{|v| tk_tcl2ruby(v)}
        #end
        case val
        when /\A@font\S+\z/
          TkFont.get_obj(val)
        when /\A-?\d+\z/
          val.to_i
        when /\A\.\S*\z/
          #Tk_WINDOWS[val] ? Tk_WINDOWS[val] : _genobj_for_tkwidget(val)
          TkCore::INTERP.tk_windows[val]?
               TkCore::INTERP.tk_windows[val] : _genobj_for_tkwidget(val)
        when /\Ai(_\d+_)?\d+\z/
          TkImage::Tk_IMGTBL.mutex.synchronize{
            TkImage::Tk_IMGTBL[val]? TkImage::Tk_IMGTBL[val] : val
          }
        when /\A-?\d+\.?\d*(e[-+]?\d+)?\z/
          val.to_f
        when /\\ /
          val.gsub(/\\ /, ' ')
        when /[^\\] /
          if listobj
            #tk_split_escstr(val).collect{|elt|
            #  tk_tcl2ruby(elt, enc_mode, listobj)
            #}
            val = _toUTF8(val) unless enc_mode
            tk_split_escstr(val, false, false).collect{|elt|
              tk_tcl2ruby(elt, true, listobj)
            }
          elsif enc_mode
            _fromUTF8(val)
          else
            val
          end
        else
          if enc_mode
            _fromUTF8(val)
          else
            val
          end
        end
      end

uninstall_win() click to toggle source

    # File lib/tk.rb, line 938
    def uninstall_win()
      #Tk_WINDOWS.delete(@path)
      TkCore::INTERP.tk_windows.delete(@path)
    end

window(val) click to toggle source

    # File lib/tk.rb, line 605
    def window(val)
      if val =~ /^\./
        #Tk_WINDOWS[val]? Tk_WINDOWS[val] : _genobj_for_tkwidget(val)
        TkCore::INTERP.tk_windows[val]?
             TkCore::INTERP.tk_windows[val] : _genobj_for_tkwidget(val)
      else
        nil
      end
    end