ochess/src/MainWindow.cpp

243 lines
7.4 KiB
C++
Raw Normal View History

2022-02-23 18:11:55 +01:00
#include "MainWindow.hpp"
#include "ChessArbiter.hpp"
2022-02-26 19:21:52 +01:00
#include "UCI.hpp"
2022-02-26 17:05:47 +01:00
#include "engine_tab/EngineTab.hpp"
2022-02-23 18:11:55 +01:00
#include "pgnp.hpp"
#include "preferences/preferences.hpp"
wxDEFINE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent);
wxDEFINE_EVENT(NEW_GAME_EVENT, wxCommandEvent);
2022-02-26 20:34:42 +01:00
wxDEFINE_EVENT(CLOSE_TAB_EVENT, wxCommandEvent);
2022-02-26 21:43:09 +01:00
wxDEFINE_EVENT(REFRESH_ENGINE_LIST, wxCommandEvent);
2022-12-24 22:30:20 +01:00
wxDEFINE_EVENT(CLOSE_LINKED_TAB, wxCommandEvent);
2022-02-23 18:11:55 +01:00
/// ---------- MainWindow ----------
MainWindow::MainWindow()
2022-02-28 15:27:51 +01:00
: MainFrame(NULL, wxID_ANY, "OChess: The Open Chess software",
wxDefaultPosition, wxSize(1500, 1000)),
2022-02-23 19:41:50 +01:00
prefsEditor(NULL) {
2022-02-23 18:11:55 +01:00
SetStatusText("OChess");
/// File menu
2022-02-28 15:27:51 +01:00
menu_file->Append(1, "Open", "Open file");
menu_file->AppendSeparator();
menu_file->Append(10, "Save", "Save current game");
menu_file->Append(11, "Save As", "Save current game as");
menu_file->AppendSeparator();
menu_file->Append(4, "Settings", "Configure OChess");
menu_file->AppendSeparator();
menu_file->Append(wxID_EXIT);
2022-02-23 18:11:55 +01:00
2022-02-25 08:49:57 +01:00
// Game menu
2022-02-28 15:27:51 +01:00
menu_game->Append(2, "New", "Create new game");
menu_game->Append(3, "New from FEN", "Create new game using FEN");
2022-02-25 08:49:57 +01:00
// Game base menu
2022-12-24 14:01:39 +01:00
menu_db->Append(5, "Open", "Open a database");
2022-02-23 18:11:55 +01:00
2022-02-26 17:05:47 +01:00
// Engine menu
2022-02-28 15:27:51 +01:00
menu_engine->Append(6, "New", "Create a new engine configuration");
2022-02-26 21:43:09 +01:00
manageMenu = new wxMenu;
2022-02-28 15:27:51 +01:00
menu_engine->AppendSubMenu(manageMenu, "Manage");
wxCommandEvent dummy(REFRESH_ENGINE_LIST, GetId());
OnRefreshEngineList(dummy);
2022-02-23 18:11:55 +01:00
Bind(wxEVT_AUINOTEBOOK_PAGE_CHANGED, &MainWindow::OnPageChange, this,
wxID_ANY);
Bind(REFRESH_TAB_TITLE, &MainWindow::OnRefreshTabTitle, this, wxID_ANY);
2022-02-24 21:10:29 +01:00
Bind(NEW_GAME_EVENT, &MainWindow::OnNewGame, this, wxID_ANY);
2022-02-23 19:41:50 +01:00
Bind(wxEVT_CLOSE_WINDOW, &MainWindow::OnClose, this);
2022-02-26 20:34:42 +01:00
Bind(CLOSE_TAB_EVENT, &MainWindow::OnCloseTabEvent, this, wxID_ANY);
2022-02-26 21:43:09 +01:00
Bind(wxEVT_MENU, &MainWindow::OnMenuItemClick, this, wxID_ANY);
Bind(REFRESH_ENGINE_LIST, &MainWindow::OnRefreshEngineList, this, wxID_ANY);
2022-12-24 22:30:20 +01:00
Bind(CLOSE_LINKED_TAB, &MainWindow::OnCloseTabLinkedTo, this, wxID_ANY);
2022-02-26 12:30:07 +01:00
2022-02-28 15:27:51 +01:00
// Add new game tab by default
2022-02-28 20:16:57 +01:00
NewGame(std::shared_ptr<Game>(new Game()));
2022-12-24 14:01:39 +01:00
// Temporary TO REMOVE JUST FOR TESTS:
//BaseTab *bt = new BaseTab((wxFrame *)notebook, "/home/loic/Downloads/PGN.pgn");
//this->AddPage(bt,bt);
}
void MainWindow::AddPage(wxWindow* window, TabInfos* infos){
window->SetClientData(infos);
notebook->AddPage(window, window->GetLabel());
notebook->SetSelection(notebook->GetPageIndex(window));
2022-02-23 18:11:55 +01:00
}
2022-02-26 20:34:42 +01:00
void MainWindow::OnCloseTabEvent(wxCommandEvent &event) {
notebook->DeletePage(notebook->GetSelection());
}
2022-12-24 22:30:20 +01:00
void MainWindow::OnCloseTabLinkedTo(wxCommandEvent &event){
2022-12-25 09:57:34 +01:00
TabInfos *infosEvent=(TabInfos*)event.GetClientData();
// Now close all tabs in the notebook related to the one in the event
int i=0;
while(i<notebook->GetPageCount()){
wxWindow *page=notebook->GetPage(i);
TabInfos* infos=(TabInfos*)page->GetClientData();
if(infos->is_linked && infos->linked_id==infosEvent->id){
notebook->DeletePage(i); // Remove page
i=0; // Restart to page 0 since notebook updated (we just remove one page)
}
else
i++;
}
2022-12-24 22:30:20 +01:00
}
2022-02-26 21:43:09 +01:00
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();
2022-02-28 15:27:51 +01:00
} else if (id == 5) {
OpenFile();
2022-02-26 21:43:09 +01:00
} 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);
ApplyPreferences(); // Propagate motifications
2022-02-26 21:43:09 +01:00
}
void MainWindow::NewEngine() {
2022-02-26 19:21:52 +01:00
wxFileDialog openFileDialog(this, _("Use engine"), "", "", "Executable|*",
wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if (openFileDialog.ShowModal() != wxID_CANCEL) {
std::string path = openFileDialog.GetPath().ToStdString();
uciadapter::UCI *engine;
try {
engine = new uciadapter::UCI(path);
EngineTab *bt = new EngineTab((wxWindow *)notebook, engine, path);
2022-12-24 22:30:20 +01:00
AddPage(bt,bt);
2022-02-26 19:21:52 +01:00
} catch (...) {
SHOW_DIALOG_ERROR("Could not communicate with the engine");
}
}
}
2022-02-26 21:43:09 +01:00
void MainWindow::OpenSettings() {
2022-02-23 19:41:50 +01:00
if (prefsEditor != NULL) {
delete prefsEditor;
}
prefsEditor = new wxPreferencesEditor("Preferences");
prefsEditor->AddPage(new BoardPrefs());
prefsEditor->AddPage(new EditorPrefs());
prefsEditor->Show(this);
2022-02-23 18:11:55 +01:00
}
void MainWindow::ApplyPreferences() {
for (int i = 0; i < notebook->GetPageCount(); i++) {
TabInfos *infos = dynamic_cast<TabInfos *>(notebook->GetPage(i));
infos->ApplyPreferences();
}
}
2022-02-23 19:41:50 +01:00
void MainWindow::OnClose(wxCloseEvent &e) {
if (prefsEditor != NULL) {
prefsEditor->Dismiss();
}
e.Skip();
}
2022-12-24 14:01:39 +01:00
2022-02-26 21:43:09 +01:00
void MainWindow::OpenFile() {
2022-02-23 18:11:55 +01:00
wxFileDialog openFileDialog(this, _("Open file"), "", "",
"PGN files (*.pgn)|*.pgn",
wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if (openFileDialog.ShowModal() != wxID_CANCEL) {
std::string path = openFileDialog.GetPath().ToStdString();
2022-02-25 09:21:26 +01:00
// Test base tab
BaseTab *bt = new BaseTab((wxFrame *)notebook, path);
2022-12-24 22:30:20 +01:00
AddPage(bt,bt);
2022-02-23 18:11:55 +01:00
}
}
2022-02-26 21:43:09 +01:00
void MainWindow::NewGame(bool useFen) {
if (useFen) {
2022-02-23 18:11:55 +01:00
wxTextEntryDialog *dial =
new wxTextEntryDialog(NULL, wxT("Enter FEN:"), wxT("Error"));
if (dial->ShowModal() == wxID_OK) {
try {
NewGame(new Game(dial->GetValue().ToStdString()));
} catch (...) {
SHOW_DIALOG_ERROR("Invalid FEN");
}
}
} else {
2022-02-28 21:56:44 +01:00
NewGame(std::shared_ptr<Game>(new Game()));
2022-02-23 18:11:55 +01:00
}
}
2022-02-24 21:10:29 +01:00
void MainWindow::OnNewGame(wxCommandEvent &event) {
2022-12-24 12:46:59 +01:00
TabInfos *tab = (TabInfos*)event.GetClientData();
TabInfos *i=NewGame(tab->GetGame());
i->Link(tab);
}
2022-02-23 18:11:55 +01:00
void MainWindow::OnPageChange(wxAuiNotebookEvent &event) {
TabInfos *infos = dynamic_cast<TabInfos *>(notebook->GetCurrentPage());
if (infos->type != TabInfos::GAME) {
for (short i = 10; i < 20; i++) {
2022-02-28 15:27:51 +01:00
if (menu_game->FindChildItem(i) != NULL) {
menu_game->Enable(i, false);
2022-02-23 18:11:55 +01:00
}
}
}
}
void MainWindow::OnRefreshTabTitle(wxCommandEvent &event) {
GameTab *win = dynamic_cast<GameTab *>(event.GetEventObject());
int page = notebook->GetPageIndex(win);
if (page != wxNOT_FOUND) {
notebook->SetPageText(page, win->GetLabel());
}
}
2022-12-24 12:46:59 +01:00
TabInfos* MainWindow::NewGame(std::shared_ptr<Game> game) {
2022-02-23 18:11:55 +01:00
GameTab *gt = new GameTab((wxFrame *)notebook, game);
2022-12-24 14:01:39 +01:00
this->AddPage(gt,gt);
2022-12-24 12:46:59 +01:00
return(gt);
2022-02-23 18:11:55 +01:00
}