summaryrefslogtreecommitdiff
path: root/ext/tk/lib/tk/bindtag.rb
blob: 23b4e0b7c3ba644996120151baa6b588e7b45db9 (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
#
# tk/bind.rb : control event binding
#
require 'tk'

class TkBindTag
  include TkBindCore

  #BTagID_TBL = {}
  BTagID_TBL = TkCore::INTERP.create_table

  (Tk_BINDTAG_ID = ["btag".freeze, TkUtil.untrust("00000")]).instance_eval{
    @mutex = Mutex.new
    def mutex; @mutex; end
    freeze
  }

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

  def TkBindTag.id2obj(id)
    BTagID_TBL.mutex.synchronize{
      (BTagID_TBL[id])? BTagID_TBL[id]: id
    }
  end

=begin
  def TkBindTag.new_by_name(name, *args, &b)
    BTagID_TBL.mutex.synchronize{
      return BTagID_TBL[name] if BTagID_TBL[name]
    }

    self.new.instance_eval{
      BTagID_TBL.mutex.synchronize{
        BTagID_TBL.delete @id
        @id = name
        BTagID_TBL[@id] = self
      }
      bind(*args, &b) if args != []
      self
    }
  end
=end
  def TkBindTag.new_by_name(name, *args, &b)
    obj = nil
    BTagID_TBL.mutex.synchronize{
      if BTagID_TBL[name]
        obj = BTagID_TBL[name]
      else
        (obj = BTagID_TBL[name] = self.allocate).instance_eval{
          @id = name
        }
      end
    }
    bind(*args, &b) if obj && args != []
    obj
  end

  def initialize(*args, &b)
    Tk_BINDTAG_ID.mutex.synchronize{
      # @id = Tk_BINDTAG_ID.join('')
      @id = Tk_BINDTAG_ID.join(TkCore::INTERP._ip_id_)
      Tk_BINDTAG_ID[1].succ!
    }
    BTagID_TBL.mutex.synchronize{
      BTagID_TBL[@id] = self
    }
    bind(*args, &b) if args != []
  end

  ALL = self.new_by_name('all')

  def name
    @id
  end

  def to_eval
    @id
  end

  def inspect
    #Kernel.format "#<TkBindTag: %s>", @id
    '#<TkBindTag: ' + @id + '>'
  end
end


class TkBindTagAll<TkBindTag
  def TkBindTagAll.new(*args, &b)
    $stderr.puts "Warning: TkBindTagALL is obsolete. Use TkBindTag::ALL\n"

    TkBindTag::ALL.bind(*args, &b) if args != []
    TkBindTag::ALL
  end
end


class TkDatabaseClass<TkBindTag
=begin
  def self.new(name, *args, &b)
    BTagID_TBL.mutex.synchronize{
      return BTagID_TBL[name] if BTagID_TBL[name]
    }
    super(name, *args, &b)
  end

  def initialize(name, *args, &b)
    @id = name
    BTagID_TBL.mutex.synchronize{
      BTagID_TBL[@id] = self
    }
    bind(*args, &b) if args != []
  end
=end
  def self.new(name, *args, &b)
    BTagID_TBL.mutex.synchronize{
      if BTagID_TBL[name]
        BTagID_TBL[name]
      else
        BTagID_TBL[name] = self.allocate.instance_eval{
          initialize(name, *args, &b)
          self
        }
      end
    }
  end

  def initialize(name, *args, &b)
    @id = name
    bind(*args, &b) if args != []
  end

  def inspect
    #Kernel.format "#<TkDatabaseClass: %s>", @id
    '#<TkDatabaseClass: ' + @id + '>'
  end
end