Making Menus with wxPerl
by Roberto Alamos
|
Pages: 1, 2, 3, 4, 5, 6
To retrieve the label or help string of a particular item, use the GetLabel and GetHelpString methods. Both methods expect the menu item identifier number as the sole argument.
Every menu item has an enabled property that makes an item available or unavailable. By default, all items are enabled. To enable or disable a particular menu item, use the Enable method:
$menu->Enable($id, $boolean);
where $boolean is 0 or 1, depending if you want to disable or enable it, respectively. Maybe your next question is how to check if a menu item is enabled; use the IsEnabled method:
$menu->IsEnabled($id);
This returns TRUE or FALSE, depending on the status of the menu item.
Radio items and check items have the checked property that indicates the selection status of the item. By default, no check item is checked at the start of the execution of your program. For radio items, the first one created is checked at the start of execution. Use the Check method to check or uncheck a radio or check item:
$menu->Check($id, $boolean);
To determine if a menu item is checked, use IsChecked:
$menu->IsChecked($id);
This method, as does IsEnabled, returns TRUE or FALSE.
It's also possible to get the number of menu items your menu has. For this, use the GetMenuItemCount method:
$menu->GetMenuItemCount();
note that if @args is the argument's array, then $menu->Append(@args) and $menu->Insert($menu->GetMenuItemCount(), @args) are the same.
Finally, it's important to know that there are three ways to remove an item from a menu (honoring Larry Wall's phrase: "There's more than one way to do it"). The first is the Delete method, which just kills the menu item without compassion:
$menu->Delete($id);
This method returns nothing. Be careful--WxWidgets documentation says that the Delete method doesn't delete a menu item that's a submenu. Instead, the documentation recommends that you use the Destroy method to delete a submenu. In wxPerl, this isn't true. Delete is certainly capable of deleting a submenu, and is here equivalent to the Destroy method. I don't know the reason for this strange behavior.
The Destroy method looks like this:
$menu->Destroy($id);
If you want to remove an item but not destroy it, then the Remove method is for you. It allows you to store the menu item that you want to delete in a variable for later use, and at the same time delete it from its original menu. Use it like so:
my $removed_item = $menu->Remove($id);
Now you have your menu item with the identifier $id in the $removed_item variable (it now contains a Wx::MenuItem object). You can now use this variable to relocate the removed item into another menu with the append methods. For example:
$other_menu->Append($removed_item);
does the same thing as:
$other_menu->Append($id_removed_item, $title_removed_item,
$helpstr_removed_item);
but in a shorter way.
Finally, it's useful to be able to remove a submenu's menu item. You can't use the Destroy, Delete, or Remove methods, because they don't work. Instead, you need to do something like this:
my ($mitem, $submenu) = $menu->FindItem($mitem_id);
where $mitem_id is the identifier number of the submenu's menu item you're looking for. $submenu is a Wx::Menu object, just as $menu is, and hence you can use all the methods mentioned here, so the only thing you have to do to remove $mitem from $submenu is:
$submenu->Delete($mitem_id);
As the good reader that I am sure you are, you already have realized that this isn't the only thing you can do with the $submenu object. In fact, you can now add new menu items to your submenu, delete another menu item, and in general do everything mentioned already.

