-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.cpp
228 lines (183 loc) · 6.28 KB
/
settings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "stdafx.h"
#define EE_EXTERN_ONLY
#define ETL_FRAME_CLASS_NAME CMyFrame
#include "template/etlframe.h"
#include "CMyFrame.h"
#include "CharacterCount.h"
#include "registry.h"
#include "settings.h"
namespace settings {
const size_t stringSize = 512;
// Returns position and size for left column text.
RECT settingsLeftColumn(double cy, double i) {
double left = cy * 1;
double top = cy * 2.0 + i * cy * 1.0;
double width = cy * 15;
double height = cy * 0.7;
return { (LONG)left,(LONG)top,(LONG)width,(LONG)height };
}
// Returns position and size for right column menu.
RECT settingsRightColumn(double cy, double i) {
double left = cy * 25 / 2;
double top = cy * 2.0 + i * cy * 1.0;
double width = cy * 12 / 2;
double height = cy * 1.0;
return { (LONG)left,(LONG)top,(LONG)width,(LONG)height };
}
// Updates all combo boxes.
void updateComboBoxes(std::array<HWND, settingsSize> comboBoxes,
const std::array<unsigned char, settingsSize>& settings) {
for (int row = 0; row < settingsSize; ++row) {
int selectIndex = -1;
for (UINT i = 0; i < menus[row].size(); ++i) {
if (menus[row][i] == settings[row]) {
selectIndex = i;
break;
}
}
VERIFY(selectIndex != -1);
if (selectIndex == -1)
selectIndex = 0;
SendMessage(comboBoxes[row], CB_SETCURSEL, (WPARAM)selectIndex, NULL);
}
}
// Creates combo box and returns handle to combox box.
HWND createComboBox(HWND hwnd, int cy, int row, HFONT font, HINSTANCE localeInstanceHandle) {
RECT rCol = settingsRightColumn(cy, row);
HWND comboBox = CreateWindow(WC_COMBOBOX, NULL,
WS_VISIBLE | WS_CHILD | CBS_DROPDOWNLIST | CBS_HASSTRINGS,
rCol.left, rCol.top, rCol.right, rCol.bottom,
hwnd, NULL, NULL, NULL);
SendMessage(comboBox, WM_SETFONT, (WPARAM)font, NULL);
for (int labelIndex : menus[row]) {
WCHAR label[stringSize];
VERIFY(LoadString(localeInstanceHandle, IDS_LABEL300 + labelIndex, label, stringSize));
SendMessage(comboBox, CB_ADDSTRING, NULL, (LPARAM)label);
}
return comboBox;
}
// Initializes settings window. Returns the handles to created combo boxes.
std::array<HWND, settingsSize> initSettings(HWND hwnd,
const std::array<unsigned char, settingsSize>& settings) {
charCount::CenterWindow(hwnd);
// Add text
HINSTANCE localeInstanceHandle = EEGetLocaleInstanceHandle();
WCHAR title[stringSize];
VERIFY(LoadString(localeInstanceHandle, IDS_PLUGIN_NAME, title, stringSize));
SetWindowText(hwnd, title);
WCHAR okText[stringSize];
VERIFY(LoadString(localeInstanceHandle, IDS_OK, okText, stringSize));
SetWindowText(GetDlgItem(hwnd, IDOK), okText);
WCHAR resetText[stringSize];
HWND resetButton = GetDlgItem(hwnd, IDRESET);
VERIFY(LoadString(localeInstanceHandle, IDS_RESET, resetText, stringSize));
SetWindowText(resetButton, resetText);
HFONT font = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
RECT buttonRect;
GetClientRect(resetButton, &buttonRect);
int cy = buttonRect.bottom;
// Resize window
RECT windowRect;
GetWindowRect(hwnd, &windowRect);
SetWindowPos(hwnd, NULL, 0, 0,
static_cast<int>(cy * 20.0227273),
windowRect.bottom - windowRect.top,
SWP_NOMOVE);
// Heading
RECT clientRect;
GetClientRect(hwnd, &clientRect);
WCHAR heading[stringSize];
VERIFY(LoadString(localeInstanceHandle, IDS_HEADINGSETTINGS, heading, stringSize));
HWND text = CreateWindow(L"STATIC", heading, WS_VISIBLE | WS_CHILD | SS_CENTER, 0, cy / 2,
clientRect.right - clientRect.left, cy, hwnd, NULL, NULL, NULL);
SendMessage(text, WM_SETFONT, (WPARAM)font, NULL);
// Draw text and controls
std::array<HWND, settingsSize> comboBoxes;
for (int i = 0; i < settingsSize; ++i) {
WCHAR label[stringSize];
VERIFY(LoadString(localeInstanceHandle, IDS_LABEL200 + i, label, stringSize));
RECT lCol = settingsLeftColumn(cy, i);
text = CreateWindow(L"STATIC", label, WS_VISIBLE | WS_CHILD,
lCol.left, lCol.top, lCol.right, lCol.bottom,
hwnd, NULL, NULL, NULL);
SendMessage(text, WM_SETFONT, (WPARAM)font, NULL);
comboBoxes[i] = createComboBox(hwnd, cy, i, font, localeInstanceHandle);
}
updateComboBoxes(comboBoxes, settings);
return comboBoxes;
}
// Returns the new settings.
std::array<unsigned char, settingsSize>
storeSelSettings(HWND hwnd,
const std::array<HWND, settingsSize>& comboBoxes,
HWND editor) {
std::array<unsigned char, settingsSize> newSettings;
for (int i = 0; i < settingsSize; ++i) {
int selectedIndex = (int)SendMessage(comboBoxes[i], CB_GETCURSEL, NULL, NULL);
newSettings[i] = menus[i][selectedIndex];
}
if (!writeSettings(editor, newSettings)) {
WCHAR message[stringSize];
VERIFY(LoadString(EEGetLocaleInstanceHandle(), IDS_COULDNTSAVE, message, stringSize));
MessageBox(hwnd, message, NULL, MB_ICONWARNING);
};
return newSettings;
}
// Returns true if OK pressed on confirmation.
bool confirmReset(HWND hwnd) {
WCHAR message[stringSize];
VERIFY(LoadString(EEGetLocaleInstanceHandle(), IDS_CONFIRMRESET, message, stringSize));
return MessageBox(hwnd, message, NULL, MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2)
== IDYES;
}
// Save selected options and close window.
void saveClose(HWND window, const std::array<HWND, settingsSize>& comboBoxes,
bool reopen, HWND editor) {
storeSelSettings(window, comboBoxes, editor);
HWND parent = GetParent(window);
EndDialog(window, 0);
if (reopen) {
EndDialog(parent, 0);
GetFrame(parent)->OnCommand(parent);
}
}
// Message handler for settings window.
INT_PTR CALLBACK Window::SettingsProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_INITDIALOG: {
SetWindowLongPtr(hwnd, GWLP_USERDATA, lParam);
Window* window = (Window*)lParam;
window->comboBoxes = initSettings(hwnd, window->settings);
break;
}
case WM_CLOSE: {
EndDialog(hwnd, 0);
break;
}
case WM_COMMAND: {
switch (wParam) {
case IDOK: {
Window* window = (Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
saveClose(hwnd, window->comboBoxes, window->reopen, window->editor);
break;
}
case IDCANCEL: {
EndDialog(hwnd, 0);
break;
}
case IDRESET: {
Window* window = (Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
updateComboBoxes(window->comboBoxes, defaultSettings);
break;
}
}
}
}
return 0;
}
std::array<unsigned char, settingsSize> Window::openSettingsWindow(HWND hwnd) {
DialogBoxParam(EEGetLocaleInstanceHandle(), MAKEINTRESOURCE(IDD_DIALOG2), hwnd,
SettingsProc, (LPARAM)this);
return settings;
}
}