aboutsummaryrefslogtreecommitdiff
path: root/src/MainWindow.cpp
blob: b048c62304c1a2481ff3eab0c889eea55370ff8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "MainWindow.hpp"
#include "ChessArbiter.hpp"
#include "UCI.hpp"
#include "engine_tab/EngineTab.hpp"
#include "pgnp.hpp"
#include "preferences/preferences.hpp"

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 ----------

MainWindow::MainWindow()
    : wxFrame(NULL, wxID_ANY, "OChess: The Open Chess software",
              wxDefaultPosition, wxSize(1500, 1000)),
      prefsEditor(NULL) {
  CreateStatusBar();
  SetStatusText("OChess");

  /// File menu
  wxMenu *menuFile = new wxMenu;
  menuFile->Append(1, "Open", "Open file");
  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");
  menuFile->AppendSeparator();
  menuFile->Append(wxID_EXIT);

  // Game menu
  menuGame = new wxMenu;
  menuGame->Append(2, "New", "Create new game");
  menuGame->Append(3, "New from FEN", "Create new game using FEN");

  // Game base menu
  wxMenu *menuBase = new wxMenu;
  menuBase->Append(5, "New", "Create new database");

  // Engine menu
  wxMenu *engineMenu = new wxMenu;
  engineMenu->Append(6, "New", "Create a new engine configuration");
  manageMenu = new wxMenu;
  engineMenu->AppendSubMenu(manageMenu, "Manage");
  wxCommandEvent dummy(REFRESH_ENGINE_LIST, GetId());
  OnRefreshEngineList(dummy);

  /// Menu bar
  menuBar = new wxMenuBar;
  menuBar->Append(menuFile, "&File");
  menuBar->Append(menuGame, "&Game");
  menuBar->Append(menuBase, "&Database");
  menuBar->Append(engineMenu, "&Engines");
  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);
  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));*/
}

void MainWindow::OnCloseTabEvent(wxCommandEvent &event) {
  notebook->DeletePage(notebook->GetSelection());
}

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) {
    std::string path = openFileDialog.GetPath().ToStdString();
    uciadapter::UCI *engine;
    try {
      engine = new uciadapter::UCI(path);
      EngineTab *bt = new EngineTab((wxWindow *)notebook, engine, path);
      notebook->AddPage(bt, bt->GetLabel());
      notebook->SetSelection(notebook->GetPageIndex(bt));
    } catch (...) {
      SHOW_DIALOG_ERROR("Could not communicate with the engine");
    }
  }
}

void MainWindow::OpenSettings() {
  if (prefsEditor != NULL) {
    delete prefsEditor;
  }
  prefsEditor = new wxPreferencesEditor("Preferences");
  prefsEditor->AddPage(new BoardPrefs());
  prefsEditor->AddPage(new EditorPrefs());
  prefsEditor->Show(this);
}

void MainWindow::ApplyPreferences() {
  for (int i = 0; i < notebook->GetPageCount(); i++) {
    TabInfos *infos = dynamic_cast<TabInfos *>(notebook->GetPage(i));
    infos->ApplyPreferences();
  }
}

std::vector<TabInfos *> MainWindow::ListTabInfos() {
  std::vector<TabInfos *> tinfos;
  for (int i = 0; i < notebook->GetPageCount(); i++) {
    tinfos.push_back(dynamic_cast<TabInfos *>(notebook->GetPage(i)));
  }
  return (tinfos);
}

void MainWindow::OnClose(wxCloseEvent &e) {
  if (prefsEditor != NULL) {
    prefsEditor->Dismiss();
  }
  e.Skip();
}

void MainWindow::OpenFile() {
  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();
    // Test base tab
    BaseTab *bt = new BaseTab((wxFrame *)notebook, path);
    notebook->AddPage(bt, bt->GetLabel());
    notebook->SetSelection(notebook->GetPageIndex(bt));
  }
}

void MainWindow::NewGame(bool useFen) {
  if (useFen) {
    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::OnNewGame(wxCommandEvent &event) {
  Game *g = (Game *)event.GetClientData();
  NewGame(g);
}

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));
}