1#
2#               tk/spinbox.rb - Tk spinbox classes
3#                       by Yukihiro Matsumoto <matz@caelum.co.jp>
4#
5require 'tk'
6require 'tk/entry'
7
8class Tk::Spinbox<Tk::Entry
9  TkCommandNames = ['spinbox'.freeze].freeze
10  WidgetClassName = 'Spinbox'.freeze
11  WidgetClassNames[WidgetClassName] ||= self
12
13  class SpinCommand < TkValidateCommand
14    class ValidateArgs < TkUtil::CallbackSubst
15      KEY_TBL = [
16        [ ?d, ?s, :direction ],
17        [ ?s, ?e, :current ],
18        [ ?W, ?w, :widget ],
19        nil
20      ]
21
22      PROC_TBL = [
23        [ ?s, TkComm.method(:string) ],
24        [ ?w, TkComm.method(:window) ],
25
26        [ ?e, proc{|val|
27            #enc = Tk.encoding
28            enc = ((Tk.encoding)? Tk.encoding : Tk.encoding_system)
29            if enc
30              Tk.fromUTF8(TkComm::string(val), enc)
31            else
32              TkComm::string(val)
33            end
34          }
35        ],
36
37        nil
38      ]
39
40=begin
41      # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
42      KEY_TBL.map!{|inf|
43        if inf.kind_of?(Array)
44          inf[0] = inf[0].getbyte(0) if inf[0].kind_of?(String)
45          inf[1] = inf[1].getbyte(0) if inf[1].kind_of?(String)
46        end
47        inf
48      }
49
50      PROC_TBL.map!{|inf|
51        if inf.kind_of?(Array)
52          inf[0] = inf[0].getbyte(0) if inf[0].kind_of?(String)
53        end
54        inf
55      }
56=end
57
58      _setup_subst_table(KEY_TBL, PROC_TBL);
59
60      def self.ret_val(val)
61        (val)? '1': '0'
62      end
63    end
64
65    def self._config_keys
66      ['command']
67    end
68  end
69
70  def __validation_class_list
71    super() << SpinCommand
72  end
73
74  Tk::ValidateConfigure.__def_validcmd(binding, SpinCommand)
75
76  #def create_self(keys)
77  #  tk_call_without_enc('spinbox', @path)
78  #  if keys and keys != None
79  #    configure(keys)
80  #  end
81  #end
82  #private :create_self
83
84  def __boolval_optkeys
85    super() << 'wrap'
86  end
87  private :__boolval_optkeys
88
89  def __strval_optkeys
90    super() << 'buttonbackground' << 'format'
91  end
92  private :__strval_optkeys
93
94  def __listval_optkeys
95    super() << 'values'
96  end
97  private :__listval_optkeys
98
99  def identify(x, y)
100    tk_send_without_enc('identify', x, y)
101  end
102
103  def invoke(elem)
104    tk_send_without_enc('invoke', elem)
105    self
106  end
107
108  def spinup
109    begin
110      tk_send_without_enc('invoke', 'buttonup')
111    rescue RuntimeError => e
112      # old version of element?
113      begin
114        tk_send_without_enc('invoke', 'spinup')
115      rescue
116        fail e
117      end
118    end
119    self
120  end
121
122  def spindown
123    begin
124      tk_send_without_enc('invoke', 'buttondown')
125    rescue RuntimeError => e
126      # old version of element?
127      begin
128        tk_send_without_enc('invoke', 'spinup')
129      rescue
130        fail e
131      end
132    end
133    self
134  end
135
136  def set(str)
137    _fromUTF8(tk_send_without_enc('set', _get_eval_enc_str(str)))
138  end
139end
140
141#TkSpinbox = Tk::Spinbox unless Object.const_defined? :TkSpinbox
142#Tk.__set_toplevel_aliases__(:Tk, Tk::Spinbox, :TkSpinbox)
143Tk.__set_loaded_toplevel_aliases__('tk/spinbox.rb', :Tk, Tk::Spinbox,
144                                   :TkSpinbox)
145