1 /*! 2 @file 3 @author Albert Semenov 4 @date 08/2010 5 */ 6 7 #include "Precompiled.h" 8 #include "MainMenuControl.h" 9 #include "CommandManager.h" 10 #include "RecentFilesManager.h" 11 #include "Localise.h" 12 13 namespace tools 14 { 15 MainMenuControl()16 MainMenuControl::MainMenuControl() : 17 mMainMenu(nullptr), 18 mScaleMenu(nullptr) 19 { 20 } 21 ~MainMenuControl()22 MainMenuControl::~MainMenuControl() 23 { 24 mMainMenu->eventMenuCtrlAccept -= MyGUI::newDelegate(this, &MainMenuControl::notifyMenuCtrlAccept); 25 } 26 OnInitialise(Control * _parent,MyGUI::Widget * _place,const std::string & _layoutName)27 void MainMenuControl::OnInitialise(Control* _parent, MyGUI::Widget* _place, const std::string& _layoutName) 28 { 29 Control::OnInitialise(_parent, _place, _layoutName); 30 31 assignWidget(mMainMenu, "MainMenu"); 32 33 mScaleMenu = mMainMenu->findItemById("Scale"); 34 35 CommandManager::getInstance().getEvent("Command_UpdateAppCaption")->connect(this, &MainMenuControl::command_UpdateAppCaption); 36 37 mMainMenu->eventMenuCtrlAccept += MyGUI::newDelegate(this, &MainMenuControl::notifyMenuCtrlAccept); 38 39 updateRecentFilesMenu(); 40 } 41 notifyMenuCtrlAccept(MyGUI::MenuControl * _sender,MyGUI::MenuItem * _item)42 void MainMenuControl::notifyMenuCtrlAccept(MyGUI::MenuControl* _sender, MyGUI::MenuItem* _item) 43 { 44 MyGUI::UString* data = _item->getItemData<MyGUI::UString>(false); 45 if (data != nullptr) 46 CommandManager::getInstance().setCommandData(*data); 47 48 const std::string& command = _item->getItemId(); 49 if (MyGUI::utility::startWith(command, "Command_")) 50 CommandManager::getInstance().executeCommand(command); 51 } 52 updateRecentFilesMenu()53 void MainMenuControl::updateRecentFilesMenu() 54 { 55 MyGUI::MenuItem* recentFilesMenu = mMainMenu->findItemById("RecentFiles", true); 56 if (recentFilesMenu != nullptr) 57 { 58 recentFilesMenu->getItemChild()->removeAllItems(); 59 // список последних открытых файлов 60 const RecentFilesManager::VectorUString& recentFiles = RecentFilesManager::getInstance().getRecentFiles(); 61 if (!recentFiles.empty()) 62 { 63 size_t index = 1; 64 for (RecentFilesManager::VectorUString::const_iterator iter = recentFiles.begin(); iter != recentFiles.end(); ++iter, ++index) 65 { 66 addUserTag("IndexRecentFile", MyGUI::utility::toString(index)); 67 addUserTag("RecentFile", *iter); 68 recentFilesMenu->getItemChild()->addItem(replaceTags("FormatRecentFile"), MyGUI::MenuItemType::Normal, "Command_RecentFiles", *iter); 69 } 70 } 71 } 72 } 73 command_UpdateAppCaption(const MyGUI::UString & _commandName,bool & _result)74 void MainMenuControl::command_UpdateAppCaption(const MyGUI::UString& _commandName, bool& _result) 75 { 76 if (mScaleMenu != nullptr) 77 { 78 MyGUI::MenuControl* menu = mScaleMenu->getItemChild(); 79 if (menu != nullptr) 80 { 81 int value = MyGUI::utility::parseValue<int>(replaceTags("CurrentScale")); 82 std::string id = MyGUI::utility::toString("Command_ChangeScale", ".", value); 83 for (size_t index = 0; index < menu->getItemCount(); index ++) 84 { 85 MyGUI::MenuItem* item = menu->getItemAt(index); 86 item->setItemChecked(item->getItemId() == id); 87 } 88 } 89 } 90 } 91 92 } 93