1#
2#  tkextlib/iwidgets/entryfield.rb
3#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
4#
5
6require 'tk'
7require 'tkextlib/iwidgets.rb'
8
9module Tk
10  module Iwidgets
11    class  Entryfield < Tk::Iwidgets::Labeledwidget
12    end
13  end
14end
15
16class Tk::Iwidgets::Entryfield
17  TkCommandNames = ['::iwidgets::entryfield'.freeze].freeze
18  WidgetClassName = 'Entryfield'.freeze
19  WidgetClassNames[WidgetClassName] ||= self
20
21  def __font_optkeys
22    super() << 'textfont'
23  end
24  private :__font_optkeys
25
26  ####################################
27
28  include Tk::ValidateConfigure
29
30  class EntryfieldValidate < TkValidateCommand
31    #class CalCmdArgs < TkUtil::CallbackSubst
32    class ValidateArgs < TkUtil::CallbackSubst
33      KEY_TBL  = [
34        [ ?c, ?s, :char ],
35        [ ?P, ?s, :post ],
36        [ ?S, ?s, :current ],
37        [ ?W, ?w, :widget ],
38        nil
39      ]
40      PROC_TBL = [
41        [ ?s, TkComm.method(:string) ],
42        [ ?w, TkComm.method(:window) ],
43        nil
44      ]
45
46=begin
47      # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
48      KEY_TBL.map!{|inf|
49        if inf.kind_of?(Array)
50          inf[0] = inf[0].getbyte(0) if inf[0].kind_of?(String)
51          inf[1] = inf[1].getbyte(0) if inf[1].kind_of?(String)
52        end
53        inf
54      }
55
56      PROC_TBL.map!{|inf|
57        if inf.kind_of?(Array)
58          inf[0] = inf[0].getbyte(0) if inf[0].kind_of?(String)
59        end
60        inf
61      }
62=end
63
64      _setup_subst_table(KEY_TBL, PROC_TBL);
65    end
66
67    def self._config_keys
68      ['validate', 'invalid']
69    end
70  end
71
72  def __validation_class_list
73    super() << EntryfieldValidate
74  end
75
76  Tk::ValidateConfigure.__def_validcmd(binding, EntryfieldValidate)
77=begin
78  def validate(cmd = Proc.new, args = nil)
79    if cmd.kind_of?(ValidateCmd)
80      configure('validate', cmd)
81    elsif args
82      configure('validate', [cmd, args])
83    else
84      configure('validate', cmd)
85    end
86  end
87
88  def invalid(cmd = Proc.new, args = nil)
89    if cmd.kind_of?(ValidateCmd)
90      configure('invalid', cmd)
91    elsif args
92      configure('invalid', [cmd, args])
93    else
94      configure('invalid', cmd)
95    end
96  end
97=end
98
99  ####################################
100
101  def clear
102    tk_call(@path, 'clear')
103    self
104  end
105
106  def delete(first, last=None)
107    tk_send_without_enc('delete', first, last)
108    self
109  end
110
111  def value
112    _fromUTF8(tk_send_without_enc('get'))
113  end
114  def value= (val)
115    tk_send_without_enc('delete', 0, 'end')
116    tk_send_without_enc('insert', 0, _get_eval_enc_str(val))
117    val
118  end
119  alias get value
120  alias set value=
121
122  def cursor=(index)
123    tk_send_without_enc('icursor', index)
124    #self
125    index
126  end
127  alias icursor cursor=
128
129  def index(index)
130    number(tk_send_without_enc('index', index))
131  end
132
133  def insert(pos,text)
134    tk_send_without_enc('insert', pos, _get_eval_enc_str(text))
135    self
136  end
137
138  def mark(pos)
139    tk_send_without_enc('scan', 'mark', pos)
140    self
141  end
142  def dragto(pos)
143    tk_send_without_enc('scan', 'dragto', pos)
144    self
145  end
146  def selection_adjust(index)
147    tk_send_without_enc('selection', 'adjust', index)
148    self
149  end
150  def selection_clear
151    tk_send_without_enc('selection', 'clear')
152    self
153  end
154  def selection_from(index)
155    tk_send_without_enc('selection', 'from', index)
156    self
157  end
158  def selection_present()
159    bool(tk_send_without_enc('selection', 'present'))
160  end
161  def selection_range(s, e)
162    tk_send_without_enc('selection', 'range', s, e)
163    self
164  end
165  def selection_to(index)
166    tk_send_without_enc('selection', 'to', index)
167    self
168  end
169
170  # based on tk/scrollable.rb
171  def xview(*index)
172    if index.size == 0
173      list(tk_send_without_enc('xview'))
174    else
175      tk_send_without_enc('xview', *index)
176      self
177    end
178  end
179  def xview_moveto(*index)
180    xview('moveto', *index)
181  end
182  def xview_scroll(*index)
183    xview('scroll', *index)
184  end
185end
186