From 5a43d62920f82bac38490ac92dd9a49edd1d44de Mon Sep 17 00:00:00 2001
From: Loic Guegan <manzerbredes@mailbox.org>
Date: Wed, 23 Feb 2022 19:41:50 +0100
Subject: [PATCH] Debug and improve preferences editor

---
 src/MainWindow.cpp                     | 28 +++++++++----
 src/MainWindow.hpp                     |  3 ++
 src/preferences/BoardPrefs.hpp         |  1 -
 src/preferences/EditorPrefs.hpp        | 44 ++++++++++++++++++++
 src/preferences/EditorPrefsPanelBF.cpp | 18 ++++++++
 src/preferences/EditorPrefsPanelBF.h   | 38 +++++++++++++++++
 src/preferences/preferences.hpp        |  3 +-
 tools/wxframebuilder/EditorPrefs.fbp   | 57 ++++++++++++++++++++++++++
 8 files changed, 182 insertions(+), 10 deletions(-)
 create mode 100644 src/preferences/EditorPrefs.hpp
 create mode 100644 src/preferences/EditorPrefsPanelBF.cpp
 create mode 100644 src/preferences/EditorPrefsPanelBF.h
 create mode 100644 tools/wxframebuilder/EditorPrefs.fbp

diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp
index a1afc7b..83fd964 100644
--- a/src/MainWindow.cpp
+++ b/src/MainWindow.cpp
@@ -2,7 +2,6 @@
 #include "ChessArbiter.hpp"
 #include "pgnp.hpp"
 #include "preferences/preferences.hpp"
-#include <wx/preferences.h>
 
 wxDEFINE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent);
 
