diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 2243f86..8b6e0fe 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -191,9 +191,9 @@ void MainWindow::NewGame(bool useFen) { } void MainWindow::OnNewGame(wxCommandEvent &event) { - std::shared_ptr *g = (std::shared_ptr*)event.GetClientData(); - NewGame(*g); - delete g; + TabInfos *tab = (TabInfos*)event.GetClientData(); + TabInfos *i=NewGame(tab->GetGame()); + i->Link(tab); } void MainWindow::OnPageChange(wxAuiNotebookEvent &event) { @@ -215,8 +215,9 @@ void MainWindow::OnRefreshTabTitle(wxCommandEvent &event) { } } -void MainWindow::NewGame(std::shared_ptr game) { +TabInfos* MainWindow::NewGame(std::shared_ptr game) { GameTab *gt = new GameTab((wxFrame *)notebook, game); notebook->AddPage(gt, gt->GetLabel()); notebook->SetSelection(notebook->GetPageIndex(gt)); + return(gt); } diff --git a/src/MainWindow.hpp b/src/MainWindow.hpp index bd33ad7..d809736 100644 --- a/src/MainWindow.hpp +++ b/src/MainWindow.hpp @@ -23,7 +23,7 @@ class MainWindow : public MainFrame { void OpenFile(); void OnPageChange(wxAuiNotebookEvent &event); void OnRefreshTabTitle(wxCommandEvent &event); - void NewGame(std::shared_ptr game); + TabInfos* NewGame(std::shared_ptr game); void OpenSettings(); void NewEngine(); void OnCloseTabEvent(wxCommandEvent &event); diff --git a/src/base_tab/BaseGameTab.cpp b/src/base_tab/BaseGameTab.cpp index 79f28c1..dafa71a 100644 --- a/src/base_tab/BaseGameTab.cpp +++ b/src/base_tab/BaseGameTab.cpp @@ -2,6 +2,9 @@ #include "AppendGameDialog.hpp" #include +wxDEFINE_EVENT(OPEN_GAME_EVENT, wxCommandEvent); + + BaseGameTab::BaseGameTab(wxFrame *parent, std::string base_file) : TabBase_TabGames(parent), base_file(base_file), base(NULL) { @@ -72,10 +75,10 @@ void BaseGameTab::OnOpenGame(wxListEvent &event) { edited.push_back(*g); deleted.push_back(id); game_list->SetItemBackgroundColour(event.GetIndex(), *wxGREEN); - wxCommandEvent newGameEvent(NEW_GAME_EVENT, GetId()); - newGameEvent.SetEventObject(this); - newGameEvent.SetClientData(g); - ProcessEvent(newGameEvent); + wxCommandEvent openGameEvent(OPEN_GAME_EVENT, GetId()); + openGameEvent.SetEventObject(this); + openGameEvent.SetClientData(g); + ProcessEvent(openGameEvent); } } diff --git a/src/base_tab/BaseGameTab.hpp b/src/base_tab/BaseGameTab.hpp index d90163f..f93903c 100644 --- a/src/base_tab/BaseGameTab.hpp +++ b/src/base_tab/BaseGameTab.hpp @@ -4,7 +4,7 @@ #include "ochess.hpp" // Foreign events -wxDECLARE_EVENT(NEW_GAME_EVENT, wxCommandEvent); +wxDECLARE_EVENT(OPEN_GAME_EVENT, wxCommandEvent); wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent); class BaseGameTab : public TabBase_TabGames { diff --git a/src/base_tab/BaseManageTab.cpp b/src/base_tab/BaseManageTab.cpp index 46306df..db42b8b 100644 --- a/src/base_tab/BaseManageTab.cpp +++ b/src/base_tab/BaseManageTab.cpp @@ -6,3 +6,4 @@ TabBase_TabManage(parent) { } + diff --git a/src/base_tab/BaseTab.cpp b/src/base_tab/BaseTab.cpp index a76ba38..a2b7207 100644 --- a/src/base_tab/BaseTab.cpp +++ b/src/base_tab/BaseTab.cpp @@ -16,6 +16,17 @@ BaseTab::BaseTab(wxFrame *parent, std::string base_file) notebook->AddPage(manage_tab, "Manage database"); RefreshLabel(); + this->Bind(OPEN_GAME_EVENT, &BaseTab::OnNewGame, this, wxID_ANY); +} + + +void BaseTab::OnNewGame(wxCommandEvent &event){ + std::shared_ptr *g = (std::shared_ptr*)event.GetClientData(); + this->game=*g; + wxCommandEvent newGameEvent(NEW_GAME_EVENT, GetId()); + newGameEvent.SetEventObject(this); + newGameEvent.SetClientData((TabInfos*)this); + ProcessEvent(newGameEvent); } void BaseTab::ApplyPreferences() {} diff --git a/src/base_tab/BaseTab.hpp b/src/base_tab/BaseTab.hpp index 6136eed..788ba45 100644 --- a/src/base_tab/BaseTab.hpp +++ b/src/base_tab/BaseTab.hpp @@ -5,16 +5,22 @@ #include "BaseImportTab.hpp" #include "BaseManageTab.hpp" +wxDECLARE_EVENT(NEW_GAME_EVENT, wxCommandEvent); + + class BaseTab : public TabBase, public TabInfos { std::shared_ptr base; + std::shared_ptr game; BaseGameTab *games_tab; BaseImportTab *import_tab; - BaseManageTab * manage_tab; + BaseManageTab *manage_tab; + + void OnNewGame(wxCommandEvent &event); public: BaseTab(wxFrame *parent, std::string base_file); void ApplyPreferences(); void RefreshLabel(); - std::shared_ptr GetGame() { return (std::shared_ptr(NULL)); } + std::shared_ptr GetGame() { return (std::shared_ptr(game)); } std::shared_ptr GetBase() { return (std::shared_ptr(base)); }; }; \ No newline at end of file diff --git a/src/ochess.hpp b/src/ochess.hpp index 805de31..47e08e4 100644 --- a/src/ochess.hpp +++ b/src/ochess.hpp @@ -63,7 +63,7 @@ public: long linked_id; /// @brief Set to true if this tab is attach to another one (c.f linked_id) bool is_linked; - TabInfos(Type type_) : type(type_), id(tab_count), is_linked(false) { tab_count++; } + TabInfos(Type type_) : type(type_), id(tab_count), is_linked(false) { tab_count++; wxLogDebug("Tabid=%d",(int)id); } void Link(TabInfos *tab); virtual void ApplyPreferences() = 0; virtual std::shared_ptr GetGame() = 0;