Module: Wx::Extensions::EasyMenu

Includes:
EasyMenuName
Included in:
Menu
Defined in:
lib/wx_sugar/menu.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EasyMenuName

#clean_name, #internal_name

Instance Attribute Details

#sourceObject

The target window for menu events from this source



77
78
79
# File 'lib/wx_sugar/menu.rb', line 77

def source
  @source
end

#targetObject

The target window for menu events from this source



77
78
79
# File 'lib/wx_sugar/menu.rb', line 77

def target
  @target
end

Class Method Details

.next_idObject



72
73
74
# File 'lib/wx_sugar/menu.rb', line 72

def self.next_id()
  @base_id = @base_id ? @base_id + 1 : 2000
end

Instance Method Details

#[](ident) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/wx_sugar/menu.rb', line 86

def [](ident)
  case ident
  when String, Symbol
    menu_id = menu_ids[internal_name(ident)] or
      raise RuntimeError, "Not found #{ident} #{menu_ids.inspect}"
  when Fixnum
    menu_id = ident
  end
  # find_item(menu_id)
end

#add(command_str, options = {}, &block) ⇒ Object

adds the item command_str to the menu, with the optional shortcut key command_key, and sets it to run the associated block when the menu item is chosen. The menu item can later be referred to by the symbol with the commands name with special characters removed andspaces turned to underscores. For example, “Save Project” becomes :save_project

The handler event can be specified in a number of ways:

  1. Passing a block, which can optionally receive an event parameter

  2. Passing a :handler option, which can be a method name in the target, or a Proc or BoundMethod object.

  3. Simply defining a method in the target called on_xxxx, where xxxx is the lower-case name of the menu item with special characters removed.

So, if creating a menu item called ‘Save Project’, simply define a method called on_save_project in the event target.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/wx_sugar/menu.rb', line 113

def add(command_str, options = {}, &block)
  ident = internal_name(command_str)
  const = options[:sys_id] || self.next_id()
  menu_ids[ident] = const
  
  # Find out how the handler is defined:
  # Using an explicit block?
  if block
    handler = block
  # Specifying a :handler in the options hash
  elsif meth = options[:handler]
    # .. a method object?
    if meth.respond_to?(:call)
      handler = meth
    # .. or a method name?
    else
      handler = target.method(meth)
    end
  # or else try guessing name from among target's methods
  else
    handler = target.method('on_' << ident)
  end
  
  if options[:checked]
    itemtype = Wx::ITEM_CHECK
  elsif options[:radio]
    itemtype = Wx::ITEM_RADIO
  else
    itemtype = Wx::ITEM_NORMAL
  end
  
  command_key = options[:shortcut] || ''
  append(const, "#{command_str}\t#{command_key}", "", itemtype)
  # define the WxWidgets handler, passing a event object if required
  if handler.arity == 1
    source.evt_menu(const) { | e | handler.call(e) }
  else
    source.evt_menu(const) { handler.call() }
  end
  return ident
end

#add_separatorObject



155
156
157
# File 'lib/wx_sugar/menu.rb', line 155

def add_separator
  append_separator
end

#check_items(*idents) ⇒ Object Also known as: check_item



170
171
172
# File 'lib/wx_sugar/menu.rb', line 170

def check_items(*idents)
  idents.each { | ident | self.check( self[ident], true ) }
end

#disable_items(*idents) ⇒ Object Also known as: disable_item



165
166
167
# File 'lib/wx_sugar/menu.rb', line 165

def disable_items(*idents)
  idents.each { | ident | self.enable( self[ident], false ) }
end

#enable_items(*idents) ⇒ Object Also known as: enable_item

pass a series of symbol idents eg :save_project, :close



160
161
162
# File 'lib/wx_sugar/menu.rb', line 160

def enable_items(*idents)
  idents.each { | ident | self.enable( self[ident], true ) }
end


78
79
80
# File 'lib/wx_sugar/menu.rb', line 78

def menu_ids(*args)
  @menu_ids ||= {}
end

#next_idObject



82
83
84
# File 'lib/wx_sugar/menu.rb', line 82

def next_id()
  EasyMenu.next_id()
end

#uncheck_items(*idents) ⇒ Object Also known as: uncheck_item



175
176
177
# File 'lib/wx_sugar/menu.rb', line 175

def uncheck_items(*idents)
  idents.each { | ident | self.check( self[ident], false ) }
end