aboutsummaryrefslogtreecommitdiff
path: root/src/MainWindow.cpp
blob: 55f35f5d17077aedd0df01866c11c0df7c4a485a (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#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);
wxDEFINE_EVENT(CLOSE_LINKED_TAB, wxCommandEvent);


/// ---------- MainWindow ----------

MainWindow::MainWindow()
    : MainFrame(NULL, wxID_ANY, "OChess: The Open Chess software",
                wxDefaultPosition, wxSize(1500, 1000)),
      prefsEditor(nullptr), engine_count(0) {
  SetStatusText("OChess v"+std::string(OCHESS_VERSION));

  /// File menu
  menu_file->Append(1, "Settings", "Configure OChess");
  menu_file->AppendSeparator();
  menu_file->Append(wxID_EXIT);

  // Game menu
  menu_game->Append(2, "New", "Create new game");
  menu_game->Append(3, "New from position", "Create new game using FEN");
  menu_game->Append(4, "Open", "Open first game of a file");

  // Game base menu
  menu_db->Append(5, "New", "Create database");
  menu_db->Append(6, "Open", "Open database");

  // Engine menu
  menu_engine->Append(7, "New", "Create a new engine configuration");
  menu_engine->AppendSeparator();
  // Dynamically build manage submenu
  manageMenu = new wxMenu;
  menu_engine->AppendSubMenu(manageMenu, "Manage");
  wxCommandEvent dummy(REFRESH_ENGINE_LIST, GetId());
  OnRefreshEngineList(dummy);

  // Help menu
  menu_help->Append(wxID_ABOUT, "About", "OChess Informations");

  Bind(wxEVT_AUINOTEBOOK_PAGE_CHANGED, &MainWindow::OnPageChange, this,
       wxID_ANY);
  Bind(REFRESH_TAB_TITLE, &MainWindow::OnRefreshTabTitle, 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);
  Bind(CLOSE_LINKED_TAB, &MainWindow::OnCloseTabLinkedTo, this, wxID_ANY);
  Bind(wxEVT_AUINOTEBOOK_PAGE_CLOSED, &MainWindow::OnAuiNotebookPageClosed, this, wxID_ANY);
  Bind(wxEVT_AUINOTEBOOK_PAGE_CLOSE, &MainWindow::OnAuiNotebookPageCheck, this, wxID_ANY);

  // Add new game tab by default
  NewGame(std::shared_ptr<Game>(new Game()));


  // Temporary TO REMOVE JUST FOR TESTS:
  //BaseTab *bt = new BaseTab((wxFrame *)notebook, "/home/loic/pgn/wijk_2003_annotated.pgn");
  //this->AddPage(bt,bt);
}

void MainWindow::OnAuiNotebookPageCheck(wxAuiNotebookEvent& event){
  int selection=event.GetSelection();
  TabInfos *t=(TabInfos*)notebook->GetPage(selection)->GetClientData();
  if(t->is_dirty){
      wxMessageDialog *dial = new wxMessageDialog(NULL,
      wxT("This tab contains data that are not saved. Are you sure you want to close it?"), wxT("Information"),
      wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);
      if(dial->ShowModal() == wxID_YES)
        event.Allow();
      else
        event.Veto();
  }
}

void MainWindow::AddPage(wxWindow* window, TabInfos* infos){
  window->SetClientData(infos); // Allows to have safer cast in this class
  notebook->AddPage(window, window->GetLabel());
  notebook->SetSelection(notebook->GetPageIndex(window));
  // Refresh tab that require knowledge on other tabs
  for(auto i: wxGetApp().ListTabInfos()){i->Refresh();}
}

void MainWindow::OnAuiNotebookPageClosed(wxAuiNotebookEvent& event){
  // Refresh tab that require knowledge on other tabs
  for(auto i: wxGetApp().ListTabInfos()){i->Refresh();}
}

void MainWindow::OnCloseTabEvent(wxCommandEvent &event) {
  notebook->DeletePage(notebook->GetSelection());
  // Refresh tab that require knowledge on other tabs
  for(auto i: wxGetApp().ListTabInfos()){i->Refresh();}
}

void MainWindow::OnCloseTabLinkedTo(wxCommandEvent &event){
  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++;
  }
}

