Module: EventBinder
- Defined in:
- lib/xrc_pepper.rb
Overview
Easily bind method named after this pattern: on_#control_name_#event_name to the corresponding event. Injected into Wx::EvtHandler
Instance Method Summary collapse
-
#_bind(window, events) ⇒ Object
A little more rubyesque version of wxSugar#listen method Pass the window (control) to be bound and then a hash with event name as keys and method name as value (You may also pass a Proc as value).
- #bind_events ⇒ Object
- #bind_menus ⇒ Object
- #bind_standard_buttons ⇒ Object
- #load_and_bind(options = {}) ⇒ Object
Instance Method Details
#_bind(window, events) ⇒ Object
A little more rubyesque version of wxSugar#listen method Pass the window (control) to be bound and then a hash with event name as keys and method name as value (You may also pass a Proc as value)
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/xrc_pepper.rb', line 114 def _bind(window, events) events.each do |event, method| case method when Symbol listen(event, window, method) when Proc listen(event, window, nil, &method) end end end |
#bind_events ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/xrc_pepper.rb', line 140 def bind_events # get all methods that are named 'on_*' methods = self.methods.select{|method| method[0..2] == "on_" } # for each control in this window each do |control| # iterate over the methods that target the current control (named 'on_#{control.name}_*') methods.grep(Regexp.new("^on_#{control.name}_(.*)")) {|method| [$1,method]}.each do |event,method| # bind the found method with the event listen(event, control, method) end end end |
#bind_menus ⇒ Object
154 155 156 157 158 159 160 |
# File 'lib/xrc_pepper.rb', line 154 def # get all methods that are named 'on_*_menu' self.methods.grep(/^on_(.*)_menu$/) {|method| [$1,method]}.each do |name,method| # bind the found method with the event (Wx::xrcid(name), method) end end |
#bind_standard_buttons ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/xrc_pepper.rb', line 125 def # bind the close button (something like a cross on top-right corner) if respond_to? :on_close arity = method(:on_close).arity evt_close {|event| arity == 0 ? on_close : on_close(event)} end # bind the buttons with standard wxID_* select{|item| item.kind_of?(Wx::Button) && item.name[0..4] == "wxID_"}.each do || method_name = "on_#{.name[5..-1].downcase}" arity = method(method_name).arity (.get_id) {|event| arity == 0 ? send(method_name) : send(method_name, event)} if respond_to? method_name end end |
#load_and_bind(options = {}) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/xrc_pepper.rb', line 101 def load_and_bind( = {}) [:name] ||= self.class.to_s [:class] ||= "wx" + self.class.ancestors.find {|ancestor| ancestor.to_s =~ /Wxruby2::/}.to_s.split("Wxruby2::")[1] Xrc.instance.bind_object_subclass(self, [:parent], [:name], [:class]) # needed anymore? Xrc.instance.bind_menu_bar(self, options[:menu]) if options[:menu] end |