@@ -10,7 +9,8 @@ wxDEFINE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent);
 
 MainWindow::MainWindow()
     : wxFrame(NULL, wxID_ANY, "OChess: The Open Chess software",
-              wxDefaultPosition, wxSize(1500, 1000)) {
+              wxDefaultPosition, wxSize(1500, 1000)),
+      prefsEditor(NULL) {
   CreateStatusBar();
   SetStatusText("OChess");
 
@@ -46,6 +46,7 @@ MainWindow::MainWindow()
   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);
 }
 
 class AdvancePage : public wxPreferencesPage {
@@ -65,9 +66,13 @@ public:
 };
 
 void MainWindow::OnSettings(wxCommandEvent &event) {
-  wxPreferencesEditor *edt = new wxPreferencesEditor("Preferences");
-  edt->AddPage(new BoardPrefs());
-  edt->Show(this);
+  if (prefsEditor != NULL) {
+    delete prefsEditor;
+  }
+  prefsEditor = new wxPreferencesEditor("Preferences");
+  prefsEditor->AddPage(new BoardPrefs());
+  prefsEditor->AddPage(new EditorPrefs());
+  prefsEditor->Show(this);
 }
 
 void MainWindow::ApplyPreferences() {
@@ -79,6 +84,13 @@ void MainWindow::ApplyPreferences() {
 
 void MainWindow::OnExit(wxCommandEvent &event) { Close(true); }
 
+void MainWindow::OnClose(wxCloseEvent &e) {
+  if (prefsEditor != NULL) {
+    prefsEditor->Dismiss();
+  }
+  e.Skip();
+}
+
 void MainWindow::OnOpen(wxCommandEvent &event) {
   wxFileDialog openFileDialog(this, _("Open file"), "", "",
                               "PGN files (*.pgn)|*.pgn",
@@ -97,9 +109,9 @@ void MainWindow::OnOpen(wxCommandEvent &event) {
         fen = pgn.GetTagValue("FEN");
       }
       HalfMove *m = new HalfMove(pgnp_moves, fen);
-      Game *g=new Game(m,fen);
-      for(std::string &s:pgn.GetTagList()){
-        g->SetTag(s,pgn.GetTagValue(s));
+      Game *g = new Game(m, fen);
+      for (std::string &s : pgn.GetTagList()) {
+        g->SetTag(s, pgn.GetTagValue(s));
       }
       NewGame(g);
     } catch (std::exception &e) {
diff --git a/src/MainWindow.hpp b/src/MainWindow.hpp
index 78d481e..31e63d8 100644
--- a/src/MainWindow.hpp
+++ b/src/MainWindow.hpp
@@ -3,6 +3,7 @@
 #include <wx/aui/auibook.h>
 #include <wx/filedlg.h>
 #include <wx/textdlg.h>
+#include <wx/preferences.h>
 
 wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent);
 
@@ -10,8 +11,10 @@ class MainWindow : public wxFrame {
   wxAuiNotebook *notebook;
   wxMenu *menuGame;
   wxMenuBar *menuBar;
+  wxPreferencesEditor *prefsEditor;
 
   void OnExit(wxCommandEvent &event);
+  void OnClose(wxCloseEvent &e);
   void OnNewGame(wxCommandEvent &event);
   void OnOpen(wxCommandEvent &event);
   void OnPageChange(wxAuiNotebookEvent &event);
diff --git a/src/preferences/BoardPrefs.hpp b/src/preferences/BoardPrefs.hpp
index eff3b58..21fe1ce 100644
--- a/src/preferences/BoardPrefs.hpp
+++ b/src/preferences/BoardPrefs.hpp
@@ -96,7 +96,6 @@ public:
 
   virtual bool TransferDataFromWindow() {
     ApplyPreferences();
-    MAINWIN->ApplyPreferences();
     return (true);
   }
 };
diff --git a/src/preferences/EditorPrefs.hpp b/src/preferences/EditorPrefs.hpp
new file mode 100644
index 0000000..dab05ca
--- /dev/null
+++ b/src/preferences/EditorPrefs.hpp
@@ -0,0 +1,44 @@
+#include "EditorPrefsPanelBF.h"
+#include "game_tab/board/BoardCanvas.hpp"
+#include "ochess.hpp"
+#include <wx/combobox.h>
+#include <wx/dir.h>
+#include <wx/filename.h>
+#include <wx/preferences.h>
+#include <wx/spinctrl.h>
+#include <wx/stdpaths.h>
+
+class EditorPrefsPanel : public EditorPrefsPanelBF {
+
+public:
+  EditorPrefsPanel(wxWindow *parent) : EditorPrefsPanelBF(parent) {
+   
+   // Bind(wxEVT_SPINCTRL, &BoardPrefsPanel::OnConfChange, this, wxID_ANY);
+  }
+  void OnConfChange(wxCommandEvent &event) {
+  }
+
+  virtual bool TransferDataToWindow() {
+    return true;
+  }
+
+  void ApplyPreferences() {
+  
+  }
+
+  virtual bool TransferDataFromWindow() {
+
+    return (true);
+  }
+};
+
+class EditorPrefs : public wxPreferencesPage {
+public:
+  virtual wxString GetName() const { return "Editor"; }
+  virtual wxBitmap GetLargeIcon() {
+    return wxArtProvider::GetBitmap(wxART_HELP, wxART_TOOLBAR);
+  }
+  virtual wxWindow *CreateWindow(wxWindow *parent) {
+    return new EditorPrefsPanel(parent);
+  }
+};
\ No newline at end of file
diff --git a/src/preferences/EditorPrefsPanelBF.cpp b/src/preferences/EditorPrefsPanelBF.cpp
new file mode 100644
index 0000000..08a1b50
--- /dev/null
+++ b/src/preferences/EditorPrefsPanelBF.cpp
@@ -0,0 +1,18 @@
+///////////////////////////////////////////////////////////////////////////
+// C++ code generated with wxFormBuilder (version 3.10.1-40-g8042f487)
+// http://www.wxformbuilder.org/
+//
+// PLEASE DO *NOT* EDIT THIS FILE!
+///////////////////////////////////////////////////////////////////////////
+
+#include "EditorPrefsPanelBF.h"
+
+///////////////////////////////////////////////////////////////////////////
+
+EditorPrefsPanelBF::EditorPrefsPanelBF( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name ) : wxPanel( parent, id, pos, size, style, name )
+{
+}
+
+EditorPrefsPanelBF::~EditorPrefsPanelBF()
+{
+}
diff --git a/src/preferences/EditorPrefsPanelBF.h b/src/preferences/EditorPrefsPanelBF.h
new file mode 100644
index 0000000..8736a59
--- /dev/null
+++ b/src/preferences/EditorPrefsPanelBF.h
@@ -0,0 +1,38 @@
+///////////////////////////////////////////////////////////////////////////
+// C++ code generated with wxFormBuilder (version 3.10.1-40-g8042f487)
+// http://www.wxformbuilder.org/
+//
+// PLEASE DO *NOT* EDIT THIS FILE!
+///////////////////////////////////////////////////////////////////////////
+
+#pragma once
+
+#include <wx/artprov.h>
+#include <wx/xrc/xmlres.h>
+#include <wx/panel.h>
+#include <wx/gdicmn.h>
+#include <wx/font.h>
+#include <wx/colour.h>
+#include <wx/settings.h>
+#include <wx/string.h>
+
+///////////////////////////////////////////////////////////////////////////
+
+
+///////////////////////////////////////////////////////////////////////////////
+/// Class EditorPrefsPanelBF
+///////////////////////////////////////////////////////////////////////////////
+class EditorPrefsPanelBF : public wxPanel
+{
+	private:
+
+	protected:
+
+	public:
+
+		EditorPrefsPanelBF( wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 500,300 ), long style = wxTAB_TRAVERSAL, const wxString& name = wxEmptyString );
+
+		~EditorPrefsPanelBF();
+
+};
+
diff --git a/src/preferences/preferences.hpp b/src/preferences/preferences.hpp
index d5dd603..2566111 100644
--- a/src/preferences/preferences.hpp
+++ b/src/preferences/preferences.hpp
@@ -1,2 +1,3 @@
 #pragma once
-#include "BoardPrefs.hpp"
\ No newline at end of file
+#include "BoardPrefs.hpp"
+#include "EditorPrefs.hpp"
diff --git a/tools/wxframebuilder/EditorPrefs.fbp b/tools/wxframebuilder/EditorPrefs.fbp
new file mode 100644
index 0000000..586f241
--- /dev/null
+++ b/tools/wxframebuilder/EditorPrefs.fbp
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
+<wxFormBuilder_Project>
+    <FileVersion major="1" minor="16" />
+    <object class="Project" expanded="1">
+        <property name="class_decoration">; </property>
+        <property name="code_generation">C++</property>
+        <property name="disconnect_events">1</property>
+        <property name="disconnect_mode">source_name</property>
+        <property name="disconnect_php_events">0</property>
+        <property name="disconnect_python_events">0</property>
+        <property name="embedded_files_path">res</property>
+        <property name="encoding">UTF-8</property>
+        <property name="event_generation">connect</property>
+        <property name="file">EditorPrefsPanelBF</property>
+        <property name="first_id">1000</property>
+        <property name="help_provider">none</property>
+        <property name="image_path_wrapper_function_name"></property>
+        <property name="indent_with_spaces"></property>
+        <property name="internationalize">0</property>
+        <property name="name">EditorPrefsPanelBF</property>
+        <property name="namespace"></property>
+        <property name="path">../../src/preferences/</property>
+        <property name="precompiled_header"></property>
+        <property name="relative_path">1</property>
+        <property name="skip_lua_events">1</property>
+        <property name="skip_php_events">1</property>
+        <property name="skip_python_events">1</property>
+        <property name="ui_table">UI</property>
+        <property name="use_array_enum">0</property>
+        <property name="use_enum">0</property>
+        <property name="use_microsoft_bom">0</property>
+        <object class="Panel" expanded="1">
+            <property name="aui_managed">0</property>
+            <property name="aui_manager_style">wxAUI_MGR_DEFAULT</property>
+            <property name="bg"></property>
+            <property name="context_help"></property>
+            <property name="context_menu">1</property>
+            <property name="enabled">1</property>
+            <property name="event_handler">impl_virtual</property>
+            <property name="fg"></property>
+            <property name="font"></property>
+            <property name="hidden">0</property>
+            <property name="id">wxID_ANY</property>
+            <property name="maximum_size"></property>
+            <property name="minimum_size"></property>
+            <property name="name">EditorPrefsPanelBF</property>
+            <property name="pos"></property>
+            <property name="size">500,300</property>
+            <property name="subclass">; ; forward_declare</property>
+            <property name="tooltip"></property>
+            <property name="two_step_creation">0</property>
+            <property name="window_extra_style"></property>
+            <property name="window_name"></property>
+            <property name="window_style">wxTAB_TRAVERSAL</property>
+        </object>
+    </object>
+</wxFormBuilder_Project>