From 8956ef71c4dec681d940557bf4bcb1cca1261a08 Mon Sep 17 00:00:00 2001 From: George Tokmaji Date: Wed, 22 Mar 2023 09:45:57 +0100 Subject: [PATCH] C4ComponentHost: Move Windows-only dialog-related functionality into C4Console --- src/C4ComponentHost.cpp | 87 ------------------------------------ src/C4ComponentHost.h | 10 ++--- src/C4Console.cpp | 98 +++++++++++++++++++++++++++++++++++++++-- src/C4Console.h | 1 + 4 files changed, 100 insertions(+), 96 deletions(-) diff --git a/src/C4ComponentHost.cpp b/src/C4ComponentHost.cpp index 31bb5b716..bfe3e4a3f 100644 --- a/src/C4ComponentHost.cpp +++ b/src/C4ComponentHost.cpp @@ -21,68 +21,6 @@ #include #include -#ifdef _WIN32 -#include "StdRegistry.h" -#include "res/engine_resource.h" -#endif - -C4ComponentHost *pCmpHost = nullptr; - -#ifdef _WIN32 - -INT_PTR CALLBACK ComponentDlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam) -{ - if (!pCmpHost) return FALSE; - - switch (Msg) - { - case WM_CLOSE: - pCmpHost->Close(); - break; - - case WM_DESTROY: - StoreWindowPosition(hDlg, "Component", Config.GetSubkeyPath("Console"), false); - break; - - case WM_INITDIALOG: - pCmpHost->InitDialog(hDlg); - RestoreWindowPosition(hDlg, "Component", Config.GetSubkeyPath("Console")); - return TRUE; - - case WM_COMMAND: - // Evaluate command - switch (LOWORD(wParam)) - { - case IDCANCEL: - pCmpHost->Close(); - return TRUE; - - case IDOK: - // IDC_EDITDATA to Data - char buffer[65000]; - GetDlgItemText(hDlg, IDC_EDITDATA, buffer, 65000); - pCmpHost->Modified = true; - pCmpHost->Data.Copy(buffer); - pCmpHost->Close(); - return TRUE; - } - return FALSE; - } - return FALSE; -} - -void C4ComponentHost::InitDialog(HWND hDlg) -{ - hDialog = hDlg; - // Set text - SetWindowText(hDialog, Name); - SetDlgItemText(hDialog, IDOK, LoadResStr("IDS_BTN_OK")); - SetDlgItemText(hDialog, IDCANCEL, LoadResStr("IDS_BTN_CANCEL")); - if (Data.getLength()) SetDlgItemText(hDialog, IDC_EDITDATA, Data.getData()); -} - -#endif - C4ComponentHost::C4ComponentHost() { Default(); @@ -100,17 +38,11 @@ void C4ComponentHost::Default() Name[0] = 0; Filename[0] = 0; FilePath[0] = 0; -#ifdef _WIN32 - hDialog = nullptr; -#endif } void C4ComponentHost::Clear() { Data.Clear(); -#ifdef _WIN32 - if (hDialog) DestroyWindow(hDialog); hDialog = nullptr; -#endif } bool C4ComponentHost::Load(const char *szName, @@ -289,20 +221,6 @@ bool C4ComponentHost::Save(C4Group &hGroup) return hGroup.Add(Filename, Data); } -void C4ComponentHost::Open() -{ - pCmpHost = this; - -#ifdef _WIN32 - DialogBox(Application.hInstance, - MAKEINTRESOURCE(IDD_COMPONENT), - Application.pWindow->hWindow, - ComponentDlgProc); -#endif - - pCmpHost = nullptr; -} - bool C4ComponentHost::GetLanguageString(const char *szLanguage, StdStrBuf &rTarget) { const char *cptr; @@ -329,11 +247,6 @@ bool C4ComponentHost::GetLanguageString(const char *szLanguage, StdStrBuf &rTarg void C4ComponentHost::Close() { -#ifdef _WIN32 - if (!hDialog) return; - EndDialog(hDialog, 1); - hDialog = nullptr; -#endif } void C4ComponentHost::TrimSpaces() diff --git a/src/C4ComponentHost.h b/src/C4ComponentHost.h index fe0398622..5e462cbf1 100644 --- a/src/C4ComponentHost.h +++ b/src/C4ComponentHost.h @@ -30,9 +30,8 @@ class C4ComponentHost const char *GetFilePath() const { return FilePath; } void Default(); void Clear(); - void Open(); - const char *GetData() { return Data.getData(); } - size_t GetDataSize() { return Data.getLength(); } + const char *GetData() const { return Data.getData(); } + size_t GetDataSize() const { return Data.getLength(); } virtual void Close(); bool Load(const char *szName, C4Group &hGroup, const char *szFilename, const char *szLanguage = nullptr); bool Load(const char *szName, C4GroupSet &hGroupSet, const char *szFilename, const char *szLanguage = nullptr); @@ -49,9 +48,8 @@ class C4ComponentHost char Filename[_MAX_FNAME + 1]; char FilePath[_MAX_PATH + 1]; void CopyFilePathFromGroup(const C4Group &hGroup); + #ifdef _WIN32 - HWND hDialog; - void InitDialog(HWND hDlg); - friend INT_PTR CALLBACK ComponentDlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam); + friend INT_PTR CALLBACK ComponentDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam); #endif }; diff --git a/src/C4Console.cpp b/src/C4Console.cpp index ff8dd69c4..1cdd24f0e 100644 --- a/src/C4Console.cpp +++ b/src/C4Console.cpp @@ -280,6 +280,87 @@ INT_PTR CALLBACK ConsoleDlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lPara return FALSE; } +static INT_PTR CALLBACK ComponentDlgProc(const HWND hDlg, const UINT msg, const WPARAM wParam, const LPARAM lParam) +{ + const auto close = [hDlg](C4ComponentHost &host) + { + host.Close(); + EndDialog(hDlg, 1); + }; + + switch (msg) + { + case WM_INITDIALOG: + { + const auto &componentHost = *reinterpret_cast(lParam); + + SetWindowLongPtr(hDlg, DWLP_USER, lParam); + SetWindowText(hDlg, componentHost.Name); + SetDlgItemText(hDlg, IDOK, LoadResStr("IDS_BTN_OK")); + SetDlgItemText(hDlg, IDCANCEL, LoadResStr("IDS_BTN_CANCEL")); + + if (const char *const data{componentHost.GetData()}; data) + { + SetDlgItemText(hDlg, IDC_EDITDATA, data); + } + + RestoreWindowPosition(hDlg, "Component", Config.GetSubkeyPath("Console")); + + return TRUE; + } + + case WM_CLOSE: + close(*reinterpret_cast(GetWindowLongPtr(hDlg, DWLP_USER))); + return 0; + + case WM_DESTROY: + StoreWindowPosition(hDlg, "Component", Config.GetSubkeyPath("Console"), false); + return 0; + + case WM_COMMAND: + { + auto &componentHost = *reinterpret_cast(GetWindowLongPtr(hDlg, DWLP_USER)); + switch (LOWORD(wParam)) + { + case IDCANCEL: + close(componentHost); + return TRUE; + + case IDOK: + { + StdStrBuf &data{componentHost.Data}; + const auto size = static_cast(SendDlgItemMessage(hDlg, IDC_EDITDATA, WM_GETTEXTLENGTH, 0, 0)); + if (size == 0) + { + data.Clear(); + } + else + { + StdStrBuf &data{componentHost.Data}; + data.SetLength(size); + GetDlgItemText(hDlg, IDC_EDITDATA, data.getMData(), static_cast(size + 1)); + + const std::size_t actualSize{std::strlen(data.getData())}; + if (actualSize != size) + { + data.SetLength(size); + } + } + + componentHost.Modified = true; + close(componentHost); + + return TRUE; + } + } + + return FALSE; + } + } + + return FALSE; +} + #elif defined(USE_X11) && !WITH_DEVELOPER_MODE void C4Console::HandleMessage(XEvent &e) @@ -1285,6 +1366,11 @@ bool C4Console::AddMenuItem(HMENU hMenu, DWORD dwID, const char *szString, bool return InsertMenuItem(hMenu, 0, FALSE, &minfo); } +void C4Console::OpenComponentHostDialog(C4ComponentHost &host) +{ + DialogBoxParam(Application.hInstance, MAKEINTRESOURCE(IDD_COMPONENT), hWindow, &ComponentDlgProc, reinterpret_cast(&host)); +} + bool C4Console::GetPositionData(std::string &id, std::string &subKey, bool &storeSize) const { id = "Main"; @@ -1325,20 +1411,26 @@ bool C4Console::UpdateModeCtrls(int iMode) void C4Console::EditTitle() { if (Game.Network.isEnabled()) return; - Game.Title.Open(); +#ifdef _WIN32 + OpenComponentHostDialog(Game.Title); +#endif } void C4Console::EditScript() { if (Game.Network.isEnabled()) return; - Game.Script.Open(); +#ifdef _WIN32 + OpenComponentHostDialog(Game.Script); +#endif Game.ScriptEngine.ReLink(&Game.Defs); } void C4Console::EditInfo() { if (Game.Network.isEnabled()) return; - Game.Info.Open(); +#ifdef _WIN32 + OpenComponentHostDialog(Game.Info); +#endif } void C4Console::EditObjects() diff --git a/src/C4Console.h b/src/C4Console.h index bdb337f7c..10e46dd7d 100644 --- a/src/C4Console.h +++ b/src/C4Console.h @@ -128,6 +128,7 @@ class C4Console : public C4ConsoleBase void UpdateMenuText(HMENU hMenu); bool AddMenuItem(HMENU hMenu, DWORD dwID, const char *szString, bool fEnabled = true); + void OpenComponentHostDialog(C4ComponentHost &host); virtual bool Win32DialogMessageHandling(MSG *msg) override {