CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Windows/InputBox.cpp
Views: 1401
#include "Common/Common.h"1#include "Common/CommonTypes.h"2#include "Common/CommonWindows.h"3#include "Windows/InputBox.h"4#include "Windows/resource.h"5#include "Windows/W32Util/Misc.h"6#include "Common/Data/Encoding/Utf8.h"78struct DialogBoxParams {9std::wstring textBoxContents;10std::wstring passwordContents;11std::wstring out;12std::wstring windowTitle;13bool defaultSelected;14bool passwordMasking;15std::string userName;16std::string passWord;17};1819static DialogBoxParams g_params;2021static INT_PTR CALLBACK InputBoxFunc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)22{23switch (message) {24case WM_INITDIALOG:25{26HWND hwndTextBox = GetDlgItem(hDlg, IDC_INPUTBOX);27SetWindowText(hwndTextBox, g_params.textBoxContents.c_str());28SetWindowText(hDlg, g_params.windowTitle.c_str());29if (!g_params.defaultSelected) {30PostMessage(hwndTextBox, EM_SETSEL, -1, -1);31}32if (g_params.passwordMasking) {33LONG_PTR style = GetWindowLongPtr(hwndTextBox, GWL_STYLE);34SetWindowLongPtr(hwndTextBox, GWL_STYLE, style | ES_PASSWORD);35SendMessage(hwndTextBox, EM_SETPASSWORDCHAR, (WPARAM)'*', 0);36}37W32Util::CenterWindow(hDlg);38return TRUE;39}40case WM_COMMAND:41switch (wParam) {42case IDOK:43{44wchar_t temp[512];45GetWindowText(GetDlgItem(hDlg, IDC_INPUTBOX), temp, ARRAY_SIZE(temp));46g_params.out = temp;47EndDialog(hDlg, IDOK);48return TRUE;49}50case IDCANCEL:51EndDialog(hDlg, IDCANCEL);52return TRUE;53default:54return FALSE;55}56default:57return FALSE;58}59}6061bool InputBox_GetString(HINSTANCE hInst, HWND hParent, const wchar_t *title, const std::string &defaultValue, std::string &outvalue, InputBoxFlags flags) {62const wchar_t *defaultTitle = L"Input value";6364g_params.defaultSelected = flags & InputBoxFlags::Selected;65g_params.passwordMasking = flags & InputBoxFlags::PasswordMasking;66if (defaultValue.size() < 255) {67g_params.textBoxContents = ConvertUTF8ToWString(defaultValue);68} else {69g_params.textBoxContents.clear();70}7172if (title && wcslen(title) <= 0) {73g_params.windowTitle = defaultTitle;74} else if (title && wcslen(title) < 255) {75g_params.windowTitle = title;76} else {77g_params.windowTitle = defaultTitle;78}7980if (IDOK == DialogBox(hInst, (LPCWSTR)IDD_INPUTBOX, hParent, InputBoxFunc)) {81outvalue = ConvertWStringToUTF8(g_params.out);82return true;83} else {84return false;85}86}8788bool InputBox_GetHex(HINSTANCE hInst, HWND hParent, const wchar_t* title, u32 defaultvalue, u32 &outvalue) {89const wchar_t *defaultTitle = L"Input value";90wchar_t temp[256];91wsprintf(temp, L"%08x", defaultvalue);92g_params.textBoxContents = temp;9394if (title && wcslen(title) <= 0)95g_params.windowTitle = defaultTitle;96else if (title && wcslen(title) < 255)97g_params.windowTitle = title;98else99g_params.windowTitle = defaultTitle;100101INT_PTR value = DialogBox(hInst, (LPCWSTR)IDD_INPUTBOX, hParent, InputBoxFunc);102103if (value == IDOK) {104if (swscanf(g_params.out.c_str(), L"0x%08x", &outvalue) == 1)105return true;106if (swscanf(g_params.out.c_str(), L"%08x", &outvalue) == 1)107return true;108return false;109} else {110outvalue = 0;111return false;112}113}114115static INT_PTR CALLBACK UserPasswordBoxFunc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)116{117switch (message) {118case WM_INITDIALOG:119SetWindowText(GetDlgItem(hDlg, IDC_INPUTBOX), L"");120SetWindowText(GetDlgItem(hDlg, IDC_PASSWORDBOX), L"");121SetWindowText(hDlg, g_params.windowTitle.c_str());122PostMessage(GetDlgItem(hDlg, IDC_INPUTBOX), EM_SETSEL, -1, -1);123PostMessage(GetDlgItem(hDlg, IDC_PASSWORDBOX), EM_SETSEL, -1, -1);124W32Util::CenterWindow(hDlg);125return TRUE;126case WM_COMMAND:127switch (wParam)128{129case IDOK:130{131wchar_t temp[256];132GetWindowText(GetDlgItem(hDlg, IDC_INPUTBOX), temp, sizeof(temp));133g_params.userName = ConvertWStringToUTF8(temp);134GetWindowText(GetDlgItem(hDlg, IDC_PASSWORDBOX), temp, sizeof(temp));135g_params.passWord = ConvertWStringToUTF8(temp);136EndDialog(hDlg, IDOK);137return TRUE;138}139case IDCANCEL:140EndDialog(hDlg, IDCANCEL);141return TRUE;142default:143return FALSE;144}145default:146return FALSE;147}148}149150bool UserPasswordBox_GetStrings(HINSTANCE hInst, HWND hParent, const wchar_t *title, std::string *username, std::string *password) {151g_params.windowTitle = title;152INT_PTR value = DialogBox(hInst, (LPCWSTR)IDD_USERPASSWORDBOX, hParent, UserPasswordBoxFunc);153154if (value == IDOK) {155*username = g_params.userName;156*password = g_params.passWord;157return true;158} else {159return false;160}161}162163164