Develop and Download Open Source Software

Browse CVS Repository

Contents of /tombo/Tombo/Src/FileSelector.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download) (as text)
Sat Sep 4 14:54:30 2004 UTC (19 years, 7 months ago) by hirami
Branch: MAIN
CVS Tags: B155, Tombo_2_0a3, Tombo_2_0a2, Tombo_2_0a1, Tombo_1_17_1, B153, B191, B192, B193, B194, B196, B197, B198, B199, B200, B201, B202, B203, B205, B206, B207, B208, SNAPSHOT_20040920, SNAPSHOT_20040925, B183, B181, B180, B187, B186, B184, B189, B188, B213, B212, B211, B217, B216, B215, B214, B219, B218, Tombo_2_0b2, Tombo_2_0b3, Tombo_2_0b1, Tombo_1_9, Tombo_2_0b4, B228, B229, B226, B227, B224, B225, B222, B223, B220, B221, B154, B172, B156, B157, B150, B151, B152, B173, B158, B159, B171, Tombo_1_10, Tombo_1_13, SNAPSHOT20051220, Tombo_1_12, B231, B230, B149, B148, Tombo_1_15, Tombo_1_14, Tombo_1_17, Tombo_1_16, Tombo_1_11, B177, B174, B175, B178, B179, B164, B169, B168, B165, SNAPSHOT_20041121, B166, B161, B160, B163, B162, HEAD
Branch point for: Tombo_1_17_1_branch
Changes since 1.5: +38 -20 lines
File MIME type: text/x-c++src
* External editor linking (HPC,Win32)

