Class: TclTkIp

Inherits:
Object show all
Defined in:
lib/tk.rb,
lib/tk.rb,
lib/tk.rb

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

from tkencoding.rb by [email protected] attr_accessor :encoding



2808
2809
2810
2811
# File 'lib/tk.rb', line 2808

def encoding=(name)
  self.force_default_encoding = true  # for comaptibility
  self.default_encoding = name
end

Class Method Details

.__new__Object



24
# File 'lib/multi-tk.rb', line 24

alias __new__ new

.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



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

alias __fromUTF8 _fromUTF8

#__invoke__Object



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

alias __invoke__ _invoke

#__toUTF8Object



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

alias __toUTF8 _toUTF8

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



2927
2928
2929
# File 'lib/tk.rb', line 2927

def _eval(cmd)
  _fromUTF8(__eval(_toUTF8(cmd)))
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



2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
# File 'lib/tk.rb', line 2886

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



2931
2932
2933
# File 'lib/tk.rb', line 2931

def _invoke(*cmds)
  _fromUTF8(__invoke(*(cmds.collect{|cmd| _toUTF8(cmd)})))
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

without Encoding (Ruby 1.8)



2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
# File 'lib/tk.rb', line 2836

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



2801
2802
2803
2804
# File 'lib/tk.rb', line 2801

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



2813
2814
2815
# File 'lib/tk.rb', line 2813

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

#encoding_objObject



2819
2820
2821
2822
2823
2824
2825
# File 'lib/tk.rb', line 2819

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



2793
2794
2795
# File 'lib/tk.rb', line 2793

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

#force_default_encoding?Boolean

Returns:

  • (Boolean)


2797
2798
2799
# File 'lib/tk.rb', line 2797

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