void QuitConfirmMenu::Init() { TEXTURES.Add(BACKGROUND); FONTS.Add(FONT); MenuItem::Init(); // Add menu items // Hack. Magic numbers at the moment to get the menu items spaced correctly. m_titleOrigin.Set(APP.GetScreenWidth() / 2.0f, TITLE_STARTY); float y = TITLE_STARTY + ITEM_SPACING + TITLE_HEIGHT; float x = 350.0f; AddMenuItem(MenuItemPtr(new MenuItem(juzutil::Vector2(x, y), "Yes", MENUITEM_YES, ITEM_HEIGHT, true))); x += 100.0f; AddMenuItem(MenuItemPtr(new MenuItem(juzutil::Vector2(x, y), "No", MENUITEM_NO, ITEM_HEIGHT, false))); }
void MainMenu::Init() { FONTS.Add(VERSION_FONT); TEXTURES.Add(BACKGROUND); MenuItem::Init(); // Add menu items float x = APP.GetScreenWidth() / 2.0f; float y = ITEM_STARTY; AddMenuItem(MenuItemPtr(new MenuItem(juzutil::Vector2(x, y), "Start Game", MENUITEM_START, ITEM_HEIGHT, true))); y += ITEM_HEIGHT + ITEM_SPACING; AddMenuItem(MenuItemPtr(new MenuItem(juzutil::Vector2(x, y), "High Scores", MENUITEM_SCORES, ITEM_HEIGHT, false))); y += ITEM_HEIGHT + ITEM_SPACING; AddMenuItem(MenuItemPtr(new MenuItem(juzutil::Vector2(x, y), "Quit", MENUITEM_QUIT, ITEM_HEIGHT, false))); }
void MenuManager::loadFromRegistry() { xml::NodeList menuNodes = GlobalRegistry().findXPath(RKEY_MENU_ROOT); if (!menuNodes.empty()) { for (std::size_t i = 0; i < menuNodes.size(); i++) { std::string name = menuNodes[i].getAttributeValue("name"); // Allocate a new MenuItem with root as parent MenuItemPtr menubar = MenuItemPtr(new MenuItem(_root)); menubar->setName(name); // Populate the root menuitem using the current node menubar->parseNode(menuNodes[i], menubar); // Add the menubar as child of the root (child is already parented to _root) _root->addChild(menubar); } } else { rError() << "MenuManager: Could not find menu root in registry.\n"; } }
void MenuManager::clear() { _root = MenuItemPtr(); }
MenuManager::MenuManager() : _root(new MenuItem(MenuItemPtr())) // Allocate the root item (type is set automatically) {}
wxObject* MenuManager::insert(const std::string& insertPath, const std::string& name, eMenuItemType type, const std::string& caption, const std::string& icon, const std::string& eventName) { // Sanity check for empty menu if (_root == NULL) return NULL; MenuItemPtr found = _root->find(insertPath); if (found != NULL) { if (found->parent() != NULL) { // Get the Menu position of the child widget int position = found->parent()->getMenuPosition(found); // Allocate a new MenuItem MenuItemPtr newItem = MenuItemPtr(new MenuItem(found->parent())); found->parent()->addChild(newItem); // Load the properties into the new child newItem->setName(name); newItem->setType(type); newItem->setCaption(caption); newItem->setEvent(eventName); newItem->setIcon(icon); wxObject* parentWidget = found->parent()->getWidget(); // Insert it at the given position if (found->parent()->getType() == menuBar) { // The parent is a menubar, require a menu in the first place if (newItem->getType() != menuFolder) { rError() << "Cannot insert non-menu into menu bar." << std::endl; return NULL; } wxMenu* newMenu = static_cast<wxMenu*>(newItem->getWidget()); static_cast<wxMenuBar*>(parentWidget)->Insert(position, newMenu, newItem->getCaption()); } else if (found->parent()->getType() == menuFolder) { // The parent is a submenu (=menuitem), try to retrieve the menushell first wxMenu* menu = dynamic_cast<wxMenu*>(parentWidget); if (newItem->getType() == menuSeparator) { newItem->setWidget(menu->InsertSeparator(position)); return newItem->getWidget(); } else if (newItem->getType() == menuFolder) { wxMenu* subMenu = dynamic_cast<wxMenu*>(newItem->getWidget()); if (subMenu == NULL) { rError() << "Cannot cast item to a wxMenu." << std::endl; return NULL; } if (menu != NULL) { menu->Insert(position, wxID_ANY, newItem->getCaption(), subMenu); } else { rError() << "Cannot cast parent item to a wxMenu*." << std::endl; } } else if (newItem->getType() == menuItem) { wxMenuItem* item = dynamic_cast<wxMenuItem*>(newItem->getWidget()); if (item == NULL) { rError() << "Cannot cast item to a wxMenuItem." << std::endl; return NULL; } if (menu != NULL) { menu->Insert(position, item); if (!newItem->getEvent().empty()) { newItem->connectEvent(); } else { // No event, disable this item menu->Enable(item->GetId(), false); } } else { rError() << "Cannot cast parent item to a wxMenu*." << std::endl; } } } return newItem->getWidget(); } else { rError() << "MenuManager: Unparented menuitem, can't determine position: "; rError() << insertPath << std::endl; return NULL; } } else { rError() << "MenuManager: Could not find insertPath: " << insertPath << std::endl; return NULL; } }
wxObject* MenuManager::add(const std::string& insertPath, const std::string& name, eMenuItemType type, const std::string& caption, const std::string& icon, const std::string& eventName) { // Sanity check for empty menu if (_root == NULL) return NULL; MenuItemPtr found = _root->find(insertPath); if (found != NULL) { // Allocate a new MenuItem MenuItemPtr newItem = MenuItemPtr(new MenuItem(found)); newItem->setName(name); newItem->setCaption(caption); newItem->setType(type); newItem->setIcon(icon); newItem->setEvent(eventName); // Get the parent widget wxObject* parentItem = found->getWidget(); if (found->getType() == menuBar) { // The parent is a menubar, require a menu in the first place if (newItem->getType() != menuFolder) { rError() << "Cannot insert non-menu into menu bar." << std::endl; return NULL; } wxMenu* newMenu = static_cast<wxMenu*>(newItem->getWidget()); static_cast<wxMenuBar*>(parentItem)->Append(newMenu, newItem->getCaption()); } else { // Retrieve the submenu widget from the item wxMenu* menu = dynamic_cast<wxMenu*>(parentItem); if (menu == NULL) { rError() << "Cannot cast parent item to a wxMenu." << std::endl; return NULL; } // Special handling for separators if (newItem->getType() == menuSeparator) { newItem->setWidget(menu->AppendSeparator()); } wxMenuItem* item = dynamic_cast<wxMenuItem*>(newItem->getWidget()); if (item != NULL && newItem->getType() != menuSeparator) { menu->Append(item); if (!newItem->getEvent().empty()) { newItem->connectEvent(); } else { // No event, disable this item menu->Enable(item->GetId(), false); } } } // Add the child to the <found> parent, AFTER its wxMenuItem* operator // was invoked, otherwise the parent tries to instantiate it before it's actually // added. found->addChild(newItem); return newItem->getWidget(); } else if (insertPath.empty()) { // We have a new top-level menu item, create it as child of root MenuItemPtr newItem = MenuItemPtr(new MenuItem(_root)); newItem->setName(name); newItem->setCaption(caption); newItem->setType(type); newItem->setIcon(icon); newItem->setEvent(eventName); // Insert into root _root->addChild(newItem); return newItem->getWidget(); } else { // not found and not a top-level item either. } return NULL; }
int InternalTrackPopupMenu(MenuPtr mainMenu) { MenuPtr trMenu = mainMenu; g_mainmenu = mainMenu; while (g_menuPop) { suic::PopupPtr menuPop = g_menuPop; suic::Size availableSize; MenuPtr menu(MenuPtr::cast(menuPop->GetPopupRoot())); g_menuPop = suic::PopupPtr(); menu->SetStyle(menu->FindResource(_T("Menu"))); menu->DoInit(); menu->Measure(availableSize); int cx = menu->GetDesiredSize().cx; int cy = menu->GetDesiredSize().cy; menuPop->SetWidth(cx); menuPop->SetHeight(cy); SetTrackMenu(menuPop); if (g_mainmenu != trMenu) { trMenu = g_mainmenu; } g_trackmenupopup = new HwndSourceHookMenuPopup(menuPop, trMenu); if (g_pretrackmenuitem && g_pretrackmenuitem != g_trackmenuitem) { g_pretrackmenuitem->SetSelected(false); g_pretrackmenuitem->InvalidateVisual(true); } // 去掉焦点 menuPop->SetFocusable(false); menuPop->WriteFlag(CoreFlags::IsLayeredWindow, true); menuPop->TrackingPopup(g_x, g_y, true, cx, cy, g_trackmenupopup.get()); suic::SystemHelper::RemoveHwndMessageSystemHook(g_trackmenupopup.get()); g_trackmenupopup = NULL; SetTrackMenu(NULL); } g_mainmenu = MenuPtr(); if (g_trackmenuitem) { g_trackmenuitem->SetSelected(false); g_trackmenuitem->InvalidateVisual(true); } g_pretrackmenuitem = MenuItemPtr(); g_trackmenuitem = MenuItemPtr(); return 0; }