Class: TclTkIp

Inherits:
Object show all
Defined in:
sample/demos-en/tkencoding.rb,
lib/tk.rb,
lib/tk.rb,
lib/tk.rb

Overview

-*- ruby -*-

tkencoding.rb written by [email protected]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TclTkIp

Returns a new instance of TclTkIp.



30
31
32
33
34
35
36
# File 'lib/tk.rb', line 30

def initialize(*args)
  __initialize__(*args)

  @force_default_encoding ||= TkUtil.untrust([false])
  @encoding ||= TkUtil.untrust([nil])
  def @encoding.to_s; self.join(nil); end
end

Instance Attribute Details

#encoding=(value) ⇒ Object

Sets the attribute encoding

Parameters:

  • value

    the value to set the attribute encoding to.



12
13
14
# File 'sample/demos-en/tkencoding.rb', line 12

def encoding=(value)
  @encoding = value
end

Class Method Details

.new(*args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/multi-tk.rb', line 27

def new(*args)
  if Thread.current.group != ThreadGroup::Default
    raise SecurityError, 'only ThreadGroup::Default can call TclTkIp.new'
  end
  obj = __new__(*args)
  obj.instance_eval{
    @force_default_encoding ||= TkUtil.untrust([false])
    @encoding ||= TkUtil.untrust([nil])
    def @encoding.to_s; self.join(nil); end
  }
  obj
end

.to_sObject



35
# File 'lib/tk.rb', line 35

def @encoding.to_s; self.join(nil); end

Instance Method Details

#__eval__Object



18
# File 'lib/tk.rb', line 18

alias __eval__ _eval

#__fromUTF8Object



2907
# File 'lib/tk.rb', line 2907

alias __fromUTF8 _fromUTF8

#__invoke__Object



20
# File 'lib/tk.rb', line 20

alias __invoke__ _invoke

#__toUTF8Object



2906
# File 'lib/tk.rb', line 2906

alias __toUTF8 _toUTF8

#_eval(cmd) ⇒ Object Also known as: _eval_with_enc, __eval



14
15
16
17
18
19
20
# File 'sample/demos-en/tkencoding.rb', line 14

def _eval(cmd)
  if( @encoding )
    _fromUTF8(__eval(_toUTF8(cmd,@encoding)),@encoding)
  else
    __eval(cmd)
  end
end

#_eval_without_encObject

backup original (without encoding) _eval and _invoke



17
# File 'lib/tk.rb', line 17

alias _eval_without_enc _eval

#_fromUTF8(str, encoding = nil) ⇒ Object



2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
# File 'lib/tk.rb', line 2965

def _fromUTF8(str, enc = nil)
  # str must be UTF-8 or binary.
  enc_name = str.instance_variable_get(:@encoding)
  enc_name ||=
    Tk::Encoding::ENCODING_TABLE.get_name(str.encoding) rescue nil

  # is 'binary' encoding?
  if enc_name == Tk::Encoding::BINARY_NAME
    return str.dup.force_encoding(Tk::Encoding::BINARY_NAME)
  end

  # get target encoding name (if enc == nil, use default encoding)
  begin
    enc_name = Tk::Encoding::ENCODING_TABLE.get_name(enc)
  rescue
    # then, enc != nil
    # unknown encoding for Tk -> try to convert encoding on Ruby
    str = str.dup.force_encoding(Tk::Encoding::UTF8_NAME)
    str.encode!(enc) # modify self !!
    return str  # if no error, probably succeed converting
  end

  encstr = __fromUTF8(str, enc_name)
  encstr.force_encoding(Tk::Encoding::ENCODING_TABLE.get_obj(enc_name))
  encstr
end

#_invoke(*cmds) ⇒ Object Also known as: _invoke_with_enc, __invoke



22
23
24
25
26
27
28
29
# File 'sample/demos-en/tkencoding.rb', line 22

def _invoke(*cmds)
  if( @encoding )
    cmds = cmds.collect{|cmd| _toUTF8(cmd,@encoding)}
    _fromUTF8(__invoke(*cmds),@encoding)
  else
    __invoke(*cmds)
  end
end

#_invoke_without_encObject



19
# File 'lib/tk.rb', line 19

alias _invoke_without_enc _invoke

#_ip_id_Object



22
23
24
25
# File 'lib/tk.rb', line 22

def _ip_id_
  # for RemoteTkIp
  ''
end

#_toUTF8(str, encoding = nil) ⇒ Object



2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
# File 'lib/tk.rb', line 2915

def _toUTF8(str, enc = nil)
  if enc
    # use given encoding
    begin
      enc_name = Tk::Encoding::ENCODING_TABLE.get_name(enc)
    rescue
      # unknown encoding for Tk -> try to convert encoding on Ruby
      str = str.dup.force_encoding(enc)
      str.encode!(Tk::Encoding::UTF8_NAME) # modify self !!
      return str  # if no error, probably succeed converting
    end
  end

  enc_name ||= str.instance_variable_get(:@encoding)

  enc_name ||=
    Tk::Encoding::ENCODING_TABLE.get_name(str.encoding) rescue nil

  if enc_name
    # str has its encoding information
    encstr = __toUTF8(str, enc_name)
    encstr.force_encoding(Tk::Encoding::UTF8_NAME)
    return encstr
  else
    # str.encoding isn't supported by Tk -> try to convert on Ruby
    begin
      return str.encode(Tk::Encoding::UTF8_NAME) # new string
    rescue
      # error -> ignore, try to use default encoding of Ruby/Tk
    end
  end

  #enc_name ||=
  #  Tk::Encoding::ENCODING_TABLE.get_name(Tk.encoding) rescue nil
  enc_name ||= Tk::Encoding::ENCODING_TABLE.get_name(nil)

  # is 'binary' encoding?
  if enc_name == Tk::Encoding::BINARY_NAME
    return str.dup.force_encoding(Tk::Encoding::BINARY_NAME)
  end

  # force default encoding?
  if ! str.kind_of?(Tk::EncodedString) && self.force_default_encoding?
    enc_name = Tk::Encoding::ENCODING_TABLE.get_name(Tk.default_encoding)
  end

  encstr = __toUTF8(str, enc_name)
  encstr.force_encoding(Tk::Encoding::UTF8_NAME)
  encstr
end

#default_encoding=(name) ⇒ Object



2880
2881
2882
2883
# File 'lib/tk.rb', line 2880

def default_encoding=(name)
  name = name.name if Tk::WITH_ENCODING && name.kind_of?(::Encoding)
  @encoding[0] = name.to_s.dup
end

#encoding_nameObject Also known as: encoding, default_encoding



2892
2893
2894
# File 'lib/tk.rb', line 2892

def encoding_name
  (@encoding[0])? @encoding[0].dup: nil
end

#encoding_objObject



2898
2899
2900
2901
2902
2903
2904
# File 'lib/tk.rb', line 2898

def encoding_obj
  if Tk::WITH_ENCODING
    Tk::Encoding.tcl2rb_encoding(@encoding[0])
  else
    (@encoding[0])? @encoding[0].dup: nil
  end
end

#force_default_encoding=(mode) ⇒ Object



2872
2873
2874
# File 'lib/tk.rb', line 2872

def force_default_encoding=(mode)
  @force_default_encoding[0] = (mode)? true: false
end

#force_default_encoding?Boolean

Returns:

  • (Boolean)


2876
2877
2878
# File 'lib/tk.rb', line 2876

def force_default_encoding?
  @force_default_encoding[0] ||= false
end