Add a custom Window Menu to your Real-time Application in TurtleBrains.
The following guide is a continuation of HowTo: Start a Real-time Application which provides a great way to start an application if you haven't already.
The following steps are the same basic steps taken to add a Status Bar, Menu, or Dialog Prompt to your application.
- Create the necessary identifiers for the menu and menu items. (ex. MenuIdentifier / MenuItemIdentifier)
- Create and customized the menu as desired, setting up everything as needed. (ex. ApplicationMenu / AddMenuItem)
- Handle events in the ApplicationHandler when the user interacts with the menu. (ex. OnMenuAction).
This is fairly straight forward, so the following code snippets will speak for themselves. Adding a window menu is a reasonably high priority for most applications so we start there.
MyApplicationValues.h
enum MyMenus { kWindowMenu, kContextMenu, }
enum MyMenuItems { kMenuItemFile, kMenuItemFileCreate, kMenuItemFileExit, kMenuItemAbout, }
MyApplicationHandler.cpp (modify to add window menu)
void MyApplicationHandler::OnWindowOpen(void)
{
windowMenu.AddMenuItem(kMenuItemFile,
tb_string(
"File"));
windowMenu.AddMenuItem(kMenuItemFileCreate,
tb_string(
"Create"), kMenuItemFile);
windowMenu.AddMenuItem(kMenuItemFileExit,
tb_string(
"Exit"), kMenuItemFile);
windowMenu.AddMenuItem(kMenuItemAbout,
tb_string(
"About"));
theApplication.SetWindowMenu(windowMenu);
}
MyApplicationHandler.cpp (modify to add window menu)
void MyApplicationHandler::OnMenuAction(const MenuIdentifier& menu, const MenuItemIdentifier& menuItem)
{
if (kWindowMenu == menu)
{
if (kMenuItemFileCreate == menuItem)
{
}
else if (kMenuItemFileExit == menuItem)
{
theApplication.Close();
}
else if (kMenuItemAbout == menuItem)
{
}
}
}