Windows inputbox: Add flags field, delete dupe code, add ability to mask passwords

This commit is contained in:
Henrik Rydgård committed 2024-09-26 10:48:31 +02:00
1 parent 9699be2110
commit f020d1d815
16 files changed
+109 -121

No files matched your search

+62 -90
View File
@@ -6,31 +6,45 @@
#include "Windows/W32Util/Misc.h"
#include "Common/Data/Encoding/Utf8.h"
static std::wstring textBoxContents;
static std::wstring passwordContents;
static std::wstring out;
static std::wstring windowTitle;
static bool defaultSelected;
static std::string g_userName;
static std::string g_passWord;
struct DialogBoxParams {
std::wstring textBoxContents;
std::wstring passwordContents;
std::wstring out;
std::wstring windowTitle;
bool defaultSelected;
bool passwordMasking;
std::string userName;
std::string passWord;
};
static DialogBoxParams g_params;
static INT_PTR CALLBACK InputBoxFunc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_INITDIALOG:
SetWindowText(GetDlgItem(hDlg, IDC_INPUTBOX), textBoxContents.c_str());
SetWindowText(hDlg, windowTitle.c_str());
if (defaultSelected == false)
PostMessage(GetDlgItem(hDlg,IDC_INPUTBOX),EM_SETSEL,-1,-1);
{
HWND hwndTextBox = GetDlgItem(hDlg, IDC_INPUTBOX);
SetWindowText(hwndTextBox, g_params.textBoxContents.c_str());
SetWindowText(hDlg, g_params.windowTitle.c_str());
if (!g_params.defaultSelected) {
PostMessage(hwndTextBox, EM_SETSEL, -1, -1);
}
if (g_params.passwordMasking) {
LONG_PTR style = GetWindowLongPtr(hwndTextBox, GWL_STYLE);
SetWindowLongPtr(hwndTextBox, GWL_STYLE, style | ES_PASSWORD);
SendMessage(hwndTextBox, EM_SETPASSWORDCHAR, (WPARAM)'*', 0);
}
W32Util::CenterWindow(hDlg);
return TRUE;
}
case WM_COMMAND:
switch (wParam) {
case IDOK:
{
wchar_t temp[512];
GetWindowText(GetDlgItem(hDlg, IDC_INPUTBOX), temp, ARRAY_SIZE(temp) - 1);
out = temp;
GetWindowText(GetDlgItem(hDlg, IDC_INPUTBOX), temp, ARRAY_SIZE(temp));
g_params.out = temp;
EndDialog(hDlg, IDOK);
return TRUE;
}
@@ -45,97 +59,55 @@ static INT_PTR CALLBACK InputBoxFunc(HWND hDlg, UINT message, WPARAM wParam, LPA
}
}
bool InputBox_GetString(HINSTANCE hInst, HWND hParent, const wchar_t *title, const std::string &defaultValue, std::string &outvalue, bool selected)
{
defaultSelected = selected;
if (defaultValue.size() < 255)
textBoxContents = ConvertUTF8ToWString(defaultValue);
else
textBoxContents.clear();
if (title != NULL)
windowTitle = title;
else
windowTitle.clear();
if (IDOK == DialogBox(hInst, (LPCWSTR)IDD_INPUTBOX, hParent, InputBoxFunc)) {
outvalue = ConvertWStringToUTF8(out);
return true;
}
else
return false;
}
bool InputBox_GetString(HINSTANCE hInst, HWND hParent, const wchar_t *title, const std::string &defaultValue, std::string &outvalue)
{
bool InputBox_GetString(HINSTANCE hInst, HWND hParent, const wchar_t *title, const std::string &defaultValue, std::string &outvalue, InputBoxFlags flags) {
const wchar_t *defaultTitle = L"Input value";
defaultSelected = true;
textBoxContents = ConvertUTF8ToWString(defaultValue);
g_params.defaultSelected = flags & InputBoxFlags::Selected;
g_params.passwordMasking = flags & InputBoxFlags::PasswordMasking;
if (defaultValue.size() < 255) {
g_params.textBoxContents = ConvertUTF8ToWString(defaultValue);
} else {
g_params.textBoxContents.clear();
}
if (title && wcslen(title) <= 0)
windowTitle = defaultTitle;
else if (title && wcslen(title) < 255)
windowTitle = title;
else
windowTitle = defaultTitle;
if (title && wcslen(title) <= 0) {
g_params.windowTitle = defaultTitle;
} else if (title && wcslen(title) < 255) {
g_params.windowTitle = title;
} else {
g_params.windowTitle = defaultTitle;
}
if (IDOK == DialogBox(hInst, (LPCWSTR)IDD_INPUTBOX, hParent, InputBoxFunc)) {
outvalue = ConvertWStringToUTF8(out);
outvalue = ConvertWStringToUTF8(g_params.out);
return true;
}
else
} else {
return false;
}
}
bool InputBox_GetWString(HINSTANCE hInst, HWND hParent, const wchar_t *title, const std::wstring &defaultValue, std::wstring &outvalue)
{
const wchar_t *defaultTitle = L"Input value";
defaultSelected = true;
textBoxContents = defaultValue;
if (title && wcslen(title) <= 0)
windowTitle = defaultTitle;
else if (title && wcslen(title) < 255)
windowTitle = title;
else
windowTitle = defaultTitle;
if (IDOK == DialogBox(hInst, (LPCWSTR)IDD_INPUTBOX, hParent, InputBoxFunc)) {
outvalue = out;
return true;
}
else
return false;
}
bool InputBox_GetHex(HINSTANCE hInst, HWND hParent, const wchar_t* title, u32 defaultvalue, u32& outvalue)
{
bool InputBox_GetHex(HINSTANCE hInst, HWND hParent, const wchar_t* title, u32 defaultvalue, u32 &outvalue) {
const wchar_t *defaultTitle = L"Input value";
wchar_t temp[256];
wsprintf(temp, L"%08x", defaultvalue);
textBoxContents = temp;
g_params.textBoxContents = temp;
if (title && wcslen(title) <= 0)
windowTitle = defaultTitle;
g_params.windowTitle = defaultTitle;
else if (title && wcslen(title) < 255)
windowTitle = title;
g_params.windowTitle = title;
else
windowTitle = defaultTitle;
g_params.windowTitle = defaultTitle;
INT_PTR value = DialogBox(hInst, (LPCWSTR)IDD_INPUTBOX, hParent, InputBoxFunc);
if (value == IDOK)
{
if (swscanf(out.c_str(), L"0x%08x", &outvalue) == 1)
if (value == IDOK) {
if (swscanf(g_params.out.c_str(), L"0x%08x", &outvalue) == 1)
return true;
if (swscanf(out.c_str(), L"%08x", &outvalue) == 1)
if (swscanf(g_params.out.c_str(), L"%08x", &outvalue) == 1)
return true;
return false;
}
else
{
} else {
outvalue = 0;
return false;
}
@@ -147,7 +119,7 @@ static INT_PTR CALLBACK UserPasswordBoxFunc(HWND hDlg, UINT message, WPARAM wPar
case WM_INITDIALOG:
SetWindowText(GetDlgItem(hDlg, IDC_INPUTBOX), L"");
SetWindowText(GetDlgItem(hDlg, IDC_PASSWORDBOX), L"");
SetWindowText(hDlg, windowTitle.c_str());
SetWindowText(hDlg, g_params.windowTitle.c_str());
PostMessage(GetDlgItem(hDlg, IDC_INPUTBOX), EM_SETSEL, -1, -1);
PostMessage(GetDlgItem(hDlg, IDC_PASSWORDBOX), EM_SETSEL, -1, -1);
W32Util::CenterWindow(hDlg);
@@ -158,10 +130,10 @@ static INT_PTR CALLBACK UserPasswordBoxFunc(HWND hDlg, UINT message, WPARAM wPar
case IDOK:
{
wchar_t temp[256];
GetWindowText(GetDlgItem(hDlg, IDC_INPUTBOX), temp, 255);
g_userName = ConvertWStringToUTF8(temp);
GetWindowText(GetDlgItem(hDlg, IDC_PASSWORDBOX), temp, 255);
g_passWord = ConvertWStringToUTF8(temp);
GetWindowText(GetDlgItem(hDlg, IDC_INPUTBOX), temp, sizeof(temp));
g_params.userName = ConvertWStringToUTF8(temp);
GetWindowText(GetDlgItem(hDlg, IDC_PASSWORDBOX), temp, sizeof(temp));
g_params.passWord = ConvertWStringToUTF8(temp);
EndDialog(hDlg, IDOK);
return TRUE;
}
@@ -177,12 +149,12 @@ static INT_PTR CALLBACK UserPasswordBoxFunc(HWND hDlg, UINT message, WPARAM wPar
}
bool UserPasswordBox_GetStrings(HINSTANCE hInst, HWND hParent, const wchar_t *title, std::string *username, std::string *password) {
windowTitle = title;
g_params.windowTitle = title;
INT_PTR value = DialogBox(hInst, (LPCWSTR)IDD_USERPASSWORDBOX, hParent, UserPasswordBoxFunc);
if (value == IDOK) {
*username = g_userName;
*password = g_passWord;
*username = g_params.userName;
*password = g_params.passWord;
return true;
} else {
return false;