Making Menus with wxPerl
by Roberto Alamos
|
Pages: 1, 2, 3, 4, 5, 6
Using Wx::MenuBar
You have created your menus and obviously want to use them. The last step to get the job done is to create the menu bar that will handle your menus. When you want to create a menu bar, the first step is to enable your code to handle menu events. This is the job of the Wx::Event module:
use Wx::Event qw(EVT_MENU)
Now create a Wx::MenuBar object:
my $menubar = Wx::MenuBar->new();
This object will contain all of the menus that you want to show on your window. To associate a menu bar with a frame, call the SetMenuBar method from Wx::Frame:
$self->SetMenuBar($menubar);
where $self is the Wx::Frame object inherited in WxPerlComExampleFrame's constructor. Note that if your application has MDI characteristics, or has many windows, then you have to take in account that Wx first sends menu events to the focused window. (I won't cover this issue in this article, so for more information, review the WxWidgets documentation.) Finally, be sure to call the EVT_MENU subroutine as many times as you have menu items that execute some action when clicked:
EVT_MENU($self, $menu_item_id, \&subroutine);
where $self is the object of your package's new method, $menu_item_id is the unique identifier of the menu item involved, and subroutine is the name of the subroutine that will handle the click event you want to catch.
Setting Up Menus
The first thing to do once you have created your menu bar is to attach your menus to the menu bar. There are two methods for this: Append and Insert. Append, as you might expect, attaches a menu in the last position:
$menubar->Append($menu, $label);
where $menu is the menu created in the previous section and $label is the name to display for this menu in the menu bar. To insert a menu in an arbitrary position, use the Insert method:
$menubar->Insert($pos, $menu, $label);
where $pos is the position of your menu, starting at 0.
Menu Methods
Wx::MenuBar provides some methods that are also present in Wx::Menu and work in the same way. This methods are Check, Enable, FindItem, GetLabel, GetHelpString, SetLabel, SetHelpString, IsChecked, and IsEnabled. Besides these methods, Wx::MenuBar has its own set of methods to manage the properties of the menu bar. For example, as a menu item, a menu has its own enabled property, which you toggle with the EnableTop method:
$menubar->EnableTop($pos, $boolean);
where $pos is the position of your menu (starting at 0) and $boolean is TRUE or FALSE, depending on whether you want that menu enabled. Note that you can use this method only after you attach your menu bar to the window through the SetMenuBar method.
Wx::MenuBar has methods to retrieve an entire menu or menu item given its title or (menu title, menu item label) pair, respectively. In the first case, use the code:
$menu_with_the_given_title = $menubar->FindMenu($title);
In the second case:
$menu_item = $menubar->FindMenuItem($menu_title, $menu_item_label);
In both cases, the returned variables are Wx::Menu objects. You can also retrieve a menu if you provide its position (starting at 0):
$menu_with_the_given_pos = $menubar->GetMenu($pos);
As in the Wx::Menu case, Wx::MenuBar provides methods to set or get the label of a specific menu and to retrieve the number of menus in a menu bar. Those methods are SetLabelTop, GetLabelTop, and GetMenuCount respectively. Use them like this:
$menu->SetLabelTop($pos, $label);
my $menu_label = $menu->GetLabelTop($pos);
my $num_menu = $menu->GetMenuCount();
where $pos is the position of the menu and $label is the new label that you want to put on your menu. Note that GetLabelTop's result doesn't include accelerator characters inside the returned string.
Finally, Wx::MenuBar gives two more choices to remove a menu. The first method is Replace, which replaces it with another menu:
$menubar->Replace($pos, $new_menu, $label);
where $pos is the position of the menu to remove, $new_menu is the new menu that will be in the $pos position, and $label is the label to display on the menu bar for $new_menu. The second choice is to remove a menu, just by removing it with the Remove method:
my $removed_menu = $menubar->Remove($pos);
Remove returns the $removed_menu object, so if you need it in the future, it'll be still there waiting for you.

