aboutsummaryrefslogtreecommitdiff
path: root/src/MainWindow.cpp
diff options
context:
space:
mode:
authorLoic Guegan <manzerbredes@mailbox.org>2022-02-23 18:11:55 +0100
committerLoic Guegan <manzerbredes@mailbox.org>2022-02-23 18:11:55 +0100
commitce941c146aea7925bded6b9d2a0d0559d3156ad3 (patch)
tree4c52e02600e3fd127bfb28b3e974d45541ec9e4e /src/MainWindow.cpp
Create repository
Diffstat (limited to 'src/MainWindow.cpp')
-rw-r--r--src/MainWindow.cpp146
1 files changed, 146 insertions, 0 deletions
diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp
new file mode 100644
index 0000000..09d42df
--- /dev/null
+++ b/src/MainWindow.cpp
@@ -0,0 +1,146 @@
+#include "MainWindow.hpp"
+#include "ChessArbiter.hpp"
+#include "pgnp.hpp"
+#include "preferences/preferences.hpp"
+#include <wx/preferences.h>
+
+wxDEFINE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent);
+
+/// ---------- MainWindow ----------
+
+MainWindow::MainWindow()
+ : wxFrame(NULL, wxID_ANY, "OChess: The Open Chess software",
+ wxDefaultPosition, wxSize(1500, 1000)) {
+ CreateStatusBar();
+ SetStatusText("OChess");
+
+ /// File menu
+ wxMenu *menuFile = new wxMenu;
+ menuFile->Append(1, "Open", "Open file");
+ Bind(wxEVT_MENU, &MainWindow::OnOpen, this, 1);
+ 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);
+
+ menuGame = new wxMenu;
+ menuGame->Append(2, "New", "Create new game");
+ Bind(wxEVT_MENU, &MainWindow::OnNewGame, this, 2);
+ menuGame->Append(3, "New from FEN", "Create new game using FEN");
+ Bind(wxEVT_MENU, &MainWindow::OnNewGame, this, 3);
+ menuGame->AppendSeparator();
+ menuGame->Append(10, "Save", "Save current game");
+ menuGame->Append(11, "Save As", "Save current game as");
+
+ /// Menu bar
+ menuBar = new wxMenuBar;
+ menuBar->Append(menuFile, "&File");
+ menuBar->Append(menuGame, "&Game");
+ SetMenuBar(menuBar);
+
+ // Create the wxNotebook widget
+ notebook = new wxAuiNotebook(this, wxID_ANY);
+ NewGame(new Game());
+
+ Bind(wxEVT_AUINOTEBOOK_PAGE_CHANGED, &MainWindow::OnPageChange, this,
+ wxID_ANY);
+ Bind(REFRESH_TAB_TITLE, &MainWindow::OnRefreshTabTitle, this, wxID_ANY);
+}
+
+class AdvancePage : public wxPreferencesPage {
+public:
+ virtual wxString GetName() const { return "Topics"; }
+ virtual wxBitmap GetLargeIcon() {
+ return wxArtProvider::GetBitmap(wxART_HELP, wxART_TOOLBAR);
+ }
+ virtual wxWindow *CreateWindow(wxWindow *parent) {
+ wxPanel *p =
+ new wxPanel(parent, wxID_ANY, wxDefaultPosition, wxSize(800, 800));
+ wxBoxSizer *s = new wxBoxSizer(wxVERTICAL);
+ s->Add(new wxButton(p, COPY_FEN_BTN, L"Copy FEN"), 1, wxEXPAND);
+ p->SetSizer(s);
+ return p;
+ }
+};
+
+void MainWindow::OnSettings(wxCommandEvent &event) {
+ wxPreferencesEditor *edt = new wxPreferencesEditor("Preferences");
+ edt->AddPage(new BoardPrefs());
+ edt->Show(this);
+}
+
+void MainWindow::ApplyPreferences() {
+ for (int i = 0; i < notebook->GetPageCount(); i++) {
+ TabInfos *infos = dynamic_cast<TabInfos *>(notebook->GetPage(i));
+ infos->ApplyPreferences();
+ }
+}
+
+void MainWindow::OnExit(wxCommandEvent &event) { Close(true); }
+
+void MainWindow::OnOpen(wxCommandEvent &event) {
+ 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();
+ pgnp::PGN pgn;
+ try {
+ pgn.FromFile(path);
+ pgn.ParseNextGame();
+ pgnp::HalfMove *pgnp_moves = new pgnp::HalfMove();
+ pgn.GetMoves(pgnp_moves);
+ std::string fen =
+ "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
+ if (pgn.HasTag("FEN")) {
+ fen = pgn.GetTagValue("FEN");
+ }
+ HalfMove *m = new HalfMove(pgnp_moves, fen);
+ NewGame(new Game(m));
+ } catch (...) {
+ SHOW_DIALOG_ERROR("Invalid PGN file");
+ }
+ }
+}
+
+void MainWindow::OnNewGame(wxCommandEvent &event) {
+ if (event.GetId() == 3) {
+ 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 {
+ NewGame(new Game());
+ }
+}
+
+void MainWindow::OnPageChange(wxAuiNotebookEvent &event) {
+ TabInfos *infos = dynamic_cast<TabInfos *>(notebook->GetCurrentPage());
+ if (infos->type != TabInfos::GAME) {
+ for (short i = 10; i < 20; i++) {
+ if (menuGame->FindChildItem(i) != NULL) {
+ menuGame->Enable(i, false);
+ }
+ }
+ }
+}
+
+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());
+ }
+}
+
+void MainWindow::NewGame(Game *game) {
+ GameTab *gt = new GameTab((wxFrame *)notebook, game);
+ notebook->AddPage(gt, gt->GetLabel());
+ notebook->SetSelection(notebook->GetPageIndex(gt));
+}