summaryrefslogtreecommitdiff
path: root/jni/ruby/ext/tk/lib/tkextlib/itk/incr_tk.rb
blob: 989585e33b0a77e09db6bf3d4e76c1acdbdd66b7 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#
#  tkextlib/itk/incr_tk.rb
#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
#

require 'tk'
require 'tk/menuspec'
require 'tkextlib/itcl.rb'

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

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

module Tk
  module Itk
    include Tk
    extend Tk

    LIBRARY = TkVarAccess.new('::itk::library')

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

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

    def self.usual(arg, *args)
      tk_call('::itk::usual', arg, *args)
    end

    def self.usual_names
      list(tk_call('::itk::usual'))
    end

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

    class Archetype < TkWindow
      TkCommandNames = [].freeze
      # WidgetClassName = 'Archetype'.freeze
      # WidgetClassNames[WidgetClassName] = self

      def self.to_eval
        '::itk::' << self::WidgetClassName
      end

      def __destroy_hook__
        Tk::Itk::Component::ComponentID_TBL.delete(self.path)
      end

      #### [incr Tk] public methods
      def component
        simplelist(tk_send('component'))
      end

      def component_path(name)
        window(tk_send('component', name))
      end
      alias component_widget component_path

      def component_invoke(name, cmd, *args)
        window(tk_send('component', name, cmd, *args))
      end

      def component_obj(*names)
        names = component if names.empty?
        names.collect{|name| Tk::Itk::Component.new(self.path, name) }
      end

      #### [incr Tk] protected methods
=begin
      def itk_component_add(visibility, name, create_cmds, option_cmds=None)
        args = []
        visibility.each{|v| v = v.to_s; args << ( (v[0] == ?-)? v: "-#{v}" )}
        args << '--' << name << create_cmd << option_cmds
        tk_call('itk_component', 'add', *args)
      end

      def itk_component_delete(*names)
        tk_call('itk_component', 'delete', *names)
      end

      def itk_initialize(keys={})
        tk_call('itk_initialize', keys)
      end

      def itk_option_add(*args)
        tk_call('itk_option', 'add', *args)
      end

      def itk_option_define(name, resource, klass, init, config=None)
        tk_call('itk_option', 'define', name, resource, klass, init, config)
      end

      def itk_option_remove(*args)
        tk_call('itk_option', 'remove', *args)
      end
