-
Notifications
You must be signed in to change notification settings - Fork 1
/
kncefiledlg.cpp
458 lines (359 loc) · 13.5 KB
/
kncefiledlg.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include "kncecomm.h"
#include <string>
#include <windows.h>
#ifdef UNICODE
namespace std { typedef wstring tstring; }
#else
namespace std { typedef string tstring; }
#endif
using namespace std;
enum {
IDC_CHOOSE_FILE_FILE_LIST = 101,
IDC_CHOOSE_FILE_CURRENT_DIRECTORY_LABEL = 102,
IDC_CHOOSE_FILE_DIRECTORY_LIST = 103,
IDC_CHOOSE_FILE_FILE_NAME_BOX = 104,
IDC_CHOOSE_FILE_TYPE_COMBO = 105
};
static BOOL WINAPI dlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
static bool onInitDialog(HWND hDlg, void *createStruct);
static void onOk(HWND hDlg);
static void onCancel(HWND hDlg);
static void onFileList(HWND hDlg, int event);
static void onDirectoryList(HWND hDlg, int event);
static void onTypeCombo(HWND hDlg, int event);
static void onFileNameBox(HWND hDlg, int event);
static void updateFileList(HWND hDlg);
static void split(vector<tstring> &result, const tstring &str,
const tstring &delim);
static tstring appendFileExtension(const tstring &fileName,
const tstring &pat);
extern HINSTANCE g_hInstance;
struct KnceFileDlgData {
HFONT hFont;
KnceChooseFileParams *chooseFileParams;
vector<pair<tstring, tstring> > filters;
tstring currentPath;
bool eventProcessing;
};
bool showChooseFileDialog(HWND hOwnerWindow, KnceChooseFileParams *params) {
int ret = DialogBoxParam(g_hInstance, _T("CHOOSE_FILE"), hOwnerWindow,
(DLGPROC)dlgProc, (LPARAM)params);
return ret == IDOK;
}
static BOOL WINAPI dlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_INITDIALOG:
return onInitDialog(hDlg, (void *)lParam);
case WM_COMMAND:
{
int id = LOWORD(wParam);
int event = HIWORD(wParam);
switch (id) {
case IDOK:
onOk(hDlg);
break;
case IDCANCEL:
onCancel(hDlg);
break;
case IDC_CHOOSE_FILE_FILE_LIST:
onFileList(hDlg, event);
break;
case IDC_CHOOSE_FILE_DIRECTORY_LIST:
onDirectoryList(hDlg, event);
break;
case IDC_CHOOSE_FILE_FILE_NAME_BOX:
onFileNameBox(hDlg, event);
break;
case IDC_CHOOSE_FILE_TYPE_COMBO:
onTypeCombo(hDlg, event);
break;
default:
return false;
}
return true;
}
}
return false;
}
static bool onInitDialog(HWND hDlg, void *params) {
int i;
KnceFileDlgData *data = new KnceFileDlgData();
SetWindowLong(hDlg, GWL_USERDATA, (long)data);
data->hFont = knceCreateDefaultFont(true, 100, false, false);
knceSetDialogFont(hDlg, data->hFont);
data->chooseFileParams = (KnceChooseFileParams *)params;
data->eventProcessing = false;
vector<tstring> filterParts;
split(filterParts, data->chooseFileParams->filters, _T("|"));
int numFilters = filterParts.size() / 2;
for (i = 0; i < numFilters; i++) {
data->filters.push_back(make_pair(filterParts[i * 2],
filterParts[i * 2 + 1]));
}
HWND hFileList = GetDlgItem(hDlg, IDC_CHOOSE_FILE_FILE_LIST);
HWND hFileNameBox = GetDlgItem(hDlg, IDC_CHOOSE_FILE_FILE_NAME_BOX);
if (data->chooseFileParams->isSaveFile) {
SetWindowText(hDlg, _T("Save File"));
unsigned long style = GetWindowLong(hFileList, GWL_STYLE);
SetWindowLong(hFileList, GWL_STYLE, style | LBS_NOSEL);
SetWindowPos(hFileList, NULL, 0, 0, 0, 0, (SWP_NOMOVE | SWP_NOSIZE |
SWP_NOZORDER | SWP_FRAMECHANGED));
}
else {
ShowWindow(hFileNameBox, SW_HIDE);
RECT rect;
GetWindowRect(hFileList, &rect);
SetWindowPos(hFileList, NULL, 0, 0, rect.right - rect.left, 155,
SWP_NOMOVE | SWP_NOZORDER);
}
HWND hTypeCombo = GetDlgItem(hDlg, IDC_CHOOSE_FILE_TYPE_COMBO);
for (i = 0; i < numFilters; i++) {
SendMessage(hTypeCombo, CB_ADDSTRING, 0,
(LPARAM)data->filters[i].first.c_str());
}
if (numFilters > 0)
SendMessage(hTypeCombo, CB_SETCURSEL, 0, 0);
tstring fileName;
tstring filePath = data->chooseFileParams->fileName;
if (filePath.empty()) {
data->currentPath = data->chooseFileParams->initialPath;
if (data->currentPath.empty())
data->currentPath = _T("\\");
}
else {
int pos = filePath.rfind(_T('\\'));
if (pos == 0)
data->currentPath = _T("\\");
else
data->currentPath = filePath.substr(0, pos);
fileName = filePath.substr(pos + 1);
}
updateFileList(hDlg);
ShowWindow(hDlg, SW_SHOW);
if (data->chooseFileParams->isSaveFile) {
if (fileName.empty())
SetWindowText(hFileNameBox, _T("untitled"));
else
SetWindowText(hFileNameBox, fileName.c_str());
SendMessage(hFileNameBox, EM_SETSEL, 0, -1);
SetFocus(hFileNameBox);
return false;
}
else
return true;
}
static void onOk(HWND hDlg) {
KnceFileDlgData *data =
(KnceFileDlgData *)GetWindowLong(hDlg, GWL_USERDATA);
HWND hDirList = GetDlgItem(hDlg, IDC_CHOOSE_FILE_DIRECTORY_LIST);
if (GetFocus() == hDirList) {
onDirectoryList(hDlg, LBN_DBLCLK);
return;
}
tstring fileName;
if (data->chooseFileParams->isSaveFile) {
HWND hFileNameBox = GetDlgItem(hDlg, IDC_CHOOSE_FILE_FILE_NAME_BOX);
TCHAR fileNameCStr[MAX_PATH];
GetWindowText(hFileNameBox, fileNameCStr, MAX_PATH);
fileName = fileNameCStr;
HWND hTypeCombo = GetDlgItem(hDlg, IDC_CHOOSE_FILE_TYPE_COMBO);
int typeIndex = SendMessage(hTypeCombo, CB_GETCURSEL, 0, 0);
tstring pat = data->filters[typeIndex].second;
vector<tstring> exts;
split(exts, pat, _T(";"));
if (!exts.empty())
fileName = appendFileExtension(fileName, exts[0]);
}
else {
HWND hFileList = GetDlgItem(hDlg, IDC_CHOOSE_FILE_FILE_LIST);
int selected = SendMessage(hFileList, LB_GETCURSEL, 0, 0);
TCHAR fileNameCStr[MAX_PATH];
if (SendMessage(hFileList, LB_GETTEXTLEN, selected, 0) < MAX_PATH) {
SendMessage(hFileList, LB_GETTEXT, selected, (LPARAM)fileNameCStr);
fileName = fileNameCStr;
}
}
tstring filePath;
if (data->currentPath == _T("\\"))
filePath = data->currentPath + fileName;
else
filePath = data->currentPath + _T("\\") + fileName;
_tcsncpy(data->chooseFileParams->fileName, filePath.c_str(), MAX_PATH);
if (data->chooseFileParams->isSaveFile) {
FILE *file = _tfopen(filePath.c_str(), _T("r"));
if (file != NULL) {
fclose(file);
tstring msg = _T("File ") + fileName + _T(" already exists. ")
_T("Overwrite?");
if (MessageBox(hDlg, msg.c_str(), _T("Confirm"),
MB_ICONEXCLAMATION | MB_YESNO) == IDNO)
{
return;
}
}
}
DeleteObject(data->hFont);
EndDialog(hDlg, IDOK);
}
static void onCancel(HWND hDlg) {
KnceFileDlgData *data =
(KnceFileDlgData *)GetWindowLong(hDlg, GWL_USERDATA);
DeleteObject(data->hFont);
EndDialog(hDlg, IDCANCEL);
}
static void onFileList(HWND hDlg, int event) {
KnceFileDlgData *data =
(KnceFileDlgData *)GetWindowLong(hDlg, GWL_USERDATA);
if (event != LBN_SELCHANGE || data->eventProcessing)
return;
HWND hFileList = GetDlgItem(hDlg, IDC_CHOOSE_FILE_FILE_LIST);
int selected = SendMessage(hFileList, LB_GETCURSEL, 0, 0);
if (selected == -1)
return;
tstring fileName;
TCHAR fileNameCStr[MAX_PATH];
if (SendMessage(hFileList, LB_GETTEXTLEN, selected, 0) < MAX_PATH) {
SendMessage(hFileList, LB_GETTEXT, selected, (LPARAM)fileNameCStr);
fileName = fileNameCStr;
}
HWND hFileNameBox = GetDlgItem(hDlg,
IDC_CHOOSE_FILE_FILE_NAME_BOX);
data->eventProcessing = true;
SetWindowText(hFileNameBox, fileName.c_str());
data->eventProcessing = false;
if (!data->chooseFileParams->isSaveFile) {
HWND hOkButton = GetDlgItem(hDlg, IDOK);
EnableWindow(hOkButton, true);
}
}
static void onDirectoryList(HWND hDlg, int event) {
const int dirDispLen = MAX_PATH + 2;
KnceFileDlgData *data =
(KnceFileDlgData *)GetWindowLong(hDlg, GWL_USERDATA);
if (event != LBN_DBLCLK)
return;
HWND hDirList = GetDlgItem(hDlg, IDC_CHOOSE_FILE_DIRECTORY_LIST);
int selected = SendMessage(hDirList, LB_GETCURSEL, 0, 0);
if (selected == -1)
return;
tstring dirDisp;
TCHAR dirDispCStr[dirDispLen];
if (SendMessage(hDirList, LB_GETTEXTLEN, selected, 0) < dirDispLen) {
SendMessage(hDirList, LB_GETTEXT, selected, (LPARAM)dirDispCStr);
dirDisp = dirDispCStr;
}
if (dirDisp == _T("(Up...)")) {
int pos = data->currentPath.rfind(_T('\\'));
if (pos == 0)
data->currentPath = _T("\\");
else
data->currentPath = data->currentPath.substr(0, pos);
}
else {
tstring dirName = dirDisp.substr(1, dirDisp.length() - 2);
if (data->currentPath == _T("\\"))
data->currentPath += dirName;
else
data->currentPath += _T("\\") + dirName;
}
updateFileList(hDlg);
}
static void onFileNameBox(HWND hDlg, int event) {
KnceFileDlgData *data =
(KnceFileDlgData *)GetWindowLong(hDlg, GWL_USERDATA);
if (event != EN_CHANGE || data->eventProcessing)
return;
HWND hFileList = GetDlgItem(hDlg, IDC_CHOOSE_FILE_FILE_LIST);
data->eventProcessing = true;
SendMessage(hFileList, LB_SETCURSEL, -1, 0);
data->eventProcessing = false;
}
static void onTypeCombo(HWND hDlg, int event) {
if (event == CBN_SELCHANGE)
updateFileList(hDlg);
}
static void updateFileList(HWND hDlg) {
KnceFileDlgData *data =
(KnceFileDlgData *)GetWindowLong(hDlg, GWL_USERDATA);
bool isRoot = data->currentPath == _T("\\");
tstring dirName;
if (isRoot)
dirName = _T("[My Device]");
else {
dirName = _T("[") + data->currentPath.substr(
data->currentPath.rfind(_T('\\')) + 1) + _T("]");
}
HWND hDirLabel = GetDlgItem(hDlg, IDC_CHOOSE_FILE_CURRENT_DIRECTORY_LABEL);
SetWindowText(hDirLabel, dirName.c_str());
HWND hTypeCombo = GetDlgItem(hDlg, IDC_CHOOSE_FILE_TYPE_COMBO);
int typeIndex = SendMessage(hTypeCombo, CB_GETCURSEL, 0, 0);
tstring pat = data->filters[typeIndex].second;
HWND hFileList = GetDlgItem(hDlg, IDC_CHOOSE_FILE_FILE_LIST);
HWND hDirList = GetDlgItem(hDlg, IDC_CHOOSE_FILE_DIRECTORY_LIST);
SendMessage(hFileList, LB_RESETCONTENT, 0, 0);
SendMessage(hDirList, LB_RESETCONTENT, 0, 0);
if (!isRoot)
SendMessage(hDirList, LB_ADDSTRING, 0, (LPARAM)_T("(Up...)"));
tstring findPath;
if (isRoot)
findPath = _T("\\*.*");
else
findPath = data->currentPath + _T("\\*.*");
WIN32_FIND_DATA fd;
HANDLE hFind = FindFirstFile(findPath.c_str(), &fd);
if (hFind != INVALID_HANDLE_VALUE) {
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
tstring dirDisp = _T("[") + tstring(fd.cFileName) + _T("]");
SendMessage(hDirList, LB_ADDSTRING, 0, (LPARAM)dirDisp.c_str());
}
else if (knceMatchMultiFileExtension(fd.cFileName, pat.c_str()))
SendMessage(hFileList, LB_ADDSTRING, 0, (LPARAM)fd.cFileName);
while (FindNextFile(hFind, &fd)) {
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
tstring dirDisp = _T("[") + tstring(fd.cFileName) + _T("]");
SendMessage(hDirList, LB_ADDSTRING, 0,
(LPARAM)dirDisp.c_str());
}
else if (knceMatchMultiFileExtension(fd.cFileName, pat.c_str()))
SendMessage(hFileList, LB_ADDSTRING, 0, (LPARAM)fd.cFileName);
}
}
FindClose(hFind);
if (!data->chooseFileParams->isSaveFile) {
HWND hOkButton = GetDlgItem(hDlg, IDOK);
EnableWindow(hOkButton, false);
}
if (SendMessage(hFileList, LB_GETCOUNT, 0, 0) > 0) {
SendMessage(hFileList, LB_SETCURSEL, 0, 0);
onFileList(hDlg, LBN_SELCHANGE);
}
int numDirs = SendMessage(hDirList, LB_GETCOUNT, 0, 0);
if (isRoot)
SendMessage(hDirList, LB_SETCURSEL, 0, 0);
else {
if (numDirs > 1)
SendMessage(hDirList, LB_SETCURSEL, 1, 0);
else
SendMessage(hDirList, LB_SETCURSEL, 0, 0);
}
}
static void split(vector<tstring> &result, const tstring &str,
const tstring &delim) {
tstring strWork = str;
int cutAt = 0;
while ((cutAt = strWork.find_first_of(delim)) != tstring::npos) {
if (cutAt > 0)
result.push_back(strWork.substr(0, cutAt));
strWork = strWork.substr(cutAt + 1);
}
if (strWork.length() > 0)
result.push_back(strWork);
}
static tstring appendFileExtension(const tstring &fileName,
const tstring &pat) {
if (knceMatchFileExtension(fileName.c_str(), pat.c_str()))
return fileName;
if (pat.length() < 2 || pat.substr(0, 2) != _T("*."))
return fileName;
return fileName + pat.substr(1);
}