1 #include <windows.h>
2 #include <tchar.h>
3 #include <commctrl.h>
4 #include "FileSelector.h"
5 #include "Resource.h"
6
7 #include "DialogTemplate.h"
8 #include "Message.h"
9
10 #if defined(PLATFORM_WIN32)
11 ////////////////////////////////////////////////////
12 ////////////////////////////////////////////////////
13 // File selection(win32)
14 ////////////////////////////////////////////////////
15 ////////////////////////////////////////////////////
16
17 #include <shlobj.h>
18
19 DWORD FileSelector::Popup(HINSTANCE hInst, HWND hWnd, LPCTSTR pTitle, LPCTSTR pExt)
20 {
21 if (pExt != NULL) {
22 OPENFILENAME ofn;
23 TCHAR fname[MAX_PATH];
24
25 fname[0] = aPath[0] = TEXT('\0');
26
27 memset(&ofn, 0, sizeof(ofn));
28 ofn.lStructSize = sizeof(ofn);
29 ofn.hwndOwner = hWnd;
30 ofn.lpstrFilter = pExt;
31 ofn.lpstrFile = aPath;
32 ofn.nMaxFile = MAX_PATH;
33 ofn.lpstrFileTitle = fname;
34 ofn.nMaxFileTitle = MAX_PATH;
35 ofn.lpstrTitle = pTitle;
36 ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
37 if (GetOpenFileName(&ofn)) {
38 return IDOK;
39 } else {
40 return IDCANCEL;
41 }
42 } else {
43 LPITEMIDLIST pIdList;
44 TCHAR buf[MAX_PATH];
45 BROWSEINFO bi;
46 ZeroMemory(&bi, sizeof(bi));
47 bi.hwndOwner = hWnd;
48 bi.pszDisplayName = buf;
49 bi.lpszTitle = pTitle;
50 bi.ulFlags = BIF_RETURNONLYFSDIRS;
51
52 pIdList = SHBrowseForFolder(&bi);
53 if (pIdList == NULL) return IDCANCEL;
54
55 SHGetPathFromIDList(pIdList, aPath);
56 CoTaskMemFree(pIdList);
57 return IDOK;
58 }
59 }
60
61 #else // PLATFORM_WIN32
62 ////////////////////////////////////////////////////
63 ////////////////////////////////////////////////////
64 // File selection (CE)
65 ////////////////////////////////////////////////////
66 ////////////////////////////////////////////////////
67
68
69 #define IMAGE_CX 16
70 #define IMAGE_CY 16
71 #define NUM_BITMAPS 4
72
73 #define IMG_FOLDER 0
74 #define IMG_FOLDER_SEL 1
75 #define IMG_ARTICLE 2
76 #define IMG_DRIVE 3
77
78 //////////////////////////////////////////////////
79 // dtor
80 //////////////////////////////////////////////////
81 FileSelector::~FileSelector()
82 {
83 if (hImg) ImageList_Destroy(hImg);
84 }
85
86 //////////////////////////////////////////////////
87 // Dialog Procedure
88 //////////////////////////////////////////////////
89
90 static BOOL APIENTRY DlgProc(HWND hDlg, UINT nMessage, WPARAM wParam, LPARAM lParam)
91 {
92 FileSelector *pDlg;
93
94 if (nMessage == WM_INITDIALOG) {
95 // �I�u�W�F�N�g���L����������
96 SetWindowLong(hDlg, DWL_USER, lParam);
97 pDlg = (FileSelector*)lParam;
98
99 pDlg->InitDialog(hDlg);
100 return TRUE;
101 }
102
103 pDlg = (FileSelector*)GetWindowLong(hDlg, DWL_USER);
104 if (pDlg == NULL) return FALSE;
105
106 switch(nMessage) {
107 case WM_COMMAND:
108 switch (LOWORD(wParam)) {
109 case IDOK:
110 pDlg->OnOK(hDlg);
111 break;
112 case IDCANCEL:
113 EndDialog(hDlg, IDCANCEL);
114 break;
115 }
116 return TRUE;
117 case WM_NOTIFY:
118 if (wParam == IDC_FILESELECT_TREE) {
119 pDlg->OnTreeNotify(hDlg, wParam, lParam);
120 }
121 return TRUE;
122 }
123 return FALSE;
124 }
125
126 //////////////////////////////////////////////////
127 // Popup
128 //////////////////////////////////////////////////
129
130 DWORD FileSelector::Popup(HINSTANCE hInst, HWND hWnd, LPCTSTR pt, LPCTSTR pExt)
131 {
132 // Create image list
133 if (hImg == NULL) {
134 if ((hImg = ImageList_Create(IMAGE_CX, IMAGE_CY, ILC_MASK, NUM_BITMAPS, 0)) == NULL) return IDCANCEL;
135 HBITMAP hBmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_ARTICLEBOX));
136 COLORREF rgbTransparent = RGB(0, 255, 0);
137 ImageList_AddMasked(hImg, hBmp, rgbTransparent);
138 DeleteObject(hBmp);
139 }
140
141 pTitle = pt;
142 if (pExt != NULL) pExtension = pExt;
143
144 DWORD result;
145 result = DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_FILESELECT), hWnd, (DLGPROC)DlgProc, (LONG)this);
146 DWORD n = GetLastError();
147 return result;
148 }
149
150 //////////////////////////////////////////////////
151 // Initialize dialog
152 //////////////////////////////////////////////////
153
154 static DlgMsgRes aMsgRes[] = {
155 { IDOK, MSG_ID_DLG_CMN_OK},
156 { IDCANCEL, MSG_ID_DLG_CMN_CANCEL},
157 };
158
159 void FileSelector::InitDialog(HWND hWnd)
160 {
161 OverrideDlgMsg(hWnd, MSG_ID_DLG_FILESELECT_TITLE, aMsgRes, sizeof(aMsgRes)/sizeof(DlgMsgRes));
162
163 HWND hTree = GetDlgItem(hWnd, IDC_FILESELECT_TREE);
164
165 TreeView_SetImageList(hTree, hImg, TVSIL_NORMAL);
166 TreeView_DeleteAllItems(hTree);
167
168 // �h���C�u�m�[�h������
169 TV_INSERTSTRUCT ti;
170 ti.hParent = TVI_ROOT;
171 ti.hInsertAfter = TVI_LAST;
172
173 TCHAR buf[3];
174
175 ti.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
176 ti.item.iImage = ti.item.iSelectedImage = IMG_DRIVE;
177 ti.item.pszText = buf;
178 ti.item.lParam = NULL;
179 buf[1] = TEXT(':');
180 buf[2] = TEXT('\0');
181
182 TV_INSERTSTRUCT td;
183 td.hInsertAfter = TVI_LAST;
184 td.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
185 td.item.iImage = IMG_FOLDER;
186 td.item.iSelectedImage = IMG_FOLDER_SEL;
187 td.item.pszText = TEXT("DUMMY");
188
189 #ifdef _WIN32_WCE
190 _tcscpy(buf, TEXT("\\"));
191 td.hParent = TreeView_InsertItem(hTree, &ti);
192 TreeView_InsertItem(hTree, &td);
193 #else
194 UINT ut;
195 for (buf[0] = TEXT('A'); buf[0] <= TEXT('Z'); buf[0]++) {
196 ut = GetDriveType(buf);
197 if (ut == DRIVE_FIXED || ut == DRIVE_REMOTE || ut == DRIVE_RAMDISK) {
198 td.hParent = TreeView_InsertItem(hTree, &ti);
199 if (td.hParent == NULL) {
200 TCHAR buf[1024];
201 wsprintf(buf, TEXT("Can't insert tree item %d"), GetLastError());
202 MessageBox(NULL, buf, TEXT("WARNING"), MB_ICONEXCLAMATION | MB_OK);
203 }
204 TreeView_InsertItem(hTree, &td);
205 }
206 }
207 #endif
208
209 // �^�C�g��������
210 SetWindowText(hWnd, pTitle);
211
212 // OK�{�^����������disable
213 HWND hOk = GetDlgItem(hWnd, IDOK);
214 EnableWindow(hOk, FALSE);
215 }
216
217 ///////////////////////////////////////////////////////////////////
218 // �c���[�r���[���I���������A�C�e�������A�p�X�������������������B
219 //
220 // �����l���������������B(pBuf�������w��)
221 // pBuf�������������O�����������g�p�����B
222 static LPTSTR GeneratePath(HWND hTree, HTREEITEM hItem, LPTSTR pBuf, DWORD nSiz)
223 {
224 LPTSTR p = pBuf + nSiz - 2;
225 *(p+1) = TEXT('\0');
226
227 TV_ITEM it;
228 TCHAR buf[MAX_PATH];
229
230 HTREEITEM h = hItem;
231 it.mask = TVIF_HANDLE | TVIF_TEXT | TVIF_PARAM;
232 it.pszText = buf;
233
234 while(h) {
235 it.hItem = h;
236 it.cchTextMax = MAX_PATH;
237 TreeView_GetItem(hTree, &it);
238
239 *p-- = TEXT('\\');
240 p -= _tcslen(buf) - 1;
241 _tcsncpy(p, buf, _tcslen(buf));
242 p--;
243
244 h = TreeView_GetParent(hTree, h);
245 }
246 #ifdef _WIN32_WCE
247 // CE�������A�h���C�u�����������A������������'\\'���������������B������+1����'\'��1�������B
248 return p + 2;
249 #else
250 return p + 1;
251 #endif
252 }
253
254 //////////////////////////////////////////////////
255 // �c���[��������NOTIFY
256 //////////////////////////////////////////////////
257
258 // �t�H���_�m�[�h���r���[��'+'���\�������������A�_�~�[�m�[�h��1�������������B
259 // �m�[�h�I�[�v���������_�~�[�m�[�h���������A�t�H���_�����e�����������B
260 // ���������������q�����v�f���������A�_�~�[�m�[�h�����������B
261
262 void FileSelector::OnTreeNotify(HWND hWnd, WPARAM wParam, LPARAM lParam)
263 {
264 LPNM_TREEVIEW pHdr = (LPNM_TREEVIEW)lParam;
265 HWND hTree = GetDlgItem(hWnd, IDC_FILESELECT_TREE);
266
267 switch(pHdr->hdr.code) {
268 case TVN_ITEMEXPANDING:
269 if(pHdr->action == TVE_EXPAND) {
270 // �c���[���J����������
271 TreeExpand(hTree, pHdr->itemNew.hItem);
272 } else {
273 // �c���[����������������
274 TreeCollapse(hTree, pHdr->itemNew.hItem);
275 }
276 break;
277 case TVN_SELCHANGED:
278 ItemSelect(hWnd, hTree, pHdr->itemNew.hItem);
279 break;
280 }
281 }
282
283 //////////////////////////////////////////////////
284 // OK
285 //////////////////////////////////////////////////
286
287 void FileSelector::OnOK(HWND hWnd)
288 {
289 HWND hTree = GetDlgItem(hWnd, IDC_FILESELECT_TREE);
290 HTREEITEM h = TreeView_GetSelection(hTree);
291 pPath = GeneratePath(hTree, h, aPath, MAX_PATH);
292 if (pExtension) {
293 pPath[_tcslen(pPath) - 1] = TEXT('\0');
294 }
295
296 EndDialog(hWnd, IDOK);
297 }
298
299 //////////////////////////////////////////////////
300 //
301
302 static HTREEITEM GetInsertAfter(HWND hTree, HTREEITEM hItem, LPCTSTR pStr, BOOL bFolder)
303 {
304 HTREEITEM h = TreeView_GetChild(hTree, hItem);
305
306 if (h == NULL) return TVI_FIRST;
307
308 HTREEITEM hd = TVI_FIRST;
309 TV_ITEM ti;
310 HTREEITEM res = TVI_LAST;
311
312 TCHAR buf[MAX_PATH];
313
314 ti.mask = TVIF_HANDLE | TVIF_TEXT | TVIF_IMAGE;
315 while (h) {
316 ti.hItem = h;
317 ti.pszText = buf;
318 ti.cchTextMax = MAX_PATH;
319
320 TreeView_GetItem(hTree, &ti);
321 if (!(ti.iImage == IMG_FOLDER && bFolder == FALSE)) {
322 if (_tcsicmp(buf, pStr) >= 0) {
323 res = hd;
324 break;
325 }
326 }
327 hd = h;
328 h = TreeView_GetNextSibling(hTree, h);
329 }
330 return res;
331 }
332
333 //////////////////////////////////////////////////
334 // �c���[���W�J
335 //////////////////////////////////////////////////
336
337 void FileSelector::TreeExpand(HWND hTree, HTREEITEM hItem)
338 {
339 TCHAR buf[MAX_PATH], buf2[MAX_PATH];
340 LPTSTR pPath = GeneratePath(hTree, hItem, buf, MAX_PATH);
341
342 HTREEITEM di = TreeView_GetChild(hTree, hItem);
343 TreeView_DeleteItem(hTree, di);
344
345 // �f�B���N�g�������������E�c���[��������
346 WIN32_FIND_DATA wfd;
347 TV_INSERTSTRUCT ti;
348 ti.hParent = hItem;
349 ti.hInsertAfter = TVI_LAST;
350 ti.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
351 ti.item.iImage = IMG_FOLDER;
352 ti.item.iSelectedImage = IMG_FOLDER_SEL;
353 ti.item.pszText = wfd.cFileName;
354 ti.item.lParam = (LPARAM)hItem;
355
356 // �W�J�p��"+"�{�^�����\���������������_�~�[�m�[�h
357 TV_INSERTSTRUCT td;
358 td.hInsertAfter = TVI_LAST;
359 td.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
360 td.item.iImage = IMG_FOLDER;
361 td.item.iSelectedImage = IMG_FOLDER_SEL;
362 td.item.pszText = TEXT("DUMMY");
363
364 wsprintf(buf2, TEXT("%s*.*"), pPath);
365 HANDLE hFile = FindFirstFile(buf2, &wfd);
366 if (hFile != INVALID_HANDLE_VALUE) {
367 do {
368 if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) continue;
369 if (_tcscmp(wfd.cFileName, TEXT(".")) == 0 ||
370 _tcscmp(wfd.cFileName, TEXT("..")) == 0) continue;
371
372 // �}��
373 ti.hInsertAfter = GetInsertAfter(hTree, hItem, wfd.cFileName, TRUE);
374 td.hParent = TreeView_InsertItem(hTree, &ti);
375
376 // �_�~�[�m�[�h���}��
377 TreeView_InsertItem(hTree, &td);
378
379 } while(FindNextFile(hFile, &wfd));
380 FindClose(hFile);
381 }
382
383 // �t�@�C���I�������������t�@�C�����\������
384 if (pExtension) {
385 ti.item.iImage = ti.item.iSelectedImage = IMG_ARTICLE;
386 wsprintf(buf2, TEXT("%s%s"), pPath, pExtension);
387 hFile = FindFirstFile(buf2, &wfd);
388 if (hFile != INVALID_HANDLE_VALUE) {
389 do {
390 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;
391
392 ti.hInsertAfter = GetInsertAfter(hTree, hItem, wfd.cFileName, FALSE);
393 TreeView_InsertItem(hTree, &ti);
394 } while(FindNextFile(hFile, &wfd));
395 FindClose(hFile);
396 }
397 }
398 }
399
400 //////////////////////////////////////////////////
401 // �c���[��������
402 //////////////////////////////////////////////////
403
404 void FileSelector::TreeCollapse(HWND hTree, HTREEITEM hItem)
405 {
406 // �����m�[�h������
407 HTREEITEM h = TreeView_GetChild(hTree, hItem);
408 HTREEITEM hd;
409 while (h) {
410 hd = h;
411 h = TreeView_GetNextSibling(hTree, h);
412 TreeView_DeleteItem(hTree, hd);
413 }
414
415 // �_�~�[�m�[�h���}��
416 TV_INSERTSTRUCT td;
417 td.hParent = hItem;
418 td.hInsertAfter = TVI_LAST;
419 td.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
420 td.item.iImage = IMG_FOLDER;
421 td.item.iSelectedImage = IMG_FOLDER_SEL;
422 td.item.pszText = TEXT("DUMMY");
423 TreeView_InsertItem(hTree, &td);
424 }
425
426 //////////////////////////////////////////////////
427 // �m�[�h���I��
428 //////////////////////////////////////////////////
429 //�@�t�@�C���I���������A�t�H���_���I������������������OK�{�^����disable�������B
430
431 void FileSelector::ItemSelect(HWND hDlg, HWND hTree, HTREEITEM hItem)
432 {
433 HWND hOk = GetDlgItem(hDlg, IDOK);
434 BOOL bEnable;
435
436 if (pExtension) {
437 TV_ITEM ti;
438 ti.mask = TVIF_HANDLE | TVIF_IMAGE;
439 ti.hItem = hItem;
440 TreeView_GetItem(hTree, &ti);
441 bEnable = (ti.iImage == IMG_ARTICLE);
442 } else {
443 bEnable = TRUE;
444 }
445 EnableWindow(hOk, bEnable);
446 }
447
448 #endif // PLATFORM_WIN32

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26