CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Windows/InputBox.cpp
Views: 1401
1
#include "Common/Common.h"
2
#include "Common/CommonTypes.h"
3
#include "Common/CommonWindows.h"
4
#include "Windows/InputBox.h"
5
#include "Windows/resource.h"
6
#include "Windows/W32Util/Misc.h"
7
#include "Common/Data/Encoding/Utf8.h"
8
9
struct DialogBoxParams {
10
std::wstring textBoxContents;
11
std::wstring passwordContents;
12
std::wstring out;
13
std::wstring windowTitle;
14
bool defaultSelected;
15
bool passwordMasking;
16
std::string userName;
17
std::string passWord;
18
};
19
20
static DialogBoxParams g_params;
21
22
static INT_PTR CALLBACK InputBoxFunc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
23
{
24
switch (message) {
25
case WM_INITDIALOG:
26
{
27
HWND hwndTextBox = GetDlgItem(hDlg, IDC_INPUTBOX);
28
SetWindowText(hwndTextBox, g_params.textBoxContents.c_str());
29
SetWindowText(hDlg, g_params.windowTitle.c_str());
30
if (!g_params.defaultSelected) {
31
PostMessage(hwndTextBox, EM_SETSEL, -1, -1);
32
}
33
if (g_params.passwordMasking) {
34
LONG_PTR style = GetWindowLongPtr(hwndTextBox, GWL_STYLE);
35
SetWindowLongPtr(hwndTextBox, GWL_STYLE, style | ES_PASSWORD);
36
SendMessage(hwndTextBox, EM_SETPASSWORDCHAR, (WPARAM)'*', 0);
37
}
38
W32Util::CenterWindow(hDlg);
39
return TRUE;
40
}
41
case WM_COMMAND:
42
switch (wParam) {
43
case IDOK:
44
{
45
wchar_t temp[512];
46
GetWindowText(GetDlgItem(hDlg, IDC_INPUTBOX), temp, ARRAY_SIZE(temp));
47
g_params.out = temp;
48
EndDialog(hDlg, IDOK);
49
return TRUE;
50
}
51
case IDCANCEL:
52
EndDialog(hDlg, IDCANCEL);
53
return TRUE;
54
default:
55
return FALSE;
56
}
57
default:
58
return FALSE;
59
}
60
}
61
62
bool InputBox_GetString(HINSTANCE hInst, HWND hParent, const wchar_t *title, const std::string &defaultValue, std::string &outvalue, InputBoxFlags flags) {
63
const wchar_t *defaultTitle = L"Input value";
64
65
g_params.defaultSelected = flags & InputBoxFlags::Selected;
66
g_params.passwordMasking = flags & InputBoxFlags::PasswordMasking;
67
if (defaultValue.size() < 255) {
68
g_params.textBoxContents = ConvertUTF8ToWString(defaultValue);
69
} else {
70
g_params.textBoxContents.clear();
71
}
72
73
if (title && wcslen(title) <= 0) {
74
g_params.windowTitle = defaultTitle;
75
} else if (title && wcslen(title) < 255) {
76
g_params.windowTitle = title;
77
} else {
78
g_params.windowTitle = defaultTitle;
79
}
80
81
if (IDOK == DialogBox(hInst, (LPCWSTR)IDD_INPUTBOX, hParent, InputBoxFunc)) {
82
outvalue = ConvertWStringToUTF8(g_params.out);
83
return true;
84
} else {
85
return false;
86
}
87
}
88
89
bool InputBox_GetHex(HINSTANCE hInst, HWND hParent, const wchar_t* title, u32 defaultvalue, u32 &outvalue) {
90
const wchar_t *defaultTitle = L"Input value";
91
wchar_t temp[256];
92
wsprintf(temp, L"%08x", defaultvalue);
93
g_params.textBoxContents = temp;
94
95
if (title && wcslen(title) <= 0)
96
g_params.windowTitle = defaultTitle;
97
else if (title && wcslen(title) < 255)
98
g_params.windowTitle = title;
99
else
100
g_params.windowTitle = defaultTitle;
101
102
INT_PTR value = DialogBox(hInst, (LPCWSTR)IDD_INPUTBOX, hParent, InputBoxFunc);
103
104
if (value == IDOK) {
105
if (swscanf(g_params.out.c_str(), L"0x%08x", &outvalue) == 1)
106
return true;
107
if (swscanf(g_params.out.c_str(), L"%08x", &outvalue) == 1)
108
return true;
109
return false;
110
} else {
111
outvalue = 0;
112
return false;
113
}
114
}
115
116
static INT_PTR CALLBACK UserPasswordBoxFunc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
117
{
118
switch (message) {
119
case WM_INITDIALOG:
120
SetWindowText(GetDlgItem(hDlg, IDC_INPUTBOX), L"");
121
SetWindowText(GetDlgItem(hDlg, IDC_PASSWORDBOX), L"");
122
SetWindowText(hDlg, g_params.windowTitle.c_str());
123
PostMessage(GetDlgItem(hDlg, IDC_INPUTBOX), EM_SETSEL, -1, -1);
124
PostMessage(GetDlgItem(hDlg, IDC_PASSWORDBOX), EM_SETSEL, -1, -1);
125
W32Util::CenterWindow(hDlg);
126
return TRUE;
127
case WM_COMMAND:
128
switch (wParam)
129
{
130
case IDOK:
131
{
132
wchar_t temp[256];
133
GetWindowText(GetDlgItem(hDlg, IDC_INPUTBOX), temp, sizeof(temp));
134
g_params.userName = ConvertWStringToUTF8(temp);
135
GetWindowText(GetDlgItem(hDlg, IDC_PASSWORDBOX), temp, sizeof(temp));
136
g_params.passWord = ConvertWStringToUTF8(temp);
137
EndDialog(hDlg, IDOK);
138
return TRUE;
139
}
140
case IDCANCEL:
141
EndDialog(hDlg, IDCANCEL);
142
return TRUE;
143
default:
144
return FALSE;
145
}
146
default:
147
return FALSE;
148
}
149
}
150
151
bool UserPasswordBox_GetStrings(HINSTANCE hInst, HWND hParent, const wchar_t *title, std::string *username, std::string *password) {
152
g_params.windowTitle = title;
153
INT_PTR value = DialogBox(hInst, (LPCWSTR)IDD_USERPASSWORDBOX, hParent, UserPasswordBoxFunc);
154
155
if (value == IDOK) {
156
*username = g_params.userName;
157
*password = g_params.passWord;
158
return true;
159
} else {
160
return false;
161
}
162
}
163
164