sync to Wine Patch: format_bytes()
[reactos.git] / reactos / subsys / system / winefile / winefile.c
1 /*
2 * Winefile
3 *
4 * Copyright 2000, 2003, 2004, 2005 Martin Fuchs
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include "winefile.h"
22
23 #include "resource.h"
24
25
26 #ifdef _NO_EXTENSIONS
27 #undef _LEFT_FILES
28 #endif
29
30 #ifndef _MAX_PATH
31 #define _MAX_DRIVE 3
32 #define _MAX_FNAME 256
33 #define _MAX_DIR _MAX_FNAME
34 #define _MAX_EXT _MAX_FNAME
35 #define _MAX_PATH 260
36 #endif
37
38 #ifdef NONAMELESSUNION
39 #define UNION_MEMBER(x) DUMMYUNIONNAME.x
40 #else
41 #define UNION_MEMBER(x) x
42 #endif
43
44
45 #ifdef _SHELL_FOLDERS
46 #define DEFAULT_SPLIT_POS 300
47 #else
48 #define DEFAULT_SPLIT_POS 200
49 #endif
50
51
52 enum ENTRY_TYPE {
53 ET_WINDOWS,
54 ET_UNIX,
55 #ifdef _SHELL_FOLDERS
56 ET_SHELL
57 #endif
58 };
59
60 typedef struct _Entry {
61 struct _Entry* next;
62 struct _Entry* down;
63 struct _Entry* up;
64
65 BOOL expanded;
66 BOOL scanned;
67 int level;
68
69 WIN32_FIND_DATA data;
70
71 #ifndef _NO_EXTENSIONS
72 BY_HANDLE_FILE_INFORMATION bhfi;
73 BOOL bhfi_valid;
74 enum ENTRY_TYPE etype;
75 #endif
76 #ifdef _SHELL_FOLDERS
77 LPITEMIDLIST pidl;
78 IShellFolder* folder;
79 HICON hicon;
80 #endif
81 } Entry;
82
83 typedef struct {
84 Entry entry;
85 TCHAR path[MAX_PATH];
86 TCHAR volname[_MAX_FNAME];
87 TCHAR fs[_MAX_DIR];
88 DWORD drive_type;
89 DWORD fs_flags;
90 } Root;
91
92 enum COLUMN_FLAGS {
93 COL_SIZE = 0x01,
94 COL_DATE = 0x02,
95 COL_TIME = 0x04,
96 COL_ATTRIBUTES = 0x08,
97 COL_DOSNAMES = 0x10,
98 #ifdef _NO_EXTENSIONS
99 COL_ALL = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_DOSNAMES
100 #else
101 COL_INDEX = 0x20,
102 COL_LINKS = 0x40,
103 COL_ALL = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_DOSNAMES|COL_INDEX|COL_LINKS
104 #endif
105 };
106
107 typedef enum {
108 SORT_NAME,
109 SORT_EXT,
110 SORT_SIZE,
111 SORT_DATE
112 } SORT_ORDER;
113
114 typedef struct {
115 HWND hwnd;
116 #ifndef _NO_EXTENSIONS
117 HWND hwndHeader;
118 #endif
119
120 #ifndef _NO_EXTENSIONS
121 #define COLUMNS 10
122 #else
123 #define COLUMNS 5
124 #endif
125 int widths[COLUMNS];
126 int positions[COLUMNS+1];
127
128 BOOL treePane;
129 int visible_cols;
130 Entry* root;
131 Entry* cur;
132 } Pane;
133
134 typedef struct {
135 HWND hwnd;
136 Pane left;
137 Pane right;
138 int focus_pane; /* 0: left 1: right */
139 WINDOWPLACEMENT pos;
140 int split_pos;
141 BOOL header_wdths_ok;
142
143 TCHAR path[MAX_PATH];
144 Root root;
145
146 SORT_ORDER sortOrder;
147 } ChildWnd;
148
149
150 extern void WineLicense(HWND hwnd);
151 extern void WineWarranty(HWND hwnd);
152
153
154 #ifdef __WINE__
155
156 /* functions in unixcalls.c */
157
158 extern void call_getcwd(char* buffer, size_t len);
159 extern void* call_opendir(const char* path);
160 extern int call_readdir(void* pdir, char* name, unsigned* pinode);
161 extern void call_closedir(void* pdir);
162
163 extern int call_stat(
164 const char* path, int* pis_dir,
165 unsigned long* psize_low, unsigned long* psize_high,
166 time_t* patime, time_t* pmtime,
167 unsigned long* plinks
168 );
169
170 /* call vswprintf() in msvcrt.dll */
171 int swprintf(wchar_t* buffer, const wchar_t* fmt, ...)
172 {
173 static int (__cdecl *vswprintf)(wchar_t*, const wchar_t*, va_list);
174
175 va_list ap;
176 int ret;
177
178 if (!vswprintf) {
179 HMODULE hmod = LoadLibraryA("msvcrt");
180 vswprintf = (int(__cdecl*)(wchar_t*,const wchar_t*,va_list)) GetProcAddress(hmod, "vswprintf");
181 }
182
183 va_start(ap, fmt);
184 ret = vswprintf(buffer, fmt, ap);
185 va_end(ap);
186
187 return 0;
188 }
189
190
191 #define TMP_ALLOC(s) HeapAlloc(GetProcessHeap(), 0, s)
192 #define TMP_FREE(p) HeapFree(GetProcessHeap(), 0, p)
193
194 #else
195
196 #define TMP_ALLOC(s) alloca(s)
197 #define TMP_FREE(p)
198
199 #endif
200
201
202 static void read_directory(Entry* dir, LPCTSTR path, SORT_ORDER sortOrder, HWND hwnd);
203 static void set_curdir(ChildWnd* child, Entry* entry, HWND hwnd);
204 static void get_path(Entry* dir, PTSTR path);
205
206 LRESULT CALLBACK FrameWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
207 LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
208 LRESULT CALLBACK TreeWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam);
209
210
211 /* globals */
212 WINEFILE_GLOBALS Globals;
213
214
215 /* some common string constants */
216 const static TCHAR sEmpty[] = {'\0'};
217 const static TCHAR sSpace[] = {' ', '\0'};
218 const static TCHAR sNumFmt[] = {'%','d','\0'};
219 const static TCHAR sQMarks[] = {'?','?','?','\0'};
220
221 /* window class names */
222 const static TCHAR sWINEFILEFRAME[] = {'W','F','S','_','F','r','a','m','e','\0'};
223 const static TCHAR sWINEFILETREE[] = {'W','F','S','_','T','r','e','e','\0'};
224
225 #ifdef _MSC_VER
226 /* #define LONGLONGARG _T("I64") */
227 const static TCHAR sLongHexFmt[] = {'%','I','6','4','X','\0'};
228 const static TCHAR sLongNumFmt[] = {'%','I','6','4','d','\0'};
229 #else
230 /* #define LONGLONGARG _T("L") */
231 const static TCHAR sLongHexFmt[] = {'%','L','X','\0'};
232 const static TCHAR sLongNumFmt[] = {'%','L','d','\0'};
233 #endif
234
235
236 /* load resource string */
237 static LPTSTR load_string(LPTSTR buffer, UINT id)
238 {
239 LoadString(Globals.hInstance, id, buffer, BUFFER_LEN);
240
241 return buffer;
242 }
243
244 #define RS(b, i) load_string(b, i)
245
246
247 /* display error message for the specified WIN32 error code */
248 static void display_error(HWND hwnd, DWORD error)
249 {
250 TCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
251 PTSTR msg;
252
253 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
254 0, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (PTSTR)&msg, 0, NULL))
255 MessageBox(hwnd, msg, RS(b2,IDS_WINEFILE), MB_OK);
256 else
257 MessageBox(hwnd, RS(b1,IDS_ERROR), RS(b2,IDS_WINEFILE), MB_OK);
258
259 LocalFree(msg);
260 }
261
262
263 /* display network error message using WNetGetLastError() */
264 static void display_network_error(HWND hwnd)
265 {
266 TCHAR msg[BUFFER_LEN], provider[BUFFER_LEN], b2[BUFFER_LEN];
267 DWORD error;
268
269 if (WNetGetLastError(&error, msg, BUFFER_LEN, provider, BUFFER_LEN) == NO_ERROR)
270 MessageBox(hwnd, msg, RS(b2,IDS_WINEFILE), MB_OK);
271 }
272
273
274 /* allocate and initialise a directory entry */
275 static Entry* alloc_entry()
276 {
277 Entry* entry = (Entry*) malloc(sizeof(Entry));
278
279 #ifdef _SHELL_FOLDERS
280 entry->pidl = NULL;
281 entry->folder = NULL;
282 entry->hicon = 0;
283 #endif
284
285 return entry;
286 }
287
288 /* free a directory entry */
289 static void free_entry(Entry* entry)
290 {
291 #ifdef _SHELL_FOLDERS
292 if (entry->hicon && entry->hicon!=(HICON)-1)
293 DestroyIcon(entry->hicon);
294
295 if (entry->folder && entry->folder!=Globals.iDesktop)
296 (*entry->folder->lpVtbl->Release)(entry->folder);
297
298 if (entry->pidl)
299 (*Globals.iMalloc->lpVtbl->Free)(Globals.iMalloc, entry->pidl);
300 #endif
301
302 free(entry);
303 }
304
305 /* recursively free all child entries */
306 static void free_entries(Entry* dir)
307 {
308 Entry *entry, *next=dir->down;
309
310 if (next) {
311 dir->down = 0;
312
313 do {
314 entry = next;
315 next = entry->next;
316
317 free_entries(entry);
318 free_entry(entry);
319 } while(next);
320 }
321 }
322
323
324 static void read_directory_win(Entry* dir, LPCTSTR path)
325 {
326 Entry* first_entry = NULL;
327 Entry* last = NULL;
328 Entry* entry;
329
330 int level = dir->level + 1;
331 WIN32_FIND_DATA w32fd;
332 HANDLE hFind;
333 #ifndef _NO_EXTENSIONS
334 HANDLE hFile;
335 #endif
336
337 TCHAR buffer[MAX_PATH], *p;
338 for(p=buffer; *path; )
339 *p++ = *path++;
340
341 *p++ = '\\';
342 p[0] = '*';
343 p[1] = '\0';
344
345 hFind = FindFirstFile(buffer, &w32fd);
346
347 if (hFind != INVALID_HANDLE_VALUE) {
348 do {
349 entry = alloc_entry();
350
351 if (!first_entry)
352 first_entry = entry;
353
354 if (last)
355 last->next = entry;
356
357 memcpy(&entry->data, &w32fd, sizeof(WIN32_FIND_DATA));
358 entry->down = NULL;
359 entry->up = dir;
360 entry->expanded = FALSE;
361 entry->scanned = FALSE;
362 entry->level = level;
363
364 #ifdef _NO_EXTENSIONS
365 /* hide directory entry "." */
366 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
367 LPCTSTR name = entry->data.cFileName;
368
369 if (name[0]=='.' && name[1]=='\0')
370 continue;
371 }
372 #else
373 entry->etype = ET_WINDOWS;
374 entry->bhfi_valid = FALSE;
375
376 lstrcpy(p, entry->data.cFileName);
377
378 hFile = CreateFile(buffer, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
379 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
380
381 if (hFile != INVALID_HANDLE_VALUE) {
382 if (GetFileInformationByHandle(hFile, &entry->bhfi))
383 entry->bhfi_valid = TRUE;
384
385 CloseHandle(hFile);
386 }
387 #endif
388
389 last = entry;
390 } while(FindNextFile(hFind, &entry->data));
391
392 last->next = NULL;
393
394 FindClose(hFind);
395 }
396
397 dir->down = first_entry;
398 dir->scanned = TRUE;
399 }
400
401
402 static Entry* find_entry_win(Entry* dir, LPCTSTR name)
403 {
404 Entry* entry;
405
406 for(entry=dir->down; entry; entry=entry->next) {
407 LPCTSTR p = name;
408 LPCTSTR q = entry->data.cFileName;
409
410 do {
411 if (!*p || *p==TEXT('\\') || *p==TEXT('/'))
412 return entry;
413 } while(tolower(*p++) == tolower(*q++));
414
415 p = name;
416 q = entry->data.cAlternateFileName;
417
418 do {
419 if (!*p || *p==TEXT('\\') || *p==TEXT('/'))
420 return entry;
421 } while(tolower(*p++) == tolower(*q++));
422 }
423
424 return 0;
425 }
426
427
428 static Entry* read_tree_win(Root* root, LPCTSTR path, SORT_ORDER sortOrder, HWND hwnd)
429 {
430 TCHAR buffer[MAX_PATH];
431 Entry* entry = &root->entry;
432 LPCTSTR s = path;
433 PTSTR d = buffer;
434
435 HCURSOR old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
436
437 #ifndef _NO_EXTENSIONS
438 entry->etype = ET_WINDOWS;
439 #endif
440
441 while(entry) {
442 while(*s && *s!=TEXT('\\') && *s!=TEXT('/'))
443 *d++ = *s++;
444
445 while(*s==TEXT('\\') || *s==TEXT('/'))
446 s++;
447
448 *d++ = TEXT('\\');
449 *d = TEXT('\0');
450
451 read_directory(entry, buffer, sortOrder, hwnd);
452
453 if (entry->down)
454 entry->expanded = TRUE;
455
456 if (!*s)
457 break;
458
459 entry = find_entry_win(entry, s);
460 }
461
462 SetCursor(old_cursor);
463
464 return entry;
465 }
466
467
468 #if !defined(_NO_EXTENSIONS) && defined(__WINE__)
469
470 static BOOL time_to_filetime(const time_t* t, FILETIME* ftime)
471 {
472 struct tm* tm = gmtime(t);
473 SYSTEMTIME stime;
474
475 if (!tm)
476 return FALSE;
477
478 stime.wYear = tm->tm_year+1900;
479 stime.wMonth = tm->tm_mon+1;
480 /* stime.wDayOfWeek */
481 stime.wDay = tm->tm_mday;
482 stime.wHour = tm->tm_hour;
483 stime.wMinute = tm->tm_min;
484 stime.wSecond = tm->tm_sec;
485
486 return SystemTimeToFileTime(&stime, ftime);
487 }
488
489 static void read_directory_unix(Entry* dir, LPCTSTR path)
490 {
491 Entry* first_entry = NULL;
492 Entry* last = NULL;
493 Entry* entry;
494 void* pdir;
495
496 #ifdef UNICODE
497 char cpath[MAX_PATH];
498
499 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, cpath, MAX_PATH, NULL, NULL);
500 #else
501 const char* cpath = path;
502 #endif
503
504 pdir = call_opendir(cpath);
505
506 int level = dir->level + 1;
507
508 if (pdir) {
509 char buffer[MAX_PATH];
510 time_t atime, mtime;
511 unsigned inode;
512 int is_dir;
513 const char* s;
514 char* p;
515
516 for(p=buffer,s=cpath; *s; )
517 *p++ = *s++;
518
519 if (p==buffer || p[-1]!='/')
520 *p++ = '/';
521
522 while(call_readdir(pdir, p, &inode)) {
523 entry = alloc_entry();
524
525 if (!first_entry)
526 first_entry = entry;
527
528 if (last)
529 last->next = entry;
530
531 entry->etype = ET_UNIX;
532
533 #ifdef UNICODE
534 MultiByteToWideChar(CP_UNIXCP, 0, p, -1, entry->data.cFileName, MAX_PATH);
535 #else
536 lstrcpy(entry->data.cFileName, p);
537 #endif
538
539 entry->data.dwFileAttributes = p[0]=='.'? FILE_ATTRIBUTE_HIDDEN: 0;
540
541 if (!call_stat(buffer, &is_dir,
542 &entry->data.nFileSizeLow, &entry->data.nFileSizeHigh,
543 &atime, &mtime, &entry->bhfi.nNumberOfLinks))
544 {
545 if (is_dir)
546 entry->data.dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
547
548 memset(&entry->data.ftCreationTime, 0, sizeof(FILETIME));
549 time_to_filetime(&atime, &entry->data.ftLastAccessTime);
550 time_to_filetime(&mtime, &entry->data.ftLastWriteTime);
551
552 entry->bhfi.nFileIndexLow = inode;
553 entry->bhfi.nFileIndexHigh = 0;
554
555 entry->bhfi_valid = TRUE;
556 } else {
557 entry->data.nFileSizeLow = 0;
558 entry->data.nFileSizeHigh = 0;
559 entry->bhfi_valid = FALSE;
560 }
561
562 entry->down = NULL;
563 entry->up = dir;
564 entry->expanded = FALSE;
565 entry->scanned = FALSE;
566 entry->level = level;
567
568 last = entry;
569 }
570
571 last->next = NULL;
572
573 call_closedir(pdir);
574 }
575
576 dir->down = first_entry;
577 dir->scanned = TRUE;
578 }
579
580 static Entry* find_entry_unix(Entry* dir, LPCTSTR name)
581 {
582 Entry* entry;
583
584 for(entry=dir->down; entry; entry=entry->next) {
585 LPCTSTR p = name;
586 LPCTSTR q = entry->data.cFileName;
587
588 do {
589 if (!*p || *p==TEXT('/'))
590 return entry;
591 } while(*p++ == *q++);
592 }
593
594 return 0;
595 }
596
597 static Entry* read_tree_unix(Root* root, LPCTSTR path, SORT_ORDER sortOrder, HWND hwnd)
598 {
599 TCHAR buffer[MAX_PATH];
600 Entry* entry = &root->entry;
601 LPCTSTR s = path;
602 PTSTR d = buffer;
603
604 HCURSOR old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
605
606 entry->etype = ET_UNIX;
607
608 while(entry) {
609 while(*s && *s!=TEXT('/'))
610 *d++ = *s++;
611
612 while(*s == TEXT('/'))
613 s++;
614
615 *d++ = TEXT('/');
616 *d = TEXT('\0');
617
618 read_directory(entry, buffer, sortOrder, hwnd);
619
620 if (entry->down)
621 entry->expanded = TRUE;
622
623 if (!*s)
624 break;
625
626 entry = find_entry_unix(entry, s);
627 }
628
629 SetCursor(old_cursor);
630
631 return entry;
632 }
633
634 #endif /* !defined(_NO_EXTENSIONS) && defined(__WINE__) */
635
636
637 #ifdef _SHELL_FOLDERS
638
639 #ifdef UNICODE
640 #define tcscpyn strcpyn
641 #define get_strret get_strretW
642 #define path_from_pidl path_from_pidlW
643 #else
644 #define tcscpyn wcscpyn
645 #define get_strret get_strretA
646 #define path_from_pidl path_from_pidlA
647 #endif
648
649
650 static LPSTR strcpyn(LPSTR dest, LPCSTR source, size_t count)
651 {
652 LPCSTR s;
653 LPSTR d = dest;
654
655 for(s=source; count&&(*d++=*s++); )
656 count--;
657
658 return dest;
659 }
660
661 static LPWSTR wcscpyn(LPWSTR dest, LPCWSTR source, size_t count)
662 {
663 LPCWSTR s;
664 LPWSTR d = dest;
665
666 for(s=source; count&&(*d++=*s++); )
667 count--;
668
669 return dest;
670 }
671
672
673 static void get_strretA(STRRET* str, const SHITEMID* shiid, LPSTR buffer, int len)
674 {
675 switch(str->uType) {
676 case STRRET_WSTR:
677 WideCharToMultiByte(CP_ACP, 0, str->UNION_MEMBER(pOleStr), -1, buffer, len, NULL, NULL);
678 break;
679
680 case STRRET_OFFSET:
681 strcpyn(buffer, (LPCSTR)shiid+str->UNION_MEMBER(uOffset), len);
682 break;
683
684 case STRRET_CSTR:
685 strcpyn(buffer, str->UNION_MEMBER(cStr), len);
686 }
687 }
688
689 static void get_strretW(STRRET* str, const SHITEMID* shiid, LPWSTR buffer, int len)
690 {
691 switch(str->uType) {
692 case STRRET_WSTR:
693 wcscpyn(buffer, str->UNION_MEMBER(pOleStr), len);
694 break;
695
696 case STRRET_OFFSET:
697 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)shiid+str->UNION_MEMBER(uOffset), -1, buffer, len);
698 break;
699
700 case STRRET_CSTR:
701 MultiByteToWideChar(CP_ACP, 0, str->UNION_MEMBER(cStr), -1, buffer, len);
702 }
703 }
704
705
706 static void free_strret(STRRET* str)
707 {
708 if (str->uType == STRRET_WSTR)
709 (*Globals.iMalloc->lpVtbl->Free)(Globals.iMalloc, str->UNION_MEMBER(pOleStr));
710 }
711
712
713 HRESULT name_from_pidl(IShellFolder* folder, LPITEMIDLIST pidl, LPTSTR buffer, int len, SHGDNF flags)
714 {
715 STRRET str;
716
717 HRESULT hr = (*folder->lpVtbl->GetDisplayNameOf)(folder, pidl, flags, &str);
718
719 if (SUCCEEDED(hr)) {
720 get_strret(&str, &pidl->mkid, buffer, len);
721 free_strret(&str);
722 } else
723 buffer[0] = '\0';
724
725 return hr;
726 }
727
728
729 HRESULT path_from_pidlA(IShellFolder* folder, LPITEMIDLIST pidl, LPSTR buffer, int len)
730 {
731 STRRET str;
732
733 /* SHGDN_FORPARSING: get full path of id list */
734 HRESULT hr = (*folder->lpVtbl->GetDisplayNameOf)(folder, pidl, SHGDN_FORPARSING, &str);
735
736 if (SUCCEEDED(hr)) {
737 get_strretA(&str, &pidl->mkid, buffer, len);
738 free_strret(&str);
739 } else
740 buffer[0] = '\0';
741
742 return hr;
743 }
744
745 HRESULT path_from_pidlW(IShellFolder* folder, LPITEMIDLIST pidl, LPWSTR buffer, int len)
746 {
747 STRRET str;
748
749 /* SHGDN_FORPARSING: get full path of id list */
750 HRESULT hr = (*folder->lpVtbl->GetDisplayNameOf)(folder, pidl, SHGDN_FORPARSING, &str);
751
752 if (SUCCEEDED(hr)) {
753 get_strretW(&str, &pidl->mkid, buffer, len);
754 free_strret(&str);
755 } else
756 buffer[0] = '\0';
757
758 return hr;
759 }
760
761
762 /* create an item id list from a file system path */
763
764 static LPITEMIDLIST get_path_pidl(LPTSTR path, HWND hwnd)
765 {
766 LPITEMIDLIST pidl;
767 HRESULT hr;
768 ULONG len;
769
770 #ifdef UNICODE
771 LPWSTR buffer = path;
772 #else
773 WCHAR buffer[MAX_PATH];
774 MultiByteToWideChar(CP_ACP, 0, path, -1, buffer, MAX_PATH);
775 #endif
776
777 hr = (*Globals.iDesktop->lpVtbl->ParseDisplayName)(Globals.iDesktop, hwnd, NULL, buffer, &len, &pidl, NULL);
778 if (FAILED(hr))
779 return NULL;
780
781 return pidl;
782 }
783
784
785 /* convert an item id list from relative to absolute (=relative to the desktop) format */
786
787 static LPITEMIDLIST get_to_absolute_pidl(Entry* entry, HWND hwnd)
788 {
789 if (entry->up && entry->up->etype==ET_SHELL) {
790 IShellFolder* folder = entry->up->folder;
791 WCHAR buffer[MAX_PATH];
792
793 HRESULT hr = path_from_pidlW(folder, entry->pidl, buffer, MAX_PATH);
794
795 if (SUCCEEDED(hr)) {
796 LPITEMIDLIST pidl;
797 ULONG len;
798
799 hr = (*Globals.iDesktop->lpVtbl->ParseDisplayName)(Globals.iDesktop, hwnd, NULL, buffer, &len, &pidl, NULL);
800
801 if (SUCCEEDED(hr))
802 return pidl;
803 }
804 } else if (entry->etype == ET_WINDOWS) {
805 TCHAR path[MAX_PATH];
806
807 get_path(entry, path);
808
809 return get_path_pidl(path, hwnd);
810 } else if (entry->pidl)
811 return ILClone(entry->pidl);
812
813 return NULL;
814 }
815
816
817 HICON extract_icon(IShellFolder* folder, LPCITEMIDLIST pidl)
818 {
819 IExtractIcon* pExtract;
820
821 if (SUCCEEDED((*folder->lpVtbl->GetUIObjectOf)(folder, 0, 1, (LPCITEMIDLIST*)&pidl, &IID_IExtractIcon, 0, (LPVOID*)&pExtract))) {
822 TCHAR path[_MAX_PATH];
823 unsigned flags;
824 HICON hicon;
825 int idx;
826
827 if (SUCCEEDED((*pExtract->lpVtbl->GetIconLocation)(pExtract, GIL_FORSHELL, path, _MAX_PATH, &idx, &flags))) {
828 if (!(flags & GIL_NOTFILENAME)) {
829 if (idx == -1)
830 idx = 0; /* special case for some control panel applications */
831
832 if ((int)ExtractIconEx(path, idx, 0, &hicon, 1) > 0)
833 flags &= ~GIL_DONTCACHE;
834 } else {
835 HICON hIconLarge = 0;
836
837 HRESULT hr = (*pExtract->lpVtbl->Extract)(pExtract, path, idx, &hIconLarge, &hicon, MAKELONG(0/*GetSystemMetrics(SM_CXICON)*/,GetSystemMetrics(SM_CXSMICON)));
838
839 if (SUCCEEDED(hr))
840 DestroyIcon(hIconLarge);
841 }
842
843 return hicon;
844 }
845 }
846
847 return 0;
848 }
849
850
851 static Entry* find_entry_shell(Entry* dir, LPCITEMIDLIST pidl)
852 {
853 Entry* entry;
854
855 for(entry=dir->down; entry; entry=entry->next) {
856 if (entry->pidl->mkid.cb == pidl->mkid.cb &&
857 !memcmp(entry->pidl, pidl, entry->pidl->mkid.cb))
858 return entry;
859 }
860
861 return 0;
862 }
863
864 static Entry* read_tree_shell(Root* root, LPITEMIDLIST pidl, SORT_ORDER sortOrder, HWND hwnd)
865 {
866 Entry* entry = &root->entry;
867 Entry* next;
868 LPITEMIDLIST next_pidl = pidl;
869 IShellFolder* folder;
870 IShellFolder* child = NULL;
871 HRESULT hr;
872
873 HCURSOR old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
874
875 #ifndef _NO_EXTENSIONS
876 entry->etype = ET_SHELL;
877 #endif
878
879 folder = Globals.iDesktop;
880
881 while(entry) {
882 entry->pidl = next_pidl;
883 entry->folder = folder;
884
885 if (!pidl->mkid.cb)
886 break;
887
888 /* copy first element of item idlist */
889 next_pidl = (*Globals.iMalloc->lpVtbl->Alloc)(Globals.iMalloc, pidl->mkid.cb+sizeof(USHORT));
890 memcpy(next_pidl, pidl, pidl->mkid.cb);
891 ((LPITEMIDLIST)((LPBYTE)next_pidl+pidl->mkid.cb))->mkid.cb = 0;
892
893 hr = (*folder->lpVtbl->BindToObject)(folder, next_pidl, 0, &IID_IShellFolder, (void**)&child);
894 if (!SUCCEEDED(hr))
895 break;
896
897 read_directory(entry, NULL, sortOrder, hwnd);
898
899 if (entry->down)
900 entry->expanded = TRUE;
901
902 next = find_entry_shell(entry, next_pidl);
903 if (!next)
904 break;
905
906 folder = child;
907 entry = next;
908
909 /* go to next element */
910 pidl = (LPITEMIDLIST) ((LPBYTE)pidl+pidl->mkid.cb);
911 }
912
913 SetCursor(old_cursor);
914
915 return entry;
916 }
917
918
919 static void fill_w32fdata_shell(IShellFolder* folder, LPCITEMIDLIST pidl, SFGAOF attribs, WIN32_FIND_DATA* w32fdata)
920 {
921 if (!(attribs & SFGAO_FILESYSTEM) ||
922 FAILED(SHGetDataFromIDList(folder, pidl, SHGDFIL_FINDDATA, w32fdata, sizeof(WIN32_FIND_DATA)))) {
923 WIN32_FILE_ATTRIBUTE_DATA fad;
924 IDataObject* pDataObj;
925
926 STGMEDIUM medium = {0, {0}, 0};
927 FORMATETC fmt = {Globals.cfStrFName, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
928
929 HRESULT hr = (*folder->lpVtbl->GetUIObjectOf)(folder, 0, 1, &pidl, &IID_IDataObject, 0, (LPVOID*)&pDataObj);
930
931 if (SUCCEEDED(hr)) {
932 hr = (*pDataObj->lpVtbl->GetData)(pDataObj, &fmt, &medium);
933
934 (*pDataObj->lpVtbl->Release)(pDataObj);
935
936 if (SUCCEEDED(hr)) {
937 LPCTSTR path = (LPCTSTR)GlobalLock(medium.UNION_MEMBER(hGlobal));
938 UINT sem_org = SetErrorMode(SEM_FAILCRITICALERRORS);
939
940 if (GetFileAttributesEx(path, GetFileExInfoStandard, &fad)) {
941 w32fdata->dwFileAttributes = fad.dwFileAttributes;
942 w32fdata->ftCreationTime = fad.ftCreationTime;
943 w32fdata->ftLastAccessTime = fad.ftLastAccessTime;
944 w32fdata->ftLastWriteTime = fad.ftLastWriteTime;
945
946 if (!(fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
947 w32fdata->nFileSizeLow = fad.nFileSizeLow;
948 w32fdata->nFileSizeHigh = fad.nFileSizeHigh;
949 }
950 }
951
952 SetErrorMode(sem_org);
953
954 GlobalUnlock(medium.UNION_MEMBER(hGlobal));
955 GlobalFree(medium.UNION_MEMBER(hGlobal));
956 }
957 }
958 }
959
960 if (attribs & (SFGAO_FOLDER|SFGAO_HASSUBFOLDER))
961 w32fdata->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
962
963 if (attribs & SFGAO_READONLY)
964 w32fdata->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
965
966 if (attribs & SFGAO_COMPRESSED)
967 w32fdata->dwFileAttributes |= FILE_ATTRIBUTE_COMPRESSED;
968 }
969
970
971 static void read_directory_shell(Entry* dir, HWND hwnd)
972 {
973 IShellFolder* folder = dir->folder;
974 int level = dir->level + 1;
975 HRESULT hr;
976
977 IShellFolder* child;
978 IEnumIDList* idlist;
979
980 Entry* first_entry = NULL;
981 Entry* last = NULL;
982 Entry* entry;
983
984 if (!folder)
985 return;
986
987 hr = (*folder->lpVtbl->EnumObjects)(folder, hwnd, SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN|SHCONTF_SHAREABLE|SHCONTF_STORAGE, &idlist);
988
989 if (SUCCEEDED(hr)) {
990 for(;;) {
991 #define FETCH_ITEM_COUNT 32
992 LPITEMIDLIST pidls[FETCH_ITEM_COUNT];
993 SFGAOF attribs;
994 ULONG cnt = 0;
995 ULONG n;
996
997 memset(pidls, 0, sizeof(pidls));
998
999 hr = (*idlist->lpVtbl->Next)(idlist, FETCH_ITEM_COUNT, pidls, &cnt);
1000 if (!SUCCEEDED(hr))
1001 break;
1002
1003 if (hr == S_FALSE)
1004 break;
1005
1006 for(n=0; n<cnt; ++n) {
1007 entry = alloc_entry();
1008
1009 if (!first_entry)
1010 first_entry = entry;
1011
1012 if (last)
1013 last->next = entry;
1014
1015 memset(&entry->data, 0, sizeof(WIN32_FIND_DATA));
1016 entry->bhfi_valid = FALSE;
1017
1018 attribs = ~SFGAO_FILESYSTEM; /*SFGAO_HASSUBFOLDER|SFGAO_FOLDER; SFGAO_FILESYSTEM sorgt dafür, daß "My Documents" anstatt von "Martin's Documents" angezeigt wird */
1019
1020 hr = (*folder->lpVtbl->GetAttributesOf)(folder, 1, (LPCITEMIDLIST*)&pidls[n], &attribs);
1021
1022 if (SUCCEEDED(hr)) {
1023 if (attribs != (SFGAOF)~SFGAO_FILESYSTEM) {
1024 fill_w32fdata_shell(folder, pidls[n], attribs, &entry->data);
1025
1026 entry->bhfi_valid = TRUE;
1027 } else
1028 attribs = 0;
1029 } else
1030 attribs = 0;
1031
1032 entry->pidl = pidls[n];
1033
1034 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1035 hr = (*folder->lpVtbl->BindToObject)(folder, pidls[n], 0, &IID_IShellFolder, (void**)&child);
1036
1037 if (SUCCEEDED(hr))
1038 entry->folder = child;
1039 else
1040 entry->folder = NULL;
1041 }
1042 else
1043 entry->folder = NULL;
1044
1045 if (!entry->data.cFileName[0])
1046 /*hr = */name_from_pidl(folder, pidls[n], entry->data.cFileName, MAX_PATH, /*SHGDN_INFOLDER*/0x2000/*0x2000=SHGDN_INCLUDE_NONFILESYS*/);
1047
1048 /* get display icons for files and virtual objects */
1049 if (!(entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
1050 !(attribs & SFGAO_FILESYSTEM)) {
1051 entry->hicon = extract_icon(folder, pidls[n]);
1052
1053 if (!entry->hicon)
1054 entry->hicon = (HICON)-1; /* don't try again later */
1055 }
1056
1057 entry->down = NULL;
1058 entry->up = dir;
1059 entry->expanded = FALSE;
1060 entry->scanned = FALSE;
1061 entry->level = level;
1062
1063 #ifndef _NO_EXTENSIONS
1064 entry->etype = ET_SHELL;
1065 entry->bhfi_valid = FALSE;
1066 #endif
1067
1068 last = entry;
1069 }
1070 }
1071
1072 (*idlist->lpVtbl->Release)(idlist);
1073 }
1074
1075 if (last)
1076 last->next = NULL;
1077
1078 dir->down = first_entry;
1079 dir->scanned = TRUE;
1080 }
1081
1082 #endif /* _SHELL_FOLDERS */
1083
1084
1085 /* sort order for different directory/file types */
1086 enum TYPE_ORDER {
1087 TO_DIR = 0,
1088 TO_DOT = 1,
1089 TO_DOTDOT = 2,
1090 TO_OTHER_DIR = 3,
1091 TO_FILE = 4
1092 };
1093
1094 /* distinguish between ".", ".." and any other directory names */
1095 static int TypeOrderFromDirname(LPCTSTR name)
1096 {
1097 if (name[0] == '.') {
1098 if (name[1] == '\0')
1099 return TO_DOT; /* "." */
1100
1101 if (name[1]=='.' && name[2]=='\0')
1102 return TO_DOTDOT; /* ".." */
1103 }
1104
1105 return TO_OTHER_DIR; /* anything else */
1106 }
1107
1108 /* directories first... */
1109 static int compareType(const WIN32_FIND_DATA* fd1, const WIN32_FIND_DATA* fd2)
1110 {
1111 int order1 = fd1->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY? TO_DIR: TO_FILE;
1112 int order2 = fd2->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY? TO_DIR: TO_FILE;
1113
1114 /* Handle "." and ".." as special case and move them at the very first beginning. */
1115 if (order1==TO_DIR && order2==TO_DIR) {
1116 order1 = TypeOrderFromDirname(fd1->cFileName);
1117 order2 = TypeOrderFromDirname(fd2->cFileName);
1118 }
1119
1120 return order2==order1? 0: order1<order2? -1: 1;
1121 }
1122
1123
1124 static int compareName(const void* arg1, const void* arg2)
1125 {
1126 const WIN32_FIND_DATA* fd1 = &(*(const Entry* const*)arg1)->data;
1127 const WIN32_FIND_DATA* fd2 = &(*(const Entry* const*)arg2)->data;
1128
1129 int cmp = compareType(fd1, fd2);
1130 if (cmp)
1131 return cmp;
1132
1133 return lstrcmpi(fd1->cFileName, fd2->cFileName);
1134 }
1135
1136 static int compareExt(const void* arg1, const void* arg2)
1137 {
1138 const WIN32_FIND_DATA* fd1 = &(*(const Entry* const*)arg1)->data;
1139 const WIN32_FIND_DATA* fd2 = &(*(const Entry* const*)arg2)->data;
1140 const TCHAR *name1, *name2, *ext1, *ext2;
1141
1142 int cmp = compareType(fd1, fd2);
1143 if (cmp)
1144 return cmp;
1145
1146 name1 = fd1->cFileName;
1147 name2 = fd2->cFileName;
1148
1149 ext1 = _tcsrchr(name1, TEXT('.'));
1150 ext2 = _tcsrchr(name2, TEXT('.'));
1151
1152 if (ext1)
1153 ext1++;
1154 else
1155 ext1 = sEmpty;
1156
1157 if (ext2)
1158 ext2++;
1159 else
1160 ext2 = sEmpty;
1161
1162 cmp = lstrcmpi(ext1, ext2);
1163 if (cmp)
1164 return cmp;
1165
1166 return lstrcmpi(name1, name2);
1167 }
1168
1169 static int compareSize(const void* arg1, const void* arg2)
1170 {
1171 const WIN32_FIND_DATA* fd1 = &(*(const Entry* const*)arg1)->data;
1172 const WIN32_FIND_DATA* fd2 = &(*(const Entry* const*)arg2)->data;
1173
1174 int cmp = compareType(fd1, fd2);
1175 if (cmp)
1176 return cmp;
1177
1178 cmp = fd2->nFileSizeHigh - fd1->nFileSizeHigh;
1179
1180 if (cmp < 0)
1181 return -1;
1182 else if (cmp > 0)
1183 return 1;
1184
1185 cmp = fd2->nFileSizeLow - fd1->nFileSizeLow;
1186
1187 return cmp<0? -1: cmp>0? 1: 0;
1188 }
1189
1190 static int compareDate(const void* arg1, const void* arg2)
1191 {
1192 const WIN32_FIND_DATA* fd1 = &(*(const Entry* const*)arg1)->data;
1193 const WIN32_FIND_DATA* fd2 = &(*(const Entry* const*)arg2)->data;
1194
1195 int cmp = compareType(fd1, fd2);
1196 if (cmp)
1197 return cmp;
1198
1199 return CompareFileTime(&fd2->ftLastWriteTime, &fd1->ftLastWriteTime);
1200 }
1201
1202
1203 static int (*sortFunctions[])(const void* arg1, const void* arg2) = {
1204 compareName, /* SORT_NAME */
1205 compareExt, /* SORT_EXT */
1206 compareSize, /* SORT_SIZE */
1207 compareDate /* SORT_DATE */
1208 };
1209
1210
1211 static void SortDirectory(Entry* dir, SORT_ORDER sortOrder)
1212 {
1213 Entry* entry = dir->down;
1214 Entry** array, **p;
1215 int len;
1216
1217 len = 0;
1218 for(entry=dir->down; entry; entry=entry->next)
1219 len++;
1220
1221 if (len) {
1222 array = (Entry**) TMP_ALLOC(len*sizeof(Entry*));
1223
1224 p = array;
1225 for(entry=dir->down; entry; entry=entry->next)
1226 *p++ = entry;
1227
1228 /* call qsort with the appropriate compare function */
1229 qsort(array, len, sizeof(array[0]), sortFunctions[sortOrder]);
1230
1231 dir->down = array[0];
1232
1233 for(p=array; --len; p++)
1234 p[0]->next = p[1];
1235
1236 (*p)->next = 0;
1237
1238 TMP_FREE(array);
1239 }
1240 }
1241
1242
1243 static void read_directory(Entry* dir, LPCTSTR path, SORT_ORDER sortOrder, HWND hwnd)
1244 {
1245 TCHAR buffer[MAX_PATH];
1246 Entry* entry;
1247 LPCTSTR s;
1248 PTSTR d;
1249
1250 #ifdef _SHELL_FOLDERS
1251 if (dir->etype == ET_SHELL)
1252 {
1253 read_directory_shell(dir, hwnd);
1254
1255 if (Globals.prescan_node) {
1256 s = path;
1257 d = buffer;
1258
1259 while(*s)
1260 *d++ = *s++;
1261
1262 *d++ = TEXT('\\');
1263
1264 for(entry=dir->down; entry; entry=entry->next)
1265 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1266 read_directory_shell(entry, hwnd);
1267 SortDirectory(entry, sortOrder);
1268 }
1269 }
1270 }
1271 else
1272 #endif
1273 #if !defined(_NO_EXTENSIONS) && defined(__WINE__)
1274 if (dir->etype == ET_UNIX)
1275 {
1276 read_directory_unix(dir, path);
1277
1278 if (Globals.prescan_node) {
1279 s = path;
1280 d = buffer;
1281
1282 while(*s)
1283 *d++ = *s++;
1284
1285 *d++ = TEXT('/');
1286
1287 for(entry=dir->down; entry; entry=entry->next)
1288 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1289 lstrcpy(d, entry->data.cFileName);
1290 read_directory_unix(entry, buffer);
1291 SortDirectory(entry, sortOrder);
1292 }
1293 }
1294 }
1295 else
1296 #endif
1297 {
1298 read_directory_win(dir, path);
1299
1300 if (Globals.prescan_node) {
1301 s = path;
1302 d = buffer;
1303
1304 while(*s)
1305 *d++ = *s++;
1306
1307 *d++ = TEXT('\\');
1308
1309 for(entry=dir->down; entry; entry=entry->next)
1310 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1311 lstrcpy(d, entry->data.cFileName);
1312 read_directory_win(entry, buffer);
1313 SortDirectory(entry, sortOrder);
1314 }
1315 }
1316 }
1317
1318 SortDirectory(dir, sortOrder);
1319 }
1320
1321
1322 static ChildWnd* alloc_child_window(LPCTSTR path, LPITEMIDLIST pidl, HWND hwnd)
1323 {
1324 const static TCHAR sBackslash[] = {'\\', '\0'};
1325 #if !defined(_NO_EXTENSIONS) && defined(__WINE__)
1326 const static TCHAR sSlash[] = {'/', '\0'};
1327 #endif
1328
1329 TCHAR drv[_MAX_DRIVE+1], dir[_MAX_DIR], name[_MAX_FNAME], ext[_MAX_EXT];
1330 TCHAR b1[BUFFER_LEN];
1331
1332 ChildWnd* child = (ChildWnd*) malloc(sizeof(ChildWnd));
1333 Root* root = &child->root;
1334 Entry* entry;
1335
1336 memset(child, 0, sizeof(ChildWnd));
1337
1338 child->left.treePane = TRUE;
1339 child->left.visible_cols = 0;
1340
1341 child->right.treePane = FALSE;
1342 #ifndef _NO_EXTENSIONS
1343 child->right.visible_cols = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_INDEX|COL_LINKS;
1344 #else
1345 child->right.visible_cols = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES;
1346 #endif
1347
1348 child->pos.length = sizeof(WINDOWPLACEMENT);
1349 child->pos.flags = 0;
1350 child->pos.showCmd = SW_SHOWNORMAL;
1351 child->pos.rcNormalPosition.left = CW_USEDEFAULT;
1352 child->pos.rcNormalPosition.top = CW_USEDEFAULT;
1353 child->pos.rcNormalPosition.right = CW_USEDEFAULT;
1354 child->pos.rcNormalPosition.bottom = CW_USEDEFAULT;
1355
1356 child->focus_pane = 0;
1357 child->split_pos = DEFAULT_SPLIT_POS;
1358 child->sortOrder = SORT_NAME;
1359 child->header_wdths_ok = FALSE;
1360
1361 if (path)
1362 {
1363 lstrcpy(child->path, path);
1364
1365 _tsplitpath(path, drv, dir, name, ext);
1366 }
1367
1368 root->entry.level = 0;
1369
1370 #ifdef _SHELL_FOLDERS
1371 if (pidl)
1372 {
1373 root->drive_type = DRIVE_UNKNOWN;
1374 drv[0] = '\\';
1375 drv[1] = '\0';
1376 load_string(root->volname, IDS_DESKTOP);
1377 root->fs_flags = 0;
1378 load_string(root->fs, IDS_SHELL);
1379
1380 entry = read_tree_shell(root, pidl, child->sortOrder, hwnd);
1381 }
1382 else
1383 #endif
1384 #if !defined(_NO_EXTENSIONS) && defined(__WINE__)
1385 if (*path == '/')
1386 {
1387 root->drive_type = GetDriveType(path);
1388
1389 lstrcat(drv, sSlash);
1390 load_string(root->volname, IDS_ROOT_FS);
1391 root->fs_flags = 0;
1392 load_string(root->fs, IDS_UNIXFS);
1393
1394 lstrcpy(root->path, sSlash);
1395 entry = read_tree_unix(root, path, child->sortOrder, hwnd);
1396 }
1397 else
1398 #endif
1399 {
1400 root->drive_type = GetDriveType(path);
1401
1402 lstrcat(drv, sBackslash);
1403 GetVolumeInformation(drv, root->volname, _MAX_FNAME, 0, 0, &root->fs_flags, root->fs, _MAX_DIR);
1404
1405 lstrcpy(root->path, drv);
1406 entry = read_tree_win(root, path, child->sortOrder, hwnd);
1407 }
1408
1409 #ifdef _SHELL_FOLDERS
1410 if (root->entry.etype == ET_SHELL)
1411 load_string(root->entry.data.cFileName, IDS_DESKTOP);
1412 else
1413 #endif
1414 wsprintf(root->entry.data.cFileName, RS(b1,IDS_TITLEFMT), drv, root->fs);
1415
1416 root->entry.data.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1417
1418 child->left.root = &root->entry;
1419 child->right.root = NULL;
1420
1421 set_curdir(child, entry, hwnd);
1422
1423 return child;
1424 }
1425
1426
1427 /* free all memory associated with a child window */
1428 static void free_child_window(ChildWnd* child)
1429 {
1430 free_entries(&child->root.entry);
1431 free(child);
1432 }
1433
1434
1435 /* get full path of specified directory entry */
1436 static void get_path(Entry* dir, PTSTR path)
1437 {
1438 Entry* entry;
1439 int len = 0;
1440 int level = 0;
1441
1442 #ifdef _SHELL_FOLDERS
1443 if (dir->etype == ET_SHELL)
1444 {
1445 SFGAOF attribs;
1446 HRESULT hr = S_OK;
1447
1448 path[0] = TEXT('\0');
1449
1450 attribs = 0;
1451
1452 if (dir->folder)
1453 hr = (*dir->folder->lpVtbl->GetAttributesOf)(dir->folder, 1, (LPCITEMIDLIST*)&dir->pidl, &attribs);
1454
1455 if (SUCCEEDED(hr) && (attribs&SFGAO_FILESYSTEM)) {
1456 IShellFolder* parent = dir->up? dir->up->folder: Globals.iDesktop;
1457
1458 hr = path_from_pidl(parent, dir->pidl, path, MAX_PATH);
1459 }
1460 }
1461 else
1462 #endif
1463 {
1464 for(entry=dir; entry; level++) {
1465 LPCTSTR name;
1466 int l;
1467
1468 {
1469 LPCTSTR s;
1470 name = entry->data.cFileName;
1471 s = name;
1472
1473 for(l=0; *s && *s!=TEXT('/') && *s!=TEXT('\\'); s++)
1474 l++;
1475 }
1476
1477 if (entry->up) {
1478 if (l > 0) {
1479 memmove(path+l+1, path, len*sizeof(TCHAR));
1480 memcpy(path+1, name, l*sizeof(TCHAR));
1481 len += l+1;
1482
1483 #ifndef _NO_EXTENSIONS
1484 if (entry->etype == ET_UNIX)
1485 path[0] = TEXT('/');
1486 else
1487 #endif
1488 path[0] = TEXT('\\');
1489 }
1490
1491 entry = entry->up;
1492 } else {
1493 memmove(path+l, path, len*sizeof(TCHAR));
1494 memcpy(path, name, l*sizeof(TCHAR));
1495 len += l;
1496 break;
1497 }
1498 }
1499
1500 if (!level) {
1501 #ifndef _NO_EXTENSIONS
1502 if (entry->etype == ET_UNIX)
1503 path[len++] = TEXT('/');
1504 else
1505 #endif
1506 path[len++] = TEXT('\\');
1507 }
1508
1509 path[len] = TEXT('\0');
1510 }
1511 }
1512
1513
1514 static void resize_frame_rect(HWND hwnd, PRECT prect)
1515 {
1516 int new_top;
1517 RECT rt;
1518
1519 if (IsWindowVisible(Globals.htoolbar)) {
1520 SendMessage(Globals.htoolbar, WM_SIZE, 0, 0);
1521 GetClientRect(Globals.htoolbar, &rt);
1522 prect->top = rt.bottom+3;
1523 prect->bottom -= rt.bottom+3;
1524 }
1525
1526 if (IsWindowVisible(Globals.hdrivebar)) {
1527 SendMessage(Globals.hdrivebar, WM_SIZE, 0, 0);
1528 GetClientRect(Globals.hdrivebar, &rt);
1529 new_top = --prect->top + rt.bottom+3;
1530 MoveWindow(Globals.hdrivebar, 0, prect->top, rt.right, new_top, TRUE);
1531 prect->top = new_top;
1532 prect->bottom -= rt.bottom+2;
1533 }
1534
1535 if (IsWindowVisible(Globals.hstatusbar)) {
1536 int parts[] = {300, 500};
1537
1538 SendMessage(Globals.hstatusbar, WM_SIZE, 0, 0);
1539 SendMessage(Globals.hstatusbar, SB_SETPARTS, 2, (LPARAM)&parts);
1540 GetClientRect(Globals.hstatusbar, &rt);
1541 prect->bottom -= rt.bottom;
1542 }
1543
1544 MoveWindow(Globals.hmdiclient, prect->left-1,prect->top-1,prect->right+2,prect->bottom+1, TRUE);
1545 }
1546
1547 static void resize_frame(HWND hwnd, int cx, int cy)
1548 {
1549 RECT rect;
1550
1551 rect.left = 0;
1552 rect.top = 0;
1553 rect.right = cx;
1554 rect.bottom = cy;
1555
1556 resize_frame_rect(hwnd, &rect);
1557 }
1558
1559 static void resize_frame_client(HWND hwnd)
1560 {
1561 RECT rect;
1562
1563 GetClientRect(hwnd, &rect);
1564
1565 resize_frame_rect(hwnd, &rect);
1566 }
1567
1568
1569 static HHOOK hcbthook;
1570 static ChildWnd* newchild = NULL;
1571
1572 LRESULT CALLBACK CBTProc(int code, WPARAM wparam, LPARAM lparam)
1573 {
1574 if (code==HCBT_CREATEWND && newchild) {
1575 ChildWnd* child = newchild;
1576 newchild = NULL;
1577
1578 child->hwnd = (HWND) wparam;
1579 SetWindowLong(child->hwnd, GWL_USERDATA, (LPARAM)child);
1580 }
1581
1582 return CallNextHookEx(hcbthook, code, wparam, lparam);
1583 }
1584
1585 static HWND create_child_window(ChildWnd* child)
1586 {
1587 MDICREATESTRUCT mcs;
1588 int idx;
1589
1590 mcs.szClass = sWINEFILETREE;
1591 mcs.szTitle = (LPTSTR)child->path;
1592 mcs.hOwner = Globals.hInstance;
1593 mcs.x = child->pos.rcNormalPosition.left;
1594 mcs.y = child->pos.rcNormalPosition.top;
1595 mcs.cx = child->pos.rcNormalPosition.right-child->pos.rcNormalPosition.left;
1596 mcs.cy = child->pos.rcNormalPosition.bottom-child->pos.rcNormalPosition.top;
1597 mcs.style = 0;
1598 mcs.lParam = 0;
1599
1600 hcbthook = SetWindowsHookEx(WH_CBT, CBTProc, 0, GetCurrentThreadId());
1601
1602 newchild = child;
1603 child->hwnd = (HWND) SendMessage(Globals.hmdiclient, WM_MDICREATE, 0, (LPARAM)&mcs);
1604 if (!child->hwnd) {
1605 UnhookWindowsHookEx(hcbthook);
1606 return 0;
1607 }
1608
1609 UnhookWindowsHookEx(hcbthook);
1610
1611 ListBox_SetItemHeight(child->left.hwnd, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
1612 ListBox_SetItemHeight(child->right.hwnd, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
1613 idx = ListBox_FindItemData(child->left.hwnd, ListBox_GetCurSel(child->left.hwnd), child->left.cur);
1614 ListBox_SetCurSel(child->left.hwnd, idx);
1615
1616 return child->hwnd;
1617 }
1618
1619
1620 struct ExecuteDialog {
1621 TCHAR cmd[MAX_PATH];
1622 int cmdshow;
1623 };
1624
1625
1626 static INT_PTR CALLBACK ExecuteDialogDlgProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
1627 {
1628 static struct ExecuteDialog* dlg;
1629
1630 switch(nmsg) {
1631 case WM_INITDIALOG:
1632 dlg = (struct ExecuteDialog*) lparam;
1633 return 1;
1634
1635 case WM_COMMAND: {
1636 int id = (int)wparam;
1637
1638 if (id == IDOK) {
1639 GetWindowText(GetDlgItem(hwnd, 201), dlg->cmd, MAX_PATH);
1640 dlg->cmdshow = Button_GetState(GetDlgItem(hwnd,214))&BST_CHECKED?
1641 SW_SHOWMINIMIZED: SW_SHOWNORMAL;
1642 EndDialog(hwnd, id);
1643 } else if (id == IDCANCEL)
1644 EndDialog(hwnd, id);
1645
1646 return 1;}
1647 }
1648
1649 return 0;
1650 }
1651
1652 static INT_PTR CALLBACK DestinationDlgProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
1653 {
1654 TCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
1655
1656 switch(nmsg) {
1657 case WM_INITDIALOG:
1658 SetWindowLong(hwnd, GWL_USERDATA, lparam);
1659 SetWindowText(GetDlgItem(hwnd, 201), (LPCTSTR)lparam);
1660 return 1;
1661
1662 case WM_COMMAND: {
1663 int id = (int)wparam;
1664
1665 switch(id) {
1666 case IDOK: {
1667 LPTSTR dest = (LPTSTR) GetWindowLong(hwnd, GWL_USERDATA);
1668 GetWindowText(GetDlgItem(hwnd, 201), dest, MAX_PATH);
1669 EndDialog(hwnd, id);
1670 break;}
1671
1672 case IDCANCEL:
1673 EndDialog(hwnd, id);
1674 break;
1675
1676 case 254:
1677 MessageBox(hwnd, RS(b1,IDS_NO_IMPL), RS(b2,IDS_WINEFILE), MB_OK);
1678 break;
1679 }
1680
1681 return 1;
1682 }
1683 }
1684
1685 return 0;
1686 }
1687
1688
1689 #ifndef _NO_EXTENSIONS
1690
1691 static struct FullScreenParameters {
1692 BOOL mode;
1693 RECT orgPos;
1694 BOOL wasZoomed;
1695 } g_fullscreen = {
1696 FALSE, /* mode */
1697 {0, 0, 0, 0},
1698 FALSE
1699 };
1700
1701 void frame_get_clientspace(HWND hwnd, PRECT prect)
1702 {
1703 RECT rt;
1704
1705 if (!IsIconic(hwnd))
1706 GetClientRect(hwnd, prect);
1707 else {
1708 WINDOWPLACEMENT wp;
1709
1710 GetWindowPlacement(hwnd, &wp);
1711
1712 prect->left = prect->top = 0;
1713 prect->right = wp.rcNormalPosition.right-wp.rcNormalPosition.left-
1714 2*(GetSystemMetrics(SM_CXSIZEFRAME)+GetSystemMetrics(SM_CXEDGE));
1715 prect->bottom = wp.rcNormalPosition.bottom-wp.rcNormalPosition.top-
1716 2*(GetSystemMetrics(SM_CYSIZEFRAME)+GetSystemMetrics(SM_CYEDGE))-
1717 GetSystemMetrics(SM_CYCAPTION)-GetSystemMetrics(SM_CYMENUSIZE);
1718 }
1719
1720 if (IsWindowVisible(Globals.htoolbar)) {
1721 GetClientRect(Globals.htoolbar, &rt);
1722 prect->top += rt.bottom+2;
1723 }
1724
1725 if (IsWindowVisible(Globals.hdrivebar)) {
1726 GetClientRect(Globals.hdrivebar, &rt);
1727 prect->top += rt.bottom+2;
1728 }
1729
1730 if (IsWindowVisible(Globals.hstatusbar)) {
1731 GetClientRect(Globals.hstatusbar, &rt);
1732 prect->bottom -= rt.bottom;
1733 }
1734 }
1735
1736 static BOOL toggle_fullscreen(HWND hwnd)
1737 {
1738 RECT rt;
1739
1740 if ((g_fullscreen.mode=!g_fullscreen.mode)) {
1741 GetWindowRect(hwnd, &g_fullscreen.orgPos);
1742 g_fullscreen.wasZoomed = IsZoomed(hwnd);
1743
1744 Frame_CalcFrameClient(hwnd, &rt);
1745 ClientToScreen(hwnd, (LPPOINT)&rt.left);
1746 ClientToScreen(hwnd, (LPPOINT)&rt.right);
1747
1748 rt.left = g_fullscreen.orgPos.left-rt.left;
1749 rt.top = g_fullscreen.orgPos.top-rt.top;
1750 rt.right = GetSystemMetrics(SM_CXSCREEN)+g_fullscreen.orgPos.right-rt.right;
1751 rt.bottom = GetSystemMetrics(SM_CYSCREEN)+g_fullscreen.orgPos.bottom-rt.bottom;
1752
1753 MoveWindow(hwnd, rt.left, rt.top, rt.right-rt.left, rt.bottom-rt.top, TRUE);
1754 } else {
1755 MoveWindow(hwnd, g_fullscreen.orgPos.left, g_fullscreen.orgPos.top,
1756 g_fullscreen.orgPos.right-g_fullscreen.orgPos.left,
1757 g_fullscreen.orgPos.bottom-g_fullscreen.orgPos.top, TRUE);
1758
1759 if (g_fullscreen.wasZoomed)
1760 ShowWindow(hwnd, WS_MAXIMIZE);
1761 }
1762
1763 return g_fullscreen.mode;
1764 }
1765
1766 static void fullscreen_move(HWND hwnd)
1767 {
1768 RECT rt, pos;
1769 GetWindowRect(hwnd, &pos);
1770
1771 Frame_CalcFrameClient(hwnd, &rt);
1772 ClientToScreen(hwnd, (LPPOINT)&rt.left);
1773 ClientToScreen(hwnd, (LPPOINT)&rt.right);
1774
1775 rt.left = pos.left-rt.left;
1776 rt.top = pos.top-rt.top;
1777 rt.right = GetSystemMetrics(SM_CXSCREEN)+pos.right-rt.right;
1778 rt.bottom = GetSystemMetrics(SM_CYSCREEN)+pos.bottom-rt.bottom;
1779
1780 MoveWindow(hwnd, rt.left, rt.top, rt.right-rt.left, rt.bottom-rt.top, TRUE);
1781 }
1782
1783 #endif
1784
1785
1786 static void toggle_child(HWND hwnd, UINT cmd, HWND hchild)
1787 {
1788 BOOL vis = IsWindowVisible(hchild);
1789
1790 CheckMenuItem(Globals.hMenuOptions, cmd, vis?MF_BYCOMMAND:MF_BYCOMMAND|MF_CHECKED);
1791
1792 ShowWindow(hchild, vis?SW_HIDE:SW_SHOW);
1793
1794 #ifndef _NO_EXTENSIONS
1795 if (g_fullscreen.mode)
1796 fullscreen_move(hwnd);
1797 #endif
1798
1799 resize_frame_client(hwnd);
1800 }
1801
1802 BOOL activate_drive_window(LPCTSTR path)
1803 {
1804 TCHAR drv1[_MAX_DRIVE], drv2[_MAX_DRIVE];
1805 HWND child_wnd;
1806
1807 _tsplitpath(path, drv1, 0, 0, 0);
1808
1809 /* search for a already open window for the same drive */
1810 for(child_wnd=GetNextWindow(Globals.hmdiclient,GW_CHILD); child_wnd; child_wnd=GetNextWindow(child_wnd, GW_HWNDNEXT)) {
1811 ChildWnd* child = (ChildWnd*) GetWindowLong(child_wnd, GWL_USERDATA);
1812
1813 if (child) {
1814 _tsplitpath(child->root.path, drv2, 0, 0, 0);
1815
1816 if (!lstrcmpi(drv2, drv1)) {
1817 SendMessage(Globals.hmdiclient, WM_MDIACTIVATE, (WPARAM)child_wnd, 0);
1818
1819 if (IsMinimized(child_wnd))
1820 ShowWindow(child_wnd, SW_SHOWNORMAL);
1821
1822 return TRUE;
1823 }
1824 }
1825 }
1826
1827 return FALSE;
1828 }
1829
1830 BOOL activate_fs_window(LPCTSTR filesys)
1831 {
1832 HWND child_wnd;
1833
1834 /* search for a already open window of the given file system name */
1835 for(child_wnd=GetNextWindow(Globals.hmdiclient,GW_CHILD); child_wnd; child_wnd=GetNextWindow(child_wnd, GW_HWNDNEXT)) {
1836 ChildWnd* child = (ChildWnd*) GetWindowLong(child_wnd, GWL_USERDATA);
1837
1838 if (child) {
1839 if (!lstrcmpi(child->root.fs, filesys)) {
1840 SendMessage(Globals.hmdiclient, WM_MDIACTIVATE, (WPARAM)child_wnd, 0);
1841
1842 if (IsMinimized(child_wnd))
1843 ShowWindow(child_wnd, SW_SHOWNORMAL);
1844
1845 return TRUE;
1846 }
1847 }
1848 }
1849
1850 return FALSE;
1851 }
1852
1853 LRESULT CALLBACK FrameWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
1854 {
1855 TCHAR b1[BUFFER_LEN], b2[BUFFER_LEN];
1856
1857 switch(nmsg) {
1858 case WM_CLOSE:
1859 DestroyWindow(hwnd);
1860
1861 /* clear handle variables */
1862 Globals.hMenuFrame = 0;
1863 Globals.hMenuView = 0;
1864 Globals.hMenuOptions = 0;
1865 Globals.hMainWnd = 0;
1866 Globals.hmdiclient = 0;
1867 Globals.hdrivebar = 0;
1868 break;
1869
1870 case WM_DESTROY:
1871 /* don't exit desktop when closing file manager window */
1872 if (!Globals.hwndParent)
1873 PostQuitMessage(0);
1874 break;
1875
1876 case WM_COMMAND: {
1877 UINT cmd = LOWORD(wparam);
1878 HWND hwndClient = (HWND) SendMessage(Globals.hmdiclient, WM_MDIGETACTIVE, 0, 0);
1879
1880 if (SendMessage(hwndClient, WM_DISPATCH_COMMAND, wparam, lparam))
1881 break;
1882
1883 if (cmd>=ID_DRIVE_FIRST && cmd<=ID_DRIVE_FIRST+0xFF) {
1884 TCHAR drv[_MAX_DRIVE], path[MAX_PATH];
1885 ChildWnd* child;
1886 LPCTSTR root = Globals.drives;
1887 int i;
1888
1889 for(i=cmd-ID_DRIVE_FIRST; i--; root++)
1890 while(*root)
1891 root++;
1892
1893 if (activate_drive_window(root))
1894 return 0;
1895
1896 _tsplitpath(root, drv, 0, 0, 0);
1897
1898 if (!SetCurrentDirectory(drv)) {
1899 display_error(hwnd, GetLastError());
1900 return 0;
1901 }
1902
1903 GetCurrentDirectory(MAX_PATH, path); /*TODO: store last directory per drive */
1904 child = alloc_child_window(path, NULL, hwnd);
1905
1906 if (!create_child_window(child))
1907 free(child);
1908 } else switch(cmd) {
1909 case ID_FILE_EXIT:
1910 SendMessage(hwnd, WM_CLOSE, 0, 0);
1911 break;
1912
1913 case ID_WINDOW_NEW: {
1914 TCHAR path[MAX_PATH];
1915 ChildWnd* child;
1916
1917 GetCurrentDirectory(MAX_PATH, path);
1918 child = alloc_child_window(path, NULL, hwnd);
1919
1920 if (!create_child_window(child))
1921 free(child);
1922 break;}
1923
1924 case ID_WINDOW_CASCADE:
1925 SendMessage(Globals.hmdiclient, WM_MDICASCADE, 0, 0);
1926 break;
1927
1928 case ID_WINDOW_TILE_HORZ:
1929 SendMessage(Globals.hmdiclient, WM_MDITILE, MDITILE_HORIZONTAL, 0);
1930 break;
1931
1932 case ID_WINDOW_TILE_VERT:
1933 SendMessage(Globals.hmdiclient, WM_MDITILE, MDITILE_VERTICAL, 0);
1934 break;
1935
1936 case ID_WINDOW_ARRANGE:
1937 SendMessage(Globals.hmdiclient, WM_MDIICONARRANGE, 0, 0);
1938 break;
1939
1940 case ID_SELECT_FONT: {
1941 TCHAR dlg_name[BUFFER_LEN], dlg_info[BUFFER_LEN];
1942 CHOOSEFONT chFont;
1943 LOGFONT lFont;
1944
1945 HDC hdc = GetDC(hwnd);
1946 chFont.lStructSize = sizeof(CHOOSEFONT);
1947 chFont.hwndOwner = hwnd;
1948 chFont.hDC = NULL;
1949 chFont.lpLogFont = &lFont;
1950 chFont.Flags = CF_SCREENFONTS | CF_FORCEFONTEXIST | CF_LIMITSIZE | CF_NOSCRIPTSEL;
1951 chFont.rgbColors = RGB(0,0,0);
1952 chFont.lCustData = 0;
1953 chFont.lpfnHook = NULL;
1954 chFont.lpTemplateName = NULL;
1955 chFont.hInstance = Globals.hInstance;
1956 chFont.lpszStyle = NULL;
1957 chFont.nFontType = SIMULATED_FONTTYPE;
1958 chFont.nSizeMin = 0;
1959 chFont.nSizeMax = 24;
1960
1961 if (ChooseFont(&chFont)) {
1962 HWND childWnd;
1963
1964 Globals.hfont = CreateFontIndirect(&lFont);
1965 SelectFont(hdc, Globals.hfont);
1966 GetTextExtentPoint32(hdc, sSpace, 1, &Globals.spaceSize);
1967
1968 /* change font in all open child windows */
1969 for(childWnd=GetWindow(Globals.hmdiclient,GW_CHILD); childWnd; childWnd=GetNextWindow(childWnd,GW_HWNDNEXT)) {
1970 ChildWnd* child = (ChildWnd*) GetWindowLong(childWnd, GWL_USERDATA);
1971 SetWindowFont(child->left.hwnd, Globals.hfont, TRUE);
1972 SetWindowFont(child->right.hwnd, Globals.hfont, TRUE);
1973 ListBox_SetItemHeight(child->left.hwnd, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
1974 ListBox_SetItemHeight(child->right.hwnd, 1, max(Globals.spaceSize.cy,IMAGE_HEIGHT+3));
1975 InvalidateRect(child->left.hwnd, NULL, TRUE);
1976 InvalidateRect(child->right.hwnd, NULL, TRUE);
1977 }
1978 }
1979 else if (CommDlgExtendedError()) {
1980 LoadString(Globals.hInstance, IDS_FONT_SEL_DLG_NAME, dlg_name, BUFFER_LEN);
1981 LoadString(Globals.hInstance, IDS_FONT_SEL_ERROR, dlg_info, BUFFER_LEN);
1982 MessageBox(hwnd, dlg_info, dlg_name, MB_OK);
1983 }
1984
1985 ReleaseDC(hwnd, hdc);
1986 break;
1987 }
1988
1989 case ID_VIEW_TOOL_BAR:
1990 toggle_child(hwnd, cmd, Globals.htoolbar);
1991 break;
1992
1993 case ID_VIEW_DRIVE_BAR:
1994 toggle_child(hwnd, cmd, Globals.hdrivebar);
1995 break;
1996
1997 case ID_VIEW_STATUSBAR:
1998 toggle_child(hwnd, cmd, Globals.hstatusbar);
1999 break;
2000
2001 case ID_EXECUTE: {
2002 struct ExecuteDialog dlg;
2003
2004 memset(&dlg, 0, sizeof(struct ExecuteDialog));
2005
2006 if (DialogBoxParam(Globals.hInstance, MAKEINTRESOURCE(IDD_EXECUTE), hwnd, ExecuteDialogDlgProc, (LPARAM)&dlg) == IDOK) {
2007 HINSTANCE hinst = ShellExecute(hwnd, NULL/*operation*/, dlg.cmd/*file*/, NULL/*parameters*/, NULL/*dir*/, dlg.cmdshow);
2008
2009 if ((int)hinst <= 32)
2010 display_error(hwnd, GetLastError());
2011 }
2012 break;}
2013
2014 case ID_CONNECT_NETWORK_DRIVE: {
2015 DWORD ret = WNetConnectionDialog(hwnd, RESOURCETYPE_DISK);
2016 if (ret!=NO_ERROR && ret!=(DWORD)-1) {
2017 if (ret == ERROR_EXTENDED_ERROR)
2018 display_network_error(hwnd);
2019 else
2020 display_error(hwnd, ret);
2021 }
2022 break;}
2023
2024 case ID_DISCONNECT_NETWORK_DRIVE: {
2025 DWORD ret = WNetDisconnectDialog(hwnd, RESOURCETYPE_DISK);
2026 if (ret!=NO_ERROR && ret!=(DWORD)-1) {
2027 if (ret == ERROR_EXTENDED_ERROR)
2028 display_network_error(hwnd);
2029 else
2030 display_error(hwnd, ret);
2031 }
2032 break;}
2033
2034 #ifndef __MINGW32__ /* SHFormatDrive missing in MinGW (as of 13.5.2005) */
2035 case ID_FORMAT_DISK: {
2036 UINT sem_org = SetErrorMode(0); /* Get the current Error Mode settings. */
2037 SetErrorMode(sem_org & ~SEM_FAILCRITICALERRORS); /* Force O/S to handle */
2038 SHFormatDrive(hwnd, 0 /* A: */, SHFMT_ID_DEFAULT, 0);
2039 SetErrorMode(sem_org); /* Put it back the way it was. */
2040 break;}
2041 #endif
2042
2043 case ID_HELP:
2044 WinHelp(hwnd, RS(b1,IDS_WINEFILE), HELP_INDEX, 0);
2045 break;
2046
2047 #ifndef _NO_EXTENSIONS
2048 case ID_VIEW_FULLSCREEN:
2049 CheckMenuItem(Globals.hMenuOptions, cmd, toggle_fullscreen(hwnd)?MF_CHECKED:0);
2050 break;
2051
2052 #ifdef __WINE__
2053 case ID_DRIVE_UNIX_FS: {
2054 TCHAR path[MAX_PATH];
2055 #ifdef UNICODE
2056 char cpath[MAX_PATH];
2057 #endif
2058 ChildWnd* child;
2059
2060 if (activate_fs_window(RS(b1,IDS_UNIXFS)))
2061 break;
2062
2063
2064 #ifdef UNICODE
2065 call_getcwd(cpath, MAX_PATH);
2066 MultiByteToWideChar(CP_UNIXCP, 0, cpath, -1, path, MAX_PATH);
2067 #else
2068 call_getcwd(path, MAX_PATH);
2069 #endif
2070 child = alloc_child_window(path, NULL, hwnd);
2071
2072 if (!create_child_window(child))
2073 free(child);
2074 break;}
2075 #endif
2076
2077 #ifdef _SHELL_FOLDERS
2078 case ID_DRIVE_SHELL_NS: {
2079 TCHAR path[MAX_PATH];
2080 ChildWnd* child;
2081
2082 if (activate_fs_window(RS(b1,IDS_SHELL)))
2083 break;
2084
2085 GetCurrentDirectory(MAX_PATH, path);
2086 child = alloc_child_window(path, get_path_pidl(path,hwnd), hwnd);
2087
2088 if (!create_child_window(child))
2089 free(child);
2090 break;}
2091 #endif
2092 #endif
2093
2094 /*TODO: There are even more menu items! */
2095
2096 #ifndef _NO_EXTENSIONS
2097 #ifdef __WINE__
2098 case ID_LICENSE:
2099 WineLicense(Globals.hMainWnd);
2100 break;
2101
2102 case ID_NO_WARRANTY:
2103 WineWarranty(Globals.hMainWnd);
2104 break;
2105 #endif
2106
2107 case ID_ABOUT_WINE:
2108 ShellAbout(hwnd, RS(b2,IDS_WINE), RS(b1,IDS_WINEFILE), 0);
2109 break;
2110
2111 case ID_ABOUT:
2112 ShellAbout(hwnd, RS(b1,IDS_WINEFILE), NULL, 0);
2113 break;
2114 #endif /* _NO_EXTENSIONS */
2115
2116 default:
2117 /*TODO: if (wParam >= PM_FIRST_LANGUAGE && wParam <= PM_LAST_LANGUAGE)
2118 STRING_SelectLanguageByNumber(wParam - PM_FIRST_LANGUAGE);
2119 else */if ((cmd<IDW_FIRST_CHILD || cmd>=IDW_FIRST_CHILD+0x100) &&
2120 (cmd<SC_SIZE || cmd>SC_RESTORE))
2121 MessageBox(hwnd, RS(b2,IDS_NO_IMPL), RS(b1,IDS_WINEFILE), MB_OK);
2122
2123 return DefFrameProc(hwnd, Globals.hmdiclient, nmsg, wparam, lparam);
2124 }
2125 break;}
2126
2127 case WM_SIZE:
2128 resize_frame(hwnd, LOWORD(lparam), HIWORD(lparam));
2129 break; /* do not pass message to DefFrameProc */
2130
2131 #ifndef _NO_EXTENSIONS
2132 case WM_GETMINMAXINFO: {
2133 LPMINMAXINFO lpmmi = (LPMINMAXINFO)lparam;
2134
2135 lpmmi->ptMaxTrackSize.x <<= 1;/*2*GetSystemMetrics(SM_CXSCREEN) / SM_CXVIRTUALSCREEN */
2136 lpmmi->ptMaxTrackSize.y <<= 1;/*2*GetSystemMetrics(SM_CYSCREEN) / SM_CYVIRTUALSCREEN */
2137 break;}
2138
2139 case FRM_CALC_CLIENT:
2140 frame_get_clientspace(hwnd, (PRECT)lparam);
2141 return TRUE;
2142 #endif /* _NO_EXTENSIONS */
2143
2144 default:
2145 return DefFrameProc(hwnd, Globals.hmdiclient, nmsg, wparam, lparam);
2146 }
2147
2148 return 0;
2149 }
2150
2151
2152 static TCHAR g_pos_names[COLUMNS][20] = {
2153 {'\0'} /* symbol */
2154 };
2155
2156 static const int g_pos_align[] = {
2157 0,
2158 HDF_LEFT, /* Name */
2159 HDF_RIGHT, /* Size */
2160 HDF_LEFT, /* CDate */
2161 #ifndef _NO_EXTENSIONS
2162 HDF_LEFT, /* ADate */
2163 HDF_LEFT, /* MDate */
2164 HDF_LEFT, /* Index */
2165 HDF_CENTER, /* Links */
2166 #endif
2167 HDF_CENTER, /* Attributes */
2168 #ifndef _NO_EXTENSIONS
2169 HDF_LEFT /* Security */
2170 #endif
2171 };
2172
2173 static void resize_tree(ChildWnd* child, int cx, int cy)
2174 {
2175 HDWP hdwp = BeginDeferWindowPos(4);
2176 RECT rt;
2177
2178 rt.left = 0;
2179 rt.top = 0;
2180 rt.right = cx;
2181 rt.bottom = cy;
2182
2183 cx = child->split_pos + SPLIT_WIDTH/2;
2184
2185 #ifndef _NO_EXTENSIONS
2186 {
2187 WINDOWPOS wp;
2188 HD_LAYOUT hdl;
2189
2190 hdl.prc = &rt;
2191 hdl.pwpos = &wp;
2192
2193 Header_Layout(child->left.hwndHeader, &hdl);
2194
2195 DeferWindowPos(hdwp, child->left.hwndHeader, wp.hwndInsertAfter,
2196 wp.x-1, wp.y, child->split_pos-SPLIT_WIDTH/2+1, wp.cy, wp.flags);
2197 DeferWindowPos(hdwp, child->right.hwndHeader, wp.hwndInsertAfter,
2198 rt.left+cx+1, wp.y, wp.cx-cx+2, wp.cy, wp.flags);
2199 }
2200 #endif /* _NO_EXTENSIONS */
2201
2202 DeferWindowPos(hdwp, child->left.hwnd, 0, rt.left, rt.top, child->split_pos-SPLIT_WIDTH/2-rt.left, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
2203 DeferWindowPos(hdwp, child->right.hwnd, 0, rt.left+cx+1, rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
2204
2205 EndDeferWindowPos(hdwp);
2206 }
2207
2208
2209 #ifndef _NO_EXTENSIONS
2210
2211 static HWND create_header(HWND parent, Pane* pane, int id)
2212 {
2213 HD_ITEM hdi;
2214 int idx;
2215
2216 HWND hwnd = CreateWindow(WC_HEADER, 0, WS_CHILD|WS_VISIBLE|HDS_HORZ/*TODO: |HDS_BUTTONS + sort orders*/,
2217 0, 0, 0, 0, parent, (HMENU)id, Globals.hInstance, 0);
2218 if (!hwnd)
2219 return 0;
2220
2221 SetWindowFont(hwnd, GetStockObject(DEFAULT_GUI_FONT), FALSE);
2222
2223 hdi.mask = HDI_TEXT|HDI_WIDTH|HDI_FORMAT;
2224
2225 for(idx=0; idx<COLUMNS; idx++) {
2226 hdi.pszText = g_pos_names[idx];
2227 hdi.fmt = HDF_STRING | g_pos_align[idx];
2228 hdi.cxy = pane->widths[idx];
2229 Header_InsertItem(hwnd, idx, &hdi);
2230 }
2231
2232 return hwnd;
2233 }
2234
2235 #endif /* _NO_EXTENSIONS */
2236
2237
2238 static void init_output(HWND hwnd)
2239 {
2240 const static TCHAR s1000[] = {'1','0','0','0','\0'};
2241
2242 TCHAR b[16];
2243 HFONT old_font;
2244 HDC hdc = GetDC(hwnd);
2245
2246 if (GetNumberFormat(LOCALE_USER_DEFAULT, 0, s1000, 0, b, 16) > 4)
2247 Globals.num_sep = b[1];
2248 else
2249 Globals.num_sep = TEXT('.');
2250
2251 old_font = SelectFont(hdc, Globals.hfont);
2252 GetTextExtentPoint32(hdc, sSpace, 1, &Globals.spaceSize);
2253 SelectFont(hdc, old_font);
2254 ReleaseDC(hwnd, hdc);
2255 }
2256
2257 static void draw_item(Pane* pane, LPDRAWITEMSTRUCT dis, Entry* entry, int calcWidthCol);
2258
2259
2260 /* calculate preferred width for all visible columns */
2261
2262 static BOOL calc_widths(Pane* pane, BOOL anyway)
2263 {
2264 int col, x, cx, spc=3*Globals.spaceSize.cx;
2265 int entries = ListBox_GetCount(pane->hwnd);
2266 int orgWidths[COLUMNS];
2267 int orgPositions[COLUMNS+1];
2268 HFONT hfontOld;
2269 HDC hdc;
2270 int cnt;
2271
2272 if (!anyway) {
2273 memcpy(orgWidths, pane->widths, sizeof(orgWidths));
2274 memcpy(orgPositions, pane->positions, sizeof(orgPositions));
2275 }
2276
2277 for(col=0; col<COLUMNS; col++)
2278 pane->widths[col] = 0;
2279
2280 hdc = GetDC(pane->hwnd);
2281 hfontOld = SelectFont(hdc, Globals.hfont);
2282
2283 for(cnt=0; cnt<entries; cnt++) {
2284 Entry* entry = (Entry*) ListBox_GetItemData(pane->hwnd, cnt);
2285
2286 DRAWITEMSTRUCT dis;
2287
2288 dis.CtlType = 0;
2289 dis.CtlID = 0;
2290 dis.itemID = 0;
2291 dis.itemAction = 0;
2292 dis.itemState = 0;
2293 dis.hwndItem = pane->hwnd;
2294 dis.hDC = hdc;
2295 dis.rcItem.left = 0;
2296 dis.rcItem.top = 0;
2297 dis.rcItem.right = 0;
2298 dis.rcItem.bottom = 0;
2299 /*dis.itemData = 0; */
2300
2301 draw_item(pane, &dis, entry, COLUMNS);
2302 }
2303
2304 SelectObject(hdc, hfontOld);
2305 ReleaseDC(pane->hwnd, hdc);
2306
2307 x = 0;
2308 for(col=0; col<COLUMNS; col++) {
2309 pane->positions[col] = x;
2310 cx = pane->widths[col];
2311
2312 if (cx) {
2313 cx += spc;
2314
2315 if (cx < IMAGE_WIDTH)
2316 cx = IMAGE_WIDTH;
2317
2318 pane->widths[col] = cx;
2319 }
2320
2321 x += cx;
2322 }
2323
2324 pane->positions[COLUMNS] = x;
2325
2326 ListBox_SetHorizontalExtent(pane->hwnd, x);
2327
2328 /* no change? */
2329 if (!memcmp(orgWidths, pane->widths, sizeof(orgWidths)))
2330 return FALSE;
2331
2332 /* don't move, if only collapsing an entry */
2333 if (!anyway && pane->widths[0]<orgWidths[0] &&
2334 !memcmp(orgWidths+1, pane->widths+1, sizeof(orgWidths)-sizeof(int))) {
2335 pane->widths[0] = orgWidths[0];
2336 memcpy(pane->positions, orgPositions, sizeof(orgPositions));
2337
2338 return FALSE;
2339 }
2340
2341 InvalidateRect(pane->hwnd, 0, TRUE);
2342
2343 return TRUE;
2344 }
2345
2346
2347 /* calculate one preferred column width */
2348
2349 static void calc_single_width(Pane* pane, int col)
2350 {
2351 HFONT hfontOld;
2352 int x, cx;
2353 int entries = ListBox_GetCount(pane->hwnd);
2354 int cnt;
2355 HDC hdc;
2356
2357 pane->widths[col] = 0;
2358
2359 hdc = GetDC(pane->hwnd);
2360 hfontOld = SelectFont(hdc, Globals.hfont);
2361
2362 for(cnt=0; cnt<entries; cnt++) {
2363 Entry* entry = (Entry*) ListBox_GetItemData(pane->hwnd, cnt);
2364 DRAWITEMSTRUCT dis;
2365
2366 dis.CtlType = 0;
2367 dis.CtlID = 0;
2368 dis.itemID = 0;
2369 dis.itemAction = 0;
2370 dis.itemState = 0;
2371 dis.hwndItem = pane->hwnd;
2372 dis.hDC = hdc;
2373 dis.rcItem.left = 0;
2374 dis.rcItem.top = 0;
2375 dis.rcItem.right = 0;
2376 dis.rcItem.bottom = 0;
2377 /*dis.itemData = 0; */
2378
2379 draw_item(pane, &dis, entry, col);
2380 }
2381
2382 SelectObject(hdc, hfontOld);
2383 ReleaseDC(pane->hwnd, hdc);
2384
2385 cx = pane->widths[col];
2386
2387 if (cx) {
2388 cx += 3*Globals.spaceSize.cx;
2389
2390 if (cx < IMAGE_WIDTH)
2391 cx = IMAGE_WIDTH;
2392 }
2393
2394 pane->widths[col] = cx;
2395
2396 x = pane->positions[col] + cx;
2397
2398 for(; col<COLUMNS; ) {
2399 pane->positions[++col] = x;
2400 x += pane->widths[col];
2401 }
2402
2403 ListBox_SetHorizontalExtent(pane->hwnd, x);
2404 }
2405
2406
2407 /* insert listbox entries after index idx */
2408
2409 static int insert_entries(Pane* pane, Entry* dir, int idx)
2410 {
2411 Entry* entry = dir;
2412
2413 if (!entry)
2414 return idx;
2415
2416 ShowWindow(pane->hwnd, SW_HIDE);
2417
2418 for(; entry; entry=entry->next) {
2419 #ifndef _LEFT_FILES
2420 if (pane->treePane && !(entry->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
2421 continue;
2422 #endif
2423
2424 /* don't display entries "." and ".." in the left pane */
2425 if (pane->treePane && (entry->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
2426 && entry->data.cFileName[0]==TEXT('.'))
2427 if (
2428 #ifndef _NO_EXTENSIONS
2429 entry->data.cFileName[1]==TEXT('\0') ||
2430 #endif
2431 (entry->data.cFileName[1]==TEXT('.') && entry->data.cFileName[2]==TEXT('\0')))
2432 continue;
2433
2434 if (idx != -1)
2435 idx++;
2436
2437 ListBox_InsertItemData(pane->hwnd, idx, entry);
2438
2439 if (pane->treePane && entry->expanded)
2440 idx = insert_entries(pane, entry->down, idx);
2441 }
2442
2443 ShowWindow(pane->hwnd, SW_SHOW);
2444
2445 return idx;
2446 }
2447
2448
2449 static void format_bytes(LPTSTR buffer, LONGLONG bytes)
2450 {
2451 const static TCHAR sFmtGB[] = {'%', '.', '1', 'f', ' ', 'G', 'B', '\0'};
2452 const static TCHAR sFmtMB[] = {'%', '.', '1', 'f', ' ', 'M', 'B', '\0'};
2453 const static TCHAR sFmtkB[] = {'%', '.', '1', 'f', ' ', 'k', 'B', '\0'};
2454
2455 float fBytes = (float)bytes;
2456
2457 if (bytes >= 1073741824) /* 1 GB */
2458 _stprintf(buffer, sFmtGB, fBytes/1073741824.f+.5f);
2459 else if (bytes >= 1048576) /* 1 MB */
2460 _stprintf(buffer, sFmtMB, fBytes/1048576.f+.5f);
2461 else if (bytes >= 1024) /* 1 kB */
2462 _stprintf(buffer, sFmtkB, fBytes/1024.f+.5f);
2463 else
2464 _stprintf(buffer, sLongNumFmt, bytes);
2465 }
2466
2467 static void set_space_status()
2468 {
2469 ULARGE_INTEGER ulFreeBytesToCaller, ulTotalBytes, ulFreeBytes;
2470 TCHAR fmt[64], b1[64], b2[64], buffer[BUFFER_LEN];
2471
2472 if (GetDiskFreeSpaceEx(NULL, &ulFreeBytesToCaller, &ulTotalBytes, &ulFreeBytes)) {
2473 format_bytes(b1, ulFreeBytesToCaller.QuadPart);
2474 format_bytes(b2, ulTotalBytes.QuadPart);
2475 _stprintf(buffer, RS(fmt,IDS_FREE_SPACE_FMT), b1, b2);
2476 } else
2477 _tcscpy(buffer, sQMarks);
2478
2479 SendMessage(Globals.hstatusbar, SB_SETTEXT, 0, (LPARAM)buffer);
2480 }
2481
2482
2483 static WNDPROC g_orgTreeWndProc;
2484
2485 static void create_tree_window(HWND parent, Pane* pane, int id, int id_header)
2486 {
2487 const static TCHAR sListBox[] = {'L','i','s','t','B','o','x','\0'};
2488
2489 static int s_init = 0;
2490 Entry* entry = pane->root;
2491
2492 pane->hwnd = CreateWindow(sListBox, sEmpty, WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|
2493 LBS_DISABLENOSCROLL|LBS_NOINTEGRALHEIGHT|LBS_OWNERDRAWFIXED|LBS_NOTIFY,
2494 0, 0, 0, 0, parent, (HMENU)id, Globals.hInstance, 0);
2495
2496 SetWindowLong(pane->hwnd, GWL_USERDATA, (LPARAM)pane);
2497 g_orgTreeWndProc = SubclassWindow(pane->hwnd, TreeWndProc);
2498
2499 SetWindowFont(pane->hwnd, Globals.hfont, FALSE);
2500
2501 /* insert entries into listbox */
2502 if (entry)
2503 insert_entries(pane, entry, -1);
2504
2505 /* calculate column widths */
2506 if (!s_init) {
2507 s_init = 1;
2508 init_output(pane->hwnd);
2509 }
2510
2511 calc_widths(pane, TRUE);
2512
2513 #ifndef _NO_EXTENSIONS
2514 pane->hwndHeader = create_header(parent, pane, id_header);
2515 #endif
2516 }
2517
2518
2519 static void InitChildWindow(ChildWnd* child)
2520 {
2521 create_tree_window(child->hwnd, &child->left, IDW_TREE_LEFT, IDW_HEADER_LEFT);
2522 create_tree_window(child->hwnd, &child->right, IDW_TREE_RIGHT, IDW_HEADER_RIGHT);
2523 }
2524
2525
2526 static void format_date(const FILETIME* ft, TCHAR* buffer, int visible_cols)
2527 {
2528 SYSTEMTIME systime;
2529 FILETIME lft;
2530 int len = 0;
2531
2532 *buffer = TEXT('\0');
2533
2534 if (!ft->dwLowDateTime && !ft->dwHighDateTime)
2535 return;
2536
2537 if (!FileTimeToLocalFileTime(ft, &lft))
2538 {err: _tcscpy(buffer,sQMarks); return;}
2539
2540 if (!FileTimeToSystemTime(&lft, &systime))
2541 goto err;
2542
2543 if (visible_cols & COL_DATE) {
2544 len = GetDateFormat(LOCALE_USER_DEFAULT, 0, &systime, 0, buffer, BUFFER_LEN);
2545 if (!len)
2546 goto err;
2547 }
2548
2549 if (visible_cols & COL_TIME) {
2550 if (len)
2551 buffer[len-1] = ' ';
2552
2553 buffer[len++] = ' ';
2554
2555 if (!GetTimeFormat(LOCALE_USER_DEFAULT, 0, &systime, 0, buffer+len, BUFFER_LEN-len))
2556 buffer[len] = TEXT('\0');
2557 }
2558 }
2559
2560
2561 static void calc_width(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str)
2562 {
2563 RECT rt = {0, 0, 0, 0};
2564
2565 DrawText(dis->hDC, (LPTSTR)str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX);
2566
2567 if (rt.right > pane->widths[col])
2568 pane->widths[col] = rt.right;
2569 }
2570
2571 static void calc_tabbed_width(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str)
2572 {
2573 RECT rt = {0, 0, 0, 0};
2574
2575 /* DRAWTEXTPARAMS dtp = {sizeof(DRAWTEXTPARAMS), 2};
2576 DrawTextEx(dis->hDC, (LPTSTR)str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX|DT_EXPANDTABS|DT_TABSTOP, &dtp);*/
2577
2578 DrawText(dis->hDC, (LPTSTR)str, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_EXPANDTABS|DT_TABSTOP|(2<<8));
2579 /*FIXME rt (0,0) ??? */
2580
2581 if (rt.right > pane->widths[col])
2582 pane->widths[col] = rt.right;
2583 }
2584
2585
2586 static void output_text(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str, DWORD flags)
2587 {
2588 int x = dis->rcItem.left;
2589 RECT rt;
2590
2591 rt.left = x+pane->positions[col]+Globals.spaceSize.cx;
2592 rt.top = dis->rcItem.top;
2593 rt.right = x+pane->positions[col+1]-Globals.spaceSize.cx;
2594 rt.bottom = dis->rcItem.bottom;
2595
2596 DrawText(dis->hDC, (LPTSTR)str, -1, &rt, DT_SINGLELINE|DT_NOPREFIX|flags);
2597 }
2598
2599 static void output_tabbed_text(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str)
2600 {
2601 int x = dis->rcItem.left;
2602 RECT rt;
2603
2604 rt.left = x+pane->positions[col]+Globals.spaceSize.cx;
2605 rt.top = dis->rcItem.top;
2606 rt.right = x+pane->positions[col+1]-Globals.spaceSize.cx;
2607 rt.bottom = dis->rcItem.bottom;
2608
2609 /* DRAWTEXTPARAMS dtp = {sizeof(DRAWTEXTPARAMS), 2};
2610 DrawTextEx(dis->hDC, (LPTSTR)str, -1, &rt, DT_SINGLELINE|DT_NOPREFIX|DT_EXPANDTABS|DT_TABSTOP, &dtp);*/
2611
2612 DrawText(dis->hDC, (LPTSTR)str, -1, &rt, DT_SINGLELINE|DT_EXPANDTABS|DT_TABSTOP|(2<<8));
2613 }
2614
2615 static void output_number(Pane* pane, LPDRAWITEMSTRUCT dis, int col, LPCTSTR str)
2616 {
2617 int x = dis->rcItem.left;
2618 RECT rt;
2619 LPCTSTR s = str;
2620 TCHAR b[128];
2621 LPTSTR d = b;
2622 int pos;
2623
2624 rt.left = x+pane->positions[col]+Globals.spaceSize.cx;
2625 rt.top = dis->rcItem.top;
2626 rt.right = x+pane->positions[col+1]-Globals.spaceSize.cx;
2627 rt.bottom = dis->rcItem.bottom;
2628
2629 if (*s)
2630 *d++ = *s++;
2631
2632 /* insert number separator characters */
2633 pos = lstrlen(s) % 3;
2634
2635 while(*s)
2636 if (pos--)
2637 *d++ = *s++;
2638 else {
2639 *d++ = Globals.num_sep;
2640 pos = 3;
2641 }
2642
2643 DrawText(dis->hDC, b, d-b, &rt, DT_RIGHT|DT_SINGLELINE|DT_NOPREFIX|DT_END_ELLIPSIS);
2644 }
2645
2646
2647 static int is_exe_file(LPCTSTR ext)
2648 {
2649 static const TCHAR executable_extensions[][4] = {
2650 {'C','O','M','\0'},
2651 {'E','X','E','\0'},
2652 {'B','A','T','\0'},
2653 {'C','M','D','\0'},
2654 #ifndef _NO_EXTENSIONS
2655 {'C','M','M','\0'},
2656 {'B','T','M','\0'},
2657 {'A','W','K','\0'},
2658 #endif /* _NO_EXTENSIONS */
2659 {'\0'}
2660 };
2661
2662 TCHAR ext_buffer[_MAX_EXT];
2663 const TCHAR (*p)[4];
2664 LPCTSTR s;
2665 LPTSTR d;
2666
2667 for(s=ext+1,d=ext_buffer; (*d=tolower(*s)); s++)
2668 d++;
2669
2670 for(p=executable_extensions; (*p)[0]; p++)
2671 if (!_tcscmp(ext_buffer, *p))
2672 return 1;
2673
2674 return 0;
2675 }
2676
2677 static int is_registered_type(LPCTSTR ext)
2678 {
2679 /* TODO */
2680
2681 return 1;
2682 }
2683
2684
2685 static void draw_item(Pane* pane, LPDRAWITEMSTRUCT dis, Entry* entry, int calcWidthCol)
2686 {
2687 TCHAR buffer[BUFFER_LEN];
2688 DWORD attrs;
2689 int visible_cols = pane->visible_cols;
2690 COLORREF bkcolor, textcolor;
2691 RECT focusRect = dis->rcItem;
2692 HBRUSH hbrush;
2693 enum IMAGE img;
2694 int img_pos, cx;
2695 int col = 0;
2696
2697 if (entry) {
2698 attrs = entry->data.dwFileAttributes;
2699
2700 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
2701 if (entry->data.cFileName[0]==TEXT('.') && entry->data.cFileName[1]==TEXT('.')
2702 && entry->data.cFileName[2]==TEXT('\0'))
2703 img = IMG_FOLDER_UP;
2704 #ifndef _NO_EXTENSIONS
2705 else if (entry->data.cFileName[0]==TEXT('.') && entry->data.cFileName[1]==TEXT('\0'))
2706 img = IMG_FOLDER_CUR;
2707 #endif
2708 else if (
2709 #ifdef _NO_EXTENSIONS
2710 entry->expanded ||
2711 #endif
2712 (pane->treePane && (dis->itemState&ODS_FOCUS)))
2713 img = IMG_OPEN_FOLDER;
2714 else
2715 img = IMG_FOLDER;
2716 } else {
2717 LPCTSTR ext = _tcsrchr(entry->data.cFileName, '.');
2718 if (!ext)
2719 ext = sEmpty;
2720
2721 if (is_exe_file(ext))
2722 img = IMG_EXECUTABLE;
2723 else if (is_registered_type(ext))
2724 img = IMG_DOCUMENT;
2725 else
2726 img = IMG_FILE;
2727 }
2728 } else {
2729 attrs = 0;
2730 img = IMG_NONE;
2731 }
2732
2733 if (pane->treePane) {
2734 if (entry) {
2735 img_pos = dis->rcItem.left + entry->level*(IMAGE_WIDTH+TREE_LINE_DX);
2736
2737 if (calcWidthCol == -1) {
2738 int x;
2739 int y = dis->rcItem.top + IMAGE_HEIGHT/2;
2740 Entry* up;
2741 RECT rt_clip;
2742 HRGN hrgn_org = CreateRectRgn(0, 0, 0, 0);
2743 HRGN hrgn;
2744
2745 rt_clip.left = dis->rcItem.left;
2746 rt_clip.top = dis->rcItem.top;
2747 rt_clip.right = dis->rcItem.left+pane->widths[col];
2748 rt_clip.bottom = dis->rcItem.bottom;
2749
2750 hrgn = CreateRectRgnIndirect(&rt_clip);
2751
2752 if (!GetClipRgn(dis->hDC, hrgn_org)) {
2753 DeleteObject(hrgn_org);
2754 hrgn_org = 0;
2755 }
2756
2757 /* HGDIOBJ holdPen = SelectObject(dis->hDC, GetStockObject(BLACK_PEN)); */
2758 ExtSelectClipRgn(dis->hDC, hrgn, RGN_AND);
2759 DeleteObject(hrgn);
2760
2761 if ((up=entry->up) != NULL) {
2762 MoveToEx(dis->hDC, img_pos-IMAGE_WIDTH/2, y, 0);
2763 LineTo(dis->hDC, img_pos-2, y);
2764
2765 x = img_pos - IMAGE_WIDTH/2;
2766
2767 do {
2768 x -= IMAGE_WIDTH+TREE_LINE_DX;
2769
2770 if (up->next
2771 #ifndef _LEFT_FILES
2772 && (up->next->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2773 #endif
2774 ) {
2775 MoveToEx(dis->hDC, x, dis->rcItem.top, 0);
2776 LineTo(dis->hDC, x, dis->rcItem.bottom);
2777 }
2778 } while((up=up->up) != NULL);
2779 }
2780
2781 x = img_pos - IMAGE_WIDTH/2;
2782
2783 MoveToEx(dis->hDC, x, dis->rcItem.top, 0);
2784 LineTo(dis->hDC, x, y);
2785
2786 if (entry->next
2787 #ifndef _LEFT_FILES
2788 && (entry->next->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
2789 #endif
2790 )
2791 LineTo(dis->hDC, x, dis->rcItem.bottom);
2792 /*@@
2793 if (entry->down && entry->expanded) {
2794 x += IMAGE_WIDTH+TREE_LINE_DX;
2795 MoveToEx(dis->hDC, x, dis->rcItem.top+IMAGE_HEIGHT, 0);
2796 LineTo(dis->hDC, x, dis->rcItem.bottom);
2797 }
2798 */
2799 SelectClipRgn(dis->hDC, hrgn_org);
2800 if (hrgn_org) DeleteObject(hrgn_org);
2801 /* SelectObject(dis->hDC, holdPen); */
2802 } else if (calcWidthCol==col || calcWidthCol==COLUMNS) {
2803 int right = img_pos + IMAGE_WIDTH - TREE_LINE_DX;
2804
2805 if (right > pane->widths[col])
2806 pane->widths[col] = right;
2807 }
2808 } else {
2809 img_pos = dis->rcItem.left;
2810 }
2811 } else {
2812 img_pos = dis->rcItem.left;
2813
2814 if (calcWidthCol==col || calcWidthCol==COLUMNS)
2815 pane->widths[col] = IMAGE_WIDTH;
2816 }
2817
2818 if (calcWidthCol == -1) {
2819 focusRect.left = img_pos - 2;
2820
2821 #ifdef _NO_EXTENSIONS
2822 if (pane->treePane && entry) {
2823 RECT rt = {0};
2824
2825 DrawText(dis->hDC, entry->data.cFileName, -1, &rt, DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX);
2826
2827 focusRect.right = dis->rcItem.left+pane->positions[col+1]+TREE_LINE_DX + rt.right +2;
2828 }
2829 #else
2830
2831 if (attrs & FILE_ATTRIBUTE_COMPRESSED)
2832 textcolor = COLOR_COMPRESSED;
2833 else
2834 #endif /* _NO_EXTENSIONS */
2835 textcolor = RGB(0,0,0);
2836
2837 if (dis->itemState & ODS_FOCUS) {
2838 textcolor = RGB(255,255,255);
2839 bkcolor = COLOR_SELECTION;
2840 } else {
2841 bkcolor = RGB(255,255,255);
2842 }
2843
2844 hbrush = CreateSolidBrush(bkcolor);
2845 FillRect(dis->hDC, &focusRect, hbrush);
2846 DeleteObject(hbrush);
2847
2848 SetBkMode(dis->hDC, TRANSPARENT);
2849 SetTextColor(dis->hDC, textcolor);
2850
2851 cx = pane->widths[col];
2852
2853 if (cx && img!=IMG_NONE) {
2854 if (cx > IMAGE_WIDTH)
2855 cx = IMAGE_WIDTH;
2856
2857 #ifdef _SHELL_FOLDERS
2858 if (entry->hicon && entry->hicon!=(HICON)-1)
2859 DrawIconEx(dis->hDC, img_pos, dis->rcItem.top, entry->hicon, cx, GetSystemMetrics(SM_CYSMICON), 0, 0, DI_NORMAL);
2860 else
2861 #endif
2862 ImageList_DrawEx(Globals.himl, img, dis->hDC,
2863 img_pos, dis->rcItem.top, cx,
2864 IMAGE_HEIGHT, bkcolor, CLR_DEFAULT, ILD_NORMAL);
2865 }
2866 }
2867
2868 if (!entry)
2869 return;
2870
2871 #ifdef _NO_EXTENSIONS
2872 if (img >= IMG_FOLDER_UP)
2873 return;
2874 #endif
2875
2876 col++;
2877
2878 /* ouput file name */
2879 if (calcWidthCol == -1)
2880 output_text(pane, dis, col, entry->data.cFileName, 0);
2881 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
2882 calc_width(pane, dis, col, entry->data.cFileName);
2883
2884 col++;
2885
2886 #ifdef _NO_EXTENSIONS
2887 if (!pane->treePane) {
2888 #endif
2889
2890 /* display file size */
2891 if (visible_cols & COL_SIZE) {
2892 #ifdef _NO_EXTENSIONS
2893 if (!(attrs&FILE_ATTRIBUTE_DIRECTORY))
2894 #endif
2895 {
2896 ULONGLONG size;
2897
2898 size = ((ULONGLONG)entry->data.nFileSizeHigh << 32) | entry->data.nFileSizeLow;
2899
2900 _stprintf(buffer, sLongNumFmt, size);
2901
2902 if (calcWidthCol == -1)
2903 output_number(pane, dis, col, buffer);
2904 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
2905 calc_width(pane, dis, col, buffer);/*TODO: not in every case time enough */
2906 }
2907
2908 col++;
2909 }
2910
2911 /* display file date */
2912 if (visible_cols & (COL_DATE|COL_TIME)) {
2913 #ifndef _NO_EXTENSIONS
2914 format_date(&entry->data.ftCreationTime, buffer, visible_cols);
2915 if (calcWidthCol == -1)
2916 output_text(pane, dis, col, buffer, 0);
2917 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
2918 calc_width(pane, dis, col, buffer);
2919 col++;
2920
2921 format_date(&entry->data.ftLastAccessTime, buffer, visible_cols);
2922 if (calcWidthCol == -1)
2923 output_text(pane, dis, col, buffer, 0);
2924 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
2925 calc_width(pane, dis, col, buffer);
2926 col++;
2927 #endif /* _NO_EXTENSIONS */
2928
2929 format_date(&entry->data.ftLastWriteTime, buffer, visible_cols);
2930 if (calcWidthCol == -1)
2931 output_text(pane, dis, col, buffer, 0);
2932 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
2933 calc_width(pane, dis, col, buffer);
2934 col++;
2935 }
2936
2937 #ifndef _NO_EXTENSIONS
2938 if (entry->bhfi_valid) {
2939 ULONGLONG index = ((ULONGLONG)entry->bhfi.nFileIndexHigh << 32) | entry->bhfi.nFileIndexLow;
2940
2941 if (visible_cols & COL_INDEX) {
2942 _stprintf(buffer, sLongHexFmt, index);
2943
2944 if (calcWidthCol == -1)
2945 output_text(pane, dis, col, buffer, DT_RIGHT);
2946 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
2947 calc_width(pane, dis, col, buffer);
2948
2949 col++;
2950 }
2951
2952 if (visible_cols & COL_LINKS) {
2953 wsprintf(buffer, sNumFmt, entry->bhfi.nNumberOfLinks);
2954
2955 if (calcWidthCol == -1)
2956 output_text(pane, dis, col, buffer, DT_CENTER);
2957 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
2958 calc_width(pane, dis, col, buffer);
2959
2960 col++;
2961 }
2962 } else
2963 col += 2;
2964 #endif /* _NO_EXTENSIONS */
2965
2966 /* show file attributes */
2967 if (visible_cols & COL_ATTRIBUTES) {
2968 #ifdef _NO_EXTENSIONS
2969 const static TCHAR s4Tabs[] = {' ','\t',' ','\t',' ','\t',' ','\t',' ','\0'};
2970 _tcscpy(buffer, s4Tabs);
2971 #else
2972 const static TCHAR s11Tabs[] = {' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\t',' ','\0'};
2973 _tcscpy(buffer, s11Tabs);
2974 #endif
2975
2976 if (attrs & FILE_ATTRIBUTE_NORMAL) buffer[ 0] = 'N';
2977 else {
2978 if (attrs & FILE_ATTRIBUTE_READONLY) buffer[ 2] = 'R';
2979 if (attrs & FILE_ATTRIBUTE_HIDDEN) buffer[ 4] = 'H';
2980 if (attrs & FILE_ATTRIBUTE_SYSTEM) buffer[ 6] = 'S';
2981 if (attrs & FILE_ATTRIBUTE_ARCHIVE) buffer[ 8] = 'A';
2982 if (attrs & FILE_ATTRIBUTE_COMPRESSED) buffer[10] = 'C';
2983 #ifndef _NO_EXTENSIONS
2984 if (attrs & FILE_ATTRIBUTE_DIRECTORY) buffer[12] = 'D';
2985 if (attrs & FILE_ATTRIBUTE_ENCRYPTED) buffer[14] = 'E';
2986 if (attrs & FILE_ATTRIBUTE_TEMPORARY) buffer[16] = 'T';
2987 if (attrs & FILE_ATTRIBUTE_SPARSE_FILE) buffer[18] = 'P';
2988 if (attrs & FILE_ATTRIBUTE_REPARSE_POINT) buffer[20] = 'Q';
2989 if (attrs & FILE_ATTRIBUTE_OFFLINE) buffer[22] = 'O';
2990 if (attrs & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) buffer[24] = 'X';
2991 #endif /* _NO_EXTENSIONS */
2992 }
2993
2994 if (calcWidthCol == -1)
2995 output_tabbed_text(pane, dis, col, buffer);
2996 else if (calcWidthCol==col || calcWidthCol==COLUMNS)
2997 calc_tabbed_width(pane, dis, col, buffer);
2998
2999 col++;
3000 }
3001
3002 /*TODO
3003 if (flags.security) {
3004 const static TCHAR sSecTabs[] = {
3005 ' ','\t',' ','\t',' ','\t',' ',
3006 ' ','\t',' ',
3007 ' ','\t',' ','\t',' ','\t',' ',
3008 ' ','\t',' ',
3009 ' ','\t',' ','\t',' ','\t',' ',
3010 '\0'
3011 };
3012
3013 DWORD rights = get_access_mask();
3014
3015 tcscpy(buffer, sSecTabs);
3016
3017 if (rights & FILE_READ_DATA) buffer[ 0] = 'R';
3018 if (rights & FILE_WRITE_DATA) buffer[ 2] = 'W';
3019 if (rights & FILE_APPEND_DATA) buffer[ 4] = 'A';
3020 if (rights & FILE_READ_EA) {buffer[6] = 'entry'; buffer[ 7] = 'R';}
3021 if (rights & FILE_WRITE_EA) {buffer[9] = 'entry'; buffer[10] = 'W';}
3022 if (rights & FILE_EXECUTE) buffer[12] = 'X';
3023 if (rights & FILE_DELETE_CHILD) buffer[14] = 'D';
3024 if (rights & FILE_READ_ATTRIBUTES) {buffer[16] = 'a'; buffer[17] = 'R';}
3025 if (rights & FILE_WRITE_ATTRIBUTES) {buffer[19] = 'a'; buffer[20] = 'W';}
3026 if (rights & WRITE_DAC) buffer[22] = 'C';
3027 if (rights & WRITE_OWNER) buffer[24] = 'O';
3028 if (rights & SYNCHRONIZE) buffer[26] = 'S';
3029
3030 output_text(dis, col++, buffer, DT_LEFT, 3, psize);
3031 }
3032
3033 if (flags.description) {
3034 get_description(buffer);
3035 output_text(dis, col++, buffer, 0, psize);
3036 }
3037 */
3038
3039 #ifdef _NO_EXTENSIONS
3040 }
3041
3042 /* draw focus frame */
3043 if ((dis->itemState&ODS_FOCUS) && calcWidthCol==-1) {
3044 /* Currently [04/2000] Wine neither behaves exactly the same */
3045 /* way as WIN 95 nor like Windows NT... */
3046 HGDIOBJ lastBrush;
3047 HPEN lastPen;
3048 HPEN hpen;
3049
3050 if (!(GetVersion() & 0x80000000)) { /* Windows NT? */
3051 LOGBRUSH lb = {PS_SOLID, RGB(255,255,255)};
3052 hpen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, 0);
3053 } else
3054 hpen = CreatePen(PS_DOT, 0, RGB(255,255,255));
3055
3056 lastPen = SelectPen(dis->hDC, hpen);
3057 lastBrush = SelectObject(dis->hDC, GetStockObject(HOLLOW_BRUSH));
3058 SetROP2(dis->hDC, R2_XORPEN);
3059 Rectangle(dis->hDC, focusRect.left, focusRect.top, focusRect.right, focusRect.bottom);
3060 SelectObject(dis->hDC, lastBrush);
3061 SelectObject(dis->hDC, lastPen);
3062 DeleteObject(hpen);
3063 }
3064 #endif /* _NO_EXTENSIONS */
3065 }
3066
3067
3068 #ifdef _NO_EXTENSIONS
3069
3070 static void draw_splitbar(HWND hwnd, int x)
3071 {
3072 RECT rt;
3073 HDC hdc = GetDC(hwnd);
3074
3075 GetClientRect(hwnd, &rt);
3076
3077 rt.left = x - SPLIT_WIDTH/2;
3078 rt.right = x + SPLIT_WIDTH/2+1;
3079
3080 InvertRect(hdc, &rt);
3081
3082 ReleaseDC(hwnd, hdc);
3083 }
3084
3085 #endif /* _NO_EXTENSIONS */
3086
3087
3088 #ifndef _NO_EXTENSIONS
3089
3090 static void set_header(Pane* pane)
3091 {
3092 HD_ITEM item;
3093 int scroll_pos = GetScrollPos(pane->hwnd, SB_HORZ);
3094 int i=0, x=0;
3095
3096 item.mask = HDI_WIDTH;
3097 item.cxy = 0;
3098
3099 for(; x+pane->widths[i]<scroll_pos && i<COLUMNS; i++) {
3100 x += pane->widths[i];
3101 Header_SetItem(pane->hwndHeader, i, &item);
3102 }
3103
3104 if (i < COLUMNS) {
3105 x += pane->widths[i];
3106 item.cxy = x - scroll_pos;
3107 Header_SetItem(pane->hwndHeader, i++, &item);
3108
3109 for(; i<COLUMNS; i++) {
3110 item.cxy = pane->widths[i];
3111 x += pane->widths[i];
3112 Header_SetItem(pane->hwndHeader, i, &item);
3113 }
3114 }
3115 }
3116
3117 static LRESULT pane_notify(Pane* pane, NMHDR* pnmh)
3118 {
3119 switch(pnmh->code) {
3120 case HDN_TRACK:
3121 case HDN_ENDTRACK: {
3122 HD_NOTIFY* phdn = (HD_NOTIFY*) pnmh;
3123 int idx = phdn->iItem;
3124 int dx = phdn->pitem->cxy - pane->widths[idx];
3125 int i;
3126
3127 RECT clnt;
3128 GetClientRect(pane->hwnd, &clnt);
3129
3130 /* move immediate to simulate HDS_FULLDRAG (for now [04/2000] not really needed with WINELIB) */
3131 Header_SetItem(pane->hwndHeader, idx, phdn->pitem);
3132
3133 pane->widths[idx] += dx;
3134
3135 for(i=idx; ++i<=COLUMNS; )
3136 pane->positions[i] += dx;
3137
3138 {
3139 int scroll_pos = GetScrollPos(pane->hwnd, SB_HORZ);
3140 RECT rt_scr;
3141 RECT rt_clip;
3142
3143 rt_scr.left = pane->positions[idx+1]-scroll_pos;
3144 rt_scr.top = 0;
3145 rt_scr.right = clnt.right;
3146 rt_scr.bottom = clnt.bottom;
3147
3148 rt_clip.left = pane->positions[idx]-scroll_pos;
3149 rt_clip.top = 0;
3150 rt_clip.right = clnt.right;
3151 rt_clip.bottom = clnt.bottom;
3152
3153 if (rt_scr.left < 0) rt_scr.left = 0;
3154 if (rt_clip.left < 0) rt_clip.left = 0;
3155
3156 ScrollWindowEx(pane->hwnd, dx, 0, &rt_scr, &rt_clip, 0, 0, SW_INVALIDATE);
3157
3158 rt_clip.right = pane->positions[idx+1];
3159 RedrawWindow(pane->hwnd, &rt_clip, 0, RDW_INVALIDATE|RDW_UPDATENOW);
3160
3161 if (pnmh->code == HDN_ENDTRACK) {
3162 ListBox_SetHorizontalExtent(pane->hwnd, pane->positions[COLUMNS]);
3163
3164 if (GetScrollPos(pane->hwnd, SB_HORZ) != scroll_pos)
3165 set_header(pane);
3166 }
3167 }
3168
3169 return FALSE;
3170 }
3171
3172 case HDN_DIVIDERDBLCLICK: {
3173 HD_NOTIFY* phdn = (HD_NOTIFY*) pnmh;
3174 HD_ITEM item;
3175
3176 calc_single_width(pane, phdn->iItem);
3177 item.mask = HDI_WIDTH;
3178 item.cxy = pane->widths[phdn->iItem];
3179
3180 Header_SetItem(pane->hwndHeader, phdn->iItem, &item);
3181 InvalidateRect(pane->hwnd, 0, TRUE);
3182 break;}
3183 }
3184
3185 return 0;
3186 }
3187
3188 #endif /* _NO_EXTENSIONS */
3189
3190
3191 static void scan_entry(ChildWnd* child, Entry* entry, HWND hwnd)
3192 {
3193 TCHAR path[MAX_PATH];
3194 int idx = ListBox_GetCurSel(child->left.hwnd);
3195 HCURSOR old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
3196
3197 /* delete sub entries in left pane */
3198 for(;;) {
3199 LRESULT res = ListBox_GetItemData(child->left.hwnd, idx+1);
3200 Entry* sub = (Entry*) res;
3201
3202 if (res==LB_ERR || !sub || sub->level<=entry->level)
3203 break;
3204
3205 ListBox_DeleteString(child->left.hwnd, idx+1);
3206 }
3207
3208 /* empty right pane */
3209 ListBox_ResetContent(child->right.hwnd);
3210
3211 /* release memory */
3212 free_entries(entry);
3213
3214 /* read contents from disk */
3215 #ifdef _SHELL_FOLDERS
3216 if (entry->etype == ET_SHELL)
3217 {
3218 read_directory(entry, NULL, child->sortOrder, hwnd);
3219 }
3220 else
3221 #endif
3222 {
3223 get_path(entry, path);
3224 read_directory(entry, path, child->sortOrder, hwnd);
3225 }
3226
3227 /* insert found entries in right pane */
3228 insert_entries(&child->right, entry->down, -1);
3229 calc_widths(&child->right, FALSE);
3230 #ifndef _NO_EXTENSIONS
3231 set_header(&child->right);
3232 #endif
3233
3234 child->header_wdths_ok = FALSE;
3235
3236 SetCursor(old_cursor);
3237 }
3238
3239
3240 /* expand a directory entry */
3241
3242 static BOOL expand_entry(ChildWnd* child, Entry* dir)
3243 {
3244 int idx;
3245 Entry* p;
3246
3247 if (!dir || dir->expanded || !dir->down)
3248 return FALSE;
3249
3250 p = dir->down;
3251
3252 if (p->data.cFileName[0]=='.' && p->data.cFileName[1]=='\0' && p->next) {
3253 p = p->next;
3254
3255 if (p->data.cFileName[0]=='.' && p->data.cFileName[1]=='.' &&
3256 p->data.cFileName[2]=='\0' && p->next)
3257 p = p->next;
3258 }
3259
3260 /* no subdirectories ? */
3261 if (!(p->data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
3262 return FALSE;
3263
3264 idx = ListBox_FindItemData(child->left.hwnd, 0, dir);
3265
3266 dir->expanded = TRUE;
3267
3268 /* insert entries in left pane */
3269 insert_entries(&child->left, p, idx);
3270
3271 if (!child->header_wdths_ok) {
3272 if (calc_widths(&child->left, FALSE)) {
3273 #ifndef _NO_EXTENSIONS
3274 set_header(&child->left);
3275 #endif
3276
3277 child->header_wdths_ok = TRUE;
3278 }
3279 }
3280
3281 return TRUE;
3282 }
3283
3284
3285 static void collapse_entry(Pane* pane, Entry* dir)
3286 {
3287 int idx = ListBox_FindItemData(pane->hwnd, 0, dir);
3288
3289 ShowWindow(pane->hwnd, SW_HIDE);
3290
3291 /* hide sub entries */
3292 for(;;) {
3293 LRESULT res = ListBox_GetItemData(pane->hwnd, idx+1);
3294 Entry* sub = (Entry*) res;
3295
3296 if (res==LB_ERR || !sub || sub->level<=dir->level)
3297 break;
3298
3299 ListBox_DeleteString(pane->hwnd, idx+1);
3300 }
3301
3302 dir->expanded = FALSE;
3303
3304 ShowWindow(pane->hwnd, SW_SHOW);
3305 }
3306
3307
3308 static void set_curdir(ChildWnd* child, Entry* entry, HWND hwnd)
3309 {
3310 TCHAR path[MAX_PATH];
3311
3312 path[0] = '\0';
3313
3314 child->left.cur = entry;
3315 child->right.root = entry->down? entry->down: entry;
3316 child->right.cur = entry;
3317
3318 if (!entry->scanned)
3319 scan_entry(child, entry, hwnd);
3320 else {
3321 ListBox_ResetContent(child->right.hwnd);
3322 insert_entries(&child->right, entry->down, -1);
3323 calc_widths(&child->right, FALSE);
3324 #ifndef _NO_EXTENSIONS
3325 set_header(&child->right);
3326 #endif
3327 }
3328
3329 get_path(entry, path);
3330 lstrcpy(child->path, path);
3331
3332 if (child->hwnd) /* only change window title, if the window already exists */
3333 SetWindowText(child->hwnd, path);
3334
3335 if (path[0])
3336 if (SetCurrentDirectory(path))
3337 set_space_status();
3338 }
3339
3340
3341 BOOL launch_file(HWND hwnd, LPCTSTR cmd, UINT nCmdShow)
3342 {
3343 HINSTANCE hinst = ShellExecute(hwnd, NULL/*operation*/, cmd, NULL/*parameters*/, NULL/*dir*/, nCmdShow);
3344
3345 if ((int)hinst <= 32) {
3346 display_error(hwnd, GetLastError());
3347 return FALSE;
3348 }
3349
3350 return TRUE;
3351 }
3352
3353 #ifdef UNICODE
3354 BOOL launch_fileA(HWND hwnd, LPSTR cmd, UINT nCmdShow)
3355 {
3356 HINSTANCE hinst = ShellExecuteA(hwnd, NULL/*operation*/, cmd, NULL/*parameters*/, NULL/*dir*/, nCmdShow);
3357
3358 if ((int)hinst <= 32) {
3359 display_error(hwnd, GetLastError());
3360 return FALSE;
3361 }
3362
3363 return TRUE;
3364 }
3365 #endif
3366
3367
3368 BOOL launch_entry(Entry* entry, HWND hwnd, UINT nCmdShow)
3369 {
3370 TCHAR cmd[MAX_PATH];
3371
3372 #ifdef _SHELL_FOLDERS
3373 if (entry->etype == ET_SHELL) {
3374 BOOL ret = TRUE;
3375
3376 SHELLEXECUTEINFO shexinfo;
3377
3378 shexinfo.cbSize = sizeof(SHELLEXECUTEINFO);
3379 shexinfo.fMask = SEE_MASK_IDLIST;
3380 shexinfo.hwnd = hwnd;
3381 shexinfo.lpVerb = NULL;
3382 shexinfo.lpFile = NULL;
3383 shexinfo.lpParameters = NULL;
3384 shexinfo.lpDirectory = NULL;
3385 shexinfo.nShow = nCmdShow;
3386 shexinfo.lpIDList = get_to_absolute_pidl(entry, hwnd);
3387
3388 if (!ShellExecuteEx(&shexinfo)) {
3389 display_error(hwnd, GetLastError());
3390 ret = FALSE;
3391 }
3392
3393 if (shexinfo.lpIDList != entry->pidl)
3394 (*Globals.iMalloc->lpVtbl->Free)(Globals.iMalloc, shexinfo.lpIDList);
3395
3396 return ret;
3397 }
3398 #endif
3399
3400 get_path(entry, cmd);
3401
3402 /* start program, open document... */
3403 return launch_file(hwnd, cmd, nCmdShow);
3404 }
3405
3406
3407 static void activate_entry(ChildWnd* child, Pane* pane, HWND hwnd)
3408 {
3409 Entry* entry = pane->cur;
3410
3411 if (!entry)
3412 return;
3413
3414 if (entry->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
3415 int scanned_old = entry->scanned;
3416
3417 if (!scanned_old)
3418 scan_entry(child, entry, hwnd);
3419
3420 #ifndef _NO_EXTENSIONS
3421 if (entry->data.cFileName[0]=='.' && entry->data.cFileName[1]=='\0')
3422 return;
3423 #endif
3424
3425 if (entry->data.cFileName[0]=='.' && entry->data.cFileName[1]=='.' && entry->data.cFileName[2]=='\0') {
3426 entry = child->left.cur->up;
3427 collapse_entry(&child->left, entry);
3428 goto focus_entry;
3429 } else if (entry->expanded)
3430 collapse_entry(pane, child->left.cur);
3431 else {
3432 expand_entry(child, child->left.cur);
3433
3434 if (!pane->treePane) focus_entry: {
3435 int idx = ListBox_FindItemData(child->left.hwnd, ListBox_GetCurSel(child->left.hwnd), entry);
3436 ListBox_SetCurSel(child->left.hwnd, idx);
3437 set_curdir(child, entry, hwnd);
3438 }
3439 }
3440
3441 if (!scanned_old) {
3442 calc_widths(pane, FALSE);
3443
3444 #ifndef _NO_EXTENSIONS
3445 set_header(pane);
3446 #endif
3447 }
3448 } else {
3449 launch_entry(entry, child->hwnd, SW_SHOWNORMAL);
3450 }
3451 }
3452
3453
3454 static BOOL pane_command(Pane* pane, UINT cmd)
3455 {
3456 switch(cmd) {
3457 case ID_VIEW_NAME:
3458 if (pane->visible_cols) {
3459 pane->visible_cols = 0;
3460 calc_widths(pane, TRUE);
3461 #ifndef _NO_EXTENSIONS
3462 set_header(pane);
3463 #endif
3464 InvalidateRect(pane->hwnd, 0, TRUE);
3465 CheckMenuItem(Globals.hMenuView, ID_VIEW_NAME, MF_BYCOMMAND|MF_CHECKED);
3466 CheckMenuItem(Globals.hMenuView, ID_VIEW_ALL_ATTRIBUTES, MF_BYCOMMAND);
3467 CheckMenuItem(Globals.hMenuView, ID_VIEW_SELECTED_ATTRIBUTES, MF_BYCOMMAND);
3468 }
3469 break;
3470
3471 case ID_VIEW_ALL_ATTRIBUTES:
3472 if (pane->visible_cols != COL_ALL) {
3473 pane->visible_cols = COL_ALL;
3474 calc_widths(pane, TRUE);
3475 #ifndef _NO_EXTENSIONS
3476 set_header(pane);
3477 #endif
3478 InvalidateRect(pane->hwnd, 0, TRUE);
3479 CheckMenuItem(Globals.hMenuView, ID_VIEW_NAME, MF_BYCOMMAND);
3480 CheckMenuItem(Globals.hMenuView, ID_VIEW_ALL_ATTRIBUTES, MF_BYCOMMAND|MF_CHECKED);
3481 CheckMenuItem(Globals.hMenuView, ID_VIEW_SELECTED_ATTRIBUTES, MF_BYCOMMAND);
3482 }
3483 break;
3484
3485 #ifndef _NO_EXTENSIONS
3486 case ID_PREFERRED_SIZES: {
3487 calc_widths(pane, TRUE);
3488 set_header(pane);
3489 InvalidateRect(pane->hwnd, 0, TRUE);
3490 break;}
3491 #endif
3492
3493 /* TODO: more command ids... */
3494
3495 default:
3496 return FALSE;
3497 }
3498
3499 return TRUE;
3500 }
3501
3502
3503 static IContextMenu2* s_pctxmenu2 = NULL;
3504
3505 #ifndef __MINGW32__ /* IContextMenu3 missing in MinGW (as of 6.2.2005) */
3506 static IContextMenu3* s_pctxmenu3 = NULL;
3507 #endif
3508
3509 static void CtxMenu_reset()
3510 {
3511 s_pctxmenu2 = NULL;
3512
3513 #ifndef __MINGW32__ /* IContextMenu3 missing in MinGW (as of 6.2.2005) */
3514 s_pctxmenu3 = NULL;
3515 #endif
3516 }
3517
3518 IContextMenu* CtxMenu_query_interfaces(IContextMenu* pcm1)
3519 {
3520 IContextMenu* pcm = NULL;
3521
3522 CtxMenu_reset();
3523
3524 #ifndef __MINGW32__ /* IContextMenu3 missing in MinGW (as of 6.2.2005) */
3525 if ((*pcm1->lpVtbl->QueryInterface)(pcm1, &IID_IContextMenu3, (void**)&pcm) == NOERROR)
3526 s_pctxmenu3 = (LPCONTEXTMENU3)pcm;
3527 else
3528 #endif
3529 if ((*pcm1->lpVtbl->QueryInterface)(pcm1, &IID_IContextMenu2, (void**)&pcm) == NOERROR)
3530 s_pctxmenu2 = (LPCONTEXTMENU2)pcm;
3531
3532 if (pcm) {
3533 (*pcm1->lpVtbl->Release)(pcm1);
3534 return pcm;
3535 } else
3536 return pcm1;
3537 }
3538
3539 static BOOL CtxMenu_HandleMenuMsg(UINT nmsg, WPARAM wparam, LPARAM lparam)
3540 {
3541 #ifndef __MINGW32__ /* IContextMenu3 missing in MinGW (as of 6.2.2005) */
3542 if (s_pctxmenu3) {
3543 if (SUCCEEDED((*s_pctxmenu3->lpVtbl->HandleMenuMsg)(s_pctxmenu3, nmsg, wparam, lparam)))
3544 return TRUE;
3545 }
3546 #endif
3547
3548 if (s_pctxmenu2)
3549 if (SUCCEEDED((*s_pctxmenu2->lpVtbl->HandleMenuMsg)(s_pctxmenu2, nmsg, wparam, lparam)))
3550 return TRUE;
3551
3552 return FALSE;
3553 }
3554
3555
3556 static HRESULT ShellFolderContextMenu(IShellFolder* shell_folder, HWND hwndParent, int cidl, LPCITEMIDLIST* apidl, int x, int y)
3557 {
3558 IContextMenu* pcm;
3559
3560 HRESULT hr = (*shell_folder->lpVtbl->GetUIObjectOf)(shell_folder, hwndParent, cidl, apidl, &IID_IContextMenu, NULL, (LPVOID*)&pcm);
3561 /* HRESULT hr = CDefFolderMenu_Create2(dir?dir->_pidl:DesktopFolder(), hwndParent, 1, &pidl, shell_folder, NULL, 0, NULL, &pcm); */
3562
3563 if (SUCCEEDED(hr)) {
3564 HMENU hmenu = CreatePopupMenu();
3565
3566 pcm = CtxMenu_query_interfaces(pcm);
3567
3568 if (hmenu) {
3569 hr = (*pcm->lpVtbl->QueryContextMenu)(pcm, hmenu, 0, FCIDM_SHVIEWFIRST, FCIDM_SHVIEWLAST, CMF_NORMAL);
3570
3571 if (SUCCEEDED(hr)) {
3572 UINT idCmd = TrackPopupMenu(hmenu, TPM_LEFTALIGN|TPM_RETURNCMD|TPM_RIGHTBUTTON, x, y, 0, hwndParent, NULL);
3573
3574 CtxMenu_reset();
3575
3576 if (idCmd) {
3577 CMINVOKECOMMANDINFO cmi;
3578
3579 cmi.cbSize = sizeof(CMINVOKECOMMANDINFO);
3580 cmi.fMask = 0;
3581 cmi.hwnd = hwndParent;
3582 cmi.lpVerb = (LPCSTR)(INT_PTR)(idCmd - FCIDM_SHVIEWFIRST);
3583 cmi.lpParameters = NULL;
3584 cmi.lpDirectory = NULL;
3585 cmi.nShow = SW_SHOWNORMAL;
3586 cmi.dwHotKey = 0;
3587 cmi.hIcon = 0;
3588
3589 hr = (*pcm->lpVtbl->InvokeCommand)(pcm, &cmi);
3590 }
3591 } else
3592 CtxMenu_reset();
3593 }
3594
3595 (*pcm->lpVtbl->Release)(pcm);
3596 }
3597
3598 return hr;
3599 }
3600
3601
3602 LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
3603 {
3604 static int last_split;
3605
3606 ChildWnd* child = (ChildWnd*) GetWindowLong(hwnd, GWL_USERDATA);
3607 ASSERT(child);
3608
3609 switch(nmsg) {
3610 case WM_DRAWITEM: {
3611 LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT)lparam;
3612 Entry* entry = (Entry*) dis->itemData;
3613
3614 if (dis->CtlID == IDW_TREE_LEFT)
3615 draw_item(&child->left, dis, entry, -1);
3616 else if (dis->CtlID == IDW_TREE_RIGHT)
3617 draw_item(&child->right, dis, entry, -1);
3618 else
3619 goto draw_menu_item;
3620
3621 return TRUE;}
3622
3623 case WM_CREATE:
3624 InitChildWindow(child);
3625 break;
3626
3627 case WM_NCDESTROY:
3628 free_child_window(child);
3629 SetWindowLong(hwnd, GWL_USERDATA, 0);
3630 break;
3631
3632 case WM_PAINT: {
3633 PAINTSTRUCT ps;
3634 HBRUSH lastBrush;
3635 RECT rt;
3636 GetClientRect(hwnd, &rt);
3637 BeginPaint(hwnd, &ps);
3638 rt.left = child->split_pos-SPLIT_WIDTH/2;
3639 rt.right = child->split_pos+SPLIT_WIDTH/2+1;
3640 lastBrush = SelectBrush(ps.hdc, (HBRUSH)GetStockObject(COLOR_SPLITBAR));
3641 Rectangle(ps.hdc, rt.left, rt.top-1, rt.right, rt.bottom+1);
3642 SelectObject(ps.hdc, lastBrush);
3643 #ifdef _NO_EXTENSIONS
3644 rt.top = rt.bottom - GetSystemMetrics(SM_CYHSCROLL);
3645 FillRect(ps.hdc, &rt, GetStockObject(BLACK_BRUSH));
3646 #endif
3647 EndPaint(hwnd, &ps);
3648 break;}
3649
3650 case WM_SETCURSOR:
3651 if (LOWORD(lparam) == HTCLIENT) {
3652 POINT pt;
3653 GetCursorPos(&pt);
3654 ScreenToClient(hwnd, &pt);
3655
3656 if (pt.x>=child->split_pos-SPLIT_WIDTH/2 && pt.x<child->split_pos+SPLIT_WIDTH/2+1) {
3657 SetCursor(LoadCursor(0, IDC_SIZEWE));
3658 return TRUE;
3659 }
3660 }
3661 goto def;
3662
3663 case WM_LBUTTONDOWN: {
3664 RECT rt;
3665 int x = GET_X_LPARAM(lparam);
3666
3667 GetClientRect(hwnd, &rt);
3668
3669 if (x>=child->split_pos-SPLIT_WIDTH/2 && x<child->split_pos+SPLIT_WIDTH/2+1) {
3670 last_split = child->split_pos;
3671 #ifdef _NO_EXTENSIONS
3672 draw_splitbar(hwnd, last_split);
3673 #endif
3674 SetCapture(hwnd);
3675 }
3676
3677 break;}
3678
3679 case WM_LBUTTONUP:
3680 if (GetCapture() == hwnd) {
3681 #ifdef _NO_EXTENSIONS
3682 RECT rt;
3683 int x = LOWORD(lparam);
3684 draw_splitbar(hwnd, last_split);
3685 last_split = -1;
3686 GetClientRect(hwnd, &rt);
3687 child->split_pos = x;
3688 resize_tree(child, rt.right, rt.bottom);
3689 #endif
3690 ReleaseCapture();
3691 }
3692 break;
3693
3694 #ifdef _NO_EXTENSIONS
3695 case WM_CAPTURECHANGED:
3696 if (GetCapture()==hwnd && last_split>=0)
3697 draw_splitbar(hwnd, last_split);
3698 break;
3699 #endif
3700
3701 case WM_KEYDOWN:
3702 if (wparam == VK_ESCAPE)
3703 if (GetCapture() == hwnd) {
3704 RECT rt;
3705 #ifdef _NO_EXTENSIONS
3706 draw_splitbar(hwnd, last_split);
3707 #else
3708 child->split_pos = last_split;
3709 #endif
3710 GetClientRect(hwnd, &rt);
3711 resize_tree(child, rt.right, rt.bottom);
3712 last_split = -1;
3713 ReleaseCapture();
3714 SetCursor(LoadCursor(0, IDC_ARROW));
3715 }
3716 break;
3717
3718 case WM_MOUSEMOVE:
3719 if (GetCapture() == hwnd) {
3720 RECT rt;
3721 int x = LOWORD(lparam);
3722
3723 #ifdef _NO_EXTENSIONS
3724 HDC hdc = GetDC(hwnd);
3725 GetClientRect(hwnd, &rt);
3726
3727 rt.left = last_split-SPLIT_WIDTH/2;
3728 rt.right = last_split+SPLIT_WIDTH/2+1;
3729 InvertRect(hdc, &rt);
3730
3731 last_split = x;
3732 rt.left = x-SPLIT_WIDTH/2;
3733 rt.right = x+SPLIT_WIDTH/2+1;
3734 InvertRect(hdc, &rt);
3735
3736 ReleaseDC(hwnd, hdc);
3737 #else
3738 GetClientRect(hwnd, &rt);
3739
3740 if (x>=0 && x<rt.right) {
3741 child->split_pos = x;
3742 resize_tree(child, rt.right, rt.bottom);
3743 rt.left = x-SPLIT_WIDTH/2;
3744 rt.right = x+SPLIT_WIDTH/2+1;
3745 InvalidateRect(hwnd, &rt, FALSE);
3746 UpdateWindow(child->left.hwnd);
3747 UpdateWindow(hwnd);
3748 UpdateWindow(child->right.hwnd);
3749 }
3750 #endif
3751 }
3752 break;
3753
3754 #ifndef _NO_EXTENSIONS
3755 case WM_GETMINMAXINFO:
3756 DefMDIChildProc(hwnd, nmsg, wparam, lparam);
3757
3758 {LPMINMAXINFO lpmmi = (LPMINMAXINFO)lparam;
3759
3760 lpmmi->ptMaxTrackSize.x <<= 1;/*2*GetSystemMetrics(SM_CXSCREEN) / SM_CXVIRTUALSCREEN */
3761 lpmmi->ptMaxTrackSize.y <<= 1;/*2*GetSystemMetrics(SM_CYSCREEN) / SM_CYVIRTUALSCREEN */
3762 break;}
3763 #endif /* _NO_EXTENSIONS */
3764
3765 case WM_SETFOCUS:
3766 if (SetCurrentDirectory(child->path))
3767 set_space_status();
3768 SetFocus(child->focus_pane? child->right.hwnd: child->left.hwnd);
3769 break;
3770
3771 case WM_DISPATCH_COMMAND: {
3772 Pane* pane = GetFocus()==child->left.hwnd? &child->left: &child->right;
3773
3774 switch(LOWORD(wparam)) {
3775 case ID_WINDOW_NEW: {
3776 ChildWnd* new_child = alloc_child_window(child->path, NULL, hwnd);
3777
3778 if (!create_child_window(new_child))
3779 free(new_child);
3780
3781 break;}
3782
3783 case ID_REFRESH:
3784 scan_entry(child, pane->cur, hwnd);
3785 break;
3786
3787 case ID_ACTIVATE:
3788 activate_entry(child, pane, hwnd);
3789 break;
3790
3791 case ID_FILE_MOVE: {
3792 TCHAR new_name[BUFFER_LEN], old_name[BUFFER_LEN];
3793 int len, ret;
3794
3795 get_path(pane->cur, new_name);
3796
3797 ret = DialogBoxParam(Globals.hInstance, MAKEINTRESOURCE(IDD_SELECT_DESTINATION), hwnd, DestinationDlgProc, (LPARAM)new_name);
3798 if (ret != IDOK)
3799 break;
3800
3801 if (new_name[0]!='/' && new_name[1]!=':') {
3802 get_path(pane->cur->up, old_name);
3803 len = lstrlen(old_name);
3804
3805 if (old_name[len-1]!='\\' && old_name[len-1]!='/')
3806 old_name[len++] = '/';
3807
3808 lstrcpy(old_name+len, new_name);
3809 lstrcpy(new_name, old_name);
3810 }
3811
3812 get_path(pane->cur, old_name);
3813
3814 if (MoveFileEx(old_name, new_name, MOVEFILE_COPY_ALLOWED)) {
3815 if (pane->treePane) {
3816 pane->root->scanned = FALSE;
3817 pane->cur = pane->root;
3818 activate_entry(child, pane, hwnd);
3819 }
3820 else
3821 scan_entry(child, pane->root, hwnd);
3822 }
3823 else
3824 display_error(hwnd, GetLastError());
3825 break;}
3826
3827 default:
3828 return pane_command(pane, LOWORD(wparam));
3829 }
3830
3831 return TRUE;}
3832
3833 case WM_COMMAND: {
3834 Pane* pane = GetFocus()==child->left.hwnd? &child->left: &child->right;
3835
3836 switch(HIWORD(wparam)) {
3837 case LBN_SELCHANGE: {
3838 int idx = ListBox_GetCurSel(pane->hwnd);
3839 Entry* entry = (Entry*) ListBox_GetItemData(pane->hwnd, idx);
3840
3841 if (pane == &child->left)
3842 set_curdir(child, entry, hwnd);
3843 else
3844 pane->cur = entry;
3845 break;}
3846
3847 case LBN_DBLCLK:
3848 activate_entry(child, pane, hwnd);
3849 break;
3850 }
3851 break;}
3852
3853 #ifndef _NO_EXTENSIONS
3854 case WM_NOTIFY: {
3855 NMHDR* pnmh = (NMHDR*) lparam;
3856 return pane_notify(pnmh->idFrom==IDW_HEADER_LEFT? &child->left: &child->right, pnmh);}
3857 #endif
3858
3859 #ifdef _SHELL_FOLDERS
3860 case WM_CONTEXTMENU: {
3861 POINT pt, pt_clnt;
3862 Pane* pane;
3863 int idx;
3864
3865 /* first select the current item in the listbox */
3866 HWND hpanel = (HWND) wparam;
3867 pt_clnt.x = pt.x = (short)LOWORD(lparam);
3868 pt_clnt.y = pt.y = (short)HIWORD(lparam);
3869 ScreenToClient(hpanel, &pt_clnt);
3870 SendMessage(hpanel, WM_LBUTTONDOWN, 0, MAKELONG(pt_clnt.x, pt_clnt.y));
3871 SendMessage(hpanel, WM_LBUTTONUP, 0, MAKELONG(pt_clnt.x, pt_clnt.y));
3872
3873 /* now create the popup menu using shell namespace and IContextMenu */
3874 pane = GetFocus()==child->left.hwnd? &child->left: &child->right;
3875 idx = ListBox_GetCurSel(pane->hwnd);
3876
3877 if (idx != -1) {
3878 HRESULT hr;
3879 Entry* entry = (Entry*) ListBox_GetItemData(pane->hwnd, idx);
3880
3881 LPITEMIDLIST pidl_abs = get_to_absolute_pidl(entry, hwnd);
3882
3883 if (pidl_abs) {
3884 IShellFolder* parentFolder;
3885 LPCITEMIDLIST pidlLast;
3886
3887 /* get and use the parent folder to display correct context menu in all cases */
3888 if (SUCCEEDED(SHBindToParent(pidl_abs, &IID_IShellFolder, (LPVOID*)&parentFolder, &pidlLast))) {
3889 hr = ShellFolderContextMenu(parentFolder, hwnd, 1, &pidlLast, pt.x, pt.y);
3890
3891 (*parentFolder->lpVtbl->Release)(parentFolder);
3892 }
3893
3894 (*Globals.iMalloc->lpVtbl->Free)(Globals.iMalloc, pidl_abs);
3895 }
3896 }
3897 break;}
3898 #endif
3899
3900 case WM_MEASUREITEM:
3901 draw_menu_item:
3902 if (!wparam) /* Is the message menu-related? */
3903 if (CtxMenu_HandleMenuMsg(nmsg, wparam, lparam))
3904 return TRUE;
3905
3906 break;
3907
3908 case WM_INITMENUPOPUP:
3909 if (CtxMenu_HandleMenuMsg(nmsg, wparam, lparam))
3910 return 0;
3911
3912 break;
3913
3914 #ifndef __MINGW32__ /* IContextMenu3 missing in MinGW (as of 6.2.2005) */
3915 case WM_MENUCHAR: /* only supported by IContextMenu3 */
3916 if (s_pctxmenu3) {
3917 LRESULT lResult = 0;
3918
3919 (*s_pctxmenu3->lpVtbl->HandleMenuMsg2)(s_pctxmenu3, nmsg, wparam, lparam, &lResult);
3920
3921 return lResult;
3922 }
3923
3924 break;
3925 #endif
3926
3927 case WM_SIZE:
3928 if (wparam != SIZE_MINIMIZED)
3929 resize_tree(child, LOWORD(lparam), HIWORD(lparam));
3930 /* fall through */
3931
3932 default: def:
3933 return DefMDIChildProc(hwnd, nmsg, wparam, lparam);
3934 }
3935
3936 return 0;
3937 }
3938
3939
3940 LRESULT CALLBACK TreeWndProc(HWND hwnd, UINT nmsg, WPARAM wparam, LPARAM lparam)
3941 {
3942 ChildWnd* child = (ChildWnd*) GetWindowLong(GetParent(hwnd), GWL_USERDATA);
3943 Pane* pane = (Pane*) GetWindowLong(hwnd, GWL_USERDATA);
3944 ASSERT(child);
3945
3946 switch(nmsg) {
3947 #ifndef _NO_EXTENSIONS
3948 case WM_HSCROLL:
3949 set_header(pane);
3950 break;
3951 #endif
3952
3953 case WM_SETFOCUS:
3954 child->focus_pane = pane==&child->right? 1: 0;
3955 ListBox_SetSel(hwnd, TRUE, 1);
3956 /*TODO: check menu items */
3957 break;
3958
3959 case WM_KEYDOWN:
3960 if (wparam == VK_TAB) {
3961 /*TODO: SetFocus(Globals.hdrivebar) */
3962 SetFocus(child->focus_pane? child->left.hwnd: child->right.hwnd);
3963 }
3964 }
3965
3966 return CallWindowProc(g_orgTreeWndProc, hwnd, nmsg, wparam, lparam);
3967 }
3968
3969
3970 static void InitInstance(HINSTANCE hinstance)
3971 {
3972 const static TCHAR sFont[] = {'M','i','c','r','o','s','o','f','t',' ','S','a','n','s',' ','S','e','r','i','f','\0'};
3973
3974 WNDCLASSEX wcFrame;
3975 WNDCLASS wcChild;
3976 ATOM hChildClass;
3977 int col;
3978
3979 INITCOMMONCONTROLSEX icc = {
3980 sizeof(INITCOMMONCONTROLSEX),
3981 ICC_BAR_CLASSES
3982 };
3983
3984 HDC hdc = GetDC(0);
3985
3986 setlocale(LC_COLLATE, ""); /* set collating rules to local settings for compareName */
3987
3988 InitCommonControlsEx(&icc);
3989
3990
3991 /* register frame window class */
3992
3993 wcFrame.cbSize = sizeof(WNDCLASSEX);
3994 wcFrame.style = 0;
3995 wcFrame.lpfnWndProc = FrameWndProc;
3996 wcFrame.cbClsExtra = 0;
3997 wcFrame.cbWndExtra = 0;
3998 wcFrame.hInstance = hinstance;
3999 wcFrame.hIcon = LoadIcon(hinstance, MAKEINTRESOURCE(IDI_WINEFILE));
4000 wcFrame.hCursor = LoadCursor(0, IDC_ARROW);
4001 wcFrame.hbrBackground = 0;
4002 wcFrame.lpszMenuName = 0;
4003 wcFrame.lpszClassName = sWINEFILEFRAME;
4004 wcFrame.hIconSm = (HICON)LoadImage(hinstance,
4005 MAKEINTRESOURCE(IDI_WINEFILE),
4006 IMAGE_ICON,
4007 GetSystemMetrics(SM_CXSMICON),
4008 GetSystemMetrics(SM_CYSMICON),
4009 LR_SHARED);
4010
4011 Globals.hframeClass = RegisterClassEx(&wcFrame);
4012
4013
4014 /* register tree windows class */
4015
4016 wcChild.style = CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW;
4017 wcChild.lpfnWndProc = ChildWndProc;
4018 wcChild.cbClsExtra = 0;
4019 wcChild.cbWndExtra = 0;
4020 wcChild.hInstance = hinstance;
4021 wcChild.hIcon = 0;
4022 wcChild.hCursor = LoadCursor(0, IDC_ARROW);
4023 wcChild.hbrBackground = 0;
4024 wcChild.lpszMenuName = 0;
4025 wcChild.lpszClassName = sWINEFILETREE;
4026
4027 hChildClass = RegisterClass(&wcChild);
4028
4029
4030 Globals.haccel = LoadAccelerators(hinstance, MAKEINTRESOURCE(IDA_WINEFILE));
4031
4032 Globals.hfont = CreateFont(-MulDiv(8,GetDeviceCaps(hdc,LOGPIXELSY),72), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, sFont);
4033
4034 ReleaseDC(0, hdc);
4035
4036 Globals.hInstance = hinstance;
4037
4038 #ifdef _SHELL_FOLDERS
4039 CoInitialize(NULL);
4040 CoGetMalloc(MEMCTX_TASK, &Globals.iMalloc);
4041 SHGetDesktopFolder(&Globals.iDesktop);
4042 #ifdef __WINE__
4043 Globals.cfStrFName = RegisterClipboardFormatA(CFSTR_FILENAME);
4044 #else
4045 Globals.cfStrFName = RegisterClipboardFormat(CFSTR_FILENAME);
4046 #endif
4047 #endif
4048
4049 /* load column strings */
4050 col = 0;
4051
4052 load_string(g_pos_names[col++], IDS_COL_NAME);
4053 load_string(g_pos_names[col++], IDS_COL_SIZE);
4054 load_string(g_pos_names[col++], IDS_COL_CDATE);
4055 #ifndef _NO_EXTENSIONS
4056 load_string(g_pos_names[col++], IDS_COL_ADATE);
4057 load_string(g_pos_names[col++], IDS_COL_MDATE);
4058 load_string(g_pos_names[col++], IDS_COL_IDX);
4059 load_string(g_pos_names[col++], IDS_COL_LINKS);
4060 #endif
4061 load_string(g_pos_names[col++], IDS_COL_ATTR);
4062 #ifndef _NO_EXTENSIONS
4063 load_string(g_pos_names[col++], IDS_COL_SEC);
4064 #endif
4065 }
4066
4067
4068 void show_frame(HWND hwndParent, int cmdshow)
4069 {
4070 const static TCHAR sMDICLIENT[] = {'M','D','I','C','L','I','E','N','T','\0'};
4071
4072 TCHAR path[MAX_PATH], b1[BUFFER_LEN];
4073 ChildWnd* child;
4074 HMENU hMenuFrame, hMenuWindow;
4075
4076 CLIENTCREATESTRUCT ccs;
4077
4078 if (Globals.hMainWnd)
4079 return;
4080
4081 Globals.hwndParent = hwndParent;
4082
4083 hMenuFrame = LoadMenu(Globals.hInstance, MAKEINTRESOURCE(IDM_WINEFILE));
4084 hMenuWindow = GetSubMenu(hMenuFrame, GetMenuItemCount(hMenuFrame)-2);
4085
4086 Globals.hMenuFrame = hMenuFrame;
4087 Globals.hMenuView = GetSubMenu(hMenuFrame, 3);
4088 Globals.hMenuOptions = GetSubMenu(hMenuFrame, 4);
4089
4090 ccs.hWindowMenu = hMenuWindow;
4091 ccs.idFirstChild = IDW_FIRST_CHILD;
4092
4093
4094 /* create main window */
4095 Globals.hMainWnd = CreateWindowEx(0, (LPCTSTR)(int)Globals.hframeClass, RS(b1,IDS_WINE_FILE), WS_OVERLAPPEDWINDOW,
4096 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4097 hwndParent, Globals.hMenuFrame, Globals.hInstance, 0/*lpParam*/);
4098
4099
4100 Globals.hmdiclient = CreateWindowEx(0, sMDICLIENT, NULL,
4101 WS_CHILD|WS_CLIPCHILDREN|WS_VSCROLL|WS_HSCROLL|WS_VISIBLE|WS_BORDER,
4102 0, 0, 0, 0,
4103 Globals.hMainWnd, 0, Globals.hInstance, &ccs);
4104
4105
4106 {
4107 TBBUTTON drivebarBtn = {0, 0, TBSTATE_ENABLED, BTNS_SEP, {0, 0}, 0, 0};
4108 int btn = 1;
4109 PTSTR p;
4110
4111 Globals.hdrivebar = CreateToolbarEx(Globals.hMainWnd, WS_CHILD|WS_VISIBLE|CCS_NOMOVEY|TBSTYLE_LIST,
4112 IDW_DRIVEBAR, 2, Globals.hInstance, IDB_DRIVEBAR, &drivebarBtn,
4113 1, 16, 13, 16, 13, sizeof(TBBUTTON));
4114 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_DRIVE_BAR, MF_BYCOMMAND|MF_CHECKED);
4115
4116 GetLogicalDriveStrings(BUFFER_LEN, Globals.drives);
4117
4118 drivebarBtn.fsStyle = BTNS_BUTTON;
4119
4120 #ifndef _NO_EXTENSIONS
4121 #ifdef __WINE__
4122 /* insert unix file system button */
4123 b1[0] = '/';
4124 b1[1] = '\0';
4125 b1[2] = '\0';
4126 SendMessage(Globals.hdrivebar, TB_ADDSTRING, 0, (LPARAM)b1);
4127
4128 drivebarBtn.idCommand = ID_DRIVE_UNIX_FS;
4129 SendMessage(Globals.hdrivebar, TB_INSERTBUTTON, btn++, (LPARAM)&drivebarBtn);
4130 drivebarBtn.iString++;
4131 #endif
4132 #ifdef _SHELL_FOLDERS
4133 /* insert shell namespace button */
4134 load_string(b1, IDS_SHELL);
4135 b1[lstrlen(b1)+1] = '\0';
4136 SendMessage(Globals.hdrivebar, TB_ADDSTRING, 0, (LPARAM)b1);
4137
4138 drivebarBtn.idCommand = ID_DRIVE_SHELL_NS;
4139 SendMessage(Globals.hdrivebar, TB_INSERTBUTTON, btn++, (LPARAM)&drivebarBtn);
4140 drivebarBtn.iString++;
4141 #endif
4142
4143 /* register windows drive root strings */
4144 SendMessage(Globals.hdrivebar, TB_ADDSTRING, 0, (LPARAM)Globals.drives);
4145 #endif
4146
4147 drivebarBtn.idCommand = ID_DRIVE_FIRST;
4148
4149 for(p=Globals.drives; *p; ) {
4150 #ifdef _NO_EXTENSIONS
4151 /* insert drive letter */
4152 TCHAR b[3] = {tolower(*p)};
4153 SendMessage(Globals.hdrivebar, TB_ADDSTRING, 0, (LPARAM)b);
4154 #endif
4155 switch(GetDriveType(p)) {
4156 case DRIVE_REMOVABLE: drivebarBtn.iBitmap = 1; break;
4157 case DRIVE_CDROM: drivebarBtn.iBitmap = 3; break;
4158 case DRIVE_REMOTE: drivebarBtn.iBitmap = 4; break;
4159 case DRIVE_RAMDISK: drivebarBtn.iBitmap = 5; break;
4160 default:/*DRIVE_FIXED*/ drivebarBtn.iBitmap = 2;
4161 }
4162
4163 SendMessage(Globals.hdrivebar, TB_INSERTBUTTON, btn++, (LPARAM)&drivebarBtn);
4164 drivebarBtn.idCommand++;
4165 drivebarBtn.iString++;
4166
4167 while(*p++);
4168 }
4169 }
4170
4171 {
4172 TBBUTTON toolbarBtns[] = {
4173 {0, 0, 0, BTNS_SEP, {0, 0}, 0, 0},
4174 {0, ID_WINDOW_NEW, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4175 {1, ID_WINDOW_CASCADE, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4176 {2, ID_WINDOW_TILE_HORZ, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4177 {3, ID_WINDOW_TILE_VERT, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4178 /*TODO
4179 {4, ID_... , TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4180 {5, ID_... , TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
4181 */ };
4182
4183 Globals.htoolbar = CreateToolbarEx(Globals.hMainWnd, WS_CHILD|WS_VISIBLE,
4184 IDW_TOOLBAR, 2, Globals.hInstance, IDB_TOOLBAR, toolbarBtns,
4185 sizeof(toolbarBtns)/sizeof(TBBUTTON), 16, 15, 16, 15, sizeof(TBBUTTON));
4186 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_TOOL_BAR, MF_BYCOMMAND|MF_CHECKED);
4187 }
4188
4189 Globals.hstatusbar = CreateStatusWindow(WS_CHILD|WS_VISIBLE, 0, Globals.hMainWnd, IDW_STATUSBAR);
4190 CheckMenuItem(Globals.hMenuOptions, ID_VIEW_STATUSBAR, MF_BYCOMMAND|MF_CHECKED);
4191
4192 /* CreateStatusWindow does not accept WS_BORDER
4193 Globals.hstatusbar = CreateWindowEx(WS_EX_NOPARENTNOTIFY, STATUSCLASSNAME, 0,
4194 WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_BORDER|CCS_NODIVIDER, 0,0,0,0,
4195 Globals.hMainWnd, (HMENU)IDW_STATUSBAR, hinstance, 0);*/
4196
4197 /*TODO: read paths and window placements from registry */
4198 GetCurrentDirectory(MAX_PATH, path);
4199
4200 ShowWindow(Globals.hMainWnd, cmdshow);
4201
4202 //#if defined(_SHELL_FOLDERS) && !defined(__WINE__)
4203 // // Shell Namespace as default:
4204 // child = alloc_child_window(path, get_path_pidl(path,Globals.hMainWnd), Globals.hMainWnd);
4205 //#else
4206 child = alloc_child_window(path, NULL, Globals.hMainWnd);
4207 //#endif
4208
4209 child->pos.showCmd = SW_SHOWMAXIMIZED;
4210 child->pos.rcNormalPosition.left = 0;
4211 child->pos.rcNormalPosition.top = 0;
4212 child->pos.rcNormalPosition.right = 320;
4213 child->pos.rcNormalPosition.bottom = 280;
4214
4215 if (!create_child_window(child))
4216 free(child);
4217
4218 SetWindowPlacement(child->hwnd, &child->pos);
4219
4220 Globals.himl = ImageList_LoadBitmap(Globals.hInstance, MAKEINTRESOURCE(IDB_IMAGES), 16, 0, RGB(0,255,0));
4221
4222 Globals.prescan_node = FALSE;
4223
4224 UpdateWindow(Globals.hMainWnd);
4225 }
4226
4227 void ExitInstance()
4228 {
4229 #ifdef _SHELL_FOLDERS
4230 (*Globals.iDesktop->lpVtbl->Release)(Globals.iDesktop);
4231 (*Globals.iMalloc->lpVtbl->Release)(Globals.iMalloc);
4232 CoUninitialize();
4233 #endif
4234
4235 ImageList_Destroy(Globals.himl);
4236 }
4237
4238
4239 /* search for already running win[e]files */
4240
4241 static int g_foundPrevInstance = 0;
4242
4243 static BOOL CALLBACK EnumWndProc(HWND hwnd, LPARAM lparam)
4244 {
4245 TCHAR cls[128];
4246
4247 GetClassName(hwnd, cls, 128);
4248
4249 if (!lstrcmp(cls, (LPCTSTR)lparam)) {
4250 g_foundPrevInstance++;
4251 return FALSE;
4252 }
4253
4254 return TRUE;
4255 }
4256
4257 /* search for window of given class name to allow only one running instance */
4258 int find_window_class(LPCTSTR classname)
4259 {
4260 EnumWindows(EnumWndProc, (LPARAM)classname);
4261
4262 if (g_foundPrevInstance)
4263 return 1;
4264
4265 return 0;
4266 }
4267
4268
4269 int winefile_main(HINSTANCE hinstance, HWND hwndParent, int cmdshow)
4270 {
4271 MSG msg;
4272
4273 InitInstance(hinstance);
4274
4275 if (cmdshow == SW_SHOWNORMAL)
4276 /*TODO: read window placement from registry */
4277 cmdshow = SW_MAXIMIZE;
4278
4279 show_frame(hwndParent, cmdshow);
4280
4281 while(GetMessage(&msg, 0, 0, 0)) {
4282 if (Globals.hmdiclient && TranslateMDISysAccel(Globals.hmdiclient, &msg))
4283 continue;
4284
4285 if (Globals.hMainWnd && TranslateAccelerator(Globals.hMainWnd, Globals.haccel, &msg))
4286 continue;
4287
4288 TranslateMessage(&msg);
4289 DispatchMessage(&msg);
4290 }
4291
4292 ExitInstance();
4293
4294 return msg.wParam;
4295 }
4296
4297
4298 int APIENTRY WinMain(HINSTANCE hinstance,
4299 HINSTANCE previnstance,
4300 LPSTR cmdline,
4301 int cmdshow)
4302 {
4303 #ifdef _NO_EXTENSIONS
4304 if (find_window_class(sWINEFILEFRAME))
4305 return 1;
4306 #endif
4307
4308 winefile_main(hinstance, 0, cmdshow);
4309
4310 return 0;
4311 }