# How do I add menu items from XRC source?

**URL:** <https://www.ruby-forum.com/t/how-do-i-add-menu-items-from-xrc-source/182359>\
**Category:** wxRuby\
**Created:** [January 27, 2010, 12:44am UTC](https://www.ruby-forum.com/t/how-do-i-add-menu-items-from-xrc-source/182359 "2010-01-27T00:44:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 27, 2010, 12:44am UTC](https://www.ruby-forum.com/t/how-do-i-add-menu-items-from-xrc-source/182359/1 "2010-01-27T00:44:49Z")

</div>

I understand how to add normal menus and menu items by using append.  
However, when creating menus inside a designer and it’s output to  
source, I can’t figure out how to reference the menu from xrc to append  
items to. Example:

 Favorites Add Query to Favorites 

I have a Menu called Favorites. Currently it has one item on it called  
Add Query to Favorites along with a separator (divider).

I want to read from file and add favorite items beneath this separator,  
so normally if the code was written by me, I could do something like:

favorites = Wx::Menu.new()  
favorites.append(101, “&Some Name”, “Some Statusbar description”)  
etc…

However, in the ruby file that’s pulled from XRC, the only variables  
listed are:

@mb\_main = finder.call(“mb\_main”)  
… which points to the entire menubar

and…

@mb\_fm\_addfavorite = finder.call(“mb\_fm\_addfavorite”)  
… which points to the add favorite menu item beneath the favorite menu.

Notice that there’s no favorite menu variable stored in the ruby file  
from xrc source and there’s no place in the GUI designer (using  
dialogblocks) to place a custom ID for this menu.

So, how do I reference the menu and append items to it?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 27, 2010, 11:19pm UTC](https://www.ruby-forum.com/t/how-do-i-add-menu-items-from-xrc-source/182359/2 "2010-01-27T23:19:17Z")

</div>

Anyone able to answer this? I would think this would be an easy  
question to answer but it’s looking more difficult by the minute.

Other than creating all of my menus without using the GUI designer, what  
are my options for finding a menu only by the label?

The only visible thing in the xrc source is:

Favorites

Under object class type of WxMenu. If I try to do a  
Wx::Menu.get\_label(‘Favorites’) I receive an undefined method for Menu.  
Yet in the api it says there’s a get\_label method which is a bit  
boggling.

What am I doing wrong?

I simply want to append one tiny menu item as a test underneath the menu  
label of Favorites, which is housed in XRC source, which does not  
provide a finder call variable assignment.

Can anyone provide me a snippet of code that works for this? I’m  
certain I’m not the only one who designs a complete GUI inside of a  
designer. Don’t be afraid to respond, even if you aren’t completely  
sure. I’d rather have a few opinions that help me get pointed in the  
right direction than not having any opinions at all.

Thanks.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 28, 2010, 4:23am UTC](https://www.ruby-forum.com/t/how-do-i-add-menu-items-from-xrc-source/182359/3 "2010-01-28T04:23:41Z")

</div>

Well, I don’t give up and because of that, I figured it out myself after  
a lot of trial and error.

Because the xrc source does not provide anything for the menu besides  
the menu label, in order to append a menu item to that label you have to  
use the following example:

Per above, the favorites menu was labeled as “Favorites”. So, I put in  
the following code and it successfully added a menu item to the  
Favorites menu.

favorites\_menu = menu\_bar.get\_menu(menu\_bar.find\_menu(‘Favorites’))  
favorites\_menu.append(Wx::ID\_ANY, “My Favorite”)

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 28, 2010, 5:19am UTC](https://www.ruby-forum.com/t/how-do-i-add-menu-items-from-xrc-source/182359/4 "2010-01-28T05:19:16Z")

</div>

So, just adding some extra information for the next guy that comes after  
me that has a similar issue.

Using the same format below:

favorites\_menu = menu\_bar.get\_menu(menu\_bar.find\_menu(‘Favorites’))  
favorites\_menu.append(Wx::ID\_ANY, “My Favorite”)

… because I’ve set ID\_ANY, I need to be able to catch and process that  
ID\_ANY, which will automatically have an ID assigned by the application.  
You can do that by adding the following quick method:

evt\_menu(Wx::ID\_ANY) do | event |  
puts @favorites\_menu.label(event.id)  
end

… the only catch or pit fall that you need to avoid is where you place  
the event listener in an ordering process. If for example, you did the  
following:

== WRONG WAY ==

evt\_menu( @mb\_fm\_exit, :on\_exit )  
evt\_menu(Wx::ID\_ANY) do | event |  
puts @favorites\_menu.label(event.id)  
end

… the :on\_exit listener would not work because the ID\_ANY listener  
overwrites the event based on order of operation. So, you need to place  
the ID\_ANY listener at the top of all your listeners and then process  
other events below it. This includes both menu and toolbar events.

== RIGHT WAY ==

evt\_menu(Wx::ID\_ANY) do | event |  
puts @favorites\_menu.label(event.id)  
end  
evt\_menu( @mb\_fm\_exit, :on\_exit )

I hope this helps anyone else out down the road.

Take care,

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 29, 2010, 9:37am UTC](https://www.ruby-forum.com/t/how-do-i-add-menu-items-from-xrc-source/182359/5 "2010-01-29T09:37:15Z")

</div>

Whoa, thanks  
I always read your post regarding on this matter  
since I encountered same problem back then  
and cannot find the solution

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 29, 2010, 11:20am UTC](https://www.ruby-forum.com/t/how-do-i-add-menu-items-from-xrc-source/182359/6 "2010-01-29T11:20:52Z")

</div>

Too late but this is the way i do:

You can design your menu directly into the xrc (i’m using wxformbuilder)  
and  
access the menu as follow (self is the frame):

```
  @menu_bar = self.get_menu_bar()
  @mnu_open = @menu_bar.find_menu_item('File', 'Open')
  @mnu_exit = @menu_bar.find_menu_item('File', 'Exit')

```

to access submenu…

```
  mnu_invoice = 

```

@menu\_bar.find\_item(@menu\_bar.find\_menu\_item(‘Options’,  
‘Invoice’)).get\_sub\_menu()  
@mnu\_commerce = mnu\_fattura.find\_item(‘Commerce’)  
@mnu\_service = mnu\_fattura.find\_item(‘Service’)

and to connect the event…

evt\_menu(@mnu\_exit) do | event |  
Wx::get\_app.exit\_main\_loop()  
end

evt\_menu(@mnu\_service) do | event |  
…  
end

hope this help.

bio.
