Class: TclTkIp

Inherits:
Object show all
Defined in:
ext/lib/tk.rb,
ext/lib/tk.rb,
ext/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.



28
29
30
31
32
33
34
# File 'ext/lib/tk.rb', line 28

def initialize(*args)
  __initialize__(*args)

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

Instance Attribute Details

#encoding=(value) ⇒ Object

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



2718
2719
2720
2721
# File 'ext/lib/tk.rb', line 2718

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

Class Method Details

.__new__Object



24
# File 'ext/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 'ext/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 ||= [false].taint
    @encoding ||= [nil].taint
    def @encoding.to_s; self.join(nil); end
  }
  obj
end

.to_sObject



33
# File 'ext/lib/tk.rb', line 33

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

Instance Method Details

#__fromUTF8Object



2738
# File 'ext/lib/tk.rb', line 2738

alias __fromUTF8 _fromUTF8

#__toUTF8Object



2737
# File 'ext/lib/tk.rb', line 2737

alias __toUTF8 _toUTF8

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



2837
2838
2839
# File 'ext/lib/tk.rb', line 2837

def _eval(cmd)
  _fromUTF8(__eval(_toUTF8(cmd)))
end

#_eval_without_encObject

backup original (without encoding) _eval and _invoke



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

alias _eval_without_enc _eval

#_fromUTF8(str, encoding = nil) ⇒ Object



2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
# File 'ext/lib/tk.rb', line 2796

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



2841
2842
2843
# File 'ext/lib/tk.rb', line 2841

def _invoke(*cmds)
  _fromUTF8(__invoke(*(cmds.collect{|cmd| _toUTF8(cmd)})))
end

#_invoke_without_encObject



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

alias _invoke_without_enc _invoke

#_ip_id_Object



20
21
22
23
# File 'ext/lib/tk.rb', line 20

def _ip_id_
  # for RemoteTkIp
  ''
end

#_toUTF8(str, encoding = nil) ⇒ Object

without Encoding (Ruby 1.8)



2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
# File 'ext/lib/tk.rb', line 2746

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



2711
2712
2713
2714
# File 'ext/lib/tk.rb', line 2711

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



2723
2724
2725
# File 'ext/lib/tk.rb', line 2723

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

#encoding_objObject



2729
2730
2731
2732
2733
2734
2735
# File 'ext/lib/tk.rb', line 2729

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



2703
2704
2705
# File 'ext/lib/tk.rb', line 2703

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

#force_default_encoding?Boolean

Returns:

  • (Boolean)


2707
2708
2709
# File 'ext/lib/tk.rb', line 2707

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