-
Notifications
You must be signed in to change notification settings - Fork 2
/
FileListWindow.cpp
400 lines (340 loc) · 10.4 KB
/
FileListWindow.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
// FileListWindow.cpp : implementation file
//
#include "stdafx.h"
#include "thhyl.h"
#include "FileListWindow.h"
#include "filepath_utils.h"
#include "cfgfile.h"
#include "indep/globalxp.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFileListWindow dialog
static LPCTSTR s_title = _T("文件列表");
CFileListWindow::CFileListWindow(CWnd* pParent /*=NULL*/)
: CDialog(CFileListWindow::IDD, pParent)
{
//{{AFX_DATA_INIT(CFileListWindow)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_pWndMain = dynamic_cast<CThhylDlg*>(pParent);
ASSERT(m_pWndMain != NULL);
m_hAccel = ::LoadAccelerators(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_ACCEL_FILELIST));
}
void CFileListWindow::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFileListWindow)
DDX_Control(pDX, IDC_FILELISTTREE, m_filetree);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFileListWindow, CDialog)
//{{AFX_MSG_MAP(CFileListWindow)
ON_WM_SIZE()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFileListWindow message handlers
void CFileListWindow::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if (m_filetree.m_hWnd != NULL) {
m_filetree.SetWindowPos(NULL, 0, 0, cx, cy, 0);
}
}
BOOL CFileListWindow::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
if (cfg.WinPlace_FileList.length == sizeof(WINDOWPLACEMENT)) {
SetWindowPlacement(&cfg.WinPlace_FileList);
}
if (cfg.byteAlphaForFileList == 0) cfg.byteAlphaForFileList = 255; // 第一次升级到 1.85 时 alpha 应该为 0,必须修正为 255
SetWindowAlpha(this->m_hWnd, cfg.byteAlphaForFileList);
m_filetree.SetBkColor(0x223344);
m_filetree.SetTextColor(0x00FFFF);
this->Clear();
m_pWndMain->m_pWindowGluer->addFollower(m_hWnd);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CFileListWindow::ChangeDir(const CString& strNewDir, BOOL bMustRefresh)
{
if (bMustRefresh || m_strDir != strNewDir) {
m_strDir = strNewDir;
Refresh();
}
}
CString CFileListWindow::GetFirstFilePath() const
{
HTREEITEM const hRoot = m_filetree.GetRootItem();
HTREEITEM const hChild = m_filetree.GetChildItem(hRoot);
if (hChild != NULL) {
return m_strDir / m_filetree.GetItemText(hChild);
}
else
return _T("");
}
CString CFileListWindow::GetPreviousFilePath() const
{
HTREEITEM const hSelected = m_filetree.GetSelectedItem();
if (hSelected != NULL && hSelected != m_filetree.GetRootItem()) {
HTREEITEM const hPrevious = m_filetree.GetPrevSiblingItem(hSelected);
if (hPrevious != NULL) {
return m_strDir / m_filetree.GetItemText(hPrevious);
}
}
return _T("");
}
CString CFileListWindow::GetNextFilePath() const
{
HTREEITEM const hSelected = m_filetree.GetSelectedItem();
if (hSelected != NULL && hSelected != m_filetree.GetRootItem()) {
HTREEITEM const hNext = m_filetree.GetNextSiblingItem(hSelected);
if (hNext != NULL) {
return m_strDir / m_filetree.GetItemText(hNext);
}
}
return _T("");
}
CString CFileListWindow::GetLastFilePath() const
{
HTREEITEM const hLastFileItem = this->TV_GetLastFileItem();
if (hLastFileItem != NULL) {
return m_strDir / m_filetree.GetItemText(hLastFileItem);
}
else
return _T("");
}
HTREEITEM CFileListWindow::FindFileItemByText(const CString& text) const
{
HTREEITEM const hRoot = m_filetree.GetRootItem();
HTREEITEM hWantedItem = m_filetree.GetChildItem(hRoot);
while (hWantedItem != NULL) {
if ( m_filetree.GetItemText(hWantedItem) == text ) {
break;
}
hWantedItem = m_filetree.GetNextSiblingItem(hWantedItem);
}
return hWantedItem;
}
bool CFileListWindow::SelectFileItemByText(const CString& text)
{
HTREEITEM const result = FindFileItemByText(text);
if ( result ) {
// Select file item if we found
m_filetree.SelectItem(result);
return true;
}
else
return false;
}
CString CFileListWindow::GetFileItemText() const
{
HTREEITEM const hSelected = m_filetree.GetSelectedItem();
if (hSelected != NULL && hSelected != m_filetree.GetRootItem()) {
return m_filetree.GetItemText(hSelected);
}
else
return _T("");
}
HTREEITEM CFileListWindow::TV_GetLastFileItem() const
{
HTREEITEM const hRoot = m_filetree.GetRootItem();
HTREEITEM hChild = m_filetree.GetChildItem(hRoot);
HTREEITEM hMaybeLast = hChild;
while (hChild != NULL) {
hMaybeLast = hChild;
hChild = m_filetree.GetNextSiblingItem(hChild);
}
return hMaybeLast;
}
void CFileListWindow::ChangeFilePath(const CString& strFilePath, BOOL bMustRefresh)
{
CString dir_part;
filepath_utils::GetDir(strFilePath, dir_part, false);
this->ChangeDir(dir_part, bMustRefresh);
CString filename_part;
filepath_utils::GetFilename(strFilePath, filename_part);
this->SelectFileItemByText(filename_part);
}
void CFileListWindow::Refresh()
{
CString filename = GetFileItemText();
this->Clear();
if (m_strDir.IsEmpty()) {
return;
}
m_filetree.SetItemText(m_filetree.GetRootItem(), m_strDir);
// Search
HTREEITEM hRoot = m_filetree.GetRootItem();
CFileFind ff;
for (BOOL bWorking=ff.FindFile( m_strDir + _T("\\*.rpy") ); bWorking; ) {
bWorking = ff.FindNextFile();
if (!ff.IsDirectory()) {
m_filetree.InsertItem(ff.GetFileName(), hRoot);
}
}
m_filetree.Expand(hRoot, TVE_EXPAND);
if (!filename.IsEmpty()) {
SelectFileItemByText(filename);
}
}
CString CFileListWindow::GetFilePath() const
{
CString filename = this->GetFileItemText();
if (filename.IsEmpty()) {
return filename;
}
else {
return m_strDir / filename;
}
}
CString CFileListWindow::PrepareForDeleteFile()
{
// 先获取下面的
CString newfilepath = GetNextFilePath();
CString newitemtext;
if ( newfilepath.IsEmpty() ) {
newfilepath = GetPreviousFilePath(); // 如果下面没有,获取上面的
}
if ( !newfilepath.IsEmpty() ) {
filepath_utils::GetFilename(newfilepath, newitemtext);
}
Refresh(); // 刷新一下
// 如果刷新后,前一个或后一个文件也没有了(可能从外部删除了)
// 或者刷新前删除的就是最后一个文件
// 那么返回第一个文件
if ( newitemtext.IsEmpty() || FindFileItemByText(newitemtext) == NULL )
return GetFirstFilePath();
else
return newfilepath;
}
void CFileListWindow::Clear()
{
m_filetree.DeleteAllItems();
m_filetree.InsertItem(_T("不知道该显示啥…先打开个文件吧… >_<"));
}
BOOL CFileListWindow::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
// TODO: Add your specialized code here and/or call the base class
LPNMHDR const pnmh = (LPNMHDR)lParam;
if (pnmh->hwndFrom == m_filetree.m_hWnd) {
switch (pnmh->code) {
case TVN_SELCHANGED:
{
LPNMTREEVIEW const pnmtv = (LPNMTREEVIEW)lParam;
switch(pnmtv->action) {
case TVC_BYKEYBOARD:
case TVC_BYMOUSE:
{
CString selected_file = this->GetFilePath();
if (!selected_file.IsEmpty()) {
m_pWndMain->m_rpyfile = selected_file;
m_pWndMain->Analyze();
}
}
return TRUE;
}
break;
}
case TVN_SELCHANGING:
// if (m_pWndMain->GetDialogCount() > 1) {
if (!m_pWndMain->IsWindowEnabled()) {
CString const strMessage = _T("现在不可以打开其他文件,因为有其他对话框正在使用已打开的文件。\r\n\r\n请关闭这些对话框后重试。");
MessageBox(strMessage, s_title, MB_ICONEXCLAMATION);
/* // TODO: 解决在当前窗口置顶时,难以看到新窗口
LPNMTREEVIEW const pnmtv = (LPNMTREEVIEW)lParam;
CString strFilename = m_strDir / m_filetree.GetItemText(pnmtv->itemNew.hItem);
CString strMessage = _T("不可更改打开的文件。因为有其他对话框正在使用已打开的文件。\r\n\r\n是否要在新窗口中打开刚刚选择的这个文件?\r\n\r\n");
strMessage += strFilename;
const int mbret = MessageBox(strMessage, s_title, MB_ICONEXCLAMATION | MB_YESNO);
if (mbret == IDYES) {
if (!m_strDir.IsEmpty())
m_pWndMain->SpawnInstance( strFilename );
else
MessageBox(_T("ERR100"), s_title, MB_ICONSTOP); // this should not happen
}
*/
*pResult = static_cast<LRESULT>(TRUE);
return TRUE;
}
break;
case NM_DBLCLK:
OnOK();
return TRUE;
}
}
return CDialog::OnNotify(wParam, lParam, pResult);
}
void CFileListWindow::OnOK()
{
CString selected_file = this->GetFilePath();
if (!selected_file.IsEmpty()) {
m_pWndMain->SpawnInstance(selected_file);
}
}
BOOL CFileListWindow::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
ZeroMemory(&cfg.WinPlace_FileList, sizeof(WINDOWPLACEMENT));
cfg.WinPlace_FileList.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(&cfg.WinPlace_FileList);
cfg.WinPlace_FileList.showCmd = IsWindowVisible() ? SW_SHOW : SW_HIDE;
cfg.byteAlphaForFileList = GetWindowAlpha(this->m_hWnd);
return CDialog::DestroyWindow();
}
#define IDTIMER_RESETTITLE (42)
BOOL CFileListWindow::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if (m_hAccel) {
if (::TranslateAccelerator(m_hWnd, m_hAccel, pMsg))
return TRUE;
if (m_pWndMain->m_pWindowGluer->handleSwitchMessage(this->GetSafeHwnd(), pMsg->message, pMsg->wParam, pMsg->lParam)) return TRUE;
// other keys
switch (pMsg->message)
{
case WM_MOUSEWHEEL:
{
const WORD vKey = LOWORD(pMsg->wParam);
if ( (vKey & MK_LBUTTON) ) {
// 鼠标左键+鼠标滚轮: 设置不透明度
const HWND hWnd = this->GetSafeHwnd();
const SHORT threshold = HIWORD(pMsg->wParam);
IncreaseWindowAlpha(hWnd, threshold/WHEEL_DELTA*5);
CString strMsg;
const int alpha = (int)GetWindowAlpha(hWnd);
strMsg.Format(_T("%s - 当前的不透明度 = %d/255(%.2f%%)"), s_title, alpha, (double)(alpha)*100/255 );
SetWindowText(strMsg);
SetTimer(IDTIMER_RESETTITLE, 1000, NULL);
return TRUE;
}
}
}
}
return CDialog::PreTranslateMessage(pMsg);
}
void CFileListWindow::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
switch(nIDEvent)
{
case IDTIMER_RESETTITLE:
SetWindowText(s_title);
KillTimer(IDTIMER_RESETTITLE);
break;
}
CDialog::OnTimer(nIDEvent);
}
LRESULT CFileListWindow::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
if (m_pWndMain->m_pWindowGluer->handleFollowerMessage(this->GetSafeHwnd(), message, wParam, lParam)) return 0;
return CDialog::WindowProc(message, wParam, lParam);
}