Class: Tk::OptionObj

Inherits:
Hash show all
Includes:
TkUtil
Defined in:
ext/lib/tk/optionobj.rb

Constant Summary

Constants included from TkUtil

TkUtil::None, TkUtil::RELEASE_DATE

Instance Method Summary collapse

Methods included from TkUtil

_conv_args, #_conv_args, #_fromUTF8, _get_eval_enc_str, #_get_eval_enc_str, #_get_eval_string, _get_eval_string, _symbolkey2str, #_symbolkey2str, #_toUTF8, bool, #bool, callback, eval_cmd, #hash_kv, hash_kv, install_cmd, num_or_str, #num_or_str, number, #number, string, #string, uninstall_cmd

Constructor Details

#initialize(hash = nil) ⇒ OptionObj

Returns a new instance of OptionObj.



16
17
18
19
20
# File 'ext/lib/tk/optionobj.rb', line 16

def initialize(hash = nil)
  super()
  @observ = []
  update_without_notify(_symbolkey2str(hash)) if hash
end

Instance Method Details

#+(hash) ⇒ Object



162
163
164
165
166
167
168
169
# File 'ext/lib/tk/optionobj.rb', line 162

def +(hash)
  unless hash.kind_of?(Hash)
    fail ArgumentError, "expect a Hash"
  end
  new_obj = self.dup
  new_obj.update_without_notify(_symbolkey2str(hash))
  new_obj
end

#[](key) ⇒ Object Also known as: cget



186
187
188
# File 'ext/lib/tk/optionobj.rb', line 186

def [](key)
  super(key.to_s)
end

#[]=(key, val) ⇒ Object



196
197
198
# File 'ext/lib/tk/optionobj.rb', line 196

def []=(key, val)
  store(key,val)
end

#assign(*wins) ⇒ Object



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
# File 'ext/lib/tk/optionobj.rb', line 58

def assign(*wins)
  # win := 
  #   widget             #==> call widget.configure(hash)
  #   [widget]           #==> call widget.configure(hash)
  #   [widget, nil, {src=>target, ... }]
  #                      #==> call widget.configure(hash) 
  #                               with converting hash-key
  #   [widget, method]   #==> call widget.method(hash)
  #   [widget, method, {src=>target, ... }]
  #                      #==> call widget.method(hash) 
  #                               with converting hash-key
  #   [widget [receiver, method, arg, ... ]]
  #                      #==> call receiver.method(arg, ... , hash)
  #   [widget [receiver, method, arg, ... ], {src=>target, ... }]
  #                      #==> call receiver.method(arg, ... , hash)
  #                               with onverting hash-key
  #
  # src := option_name_on_optobj
  #
  # target := 
  #   nil                #==> not use the src
  #   option_name_on_target_widget
  #   [ option_name_on_target_widget, ... ]
  #                      #==> set all of them
  #
  wins.each{|win|
    _remove_win(win)
    @observ << win
    notify(win)
  }
  self
end

#configure(key, value = nil) ⇒ Object



178
179
180
181
182
183
184
# File 'ext/lib/tk/optionobj.rb', line 178

def configure(key, value=nil)
  if key.kind_of?(Hash)
    update(key)
  else
    store(key,value)
  end
end

#notify(target = nil) ⇒ Object Also known as: apply



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
# File 'ext/lib/tk/optionobj.rb', line 98

def notify(target = nil)
  if target
    targets = [target]
  elsif @observ.empty?
    return self
  else
    targets = @observ.dup
  end

  return self if empty?

  org_hash = _symbolkey2str(self)

  targets.each{|win|
    widget = receiver = win
    hash = org_hash
    begin
      if win.kind_of?(Array)
        widget, method, conv_tbl = win
        receiver = widget

        if conv_tbl
          hash = {}
          org_hash.each{|key, val|
            key = conv_tbl[key] if conv_tbl.key?(key)
            next unless key
            if key.kind_of?(Array)
              key.each{|k| hash[k] = val}
            else              
              hash[key] = val
            end
          }
        end

        if method.kind_of?(Array)
          receiver, method, *args = method
          receiver.__send__(method, *(args << hash))
        elsif method
          widget.__send__(method, hash)
        else
          widget.configure(hash)
        end

      else
        widget.configure(self)
      end
    rescue => e
      if ( ( widget.kind_of?(TkObject) \
            && widget.respond_to?('exist?') \
            && ! receiver.exist? ) \
        || ( receiver.kind_of?(TkObject) \
            && receiver.respond_to?('exist?') \
            && ! receiver.exist? ) )
        @observ.delete(win)
      else
        fail e
      end
    end
  }

  self
end

#observ_infoObject



22
23
24
# File 'ext/lib/tk/optionobj.rb', line 22

def observ_info
  @observ.dup
end

#observsObject



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

def observs
  @observ.collect{|win|
    if win.kind_of?(Array)
      win[0]
    else
      win
    end
  }
end

#replace(hash) ⇒ Object



200
201
202
203
# File 'ext/lib/tk/optionobj.rb', line 200

def replace(hash)
  super(_symbolkey2str(hash))
  notify
end

#store(key, val) ⇒ Object



191
192
193
194
195
# File 'ext/lib/tk/optionobj.rb', line 191

def store(key, val)
  key = key.to_s
  super(key, val)
  notify
end

#unassign(*wins) ⇒ Object



91
92
93
94
95
96
# File 'ext/lib/tk/optionobj.rb', line 91

def unassign(*wins)
  wins.each{|win|
    _remove_win(win)
  }
  self
end

#update(hash) ⇒ Object



173
174
175
176
# File 'ext/lib/tk/optionobj.rb', line 173

def update(hash)
  update_without_notify(_symbolkey2str(hash))
  notify
end

#update_without_notifyObject



171
# File 'ext/lib/tk/optionobj.rb', line 171

alias update_without_notify update