Class: Button_clone

Inherits:
TkLabel show all
Defined in:
sample/binding_sample.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Button_clone

Returns a new instance of Button_clone.



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
# File 'sample/binding_sample.rb', line 7

def initialize(*args)
  @command = nil

  if args[-1].kind_of?(Hash)
    keys = _symbolkey2str(args.pop)
    @command = keys.delete('command')

    keys['highlightthickness'] = 1 unless keys.key?('highlightthickness')
    keys['padx'] = '3m' unless keys.key?('padx')
    keys['pady'] = '1m' unless keys.key?('pady')
    keys['relief'] = 'raised' unless keys.key?('relief')

    args.push(keys)
  end

  super(*args)

  @press = false

  self.bind('Enter', proc{self.background(self.activebackground)})
  self.bind('Leave', proc{
              @press = false
              self.background(self.highlightbackground)
              self.relief('raised')
            })

  self.bind('ButtonPress-1', proc{@press = true; self.relief('sunken')})
  self.bind('ButtonRelease-1', proc{
              self.relief('raised')
              @command.call if @press && @command
              @press = false
            })
end

Instance Method Details

#command(cmd = Proc.new) ⇒ Object



41
42
43
# File 'sample/binding_sample.rb', line 41

def command(cmd = Proc.new)
  @command = cmd
end

#invokeObject



45
46
47
48
49
50
51
# File 'sample/binding_sample.rb', line 45

def invoke
  if @command
    @command.call
  else
    ''
  end
end