-
Notifications
You must be signed in to change notification settings - Fork 1
/
kncedirdlg.cpp
219 lines (167 loc) · 5.9 KB
/
kncedirdlg.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
#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_CURRENT_DIRECTORY_LABEL = 102,
IDC_CHOOSE_FILE_DIRECTORY_LIST = 103,
};
static BOOL WINAPI dlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
static void onInitDialog(HWND hDlg, void *createStruct);
static void onOk(HWND hDlg);
static void onCancel(HWND hDlg);
static void onDirectoryList(HWND hDlg, int event);
static void updateDirectoryList(HWND hDlg);
extern HINSTANCE g_hInstance;
struct KnceDirDlgData {
HFONT hFont;
KnceChooseDirectoryParams *chooseDirectoryParams;
tstring currentPath;
};
bool showChooseDirectoryDialog(HWND hOwnerWindow,
KnceChooseDirectoryParams *params) {
int ret = DialogBoxParam(g_hInstance, _T("CHOOSE_DIRECTORY"), 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:
onInitDialog(hDlg, (void *)lParam);
return true;
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_DIRECTORY_LIST:
onDirectoryList(hDlg, event);
break;
default:
return false;
}
return true;
}
}
return false;
}
static void onInitDialog(HWND hDlg, void *params) {
KnceDirDlgData *data = new KnceDirDlgData();
SetWindowLong(hDlg, GWL_USERDATA, (long)data);
data->hFont = knceCreateDefaultFont(true, 100, false, false);
knceSetDialogFont(hDlg, data->hFont);
data->chooseDirectoryParams = (KnceChooseDirectoryParams *)params;
data->currentPath = data->chooseDirectoryParams->initialPath;
if (data->currentPath.empty())
data->currentPath = _T("\\");
updateDirectoryList(hDlg);
ShowWindow(hDlg, SW_SHOW);
}
static void onOk(HWND hDlg) {
KnceDirDlgData *data = (KnceDirDlgData *)GetWindowLong(hDlg, GWL_USERDATA);
HWND hDirList = GetDlgItem(hDlg, IDC_CHOOSE_FILE_DIRECTORY_LIST);
if (GetFocus() == hDirList) {
onDirectoryList(hDlg, LBN_DBLCLK);
return;
}
_tcsncpy(data->chooseDirectoryParams->directory, data->currentPath.c_str(),
MAX_PATH);
DeleteObject(data->hFont);
EndDialog(hDlg, IDOK);
delete data;
}
static void onCancel(HWND hDlg) {
KnceDirDlgData *data = (KnceDirDlgData *)GetWindowLong(hDlg, GWL_USERDATA);
DeleteObject(data->hFont);
EndDialog(hDlg, IDCANCEL);
delete data;
}
static void onDirectoryList(HWND hDlg, int event) {
const int dirDispLen = MAX_PATH + 2;
KnceDirDlgData *data = (KnceDirDlgData *)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;
}
updateDirectoryList(hDlg);
}
static void updateDirectoryList(HWND hDlg) {
KnceDirDlgData *data = (KnceDirDlgData *)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 hDirList = GetDlgItem(hDlg, IDC_CHOOSE_FILE_DIRECTORY_LIST);
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());
}
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());
}
}
}
FindClose(hFind);
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);
}
}