summaryrefslogtreecommitdiff
path: root/ext/tk/lib/tkentry.rb
blob: fe9a7b62772c37db3bdc57e759d40dceb4fcfdeb (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#
#		tkentry.rb - Tk entry classes
#			$Date$
#			by Yukihiro Matsumoto <matz@caelum.co.jp>

require 'tk.rb'

class TkEntry<TkLabel
  include Scrollable

  TkCommandNames = ['entry'.freeze].freeze
  WidgetClassName = 'Entry'.freeze
  WidgetClassNames[WidgetClassName] = self

  class ValidateCmd
    include TkComm

    module Action
      Insert = 1
      Delete = 0
      Others = -1
      Focus  = -1
      Forced = -1
      Textvariable = -1
      TextVariable = -1
    end

    class ValidateArgs
      VARG_KEY  = 'disvPSVW'
      VARG_TYPE = 'nxeseesw'

      def self.scan_args(arg_str, arg_val)
	enc = Tk.encoding
	arg_cnv = []
	arg_str.strip.split(/\s+/).each_with_index{|kwd,idx|
	  if kwd =~ /^%(.)$/
	    if num = VARG_KEY.index($1)
	      case VARG_TYPE[num]
	      when ?n
		arg_cnv << TkComm::number(arg_val[idx])
	      when ?s
		arg_cnv << TkComm::string(arg_val[idx])
	      when ?e
		if enc
		  arg_cnv << Tk.fromUTF8(TkComm::string(arg_val[idx]), enc)
		else
		  arg_cnv << TkComm::string(arg_val[idx])
		end
	      when ?w
		arg_cnv << TkComm::window(arg_val[idx])
	      when ?x
		idx = TkComm::number(arg_val[idx])
		if idx < 0
		  arg_cnv << nil
		else
		  arg_cnv << idx
		end
	      else
		arg_cnv << arg_val[idx]
	      end
	    else
	      arg_cnv << arg_val[idx]
	    end
	  else
	    arg_cnv << arg_val[idx]
	  end
	}
	arg_cnv
      end

      def initialize(d,i,s,v,pp,ss,vv,ww)
	@action = d
	@index = i
	@current = s
	@type = v
	@value = pp
	@string = ss
	@triggered = vv
	@widget = ww
      end
      attr :action
      attr :index
      attr :current
      attr :type
      attr :value
      attr :string
      attr :triggered
      attr :widget
    end

    def initialize(cmd = Proc.new, args=nil)
      if args
	@id = 
	  install_cmd(proc{|*arg|
			TkUtil.eval_cmd(proc{|*v| (cmd.call(*v))? '1': '0'}, 
					*ValidateArgs.scan_args(args, arg))
		      }) + " " + args
      else
	args = ' %d %i %s %v %P %S %V %W'
	@id = 
	  install_cmd(proc{|*arg|
			TkUtil.eval_cmd(proc{|*v| (cmd.call(*v))? '1': '0'}, 
					ValidateArgs.new(*ValidateArgs \
							 .scan_args(args,arg)))
	  }) + args
      end
    end

    def to_eval
      @id
    end
  end

  def create_self(keys)
    tk_call 'entry', @path
    if keys and keys != None
      configure(keys)
    end
  end
  private :create_self

  def configure(slot, value=None)
    if slot.kind_of? Hash
      slot = _symbolkey2str(slot)
      if slot['vcmd'].kind_of? Array
	cmd, *args = slot['vcmd']
	slot['vcmd'] = ValidateCmd.new(cmd, args.join(' '))
      elsif slot['vcmd'].kind_of? Proc
	slot['vcmd'] = ValidateCmd.new(slot['vcmd'])
      end
      if slot['validatecommand'].kind_of? Array
	cmd, *args = slot['validatecommand']
	slot['validatecommand'] = ValidateCmd.new(cmd, args.join(' '))
      elsif slot['validatecommand'].kind_of? Proc
	slot['validatecommand'] = ValidateCmd.new(slot['validatecommand'])
      end
      if slot['invcmd'].kind_of? Array
	cmd, *args = slot['invcmd']
	slot['invcmd'] = ValidateCmd.new(cmd, args.join(' '))
      elsif slot['invcmd'].kind_of? Proc
	slot['invcmd'] = ValidateCmd.new(slot['invcmd'])
      end
      if slot['invalidcommand'].kind_of? Array
	cmd, *args = slot['invalidcommand']
	slot['invalidcommand'] = ValidateCmd.new(cmd, args.join(' '))
      elsif slot['invalidcommand'].kind_of? Proc
	slot['invalidcommand'] = ValidateCmd.new(slot['invalidcommand'])
      end
      super(slot)
    else
      if (slot == 'vcmd' || slot == :vcmd || 
          slot == 'validatecommand' || slot == :validatecommand || 
	  slot == 'invcmd' || slot == :invcmd || 
          slot == 'invalidcommand' || slot == :invalidcommand)
	if value.kind_of? Array
	  cmd, *args = value
	  value = ValidateCmd.new(cmd, args.join(' '))
	elsif value.kind_of? Proc
	  value = ValidateCmd.new(value)
	end
      end
      super(slot, value)
    end
    self
  end

  def bbox(index)
    list(tk_send('bbox', index))
  end
  def cursor
    number(tk_send('index', 'insert'))
  end
  def cursor=(index)
    tk_send 'icursor', index
    self
  end
  def index(index)
    number(tk_send('index', index))
  end
  def insert(pos,text)
    tk_send 'insert', pos, text
    self
  end
  def delete(first, last=None)
    tk_send 'delete', first, last
    self
  end
  def mark(pos)
    tk_send 'scan', 'mark', pos
    self
  end
  def dragto(pos)
    tk_send 'scan', 'dragto', pos
    self
  end
  def selection_adjust(index)
    tk_send 'selection', 'adjust', index
    self
  end
  def selection_clear
    tk_send 'selection', 'clear'
    self
  end
  def selection_from(index)
    tk_send 'selection', 'from', index
    self
  end
  def selection_present()
    bool(tk_send('selection', 'present'))
  end
  def selection_range(s, e)
    tk_send 'selection', 'range', s, e
    self
  end
  def selection_to(index)
    tk_send 'selection', 'to', index
    self
  end

  def invoke_validate
    bool(tk_send('validate'))
  end
  def validate(mode = nil)
    if mode
      configure 'validate', mode
    else
      invoke_validate
    end
  end

  def validatecommand(cmd = Proc.new, args = nil)
    if cmd.kind_of?(ValidateCmd)
      configure('validatecommand', cmd)
    elsif args
      configure('validatecommand', [cmd, args])
    else
      configure('validatecommand', cmd)
    end
  end
  alias vcmd validatecommand

  def invalidcommand(cmd = Proc.new, args = nil)
    if cmd.kind_of?(ValidateCmd)
      configure('invalidcommand', cmd)
    elsif args
      configure('invalidcommand', [cmd, args])
    else
      configure('invalidcommand', cmd)
    end
  end
  alias invcmd invalidcommand

  def value
    tk_send 'get'
  end
  def value= (val)
    tk_send 'delete', 0, 'end'
    tk_send 'insert', 0, val
  end
end

class TkSpinbox<TkEntry
  TkCommandNames = ['spinbox'.freeze].freeze
  WidgetClassName = 'Spinbox'.freeze
  WidgetClassNames[WidgetClassName] = self

  def create_self(keys)
    tk_call 'spinbox', @path
    if keys and keys != None
      configure(keys)
    end
  end
  private :create_self

  def identify(x, y)
    tk_send 'identify', x, y
  end

  def spinup
    tk_send 'invoke', 'spinup'
    self
  end

  def spindown
    tk_send 'invoke', 'spindown'
    self
  end

  def set(str)
    tk_send 'set', str
  end
end