void MainWindow::OnMenuItemClick(wxCommandEvent &event) {
  int id = event.GetId();
  if (id == wxID_EXIT) {
    Close(true);
  } else if (id >= 100 && id <=(100+engine_count)) { // Engine from manage menu
    wxMenuItemList items = manageMenu->GetMenuItems();
    for (wxMenuItem *item : items) {
      if (item->GetId() == id) {
        std::uint32_t engine_id=item->GetId()-100;
        // Check if not already opened
        for(auto i: wxGetApp().ListTabInfos()){
          if(i->type==TabInfos::ENGINE && i->GetEngineId()==engine_id){
            wxGetApp().FocusOnTab(i);
            return;
          }
        }
        // Open engine configuration tag:
        wxLogDebug("Selected %s", item->GetItemLabel());
        EngineTab *et = new EngineTab((wxWindow *)notebook,
                                      engine_id);
        AddPage(et,et);
      }
    }
  } else if (id == 1) { // Settings
    OpenSettings();
  } else if (id == 2) { // New game
    NewGame(false);
  } else if (id == 3) { // New game from FEN
    NewGame(true);
  } else if (id == 4) { // Open first game
    wxFileDialog openFileDialog(this, _("Open single game"), "", "",
                              "PGN files (*.pgn)|*.pgn",
                              wxFD_OPEN | wxFD_FILE_MUST_EXIST);
    if (openFileDialog.ShowModal() != wxID_CANCEL) {
      std::string path = openFileDialog.GetPath().ToStdString();
      NewGame(OpenGameX(path,0));
    }
  } else if (id == 5) { // Create database
    wxFileDialog 
        newFileDialog(this, _("Create database file"), "", "",
                       "PGN files (*.pgn)|*.pgn", wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
    if (newFileDialog.ShowModal() == wxID_CANCEL)
        return;
    // Create and open new db
    std::string path = newFileDialog.GetPath().ToStdString();
    BaseTab *bt = new BaseTab((wxFrame *)notebook, path);
    AddPage(bt,bt);
  } else if (id == 6) { // Open Database
    wxFileDialog openFileDialog(this, _("Open database"), "", "",
                              "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);
      AddPage(bt,bt);
    }
  } else if (id == 7) { // Create new engine
    NewEngine();
  } else if (id == wxID_ABOUT) { // Create new engine
    ShowAbout();
  }
}

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_id;
  long index;
  if (conf->GetFirstGroup(engine_id, index)) {
    std::uint32_t id = 0;
    do {
      wxString engine_name=conf->Read(engine_id+"/name");
      manageMenu->Append(100 + id, engine_name, "Configure " + engine_name);
      id++;
    } while (conf->GetNextGroup(engine_id, index));
    engine_count=id;
  }
  CONFIG_CLOSE(conf);
  ApplyPreferences(); // Propagate informations to the tabs that require it
}

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);
      AddPage(bt,bt);
    } catch (...) {
      SHOW_DIALOG_ERROR("Could not communicate with the engine");
    }
  }
}

void MainWindow::OpenSettings() {
  if (prefsEditor != nullptr) {
    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 = (TabInfos*)(notebook->GetPage(i))->GetClientData();
    infos->ApplyPreferences();
  }
}

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


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(std::shared_ptr<Game>(new Game()));
  }
}

void MainWindow::OnPageChange(wxAuiNotebookEvent &event) {
  TabInfos *infos = (TabInfos*)(notebook->GetCurrentPage())->GetClientData();
  if (infos->type != TabInfos::GAME) {
    for (short i = 10; i < 20; i++) {
      if (menu_game->FindChildItem(i) != NULL) {
        menu_game->Enable(i, false);
      }
    }
  }
}

void MainWindow::OnRefreshTabTitle(wxCommandEvent &event) {
  wxWindow *win = (wxWindow*)event.GetEventObject();
  int page = notebook->GetPageIndex(win);
  if (page != wxNOT_FOUND) {
    notebook->SetPageText(page, win->GetLabel());
  }
}

TabInfos* MainWindow::NewGame(std::shared_ptr<Game> game) {
  GameTab *gt = new GameTab((wxFrame *)notebook, game);
  this->AddPage(gt,gt);
  return(gt);
}

void MainWindow::ShowAbout(){
  DialogAbout *dialog=new DialogAbout(this);
  wxFont font(wxFontInfo(12));
  dialog->app_icon->SetBitmap(LoadPNG("ochess",wxSize(80,80)));
  dialog->appname_text->SetFont(wxFont(wxFontInfo(18).Bold()));
  dialog->appname_text->SetLabel(wxT("OChess v"+std::string(OCHESS_VERSION)));

  // Populate info:
  wxRichTextCtrl *t=dialog->info_richtext;
  t->SetFont(font);
  t->BeginAlignment(wxTEXT_ALIGNMENT_CENTRE);
  t->BeginBold();
  t->BeginFontSize(20);
  t->WriteText(wxT("OChess"));
  t->EndFontSize();
  t->Newline();
  t->EndBold();
  t->WriteText(wxT("An open source software for chess games analysis and games databases management."));
  t->Newline();
  t->Newline();
  t->BeginFontSize(8);
  t->WriteText(wxT("OChess is delivered under GPLv3 license"));
  t->Newline();
  t->WriteText(wxT("Built on "+std::string(BUILD_TIME)));
  t->EndFontSize();
  t->Newline();
  t->Newline();
  t->WriteText("Repository:");
  t->Newline();
  t->BeginURL("https://gitlab.com/manzerbredes/ochess");
  t->BeginUnderline();
  t->WriteText(wxT("https://gitlab.com/manzerbredes/ochess"));
  t->EndUnderline();
  t->EndURL();
  t->Newline();
  t->Newline();
  t->BeginFontSize(10);
  t->WriteText("For any questions/discussion please join the IRC chat at ");
  t->BeginURL("irc://irc.libera.chat/ochess");
  t->BeginUnderline();
  t->WriteText(wxT("irc://irc.libera.chat/ochess"));
  t->EndUnderline();
  t->EndURL();
  t->EndFontSize();
  t->Newline();
  t->EndAlignment();

  // Populate credits:
  wxRichTextCtrl *c=dialog->credits_richtext;
  c->SetFont(font);
  c->BeginBold();
  c->WriteText("Developers:");
  c->EndBold();
  c->Newline();
  c->WriteText(" - manzerbredes <manzerbredes@mailbox.org>");

  dialog->Show();
}