1#
2# tk/scale.rb : treat scale widget
3#
4require 'tk'
5
6class Tk::Scale<TkWindow
7  TkCommandNames = ['scale'.freeze].freeze
8  WidgetClassName = 'Scale'.freeze
9  WidgetClassNames[WidgetClassName] ||= self
10
11  def create_self(keys)
12    if keys and keys != None
13      if keys.key?('command') && ! keys['command'].kind_of?(String)
14        cmd = keys.delete('command')
15        keys['command'] = proc{|val| cmd.call(val.to_f)}
16      end
17      unless TkConfigMethod.__IGNORE_UNKNOWN_CONFIGURE_OPTION__
18        #tk_call_without_enc('scale', @path, *hash_kv(keys, true))
19        tk_call_without_enc(self.class::TkCommandNames[0], @path,
20                            *hash_kv(keys, true))
21      else
22        begin
23          tk_call_without_enc(self.class::TkCommandNames[0], @path,
24                              *hash_kv(keys, true))
25        rescue
26          tk_call_without_enc(self.class::TkCommandNames[0], @path)
27          keys = __check_available_configure_options(keys)
28          unless keys.empty?
29            begin
30              tk_call_without_enc('destroy', @path)
31            rescue
32              # cannot destroy
33              configure(keys)
34            else
35              # re-create widget
36              tk_call_without_enc(self.class::TkCommandNames[0], @path,
37                                  *hash_kv(keys, true))
38            end
39          end
40        end
41      end
42    else
43      #tk_call_without_enc('scale', @path)
44      tk_call_without_enc(self.class::TkCommandNames[0], @path)
45    end
46  end
47  private :create_self
48
49  def __strval_optkeys
50    super() << 'label'
51  end
52  private :__strval_optkeys
53
54  def _wrap_command_arg(cmd)
55    proc{|val|
56      if val.kind_of?(String)
57        cmd.call(number(val))
58      else
59        cmd.call(val)
60      end
61    }
62  end
63  private :_wrap_command_arg
64
65  def configure_cmd(slot, value)
66    configure(slot=>value)
67  end
68
69  def configure(slot, value=None)
70    if (slot == 'command' || slot == :command)
71      configure('command'=>value)
72    elsif slot.kind_of?(Hash) &&
73        (slot.key?('command') || slot.key?(:command))
74      slot = _symbolkey2str(slot)
75      slot['command'] = _wrap_command_arg(slot.delete('command'))
76    end
77    super(slot, value)
78  end
79
80  def command(cmd=Proc.new)
81    configure('command'=>cmd)
82  end
83
84  def get(x=None, y=None)
85    number(tk_send_without_enc('get', x, y))
86  end
87
88  def coords(val=None)
89    tk_split_list(tk_send_without_enc('coords', val))
90  end
91
92  def identify(x, y)
93    tk_send_without_enc('identify', x, y)
94  end
95
96  def set(val)
97    tk_send_without_enc('set', val)
98  end
99
100  def value
101    get
102  end
103
104  def value= (val)
105    set(val)
106    val
107  end
108end
109
110#TkScale = Tk::Scale unless Object.const_defined? :TkScale
111#Tk.__set_toplevel_aliases__(:Tk, Tk::Scale, :TkScale)
112Tk.__set_loaded_toplevel_aliases__('tk/scale.rb', :Tk, Tk::Scale, :TkScale)
113