-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdlgsetting.cpp
More file actions
345 lines (300 loc) · 9.37 KB
/
Copy pathdlgsetting.cpp
File metadata and controls
345 lines (300 loc) · 9.37 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// dlgsetting.cpp
#include "misc.h"
#include "mayu.h"
#include "mayurc.h"
#include "inifile.h"
#include "stringtool.h"
#include "windowstool.h"
#include "setting.h"
#include "dlgeditsetting.h"
#include "layoutmanager.h"
#include <commctrl.h>
#include <windowsx.h>
///
class DlgSetting : public LayoutManager
{
HWND m_hwndMayuPaths; ///
///
IniFile m_ini;
using Data = DlgEditSettingData; ///
///
void insertItem(int i_index, const Data &i_data) {
LVITEM item;
item.mask = LVIF_TEXT;
item.iItem = i_index;
item.iSubItem = 0;
item.pszText = const_cast<wchar_t *>(i_data.m_name.c_str());
CHECK_TRUE( ListView_InsertItem(m_hwndMayuPaths, &item) != -1 );
ListView_SetItemText(m_hwndMayuPaths, i_index, 1,
const_cast<wchar_t *>(i_data.m_filename.c_str()));
ListView_SetItemText(m_hwndMayuPaths, i_index, 2,
const_cast<wchar_t *>(i_data.m_symbols.c_str()));
}
///
void setItem(int i_index, const Data &i_data) {
ListView_SetItemText(m_hwndMayuPaths, i_index, 0,
const_cast<wchar_t *>(i_data.m_name.c_str()));
ListView_SetItemText(m_hwndMayuPaths, i_index, 1,
const_cast<wchar_t *>(i_data.m_filename.c_str()));
ListView_SetItemText(m_hwndMayuPaths, i_index, 2,
const_cast<wchar_t *>(i_data.m_symbols.c_str()));
}
///
void getItem(int i_index, Data *o_data) {
wchar_t buf[GANA_MAX_PATH];
LVITEM item;
item.mask = LVIF_TEXT;
item.iItem = i_index;
item.pszText = buf;
item.cchTextMax = NUMBER_OF(buf);
item.iSubItem = 0;
CHECK_TRUE( ListView_GetItem(m_hwndMayuPaths, &item) );
o_data->m_name = item.pszText;
item.iSubItem = 1;
CHECK_TRUE( ListView_GetItem(m_hwndMayuPaths, &item) );
o_data->m_filename = item.pszText;
item.iSubItem = 2;
CHECK_TRUE( ListView_GetItem(m_hwndMayuPaths, &item) );
o_data->m_symbols = item.pszText;
}
///
void setSelectedItem(int i_index) {
ListView_SetItemState(m_hwndMayuPaths, i_index,
LVIS_SELECTED, LVIS_SELECTED);
}
///
int getSelectedItem() {
if (ListView_GetSelectedCount(m_hwndMayuPaths) == 0)
return -1;
for (int i = 0; ; ++ i) {
if (ListView_GetItemState(m_hwndMayuPaths, i, LVIS_SELECTED))
return i;
}
}
public:
///
DlgSetting(HWND i_hwnd)
: LayoutManager(i_hwnd),
m_hwndMayuPaths(NULL) {
}
/// WM_INITDIALOG
BOOL wmInitDialog(HWND /* i_focus */, LPARAM /* i_lParam */) {
setSmallIcon(m_hwnd, IDI_ICON_mayu);
setBigIcon(m_hwnd, IDI_ICON_mayu);
CHECK_TRUE( m_hwndMayuPaths = GetDlgItem(m_hwnd, IDC_LIST_mayuPaths) );
// create list view colmn
RECT rc;
GetClientRect(m_hwndMayuPaths, &rc);
LVCOLUMN lvc;
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT;
lvc.fmt = LVCFMT_LEFT;
lvc.cx = (rc.right - rc.left) / 3;
wstringi str = loadString(IDS_mayuPathName);
lvc.pszText = const_cast<wchar_t *>(str.c_str());
CHECK( 0 ==, ListView_InsertColumn(m_hwndMayuPaths, 0, &lvc) );
str = loadString(IDS_mayuPath);
lvc.pszText = const_cast<wchar_t *>(str.c_str());
CHECK( 1 ==, ListView_InsertColumn(m_hwndMayuPaths, 1, &lvc) );
str = loadString(IDS_mayuSymbols);
lvc.pszText = const_cast<wchar_t *>(str.c_str());
CHECK( 2 ==, ListView_InsertColumn(m_hwndMayuPaths, 2, &lvc) );
Data data;
insertItem(0, data); // TODO: why ?
// set list view
wregex_stored split(L"^([^;]*);([^;]*);(.*)$");
wstringi dot_mayu;
int i;
for (i = 0; i < MAX_MAYU_INI_ENTRIES; ++ i) {
wchar_t buf[100];
_snwprintf(buf, NUMBER_OF(buf), L".mayu%d", i);
if (!m_ini.read(buf, &dot_mayu))
break;
std::wsmatch what;
if (std::regex_match(dot_mayu, what, split)) {
data.m_name = what.str(1);
data.m_filename = what.str(2);
data.m_symbols = what.str(3);
insertItem(i, data);
}
}
CHECK_TRUE( ListView_DeleteItem(m_hwndMayuPaths, i) ); // TODO: why ?
// arrange list view size
ListView_SetColumnWidth(m_hwndMayuPaths, 0, LVSCW_AUTOSIZE);
ListView_SetColumnWidth(m_hwndMayuPaths, 1, LVSCW_AUTOSIZE);
ListView_SetColumnWidth(m_hwndMayuPaths, 2, LVSCW_AUTOSIZE);
ListView_SetExtendedListViewStyle(m_hwndMayuPaths, LVS_EX_FULLROWSELECT);
// set selection
int index;
m_ini.read(L".mayuIndex", &index, 0);
setSelectedItem(index);
// set layout manager
using LM = LayoutManager;
addItem(GetDlgItem(m_hwnd, IDC_STATIC_mayuPaths),
LM::ORIGIN_LEFT_EDGE, LM::ORIGIN_TOP_EDGE,
LM::ORIGIN_RIGHT_EDGE, LM::ORIGIN_BOTTOM_EDGE);
addItem(GetDlgItem(m_hwnd, IDC_LIST_mayuPaths),
LM::ORIGIN_LEFT_EDGE, LM::ORIGIN_TOP_EDGE,
LM::ORIGIN_RIGHT_EDGE, LM::ORIGIN_BOTTOM_EDGE);
addItem(GetDlgItem(m_hwnd, IDC_BUTTON_up),
LM::ORIGIN_RIGHT_EDGE, LM::ORIGIN_CENTER,
LM::ORIGIN_RIGHT_EDGE, LM::ORIGIN_CENTER);
addItem(GetDlgItem(m_hwnd, IDC_BUTTON_down),
LM::ORIGIN_RIGHT_EDGE, LM::ORIGIN_CENTER,
LM::ORIGIN_RIGHT_EDGE, LM::ORIGIN_CENTER);
addItem(GetDlgItem(m_hwnd, IDC_BUTTON_add),
LM::ORIGIN_CENTER, LM::ORIGIN_BOTTOM_EDGE,
LM::ORIGIN_CENTER, LM::ORIGIN_BOTTOM_EDGE);
addItem(GetDlgItem(m_hwnd, IDC_BUTTON_edit),
LM::ORIGIN_CENTER, LM::ORIGIN_BOTTOM_EDGE,
LM::ORIGIN_CENTER, LM::ORIGIN_BOTTOM_EDGE);
addItem(GetDlgItem(m_hwnd, IDC_BUTTON_delete),
LM::ORIGIN_CENTER, LM::ORIGIN_BOTTOM_EDGE,
LM::ORIGIN_CENTER, LM::ORIGIN_BOTTOM_EDGE);
addItem(GetDlgItem(m_hwnd, IDCANCEL),
LM::ORIGIN_CENTER, LM::ORIGIN_BOTTOM_EDGE,
LM::ORIGIN_CENTER, LM::ORIGIN_BOTTOM_EDGE);
addItem(GetDlgItem(m_hwnd, IDOK),
LM::ORIGIN_CENTER, LM::ORIGIN_BOTTOM_EDGE,
LM::ORIGIN_CENTER, LM::ORIGIN_BOTTOM_EDGE);
restrictSmallestSize();
return TRUE;
}
/// WM_CLOSE
BOOL wmClose() {
EndDialog(m_hwnd, 0);
return TRUE;
}
/// WM_NOTIFY
BOOL wmNotify(int i_id, NMHDR *i_nmh) {
switch (i_id) {
case IDC_LIST_mayuPaths:
if (i_nmh->code == NM_DBLCLK)
FORWARD_WM_COMMAND(m_hwnd, IDC_BUTTON_edit, NULL, 0, SendMessage);
return TRUE;
}
return TRUE;
}
/// WM_COMMAND
BOOL wmCommand(int /* i_notifyCode */, int i_id, HWND /* i_hwndControl */) {
wchar_t buf[GANA_MAX_PATH];
switch (i_id) {
case IDC_BUTTON_up:
case IDC_BUTTON_down: {
int count = ListView_GetItemCount(m_hwndMayuPaths);
if (count < 2)
return TRUE;
int index = getSelectedItem();
if (index < 0 ||
(i_id == IDC_BUTTON_up && index == 0) ||
(i_id == IDC_BUTTON_down && index == count - 1))
return TRUE;
int target = (i_id == IDC_BUTTON_up) ? index - 1 : index + 1;
Data dataIndex, dataTarget;
getItem(index, &dataIndex);
getItem(target, &dataTarget);
setItem(index, dataTarget);
setItem(target, dataIndex);
setSelectedItem(target);
return TRUE;
}
case IDC_BUTTON_add: {
Data data;
int index = getSelectedItem();
if (0 <= index)
getItem(index, &data);
if (DialogBoxParam(g_hInst, MAKEINTRESOURCE(IDD_DIALOG_editSetting),
m_hwnd, dlgEditSetting_dlgProc, (LPARAM)&data))
if (!data.m_name.empty()) {
insertItem(0, data);
setSelectedItem(0);
}
return TRUE;
}
case IDC_BUTTON_delete: {
int index = getSelectedItem();
if (0 <= index) {
CHECK_TRUE( ListView_DeleteItem(m_hwndMayuPaths, index) );
int count = ListView_GetItemCount(m_hwndMayuPaths);
if (count == 0)
;
else if (count == index)
setSelectedItem(index - 1);
else
setSelectedItem(index);
}
return TRUE;
}
case IDC_BUTTON_edit: {
Data data;
int index = getSelectedItem();
if (index < 0)
return TRUE;
getItem(index, &data);
if (DialogBoxParam(g_hInst, MAKEINTRESOURCE(IDD_DIALOG_editSetting),
m_hwnd, dlgEditSetting_dlgProc, (LPARAM)&data)) {
setItem(index, data);
setSelectedItem(index);
}
return TRUE;
}
case IDOK: {
int count = ListView_GetItemCount(m_hwndMayuPaths);
int index;
for (index = 0; index < count; ++ index) {
_snwprintf(buf, NUMBER_OF(buf), L".mayu%d", index);
Data data;
getItem(index, &data);
m_ini.write(buf, data.m_name + L";" +
data.m_filename + L";" + data.m_symbols);
}
for (; ; ++ index) {
_snwprintf(buf, NUMBER_OF(buf), L".mayu%d", index);
if (!m_ini.remove(buf))
break;
}
index = getSelectedItem();
if (index < 0)
index = 0;
m_ini.write(L".mayuIndex", index);
EndDialog(m_hwnd, 1);
return TRUE;
}
case IDCANCEL: {
CHECK_TRUE( EndDialog(m_hwnd, 0) );
return TRUE;
}
}
return FALSE;
}
};
//
INT_PTR CALLBACK dlgSetting_dlgProc(
HWND i_hwnd, UINT i_message, WPARAM i_wParam, LPARAM i_lParam)
{
DlgSetting *wc;
getUserData(i_hwnd, &wc);
if (!wc)
switch (i_message) {
case WM_INITDIALOG:
wc = setUserData(i_hwnd, new DlgSetting(i_hwnd));
return wc->wmInitDialog(reinterpret_cast<HWND>(i_wParam), i_lParam);
}
else
switch (i_message) {
case WM_COMMAND:
return wc->wmCommand(HIWORD(i_wParam), LOWORD(i_wParam),
reinterpret_cast<HWND>(i_lParam));
case WM_CLOSE:
return wc->wmClose();
case WM_NCDESTROY:
delete wc;
return TRUE;
case WM_NOTIFY:
return wc->wmNotify(static_cast<int>(i_wParam),
reinterpret_cast<NMHDR *>(i_lParam));
default:
return wc->defaultWMHandler(i_message, i_wParam, i_lParam);
}
return FALSE;
}