summaryrefslogtreecommitdiff
path: root/ext/tk/lib/tk/validation.rb
blob: 70e84e6cbfb793f41de12ce4428781e35708583a (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
#
#  tk/validation.rb - validation support module for entry, spinbox, and so on
#
require 'tk'

module TkValidation
  class ValidateCmd
    include TkComm

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

    class ValidateArgs < TkUtil::CallbackSubst
      key_tbl = [
	[ ?d, ?n, :action ], 
	[ ?i, ?x, :index ], 
	[ ?s, ?e, :current ], 
	[ ?v, ?s, :type ], 
	[ ?P, ?e, :value ], 
	[ ?S, ?e, :string ], 
	[ ?V, ?s, :triggered ], 
	[ ?W, ?w, :widget ], 
	nil
      ]

      proc_tbl = [
	[ ?n, TkComm.method(:number) ], 
	[ ?s, TkComm.method(:string) ], 
	[ ?w, TkComm.method(:window) ], 

	[ ?e, proc{|val|
	    enc = Tk.encoding
	    if enc
	      Tk.fromUTF8(TkComm::string(val), enc)
	    else
	      TkComm::string(val)
	    end
	  }
	], 

	[ ?x, proc{|val|
	    idx = TkComm::number(val)
	    if idx < 0
	      nil
	    else
	      idx
	    end
	  }
	], 

	nil
      ]

      _setup_subst_table(key_tbl, proc_tbl);
    end

    def initialize(cmd = Proc.new, *args)
      if args.compact.size > 0
	args = args.join(' ')
	keys = ValidateArgs._get_subst_key(args)
	if cmd.kind_of?(String)
	  id = cmd
	elsif cmd.kind_of?(TkCallbackEntry)
	  @id = install_cmd(cmd)
	else
	  @id = install_cmd(proc{|*arg|
	     (cmd.call(*ValidateArgs.scan_args(keys, arg)))? '1':'0'
	  }) + ' ' + args
	end
      else
	keys, args = ValidateArgs._get_all_subst_keys
	if cmd.kind_of?(String)
	  id = cmd
	elsif cmd.kind_of?(TkCallbackEntry)
	  @id = install_cmd(cmd)
	else
	  @id = install_cmd(proc{|*arg|
	     (cmd.call(
                ValidateArgs.new(*ValidateArgs.scan_args(keys,arg)))
             )? '1': '0'
	  }) + ' ' + args
	end
      end
    end

    def to_eval
      @id
    end
  end

  #####################################

  def configure(slot, value=TkComm::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 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
end