ext/tk/lib/tkvirtevent.rb


DEFINITIONS

This source file includes following functions.


   1  #
   2  #   tkvirtevent.rb : treats virtual events
   3  #                     1998/07/16 by Hidetoshi Nagai <nagai@ai.kyutech.ac.jp>
   4  #
   5  require 'tk'
   6  
   7  class TkVirtualEvent<TkObject
   8    extend Tk
   9  
  10    TkVirtualEventID = [0]
  11    TkVirtualEventTBL = {}
  12  
  13    class PreDefVirtEvent<self
  14      def initialize(event)
  15        @path = @id = event
  16        TkVirtualEvent::TkVirtualEventTBL[@id] = self
  17      end
  18    end
  19  
  20    def TkVirtualEvent.getobj(event)
  21      obj = TkVirtualEventTBL[event]
  22      if obj
  23        obj
  24      else
  25        if tk_call('event', 'info').index("<#{event}>")
  26          PreDefVirtEvent.new(event)
  27        else
  28          fail ArgumentError, "undefined virtual event '<#{event}>'"
  29        end
  30      end
  31    end
  32  
  33    def TkVirtualEvent.info
  34      tk_call('event', 'info').split(/\s+/).collect!{|seq|
  35        TkVirtualEvent.getobj(seq[1..-2])
  36      }
  37    end
  38  
  39    def initialize(*sequences)
  40      @path = @id = format("<VirtEvent%.4d>", TkVirtualEventID[0])
  41      TkVirtualEventID[0] += 1
  42      add(*sequences)
  43    end
  44  
  45    def add(*sequences)
  46      if sequences != []
  47        tk_call('event', 'add', "<#{@id}>", 
  48                *(sequences.collect{|seq| "<#{tk_event_sequence(seq)}>"}) )
  49        TkVirtualEventTBL[@id] = self
  50      end
  51      self
  52    end
  53  
  54    def delete(*sequences)
  55      if sequences == []
  56        tk_call('event', 'delete', "<#{@id}>")
  57        TkVirtualEventTBL[@id] = nil
  58      else
  59        tk_call('event', 'delete', "<#{@id}>", 
  60                *(sequences.collect{|seq| "<#{tk_event_sequence(seq)}>"}) )
  61        TkVirtualEventTBL[@id] = nil if info == []
  62      end
  63      self
  64    end
  65  
  66    def info
  67      tk_call('event', 'info', "<#{@id}>").split(/\s+/).collect!{|seq|
  68        l = seq.scan(/<*[^<>]+>*/).collect!{|subseq|
  69          case (subseq)
  70          when /^<<[^<>]+>>$/
  71            TkVirtualEvent.getobj(subseq[1..-2])
  72          when /^<[^<>]+>$/
  73            subseq[1..-2]
  74          else
  75            subseq.split('')
  76          end
  77        }.flatten
  78        (l.size == 1) ? l[0] : l
  79      }
  80    end
  81  end