ext/tk/lib/tkentry.rb


DEFINITIONS

This source file includes following functions.


   1  #
   2  #               tkentry.rb - Tk entry classes
   3  #                       $Date: 2002/06/04 07:48:30 $
   4  #                       by Yukihiro Matsumoto <matz@caelum.co.jp>
   5  
   6  require 'tk.rb'
   7  
   8  class TkEntry<TkLabel
   9    include Scrollable
  10  
  11    WidgetClassName = 'Entry'.freeze
  12    WidgetClassNames[WidgetClassName] = self
  13    def self.to_eval
  14      WidgetClassName
  15    end
  16  
  17    class ValidateCmd
  18      include TkComm
  19  
  20      class ValidateArgs
  21        def initialize(d,i,s,v,pp,ss,vv,ww)
  22          @action = d
  23          @index = i
  24          @current = s
  25          @type = v
  26          @value = pp
  27          @string = ss
  28          @triggered = vv
  29          @widget = ww
  30        end
  31        attr :action
  32        attr :index
  33        attr :current
  34        attr :type
  35        attr :value
  36        attr :string
  37        attr :triggered
  38        attr :widget
  39      end
  40  
  41      def initialize(cmd = Proc.new, args=nil)
  42        if args
  43          @id = install_cmd(proc{|*arg|
  44                              TkUtil.eval_cmd cmd, *arg
  45                            }) + " " + args
  46        else
  47          @id = install_cmd(proc{|arg|
  48                              TkUtil.eval_cmd cmd, ValidateArgs.new(*arg)
  49                            }) + ' %d %i %s %v %P %S %V %W'
  50        end
  51      end
  52  
  53      def to_eval
  54        @id
  55      end
  56    end
  57  
  58    def create_self(keys)
  59      if keys and keys != None
  60        tk_call 'entry', @path, *hash_kv(keys)
  61      else
  62        tk_call 'entry', @path
  63      end
  64    end
  65  
  66    def bbox(index)
  67      tk_send 'bbox', index
  68    end
  69  
  70    def delete(s, e=None)
  71      tk_send 'delete', s, e
  72    end
  73  
  74    def configure(slot, value=None)
  75      if slot.kind_of? Hash
  76        slot = _symbolkey2str(slot)
  77        if slot['vcmd'].kind_of? Array
  78          cmd, *args = slot['vcmd']
  79          slot['vcmd'] = ValidateCmd.new(cmd, args.join(' '))
  80        elsif slot['vcmd'].kind_of? Proc
  81          slot['vcmd'] = ValidateCmd.new(slot['vcmd'])
  82        end
  83        if slot['validatecommand'].kind_of? Array
  84          cmd, *args = slot['validatecommand']
  85          slot['validatecommand'] = ValidateCmd.new(cmd, args.join(' '))
  86        elsif slot['validatecommand'].kind_of? Proc
  87          slot['validatecommand'] = ValidateCmd.new(slot['validatecommand'])
  88        end
  89        if slot['invcmd'].kind_of? Array
  90          cmd, *args = slot['invcmd']
  91          slot['invcmd'] = ValidateCmd.new(cmd, args.join(' '))
  92        elsif slot['invcmd'].kind_of? Proc
  93          slot['invcmd'] = ValidateCmd.new(slot['invcmd'])
  94        end
  95        if slot['invalidcommand'].kind_of? Array
  96          cmd, *args = slot['invalidcommand']
  97          slot['invalidcommand'] = ValidateCmd.new(cmd, args.join(' '))
  98        elsif slot['invalidcommand'].kind_of? Proc
  99          slot['invalidcommand'] = ValidateCmd.new(slot['invalidcommand'])
 100        end
 101        super(slot)
 102      else
 103        if (slot == 'vcmd' || slot == :vcmd || 
 104            slot == 'validatecommand' || slot == :validatecommand || 
 105            slot == 'invcmd' || slot == :invcmd || 
 106            slot == 'invalidcommand' || slot == :invalidcommand)
 107          if value.kind_of? Array
 108            cmd, *args = value
 109            value = ValidateCmd.new(cmd, args.join(' '))
 110          elsif value.kind_of? Proc
 111            value = ValidateCmd.new(value)
 112          end
 113        end
 114        super(slot, value)
 115      end
 116    end
 117  
 118    def cursor
 119      tk_send 'index', 'insert'
 120    end
 121    def cursor=(index)
 122      tk_send 'icursor', index
 123    end
 124    def index(index)
 125      number(tk_send('index', index))
 126    end
 127    def insert(pos,text)
 128      tk_send 'insert', pos, text
 129    end
 130    def mark(pos)
 131      tk_send 'scan', 'mark', pos
 132    end
 133    def dragto(pos)
 134      tk_send 'scan', 'dragto', pos
 135    end
 136    def selection_adjust(index)
 137      tk_send 'selection', 'adjust', index
 138    end
 139    def selection_clear
 140      tk_send 'selection', 'clear'
 141    end
 142    def selection_from(index)
 143      tk_send 'selection', 'from', index
 144    end
 145    def selection_present()
 146      bool(tk_send('selection', 'present'))
 147    end
 148    def selection_range(s, e)
 149      tk_send 'selection', 'range', s, e
 150    end
 151    def selection_to(index)
 152      tk_send 'selection', 'to', index
 153    end
 154  
 155    def validate(mode = nil)
 156      if mode
 157        configure 'validate', mode
 158      else
 159        if tk_send('validate') == '0'
 160          false
 161        else 
 162          true
 163        end
 164      end
 165    end
 166  
 167    def validatecommand(cmd = ValidateCmd.new, args = nil)
 168      if cmd.kind_of?(ValidateCmd)
 169        configure('validatecommand', cmd)
 170      else
 171        configure('validatecommand', ValidateCmd.new(cmd, args))
 172      end
 173    end
 174    alias vcmd validatecommand
 175  
 176    def invalidcommand(cmd = ValidateCmd.new, args = nil)
 177      if cmd.kind_of?(ValidateCmd)
 178        configure('invalidcommand', cmd)
 179      else
 180        configure('invalidcommand', ValidateCmd.new(cmd, args))
 181      end
 182    end
 183    alias invcmd invalidcommand
 184  
 185    def value
 186      tk_send 'get'
 187    end
 188    def value= (val)
 189      tk_send 'delete', 0, 'end'
 190      tk_send 'insert', 0, val
 191    end
 192  end
 193  
 194  class TkSpinbox<TkEntry
 195    WidgetClassName = 'Spinbox'.freeze
 196    WidgetClassNames[WidgetClassName] = self
 197    def self.to_eval
 198      WidgetClassName
 199    end
 200  
 201    def create_self(keys)
 202      if keys and keys != None
 203        tk_call 'spinbox', @path, *hash_kv(keys)
 204      else
 205        tk_call 'spinbox', @path
 206      end
 207    end
 208  
 209    def identify(x, y)
 210      tk_send 'identify', x, y
 211    end
 212  
 213    def spinup
 214      tk_send 'invoke', 'spinup'
 215    end
 216  
 217    def spindown
 218      tk_send 'invoke', 'spindown'
 219    end
 220  
 221    def set(str)
 222      tk_send 'set', str
 223    end
 224  end