Module: Wx::Extensions::EasyMenu
Instance Attribute Summary collapse
-
#source ⇒ Object
The target window for menu events from this source.
-
#target ⇒ Object
The target window for menu events from this source.
Class Method Summary collapse
Instance Method Summary collapse
- #[](ident) ⇒ Object
-
#add(command_str, options = {}, &block) ⇒ Object
adds the item
command_str
to the menu, with the optional shortcut keycommand_key
, and sets it to run the associated block when the menu item is chosen. - #add_separator ⇒ Object
- #check_items(*idents) ⇒ Object (also: #check_item)
- #disable_items(*idents) ⇒ Object (also: #disable_item)
-
#enable_items(*idents) ⇒ Object
(also: #enable_item)
pass a series of symbol idents eg :save_project, :close.
- #menu_ids(*args) ⇒ Object
- #next_id ⇒ Object
- #uncheck_items(*idents) ⇒ Object (also: #uncheck_item)
Methods included from EasyMenuName
Instance Attribute Details
#source ⇒ Object
The target window for menu events from this source
77 78 79 |
# File 'lib/wx_sugar/menu.rb', line 77 def source @source end |
#target ⇒ Object
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_id ⇒ Object
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 = [internal_name(ident)] or raise RuntimeError, "Not found #{ident} #{.inspect}" when Fixnum = 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:
-
Passing a block, which can optionally receive an event parameter
-
Passing a :handler option, which can be a method name in the target, or a Proc or BoundMethod object.
-
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, = {}, &block) ident = internal_name(command_str) const = [:sys_id] || self.next_id() [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 = [: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 [:checked] itemtype = Wx::ITEM_CHECK elsif [:radio] itemtype = Wx::ITEM_RADIO else itemtype = Wx::ITEM_NORMAL end command_key = [:shortcut] || '' append(const, "#{command_str}\t#{command_key}", "", itemtype) # define the WxWidgets handler, passing a event object if required if handler.arity == 1 source.(const) { | e | handler.call(e) } else source.(const) { handler.call() } end return ident end |
#add_separator ⇒ Object
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 |
#menu_ids(*args) ⇒ Object
78 79 80 |
# File 'lib/wx_sugar/menu.rb', line 78 def (*args) @menu_ids ||= {} end |
#next_id ⇒ Object
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 |