=end
    end

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

    class Toplevel < Archetype
      TkCommandNames = ['::itk::Toplevel'].freeze
      WidgetClassName = 'Toplevel'.freeze
      WidgetClassNames[WidgetClassName] ||= self

      include Wm
      include TkMenuSpec

      def __strval_optkeys
        super() << 'title'
      end
      private :__strval_optkeys
    end

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

    class Widget < Archetype
      TkCommandNames = ['::itk::Widget'].freeze
      WidgetClassName = 'Widget'.freeze
      WidgetClassNames[WidgetClassName] ||= self
    end


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

    class Component < TkObject
      def __cget_cmd
        [self.master, 'component', self.name, 'cget']
      end
      private :__cget_cmd

      def __config_cmd
        [self.master, 'component', self.name, 'configure']
      end
      private :__config_cmd

      ComponentID_TBL = TkCore::INTERP.create_table

      (Itk_Component_ID = ['itk:component'.freeze, TkUtil.untrust('00000')]).instance_eval{
        @mutex = Mutex.new
        def mutex; @mutex; end
        freeze
      }

      TkCore::INTERP.init_ip_env{
        ComponentID_TBL.mutex.synchronize{ ComponentID_TBL.clear }
      }

      def self.id2obj(master, id)
        if master.kind_of?(TkObject)
          master = master.path
        else
          master = master.to_s
        end
        ComponentID_TBL.mutex.synchronize{
          if ComponentID_TBL.key?(master)
            (ComponentID_TBL[master].key?(id))? ComponentID_TBL[master][id]: id
          else
            id
          end
        }
      end

      def self.new(master, component=nil)
        if master.kind_of?(TkObject)
          master = master.path
        else
          master = master.to_s
        end

        if component.kind_of?(Tk::Itk::Component)
          component = component.name
        elsif component
          component = component.to_s
        else
          Itk_Component_ID.mutex.synchronize{
            component = Itk_Component_ID.join(TkCore::INTERP._ip_id_)
            Itk_Component_ID[1].succ!
          }
        end

        ComponentID_TBL.mutex.synchronize{
          if ComponentID_TBL.key?(master)
            if ComponentID_TBL[master].key?(component)
              return ComponentID_TBL[master][component]
            end
          else
            ComponentID_TBL[master] = {}
          end
        }

        super(master, component)
      end

      def initialize(master, component)
        @master = master
        @component = component

        ComponentID_TBL.mutex.synchronize{
          ComponentID_TBL[@master][@component] = self
        }

        begin
          @widget = window(tk_call(@master, 'component', @component))
          @path = @widget.path
        rescue
          @widget = nil
          @path = nil
        end
      end

      def path
        unless @path
          begin
            @widget = window(tk_call(@master, 'component', @component))
            @path = @widget.path
          rescue
            fail RuntimeError, 'component is not assigned to a widget'
          end
        end
        @path
      end

      def epath
        path()
      end

      def to_eval
        path()
      end

      def master
        @master
      end

      def name
        @component
      end

      def widget
        unless @widget
          begin
            @widget = window(tk_call(@master, 'component', @component))
            @path = @widget.path
          rescue
            fail RuntimeError, 'component is not assigned to a widget'
          end
        end
        @widget
      end

      def widget_class
        unless @widget
          begin
            @widget = window(tk_call(@master, 'component', @component))
            @path = @widget.path
            @widget.classname
          rescue
            nil
          end
        end
      end

      def method_missing(id, *args)
        name = id.id2name

        # try 1 : component command
        begin
          return tk_call(@master, 'component', @component, name, *args)
        rescue
        end

        # try 2 : component configure
        len = args.length
        begin
          case len
          when 1
            if name[-1] == ?=
              return configure(name[0..-2], args[0])
            else
              return configure(name, args[0])
            end
          when 0
            return cget(name)
          end
        rescue
        end

        # try 3 : widget method or widget configure
        begin
          unless @widget
            @widget = window(tk_call(@master, 'component', @component))
            @path = @widget.path
          end
          @widget.__send__(id, *args)
        rescue
        end

        # unknown method
        super(id, *args)
        # fail RuntimeError, "unknown method '#{name}' for #{self.inspect}"
      end

      def tk_send(cmd, *rest)
        begin
          tk_call(@master, 'component', @component, cmd, *rest)
        rescue
          unless @path
            begin
              @widget = window(tk_call(@master, 'component', @component))
              @path = @widget.path
            rescue
              fail RuntimeError, 'component is not assigned to a widget'
            end
          end
          tk_call(@path, cmd, *rest)
        end
      end

      def tk_send_without_enc(cmd, *rest)
        begin
          tk_call_without_enc(@master, 'component', @component, cmd, *rest)
        rescue
          unless @path
            begin
              @widget = window(tk_call(@master, 'component', @component))
              @path = @widget.path
            rescue
              fail RuntimeError, 'component is not assigned to a widget'
            end
          end
          tk_call_without_enc(@path, cmd, *rest)
        end
      end

      def tk_send_with_enc(cmd, *rest)
        begin
          tk_call_with_enc(@master, 'component', @component, cmd, *rest)
        rescue
          unless @path
            begin
              @widget = window(tk_call(@master, 'component', @component))
              @path = @widget.path
            rescue
              fail RuntimeError, 'component is not assigned to a widget'
            end
          end
          tk_call_with_enc(@path, cmd, *rest)
        end
      end

      #def bind(*args)
      #  unless @widget
      #    begin
      #      @widget = window(tk_call(@master, 'component', @component))
      #      @path = @widget.path
      #    rescue
      #      fail RuntimeError, 'component is not assigned to a widget'
      #    end
      #  end
      #  @widget.bind(*args)
      #end
      def bind(context, *args)
        unless @widget
          begin
            @widget = window(tk_call(@master, 'component', @component))
            @path = @widget.path
          rescue
            fail RuntimeError, 'component is not assigned to a widget'
          end
        end
        # 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
        @widget.bind(context, cmd, *args)
      end

      #def bind_append(*args)
      #  unless @widget
      #    begin
      #      @widget = window(tk_call(@master, 'component', @component))
      #      @path = @widget.path
      #    rescue
      #      fail RuntimeError, 'component is not assigned to a widget'
      #    end
      #  end
      #  @widget.bind_append(*args)
      #end
      def bind_append(context, *args)
        unless @widget
          begin
            @widget = window(tk_call(@master, 'component', @component))
            @path = @widget.path
          rescue
            fail RuntimeError, 'component is not assigned to a widget'
          end
        end
        # 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
        @widget.bind_append(context, cmd, *args)
      end

      def bind_remove(*args)
        unless @widget
          begin
            @widget = window(tk_call(@master, 'component', @component))
            @path = @widget.path
          rescue
            fail RuntimeError, 'component is not assigned to a widget'
          end
        end
        @widget.bind_remove(*args)
      end

      def bindinfo(*args)
        unless @widget
          begin
            @widget = window(tk_call(@master, 'component', @component))
            @path = @widget.path
          rescue
            fail RuntimeError, 'component is not assigned to a widget'
          end
        end
        @widget.bindinfo(*args)
      end

    end
  end
end