summaryrefslogtreecommitdiff
path: root/ext/tk/lib/tkextlib/itcl/incr_tcl.rb
blob: 8f6bb33abe026b0a17c7ecc7308b913235c07338 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#
#  tkextlib/itk/incr_tcl.rb
#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
#

require 'tk'

# call setup script
require 'tkextlib/itcl.rb'

# TkPackage.require('Itcl', '3.2')
TkPackage.require('Itcl')

module Tk
  module Itcl
    include Tk
    extend Tk

    LIBRARY = TkVarAccess.new('::itcl::library')
    PURIST = TkVarAccess.new('::itcl::purist')

    VERSION    = TkCore::INTERP._invoke("set", "::itcl::version").freeze
    PATCHLEVEL = TkCore::INTERP._invoke("set", "::itcl::patchLevel").freeze

    PACKAGE_NAME = 'Itcl'.freeze
    def self.package_name
      PACKAGE_NAME
    end

    def self.package_version
      begin
        TkPackage.require('Itcl')
      rescue
        ''
      end
    end

    ##############################################

    class ItclObject < TkObject
      ITCL_CLASSNAME = ''.freeze

      (ITCL_OBJ_ID = ['itclobj'.freeze, TkUtil.untrust('00000')]).instance_eval{
        @mutex = Mutex.new
        def mutex; @mutex; end
        freeze
      }
      ITCL_OBJ_TBL = TkUtil.untrust({})

      def initialize(*args)
        if (@klass = self.class::ITCL_CLASSNAME).empty?
          fail RuntimeError, 'unknown itcl class (abstract class?)'
        end
        Tk::Itcl::ItclObject::ITCL_OBJ_ID.mutex.synchronize{
          @id = Tk::Itcl::ItclObject::TCL_OBJ_ID.join(TkCore::INTERP._ip_id_)
          Tk::Itcl::ItclObject::ITCL_OBJ_ID[1].succ!
        }
        @path = @id
      end

      def self.call_proc(name, *args)
        tk_call("#{ITCL_CLASSNAME}::#{cmd}", *args)
      end

      def call_method(name, *args)
        tk_call(@path, name, *args)
      end

      def isa(klass)
        bool(tk_call(@path, 'isa', klass))
      end
      alias itcl_kind_of? isa

      def info_class
        tk_call(@path, 'info', 'class')
      end

      def info_inherit
        simplelist(tk_call(@path, 'info', 'inherit'))
      end

      def info_heritage
        list(tk_call(@path, 'info', 'heritage'))
      end

      def info_function(*args)
        if args[-1].kind_of?(Array)
          params = args.pop
          params.each{|param|
            param = param.to_s
            args << ( (param[0] == ?-)? param: "-#{param}" )
          }
        end
        list(tk_call(@path, 'info', 'function', *args))
      end

      def info_variable(*args)
        if args[-1].kind_of?(Array)
          params = args.pop
          params.each{|param|
            param = param.to_s
            args << ( (param[0] == ?-)? param: "-#{param}" )
          }
        end
        list(tk_call(@path, 'info', 'variable', *args))
      end
    end

    ##############################################

    def self.body(klass, func, args, body)
      tk_call('::itcl::body', "#{klass}::#{func}", args, body)
    end

    def self.code(cmd, *args)
      tk_call('::itcl::code', cmd, *args)
    end

    def self.code_in_namespace(namespace, cmd, *args)
      tk_call('::itcl::code', '-namespace', namespace, cmd, *args)
    end

    def self.configbody(klass, var, body)
      tk_call('::itcl::configbody', "#{klass}::#{var}", body)
    end

    def self.create_itcl_class(name, body)
      TkCore::INTERP._invoke('::itcl::class', name, body)
      klass = Class.new(Tk::Itcl::ItclObject)
      klass.const_set('ITCL_CLASSNAME', name.dup.freeze)
      klass
    end

    def self.delete_itcl_class(*names)
      tk_call('::itcl::delete', 'class',  *names)
    end

    def self.delete_itcl_object(*names)
      tk_call('::itcl::delete', 'object',  *names)
    end

    def self.delete_namespace(*names)
      tk_call('::itcl::delete', 'namespace',  *names)
    end

    def self.ensemble(name, *args)
      tk_call('::itcl::ensemble', name, *args)
    end

    def self.find_classes(pat=None)
      simplelist(tk_call('::itcl::find', 'classes', pat))
    end

    def self.find_objects(*args)
      simplelist(tk_call('::itcl::find', 'objects', *args))
    end

    def self.is_itcl_class(target)
      bool(tk_call('::itcl::is', 'class', target))
    end

    def self.is_itcl_object(target)
      bool(tk_call('::itcl::is', 'object', target))
    end

    def self.create_local_obj(klass, name, *args)
      tk_call('::itcl::local', klass, name, *args)
    end

    def self.is_itcl_instance(klass, target)
      bool(tk_call('::itcl::is', 'object', '-class', klass, target))
    end

    def self.scope(var)
      tk_call('::itcl::scope', var)
    end
  end
end