Update tab management system

This commit is contained in:
Loic Guegan 2022-12-24 12:46:59 +01:00
parent 273610cb0f
commit 2d9730e216
8 changed files with 35 additions and 13 deletions

View file

@ -191,9 +191,9 @@ void MainWindow::NewGame(bool useFen) {
} }
void MainWindow::OnNewGame(wxCommandEvent &event) { void MainWindow::OnNewGame(wxCommandEvent &event) {
std::shared_ptr<Game> *g = (std::shared_ptr<Game>*)event.GetClientData(); TabInfos *tab = (TabInfos*)event.GetClientData();
NewGame(*g); TabInfos *i=NewGame(tab->GetGame());
delete g; i->Link(tab);
} }
void MainWindow::OnPageChange(wxAuiNotebookEvent &event) { void MainWindow::OnPageChange(wxAuiNotebookEvent &event) {
@ -215,8 +215,9 @@ void MainWindow::OnRefreshTabTitle(wxCommandEvent &event) {
} }
} }
void MainWindow::NewGame(std::shared_ptr<Game> game) { TabInfos* MainWindow::NewGame(std::shared_ptr<Game> game) {
GameTab *gt = new GameTab((wxFrame *)notebook, game); GameTab *gt = new GameTab((wxFrame *)notebook, game);
notebook->AddPage(gt, gt->GetLabel()); notebook->AddPage(gt, gt->GetLabel());
notebook->SetSelection(notebook->GetPageIndex(gt)); notebook->SetSelection(notebook->GetPageIndex(gt));
return(gt);
} }

View file

@ -23,7 +23,7 @@ class MainWindow : public MainFrame {
void OpenFile(); void OpenFile();
void OnPageChange(wxAuiNotebookEvent &event); void OnPageChange(wxAuiNotebookEvent &event);
void OnRefreshTabTitle(wxCommandEvent &event); void OnRefreshTabTitle(wxCommandEvent &event);
void NewGame(std::shared_ptr<Game> game); TabInfos* NewGame(std::shared_ptr<Game> game);
void OpenSettings(); void OpenSettings();
void NewEngine(); void NewEngine();
void OnCloseTabEvent(wxCommandEvent &event); void OnCloseTabEvent(wxCommandEvent &event);

View file

@ -2,6 +2,9 @@
#include "AppendGameDialog.hpp" #include "AppendGameDialog.hpp"
#include <wx/filename.h> #include <wx/filename.h>
wxDEFINE_EVENT(OPEN_GAME_EVENT, wxCommandEvent);
BaseGameTab::BaseGameTab(wxFrame *parent, std::string base_file) BaseGameTab::BaseGameTab(wxFrame *parent, std::string base_file)
: TabBase_TabGames(parent), base_file(base_file), : TabBase_TabGames(parent), base_file(base_file),
base(NULL) { base(NULL) {
@ -72,10 +75,10 @@ void BaseGameTab::OnOpenGame(wxListEvent &event) {
edited.push_back(*g); edited.push_back(*g);
deleted.push_back(id); deleted.push_back(id);
game_list->SetItemBackgroundColour(event.GetIndex(), *wxGREEN); game_list->SetItemBackgroundColour(event.GetIndex(), *wxGREEN);
wxCommandEvent newGameEvent(NEW_GAME_EVENT, GetId()); wxCommandEvent openGameEvent(OPEN_GAME_EVENT, GetId());
newGameEvent.SetEventObject(this); openGameEvent.SetEventObject(this);
newGameEvent.SetClientData(g); openGameEvent.SetClientData(g);
ProcessEvent(newGameEvent); ProcessEvent(openGameEvent);
} }
} }

View file

@ -4,7 +4,7 @@
#include "ochess.hpp" #include "ochess.hpp"
// Foreign events // Foreign events
wxDECLARE_EVENT(NEW_GAME_EVENT, wxCommandEvent); wxDECLARE_EVENT(OPEN_GAME_EVENT, wxCommandEvent);
wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent); wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent);
class BaseGameTab : public TabBase_TabGames { class BaseGameTab : public TabBase_TabGames {

View file

@ -6,3 +6,4 @@ TabBase_TabManage(parent)
{ {
} }

View file

@ -16,6 +16,17 @@ BaseTab::BaseTab(wxFrame *parent, std::string base_file)
notebook->AddPage(manage_tab, "Manage database"); notebook->AddPage(manage_tab, "Manage database");
RefreshLabel(); RefreshLabel();
this->Bind(OPEN_GAME_EVENT, &BaseTab::OnNewGame, this, wxID_ANY);
}
void BaseTab::OnNewGame(wxCommandEvent &event){
std::shared_ptr<Game> *g = (std::shared_ptr<Game>*)event.GetClientData();
this->game=*g;
wxCommandEvent newGameEvent(NEW_GAME_EVENT, GetId());
newGameEvent.SetEventObject(this);
newGameEvent.SetClientData((TabInfos*)this);
ProcessEvent(newGameEvent);
} }
void BaseTab::ApplyPreferences() {} void BaseTab::ApplyPreferences() {}

View file

@ -5,16 +5,22 @@
#include "BaseImportTab.hpp" #include "BaseImportTab.hpp"
#include "BaseManageTab.hpp" #include "BaseManageTab.hpp"
wxDECLARE_EVENT(NEW_GAME_EVENT, wxCommandEvent);
class BaseTab : public TabBase, public TabInfos { class BaseTab : public TabBase, public TabInfos {
std::shared_ptr<GameBase> base; std::shared_ptr<GameBase> base;
std::shared_ptr<Game> game;
BaseGameTab *games_tab; BaseGameTab *games_tab;
BaseImportTab *import_tab; BaseImportTab *import_tab;
BaseManageTab * manage_tab; BaseManageTab *manage_tab;
void OnNewGame(wxCommandEvent &event);
public: public:
BaseTab(wxFrame *parent, std::string base_file); BaseTab(wxFrame *parent, std::string base_file);
void ApplyPreferences(); void ApplyPreferences();
void RefreshLabel(); void RefreshLabel();
std::shared_ptr<Game> GetGame() { return (std::shared_ptr<Game>(NULL)); } std::shared_ptr<Game> GetGame() { return (std::shared_ptr<Game>(game)); }
std::shared_ptr<GameBase> GetBase() { return (std::shared_ptr<GameBase>(base)); }; std::shared_ptr<GameBase> GetBase() { return (std::shared_ptr<GameBase>(base)); };
}; };

View file

@ -63,7 +63,7 @@ public:
long linked_id; long linked_id;
/// @brief Set to true if this tab is attach to another one (c.f linked_id) /// @brief Set to true if this tab is attach to another one (c.f linked_id)
bool is_linked; 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); void Link(TabInfos *tab);
virtual void ApplyPreferences() = 0; virtual void ApplyPreferences() = 0;
virtual std::shared_ptr<Game> GetGame() = 0; virtual std::shared_ptr<Game> GetGame() = 0;