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