diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 348e772..b048c62 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -8,6 +8,7 @@ wxDEFINE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent); wxDEFINE_EVENT(NEW_GAME_EVENT, wxCommandEvent); wxDEFINE_EVENT(CLOSE_TAB_EVENT, wxCommandEvent); +wxDEFINE_EVENT(REFRESH_ENGINE_LIST, wxCommandEvent); /// ---------- MainWindow ---------- @@ -21,23 +22,18 @@ MainWindow::MainWindow() /// File menu wxMenu *menuFile = new wxMenu; menuFile->Append(1, "Open", "Open file"); - Bind(wxEVT_MENU, &MainWindow::OnOpen, this, 1); menuFile->AppendSeparator(); menuFile->Append(10, "Save", "Save current game"); menuFile->Append(11, "Save As", "Save current game as"); menuFile->AppendSeparator(); menuFile->Append(4, "Settings", "Configure OChess"); - Bind(wxEVT_MENU, &MainWindow::OnSettings, this, 4); menuFile->AppendSeparator(); menuFile->Append(wxID_EXIT); - Bind(wxEVT_MENU, &MainWindow::OnExit, this, wxID_EXIT); // Game menu menuGame = new wxMenu; menuGame->Append(2, "New", "Create new game"); - Bind(wxEVT_MENU, &MainWindow::OnMenuNewGame, this, 2); menuGame->Append(3, "New from FEN", "Create new game using FEN"); - Bind(wxEVT_MENU, &MainWindow::OnMenuNewGame, this, 3); // Game base menu wxMenu *menuBase = new wxMenu; @@ -46,7 +42,10 @@ MainWindow::MainWindow() // Engine menu wxMenu *engineMenu = new wxMenu; engineMenu->Append(6, "New", "Create a new engine configuration"); - Bind(wxEVT_MENU, &MainWindow::OnNewEngine, this, 6); + manageMenu = new wxMenu; + engineMenu->AppendSubMenu(manageMenu, "Manage"); + wxCommandEvent dummy(REFRESH_ENGINE_LIST, GetId()); + OnRefreshEngineList(dummy); /// Menu bar menuBar = new wxMenuBar; @@ -66,24 +65,77 @@ MainWindow::MainWindow() Bind(NEW_GAME_EVENT, &MainWindow::OnNewGame, this, wxID_ANY); Bind(wxEVT_CLOSE_WINDOW, &MainWindow::OnClose, this); Bind(CLOSE_TAB_EVENT, &MainWindow::OnCloseTabEvent, this, wxID_ANY); + Bind(wxEVT_MENU, &MainWindow::OnMenuItemClick, this, wxID_ANY); + Bind(REFRESH_ENGINE_LIST, &MainWindow::OnRefreshEngineList, this, wxID_ANY); /*BaseTab *bt = new BaseTab((wxFrame *)notebook, "/home/loic/hartwig_tests.pgn"); notebook->AddPage(bt, bt->GetLabel()); notebook->SetSelection(notebook->GetPageIndex(bt));*/ -/* - EngineTab *bt = - new EngineTab((wxWindow *)notebook, - new uciadapter::UCI("/home/loic/.local/bin/stockfish"), - "/home/loic/.local/bin/stockfish"); - notebook->AddPage(bt, bt->GetLabel()); - notebook->SetSelection(notebook->GetPageIndex(bt));*/ + /* + EngineTab *bt = + new EngineTab((wxWindow *)notebook, + new uciadapter::UCI("/home/loic/.local/bin/stockfish"), + "/home/loic/.local/bin/stockfish"); + notebook->AddPage(bt, bt->GetLabel()); + notebook->SetSelection(notebook->GetPageIndex(bt));*/ } void MainWindow::OnCloseTabEvent(wxCommandEvent &event) { notebook->DeletePage(notebook->GetSelection()); } -void MainWindow::OnNewEngine(wxCommandEvent &event) { +void MainWindow::OnMenuItemClick(wxCommandEvent &event) { + std::uint32_t id = event.GetId(); + if (id == wxID_EXIT) { + Close(true); + } else if (id >= 100) { + wxLogDebug("Engine selected!"); + wxMenuItemList items = manageMenu->GetMenuItems(); + for (wxMenuItem *item : items) { + if (item->GetId() == id) { + wxLogDebug("Selected %s", item->GetItemLabel()); + EngineTab *bt = new EngineTab((wxWindow *)notebook, + item->GetItemLabel().ToStdString()); + notebook->AddPage(bt, bt->GetLabel()); + notebook->SetSelection(notebook->GetPageIndex(bt)); + } + } + } else if (id == 1) { + OpenFile(); + } else if (id == 2) { + NewGame(false); + } else if (id == 3) { + NewGame(true); + } else if (id == 4) { + OpenSettings(); + } else if (id == 6) { + NewEngine(); + } +} + +void MainWindow::OnRefreshEngineList(wxCommandEvent &event) { + // Delete all items + wxMenuItemList items = manageMenu->GetMenuItems(); + for (wxMenuItem *item : items) { + manageMenu->Delete(item->GetId()); + } + // Refresh items + CONFIG_OPEN(conf); + conf->SetPath("engines/"); + wxString engine_name; + long index; + if (conf->GetFirstGroup(engine_name, index)) { + std::uint32_t id = 0; + do { + manageMenu->Append(100 + id, engine_name, "Configure " + engine_name); + id++; + } while (conf->GetNextGroup(engine_name, index)); + } + + CONFIG_CLOSE(conf); +} + +void MainWindow::NewEngine() { wxFileDialog openFileDialog(this, _("Use engine"), "", "", "Executable|*", wxFD_OPEN | wxFD_FILE_MUST_EXIST); if (openFileDialog.ShowModal() != wxID_CANCEL) { @@ -100,7 +152,7 @@ void MainWindow::OnNewEngine(wxCommandEvent &event) { } } -void MainWindow::OnSettings(wxCommandEvent &event) { +void MainWindow::OpenSettings() { if (prefsEditor != NULL) { delete prefsEditor; } @@ -117,8 +169,6 @@ void MainWindow::ApplyPreferences() { } } -void MainWindow::OnExit(wxCommandEvent &event) { Close(true); } - std::vector MainWindow::ListTabInfos() { std::vector tinfos; for (int i = 0; i < notebook->GetPageCount(); i++) { @@ -134,7 +184,7 @@ void MainWindow::OnClose(wxCloseEvent &e) { e.Skip(); } -void MainWindow::OnOpen(wxCommandEvent &event) { +void MainWindow::OpenFile() { wxFileDialog openFileDialog(this, _("Open file"), "", "", "PGN files (*.pgn)|*.pgn", wxFD_OPEN | wxFD_FILE_MUST_EXIST); @@ -147,8 +197,8 @@ void MainWindow::OnOpen(wxCommandEvent &event) { } } -void MainWindow::OnMenuNewGame(wxCommandEvent &event) { - if (event.GetId() == 3) { +void MainWindow::NewGame(bool useFen) { + if (useFen) { wxTextEntryDialog *dial = new wxTextEntryDialog(NULL, wxT("Enter FEN:"), wxT("Error")); if (dial->ShowModal() == wxID_OK) { diff --git a/src/MainWindow.hpp b/src/MainWindow.hpp index 4111612..4fa3318 100644 --- a/src/MainWindow.hpp +++ b/src/MainWindow.hpp @@ -11,27 +11,30 @@ wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent); wxDECLARE_EVENT(NEW_GAME_EVENT, wxCommandEvent); wxDECLARE_EVENT(CLOSE_TAB_EVENT, wxCommandEvent); +wxDECLARE_EVENT(REFRESH_ENGINE_LIST, wxCommandEvent); class MainWindow : public wxFrame { wxAuiNotebook *notebook; wxMenu *menuGame; wxMenuBar *menuBar; wxPreferencesEditor *prefsEditor; + wxMenu *manageMenu; - void OnExit(wxCommandEvent &event); void OnClose(wxCloseEvent &e); - void OnMenuNewGame(wxCommandEvent &event); + void NewGame(bool useFen); void OnNewGame(wxCommandEvent &event); - void OnOpen(wxCommandEvent &event); + void OpenFile(); void OnPageChange(wxAuiNotebookEvent &event); void OnRefreshTabTitle(wxCommandEvent &event); void NewGame(Game *game); - void OnSettings(wxCommandEvent &event); - void OnNewEngine(wxCommandEvent &event); + void OpenSettings(); + void NewEngine(); void OnCloseTabEvent(wxCommandEvent &event); + void OnRefreshEngineList(wxCommandEvent &event); + void OnMenuItemClick(wxCommandEvent &event); public: MainWindow(); void ApplyPreferences(); - std::vector ListTabInfos(); + std::vector ListTabInfos(); }; \ No newline at end of file diff --git a/src/engine_tab/EngineTab.cpp b/src/engine_tab/EngineTab.cpp index ca2880f..ca5f2e9 100644 --- a/src/engine_tab/EngineTab.cpp +++ b/src/engine_tab/EngineTab.cpp @@ -22,7 +22,46 @@ EngineTab::EngineTab(wxWindow *parent, uciadapter::UCI *engine, // conf->Write(confGroup + "/path", wxString(engine_path_or_name)); CONFIG_CLOSE(conf); InitConfiguration(); + LoadConfiguration(); + RefreshItemList(); + Bind(wxEVT_BUTTON, &EngineTab::OnSave, this, ENGINE_SAVE_CONF_BUTTON); + Bind(wxEVT_BUTTON, &EngineTab::OnDelete, this, ENGINE_DELETE_CONF_BUTTON); +} + +EngineTab::EngineTab(wxWindow *parent, std::string name) + : EngineTabBF(parent), TabInfos(TabInfos::ENGINE) { + SetLabel(name); + engineName = name; + confGroup = "engines/" + engineName; + engine_name->SetValue(engineName); + CONFIG_OPEN(conf); + engine_location->SetValue(conf->Read(confGroup + "/path")); + CONFIG_CLOSE(conf); + LoadConfiguration(); + + Bind(wxEVT_BUTTON, &EngineTab::OnSave, this, ENGINE_SAVE_CONF_BUTTON); + Bind(wxEVT_BUTTON, &EngineTab::OnDelete, this, ENGINE_DELETE_CONF_BUTTON); +} + +void EngineTab::OnDelete(wxCommandEvent &event) { + CONFIG_OPEN(conf); + conf->DeleteGroup(confGroup); + CONFIG_CLOSE(conf); + RefreshItemList(); + + wxCommandEvent closeTabEvent(CLOSE_TAB_EVENT, GetId()); + closeTabEvent.SetEventObject(this); + ProcessEvent(closeTabEvent); +} + +void EngineTab::RefreshItemList() { + wxCommandEvent refreshEngineList(REFRESH_ENGINE_LIST, GetId()); + refreshEngineList.SetEventObject(this); + ProcessEvent(refreshEngineList); +} + +void EngineTab::LoadConfiguration() { // Build wxPropertyGrid according to engine configuration CONFIG_OPEN(conf2); long index; @@ -48,28 +87,17 @@ EngineTab::EngineTab(wxWindow *parent, uciadapter::UCI *engine, } while (conf2->GetNextGroup(opt_name, index)); } CONFIG_CLOSE(conf2); - - Bind(wxEVT_BUTTON, &EngineTab::OnSave, this, ENGINE_SAVE_CONF_BUTTON); - Bind(wxEVT_BUTTON, &EngineTab::OnDelete, this, ENGINE_DELETE_CONF_BUTTON); -} - -void EngineTab::OnDelete(wxCommandEvent &event) { - CONFIG_OPEN(conf); - conf->DeleteGroup(confGroup); - CONFIG_CLOSE(conf); - - wxCommandEvent closeTabEvent(CLOSE_TAB_EVENT, GetId()); - closeTabEvent.SetEventObject(this); - ProcessEvent(closeTabEvent); } void EngineTab::OnSave(wxCommandEvent &event) { CONFIG_OPEN(conf2); wxString new_engine_name = engine_name->GetValue(); if (new_engine_name != engineName) { - conf2->RenameGroup(confGroup, "engines/" + new_engine_name); + conf2->SetPath("engines/"); + conf2->RenameGroup(engineName, new_engine_name); engineName = new_engine_name; confGroup = "engines/" + engineName; + conf2->SetPath(".."); } long index; std::string optsPath = confGroup + "/options"; @@ -91,6 +119,7 @@ void EngineTab::OnSave(wxCommandEvent &event) { } while (conf2->GetNextGroup(opt_name, index)); } CONFIG_CLOSE(conf2); + RefreshItemList(); } void EngineTab::InitConfiguration() { diff --git a/src/engine_tab/EngineTab.hpp b/src/engine_tab/EngineTab.hpp index 6e4913b..3410964 100644 --- a/src/engine_tab/EngineTab.hpp +++ b/src/engine_tab/EngineTab.hpp @@ -4,16 +4,19 @@ // Foreign event wxDECLARE_EVENT(CLOSE_TAB_EVENT, wxCommandEvent); +wxDECLARE_EVENT(REFRESH_ENGINE_LIST, wxCommandEvent); class EngineTab : public EngineTabBF, public TabInfos { uciadapter::UCI *engine; std::string confGroup, enginePath; std::string engineName; void InitConfiguration(); - + void LoadConfiguration(); + void RefreshItemList(); public: EngineTab(wxWindow *parent, uciadapter::UCI *engine, std::string engine_path_or_name); + EngineTab(wxWindow *parent, std::string name); void ApplyPreferences() {} void *GetGame() { return (NULL); } void *GetBase() { return (NULL); }