Class: TkComponent::Builder::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/tk_component/builder/event.rb

Constant Summary collapse

EVENT_ATTRS =
"%x %y %X %Y %b %D %A %k %d"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, sender) ⇒ Event

Returns a new instance of Event.



18
19
20
21
# File 'lib/tk_component/builder/event.rb', line 18

def initialize(name, sender)
  @name = name.to_sym
  @sender = sender
end

Instance Attribute Details

#button_indexObject

Returns the value of attribute button_index.



6
7
8
# File 'lib/tk_component/builder/event.rb', line 6

def button_index
  @button_index
end

#dataObject

Returns the value of attribute data.



14
15
16
# File 'lib/tk_component/builder/event.rb', line 14

def data
  @data
end

#key_codeObject

Returns the value of attribute key_code.



7
8
9
# File 'lib/tk_component/builder/event.rb', line 7

def key_code
  @key_code
end

#key_stringObject

Returns the value of attribute key_string.



8
9
10
# File 'lib/tk_component/builder/event.rb', line 8

def key_string
  @key_string
end

#mouse_wheel_deltaObject

Returns the value of attribute mouse_wheel_delta.



13
14
15
# File 'lib/tk_component/builder/event.rb', line 13

def mouse_wheel_delta
  @mouse_wheel_delta
end

#mouse_xObject

Returns the value of attribute mouse_x.



9
10
11
# File 'lib/tk_component/builder/event.rb', line 9

def mouse_x
  @mouse_x
end

#mouse_yObject

Returns the value of attribute mouse_y.



10
11
12
# File 'lib/tk_component/builder/event.rb', line 10

def mouse_y
  @mouse_y
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/tk_component/builder/event.rb', line 4

def name
  @name
end

#root_mouse_xObject

Returns the value of attribute root_mouse_x.



11
12
13
# File 'lib/tk_component/builder/event.rb', line 11

def root_mouse_x
  @root_mouse_x
end

#root_mouse_yObject

Returns the value of attribute root_mouse_y.



12
13
14
# File 'lib/tk_component/builder/event.rb', line 12

def root_mouse_y
  @root_mouse_y
end

#senderObject

Returns the value of attribute sender.



5
6
7
# File 'lib/tk_component/builder/event.rb', line 5

def sender
  @sender
end

Class Method Details

.bind_command(name, sender, options, lambda) ⇒ Object



27
28
29
30
31
32
# File 'lib/tk_component/builder/event.rb', line 27

def self.bind_command(name, sender, options, lambda)
  sender.native_item.command do
    event = self.new(name, sender)
    lambda.call(event)
  end
end

.bind_event(name, sender, options, lambda, pre_lambda = nil, post_lambda = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tk_component/builder/event.rb', line 42

def self.bind_event(name, sender, options, lambda, pre_lambda = nil, post_lambda = nil)
  event_string = self.event_string_for(name, options)
  handler = proc do |x, y, rx, ry, bi, mw, ks, kc, data|
    event = self.new(name, sender)
    event.mouse_x = x
    event.mouse_y = y
    event.root_mouse_x = rx
    event.root_mouse_y = ry
    event.button_index = bi
    event.mouse_wheel_delta = mw
    event.key_string = ks
    event.key_code = kc
    event.data = data
    # The pre_lambda returns true if it wants to prevent the event from firing
    return if pre_lambda.present? && pre_lambda.call(event)
    lambda.call(event)
    post_lambda.call(event) if post_lambda.present?
  end
  sender.native_item.bind(event_string, handler, EVENT_ATTRS)
end

.bind_variable(name, sender, options, lambda) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/tk_component/builder/event.rb', line 34

def self.bind_variable(name, sender, options, lambda)
  handler = proc do
    event = self.new(name, sender)
    lambda.call(event)
  end
  sender.tk_variable.trace('write', handler)
end

.emit(name, source, data) ⇒ Object



23
24
25
# File 'lib/tk_component/builder/event.rb', line 23

def self.emit(name, source, data)
  Tk.event_generate(source, "<#{name}>", data: data)
end

Instance Method Details

#data_objectObject



63
64
65
66
67
68
69
# File 'lib/tk_component/builder/event.rb', line 63

def data_object
  @data_object ||= begin
                     ObjectSpace._id2ref(self.data.to_i)
                   rescue
                     nil
                   end
end