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

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_eventsObject



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_menusObject



154
155
156
157
158
159
160
# File 'lib/xrc_pepper.rb', line 154

def bind_menus
  # 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
    evt_menu(Wx::xrcid(name), method)
  end
end

#bind_standard_buttonsObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/xrc_pepper.rb', line 125

def bind_standard_buttons
  # 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 |button|
    method_name = "on_#{button.name[5..-1].downcase}"
    arity = method(method_name).arity
    evt_button(button.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(options = {})
  options[:name] ||= self.class.to_s
  options[:class] ||= "wx" + self.class.ancestors.find {|ancestor| ancestor.to_s =~ /Wxruby2::/}.to_s.split("Wxruby2::")[1]
  Xrc.instance.bind_object_subclass(self, options[:parent], options[:name], options[:class])
  
  # needed anymore? Xrc.instance.bind_menu_bar(self, options[:menu]) if options[:menu]
end