summaryrefslogtreecommitdiff
path: root/ext/tk/lib/tkextlib/blt/watch.rb
blob: 2daf417e0b714385f6f831fbc744db62c8885914 (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
#
#  tkextlib/blt/watch.rb
#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
#

require 'tk'
require 'tkextlib/blt.rb'

module Tk::BLT
  class Watch < TkObject
    extend TkCore

    TkCommandNames = ['::blt::watch'.freeze].freeze

    WATCH_ID_TBL = TkCore::INTERP.create_table

    (BLT_WATCH_ID = ['blt_watch_id'.freeze, '00000'.taint]).instance_eval{
      @mutex = Mutex.new
      def mutex; @mutex; end
      freeze
    }

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

    def self.names(state = None)
      lst = tk_split_list(tk_call('::blt::watch', 'names', state))
      WATCH_ID_TBL.mutex.synchronize{
        lst.collect{|name|
          WATCH_ID_TBL[name] || name
        }
      }
    end

    def __numval_optkeys
      ['maxlevel']
    end
    private :__numval_optkeys

    def __boolval_optkeys
      ['active']
    end
    private :__boolval_optkeys

    def __config_cmd
      ['::blt::watch', 'configure', self.path]
    end
    private :__config_cmd

    def initialize(name = nil, keys = {})
      if name.kind_of?(Hash)
        keys = name
        name = nil
      end

      if name
        @id = name.to_s
      else
        BLT_WATCH_ID.mutex.synchronize{
          @id = BLT_WATCH_ID.join(TkCore::INTERP._ip_id_)
          BLT_WATCH_ID[1].succ!
        }
      end

      @path = @id

      WATCH_ID_TBL.mutex.synchronize{
        WATCH_ID_TBL[@id] = self
      }
      tk_call('::blt::watch', 'create', @id, *hash_kv(keys))
    end

    def activate
      tk_call('::blt::watch', 'activate', @id)
      self
    end
    def deactivate
      tk_call('::blt::watch', 'deactivate', @id)
      self
    end
    def delete
      tk_call('::blt::watch', 'delete', @id)
      self
    end
    def info
      ret = []
      lst = tk_split_simplelist(tk_call('::blt::watch', 'info', @id))
      until lst.empty?
        k, v, *lst = lst
        k = k[1..-1]
        case k
        when /^(#{__strval_optkeys.join('|')})$/
          # do nothing

        when /^(#{__numval_optkeys.join('|')})$/
          begin
            v = number(v)
          rescue
            v = nil
          end

        when /^(#{__numstrval_optkeys.join('|')})$/
          v = num_or_str(v)

        when /^(#{__boolval_optkeys.join('|')})$/
          begin
            v = bool(v)
          rescue
            v = nil
          end

        when /^(#{__listval_optkeys.join('|')})$/
          v = simplelist(v)

        when /^(#{__numlistval_optkeys.join('|')})$/
          v = list(v)

        else
          if v.index('{')
            v = tk_split_list(v)
          else
            v = tk_tcl2ruby(v)
          end
        end

        ret << [k, v]
      end

      ret
    end
    def configinfo(slot = nil)
      if slot
        slot = slot.to_s
        v = cget(slot)
        if TkComm::GET_CONFIGINFO_AS_ARRAY
          [slot, v]
        else
          {slot=>v}
        end
      else
        if TkComm::GET_CONFIGINFO_AS_ARRAY
          info
        else
          Hash[*(info.flatten)]
        end
      end
    end
    def cget(key)
      key = key.to_s
      begin
        info.assoc(key)[1]
      rescue
        fail ArgumentError, "unknown option '#{key}'"
      end
    end
  end
end