How do I add menu items from XRC source?

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.

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.

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”)

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,

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

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.